diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts index 2155c22a75f1c..c6478ba233d10 100644 --- a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts @@ -421,6 +421,64 @@ it.effect("sql: the resuming message time counts MCP sends and ignores agent not }).pipe(Effect.provide(layerSql)), ); +it.effect("sql: a held queued user message is not a settlement candidate", () => + Effect.gen(function* () { + const store = yield* ProjectionStore.ProjectionStoreV2; + const held = yield* createThread("held-queued-message"); + yield* createRun(held, "failed", 1); + const heldRun = yield* createRun(held, "queued", 2); + yield* store.apply({ + id: EventId.make("event:settlement:held-queued-message"), + type: "message.updated", + threadId: held, + occurredAt: old, + payload: { + createdBy: "user", + creationSource: "web", + id: MessageId.make(`message:${heldRun}`), + threadId: held, + runId: null, + nodeId: null, + role: "user", + text: "Held message", + attachments: [], + streaming: false, + createdAt: old, + updatedAt: old, + }, + }); + + // Automatic wakes do not block settlement, matching the orchestrator's queued-message check. + const automatic = yield* createThread("automatic-queued-message"); + yield* createRun(automatic, "failed", 1); + const automaticRun = yield* createRun(automatic, "queued", 2); + yield* store.apply({ + id: EventId.make("event:settlement:automatic-queued-message"), + type: "message.updated", + threadId: automatic, + occurredAt: old, + payload: { + createdBy: "agent", + creationSource: "server", + id: MessageId.make(`message:${automaticRun}`), + threadId: automatic, + runId: null, + nodeId: null, + role: "user", + text: "Scheduled wake", + attachments: [], + streaming: false, + createdAt: old, + updatedAt: old, + }, + }); + + const ids = (yield* store.getSettlementCandidates()).map((candidate) => candidate.id); + assert.isFalse(ids.includes(held)); + assert.isTrue(ids.includes(automatic)); + }).pipe(Effect.provide(layerSql)), +); + const pullRequestLink = (number: number) => ({ host: "github.com", repository: "owner/repository", diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index d7ea43543d2bd..cf859472c8190 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -9489,6 +9489,21 @@ export const layer: Layer.Layer