Skip to content

Commit 0ec2510

Browse files
committed
fix(memory): preserve QMD results in noisy command output
1 parent cb167ac commit 0ec2510

2 files changed

Lines changed: 78 additions & 16 deletions

File tree

‎packages/memory-host-sdk/src/host/qmd-query-parser.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ complete`,
2525
expect(results).toEqual([{ docid: "abc", score: 0.5 }]);
2626
});
2727

28+
it.each([
29+
{
30+
name: "bracketed log prefix",
31+
stdout: '[qmd] initializing search\n[{"docid":"abc","score":0.5}]',
32+
},
33+
{
34+
name: "bracketed log with an empty array",
35+
stdout: '[qmd] filters=[]\n[{"docid":"abc","score":0.5}]',
36+
},
37+
{
38+
name: "bracketed log with result-shaped data",
39+
stdout: '[qmd] previous=[{"docid":"stale","score":1}]\n[{"docid":"abc","score":0.5}]',
40+
},
41+
{
42+
name: "leading no-results marker",
43+
stdout: 'warning: no results found\n[{"docid":"abc","score":0.5}]',
44+
},
45+
{
46+
name: "trailing no-results marker",
47+
stdout: '[{"docid":"abc","score":0.5}]\nwarning: no results found',
48+
},
49+
{
50+
name: "bracketed no-results marker",
51+
stdout: '[qmd] warning: no results found\n[{"docid":"abc","score":0.5}]',
52+
},
53+
])("preserves query results after a $name", ({ stdout }) => {
54+
expect(parseQmdQueryJson(stdout, "")).toEqual([{ docid: "abc", score: 0.5 }]);
55+
});
56+
2857
it("preserves explicit qmd line metadata when present", () => {
2958
const results = parseQmdQueryJson(
3059
'[{"docid":"abc","score":0.5,"start_line":4,"end_line":6,"snippet":"@@ -10,1\\nignored"}]',
@@ -58,6 +87,9 @@ complete`,
5887
it("treats prefixed no-results marker output as an empty result set", () => {
5988
expect(parseQmdQueryJson("warning: no results found", "")).toStrictEqual([]);
6089
expect(parseQmdQueryJson("", "[qmd] warning: no results found\n")).toStrictEqual([]);
90+
expect(
91+
parseQmdQueryJson("[qmd] initializing search\nwarning: no results found", ""),
92+
).toStrictEqual([]);
6193
});
6294

6395
it("keeps bounded stderr context UTF-16 safe", () => {
@@ -81,6 +113,12 @@ complete`,
81113
);
82114
});
83115

116+
it("rejects malformed bracket-heavy stdout without repeated rescanning", () => {
117+
expect(() => parseQmdQueryJson("[".repeat(4_096), "")).toThrow(
118+
/qmd query returned invalid JSON/i,
119+
);
120+
});
121+
84122
it("routes invalid-output diagnostics through console capture", () => {
85123
vi.stubEnv("VITEST", "");
86124
vi.stubEnv("NODE_ENV", "production");

‎packages/memory-host-sdk/src/host/qmd-query-parser.ts‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ export type QmdQueryResult = {
2121
export function parseQmdQueryJson(stdout: string, stderr: string): QmdQueryResult[] {
2222
const trimmedStdout = stdout.trim();
2323
const trimmedStderr = stderr.trim();
24-
const stdoutIsMarker = trimmedStdout.length > 0 && isQmdNoResultsOutput(trimmedStdout);
2524
const stderrIsMarker = trimmedStderr.length > 0 && isQmdNoResultsOutput(trimmedStderr);
26-
if (stdoutIsMarker || (!trimmedStdout && stderrIsMarker)) {
25+
if (!trimmedStdout && stderrIsMarker) {
2726
return [];
2827
}
2928
if (!trimmedStdout) {
@@ -39,6 +38,9 @@ export function parseQmdQueryJson(stdout: string, stderr: string): QmdQueryResul
3938
}
4039
const noisyPayload = extractFirstJsonArray(trimmedStdout);
4140
if (!noisyPayload) {
41+
if (isQmdNoResultsOutput(trimmedStdout)) {
42+
return [];
43+
}
4244
throw new Error("qmd query JSON response was not an array");
4345
}
4446
const fallback = parseQmdQueryResultArray(noisyPayload);
@@ -127,26 +129,43 @@ function parseQmdLineNumber(value: unknown): number | undefined {
127129
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
128130
}
129131

130-
/** Extract the first complete JSON array from noisy stdout. */
132+
/** Extract the first complete, standalone JSON result array from noisy stdout. */
131133
function extractFirstJsonArray(raw: string): string | null {
132-
const start = raw.indexOf("[");
133-
if (start < 0) {
134-
return null;
135-
}
134+
let start = -1;
136135
let depth = 0;
137136
let inString = false;
138137
let escaped = false;
139-
for (let i = start; i < raw.length; i += 1) {
138+
let atLineStart = true;
139+
140+
for (let i = 0; i < raw.length; i += 1) {
140141
const char = raw[i];
141142
if (char === undefined) {
142143
break;
143144
}
145+
146+
if (start < 0) {
147+
if (char === "\n") {
148+
atLineStart = true;
149+
continue;
150+
}
151+
if (atLineStart && (char === " " || char === "\t" || char === "\r")) {
152+
continue;
153+
}
154+
// QMD emits result arrays on their own line; log fields can contain arrays too.
155+
if (!atLineStart || char !== "[") {
156+
atLineStart = false;
157+
continue;
158+
}
159+
start = i;
160+
depth = 1;
161+
atLineStart = false;
162+
continue;
163+
}
164+
144165
if (inString) {
145166
if (escaped) {
146167
escaped = false;
147-
continue;
148-
}
149-
if (char === "\\") {
168+
} else if (char === "\\") {
150169
escaped = true;
151170
} else if (char === '"') {
152171
inString = false;
@@ -159,12 +178,17 @@ function extractFirstJsonArray(raw: string): string | null {
159178
}
160179
if (char === "[") {
161180
depth += 1;
162-
} else if (char === "]") {
163-
depth -= 1;
164-
if (depth === 0) {
165-
return raw.slice(start, i + 1);
166-
}
181+
continue;
182+
}
183+
if (char !== "]" || --depth !== 0) {
184+
continue;
185+
}
186+
187+
const candidate = raw.slice(start, i + 1);
188+
if (parseQmdQueryResultArray(candidate) !== null) {
189+
return candidate;
167190
}
191+
start = -1;
168192
}
169193
return null;
170194
}

0 commit comments

Comments
 (0)