Skip to content

Commit 60823d1

Browse files
committed
test(gateway): prove session registry recovery
1 parent df5178c commit 60823d1

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { afterEach, expect, it, vi } from "vitest";
4+
import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js";
5+
import * as sessionDirs from "../../agents/session-dirs.js";
6+
import { EMPTY_LEGACY_SESSION_SURFACES } from "../../plugins/legacy-session-surfaces.types.js";
7+
import { unregisterOpenClawAgentDatabase } from "../../state/openclaw-agent-db-registry.js";
8+
import {
9+
closeOpenClawAgentDatabasesForTest,
10+
listOpenClawRegisteredAgentDatabases,
11+
} from "../../state/openclaw-agent-db.js";
12+
import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js";
13+
import { withEnvAsync } from "../../test-utils/env.js";
14+
import type { OpenClawConfig } from "../types.openclaw.js";
15+
import { loadCombinedSessionStoreForGatewayCore } from "./combined-store-gateway.js";
16+
import { replaceSessionEntry } from "./session-accessor.js";
17+
import { resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target.js";
18+
import { runSessionStartupMigration } from "./startup-migration.js";
19+
20+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
21+
22+
afterEach(() => {
23+
closeOpenClawAgentDatabasesForTest();
24+
closeOpenClawStateDatabaseForTest();
25+
});
26+
27+
it("re-registers durable lineage children before configured-only runtime reads", async () => {
28+
const root = fs.realpathSync.native(tempDirs.make("openclaw-startup-registry-recovery-"));
29+
const stateDir = path.join(root, "state");
30+
await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => {
31+
const env = { ...process.env };
32+
const storeTemplate = path.join(stateDir, "agents", "{agentId}", "sessions", "sessions.json");
33+
const cfg: OpenClawConfig = {
34+
agents: { entries: { ops: { default: true } } },
35+
session: { store: storeTemplate },
36+
};
37+
const mainKey = "agent:ops:main";
38+
const childKey = "agent:codex:subagent:upgrade-child";
39+
const storePathFor = (agentId: string) => storeTemplate.replace("{agentId}", agentId);
40+
41+
await replaceSessionEntry(
42+
{ agentId: "ops", env, sessionKey: mainKey, storePath: storePathFor("ops") },
43+
{ sessionId: "session-ops", updatedAt: 20 },
44+
);
45+
await replaceSessionEntry(
46+
{ agentId: "codex", env, sessionKey: childKey, storePath: storePathFor("codex") },
47+
{ sessionId: "session-codex", spawnedBy: mainKey, updatedAt: 30 },
48+
);
49+
await replaceSessionEntry(
50+
{
51+
agentId: "local",
52+
env,
53+
sessionKey: "agent:local:main",
54+
storePath: storePathFor("local"),
55+
},
56+
{ sessionId: "session-local", updatedAt: 10 },
57+
);
58+
59+
const childDatabasePath = resolveSqliteTargetFromSessionStorePath(storePathFor("codex"), {
60+
agentId: "codex",
61+
env,
62+
}).path;
63+
closeOpenClawAgentDatabasesForTest();
64+
unregisterOpenClawAgentDatabase({ agentId: "codex", env, path: childDatabasePath });
65+
66+
expect(fs.existsSync(childDatabasePath)).toBe(true);
67+
expect(
68+
listOpenClawRegisteredAgentDatabases({ env }).some(
69+
(entry) => entry.agentId === "codex" && entry.path === childDatabasePath,
70+
),
71+
).toBe(false);
72+
73+
await runSessionStartupMigration({
74+
cfg,
75+
env,
76+
log: { info: vi.fn(), warn: vi.fn() },
77+
deps: {
78+
migrateLegacyMainSessionKeys: vi.fn(async () => ({
79+
armed: false,
80+
changes: [],
81+
complete: false,
82+
ledgerComplete: false,
83+
legacyAgentId: "main",
84+
mainKey: "main",
85+
outcomes: [{ kind: "not-armed" as const }],
86+
warnings: [],
87+
})),
88+
migrateOrphanedSessionKeys: vi.fn(async () => ({ changes: [], warnings: [] })),
89+
prepareLegacySessionSurfaces: () => EMPTY_LEGACY_SESSION_SURFACES,
90+
sweepOrphanSessionStoreTemps: vi.fn(async () => 0),
91+
},
92+
});
93+
94+
expect(listOpenClawRegisteredAgentDatabases({ env })).toContainEqual(
95+
expect.objectContaining({ agentId: "codex", path: childDatabasePath }),
96+
);
97+
98+
const enumerateAgentDirs = vi.spyOn(sessionDirs, "resolveAgentSessionDirsFromAgentsDirSync");
99+
try {
100+
const store = loadCombinedSessionStoreForGatewayCore(cfg, {
101+
configuredAgentsOnly: true,
102+
}).store;
103+
expect(store[mainKey]?.sessionId).toBe("session-ops");
104+
expect(store[childKey]?.sessionId).toBe("session-codex");
105+
expect(store["agent:local:main"]).toBeUndefined();
106+
expect(enumerateAgentDirs).not.toHaveBeenCalled();
107+
} finally {
108+
enumerateAgentDirs.mockRestore();
109+
}
110+
});
111+
});

0 commit comments

Comments
 (0)