From f2745aa03ac9620dc488dc717d160ba0c78cb6b4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 25 Apr 2026 06:19:50 +0100 Subject: [PATCH] refactor(cron): clarify ambient session context rollover --- src/cron/isolated-agent/session.test.ts | 2 +- src/cron/isolated-agent/session.ts | 19 +++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/cron/isolated-agent/session.test.ts b/src/cron/isolated-agent/session.test.ts index fcd8b379520..8bfc20636c9 100644 --- a/src/cron/isolated-agent/session.test.ts +++ b/src/cron/isolated-agent/session.test.ts @@ -400,7 +400,7 @@ describe("resolveCronSession", () => { expect(result.sessionEntry.authProfileOverrideCompactionCount).toBe(3); }); - it("preserves session context for stale non-isolated rollovers", () => { + it("preserves ambient session context for non-isolated expiration rollovers", () => { const result = resolveWithStoredEntry({ entry: { sessionId: "existing-session-id-321", diff --git a/src/cron/isolated-agent/session.ts b/src/cron/isolated-agent/session.ts index 8042309d9a5..7772a3791a3 100644 --- a/src/cron/isolated-agent/session.ts +++ b/src/cron/isolated-agent/session.ts @@ -9,9 +9,7 @@ import { loadSessionStore } from "../../config/sessions/store-load.js"; import type { SessionEntry } from "../../config/sessions/types.js"; import type { OpenClawConfig } from "../../config/types.openclaw.js"; -type FreshCronSessionSanitizeMode = "isolated-force-new" | "stale-rollover"; - -const FRESH_CRON_SAFE_PREFERENCE_FIELDS = [ +const FRESH_CRON_CARRIED_PREFERENCE_FIELDS = [ "heartbeatTaskState", "chatType", "thinkingLevel", @@ -25,7 +23,7 @@ const FRESH_CRON_SAFE_PREFERENCE_FIELDS = [ "displayName", ] as const satisfies readonly (keyof SessionEntry)[]; -const STALE_SESSION_CONTEXT_PRESERVED_FIELDS = [ +const AMBIENT_SESSION_CONTEXT_FIELDS = [ "elevatedLevel", "groupActivation", "groupActivationNeedsSystemIntro", @@ -87,13 +85,13 @@ function preserveUserAuthOverride(target: SessionEntry, entry: SessionEntry): vo function sanitizeFreshCronSessionEntry( entry: SessionEntry, - mode: FreshCronSessionSanitizeMode, + options: { preserveAmbientContext: boolean }, ): SessionEntry { const next = {} as SessionEntry; - copySessionFields(next, entry, FRESH_CRON_SAFE_PREFERENCE_FIELDS); - if (mode === "stale-rollover") { - copySessionFields(next, entry, STALE_SESSION_CONTEXT_PRESERVED_FIELDS); + copySessionFields(next, entry, FRESH_CRON_CARRIED_PREFERENCE_FIELDS); + if (options.preserveAmbientContext) { + copySessionFields(next, entry, AMBIENT_SESSION_CONTEXT_FIELDS); } preserveNonAutoModelOverride(next, entry); preserveUserAuthOverride(next, entry); @@ -159,10 +157,7 @@ export function resolveCronSession(params: { const baseEntry = entry ? isNewSession - ? sanitizeFreshCronSessionEntry( - entry, - params.forceNew ? "isolated-force-new" : "stale-rollover", - ) + ? sanitizeFreshCronSessionEntry(entry, { preserveAmbientContext: !params.forceNew }) : entry : undefined;