Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: surface real claude CLI failure cause instead of bare "exit 1"

nash_su62e471beb59c
diff --git a/src/components/settings/sections/llm-provider-section.tsx b/src/components/settings/sections/llm-provider-section.tsx
index 49a5fa32..fd0930c1 100644
--- a/src/components/settings/sections/llm-provider-section.tsx
+++ b/src/components/settings/sections/llm-provider-section.tsx
@@ -496,6 +496,18 @@ function ClaudeCliStatusPill() {
{result.path}
</div>
)}
+ {/* `claude --version` doesn't validate OAuth, so even a
+ green pill can hide an expired login. Surface the
+ remediation up front so users don't mis-diagnose
+ the resulting "Unauthenticated" exit-1 as a LLM
+ Wiki bug. */}
+ <div className="text-muted-foreground">
+ If chat fails with an authentication error, run{" "}
+ <code className="rounded bg-background/60 px-1 py-0.5 font-mono text-[10px]">
+ claude
+ </code>{" "}
+ in a terminal to refresh the OAuth login.
+ </div>
</>
)}
{state === "err" && (
diff --git a/src/lib/__tests__/claude-cli-transport.test.ts b/src/lib/__tests__/claude-cli-transport.test.ts
index 508b7153..845d207c 100644
--- a/src/lib/__tests__/claude-cli-transport.test.ts
+++ b/src/lib/__tests__/claude-cli-transport.test.ts
@@ -1,5 +1,8 @@
import { describe, it, expect } from "vitest"
-import { createClaudeCodeStreamParser } from "../claude-cli-transport"
+import {
+ createClaudeCodeStreamParser,
+ buildExitError,
+} from "../claude-cli-transport"
describe("createClaudeCodeStreamParser", () => {
it("emits text from a single stream_event text_delta", () => {
@@ -120,3 +123,62 @@ describe("createClaudeCodeStreamParser", () => {
).toBeNull()
})
})
+
+describe("buildExitError", () => {
+ it("translates Unauthenticated stderr into an actionable login hint", () => {
+ const msg = buildExitError(1, "Unauthenticated: please log in")
+ expect(msg).toMatch(/not authenticated/i)
+ expect(msg).toMatch(/`claude`/)
+ expect(msg).toMatch(/terminal/i)
+ })
+
+ it("includes the original stderr at the bottom for context", () => {
+ const stderr = "Unauthenticated: token expired"
+ const msg = buildExitError(1, stderr)
+ expect(msg).toContain(stderr)
+ })
+
+ it("falls through to the bare exit-code form for unrecognized stderr", () => {
+ expect(buildExitError(2, "Unknown flag: --foo")).toBe(
+ "claude CLI exited with code 2: Unknown flag: --foo",
+ )
+ })
+
+ it("works without stderr at all (truly silent exit)", () => {
+ const msg = buildExitError(127, "")
+ expect(msg).toMatch(/silently/)
+ expect(msg).toMatch(/127/)
+ expect(msg).toMatch(/terminal/)
+ })
+
+ it("matches the case-insensitive Authentication failed variant", () => {
+ const msg = buildExitError(1, "Authentication failed (401)")
+ expect(msg).toMatch(/not authenticated/i)
+ })
+
+ it("falls back to unparsed stdout when stderr is empty (the real-user case)", () => {
+ // Real-user scenario: claude exit 1, stderr empty, but stdout
+ // had a structured error event our parser didn't recognize.
+ // Without this branch the user just saw "exited with code 1"
+ // and had to grep the binary to guess what went wrong.
+ const stdout = '{"type":"error","subtype":"oauth_expired","message":"token revoked"}'
+ const msg = buildExitError(1, "", stdout)
+ expect(msg).toContain("code 1")
+ expect(msg).toContain("no stderr")
+ expect(msg).toContain("oauth_expired")
+ expect(msg).toContain("token revoked")
+ })
+
+ it("prefers stderr over unparsed stdout when both are present", () => {
+ const msg = buildExitError(1, "real stderr here", "unrelated stdout")
+ expect(msg).toContain("real stderr here")
+ expect(msg).not.toContain("unrelated stdout")
+ })
+
+ it("recommends terminal reproduction when both stderr and stdout are empty", () => {
+ const msg = buildExitError(1, "", "")
+ expect(msg).toMatch(/silently/)
+ expect(msg).toMatch(/terminal/)
+ expect(msg).toMatch(/Anthropic API/)
+ })
+})
diff --git a/src/lib/claude-cli-transport.ts b/src/lib/claude-cli-transport.ts
index 3f464c7d..2c0c9fe4 100644
--- a/src/lib/claude-cli-transport.ts
+++ b/src/lib/claude-cli-transport.ts
@@ -144,6 +144,25 @@ export async function streamClaudeCodeCli(
let unlistenDone: UnlistenFn | undefined
let finished = false
+ // Diagnostic capture for failure paths. The Rust side emits every
+ // stdout line; lines the parser doesn't recognize (non-JSON,
+ // unknown event types, the stream-json `{"type":"error",...}`
+ // shape claude can emit on auth failure) used to be silently
+ // dropped — leaving users staring at a bare "exit code 1" with
+ // nothing to act on. We collect them up to a hard cap so that if
+ // the child exits non-zero AND stderr is empty, we have something
+ // concrete to show in the error message.
+ const UNPARSED_BUFFER_CAP = 4096
+ const unparsedLines: string[] = []
+ let unparsedSize = 0
+ function captureUnparsed(line: string) {
+ if (unparsedSize >= UNPARSED_BUFFER_CAP) return
+ const trimmed = line.trim()
+ if (trimmed.length === 0) return
+ unparsedLines.push(line)
+ unparsedSize += line.length + 1
+ }
+
const cleanup = () => {
unlistenData?.()
unlistenDone?.()
@@ -169,7 +188,15 @@ export async function streamClaudeCodeCli(
// Listen FIRST so we don't miss the very first event on fast CLIs.
unlistenData = await listen<string>(`claude-cli:${streamId}`, (event) => {
const token = parse(event.payload)
- if (token !== null) onToken(token)
+ if (token !== null) {
+ onToken(token)
+ } else {
+ // Parser didn't recognize this line. Stash it in case the
+ // child later exits non-zero with empty stderr — at that
+ // point this captured stdout is the only diagnostic the
+ // user has.
+ captureUnparsed(event.payload)
+ }
})
unlistenDone = await listen<{ code: number | null; stderr: string }>(
@@ -178,12 +205,10 @@ export async function streamClaudeCodeCli(
const code = event.payload?.code
const stderr = event.payload?.stderr?.trim() ?? ""
if (code !== null && code !== undefined && code !== 0) {
- // Include stderr in the message so the user sees the actual
- // failure reason (missing flag, bad model id, auth issue, etc.)
- // rather than a bare exit code.
- const detail = stderr ? `: ${stderr}` : ""
finishWith(() =>
- onError(new Error(`claude CLI exited with code ${code}${detail}`)),
+ onError(
+ new Error(buildExitError(code, stderr, unparsedLines.join("\n"))),
+ ),
)
} else {
finishWith(onDone)
@@ -215,3 +240,59 @@ export async function streamClaudeCodeCli(
signal?.removeEventListener("abort", abortListener)
}
}
+
+/**
+ * Translate `claude` CLI exit-with-stderr into an actionable error
+ * message for the user. The bare "exited with code N: <stderr>"
+ * we used to throw was correct but unactionable — users had to
+ * read JSON-shaped stderr text to figure out what to do.
+ *
+ * Three diagnostic sources, used in priority order:
+ * 1. stderr — the canonical place. The most common content is
+ * `Unauthenticated:` from Claude Code itself, meaning the
+ * user's ~/.claude OAuth token expired / was revoked / they
+ * logged out. We surface that case explicitly because users
+ * otherwise mis-diagnose it as an LLM Wiki bug.
+ * 2. unparsedStdout — stdout lines the parser didn't recognize
+ * (non-JSON, unknown event types, the stream-json `error`
+ * event shape). Used as a fallback when stderr is empty —
+ * claude sometimes writes its real diagnostic to stdout via
+ * the stream-json channel, and our parser silently drops
+ * anything it doesn't classify, leaving users with no info
+ * at all.
+ * 3. Neither — silent exit. We can't help much here other than
+ * telling the user to reproduce in a terminal where they can
+ * see whatever output the CLI does produce.
+ */
+export function buildExitError(
+ code: number,
+ stderr: string,
+ unparsedStdout: string = "",
+): string {
+ if (/unauthenticated|please.*log\s*in|authentication.*failed/i.test(stderr)) {
+ return [
+ "Claude Code CLI is not authenticated.",
+ "Please open a terminal and run `claude` to complete the OAuth login,",
+ "then retry. (LLM Wiki only spawns the binary — it can't run the",
+ "login flow on your behalf.)",
+ stderr ? `\n\n— stderr —\n${stderr}` : "",
+ ].join(" ").trim()
+ }
+ if (stderr) {
+ return `claude CLI exited with code ${code}: ${stderr}`
+ }
+ if (unparsedStdout.trim()) {
+ return [
+ `claude CLI exited with code ${code} (no stderr).`,
+ "Captured stdout output that LLM Wiki couldn't parse — pasting it",
+ "here so you can see what the CLI actually emitted:\n",
+ unparsedStdout.trim(),
+ ].join(" ")
+ }
+ return [
+ `claude CLI exited silently with code ${code}.`,
+ "No stdout or stderr was captured — try running `claude -p` in a",
+ "terminal with the same prompt to see what's wrong, or switch to",
+ "the official Anthropic API in Settings.",
+ ].join(" ")
+}