Skip to content

fix(agent): bind schedule origin to run context - #484

Merged
jhrozek merged 2 commits into
mainfrom
fix/session-origin-run-context
Aug 13, 2026
Merged

jhrozek merged 2 commits into
mainfrom
fix/session-origin-run-context

Conversation

@jhrozek

@jhrozek jhrozek commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Closes #482.

Schedule-origin attribution moves from mutable state on a shared wrapper to the run context, and the wrapper is deleted.

The bug

SessionOriginScheduleManager held the origin session id in a process-wide atomic.Pointer. startRun bound it; CreateSchedule read it later, when the model called the Schedule tool. A shared Engine is safe for concurrent use, so two runs interleave between those points and both creates read the last-bound id. Atomic access prevented the data race, not the mis-attribution — and via captureScheduleOwner the schedule then inherits the other session's owner principal.

Present on main since 447322e (2026-07-28) made the shared catalog carry the tool; introduced latent in 5154b49 (2026-07-27). Full archaeology in #482.

The fix

  • startRun stamps the executing session id onto the cancellation-derived context via an unexported withSessionOrigin. Both run-entry seams (Run, ResumeApproval) route through it, so normal runs and awaiting-approval resumes attribute identically.
  • ScheduleTool.create reads it when building the ScheduleSpec — the single site that assigns OriginSessionID. scheduleArgs has no origin field, so nothing model-supplied has anywhere to enter. An unbound context yields the empty id: no delivery, never delivery to an arbitrary session.
  • The wrapper is removed, not made stateless. Without the binder state it is one field assignment behind ten delegating methods, and omitting it fails silently — an empty origin is also the legitimate originless posture, so a composition root that forgets to wrap looks exactly like one that does not. Stamping inside the thing that builds the spec cannot be forgotten. Composition passes the port.ScheduleManager straight through.

Also fixes a second defect of the same class that #482 records: assets.scheduleOriginBinder was written by every per-session catalog assembly over one shared struct, so concurrent CreateSession calls raced a non-atomic interface field. The field is gone.

Engine API (breaking, pre-v1)

Removes agent.OriginBinder, agent.Deps.OriginBinder, and agent.SessionOriginScheduleManager (constructor + ten methods). Nothing is added — the exported surface net shrinks by fourteen symbols. engine/api/agent.txt regenerated, engine/CHANGELOG.md classified Changed per COMPATIBILITY.md with the migration note (delete the Deps.OriginBinder assignment; pass the manager straight to the tool constructors).

Nothing exported replaces it: an origin is acquired by running under Engine.Run, and an out-of-band create is originless by design (ADR 0075 decision #1), which is what the REST/gRPC surface already does. An exported writer would let an embedder name any session as the origin, and validateScheduleOrigin only checks that the session exists, not that the caller owns it. ADR 0104 records that if it is ever exported, an ownership check must land first.

Tests

TestScheduleTool_ConcurrentSharedEngineRunsDoNotCrossStamp forces the interleaving with a provider-blocking rendezvous — run A parks inside the provider until run B has entered — so it fails deterministically on the atomic-pointer implementation rather than racing opportunistically. It bounds both rendezvous points and bails out silently if the test goroutine has already returned, so a catastrophic failure surfaces as a named error instead of a post-completion log panic.

The three TestFireDelivery_Scenario1_* tests now drive the tool directly. One arm changed meaning and is worth a reviewer's eye: the old "unbound context clears a pre-populated forged spec" case exercised the wrapper's overwrite, a seam that no longer exists, so it is now "an unbound tool create yields the empty origin". Narrower, but it is the property that actually exists — OriginSessionID has exactly one writer, and protoToScheduleSpec never maps the field from the wire.

Docs

New ADR 0104 (supersedes ADR 0075's origin-binding mechanism only; the OriginSessionID contract, delivery queue, turn-boundary drain, and note rendering all stand), with a forward pointer to ADR 0100 since that frozen record still names the removed binder. docs/architecture.md, IMPLEMENTATION-NOTES.md, and the captureScheduleOwner comment describe the new mechanism. AC3.3 in the landed schedule-shared-catalog plan named the removed wiring while the test verifying it was reworded — the AC now states the mechanism change explicitly, with its observable claim and verify: line unchanged.

Verification

task lint (0 issues across all seven module runs), task test (full suite, incl. apicheck and the engine/arch layering DAG), task docs:check --strict (0 broken links/anchors/orphans), and go run ./cmd/mecademo prints a complete offline session. Rebased onto origin/main.

Reviewed by a seven-agent panel across spec / standards / domain axes before this branch was finalised; every ship-blocker-class finding is addressed in this commit.

🤖 Generated with Claude Code

@jhrozek jhrozek added the bug Something isn't working label Aug 12, 2026
@jhrozek
jhrozek force-pushed the fix/session-origin-run-context branch from 62b413e to f275436 Compare August 13, 2026 11:17
jhrozek and others added 2 commits August 13, 2026 14:58
Two concurrent runs on a shared Engine could interleave between the
OriginBinder's per-run bind and the Schedule tool's execution, so one
session could stamp another session's id onto a schedule — and, via
captureScheduleOwner, its owner principal too. Atomic access prevented
the data race but not the mis-attribution.

Bind the executing session id onto the cancellation-derived context in
startRun, and have ScheduleTool.create read it there when it builds the
ScheduleSpec. Both run-entry seams (Run and ResumeApproval) route through
startRun, so normal runs and awaiting-approval resumes inherit identical
attribution.

The spec literal in create is the only site that assigns
OriginSessionID, and scheduleArgs has no origin field, so neither
model-supplied args nor a stale binding can supply one. An unbound
context yields the empty id, which means no delivery rather than
delivery to an arbitrary session.

Remove the SessionOriginScheduleManager wrapper outright rather than
keep it stateless. Once the binder state is gone it is a single field
assignment behind ten delegating methods, and omitting it fails
SILENTLY: an empty origin is also the legitimate originless posture, so
a composition root that forgets to wrap is indistinguishable from one
that does not. Stamping at the construction site cannot be forgotten,
and composition passes the port.ScheduleManager straight through.

withSessionOrigin is unexported and replaces no exported surface. An
origin is acquired by running under Engine.Run; an out-of-band create is
originless by design (ADR 0075 decision #1) and goes to the manager
directly, as Service.CreateSchedule already does. Exporting later is
Added (minor); un-exporting later would be breaking. Net: fourteen
engine symbols removed, none added.

The regression test forces the interleaving through a provider-blocking
rendezvous rather than racing opportunistically, so it fails
deterministically on the atomic-pointer implementation.

Closes #482. See ADR 0104.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
@jhrozek
jhrozek force-pushed the fix/session-origin-run-context branch from 094dfeb to 297287c Compare August 13, 2026 13:14
@jhrozek
jhrozek merged commit 85b9d6a into main Aug 13, 2026
15 checks passed
@jhrozek
jhrozek deleted the fix/session-origin-run-context branch August 13, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent sessions on a shared Engine cross-stamp schedule origins, so a schedule can inherit another user's owner

2 participants