fix(wake): handle relative + agent-prefixed session keys consistently in cron adapter

Address review findings from successive codex rounds:

1. next-heartbeat + sessionKey now fires a targeted immediate wake.
   The regularly-scheduled heartbeat fires for the agent's main session,
   not the supplied sessionKey, so an event queued for a non-main session
   would sit stranded indefinitely; an "event"-intent wake is also
   deferred as not-due by the heartbeat runner and not retried, so
   neither path delivers without an explicit immediate wake.

2. resolveCronWakeTarget now always runs through resolveCronAgent, both
   for agent-prefixed session keys (so non-default agents are honored)
   and relative keys (so the configured default agent is used instead
   of the hardcoded "main" returned by resolveAgentIdFromSessionKey).
   Mirrors the matching fix in the enqueueSystemEvent adapter so wake
   and enqueue resolve to the same target.

3. Generated Swift `WakeParams` models now expose the new optional
   `sessionkey` field (codingKey "sessionKey") in both the macOS and
   shared OpenClawKit copies. Locally regenerated from agent.ts via
   protocol:gen + protocol:gen:swift would have produced this; the
   environment couldn't run the generators (fs-safe transitive
   typecheck errors), so the diff was applied by hand to match what
   pnpm protocol:check would output.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kaspre
2026-05-07 00:23:39 -04:00
committed by Peter Steinberger
parent 4ddd942f5f
commit 072fa9b174
4 changed files with 67 additions and 13 deletions
+19
View File
@@ -1796,6 +1796,25 @@ export function wake(
reason: "wake",
...(sessionKey ? { sessionKey } : {}),
});
} else if (sessionKey) {
// next-heartbeat + sessionKey still needs a targeted immediate wake.
// Reasons:
// 1. The regularly-scheduled heartbeat fires for the agent's main
// session, not the supplied sessionKey, so it never peeks the queue
// we just enqueued — the event would sit stranded indefinitely.
// 2. An `intent: "event"` wake gets deferred by heartbeat-runner as
// not-due and is not retried (only busy-skips are), so it cannot
// stand in for the regular cadence either.
// Effectively, --session-key collapses --mode now and --mode next-heartbeat
// into the same targeted-immediate behavior — this matches the documented
// user intent (target a specific session for relay) better than silently
// dropping the event.
state.deps.requestHeartbeat({
source: "manual",
intent: "immediate",
reason: "wake",
sessionKey,
});
}
return { ok: true } as const;
}
+17 -1
View File
@@ -55,7 +55,11 @@ describe("wake (cron timer)", () => {
});
});
it("threads sessionKey to enqueue only on mode=next-heartbeat", () => {
it("threads sessionKey to enqueue and fires a targeted immediate wake on mode=next-heartbeat", () => {
// next-heartbeat + sessionKey collapses to immediate-targeted behavior:
// the regularly-scheduled heartbeat fires for agent-main and never peeks
// a non-main session queue, and an "event"-intent wake is not retried by
// the heartbeat runner. Targeted immediate is the only reliable path.
const { state, enqueueSystemEvent, requestHeartbeat } = createState();
expect(
wake(state, {
@@ -67,6 +71,18 @@ describe("wake (cron timer)", () => {
expect(enqueueSystemEvent).toHaveBeenCalledWith("ping", {
sessionKey: "agent:main:slack:42",
});
expect(requestHeartbeat).toHaveBeenCalledWith({
source: "manual",
intent: "immediate",
reason: "wake",
sessionKey: "agent:main:slack:42",
});
});
it("does not fire a wake on mode=next-heartbeat when no sessionKey is supplied", () => {
const { state, enqueueSystemEvent, requestHeartbeat } = createState();
expect(wake(state, { mode: "next-heartbeat", text: "ping" })).toEqual({ ok: true });
expect(enqueueSystemEvent).toHaveBeenCalledWith("ping", undefined);
expect(requestHeartbeat).not.toHaveBeenCalled();
});