Skip to content

Heartbeat: overlapping runs overwrite session and cause nested session keys #52015

Closed

Description

@wiwaxia-ai

Bug

When isolatedSession: true is configured for heartbeat, two issues occur:

1. No overlap protection — concurrent heartbeats overwrite each other

runHeartbeatOnce calls resolveCronSession with forceNew: true every time it fires. If a previous heartbeat is still running (e.g., LLM is mid-execution), the next scheduled heartbeat:

  • Creates a brand-new sessionId, overwriting the store entry
  • Starts a second concurrent execution against the same agent workspace
  • The previous run's session is silently orphaned

There is no guard preventing a second heartbeat from starting while the first is still in flight.

2. session + isolatedSession causes nested session keys

When both "session": "heartbeat" and "isolatedSession": true are configured:

  1. resolveHeartbeatSession reads "session": "heartbeat" → returns agent:<id>:heartbeat
  2. runHeartbeatOnce appends :heartbeat suffix → creates agent:<id>:heartbeat:heartbeat
  3. Next cycle repeats → agent:<id>:heartbeat:heartbeat:heartbeat
  4. Session keys grow unboundedly

Without "session", the key becomes agent:<id>:main:heartbeat — which works but is a sub-session of main, not a truly independent heartbeat session.

Expected behavior

  1. If a heartbeat run is still executing, the next scheduled trigger should be skipped (not start a parallel run)
  2. "session": "heartbeat" + "isolatedSession": true should produce agent:<id>:heartbeat — a single independent session key that gets a fresh sessionId each run, without suffix nesting

Reproduction

// openclaw.json
{
  "heartbeat": {
    "every": "15m",
    "session": "heartbeat",
    "isolatedSession": true,
    "target": "feishu",
    // ...
  }
}

After a few cycles, sessions.json shows:

agent:<id>:heartbeat:heartbeat
agent:<id>:heartbeat:heartbeat:heartbeat
...

