Skip to content

fix(agents): keep settled-tool finalization on the settled transcript - #3

Merged
tomorrowflow merged 2 commits into
fix/mcp-runtime-rollover-leakfrom
fix/settled-finalization-heartbeat
Sep 11, 2026
Merged

tomorrowflow merged 2 commits into
fix/mcp-runtime-rollover-leakfrom
fix/settled-finalization-heartbeat

Conversation

@tomorrowflow

Copy link
Copy Markdown
Owner

Stacked on #2 (fix/mcp-runtime-rollover-leak), which the live gateway runs as 2026.9.3 (7a71f6c). Retarget to main once #2 lands.

Problem

After #2 stopped the empty-response retry from re-prompting an authored NO_REPLY, the owner still received this on Signal from the isolated heartbeat, roughly once per hour when it happened:

I don’t see the previous tool-call results or the original question in our current transcript. To give you the final user-visible answer, please paste the tool results or the question you wanted answered.

The heartbeat run settles its tool batch (read / calendar / todo / write) and ends with an assistant message that has no content blocks. The runner then logs settled post-tool turn lacked a final answer … running isolated finalization, and the isolated finalizer's request to the model is ~50 input tokens: the continuation instruction and nothing else. The model can only say it cannot see the transcript, the finalizer accepts that as the answer, and heartbeat delivery (which only suppresses HEARTBEAT_OK / NO_REPLY) sends it. Observed 9× on 2026-09-10 and again 2026-09-11 08:06Z; every finalizer request measured input=50 cacheRead=0.

Two runner defects combine:

  1. Heartbeats were finalized at all. resolveSettledToolTerminalContinuationInstruction resolves the tool-use message as the batch owner and then branches on that message's stopReason. A heartbeat (optional reply) whose current assistant is a bare stop after settled tools therefore took the tool-use branch and requested a visible-answer continuation, bypassing the allowEmptyStopContinuation gate that only a required reply should open.
  2. The finalizer did not carry the transcript. The built-in harness reopens the session from the store via SessionManager.open(target, cwd, { maxBytes: max(1024, contextTokenBudget * 8), maxEvents }), a view sized for a fresh turn that keeps the newest rows first. When that view cannot hold the settled batch, history is empty and only the instruction is sent. The harness already receives the settled attempt, whose messagesSnapshot is exactly the context being finalized, but ignored it.

