| ... | ... | @@ -1,25 +1,234 @@ |
| 1 | | import { ProjectId, ThreadId, ProviderInstanceId } from "@t3tools/contracts"; |
| 1 | import { ProjectId, ThreadId, TurnId, ProviderInstanceId } from "@t3tools/contracts"; |
| 2 | 2 | import { assert, it } from "@effect/vitest"; |
| 3 | 3 | import * as Effect from "effect/Effect"; |
| 4 | 4 | import * as Layer from "effect/Layer"; |
| 5 | 5 | import * as Option from "effect/Option"; |
| 6 | 6 | import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| 7 | import * as Statement from "effect/unstable/sql/Statement"; |
| 7 | 8 | |
| 8 | 9 | import { SqlitePersistenceMemory } from "./Sqlite.ts"; |
| 9 | 10 | import { ProjectionProjectRepositoryLive } from "./ProjectionProjects.ts"; |
| 10 | 11 | import { ProjectionThreadRepositoryLive } from "./ProjectionThreads.ts"; |
| 12 | import { ProjectionThreadProposedPlanRepositoryLive } from "./ProjectionThreadProposedPlans.ts"; |
| 11 | 13 | import { ProjectionProjectRepository } from "../Services/ProjectionProjects.ts"; |
| 12 | 14 | import { ProjectionThreadRepository } from "../Services/ProjectionThreads.ts"; |
| 15 | import { ProjectionThreadProposedPlanRepository } from "../Services/ProjectionThreadProposedPlans.ts"; |
| 13 | 16 | |
| 14 | 17 | const projectionRepositoriesLayer = it.layer( |
| 15 | 18 | Layer.mergeAll( |
| 16 | 19 | ProjectionProjectRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)), |
| 17 | 20 | ProjectionThreadRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)), |
| 21 | ProjectionThreadProposedPlanRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)), |
| 18 | 22 | SqlitePersistenceMemory, |
| 19 | 23 | ), |
| 20 | 24 | ); |
| 21 | 25 | |
| 22 | 26 | projectionRepositoriesLayer("Projection repositories", (it) => { |
| 27 | it.effect("selects the latest-turn plan before checking implementation status", () => |
| 28 | Effect.gen(function* () { |
| 29 | const plans = yield* ProjectionThreadProposedPlanRepository; |
| 30 | const threadId = ThreadId.make("thread-plan-status"); |
| 31 | const latestTurnId = TurnId.make("turn-plan-status-current"); |
| 32 | const firstPlan = { |
| 33 | planId: "plan-status-first", |
| 34 | threadId, |
| 35 | turnId: latestTurnId, |
| 36 | planMarkdown: "# First plan", |
| 37 | implementedAt: null, |
| 38 | implementationThreadId: null, |
| 39 | createdAt: "2026-03-24T00:00:01.000Z", |
| 40 | updatedAt: "2026-03-24T00:00:01.000Z", |
| 41 | }; |
| 42 | yield* plans.upsert(firstPlan); |
| 43 | yield* plans.upsert({ |
| 44 | ...firstPlan, |
| 45 | planId: "plan-status-implemented", |
| 46 | implementedAt: "2026-03-24T00:00:02.000Z", |
| 47 | createdAt: "2026-03-24T00:00:02.000Z", |
| 48 | updatedAt: "2026-03-24T00:00:02.000Z", |
| 49 | }); |
| 50 | yield* plans.upsert({ |
| 51 | ...firstPlan, |
| 52 | planId: "plan-status-other-turn", |
| 53 | turnId: TurnId.make("turn-plan-status-old"), |
| 54 | updatedAt: "2026-03-24T00:00:10.000Z", |
| 55 | }); |
| 56 | |
| 57 | assert.isFalse(yield* plans.hasActionableByThreadId({ threadId, latestTurnId })); |
| 58 | assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null })); |
| 59 | |
| 60 | yield* plans.upsert({ ...firstPlan, updatedAt: "2026-03-24T00:00:03.000Z" }); |
| 61 | assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId })); |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | it.effect("falls back within the thread when the latest turn has no plan", () => |
| 66 | Effect.gen(function* () { |
| 67 | const plans = yield* ProjectionThreadProposedPlanRepository; |
| 68 | const threadId = ThreadId.make("thread-plan-fallback"); |
| 69 | const latestTurnId = TurnId.make("turn-plan-fallback-missing"); |
| 70 | assert.isFalse(yield* plans.hasActionableByThreadId({ threadId, latestTurnId })); |
| 71 | assert.isFalse(yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null })); |
| 72 | |
| 73 | const firstPlan = { |
| 74 | planId: "plan-fallback-without-turn", |
| 75 | threadId, |
| 76 | turnId: null, |
| 77 | planMarkdown: "# Old plan", |
| 78 | implementedAt: "2026-03-24T00:00:01.000Z", |
| 79 | implementationThreadId: null, |
| 80 | createdAt: "2026-03-24T00:00:01.000Z", |
| 81 | updatedAt: "2026-03-24T00:00:01.000Z", |
| 82 | }; |
| 83 | yield* plans.upsert(firstPlan); |
| 84 | yield* plans.upsert({ |
| 85 | ...firstPlan, |
| 86 | planId: "plan-fallback-with-turn", |
| 87 | turnId: TurnId.make("turn-plan-fallback-old"), |
| 88 | implementedAt: null, |
| 89 | updatedAt: "2026-03-24T00:00:02.000Z", |
| 90 | }); |
| 91 | yield* plans.upsert({ |
| 92 | ...firstPlan, |
| 93 | planId: "plan-fallback-other-thread", |
| 94 | threadId: ThreadId.make("thread-plan-fallback-other"), |
| 95 | turnId: latestTurnId, |
| 96 | updatedAt: "2026-03-24T00:00:03.000Z", |
| 97 | }); |
| 98 | |
| 99 | assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId })); |
| 100 | assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null })); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | it.effect("preserves locale ordering and stable ties when selecting plan status", () => |
| 105 | Effect.gen(function* () { |
| 106 | const plans = yield* ProjectionThreadProposedPlanRepository; |
| 107 | const timestamp = "2026-03-24T00:00:00.000Z"; |
| 108 | const cases = [ |
| 109 | { |
| 110 | name: "mixed-case-ids", |
| 111 | expected: "plan-A".localeCompare("plan-a") > 0, |
| 112 | rows: [ |
| 113 | { |
| 114 | planId: "plan-a", |
| 115 | implementedAt: timestamp, |
| 116 | createdAt: timestamp, |
| 117 | updatedAt: timestamp, |
| 118 | }, |
| 119 | { planId: "plan-A", implementedAt: null, createdAt: timestamp, updatedAt: timestamp }, |
| 120 | ], |
| 121 | }, |
| 122 | { |
| 123 | name: "equivalent-ids", |
| 124 | expected: true, |
| 125 | rows: [ |
| 126 | { |
| 127 | planId: "plan-\u00e9", |
| 128 | implementedAt: timestamp, |
| 129 | createdAt: timestamp, |
| 130 | updatedAt: timestamp, |
| 131 | }, |
| 132 | { |
| 133 | planId: "plan-e\u0301", |
| 134 | implementedAt: null, |
| 135 | createdAt: "2026-03-24T00:00:01.000Z", |
| 136 | updatedAt: timestamp, |
| 137 | }, |
| 138 | ], |
| 139 | }, |
| 140 | { |
| 141 | name: "timestamp-formats", |
| 142 | expected: "2026-03-24T00:00:00+00:00".localeCompare("2026-03-24T00:00:00-01:00") > 0, |
| 143 | rows: [ |
| 144 | { |
| 145 | planId: "plan-minus", |
| 146 | implementedAt: timestamp, |
| 147 | createdAt: timestamp, |
| 148 | updatedAt: "2026-03-24T00:00:00-01:00", |
| 149 | }, |
| 150 | { |
| 151 | planId: "plan-plus", |
| 152 | implementedAt: null, |
| 153 | createdAt: timestamp, |
| 154 | updatedAt: "2026-03-24T00:00:00+00:00", |
| 155 | }, |
| 156 | ], |
| 157 | }, |
| 158 | ]; |
| 159 | for (const testCase of cases) { |
| 160 | const threadId = ThreadId.make(`thread-plan-order-${testCase.name}`); |
| 161 | const latestTurnId = TurnId.make(`turn-plan-order-${testCase.name}`); |
| 162 | for (const plan of testCase.rows) { |
| 163 | yield* plans.upsert({ |
| 164 | ...plan, |
| 165 | planId: `${testCase.name}-${plan.planId}`, |
| 166 | threadId, |
| 167 | turnId: latestTurnId, |
| 168 | planMarkdown: "# Plan", |
| 169 | implementationThreadId: null, |
| 170 | }); |
| 171 | } |
| 172 | assert.strictEqual( |
| 173 | yield* plans.hasActionableByThreadId({ threadId, latestTurnId }), |
| 174 | testCase.expected, |
| 175 | testCase.name, |
| 176 | ); |
| 177 | assert.strictEqual( |
| 178 | yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null }), |
| 179 | testCase.expected, |
| 180 | testCase.name, |
| 181 | ); |
| 182 | } |
| 183 | }), |
| 184 | ); |
| 185 | |
| 186 | it.effect("returns only current-turn status metadata when old plans have large bodies", () => |
| 187 | Effect.gen(function* () { |
| 188 | const plans = yield* ProjectionThreadProposedPlanRepository; |
| 189 | const sql = yield* SqlClient.SqlClient; |
| 190 | const threadId = ThreadId.make("thread-plan-metadata"); |
| 191 | const latestTurnId = TurnId.make("turn-plan-metadata-current"); |
| 192 | const updatedAt = "2026-03-24T00:00:00.000Z"; |
| 193 | yield* sql` |
| 194 | WITH RECURSIVE history(n) AS ( |
| 195 | SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 256 |
| 196 | ) |
| 197 | INSERT INTO projection_thread_proposed_plans ( |
| 198 | plan_id, thread_id, turn_id, plan_markdown, implemented_at, |
| 199 | implementation_thread_id, created_at, updated_at |
| 200 | ) |
| 201 | SELECT 'plan-metadata-old-' || n, ${threadId}, 'turn-plan-metadata-old', |
| 202 | ${"# Old plan\n".repeat(1024)}, ${updatedAt}, NULL, ${updatedAt}, ${updatedAt} |
| 203 | FROM history |
| 204 | `; |
| 205 | yield* plans.upsert({ |
| 206 | planId: "plan-metadata-current", |
| 207 | threadId, |
| 208 | turnId: latestTurnId, |
| 209 | planMarkdown: "# Current plan", |
| 210 | implementedAt: null, |
| 211 | implementationThreadId: null, |
| 212 | createdAt: updatedAt, |
| 213 | updatedAt, |
| 214 | }); |
| 215 | const statements: Array<Statement.Statement<unknown>> = []; |
| 216 | const actionable = yield* plans.hasActionableByThreadId({ threadId, latestTurnId }).pipe( |
| 217 | Effect.provideService(Statement.CurrentTransformer, (statement) => { |
| 218 | statements.push(statement); |
| 219 | return Effect.succeed(statement); |
| 220 | }), |
| 221 | ); |
| 222 | assert.isTrue(actionable); |
| 223 | assert.strictEqual(statements.length, 1); |
| 224 | const statement = statements[0]; |
| 225 | if (statement === undefined) return yield* Effect.die("Expected a plan status query."); |
| 226 | assert.deepEqual(yield* statement, [ |
| 227 | { planId: "plan-metadata-current", implementedAt: null, updatedAt }, |
| 228 | ]); |
| 229 | }), |
| 230 | ); |
| 231 | |
| 23 | 232 | it.effect("stores SQL NULL for missing project model options", () => |
| 24 | 233 | Effect.gen(function* () { |
| 25 | 234 | const projects = yield* ProjectionProjectRepository; |