decisions 0.2.0, part 3: Gemini in the case study Implementation Plan

plans/2026-11-24-decisions-0.2.0-case-study-plan.md

# decisions 0.2.0, part 3: Gemini in the case study 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:** Gemini gets the treatment of gpt-6-luna in the vignette "Industrial image inspection" (D197, D302): its own description loop on each of the four datasets, the grid, the calibration, the folds and the test, and a second full run for the section "Reruns" (D311).

**Architecture:** The loop kit (`data-raw/study_again/loop/`) and `data-raw/study_again.R` get a model setting, so that Gemini has its own sandbox, files, purpose labels and work folder, and the files of gpt-6-luna stay as they are. `case_save()` gets a path that saves a model before its test. The vignette reads every Gemini row from `inst/extdata/study_again.rds`.

**Tech Stack:** R, decisions with `decider_gemini()`, the Claude Code CLI (`claude -p`, model `claude-opus-5-5`, the flags of `run_loop.sh`), the study's audit code (`R/fair_loop.R`).

**Spec:** `replication_materials/plans/2026-11-24-decisions-0.2.0-design.md` section 3 (D313); D197, D213, D301, D311.

Paths: `decisions/`, `study/` as in part 2. The map of the current pipeline is in the report of 2026-11-24 (study_again.R lines 82, 88, 91-98, 264-294, 373-379, 457, 508, 786-836, 915-1088, 1106-1111; loop kit files).

## Global Constraints

- Branch `case-study-gemini` of decisions, from `vignettes-gemini-venn` (part 2).
- Paid calls: Gemini only, budget row of D301 (USD 15, 30,000 calls); purpose prefix `decisions-0.2.0-study-` and `decisions-0.2.0-gemini-loop-`; caps in `study_again.R` for Gemini: 3,000 calls for the grid and calibration, 1,050 for the test, as for gpt-6-luna. Estimated cost of the two runs and the loops: USD 5 to 8.
- The loops: one fresh agent for each dataset in a blind sandbox, the loop instructions of the study, two references per class, at most 6 candidates, candidate 1 the generic description, scored with `score_setup()` (D197). Sandbox root `C:/Users/megahefm/decisions-gemini-loops`, outside Dropbox. The transcripts are kept (the gpt-6-luna transcripts are lost).
- The test images are scored once per run: run 1 and the rerun of D311. `evaluate_once()` refuses a second write in the same folder; the rerun has its own work folder. The first run is the reported result.
- The files of gpt-6-luna, Jev and Clef-Flash do not change; a check before each save compares them.
- "No claim in a vignette that the numbers do not support": each `stopifnot()` of the vignette that Gemini breaks is a fact to change in the text, not a check to remove.

## Review Focus

1. A Gemini loop file that overwrites a gpt-6-luna file (no model prefix): Task 1 checks that the gpt-6-luna loop files keep their md5.
2. A scoring call of a loop agent outside the sandbox: the audit of each loop must say "No call outside the sandbox".
3. The rerun reading the state of run 1 (the state files would replay old answers): the rerun folder starts empty, and the run checks that it made its own calls.
4. A test row that is not a test row of the study manifest: `check_test()` as for gpt-6-luna.
5. A vignette sentence that compares "the other two models" or names "three models": every model count and model name in the text comes from `model_label`.

---

### Task 1: The loop kit by model

**Files:** `decisions/data-raw/study_again/loop/loop_common.R`, `loop_score.R`, `make_sandbox.R`, `run_loop.sh`, `audit.R`, `README.md`.

- [ ] **Step 1:** A setting `DECISIONS_LOOP_MODEL` (`openai` or `gemini`, default `openai`): the sandbox root, the file prefix (`gemini_<dataset>_*.csv`; the openai files keep their names), the model text of `LOOP.md` ("gemini-decision-1 (the Gemini Decisions API)"), the decider and the service of `loop_score.R`, the purpose label, and the keys (`.Renviron` and `.env`).
- [ ] **Step 2:** Check: the md5 of every gpt-6-luna loop file is unchanged; `make_sandbox.R textiles` with `gemini` makes the sandbox; the rehearsal with no paid call (`DECISIONS_LOOP_CHECK_ONLY=true bash score.sh 1`) passes; `run_loop.sh --dry-run` prints the command. Commit.

### Task 2: The four Gemini loops

- [ ] **Step 1:** For each dataset in turn (textiles, extrusion, microstructure, pipe_staple): make the sandbox, rehearse, run the loop, audit it. Expected: at most 6 candidates; the audit says "No call outside the sandbox"; every scoring call in `gemini-calls.csv`.
- [ ] **Step 2:** Commit the loop files and the audits; record the loop calls in `calls/README.md` (record-only commit).

### Task 3: The study steps for Gemini

**Files:** `decisions/data-raw/study_again.R`.

- [ ] **Step 1:** `gemini` in `services`, the caps, the purpose prefix, `read_loops()` (the Gemini loop files), the `deciders` chunk, the no-call wording branch (the loop AUCs as they are, as for openai), the key map and `.env`, a work folder setting (`STUDY_AGAIN_DIR`, default `data-raw/study_again`), and a `case_save()` path that saves a model that has no test yet (no test rows, no run dates for the test).
- [ ] **Step 2:** Check with `STUDY_AGAIN_REPLAY=true` that the saved results of the other models are unchanged after a save.
- [ ] **Step 3:** Run `wording`, `grid`, `calibration`, `folds` for gemini; then `STUDY_TEST_DRY_RUN=true` for the test checks; then `test`; then `save`. Expected: 15 + 5 setups in `chosen` (20 rows), Gemini test rows for the 1,027 test images, the calls in the log within the caps. Commit.

### Task 4: The rerun of D311

- [ ] **Step 1:** With `STUDY_AGAIN_DIR` set to an empty folder, run `wording` (the kept candidates scored again), `grid`, `calibration`, `folds` and `test` for gemini. Expected: new calls only (no state of run 1 read).
- [ ] **Step 2:** Compare with run 1: the largest change of a wording AUC and of a grid AUC, the changed choices, and the change of each test AUC; save the comparison as an element `reruns` of `study_again.rds`. Commit.

### Task 5: The vignette

**Files:** `decisions/vignettes/industrial-image-inspection.Rmd`.

- [ ] **Step 1:** `model_label` gets `gemini = "Gemini"`; the `deciders` chunk equals `study_again.R`; the lists of models, keys and model names; the loop text; `chosen_k`; every "three models", "all 15 setups" and "the other two models" from the data; the cost text.
- [ ] **Step 2:** Build the vignette. For each `stopifnot()` that fails with the Gemini rows (lines 580-581, 864, 871, 886-897 today), change the sentence to what the data say and keep a check of the new sentence.
- [ ] **Step 3:** The section "Reruns": the Gemini comparison from `saved$reruns`, next to the text of D213.
- [ ] **Step 4:** `devtools::check(args = "--as-cran", remote = TRUE, manual = TRUE)`: 0 errors, 0 warnings, 0 notes. Record the calls (a section in `calls/README.md`), a process row, record-only commit. Push the branch and open a pull request; wait for its checks.