Skip to content

fix(session): repair dangling tool_calls anywhere in journal, not only at end - #69

Merged
ace331 merged 3 commits into
mainfrom
fix/repair-dangling-tool-calls-all-assistants
Jul 31, 2026
Merged

ace331 merged 3 commits into
mainfrom
fix/repair-dangling-tool-calls-all-assistants

Conversation

@fgclaw01

Copy link
Copy Markdown
Collaborator

Problem

repair_dangling_tool_calls only checked the last entry in the journal for dangling tool_calls. When a crash happens mid-turn and a subsequent restart appends run markers + new user messages via append_run_start, the orphaned assistant(tool_calls) is buried in the middle of the journal — and the check misses it entirely.

The conversation then hits HTTP 400 from the LLM API:

an assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'

Trigger scenario

  1. Run in progress: assistant(tool_calls) persisted to JSONL
  2. Process hard-crashes before tool results are written
  3. Restart → new prompt → append_run_start appends run_terminal + new user + new run_started
  4. Journal now: user → run_started → assistant(tool_calls) → run_terminal → user → run_started
  5. Load: repair_dangling_tool_calls checks last entry (run_started) → no-op
  6. entries_to_agent_messages filters out markers → orphaned assistant(tool_calls) sent to API → 💥

Fix

Rewrite repair_dangling_tool_calls to walk every assistant entry with tool_calls:

  • Scan forward for matching tool responses (up to next user/system/assistant/run marker)
  • Insert placeholder tool results immediately after each orphaned assistant

Test

Added repair_dangling_tool_calls_finds_orphan_after_restart: reproduces the exact crash-then-restart scenario, validates placeholder insertion position, and verifies entries_to_agent_messages output is API-valid (every assistant tool_call has a matching tool response).

All 185 existing session tests pass unchanged.

@ace331
ace331 force-pushed the fix/repair-dangling-tool-calls-all-assistants branch from 575b71e to 93c8477 Compare July 30, 2026 05:46
@ace331
ace331 enabled auto-merge (squash) July 30, 2026 05:47
@ace331
ace331 force-pushed the fix/repair-dangling-tool-calls-all-assistants branch from 93c8477 to 2581153 Compare July 30, 2026 06:51
fgbot added 2 commits July 31, 2026 11:15
…y at end

When a crash happens mid-turn (assistant-with-tool_calls persisted but
tool results not), and a subsequent restart appends run markers + new
user messages via append_run_start, the orphaned assistant is buried in
the middle of the journal.  repair_dangling_tool_calls only checked the
LAST entry, missing it entirely.  The conversation then hits HTTP 400
from the LLM API: 'assistant message with tool_calls must be followed
by tool messages'.

Rewrite repair_dangling_tool_calls to walk every assistant entry with
tool_calls, scan forward for matching tool responses (stopping at the
next user/system/assistant or run marker), and insert placeholder tool
results immediately after each orphaned assistant.

Add regression test that reproduces the crash-then-restart scenario
and validates placeholder insertion position + API-validity of the
resulting entries_to_agent_messages output.
@fgclaw01
fgclaw01 force-pushed the fix/repair-dangling-tool-calls-all-assistants branch from 2581153 to 18eab73 Compare July 31, 2026 03:17
@ace331
ace331 merged commit 4a1b823 into main Jul 31, 2026
17 checks passed
@ace331
ace331 deleted the fix/repair-dangling-tool-calls-all-assistants branch July 31, 2026 03:24
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.

2 participants