Skip to content

[Bug]: message.dispatch is accepted on deleted and archived threads, starting live provider turns on hidden threads #41220

Open

Description

@gpu004

What I observed

Orchestrator.dispatchMessage (apps/server/src/orchestration-v2/Orchestrator.ts:7126) never checks projection.thread.deletedAt !== null or projection.thread.archivedAt !== null for normal message.dispatch commands (it only checks them inside four internal continuation branches: steerFollowUpOfRunId, manualContinuationOfRunId, usageLimitContinuationOfRunId, and restartContinuationOfRunId).

Because the WebSocket RPC path (ws.ts → ThreadMessageIntake.dispatchCommand → ThreadManagementService.dispatch → Orchestrator.dispatch) also has no deletedAt or archivedAt check:

  1. A message.dispatch arriving for a deleted thread is accepted, commits domain events, leaves deletedAt non-null, and enqueues provider-turn.start — running a real paid agent turn in the workspace on a deleted, invisible thread that the user has no UI way to see or stop.
  2. A message.dispatch arriving for an archived thread is accepted, leaves archivedAt non-null, and runs a live provider turn. If a second message is queued while that turn runs, startNextQueuedRunOnce (Orchestrator.ts) refuses to start it because archivedAt !== null, leaving the second message permanently stranded in queued.

Reproduction

  • Commit: main @ 9d3054a7bb
  • Environment: macOS 27.2.1, Node v26.13.1

Reachable real-world triggers:

  • Open the same thread on two clients/tabs/devices (e.g., desktop and mobile). Delete or archive the thread on one client, then tap Send (or approve a pending guest message) on the other.
  • Client reconnect resending an in-flight message.dispatch (packages/client-runtime/src/operations/commands.ts:302) after the thread was deleted or archived elsewhere.
  • TOCTOU race in ThreadManagementService.sendToThread: it checks deletedAt/archivedAt before running worktreeRestore.restore I/O outside the thread lock, then calls dispatch without re-checking under the lock.

Minimal server repro (ThreadManagementService + OrchestratorV2 + OrchestrationEffectWorkerV2):

// Case 1: deleted thread
yield* threads.dispatch({ type: "thread.create", commandId: CommandId.make("c-create"), threadId, ... });
yield* threads.dispatch({ type: "thread.delete", commandId: CommandId.make("c-delete"), threadId });
const sendExit = yield* Effect.exit(
  threads.dispatch({
    type: "message.dispatch",
    commandId: CommandId.make("c-send"),
    threadId,
    messageId: MessageId.make("msg:after-delete"),
    text: "Do work on a deleted thread",
    attachments: [],
    dispatchMode: { type: "start_immediately" },
    createdBy: "user",
    creationSource: "web",
  }),
);
yield* drainEffectWorker();

// Case 2: archived thread + second queued message
// Same setup with thread.archive, then dispatch message 1 (start_immediately),
// dispatch message 2 (queue_after_active), then complete turn 1.

Expected behavior

Orchestrator.dispatch rejects message.dispatch with OrchestratorDispatchError (emitting 0 events and starting no provider turn) whenever projection.thread.deletedAt !== null or projection.thread.archivedAt !== null, matching dispatchThreadMutation, queue.resume, queued-message.promote-to-steer, queued-run.start, and ThreadManagementService.sendToThread.

Actual behavior

Both deleted and archived threads accept message.dispatch, commit events, and start a live provider turn while keeping deletedAt / archivedAt non-null. On an archived thread, a second queued message stays stranded in queued with queueHeld: false once the first turn completes.

Evidence

Observed output from the deterministic Effect/SQLite server repro:

REPRO 1 (deleted):
{"outcome":"Success","deletedAt":true,"startedProviderTurns":1,"runs":[{"id":"run:thread:thread%3Arepro-deleted:ordinal:1","status":"running"}],"shellActiveRunId":"run:thread:thread%3Arepro-deleted:ordinal:1"}

REPRO 2 (archived + queued 2nd message after turn 1 completes):
{"archivedAt":true,"startedProviderTurns":1,"runs":[{"id":"run:thread:thread%3Arepro-archived:ordinal:1","status":"completed","queueHeld":false},{"id":"run:thread:thread%3Arepro-archived:ordinal:2","status":"queued","queueHeld":false}]}

Additional investigation

  • How found: Stateful property-based testing of OrchestratorV2, minimized to deterministic non-PBT Vitest/Effect repros.
  • Verified: Confirmed still unguarded on origin/main (9d3054a7bb). All other thread-work commands (dispatchThreadMutation, queue.resume, queued-run.start, queued-message.promote-to-steer, startNextQueuedRunOnce) already check deletedAt/archivedAt.
  • Duplicate check: Searched open and closed issues/PRs for deleted thread message, archived thread message, dispatchMessage deleted, and message.dispatch archived — no matching issue found.
  • Impact: Major — silently spends tokens and mutates workspace files on a deleted/hidden thread with no UI to observe or stop it.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions