diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts index 2155c22a75f1c..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"), @@ -370,6 +391,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; 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.