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: stop ingest writes after cancellation

nash_su006327d30e9a
diff --git a/src/lib/ingest.scenarios.test.ts b/src/lib/ingest.scenarios.test.ts
index cb317cf2..b9d1e903 100644
--- a/src/lib/ingest.scenarios.test.ts
+++ b/src/lib/ingest.scenarios.test.ts
@@ -23,10 +23,15 @@ vi.mock("@/commands/fs", () => realFs)
// generationResponse. Any further calls return empty (shouldn't happen in a
// typical autoIngest run).
let pendingResponses: string[] = []
+let streamCallCount = 0
+let afterStreamToken: ((callIndex: number, token: string) => void) | null = null
vi.mock("./llm-client", () => ({
streamChat: vi.fn(async (_cfg, _msgs, cb) => {
+ streamCallCount += 1
+ const callIndex = streamCallCount
const resp = pendingResponses.shift() ?? ""
cb.onToken(resp)
+ afterStreamToken?.(callIndex, resp)
cb.onDone()
}),
}))
@@ -54,6 +59,8 @@ beforeAll(async () => {
beforeEach(() => {
pendingResponses = []
+ streamCallCount = 0
+ afterStreamToken = null
useReviewStore.setState({ items: [] })
useActivityStore.setState({ items: [] })
useChatStore.setState({
@@ -362,4 +369,75 @@ describe("ingest scenarios (fixture-driven)", () => {
expect(projectB).toContain('sources: ["project-b/config.yaml"]')
expect(projectB).toContain("analysis for project B")
})
+
+ it("does not write partial generation output after cancellation", async () => {
+ ctx = { tmp: await createTempProject("ingest-cancel-generation") }
+ const projectPath = ctx.tmp.path
+
+ await writeFileRaw(`${projectPath}/schema.md`, "")
+ await writeFileRaw(`${projectPath}/purpose.md`, "")
+ await writeFileRaw(`${projectPath}/wiki/index.md`, "# Index\n")
+ await writeFileRaw(`${projectPath}/wiki/overview.md`, "# Overview\n")
+ await writeFileRaw(`${projectPath}/raw/sources/cancel.md`, "source")
+
+ useWikiStore.setState({
+ project: {
+ name: "t",
+ path: projectPath,
+ createdAt: 0,
+ purposeText: "",
+ fileTree: [],
+ } as unknown as ReturnType<typeof useWikiStore.getState>["project"],
+ })
+ useWikiStore.getState().setLlmConfig({
+ provider: "openai",
+ apiKey: "test-key",
+ model: "gpt-4",
+ ollamaUrl: "",
+ customEndpoint: "",
+ maxContextSize: 128000,
+ })
+
+ const controller = new AbortController()
+ pendingResponses = [
+ "analysis",
+ [
+ "---FILE: wiki/concepts/partial.md---",
+ "---",
+ "type: concept",
+ "title: Partial",
+ "sources: [cancel.md]",
+ "tags: []",
+ "related: []",
+ "---",
+ "",
+ "# Partial",
+ "---END FILE---",
+ "",
+ "---REVIEW: missing-page | Partial follow-up---",
+ "This should not be parsed.",
+ "---END REVIEW---",
+ ].join("\n"),
+ ]
+ afterStreamToken = (_callIndex, token) => {
+ if (token.includes("---FILE:")) controller.abort()
+ }
+
+ await expect(
+ autoIngest(
+ projectPath,
+ `${projectPath}/raw/sources/cancel.md`,
+ useWikiStore.getState().llmConfig,
+ controller.signal,
+ ),
+ ).rejects.toThrow(/Ingest cancelled/)
+
+ expect(await fileExists(`${projectPath}/wiki/concepts/partial.md`)).toBe(false)
+ expect(await fileExists(`${projectPath}/wiki/sources/cancel.md`)).toBe(false)
+ expect(useReviewStore.getState().items).toHaveLength(0)
+ expect(useActivityStore.getState().items[0]).toMatchObject({
+ status: "error",
+ detail: "Ingest cancelled",
+ })
+ })
})
diff --git a/src/lib/ingest.ts b/src/lib/ingest.ts
index 986da579..26713e9d 100644
--- a/src/lib/ingest.ts
+++ b/src/lib/ingest.ts
@@ -482,6 +482,17 @@ export async function autoIngest(
)
}
+function throwIfIngestAborted(signal: AbortSignal | undefined, activityId?: string): void {
+ if (!signal?.aborted) return
+ if (activityId) {
+ useActivityStore.getState().updateItem(activityId, {
+ status: "error",
+ detail: "Ingest cancelled",
+ })
+ }
+ throw new Error("Ingest cancelled")
+}
+
async function autoIngestImpl(
projectPath: string,
sourcePath: string,
@@ -527,7 +538,7 @@ async function autoIngestImpl(
mineruSucceeded = true
console.log(`[ingest:mineru] cached MinerU output for "${fileName}" (${markdown.length} chars)`)
} catch (err) {
- if (signal?.aborted) throw err
+ throwIfIngestAborted(signal, activityId)
const msg = trimInlineStatus(err instanceof Error ? err.message : String(err))
console.warn(`[ingest:mineru] MinerU parsing failed, falling back to pdfium: ${msg}`)
activity.updateItem(activityId, {
@@ -886,6 +897,7 @@ async function autoIngestImpl(
if (generationActivity?.status === "error") {
throw new Error(generationActivity.detail || "Generation stream failed")
}
+ throwIfIngestAborted(signal, activityId)
let reviewSuggestionOutput = ""
if (!signal?.aborted && shouldRunDedicatedReviewStage(generation)) {
@@ -927,14 +939,15 @@ async function autoIngestImpl(
},
)
} catch (err) {
- if (signal?.aborted) throw err
+ throwIfIngestAborted(signal, activityId)
console.warn(`[ingest] Review suggestion generation failed for "${sourceIdentity}":`, err)
}
- if (signal?.aborted) throw new Error("Ingest cancelled")
+ throwIfIngestAborted(signal, activityId)
if (reviewStageHadError) reviewSuggestionOutput = ""
}
// ── Step 3: Write files ───────────────────────────────────────
+ throwIfIngestAborted(signal, activityId)
activity.updateItem(activityId, { detail: "Writing files..." })
await migrateLegacySourceSummaryIfSafe(pp, sourceIdentity, sourceSummaryPath)
const writeResult = await writeFileBlocks(
@@ -944,7 +957,9 @@ async function autoIngestImpl(
sourceIdentity,
sourceSummaryPath,
signal,
+ activityId,
)
+ throwIfIngestAborted(signal, activityId)
const writtenPaths = writeResult.writtenPaths
const writeWarnings = writeResult.warnings
const hardFailures = writeResult.hardFailures
@@ -1003,7 +1018,7 @@ async function autoIngestImpl(
max_tokens: computeIngestReviewMaxTokens(llmConfig.maxContextSize),
},
)
- if (signal?.aborted) throw new Error("Ingest cancelled")
+ throwIfIngestAborted(signal, activityId)
if (aggregateRepairOutput.trim()) {
const filteredRepair = filterAggregateRepairOutput(
aggregateRepairOutput,
@@ -1017,13 +1032,14 @@ async function autoIngestImpl(
sourceIdentity,
sourceSummaryPath,
signal,
+ activityId,
)
writtenPaths.push(...repairResult.writtenPaths)
writeWarnings.push(...repairResult.warnings)
hardFailures.push(...repairResult.hardFailures)
}
} catch (err) {
- if (signal?.aborted) throw err
+ throwIfIngestAborted(signal, activityId)
writeWarnings.push(
`Aggregate repair failed: ${err instanceof Error ? err.message : String(err)}`,
)
@@ -1097,6 +1113,7 @@ async function autoIngestImpl(
}
// ── Step 4: Parse review items ────────────────────────────────
+ throwIfIngestAborted(signal, activityId)
const reviewItems = [
...parseReviewBlocks(generation, sp),
...parseReviewBlocks(reviewSuggestionOutput, sp),
@@ -1518,6 +1535,7 @@ async function writeFileBlocks(
sourceFileName: string,
sourceSummaryPath?: string,
signal?: AbortSignal,
+ activityId?: string,
): Promise<{ writtenPaths: string[]; warnings: string[]; hardFailures: string[] }> {
const { blocks, warnings: parseWarnings } = parseFileBlocks(text)
const warnings = [...parseWarnings]
@@ -1537,6 +1555,7 @@ async function writeFileBlocks(
const today = currentWikiDate()
for (const { path: rawRelativePath, content: rawContent } of blocks) {
+ throwIfIngestAborted(signal, activityId)
let relativePath = rawRelativePath
if (sourceSummaryPath && relativePath.startsWith("wiki/sources/")) {
relativePath = sourceSummaryPath
@@ -2465,7 +2484,7 @@ async function analyzeLongSourceInChunks(
for (const chunk of chunks) {
if (chunk.index <= completedThrough) continue
- if (signal?.aborted) throw new Error("Ingest cancelled")
+ throwIfIngestAborted(signal, activityId)
activity.updateItem(activityId, {
detail: `Analyzing long source chunk ${chunk.index}/${chunk.total}...`,
})
@@ -2498,7 +2517,7 @@ async function analyzeLongSourceInChunks(
{ temperature: 0.1, reasoning: { mode: "off" }, max_tokens: 4096 },
)
- if (signal?.aborted) throw new Error("Ingest cancelled")
+ throwIfIngestAborted(signal, activityId)
if (hadError) throw new Error("Chunk analysis stream failed")
const chunkAnalysis = extractMarkedSection(raw, "Chunk Analysis") || raw.trim()