Skip to content

Commit f7e5ce4

Browse files
fix(observability): trace full shell snapshot decoding
1 parent 2f6d64f commit f7e5ce4

1 file changed

Lines changed: 41 additions & 43 deletions

File tree

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

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5965,7 +5965,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
59655965
);
59665966

59675967
const shellThreadStateFromRow =
5968-
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
5968+
/* Runs once per shell row; the enclosing snapshot or thread read owns the span. */
59695969
Effect.fnUntraced(function* (input: {
59705970
readonly row: ShellThreadRow;
59715971
readonly runOrdinalsByThreadId: ReadonlyMap<ThreadId, Map<RunId, number>>;
@@ -6171,53 +6171,51 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61716171
};
61726172
});
61736173

6174-
const decodeShellSnapshot =
6175-
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
6176-
Effect.fnUntraced(function* ({
6177-
targetThreadIds,
6178-
threadRows,
6174+
const decodeShellSnapshot = Effect.fn("ProjectionStore.decodeShellSnapshot")(function* ({
6175+
targetThreadIds,
6176+
threadRows,
6177+
runRows,
6178+
itemCountRows,
6179+
sequenceRows,
6180+
providerThreadRows,
6181+
pendingTurnItemRows,
6182+
}: Effect.Success<ReturnType<typeof readShellSnapshotRows>>) {
6183+
const { runOrdinalsByThreadId, itemCountsByThreadId } = runMapsByThreadId({
61796184
runRows,
61806185
itemCountRows,
6181-
sequenceRows,
6182-
providerThreadRows,
6183-
pendingTurnItemRows,
6184-
}: Effect.Success<ReturnType<typeof readShellSnapshotRows>>) {
6185-
const { runOrdinalsByThreadId, itemCountsByThreadId } = runMapsByThreadId({
6186-
runRows,
6187-
itemCountRows,
6188-
});
6189-
const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
6190-
yield* pendingBackgroundDataByThreadId({ providerThreadRows, pendingTurnItemRows });
6191-
const states = yield* Effect.forEach(threadRows, (row) =>
6192-
shellThreadStateFromRow({
6193-
row,
6194-
runOrdinalsByThreadId,
6195-
itemCountsByThreadId,
6196-
providerThreadsByThreadId,
6197-
pendingTurnItemsByThreadId,
6186+
});
6187+
const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
6188+
yield* pendingBackgroundDataByThreadId({ providerThreadRows, pendingTurnItemRows });
6189+
const states = yield* Effect.forEach(threadRows, (row) =>
6190+
shellThreadStateFromRow({
6191+
row,
6192+
runOrdinalsByThreadId,
6193+
itemCountsByThreadId,
6194+
providerThreadsByThreadId,
6195+
pendingTurnItemsByThreadId,
6196+
}),
6197+
);
6198+
const statesByThreadId = new Map(states.map((state) => [state.thread.id, state]));
6199+
6200+
const shells = states
6201+
.filter((state) => targetThreadIds.has(state.thread.id))
6202+
.map((state) =>
6203+
shellFromState({
6204+
state,
6205+
visibleItemCount: visibleItemCountForShell({
6206+
threadId: state.thread.id,
6207+
statesByThreadId,
6208+
}),
61986209
}),
61996210
);
6200-
const statesByThreadId = new Map(states.map((state) => [state.thread.id, state]));
6201-
6202-
const shells = states
6203-
.filter((state) => targetThreadIds.has(state.thread.id))
6204-
.map((state) =>
6205-
shellFromState({
6206-
state,
6207-
visibleItemCount: visibleItemCountForShell({
6208-
threadId: state.thread.id,
6209-
statesByThreadId,
6210-
}),
6211-
}),
6212-
);
62136211

6214-
return {
6215-
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
6216-
snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0,
6217-
threads: shells.filter((thread) => thread.archivedAt === null),
6218-
archivedThreads: shells.filter((thread) => thread.archivedAt !== null),
6219-
};
6220-
}, Effect.mapError(shellSnapshotReadError));
6212+
return {
6213+
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
6214+
snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0,
6215+
threads: shells.filter((thread) => thread.archivedAt === null),
6216+
archivedThreads: shells.filter((thread) => thread.archivedAt !== null),
6217+
};
6218+
}, Effect.mapError(shellSnapshotReadError));
62216219

62226220
const readShellSnapshot: ProjectionStoreV2Shape["readShellSnapshot"] = (options) =>
62236221
readShellSnapshotRows(options).pipe(

0 commit comments

Comments
 (0)