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
77// activity window. Applying the limit in SQL avoids decoding an unbounded77// activity window. Applying the limit in SQL avoids decoding an unbounded
78// payload_json set before the projector can enforce that invariant.78// payload_json set before the projector can enforce that invariant.
79const THREAD_DETAIL_ACTIVITY_LIMIT = 500;79const 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.
82const THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE = 25;
83const ProjectionProjectDbRowSchema = ProjectionProject.mapFields(80const ProjectionProjectDbRowSchema = ProjectionProject.mapFields(
84 Struct.assign({81 Struct.assign({
85 defaultModelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)),82 defaultModelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)),
@@ -104,9 +101,6 @@ const ProjectionThreadActivityDbRowSchema = ProjectionThreadActivity.mapFields( · 18 unmodified lines
104 sequence: Schema.NullOr(NonNegativeInt),101 sequence: Schema.NullOr(NonNegativeInt),
105 }),102 }),
106);103);
107const ProjectionThreadActivityIdRowSchema = Schema.Struct({
108 activityId: ProjectionThreadActivity.fields.activityId,
109});
110const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession;104const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession;
111const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({105const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({
112 id: ThreadId,106 id: ThreadId,
@@ -166,9 +160,6 @@ const ThreadActivityKindsLookupInput = Schema.Struct({ · 53 unmodified lines
166 threadId: ThreadId,160 threadId: ThreadId,
167 activityKinds: Schema.Array(Schema.String),161 activityKinds: Schema.Array(Schema.String),
168});162});
169const ThreadActivityIdsLookupInput = Schema.Struct({
170 activityIds: Schema.Array(ProjectionThreadActivity.fields.activityId),
171});
172// Windowed reads order turns by the stable keyset (anchor, turn key), where163// Windowed reads order turns by the stable keyset (anchor, turn key), where
173// anchor is requested_at and turn key is164// anchor is requested_at and turn key is
174// COALESCE(turn_id, ''). Both are event-derived, so cursors survive the165// COALESCE(turn_id, ''). Both are event-derived, so cursors survive the
@@ -372,21 +363,6 @@ function mapProposedPlanRow( · 197 unmodified lines
372 };363 };
373}364}
374 365
375function 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
390function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {366function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
391 return (cause: unknown): ProjectionRepositoryError =>367 return (cause: unknown): ProjectionRepositoryError =>
392 Schema.isSchemaError(cause)368 Schema.isSchemaError(cause)
@@ -1187,44 +1163,6 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { · 794 unmodified lines
1187 `,1163 `,
1188 });1164 });
1189 1165
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
1228 const listThreadActivityRowsByThreadAndKinds = SqlSchema.findAll({1166 const listThreadActivityRowsByThreadAndKinds = SqlSchema.findAll({
1229 Request: ThreadActivityKindsLookupInput,1167 Request: ThreadActivityKindsLookupInput,
1230 Result: ProjectionThreadActivityDbRowSchema,1168 Result: ProjectionThreadActivityDbRowSchema,
@@ -1475,8 +1413,15 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { · 244 unmodified lines
1475 `,1413 `,
1476 });1414 });
1477 1415
1478 const pinnedThreadActivityIdsCte = (threadId: string) => sql`1416 // Blocking request payloads must remain available even if they predate the
1479pending_approval_requests AS (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 (
1480 SELECT request_id, thread_id1425 SELECT request_id, thread_id
1481 FROM projection_pending_approvals1426 FROM projection_pending_approvals
1482 WHERE thread_id = ${threadId}1427 WHERE thread_id = ${threadId}
@@ -1540,17 +1485,6 @@ pending_approval_requests AS ( · 57 unmodified lines
1540 WHERE request_order = 11485 WHERE request_order = 1
1541 AND kind = 'user-input.requested'1486 AND kind = 'user-input.requested'
1542 )1487 )
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)}
1554 SELECT1488 SELECT
1555 activity.activity_id AS "activityId",1489 activity.activity_id AS "activityId",
1556 activity.thread_id AS "threadId",1490 activity.thread_id AS "threadId",
@@ -1568,17 +1502,6 @@ pending_approval_requests AS ( · 11 unmodified lines
1568 `,1502 `,
1569 });1503 });
1570 1504
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
1582 const listThreadActivityRowsByThreadWindow = SqlSchema.findAll({1505 const listThreadActivityRowsByThreadWindow = SqlSchema.findAll({
1583 Request: ThreadTurnRangeLookupInput,1506 Request: ThreadTurnRangeLookupInput,
1584 Result: ProjectionThreadActivityDbRowSchema,1507 Result: ProjectionThreadActivityDbRowSchema,
@@ -1646,48 +1569,6 @@ pending_approval_requests AS ( · 61 unmodified lines
1646 `,1569 `,
1647 });1570 });
1648 1571
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
1691 const getFullThreadDiffContextRow = SqlSchema.findOneOption({1572 const getFullThreadDiffContextRow = SqlSchema.findOneOption({
1692 Request: FullThreadDiffContextLookupInput,1573 Request: FullThreadDiffContextLookupInput,
1693 Result: ProjectionFullThreadDiffContextRowSchema,1574 Result: ProjectionFullThreadDiffContextRowSchema,
@@ -2832,136 +2713,49 @@ pending_approval_requests AS ( · 1138 unmodified lines
2832 readonly beforeTurnKey: string;2713 readonly beforeTurnKey: string;
2833 }2714 }
2834 2715
2835 type ThreadDetailActivityRead =2716 const getThreadDetailByIdBounded = (
2836 | {2717 threadId: ThreadId,
2837 readonly mode: "raw";2718 bounds: ThreadDetailBounds | undefined,
2838 readonly query?: ProjectionThreadDetailQuery;2719 query: ProjectionThreadDetailQuery | undefined = undefined,
2839 }2720 ) =>
2840 | {2721 Effect.gen(function* () {
2841 readonly mode: "client";2722 const activityRowsEffect = (
2842 };2723 query?.activityKinds === undefined
2843 2724 ? bounds === undefined
2844 const listProjectedThreadActivities = Effect.fn(2725 ? listThreadActivityRowsByThread({ threadId })
2845 "ProjectionSnapshotQuery.listProjectedThreadActivities",2726 : listThreadActivityRowsByThreadWindow({ threadId, ...bounds })
2846 )(function* (threadId: ThreadId, bounds: ThreadDetailBounds | undefined) {2727 : query.activityKinds.length === 0
2847 const [activityIdRows, pinnedActivityIdRows] = yield* Effect.all([2728 ? Effect.succeed([])
2848 (bounds === undefined2729 : listThreadActivityRowsByThreadAndKinds({
2849 ? listThreadActivityIdsByThread({ threadId })2730 threadId,
2850 : listThreadActivityIdsByThreadWindow({ threadId, ...bounds })2731 activityKinds: query.activityKinds,
2732 })
2851 ).pipe(2733 ).pipe(
2852 Effect.mapError(2734 Effect.mapError(
2853 toPersistenceSqlOrDecodeError(2735 toPersistenceSqlOrDecodeError(
2854 "ProjectionSnapshotQuery.getThreadDetailById:listActivityIds:query",2736 "ProjectionSnapshotQuery.getThreadDetailById:listActivities:query",
2855 "ProjectionSnapshotQuery.getThreadDetailById:listActivityIds:decodeRows",2737 "ProjectionSnapshotQuery.getThreadDetailById:listActivities:decodeRows",
2856 ),2738 ),
2857 ),2739 ),
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,
2881 );2740 );
2882 const batchRows = yield* listThreadActivityRowsByIds({ activityIds: batchIds }).pipe(2741 const pinnedActivityRowsEffect =
2883 Effect.mapError(2742 query?.activityKinds === undefined
2884 toPersistenceSqlOrDecodeError(2743 ? listPinnedThreadActivityRowsByThread({ threadId }).pipe(
2885 "ProjectionSnapshotQuery.getThreadDetailById:listActivityPayloadBatch:query",2744 Effect.mapError(
2886 "ProjectionSnapshotQuery.getThreadDetailById:listActivityPayloadBatch:decodeRows",2745 toPersistenceSqlOrDecodeError(
2887 ),2746 "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:query",
2888 ),2747 "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:decodeRows",
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 ),
2929 ),2748 ),
2930 ),2749 ),
2931 activityRead.query?.activityKinds === undefined2750 )
2932 ? listPinnedThreadActivityRowsByThread({ threadId }).pipe(2751 : Effect.succeed([]);
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 );
2959 2752
2960 const [2753 const [
2961 threadRow,2754 threadRow,
2962 messageRows,2755 messageRows,
2963 proposedPlanRows,2756 proposedPlanRows,
2964 activities,2757 activityRows,
2758 pinnedActivityRows,
2965 checkpointRows,2759 checkpointRows,
2966 latestTurnRow,2760 latestTurnRow,
2967 sessionRow,2761 sessionRow,
@@ -2993,7 +2787,8 @@ pending_approval_requests AS ( · 25 unmodified lines
2993 ),2787 ),
2994 ),2788 ),
2995 ),2789 ),
2996 activitiesEffect,2790 activityRowsEffect,
2791 pinnedActivityRowsEffect,
2997 listCheckpointRowsByThread({ threadId }).pipe(2792 listCheckpointRowsByThread({ threadId }).pipe(
2998 Effect.mapError(2793 Effect.mapError(
2999 toPersistenceSqlOrDecodeError(2794 toPersistenceSqlOrDecodeError(
@@ -3024,6 +2819,17 @@ pending_approval_requests AS ( · 24 unmodified lines
3024 return Option.none<OrchestrationThread>();2819 return Option.none<OrchestrationThread>();
3025 }2820 }
3026 2821
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
3027 const thread = {2833 const thread = {
3028 id: threadRow.value.threadId,2834 id: threadRow.value.threadId,
3029 projectId: threadRow.value.projectId,2835 projectId: threadRow.value.projectId,
@@ -3061,7 +2867,21 @@ pending_approval_requests AS ( · 31 unmodified lines
3061 return message;2867 return message;
3062 }),2868 }),
3063 proposedPlans: proposedPlanRows.map(mapProposedPlanRow),2869 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 }),
3065 checkpoints: checkpointRows.map((row) => ({2885 checkpoints: checkpointRows.map((row) => ({
3066 turnId: row.turnId,2886 turnId: row.turnId,
3067 checkpointTurnCount: row.checkpointTurnCount,2887 checkpointTurnCount: row.checkpointTurnCount,
@@ -3086,11 +2906,7 @@ pending_approval_requests AS ( · 18 unmodified lines
3086 const getThreadDetailById: ProjectionSnapshotQueryShape["getThreadDetailById"] = (2906 const getThreadDetailById: ProjectionSnapshotQueryShape["getThreadDetailById"] = (
3087 threadId,2907 threadId,
3088 query,2908 query,
3089 ) =>2909 ) => getThreadDetailByIdBounded(threadId, undefined, query);
3090 getThreadDetailByIdBounded(threadId, undefined, {
3091 mode: "raw",
3092 ...(query === undefined ? {} : { query }),
3093 });
3094 2910
3095 // Bounds pathological fan-out: one user turn that spawned hundreds of2911 // Bounds pathological fan-out: one user turn that spawned hundreds of
3096 // subagent turns still pages in bounded chunks, at the cost of splitting the2912 // subagent turns still pages in bounded chunks, at the cost of splitting the
@@ -3114,9 +2930,7 @@ pending_approval_requests AS ( · 17 unmodified lines
3114 .withTransaction(2930 .withTransaction(
3115 Effect.gen(function* () {2931 Effect.gen(function* () {
3116 if (window?.turnLimit === undefined) {2932 if (window?.turnLimit === undefined) {
3117 const thread = yield* getThreadDetailByIdBounded(threadId, undefined, {2933 const thread = yield* getThreadDetailById(threadId);
3118 mode: "client",
3119 });
3120 if (Option.isNone(thread)) {2934 if (Option.isNone(thread)) {
3121 return Option.none<OrchestrationThreadDetailSnapshot>();2935 return Option.none<OrchestrationThreadDetailSnapshot>();
3122 }2936 }
@@ -3169,9 +2983,7 @@ pending_approval_requests AS ( · 46 unmodified lines
3169 ? { minAnchorAt: "", minTurnKey: "", beforeAnchorAt: "", beforeTurnKey: "" }2983 ? { minAnchorAt: "", minTurnKey: "", beforeAnchorAt: "", beforeTurnKey: "" }
3170 : undefined;2984 : undefined;
3171 2985
3172 const thread = yield* getThreadDetailByIdBounded(threadId, emptyBounds ?? bounds, {2986 const thread = yield* getThreadDetailByIdBounded(threadId, emptyBounds ?? bounds);
3173 mode: "client",
3174 });
3175 if (Option.isNone(thread)) {2987 if (Option.isNone(thread)) {
3176 return Option.none<OrchestrationThreadDetailSnapshot>();2988 return Option.none<OrchestrationThreadDetailSnapshot>();
3177 }2989 }

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