Skip to content

refactor(campaign): move the adapt recommendation choice out of the proxy (3/3, #1302) - #1334

Merged
amiddavid merged 9 commits into
mainfrom
feat/mv-1302-campaign
Nov 24, 2026
Merged

amiddavid merged 9 commits into
mainfrom
feat/mv-1302-campaign

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

Last of 3 stacked PRs. Based on feat/mv-1302-campaign-arms. Closes #1302. Part of #1309.

What changed

  • The adapt report's choice of the best option, the option that passes the builder's gate, the one-line gate/dollar disagreement, and the summary sentence moved into campaign.Picker.
  • adaptReport in the proxy builds the rows and JSON, and calls the picker.

Behaviour

None. The sentences and the order of the checks are the same.

Left in the proxy, on purpose

Data dependence

Same as before. No new reader of stored text.

How it was tested

macOS, Go 1.27.4, at this head:

  • go build ./... && go vet ./... && gofmt -l .: clean.
  • CGO_ENABLED=1 go test -race -timeout 30m: ./campaign, ./cmd/..., ./internal/..., ./components/all, ./predictive, ./tenant, ./keepalive pass. ./proxy (1204 s) pass.
  • Mutation: o.Net > p.bestNet to < fails 3 picker tests. o.GatePass negated fails 2.

Closes #1302. Refs #1309.

…the proxy

Add the library package `campaign`. It holds the calibrated constants, the
closed-form replay, the fidelity model, the ship gate, the auto builder and the
decision math that proxy/predictive.go, campaignauto.go, adaptreplay.go and
adaptcache.go used to hold. The proxy reads history and files and calls it.

No behaviour change. The rowFloor wait-cause gap stays as it is (#1298).

Refs #1309
Closes #1302 (with the next PR in the stack)

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 c3b15632dc43 approve

Verdict: approve

Findings

None.

Non-blocking

  • Pick.GateDiff and Pick.GateCI read as zero when GateID == "". The proxy reads them only under Recommends(), so no wrong output. A caller could misread them; not worth a change now.
  • campaign.Option.Diff is unused for the "never" baseline (the tests set it). Harmless.

What I checked

  • Read the full diff (campaign/recommend.go new, campaign/recommend_test.go new, proxy/adaptcache.go rewritten block) and the base version of proxy/adaptcache.go at bc42e0f0.
  • Behavior equality, line by line. I compared the old inline block against Picker.Add and Picker.Result:
    • Best tracking: same "never" exclusion, same start at -Inf, same strict > (first option wins a tie).
    • Gate and fail tracking: same gateID == "" / failID == "" guards, same strict >, same strings.Join(why, "; ").
    • Disagreement: same realBeats != GatePass test, same reset-then-append, same leader-by-net choice.
    • Summary switch: same case order (BestID == "" first, GateID != "" second, then beats && failID == bestID, beats, default id, else). The recommendation map is built under Recommends(), which is set only in the same case that built it before.
  • Wire format. I extracted every format string from the old file and the new file and diffed them. All five moved strings (three summaries, two disagreement lines, plus the gate summary) are byte-identical. The strings that stayed in the proxy are unchanged.
  • Compile-level checks by reading (no builds, per the review rules): math, strings, fmt are all still used in proxy/adaptcache.go (lines 471-518, strings.Join(disagree, ""), fmt throughout). recommend.go imports only fmt, math, strings, all used. No name collisions: Option, Pick, Picker, NewPicker do not exist elsewhere in campaign; the test identifiers pick, never, def collide with nothing in the package's other test files.
  • Hard boundaries. The move follows the library/proxy rule: the choice logic now lives in campaign, which imports no host package. The proxy keeps only the report assembly. No data-dependence change: the code reads the same inputs it read before, so no docs/data-dependence/ row change is needed. No behavior change for users, so no human sign-off is required.
  • Tests. The new tests cover all six summary branches (no option, gate pass, adapted leader fails the gate, fixed leader, default leads, interval includes zero) and both disagreement directions. I traced each assertion against Add/Result by hand; each expectation matches the code. The proxy's recommendation, gain and warning assembly still read the same fields they read before.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 c3b15632dc43 approve

Verdict: approve

Findings

None. The change moves the adapt report's recommendation choice out of the proxy, and I found no behavior difference.

Non-blocking

  • proxy/adaptcache.go:345 — the K3 baseline (campaign.ScoreRows(facts, test, fixed(3, se), &fp)) is recomputed inside the loop, once per adapted option. Pre-existing, unchanged by this PR. K3 is also scored as its own arm earlier, so the same numbers are computed twice per adapted arm.
  • campaign/recommend.go:44 — the Add doc comment spans three lines and mixes the "never" rule with the Disagreement rule. A short second sentence on Disagreement would read easier. Style only.

What I checked

  • Read CLAUDE.md and docs/design.md.
  • Read the full diff (3 files, +199/−45) and the code around each change.
  • Compared the moved logic line by line against bc42e0f0:proxy/adaptcache.go:
    • Best pick: same rule (id != "never", best net, -Inf start, strict > so the first of a tie wins), same fields (Net, CI, Diff = SumOf(d)).
    • Gate pick, fail pick, disagreement pick: same conditions, same tie-breaks, same reset-to-nil of disagree.
    • All six summary strings and both disagreement strings: byte-identical to the old ones.
    • beats (BestID != "default" && BestCI[0] > 0), the recommendation condition (GateID != "" ↔ Recommends()), and the switch order: identical.
    • Unavailable strategies still continue before picker.Add, so they enter neither the best nor the gate pick, as before.
  • Checked the boundaries the repo sets: logic moved from proxy into the library package campaign (the direction the rules ask for); campaign imports nothing from proxy; no new data dependence (the picker works on computed numbers only), so no docs/data-dependence/ row is needed; no behavior change, so no human sign-off is needed.
  • Checked for symbol collisions: Option, Picker, Pick, NewPicker are new in campaign; the test helper pick and vars never, def collide with nothing at package scope (def in campaign/replay.go is a parameter).
  • Checked the new tests: they cover the recommend path, the leader-fails-gate path, the fixed-plan leader, the interval-includes-zero case, the default leading, no option at all, and both disagreement directions. The proxy's own adaptcache_test.go still exercises the report end to end.
  • Did not run builds or tests (live tests not approved).

- Document that Eval and BuildSpec need facts when segBy is set.
- Drop the dead `if ss := ...; true` wrapper in Rows.
- Drop the idle loop in the Apply test: Apply takes no idle.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
Move the mapping of a suggest arm to a live keep-alive strategy, the cell
resolution, the overlap and duplicate checks, the weekday and hour tiling and
the grouping of cells into strategies into the `campaign` library package.

- The mirror constants of config.Default* are gone. The library reads
  config.DefaultKeepAliveIdle, DefaultKeepAliveMaxPings,
  DefaultKeepAliveMinPrefix and DefaultHeadTTLMinTokens. The values do not change.
- `campaign.HonorsHeadTTL1h` replaces two copies of the "every model honors
  the 1h tier" rule: the proxy's and cmd/cg-cache-campaign's.
- The proxy keeps the HTTP routes, the views and validateGroupStrategy.

No behaviour change.

Refs #1309
Closes #1302

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
- DefaultWeekdays is a function that returns a copy; a test pins it.
- Explain the 50,000-token head-TTL gate and why the deprecated config
  constant is the right value there.
- Fix stale comments that named the old proxy functions.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid
amiddavid force-pushed the feat/mv-1302-campaign branch from c3b1563 to cee61e6 Compare November 24, 2026 09:30
@amiddavid
amiddavid force-pushed the feat/mv-1302-campaign-arms branch from bc42e0f to f27cf87 Compare November 24, 2026 09:30
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round-1 notes, fixed in cee61e6 (macOS, Go 1.27.4). At this head: build, vet, gofmt clean; go test -race ./campaign ./kvcache ./cmd/cg-cache-campaign pass; go test ./proxy pass.

  1. Pick.GateDiff/GateCI: fixed. Documented as zero when GateID == "". The Add doc is now two short sentences.
  2. docs/design.md: fixed. Rows added for campaign/ and predictive/.

Kept as is:

  • The K3 recompute in proxy/adaptcache.go:345: it is the old code, moved unchanged. A cache would be a separate change.
  • Option.Diff for "never": the picker ignores "never" as a best option, and Diff is not read for it.
  • The bounds-test floor in proxy/campaign_test.go:32: old test, covered by TestCtlCreateCampaignEveryArmCarriesItsOwnLiveParameters.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 cee61e68bad1 approve

Verdict: approve

Findings

None.

Earlier findings, status

  • Mine, round 1 (non-blocking): Pick.GateDiff/Pick.GateCI read as zero when GateID == "". Fixed. cee61e68 documents both fields as "zero when GateID is """.
  • Mine, round 1 (non-blocking): Option.Diff unused for the "never" baseline. Kept as is, with a correct reason: the picker never picks "never" as best, and nothing reads its Diff. I verified this again in round 1; the reason is right. Accepted.
  • Reviewer 2, round 1 (non-blocking): the Add doc comment mixed two rules in three lines. Fixed. It is now two short sentences.
  • Reviewer 2, round 1 (non-blocking): the K3 baseline recomputed once per adapted option. Kept as is. The reason is right: the line is old code moved unchanged, and a cache is a separate change. It is also outside this PR's diff (the base moved to f27cf87f in the rebase).

Non-blocking

  • campaign/recommend.go:46 — the new Add doc says the option "is named in Disagree". Disagree is a field of Pick, the result of Result(), not of Picker. A reader still finds it. Doc precision only.

What I checked

  • Last review: round 1, c3b15632dc43838cac99da24012421f903485be9, 2026-11-24T09:14:14Z.
  • New commits read: 2.
    • ad25a17b "refactor(campaign): move the adapt report's recommendation choice out of the proxy" — the reviewed commit, rebased onto the new base f27cf87f. I diffed its tree against c3b15632 for the three PR files: identical. No code change to re-review.
    • cee61e68 "docs(campaign): address round-1 notes on the picker, add the package map rows" — read in full. Two doc-comment edits in campaign/recommend.go, two new rows in docs/design.md. No code change.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files; the inline file is empty).
  • New items since my last review, and what I did about each:
    • Reviewer 2's round-1 review (09:14:49Z): two non-blocking notes. Both checked above.
    • Author's comment (09:30:40Z): states the fixes and three kept-as-is items. Each checked above. The comment also reports build, vet, gofmt and tests green at this head (macOS, Go 1.27.4). I did not run them; live tests are not approved.
    • Commit cee61e68: verified the docs/design.md rows against the code. BuildSpec, Choose, Apply, Picker exist in campaign; AutoTune exists in predictive; the arm mapping is campaign/arms.go. Both rows' import claims hold: campaign and predictive import only library packages (config, internal/*, keepalive, kvcache, predictive, tenant), never proxy or proxyapp.
  • The PR diff at this head is the same three-file change I approved in round 1, plus the two doc edits. The rebase changed the base to f27cf87f; that commit is not part of this PR's diff.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.331 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 cee61e68bad1 approve

Verdict: approve

Findings

None. The new commit changes doc comments and docs/design.md only. No code path changed.

Earlier findings, status

  • Mine, round 1 (non-blocking): the Add doc comment mixed two rules in three lines. Fixed. cee61e68 splits it into two short sentences. The new text matches the code: "leads in gateway dollars while the gate disagrees, or the reverse" is exactly realBeats != GatePass.
  • Mine, round 1 (non-blocking): the K3 baseline recomputed once per adapted option. Kept as is. The reason is right: the line is old code, moved unchanged, and it sits outside this PR's diff now that the base moved to f27cf87f. A cache is a separate change. Accepted.
  • Reviewer 1, round 1 (non-blocking): Pick.GateDiff/Pick.GateCI read as zero when GateID == "". Fixed. Both fields now say "zero when GateID is """ (campaign/recommend.go:74-75). Checked myself: the fields are set only inside the gateID != "" branch, so the doc is true.
  • Reviewer 1, round 1 (non-blocking): Option.Diff unused for the "never" baseline. Kept as is, with a right reason: the picker never picks "never" as best, and nothing reads its Diff. Verified. Accepted.

Non-blocking

  • Agree with reviewer 1, round 2: campaign/recommend.go:46 — the Add doc says the option "is named in Disagree", but Disagree is a field of Pick (the result of Result()), not of Picker. A reader still finds it. Doc precision only.

What I checked

  • Last review: round 1, c3b15632dc43838cac99da24012421f903485be9, 2026-11-24T09:14:49Z.
  • New commits read: 5 in c3b15632..cee61e68 (the rebase abandoned the old shas):
    • ad25a17b "refactor(campaign): move the adapt report's recommendation choice out of the proxy" — this PR's commit, re-based. git diff c3b15632 ad25a17b is empty: the tree is identical to the commit I approved in round 1. No code to re-review.
    • cee61e68 "docs(campaign): address round-1 notes on the picker, add the package map rows" — read in full. Two doc edits in campaign/recommend.go, two rows in docs/design.md. No code change.
    • beb1292d, ba18d136, f27cf87f — the sibling stacked PRs' round-1 note fixes, now below this PR's base f27cf87f. Read all three diffs in full, although they are outside this PR's diff: doc and comment fixes, DefaultWeekdays now a function returning a copy (with a pinning test), a dead if ss := ...; true wrapper dropped in campaign/replay.go, and a dead idle loop dropped in the Apply test. Nothing that touches the picker or the adapt report.
  • Read in full: 3 reviews, 1 comment, 0 inline comments (from the three saved files; the inline file is empty).
  • New items since my last review, and what I did about each:
    • Reviewer 1's round-1 review (09:14:14Z): two non-blocking notes. Both statused above.
    • Author's comment (09:30:40Z): claims fixes in cee61e68 and three kept-as-is items. Each checked above. The comment also reports build, vet, gofmt and tests green at this head (macOS, Go 1.27.4). I did not run them; live tests are not approved. Its item "the bounds-test floor in proxy/campaign_test.go:32" maps to no finding in either round-1 review; it answers a note from outside this PR's conversation. Nothing to act on.
    • Reviewer 1's round-2 review (09:33:09Z): read. Its status calls match my own checks.
    • Commit cee61e68: verified the docs/design.md rows against the code. BuildSpec, Choose, Apply, Picker exist in campaign; AutoTune exists in predictive; the arm mapping is campaign/arms.go. The "no host code" claims hold: go list -deps shows predictive depends only on kvcache, and campaign on library packages only (config, kvcache, predictive, tenant, keepalive, schema, and internal/*) — never proxy or proxyapp.
  • The PR diff at this head (git diff f27cf87f..cee61e68) is the three-file change I approved in round 1, plus the two doc edits. No behavior difference remains.
  • Did not run builds or tests (live tests not approved). go list was used only to read the package graph.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
… of the proxy

Move the choice of the best option, the option that passes the builder's gate,
the gate and gateway-dollar disagreement line, and the summary sentence of
the adapt report into `campaign.Picker`. The proxy still builds the report
rows and the JSON.

No behaviour change.

Refs #1309
Closes #1302

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
…map rows

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid
amiddavid force-pushed the feat/mv-1302-campaign branch from cee61e6 to 9854ddf Compare November 24, 2026 09:37
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Output of scripts/release/approval-still-valid.sh 1334 cee61e68:

approved: cee61e68 (base f27cf87f9092) patch-id 861a7dc18812
head:     9854ddfe1666 (base b13939303bca) patch-id 45a09248d2b5
--- git range-diff
1:  ad25a17b = 1:  5d7ba01a refactor(campaign): move the adapt report's recommendation choice out of the proxy
2:  cee61e68 = 2:  3ada52d4 docs(campaign): address round-1 notes on the picker, add the package map rows
-:  -------- > 3:  9854ddfe docs(campaign): name Pick.Disagree in the Add doc
CHANGED: the head has new code or a conflict resolution. Each reviewer re-checks the delta.

Why CHANGED: the base moved too (#1333 got a comments-only commit), so the merge base differs, and the script only accepts comments-only with the same merge base. The range-diff shows commits 1 and 2 are equal and commit 3 (9854ddf) is one // line: Add doc now names Pick.Disagree. No code changed. Reviewers: please re-check that one line.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Approval check by hand: comments only. The approvals of cee61e68 still hold at 9854ddfe.

approval-still-valid.sh prints CHANGED only because the base moved. #1333 got a comment-only commit, then this PR was restacked on it. I compared this PR's own diff before and after:

diff <(git diff f27cf87f cee61e68) <(git diff b13939303bca 9854ddfe1666)

The only difference is one line in campaign/recommend.go, a Go // comment. The blob index line changes with it.

< +// ... is named in Disagree.
> +// ... is named in `Pick.Disagree` (from `Result()`).

Secret scan by hand (vault-radar, offline) on the files changed at b13939303bca and 9854ddfe1666: 0 findings.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid
amiddavid changed the base branch from feat/mv-1302-campaign-arms to main November 24, 2026 12:09
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Merged origin/main into this branch (merge commit 2f97803c, signed off). #1332 and #1333 are in main now.

Conflict: one file, proxy/adaptcache.go, 2 hunks.

  • Main still holds the old inline choice code in adaptReport (disagree, disID, bestID, gateID, failID and their updates). This PR replaces that code with campaign.Picker.

Resolution: I kept this branch's side of the file.

  • Main has no other change to adaptcache.go: git diff origin/main..<previous base> -- proxy/adaptcache.go is empty.
  • So the resolved file is main plus this PR's own diff.

The PR's changed lines are unchanged. The +/- lines of git diff b13939303bca 9854ddfe1666 and git diff origin/main...2f97803c are identical.

Tested on macOS, Go 1.27.4: build, vet and gofmt -l clean. go test of ./campaign ./proxy ./cmd/... ./kvcache ./tenant pass. The base is now main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

refactor(predictive): move campaign, replay and fidelity math out of the proxy

1 participant