History

fix: reconcile main's round-18 features after the rebase

Adopts the round's main features into the v2 architecture: the #9023 media rework (streamed videos, media-file assets, protocol-relative links), #9098 shared live-activity row folded into the v2 working and thinking rows, the #9084/#9078 Claude model catalog for v2 consumers, a native #9005 OpenCode child-session abort in the v2 adapter, #9013's landed LegendList patch, and per-environment sidebar provider entries. For #8600 the server-side pieces land, but auto-settle evaluation stays client-side (reading the new server-owned settings) until the v2 orchestrator grows its own settlement reactor; main's v1-only reactor and coalescer additions are dropped with the rest of the v1 path.

Julius Marminge committed 2 months ago

Commit 7b13bca1 parent 786a25847 files · +1855 −2616Browse files

Showing the changes to apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts (1 of 47 files). Show all

Unified · Split

apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts+70−258
@@ -77,9 +77,6 @@ const decodeThread = Schema.decodeUnknownEffect(OrchestrationThread); · 76 unmodified lines
7777 // activity window. Applying the limit in SQL avoids decoding an unbounded
7878 // payload_json set before the projector can enforce that invariant.
7979 const THREAD_DETAIL_ACTIVITY_LIMIT = 500;
80−// Snapshot payloads are decoded and projected in small sequential batches so
81−// one client read does not retain the raw payloads for the full activity window.
82−const THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE = 25;
8380 const ProjectionProjectDbRowSchema = ProjectionProject.mapFields(
8481 Struct.assign({
8582 defaultModelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)),
@@ -104,9 +101,6 @@ const ProjectionThreadActivityDbRowSchema = ProjectionThreadActivity.mapFields( · 18 unmodified lines
104101 sequence: Schema.NullOr(NonNegativeInt),
105102 }),
106103 );
107−const ProjectionThreadActivityIdRowSchema = Schema.Struct({
108− activityId: ProjectionThreadActivity.fields.activityId,
109−});
110104 const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession;
111105 const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({
112106 id: ThreadId,
@@ -166,9 +160,6 @@ const ThreadActivityKindsLookupInput = Schema.Struct({ · 53 unmodified lines
166160 threadId: ThreadId,
167161 activityKinds: Schema.Array(Schema.String),
168162 });
169−const ThreadActivityIdsLookupInput = Schema.Struct({
170− activityIds: Schema.Array(ProjectionThreadActivity.fields.activityId),
171−});
172163 // Windowed reads order turns by the stable keyset (anchor, turn key), where
173164 // anchor is requested_at and turn key is
174165 // COALESCE(turn_id, ''). Both are event-derived, so cursors survive the
@@ -372,21 +363,6 @@ function mapProposedPlanRow( · 197 unmodified lines
372363 };
373364 }
374365
375−function mapThreadActivityRow(
376− row: Schema.Schema.Type<typeof ProjectionThreadActivityDbRowSchema>,
377−): OrchestrationThreadActivity {
378− return {
379− id: row.activityId,
380− tone: row.tone,
381− kind: row.kind,
382− summary: row.summary,
383− payload: row.payload,
384− turnId: row.turnId,
385− createdAt: row.createdAt,
386− ...(row.sequence !== null ? { sequence: row.sequence } : {}),
387− };
388−}
389−
390366 function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
391367 return (cause: unknown): ProjectionRepositoryError =>
392368 Schema.isSchemaError(cause)
@@ -1187,44 +1163,6 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { · 794 unmodified lines
11871163 `,
11881164 });
11891165
1190− const listThreadActivityIdsByThread = SqlSchema.findAll({
1191− Request: ThreadIdLookupInput,
1192− Result: ProjectionThreadActivityIdRowSchema,
1193− execute: ({ threadId }) =>
1194− sql`
1195− SELECT activity_id AS "activityId"
1196− FROM projection_thread_activities
1197− WHERE thread_id = ${threadId}
1198− ORDER BY
1199− sequence DESC,
1200− created_at DESC,
1201− activity_id DESC
1202− LIMIT ${THREAD_DETAIL_ACTIVITY_LIMIT}
1203− `,
1204− });
1205−
1206− const listThreadActivityRowsByIds = SqlSchema.findAll({
1207− Request: ThreadActivityIdsLookupInput,
1208− Result: ProjectionThreadActivityDbRowSchema,
1209− execute: ({ activityIds }) =>
1210− sql`
1211− SELECT
1212− activity_id AS "activityId",
1213− thread_id AS "threadId",
1214− turn_id AS "turnId",
1215− tone,
1216− kind,
1217− summary,
1218− payload_json AS "payload",
1219− sequence,
1220− created_at AS "createdAt"
1221− FROM projection_thread_activities
1222− -- The selectors already scoped these globally unique ids to the
1223− -- thread inside this transaction. Keep this as a primary-key lookup.
1224− WHERE ${sql.in("activity_id", activityIds)}
1225− `,
1226− });
1227−
12281166 const listThreadActivityRowsByThreadAndKinds = SqlSchema.findAll({
12291167 Request: ThreadActivityKindsLookupInput,
12301168 Result: ProjectionThreadActivityDbRowSchema,
@@ -1475,8 +1413,15 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { · 244 unmodified lines
14751413 `,
14761414 });
14771415
1478− const pinnedThreadActivityIdsCte = (threadId: string) => sql`
1479−pending_approval_requests AS (
1416+ // Blocking request payloads must remain available even if they predate the
1417+ // recent activity window. Each CTE returns at most one unresolved row per
1418+ // request, so the merge below stays bounded by actionable work.
1419+ const listPinnedThreadActivityRowsByThread = SqlSchema.findAll({
1420+ Request: ThreadIdLookupInput,
1421+ Result: ProjectionThreadActivityDbRowSchema,
1422+ execute: ({ threadId }) =>
1423+ sql`
1424+ WITH pending_approval_requests AS (
14801425 SELECT request_id, thread_id
14811426 FROM projection_pending_approvals
14821427 WHERE thread_id = ${threadId}
@@ -1540,17 +1485,6 @@ pending_approval_requests AS ( · 57 unmodified lines
15401485 WHERE request_order = 1
15411486 AND kind = 'user-input.requested'
15421487 )
1543− `;
1544−
1545− // Blocking request payloads must remain available even if they predate the
1546− // recent activity window. Each CTE returns at most one unresolved row per
1547− // request, so the merge below stays bounded by actionable work.
1548− const listPinnedThreadActivityRowsByThread = SqlSchema.findAll({
1549− Request: ThreadIdLookupInput,
1550− Result: ProjectionThreadActivityDbRowSchema,
1551− execute: ({ threadId }) =>
1552− sql`
1553− WITH ${pinnedThreadActivityIdsCte(threadId)}
15541488 SELECT
15551489 activity.activity_id AS "activityId",
15561490 activity.thread_id AS "threadId",
@@ -1568,17 +1502,6 @@ pending_approval_requests AS ( · 11 unmodified lines
15681502 `,
15691503 });
15701504
1571− const listPinnedThreadActivityIdsByThread = SqlSchema.findAll({
1572− Request: ThreadIdLookupInput,
1573− Result: ProjectionThreadActivityIdRowSchema,
1574− execute: ({ threadId }) =>
1575− sql`
1576− WITH ${pinnedThreadActivityIdsCte(threadId)}
1577− SELECT activity_id AS "activityId"
1578− FROM pinned_activity_ids
1579− `,
1580− });
1581−
15821505 const listThreadActivityRowsByThreadWindow = SqlSchema.findAll({
15831506 Request: ThreadTurnRangeLookupInput,
15841507 Result: ProjectionThreadActivityDbRowSchema,
@@ -1646,48 +1569,6 @@ pending_approval_requests AS ( · 61 unmodified lines
16461569 `,
16471570 });
16481571
1649− const listThreadActivityIdsByThreadWindow = SqlSchema.findAll({
1650− Request: ThreadTurnRangeLookupInput,
1651− Result: ProjectionThreadActivityIdRowSchema,
1652− execute: ({ threadId, minAnchorAt, minTurnKey, beforeAnchorAt, beforeTurnKey }) =>
1653− sql`
1654− SELECT activity_id AS "activityId"
1655− FROM projection_thread_activities
1656− WHERE thread_id = ${threadId}
1657− AND (
1658− turn_id IN (
1659− SELECT turn_id FROM projection_turns
1660− WHERE thread_id = ${threadId}
1661− AND turn_id IS NOT NULL
1662− AND (
1663− requested_at > ${minAnchorAt}
1664− OR (
1665− requested_at = ${minAnchorAt}
1666− AND turn_id >= ${minTurnKey}
1667− )
1668− )
1669− AND (
1670− requested_at < ${beforeAnchorAt}
1671− OR (
1672− requested_at = ${beforeAnchorAt}
1673− AND turn_id < ${beforeTurnKey}
1674− )
1675− )
1676− )
1677− OR (
1678− turn_id IS NULL
1679− AND created_at >= ${minAnchorAt}
1680− AND created_at < ${beforeAnchorAt}
1681− )
1682− )
1683− ORDER BY
1684− sequence DESC,
1685− created_at DESC,
1686− activity_id DESC
1687− LIMIT ${THREAD_DETAIL_ACTIVITY_LIMIT}
1688− `,
1689− });
1690−
16911572 const getFullThreadDiffContextRow = SqlSchema.findOneOption({
16921573 Request: FullThreadDiffContextLookupInput,
16931574 Result: ProjectionFullThreadDiffContextRowSchema,
@@ -2832,136 +2713,49 @@ pending_approval_requests AS ( · 1138 unmodified lines
28322713 readonly beforeTurnKey: string;
28332714 }
28342715
2835− type ThreadDetailActivityRead =
2836− | {
2837− readonly mode: "raw";
2838− readonly query?: ProjectionThreadDetailQuery;
2839− }
2840− | {
2841− readonly mode: "client";
2842− };
2843−
2844− const listProjectedThreadActivities = Effect.fn(
2845− "ProjectionSnapshotQuery.listProjectedThreadActivities",
2846− )(function* (threadId: ThreadId, bounds: ThreadDetailBounds | undefined) {
2847− const [activityIdRows, pinnedActivityIdRows] = yield* Effect.all([
2848− (bounds === undefined
2849− ? listThreadActivityIdsByThread({ threadId })
2850− : listThreadActivityIdsByThreadWindow({ threadId, ...bounds })
2716+ const getThreadDetailByIdBounded = (
2717+ threadId: ThreadId,
2718+ bounds: ThreadDetailBounds | undefined,
2719+ query: ProjectionThreadDetailQuery | undefined = undefined,
2720+ ) =>
2721+ Effect.gen(function* () {
2722+ const activityRowsEffect = (
2723+ query?.activityKinds === undefined
2724+ ? bounds === undefined
2725+ ? listThreadActivityRowsByThread({ threadId })
2726+ : listThreadActivityRowsByThreadWindow({ threadId, ...bounds })
2727+ : query.activityKinds.length === 0
2728+ ? Effect.succeed([])
2729+ : listThreadActivityRowsByThreadAndKinds({
2730+ threadId,
2731+ activityKinds: query.activityKinds,
2732+ })
28512733 ).pipe(
28522734 Effect.mapError(
28532735 toPersistenceSqlOrDecodeError(
2854− "ProjectionSnapshotQuery.getThreadDetailById:listActivityIds:query",
2855− "ProjectionSnapshotQuery.getThreadDetailById:listActivityIds:decodeRows",
2736+ "ProjectionSnapshotQuery.getThreadDetailById:listActivities:query",
2737+ "ProjectionSnapshotQuery.getThreadDetailById:listActivities:decodeRows",
28562738 ),
28572739 ),
2858− ),
2859− listPinnedThreadActivityIdsByThread({ threadId }).pipe(
2860− Effect.mapError(
2861− toPersistenceSqlOrDecodeError(
2862− "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivityIds:query",
2863− "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivityIds:decodeRows",
2864− ),
2865− ),
2866− ),
2867− ]);
2868− const activityIds = [
2869− ...new Set([...activityIdRows, ...pinnedActivityIdRows].map(({ activityId }) => activityId)),
2870− ];
2871− const activities: OrchestrationThreadActivity[] = [];
2872−
2873− for (
2874− let offset = 0;
2875− offset < activityIds.length;
2876− offset += THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE
2877− ) {
2878− const batchIds = activityIds.slice(
2879− offset,
2880− offset + THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE,
28812740 );
2882− const batchRows = yield* listThreadActivityRowsByIds({ activityIds: batchIds }).pipe(
2883− Effect.mapError(
2884− toPersistenceSqlOrDecodeError(
2885− "ProjectionSnapshotQuery.getThreadDetailById:listActivityPayloadBatch:query",
2886− "ProjectionSnapshotQuery.getThreadDetailById:listActivityPayloadBatch:decodeRows",
2887− ),
2888− ),
2889− );
2890− for (const row of batchRows) {
2891− activities.push(projectActivityPayload(mapThreadActivityRow(row)));
2892− }
2893− }
2894−
2895− return activities.toSorted(
2896− (left, right) =>
2897− (left.sequence ?? -1) - (right.sequence ?? -1) ||
2898− left.createdAt.localeCompare(right.createdAt) ||
2899− left.id.localeCompare(right.id),
2900− );
2901− });
2902−
2903− const getThreadDetailByIdBounded = (
2904− threadId: ThreadId,
2905− bounds: ThreadDetailBounds | undefined,
2906− activityRead: ThreadDetailActivityRead = { mode: "raw" },
2907− ) =>
2908− Effect.gen(function* () {
2909− const activitiesEffect =
2910− activityRead.mode === "client"
2911− ? listProjectedThreadActivities(threadId, bounds)
2912− : Effect.all([
2913− (activityRead.query?.activityKinds === undefined
2914− ? bounds === undefined
2915− ? listThreadActivityRowsByThread({ threadId })
2916− : listThreadActivityRowsByThreadWindow({ threadId, ...bounds })
2917− : activityRead.query.activityKinds.length === 0
2918− ? Effect.succeed([])
2919− : listThreadActivityRowsByThreadAndKinds({
2920− threadId,
2921− activityKinds: activityRead.query.activityKinds,
2922− })
2923− ).pipe(
2924− Effect.mapError(
2925− toPersistenceSqlOrDecodeError(
2926− "ProjectionSnapshotQuery.getThreadDetailById:listActivities:query",
2927− "ProjectionSnapshotQuery.getThreadDetailById:listActivities:decodeRows",
2928− ),
2741+ const pinnedActivityRowsEffect =
2742+ query?.activityKinds === undefined
2743+ ? listPinnedThreadActivityRowsByThread({ threadId }).pipe(
2744+ Effect.mapError(
2745+ toPersistenceSqlOrDecodeError(
2746+ "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:query",
2747+ "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:decodeRows",
29292748 ),
29302749 ),
2931− activityRead.query?.activityKinds === undefined
2932− ? listPinnedThreadActivityRowsByThread({ threadId }).pipe(
2933− Effect.mapError(
2934− toPersistenceSqlOrDecodeError(
2935− "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:query",
2936− "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:decodeRows",
2937− ),
2938− ),
2939− )
2940− : Effect.succeed([]),
2941− ]).pipe(
2942− Effect.map(([activityRows, pinnedActivityRows]) =>
2943− [
2944− ...new Map(
2945− [...activityRows, ...pinnedActivityRows].map(
2946− (row) => [row.activityId, row] as const,
2947− ),
2948− ).values(),
2949− ]
2950− .toSorted(
2951− (left, right) =>
2952− (left.sequence ?? -1) - (right.sequence ?? -1) ||
2953− left.createdAt.localeCompare(right.createdAt) ||
2954− left.activityId.localeCompare(right.activityId),
2955− )
2956− .map(mapThreadActivityRow),
2957− ),
2958− );
2750+ )
2751+ : Effect.succeed([]);
29592752
29602753 const [
29612754 threadRow,
29622755 messageRows,
29632756 proposedPlanRows,
2964− activities,
2757+ activityRows,
2758+ pinnedActivityRows,
29652759 checkpointRows,
29662760 latestTurnRow,
29672761 sessionRow,
@@ -2993,7 +2787,8 @@ pending_approval_requests AS ( · 25 unmodified lines
29932787 ),
29942788 ),
29952789 ),
2996− activitiesEffect,
2790+ activityRowsEffect,
2791+ pinnedActivityRowsEffect,
29972792 listCheckpointRowsByThread({ threadId }).pipe(
29982793 Effect.mapError(
29992794 toPersistenceSqlOrDecodeError(
@@ -3024,6 +2819,17 @@ pending_approval_requests AS ( · 24 unmodified lines
30242819 return Option.none<OrchestrationThread>();
30252820 }
30262821
2822+ const selectedActivityRows = [
2823+ ...new Map(
2824+ [...activityRows, ...pinnedActivityRows].map((row) => [row.activityId, row] as const),
2825+ ).values(),
2826+ ].toSorted(
2827+ (left, right) =>
2828+ (left.sequence ?? -1) - (right.sequence ?? -1) ||
2829+ left.createdAt.localeCompare(right.createdAt) ||
2830+ left.activityId.localeCompare(right.activityId),
2831+ );
2832+
30272833 const thread = {
30282834 id: threadRow.value.threadId,
30292835 projectId: threadRow.value.projectId,
@@ -3061,7 +2867,21 @@ pending_approval_requests AS ( · 31 unmodified lines
30612867 return message;
30622868 }),
30632869 proposedPlans: proposedPlanRows.map(mapProposedPlanRow),
3064− activities,
2870+ activities: selectedActivityRows.map((row) => {
2871+ const activity = {
2872+ id: row.activityId,
2873+ tone: row.tone,
2874+ kind: row.kind,
2875+ summary: row.summary,
2876+ payload: row.payload,
2877+ turnId: row.turnId,
2878+ createdAt: row.createdAt,
2879+ };
2880+ if (row.sequence !== null) {
2881+ return Object.assign(activity, { sequence: row.sequence });
2882+ }
2883+ return activity;
2884+ }),
30652885 checkpoints: checkpointRows.map((row) => ({
30662886 turnId: row.turnId,
30672887 checkpointTurnCount: row.checkpointTurnCount,
@@ -3086,11 +2906,7 @@ pending_approval_requests AS ( · 18 unmodified lines
30862906 const getThreadDetailById: ProjectionSnapshotQueryShape["getThreadDetailById"] = (
30872907 threadId,
30882908 query,
3089− ) =>
3090− getThreadDetailByIdBounded(threadId, undefined, {
3091− mode: "raw",
3092− ...(query === undefined ? {} : { query }),
3093− });
2909+ ) => getThreadDetailByIdBounded(threadId, undefined, query);
30942910
30952911 // Bounds pathological fan-out: one user turn that spawned hundreds of
30962912 // subagent turns still pages in bounded chunks, at the cost of splitting the
@@ -3114,9 +2930,7 @@ pending_approval_requests AS ( · 17 unmodified lines
31142930 .withTransaction(
31152931 Effect.gen(function* () {
31162932 if (window?.turnLimit === undefined) {
3117− const thread = yield* getThreadDetailByIdBounded(threadId, undefined, {
3118− mode: "client",
3119− });
2933+ const thread = yield* getThreadDetailById(threadId);
31202934 if (Option.isNone(thread)) {
31212935 return Option.none<OrchestrationThreadDetailSnapshot>();
31222936 }
@@ -3169,9 +2983,7 @@ pending_approval_requests AS ( · 46 unmodified lines
31692983 ? { minAnchorAt: "", minTurnKey: "", beforeAnchorAt: "", beforeTurnKey: "" }
31702984 : undefined;
31712985
3172− const thread = yield* getThreadDetailByIdBounded(threadId, emptyBounds ?? bounds, {
3173− mode: "client",
3174− });
2986+ const thread = yield* getThreadDetailByIdBounded(threadId, emptyBounds ?? bounds);
31752987 if (Option.isNone(thread)) {
31762988 return Option.none<OrchestrationThreadDetailSnapshot>();
31772989 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t