mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 09:57:40 +00:00
fix(gateway): keep boot sessions ephemeral without remapping main
This commit is contained in:
@@ -8,14 +8,23 @@ const agentCommand = vi.fn();
|
|||||||
vi.mock("../commands/agent.js", () => ({ agentCommand }));
|
vi.mock("../commands/agent.js", () => ({ agentCommand }));
|
||||||
|
|
||||||
const { runBootOnce } = await import("./boot.js");
|
const { runBootOnce } = await import("./boot.js");
|
||||||
const { resolveMainSessionKey } = await import("../config/sessions/main-session.js");
|
const { resolveAgentIdFromSessionKey, resolveMainSessionKey } =
|
||||||
const { saveSessionStore } = await import("../config/sessions/store.js");
|
await import("../config/sessions/main-session.js");
|
||||||
const { resolveStorePath } = await import("../config/sessions/paths.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", () => {
|
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();
|
vi.clearAllMocks();
|
||||||
|
const { storePath } = resolveMainStore();
|
||||||
|
await fs.rm(storePath, { force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
const makeDeps = () => ({
|
const makeDeps = () => ({
|
||||||
@@ -93,17 +102,14 @@ describe("runBootOnce", () => {
|
|||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
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 workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-boot-"));
|
||||||
const content = "Say hello when you wake up.";
|
const content = "Say hello when you wake up.";
|
||||||
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), content, "utf-8");
|
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), content, "utf-8");
|
||||||
|
|
||||||
// Create a session store with an existing session
|
|
||||||
const cfg = {};
|
const cfg = {};
|
||||||
const sessionKey = resolveMainSessionKey(cfg);
|
const { sessionKey, storePath } = resolveMainStore(cfg);
|
||||||
const agentId = resolveAgentIdFromSessionKey(sessionKey);
|
const existingSessionId = "main-session-abc123";
|
||||||
const storePath = resolveStorePath(undefined, { agentId });
|
|
||||||
const existingSessionId = "existing-session-abc123";
|
|
||||||
|
|
||||||
await saveSessionStore(storePath, {
|
await saveSessionStore(storePath, {
|
||||||
[sessionKey]: {
|
[sessionKey]: {
|
||||||
@@ -120,24 +126,21 @@ describe("runBootOnce", () => {
|
|||||||
expect(agentCommand).toHaveBeenCalledTimes(1);
|
expect(agentCommand).toHaveBeenCalledTimes(1);
|
||||||
const call = agentCommand.mock.calls[0]?.[0];
|
const call = agentCommand.mock.calls[0]?.[0];
|
||||||
|
|
||||||
// Verify the existing session ID was reused
|
expect(call?.sessionId).not.toBe(existingSessionId);
|
||||||
expect(call?.sessionId).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);
|
expect(call?.sessionKey).toBe(sessionKey);
|
||||||
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
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 workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-boot-"));
|
||||||
const content = "Check if the system is healthy.";
|
const content = "Check if the system is healthy.";
|
||||||
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), content, "utf-8");
|
await fs.writeFile(path.join(workspaceDir, "BOOT.md"), content, "utf-8");
|
||||||
|
|
||||||
// Create a session store with an existing session
|
|
||||||
const cfg = {};
|
const cfg = {};
|
||||||
const sessionKey = resolveMainSessionKey(cfg);
|
const { sessionKey, storePath } = resolveMainStore(cfg);
|
||||||
const agentId = resolveAgentIdFromSessionKey(sessionKey);
|
const existingSessionId = "main-session-xyz789";
|
||||||
const storePath = resolveStorePath(undefined, { agentId });
|
|
||||||
const existingSessionId = "test-session-xyz789";
|
|
||||||
|
|
||||||
await saveSessionStore(storePath, {
|
await saveSessionStore(storePath, {
|
||||||
[sessionKey]: {
|
[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({
|
await expect(runBootOnce({ cfg, deps: makeDeps(), workspaceDir })).resolves.toEqual({
|
||||||
status: "ran",
|
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
|
await fs.rm(workspaceDir, { recursive: true, force: true });
|
||||||
expect(call?.sessionId).toBe(existingSessionId);
|
});
|
||||||
expect(call?.sessionKey).toBe(sessionKey);
|
|
||||||
|
|
||||||
// The agent command should append to the existing session's JSONL file
|
it("removes a boot-created main-session mapping when none existed before", async () => {
|
||||||
// (actual file append is handled by agentCommand, we just verify the IDs match)
|
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 });
|
await fs.rm(workspaceDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ import fs from "node:fs/promises";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { CliDeps } from "../cli/deps.js";
|
import type { CliDeps } from "../cli/deps.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import type { SessionEntry } from "../config/sessions/types.js";
|
||||||
import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
|
import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
|
||||||
import { agentCommand } from "../commands/agent.js";
|
import { agentCommand } from "../commands/agent.js";
|
||||||
import { resolveMainSessionKey } from "../config/sessions/main-session.js";
|
import {
|
||||||
import { resolveAgentIdFromSessionKey } from "../config/sessions/main-session.js";
|
resolveAgentIdFromSessionKey,
|
||||||
|
resolveMainSessionKey,
|
||||||
|
} from "../config/sessions/main-session.js";
|
||||||
import { resolveStorePath } from "../config/sessions/paths.js";
|
import { resolveStorePath } from "../config/sessions/paths.js";
|
||||||
import { loadSessionStore } from "../config/sessions/store.js";
|
import { loadSessionStore, updateSessionStore } from "../config/sessions/store.js";
|
||||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||||
import { type RuntimeEnv, defaultRuntime } from "../runtime.js";
|
import { type RuntimeEnv, defaultRuntime } from "../runtime.js";
|
||||||
|
|
||||||
@@ -19,38 +22,13 @@ function generateBootSessionId(): string {
|
|||||||
return `boot-${ts}-${suffix}`;
|
return `boot-${ts}-${suffix}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
type SessionMappingSnapshot = {
|
||||||
* Resolve the session ID for the boot message.
|
storePath: string;
|
||||||
* If there's an existing session mapped to the main session key, reuse it to avoid orphaning.
|
sessionKey: string;
|
||||||
* Otherwise, generate a new ephemeral boot session ID.
|
canRestore: boolean;
|
||||||
*/
|
hadEntry: boolean;
|
||||||
function resolveBootSessionId(cfg: OpenClawConfig): string {
|
entry?: SessionEntry;
|
||||||
const sessionKey = resolveMainSessionKey(cfg);
|
};
|
||||||
const agentId = resolveAgentIdFromSessionKey(sessionKey);
|
|
||||||
const storePath = resolveStorePath(cfg.session?.store, { agentId });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const sessionStore = loadSessionStore(storePath);
|
|
||||||
const existingEntry = sessionStore[sessionKey];
|
|
||||||
|
|
||||||
if (existingEntry?.sessionId) {
|
|
||||||
log.info("reusing existing session for boot message", {
|
|
||||||
sessionKey,
|
|
||||||
sessionId: existingEntry.sessionId,
|
|
||||||
});
|
|
||||||
return existingEntry.sessionId;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
// If we can't load the session store (e.g., first boot), fall through to generate new ID
|
|
||||||
log.debug("could not load session store for boot; generating new session ID", {
|
|
||||||
error: String(err),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const newSessionId = generateBootSessionId();
|
|
||||||
log.info("generating new boot session", { sessionKey, sessionId: newSessionId });
|
|
||||||
return newSessionId;
|
|
||||||
}
|
|
||||||
|
|
||||||
const log = createSubsystemLogger("gateway/boot");
|
const log = createSubsystemLogger("gateway/boot");
|
||||||
const BOOT_FILENAME = "BOOT.md";
|
const BOOT_FILENAME = "BOOT.md";
|
||||||
@@ -94,6 +72,68 @@ async function loadBootFile(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function snapshotMainSessionMapping(params: {
|
||||||
|
cfg: OpenClawConfig;
|
||||||
|
sessionKey: string;
|
||||||
|
}): SessionMappingSnapshot {
|
||||||
|
const agentId = resolveAgentIdFromSessionKey(params.sessionKey);
|
||||||
|
const storePath = resolveStorePath(params.cfg.session?.store, { agentId });
|
||||||
|
try {
|
||||||
|
const store = loadSessionStore(storePath, { skipCache: true });
|
||||||
|
const entry = store[params.sessionKey];
|
||||||
|
if (!entry) {
|
||||||
|
return {
|
||||||
|
storePath,
|
||||||
|
sessionKey: params.sessionKey,
|
||||||
|
canRestore: true,
|
||||||
|
hadEntry: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
storePath,
|
||||||
|
sessionKey: params.sessionKey,
|
||||||
|
canRestore: true,
|
||||||
|
hadEntry: true,
|
||||||
|
entry: structuredClone(entry),
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
log.debug("boot: could not snapshot main session mapping", {
|
||||||
|
sessionKey: params.sessionKey,
|
||||||
|
error: String(err),
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
storePath,
|
||||||
|
sessionKey: params.sessionKey,
|
||||||
|
canRestore: false,
|
||||||
|
hadEntry: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function restoreMainSessionMapping(
|
||||||
|
snapshot: SessionMappingSnapshot,
|
||||||
|
): Promise<string | undefined> {
|
||||||
|
if (!snapshot.canRestore) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await updateSessionStore(
|
||||||
|
snapshot.storePath,
|
||||||
|
(store) => {
|
||||||
|
if (snapshot.hadEntry && snapshot.entry) {
|
||||||
|
store[snapshot.sessionKey] = snapshot.entry;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delete store[snapshot.sessionKey];
|
||||||
|
},
|
||||||
|
{ activeSessionKey: snapshot.sessionKey },
|
||||||
|
);
|
||||||
|
return undefined;
|
||||||
|
} catch (err) {
|
||||||
|
return err instanceof Error ? err.message : String(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function runBootOnce(params: {
|
export async function runBootOnce(params: {
|
||||||
cfg: OpenClawConfig;
|
cfg: OpenClawConfig;
|
||||||
deps: CliDeps;
|
deps: CliDeps;
|
||||||
@@ -119,8 +159,13 @@ export async function runBootOnce(params: {
|
|||||||
|
|
||||||
const sessionKey = resolveMainSessionKey(params.cfg);
|
const sessionKey = resolveMainSessionKey(params.cfg);
|
||||||
const message = buildBootPrompt(result.content ?? "");
|
const message = buildBootPrompt(result.content ?? "");
|
||||||
const sessionId = resolveBootSessionId(params.cfg);
|
const sessionId = generateBootSessionId();
|
||||||
|
const mappingSnapshot = snapshotMainSessionMapping({
|
||||||
|
cfg: params.cfg,
|
||||||
|
sessionKey,
|
||||||
|
});
|
||||||
|
|
||||||
|
let agentFailure: string | undefined;
|
||||||
try {
|
try {
|
||||||
await agentCommand(
|
await agentCommand(
|
||||||
{
|
{
|
||||||
@@ -132,10 +177,22 @@ export async function runBootOnce(params: {
|
|||||||
bootRuntime,
|
bootRuntime,
|
||||||
params.deps,
|
params.deps,
|
||||||
);
|
);
|
||||||
return { status: "ran" };
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const messageText = err instanceof Error ? err.message : String(err);
|
agentFailure = err instanceof Error ? err.message : String(err);
|
||||||
log.error(`boot: agent run failed: ${messageText}`);
|
log.error(`boot: agent run failed: ${agentFailure}`);
|
||||||
return { status: "failed", reason: messageText };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mappingRestoreFailure = await restoreMainSessionMapping(mappingSnapshot);
|
||||||
|
if (mappingRestoreFailure) {
|
||||||
|
log.error(`boot: failed to restore main session mapping: ${mappingRestoreFailure}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!agentFailure && !mappingRestoreFailure) {
|
||||||
|
return { status: "ran" };
|
||||||
|
}
|
||||||
|
const reasonParts = [
|
||||||
|
agentFailure ? `agent run failed: ${agentFailure}` : undefined,
|
||||||
|
mappingRestoreFailure ? `mapping restore failed: ${mappingRestoreFailure}` : undefined,
|
||||||
|
].filter((part): part is string => Boolean(part));
|
||||||
|
return { status: "failed", reason: reasonParts.join("; ") };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user