Skip to content

fix(cron): use agent-turn safety timeout for systemEvent jobs with wakeMode=now - #50723

Closed
Hollychou924 wants to merge 2 commits into
openclaw:mainfrom
Hollychou924:fix/cron-systemevent-timeout-50621
Closed

Hollychou924 wants to merge 2 commits into
openclaw:mainfrom
Hollychou924:fix/cron-systemevent-timeout-50621

Conversation

@Hollychou924

Copy link
Copy Markdown
Contributor

Problem

Closes #50621

When a cron job has sessionTarget: "main", payload.kind: "systemEvent", and wakeMode: "now", executeJobCore() calls runHeartbeatOnce() which blocks until the full agent turn completes. Multi-step tasks (file I/O, web search, git push, etc.) can take 30+ minutes.

However, resolveCronJobTimeoutMs() was returning DEFAULT_JOB_TIMEOUT_MS (10 minutes) for all systemEvent jobs, regardless of wakeMode. This caused legitimate long-running main-session heartbeats to be killed with error: "cron: job execution timed out".

Root cause

// Before: both wakeMode paths got DEFAULT_JOB_TIMEOUT_MS (10 min)
systemEvent → DEFAULT_JOB_TIMEOUT_MS  // 10 min ❌
agentTurn   → AGENT_TURN_SAFETY_TIMEOUT_MS  // 60 min ✓

The two wakeMode paths have fundamentally different blocking behaviour:

wakeMode behaviour appropriate timeout
next-heartbeat fire-and-forget via requestHeartbeatNow(), returns immediately DEFAULT_JOB_TIMEOUT_MS (10 min) is fine
now blocks in runHeartbeatOnce() until turn finishes needs AGENT_TURN_SAFETY_TIMEOUT_MS (60 min)

Fix

In resolveCronJobTimeoutMs(), detect the systemEvent + sessionTarget=main + wakeMode=now combination and return AGENT_TURN_SAFETY_TIMEOUT_MS (60 min) instead of the short default.

Jobs with wakeMode !== "now" are unaffected — they remain fire-and-forget and keep the 10-minute ceiling.

Testing

  • Added a new unit test for the wakeMode=now case in timeout-policy.test.ts
  • All existing tests continue to pass (5 tests passed)

HollyChou added 2 commits March 20, 2026 08:46
…keMode=now

When a cron job has sessionTarget="main", payload.kind="systemEvent", and
wakeMode="now", executeJobCore() calls runHeartbeatOnce() which blocks until
the full agent turn completes. The turn duration is unbounded (multi-step
tasks may take 30+ minutes), but resolveCronJobTimeoutMs() was returning
DEFAULT_JOB_TIMEOUT_MS (10 minutes) for all systemEvent jobs, causing
spurious "cron: job execution timed out" errors.

Fix: when wakeMode="now" + sessionTarget="main" + kind="systemEvent",
return AGENT_TURN_SAFETY_TIMEOUT_MS (60 minutes) instead of the short 10-minute default.

Fixes openclaw#50621
@greptile-apps

greptile-apps Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a legitimate bug where systemEvent cron jobs targeting the main session with wakeMode="now" were being killed with a timeout error after 10 minutes, even though those jobs block synchronously on runHeartbeatOnce() until the full agent turn completes (which can take 30+ minutes).

The root cause was that resolveCronJobTimeoutMs() returned DEFAULT_JOB_TIMEOUT_MS (10 min) for all systemEvent jobs regardless of wakeMode, while only the agentTurn kind received the longer AGENT_TURN_SAFETY_TIMEOUT_MS (60 min). The fix correctly identifies the blocking execution path — confirmed by reading executeJobCore() in timer.ts, which shows the runHeartbeatOnce() call is only made when job.wakeMode === "now" — and applies the same generous ceiling to that specific combination.

  • The fix is logically correct and well-targeted.
  • The new test exercises both wakeMode values for systemEvent jobs, closing the existing coverage gap.
  • One minor observation: the job.sessionTarget === "main" guard inside the new condition is always true for a valid systemEvent job (enforced by assertSupportedJobSpec), making it redundant. It is harmless and arguably improves readability, but could be removed for brevity.

