Skip to content

feat(server): expected_run_id on controls, and strict steer - #828

Merged
samuv merged 2 commits into
sdk/04-run-idfrom
sdk/05-expected-run-id
Aug 31, 2026
Merged

samuv merged 2 commits into
sdk/04-run-idfrom
sdk/05-expected-run-id

Conversation

@samuv

@samuv samuv commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #827 → #826 → #825 → #823 → #822. Review those first.

What

PR 5 of 9 in the #821 server-enabler stack. Implements Scenario 5 — AC5.1–AC5.5 — per ADR 0245.

Optional expected_run_id on 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.Seq restarts every run and can't tell the two apart; until run ids existed there was no way to say "this one."

Empty expected_run_id is the legacy path exactly, so every deployed mecatui is unaffected. A non-empty value that doesn't 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:

Path Source
live fast path the *agent.Run's own id
cross-process resume the loaded session's id (there, 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's 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'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_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 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:

  • HTTP control endpoints → 409 application/problem+json, code stale_run_control
  • steer frame → 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 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

AC Test
5.1 matching id succeeds TestSDKServerEnablers_Scenario5_MatchingExpectedRunIDSucceeds
5.2 stale control can't touch a newer run TestADR_0245_StaleControlCannotTouchNewerRun
5.3 omitted id unchanged TestSDKServerEnablers_Scenario5_OmittedExpectedRunIDUnchanged
5.4 strict steer never promotes TestADR_0245_StrictSteerNeverPromotes
5.5 unqualified steer still promotes TestSDKServerEnablers_Scenario5_PromotionRetainedOnRawAPI
— typed over the wire TestSDKServerEnablers_Scenario5_StaleControlOverHTTPIsTyped

AC5.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, with api:update output and a changelog entry.

Verification

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.

Widening three Service signatures touched a number of existing call sites (ACP, scheduler, tests); all pass "" for the legacy behaviour.

Refs #821.

🤖 Generated with Claude Code

@samuv samuv self-assigned this Aug 28, 2026

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@samuv
samuv force-pushed the sdk/05-expected-run-id branch from 7c3ce27 to 1680c9a Compare August 31, 2026 08:11
@samuv

samuv commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto the updated stack, plus one self-correction to this PR's own golden-table change (1680c9a).

Replacing count-equality with a shadow check was right, but the shadow check as written could not fail. classifyError returns the first row matching errors.Is, and a specific sentinel wraps the general one — so errors.Is(specific, general) is true whether the row resolved correctly or was shadowed by the very row it wraps. It passed in exactly the case it was added to catch. Now compared by pointer identity.

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 registry row 25 ("failed_step_retry_ineligible") is SHADOWED.

No other changes — the expected_run_id behaviour is untouched.

@samuv
samuv force-pushed the sdk/05-expected-run-id branch from 1680c9a to b093b78 Compare August 31, 2026 08:50
@samuv
samuv force-pushed the sdk/05-expected-run-id branch from b093b78 to 40a7813 Compare August 31, 2026 08:53
@samuv
samuv force-pushed the sdk/05-expected-run-id branch from 40a7813 to 3131a45 Compare August 31, 2026 09:02
@samuv
samuv force-pushed the sdk/05-expected-run-id branch from 3131a45 to a36da83 Compare August 31, 2026 09:16
@samuv
samuv force-pushed the sdk/05-expected-run-id branch from a36da83 to 2ebd64b Compare August 31, 2026 10:18
samuv added 2 commits August 31, 2026 12:31
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]>
@samuv
samuv force-pushed the sdk/05-expected-run-id branch from 2ebd64b to 350ab8b Compare August 31, 2026 10:31
@samuv
samuv merged commit e9341a0 into main Aug 31, 2026
9 of 29 checks passed
@samuv
samuv deleted the sdk/05-expected-run-id branch August 31, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants