Skip to content

Commit 7074b6c

Browse files
anandgupta42claude
andcommitted
fix: [#830] address consensus review — Skill.all/PostConnectSuggestions in degraded + detectDataTools via safeSpawnSync
Second-pass on PR #831 after code review. Three follow-ups in scope. 1. Skill.all() in degraded (MAJOR per reviewer) The `Skill.all().then(...).catch(() => 0)` block silently swallowed failures. If skills failed to load, the user saw `skill_count: 0` with no indication anything was wrong. Pushed "skills" into the degraded list on catch. 2. detectDataTools consistency (MAJOR) `detectDataTools` still used a bare `Bun.spawnSync` + try/catch pattern, solving the same "missing binary" problem detectGit now solves with safeSpawnSync. Two different code paths for the same semantic. Routed through safeSpawnSync for uniformity. Also exported safeSpawnSync so tests can exercise the primitive directly (was internal-only before). 3. PostConnectSuggestions degraded tracking (MINOR) The dynamic import / `trackSuggestions` call had a swallowing try/catch with a "Telemetry must never break scan output" comment. Fair principle, but the failure was invisible post-deploy. Pushed "post-connect-suggestions" into degraded so the dashboard sees when it silently misfires. 4. safeSpawnSync docs (MINOR) Reviewer flagged that the `exitCode: number` return type collapses "signaled child" and "exited 1" into the same value. Documented that contract explicitly in the JSDoc — future callers know what the helper guarantees and what it doesn't. 5. safeSpawnSync timeout passthrough detectDataTools needed the 5000ms timeout, which the original safeSpawnSync signature didn't expose. Added an optional opts.timeout parameter that forwards to Bun.spawnSync. Tests - 4 new tests for safeSpawnSync directly: returns null on missing binary, returns {exitCode, stdout} on success, returns nonzero exitCode on failed run, forwards timeout option without crashing. Validation - `tsgo --noEmit`: pass. - `bun test test/tool/project-scan.test.ts`: 82 pass / 0 fail (4 new). - Marker check: pass (no upstream-shared files in this file's path). Reviewer note - Gemini-3.1-pro second opinion failed (OpenRouter credit exhaustion). Review proceeded with single-reviewer Claude analysis. Closes review feedback for PR #831. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 4402667 commit 7074b6c

2 files changed

Lines changed: 81 additions & 19 deletions

File tree

‎packages/opencode/src/altimate/tools/project-scan.ts‎

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,29 @@ export interface ConfigFileInfo {
5656
* `project_scan` (the binary name `"git"` was masked to `?` downstream,
5757
* which made it look like a generic shell error). Wrap every spawn so a
5858
* missing binary degrades gracefully instead of crashing the whole tool.
59+
*
60+
* Return value semantics:
61+
* - `null` → could not spawn (binary missing, permission denied,
62+
* Bun internal failure). Distinct from "ran and failed".
63+
* - `{exitCode: 0, stdout}` → ran successfully.
64+
* - `{exitCode: N>0, stdout}` → ran and exited non-zero.
65+
* - `{exitCode: 1, stdout: ""}` → also returned when `Bun.spawnSync`
66+
* gives back `exitCode: null` (signaled
67+
* child). Coalesced to `1` because no
68+
* current caller distinguishes "killed
69+
* by signal" from "exited with status 1";
70+
* expand this contract if that changes.
5971
*/
60-
function safeSpawnSync(args: string[]): { exitCode: number; stdout: string } | null {
72+
export function safeSpawnSync(
73+
args: string[],
74+
opts?: { timeout?: number },
75+
): { exitCode: number; stdout: string } | null {
6176
try {
62-
const result = Bun.spawnSync(args, { stdout: "pipe", stderr: "pipe" })
77+
const result = Bun.spawnSync(args, {
78+
stdout: "pipe",
79+
stderr: "pipe",
80+
...(opts?.timeout !== undefined && { timeout: opts.timeout }),
81+
})
6382
return {
6483
exitCode: result.exitCode ?? 1,
6584
stdout: result.stdout?.toString() ?? "",
@@ -352,25 +371,23 @@ export function parseToolVersion(output: string): string | undefined {
352371
export async function detectDataTools(skip: boolean): Promise<DataToolInfo[]> {
353372
if (skip) return []
354373

374+
// Route through safeSpawnSync for consistency with detectGit. The previous
375+
// implementation used a bare try/catch around Bun.spawnSync — same outcome
376+
// for the caller, but the two functions solved the same problem two
377+
// different ways. Using one helper keeps the contract uniform: `null`
378+
// means "could not spawn" (binary missing), distinct from "ran and exited
379+
// non-zero" (binary present but the version check failed).
355380
const results = await Promise.all(
356381
DATA_TOOL_NAMES.map(async (tool): Promise<DataToolInfo> => {
357-
try {
358-
const result = Bun.spawnSync([tool, "--version"], {
359-
stdout: "pipe",
360-
stderr: "pipe",
361-
timeout: 5000,
362-
})
363-
if (result.exitCode === 0) {
364-
return {
365-
name: tool,
366-
installed: true,
367-
version: parseToolVersion(result.stdout.toString()),
368-
}
382+
const result = safeSpawnSync([tool, "--version"], { timeout: 5000 })
383+
if (result && result.exitCode === 0) {
384+
return {
385+
name: tool,
386+
installed: true,
387+
version: parseToolVersion(result.stdout),
369388
}
370-
return { name: tool, installed: false }
371-
} catch {
372-
return { name: tool, installed: false }
373389
}
390+
return { name: tool, installed: false }
374391
}),
375392
)
376393

@@ -608,7 +625,10 @@ export const ProjectScanTool = Tool.define("project_scan", {
608625
suggestionsShown: ["dbt-develop", "dbt-troubleshoot", "dbt-analyze"],
609626
})
610627
} catch {
611-
// Telemetry must never break scan output
628+
// Telemetry must never break scan output, but record it in the
629+
// degraded list so we can see post-deploy whether the dynamic
630+
// import is silently failing for some users.
631+
degraded.push("post-connect-suggestions")
612632
}
613633
// altimate_change end
614634
} else {
@@ -733,7 +753,10 @@ export const ProjectScanTool = Tool.define("project_scan", {
733753

734754
const skillCount = await Skill.all()
735755
.then((s) => s.length)
736-
.catch(() => 0)
756+
.catch(() => {
757+
degraded.push("skills")
758+
return 0
759+
})
737760

738761
Telemetry.track({
739762
type: "environment_census",

‎packages/opencode/test/tool/project-scan.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
detectDataTools,
1212
detectConfigFiles,
1313
parseToolVersion,
14+
safeSpawnSync,
1415
DATA_TOOL_NAMES,
1516
type GitInfo,
1617
type DbtProjectInfo,
@@ -930,3 +931,41 @@ describe("return type contracts", () => {
930931
expect(typeof result.preCommit).toBe("boolean")
931932
})
932933
})
934+
935+
// ---------------------------------------------------------------------------
936+
// safeSpawnSync — the new primitive both detectGit and detectDataTools route through
937+
// ---------------------------------------------------------------------------
938+
939+
describe("safeSpawnSync", () => {
940+
test("returns null when the binary is missing from PATH", () => {
941+
const result = safeSpawnSync(["this-binary-does-not-exist-anywhere-on-the-system-xyz123"])
942+
expect(result).toBeNull()
943+
})
944+
945+
test("returns { exitCode, stdout } when the binary runs successfully", () => {
946+
// Use `node --version` — present on any system that can run this test.
947+
const result = safeSpawnSync(["node", "--version"])
948+
expect(result).not.toBeNull()
949+
if (result) {
950+
expect(result.exitCode).toBe(0)
951+
expect(result.stdout).toMatch(/^v?\d+\.\d+/)
952+
}
953+
})
954+
955+
test("returns { exitCode: nonzero } when the binary exits non-zero", () => {
956+
// `node -e 'process.exit(7)'` runs but exits 7.
957+
const result = safeSpawnSync(["node", "-e", "process.exit(7)"])
958+
expect(result).not.toBeNull()
959+
if (result) {
960+
expect(result.exitCode).toBe(7)
961+
}
962+
})
963+
964+
test("forwards the timeout option to Bun.spawnSync", () => {
965+
// Just verify the helper doesn't crash when given timeout — actually
966+
// exercising the timeout path requires a long-running command, which
967+
// is flaky in CI. Smoke-check the wiring instead.
968+
const result = safeSpawnSync(["node", "--version"], { timeout: 5000 })
969+
expect(result).not.toBeNull()
970+
})
971+
})

0 commit comments

Comments
 (0)