Open History to see the last commit
1import {
2 WORKTREE_SETUP_ACTIVITY_KIND,
3 WorktreeSetupSnapshot,
4 type ThreadId,
5} from "@t3tools/contracts";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8
9const decodeWorktreeSetupSnapshot = Schema.decodeUnknownOption(WorktreeSetupSnapshot);
10
11export function findRecordedWorktreeSetup(
12 activities: ReadonlyArray<{ readonly kind: string; readonly payload: unknown }>,
13 threadId: ThreadId,
14): WorktreeSetupSnapshot | null {
15 for (let index = activities.length - 1; index >= 0; index -= 1) {
16 const activity = activities[index]!;
17 if (activity.kind !== WORKTREE_SETUP_ACTIVITY_KIND) continue;
18 const decoded = decodeWorktreeSetupSnapshot(activity.payload);
19 if (Option.isSome(decoded) && decoded.value.threadId === threadId) return decoded.value;
20 }
21 return null;
22}
23
24export function resolveVisibleWorktreeSetup(input: {
25 live: WorktreeSetupSnapshot | null;
26 recorded: WorktreeSetupSnapshot | null;
27 turnStarted: boolean;
28 followUpSent: boolean;
29}): WorktreeSetupSnapshot | null {
30 const snapshot =
31 input.live && (!input.recorded || input.live.sequence >= input.recorded.sequence)
32 ? input.live
33 : input.recorded;
34 if (!snapshot) return null;
35 if (snapshot.phase === "running") return snapshot;
36 if (input.followUpSent) return null;
37 if (snapshot.phase !== "done") return snapshot;
38 if (!input.turnStarted) return snapshot;
39 return snapshot.stages.some((stage) => stage.status === "failed") ? snapshot : null;
40}
41
42export function worktreeSetupAgentStarted(snapshot: WorktreeSetupSnapshot): boolean {
43 return snapshot.stages.some((stage) => stage.id === "agent" && stage.status === "done");
44}