From bb7e9f602279ef61d0f2bb0ad9f8bc9f9077327b Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Mon, 16 Nov 2026 13:59:18 +0800 Subject: [PATCH] fix(server): skip auto-settlement while user messages are queued --- .../ProjectionSettlement.test.ts | 52 +++++++++++++++++-- .../src/orchestration-v2/ProjectionStore.ts | 15 ++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts index 86a4c6c77375d..3a43cdef11de9 100644 --- a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts @@ -232,7 +232,49 @@ it.effect( yield* createRun(yield* createThread(status), status); } const queued = yield* createThread("queued"); - yield* createRun(queued, "queued"); + const queuedRun = yield* createRun(queued, "queued"); + yield* store.apply({ + id: EventId.make("event:settlement:queued-message"), + type: "message.updated", + threadId: queued, + occurredAt: old, + payload: { + createdBy: "user", + creationSource: "web", + id: MessageId.make(`message:${queuedRun}`), + threadId: queued, + runId: queuedRun, + nodeId: null, + role: "user", + text: "Queued input", + attachments: [], + streaming: false, + createdAt: old, + updatedAt: old, + }, + }); + const automatic = yield* createThread("automatic-queued"); + const automaticRun = yield* createRun(automatic, "queued"); + yield* store.apply({ + id: EventId.make("event:settlement:automatic-message"), + type: "message.updated", + threadId: automatic, + occurredAt: old, + payload: { + createdBy: "agent", + creationSource: "provider", + id: MessageId.make(`message:${automaticRun}`), + threadId: automatic, + runId: automaticRun, + nodeId: null, + role: "user", + text: "Automatic input", + attachments: [], + streaming: false, + createdAt: old, + updatedAt: old, + }, + }); const blocked = yield* createThread("blocked"); yield* store.apply({ id: EventId.make("event:settlement:request"), @@ -324,13 +366,17 @@ it.effect( ); assert.deepEqual( new Set(eligible.map((thread) => thread.id)), - new Set([idle, completed, queued, woke, background, persistent, rolledBack]), + new Set([idle, completed, automatic, woke, background, persistent, rolledBack]), ); assert.deepEqual( new Set(eligible.map((thread) => thread.id)), new Set( shell.threads - .filter((thread) => isAutoSettlementCandidate(thread, DateTime.toEpochMillis(now))) + .filter( + (thread) => + thread.id !== queued && + isAutoSettlementCandidate(thread, DateTime.toEpochMillis(now)), + ) .map((thread) => thread.id), ), ); diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 5b71a6ac042f1..59ced9d2b2678 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -8330,6 +8330,21 @@ export const layer: Layer.Layer = WHERE active.thread_id = t.thread_id AND active.status IN ('preparing', 'starting', 'running', 'waiting') ) + AND NOT EXISTS ( + SELECT 1 FROM orchestration_v2_projection_runs queued + WHERE queued.thread_id = t.thread_id AND queued.status = 'queued' + AND NOT EXISTS ( + SELECT 1 FROM orchestration_v2_projection_messages message + WHERE message.message_id = json_extract(queued.payload_json, '$.userMessageId') + AND message.thread_id = t.thread_id AND message.role = 'user' + AND ( + json_extract(message.payload_json, '$.createdBy') IS NOT 'user' + OR json_type(message.payload_json, '$.scheduledTaskId') IS NOT NULL + OR json_type(message.payload_json, '$.notification') IS NOT NULL + OR json_type(message.payload_json, '$.delegatedCompletion') IS NOT NULL + ) + ) + ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending'