The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import { OrchestrationV2AppThreadJson } from "@t3tools/contracts"; |
| 3 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import * as SqlClient from "effect/sql/SqlClient"; |
| 7 | |
| 8 | import { runMigrations } from "../Migrations.ts"; |
| 9 | |
| 10 | const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); |
| 11 | const createdAt = "2026-10-05T08:45:02.809Z"; |
| 12 | const decodeThreadPayload = Schema.decodeUnknownEffect( |
| 13 | Schema.fromJsonString(OrchestrationV2AppThreadJson), |
| 14 | ); |
| 15 | |
| 16 | layer("096_RepairStaleTitleRegeneration", (it) => { |
| 17 | it.effect("clears title markers that no pending or running title job will complete", () => |
| 18 | Effect.gen(function* () { |
| 19 | const sql = yield* SqlClient.SqlClient; |
| 20 | yield* runMigrations({ toMigrationInclusive: 95 }); |
| 21 | |
| 22 | const insertThread = (id: string, titleRegeneration: unknown, parentThreadId?: string) => { |
| 23 | const payload = { |
| 24 | id, |
| 25 | projectId: "project-1", |
| 26 | title: id, |
| 27 | providerInstanceId: "codex", |
| 28 | modelSelection: { instanceId: "codex", model: "gpt-5.4" }, |
| 29 | runtimeMode: "full-access", |
| 30 | interactionMode: "default", |
| 31 | createdBy: parentThreadId === undefined ? "user" : "agent", |
| 32 | creationSource: "web", |
| 33 | branch: null, |
| 34 | worktreePath: null, |
| 35 | activeProviderThreadId: null, |
| 36 | lineage: { |
| 37 | parentThreadId: parentThreadId ?? null, |
| 38 | relationshipToParent: parentThreadId === undefined ? null : "subagent", |
| 39 | rootThreadId: parentThreadId ?? id, |
| 40 | }, |
| 41 | forkedFrom: null, |
| 42 | createdAt, |
| 43 | updatedAt: createdAt, |
| 44 | archivedAt: null, |
| 45 | deletedAt: null, |
| 46 | titleRegeneration, |
| 47 | }; |
| 48 | return sql` |
| 49 | INSERT INTO orchestration_v2_projection_threads ( |
| 50 | thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, |
| 51 | created_at, updated_at, payload_json |
| 52 | ) VALUES ( |
| 53 | ${id}, 'project-1', ${id}, 'codex', 'full-access', 'default', |
| 54 | ${createdAt}, ${createdAt}, ${JSON.stringify(payload)} |
| 55 | ) |
| 56 | `; |
| 57 | }; |
| 58 | const insertTitleJob = (threadId: string, commandId: string, status: string) => |
| 59 | sql` |
| 60 | INSERT INTO orchestration_v2_effect_outbox ( |
| 61 | effect_id, command_id, thread_id, effect_type, payload_json, status, |
| 62 | available_at, created_at, updated_at |
| 63 | ) VALUES ( |
| 64 | ${`effect:${commandId}:${threadId}`}, ${commandId}, ${threadId}, |
| 65 | 'thread-title.generate', '{}', ${status}, ${createdAt}, ${createdAt}, ${createdAt} |
| 66 | ) |
| 67 | `; |
| 68 | const marker = (requestId: string) => ({ requestId, startedAt: createdAt }); |
| 69 | |
| 70 | // A subagent copied its parent's marker, and only the parent had a title job. |
| 71 | yield* insertThread("parent", null); |
| 72 | yield* insertTitleJob("parent", "parent:initial-message", "succeeded"); |
| 73 | yield* insertThread("subagent", marker("parent:initial-message"), "parent"); |
| 74 | // A subagent whose parent's title job is still queued has no job of its own either. |
| 75 | yield* insertThread("queued-parent", marker("queued:initial-message")); |
| 76 | yield* insertTitleJob("queued-parent", "queued:initial-message", "pending"); |
| 77 | yield* insertThread("queued-subagent", marker("queued:initial-message"), "queued-parent"); |
| 78 | // Jobs that finished without clearing the marker will not run again. |
| 79 | yield* insertThread("failed-job", marker("failed:regenerate")); |
| 80 | yield* insertTitleJob("failed-job", "failed:regenerate", "failed"); |
| 81 | yield* insertThread("cancelled-job", marker("cancelled:regenerate")); |
| 82 | yield* insertTitleJob("cancelled-job", "cancelled:regenerate", "cancelled"); |
| 83 | // A live job for an older request cannot clear a newer marker. |
| 84 | yield* insertThread("superseded-job", marker("new:regenerate")); |
| 85 | yield* insertTitleJob("superseded-job", "old:regenerate", "running"); |
| 86 | // Live jobs still complete their own marker. |
| 87 | yield* insertThread("running-job", marker("running:regenerate")); |
| 88 | yield* insertTitleJob("running-job", "running:regenerate", "running"); |
| 89 | yield* insertThread("absent", undefined); |
| 90 | |
| 91 | const rows = sql<{ readonly thread_id: string; readonly payload_json: string }>` |
| 92 | SELECT thread_id, payload_json FROM orchestration_v2_projection_threads ORDER BY thread_id |
| 93 | `; |
| 94 | const before = new Map((yield* rows).map((row) => [row.thread_id, row.payload_json])); |
| 95 | |
| 96 | yield* runMigrations({ toMigrationInclusive: 96 }); |
| 97 | const after = new Map((yield* rows).map((row) => [row.thread_id, row.payload_json])); |
| 98 | for (const id of ["parent", "queued-parent", "running-job", "absent"]) { |
| 99 | assert.strictEqual(after.get(id), before.get(id)); |
| 100 | } |
| 101 | const markers = yield* sql` |
| 102 | SELECT thread_id, json_type(payload_json, '$.titleRegeneration') AS type |
| 103 | FROM orchestration_v2_projection_threads |
| 104 | WHERE thread_id NOT IN ('parent', 'queued-parent', 'running-job', 'absent') |
| 105 | ORDER BY thread_id |
| 106 | `; |
| 107 | assert.deepStrictEqual(markers, [ |
| 108 | { thread_id: "cancelled-job", type: "null" }, |
| 109 | { thread_id: "failed-job", type: "null" }, |
| 110 | { thread_id: "queued-subagent", type: "null" }, |
| 111 | { thread_id: "subagent", type: "null" }, |
| 112 | { thread_id: "superseded-job", type: "null" }, |
| 113 | ]); |
| 114 | const subagent = yield* decodeThreadPayload(after.get("subagent")); |
| 115 | assert.strictEqual(subagent.titleRegeneration, null); |
| 116 | assert.strictEqual(subagent.title, "subagent"); |
| 117 | }), |
| 118 | ); |
| 119 | }); |