Open History to see the last commit
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/unstable/sql/SqlClient";
22import * as Statement from "effect/unstable/sql/Statement";
23
24import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
25import * as ProjectionStore from "./ProjectionStore.ts";
26import { isAutoSettlementCandidate, resolveAutoSettlementAt } from "./ThreadSettlementService.ts";
27
28const SqlLayer = ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistenceMemory));
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", SqlLayer],
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);
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: "command" }],
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({ ...settings, thread: expected }),
282 );
283 }
284 assert.equal(
285 DateTime.formatIso((yield* store.getThread(completed)).createdAt),
286 DateTime.formatIso(old),
287 );
288 assert.equal(
289 (yield* store.getThread(ThreadId.make("missing")).pipe(Effect.flip))._tag,
290 "ProjectionStoreThreadNotFoundError",
291 );
292 }).pipe(Effect.provide(testLayer)),
293);
294
295const pullRequestLink = (number: number) => ({
296 host: "github.com",
297 repository: "owner/repository",
298 number,
299 url: `https://github.com/owner/repository/pull/${number}`,
300 source: "manual" as const,
301 linkedAt: DateTime.formatIso(old),
302 snapshot: null,
303 stack: null,
304});
305
306it.effect.each([
307 ["sql", SqlLayer],
308 ["memory", ProjectionStore.layerMemory],
309] as const)(
310 "%s: lists only active threads with pull request links, oldest first",
311 ([, testLayer]) =>
312 Effect.gen(function* () {
313 const store = yield* ProjectionStore.ProjectionStoreV2;
314 yield* createThread("no-links");
315 yield* createThread("empty-links", { pullRequests: [] });
316 yield* createThread("archived-link", {
317 archivedAt: old,
318 pullRequests: [pullRequestLink(1)],
319 });
320 const settled = yield* createThread("settled-link", {
321 settledOverride: "settled",
322 settledAt: old,
323 updatedAt: DateTime.subtract(now, { days: 12 }),
324 pullRequests: [pullRequestLink(2)],
325 });
326 const open = yield* createThread("open-link", {
327 pullRequests: [pullRequestLink(3), pullRequestLink(4)],
328 });
329
330 const threads = yield* store.getThreadsWithPullRequests();
331 assert.deepEqual(
332 threads.map((thread) => [thread.id, thread.settledOverride, thread.pullRequests?.length]),
333 [
334 [settled, "settled", 1],
335 [open, null, 2],
336 ],
337 );
338 }).pipe(Effect.provide(testLayer)),
339);
340
341it.effect.each([
342 ["sql", SqlLayer],
343 ["memory", ProjectionStore.layerMemory],
344] as const)("%s: an unsettled-only shell read skips settled threads", ([, testLayer]) =>
345 Effect.gen(function* () {
346 const store = yield* ProjectionStore.ProjectionStoreV2;
347 const open = yield* createThread("unsettled-open");
348 const reopened = yield* createThread("unsettled-reopened", { settledOverride: "active" });
349 yield* createThread("unsettled-manual", { settledOverride: "settled", settledAt: old });
350 yield* createThread("unsettled-auto", { settledAt: old });
351 yield* createThread("unsettled-archived", { archivedAt: old });
352
353 const shell = yield* store.getShellSnapshot({ location: "active", unsettledOnly: true });
354 assert.deepEqual(new Set(shell.threads.map((thread) => thread.id)), new Set([open, reopened]));
355 assert.equal(shell.archivedThreads.length, 0);
356 const all = yield* store.getShellSnapshot({ location: "active" });
357 assert.equal(all.threads.length, 4);
358 }).pipe(Effect.provide(testLayer)),
359);
360
361it.effect(
362 "reads settlement candidates and thread metadata without loading historical or archived payloads",
363 () =>
364 Effect.gen(function* () {
365 const store = yield* ProjectionStore.ProjectionStoreV2;
366 const sql = yield* SqlClient.SqlClient;
367 const source = yield* createThread("historical-source", { archivedAt: old });
368 const sourceRun = yield* createRun(source);
369 const threadId = yield* createThread("fork", {
370 forkedFrom: { type: "run", threadId: source, runId: sourceRun },
371 });
372 const runId = yield* createRun(threadId);
373 yield* createItem(threadId, runId, "completed");
374 yield* sql`UPDATE orchestration_v2_projection_turn_items SET payload_json = '{broken' WHERE thread_id = ${threadId}`;
375 yield* sql`UPDATE orchestration_v2_projection_runs SET payload_json = '{broken' WHERE thread_id = ${source}`;
376 yield* sql`INSERT INTO orchestration_v2_projection_turn_items
377 SELECT 'history:' || value, i.thread_id, i.run_id, i.node_id, i.provider_thread_id, i.provider_turn_id,
378 i.parent_item_id, i.ordinal + value, i.type, i.status, i.updated_at, i.payload_json
379 FROM orchestration_v2_projection_turn_items i, json_each(${encodeNumbers(Array.from({ length: 2000 }, (_, i) => i + 1))})
380 WHERE i.thread_id = ${threadId}`;
381 const queries: Array<readonly [string, ReadonlyArray<unknown>]> = [];
382 const record: Statement.Transformer = (statement) =>
383 Effect.sync(() => {
384 queries.push(statement.compile());
385 return statement;
386 });
387 const candidates = yield* store
388 .getSettlementCandidates()
389 .pipe(Effect.provideService(Statement.CurrentTransformer, record));
390 assert.deepEqual(
391 candidates.map((thread) => thread.id),
392 [threadId],
393 );
394 assert.isTrue(queries.every(([query]) => !query.includes("COUNT(")));
395 const pendingQuery = queries.find(([query]) =>
396 query.includes("FROM orchestration_v2_projection_turn_items"),
397 );
398 assert.isDefined(pendingQuery);
399 const metadata = yield* store.getThread(threadId);
400 assert.equal(metadata.id, threadId);
401 assert.equal((yield* store.getThreadProjection(threadId).pipe(Effect.exit))._tag, "Failure");
402 const plan = yield* sql.unsafe<{ readonly detail: string }>(
403 `EXPLAIN QUERY PLAN ${pendingQuery![0]}`,
404 pendingQuery![1],
405 );
406 assert.isTrue(plan.some((row) => row.detail.includes("turn_items_shell_pending_idx")));
407 const countsPlan = yield* sql<{ readonly detail: string }>`EXPLAIN QUERY PLAN
408 SELECT thread_id, run_id, COUNT(*) FROM orchestration_v2_projection_turn_items
409 WHERE thread_id = ${threadId} AND run_id IS NOT NULL GROUP BY thread_id, run_id`;
410 assert.isTrue(
411 countsPlan.some((row) =>
412 row.detail.includes(
413 "COVERING INDEX orchestration_v2_projection_turn_items_thread_run_idx",
414 ),
415 ),
416 );
417 assert.isFalse(countsPlan.some((row) => row.detail.includes("TEMP B-TREE")));
418 const messagePlan = yield* sql<{ readonly detail: string }>`EXPLAIN QUERY PLAN
419 SELECT updated_at FROM orchestration_v2_projection_messages
420 WHERE thread_id = ${threadId} AND role = 'user' ORDER BY updated_at DESC, message_id DESC LIMIT 1`;
421 assert.isTrue(messagePlan.some((row) => row.detail.includes("messages_latest_user_idx")));
422 assert.isFalse(messagePlan.some((row) => row.detail.includes("TEMP B-TREE")));
423 }).pipe(Effect.provide(SqlLayer)),
424);
425
426it.effect("shell failure lookups stay on the thread's own turn items", () =>
427 Effect.gen(function* () {
428 const store = yield* ProjectionStore.ProjectionStoreV2;
429 const sql = yield* SqlClient.SqlClient;
430 const failed = yield* createThread("failed-latest");
431 yield* createRun(failed, "failed");
432 const queries: Array<readonly [string, ReadonlyArray<unknown>]> = [];
433 const record: Statement.Transformer = (statement) =>
434 Effect.sync(() => {
435 queries.push(statement.compile());
436 return statement;
437 });
438 const shell = yield* store
439 .getShellSnapshot()
440 .pipe(Effect.provideService(Statement.CurrentTransformer, record));
441 assert.deepEqual(
442 shell.threads.map((thread) => thread.id),
443 [failed],
444 );
445 const shellQuery = queries.find(([query]) =>
446 query.includes("AS blocking_failure_payload_json"),
447 );
448 assert.isDefined(shellQuery);
449 const plan = yield* sql.unsafe<{ readonly detail: string }>(
450 `EXPLAIN QUERY PLAN ${shellQuery![0]}`,
451 shellQuery![1],
452 );
453 // A failed run's root node is often null, and every runless item shares that
454 // node_id, so a node_ordinal lookup walks the whole history once per thread.
455 const itemLookups = plan.filter((row) => row.detail.startsWith("SEARCH item "));
456 assert.lengthOf(itemLookups, 2);
457 assert.isTrue(itemLookups.every((row) => row.detail.includes("turn_items_thread_run_idx")));
458 }).pipe(Effect.provide(SqlLayer)),
459);