Skip to content

Commit 5f33245

Browse files
authored
refactor(diagnostics): remove retired pressure-bundle writer (#130754)
1 parent ab25508 commit 5f33245

5 files changed

Lines changed: 46 additions & 520 deletions

File tree

‎src/logging/diagnostic-memory.test.ts‎

Lines changed: 7 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Diagnostic memory tests cover memory snapshot capture and diagnostic log output.
2-
import fs from "node:fs";
3-
import os from "node:os";
4-
import path from "node:path";
52
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
63
import {
74
onInternalDiagnosticEvent,
85
onDiagnosticEvent,
96
resetDiagnosticEventsForTest,
107
type DiagnosticEventPayload,
118
} from "../infra/diagnostic-events.js";
9+
import { createOpenClawTestState } from "../test-utils/openclaw-test-state.js";
1210
import { emitDiagnosticMemorySample, resetDiagnosticMemoryForTest } from "./diagnostic-memory.js";
1311
import {
1412
readLatestDiagnosticStabilityBundleSync,
@@ -397,101 +395,24 @@ describe("diagnostic memory", () => {
397395
).toBe(1);
398396
});
399397

400-
it("resolves session store paths only for enabled critical bundle writes", () => {
401-
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-memory-pressure-lazy-"));
402-
const resolveSessionStorePaths = vi.fn(() => []);
403-
try {
404-
emitDiagnosticMemorySample({
405-
now: 1000,
406-
stateDir,
407-
resolveSessionStorePaths,
408-
memoryUsage: memoryUsage({ rss: 500 }),
409-
thresholds: {
410-
rssWarningBytes: 1000,
411-
rssCriticalBytes: 3000,
412-
},
413-
});
414-
emitDiagnosticMemorySample({
415-
now: 2000,
416-
stateDir,
417-
resolveSessionStorePaths,
418-
memoryUsage: memoryUsage({ rss: 2000 }),
419-
thresholds: {
420-
rssWarningBytes: 1000,
421-
rssCriticalBytes: 3000,
422-
},
423-
});
424-
425-
expect(resolveSessionStorePaths).not.toHaveBeenCalled();
426-
427-
emitDiagnosticMemorySample({
428-
now: 3000,
429-
stateDir,
430-
writeCriticalBundle: true,
431-
resolveSessionStorePaths,
432-
memoryUsage: memoryUsage({ rss: 4000 }),
433-
thresholds: {
434-
rssWarningBytes: 1000,
435-
rssCriticalBytes: 3000,
436-
},
437-
});
438-
439-
expect(resolveSessionStorePaths).toHaveBeenCalledTimes(1);
440-
} finally {
441-
fs.rmSync(stateDir, { recursive: true, force: true });
442-
}
443-
});
444-
445-
it("can disable critical pressure bundle writes", () => {
446-
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-memory-pressure-disabled-"));
447-
const resolveSessionStorePaths = vi.fn(() => []);
398+
it("does not write bundles when critical pressure is emitted", async () => {
399+
const state = await createOpenClawTestState({ label: "memory-pressure" });
448400
try {
449401
startDiagnosticStabilityRecorder();
450-
451402
emitDiagnosticMemorySample({
452403
now: Date.parse("2026-04-22T12:00:00.000Z"),
453-
stateDir,
454-
writeCriticalBundle: false,
455-
resolveSessionStorePaths,
456404
memoryUsage: memoryUsage({ rss: 4000, heapUsed: 3000 }),
457405
thresholds: {
458406
rssWarningBytes: 1000,
459407
rssCriticalBytes: 3000,
460408
pressureRepeatMs: 60_000,
461409
},
462410
});
463-
464-
expect(resolveSessionStorePaths).not.toHaveBeenCalled();
465-
expect(readLatestDiagnosticStabilityBundleSync({ stateDir }).status).toBe("missing");
466-
} finally {
467-
fs.rmSync(stateDir, { recursive: true, force: true });
468-
}
469-
});
470-
471-
it("leaves critical pressure bundle writes off by default", () => {
472-
const stateDir = fs.mkdtempSync(
473-
path.join(os.tmpdir(), "openclaw-memory-pressure-default-off-"),
474-
);
475-
const resolveSessionStorePaths = vi.fn(() => []);
476-
try {
477-
startDiagnosticStabilityRecorder();
478-
479-
emitDiagnosticMemorySample({
480-
now: Date.parse("2026-04-22T12:00:00.000Z"),
481-
stateDir,
482-
resolveSessionStorePaths,
483-
memoryUsage: memoryUsage({ rss: 4000, heapUsed: 3000 }),
484-
thresholds: {
485-
rssWarningBytes: 1000,
486-
rssCriticalBytes: 3000,
487-
pressureRepeatMs: 60_000,
488-
},
489-
});
490-
491-
expect(resolveSessionStorePaths).not.toHaveBeenCalled();
492-
expect(readLatestDiagnosticStabilityBundleSync({ stateDir }).status).toBe("missing");
411+
expect(readLatestDiagnosticStabilityBundleSync({ stateDir: state.stateDir }).status).toBe(
412+
"missing",
413+
);
493414
} finally {
494-
fs.rmSync(stateDir, { recursive: true, force: true });
415+
await state.cleanup();
495416
}
496417
});
497418

@@ -580,69 +501,4 @@ describe("diagnostic memory", () => {
580501
"nextStep=run openclaw gateway status --deep and openclaw gateway diagnostics export; restart gateway if pressure persists",
581502
);
582503
});
583-
584-
it("writes a stability bundle when critical pressure is emitted", () => {
585-
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-memory-pressure-"));
586-
const customRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-memory-custom-sessions-"));
587-
try {
588-
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
589-
const customSessionsDir = path.join(customRoot, "custom-sessions");
590-
fs.mkdirSync(sessionsDir, { recursive: true });
591-
fs.mkdirSync(customSessionsDir, { recursive: true });
592-
fs.writeFileSync(path.join(sessionsDir, "small.jsonl"), "small\n", "utf8");
593-
fs.writeFileSync(path.join(sessionsDir, "large.jsonl"), "x".repeat(4096), "utf8");
594-
fs.writeFileSync(path.join(customSessionsDir, "sessions.json"), "{}\n", "utf8");
595-
fs.writeFileSync(
596-
path.join(customSessionsDir, "custom-secret-session.jsonl"),
597-
"x".repeat(8192),
598-
"utf8",
599-
);
600-
startDiagnosticStabilityRecorder();
601-
602-
emitDiagnosticMemorySample({
603-
now: Date.parse("2026-04-22T12:00:00.000Z"),
604-
uptimeMs: 0,
605-
stateDir,
606-
writeCriticalBundle: true,
607-
sessionStorePaths: [path.join(customSessionsDir, "sessions.json")],
608-
memoryUsage: memoryUsage({ rss: 4000, heapUsed: 3000 }),
609-
thresholds: {
610-
rssWarningBytes: 1000,
611-
rssCriticalBytes: 3000,
612-
pressureRepeatMs: 60_000,
613-
},
614-
});
615-
616-
const latest = readLatestDiagnosticStabilityBundleSync({ stateDir });
617-
expect(latest.status).toBe("found");
618-
if (latest.status !== "found") {
619-
return;
620-
}
621-
expect(latest.bundle.reason).toBe("diagnostic.memory.pressure.critical");
622-
expect(latest.bundle.snapshot.summary.byType["diagnostic.memory.pressure"]).toBe(1);
623-
expect(latest.bundle.evidence?.memoryPressure).toMatchObject({
624-
level: "critical",
625-
reason: "rss_threshold",
626-
thresholdBytes: 3000,
627-
memory: expect.objectContaining({
628-
rssBytes: 4000,
629-
heapUsedBytes: 3000,
630-
}),
631-
});
632-
expect(latest.bundle.evidence?.memoryPressure?.heapStatistics?.heapSizeLimitBytes).toEqual(
633-
expect.any(Number),
634-
);
635-
expect(latest.bundle.evidence?.memoryPressure?.activeResources?.total).toEqual(
636-
expect.any(Number),
637-
);
638-
expect(latest.bundle.evidence?.memoryPressure?.topSessionFiles?.[0]).toMatchObject({
639-
relativePath: "sessions/<session>.jsonl",
640-
sizeBytes: 8192,
641-
});
642-
expect(JSON.stringify(latest.bundle)).not.toContain("custom-secret-session");
643-
} finally {
644-
fs.rmSync(stateDir, { recursive: true, force: true });
645-
fs.rmSync(customRoot, { recursive: true, force: true });
646-
}
647-
});
648504
});

