Repository navigation
feat(server): expected_run_id on controls, and strict steer - #828
Conversation
jhrozek
left a comment
There was a problem hiding this comment.
Verified the guard resolves against the actual receiving run (run.RunID() live, sess.RunID() on resume), the strict-steer guard is correctly placed at the promote branch only, the golden-table gate change is a genuine tightening (catches shadowing via errors.Is, not just row count), expected_run_id is correctly left as a bare string (no third meaningful empty state to lose here, unlike Event.RunID), and the cancel endpoint's best-effort body decode preserves legacy no-body callers as claimed. No findings — approving.
7c3ce27 to
1680c9a
Compare
|
Rebased onto the updated stack, plus one self-correction to this PR's own golden-table change ( Replacing count-equality with a shadow check was right, but the shadow check as written could not fail. Found while implementing the generic registry-wide version of the same gate that #825's review asked for; both are fixed. Mutation-verified: swapping the two real rows now reports No other changes — the |
1680c9a to
b093b78
Compare
b093b78 to
40a7813
Compare
40a7813 to
3131a45
Compare
3131a45 to
a36da83
Compare
a36da83 to
2ebd64b
Compare
PR 5 of the #821 server-enabler stack. Implements Scenario 5 of docs/acceptance/sdk-server-enablers.md (AC5.1-AC5.5) per ADR 0245. This is the PR that actually closes the bug PR 4 made addressable. Controls are addressed at a SESSION, so a control still in flight when a run ends lands on whatever run started next — approving a tool call the user never saw, or cancelling work they did not ask to stop. Event.Seq restarts every run and so cannot tell the two apart; until run ids existed there was no way to say "this one". expected_run_id is optional on approve, cancel, steer, and steer_cancel. Empty is the legacy path exactly — the control applies to whatever run is current — so every deployed mecatui is unaffected. A non-empty value that does not name the run the control would affect is refused with the new ErrStaleRunControl and the current run is left untouched. COMPARE AGAINST THE RUN, NOT THE SESSION. The guard takes the id of the run that would ACTUALLY receive the control: a live *agent.Run's own id on the fast path, the loaded session's id on the cross-process resume path (where the persisted session IS the run). Those two differ after precisely the terminal race being refused, so comparing against the aggregate everywhere would silently permit the case this exists to catch. That is why the engine gains Run.RunID() — a caller holding a *Run can now ask which run it holds instead of inferring it. STRICT STEER. An unqualified steer that loses the terminal race is PROMOTED into a fresh follow-up run, and that is right: the operator meant "say this to the agent", and a new run says it. A steer naming a specific run did not ask to start a different one, so with expected_run_id set the promote path is refused instead. The guard sits at the promote branch, not the top, because expected_run_id forbids PROMOTION — it does not forbid steering the run it names. AC5.5 pins that the unqualified path still promotes; losing that would silently drop operator input. Steer is gRPC-only (ADR 0232 deferred an HTTP route and none exists). The plan said "strict HTTP steer", which was wrong; corrected in this commit. WHERE THE TYPED ERROR SURFACES, HONESTLY. Converse's approve and cancel frames are fire-and-forget: the stream carries no per-control ack to put an error on. So a stale frame there is refused and logged (staleStreamControl) rather than tearing down the whole stream over a race the client cannot avoid; the operator-visible half is a diagnostic, because nothing in the event taxonomy reports a refused control. The typed ErrStaleRunControl reaches clients on the HTTP control endpoints as a 409 application/problem+json with code "stale_run_control", and the steer frame reports too_late on the ack channel it already has. The cancel endpoint gained an OPTIONAL body, decoded best-effort: it predates the field and must keep accepting an empty body, so a missing or malformed body leaves the id empty (the legacy behaviour) rather than rejecting callers that send none. PR 2's golden-table gate fired on the 50th registry row, which is the gate working. Its strict count-equality was the wrong invariant though: the registry legitimately GROWS, and asserting the count turns every addition into a failure the next person "fixes" by bumping a number — teaching people to edit the proof instead of read it. It now asserts what actually matters: no pre-refactor sentinel may be lost, reclassified, or SHADOWED by a more general row (which would pass the mapping loop by accident and hand clients the wrong stable code). Engine API: agent.Run.RunID() — Added/minor, with api:update output and a changelog entry. Gates: task lint 0 issues all modules; task api:check green; go test -race green on root and engine; docs/lint green; mecademo prints a full offline session. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
Follow-up to this PR's own golden-table change. Replacing count-equality with a shadow check was the right move; the shadow check as written could not fail. classifyError returns the first row whose sentinel matches errors.Is. A specific sentinel WRAPS the general one, so errors.Is(specific, general) is true — which means the assertion held when the row resolved correctly AND when it had been shadowed by the very row it wraps. It passed in precisely the case it was added to catch. Only pointer identity separates the two. Verified by mutation on the sibling registry-wide gate: swapping the two real rows now reports "registry row 25 (failed_step_retry_ineligible) is SHADOWED" instead of passing. The golden-table check is kept alongside the registry-wide one rather than folded into it: this one additionally covers sentinels that predate the registry, which is the property the golden table exists for. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
2ebd64b to
350ab8b
Compare
What
PR 5 of 9 in the #821 server-enabler stack. Implements Scenario 5 — AC5.1–AC5.5 — per ADR 0245.
Optional
expected_run_idon approve / cancel / steer / steer_cancel, plus strict steer.This is the PR that closes the bug
PR 4 made run identity addressable; this one uses it. Controls are addressed at a session, so a control still in flight when a run ends lands on whatever run started next — approving a tool call the user never saw, or cancelling work they didn't ask to stop.
Event.Seqrestarts every run and can't tell the two apart; until run ids existed there was no way to say "this one."Empty
expected_run_idis the legacy path exactly, so every deployedmecatuiis unaffected. A non-empty value that doesn't name the run the control would affect is refused with the newErrStaleRunControl, and the current run is left untouched.Compare against the run, not the session
The guard takes the id of the run that would actually receive the control:
*agent.Run's own idThose two differ after precisely the terminal race being refused — so comparing against the aggregate everywhere would silently permit the case this exists to catch. That's why the engine gains
Run.RunID(): a caller holding a*Runcan now ask which run it holds instead of inferring it.Strict steer
An unqualified steer that loses the terminal race is promoted into a fresh follow-up run, and that's right — the operator meant "say this to the agent," and a new run says it.
A steer naming a run did not ask to start a different one, so with
expected_run_idset the promote path is refused instead. The guard sits at the promote branch, not the top, becauseexpected_run_idforbids promotion — it doesn't forbid steering the run it names.AC5.5 pins that the unqualified path still promotes. Losing that would silently drop operator input.
Steer is gRPC-only — ADR 0232 deferred an HTTP route and none exists. The plan said "strict HTTP steer", which was wrong; corrected here.
Where the typed error surfaces — stated honestly
Converse's approve and cancel frames are fire-and-forget: the stream carries no per-control ack to put an error on. So a stale frame there is refused and logged (
staleStreamControl) rather than tearing down the whole stream over a race the client cannot avoid. The operator-visible half is a diagnostic, because nothing in the event taxonomy reports a refused control.The typed error reaches clients where it can:
409application/problem+json, codestale_run_controltoo_lateon the ack channel it already hasThe cancel endpoint gained an optional body, decoded best-effort: it predates the field and must keep accepting an empty body, so a missing or malformed body leaves the id empty rather than rejecting callers that send none.
A gate from PR 2 fired, and I changed the gate
The golden-table check tripped on the 50th registry row. That's the gate working — but its strict count-equality was the wrong invariant. The registry legitimately grows, and asserting the count turns every future addition into a failure the next person "fixes" by bumping a number — teaching people to edit the proof instead of read it.
It now asserts what actually matters: no pre-refactor sentinel may be lost, reclassified, or shadowed by a more general row — that last one would pass the mapping loop by accident and hand clients the wrong stable code.
Acceptance criteria
TestSDKServerEnablers_Scenario5_MatchingExpectedRunIDSucceedsTestADR_0245_StaleControlCannotTouchNewerRunTestSDKServerEnablers_Scenario5_OmittedExpectedRunIDUnchangedTestADR_0245_StrictSteerNeverPromotesTestSDKServerEnablers_Scenario5_PromotionRetainedOnRawAPITestSDKServerEnablers_Scenario5_StaleControlOverHTTPIsTypedAC5.2's test is the one worth reading: it runs two real runs back to back, then proves the stale cancel is refused and that the newer run still completes on its own terms.
Engine API
agent.Run.RunID()— Added = minor, withapi:updateoutput and a changelog entry.Verification
task lint0 issues all modules ·task api:checkgreen ·go test -race ./...green on root and engine ·docs/lintgreen ·mecademoprints a full offline session.Widening three
Servicesignatures touched a number of existing call sites (ACP, scheduler, tests); all pass""for the legacy behaviour.Refs #821.
🤖 Generated with Claude Code