Repository navigation
fix(agents): heartbeat always targets main session — prevent routing to active subagent sessions - #61803
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes heartbeat session resolution so heartbeat prompts never route to subagent sessions (preventing subagent task corruption described in #58878).
Changes:
- Add
isSubagentSessionKeyguards across all heartbeat session-resolution entry paths (forcedopts.sessionKey,heartbeat.sessionconfig, and post-canonicalization). - Stop inferring
agentIdfrom a forcedsessionKeyinrunHeartbeatOnce, 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. |
There was a problem hiding this comment.
💡 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 SummaryThis PR adds However, the new regression test (
Confidence Score: 2/5Not 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 AIThis 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 |
|
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. |
There was a problem hiding this comment.
💡 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".
|
Merged via squash.
Thanks @100yenadmin! |
…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
…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
…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
…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
…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
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:opts.sessionKey(forced session key)heartbeat.sessionconfig valueFix: Added
isSubagentSessionKey()guards to all three paths inresolveHeartbeatSession. Any subagent session key is now stripped at every entry point, falling back tomainSessionKey.Files changed:
src/infra/heartbeat-runner.ts— importisSubagentSessionKey, add guards to all three resolution paths, simplify agent ID resolutionsrc/infra/heartbeat-runner.returns-default-unset.test.ts— parameterized regression test covering both entry vectorsRelated
Test plan