From d75ee4d8cee07c06c5bc184dfe224b13e094811b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 3 Dec 2026 20:18:53 +0000 Subject: [PATCH 1/2] test(server): auto-settle sweep skips a failed thread with a queued message Reproduces #48419: the sweep treats a failed thread holding a queued user message as a settlement candidate, while thread.auto-settle rejects it. Co-Authored-By: Claude Opus 5.5 --- .../ProjectionSettlement.test.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts index 2155c22a75f1c..20d2b1385e6a5 100644 --- a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts @@ -370,6 +370,60 @@ it.effect( }).pipe(Effect.provide(layerSql)), ); +it.effect("sql: a failed thread holding a queued user message is not a settlement candidate", () => + Effect.gen(function* () { + const store = yield* ProjectionStore.ProjectionStoreV2; + const threadId = yield* createThread("failed-with-queued"); + const failedRunId = yield* createRun(threadId, "failed"); + const messageId = MessageId.make("message:settlement:queued-after-failure"); + yield* store.apply({ + id: EventId.make("event:settlement:queued-after-failure:message"), + type: "message.updated", + threadId, + occurredAt: old, + payload: { + createdBy: "user", + creationSource: "web", + id: messageId, + threadId, + runId: null, + nodeId: null, + role: "user", + text: "Queued while the last turn failed", + attachments: [], + streaming: false, + createdAt: old, + updatedAt: old, + }, + }); + const failedRun = (yield* store.getThreadProjection(threadId)).runs.find( + (run) => run.id === failedRunId, + )!; + yield* store.apply({ + id: EventId.make("event:settlement:queued-after-failure:run"), + type: "run.created", + threadId, + occurredAt: old, + payload: { + ...failedRun, + id: RunId.make("run:settlement:queued-after-failure"), + ordinal: 2, + userMessageId: messageId, + status: "queued", + startedAt: null, + completedAt: null, + queueHeld: true, + }, + }); + + // thread.auto-settle rejects this thread ("has a queued message"), so the sweep must skip it. + const candidate = (yield* store.getSettlementCandidates()).find( + (thread) => thread.id === threadId, + )!; + assert.isFalse(isAutoSettlementCandidate(candidate, DateTime.toEpochMillis(now))); + }).pipe(Effect.provide(layerSql)), +); + it.effect("sql: the resuming message time counts MCP sends and ignores agent notifications", () => Effect.gen(function* () { const store = yield* ProjectionStore.ProjectionStoreV2; From 9e9c20769591d69e520d55c009b7000394063d94 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 3 Dec 2026 20:40:27 +0000 Subject: [PATCH 2/2] fix(server): auto-settle sweep skips threads holding a queued user message The sweep treated an old or failed-thread queued message as no obstacle, while thread.settle rejects any thread whose queue holds a message the user sent. The sweep then dispatched a doomed thread.auto-settle every minute. Settlement candidates now carry whether a queued run waits on a non-automatic message, using the same rule as thread.settle, and isAutoSettlementCandidate skips those threads. Co-Authored-By: Claude Opus 5.5 --- .../ProjectionSettlement.test.ts | 23 ++++++++++++++++++- .../src/orchestration-v2/ProjectionStore.ts | 22 +++++++++++++++++- .../ThreadSettlementService.ts | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts index 20d2b1385e6a5..207cee9cc3335 100644 --- a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts @@ -238,7 +238,28 @@ it.effect( yield* createRun(yield* createThread(status), status); } const queued = yield* createThread("queued"); - yield* createRun(queued, "queued"); + const queuedRunId = yield* createRun(queued, "queued"); + // thread.settle drops an automatic queued message, so the thread stays a candidate. + yield* store.apply({ + id: EventId.make("event:settlement:queued:message"), + type: "message.updated", + threadId: queued, + occurredAt: old, + payload: { + createdBy: "agent", + creationSource: "mcp", + id: MessageId.make(`message:${queuedRunId}`), + threadId: queued, + runId: null, + nodeId: null, + role: "user", + text: "Queued by an agent", + attachments: [], + streaming: false, + createdAt: old, + updatedAt: old, + }, + }); const blocked = yield* createThread("blocked"); yield* store.apply({ id: EventId.make("event:settlement:request"), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index d7ea43543d2bd..52c53c3750c3d 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -258,6 +258,8 @@ export type ProjectionSettlementCandidate = Pick< Pick & { /** Latest user or MCP agent message; automatic server and provider wakes are excluded. */ readonly latestResumingMessageAt: DateTime.Utc | null; + /** A queued run waits on a message the user sent; thread.settle rejects the thread. */ + readonly hasQueuedUserMessage?: boolean; }; const ProjectionCheckpointContext = Schema.Struct({ @@ -1348,6 +1350,7 @@ type SettlementThreadRow = Pick< | "latest_user_message_at" > & { readonly latest_resuming_message_at: string | null; + readonly has_queued_user_message: 0 | 1; }; type ShellRunItemCountRow = { @@ -9408,7 +9411,23 @@ export const layer: Layer.Layer 0) return false; if (thread.activityRunStatus != null) return false; + if (thread.hasQueuedUserMessage === true) return false; const pendingTasks = thread.pendingBackgroundTasks ?? []; if (backgroundWorkHoldsCompletion(pendingTasks)) return false; // Settling ends a watch, which would silently drop a reviewer's later comments.