Skip to content

Commit e9352b0

Browse files
committed
fix(server): stop and limit evidence read only what recovery needs
Stop read every user message, error and notification in the thread. It now reads the latest failed run's errors, then the inputs of runs completed before that failure resets. The limit-evidence query drops notification detail, which paging strips anyway.
1 parent e7ef288 commit e9352b0

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11313,12 +11313,35 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1131311313
const { runs, subagents } = input.projection;
1131411314
let { thread } = input.projection;
1131511315
let recovery = thread.limitRecovery;
11316-
const { turnItems } = yield* projectionStore
11317-
.getThreadRecords(thread.id, ["turnItems"], {
11318-
turnItemTypes: ["user_message", "error", "notification"],
11319-
})
11320-
.pipe(mapDispatchError(input.command));
11321-
// Stop must also disarm a limited run that completed notification wakes hide.
11316+
// Stop must also disarm a limited run that completed notification wakes hide. Only the
11317+
// latest failure's errors and the inputs of runs completed before its reset can matter.
11318+
const readItems = (filter: Parameters<typeof projectionStore.getThreadRecords>[2]) =>
11319+
projectionStore
11320+
.getThreadRecords(thread.id, ["turnItems"], filter)
11321+
.pipe(mapDispatchError(input.command));
11322+
const failed = latestExecutedRun(runs.filter((run) => run.status === "failed"));
11323+
const errors =
11324+
failed === null
11325+
? []
11326+
: (yield* readItems({ turnItemTypes: ["error"], turnItemRunIds: [failed.id] })).turnItems;
11327+
const resetAt = latestRootProviderFailure(failed, errors)?.resetAt;
11328+
const resetMs = resetAt == null ? Number.NaN : Date.parse(resetAt);
11329+
const wakeRunIds = runs
11330+
.filter(
11331+
(run) =>
11332+
run.status === "completed" &&
11333+
run.completedAt !== null &&
11334+
DateTime.toEpochMillis(run.completedAt) <= resetMs,
11335+
)
11336+
.map((run) => run.id);
11337+
const inputs =
11338+
wakeRunIds.length === 0
11339+
? []
11340+
: (yield* readItems({
11341+
turnItemTypes: ["user_message", "notification"],
11342+
turnItemRunIds: wakeRunIds,
11343+
})).turnItems;
11344+
const turnItems = [...errors, ...inputs];
1132211345
const latest = usageLimitBlockedRun(runs, turnItems, null) ?? latestExecutedRun(runs);
1132311346
if (latest?.status === "failed") {
1132411347
const failure = latestRootProviderFailure(latest, turnItems);

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6252,8 +6252,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62526252
WHERE thread_id = ${threadId}
62536253
AND run_id IN (SELECT value FROM json_each(${encodeIdList(runIds)}))
62546254
`;
6255+
// Paging stubs these notifications anyway; skip decoding their unbounded detail.
62556256
const itemRows = yield* sql<PayloadRow>`
6256-
SELECT payload_json FROM orchestration_v2_projection_turn_items
6257+
SELECT CASE WHEN type = 'notification'
6258+
THEN json_remove(payload_json, '$.detail') ELSE payload_json END AS payload_json
6259+
FROM orchestration_v2_projection_turn_items
62576260
WHERE thread_id = ${threadId}
62586261
AND turn_item_id IN (SELECT value FROM json_each(${encodeIdList(itemIds)}))
62596262
`;

0 commit comments

Comments
 (0)