auto-optimize 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. auto_optimize/__init__.py +3 -0
  2. auto_optimize/__main__.py +5 -0
  3. auto_optimize/cli.py +651 -0
  4. auto_optimize/skills/auto-optimize-code/LICENSE +21 -0
  5. auto_optimize/skills/auto-optimize-code/SKILL.md +251 -0
  6. auto_optimize/skills/auto-optimize-code/VERSION +1 -0
  7. auto_optimize/skills/auto-optimize-code/references/algorithm.md +182 -0
  8. auto_optimize/skills/auto-optimize-code/references/changes.md +146 -0
  9. auto_optimize/skills/auto-optimize-code/references/cli.md +206 -0
  10. auto_optimize/skills/auto-optimize-code/references/dashboard.md +203 -0
  11. auto_optimize/skills/auto-optimize-code/references/documents.md +55 -0
  12. auto_optimize/skills/auto-optimize-code/references/extending.md +87 -0
  13. auto_optimize/skills/auto-optimize-code/references/external-validation.md +98 -0
  14. auto_optimize/skills/auto-optimize-code/references/glossary.md +134 -0
  15. auto_optimize/skills/auto-optimize-code/references/idea-miner.md +108 -0
  16. auto_optimize/skills/auto-optimize-code/references/issue-bugs.md +182 -0
  17. auto_optimize/skills/auto-optimize-code/references/layout.md +300 -0
  18. auto_optimize/skills/auto-optimize-code/references/main-session.md +45 -0
  19. auto_optimize/skills/auto-optimize-code/references/measuring.md +208 -0
  20. auto_optimize/skills/auto-optimize-code/references/pr-optimization.md +127 -0
  21. auto_optimize/skills/auto-optimize-code/references/pr-tests.md +254 -0
  22. auto_optimize/skills/auto-optimize-code/references/promotion.md +133 -0
  23. auto_optimize/skills/auto-optimize-code/references/reading-a-search.md +65 -0
  24. auto_optimize/skills/auto-optimize-code/references/repo-mode.md +224 -0
  25. auto_optimize/skills/auto-optimize-code/references/resume.md +81 -0
  26. auto_optimize/skills/auto-optimize-code/references/shared-knowledge.md +114 -0
  27. auto_optimize/skills/auto-optimize-code/references/step0-interview.md +298 -0
  28. auto_optimize/skills/auto-optimize-code/references/step1-unit-tests.md +136 -0
  29. auto_optimize/skills/auto-optimize-code/references/step2-baseline.md +708 -0
  30. auto_optimize/skills/auto-optimize-code/references/step3-rounds.md +350 -0
  31. auto_optimize/skills/auto-optimize-code/references/step4-final.md +121 -0
  32. auto_optimize/skills/auto-optimize-code/references/step5-ship.md +220 -0
  33. auto_optimize/skills/auto-optimize-code/references/step6-finish.md +67 -0
  34. auto_optimize/skills/auto-optimize-code/references/subagent.md +467 -0
  35. auto_optimize/skills/auto-optimize-code/references/symptoms.md +67 -0
  36. auto_optimize/skills/auto-optimize-code/references/writing-briefs.md +117 -0
  37. auto_optimize/skills/auto-optimize-code/scripts/attribution.py +156 -0
  38. auto_optimize/skills/auto-optimize-code/scripts/autoopt.py +12863 -0
  39. auto_optimize/skills/auto-optimize-code/scripts/check_seeding.py +334 -0
  40. auto_optimize/skills/auto-optimize-code/scripts/cold.py +90 -0
  41. auto_optimize/skills/auto-optimize-code/scripts/dashboard/README.md +165 -0
  42. auto_optimize/skills/auto-optimize-code/scripts/dashboard/autoopt_monitor.py +850 -0
  43. auto_optimize/skills/auto-optimize-code/scripts/dashboard/gpu_scan.py +954 -0
  44. auto_optimize/skills/auto-optimize-code/scripts/dashboard/monitor_writer.py +684 -0
  45. auto_optimize/skills/auto-optimize-code/scripts/dashboard/scan.py +1030 -0
  46. auto_optimize/skills/auto-optimize-code/scripts/dashboard/static/dashboard.css +775 -0
  47. auto_optimize/skills/auto-optimize-code/scripts/dashboard/static/dashboard.js +3652 -0
  48. auto_optimize/skills/auto-optimize-code/scripts/dashboard/static/index.html +191 -0
  49. auto_optimize/skills/auto-optimize-code/scripts/issue_body.py +2118 -0
  50. auto_optimize/skills/auto-optimize-code/scripts/lint_text.py +448 -0
  51. auto_optimize/skills/auto-optimize-code/scripts/opt_pr_body.py +894 -0
  52. auto_optimize/skills/auto-optimize-code/scripts/opt_pr_tables.py +367 -0
  53. auto_optimize/skills/auto-optimize-code/scripts/paired.py +188 -0
  54. auto_optimize/skills/auto-optimize-code/scripts/preflight.py +280 -0
  55. auto_optimize/skills/auto-optimize-code/scripts/probe_run.py +72 -0
  56. auto_optimize/skills/auto-optimize-code/scripts/publish.py +385 -0
  57. auto_optimize/skills/auto-optimize-code/scripts/run_iteration.py +1382 -0
  58. auto_optimize/skills/auto-optimize-code/scripts/ship.py +1193 -0
  59. auto_optimize/skills/auto-optimize-code/scripts/stages.py +651 -0
  60. auto_optimize/skills/auto-optimize-code/scripts/tests_pr_body.py +1267 -0
  61. auto_optimize/skills/auto-optimize-code/scripts/tests_pr_push.py +426 -0
  62. auto_optimize/skills/auto-optimize-code/scripts/tests_pr_tables.py +779 -0
  63. auto_optimize-1.0.0.dist-info/METADATA +610 -0
  64. auto_optimize-1.0.0.dist-info/RECORD +67 -0
  65. auto_optimize-1.0.0.dist-info/WHEEL +4 -0
  66. auto_optimize-1.0.0.dist-info/entry_points.txt +2 -0
  67. auto_optimize-1.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,298 @@
