Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

024_BackfillProjectionThreadShellSummary.test.ts

218 lines5.7 KB

1import { assert, it } from "@effect/vitest";
2import { Effect, Layer } from "effect";
3import * as SqlClient from "effect/unstable/sql/SqlClient";
4​
5import { runMigrations } from "../Migrations.ts";
6import * as NodeSqliteClient from "../NodeSqliteClient.ts";
7​
8const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory()));
9​
10layer("024_BackfillProjectionThreadShellSummary", (it) => {
11 it.effect("backfills thread shell summary fields and clears stale projected approvals", () =>
12 Effect.gen(function* () {
13 const sql = yield* SqlClient.SqlClient;
14​
15 yield* runMigrations({ toMigrationInclusive: 23 });
16​
17 yield* sql`
18 INSERT INTO projection_threads (
19 thread_id,
20 project_id,
21 title,
22 model_selection_json,
23 runtime_mode,
24 interaction_mode,
25 branch,
26 worktree_path,
27 latest_turn_id,
28 created_at,
29 updated_at,
30 archived_at,
31 latest_user_message_at,
32 pending_approval_count,
33 pending_user_input_count,
34 has_actionable_proposed_plan,
35 deleted_at
36 )
37 VALUES (
38 'thread-1',
39 'project-1',
40 'Thread 1',
41 '{"provider":"codex","model":"gpt-5-codex"}',
42 'approval-required',
43 'plan',
44 NULL,
45 NULL,
46 'turn-1',
47 '2026-02-24T00:00:00.000Z',
48 '2026-02-24T00:00:00.000Z',
49 NULL,
50 NULL,
51 0,
52 0,
53 0,
54 NULL
55 )
56 `;
57​
58 yield* sql`
59 INSERT INTO projection_thread_messages (
60 message_id,
61 thread_id,
62 turn_id,
63 role,
64 text,
65 attachments_json,
66 is_streaming,
67 created_at,
68 updated_at
69 )
70 VALUES (
71 'message-user-1',
72 'thread-1',
73 'turn-1',
74 'user',
75 'Need help',
76 NULL,
77 0,
78 '2026-02-24T00:01:00.000Z',
79 '2026-02-24T00:01:00.000Z'
80 )
81 `;
82​
83 yield* sql`
84 INSERT INTO projection_thread_activities (
85 activity_id,
86 thread_id,
87 turn_id,
88 tone,
89 kind,
90 summary,
91 payload_json,
92 sequence,
93 created_at
94 )
95 VALUES
96 (
97 'activity-approval-requested',
98 'thread-1',
99 'turn-1',
100 'approval',
101 'approval.requested',
102 'Command approval requested',
103 '{"requestId":"approval-1","requestKind":"command"}',
104 NULL,
105 '2026-02-24T00:02:00.000Z'
106 ),
107 (
108 'activity-approval-stale',
109 'thread-1',
110 'turn-1',
111 'error',
112 'provider.approval.respond.failed',
113 'Provider approval response failed',
114 '{"requestId":"approval-1","detail":"Unknown pending permission request: approval-1"}',
115 NULL,
116 '2026-02-24T00:03:00.000Z'
117 ),
118 (
119 'activity-user-input-requested',
120 'thread-1',
121 'turn-1',
122 'info',
123 'user-input.requested',
124 'User input requested',
125 '{"requestId":"input-1","questions":[{"id":"area","header":"Area","question":"Which repo area should I inspect next?","options":[{"label":"Server","description":"Server orchestration."}]}]}',
126 NULL,
127 '2026-02-24T00:04:00.000Z'
128 )
129 `;
130​
131 yield* sql`
132 INSERT INTO projection_thread_proposed_plans (
133 plan_id,
134 thread_id,
135 turn_id,
136 plan_markdown,
137 implemented_at,
138 implementation_thread_id,
139 created_at,
140 updated_at
141 )
142 VALUES (
143 'plan-1',
144 'thread-1',
145 'turn-1',
146 '# Do the thing',
147 NULL,
148 NULL,
149 '2026-02-24T00:05:00.000Z',
150 '2026-02-24T00:05:00.000Z'
151 )
152 `;
153​
154 yield* sql`
155 INSERT INTO projection_pending_approvals (
156 request_id,
157 thread_id,
158 turn_id,
159 status,
160 decision,
161 created_at,
162 resolved_at
163 )
164 VALUES (
165 'approval-1',
166 'thread-1',
167 'turn-1',
168 'pending',
169 NULL,
170 '2026-02-24T00:02:00.000Z',
171 NULL
172 )
173 `;
174​
175 yield* runMigrations({ toMigrationInclusive: 24 });
176​
177 const threadRows = yield* sql<{
178 readonly latestUserMessageAt: string | null;
179 readonly pendingApprovalCount: number;
180 readonly pendingUserInputCount: number;
181 readonly hasActionableProposedPlan: number;
182 }>`
183 SELECT
184 latest_user_message_at AS "latestUserMessageAt",
185 pending_approval_count AS "pendingApprovalCount",
186 pending_user_input_count AS "pendingUserInputCount",
187 has_actionable_proposed_plan AS "hasActionableProposedPlan"
188 FROM projection_threads
189 WHERE thread_id = 'thread-1'
190 `;
191 assert.deepStrictEqual(threadRows, [
192 {
193 latestUserMessageAt: "2026-02-24T00:01:00.000Z",
194 pendingApprovalCount: 0,
195 pendingUserInputCount: 1,
196 hasActionableProposedPlan: 1,
197 },
198 ]);
199​
200 const approvalRows = yield* sql<{
201 readonly status: string;
202 readonly resolvedAt: string | null;
203 }>`
204 SELECT
205 status,
206 resolved_at AS "resolvedAt"
207 FROM projection_pending_approvals
208 WHERE request_id = 'approval-1'
209 `;
210 assert.deepStrictEqual(approvalRows, [
211 {
212 status: "resolved",
213 resolvedAt: "2026-02-24T00:03:00.000Z",
214 },
215 ]);
216 }),
217 );
218});