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

ProjectionSettlement.test.ts

525 lines19.2 KB

1import { assert, it } from "@effect/vitest";
2import {
3 EventId,
4 MessageId,
5 NodeId,
6 type OrchestrationV2AppThread,
7 type OrchestrationV2Run,
8 ProjectId,
9 ProviderDriverKind,
10 ProviderInstanceId,
11 ProviderThreadId,
12 RunId,
13 RuntimeRequestId,
14 ThreadId,
15 TurnItemId,
16} from "@t3tools/contracts";
17import * as DateTime from "effect/DateTime";
18import * as Effect from "effect/Effect";
19import * as Layer from "effect/Layer";
20import * as Schema from "effect/Schema";
21import * as SqlClient from "effect/sql/SqlClient";
22import * as Statement from "effect/sql/Statement";
23​
24import * as SqlitePersistence from "../persistence/Sqlite.ts";
25import * as ProjectionStore from "./ProjectionStore.ts";
26import { isAutoSettlementCandidate, resolveAutoSettlementAt } from "./ThreadSettlementService.ts";
27​
28const layerSql = ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory));
29const now = DateTime.makeUnsafe("2026-09-04T12:00:00Z");
30const old = DateTime.subtract(now, { days: 10 });
31const providerInstanceId = ProviderInstanceId.make("codex");
32const modelSelection = { instanceId: providerInstanceId, model: "test-model" };
33const encodeNumbers = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.Number)));
34​
35const createThread = Effect.fn(function* (
36 name: string,
37 overrides: Partial<OrchestrationV2AppThread> = {},
38) {
39 const store = yield* ProjectionStore.ProjectionStoreV2;
40 const threadId = ThreadId.make(`thread:settlement:${name}`);
41 const thread: OrchestrationV2AppThread = {
42 createdBy: "user",
43 creationSource: "web",
44 id: threadId,
45 projectId: ProjectId.make("project:settlement"),
46 title: name,
47 providerInstanceId,
48 modelSelection,
49 runtimeMode: "full-access",
50 interactionMode: "default",
51 branch: "feature",
52 worktreePath: null,
53 activeProviderThreadId: null,
54 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
55 forkedFrom: null,
56 createdAt: old,
57 updatedAt: old,
58 archivedAt: null,
59 settledOverride: null,
60 settledAt: null,
61 lastVisitedAt: null,
62 deletedAt: null,
63 ...overrides,
64 };
65 yield* store.apply({
66 id: EventId.make(`event:${threadId}`),
67 type: "thread.created",
68 threadId,
69 occurredAt: old,
70 payload: thread,
71 });
72 return threadId;
73});
74​
75const createRun = Effect.fn(function* (
76 threadId: ThreadId,
77 status: OrchestrationV2Run["status"] = "completed",
78 ordinal = 1,
79) {
80 const store = yield* ProjectionStore.ProjectionStoreV2;
81 const runId = RunId.make(`run:${threadId}:${ordinal}`);
82 yield* store.apply({
83 id: EventId.make(`event:${runId}`),
84 type: "run.created",
85 threadId,
86 occurredAt: old,
87 payload: {
88 id: runId,
89 threadId,
90 ordinal,
91 providerInstanceId,
92 modelSelection,
93 providerThreadId: null,
94 userMessageId: MessageId.make(`message:${runId}`),
95 rootNodeId: null,
96 activeAttemptId: null,
97 status,
98 requestedAt: old,
99 startedAt: status === "queued" ? null : old,
100 completedAt: ["completed", "rolled_back", "failed"].includes(status) ? old : null,
101 checkpointId: null,
102 contextHandoffId: null,
103 },
104 });
105 return runId;
106});
107​
108const createItem = Effect.fn(function* (
109 threadId: ThreadId,
110 runId: RunId,
111 status: "idle" | "running" | "completed",
112 persistent = false,
113) {
114 const store = yield* ProjectionStore.ProjectionStoreV2;
115 yield* store.apply({
116 id: EventId.make(`event:item:${runId}`),
117 type: "turn-item.updated",
118 threadId,
119 occurredAt: old,
120 payload: {
121 id: TurnItemId.make(`item:${runId}`),
122 threadId,
123 runId,
124 nodeId: null,
125 providerThreadId: null,
126 providerTurnId: null,
127 nativeItemRef: null,
128 parentItemId: null,
129 ordinal: 1,
130 type: "dynamic_tool",
131 status,
132 title: null,
133 startedAt: old,
134 completedAt: status === "completed" ? old : null,
135 updatedAt: old,
136 toolName: "background",
137 input: persistent ? { persistent: true } : null,
138 output: null,
139 },
140 });
141});
142​
143it.effect.each([
144 ["sql", layerSql],
145 ["memory", ProjectionStore.layerMemory],
146] as const)(
147 "%s: discovers settlement work with the same activity and background semantics as the shell",
148 ([, testLayer]) =>
149 Effect.gen(function* () {
150 const store = yield* ProjectionStore.ProjectionStoreV2;
151 const idle = yield* createThread("idle");
152 const completed = yield* createThread("completed");
153 yield* createRun(completed);
154 for (const [name, overrides] of [
155 ["archived", { archivedAt: old }],
156 ["deleted", { deletedAt: old }],
157 ["settled", { settledOverride: "settled" }],
158 ["unsettled", { settledOverride: "active" }],
159 ["pinned", { pinnedAt: old }],
160 ["auto-settle-disabled", { autoSettleDisabledAt: old }],
161 ] satisfies ReadonlyArray<readonly [string, Partial<OrchestrationV2AppThread>]>) {
162 yield* createRun(yield* createThread(name, overrides));
163 }
164 for (const status of ["preparing", "starting", "running", "waiting"] as const) {
165 yield* createRun(yield* createThread(status), status);
166 }
167 const queued = yield* createThread("queued");
168 yield* createRun(queued, "queued");
169 const blocked = yield* createThread("blocked");
170 yield* store.apply({
171 id: EventId.make("event:settlement:request"),
172 type: "runtime-request.updated",
173 threadId: blocked,
174 occurredAt: old,
175 payload: {
176 id: RuntimeRequestId.make("request:settlement"),
177 nodeId: NodeId.make("node:settlement"),
178 providerTurnId: null,
179 nativeRequestRef: null,
180 kind: "user_input",
181 status: "pending",
182 responseCapability: { type: "not_resumable", reason: "Process stopped" },
183 createdAt: old,
184 resolvedAt: null,
185 },
186 });
187 const recent = yield* createThread("recent-message");
188 yield* createRun(recent);
189 yield* store.apply({
190 id: EventId.make("event:settlement:message"),
191 type: "message.updated",
192 threadId: recent,
193 occurredAt: now,
194 payload: {
195 createdBy: "user",
196 creationSource: "web",
197 id: MessageId.make("message:settlement:recent"),
198 threadId: recent,
199 runId: null,
200 nodeId: null,
201 role: "user",
202 text: "Next turn",
203 attachments: [],
204 streaming: false,
205 createdAt: now,
206 updatedAt: now,
207 },
208 });
209 yield* createRun(
210 yield* createThread("snoozed", {
211 snoozedUntil: DateTime.add(now, { days: 1 }),
212 snoozedAt: now,
213 }),
214 );
215 const woke = yield* createThread("woke", {
216 snoozedUntil: DateTime.add(now, { days: 1 }),
217 snoozedAt: DateTime.subtract(old, { days: 1 }),
218 });
219 yield* createRun(woke, "failed");
220 const background = yield* createThread("idle-background");
221 yield* createItem(background, yield* createRun(background), "idle");
222 const persistent = yield* createThread("persistent-monitor");
223 yield* createItem(persistent, yield* createRun(persistent), "running", true);
224 const rolledBack = yield* createThread("rolled-back-background");
225 yield* createItem(rolledBack, yield* createRun(rolledBack, "rolled_back"), "running");
226 yield* createRun(rolledBack, "completed", 2);
227 const roster = yield* createThread("provider-roster");
228 yield* createRun(roster);
229 yield* store.apply({
230 id: EventId.make("event:settlement:roster"),
231 type: "provider-thread.updated",
232 threadId: roster,
233 occurredAt: old,
234 payload: {
235 id: ProviderThreadId.make("provider-thread:settlement"),
236 appThreadId: roster,
237 ownerNodeId: null,
238 driver: ProviderDriverKind.make("codex"),
239 providerInstanceId,
240 providerSessionId: null,
241 nativeThreadRef: null,
242 nativeConversationHeadRef: null,
243 status: "idle",
244 firstRunOrdinal: null,
245 lastRunOrdinal: null,
246 handoffIds: [],
247 forkedFrom: null,
248 createdAt: old,
249 updatedAt: old,
250 pendingBackgroundTasks: [{ taskId: "running-task", kind: "subagent" }],
251 },
252 });
253 const candidates = yield* store.getSettlementCandidates();
254 const shell = yield* store.getShellSnapshot({ location: "active" });
255 const eligible = candidates.filter((thread) =>
256 isAutoSettlementCandidate(thread, DateTime.toEpochMillis(now)),
257 );
258 assert.deepEqual(
259 new Set(eligible.map((thread) => thread.id)),
260 new Set([idle, completed, queued, woke, background, persistent, rolledBack]),
261 );
262 assert.deepEqual(
263 new Set(eligible.map((thread) => thread.id)),
264 new Set(
265 shell.threads
266 .filter((thread) => isAutoSettlementCandidate(thread, DateTime.toEpochMillis(now)))
267 .map((thread) => thread.id),
268 ),
269 );
270 for (const candidate of candidates) {
271 const expected = shell.threads.find((thread) => thread.id === candidate.id)!;
272 assert.deepEqual(candidate.pendingBackgroundTasks, expected.pendingBackgroundTasks);
273 const settings = {
274 pullRequest: null,
275 nowMs: DateTime.toEpochMillis(now),
276 autoSettleAfterDays: 7,
277 autoSettleOnMerge: false,
278 };
279 assert.deepEqual(
280 resolveAutoSettlementAt({ ...settings, thread: candidate }),
281 resolveAutoSettlementAt({
282 ...settings,
283 thread: {
284 ...expected,
285 latestUserAuthoredMessageAt: candidate.latestUserAuthoredMessageAt,
286 },
287 }),
288 );
289 }
290 assert.equal(
291 DateTime.formatIso((yield* store.getThread(completed)).createdAt),
292 DateTime.formatIso(old),
293 );
294 assert.equal(
295 (yield* store.getThread(ThreadId.make("missing")).pipe(Effect.flip))._tag,
296 "ProjectionStoreThreadNotFoundError",
297 );
298 }).pipe(Effect.provide(testLayer)),
299);
300​
301it.effect.each([
302 ["sql", layerSql],
303 ["memory", ProjectionStore.layerMemory],
304] as const)("%s: the user-authored message time ignores agent notifications", ([, testLayer]) =>
305 Effect.gen(function* () {
306 const store = yield* ProjectionStore.ProjectionStoreV2;
307 const threadId = yield* createThread("agent-woken");
308 yield* createRun(threadId);
309 const message = (
310 id: string,
311 createdBy: "user" | "agent",
312 creationSource: "web" | "provider",
313 at: DateTime.Utc,
314 ) =>
315 store.apply({
316 id: EventId.make(`event:settlement:${id}`),
317 type: "message.updated",
318 threadId,
319 occurredAt: at,
320 payload: {
321 createdBy,
322 creationSource,
323 id: MessageId.make(`message:settlement:${id}`),
324 threadId,
325 runId: null,
326 nodeId: null,
327 role: "user",
328 text: id,
329 attachments: [],
330 streaming: false,
331 createdAt: at,
332 updatedAt: at,
333 },
334 });
335 const written = DateTime.subtract(now, { hours: 2 });
336 yield* message("written", "user", "web", written);
337 yield* message("notification", "agent", "provider", now);
338​
339 const [candidate] = yield* store.getSettlementCandidates(threadId);
340 assert.isDefined(candidate);
341 assert.equal(DateTime.formatIso(candidate.latestUserMessageAt!), DateTime.formatIso(now));
342 assert.equal(
343 DateTime.formatIso(candidate.latestUserAuthoredMessageAt!),
344 DateTime.formatIso(written),
345 );
346 const shellThread = (yield* store.getShellSnapshot()).threads.find(
347 (thread) => thread.id === threadId,
348 );
349 assert.equal(
350 DateTime.formatIso(shellThread!.latestUserAuthoredMessageAt!),
351 DateTime.formatIso(written),
352 );
353 }).pipe(Effect.provide(testLayer)),
354);
355​
356const pullRequestLink = (number: number) => ({
357 host: "github.com",
358 repository: "owner/repository",
359 number,
360 url: `https://github.com/owner/repository/pull/${number}`,
361 source: "manual" as const,
362 linkedAt: DateTime.formatIso(old),
363 snapshot: null,
364 stack: null,
365});
366​
367it.effect.each([
368 ["sql", layerSql],
369 ["memory", ProjectionStore.layerMemory],
370] as const)(
371 "%s: lists only active threads with pull request links, oldest first",
372 ([, testLayer]) =>
373 Effect.gen(function* () {
374 const store = yield* ProjectionStore.ProjectionStoreV2;
375 yield* createThread("no-links");
376 yield* createThread("empty-links", { pullRequests: [] });
377 yield* createThread("archived-link", {
378 archivedAt: old,
379 pullRequests: [pullRequestLink(1)],
380 });
381 const settled = yield* createThread("settled-link", {
382 settledOverride: "settled",
383 settledAt: old,
384 updatedAt: DateTime.subtract(now, { days: 12 }),
385 pullRequests: [pullRequestLink(2)],
386 });
387 const open = yield* createThread("open-link", {
388 pullRequests: [pullRequestLink(3), pullRequestLink(4)],
389 });
390​
391 const threads = yield* store.getThreadsWithPullRequests();
392 assert.deepEqual(
393 threads.map((thread) => [thread.id, thread.settledOverride, thread.pullRequests?.length]),
394 [
395 [settled, "settled", 1],
396 [open, null, 2],
397 ],
398 );
399 assert.deepEqual(
400 (yield* store.getThreadsWithPullRequests(open)).map((thread) => thread.id),
401 [open],
402 );
403 }).pipe(Effect.provide(testLayer)),
404);
405​
406it.effect.each([
407 ["sql", layerSql],
408 ["memory", ProjectionStore.layerMemory],
409] as const)("%s: an unsettled-only shell read skips settled threads", ([, testLayer]) =>
410 Effect.gen(function* () {
411 const store = yield* ProjectionStore.ProjectionStoreV2;
412 const open = yield* createThread("unsettled-open");
413 const reopened = yield* createThread("unsettled-reopened", { settledOverride: "active" });
414 yield* createThread("unsettled-manual", { settledOverride: "settled", settledAt: old });
415 yield* createThread("unsettled-auto", { settledAt: old });
416 yield* createThread("unsettled-archived", { archivedAt: old });
417​
418 const shell = yield* store.getShellSnapshot({ location: "active", unsettledOnly: true });
419 assert.deepEqual(new Set(shell.threads.map((thread) => thread.id)), new Set([open, reopened]));
420 assert.equal(shell.archivedThreads.length, 0);
421 const all = yield* store.getShellSnapshot({ location: "active" });
422 assert.equal(all.threads.length, 4);
423 }).pipe(Effect.provide(testLayer)),
424);
425​
426it.effect(
427 "reads settlement candidates and thread metadata without loading historical or archived payloads",
428 () =>
429 Effect.gen(function* () {
430 const store = yield* ProjectionStore.ProjectionStoreV2;
431 const sql = yield* SqlClient.SqlClient;
432 const source = yield* createThread("historical-source", { archivedAt: old });
433 const sourceRun = yield* createRun(source);
434 const threadId = yield* createThread("fork", {
435 forkedFrom: { type: "run", threadId: source, runId: sourceRun },
436 });
437 const runId = yield* createRun(threadId);
438 yield* createItem(threadId, runId, "completed");
439 yield* sql`UPDATE orchestration_v2_projection_turn_items SET payload_json = '{broken' WHERE thread_id = ${threadId}`;
440 yield* sql`UPDATE orchestration_v2_projection_runs SET payload_json = '{broken' WHERE thread_id = ${source}`;
441 yield* sql`INSERT INTO orchestration_v2_projection_turn_items
442 SELECT 'history:' || value, i.thread_id, i.run_id, i.node_id, i.provider_thread_id, i.provider_turn_id,
443 i.parent_item_id, i.ordinal + value, i.type, i.status, i.updated_at, i.payload_json
444 FROM orchestration_v2_projection_turn_items i, json_each(${encodeNumbers(Array.from({ length: 2000 }, (_, i) => i + 1))})
445 WHERE i.thread_id = ${threadId}`;
446 const queries: Array<readonly [string, ReadonlyArray<unknown>]> = [];
447 const record: Statement.Transformer = (statement) =>
448 Effect.sync(() => {
449 queries.push(statement.compile());
450 return statement;
451 });
452 const candidates = yield* store
453 .getSettlementCandidates()
454 .pipe(Effect.provideService(Statement.CurrentTransformer, record));
455 assert.deepEqual(
456 candidates.map((thread) => thread.id),
457 [threadId],
458 );
459 assert.isTrue(queries.every(([query]) => !query.includes("COUNT(")));
460 const pendingQuery = queries.find(([query]) =>
461 query.includes("FROM orchestration_v2_projection_turn_items"),
462 );
463 assert.isDefined(pendingQuery);
464 const metadata = yield* store.getThread(threadId);
465 assert.equal(metadata.id, threadId);
466 assert.equal((yield* store.getThreadProjection(threadId).pipe(Effect.exit))._tag, "Failure");
467 const plan = yield* sql.unsafe<{ readonly detail: string }>(
468 `EXPLAIN QUERY PLAN ${pendingQuery![0]}`,
469 pendingQuery![1],
470 );
471 assert.isTrue(plan.some((row) => row.detail.includes("turn_items_shell_pending_idx")));
472 const countsPlan = yield* sql<{ readonly detail: string }>`EXPLAIN QUERY PLAN
473 SELECT thread_id, run_id, COUNT(*) FROM orchestration_v2_projection_turn_items
474 WHERE thread_id = ${threadId} AND run_id IS NOT NULL GROUP BY thread_id, run_id`;
475 assert.isTrue(
476 countsPlan.some((row) =>
477 row.detail.includes(
478 "COVERING INDEX orchestration_v2_projection_turn_items_thread_run_idx",
479 ),
480 ),
481 );
482 assert.isFalse(countsPlan.some((row) => row.detail.includes("TEMP B-TREE")));
483 const messagePlan = yield* sql<{ readonly detail: string }>`EXPLAIN QUERY PLAN
484 SELECT updated_at FROM orchestration_v2_projection_messages
485 WHERE thread_id = ${threadId} AND role = 'user' ORDER BY updated_at DESC, message_id DESC LIMIT 1`;
486 assert.isTrue(messagePlan.some((row) => row.detail.includes("messages_latest_user_idx")));
487 assert.isFalse(messagePlan.some((row) => row.detail.includes("TEMP B-TREE")));
488 }).pipe(Effect.provide(layerSql)),
489);
490​
491it.effect("shell failure lookups stay on the thread's own turn items", () =>
492 Effect.gen(function* () {
493 const store = yield* ProjectionStore.ProjectionStoreV2;
494 const sql = yield* SqlClient.SqlClient;
495 const failed = yield* createThread("failed-latest");
496 yield* createRun(failed, "failed");
497 const queries: Array<readonly [string, ReadonlyArray<unknown>]> = [];
498 const record: Statement.Transformer = (statement) =>
499 Effect.sync(() => {
500 queries.push(statement.compile());
501 return statement;
502 });
503 const shell = yield* store
504 .getShellSnapshot()
505 .pipe(Effect.provideService(Statement.CurrentTransformer, record));
506 assert.deepEqual(
507 shell.threads.map((thread) => thread.id),
508 [failed],
509 );
510 const shellQuery = queries.find(([query]) =>
511 query.includes("AS blocking_failure_payload_json"),
512 );
513 assert.isDefined(shellQuery);
514 const plan = yield* sql.unsafe<{ readonly detail: string }>(
515 `EXPLAIN QUERY PLAN ${shellQuery![0]}`,
516 shellQuery![1],
517 );
518 const itemLookups = plan.filter((row) => row.detail.startsWith("SEARCH item "));
519 assert.lengthOf(itemLookups, 2);
520 assert.isTrue(itemLookups.every((row) => row.detail.includes("turn_items_failed_error_idx")));
521 const secretLookups = plan.filter((row) => row.detail.startsWith("SEARCH secret "));
522 assert.lengthOf(secretLookups, 1);
523 assert.include(secretLookups[0]!.detail, "turn_items_thread_run_idx");
524 }).pipe(Effect.provide(layerSql)),
525);