|
1 | 1 | // 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"; |
5 | 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
6 | 3 | import { |
7 | 4 | onInternalDiagnosticEvent, |
8 | 5 | onDiagnosticEvent, |
9 | 6 | resetDiagnosticEventsForTest, |
10 | 7 | type DiagnosticEventPayload, |
11 | 8 | } from "../infra/diagnostic-events.js"; |
| 9 | +import { createOpenClawTestState } from "../test-utils/openclaw-test-state.js"; |
12 | 10 | import { emitDiagnosticMemorySample, resetDiagnosticMemoryForTest } from "./diagnostic-memory.js"; |
13 | 11 | import { |
14 | 12 | readLatestDiagnosticStabilityBundleSync, |
@@ -397,101 +395,24 @@ describe("diagnostic memory", () => { |
397 | 395 | ).toBe(1); |
398 | 396 | }); |
399 | 397 |
|
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" }); |
448 | 400 | try { |
449 | 401 | startDiagnosticStabilityRecorder(); |
450 | | - |
451 | 402 | emitDiagnosticMemorySample({ |
452 | 403 | now: Date.parse("2026-04-22T12:00:00.000Z"), |
453 | | - stateDir, |
454 | | - writeCriticalBundle: false, |
455 | | - resolveSessionStorePaths, |
456 | 404 | memoryUsage: memoryUsage({ rss: 4000, heapUsed: 3000 }), |
457 | 405 | thresholds: { |
458 | 406 | rssWarningBytes: 1000, |
459 | 407 | rssCriticalBytes: 3000, |
460 | 408 | pressureRepeatMs: 60_000, |
461 | 409 | }, |
462 | 410 | }); |
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 | + ); |
493 | 414 | } finally { |
494 | | - fs.rmSync(stateDir, { recursive: true, force: true }); |
| 415 | + await state.cleanup(); |
495 | 416 | } |
496 | 417 | }); |
497 | 418 |
|
@@ -580,69 +501,4 @@ describe("diagnostic memory", () => { |
580 | 501 | "nextStep=run openclaw gateway status --deep and openclaw gateway diagnostics export; restart gateway if pressure persists", |
581 | 502 | ); |
582 | 503 | }); |
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 | | - }); |
648 | 504 | }); |
0 commit comments