Skip to content

fix(agents): suppress heartbeat section for cron/subagent CLI-backend sessions - #43291

Closed
NayukiChiba wants to merge 11 commits into
openclaw:mainfrom
NayukiChiba:fix/cli-runner-suppress-heartbeat-for-cron-sessions
Closed

NayukiChiba wants to merge 11 commits into
openclaw:mainfrom
NayukiChiba:fix/cli-runner-suppress-heartbeat-for-cron-sessions

Conversation

@NayukiChiba

Copy link
Copy Markdown

Summary

Fixes #43274.

For CLI-backend runs (claude-code, gemini-cli, codex), the buildSystemPrompt helper in src/agents/cli-runner/helpers.ts did not accept a sessionKey and always passed promptMode='full' to buildAgentSystemPrompt. This caused the ## Heartbeats section (including the HEARTBEAT_OK instruction) to be injected even for cron isolated sessions, making the model reply with HEARTBEAT_OK instead of executing the scheduled task.

The embedded Pi runner (attempt.ts) already handled this correctly via resolvePromptModeForSession, which returns 'minimal' for cron/subagent keys. This PR applies the same logic to the CLI-backend path.

Root cause

cli-runner/helpers.ts:buildSystemPrompt had no sessionKey parameter, so promptMode was never derived from the session key and always defaulted to 'full' inside buildAgentSystemPrompt.

Changes

  • src/agents/cli-runner/helpers.ts: add sessionKey? param; derive promptMode as 'minimal' when the key belongs to a cron or subagent session, 'full' otherwise
  • src/agents/cli-runner.ts: forward params.sessionKey to buildSystemPrompt
  • src/agents/cli-runner/helpers.test.ts: 5 new tests covering full vs minimal prompt mode selection based on session key type

Testing

pnpm test -- --run src/agents/cli-runner/helpers.test.ts
# Test Files  1 passed (1)
# Tests       5 passed (5)

… sessions

For CLI-backend runs (claude-code, gemini-cli, codex), the buildSystemPrompt
helper in cli-runner/helpers.ts did not accept a sessionKey and always passed
promptMode='full' to buildAgentSystemPrompt. This caused the '## Heartbeats'
section (including the HEARTBEAT_OK instruction) to be injected even for cron
isolated sessions, making the model reply with HEARTBEAT_OK instead of
executing the scheduled task.

The embedded Pi runner (attempt.ts) already handled this correctly by calling
resolvePromptModeForSession, which returns 'minimal' for cron/subagent keys.
This commit applies the same logic to the CLI-backend path.

Changes:
- cli-runner/helpers.ts: add sessionKey param; derive promptMode as 'minimal'
  when the key is a cron or subagent session, 'full' otherwise
- cli-runner.ts: forward params.sessionKey to buildSystemPrompt
- cli-runner/helpers.test.ts: 5 new tests covering full vs minimal prompt mode
  based on session key type

close #43274
@greptile-apps

greptile-apps Bot commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where CLI-backend sessions (claude-code, gemini-cli, codex) incorrectly received the ## Heartbeats / HEARTBEAT_OK instruction in their system prompt even when running as cron jobs or subagents. The root cause was that buildSystemPrompt in cli-runner/helpers.ts had no sessionKey parameter and therefore always passed promptMode='full' to buildAgentSystemPrompt. The fix adds sessionKey? to buildSystemPrompt, derives promptMode via isCronSessionKey/isSubagentSessionKey (matching the logic already used by the embedded Pi runner), and forwards params.sessionKey from the call site in cli-runner.ts.

Key changes:

  • cli-runner/helpers.ts: new sessionKey? param; inline check sets promptMode to 'minimal' for cron/subagent keys and 'full' otherwise — functionally identical to the existing resolvePromptModeForSession helper in attempt.ts, though inlined rather than reused
  • cli-runner.ts: trivial one-line forwarding of the already-present params.sessionKey
  • helpers.test.ts: five integration-style tests covering no-key (full), regular-agent-key (full), cron-key (minimal), subagent-key (minimal), and structural section checks for cron minimal mode

Confidence Score: 4/5

  • This PR is safe to merge; the fix is correct and well-tested, with only a minor maintainability concern around duplicated logic.
  • The change is minimal, targeted, and directly mirrors the pattern already used by the embedded Pi runner. Both isCronSessionKey and isSubagentSessionKey correctly handle undefined input (returning false), so the promptMode defaults to "full" when no key is present — matching the previous behaviour for normal sessions. The five new tests cover all key scenarios. The only deduction is the minor duplication of the resolvePromptModeForSession logic rather than extracting it to a shared location.
  • No files require special attention; the change is self-contained and low-risk.

Last reviewed commit: 7f21f9d

Comment thread src/agents/cli-runner/helpers.ts Outdated
Comment on lines +79 to +83
// cron and subagent sessions use minimal mode — suppress heartbeat instructions
const promptMode: PromptMode =
isCronSessionKey(params.sessionKey) || isSubagentSessionKey(params.sessionKey)
? "minimal"
: "full";

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.

Duplicated logic already extracted in resolvePromptModeForSession

src/agents/pi-embedded-runner/run/attempt.ts already exports an identical helper:

export function resolvePromptModeForSession(sessionKey?: string): "minimal" | "full" {
  if (!sessionKey) return "full";
  return isSubagentSessionKey(sessionKey) || isCronSessionKey(sessionKey) ? "minimal" : "full";
}

Rather than inlining the same two-condition check here, consider moving resolvePromptModeForSession to a shared location (e.g., src/routing/session-key.ts alongside isCronSessionKey/isSubagentSessionKey, or a small agents/prompt-mode.ts utility) and importing it from both callers. As written, if a third session-key type ever needs "minimal" mode the fix will need to be applied in two places instead of one.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/cli-runner/helpers.ts
Line: 79-83

Comment:
**Duplicated logic already extracted in `resolvePromptModeForSession`**

`src/agents/pi-embedded-runner/run/attempt.ts` already exports an identical helper:

```ts
export function resolvePromptModeForSession(sessionKey?: string): "minimal" | "full" {
  if (!sessionKey) return "full";
  return isSubagentSessionKey(sessionKey) || isCronSessionKey(sessionKey) ? "minimal" : "full";
}
```

Rather than inlining the same two-condition check here, consider moving `resolvePromptModeForSession` to a shared location (e.g., `src/routing/session-key.ts` alongside `isCronSessionKey`/`isSubagentSessionKey`, or a small `agents/prompt-mode.ts` utility) and importing it from both callers. As written, if a third session-key type ever needs `"minimal"` mode the fix will need to be applied in two places instead of one.

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

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Mar 11, 2026
…session-key

The same two-condition check (isCronSessionKey || isSubagentSessionKey → "minimal")
was inlined in cli-runner/helpers.ts and already existed as
resolvePromptModeForSession in pi-embedded-runner/run/attempt.ts.

Move the function to src/routing/session-key.ts alongside the helpers it depends
on, so all callers share one definition. If a third session-key type ever needs
"minimal" mode the fix only needs to happen in one place.

- src/routing/session-key.ts: export resolvePromptModeForSession
- src/agents/pi-embedded-runner/run/attempt.ts: import from routing/session-key
  and re-export to keep existing callers working
- src/agents/cli-runner/helpers.ts: replace inline check with the shared helper
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Apr 27, 2026
@clawsweeper

clawsweeper Bot commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Workflow note: Future ClawSweeper reviews update this same comment in place.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by maintainer comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors can comment @clawsweeper re-review or @clawsweeper re-run on their own open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

Summary
The PR adds session-key-based minimal prompt mode to the CLI runner prompt helper, forwards sessionKey from runCliAgent, moves prompt-mode resolution toward session-key utilities, and adds helper tests.

Reproducibility: yes. by source inspection. Cron CLI execution passes a cron sessionKey and trigger: "cron" into runCliAgent, while current main’s prepare path still builds the CLI system prompt with heartbeatPrompt and no session-derived minimal promptMode.

Real behavior proof
Needs real behavior proof before merge: Missing: the PR body and comments show unit-test output only, not redacted after-fix cron/subagent CLI-backend terminal output, logs, recording, or linked artifact. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, ask a maintainer to comment @clawsweeper re-review.

Next step before merge
Needs contributor or maintainer retargeting plus real behavior proof before merge; this external PR should not be repaired automatically without proof from a real setup.

Security
Cleared: Cleared: the diff only changes TypeScript prompt/session-key helper code and tests, with no CI, dependency, install, secret, or supply-chain surface touched.

Review findings

  • [P2] Derive prompt mode in the active prepare path — src/agents/cli-runner.ts:157
Review details

Best possible solution:

Retarget heartbeat suppression into prepareCliRunContext and buildCliAgentSystemPrompt, preserve system prompt overrides, add prepare-level regression coverage, and attach redacted CLI-backend runtime proof.

Do we have a high-confidence way to reproduce the issue?

Yes by source inspection. Cron CLI execution passes a cron sessionKey and trigger: "cron" into runCliAgent, while current main’s prepare path still builds the CLI system prompt with heartbeatPrompt and no session-derived minimal promptMode.

Is this the best way to solve the issue?

No. The intended policy is right, but the patch should thread prompt mode through prepareCliRunContext and buildCliAgentSystemPrompt rather than changing stale direct prompt wiring.

