openclaw - ✅(Solved) Fix [Bug]: Cron runtime crashes with TypeError when job.sessionTarget is missing [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#63383Fetched 2026-04-09 07:54:31
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Participants
Timeline (top)
cross-referenced ×1referenced ×1

Cron jobs can fail with an unhandled TypeError when sessionTarget is missing from a persisted job spec.

Observed error: TypeError: Cannot read properties of undefined (reading 'startsWith')

Error Message

Observed error: the runtime calls .startsWith("session:") on job.sessionTarget in multiple code paths, which crashes execution with a raw TypeError instead of returning a structured validation error or auto-defaulting. Runtime throws unhandled TypeError and marks the run as error/skipped.

  1. Validation path: return explicit invalid-spec error (for example: "missing sessionTarget") without raw JS TypeError.
  • status=error, error=TypeError: Cannot read properties of undefined (reading 'startsWith'), durationMs=1
  • subsequent runs with same error and durationMs=0 A single malformed or legacy cron job spec can keep failing indefinitely and is hard to diagnose from user perspective because the error is a low-level JS exception.

Root Cause

A single malformed or legacy cron job spec can keep failing indefinitely and is hard to diagnose from user perspective because the error is a low-level JS exception.

Fix Action

Fixed

PR fix notes

PR #63402: fix(cron): guard against undefined sessionTarget in startsWith calls

Description (problem / solution / changelog)

Bug

Cron jobs crash with TypeError: Cannot read properties of undefined (reading 'startsWith') when payload.kind='agentTurn' but sessionTarget is missing.

Root Cause

Two call sites in the cron runtime assume job.sessionTarget is always defined:

  • src/cron/delivery-plan.tsjob.sessionTarget.startsWith("session:")
  • src/gateway/server-cron.tsjob.sessionTarget.startsWith("session:")

Fix

Add optional chaining + nullish coalescing: job.sessionTarget?.startsWith("session:") ?? false

Fixes #63383

Changed files

  • src/cron/delivery-plan.ts (modified, +1/-1)
  • src/gateway/server-cron.ts (modified, +1/-1)
RAW_BUFFERClick to expand / collapse

Summary

Cron jobs can fail with an unhandled TypeError when sessionTarget is missing from a persisted job spec.

Observed error: TypeError: Cannot read properties of undefined (reading 'startsWith')

Environment

  • OpenClaw: 2026.4.8 (9ece252)
  • Surface: cron runtime (cron.run / scheduled cron execution)
  • Repo: openclaw/openclaw

Problem

If a cron job has:

  • payload.kind: "agentTurn"
  • missing sessionTarget

the runtime calls .startsWith("session:") on job.sessionTarget in multiple code paths, which crashes execution with a raw TypeError instead of returning a structured validation error or auto-defaulting.

Reproduction

  1. Create (or load) a cron job where payload.kind is agentTurn and sessionTarget is absent.
  2. Trigger the job (cron.run) or wait for schedule.
  3. Observe run failure with:
    • TypeError: Cannot read properties of undefined (reading 'startsWith')
    • very short run duration (0-1ms) because it fails before payload execution.

Actual result

Runtime throws unhandled TypeError and marks the run as error/skipped.

Expected result

One of these should happen instead:

  1. Validation path: return explicit invalid-spec error (for example: "missing sessionTarget") without raw JS TypeError.
  2. Compatibility path: infer defaults for missing field:
    • agentTurn -> sessionTarget: "isolated"
    • systemEvent -> sessionTarget: "main"

Evidence

Failing run records

  • status=error, error=TypeError: Cannot read properties of undefined (reading 'startsWith'), durationMs=1
  • subsequent runs with same error and durationMs=0

Relevant runtime call sites (2026.4.8 build)

dist/server.impl-*.js:

  • job.sessionTarget.startsWith("session:") in assertSupportedJobSpec(...)
  • job.sessionTarget.startsWith("session:") in delivery/session execution paths

This assumes job.sessionTarget is always defined.

Why this matters

A single malformed or legacy cron job spec can keep failing indefinitely and is hard to diagnose from user perspective because the error is a low-level JS exception.

Suggested fix

  1. Add normalization on cron job load/migration:
    • if sessionTarget missing and payload.kind===agentTurn => set isolated
    • if sessionTarget missing and payload.kind===systemEvent => set main
  2. Add null-safe guards before .startsWith(...) checks.
  3. Return structured, actionable errors (invalid-spec: missing sessionTarget) instead of raw TypeError.
  4. Add regression test for persisted jobs missing sessionTarget.

Notes

I confirmed locally that adding sessionTarget: "isolated" to the affected job immediately resolves the failure and allows normal cron execution.

Requested labels (no maintainer label permission on reporter account)

  • bug
  • bug:behavior
  • gateway

extent analysis

TL;DR

To fix the issue, add null-safe guards and normalization for missing sessionTarget fields in cron job specs, and return structured errors instead of raw TypeErrors.

Guidance

  • Add a check for sessionTarget existence before calling startsWith on it to prevent TypeErrors.
  • Implement job spec normalization to set default sessionTarget values based on payload.kind (e.g., "isolated" for "agentTurn" and "main" for "systemEvent").
  • Modify error handling to return explicit, actionable errors (e.g., "invalid-spec: missing sessionTarget") instead of raw TypeErrors.
  • Consider adding regression tests to ensure the fix works for persisted jobs missing sessionTarget.

Example

if (job.sessionTarget) {
  if (job.sessionTarget.startsWith("session:")) {
    // existing code
  }
} else {
  // handle missing sessionTarget, e.g., set default value or return error
  if (job.payload.kind === "agentTurn") {
    job.sessionTarget = "isolated";
  } else if (job.payload.kind === "systemEvent") {
    job.sessionTarget = "main";
  } else {
    throw new Error("invalid-spec: missing sessionTarget");
  }
}

Notes

The provided example code snippet assumes that the job object and its properties are already defined and accessible. The actual implementation may vary depending on the specific requirements and constraints of the OpenClaw project.

Recommendation

Apply the suggested fix by adding null-safe guards, normalization, and structured error handling to prevent cron job failures due to missing sessionTarget fields. This approach addresses the root cause of the issue and provides a more robust and user-friendly experience.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING