mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-18 10:31:30 +08:00
feat(claude-bin): Bun.which wrapper for cross-platform claude resolution
Replaces 75 LOC of fork-side reimplementation (PATH parsing, Windows PATHEXT, case-insensitive Path/PATH, X_OK) with a thin wrapper around Bun.which() — the runtime built-in that already does all of it. New file is ~70 LOC including the override + arg-prefix logic the runtime doesn't cover. Override branch fixed: GSTACK_CLAUDE_BIN=wsl now resolves through Bun.which() just like a bare claude lookup would. The McGluut fork's claude-bin.ts only handled absolute-path overrides; bare commands silently returned null. Passing the override value through Bun.which fixes the documented use case for free. Five hardcoded claude spawn sites rewired through resolveClaudeCommand: - browse/src/security-classifier.ts:396 — version probe - browse/src/security-classifier.ts:496 — Haiku transcript classifier - scripts/preflight-agent-sdk.ts — preflight binary pinning - test/helpers/providers/claude.ts — LLM judge availability + run - test/helpers/agent-sdk-runner.ts — SDK harness binary resolver All retain their existing degrade-on-missing semantics. Tests: browse/test/claude-bin.test.ts has 9 unit tests including the override-PATH-resolution case the fork's version got wrong. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
73
browse/src/claude-bin.ts
Normal file
73
browse/src/claude-bin.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* claude-bin.ts — Cross-platform `claude` binary resolution.
|
||||
*
|
||||
* Uses Bun.which() for the platform handling (PATH parsing, Windows PATHEXT,
|
||||
* X_OK, case-insensitive Path/PATH on Windows). Adds the gstack-specific
|
||||
* override + arg-prefix logic on top.
|
||||
*
|
||||
* Override precedence:
|
||||
* 1. GSTACK_CLAUDE_BIN (or CLAUDE_BIN as fallback) — absolute path or
|
||||
* PATH-resolvable command. `wsl` resolves through Bun.which('wsl') just
|
||||
* like a bare `claude` lookup would.
|
||||
* 2. Plain `Bun.which('claude')` if no override is set.
|
||||
*
|
||||
* Arg prefix:
|
||||
* GSTACK_CLAUDE_BIN_ARGS (or CLAUDE_BIN_ARGS) prepends arguments to every
|
||||
* spawn. Accepts a JSON array (e.g. '["claude", "--no-cache"]') or a single
|
||||
* scalar string treated as one argument. Only applied when an override is
|
||||
* active — bare `claude` resolution doesn't pick up an arg prefix.
|
||||
*
|
||||
* Returns null when nothing resolves; callers should degrade (e.g. transcript
|
||||
* classifier returns degraded:true) rather than throw.
|
||||
*/
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
export interface ClaudeCommand {
|
||||
command: string;
|
||||
argsPrefix: string[];
|
||||
}
|
||||
|
||||
function stripWrappingQuotes(value: string): string {
|
||||
return value.replace(/^"(.*)"$/, '$1');
|
||||
}
|
||||
|
||||
function parseOverrideArgs(env: NodeJS.ProcessEnv): string[] {
|
||||
const raw = env.GSTACK_CLAUDE_BIN_ARGS ?? env.CLAUDE_BIN_ARGS;
|
||||
if (!raw?.trim()) return [];
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (Array.isArray(parsed) && parsed.every((v) => typeof v === 'string')) {
|
||||
return parsed;
|
||||
}
|
||||
} catch {
|
||||
// Not JSON — treat as a single scalar argument.
|
||||
}
|
||||
return [stripWrappingQuotes(raw.trim())];
|
||||
}
|
||||
|
||||
export function resolveClaudeCommand(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): ClaudeCommand | null {
|
||||
const argsPrefix = parseOverrideArgs(env);
|
||||
const override = (env.GSTACK_CLAUDE_BIN ?? env.CLAUDE_BIN)?.trim();
|
||||
// Honor case-insensitive Path/PATH on Windows. Bun.which itself reads
|
||||
// process.env so we forward whichever the caller passed.
|
||||
const PATH = env.PATH ?? env.Path ?? '';
|
||||
|
||||
if (override) {
|
||||
const trimmed = stripWrappingQuotes(override);
|
||||
// Absolute path: use as-is. Otherwise PATH-resolve through Bun.which so
|
||||
// overrides like GSTACK_CLAUDE_BIN=wsl find the actual binary.
|
||||
const resolved = path.isAbsolute(trimmed) ? trimmed : Bun.which(trimmed, { PATH });
|
||||
return resolved ? { command: resolved, argsPrefix } : null;
|
||||
}
|
||||
|
||||
const command = Bun.which('claude', { PATH });
|
||||
return command ? { command, argsPrefix: [] } : null;
|
||||
}
|
||||
|
||||
/** Convenience wrapper for callers that only need the command path. */
|
||||
export function resolveClaudeBinary(env: NodeJS.ProcessEnv = process.env): string | null {
|
||||
return resolveClaudeCommand(env)?.command ?? null;
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import { THRESHOLDS, type LayerSignal } from './security';
|
||||
import { resolveClaudeCommand } from './claude-bin';
|
||||
|
||||
/**
|
||||
* Pinned Haiku model for the transcript classifier. Bumped deliberately when a
|
||||
@@ -392,8 +393,13 @@ let haikuAvailableCache: boolean | null = null;
|
||||
|
||||
function checkHaikuAvailable(): Promise<boolean> {
|
||||
if (haikuAvailableCache !== null) return Promise.resolve(haikuAvailableCache);
|
||||
const claude = resolveClaudeCommand();
|
||||
if (!claude) {
|
||||
haikuAvailableCache = false;
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const p = spawn('claude', ['--version'], { stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
const p = spawn(claude.command, [...claude.argsPrefix, '--version'], { stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
let done = false;
|
||||
const finish = (ok: boolean) => {
|
||||
if (done) return;
|
||||
@@ -493,7 +499,12 @@ export async function checkTranscript(params: {
|
||||
// timeout rate in the v1.5.2.0 ensemble bench because of this, plus
|
||||
// ~44k cache_creation tokens per call (massive cost inflation).
|
||||
// Using os.tmpdir() gives Haiku a clean context for pure classification.
|
||||
const p = spawn('claude', [
|
||||
const claude = resolveClaudeCommand();
|
||||
if (!claude) {
|
||||
return finish({ layer: 'transcript_classifier', confidence: 0, meta: { degraded: true, reason: 'claude_cli_not_found' } });
|
||||
}
|
||||
const p = spawn(claude.command, [
|
||||
...claude.argsPrefix,
|
||||
'-p', prompt,
|
||||
'--model', HAIKU_MODEL,
|
||||
'--output-format', 'json',
|
||||
|
||||
Reference in New Issue
Block a user