Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions apps/server/src/orchestration-v2/ProjectionSettlement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
),
);
Expand Down
15 changes: 15 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8330,6 +8330,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
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'
Expand Down
Loading