|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as SqlClient from "effect/sql/SqlClient"; |
| 5 | + |
| 6 | +import { runMigrations } from "../Migrations.ts"; |
| 7 | + |
| 8 | +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); |
| 9 | +const createdAt = "2026-11-15T00:00:00.000Z"; |
| 10 | +const group = { |
| 11 | + status: "active", |
| 12 | + primary: { model: "gpt-5.4", label: "GPT", handle: "gpt" }, |
| 13 | + seats: [ |
| 14 | + { |
| 15 | + threadId: "thread:seat", |
| 16 | + handle: "claude", |
| 17 | + label: "Claude", |
| 18 | + status: "seated", |
| 19 | + joinedAfterRun: 1, |
| 20 | + }, |
| 21 | + ], |
| 22 | + discussion: { mode: "everyone", turnsLeft: 2, delivering: ["primary"] }, |
| 23 | + closedBecause: null, |
| 24 | + closedAt: null, |
| 25 | +}; |
| 26 | + |
| 27 | +function threadPayload(id: string, lineage: object, extras: object = {}) { |
| 28 | + return { |
| 29 | + id, |
| 30 | + projectId: "project-1", |
| 31 | + title: id, |
| 32 | + providerInstanceId: "codex", |
| 33 | + modelSelection: { instanceId: "codex", model: "gpt-5.4" }, |
| 34 | + runtimeMode: "full-access", |
| 35 | + interactionMode: "default", |
| 36 | + createdBy: "user", |
| 37 | + creationSource: "web", |
| 38 | + branch: null, |
| 39 | + worktreePath: null, |
| 40 | + activeProviderThreadId: null, |
| 41 | + lineage, |
| 42 | + forkedFrom: null, |
| 43 | + createdAt, |
| 44 | + updatedAt: createdAt, |
| 45 | + archivedAt: null, |
| 46 | + deletedAt: null, |
| 47 | + settledOverride: null, |
| 48 | + settledAt: null, |
| 49 | + group, |
| 50 | + ...extras, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +layer("113_RepairSubagentGroups", (it) => { |
| 55 | + it.effect("removes a copied group from a subagent and leaves the owner and the seat", () => |
| 56 | + Effect.gen(function* () { |
| 57 | + const sql = yield* SqlClient.SqlClient; |
| 58 | + yield* runMigrations({ toMigrationInclusive: 112 }); |
| 59 | + const rows: ReadonlyArray<readonly [string, object]> = [ |
| 60 | + [ |
| 61 | + "owner", |
| 62 | + threadPayload("owner", { |
| 63 | + parentThreadId: null, |
| 64 | + relationshipToParent: null, |
| 65 | + rootThreadId: "owner", |
| 66 | + }), |
| 67 | + ], |
| 68 | + [ |
| 69 | + "seat", |
| 70 | + threadPayload("seat", { |
| 71 | + parentThreadId: "owner", |
| 72 | + relationshipToParent: "fork", |
| 73 | + rootThreadId: "owner", |
| 74 | + sideChat: true, |
| 75 | + }), |
| 76 | + ], |
| 77 | + [ |
| 78 | + "subagent", |
| 79 | + threadPayload( |
| 80 | + "subagent", |
| 81 | + { |
| 82 | + parentThreadId: "owner", |
| 83 | + relationshipToParent: "subagent", |
| 84 | + rootThreadId: "owner", |
| 85 | + }, |
| 86 | + { |
| 87 | + lastSnoozeWakeAt: createdAt, |
| 88 | + activeOrderKey: "parent-active", |
| 89 | + opens: { openedAt: 1_753_348_800_000, score: 4, picks: [] }, |
| 90 | + }, |
| 91 | + ), |
| 92 | + ], |
| 93 | + ]; |
| 94 | + for (const [id, payload] of rows) { |
| 95 | + yield* sql` |
| 96 | + INSERT INTO orchestration_v2_projection_threads ( |
| 97 | + thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, |
| 98 | + created_at, updated_at, payload_json |
| 99 | + ) VALUES ( |
| 100 | + ${id}, 'project-1', ${id}, 'codex', 'full-access', 'default', |
| 101 | + ${createdAt}, ${createdAt}, ${JSON.stringify(payload)} |
| 102 | + ) |
| 103 | + `; |
| 104 | + } |
| 105 | + |
| 106 | + const read = sql<{ |
| 107 | + readonly thread_id: string; |
| 108 | + readonly payload_json: string; |
| 109 | + readonly group_type: string | null; |
| 110 | + }>` |
| 111 | + SELECT thread_id, payload_json, json_type(payload_json, '$.group') AS group_type |
| 112 | + FROM orchestration_v2_projection_threads |
| 113 | + ORDER BY thread_id |
| 114 | + `; |
| 115 | + const before = yield* read; |
| 116 | + assert.deepStrictEqual( |
| 117 | + before.map((row) => [row.thread_id, row.group_type]), |
| 118 | + [ |
| 119 | + ["owner", "object"], |
| 120 | + ["seat", "object"], |
| 121 | + ["subagent", "object"], |
| 122 | + ], |
| 123 | + ); |
| 124 | + |
| 125 | + yield* runMigrations({ toMigrationInclusive: 113 }); |
| 126 | + const after = new Map((yield* read).map((row) => [row.thread_id, row])); |
| 127 | + for (const id of ["owner", "seat"]) { |
| 128 | + const earlier = before.find((row) => row.thread_id === id); |
| 129 | + assert.strictEqual(after.get(id)?.payload_json, earlier?.payload_json); |
| 130 | + assert.strictEqual(after.get(id)?.group_type, "object"); |
| 131 | + } |
| 132 | + const subagent = after.get("subagent"); |
| 133 | + assert.strictEqual(subagent?.group_type, null); |
| 134 | + const kept = yield* sql<{ |
| 135 | + readonly wake: string | null; |
| 136 | + readonly order_key: string | null; |
| 137 | + readonly opens_score: number | null; |
| 138 | + }>` |
| 139 | + SELECT json_extract(payload_json, '$.lastSnoozeWakeAt') AS wake, |
| 140 | + json_extract(payload_json, '$.activeOrderKey') AS order_key, |
| 141 | + json_extract(payload_json, '$.opens.score') AS opens_score |
| 142 | + FROM orchestration_v2_projection_threads |
| 143 | + WHERE thread_id = 'subagent' |
| 144 | + `; |
| 145 | + assert.deepStrictEqual(kept, [ |
| 146 | + { wake: createdAt, order_key: "parent-active", opens_score: 4 }, |
| 147 | + ]); |
| 148 | + }), |
| 149 | + ); |
| 150 | +}); |
0 commit comments