Repository navigation
Description
Two concurrent sessions on a shared Engine can cross-stamp each other's schedule origin, and via captureScheduleOwner (ADR 0100 decision 6) the schedule inherits the wrong owner principal.
The defect
agent.SessionOriginScheduleManager holds the current origin session id in a process-wide sync/atomic.Pointer[session.SessionID]. Engine.startRun binds it once per run; CreateSchedule reads it later, when the model actually calls the Schedule tool.
A shared Engine is safe for concurrent use, so two runs can interleave between those two points:
- Run A (
session-a) starts → bindssession-a. - Run A blocks on its provider call, before dispatching its
Schedulecreate. - Run B (
session-b) starts → bindssession-b, overwriting the process-wide value. - Both runs dispatch their creates. Both read
session-b.
The atomic access prevents a data race; it does not preserve attribution. Mutate-serial dispatch orders tool calls within one run, not across concurrent runs.
Consequence beyond mis-delivery: captureScheduleOwner resolves a created schedule's owner from OriginSessionID, so a cross-stamped origin means the schedule is owned by the other session's owner. validateScheduleOrigin only checks that the origin session exists, not that the caller owns it, so nothing downstream catches it. Fire results are then delivered into the wrong conversation.
Related: CWE-863 (incorrect authorization), OWASP API1:2023 (broken object-level authorization).
Where it came from
-
Introduced: 5154b49 (2026-07-27, feat(schedule): fire-result delivery — a scheduled fire reports back into the originating chat, live in mecatui #311, fire-result delivery / ADR 0075).
engine/agent/sessionorigin.gowas created there and has not been modified since — the file onmaintoday is byte-identical. Born with the defect; not a regression. -
The reasoning that missed it is still in the file's doc-comment on
main:// Why race-free: Schedule tool is ReadOnly()==false → mutate-serial; the engine
// drives ONE session's run at a time → the atomic pointer always names the
// executing session. Works for shared AND per-session engines.Both premises hold and the conclusion still does not follow: mutate-serial orders calls within a run, and "one session's run at a time" is false for a shared
Engine. The comment asserts the property rather than deriving it, and explicitly claims the shared-engine case — the one that breaks. -
Became reachable: 447322e (2026-07-28, fix(schedule): the Schedule tool on every schedule-capable session (ADR 0076) #314, ADR 0076) moved the schedule seam onto a store-shaped manager available before
buildEngine, soregisterScheduleToolfires during build-time assembly and the shared catalog carries theScheduletool. That is what put two sessions behind oneEngineand one wrapper. Before it, only per-session engines had the tool, each with its own wrapper instance. -
Exposure window on
main: 2026-07-28 → present.
Secondary defect, same root cause
registerScheduleTool also assigns a.scheduleOriginBinder = wrapper, and sessionEngineFactory receives assets catalogAssets by value with the returned closure capturing that single copy — so every per-session assembly writes the same field. Two concurrent CreateSession calls race on a non-atomic interface field; the loser's engine can be bound to the other session's wrapper while its catalog holds its own, yielding a silently dropped delivery (the bind lands on a wrapper the tool never calls) rather than a cross-stamp.
This is a code read, not a reproduction. It needs no separate fix — it is the same class (shared mutable wiring state) and the same removal eliminates it.
Fix direction
Attribution is request-scoped, so carry it on the run context rather than in shared wrapper state:
startRunstamps the executing session id onto the cancellation-derived context (unexported writer). Both run-entry seams (Run,ResumeApproval) route throughstartRun, so normal runs and awaiting-approval resumes attribute identically.ScheduleTool.createreads it when it builds theScheduleSpec— the single site that assigns the field. An unbound context yields the empty origin, which means no delivery, never delivery to an arbitrary session.- The wrapper is then a stateless one-field decorator whose omission fails silently (an empty origin is also the legitimate originless posture, so miswiring is indistinguishable from correct wiring), so it goes away entirely and composition passes the
port.ScheduleManagerstraight through.
Removes agent.OriginBinder, agent.Deps.OriginBinder, and agent.SessionOriginScheduleManager — breaking pre-v1, exported engine surface net shrinks by fourteen symbols and gains none.
Acceptance
- Two concurrent runs on one shared
Engine, each creating a schedule, stamp their own session ids — with a regression test that provably fails on the atomic-pointer implementation (forced interleaving, not an opportunistic race). - An unbound context yields the empty origin rather than a stale or borrowed one.
- A model-supplied
originargument still cannot reachOriginSessionID, and the origin stays absent from every model-visible surface (tool result text, system prompt). -
RunandResumeApprovalattribute identically. - No composition step can be omitted such that origin stamping silently stops happening.
Notes for #373
#373's first acceptance criterion — "A schedule carries the authority of its creator, captured when it is created and replayed at every fire, with no live lookup that can fail or drift" — sits directly on top of this. While origin capture can cross-stamp, "authority of its creator" is wrong at the point of capture, so this is a precondition for that epic rather than part of it.
Activity
fix(agent): bind schedule origin to run context
fix(agent): bind schedule origin to run context