fix(gateway): preserve session mapping across gateway restarts

This commit is contained in:
Shakker
2026-02-15 23:57:49 +00:00
parent ee2fa5f411
commit fe73878dfc
2 changed files with 130 additions and 1 deletions

View File

@@ -6,6 +6,9 @@ import type { OpenClawConfig } from "../config/config.js";
import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
import { agentCommand } from "../commands/agent.js";
import { resolveMainSessionKey } from "../config/sessions/main-session.js";
import { resolveAgentIdFromSessionKey } from "../config/sessions/main-session.js";
import { resolveStorePath } from "../config/sessions/paths.js";
import { loadSessionStore } from "../config/sessions/store.js";
import { createSubsystemLogger } from "../logging/subsystem.js";
import { type RuntimeEnv, defaultRuntime } from "../runtime.js";
@@ -16,6 +19,39 @@ function generateBootSessionId(): string {
return `boot-${ts}-${suffix}`;
}
/**
* Resolve the session ID for the boot message.
* If there's an existing session mapped to the main session key, reuse it to avoid orphaning.
* Otherwise, generate a new ephemeral boot session ID.
*/
function resolveBootSessionId(cfg: OpenClawConfig): string {
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 BOOT_FILENAME = "BOOT.md";
@@ -83,7 +119,7 @@ export async function runBootOnce(params: {
const sessionKey = resolveMainSessionKey(params.cfg);
const message = buildBootPrompt(result.content ?? "");
const sessionId = generateBootSessionId();
const sessionId = resolveBootSessionId(params.cfg);
try {
await agentCommand(