Confidence Score: 5/5

  • This PR is safe to merge — it fixes a real timeout bug with a small, well-scoped change and adds a targeted test.
  • The change is minimal (one new conditional branch in a policy function) and addresses a confirmed root cause. Reading executeJobCore in timer.ts validates that the wakeMode="now" path is the only systemEvent code path that blocks synchronously, confirming the fix applies the correct timeout to exactly the right scenario. Existing tests continue to pass and a new test covers the previously-untested combination. The only note is a redundant guard (sessionTarget === "main") that is harmless.
  • No files require special attention.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/cron/service/timeout-policy.ts
Line: 31-37

Comment:
**Redundant `sessionTarget === "main"` guard**

`assertSupportedJobSpec()` in `src/cron/service/jobs.ts` (line 138) already enforces that `isolated` jobs must have `payload.kind === "agentTurn"`, which means any valid job with `payload.kind === "systemEvent"` is *always* `sessionTarget === "main"`. The `job.sessionTarget === "main"` check here is therefore always `true` for a well-formed `systemEvent` job and doesn't add filtering power at runtime.

This is harmless (and adds some defensive clarity), but the condition could be simplified without any behavioral change:

```suggestion
    if (job.payload.kind === "systemEvent" && job.wakeMode === "now") {
      return AGENT_TURN_SAFETY_TIMEOUT_MS;
    }
```

Leaving it as-is is also acceptable if you prefer the explicit self-documentation.

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: "test(cron): add cove..."

Comment on lines +31 to +37
if (
job.payload.kind === "systemEvent" &&
job.sessionTarget === "main" &&
job.wakeMode === "now"
) {
return AGENT_TURN_SAFETY_TIMEOUT_MS;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Redundant sessionTarget === "main" guard

assertSupportedJobSpec() in src/cron/service/jobs.ts (line 138) already enforces that isolated jobs must have payload.kind === "agentTurn", which means any valid job with payload.kind === "systemEvent" is always sessionTarget === "main". The job.sessionTarget === "main" check here is therefore always true for a well-formed systemEvent job and doesn't add filtering power at runtime.

This is harmless (and adds some defensive clarity), but the condition could be simplified without any behavioral change:

Suggested change
if (
job.payload.kind === "systemEvent" &&
job.sessionTarget === "main" &&
job.wakeMode === "now"
) {
return AGENT_TURN_SAFETY_TIMEOUT_MS;
}
if (job.payload.kind === "systemEvent" && job.wakeMode === "now") {
return AGENT_TURN_SAFETY_TIMEOUT_MS;
}

Leaving it as-is is also acceptable if you prefer the explicit self-documentation.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cron/service/timeout-policy.ts
Line: 31-37

Comment:
**Redundant `sessionTarget === "main"` guard**

`assertSupportedJobSpec()` in `src/cron/service/jobs.ts` (line 138) already enforces that `isolated` jobs must have `payload.kind === "agentTurn"`, which means any valid job with `payload.kind === "systemEvent"` is *always* `sessionTarget === "main"`. The `job.sessionTarget === "main"` check here is therefore always `true` for a well-formed `systemEvent` job and doesn't add filtering power at runtime.

This is harmless (and adds some defensive clarity), but the condition could be simplified without any behavioral change:

```suggestion
    if (job.payload.kind === "systemEvent" && job.wakeMode === "now") {
      return AGENT_TURN_SAFETY_TIMEOUT_MS;
    }
```

Leaving it as-is is also acceptable if you prefer the explicit self-documentation.

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@Hollychou924

This comment was marked as spam.

@vincentkoc

Copy link
Copy Markdown
Member

Closing this as stale. The wakeMode/systemEvent timeout behavior was folded into the closed isolated cron timeout family (#40868), and this PR is red.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cron systemEvent job times out after ~960s even though agent runs in main session

2 participants