Skip to content

Commit 33ef6bb

Browse files
Gigi Agentclaude
andcommitted
Fix: use stricter isCronRunSessionKey in timer and sweep all agent stores
- Replace overbroad isCronRunKey (/:run:/) in timer.ts with the stricter isCronRunSessionKey from sweeper.ts (/:cron:[^:]+:run:[^:]+$/) - Update sweepStaleCronRunSessions to iterate over all configured agent session stores via listAgentIds(), not just the default agent Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 086679c commit 33ef6bb

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/cron/service/timer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ import {
1010
} from "./jobs.js";
1111
import { locked } from "./locked.js";
1212
import { ensureLoaded, persist } from "./store.js";
13+
import { isCronRunSessionKey } from "./sweeper.js";
1314

1415
const MAX_TIMER_DELAY_MS = 60_000;
1516

16-
/** True if the session key is a per-run cron key (contains `:run:`). */
17-
function isCronRunKey(key: string): boolean {
18-
return /:run:/.test(key);
19-
}
20-
2117
/**
2218
* Maximum wall-clock time for a single job execution. Acts as a safety net
2319
* on top of the per-provider / per-agent timeouts to prevent one stuck job
@@ -276,7 +272,7 @@ export async function onTimer(state: CronServiceState) {
276272
// Clean up the :run: session entry from the session store.
277273
if (
278274
result.sessionKey &&
279-
isCronRunKey(result.sessionKey) &&
275+
isCronRunSessionKey(result.sessionKey) &&
280276
(job.cleanup ?? "delete") === "delete" &&
281277
state.deps.cleanupCronRunSession
282278
) {
@@ -519,7 +515,7 @@ export async function executeJob(
519515
// Clean up the :run: session entry from the session store.
520516
if (
521517
coreResult.sessionKey &&
522-
isCronRunKey(coreResult.sessionKey) &&
518+
isCronRunSessionKey(coreResult.sessionKey) &&
523519
(job.cleanup ?? "delete") === "delete" &&
524520
state.deps.cleanupCronRunSession
525521
) {

src/gateway/server-cron.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CliDeps } from "../cli/deps.js";
22
import type { CronJob } from "../cron/types.js";
3-
import { resolveDefaultAgentId } from "../agents/agent-scope.js";
3+
import { listAgentIds, resolveDefaultAgentId } from "../agents/agent-scope.js";
44
import { loadConfig } from "../config/config.js";
55
import {
66
resolveAgentMainSessionKey,
@@ -145,31 +145,33 @@ async function sweepStaleCronRunSessions(params: {
145145
log: { info: (obj: unknown, msg?: string) => void; warn: (obj: unknown, msg?: string) => void };
146146
}): Promise<number> {
147147
const { cfg, log } = params;
148-
const agentId = resolveDefaultAgentId(cfg);
149-
const sessStorePath = resolveStorePath(cfg.session?.store, { agentId });
148+
const agentIds = listAgentIds(cfg);
150149
const now = Date.now();
151150
let removed = 0;
152151

153-
try {
154-
await updateSessionStore(sessStorePath, (store) => {
155-
for (const key of Object.keys(store)) {
156-
if (!isCronRunSessionKey(key)) {
157-
continue;
158-
}
159-
const entry = store[key];
160-
const updatedAt = entry?.updatedAt ?? 0;
161-
if (now - updatedAt > STALE_THRESHOLD_MS) {
162-
delete store[key];
163-
removed++;
152+
for (const agentId of agentIds) {
153+
const sessStorePath = resolveStorePath(cfg.session?.store, { agentId });
154+
try {
155+
await updateSessionStore(sessStorePath, (store) => {
156+
for (const key of Object.keys(store)) {
157+
if (!isCronRunSessionKey(key)) {
158+
continue;
159+
}
160+
const entry = store[key];
161+
const updatedAt = entry?.updatedAt ?? 0;
162+
if (now - updatedAt > STALE_THRESHOLD_MS) {
163+
delete store[key];
164+
removed++;
165+
}
164166
}
165-
}
166-
});
167-
} catch (err) {
168-
log.warn({ err: String(err) }, "cron: sweeper failed to update session store");
167+
});
168+
} catch (err) {
169+
log.warn({ err: String(err), agentId }, "cron: sweeper failed to update session store");
170+
}
169171
}
170172

171173
if (removed > 0) {
172-
log.info({ removed }, "cron: swept stale :run: session entries");
174+
log.info({ removed, agentIds }, "cron: swept stale :run: session entries");
173175
}
174176
return removed;
175177
}

0 commit comments

Comments
 (0)