Skip to content

Commit 511633b

Browse files
nftpoetristkshitijk4poor
authored andcommitted
fix(agent): stop the run-budget wrap-up notice from mutating a persisted tool row
_maybe_inject_run_budget_wrapup() appends its wrap-up notice to the newest role:"tool" message in place, with no _DB_PERSISTED_MARKER check. Its sibling, _maybe_inject_iteration_budget_warning(), got exactly this guard added in the same recent saga (turn_iteration_prep.py), with the comment "an older turn may already be cached." The reachability is structural, not an edge case: _maybe_inject_run_budget_wrapup is only ever called from prepare_iteration(), at the START of the next iteration -- strictly after tool_executor.py's _flush_session_db_after_tool_progress has already flushed and marked the previous iteration's tool row persisted. So every successful injection was mutating an already-persisted row: the wire request for that turn carried the notice, but the durable transcript never did, diverging replay from the live bytes and invalidating the provider's prompt-cache prefix from that row onward. Fix: - Add the same _DB_PERSISTED_MARKER guard to _maybe_inject_run_budget_wrapup, scoped to the specific tool row the reversed scan lands on (not just messages[-1], since this function -- unlike its sibling -- scans backward for the newest tool row rather than only checking the tail). - Wire _maybe_inject_run_budget_wrapup into _flush_session_db_after_tool_progress (pre-flush), mirroring exactly how _maybe_inject_iteration_budget_warning is wired in both places. Without this, the guard alone would make the notice stop firing in the common case, since prepare_iteration's call site almost always hits an already-persisted row -- the pre-flush call site is what actually lets it land in durable bytes. Verified empirically: read the real call graph (tool_executor.py's three _flush_session_db_after_tool_progress call sites cover every tool-completion path) to confirm the guard's premise, then added an end-to-end test using a real AIAgent + SessionDB that flushes and checks the persisted row for the notice text. Mutation-verified: reverting the two production files drops exactly the 2 new/updated assertions (28 pass, 2 fail); reapplying restores green (30 passed). Also ran the sibling iteration-budget-warning and /steer suites (71 passed) to check for interaction regressions -- none.
1 parent bbf91b7 commit 511633b

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎agent/conversation_loop.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,13 @@ def _maybe_inject_run_budget_wrapup(agent: Any, messages: List[Dict[str, Any]])
127127
(time.time() - started) < 0.8 * float(budget)
128128
):
129129
return False
130+
from agent.context_compressor import _DB_PERSISTED_MARKER
130131
for msg in reversed(messages):
131132
if isinstance(msg, dict) and msg.get("role") == "tool":
133+
# Only the current tool-result tail is mutable; an older turn may already be
134+
# cached (same contract as _maybe_inject_iteration_budget_warning).
135+
if msg.get(_DB_PERSISTED_MARKER):
136+
return False
132137
existing = msg.get("content", "")
133138
if isinstance(existing, str):
134139
msg["content"] = existing + f"\n\n{RUN_BUDGET_WRAPUP_NOTICE}"

‎agent/tool_executor.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ def _resolve_concurrent_tool_timeout() -> float | None:
173173
def _flush_session_db_after_tool_progress(agent, messages: list, *, stage: str) -> bool:
174174
"""Flush tool-call progress to the session DB before projecting it to any UI: tool side
175175
effects can kill/restart the process before turn-end persistence runs."""
176+
from agent.conversation_loop import _maybe_inject_run_budget_wrapup
176177
from agent.turn_iteration_prep import _maybe_inject_iteration_budget_warning
177178

178179
# Persist exactly the checkpoint text the next model call will see, before stamping
179180
# this tool result as durable. Already-written rows must never be rewritten later.
181+
_maybe_inject_run_budget_wrapup(agent, messages)
180182
_maybe_inject_iteration_budget_warning(agent, messages)
181183
try:
182184
persisted = agent._flush_messages_to_session_db(messages) is not False

‎tests/agent/test_run_budget.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ def test_wrapup_not_injected_without_turn_clock():
285285
assert _maybe_inject_run_budget_wrapup(agent, messages) is False
286286

287287

288+
def test_wrapup_skips_already_persisted_tool_row():
289+
"""An already-flushed tool row is append-only; mutating it would diverge the
290+
replayed transcript from the live wire bytes and break the prompt cache (same
291+
contract as _maybe_inject_iteration_budget_warning)."""
292+
from agent.context_compressor import _DB_PERSISTED_MARKER
293+
from agent.conversation_loop import _maybe_inject_run_budget_wrapup
294+
295+
agent = _StubAgent(budget=900, started=time.time() - 800)
296+
messages = _tool_messages()
297+
messages[-1][_DB_PERSISTED_MARKER] = True
298+
snapshot = [dict(m) for m in messages]
299+
300+
assert _maybe_inject_run_budget_wrapup(agent, messages) is False
301+
assert agent._run_budget_wrapup_injected is False
302+
assert messages == snapshot
303+
304+
288305
def test_wrapup_multimodal_tool_content():
289306
"""Content-blocks tool results get a text block appended, not clobbered."""
290307
from agent.conversation_loop import (
@@ -304,6 +321,32 @@ def test_wrapup_multimodal_tool_content():
304321
assert blocks[-1] == {"type": "text", "text": RUN_BUDGET_WRAPUP_NOTICE}
305322

306323

324+
# ── pre-flush wiring (the notice must reach durable bytes) ─────────────────
325+
326+
327+
def test_wrapup_lands_in_the_persisted_row_via_pre_flush_hook(monkeypatch, tmp_path):
328+
"""The wrap-up notice must be injected BEFORE the tool row is flushed, or it
329+
never reaches SQLite: prepare_iteration's own call always runs on an
330+
already-persisted row (the previous iteration's flush already ran)."""
331+
from hermes_state import SessionDB
332+
from agent.tool_executor import _flush_session_db_after_tool_progress
333+
from agent.conversation_loop import RUN_BUDGET_WRAPUP_NOTICE
334+
335+
session_db = SessionDB(db_path=tmp_path / "proof.db")
336+
try:
337+
agent = _make_agent(tmp_path, monkeypatch, session_db=session_db, run_budget_seconds=900)
338+
agent._run_budget_started_at = time.time() - 800 # 89% elapsed
339+
messages = _tool_messages()
340+
341+
assert _flush_session_db_after_tool_progress(agent, messages, stage="checkpoint")
342+
343+
persisted = session_db.get_messages(agent.session_id)
344+
assert any(RUN_BUDGET_WRAPUP_NOTICE in str(row["content"]) for row in persisted)
345+
assert agent._run_budget_wrapup_injected is True
346+
finally:
347+
session_db.close()
348+
349+
307350
# ── turn clock stamping ────────────────────────────────────────────────────
308351

309352

0 commit comments

Comments
 (0)