1
+ # Step 0 — the interview
2
+
3
+ Everything here is expensive to change later: a gate written down wrong
4
+ invalidates every iteration measured under it. Ask with `AskUserQuestion`; do
5
+ not infer an answer the user has not given. A default the user accepted is
6
+ written "(default, accepted by the user)"; a default nobody was asked about is
7
+ written "(default, not asked: <why>)", and the user sees it in the read-back.
8
+
9
+ ## Before asking
10
+
11
+ 1. **Read `references/algorithm.md`**, and when the code lives in a git
12
+ repository, `references/repo-mode.md`. Each adds rows to the table below.
13
+ 2. **Collect what the user already said.** A question they answered is not
14
+ asked again; its answer goes into the read-back.
15
+
16
+ ## The questions
17
+
18
+ The questions come in six groups, asked in this order. A group is as many
19
+ `AskUserQuestion` calls as it needs, at most four questions each, in table
20
+ order; a question that depends on another's answer goes in a later call than
21
+ the one that asks it, and a question that does not apply (a GPU question in a
22
+ search with no GPU, a repository question in plain mode) is skipped. Put the
23
+ recommended option first and label it "(Recommended)"; where the *Default*
24
+ column says none, the answer is the user's alone, and no option is marked.
25
+ One exception: the unit-test scope row in `references/repo-mode.md`
26
+ recommends the functions being optimized, and is still always asked. Group 6
27
+ opens with one question, "Everything after the search: take the recommended
28
+ answers (list them) or choose each?": on "recommended" record every default.
29
+
30
+ A cost the table asks you to quote (a tracking dataset, an external
31
+ validation) is not known until the benchmark exists: say so, and the setup
32
+ subagent prices it at step 2 and brings back any that is large
33
+ (`references/step2-baseline.md`, *The price of the base*).
34
+
35
+ | Group | Question | Options to offer | Default | Recorded in |
36
+ |---|---|---|---|---|
37
+ | 1, the goal | Where should the search live? | `~/auto-optimize/<YYYY-MM-DD>-<name>/` (Recommended); another directory, on a disk with room for a copy of the code per iteration | the recommended one | the directory given to `init` |
38
+ | 1 | What is the objective, and is higher or lower better? | the timings, memory or sizes the code already reports | none: never guess | `spec.md` Objective |
39
+ | 1 | What does a representative run look like: which command or call, on which input? | an existing benchmark or script; a command and an input the user names; a proposal from the tests, examples and documentation, which setup writes and reports | none: never guess | Goal and scope; the *Benchmark* line setup writes at step 2 |
40
+ | 1 | What must not move, and by how much? One question per gate, in the forms of *Gates* below | the readings in the *Gates* table; when the user said the output must not change, bitwise identity against the baseline's output (Recommended) | the outputs match the baseline's, in a form the user chooses: a search with no output gate is the user's explicit choice | Gates, and each one's reference |
41
+ | 1 | Is there a result at which the search should stop? | none: run until a stop criterion is reached (Recommended); a value in the objective's unit, or a ratio of the baseline | none | Goal and scope: the root reaching it is a reason to stop (`references/step3-rounds.md`, *When to stop*) |
42
+ | 1 | A minimized objective: what is the smallest value still worth measuring? | a value in the objective's unit; none | none | `config.json` `floor` (`init --floor`): the search hands back at it, and step 2 asks again when A0 is under 20 times it |
43
+ | 2, the machine | Does the benchmark need a GPU? | yes; no | what the code uses | `config.json` `gpu` (`init --no-gpu`) |
44
+ | 2 | Whose machine is this, and how much of it may the search use? | at most half of the cores and memory idle when a step starts (Recommended on a machine shared with other jobs); a fixed number of cores and GB; the whole machine | half of what is idle | Machine and environment |
45
+ | 2 | Which environment runs the code, and may the search install packages into it? | the environment active in this session, nothing installed (Recommended); an environment or activation command the user names; a new one built under `$WORK/inputs/env/` | the session's, nothing installed | Machine and environment; the start of `--command` and `--tests` |
46
+ | 2 | Does the task name a worker, rank or thread count (`-p 4`, 8 threads)? An algorithm search asks its thread-count row instead, and this is that answer | the count; none | none | `config.json` `workers` (`init --workers`): A0 must show that many threads running at once |
47
+ | 2 | Is the code compiled? How is it built, and which compiler and flags must every build use? They are fixed for A0 and every iteration | the project's own build command and defaults, no variables (Recommended); name them (`CC=…`, `CFLAGS=…`) | the project's own | `init --build-env VAR=VALUE` (`config.json` `build_env`); the build at the start of `--command` |
48
+ | 2 | GPU search: how many GPUs does one iteration hold? Otherwise: how many of an iteration's runs go at once? | 1 (Recommended); 2; 4; the shapes in *Runs and GPUs* | 1 | Plan (`--gpus-per-agent`) |
49
+ | 2 | GPU search: which command gives a run its GPUs? | where the machine has an allocator or a scheduler, its words before a command, such as `srun --gres=gpu:1` (Recommended there); on a machine the user has to themselves, `env CUDA_VISIBLE_DEVICES=<index>` with the index they name (Recommended there). On a machine shared with other jobs, never offer a fixed index | none: always asked in a GPU search | `config.json` `gpu_command` (`init --gpu-command`) |
50
+ | 3, the rules | How many runs per iteration? | 5 (Recommended); 1; 3; 10 | 5 | Plan |
51
+ | 3 | Which observers should every run record? | your proposed list (Recommended); fewer | your proposal | Observers |
52
+ | 3 | What earns a promotion, and what breaks a tie? | a median beyond the noise band of the parent's (Recommended); plus a tie rule such as "compile time decides a runtime tie" | no tie rule | Keep rule |
53
+ | 3 | Is anything off limits? | none (Recommended); named files, flags, functions | none | Off limits |
54
+ | 3 | May a change alter the public interface (signatures, command-line options, file formats), add a dependency, or change the build files? | none of them (Recommended); a new dependency, named or any; an interface change the user describes; the build, when tuning it is the task | none of them | Off limits; a yes to the build is `config --set allow_build_changes=yes` |
55
+ | 3 | Must the optimizations work on every common OS, or may they rely on this machine's? | every common OS: Linux, macOS and Windows (Recommended); this machine's OS only | every common OS | Portability |
56
+ | 3 | May the proposed external validation run once a round? | the proposal, with its command (Recommended); none | none until signed off | External validation |
57
+ | 4, the kind of search | the rows `references/algorithm.md` adds, then in repository mode the rows `references/repo-mode.md` adds | | | |
58
+ | 4 | Plain mode: is there a test suite to run on every iteration? | its command, with thread pools pinned; none | none | `init --tests` |
59
+ | 4 | Are there bugs you already know of in this code? Do the inputs or model weights carry license terms that restrict their use? | none (Recommended); the bugs, in the user's words; the terms, or where they are | none | `$WORK/interview/known-bugs/`; `$WORK/interview/TERMS.md` |
60
+ | 5, running it | Setup's own questions (a busy machine, a tolerance, a faster documented flag): ask each, or take the recommended answer and report it? | ask (Recommended); take the recommended answers | ask | `config --set after.setup_questions=recommended` |
61
+ | 5 | Which model runs the iteration subagents and the setup subagent? (The idea miner and the orchestrator run on this session's model.) | opus (Recommended); sonnet; haiku; fable | opus | `config.json` `subagent_model` (`init --subagent-model`) |
62
+ | 5 | How will you watch the search? | the notebook, where JupyterLab runs on this machine (Recommended); a static HTML page; the terminal only | the notebook where JupyterLab is found, else the terminal | which line of the first table of `references/dashboard.md` the user is told; the publisher starts either way |
63
+ | 5 | How much may the search spend? | no limit: the stop criteria decide (Recommended); a number of iterations (A0 and same-code controls do not count); a number of idea-miner stages (each stage is one batch of ideas the miner writes from the root's profile; the search stops once every idea from them is tried or dropped); wall-clock hours | no limit | iterations: `after.budget_iterations`; stages: `after.budget_stages`; hours: `after.budget_hours`. Each is the `budget` reason to stop |
64
+ | 5 | Will you be away while it runs? | answering when asked (Recommended); away: unanswered questions continue on the answer that does the least damage | answering | Goal and scope; when away, offer Claude Code's `askUserQuestionTimeout` setting (`references/step3-rounds.md`, *Questions while the search runs*) |
65
+ | 6, after the search | End the search on the stop criteria, or ask you? Reclaim the disk at the end? | `stop`: `ask` (Recommended) or `auto`; `disk`: `ask` (Recommended), `keep` or `prune`; `pair_cap_hours`: 4 (Recommended), the most the paired timing at `end` may take | `ask`, `ask`, 4 | `config.json` `after` (`init --after JSON`, `config --set after.X=V`) |
66
+ | 6 (repository mode) | The oldest toolchain the repository supports? May the new unit tests edit the repository's test driver? The trailer line every commit message ends with? | `oldest_toolchain_command`: a command that builds and tests a tree, `{src}`, with the oldest compiler or library the repository supports, or null when there is none (`references/promotion.md`, *The preflight*); `tests_driver_edit` `yes`, `no` or `ask`; `commit_trailer` from the main session's instructions, verbatim | null; `ask`; no trailer | the same `after` block |
67
+
68
+ These `after` answers are asked here only, and step 5 asks its own five
69
+ (`references/step5-ship.md`): every later step reads `after` (`autoopt.py
70
+ config`) and asks only a field still `ask`. `after.build_outputs`
71
+ (default `[]`) lists files the build writes into the repository that the
72
+ runner committed, as paths relative to the repository's root or globs: an
73
+ untracked or edited file there is no unmeasured edit. Set it when
74
+ such files appear (`config --set 'after.build_outputs=["*.log"]' --reason …`).
75
+ `autoopt.py` keeps an `after` key that is not its own as it is, and refuses
76
+ `config --set` on one.
77
+
78
+ Then **read everything back in one paragraph, in this shape**, and go on to
79
+ `init` in the same turn: the read-back is not a question. A correction the
80
+ user makes to it changes the `init` flags or the `spec.md` line before they
81
+ are written; a later one is `config --set` for a setting and a dated rule in
82
+ `spec.md` for anything else.
83
+
84
+ > Kind: `<algorithm implementation>`, `<repository | plain>` mode. Objective:
85
+ > `<name>`, `<maximized|minimized>` (`<unit>`), measured on `<the command or
86
+ > call and its input>`; target `<value, or none>`; floor `<value, or none>`.
87
+ > Gates: `<name> <op> <threshold>` against `<reference>`, … Keep rule: "<…>".
88
+ > Observers: `<…>`. Thread counts timed, tracking inputs: `<…>`. Plan:
89
+ > `<runs>` runs per iteration; one iteration at a time on `<gpus>` GPU(s)
90
+ > through `<gpu command>`, or with no GPU and `<n>` runs at once. Unit tests:
91
+ > `<the command, or none>`, and new ones `<no | yes, scope>`. External
92
+ > validation: `<proposal, or none>`. Off limits: `<…>`; interface,
93
+ > dependencies and build `<unchanged | what may change>`. Portability:
94
+ > `<every common OS | this OS only, named>`. Machine: `<cores and memory the
95
+ > search may use>`; environment `<…>`, installs `<no | yes>`; compiler and
96
+ > flags `<the project's own | named>`. Known bugs and license terms: `<…, or
97
+ > none>`. Directory: `<path>`. Running it: setup's questions `<ask |
98
+ > recommended>`, subagent model `<…>`, watched in `<…>`, budget `<…, or
99
+ > none>`, user `<answering | away>`. After the search: stop `<auto|ask>`,
100
+ > disk `<…>`, paired timing capped at `<hours>`; in repository mode also
101
+ > oldest toolchain `<command, or none>`, test driver `<…>`, trailer `<…>`.
102
+ > Defaults taken without asking: `<…>`, or none.
103
+
104
+ ## The objective is one number
105
+
106
+ One number per run, maximized or minimized. If the user names two, one of
107
+ them is a gate or an observer. Work out which and say so:
108
+
109
+ > "Faster and more accurate" is not one objective. Which one is the thing
110
+ > being improved, and what is the other one allowed to do?
111
+
112
+ A benchmark that cannot emit the objective as a finite number is fixed at
113
+ step 2, not worked around.
114
+
115
+ ## Gates
116
+
117
+ A gate is a hard constraint checked on **every run**. One violating run fails
118
+ the iteration however good its median. The user's words almost never
119
+ determine the gate, so read back the exact form before writing it down.
120
+
121
+ | What the user says | What it could be | Ask |
122
+ |---|---|---|
123
+ | "the predictions must not change" | bitwise identity; max abs difference ≤ 1e-5; correlation ≥ 0.999 | which, and against what reference |
124
+ | "the output must be identical", for a dict, list or string | equal as values; also in the same order (dict insertion order, list order) | whether order counts |
125
+ | "must stay fast" | median latency; p99 latency; throughput | which statistic, measured over what |
126
+ | "must not use more memory" | peak allocated; peak reserved; resident | which, and measured when |
127
+ | "the error must not grow" | not above baseline; not above baseline + 1e-6 | the exact ceiling, as a number |
128
+ | "under 5000 allocations" | `<` or `<=` | which: for an integer count they differ by one |
129
+
130
+ A bitwise gate counts what differs and requires zero:
131
+ `{"name": "n_mismatch_vs_base", "op": "==", "threshold": 0}`.
132
+
133
+ Every gate ends up as `{"name", "op", "threshold"}`, with `op` one of `<=`,
134
+ `<`, `>=`, `>`, `==`, and an optional `tol`. On `==`, `tol` makes a band,
135
+ \|value − threshold\| ≤ tol. On the other four it loosens the threshold by
136
+ `tol`, so leave it off unless the user asked for that. Write the reference
137
+ into the name when there is one: `max_abs_diff_vs_base` keeps its meaning when
138
+ read alone; `diff` does not.
139
+
140
+ **A gate needs a reference that cannot move**: a file on disk written once,
141
+ the baseline's outputs, not "whatever the current best iteration produces".
142
+ The setup subagent wires it up at step 2 and proves the gate against three
143
+ kinds of change before the baseline runs (`references/step2-baseline.md`,
144
+ *Prove the gate*).
145
+
146
+ A numeric tolerance agreed here is provisional until step 2 computes the error
147
+ distributions it has to separate (*Compute a numeric tolerance against real
148
+ data*, same file), and the user decides again from them. Say so when the user
149
+ names one.
150
+
151
+ ## Observers
152
+
153
+ Recorded on every run, never optimized and never gated, observers show what a
154
+ move in the objective cost: peak memory, compile time, records per second,
155
+ the thread count reached, a secondary timing.
156
+
157
+ Propose more rather than fewer. Each is one more key in `metrics.json`, and
158
+ an observer can become a gate later, while a number nobody recorded can only
159
+ be recovered by re-running every iteration.
160
+
161
+ The group-3 proposal leaves out the observers that rows from `algorithm.md`
162
+ and the mode's file ask about in group 4 (the memory measure, the
163
+ tracking datasets, a second thread count).
164
+ Those answers join the list in the read-back, so no group-3 answer depends on
165
+ a later one.
166
+
167
+ ## Runs and GPUs
168
+
169
+ **Runs per iteration** default to 5: every iteration, A0 included, runs that
170
+ many, one seed each, and the median of five resists two bad seeds. One is
171
+ honest only for a deterministic benchmark, such as a timing harness with a
172
+ fixed input. Quote the cost as runs per iteration times the iterations, A0
173
+ included.
174
+
175
+ **One iteration runs at a time.** Each is measured against the root the
176
+ previous one left, and an iteration that passes the keep rule becomes the
177
+ root, so gains accumulate. A regression in a promoted root is inherited by every
178
+ iteration after it; the paired comparison against A0 at the end
179
+ (`references/step4-final.md`) is what shows it.
180
+
181
+ **GPUs per iteration** default to 1: the iteration holds one card and runs
182
+ its seeds one after another. Raise it and the iteration holds that many and
183
+ runs that many seeds at once, so each iteration finishes sooner. `gpus_per_agent`
184
+ must fit in the free GPUs; check what is free before promising a number, with
185
+ whatever allocates GPUs on the machine, or `nvidia-smi` where nothing does.
186
+ Without a GPU, `gpus_per_agent` is how many of an iteration's runs go at once.
187
+
188
+ **For a CPU benchmark, the concurrency agreed here is provisional.** Runs at
189
+ once (`gpus_per_agent`, or the runs an iteration starts at once without a GPU)
190
+ can push a memory-bound objective outside the noise band while most cores sit
191
+ idle, so step 2 measures how many concurrent runs the benchmark tolerates
192
+ before the baseline (`references/step2-baseline.md`, *Measure what concurrency
193
+ does to the objective*), and a narrower answer comes back to the user.
194
+
195
+ **Promotion.** Each promotion closes a round, and an iteration's runner
196
+ promotes it the moment it passes the keep rule. That is the recommended rule
197
+ only: a keep rule with a tie rule or a second measure is `init --promote
198
+ orchestrator`, and the orchestrator promotes by hand
199
+ (`references/promotion.md`).
200
+
201
+ ## Writing it down
202
+
203
+ `<name>` in the directory is what the user called the search, or a short slug
204
+ of the task; the date is the day the search starts. `init` creates
205
+ `autooptimize/` inside it, and the harness, data, notes and later work on the
206
+ search all stay under it.
207
+
208
+ In repository mode with a new unit-test suite, `init` waits: step 1 creates
209
+ the clone that `init --repo` copies, runs `init` on it, and the TODOs below are
210
+ filled then (`references/step1-unit-tests.md`).
211
+
212
+ ```bash
213
+ WORK=~/auto-optimize/2026-10-01-latency # the directory the user chose
214
+
215
+ python3 $AO/autoopt.py init $WORK \
216
+ --task 'Cut single-request latency without moving the outputs.' \
217
+ --command "AUTOOPT_REF=$WORK/autooptimize/reference python3 bench.py --iteration {iteration} --seed {seed} --out {out}" \
218
+ --objective latency_ms --direction min --unit ms \
219
+ --gates '[{"name": "max_abs_diff_vs_base", "op": "<=", "threshold": 1e-5}]' \
220
+ --observers peak_mem_gb,requests_per_s \
221
+ --runs 5 \
222
+ --gpus-per-agent 1 \
223
+ --copy /path/to/bench.py --copy /path/to/kernels.py
224
+ ```
225
+
226
+ These flags are decided here, not later:
227
+
228
+ - **`--copy`** puts a file or directory into the baseline worktree; repeat it.
229
+ Without it the baseline is empty and the benchmark is written into it by
230
+ hand. In repository mode `--copy` fills `harness/` instead, and `--repo`
231
+ and `--tests` are set here too (`references/repo-mode.md`, *Setting it up*).
232
+ - **`--no-gpu`** sets `"gpu": false` for a benchmark that needs no GPU, which
233
+ lifts the lease check.
234
+ - **`--gpu-command`** records `gpu_command`, the allocator's words (`references/step2-baseline.md`, *GPU leases*).
235
+ - **`--command`** in double quotes, so the shell writes `$WORK`'s value into
236
+ `config.json`; placeholders stay in braces (`references/layout.md`,
237
+ *config.json*).
238
+
239
+ `init` validates as it writes: a `command` without `{seed}` and `{out}`, or a
240
+ gate without a numeric threshold, fails at once. It writes
241
+ `autooptimize/spec.md` with a TODO line for each answer, and creates
242
+ `reference/`, `ideas.md`, `knowledge.md` and the `tools/` READMEs.
243
+
244
+ **Replace every TODO in `spec.md` from the interview** (the machine and
245
+ environment answers go on its *Machine and environment* line, the
246
+ representative run and any target under *Goal and scope*), except three that the
247
+ setup subagent answers at step 2: *Gate proof*, *Width measured* and
248
+ *Benchmark*. Until all are
249
+ gone, `new`, `brief` and `run_iteration.py` refuse (`refusing to fork an
250
+ iteration: step 0 is not finished`, `refusing to run A0: …`) and print the
251
+ remaining TODO lines. An existing `spec.md` is never overwritten, even by
252
+ `init --force`.
253
+
254
+ Step 0 also leaves what is not `spec.md` in the search directory
255
+ (`references/layout.md`, *The search directory*): the questions and the
256
+ user's answers, in their words, as `$WORK/interview/interview.md`; the
257
+ license terms the user named for the inputs and the model weights, copied
258
+ verbatim (`TERMS.md`, `terms_raw/`); and `known-bugs/`, the bugs the user
259
+ already knows, so later steps do not report them as new. What the benchmark
260
+ reads (data, the environment, toolchains) goes in `$WORK/inputs/` (`data/`,
261
+ `env/`, `toolchains/`), not at the top of `$WORK`.
262
+
263
+ **Every rule is in `spec.md` before it is used** (`SKILL.md`, *Rules that
264
+ hold everywhere*).
265
+
266
+ If the surrounding project is a git repository, add `autooptimize/` to its
267
+ ignore list: it holds a git repository of its own and every run's outputs.
268
+
269
+ ## Handing off step 2
270
+
271
+ Step 2 belongs to a setup subagent. The idea miner starts after it, not
272
+ beside it: stage S1 prices every idea against A0's profile, which setup writes
273
+ after the baseline, and a miner started earlier prices blind. `setup`
274
+ refuses while a TODO other than *Gate proof*, *Width measured* and *Benchmark* is left (`refusing to start …:
275
+ the interview is not written down`):
276
+
277
+ ```bash
278
+ python3 $AO/autoopt.py setup $WORK # opens step 2; prints the setup subagent's prompt (briefs/setup.md)
279
+ ```
280
+
281
+ Spawn it with the printed prompt unedited, with the description and model
282
+ its stderr line names, then end your turn: its report wakes you, a setup
283
+ question included, so start no timer, watch or polling loop. With a new
284
+ unit-test suite, `init` comes at the end of step 1, and it starts then.
285
+
286
+ `setup` alone refuses while step 2 is open (`step 2 is already with a setup
287
+ subagent`; `setup --force` prints a prompt that continues from
288
+ `autooptimize/setup/notes.md`), and `--ask`, `--answer`, `--force` and
289
+ `--done` refuse when none is (`no setup is open`). `brief` refuses (`step 2
290
+ is still with the setup subagent`) until `setup --done`. `mine` refuses stage
291
+ S1 while `checks.profile` is registered and `iterations/0/profile.txt` does
292
+ not exist; `--without-profile` starts it blind. `status` prints `first
293
+ iteration: waiting for setup (step 2) and the idea miner's S1`, naming
294
+ whichever is not done.
295
+
296
+ From here on, `references/main-session.md` lists what the main session does
297
+ at each event: a setup question, the setup subagent's report, the idea miner
298
+ and the orchestrator subagent.
@@ -0,0 +1,136 @@
1
+ # Step 1 — a new unit-test suite, before the search
2
+
3
+ Repository mode only, on a yes to "Do you want to add new unit tests?" at
4
+ step 0, with the scope the user chose: the functions being optimized, or the
5
+ whole repository. The suite is written before the baseline because every
6
+ iteration is gated on it.
7
+
8
+ The main session spawns one step-1 agent with this file as its brief; "you"
9
+ below is that agent. It may split the writing across subagents of its own, and
10
+ it reports once: the squashed commit, the bugs found, and every question for
11
+ the user together. The main session then runs `init` on the clone.
12
+
13
+ ## Where it lives
14
+
15
+ Everything step 1 writes is in `$WORK/tests/`: `suite/` (the clone below),
16
+ `analysis/` (the reading of the existing tests: what they cover, what they
17
+ pin) and `work/` (scripts, coverage inputs and outputs, and `bugs/`). Nothing
18
+ there is pruned: step 5 builds the unit-test PR and the bug report from
19
+ these folders (`references/pr-tests.md`, `references/issue-bugs.md`).
20
+
21
+ The suite is written in a clone, which `init --repo` copies: `autoopt/0` is
22
+ then the original plus the suite, and every iteration's branch is built on it.
23
+
24
+ 1. **Check the original, clone it and branch**: `python3 $AO/autoopt.py tests-branch <original> --work $WORK`
25
+ (`--date YYYY-MM-DD` to override today). It refuses uncommitted changes, a detached HEAD
26
+ and a branch that is not pushed (origin's commit must equal the branch's; it only
27
+ reads, never fetches), so the user settles those first. It clones into `$WORK/tests/suite`,
28
+ points the clone's `origin` at the original's GitHub remote, not its local path
29
+ (`init` records it as `repo.remote_url`), and branches `auto-optimize/<YYYY-MM-DD>-unit-tests`
30
+ (`references/repo-mode.md`, *Setting it up*, step 1).
31
+ 2. **Write the suite, run it, and as the last step squash the whole branch
32
+ into one commit** (the rest of this file covers the suite). Commit as often
33
+ as you like while writing; when the suite passes in every configuration
34
+ (*It passes on the base*, below), collapse everything the branch holds
35
+ above the original's commit into one commit, with a message that could be
36
+ posted as it is: `python3 $AO/autoopt.py tests-branch --work $WORK --squash -m '<message>'`
37
+ (it refuses unless exactly one commit is left above `origin/HEAD`).
38
+ `init --repo` refuses a clone on `auto-optimize/<YYYY-MM-DD>-unit-tests`
39
+ that is more than one commit ahead of `origin/HEAD`.
40
+ 3. **Init on the clone**: `python3 $AO/autoopt.py init $WORK --repo
41
+ $WORK/tests/suite …` (`references/repo-mode.md`, *Setting it up*).
42
+ 4. **Fill `spec.md`** from the interview (`references/step0-interview.md`,
43
+ *Writing it down*). Its *New unit tests* line records the scope, the clone,
44
+ the branch, and the original's remote URL and branch.
45
+
46
+ ## What it covers
47
+
48
+ | Scope | Targets |
49
+ |---|---|
50
+ | the whole repository | every public function, class and method, and every command-line option |
51
+ | the optimized functions | those functions and everything they call, from every public entry point that reaches them |
52
+
53
+ List the targets before writing a test: walk the source for public names
54
+ (`ast` for Python, the `cpdef` and `def` names of Cython modules) and the
55
+ argument parser for options, and keep the list as a file. A test that only
56
+ checks that a call does not raise is not coverage. Hundreds or thousands of
57
+ tests are normal for a repository: split the writing across subagents by
58
+ module, then have separate reviewer subagents audit the expected values.
59
+
60
+ ## Where expected values come from
61
+
62
+ Derive them independently wherever practical: inputs small enough to work by
63
+ hand, a short reference implementation inside the test, a library function
64
+ (`scipy.stats` for probabilities). A test's input file is small, written by
65
+ hand or copied into the repository's test data under a license that allows
66
+ it: a test that reads a file from outside the repository fails in its CI. A test that only records the current output says so in its
67
+ docstring ("pins the current output"), in those words, so they can be
68
+ counted. Keep these few, because they also pin the bugs.
69
+
70
+ ## It passes on the base, in every configuration the repository's CI runs
71
+
72
+ - **No known-bug tests.** A test asserting behavior the base gets wrong is a
73
+ `strict` xfail at best, and many CIs run `pytest --runxfail`, which turns
74
+ every xfail into a failure. Leave such tests out and keep each bug in
75
+ `$WORK/tests/work/bugs/<short-name>/`: what the base does, what it should
76
+ do, a command that reproduces it, and the removed tests. List the bugs in
77
+ your report.
78
+ - **No dependence on the platform, the Python version or the build.**
79
+ `ru_maxrss` is bytes on macOS and KiB on Linux; `math` domain-error messages
80
+ changed wording in Python 3.14; how argparse quotes choices in its error
81
+ messages changed between Python 3.12 patch releases; a Cython 3.0 build can
82
+ behave differently from a 3.1 build of the same source. A test that fails in
83
+ one CI configuration is dropped or made to hold in all of them.
84
+ - **Optional dependencies skip, never error.** A helper that needs an optional package uses
85
+ `pytest.importorskip`, so a CI that installs only `pytest` and
86
+ `requirements.txt` skips those tests. Run the suite once with the dependency
87
+ uninstalled, the way CI lacks it: since pytest 8.2, `importorskip` warns when
88
+ the import fails with an `ImportError` other than `ModuleNotFoundError`, and
89
+ the warning says a later release will make it an error.
90
+ - **Copy the CI before calling it done.** Read the repository's workflow
91
+ files and run each job's install and test commands locally against the
92
+ suite's branch, in each Python or compiler version the matrix names that
93
+ this machine can provide; record the commands and results in
94
+ `$WORK/tests/work/`.
95
+
96
+ ## Report its coverage
97
+
98
+ Measure line coverage of the package with the new suite against the
99
+ repository's own tests, and list the public names no test calls directly.
100
+ Leave the coverage tools' JSON and the list in `$WORK/tests/work/`, with the
101
+ commands that made them, and give the totals in your report.
102
+
103
+ **Coverage is measured before and after, per part of the repository** (a file,
104
+ or a directory when there are many; vendored or generated code is excluded
105
+ from every total). "Before" is the repository's own suite at the base commit,
106
+ which is 0 tests and 0% where it has none; "after" adds the new suite.
107
+
108
+ - **Tests per part** counts the tests that execute at least one line of that
109
+ part, from per-test coverage: coverage.py's `dynamic_context =
110
+ test_function` (or `pytest --cov-context=test`), saved with `coverage json
111
+ --show-contexts`, or, for compiled code, one gcov or llvm-cov run per test
112
+ with the counters reset between them, each test's gcovr JSON in one
113
+ directory. When a per-test run is too slow, or the tests reach the code
114
+ through a program run as a subprocess, count the tests that exercise each
115
+ part (a test of a command-line option counts for every part the program
116
+ runs) and write the counts as a table with columns `part`, `tests_before`,
117
+ `tests_after`.
118
+ - **Line coverage** uses the same tool and flags before and after; record the
119
+ tool and its version.
120
+ - Measure "before" on the base commit itself, not on the suite's branch with the
121
+ new tests deselected: a conftest or fixture the new suite adds can change what
122
+ the repository's own tests reach.
123
+
124
+ Compiled Cython needs a traced build to be measured at all:
125
+ `cythonize(..., compiler_directives={"linetrace": True})`, compiled with
126
+ `-DCYTHON_TRACE=1 -DCYTHON_TRACE_NOGIL=1`, and on Python 3.13 or later also
127
+ `-DCYTHON_USE_SYS_MONITORING=0`, without which coverage records nothing in the
128
+ compiled modules. Use the `Cython.Coverage` plugin with `parallel = true` and
129
+ `patch = subprocess` to count the command-line tests. A `coverage combine`
130
+ that fails part way has already folded some data files into `.coverage`;
131
+ re-run it with `--append` on a copy, or the result silently drops them.
132
+
133
+ Then hand off step 2 (`references/step0-interview.md`, *Handing off step
134
+ 2*). The baseline's unit-test run is the suite's first check inside the
135
+ search: it MUST pass there (`references/repo-mode.md`, *Setting it up*, step
136
+ 5).