|
| 1 | +import { |
| 2 | + ProjectId, |
| 3 | + ProviderInstanceId, |
| 4 | + ThreadId, |
| 5 | + type OrchestrationCommand, |
| 6 | + type OrchestrationShellSnapshot, |
| 7 | + type OrchestrationThreadShell, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import { assert, describe, it } from "@effect/vitest"; |
| 10 | +import * as Crypto from "effect/Crypto"; |
| 11 | +import * as Deferred from "effect/Deferred"; |
| 12 | +import * as Effect from "effect/Effect"; |
| 13 | +import * as Layer from "effect/Layer"; |
| 14 | +import * as Queue from "effect/Queue"; |
| 15 | +import * as Ref from "effect/Ref"; |
| 16 | +import { TestClock } from "effect/testing"; |
| 17 | + |
| 18 | +import { ServerActivation } from "../serverActivation.ts"; |
| 19 | +import * as AutoContinueReactor from "./AutoContinueReactor.ts"; |
| 20 | +import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts"; |
| 21 | +import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts"; |
| 22 | + |
| 23 | +const NOW = "2026-08-28T12:00:00.000Z"; |
| 24 | +const DUE_AT = "2026-08-28T11:00:00.000Z"; |
| 25 | +const LATER_AT = "2026-08-28T14:00:00.000Z"; |
| 26 | +const PROJECT_ID = ProjectId.make("auto-continue-project"); |
| 27 | + |
| 28 | +type FireCommand = Extract<OrchestrationCommand, { readonly type: "thread.auto-continue.fire" }>; |
| 29 | + |
| 30 | +const testCrypto = Crypto.make({ |
| 31 | + randomBytes: (size) => new Uint8Array(size).fill(1), |
| 32 | + digest: (_algorithm, data) => Effect.succeed(data), |
| 33 | +}); |
| 34 | + |
| 35 | +function makeThread( |
| 36 | + id: string, |
| 37 | + overrides: Partial<OrchestrationThreadShell> = {}, |
| 38 | +): OrchestrationThreadShell { |
| 39 | + return { |
| 40 | + id: ThreadId.make(id), |
| 41 | + projectId: PROJECT_ID, |
| 42 | + title: id, |
| 43 | + modelSelection: { |
| 44 | + instanceId: ProviderInstanceId.make("codex"), |
| 45 | + model: "gpt-5", |
| 46 | + }, |
| 47 | + runtimeMode: "full-access", |
| 48 | + interactionMode: "default", |
| 49 | + pullRequests: [], |
| 50 | + branch: null, |
| 51 | + worktreePath: null, |
| 52 | + latestTurn: null, |
| 53 | + createdAt: "2026-08-01T00:00:00.000Z", |
| 54 | + updatedAt: NOW, |
| 55 | + archivedAt: null, |
| 56 | + settledOverride: null, |
| 57 | + settledAt: null, |
| 58 | + session: null, |
| 59 | + latestUserMessageAt: null, |
| 60 | + hasPendingApprovals: false, |
| 61 | + hasPendingUserInput: false, |
| 62 | + hasActionableProposedPlan: false, |
| 63 | + ...overrides, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function makeSnapshot( |
| 68 | + threads: ReadonlyArray<OrchestrationThreadShell>, |
| 69 | +): OrchestrationShellSnapshot { |
| 70 | + return { |
| 71 | + snapshotSequence: 1, |
| 72 | + projects: [], |
| 73 | + threads, |
| 74 | + updatedAt: NOW, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +const makeHarness = Effect.fn("makeAutoContinueHarness")(function* ( |
| 79 | + snapshot: OrchestrationShellSnapshot, |
| 80 | +) { |
| 81 | + const activation = yield* Deferred.make<void>(); |
| 82 | + const snapshotReads = yield* Queue.unbounded<void>(); |
| 83 | + const commands = yield* Ref.make<ReadonlyArray<FireCommand>>([]); |
| 84 | + |
| 85 | + const dependencies = Layer.mergeAll( |
| 86 | + Layer.mock(ProjectionSnapshotQuery)({ |
| 87 | + getShellSnapshot: () => Queue.offer(snapshotReads, undefined).pipe(Effect.as(snapshot)), |
| 88 | + }), |
| 89 | + Layer.mock(OrchestrationEngineService)({ |
| 90 | + dispatch: (command) => { |
| 91 | + if (command.type !== "thread.auto-continue.fire") { |
| 92 | + return Effect.die(new Error(`Unexpected command: ${command.type}`)); |
| 93 | + } |
| 94 | + return Ref.update(commands, (recorded) => [...recorded, command]).pipe( |
| 95 | + Effect.as({ sequence: 1 }), |
| 96 | + ); |
| 97 | + }, |
| 98 | + }), |
| 99 | + Layer.succeed(ServerActivation, Deferred.await(activation)), |
| 100 | + Layer.succeed(Crypto.Crypto, testCrypto), |
| 101 | + ); |
| 102 | + |
| 103 | + return { |
| 104 | + activation, |
| 105 | + snapshotReads, |
| 106 | + commands, |
| 107 | + layer: AutoContinueReactor.layer.pipe(Layer.provide(dependencies)), |
| 108 | + }; |
| 109 | +}); |
| 110 | + |
| 111 | +describe("AutoContinueReactor", () => { |
| 112 | + it.effect("fires due continuations as thread.auto-continue.fire commands", () => |
| 113 | + Effect.scoped( |
| 114 | + Effect.gen(function* () { |
| 115 | + yield* TestClock.setTime(Date.parse(NOW)); |
| 116 | + const harness = yield* makeHarness( |
| 117 | + makeSnapshot([makeThread("due-thread", { autoContinueAt: DUE_AT })]), |
| 118 | + ); |
| 119 | + yield* Effect.provide( |
| 120 | + Effect.gen(function* () { |
| 121 | + const reactor = yield* AutoContinueReactor.AutoContinueReactor; |
| 122 | + yield* reactor.start(); |
| 123 | + yield* Deferred.succeed(harness.activation, undefined); |
| 124 | + yield* Queue.take(harness.snapshotReads); |
| 125 | + yield* reactor.drain; |
| 126 | + }), |
| 127 | + harness.layer, |
| 128 | + ); |
| 129 | + const dispatched = yield* Ref.get(harness.commands); |
| 130 | + assert.strictEqual(dispatched.length, 1); |
| 131 | + assert.strictEqual(dispatched[0]?.threadId, "due-thread"); |
| 132 | + assert.strictEqual(dispatched[0]?.autoContinueAt, DUE_AT); |
| 133 | + assert.isTrue(dispatched[0]?.commandId.startsWith("server:auto-continue:due-thread:")); |
| 134 | + assert.isString(dispatched[0]?.messageId); |
| 135 | + }), |
| 136 | + ), |
| 137 | + ); |
| 138 | + |
| 139 | + it.effect("leaves future schedules, archived threads, and unscheduled threads alone", () => |
| 140 | + Effect.scoped( |
| 141 | + Effect.gen(function* () { |
| 142 | + yield* TestClock.setTime(Date.parse(NOW)); |
| 143 | + const harness = yield* makeHarness( |
| 144 | + makeSnapshot([ |
| 145 | + makeThread("later-thread", { autoContinueAt: LATER_AT }), |
| 146 | + makeThread("archived-thread", { autoContinueAt: DUE_AT, archivedAt: NOW }), |
| 147 | + makeThread("plain-thread"), |
| 148 | + ]), |
| 149 | + ); |
| 150 | + yield* Effect.provide( |
| 151 | + Effect.gen(function* () { |
| 152 | + const reactor = yield* AutoContinueReactor.AutoContinueReactor; |
| 153 | + yield* reactor.start(); |
| 154 | + yield* Deferred.succeed(harness.activation, undefined); |
| 155 | + yield* Queue.take(harness.snapshotReads); |
| 156 | + yield* reactor.drain; |
| 157 | + }), |
| 158 | + harness.layer, |
| 159 | + ); |
| 160 | + const dispatched = yield* Ref.get(harness.commands); |
| 161 | + assert.strictEqual(dispatched.length, 0); |
| 162 | + }), |
| 163 | + ), |
| 164 | + ); |
| 165 | +}); |
0 commit comments