Solution

  • incomplete-turn-recovery.ts: branch on the current assistant. A completed stop after the settled batch is the model's own terminal decision; only a turn that owes a visible reply may reopen it. The batch owner still proves settlement.
  • builtin-openclaw.ts / types.ts / attempt-history-prepare.ts: the restricted finalization attempt carries settledTurnMessages (the settled attempt's messagesSnapshot), and history preparation seeds the model context from it for settled-tool-finalization instead of the bounded store read. Persistence of the finalizer's answer is unchanged (still through the store-opened manager).
  • agent-bundle-mcp-manager.ts (separate commit): fix(agents): retire superseded MCP runtimes and keep heartbeats out of memory #2 uses logWarn without importing it, which fails pnpm tsgo:core. Sibling files import it from ../logger.js; this adds the same import. Cherry-pick into fix(agents): retire superseded MCP runtimes and keep heartbeats out of memory #2 if preferred.

Impact

Heartbeat polls that end a settled tool batch silently stay silent. When a settled turn legitimately needs finalization (required reply, transient provider failure after tools), the finalizer now sees the user turn, tool calls and tool results it is asked to summarize.

Not changed: heartbeat delivery policy, the store's bounded read, the empty-response retry from #2.

Evidence

  • settled-turn-finalization.test.ts — new case: heartbeat with a tool-use batch owner and a content-less stop must not request finalization; a required user turn still does. Fails on the original code (instruction returned), passes after.
  • settled-turn-finalization.builtin-harness.test.ts — new: real built-in harness + real runEmbeddedAttempt + SQLite transcript + loopback provider, over openai-responses and openai-completions, with a context token budget too small for the bounded store view. Asserts the finalizer's request body contains the user turn and the tool output. Fails on the original code (request is the bare instruction, matching the live 50-token requests), passes after.
  • pnpm tsgo:core clean; oxfmt --check clean on changed files; git diff --check clean.
  • Focused vitest: settled-turn-finalization*, terminal-resolution, run.incomplete-turn.*, incomplete-turn-recovery, harness/builtin-openclaw, harness/selection, run-embedded-agent.finalization-scope, attempt.spawn-workspace.context-engine, agent-bundle-mcp-manager.lifecycle — 6 shards, 362 tests passed.

Proof gaps: the repo lint runner (scripts/run-lint.mts) refuses to run in this nested worktree ("Declaration input escapes checkout"); oxlint was run directly on the changed files instead. Why the live finalizer's store view was too small is not pinned down (the Kimi model declares a 256K window); the fix removes the finalizer's dependency on that view regardless. No live heartbeat proof yet; verify after deploy by grepping the gateway log for settled post-tool turn lacked a final answer on agent:main:main:heartbeat runs.

🤖 Generated with Claude Code

tomorrowflow and others added 2 commits September 11, 2026 14:46
Two defects in the isolated settled-tool finalizer let heartbeat polls deliver
"I don't see the previous tool-call results or the original question in our
current transcript" to the owner on Signal, once per poll that ended its tool
batch with a content-less assistant message (9x on 2026-09-10, again 09-11).

- resolveSettledToolTerminalContinuationInstruction branched on the batch
  owner's stopReason (the tool-use message). An optional turn whose current
  assistant ended with a bare "stop" after settled tools therefore took the
  required-reply branch and requested a visible-answer continuation. Branch on
  the current assistant; the batch owner only proves settlement.
- The built-in harness reopened the session through a store view bounded by
  max(1024, contextTokenBudget * 8) bytes, newest rows first. When that view
  cannot hold the settled batch, the finalizer sends the continuation
  instruction alone (every live finalizer request measured input=50). Carry
  the settled attempt's messagesSnapshot into the finalization attempt and
  seed its model context from it; answer persistence is unchanged.

Covered end to end through the real built-in harness and embedded runner
against a loopback provider over openai-responses and openai-completions with
a budget too small for the store view, plus a resolver test for the heartbeat
empty-stop shape. Both fail on the original code.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The superseded-runtime retirement path added in 9de3283 calls logWarn
without importing it. pnpm tsgo:core fails with TS2304, and the built runtime
would throw ReferenceError inside that catch block. Sibling runtime files
import it from ../logger.js; do the same here.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@tomorrowflow
tomorrowflow marked this pull request as ready for review September 11, 2026 12:49
@tomorrowflow
tomorrowflow merged commit ec9d934 into fix/mcp-runtime-rollover-leak Sep 11, 2026
tomorrowflow added a commit that referenced this pull request Sep 12, 2026
Heartbeat polls that ran their checks (ls, calendar query, read, write of the
state file) and then stopped with an empty assistant message were reported to
the owner as "Agent couldn't generate a response. Note: some tool actions may
have already been executed" (2026-09-12 11:06 and 13:06). PR #3 correctly
stopped finalizing that turn into prose, so it fell through to the
incomplete-turn classifier, which saw an empty stop with side effects.

The silent-turn guard needs both allowEmptyAssistantReplyAsSilent and an
optional reply expectation. Heartbeats deliberately leave the expectation to
the trigger, and only group chats set the allowance, so a heartbeat could never
qualify. Resolve the effective policy from the trigger when the caller leaves
it open: a run that owes no visible reply (heartbeat, cron, whose prompts
already offer NO_REPLY) accepts a clean empty stop after settled tools as
silence. User and manual runs, and callers that set an explicit expectation,
are unchanged.
tomorrowflow added a commit that referenced this pull request Sep 18, 2026
Heartbeat polls that ran their checks (ls, calendar query, read, write of the
state file) and then stopped with an empty assistant message were reported to
the owner as "Agent couldn't generate a response. Note: some tool actions may
have already been executed" (2026-09-12 11:06 and 13:06). PR #3 correctly
stopped finalizing that turn into prose, so it fell through to the
incomplete-turn classifier, which saw an empty stop with side effects.

The silent-turn guard needs both allowEmptyAssistantReplyAsSilent and an
optional reply expectation. Heartbeats deliberately leave the expectation to
the trigger, and only group chats set the allowance, so a heartbeat could never
qualify. Resolve the effective policy from the trigger when the caller leaves
it open: a run that owes no visible reply (heartbeat, cron, whose prompts
already offer NO_REPLY) accepts a clean empty stop after settled tools as
silence. User and manual runs, and callers that set an explicit expectation,
are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant