Skip to content

fix(agents): heartbeat always targets main session — prevent routing to active subagent sessions - #61803

Merged
jalehman merged 4 commits into
openclaw:mainfrom
electricsheephq:fix/58878-heartbeat-main-routing-v2
Apr 7, 2026
Merged

jalehman merged 4 commits into
openclaw:mainfrom
electricsheephq:fix/58878-heartbeat-main-routing-v2

Conversation

@100yenadmin

@100yenadmin 100yenadmin commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes (plain English)

When background heartbeat checks ran while sub-agents were active, they could accidentally target a sub-agent's session instead of the main one — corrupting the sub-agent's task results by overwriting them with a heartbeat status. This fix ensures heartbeats always route to the main session.

Technical details

Root cause: resolveHeartbeatSession() had three code paths where a subagent session key could slip through:

  1. Via opts.sessionKey (forced session key)
  2. Via heartbeat.session config value
  3. Via post-canonicalization (where canonicalization transforms a key into a subagent format)

Fix: Added isSubagentSessionKey() guards to all three paths in resolveHeartbeatSession. Any subagent session key is now stripped at every entry point, falling back to mainSessionKey.

Files changed:

  • src/infra/heartbeat-runner.ts — import isSubagentSessionKey, add guards to all three resolution paths, simplify agent ID resolution
  • src/infra/heartbeat-runner.returns-default-unset.test.ts — parameterized regression test covering both entry vectors

Related

Test plan

  • 32/32 tests pass
  • Subagent session keys via opts.sessionKey are rejected -> falls back to mainSessionKey
  • Subagent session keys via heartbeat.session config are rejected -> falls back to mainSessionKey
  • Post-canonicalization subagent keys are rejected
  • Legitimate main session keys still route correctly

Copilot AI review requested due to automatic review settings April 6, 2026 10:01

Copilot AI left a comment

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.

Pull request overview

Fixes heartbeat session resolution so heartbeat prompts never route to subagent sessions (preventing subagent task corruption described in #58878).

Changes:

  • Add isSubagentSessionKey guards across all heartbeat session-resolution entry paths (forced opts.sessionKey, heartbeat.session config, and post-canonicalization).
  • Stop inferring agentId from a forced sessionKey in runHeartbeatOnce, keeping heartbeat anchored to the selected/default agent.
  • Add a parameterized regression test intended to ensure subagent session keys fall back to the main session.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/infra/heartbeat-runner.ts Adds subagent-session guards to heartbeat session resolution and removes agentId inference from forced session keys.
src/infra/heartbeat-runner.returns-default-unset.test.ts Adds parameterized regression coverage for rejecting subagent session keys entering via options or config.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6a3db7c0e8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@greptile-apps

greptile-apps Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds isSubagentSessionKey guards to all three resolution paths in resolveHeartbeatSession (src/infra/heartbeat-runner.ts) to prevent heartbeats from being routed to active subagent sessions instead of the main session. The production guard logic is correctly structured and well-layered.

However, the new regression test (heartbeat-runner.returns-default-unset.test.ts) builds the subagent key using a slash separator (agent:${agentId}/subagent:task-abc) rather than the canonical colon format (agent:${agentId}:subagent:task-abc). Because parseAgentSessionKey splits on :, isSubagentSessionKey returns false for the slash format, so the guards added in this PR are never invoked by the test. Both injectVia cases pass only due to the pre-existing agent-ID mismatch fallback, meaning the intended regression coverage is absent.

  • src/infra/heartbeat-runner.ts — production guard logic looks correct
  • src/infra/heartbeat-runner.returns-default-unset.test.ts — subagentKey must use the colon format (agent:${agentId}:subagent:task-abc) to actually trigger isSubagentSessionKey and validate the new guards"

Confidence Score: 2/5

Not safe to merge — the new regression test does not exercise the guards it was written to validate, leaving the actual fix untested.

The production code change is logically sound, but the test uses a malformed subagent key format (slash instead of colon separator) that parseAgentSessionKey does not parse as a subagent key. isSubagentSessionKey returns false for it, so the added guards never fire and the test passes for the wrong reason.

src/infra/heartbeat-runner.returns-default-unset.test.ts — the subagentKey construction must use agent:${agentId}:subagent:task-abc (colon separator) rather than agent:${agentId}/subagent:task-abc (slash separator).

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/infra/heartbeat-runner.returns-default-unset.test.ts
Line: 930

Comment:
**Incorrect subagent key format — guards not actually exercised**

The test constructs the subagent key with a slash separator:
```
const subagentKey = `agent:${agentId}/subagent:task-abc`;
```
But the canonical format throughout this codebase (e.g. `src/tasks/task-registry.test.ts:1170`, `src/gateway/sessions-patch.test.ts:327`) uses colons only: `agent:${agentId}:subagent:task-abc`.

`parseAgentSessionKey` splits on `:`, so `"agent:main/subagent:task-abc"` is parsed as `{ agentId: "main/subagent", rest: "task-abc" }`. Since `rest` does not start with `"subagent:"`, `isSubagentSessionKey` returns `false` — the guard added in this PR **never fires**.

Both test cases fall back to `mainSessionKey` only because `normalizeAgentId("main/subagent")` = `"main-subagent"` ≠ `"main"` (the pre-existing agent-ID mismatch check), not because the new subagent guard triggered. Removing the `isSubagentSessionKey` guards entirely from `resolveHeartbeatSession` would not break this test.

```suggestion
      const subagentKey = `agent:${agentId}:subagent:task-abc`;
```

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

Reviews (1): Last reviewed commit: "fix(heartbeat): add subagent guard to re..." | Re-trigger Greptile

@100yenadmin

Copy link
Copy Markdown
Contributor Author

Related issues: This PR also relates to #61567 (installer fails with Cannot find name DEFAULT_CONTEXT_TOKENS in pi-embedded-runner), #53540 (embedded runner Network connection lost on LLM call after accumulating large context), and #41515 (embedded runner reports API rate limit reached when provider has full headroom). These share the same pi-embedded-runner code paths.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@100yenadmin

Copy link
Copy Markdown
Contributor Author

@steipete @jalehman I've debugged the hell out of this one, I believe it's a critical bug fix on the heartbeat.

@jalehman jalehman self-assigned this Apr 6, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c4be2e8dee

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/infra/heartbeat-runner.ts Resolved
@jalehman
jalehman merged commit caecd3c into openclaw:main Apr 7, 2026
24 of 26 checks passed
@jalehman

jalehman commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

Thanks @100yenadmin!

lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…to active subagent sessions (openclaw#61803)

Merged via squash.

Prepared head SHA: 5d79db3
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…to active subagent sessions (openclaw#61803)

Merged via squash.

Prepared head SHA: 5d79db3
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
YoshiaKefasu pushed a commit to YoshiaKefasu/DennouAibou that referenced this pull request May 18, 2026
…to active subagent sessions (openclaw#61803)

Merged via squash.

Prepared head SHA: 5d79db3
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…to active subagent sessions (openclaw#61803)

Merged via squash.

Prepared head SHA: 5d79db3
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…to active subagent sessions (openclaw#61803)

Merged via squash.

Prepared head SHA: 5d79db3
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
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.

bug(agents): heartbeat routes to subagent session instead of main — corrupts subagent task results

3 participants