Skip to content

[Bug] Gateway reply-queue state not flushed on restart — stale queued replies drain to wrong Matrix room after restart #91613

Closed

Description

@substrateindependent

Summary

Gateway reply-queue state is not flushed on restart. Queued outbound Matrix replies targeting a specific room persist across a gateway process restart and drain to their destination after the gateway comes back up — even when the originating session was isolated and completed long before the restart.

Version

2026.4.25 — reproduced on two separate Mac mini fleet agents (Charlie and Johnny), both running LaunchAgent-managed gateways.

Observed behavior

2026-06-08, ~07:00–07:56 CDT

Charlie (Mac mini, LaunchAgent):

  • 07:00:02 — Morning Briefing cron session starts (session f8986989, targeting Glenn's DM room)
  • 07:14:46 — long-running session warning, age=131s
  • 07:15:06 — UND_ERR_SOCKET fetch failure mid-model-call (Anthropic provider)
  • 07:16 — gateway self-restarts (process exits and launchd relaunches)
  • 07:34:01 — liveness warning: active + queued sessions both keyed to @johnny DM room (wrong room — Charlie's session was targeting Glenn's DM)
  • 07:34:47–07:34:58 — ReplyRunAlreadyActiveError loop; queued reply drains into Johnny's DM with Charlie's Morning Briefing tool narration as payload (memory searches, Linear issue lookups, CFO briefing reasoning)

Johnny (Mac mini, LaunchAgent):

  • Same symptom independently: post-restart boot session narration (⏰ Cron, 🧩 Linear Get Issue, 📊 Session Status, 🛠️ exec, 📖 Read, 📝 Edit) leaked into a different agent's DM room through multiple restart cycles between 07:40–07:55 CDT.
  • Second restart (launchctl kickstart) at 07:56 CDT finally stabilized after the queued boot session drained.

Key diagnostic finding

After both restarts, sessions.json showed 0 pending sessions. The reply-queue state is stored separately from the sessions index — meaning the standard sessions-store check does not reveal pending outbound message queue entries. The reply queue appears to persist in a separate store that survives process restart.

Why sessionTarget: isolated does NOT mitigate this

All fleet cron jobs use sessionTarget: isolated. Both affected cron sessions were isolated. The reply-queue corruption still occurred because isolated governs per-run context (not outbound message queue state). These are orthogonal.

Impact / severity

  • Any agent can have outbound messages queued to a wrong room after a network error or restart
  • Content leaking can include tool narration, reasoning, memory search results, Linear data — any assistant output from the corrupted session
  • In this incident: CFO briefing context (sensitive business reasoning) delivered to a peer agent's DM
  • Blast radius scales with session content: a session with GWS auth, customer PII, or credentials could produce a serious information exposure

Proposed fixes (for upstream evaluation)

  1. Flush pending outbound queue on gateway startup — discard any queued replies that predate the current boot. Safest default; may lose legitimate in-flight replies during brief restarts, but correct behavior.
  2. Per-room session lock — enforce that a queued reply can only drain if the room key matches the currently active session key. sessionScope: per-room partially addresses this at config level but should be enforced at the transport layer.
  3. TTL on queued replies — expire queued replies after N minutes (e.g., 5 min). Replies surviving a restart are almost certainly stale.

Current workaround

sessionScope: per-room on the matrix channel config prevents wrong-room drain even if the queue survives restart (config-level mitigation, not a transport-layer fix).

Steps to reproduce

  1. Run a cron/isolated session on an agent with Matrix configured
  2. Mid-session, trigger a network error (UND_ERR_SOCKET or similar) that causes the gateway to self-restart
  3. After gateway restart, observe sessions.json shows 0 pending sessions
  4. Wait: queued reply from the pre-restart session drains to its (potentially wrong) destination room
  5. Check logs: ReplyRunAlreadyActiveError loop confirms the stale reply is retrying

Activity

  1. clawsweeper commented on Jun 9, 2026

    @clawsweeper
    Contributor

    Thanks for the context here. I did a careful shell check against current main, and this is already implemented.

    Current main fixes the reported wrong-room replay path: the merged cron resolver now refuses keyless implicit delivery inherited from the shared agent-main bucket before send/enqueue, and issue-specific tests cover the refusal plus dispatch cleanup. The fix is on main only; the latest listed releases were published before the fix commit.

    I found the merged PR that appears to have closed this: #91685: fix(cron): refuse keyless implicit isolated cron delivery inherited from shared agent-main bucket.

    So I’m closing this as already implemented rather than keeping a duplicate issue open.

    Review details

    Best possible solution:

    Keep the current-main resolver refusal for ambiguous keyless cron delivery and track any broader queue TTL, boot-generation, or transport room-gate design only through separate evidence-backed follow-ups.

    Do we have a high-confidence way to reproduce the issue?

    Yes from source and tests for the historical path: a keyless implicit cron could inherit the shared main bucket target, and the generic recovery path replays queued targets verbatim. Current main no longer reproduces that path because the resolver now returns ok:false before delivery can enqueue.

    Is this the best way to solve the issue?

    Yes. The merged fix is the narrow owner-boundary solution for the observed root cause because the shared resolver refuses the ambiguous target once, and dispatch, failure notification, and preview all consume the existing ok:false contract.

    Security review:

    Security review cleared: The security-sensitive wrong-room disclosure path reported here is addressed on current main by refusing ambiguous keyless cron delivery before send/enqueue.

    AGENTS.md: found and applied where relevant.

    What I checked:

    • Current source refuses ambiguous keyless cron delivery: Current main returns ok:false when an implicit keyless cron would inherit the shared agent-main bucket's last recipient, with an error that names wrong-room replay after restart and instructs operators to set an explicit target or session context. (src/cron/isolated-agent/delivery-target.ts:303, 0a3e0d081dc4)
    • Dispatch does not send or enqueue refused targets: The dispatch !ok gate handles unresolved delivery before direct sending, marks delivery-target failure, and cleanup runs without calling the outbound send path for the refused target. (src/cron/isolated-agent/delivery-dispatch.ts:1220, 0a3e0d081dc4)
    • Issue-specific resolver regression coverage: The added regression test asserts that a keyless implicit cron inheriting another conversation's shared main-bucket recipient now returns ok:false, while explicit targets and scoped/session-bearing cases remain allowed. (src/cron/isolated-agent/delivery-target.issue-91613.test.ts:208, 0a3e0d081dc4)
    • Run-layer wiring regression coverage: The run-layer test verifies the resolver's ok:false refusal is passed through and that sessionTarget resolves into the resolver session key so scoped crons are not misread as keyless. (src/cron/isolated-agent/run.issue-91613-preflight.test.ts:24, 0a3e0d081dc4)
    • Fix provenance: git show identifies the merged fix commit as d4833e2, committed 2026-06-19T10:05:07Z, with the title fix(cron): refuse keyless implicit isolated cron delivery inherited from shared agent-main bucket. (d4833e27c775)
    • Current-main containment: GitHub compare reports current main 0a3e0d0 is ahead of the fix commit with merge_base_commit equal to d4833e2. (d4833e27c775)

    Likely related people:

    • nxmxbbd: GitHub PR metadata and commit history tie this person to the merged keyless implicit cron delivery-target fix. (role: fix author; confidence: high; commits: d4833e27c775; files: src/cron/isolated-agent/delivery-target.ts, src/cron/isolated-agent/delivery-dispatch.ts, src/cron/isolated-agent/run.ts)
    • steipete: Recent GitHub commit history shows repeated cron delivery-target and outbound delivery lifecycle work on the affected resolver and recovery surfaces. (role: feature-history owner; confidence: high; commits: c4f0da00a9fe, 2ead1502c9bf, 5a6eddf5d0d5; files: src/cron/isolated-agent/delivery-target.ts, src/infra/outbound/delivery-queue-recovery.ts)
    • vincentkoc: The merged PR comments show this member requested automerge, and the release/current-main metadata puts him close to the final mainline state used for cleanup. (role: reviewer and automerge requester; confidence: medium; commits: 0a3e0d081dc4; files: src/cron/isolated-agent/delivery-target.ts, src/cron/isolated-agent/delivery-dispatch.ts, src/cron/isolated-agent/run.ts)
    • 849261680: Recent GitHub commit history shows adjacent recovery work preserving delivery retry state in src/infra/outbound/delivery-queue-recovery.ts. (role: adjacent outbound recovery contributor; confidence: medium; commits: 75c1790b504c; files: src/infra/outbound/delivery-queue-recovery.ts)

    Codex review notes: model internal, reasoning high; reviewed against 0a3e0d081dc4; fix evidence: merged PR #91685, commit d4833e27c775, main fix timestamp 2026-06-19T10:05:07Z.

  2. added
    P1High-priority user-facing bug, regression, or broken workflow.
    impact:securitySecurity boundary, credential, authz, sandbox, or sensitive-data risk.
    impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.
    impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.
    issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.
    clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.
    clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.
    clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.
    clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.
    clawsweeper:needs-security-reviewClawSweeper marked this issue as needing security-sensitive review.
    clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.
    on Jun 9, 2026
  3. locked as resolved and limited conversation to collaborators on Jun 25, 2026
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

    P1High-priority user-facing bug, regression, or broken workflow.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:needs-security-reviewClawSweeper marked this issue as needing security-sensitive review.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.impact:securitySecurity boundary, credential, authz, sandbox, or sensitive-data risk.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions