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
58 changes: 58 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionSettlement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 @@ -9489,6 +9489,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, ProjectionStoreService
)
)
)
AND NOT EXISTS (
-- The orchestrator rejects settlement while a user's queued message is held, so a
-- candidate here would only make the sweep retry the same failing command.
SELECT 1
FROM orchestration_v2_projection_runs held
JOIN orchestration_v2_projection_messages message
ON message.message_id = json_extract(held.payload_json, '$.userMessageId')
WHERE held.thread_id = t.thread_id
AND held.status = 'queued'
AND message.role = 'user'
AND json_extract(message.payload_json, '$.createdBy') = 'user'
AND json_extract(message.payload_json, '$.scheduledTaskId') IS NULL
AND json_extract(message.payload_json, '$.notification') IS NULL
AND json_extract(message.payload_json, '$.delegatedCompletion') IS NULL
)
ORDER BY t.updated_at ASC, t.thread_id ASC
`;
if (rows.length === 0) return [];
Expand Down
Loading