Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

worktreeSetup.ts

114 lines4.1 KB

1import * as Schema from "effect/Schema";
2​
3import { IsoDateTime, NonNegativeInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
4​
5/**
6 * Live progress for a thread whose first turn is creating a worktree. The
7 * server keeps this in memory only; a client that reconnects mid-setup gets a
8 * fresh snapshot, and a finished setup is dropped once its turn starts.
9 */
10/** Producers clamp free text to these before publishing so encoding never fails. */
11export const WORKTREE_SETUP_DETAIL_MAX_LENGTH = 200;
12export const WORKTREE_SETUP_TAIL_LINE_MAX_LENGTH = 400;
13export const WORKTREE_SETUP_ERROR_MAX_LENGTH = 1000;
14​
15export const WorktreeSetupStageId = Schema.Literals([
16 "fetch",
17 "checkout",
18 "submodules",
19 "setup-script",
20 "agent",
21]);
22export type WorktreeSetupStageId = typeof WorktreeSetupStageId.Type;
23​
24export const WorktreeSetupStageStatus = Schema.Literals([
25 "pending",
26 "running",
27 "done",
28 "skipped",
29 "warning",
30 "failed",
31]);
32export type WorktreeSetupStageStatus = typeof WorktreeSetupStageStatus.Type;
33​
34export const WorktreeSetupStage = Schema.Struct({
35 id: WorktreeSetupStageId,
36 status: WorktreeSetupStageStatus,
37 startedAt: Schema.NullOr(IsoDateTime),
38 endedAt: Schema.NullOr(IsoDateTime),
39 /** Only the checkout stage reports a real percentage, parsed from git's `Updating files` lines. */
40 percent: Schema.NullOr(Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 100 }))),
41 /** Short trailing text for the row: a file count, an exit code, a submodule name. */
42 detail: Schema.NullOr(Schema.String.check(Schema.isMaxLength(WORKTREE_SETUP_DETAIL_MAX_LENGTH))),
43 /** Last few output lines from the setup script, ANSI stripped, newest last. */
44 tail: Schema.Array(Schema.String.check(Schema.isMaxLength(WORKTREE_SETUP_TAIL_LINE_MAX_LENGTH))),
45});
46export type WorktreeSetupStage = typeof WorktreeSetupStage.Type;
47​
48export const WorktreeSetupPhase = Schema.Literals(["running", "done", "failed", "cancelled"]);
49export type WorktreeSetupPhase = typeof WorktreeSetupPhase.Type;
50​
51export const WorktreeSetupSnapshot = Schema.Struct({
52 threadId: ThreadId,
53 phase: WorktreeSetupPhase,
54 startedAt: IsoDateTime,
55 endedAt: Schema.NullOr(IsoDateTime),
56 branch: Schema.NullOr(TrimmedNonEmptyString),
57 baseRef: Schema.NullOr(TrimmedNonEmptyString),
58 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
59 /** Display name plus command of the setup script from t3.json, when one runs. */
60 setupScript: Schema.NullOr(
61 Schema.Struct({
62 name: TrimmedNonEmptyString,
63 command: TrimmedNonEmptyString,
64 terminalId: TrimmedNonEmptyString,
65 }),
66 ),
67 stages: Schema.Array(WorktreeSetupStage),
68 /** Human readable reason when phase is failed. */
69 error: Schema.NullOr(Schema.String.check(Schema.isMaxLength(WORKTREE_SETUP_ERROR_MAX_LENGTH))),
70 sequence: NonNegativeInt,
71});
72export type WorktreeSetupSnapshot = typeof WorktreeSetupSnapshot.Type;
73​
74export const WorktreeSetupSubscribeInput = Schema.Struct({
75 threadId: ThreadId,
76});
77export type WorktreeSetupSubscribeInput = typeof WorktreeSetupSubscribeInput.Type;
78​
79/** Null means no setup is tracked for that thread. Sent first, then after every change. */
80export const WorktreeSetupStreamEvent = Schema.NullOr(WorktreeSetupSnapshot);
81export type WorktreeSetupStreamEvent = typeof WorktreeSetupStreamEvent.Type;
82​
83export const WorktreeSetupCancelInput = Schema.Struct({
84 threadId: ThreadId,
85});
86export type WorktreeSetupCancelInput = typeof WorktreeSetupCancelInput.Type;
87​
88export const WorktreeSetupCancelResult = Schema.Struct({
89 cancelled: Schema.Boolean,
90});
91export type WorktreeSetupCancelResult = typeof WorktreeSetupCancelResult.Type;
92​
93export const WORKTREE_SETUP_STAGE_ORDER: ReadonlyArray<WorktreeSetupStageId> = [
94 "fetch",
95 "checkout",
96 "submodules",
97 "setup-script",
98 "agent",
99];
100​
101export function worktreeSetupStageLabel(id: WorktreeSetupStageId): string {
102 switch (id) {
103 case "fetch":
104 return "Fetch base branch";
105 case "checkout":
106 return "Check out files";
107 case "submodules":
108 return "Init submodules";
109 case "setup-script":
110 return "Run setup script";
111 case "agent":
112 return "Start agent";
113 }