import { ProjectId } from "@t3tools/contracts"; import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess"; import { projectScriptRuntimeEnv, resolveProjectScripts, setupProjectScript, } from "@t3tools/shared/projectScripts"; import * as NodeCrypto from "node:crypto"; import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as TerminalManager from "../terminal/Manager.ts"; export interface ProjectSetupScriptRunnerResultNoScript { readonly status: "no-script"; } export interface ProjectSetupScriptRunnerResultStarted { readonly status: "started"; readonly scriptId: string; readonly scriptName: string; readonly scriptCommand: string; readonly terminalId: string; readonly cwd: string; /** False when the script's `async` flag asks the agent to wait for it. */ readonly async: boolean; /** * Resolves when the script's shell prints the completion sentinel. The * exit code is null when the terminal exited or was closed before the * sentinel arrived. Only present when `observeCompletion` was requested. */ readonly completion?: Effect.Effect; } export interface ProjectSetupScriptCompletion { readonly exitCode: number | null; readonly durationMs: number; } export interface ProjectSetupScriptOutputLine { readonly line: string; } export type ProjectSetupScriptRunnerResult = | ProjectSetupScriptRunnerResultNoScript | ProjectSetupScriptRunnerResultStarted; export interface ProjectSetupScriptRunnerInput { readonly threadId: string; readonly projectId?: string; readonly projectCwd?: string; readonly worktreePath: string; readonly preferredTerminalId?: string; /** * Wrap the command so the shell reports its exit code back through the * terminal stream, and forward cleaned output lines while it runs. The * bootstrap flow uses this to drive the worktree setup card. */ readonly observeCompletion?: { readonly onOutputLine?: (line: string) => Effect.Effect; }; } export class ProjectSetupScriptOperationError extends Schema.TaggedError()( "ProjectSetupScriptOperationError", { threadId: Schema.String, projectId: Schema.optional(Schema.String), projectCwd: Schema.optional(Schema.String), worktreePath: Schema.String, operation: Schema.Literals(["resolveProject", "readSettings", "openTerminal", "writeCommand"]), cause: Schema.Defect(), }, ) { override get message(): string { return `Project setup script operation '${this.operation}' failed for thread '${this.threadId}' in '${this.worktreePath}'.`; } } export class ProjectSetupScriptProjectNotFoundError extends Schema.TaggedError()( "ProjectSetupScriptProjectNotFoundError", { threadId: Schema.String, projectId: Schema.optional(Schema.String), projectCwd: Schema.optional(Schema.String), worktreePath: Schema.String, }, ) { override get message(): string { return `Project was not found for setup script execution for thread '${this.threadId}' in '${this.worktreePath}'.`; } } export const ProjectSetupScriptRunnerError = Schema.Union([ ProjectSetupScriptOperationError, ProjectSetupScriptProjectNotFoundError, ]); export type ProjectSetupScriptRunnerError = typeof ProjectSetupScriptRunnerError.Type; export class ProjectSetupScriptRunner extends Context.Service< ProjectSetupScriptRunner, { readonly runForThread: ( input: ProjectSetupScriptRunnerInput, ) => Effect.Effect; } >()("t3/project/ProjectSetupScriptRunner") {} /** @public Service construction is part of the canonical Effect module API. */ /** * Marker the wrapped setup command echoes so the exit code can be read from * the PTY stream. Each run gets its own random token so script output cannot * spoof completion, and the sentinel pattern is built per run from it. */ const COMPLETION_SENTINEL_PREFIX = "__T3_SETUP_DONE__"; const OUTPUT_LINE_MAX_LENGTH = 400; /** A partial line longer than this is a byte stream, not a line. Keep only the tail. */ const PARTIAL_LINE_MAX_LENGTH = 4_096; function completionSentinel(token: string): string { return `${COMPLETION_SENTINEL_PREFIX}_${token}:`; } function completionSentinelPattern(token: string): RegExp { return new RegExp(`${COMPLETION_SENTINEL_PREFIX}_${token}:(-?\\d+)`); } /** Removes ANSI escape sequences and cursor controls so lines can be shown as plain text. */ function stripTerminalControl(text: string): string { return ( text .replace( // eslint-disable-next-line no-control-regex /\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[()][A-Za-z0-9]|\x1b[=>]/g, "", ) // eslint-disable-next-line no-control-regex .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "") ); } type CompletionShell = "posix" | "fish" | "powershell"; /** * Predicts the shell TerminalManager will spawn for the setup terminal. The * manager takes `$SHELL` on POSIX and PowerShell on Windows, falling back to * other shells only when that one fails to spawn. */ function resolveCompletionShell( platform: NodeJS.Platform, env: NodeJS.ProcessEnv, ): CompletionShell { if (platform === "win32") return "powershell"; const shell = env.SHELL ?? ""; const name = shell.split("/").at(-1) ?? shell; if (name === "fish") return "fish"; if (name === "pwsh" || name === "powershell") return "powershell"; return "posix"; } /** * Builds the shell input for the setup script. The command runs inside a * block and the block closes on its own line, so a trailing `# comment` or a * heredoc terminator in the command cannot swallow the sentinel. The shell * reads the whole block before running any of it, so a script that reads * stdin cannot consume the sentinel line either. Lines are separated by `\r` * because that is the Enter key for every shell's line editor. */ function wrapCommandForCompletion( command: string, shell: CompletionShell, sentinel: string, ): string { const body = command.replace(/\r?\n/g, "\r"); switch (shell) { case "powershell": return `$global:LASTEXITCODE = $null; & {\r${body}\r}; if ($null -ne $LASTEXITCODE) { $__t3c = $LASTEXITCODE } elseif ($?) { $__t3c = 0 } else { $__t3c = 1 }; Write-Host "${sentinel}$__t3c"`; case "fish": return `begin\r${body}\rend; printf '\\n${sentinel}%s\\n' $status`; case "posix": return `( ${body}\r); printf '\\n${sentinel}%s\\n' "$?"`; } } /** @public Service construction is part of the canonical Effect module API. */ export const make = Effect.gen(function* () { const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery; const terminalManager = yield* TerminalManager.TerminalManager; const serverSettings = yield* ServerSettings.ServerSettingsService; const completionShell = resolveCompletionShell( yield* HostProcessPlatform, yield* HostProcessEnvironment, ); /** * Watches the setup terminal for the completion sentinel. Terminal output is * a byte stream, so partial lines are buffered until a newline. The * subscription is torn down once the sentinel, an exit, or a close arrives. */ const observeTerminalCompletion = (input: { readonly threadId: string; readonly terminalId: string; /** Per-run sentinel, so only this run's wrapper can settle completion. */ readonly sentinel: string; readonly sentinelPattern: RegExp; /** The shell echoes typed input; lines ending with these are the wrapper, not output. */ readonly echoedWrapperLines: ReadonlyArray; readonly onOutputLine: ((line: string) => Effect.Effect) | undefined; }) => Effect.gen(function* () { const startedAtMs = yield* Clock.currentTimeMillis; const done = yield* Deferred.make(); let lineBuffer = ""; let settled = false; const settle = (exitCode: number | null) => Effect.suspend(() => { if (settled) return Effect.void; settled = true; return Clock.currentTimeMillis.pipe( Effect.flatMap((nowMs) => Deferred.succeed(done, { exitCode, durationMs: nowMs - startedAtMs }), ), Effect.asVoid, ); }); const handleLine = (rawLine: string) => Effect.suspend(() => { const sentinel = input.sentinelPattern.exec(rawLine); if (sentinel) { const parsed = Number(sentinel[1]); return settle(Number.isFinite(parsed) ? parsed : null); } const cleaned = stripTerminalControl(rawLine).trimEnd(); if ( cleaned.length === 0 || cleaned.includes(input.sentinel) || input.echoedWrapperLines.some((echoed) => cleaned.endsWith(echoed)) || input.onOutputLine === undefined ) { return Effect.void; } return input.onOutputLine(cleaned.slice(0, OUTPUT_LINE_MAX_LENGTH)); }); const unsubscribe = yield* terminalManager.subscribe((event) => { if (event.threadId !== input.threadId || event.terminalId !== input.terminalId) { return Effect.void; } if (event.type === "output") { lineBuffer += event.data; // A bare carriage return is how installers redraw a progress line in // place; each redraw becomes a short line of its own instead of // being glued into one long one. The wrapper echo is filtered per // segment too, which is why `echoedWrapperLines` is split on the // same `\r`: a line editor repainting the typed command yields the // same segments. const lines = lineBuffer.split(/\r\n|\r|\n/); lineBuffer = lines.pop() ?? ""; // A script that never prints a newline must not grow this forever. // The sentinel is always on its own line, so keeping the tail is safe. if (lineBuffer.length > PARTIAL_LINE_MAX_LENGTH) { lineBuffer = lineBuffer.slice(-PARTIAL_LINE_MAX_LENGTH); } return Effect.forEach(lines, handleLine, { discard: true }); } if (event.type === "exited" || event.type === "closed") { return settle(null); } return Effect.void; }); const completion = Deferred.await(done).pipe( Effect.ensuring(Effect.sync(() => unsubscribe())), ); return { completion, unsubscribe }; }); const runForThread: ProjectSetupScriptRunner["Service"]["runForThread"] = Effect.fn( "ProjectSetupScriptRunner.runForThread", )(function* (input) { const errorContext = { threadId: input.threadId, worktreePath: input.worktreePath, ...(input.projectId === undefined ? {} : { projectId: input.projectId }), ...(input.projectCwd === undefined ? {} : { projectCwd: input.projectCwd }), }; const projectById = input.projectId ? yield* projectionSnapshotQuery.getProjectShellById(ProjectId.make(input.projectId)).pipe( Effect.map(Option.getOrUndefined), Effect.mapError( (cause) => new ProjectSetupScriptOperationError({ ...errorContext, operation: "resolveProject", cause, }), ), ) : null; const project = projectById ?? (input.projectCwd ? yield* projectionSnapshotQuery.getActiveProjectByWorkspaceRoot(input.projectCwd).pipe( Effect.map(Option.getOrUndefined), Effect.mapError( (cause) => new ProjectSetupScriptOperationError({ ...errorContext, operation: "resolveProject", cause, }), ), ) : null); if (!project) { return yield* new ProjectSetupScriptProjectNotFoundError(errorContext); } const settings = yield* serverSettings.getSettings.pipe( Effect.mapError( (cause) => new ProjectSetupScriptOperationError({ ...errorContext, operation: "readSettings", cause, }), ), ); const script = setupProjectScript(resolveProjectScripts(settings, project)); if (!script) { return { status: "no-script", } as const; } const terminalId = input.preferredTerminalId ?? `setup-${script.id}`; const cwd = input.worktreePath; const env = projectScriptRuntimeEnv({ project: { cwd: project.workspaceRoot }, worktreePath: input.worktreePath, }); const observe = input.observeCompletion; const completionToken = observe ? NodeCrypto.randomUUID().replaceAll("-", "") : null; const commandLine = observe && completionToken ? wrapCommandForCompletion( script.command, completionShell, completionSentinel(completionToken), ) : script.command; yield* terminalManager .open({ threadId: input.threadId, terminalId, cwd, worktreePath: input.worktreePath, // Setup may run before a terminal client attaches to answer color probes. env: { ...env, NO_COLOR: "1", FORCE_COLOR: "0" }, }) .pipe( Effect.mapError( (cause) => new ProjectSetupScriptOperationError({ ...errorContext, operation: "openTerminal", cause, }), ), ); // Subscribe before writing so the sentinel cannot race past the listener. const observed = observe && completionToken ? yield* observeTerminalCompletion({ threadId: input.threadId, terminalId, sentinel: completionSentinel(completionToken), sentinelPattern: completionSentinelPattern(completionToken), echoedWrapperLines: commandLine.split("\r").filter((line) => line.length > 0), onOutputLine: observe.onOutputLine, }) : undefined; yield* terminalManager .write({ threadId: input.threadId, terminalId, data: `${commandLine}\r`, }) .pipe( Effect.mapError( (cause) => new ProjectSetupScriptOperationError({ ...errorContext, operation: "writeCommand", cause, }), ), // Nothing will ever settle the completion if the command never ran. Effect.tapError(() => Effect.sync(() => observed?.unsubscribe())), ); return { status: "started", scriptId: script.id, scriptName: script.name, scriptCommand: script.command, terminalId, cwd, async: script.async !== false, ...(observed ? { completion: observed.completion } : {}), } as const; }); return ProjectSetupScriptRunner.of({ runForThread }); }); export const layer = Layer.effect(ProjectSetupScriptRunner, make);