fix(knowledge-fs): handle unknown unit ID errors with LlmSemanticChunkingOutputError

This commit is contained in:
FFXN 2026-09-03 22:11:17 +08:00
parent f291a76135
commit b904fbe066
2 changed files with 34 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import { createInMemoryDocumentSemanticWindowCheckpointRepository } from "./docu
import {
DEFAULT_MAX_SEMANTIC_WINDOWS,
LlmSemanticChunkingOutputError,
type LlmSemanticCompletionCatalogEntry,
type LlmSemanticWindowManifestEntry,
type SemanticChunkingLlmProvider,
@ -1513,6 +1514,36 @@ describe("LLM semantic chunker", () => {
).rejects.toThrow(error);
});
it("classifies an unknown response unit ID as an invalid model-runtime response", async () => {
const chunker = createLlmSemanticChunker({
maxChunkChars: 20,
reasoningProviderFactory: () =>
new ScriptedProvider([
({ units }) => ({ chunks: [chunkRange(units[0]?.id, "u-missing")] }),
]),
});
const failure = chunker.chunk({
knowledgeSpaceId: KNOWLEDGE_SPACE_ID,
parseArtifact: artifact([
{
id: "paragraph",
metadata: {},
sectionPath: ["Validation"],
text: "Alpha. Beta.",
type: "paragraph",
},
]),
retrievalProfile: profile(),
});
await expect(failure).rejects.toBeInstanceOf(LlmSemanticChunkingOutputError);
await expect(failure).rejects.toMatchObject({
code: "MODEL_RUNTIME_RESPONSE_INVALID",
retryable: false,
});
});
it("drops ungrounded image OCR entities and their relations without discarding the chunk", async () => {
const provider = new ScriptedProvider([
({ units }) => ({

View File

@ -2836,7 +2836,9 @@ function validateAndMaterializeWindowOutput({
const start = unitIndex.get(candidate.startUnitId);
const end = unitIndex.get(candidate.endUnitId);
if (start === undefined || end === undefined) {
throw new Error("LLM semantic chunking response referenced an unknown unit ID");
throw new LlmSemanticChunkingOutputError(
"LLM semantic chunking response referenced an unknown unit ID",
);
}
if (start !== expectedStart || end < start) {
throw new Error(