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: retain actionable Claude CLI diagnostics

nash_su34d8e90243af
diff --git a/src/lib/__tests__/claude-cli-transport.test.ts b/src/lib/__tests__/claude-cli-transport.test.ts
index 1653b1a0..3b5ffe34 100644
--- a/src/lib/__tests__/claude-cli-transport.test.ts
+++ b/src/lib/__tests__/claude-cli-transport.test.ts
@@ -30,7 +30,9 @@ vi.mock("@tauri-apps/api/event", () => ({
import {
createClaudeCodeStreamParser,
buildExitError,
+ createBoundedDiagnosticBuffer,
extractClaudeCodeStructuredError,
+ shouldCaptureClaudeDiagnostic,
streamClaudeCodeCli,
} from "../claude-cli-transport"
import { useWikiStore } from "@/stores/wiki-store"
@@ -506,3 +508,42 @@ describe("extractClaudeCodeStructuredError", () => {
}))).toBe("rate limit exceeded")
})
})
+
+describe("Claude CLI diagnostic buffering", () => {
+ it("ignores normal structured lifecycle and hook events", () => {
+ expect(shouldCaptureClaudeDiagnostic(JSON.stringify({
+ type: "system",
+ subtype: "hook_started",
+ message: "running hook",
+ }))).toBe(false)
+ expect(shouldCaptureClaudeDiagnostic(JSON.stringify({
+ type: "result",
+ is_error: false,
+ result: "done",
+ }))).toBe(false)
+ })
+
+ it("keeps non-JSON and structured error diagnostics", () => {
+ expect(shouldCaptureClaudeDiagnostic("fatal process failure")).toBe(true)
+ expect(shouldCaptureClaudeDiagnostic(JSON.stringify({
+ type: "result",
+ is_error: true,
+ result: "token expired",
+ }))).toBe(true)
+ })
+
+ it("retains the newest diagnostics when capacity is exceeded", () => {
+ const buffer = createBoundedDiagnosticBuffer(12)
+ buffer.append("old-message")
+ buffer.append("FINAL-ERROR")
+ expect(buffer.value()).toContain("FINAL-ERROR")
+ expect(buffer.value()).not.toContain("old-message")
+ expect(Array.from(buffer.value()).length).toBeLessThanOrEqual(12)
+ })
+
+ it("does not split unicode code points at the capacity boundary", () => {
+ const buffer = createBoundedDiagnosticBuffer(3)
+ buffer.append("错误信息")
+ expect(buffer.value()).toBe("误信息")
+ })
+})
diff --git a/src/lib/claude-cli-transport.ts b/src/lib/claude-cli-transport.ts
index 947096a1..b78715f8 100644
--- a/src/lib/claude-cli-transport.ts
+++ b/src/lib/claude-cli-transport.ts
@@ -130,6 +130,43 @@ export function extractClaudeCodeStructuredError(rawLine: string): string | null
return null
}
+export function shouldCaptureClaudeDiagnostic(rawLine: string): boolean {
+ const line = rawLine.trim()
+ if (!line) return false
+ try {
+ const value = JSON.parse(line) as unknown
+ if (!value || typeof value !== "object") return true
+ const event = value as Record<string, unknown>
+ return event.is_error === true
+ || (typeof event.type === "string" && event.type.toLowerCase().includes("error"))
+ || extractClaudeCodeStructuredError(line) !== null
+ } catch {
+ return true
+ }
+}
+
+export function createBoundedDiagnosticBuffer(capacity = 4096) {
+ let buffer = ""
+ return {
+ append(line: string) {
+ const trimmed = line.trim()
+ if (!trimmed || capacity <= 0) return
+ const incomingCharacters = Array.from(trimmed)
+ const incoming = incomingCharacters.length > capacity
+ ? incomingCharacters.slice(incomingCharacters.length - capacity).join("")
+ : trimmed
+ const combined = buffer ? `${buffer}\n${incoming}` : incoming
+ const characters = Array.from(combined)
+ buffer = characters.length > capacity
+ ? characters.slice(characters.length - capacity).join("")
+ : combined
+ },
+ value() {
+ return buffer
+ },
+ }
+}
+
// Tauri's `invoke` typing requires the payload object to satisfy
// `Record<string, unknown>` (an index signature). Plain interfaces
// don't provide one, so we use a `type` alias with the explicit
@@ -189,24 +226,10 @@ export async function streamClaudeCodeCli(
resolveCompletion = resolve
})
- // 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
- }
+ // Keep only protocol-level errors and non-JSON diagnostics. Normal lifecycle
+ // and hook events are intentionally excluded so they cannot crowd the real
+ // exit error out of this bounded tail buffer.
+ const diagnosticBuffer = createBoundedDiagnosticBuffer()
const cleanup = () => {
unlistenData?.()
@@ -251,12 +274,9 @@ export async function streamClaudeCodeCli(
if (token !== null) {
emittedToken = true
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)
+ } else if (!eventError && shouldCaptureClaudeDiagnostic(event.payload)) {
+ // Preserve the newest diagnostic in case stderr is empty.
+ diagnosticBuffer.append(event.payload)
}
})
if (aborted || finished) {
@@ -272,7 +292,7 @@ export async function streamClaudeCodeCli(
if (code !== null && code !== undefined && code !== 0) {
finishWith(() =>
onError(
- new Error(buildExitError(code, stderr, structuredError || unparsedLines.join("\n"))),
+ new Error(buildExitError(code, stderr, structuredError || diagnosticBuffer.value())),
),
)
} else if (structuredError) {
@@ -281,7 +301,7 @@ export async function streamClaudeCodeCli(
// CLI exited successfully but produced no assistant text.
// Surface this as an explicit error so the ingest pipeline
// retries rather than silently writing an empty stub page.
- const details = stderr || unparsedLines.join("\n").trim()
+ const details = stderr || diagnosticBuffer.value()
finishWith(() =>
onError(new Error(
details