Full review comments:

  • [P2] Derive prompt mode in the active prepare path — src/agents/cli-runner.ts:157
    Forwarding sessionKey into this old buildSystemPrompt call does not fix the current CLI runtime. Current main routes runCliAgent through prepareCliRunContext, whose buildCliAgentSystemPrompt call still receives heartbeatPrompt without sessionKey, trigger, or promptMode, so cron/subagent CLI-backend sessions can still receive the full Heartbeats section.
    Confidence: 0.91

Overall correctness: patch is incorrect
Overall confidence: 0.91

Acceptance criteria:

  • node scripts/run-vitest.mjs src/agents/cli-runner/prepare.test.ts src/agents/cli-runner/helpers.system-prompt.test.ts
  • node scripts/run-vitest.mjs src/cron/isolated-agent/run.session-key.test.ts

What I checked:

  • PR diff targets old direct prompt construction: gh pr diff shows the branch forwarding params.sessionKey into an old buildSystemPrompt call in runCliAgent, which is no longer the active current-main CLI prompt path. (src/agents/cli-runner.ts:157, d2c04d4fd942)
  • Current main routes CLI runs through prepare context: Current main delegates runCliAgent prompt preparation to prepareCliRunContext before execution. (src/agents/cli-runner.ts:216, c93d6d8daa37)
  • Prepare path still builds a full-capable heartbeat prompt: prepareCliRunContext resolves heartbeatPrompt and calls buildCliAgentSystemPrompt without passing sessionKey, trigger, or promptMode. (src/agents/cli-runner/prepare.ts:331, c93d6d8daa37)
  • CLI prompt helper lacks prompt-mode input: buildCliAgentSystemPrompt accepts heartbeatPrompt but no promptMode or sessionKey, then forwards the heartbeat prompt into configured prompt construction. (src/agents/cli-runner/helpers.ts:68, c93d6d8daa37)
  • System prompt suppresses Heartbeats only in minimal/none mode: buildAgentSystemPrompt defaults promptMode to full; the Heartbeats section is omitted only when the effective mode is minimal or none. (src/agents/system-prompt.ts:887, c93d6d8daa37)
  • Cron CLI runs carry the required context: Cron isolated-agent execution passes the cron sessionKey and trigger: "cron" into runCliAgent, so the prepare path has enough information to derive minimal mode. (src/cron/isolated-agent/run-executor.ts:186, c93d6d8daa37)

Likely related people:

  • BunsDev: Recent current-main commits touched src/agents/cli-runner/prepare.ts and adjacent CLI context/bootstrap metadata, which is the active path this PR needs to retarget. (role: recent prepare-path contributor; confidence: medium; commits: 930852af2959, eb4e20ca1da2; files: src/agents/cli-runner/prepare.ts, src/agents/cli-runner.ts)
  • steipete: Recent work touched lightweight command cron turns and cron/CLI execution context, including prepare.ts and run-executor.ts. (role: cron CLI adjacent contributor; confidence: medium; commits: 686b93e5c710, ddcfde148970; files: src/agents/cli-runner/prepare.ts, src/cron/isolated-agent/run-executor.ts)
  • sahilsatralkar: Recent merged CLI context-engine lifecycle work touched runCliAgent and prepareCliRunContext, the active runtime boundary for this bug. (role: recent CLI lifecycle contributor; confidence: medium; commits: 6d25ae5e0c0a; files: src/agents/cli-runner.ts, src/agents/cli-runner/prepare.ts)
  • obviyus: Recent heartbeat prompt leakage work added trigger-level policy that the CLI prepare path should mirror for cron and subagent prompt construction. (role: adjacent heartbeat prompt policy contributor; confidence: medium; commits: 27aae62d9955; files: src/agents/pi-embedded-runner/run/trigger-policy.ts)

Remaining risk / open question:

  • The branch is based on older direct CLI prompt wiring, so it needs retargeting to current main before it can fix the runtime path.
  • No redacted real cron or subagent CLI-backend proof is attached.

Codex review notes: model gpt-5.5, reasoning high; reviewed against c93d6d8daa37.

@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Apr 28, 2026
@clawsweeper clawsweeper Bot added the P2 Normal backlog priority with limited blast radius. label May 16, 2026
@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. label May 16, 2026
@clawsweeper clawsweeper Bot added impact:message-loss Channel message delivery can be lost, duplicated, or misrouted. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. labels May 17, 2026
@NayukiChiba NayukiChiba closed this by deleting the head repository May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling impact:message-loss Channel message delivery can be lost, duplicated, or misrouted. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. P2 Normal backlog priority with limited blast radius. size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cron isolated sessions should not inject HEARTBEAT_OK system prompt instructions

1 participant