fix(memory): use explicit qmd snippet line metadata (#58181)

* fix(memory): preserve qmd snippet line metadata

* Memory/QMD: preserve snippet span with partial line metadata
This commit is contained in:
Vincent Koc
2026-03-31 17:05:53 +09:00
committed by GitHub
parent fcc2488579
commit 075645f5cb
5 changed files with 264 additions and 10 deletions

View File

@@ -24,6 +24,22 @@ complete`,
expect(results).toEqual([{ docid: "abc", score: 0.5 }]);
});
it("preserves explicit qmd line metadata when present", () => {
const results = parseQmdQueryJson(
'[{"docid":"abc","score":0.5,"start_line":4,"end_line":6,"snippet":"@@ -10,1\\nignored"}]',
"",
);
expect(results).toEqual([
{
docid: "abc",
score: 0.5,
snippet: "@@ -10,1\nignored",
startLine: 4,
endLine: 6,
},
]);
});
it("treats plain-text no-results from stderr as an empty result set", () => {
const results = parseQmdQueryJson("", "No results found\n");
expect(results).toEqual([]);

View File

@@ -9,6 +9,8 @@ export type QmdQueryResult = {
file?: string;
snippet?: string;
body?: string;
startLine?: number;
endLine?: number;
};
export function parseQmdQueryJson(stdout: string, stderr: string): QmdQueryResult[] {
@@ -73,12 +75,40 @@ function parseQmdQueryResultArray(raw: string): QmdQueryResult[] | null {
if (!Array.isArray(parsed)) {
return null;
}
return parsed as QmdQueryResult[];
return parsed.map((item) => {
if (typeof item !== "object" || item === null) {
return item as QmdQueryResult;
}
const record = item as Record<string, unknown>;
const docid = typeof record.docid === "string" ? record.docid : undefined;
const score =
typeof record.score === "number" && Number.isFinite(record.score)
? record.score
: undefined;
const collection = typeof record.collection === "string" ? record.collection : undefined;
const file = typeof record.file === "string" ? record.file : undefined;
const snippet = typeof record.snippet === "string" ? record.snippet : undefined;
const body = typeof record.body === "string" ? record.body : undefined;
return {
docid,
score,
collection,
file,
snippet,
body,
startLine: parseQmdLineNumber(record.start_line ?? record.startLine),
endLine: parseQmdLineNumber(record.end_line ?? record.endLine),
} as QmdQueryResult;
});
} catch {
return null;
}
}
function parseQmdLineNumber(value: unknown): number | undefined {
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
}
function extractFirstJsonArray(raw: string): string | null {
const start = raw.indexOf("[");
if (start < 0) {