Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import {
3 EventId,
4 type OrchestrationCommand,
5 ThreadId,
6 WORKTREE_SETUP_ACTIVITY_KIND,
7 WorktreeSetupSnapshot,
8 worktreeSetupActivityId,
9 type WorktreeSetupPhase,
10} from "@t3tools/contracts";
11import { assert, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as Schema from "effect/Schema";
14import * as Stream from "effect/Stream";
15
16import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
17import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
18import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
19
20const startedAt = "2026-08-20T12:00:00.000Z";
21
22const snapshotFor = (
23 threadId: ThreadId,
24 phase: WorktreeSetupPhase,
25 agentStatus: "pending" | "done" = phase === "running" ? "pending" : "done",
26): WorktreeSetupSnapshot => ({
27 threadId,
28 phase,
29 startedAt,
30 endedAt: phase === "running" ? null : startedAt,
31 branch: "feature",
32 baseRef: "main",
33 worktreePath: null,
34 setupScript: null,
35 stages: [
36 {
37 id: "checkout",
38 status: "done",
39 startedAt,
40 endedAt: startedAt,
41 percent: null,
42 detail: null,
43 tail: [],
44 },
45 {
46 id: "setup-script",
47 status: phase === "running" ? "running" : "done",
48 startedAt,
49 endedAt: phase === "running" ? null : startedAt,
50 percent: null,
51 detail: null,
52 tail: [],
53 },
54 {
55 id: "agent",
56 status: agentStatus,
57 startedAt: null,
58 endedAt: null,
59 percent: null,
60 detail: null,
61 tail: [],
62 },
63 ],
64 error: null,
65 sequence: 4,
66});
67
68const recordedSetup = (id: string, phase: WorktreeSetupPhase, agentStatus?: "pending" | "done") => {
69 const threadId = ThreadId.make(id);
70 return {
71 id: EventId.make(worktreeSetupActivityId(threadId)),
72 tone: "info" as const,
73 kind: WORKTREE_SETUP_ACTIVITY_KIND,
74 summary: "Setting up worktree",
75 payload: snapshotFor(threadId, phase, agentStatus),
76 turnId: null,
77 createdAt: startedAt,
78 };
79};
80
81const run = (activities: ReadonlyArray<ReturnType<typeof recordedSetup>>) =>
82 Effect.gen(function* () {
83 const dispatched: Array<OrchestrationCommand> = [];
84 yield* ServerRuntimeStartup.reconcileWorktreeSetups.pipe(
85 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
86 listActivitiesByKind: (kind: string) =>
87 Effect.succeed(kind === WORKTREE_SETUP_ACTIVITY_KIND ? activities : []),
88 } as unknown as ProjectionSnapshotQuery.ProjectionSnapshotQuery["Service"]),
89 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
90 readEvents: () => Stream.empty,
91 readThreadEvents: () => Stream.empty,
92 getThreadReplayStats: () => Effect.die("unused"),
93 dispatch: (command) =>
94 Effect.sync(() => {
95 dispatched.push(command);
96 return { sequence: dispatched.length };
97 }),
98 streamDomainEvents: Stream.empty,
99 subscribeDomainEvents: Effect.succeed(Stream.empty),
100 latestSequence: Effect.succeed(0),
101 }),
102 Effect.provide(NodeServices.layer),
103 );
104 return dispatched;
105 });
106
107it.effect("marks setups still recorded as running failed after a restart", () =>
108 Effect.gen(function* () {
109 const dispatched = yield* run([
110 recordedSetup("thread-running", "running"),
111 recordedSetup("thread-done", "done"),
112 recordedSetup("thread-failed", "failed"),
113 ]);
114
115 assert.equal(dispatched.length, 1);
116 const command = dispatched[0]!;
117 assert.equal(command.type, "thread.activity.append");
118 if (command.type !== "thread.activity.append") return;
119 assert.equal(command.threadId, ThreadId.make("thread-running"));
120 assert.equal(command.activity.id, worktreeSetupActivityId(ThreadId.make("thread-running")));
121 assert.equal(command.activity.tone, "error");
122 const payload = yield* Schema.decodeUnknownEffect(WorktreeSetupSnapshot)(
123 command.activity.payload,
124 );
125 assert.equal(payload.phase, "failed");
126 assert.isNotNull(payload.endedAt);
127 assert.equal(payload.sequence, 5);
128 assert.deepEqual(
129 payload.stages.map((stage) => stage.status),
130 ["done", "failed", "failed"],
131 );
132 }),
133);
134
135it.effect(
136 "settles an async setup script whose turn already started without failing the setup",
137 () =>
138 Effect.gen(function* () {
139 const dispatched = yield* run([recordedSetup("thread-async", "running", "done")]);
140
141 assert.equal(dispatched.length, 1);
142 const command = dispatched[0]!;
143 if (command.type !== "thread.activity.append") return assert.fail(command.type);
144 const payload = yield* Schema.decodeUnknownEffect(WorktreeSetupSnapshot)(
145 command.activity.payload,
146 );
147 // The turn is live; only the background script was lost. Nothing asks the
148 // user to resend, and the setup reads as done with a failed script stage.
149 assert.equal(payload.phase, "done");
150 assert.isNull(payload.error);
151 assert.deepEqual(
152 payload.stages.map((stage) => stage.status),
153 ["done", "failed", "done"],
154 );
155 }),
156);