‎src/logging/diagnostic-memory.ts‎

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type DiagnosticMemoryPressureEvent,
77
type DiagnosticMemoryUsage,
88
} from "../infra/diagnostic-events.js";
9-
import { writeDiagnosticMemoryPressureBundleSync } from "./diagnostic-stability-bundle.js";
109
import { createSubsystemLogger } from "./subsystem.js";
1110

1211
// Diagnostic memory sampler with threshold/growth pressure detection and repeat suppression.
@@ -312,11 +311,9 @@ function formatPressureNextStep(
312311
: "nextStep=run openclaw gateway status --deep and openclaw gateway diagnostics export; restart gateway if pressure persists";
313312
}
314313

315-
function logMemoryPressure(params: {
316-
pressure: Omit<DiagnosticMemoryPressureEvent, "seq" | "ts" | "type">;
317-
writeCriticalBundle: boolean;
318-
}): void {
319-
const { pressure } = params;
314+
function logMemoryPressure(
315+
pressure: Omit<DiagnosticMemoryPressureEvent, "seq" | "ts" | "type">,
316+
): void {
320317
const message =
321318
`memory pressure: level=${pressure.level} reason=${pressure.reason}` +
322319
` ${formatPressureSummary(pressure)}` +
@@ -325,9 +322,7 @@ function logMemoryPressure(params: {
325322
formatOptionalPressureMetric("thresholdBytes", pressure.thresholdBytes) +
326323
formatOptionalPressureMetric("rssGrowthBytes", pressure.rssGrowthBytes) +
327324
formatOptionalPressureMetric("windowMs", pressure.windowMs) +
328-
(pressure.level === "critical"
329-
? ` memoryPressureSnapshot=${params.writeCriticalBundle ? "enabled" : "disabled"}`
330-
: "") +
325+
(pressure.level === "critical" ? " memoryPressureSnapshot=disabled" : "") +
331326
` ${formatPressureNextStep(pressure)}`;
332327
log.warn(message);
333328
}
@@ -342,10 +337,6 @@ export function emitDiagnosticMemorySample(options?: {
342337
uptimeMs?: number;
343338
thresholds?: DiagnosticMemoryThresholds;
344339
emitSample?: boolean;
345-
writeCriticalBundle?: boolean;
346-
stateDir?: string;
347-
sessionStorePaths?: string[];
348-
resolveSessionStorePaths?: () => string[] | undefined;
349340
}): DiagnosticMemoryUsage {
350341
const now = options?.now ?? Date.now();
351342
const memory = normalizeMemoryUsage(options?.memoryUsage ?? process.memoryUsage());
@@ -376,25 +367,8 @@ export function emitDiagnosticMemorySample(options?: {
376367
type: "diagnostic.memory.pressure",
377368
...pressure,
378369
});
379-
const writeCriticalBundle = options?.writeCriticalBundle === true;
380-
logMemoryPressure({ pressure, writeCriticalBundle });
381-
if (pressure.level === "critical" && writeCriticalBundle) {
382-
// Critical snapshots are opt-in because bundle writes can add IO during memory pressure.
383-
const sessionStorePaths = options?.sessionStorePaths ?? options?.resolveSessionStorePaths?.();
384-
const result = writeDiagnosticMemoryPressureBundleSync({
385-
pressure,
386-
stateDir: options?.stateDir,
387-
sessionStorePaths,
388-
now: new Date(now),
389-
});
390-
if (result.status === "written") {
391-
log.warn(
392-
`critical memory pressure bundle written: path=${result.path} reason=${pressure.reason} level=${pressure.level}`,
393-
);
394-
} else if (result.status === "failed") {
395-
log.warn(`critical memory pressure bundle failed: ${String(result.error)}`);
396-
}
397-
} else if (pressure.level === "critical") {
370+
logMemoryPressure(pressure);
371+
if (pressure.level === "critical") {
398372
log.warn("critical memory pressure snapshot disabled");
399373
}
400374
}

‎src/logging/diagnostic-stability-bundle.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,30 @@ describe("diagnostic stability bundles", () => {
247247
it("sanitizes imported bundles before returning them", () => {
248248
const file = path.join(tempDir, "imported.json");
249249
const bundle = createImportedBundle();
250+
const retainedRuntimeEvidence = {
251+
heapStatistics: { heapSizeLimitBytes: 8192, usedHeapSizeBytes: 1536 },
252+
heapSpaces: [
253+
{
254+
spaceName: "old_space",
255+
spaceSizeBytes: 2048,
256+
spaceUsedBytes: 1536,
257+
spaceAvailableBytes: 512,
258+
physicalSpaceSizeBytes: 2048,
259+
},
260+
],
261+
cgroup: {
262+
version: "v2",
263+
values: { current: 4096, max: "max" },
264+
events: { high: 2, "events.local.oom": 1 },
265+
},
266+
activeResources: { total: 3, byType: { Timeout: 2, PipeWrap: 1 } },
267+
};
250268
Object.assign(bundle, {
251269
reason: "private reason token=secret",
252270
privateTopLevel: "top-level-secret",
253271
evidence: {
254272
memoryPressure: {
273+
...retainedRuntimeEvidence,
255274
level: "critical",
256275
reason: "rss_threshold",
257276
memory: {
@@ -331,6 +350,7 @@ describe("diagnostic stability bundles", () => {
331350
expect(result.bundle.evidence?.memoryPressure?.topSessionFiles?.[0]?.relativePath).toBe(
332351
"agents/<agent>/sessions/<session>.jsonl",
333352
);
353+
expect(result.bundle.evidence?.memoryPressure).toMatchObject(retainedRuntimeEvidence);
334354
expect(result.bundle.snapshot.events[0]).toEqual({
335355
seq: 1,
336356
ts: 1,

0 commit comments

Comments
 (0)