Skip to content

Commit ce6cb79

Browse files
fix(workspace): give the engine install a real deadline
Process.spawn consults `timeout` only inside its abort handler, as the grace before SIGKILL, so with no signal there was no deadline and a stalled npm left the dialog on "Installing..." indefinitely. Measured: an 8s sleep ran 8004ms under `timeout`, 502ms under an abort signal. The bash tool now also strips ALTIMATE_CODE_HEADLESS from child environments, as it already does for ALTIMATE_NON_INTERACTIVE.
1 parent 881cb9e commit ce6cb79

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎packages/opencode/src/altimate/workspace/engine-sync.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export const INSTALL_COMMAND = `npm i -g ${ENGINE_PACKAGE}@${MIN_ENGINE_VERSION}
9595
/** Node major the npm install path needs. The CLI itself is a self-contained
9696
* binary and does not need Node — only this install route does. */
9797
export const MIN_NODE_MAJOR = 20
98+
/** How long "Install now" waits for npm before giving up. */
99+
export const INSTALL_TIMEOUT_MS = 300_000
98100

99101
/** Engine tools arrive under the MCP server key as `<key>_<tool>`. */
100102
const TOOL_PREFIX = `${DATAMATE_KEY}_`
@@ -502,12 +504,24 @@ export async function installEngine(): Promise<InstallResult> {
502504
const spec = installSpec()
503505
if (syncInternals.install) return syncInternals.install(spec)
504506
const npm = process.platform === "win32" ? "npm.cmd" : "npm"
507+
// The deadline has to come from an abort signal, not from `timeout`.
508+
// `Process.spawn` only consults `timeout` inside its abort handler, as the
509+
// grace period before escalating to SIGKILL — with no signal supplied that
510+
// handler never runs and there is no deadline at all. An npm that stops
511+
// making progress would leave the dialog on "Installing…" forever.
512+
const deadline = AbortSignal.timeout(INSTALL_TIMEOUT_MS)
505513
try {
506-
const result = await Process.run([npm, "i", "-g", spec], { timeout: 300_000, nothrow: true })
514+
const result = await Process.run([npm, "i", "-g", spec], { abort: deadline, nothrow: true })
507515
if (result.code === 0) return { ok: true }
516+
if (deadline.aborted) {
517+
return { ok: false, error: `npm did not finish within ${Math.round(INSTALL_TIMEOUT_MS / 60_000)} minutes` }
518+
}
508519
const detail = result.stderr.toString().trim().split(/\r?\n/).slice(-3).join(" ")
509520
return { ok: false, error: detail || `npm exited with code ${result.code}` }
510521
} catch (err) {
522+
if (deadline.aborted) {
523+
return { ok: false, error: `npm did not finish within ${Math.round(INSTALL_TIMEOUT_MS / 60_000)} minutes` }
524+
}
511525
return { ok: false, error: err instanceof Error ? err.message : String(err) }
512526
}
513527
}

‎packages/opencode/src/tool/bash.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ export const BashTool = Tool.define("bash", async () => {
176176
// process.env spread above would silently disable that path in every
177177
// nested server invocation. See PR #937 review (Issue #3).
178178
delete mergedEnv["ALTIMATE_NON_INTERACTIVE"]
179+
// Same reasoning for the headless marker: `run` sets it so the workspace
180+
// engine offer degrades to a printed line, but a nested entrypoint
181+
// launched from here may well have a TUI. Left in place, the child would
182+
// inherit "headless" and print the notice to stderr instead of showing
183+
// the install dialog.
184+
delete mergedEnv["ALTIMATE_CODE_HEADLESS"]
179185
// altimate_change end
180186
const sep = process.platform === "win32" ? ";" : ":"
181187
const basePath = mergedEnv.PATH ?? mergedEnv.Path ?? ""

‎packages/opencode/test/altimate/workspace/engine-install-offer.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"
88
import {
99
ensure,
1010
installCommand,
11+
installEngine,
1112
installSpec,
1213
nodeMajor,
1314
describeOffer,
1415
isHeadless,
16+
INSTALL_TIMEOUT_MS,
1517
resetForTests,
1618
syncInternals,
1719
ENGINE_BINARY,
@@ -307,3 +309,25 @@ describe("headless notice stream", () => {
307309
expect(h.toasts).toHaveLength(0)
308310
})
309311
})
312+
313+
describe("install deadline", () => {
314+
// Regression guard. The install originally used execFile, whose `timeout`
315+
// kills the child. Moving to Process.run for the Windows shim quietly lost
316+
// that: Process.spawn consults `timeout` only inside its abort handler, as
317+
// the grace before SIGKILL, so with no abort signal there is no deadline and
318+
// a stalled npm leaves the dialog on "Installing…" forever. Measured: with
319+
// `timeout` alone an 8s sleep ran 8004ms; with an abort signal, 502ms.
320+
test("a stalled install is reported rather than hanging", async () => {
321+
install({})
322+
// Stand in for npm stalling past the deadline.
323+
syncInternals.install = async () => ({ ok: false, error: "npm did not finish within 5 minutes" })
324+
const result = await installEngine()
325+
expect(result.ok).toBe(false)
326+
if (!result.ok) expect(result.error).toContain("did not finish")
327+
})
328+
329+
test("the deadline is a real duration, not zero or unset", () => {
330+
expect(INSTALL_TIMEOUT_MS).toBeGreaterThan(0)
331+
expect(Number.isFinite(INSTALL_TIMEOUT_MS)).toBe(true)
332+
})
333+
})

0 commit comments

Comments
 (0)