Skip to content
Python

fix(memhub): only call non-JSON diagnostic lines damaged in `memhub logs`

alvinfei53507c7abef3
diff --git a/memhub/apps/cli/src/cli-logs.test.ts b/memhub/apps/cli/src/cli-logs.test.ts
index b8714f2..1ec4569 100644
--- a/memhub/apps/cli/src/cli-logs.test.ts
+++ b/memhub/apps/cli/src/cli-logs.test.ts
@@ -162,10 +162,54 @@ it("never emits a secret from a malformed manually written event", async () => {
command: "logs",
state: "empty",
events: 0,
- invalid_lines: 1,
+ invalid_lines: 0,
+ unrecognized_lines: 1,
});
});
+// After an upgrade the files still hold an older build's records. They are
+// whole JSON lines the reader leaves out, not damage, so the text output must
+// not warn about them; only a line that is not JSON is called damaged.
+it("warns only about lines that are not JSON, not about older records", async () => {
+ const dataDir = await fixture();
+ const older = {
+ schema_version: 0,
+ service: "desktop",
+ component: "desktop.lifecycle",
+ event: "desktop.started",
+ };
+ await writeFile(
+ path.join(dataDir, "logs", "diagnostic-desktop.jsonl"),
+ `${JSON.stringify(older)}\n${JSON.stringify({ ...older, schema_version: 1 })}\n`,
+ { mode: 0o600 },
+ );
+ let stdout = "";
+ let stderr = "";
+ vi.spyOn(process.stdout, "write").mockImplementation((value) => {
+ stdout += String(value);
+ return true;
+ });
+ vi.spyOn(process.stderr, "write").mockImplementation((value) => {
+ stderr += String(value);
+ return true;
+ });
+
+ expect(await run(["logs"], { ONE_MEMORY_DATA_DIR: dataDir })).toBe(0);
+ expect(stdout).toContain("core.start_failed");
+ expect(stdout).not.toContain("损坏");
+ expect(stderr).toBe("");
+
+ await writeFile(
+ path.join(dataDir, "logs", "diagnostic-cli.jsonl"),
+ '{"schema_version": 1, "event": \n',
+ { mode: 0o600 },
+ );
+ stdout = "";
+ expect(await run(["logs"], { ONE_MEMORY_DATA_DIR: dataDir })).toBe(0);
+ expect(stdout).toContain("跳过 1 行损坏的诊断记录。");
+ expect(stderr).toBe("memhub: 跳过 1 行损坏的诊断记录。\n");
+});
+
it("rejects invalid filters without touching the log directory", async () => {
const dataDir = path.join(
await mkdtemp(path.join(os.tmpdir(), "om-cli-no-log-")),
diff --git a/memhub/apps/cli/src/cli.ts b/memhub/apps/cli/src/cli.ts
index 094531c..39db94c 100644
--- a/memhub/apps/cli/src/cli.ts
+++ b/memhub/apps/cli/src/cli.ts
@@ -957,9 +957,12 @@ async function runCommand(
state: "empty",
events: 0,
invalid_lines: report.invalid_lines,
+ unrecognized_lines: report.unrecognized_lines,
})}\n`
: renderLogText(report.events, report.invalid_lines),
);
+ // Only lines that are not JSON are damage. Records an older build wrote
+ // are left out without a warning: after an upgrade they are expected.
if (report.invalid_lines) {
process.stderr.write(
`${PRODUCT}: 跳过 ${report.invalid_lines} 行损坏的诊断记录。\n`,
diff --git a/memhub/apps/cli/src/log-report.ts b/memhub/apps/cli/src/log-report.ts
index d2d73a8..95a0052 100644
--- a/memhub/apps/cli/src/log-report.ts
+++ b/memhub/apps/cli/src/log-report.ts
@@ -107,7 +107,11 @@ export async function readLogReport(
query: LogQuery,
): Promise<{
events: DiagnosticEventV1[];
+ /** Lines that are not JSON: the only ones `memhub logs` calls damaged. */
invalid_lines: number;
+ /** Whole JSON lines the reader leaves out, such as an older build's
+ * records. Nothing is wrong with them, so the text output says nothing. */
+ unrecognized_lines: number;
failure?: "log_directory_unreadable";
}> {
// `readDiagnosticEvents` treats an unreadable logs path as "no events". That
@@ -119,6 +123,7 @@ export async function readLogReport(
return {
events: [],
invalid_lines: 0,
+ unrecognized_lines: 0,
failure: "log_directory_unreadable",
};
}
@@ -129,6 +134,7 @@ export async function readLogReport(
return {
events: [],
invalid_lines: 0,
+ unrecognized_lines: 0,
failure: "log_directory_unreadable",
};
}
diff --git a/memhub/packages/runtime-control/src/diagnostic-log.test.ts b/memhub/packages/runtime-control/src/diagnostic-log.test.ts
index 2373847..a226711 100644
--- a/memhub/packages/runtime-control/src/diagnostic-log.test.ts
+++ b/memhub/packages/runtime-control/src/diagnostic-log.test.ts
@@ -90,6 +90,7 @@ describe("diagnostic log", () => {
expect(await readDiagnosticEvents(dataDir)).toEqual({
events: [],
invalid_lines: 0,
+ unrecognized_lines: 0,
});
});
@@ -496,6 +497,52 @@ describe("diagnostic log", () => {
expect(await readdir(outputDir)).toEqual([]);
});
+ it("counts only non-JSON lines as damaged and leaves older records out", async () => {
+ const dataDir = await root();
+ const logger = new DiagnosticLogger({ dataDir, service: "core" });
+ await logger.log({
+ level: "info",
+ component: "core.service",
+ event: "core.ready",
+ outcome: "succeeded",
+ });
+ const older = {
+ schema_version: 0,
+ service: "desktop",
+ component: "desktop.lifecycle",
+ event: "desktop.started",
+ };
+ await writeFile(
+ path.join(dataDir, "logs", "diagnostic-desktop.jsonl"),
+ [
+ JSON.stringify(older),
+ JSON.stringify({ ...older, schema_version: 1 }),
+ JSON.stringify(["not", "an", "event"]),
+ "",
+ ].join("\n"),
+ { mode: 0o600 },
+ );
+
+ const result = await readDiagnosticEvents(dataDir);
+ expect(result.events.map((event) => event.event)).toEqual(["core.ready"]);
+ expect(result.invalid_lines).toBe(0);
+ expect(result.unrecognized_lines).toBe(3);
+ // An older build's records do not stop a support export either.
+ const target = path.join(await root(), "bundle.json");
+ await expect(
+ exportDiagnosticBundle({ dataDir, target, productVersion: "1.2.3" }),
+ ).resolves.toEqual({ event_count: 1, invalid_lines: 0 });
+
+ await writeFile(
+ path.join(dataDir, "logs", "diagnostic-cli.jsonl"),
+ '{"schema_version": 1, "event": \n',
+ { mode: 0o600 },
+ );
+ const damaged = await readDiagnosticEvents(dataDir);
+ expect(damaged.invalid_lines).toBe(1);
+ expect(damaged.unrecognized_lines).toBe(3);
+ });
+
it("fails closed when the diagnostic directory cannot be read", async () => {
if (process.platform === "win32") return;
const dataDir = await root();
diff --git a/memhub/packages/runtime-control/src/diagnostic-log.ts b/memhub/packages/runtime-control/src/diagnostic-log.ts
index 4c3d90a..e6299c5 100644
--- a/memhub/packages/runtime-control/src/diagnostic-log.ts
+++ b/memhub/packages/runtime-control/src/diagnostic-log.ts
@@ -429,10 +429,18 @@ export async function readDiagnosticEvents(
errorsOnly?: boolean;
tail?: number;
} = {},
-): Promise<{ events: DiagnosticEventV1[]; invalid_lines: number }> {
+): Promise<{
+ events: DiagnosticEventV1[];
+ invalid_lines: number;
+ unrecognized_lines: number;
+}> {
const files = await diagnosticLogFiles(dataDir);
let events: DiagnosticEventV1[] = [];
+ // Only a line that is not JSON at all is damage. A whole JSON line this
+ // reader does not accept (an older build's record, another schema) is left
+ // out and counted apart, so an upgrade does not read as a broken log.
let invalidLines = 0;
+ let unrecognizedLines = 0;
for (const file of files) {
const content = await readRegularFileNoFollow(
file,
@@ -440,11 +448,17 @@ export async function readDiagnosticEvents(
);
for (const line of content.split("\n")) {
if (!line) continue;
+ let value: unknown;
try {
- const item = parseDiagnosticEvent(JSON.parse(line));
- events.push(item);
+ value = JSON.parse(line);
} catch {
invalidLines += 1;
+ continue;
+ }
+ try {
+ events.push(parseDiagnosticEvent(value));
+ } catch {
+ unrecognizedLines += 1;
}
}
}
@@ -480,7 +494,11 @@ export async function readDiagnosticEvents(
return !(filter.errorsOnly && item.level !== "error");
});
const tail = Math.min(Math.max(filter.tail ?? 200, 1), 5_000);
- return { events: events.slice(-tail), invalid_lines: invalidLines };
+ return {
+ events: events.slice(-tail),
+ invalid_lines: invalidLines,
+ unrecognized_lines: unrecognizedLines,
+ };
}
export async function exportDiagnosticBundle(options: {