fix(gateway): keep boot sessions ephemeral without remapping main

This commit is contained in:
Shakker
2026-02-15 23:59:41 +00:00
parent fe73878dfc
commit b562aa6625
2 changed files with 152 additions and 65 deletions

View File

@@ -8,14 +8,23 @@ const agentCommand = vi.fn();
vi.mock("../commands/agent.js", () => ({ agentCommand }));
const { runBootOnce } = await import("./boot.js");
const { resolveMainSessionKey } = await import("../config/sessions/main-session.js");
const { saveSessionStore } = await import("../config/sessions/store.js");
const { resolveAgentIdFromSessionKey, resolveMainSessionKey } =
await import("../config/sessions/main-session.js");
const { resolveStorePath } = await import("../config/sessions/paths.js");
const { resolveAgentIdFromSessionKey } = await import("../config/sessions/main-session.js");
const { loadSessionStore, saveSessionStore } = await import("../config/sessions/store.js");
describe("runBootOnce", () => {
beforeEach(() => {
const resolveMainStore = (cfg: { session?: { store?: string } } = {}) => {
const sessionKey = resolveMainSessionKey(cfg);
const agentId = resolveAgentIdFromSessionKey(sessionKey);
const storePath = resolveStorePath(cfg.session?.store, { agentId });
return { sessionKey, storePath };
};
beforeEach(async () => {
vi.clearAllMocks();
const { storePath } = resolveMainStore();
await fs.rm(storePath, { force: true });
});
const makeDeps = () => ({
@@ -93,17 +102,14 @@ describe("runBootOnce", () => {
await fs.rm(workspaceDir, { recursive: true, force: true });
});
it("reuses existing session ID when session mapping exists", async () => {
it("uses a fresh boot session ID even when main session mapping already exists", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-boot-"));
const content = "Say hello when you wake up.";
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), content, "utf-8");
// Create a session store with an existing session
const cfg = {};
const sessionKey = resolveMainSessionKey(cfg);
const agentId = resolveAgentIdFromSessionKey(sessionKey);
const storePath = resolveStorePath(undefined, { agentId });
const existingSessionId = "existing-session-abc123";
const { sessionKey, storePath } = resolveMainStore(cfg);
const existingSessionId = "main-session-abc123";
await saveSessionStore(storePath, {
[sessionKey]: {
@@ -120,24 +126,21 @@ describe("runBootOnce", () => {
expect(agentCommand).toHaveBeenCalledTimes(1);
const call = agentCommand.mock.calls[0]?.[0];
// Verify the existing session ID was reused
expect(call?.sessionId).toBe(existingSessionId);
expect(call?.sessionId).not.toBe(existingSessionId);
expect(call?.sessionId).toMatch(/^boot-\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-\d{3}-[0-9a-f]{8}$/);
expect(call?.sessionKey).toBe(sessionKey);
await fs.rm(workspaceDir, { recursive: true, force: true });
});
it("appends boot message to existing session transcript", async () => {
it("restores the original main session mapping after the boot run", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-boot-"));
const content = "Check if the system is healthy.";
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), content, "utf-8");
// Create a session store with an existing session
const cfg = {};
const sessionKey = resolveMainSessionKey(cfg);
const agentId = resolveAgentIdFromSessionKey(sessionKey);
const storePath = resolveStorePath(undefined, { agentId });
const existingSessionId = "test-session-xyz789";
const { sessionKey, storePath } = resolveMainStore(cfg);
const existingSessionId = "main-session-xyz789";
await saveSessionStore(storePath, {
[sessionKey]: {
@@ -146,19 +149,46 @@ describe("runBootOnce", () => {
},
});
agentCommand.mockResolvedValue(undefined);
agentCommand.mockImplementation(async (opts: { sessionId?: string }) => {
const current = loadSessionStore(storePath, { skipCache: true });
current[sessionKey] = {
sessionId: String(opts.sessionId),
updatedAt: Date.now(),
};
await saveSessionStore(storePath, current);
});
await expect(runBootOnce({ cfg, deps: makeDeps(), workspaceDir })).resolves.toEqual({
status: "ran",
});
const call = agentCommand.mock.calls[0]?.[0];
const restored = loadSessionStore(storePath, { skipCache: true });
expect(restored[sessionKey]?.sessionId).toBe(existingSessionId);
// Verify boot message uses the existing session
expect(call?.sessionId).toBe(existingSessionId);
expect(call?.sessionKey).toBe(sessionKey);
await fs.rm(workspaceDir, { recursive: true, force: true });
});
// The agent command should append to the existing session's JSONL file
// (actual file append is handled by agentCommand, we just verify the IDs match)
it("removes a boot-created main-session mapping when none existed before", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-boot-"));
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), "health check", "utf-8");
const cfg = {};
const { sessionKey, storePath } = resolveMainStore(cfg);
agentCommand.mockImplementation(async (opts: { sessionId?: string }) => {
const current = loadSessionStore(storePath, { skipCache: true });
current[sessionKey] = {
sessionId: String(opts.sessionId),
updatedAt: Date.now(),
};
await saveSessionStore(storePath, current);
});
await expect(runBootOnce({ cfg, deps: makeDeps(), workspaceDir })).resolves.toEqual({
status: "ran",
});
const restored = loadSessionStore(storePath, { skipCache: true });
expect(restored[sessionKey]).toBeUndefined();
await fs.rm(workspaceDir, { recursive: true, force: true });
});