Suggested fix

  1. Overlap guard: Track active heartbeat runs per agent in a module-level Map. Skip if the agent already has a run in progress.

  2. Session key nesting: When isolatedSession is true and an explicit heartbeat.session is configured, use the resolved session key directly (it's already a dedicated heartbeat key) instead of appending :heartbeat again.

const hasExplicitSession = Boolean(heartbeat?.session?.trim());
const isolatedKey = hasExplicitSession ? sessionKey : `${sessionKey}:heartbeat`;

Environment

  • openclaw 2026.3.14
  • macOS, gateway mode

Activity

  1. Hollychou924 commented on Mar 22, 2026

    @Hollychou924
  2. GMTekAI commented on Mar 22, 2026

    @GMTekAI

    I checked this against current main, and the specific root-cause theory in the issue/comments does not line up with the current runner.

    A few source-backed points:

    • Current heartbeat runs already gate on main-lane activity: runHeartbeatOnce(...) checks getQueueSize(CommandLane.Main) and returns requests-in-flight in src/infra/heartbeat-runner.ts:639-641.
    • The scheduler is explicitly written not to advance or reschedule incorrectly on that condition in src/infra/heartbeat-runner.ts:1200-1205, and there is regression coverage for both retry behavior and preserving cadence in src/infra/heartbeat-runner.scheduler.test.ts:137-210.
    • I also could not find the claimed unconditional :heartbeat suffix append path. resolveHeartbeatSession(...) in src/infra/heartbeat-runner.ts:278-330 canonicalizes forced/explicit heartbeat session keys and returns them directly.

    So my read is:

    • the general “heartbeat/session-state weirdness under long-running work” concern may still be worth investigating,
    • but the current tree does not support the reported nested-session-key explanation,
    • and it also already has an overlap/backpressure guard at the runner/wake level.

    If this still reproduces on a current build, I’d look next at a narrower seam: targeted wake routing or an older release behavior, not unconditional suffix growth in the current heartbeat runner.

  3. ha7rock commented on Mar 27, 2026

    @ha7rock

    Same issue here on 2026.3.24 with isolatedSession: true and target: "none".

    My observation aligns with the overlap problem: gateway logs confirm heartbeat fires at the scheduled tick (e.g. :54 past each hour), but ~60-70% of runs silently produce no session transcript. Also seeing 2-hour gaps where the scheduler doesn't fire at all, which may be the orphaned-session issue cascading.

    The runs that do land work fine — it's just that most get swallowed. Overlap guard + proper session key handling would likely fix both symptoms.

  4. meinnamehier commented on Mar 29, 2026

    @meinnamehier

    Can confirm this is still happening on 2026.3.28 (Linux, gateway mode). With isolatedSession: true and heartbeat every 30m, I accumulated 145 zombie entries in sessions.json in ~2.5 days, with keys up to agent:main:main:heartbeat:heartbeat:heartbeat... (119 levels deep). Running a periodic cleanup script as a workaround until this is fixed.

  5. GMTekAI commented on Mar 30, 2026

    @GMTekAI

    I re-checked this against current main after the newer 2026.3.28 confirmations, and my read is:

    • the fresh reports in this thread still make the bug look real,
    • but the current heartbeat runner still does not match the original root-cause theory,
    • and the next place I would investigate is the isolated cron/session path rather than src/infra/heartbeat-runner.ts itself.

    Source-backed notes:

    • The runner already skips when the main lane is busy via getQueueSize(CommandLane.Main) / requests-in-flight in src/infra/heartbeat-runner.ts:639-641.
    • The scheduler is explicitly written not to advance the schedule on that skip in src/infra/heartbeat-runner.ts:1200-1205, and there is regression coverage for that behavior in src/infra/heartbeat-runner.scheduler.test.ts:137-210.
    • I also still could not find the claimed unconditional :heartbeat append path in resolveHeartbeatSession(...); current session resolution is in src/infra/heartbeat-runner.ts:256-330.

    The strongest current seam I found is the isolated cron session stack:

    • src/cron/isolated-agent/run.ts:339-362 calls resolveCronSession(...) with forceNew: params.job.sessionTarget === "isolated", then persists both agentSessionKey and a derived runSessionKey.
    • src/cron/isolated-agent/session.ts:31-77 rolls a fresh sessionId when forceNew is set, while preserving parts of the prior session entry.

    So if users are still seeing zombie / repeatedly nested keys like agent:main:main:heartbeat:heartbeat:... on 2026.3.28, I think the best next step is to reproduce that shape through the isolated-session persistence path and add a failing regression there before changing heartbeat runner logic.

    At minimum, I’m not seeing evidence in the current tree for the earlier proposed fix of adding another overlap map inside the runner itself.

  6. Megarushing commented on Mar 30, 2026

    @Megarushing

    Can confirm issue is still happening on current version:

    My openclaw doctor:

    - agent:<id>:main:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat (28m ago)
    - agent:<id>:main:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat (28m ago)
    - agent:<id>:main:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat (31m ago)
    - agent:<id>:main:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat (33m ago)
    - agent:<id>:main:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat:heartbeat (35m ago)
    
  7. yubingjiaocn commented on Apr 1, 2026

    @yubingjiaocn

    Still reproducing on 2026.3.31 (latest as of today).

    Setup: Agent with isolatedSession: true + every: "30m" heartbeat.

    Observed: Heartbeat sessions recursively spawn new heartbeat sessions. The session key keeps appending :heartbeat suffixes:

    agent:sentinel:main:heartbeat
    agent:sentinel:main:heartbeat:heartbeat
    agent:sentinel:main:heartbeat:heartbeat:heartbeat
    ...up to 7 levels deep
    

    Each nested session runs a full model invocation (~$0.15–0.50 each), so the cost adds up quickly. Had to disable heartbeat entirely as a workaround.

    The isolated heartbeat session should be excluded from heartbeat scheduling — its key already contains :heartbeat, so filtering on that suffix would prevent recursion.

  8. GMTekAI commented on Apr 2, 2026

    @GMTekAI

    I re-checked this against the current tree, and my read is that the bug still looks real, but the original root-cause theory in this issue no longer matches current main cleanly.

    Current source already has runner-level backpressure:

    • src/infra/heartbeat-runner.ts:641 returns requests-in-flight when the main lane is busy.
    • src/infra/heartbeat-runner.ts:1200 explicitly preserves scheduler cadence on that skip path.
    • src/infra/heartbeat-runner.ts:256 is where heartbeat session resolution now lives, and the broad runner always appends another :heartbeat explanation does not line up cleanly with the current file.

    That said, the repeated confirmations here through 2026.3.31 make the bug itself hard to dismiss.

    The strongest current seam I found is the isolated-session persistence path rather than the top-level heartbeat wake guard:

    • src/cron/isolated-agent/run.ts:339 calls resolveCronSession(...)
    • src/cron/isolated-agent/run.ts:345 sets forceNew: params.job.sessionTarget === "isolated"

    So my current read is:

    • the bug is likely still real,
    • the original heartbeat-runner explanation is stale or incomplete against current source,
    • the next step should be a focused repro and failing regression around isolated heartbeat sessions flowing through the isolated cron and session stack, rather than adding another overlap map to heartbeat-runner.ts first.

    Smallest sensible next step:

    1. Reproduce with heartbeat.isolatedSession: true on current main.
    2. Add a failing regression that asserts isolated heartbeat runs do not recursively create nested :heartbeat session keys.
    3. Trace the persisted agentSessionKey and runSessionKey plus fresh-session rollover behavior through src/cron/isolated-agent/run.ts and src/cron/isolated-agent/session.ts before changing runner scheduling logic.
  9. steipete commented on Apr 25, 2026

    @steipete
    Contributor

    Closing this as implemented after Codex review.

    Current main already handles the reported heartbeat session-key accumulation and overlap/backpressure paths: isolated heartbeat keys are canonicalized back to a single :heartbeat sibling, stale accumulated keys are removed, and the runner skips when the main queue or resolved session lane is busy. The changelog shows the session-key cleanup shipped in v2026.4.10.

    What I checked:

    • Main-queue overlap guard: runHeartbeatOnce returns requests-in-flight when the main lane is busy before starting a heartbeat run. (src/infra/heartbeat-runner.ts:759, 60f7a59f5ed5)
    • Resolved-session overlap guard: The runner also checks the resolved session lane and skips when that lane already has queued work, preventing a second run from starting on an active session. (src/infra/heartbeat-runner.ts:782, 60f7a59f5ed5)
    • Isolated key canonicalization: resolveIsolatedHeartbeatSessionKey collapses repeated trailing :heartbeat suffixes back to one canonical isolated key instead of appending unbounded nested suffixes. (src/infra/heartbeat-runner.ts:375, 60f7a59f5ed5)
    • Stale accumulated keys removed on persistence: When an accumulated isolated key is detected, the runner deletes the stale key, stores the canonical isolated key, and records heartbeatIsolatedBaseSessionKey for future stabilization. (src/infra/heartbeat-runner.ts:885, 60f7a59f5ed5)
    • Regression coverage for the exact failure shape: Tests cover already-suffixed keys, deeply accumulated :heartbeat suffixes, and legacy isolated entries converging back to a single canonical key; separate tests cover skipping when the target session lane is busy. (src/infra/heartbeat-runner.isolated-key-stability.test.ts:102, 60f7a59f5ed5)
    • Shipped release note: The changelog entry under ## 2026.4.10 says isolated heartbeat session cleanup shipped: stale accumulated :heartbeat:heartbeat variants are removed when the next tick converges them to the canonical sibling. (CHANGELOG.md:949, 60f7a59f5ed5)

    So I’m closing this as already implemented rather than keeping a duplicate issue open.

    Review notes: reviewed against 60f7a59f5ed5; fix evidence: release v2026.4.10.

  10. locked as resolved and limited conversation to collaborators on Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions