gsd-pi 2.78.0 → 2.78.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +52 -16
  2. package/dist/claude-cli-check.js +91 -32
  3. package/dist/resources/extensions/claude-code-cli/readiness.js +115 -31
  4. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  5. package/dist/web/standalone/.next/BUILD_ID +1 -1
  6. package/dist/web/standalone/.next/app-path-routes-manifest.json +15 -15
  7. package/dist/web/standalone/.next/build-manifest.json +2 -2
  8. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  9. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  10. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  11. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  12. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  13. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  14. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  15. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  16. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  17. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  18. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  19. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  20. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  21. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  22. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  23. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  24. package/dist/web/standalone/.next/server/app/index.html +1 -1
  25. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  26. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  27. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  28. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  29. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  30. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  31. package/dist/web/standalone/.next/server/app-paths-manifest.json +15 -15
  32. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  33. package/dist/web/standalone/.next/server/middleware-manifest.json +1 -1
  34. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  35. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  36. package/package.json +1 -1
  37. package/packages/daemon/package.json +2 -2
  38. package/packages/mcp-server/package.json +2 -2
  39. package/packages/native/package.json +1 -1
  40. package/packages/pi-agent-core/package.json +1 -1
  41. package/packages/pi-ai/package.json +1 -1
  42. package/packages/pi-coding-agent/package.json +1 -1
  43. package/packages/pi-tui/package.json +1 -1
  44. package/packages/rpc-client/package.json +1 -1
  45. package/pkg/package.json +1 -1
  46. package/src/resources/extensions/claude-code-cli/readiness.ts +116 -29
  47. /package/dist/web/standalone/.next/static/{C1zT2kEfoLhDdbWPWKrXd → 7afp7gq8-DVbxum83zRQ-}/_buildManifest.js +0 -0
  48. /package/dist/web/standalone/.next/static/{C1zT2kEfoLhDdbWPWKrXd → 7afp7gq8-DVbxum83zRQ-}/_ssgManifest.js +0 -0
package/README.md CHANGED
@@ -27,34 +27,70 @@ One command. Walk away. Come back to a built project with clean git history.
27
27
 
28
28
  ---
29
29
 
30
- ## What's New in v2.77
30
+ ## What's New in v2.78
31
31
 
32
- ### Context Mode & Execution
32
+ ### Worktree Lifecycle & Forensics
33
33
 
34
- - **Context Mode** — a dispatch behavior that builds task-ready context automatically: it pulls in the most relevant project artifacts, prior session state, milestone/slice signals, and execution metadata before the model runs. This reduces manual prompt assembly, improves continuity across turns, and helps tasks start with the right files and constraints from the first dispatch. Enabled by default for new projects (opt out with `enabled: false`).
35
- - **Sandboxed execution tools** — added `gsd_exec_search`, `gsd_resume`, and sandboxed tool-output execution paths for context-mode flows, so search/resume and follow-up execution can run with tighter boundaries and more predictable runtime behavior.
36
- - **Preflight hardening** — milestone completion now performs stricter clean-root preflight checks and can auto-stash when needed, reducing accidental dirty-tree transitions and helping completion flows fail fast with clearer remediation.
34
+ - **Slice-cadence worktree collapse (#4765)** — new `git.collapse_cadence: "milestone" | "slice"` preference. With `slice`, each validated slice squash-merges to main immediately, shrinking the orphan window from milestone-size to slice-size. Pair with `git.milestone_resquash: true` to collapse per-slice commits into one milestone commit at completion.
35
+ - **Worktree telemetry (#4764)** — new journal events (`worktree-created`, `worktree-merged`, `worktree-orphaned`, `auto-exit`, `canonical-root-redirect`, `slice-merged`, `milestone-resquash`) and a `summarizeWorktreeTelemetry` aggregator that reports orphan breakdowns, merge durations, conflict counts, exit reasons, and unmerged-exit metrics.
36
+ - **`/gsd forensics` worktree section** — surfaces the telemetry above with two new anomalies: `worktree-orphan` and `worktree-unmerged-exit`.
37
+ - **Worktree-aware canonical milestone root (#4761)** — `resolveCanonicalMilestoneRoot` routes validators and cross-session readers through the live worktree, so milestone validation no longer silently reads stale project-root state.
38
+ - **Bootstrap orphan audit (#4762)** — in-progress milestones with commits ahead of main no longer get skipped; the audit emits a warning with commit count and worktree location so interrupted auto-runs are visible.
37
39
 
38
- ### Memory Architecture (ADR-013)
40
+ ### Auto Pipeline & Component System
39
41
 
40
- - **Memories table is now authoritative** — all memory reads and writes now flow through the `memories` table as the single source of truth. By removing split legacy paths, memory state stays consistent across agents, tools, and UI surfaces, with fewer reconciliation edge cases and less sync drift.
41
- - **Structured memory fields** — `structured_fields` adds typed metadata (instead of only free-form text), so memories can carry machine-usable attributes for more accurate retrieval, stronger filtering, and more dependable downstream automation and tooling.
42
- - **Dual-write migration completed** — the staged migration that wrote to both old and new paths is now fully landed, including decisions backfill and parity wiring across agents, MCP tools, and extract-learnings flows. That gives a safer upgrade path while preserving historical data and reducing cutover risk.
42
+ - **Unified component system** — skills, agents, pipelines, and marketplace are now one component model wired through runtime, dispatch, and telemetry, replacing per-surface plumbing.
43
+ - **UnitContextManifest v2 (#4924, #4934)** — auto dispatch runs through a typed manifest with declarative tools-policy and typed computed artifacts. CI guards the schema so drift fails fast.
44
+ - **Composer migration phase 3 (#4782)** — `complete-slice`, `research-milestone`, `run-uat`, and `reassess-roadmap` now build context through the manifest composer for a consistent shape across units.
45
+ - **Milestone scope classifier + pipeline variants (#4781)** — auto picks a pipeline variant from milestone shape, so research-heavy and execution-heavy milestones no longer share a one-size dispatch path.
46
+ - **Per-unit-type skill manifest resolver (#4779)** — skills wire into specific unit types instead of being globally on, with manifests expanded across the remaining types.
47
+ - **Single-writer-v3 control plane** — closes outstanding gaps in the durable-state writer model so concurrent writers can't desync workflow state.
48
+ - **Opt-in `reassess-roadmap` (#4778)** — gated behind the `skip_clean_reassess` preference per ADR-003 §4; auto no longer triggers reassessment unprompted.
43
49
 
44
- ### Skills, Tooling, and UX
50
+ ### Extensions Framework
45
51
 
46
- - **Skill coverage expanded** — 9 gap-closing skills landed and 6 planning/design skills were surfaced, improving end-to-end workflow coverage and making specialized guidance easier to discover at the point of use.
47
- - **Hook stack upgrades** — Layer 0 shell hooks and additional Layer 2 events were added to widen extension integration points: hooks can act earlier in command execution, and lifecycle consumers now receive richer event signals for orchestration, policy checks, and observability.
48
- - **TUI polish** — skill invocations now render in a dedicated chat-frame style for clearer scanability, and active-row overflow handling was fixed to prevent terminal layout breakage during long outputs.
52
+ - **Extension lifecycle commands** — `gsd extensions install / update / uninstall / list / info / validate` for npm, git, and local sources, with dependency warnings and user-metadata tracking.
53
+ - **Topological extension load order** — Kahn's-algorithm sort with surfaced `ExtensionLoadWarning`s, so dependent extensions resolve deterministically and misconfigurations are visible instead of silent.
54
+ - **cmux ↔ gsd decoupling** — static cross-imports replaced with a shared `cmux-events` contract and dynamic imports, isolating extension boundaries.
55
+ - **Extracted `@gsd-extensions/google-search` workspace** — first reference extension carved out of core; legacy in-tree source replaced with a deprecation stub.
56
+
57
+ ### Models, Agent, and UX
58
+
59
+ - **GPT-5.5 Codex support** — added across `gsd` and `pi-ai`, including `xhigh` thinking level for custom GPT-5.5 models.
60
+ - **Auth mode in `/model`** — providers display alongside auth mode for clearer routing.
61
+ - **Permission granularity picker** — Claude Code "Always Allow" prompts let you scope the grant instead of approving the broad case.
62
+ - **Headless auto default → `bypassPermissions` (#4657)** — Claude Code CLI headless auto-mode runs without permission prompts by default.
63
+ - **`skillFilter` for system prompts** — `pi-coding-agent` filters which skills are surfaced in `buildSystemPrompt`, with consumer exceptions guarded.
64
+ - **Visual postinstall (#4641)** — install shows a spinner/banner UX so first-run state is legible.
65
+ - **PR-risk verification** — risk prompt emits a copy-pasteable code block to make follow-up commands one step.
49
66
 
50
67
  ### Reliability & Safety
51
68
 
52
- - **Worktree and dispatch resilience** — crash-recovery dispatch is more robust, path derivation is safer, and worktree context fallback is improved, reducing stuck states and misrouted artifact operations after interruptions.
53
- - **DB/schema guardrails** — migration/index ordering and schema version stamping were tightened so upgrades apply deterministically and avoid index/version drift across mixed or legacy states.
54
- - **Security and validation fixes** — multiple hardening fixes landed across redaction paths, file/path checks, and pre-execution validation, closing false-positive/false-negative gaps while improving safety boundaries.
69
+ - **Major git-safety pass** — clarified TOCTOU ancestry guard, atomic sync-lock acquire with PID-verified stale override, `.git/index.lock` force-removal gated by 5-min age, `GIT_DIR`/`GIT_WORK_TREE`/`GIT_INDEX_FILE` stripped from env overlays, rebase/cherry-pick/revert state detected and aborted in recovery, working tree stashed before `reset --hard` in self-heal/rollback, worktree create guarded against unborn branches, and user hooks + `commit.gpgsign` honored on auto-commits.
70
+ - **Atomic `.gsd/` state writes** — file-locks now actually lock and throw on contention; appends are lock-wrapped to prevent interleaved writes.
71
+ - **Compaction correctness (#4665)** — fixed chunker/truncation mismatch and silent chunk-drops that produced degenerate or empty summaries.
72
+ - **Write-gate (#4950)** — fail-closed depth confirmation, EXDEV-safe snapshot rename, opt-out persistence default, off-by-one max-attempts fix, exception capture in `gate.execute`, and audit/DB rows for unknown gate ids.
73
+ - **Auto state machine** — deterministic policy errors classified non-retriable (#4973), depth-verification bypass for non-interactive sessions, baseline restored between units (#4961), `restoreToolBaseline` gated by `isAutoMode` (#4966).
74
+ - **Slice + crash recovery** — slice orchestrator state persists across crashes; ancestry-guarded force-reset, detached-HEAD refusal, and stash-by-ref recovery.
75
+ - **Empty-turn recovery** — Claude Code CLI tool-block shape canonicalized so empty-turn recovery matches real provider output.
55
76
 
56
77
  See the full [Changelog](./CHANGELOG.md) for details on every release.
57
78
 
79
+ <details>
80
+ <summary>v2.77 highlights</summary>
81
+
82
+ - **Context Mode** — dispatch builds task-ready context automatically (artifacts, prior session, milestone/slice signals, execution metadata); enabled by default for new projects
83
+ - **Sandboxed execution tools** — `gsd_exec_search`, `gsd_resume`, and sandboxed tool-output paths for context-mode flows
84
+ - **Memory architecture (ADR-013)** — `memories` table is now authoritative; `structured_fields` adds typed metadata; dual-write migration landed with decisions backfill
85
+ - **Skill coverage** — 9 gap-closing skills landed plus 6 planning/design skills surfaced
86
+ - **Hook stack** — Layer 0 shell hooks and additional Layer 2 lifecycle events
87
+ - **TUI polish** — dedicated chat-frame style for skill invocations; active-row overflow fixes
88
+ - **Worktree + dispatch resilience** — crash-recovery dispatch hardened, safer path derivation, improved worktree context fallback
89
+ - **DB/schema guardrails** — migration/index ordering and schema version stamping tightened
90
+ - **Preflight hardening** — milestone completion enforces stricter clean-root checks with auto-stash
91
+
92
+ </details>
93
+
58
94
  <details>
59
95
  <summary>v2.75 highlights</summary>
60
96
 
@@ -1,6 +1,9 @@
1
1
  // GSD2 — Claude CLI binary detection for onboarding
2
2
  // Lightweight check used at onboarding time (before extensions load).
3
3
  // The full readiness check with caching lives in the claude-code-cli extension.
4
+ //
5
+ // Set GSD_CLAUDE_DEBUG=1 to log probe output to stderr. Useful when
6
+ // diagnosing platform-specific detection failures (Issue #4997).
4
7
  import { execFileSync } from 'node:child_process';
5
8
  /**
6
9
  * Platform-correct binary name for the Claude Code CLI.
@@ -20,60 +23,116 @@ export const CLAUDE_COMMAND = process.platform === 'win32' ? 'claude.cmd' : 'cla
20
23
  * expose a bare `claude` shim. Try all three so no valid install is missed.
21
24
  */
22
25
  const CLAUDE_COMMAND_CANDIDATES = process.platform === 'win32' ? [CLAUDE_COMMAND, 'claude.exe', 'claude'] : [CLAUDE_COMMAND];
26
+ const VERSION_TIMEOUT_MS = 5_000;
27
+ // Auth probe needs more headroom on Windows because the spawn goes through
28
+ // cmd.exe → claude.cmd → node → Claude CLI.
29
+ const AUTH_TIMEOUT_MS = 15_000;
30
+ function debugLog(...parts) {
31
+ if (process.env.GSD_CLAUDE_DEBUG) {
32
+ process.stderr.write(`[claude-cli-check] ${parts.map(p => (typeof p === 'string' ? p : JSON.stringify(p))).join(' ')}\n`);
33
+ }
34
+ }
23
35
  /**
24
- * Try to run `args` against each candidate binary.
25
- * Returns the output buffer on first success, throws the last error if all fail.
36
+ * Find the first candidate that responds to `--version`. Returns the
37
+ * candidate name on success, null if none worked.
38
+ *
39
+ * On Windows with `shell: true`, a missing candidate surfaces as a
40
+ * non-zero exit from cmd.exe rather than ENOENT — so we cannot rely on
41
+ * the error code to decide "try next". Treat any failure as "try next"
42
+ * for the version probe.
26
43
  */
27
- function execClaudeCheck(args) {
28
- let lastError;
44
+ function findWorkingCommand() {
29
45
  for (const command of CLAUDE_COMMAND_CANDIDATES) {
30
46
  try {
31
- return execFileSync(command, args, {
32
- timeout: 5_000,
47
+ execFileSync(command, ['--version'], {
48
+ timeout: VERSION_TIMEOUT_MS,
33
49
  stdio: 'pipe',
34
50
  shell: process.platform === 'win32',
35
51
  });
52
+ debugLog('version probe ok via', command);
53
+ return command;
36
54
  }
37
55
  catch (error) {
38
- lastError = error;
39
- const code = error?.code;
40
- // EINVAL can surface on Windows Git Bash for .cmd spawn failures.
41
- if (code === 'ENOENT' || code === 'EINVAL')
42
- continue;
43
- throw error;
56
+ debugLog('version probe failed for', command, 'code=', error?.code);
57
+ continue;
44
58
  }
45
59
  }
46
- throw lastError ?? new Error(`Claude CLI not found (tried: ${CLAUDE_COMMAND_CANDIDATES.join(', ')})`);
60
+ return null;
47
61
  }
48
62
  /**
49
- * Check if the `claude` binary is installed (regardless of auth state).
63
+ * Decide auth state from `claude auth status` output.
64
+ *
65
+ * Newer Claude CLI builds emit JSON with a `loggedIn` boolean. Older builds
66
+ * emit free-form text. Prefer the structured signal; fall back to a text
67
+ * heuristic. The text heuristic only covers English phrasing.
50
68
  */
51
- export function isClaudeBinaryInstalled() {
52
- try {
53
- execClaudeCheck(['--version']);
54
- return true;
69
+ function parseAuthStatus(output) {
70
+ const trimmed = output.trim();
71
+ if (!trimmed)
72
+ return null;
73
+ if (trimmed.startsWith('{')) {
74
+ try {
75
+ const parsed = JSON.parse(trimmed);
76
+ if (typeof parsed.loggedIn === 'boolean') {
77
+ return parsed.loggedIn;
78
+ }
79
+ }
80
+ catch {
81
+ // Fall through to text heuristic.
82
+ }
55
83
  }
56
- catch {
84
+ const lower = trimmed.toLowerCase();
85
+ if (/not logged in|no credentials|unauthenticated|not authenticated/.test(lower)) {
57
86
  return false;
58
87
  }
88
+ if (/logged in|authenticated|signed in|email|subscription/.test(lower)) {
89
+ return true;
90
+ }
91
+ return null;
59
92
  }
60
- /**
61
- * Check if the `claude` CLI is installed AND authenticated.
62
- */
63
- export function isClaudeCliReady() {
93
+ function probeAuth(command) {
94
+ // Try --json first (newer CLIs).
64
95
  try {
65
- execClaudeCheck(['--version']);
96
+ const out = execFileSync(command, ['auth', 'status', '--json'], {
97
+ timeout: AUTH_TIMEOUT_MS,
98
+ stdio: 'pipe',
99
+ shell: process.platform === 'win32',
100
+ }).toString();
101
+ debugLog('auth status --json output:', out.slice(0, 200));
102
+ const parsed = parseAuthStatus(out);
103
+ if (parsed !== null)
104
+ return parsed;
66
105
  }
67
- catch {
68
- return false;
106
+ catch (error) {
107
+ debugLog('auth status --json threw:', error.message?.slice(0, 200));
69
108
  }
109
+ // Fallback: plain `auth status` (older CLIs that don't accept --json).
70
110
  try {
71
- const output = execClaudeCheck(['auth', 'status'])
72
- .toString()
73
- .toLowerCase();
74
- return !(/not logged in|no credentials|unauthenticated|not authenticated/i.test(output));
111
+ const out = execFileSync(command, ['auth', 'status'], {
112
+ timeout: AUTH_TIMEOUT_MS,
113
+ stdio: 'pipe',
114
+ shell: process.platform === 'win32',
115
+ }).toString();
116
+ debugLog('auth status output:', out.slice(0, 200));
117
+ return parseAuthStatus(out);
75
118
  }
76
- catch {
77
- return false;
119
+ catch (error) {
120
+ debugLog('auth status threw:', error.message?.slice(0, 200));
121
+ return null;
78
122
  }
79
123
  }
124
+ /**
125
+ * Check if the `claude` binary is installed (regardless of auth state).
126
+ */
127
+ export function isClaudeBinaryInstalled() {
128
+ return findWorkingCommand() !== null;
129
+ }
130
+ /**
131
+ * Check if the `claude` CLI is installed AND authenticated.
132
+ */
133
+ export function isClaudeCliReady() {
134
+ const command = findWorkingCommand();
135
+ if (!command)
136
+ return false;
137
+ return probeAuth(command) === true;
138
+ }
@@ -5,8 +5,13 @@
5
5
  * Results are cached for 30 seconds to avoid shelling out on every
6
6
  * model-availability check.
7
7
  *
8
- * Auth verification follows the T3 Code pattern: run `claude auth status`
9
- * and check the exit code + output for an authenticated session.
8
+ * Auth verification runs `claude auth status --json` and inspects the
9
+ * `loggedIn` field, falling back to plain `claude auth status` and a text
10
+ * heuristic when the JSON shape is unavailable (older Claude CLI builds).
11
+ *
12
+ * Set GSD_CLAUDE_DEBUG=1 to print the probe's binary selection and auth
13
+ * outputs to stderr — useful when diagnosing platform-specific detection
14
+ * failures (Issue #4997).
10
15
  */
11
16
  import { execFileSync } from "node:child_process";
12
17
  /**
@@ -23,33 +28,117 @@ const CLAUDE_COMMAND = process.platform === "win32" ? "claude.cmd" : "claude";
23
28
  * installed" results in readiness checks.
24
29
  */
25
30
  const CLAUDE_COMMAND_CANDIDATES = process.platform === "win32" ? [CLAUDE_COMMAND, "claude.exe", "claude"] : [CLAUDE_COMMAND];
26
- function execClaude(args) {
27
- let lastError;
31
+ // Keep the version probe snappy — `claude --version` is a quick path.
32
+ const VERSION_TIMEOUT_MS = 5_000;
33
+ // Auth status can be much slower on Windows because the spawn goes through
34
+ // cmd.exe → claude.cmd → node → Claude CLI. 15s leaves headroom on cold spawns
35
+ // without making startup feel hung when the CLI is genuinely missing.
36
+ const AUTH_TIMEOUT_MS = 15_000;
37
+ function debugLog(...parts) {
38
+ if (process.env.GSD_CLAUDE_DEBUG) {
39
+ process.stderr.write(`[claude-readiness] ${parts.map((p) => (typeof p === "string" ? p : JSON.stringify(p))).join(" ")}\n`);
40
+ }
41
+ }
42
+ /**
43
+ * Find the first candidate that responds to `--version`. Returns the
44
+ * candidate name on success, null if none worked.
45
+ *
46
+ * On Windows with `shell: true`, a missing candidate surfaces as a
47
+ * non-zero exit from cmd.exe rather than ENOENT — so we cannot rely on
48
+ * the error code to decide "try next". Treat any failure as "try next"
49
+ * for the version probe; the only thing that matters for binary
50
+ * detection is whether *some* candidate produces a `claude --version`
51
+ * line.
52
+ */
53
+ function findWorkingCommand() {
28
54
  for (const command of CLAUDE_COMMAND_CANDIDATES) {
29
55
  try {
30
- return execFileSync(command, args, {
31
- timeout: 5_000,
56
+ execFileSync(command, ["--version"], {
57
+ timeout: VERSION_TIMEOUT_MS,
32
58
  stdio: "pipe",
33
59
  shell: process.platform === "win32",
34
60
  });
61
+ debugLog("version probe ok via", command);
62
+ return command;
35
63
  }
36
64
  catch (error) {
37
- lastError = error;
38
- const code = error?.code;
39
- // Windows Git Bash can surface `.cmd` spawn failures as EINVAL instead
40
- // of ENOENT. Treat both as "try next candidate".
41
- if (code === "ENOENT" || code === "EINVAL") {
42
- continue;
65
+ debugLog("version probe failed for", command, "code=", error?.code);
66
+ continue;
67
+ }
68
+ }
69
+ return null;
70
+ }
71
+ /**
72
+ * Decide auth state from `claude auth status` output.
73
+ *
74
+ * Newer Claude CLI builds emit JSON by default with a `loggedIn` boolean.
75
+ * Older builds emit free-form text. We prefer the structured signal and fall
76
+ * back to a text heuristic. Note: the text heuristic only covers English
77
+ * phrasing — the JSON path is the durable signal.
78
+ */
79
+ function parseAuthStatus(output) {
80
+ const trimmed = output.trim();
81
+ if (!trimmed)
82
+ return null;
83
+ if (trimmed.startsWith("{")) {
84
+ try {
85
+ const parsed = JSON.parse(trimmed);
86
+ if (typeof parsed.loggedIn === "boolean") {
87
+ return parsed.loggedIn;
43
88
  }
44
- throw error;
45
89
  }
90
+ catch {
91
+ // Fall through to text heuristic.
92
+ }
93
+ }
94
+ const lower = trimmed.toLowerCase();
95
+ if (/not logged in|no credentials|unauthenticated|not authenticated/.test(lower)) {
96
+ return false;
97
+ }
98
+ if (/logged in|authenticated|signed in|email|subscription/.test(lower)) {
99
+ return true;
100
+ }
101
+ return null;
102
+ }
103
+ function probeAuth(command) {
104
+ // Try --json first (newer CLIs).
105
+ try {
106
+ const out = execFileSync(command, ["auth", "status", "--json"], {
107
+ timeout: AUTH_TIMEOUT_MS,
108
+ stdio: "pipe",
109
+ shell: process.platform === "win32",
110
+ }).toString();
111
+ debugLog("auth status --json output:", out.slice(0, 200));
112
+ const parsed = parseAuthStatus(out);
113
+ if (parsed !== null)
114
+ return parsed;
115
+ }
116
+ catch (error) {
117
+ debugLog("auth status --json threw:", error.message?.slice(0, 200));
118
+ }
119
+ // Fallback: plain `auth status` (older CLIs that don't accept --json).
120
+ try {
121
+ const out = execFileSync(command, ["auth", "status"], {
122
+ timeout: AUTH_TIMEOUT_MS,
123
+ stdio: "pipe",
124
+ shell: process.platform === "win32",
125
+ }).toString();
126
+ debugLog("auth status output:", out.slice(0, 200));
127
+ return parseAuthStatus(out);
128
+ }
129
+ catch (error) {
130
+ debugLog("auth status threw:", error.message?.slice(0, 200));
131
+ return null;
46
132
  }
47
- throw lastError ?? new Error(`Claude CLI executable not found (tried: ${CLAUDE_COMMAND_CANDIDATES.join(", ")})`);
48
133
  }
49
134
  let cachedBinaryPresent = null;
50
135
  let cachedAuthed = null;
51
136
  let lastCheckMs = 0;
52
137
  const CHECK_INTERVAL_MS = 30_000;
138
+ /**
139
+ * Refresh the cached binary/auth state when the cache window has expired.
140
+ * Preserves a known auth state across soft-fail auth probes.
141
+ */
53
142
  function refreshCache() {
54
143
  const now = Date.now();
55
144
  if (cachedBinaryPresent !== null && now - lastCheckMs < CHECK_INTERVAL_MS) {
@@ -57,28 +146,23 @@ function refreshCache() {
57
146
  }
58
147
  // Set timestamp first to prevent re-entrant checks during the same window
59
148
  lastCheckMs = now;
60
- // Check binary presence
61
- try {
62
- execClaude(["--version"]);
63
- cachedBinaryPresent = true;
64
- }
65
- catch {
149
+ const command = findWorkingCommand();
150
+ if (!command) {
66
151
  cachedBinaryPresent = false;
67
152
  cachedAuthed = false;
68
153
  return;
69
154
  }
70
- // Check auth status — exit code 0 with non-error output means authenticated
71
- try {
72
- const output = execClaude(["auth", "status"])
73
- .toString()
74
- .toLowerCase();
75
- // The CLI outputs "not logged in", "no credentials", or similar when unauthenticated
76
- cachedAuthed = !(/not logged in|no credentials|unauthenticated|not authenticated/i.test(output));
77
- }
78
- catch {
79
- // Non-zero exit code means not authenticated
80
- cachedAuthed = false;
155
+ cachedBinaryPresent = true;
156
+ const authed = probeAuth(command);
157
+ if (authed === null) {
158
+ // Couldn't determine auth state from CLI output. Don't clobber a
159
+ // previously known-good cache; otherwise default to false so we don't
160
+ // silently route requests to an unauthenticated CLI.
161
+ if (cachedAuthed === null)
162
+ cachedAuthed = false;
163
+ return;
81
164
  }
165
+ cachedAuthed = authed;
82
166
  }
83
167
  /**
84
168
  * Whether the `claude` binary is installed (regardless of auth state).