decisions 0.2.0, part 1: the Gemini and Venn deciders Implementation Plan
plans/2026-11-24-decisions-0.2.0-deciders-plan.md
# decisions 0.2.0, part 1: the Gemini and Venn deciders Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** `decider_gemini()` and `decider_venn()` work with `decide()` and setups, tested against real responses.
**Architecture:** Two new providers in the existing plumbing (`provider_key()`, `provider_name()`, `decider_url()`, `new_request()`, `input_types`), two new request builders (`build_gemini()`, `build_venn()`) in `R/formats.R`, and `parse_systemone()` for both. Fixtures come first, from real calls saved by `data-raw/probe_providers.R`; the builders must reproduce the saved request bodies.
**Tech Stack:** R, S7, httr2, purrr, testthat 3 (fixtures in `tests/testthat/fixtures`).
**Spec:** `replication_materials/plans/2026-11-24-decisions-0.2.0-design.md` (approved, D313), sections 1, 2 and 5. Vignettes and the release are later plans.
Paths: `decisions/` is `C:/Users/megahefm/Dropbox/Miami/Code/claude/decisions`; `study/` is `C:/Users/megahefm/Dropbox/Miami/Code/claude/fine_tuned_jev`.
## Global Constraints
- Gemini: base URL `https://generativelanguage.googleapis.com`, path `v1beta/decisions`, default model `gemini-decision-1`, key `GEMINI_API_KEY` in the header `x-goog-api-key` (redacted), provider name "Google Gemini".
- Venn: default base URL `http://127.0.0.1:8000`, path `api/alpha/decisions`, default model `venn-1`, no key, provider name "Venn"; image requests carry `X-Venn-Experimental: media`.
- "No change to the request bodies of the existing providers. The saved fixtures of 0.1.x must still pass."
- Paid calls: only Task 1 (Gemini, budget row of D301); each one logged in `study/replication_materials/calls/gemini-calls.csv`. Venn calls are local and free.
- No network call in tests (live tests only behind `DECISIONS_LIVE_TESTS` / `DECISIONS_LIVE_LOCAL`).
- Never print or commit a key. The probe reads `GEMINI_API_KEY` with `readRenviron("study/.env")`.
- Work on the branch `deciders-gemini-venn` of decisions, from `main` after the merge of 0.1.1. Commits end with the two trailer lines of the session.
- Style: httr2, purrr, native pipe, `pkg::fun`, roxygen markdown; internal functions get `@noRd`.
## Review Focus
1. A Gemini choice whose answer has no description (`NA`) must send `""`, not the answer name and not `null`. Task 2 tests it.
2. A Gemini question name with a dot (`"is.refund"`, legal in R) must stop before any request with a message that names the rule, and send nothing. Task 2 tests it.
3. A Gemini request with references and a text: the text block comes after the inspected image, and every text block ends with `"\n"`. Task 2 tests the order and the newlines.
4. Venn with a text and an image in the same call, or with references, must stop before any request; Venn would ignore the text without an error. Task 3 tests both.
5. Venn reports `input_tokens: 0` and `cost: 0`; the output of `decide()` must show `NA`, not 0, so that a summary of cost does not read "free" as "measured". Task 3 tests it.
---
### Task 1: Real Gemini responses as fixtures
**Files:**
- Modify: `decisions/data-raw/probe_providers.R` (add `probe_gemini()`, add `gemini` to the run switch)
- Modify: `decisions/data-raw/log_call.R` (input tokens from `usage$total_input_tokens` when `usage$input_tokens` is missing; price `gemini = 0.10`)
- Create: `decisions/tests/testthat/fixtures/gemini-{noul,choice2,choice3,score3,combined,image,noul-described}.json` and their `-request.json`, and `gemini-error.json` / `gemini-error-request.json`
**Interfaces:**
- Produces: the fixtures above. The text cases use `systemone_questions()` of the probe (the Gemini question JSON is the same for these cases). `noul-described` sends `criteria = {true_description: "The message asks for money back.", false_description: "The message does not ask for money back."}`. `image` sends the question `quality` (choice, the `normal` / `defective` descriptions of the Clef probe, instructions "Inspect `inspection_image` for a surface defect. Select exactly one category.") and the input blocks, in this order: text "Reference images of the answer `normal`:\n", image `reference-normal`, text "Reference images of the answer `defective`:\n", image `reference-defect`, text "`inspection_image`, the image to inspect:\n", image `streak`. An image block is `{type: "image", mime_type: "image/jpeg", data: <base64 of the file bytes, no line breaks>}`. `bad-type` sends type `"predicate"` (expected HTTP 400).
- [ ] **Step 1: Write `probe_gemini(model = "gemini-decision-1")`**
It posts to `https://generativelanguage.googleapis.com/v1beta/decisions` with `send(..., key = NULL, headers = list("x-goog-api-key" = key), service = "gemini")`. The purpose label of `log_call()` is `decisions-0.2.0-gemini-<case>`.
- [ ] **Step 2: Run the probe**
Run (in `decisions/`): `Rscript -e 'readRenviron("C:/Users/megahefm/Dropbox/Miami/Code/claude/fine_tuned_jev/.env"); source("data-raw/probe_providers.R"); probe_gemini()'`
Expected: 8 lines `gemini <case>: HTTP 200` and one `gemini bad-type: HTTP 400`; 9 new rows in `gemini-calls.csv`, about USD 0.001. No key in any fixture (`grep -l "AIza" tests/testthat/fixtures/gemini-*` prints nothing).
- [ ] **Step 3: Commit** the probe, the logger and the fixtures. Commit the call-log rows in the study (record-only commit on `main`, build check first).
### Task 2: `decider_gemini()` and `build_gemini()`
**Files:**
- Modify: `decisions/R/deciders.R` (`decider_gemini()`, `provider_key()`, `provider_name()`, `decider_url()`)
- Modify: `decisions/R/decide.R` (`new_request()`: the Gemini header; `decide()`: call `check_gemini_questions()` after `check_input_type()`)
- Modify: `decisions/R/formats.R` (`build_body()` and `parse_answers()` switches; `build_gemini()`, `gemini_question()`, `check_gemini_questions()`; input tokens from `usage$total_input_tokens`)
- Modify: `decisions/R/input-types.R` (row `"gemini"`, `"^gemini-decision-"`, `"text+image"`, 2026-11-24)
- Modify: `decisions/inst/skills/decisions/SKILL.md` and `decisions/_pkgdown.yml` (name `decider_gemini()`)
- Test: `decisions/tests/testthat/test-formats.R`, `test-deciders.R`, `test-decide.R`, `test-live.R`; `helper-requests.R` if `probe_questions()` needs the provider `gemini`
**Interfaces:**
- Produces: `decider_gemini(model = NULL, credentials = NULL, input = NULL)` (exported, `@family deciders`); `build_gemini(model, questions, text, image, references)` and `gemini_question(question)` (`@noRd`, list for `req_body_json()`); `check_gemini_questions(questions)` (`@noRd`, returns `invisible()` or stops with `cli::cli_abort()`).
- [ ] **Step 1: Write the failing tests**
- `test-formats.R`: add `list("gemini", "gemini-decision-1", text_cases)` to the saved-text-requests test, and `gemini = "gemini"` to `provider_format`. New test "build_body() makes the saved Gemini image request": `build_body("gemini", "gemini-decision-1", list(quality = <the question of the probe>), image = img$query, references = references)` equals `fixture("gemini-image-request")` (`expect_same_body()`). New test "a Gemini noul with descriptions sends true_description and false_description": the body of `question_noul("Does the message ask for a refund?", true = "The message asks for money back.", false = "The message does not ask for money back.")` equals `fixture("gemini-noul-described-request")`. New test "a Gemini choice sends an empty description for an answer without one": `body$questions$team$criteria` is `list(billing = "Charges.", shipping = "")` for `question_choice("Q?", c(billing = "Charges.", shipping = NA))`. New test "a Gemini text with an image comes after the image, and each text block ends with a newline": with `text = "Serial 12."`, the last block is `list(type = "text", text = "Serial 12.\n")` and every block of type text ends with `"\n"`.
- Parse tests: each saved Gemini response gives the answers that `parse_systemone()` gives, and `input_tokens` equals `fixture("gemini-noul")$usage$total_input_tokens`; `cost` is `NA`.
- `test-deciders.R`: `decider_gemini()` prints the message `model = "gemini-decision-1"`; `decider_url(decider_gemini("gemini-decision-1"))` is `"https://generativelanguage.googleapis.com/v1beta/decisions"`; with `GEMINI_API_KEY` empty (`withr::local_envvar()`), `decide()` stops with a message that names `GEMINI_API_KEY`.
- `test-decide.R`: with `record_requests()`, a Gemini request has the header `x-goog-api-key` and no `Authorization` header; `decide(gemini, list(is.refund = refund), text = "x")` stops with a message that contains `[A-Za-z0-9_-]` and sends no request; a choice with 256 answers stops ("2 to 255"); an answer named `" billing"` stops ("space").
- `test-live.R`: one Gemini call behind `skip_if_not_live("GEMINI_API_KEY")`, as the OpenAI one.
- [ ] **Step 2: Run them and see them fail**
Run: `Rscript -e 'devtools::test(filter = "formats|deciders|decide")'`
Expected: the new tests FAIL (`decider_gemini` not found, unknown format "gemini").
- [ ] **Step 3: Implement**, as the Interfaces block and the spec (section 1) say. `gemini_question()`: noul gets `criteria` only when an answer has a description, with only the keys that have one; choice maps each answer to its description, or `""`; score as `systemone_question()`. `build_gemini()`: `input` is `text` alone when there is no image and no reference, else the blocks of Task 1 in that order, then the text (with `"\n"`); `purrr::compact()` drops empty fields. `check_gemini_questions()`: the four rules of the spec, each with its own message. The roxygen of `decider_gemini()` says that the API is beta (`/v1beta/`), that Gemini clamps a noul to 0.01 to 0.99, and that `.cost` is `NA`. Add `decider_gemini()` to the skill and to the reference index.
- [ ] **Step 4: Run the full suite**
Run: `Rscript -e 'devtools::test(reporter = "check")'`
Expected: `[ FAIL 0 | WARN 0 | SKIP 5 | ...]` (one more live skip).
- [ ] **Step 5: Commit**
### Task 3: `decider_venn()` and `build_venn()`
**Files:**
- Modify: `decisions/data-raw/probe_providers.R` (`probe_venn(base = "http://127.0.0.1:8000")`)
- Create: `decisions/tests/testthat/fixtures/venn-{noul,choice2,choice3,score3,combined,image}.json`, their `-request.json`, and `venn-error*.json`
- Modify: `decisions/R/deciders.R`, `decisions/R/decide.R`, `decisions/R/formats.R`, `decisions/R/input-types.R` (row `"venn"`, `"^venn-"`, `"text+image"`, 2026-11-24), `SKILL.md`, `_pkgdown.yml`
- Test: `test-formats.R`, `test-deciders.R`, `test-decide.R`, `test-live.R`
**Interfaces:**
- Produces: `decider_venn(model = "venn-1", base_url = "http://127.0.0.1:8000", input = NULL)` (exported, `@family deciders`); `build_venn(model, questions, text, image, references)` (`@noRd`); `check_venn_inputs(text, image, references)` (`@noRd`).
- [ ] **Step 1: Make the fixtures from the local server**
Start Venn with `C:\Users\megahefm\venn\start_venn.cmd` and wait for `"reader":"ready"` on `GET /health`. `probe_venn()` calls `probe_systemone_local("venn", paste0(base, "/api/alpha/decisions"), "venn-1", state = list(text = text))` and posts one image case: the question `quality` of Task 1, `state = list(image = <base64 of streak>)`, header `X-Venn-Experimental: media`. Run it; expected: `venn <case>: HTTP 200` for the six cases and HTTP 422 for `bad-type`. Stop the server.
- [ ] **Step 2: Write the failing tests**
- `test-formats.R`: add `list("venn", "venn-1", text_cases)` and `venn = "venn"`; the state conversion of that test reads `expected$state$message %||% expected$state$text`. New test "build_body() makes the saved Venn image request" (`state = list(image = base64)`, no `assets`). New test "Venn reports no tokens and no cost": `parse_answers("venn", fixture("venn-noul"), list(refund = refund))` has `input_tokens` `NA` and `cost` `NA`.
- `test-decide.R`: an image request to Venn has the header `X-Venn-Experimental: media`, a text request has none; `decide(venn, q, text = "a", image = <file>)` stops ("text or an image") and sends no request; `decide(venn, q, image = <file>, references = <refs>)` stops ("reference") and sends no request.
- `test-deciders.R`: `decider_url(decider_venn())` is `"http://127.0.0.1:8000/api/alpha/decisions"`; `decide()` with Venn sends no key header.
- `test-live.R`: one Venn call behind `DECISIONS_LIVE_LOCAL` (a helper `skip_if_no_venn()` that checks `GET /health`, as `skip_if_no_laya()` does).
- [ ] **Step 3: Run them and see them fail** (`decider_venn` not found, unknown format "venn").
- [ ] **Step 4: Implement.** `build_venn()`: with no image, `build_systemone(model, questions, text, NULL, NULL)`; with an image, `list(model, questions = purrr::map(questions, systemone_question), state = list(image = base64(image)))`. `parse_answers()`: format `venn` uses `parse_systemone()` and sets `input_tokens` and `cost` to `NA`. `new_request()`: the media header when `images` is `TRUE` and the provider is `venn`. `check_venn_inputs()` runs in `decide()` for the provider `venn`. The roxygen of `decider_venn()` gives the install and start commands and the limits of the spec (research preview, one request at a time, CPU only, uncalibrated, images experimental and at chance on the textiles without references, AUC 0.48, D304).
- [ ] **Step 5: Run the full suite** — expected `[ FAIL 0 | WARN 0 | SKIP 6 | ...]`.
- [ ] **Step 6: Commit**
### Task 4: Documentation, checks and merge
**Files:**
- Modify: `decisions/README.md` (the table of providers: Gemini and Venn), `decisions/NEWS.md` (development version: two items), `decisions/data-raw/formats.md` (the verified fields of Gemini and Venn, with the dates of the probes)
- [ ] **Step 1: Write the three files.** No change to DESCRIPTION in this plan (the release plan changes the Description).
- [ ] **Step 2: Check**
Run: `Rscript -e 'devtools::document(); devtools::check(args = "--as-cran", remote = TRUE, manual = TRUE)'`
Expected: `0 errors | 0 warnings | 0 notes`.
- [ ] **Step 3: Push, open a pull request, wait for its checks** (`gh pr checks --watch`). Expected: all pass.
- [ ] **Step 4: Merge into `main`** after the final whole-branch review (executing-plans), and record the milestone step in the study (process row, build check, record-only commit).