Skip to content
Open
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
29 changes: 27 additions & 2 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5515,9 +5515,34 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
SELECT payload_json FROM orchestration_v2_projection_checkpoint_scopes
WHERE thread_id = ${threadId} AND scope_id IN ${sql.in(nodes.flatMap((node) => (node.checkpointScopeId === null ? [] : [node.checkpointScopeId])))} ORDER BY ordinal_within_parent ASC, scope_id ASC
`.pipe(Effect.flatMap(decodeRows(decodeCheckpointScopePayload, threadId)));
// A handoff lists the ID of every item it omitted, and its delivery copies the list. On a
// long thread those are megabytes per handoff, so cut what this turn can't use before it
// reaches the JS heap. Delivery reads history's list, only of a handoff it may still send.
// Coverage reads delivery's list, only of handoffs to this run's provider thread.
const contextHandoffs = yield* sql<PayloadRow>`
SELECT payload_json FROM orchestration_v2_projection_context_handoffs
WHERE thread_id = ${threadId} ORDER BY rowid ASC
SELECT CASE
WHEN handoff.target_run_id = ${runId}
OR json_type(handoff.payload_json, '$.delivery') IS NULL
THEN handoff.payload_json
WHEN handoff.to_provider_thread_id IS (
SELECT provider_thread_id FROM orchestration_v2_projection_runs
WHERE thread_id = ${threadId} AND run_id = ${runId}
) THEN CASE
WHEN json_extract(handoff.payload_json, '$.delivery.status') = 'pending'
OR EXISTS (
SELECT 1 FROM orchestration_v2_projection_runs AS target
WHERE target.thread_id = handoff.thread_id
AND target.run_id = handoff.target_run_id
AND target.status IN ('failed', 'interrupted', 'cancelled')
) THEN handoff.payload_json
ELSE json_remove(handoff.payload_json, '$.history.omittedItemIds')
END
ELSE json_remove(
handoff.payload_json, '$.history.omittedItemIds', '$.delivery.omittedItemIds'
)
END AS payload_json
FROM orchestration_v2_projection_context_handoffs AS handoff
WHERE handoff.thread_id = ${threadId} ORDER BY handoff.rowid ASC
`.pipe(Effect.flatMap(decodeRows(decodeContextHandoffPayload, threadId)));
// The fork into this thread and the transfers its handoffs came from, not the thousands of delegated results.
const contextTransfers = yield* sql<PayloadRow>`
Expand Down
64 changes: 64 additions & 0 deletions apps/server/src/orchestration-v2/TurnStartReads.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { assert, it } from "@effect/vitest";
import {
CheckpointScopeId,
ContextHandoffId,
ContextTransferId,
NodeId,
EventId,
MessageId,
ProjectId,
ProviderDriverKind,
ProviderInstanceId,
ProviderThreadId,
RunId,
RuntimeRequestId,
ThreadId,
Expand Down Expand Up @@ -462,6 +464,68 @@ it.effect.each(["sqlite"] as const)(
"message-file",
recordingId,
]);

// Omitted IDs reach the reader only for a handoff this turn can still deliver or count as covered.
const providerThreadId = ProviderThreadId.make("provider-thread:current");
const otherProviderThreadId = ProviderThreadId.make("provider-thread:other");
const failedRunId = RunId.make("run:failed");
const completedRunId = RunId.make("run:completed");
yield* putRun({ ...run, ordinal: 3, providerThreadId });
yield* putRun({ ...run, id: failedRunId, ordinal: 99, status: "failed" });
yield* putRun({ ...run, id: completedRunId, ordinal: 98, status: "completed" });
const omitted = [TurnItemId.make("item:omitted")];
const handoffCases = [
["settled elsewhere", completedRunId, otherProviderThreadId, "inline", false, false],
["failed elsewhere", failedRunId, otherProviderThreadId, "injected", false, false],
["settled here", completedRunId, providerThreadId, "injected", false, true],
["failed here", failedRunId, providerThreadId, "inline", true, true],
["uncertain here", completedRunId, providerThreadId, "pending", true, true],
["for this run", runId, otherProviderThreadId, "inline", true, true],
["undelivered", completedRunId, otherProviderThreadId, undefined, true, false],
] as const;
for (const [name, targetRunId, toProviderThreadId, status] of handoffCases) {
yield* store.apply({
id: EventId.make(`event:handoff:${name}`),
type: "context-handoff.updated",
threadId,
runId: targetRunId,
occurredAt: now,
payload: {
id: ContextHandoffId.make(name),
threadId,
targetRunId,
fromProviderThreadIds: [],
toProviderThreadId,
coveredRunOrdinals: { from: 1, to: 1 },
strategy: "full_thread_summary",
status: "ready",
summaryMessageId: null,
summaryText: "Earlier work",
history: { messages: [], coverage: "", omittedItems: 1, omittedItemIds: omitted },
...(status === undefined
? {}
: {
delivery: {
nativeThreadId: "native:earlier",
status,
itemIds: [item.id],
omittedItemIds: omitted,
},
}),
createdByProviderInstanceId: null,
createdAt: now,
updatedAt: now,
},
});
}
const { contextHandoffs } = yield* store.getTurnStartContext(threadId, runId);
for (const [name, , , status, keepsHistory, keepsDelivery] of handoffCases) {
const read = contextHandoffs.find((handoff) => handoff.id === name)!;
assert.equal(read.summaryText, "Earlier work", name);
assert.deepEqual(read.history?.omittedItemIds, keepsHistory ? omitted : undefined, name);
assert.deepEqual(read.delivery?.omittedItemIds, keepsDelivery ? omitted : undefined, name);
if (status !== undefined) assert.deepEqual(read.delivery?.itemIds, [item.id], name);
}
}).pipe(Effect.provide(layerDatabase)),
);

Expand Down
Loading