Fix cron sessionFile persistence

This commit is contained in:
Sami Rusani
2026-04-12 08:29:25 +02:00
committed by Vincent Koc
parent a1d484d877
commit ef126f377b
4 changed files with 33 additions and 5 deletions

View File

@@ -15,6 +15,9 @@ import {
runCronTurn,
withTempHome,
} from "./isolated-agent.turn-test-helpers.js";
import { setupRunCronIsolatedAgentTurnSuite } from "./isolated-agent/run.suite-helpers.js";
setupRunCronIsolatedAgentTurnSuite();
describe("runCronIsolatedAgentTurn session identity", () => {
beforeEach(() => {
@@ -101,6 +104,22 @@ describe("runCronIsolatedAgentTurn session identity", () => {
});
});
it("passes sessionFile to isolated cron runs", async () => {
await withTempHome(async (home) => {
await runCronTurn(home, {
jobPayload: DEFAULT_AGENT_TURN_PAYLOAD,
});
const call = vi.mocked(runEmbeddedPiAgent).mock.calls.at(-1)?.[0] as {
sessionFile?: string;
};
expect(call?.sessionFile).toContain(
path.join(home, ".openclaw", "agents", "main", "sessions"),
);
expect(call?.sessionFile?.endsWith(".jsonl")).toBe(true);
});
});
it("starts a fresh session id for each cron run", async () => {
await withTempHome(async (home) => {
const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" });

View File

@@ -60,7 +60,10 @@ export function expectEmbeddedProviderModel(expected: { provider: string; model:
export async function readSessionEntry(storePath: string, key: string) {
const raw = await fs.readFile(storePath, "utf-8");
const store = JSON.parse(raw) as Record<string, { sessionId?: string; label?: string }>;
const store = JSON.parse(raw) as Record<
string,
{ sessionId?: string; label?: string; sessionFile?: string }
>;
return store[key];
}

View File

@@ -67,10 +67,12 @@ export function createCronPromptExecutor(params: {
abortSignal?: AbortSignal;
abortReason: () => string;
}) {
const sessionFile = resolveSessionTranscriptPath(
params.cronSession.sessionEntry.sessionId,
params.agentId,
);
const sessionFile =
params.cronSession.sessionEntry.sessionFile?.trim() ||
resolveSessionTranscriptPath(params.cronSession.sessionEntry.sessionId, params.agentId);
if (!params.cronSession.sessionEntry.sessionFile?.trim()) {
params.cronSession.sessionEntry.sessionFile = sessionFile;
}
const cronFallbacksOverride = resolveCronFallbacksOverride({
cfg: params.cfg,
job: params.job,

View File

@@ -18,6 +18,7 @@ import {
} from "./helpers.js";
import { resolveCronModelSelection } from "./model-selection.js";
import { buildCronAgentDefaultsConfig } from "./run-config.js";
import { resolveSessionTranscriptPath } from "./run-execution.runtime.js";
import { executeCronRun, type CronExecutionResult } from "./run-executor.js";
import {
createPersistCronSessionEntry,
@@ -272,6 +273,9 @@ async function prepareCronRunContext(params: {
forceNew: input.job.sessionTarget === "isolated",
});
const runSessionId = cronSession.sessionEntry.sessionId;
if (!cronSession.sessionEntry.sessionFile?.trim()) {
cronSession.sessionEntry.sessionFile = resolveSessionTranscriptPath(runSessionId, agentId);
}
const runSessionKey = baseSessionKey.startsWith("cron:")
? `${agentSessionKey}:run:${runSessionId}`
: agentSessionKey;