feat(web): show each worktree setup step and let users cancel it (#11372)

Starting a thread in a new worktree showed one static "Setting up worktree" line while git fetched, checked out files, and the setup script ran. Users could not tell which step was slow, see setup script output without hunting for the terminal, or stop a setup that was going wrong.

The timeline now shows a card with each step and its elapsed time. Check out files has a percent bar fed by git's own progress output. The setup script step shows the last lines of its terminal inline and links to the full terminal. Cancel stops the bootstrap and removes the half built worktree. Work locally cancels, switches the draft to the project checkout, and resends.

The server keeps an in-memory per-thread snapshot of the bootstrap stages and streams it over a new subscribeWorktreeSetup RPC. The bootstrap runs as a child fiber so worktreeSetup.cancel can interrupt it, and the turn handoff is uninterruptible. The setup script's exit code comes from a per-run sentinel echoed after the command in the setup PTY.

Created with Claude Fable 5.1 in Claude Code.
This commit is contained in:
Theo Browne authored and GitHub committed 2026-09-13 21:28:59 -07:00
1 parent b70015b6db
commit 73b206f4bf
24 files changed
+2352 -79

No files matched your search

@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from "@effect/vitest";
import { type OrchestrationProject, ProjectId } from "@t3tools/contracts";
import { type OrchestrationProject, ProjectId, type TerminalEvent } from "@t3tools/contracts";
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
@@ -54,11 +55,11 @@ const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
searchThreads: () => Effect.succeed({ matches: [] }),
});
const makeTerminalManagerLayer = (
overrides: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
) =>
type TerminalOverrides = Pick<TerminalManager.TerminalManager["Service"], "open" | "write"> &
Partial<Pick<TerminalManager.TerminalManager["Service"], "subscribe">>;
const makeTerminalManagerLayer = (overrides: TerminalOverrides) =>
Layer.succeed(TerminalManager.TerminalManager, {
...overrides,
attachStream: () => Effect.die(new Error("unused")),
resize: () => Effect.void,
clear: () => Effect.void,
@@ -66,11 +67,12 @@ const makeTerminalManagerLayer = (
close: () => Effect.void,
subscribe: () => Effect.succeed(() => undefined),
subscribeMetadata: () => Effect.succeed(() => undefined),
...overrides,
});
const testLayer = (
project: OrchestrationProject,
terminal: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
terminal: TerminalOverrides,
settings = ServerSettings.layerTest(),
) =>
ProjectSetupScriptRunner.layer.pipe(
@@ -198,6 +200,7 @@ describe("ProjectSetupScriptRunner", () => {
status: "started",
scriptId: "setup",
scriptName: "Setup",
scriptCommand: "bun install",
terminalId: "setup-setup",
cwd: "/repo/worktrees/a",
});
@@ -220,6 +223,209 @@ describe("ProjectSetupScriptRunner", () => {
},
);
it.effect(
"wraps the command with a completion sentinel and resolves the exit code from terminal output",
() => {
const open = vi.fn(() =>
Effect.succeed({
threadId: "thread-1",
terminalId: "setup-setup",
cwd: "/repo/worktrees/a",
worktreePath: "/repo/worktrees/a",
status: "running" as const,
pid: 123,
history: "",
exitCode: null,
exitSignal: null,
label: "setup-setup",
updatedAt: "2026-01-01T00:00:00.000Z",
}),
);
const writes: string[] = [];
const write = vi.fn((input: { data: string }) =>
Effect.sync(() => void writes.push(input.data)),
);
let listener: ((event: TerminalEvent) => Effect.Effect<void>) | null = null;
const subscribe = vi.fn((next: (event: TerminalEvent) => Effect.Effect<void>) => {
listener = next;
return Effect.succeed(() => {
listener = null;
});
});
const project = makeProject([
{
id: "setup",
name: "Setup",
command: "bun install",
icon: "configure",
runOnWorktreeCreate: true,
},
]);
const emit = (data: string) =>
Effect.suspend(() =>
listener
? listener({ threadId: "thread-1", terminalId: "setup-setup", type: "output", data })
: Effect.void,
);
return Effect.gen(function* () {
const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
const seen: string[] = [];
const result = yield* runner.runForThread({
threadId: "thread-1",
projectCwd: "/repo/project",
worktreePath: "/repo/worktrees/a",
observeCompletion: {
onOutputLine: (line) => Effect.sync(() => void seen.push(line)),
},
});
expect(result.status).toBe("started");
if (result.status !== "started") return;
expect(result.completion).toBeDefined();
// The subscription is attached before the command is written.
expect(subscribe).toHaveBeenCalledTimes(1);
expect(writes).toHaveLength(1);
// The block closes on its own line so a trailing comment in the
// command cannot swallow the sentinel, and the sentinel carries a
// per-run token so script output cannot spoof it.
const written = writes[0] ?? "";
const sentinel = /__T3_SETUP_DONE___[0-9a-f]{32}:/.exec(written)?.[0];
expect(sentinel).toBeDefined();
expect(written).toBe(`( bun install\r); printf '\\n${sentinel}%s\\n' "$?"\r`);
// Output arrives in chunks; partial lines are buffered until a newline,
// control sequences are stripped, and the echoed wrapper is hidden.
yield* emit(`( bun install\r\n> ); printf '\\n${sentinel}%s\\n' "$?"\r\n`);
yield* emit("\u001b[32mResolving");
yield* emit(" deps\u001b[0m\r\nDone in 2s\r\n");
// A spoofed sentinel from the script itself must not settle completion.
yield* emit("__T3_SETUP_DONE__:0\r\n");
yield* emit(`__T3_SETUP_DONE___${"0".repeat(32)}:0\r\n`);
yield* emit(`${sentinel}3\r\n`);
const completion = yield* result.completion!;
expect(completion.exitCode).toBe(3);
expect(seen).toEqual([
"Resolving deps",
"Done in 2s",
"__T3_SETUP_DONE__:0",
`__T3_SETUP_DONE___${"0".repeat(32)}:0`,
]);
// The subscription is torn down once the sentinel arrives.
expect(listener).toBeNull();
}).pipe(
Effect.provide(testLayer(project, { open, write, subscribe })),
Effect.provideService(HostProcessPlatform, "linux"),
Effect.provideService(HostProcessEnvironment, { SHELL: "/bin/zsh" }),
);
},
);
it.effect("unsubscribes from terminal output when the command cannot be written", () => {
const open = vi.fn(() =>
Effect.succeed({
threadId: "thread-1",
terminalId: "setup-setup",
cwd: "/repo/worktrees/a",
worktreePath: "/repo/worktrees/a",
status: "running" as const,
pid: 123,
history: "",
exitCode: null,
exitSignal: null,
label: "setup-setup",
updatedAt: "2026-01-01T00:00:00.000Z",
}),
);
const write = vi.fn(() =>
Effect.fail(
new TerminalManager.TerminalCwdStatError({ cwd: "/repo/worktrees/a", cause: {} }),
),
);
const unsubscribe = vi.fn();
const subscribe = vi.fn(() => Effect.succeed(unsubscribe));
const project = makeProject([
{
id: "setup",
name: "Setup",
command: "bun install",
icon: "configure",
runOnWorktreeCreate: true,
},
]);
return Effect.gen(function* () {
const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
const result = yield* runner
.runForThread({
threadId: "thread-1",
projectCwd: "/repo/project",
worktreePath: "/repo/worktrees/a",
observeCompletion: {},
})
.pipe(Effect.result);
expect(result._tag).toBe("Failure");
expect(unsubscribe).toHaveBeenCalledTimes(1);
}).pipe(Effect.provide(testLayer(project, { open, write, subscribe })));
});
it.effect.each([
{
shell: "/usr/bin/fish",
expected:
/^begin\rbun install\rend; printf '\\n__T3_SETUP_DONE___[0-9a-f]{32}:%s\\n' \$status\r$/,
},
{
shell: "/bin/bash",
expected: /^\( bun install\r\); printf '\\n__T3_SETUP_DONE___[0-9a-f]{32}:%s\\n' "\$\?"\r$/,
},
])("wraps the command for the $shell syntax", ({ shell, expected }) => {
const open = vi.fn(() =>
Effect.succeed({
threadId: "thread-1",
terminalId: "setup-setup",
cwd: "/repo/worktrees/a",
worktreePath: "/repo/worktrees/a",
status: "running" as const,
pid: 123,
history: "",
exitCode: null,
exitSignal: null,
label: "setup-setup",
updatedAt: "2026-01-01T00:00:00.000Z",
}),
);
const writes: string[] = [];
const write = vi.fn((input: { data: string }) =>
Effect.sync(() => void writes.push(input.data)),
);
const project = makeProject([
{
id: "setup",
name: "Setup",
command: "bun install",
icon: "configure",
runOnWorktreeCreate: true,
},
]);
return Effect.gen(function* () {
const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
yield* runner.runForThread({
threadId: "thread-1",
projectCwd: "/repo/project",
worktreePath: "/repo/worktrees/a",
observeCompletion: {},
});
expect(writes).toHaveLength(1);
expect(writes[0]).toMatch(expected);
}).pipe(
Effect.provide(testLayer(project, { open, write })),
Effect.provideService(HostProcessPlatform, "linux"),
Effect.provideService(HostProcessEnvironment, { SHELL: shell }),
);
});
it.effect("keeps terminal failures as the exact cause of a structured operation error", () => {
const rootCause = new Error("stat failed");
const terminalError = new TerminalManager.TerminalCwdStatError({