Skip to content

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

Closed

Description

@jhrozek

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:

  1. Run A (session-a) starts → binds session-a.
  2. Run A blocks on its provider call, before dispatching its Schedule create.
  3. Run B (session-b) starts → binds session-b, overwriting the process-wide value.
  4. 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.go was created there and has not been modified since — the file on main today 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, so registerScheduleTool fires during build-time assembly and the shared catalog carries the Schedule tool. That is what put two sessions behind one Engine and 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:

  • startRun stamps the executing session id onto the cancellation-derived context (unexported writer). Both run-entry seams (Run, ResumeApproval) route through startRun, so normal runs and awaiting-approval resumes attribute identically.
  • ScheduleTool.create reads it when it builds the ScheduleSpec — 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.ScheduleManager straight 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 origin argument still cannot reach OriginSessionID, and the origin stays absent from every model-visible surface (tool result text, system prompt).
  • Run and ResumeApproval attribute 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

  1. added
    bug
    on Aug 12, 2026
  2. added 2 commits that reference this issue on Aug 13, 2026
    f275436
    8be4166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions