Skip to content
Merged
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
67 changes: 67 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,73 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
}),
);

it.effect("selects the latest waiting secret only from active runs", () =>
Effect.gen(function* () {
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
const sql = yield* SqlClient.SqlClient;
const suffix = "shell-pending-secret";
const threadId = yield* addRolledBackRecoveryCandidate(suffix);
const runId = RunId.make(`run:${suffix}:rolled-back`);
const nodeId = NodeId.make(`node:${suffix}:rolled-back`);
const now = yield* DateTime.now;
const addSecret = (id: string, ordinal: number, status: "waiting" | "completed") =>
projectionStore.apply({
id: EventId.make(`event:${id}`),
type: "turn-item.updated",
threadId,
runId,
nodeId,
driver,
occurredAt: now,
payload: {
id: TurnItemId.make(id),
threadId,
runId,
nodeId,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal,
status,
title: null,
startedAt: now,
completedAt: null,
updatedAt: now,
type: "secret_request",
label: "Test credential",
reason: "Test pending input",
secretStatus: status === "waiting" ? "pending" : "saved",
},
});
yield* addSecret("secret:a", 2, "waiting");
yield* addSecret("secret:b", 3, "waiting");
yield* addSecret("secret:c", 4, "completed");

for (const status of [
"preparing",
"starting",
"running",
"waiting",
"completed",
"rolled_back",
]) {
yield* sql`
UPDATE orchestration_v2_projection_runs
SET status = ${status}, payload_json = json_set(payload_json, '$.status', ${status})
WHERE run_id = ${runId}
`;
const shell = yield* projectionStore.getShellSnapshot();
const thread = shell.threads.find((candidate) => candidate.id === threadId);
assert.isDefined(thread);
assert.equal(
thread?.pendingRuntimeRequest?.id ?? null,
status === "completed" || status === "rolled_back" ? null : "secret:b",
);
}
}),
);

it.effect("builds shell snapshots without decoding full turn item payloads", () =>
Effect.gen(function* () {
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
Expand Down
10 changes: 6 additions & 4 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4999,12 +4999,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
) AS pending_request_payload_json,
(
SELECT secret.payload_json
FROM orchestration_v2_projection_turn_items secret
-- Keep runs outermost so completed history is never scanned for a pending secret.
FROM orchestration_v2_projection_runs r
CROSS JOIN orchestration_v2_projection_turn_items secret
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
INNER JOIN orchestration_v2_projection_runs r ON r.run_id = secret.run_id
WHERE secret.thread_id = t.thread_id
AND secret.type = 'secret_request' AND secret.status = 'waiting'
WHERE r.thread_id = t.thread_id
AND r.status IN ('preparing', 'starting', 'running', 'waiting')
AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id
AND secret.type = 'secret_request' AND secret.status = 'waiting'
ORDER BY secret.updated_at DESC, secret.turn_item_id DESC
LIMIT 1
) AS pending_secret_request_payload_json,
Expand Down
Loading