Repository navigation
fix(server): worktree setup no longer hangs when a shell startup prompt eats the command #24775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc571cd
fix(server): worktree setup waits for the shell, so startup prompts c…
t3dotgg b4023dd
fix(server): show the setup terminal while a startup prompt waits
t3dotgg ad0ec9b
fix(server): ignore restored terminal history and clean up on cancel
t3dotgg a1b6982
fix(server): forget startup readiness state when the shell restarts
t3dotgg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
apps/server/src/project/ProjectSetupScriptRunner.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { ProjectId, type TerminalEvent, type TerminalSessionSnapshot } from "@t3tools/contracts"; | ||
| import * as HostProcess from "@t3tools/shared/HostProcess"; | ||
| import * as Deferred from "effect/Deferred"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Queue from "effect/Queue"; | ||
| import { TestClock } from "effect/testing"; | ||
|
|
||
| import * as ServerSettings from "../serverSettings.ts"; | ||
| import * as TerminalManager from "../terminal/Manager.ts"; | ||
| import * as ProjectService from "./ProjectService.ts"; | ||
| import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts"; | ||
|
|
||
| const threadId = "thread-1"; | ||
| const terminalId = "setup-setup"; | ||
| const event = { threadId, terminalId, sequence: 1, createdAt: "2026-01-01T00:00:00.000Z" }; | ||
|
|
||
| const openSnapshot = (history: string): TerminalSessionSnapshot => ({ | ||
| threadId, | ||
| terminalId, | ||
| cwd: "/repo/.t3/worktrees/feature", | ||
| worktreePath: null, | ||
| status: "running", | ||
| pid: 123, | ||
| history, | ||
| exitCode: null, | ||
| exitSignal: null, | ||
| label: "zsh", | ||
| updatedAt: "2026-01-01T00:00:00.000Z", | ||
| }); | ||
|
|
||
| const makeHarness = Effect.fn(function* (options: { | ||
| readonly history?: string; | ||
| readonly opening?: Deferred.Deferred<void>; | ||
| }) { | ||
| const listeners = new Set<(event: TerminalEvent) => Effect.Effect<void>>(); | ||
| const writes = yield* Queue.unbounded<string>(); | ||
| const outputLines = yield* Queue.unbounded<string>(); | ||
| const terminals = Layer.mock(TerminalManager.TerminalManager)({ | ||
| open: () => | ||
| (options.opening | ||
| ? Deferred.succeed(options.opening, undefined).pipe(Effect.andThen(Effect.never)) | ||
| : Effect.void | ||
| ).pipe(Effect.as(openSnapshot(options.history ?? ""))), | ||
| subscribe: (listener) => | ||
| Effect.sync(() => { | ||
| listeners.add(listener); | ||
| return () => listeners.delete(listener); | ||
| }), | ||
| write: (input) => Queue.offer(writes, input.data).pipe(Effect.asVoid), | ||
| closeIdle: () => Effect.void, | ||
| }); | ||
| const runner = yield* ProjectSetupScriptRunner.make.pipe( | ||
| Effect.provide( | ||
| Layer.mergeAll( | ||
| terminals, | ||
| Layer.mock(ProjectService.ProjectService)({}), | ||
| ServerSettings.layerTest(), | ||
| NodeCrypto.layer, | ||
| ), | ||
| ), | ||
| Effect.provideService(HostProcess.Platform, "darwin"), | ||
| Effect.provideService(HostProcess.Environment, { SHELL: "/bin/zsh" }), | ||
| ); | ||
| const start = runner.runForThread({ | ||
| threadId, | ||
| worktreePath: "/repo/.t3/worktrees/feature", | ||
| observeCompletion: { onOutputLine: (line) => Queue.offer(outputLines, line) }, | ||
| project: { | ||
| id: ProjectId.make("project-1"), | ||
| workspaceRoot: "/repo", | ||
| scripts: [ | ||
| { | ||
| id: "setup", | ||
| name: "Setup", | ||
| command: "vp i", | ||
| icon: "configure", | ||
| runOnWorktreeCreate: true, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| const emit = (terminalEvent: TerminalEvent) => | ||
| Effect.forEach([...listeners], (listener) => listener(terminalEvent), { discard: true }); | ||
| return { start, listeners, writes, outputLines, emit }; | ||
| }); | ||
|
|
||
| const setup = Effect.fn(function* (options: { readonly history?: string } = {}) { | ||
| const harness = yield* makeHarness(options); | ||
| const result = yield* harness.start; | ||
| const completion = result.status === "started" ? result.completion : undefined; | ||
| if (!completion) return yield* Effect.die("setup did not start an observed run"); | ||
| return { ...harness, result, completion }; | ||
| }); | ||
|
|
||
| describe("ProjectSetupScriptRunner", () => { | ||
| it.effect("shows a startup prompt and types only once the line editor starts", () => | ||
| Effect.gen(function* () { | ||
| const { result, writes, outputLines, emit } = yield* setup(); | ||
| expect(result).toMatchObject({ status: "started", terminalId }); | ||
|
|
||
| yield* emit({ ...event, type: "output", data: "Missing Node.\r\nInstall it? [y/N]: " }); | ||
| yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" }); | ||
| yield* TestClock.adjust("1 minute"); | ||
| expect(yield* Queue.size(writes)).toBe(0); | ||
| expect(yield* Queue.takeAll(outputLines)).toEqual(["Missing Node.", "Install it? [y/N]:"]); | ||
|
|
||
| yield* emit({ ...event, type: "activity", hasRunningSubprocess: false, label: "zsh" }); | ||
| yield* emit({ ...event, type: "output", data: "\r\n% \x1b[?2004h" }); | ||
| expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("types into a quiet shell that has no line editor", () => | ||
| Effect.gen(function* () { | ||
| const { writes } = yield* setup(); | ||
| yield* TestClock.adjust("3 seconds"); | ||
| expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("ignores line editor state restored from an earlier shell", () => | ||
| Effect.gen(function* () { | ||
| const { writes, emit } = yield* setup({ history: "old run\r\n% \x1b[?2004h" }); | ||
| yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" }); | ||
| yield* TestClock.adjust("1 minute"); | ||
| expect(yield* Queue.size(writes)).toBe(0); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("forgets a busy startup child when the shell restarts", () => | ||
| Effect.gen(function* () { | ||
| const { writes, emit } = yield* setup(); | ||
| yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" }); | ||
| yield* TestClock.adjust("1 minute"); | ||
| expect(yield* Queue.size(writes)).toBe(0); | ||
| yield* emit({ ...event, type: "restarted", snapshot: openSnapshot("") }); | ||
| // The restart lands inside a quiet window, so the next full window decides. | ||
| yield* TestClock.adjust("6 seconds"); | ||
| expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("drops its terminal listeners when cancelled while the terminal opens", () => | ||
| Effect.gen(function* () { | ||
| const opening = yield* Deferred.make<void>(); | ||
| const { start, listeners } = yield* makeHarness({ opening }); | ||
| const fiber = yield* Effect.forkChild(start); | ||
| yield* Deferred.await(opening); | ||
| expect(listeners.size).toBe(2); | ||
| yield* Fiber.interrupt(fiber); | ||
| expect(listeners.size).toBe(0); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("does not type into a shell that exits during startup", () => | ||
| Effect.gen(function* () { | ||
| const { completion, writes, emit } = yield* setup(); | ||
| yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null }); | ||
| expect(yield* completion).toMatchObject({ exitCode: null }); | ||
| yield* TestClock.adjust("1 minute"); | ||
| expect(yield* Queue.size(writes)).toBe(0); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.