Skip to content

emrg: fix task_vibe_check LLM 400 from window-boundary orphan tool messages - #874

Merged
argszero merged 1 commit into
masterfrom
feature/vibe-check-window-tool-orphan
Aug 19, 2026
Merged

emrg: fix task_vibe_check LLM 400 from window-boundary orphan tool messages#874
argszero merged 1 commit into
masterfrom
feature/vibe-check-window-tool-orphan

Conversation

@argszero

Copy link
Copy Markdown
Owner

Problem

Every task_vibe_check for long-running scheduled tasks fails with an LLM 400:

Messages with role 'tool' must be a response to a preceding message with 'tool_calls'

Observed repeatedly in the daemon log (48x on aitokenpool-opensource-task, 18x on emrg-task since 2026-08-19T18:51). Because the vibe check fails, the scheduler's empty-cycle counter is never updated — recommend_slowdown never engages.

Root cause

_task_vibe_check loads the task's own session history (rant 2026-08-19T10:15:43) and slices it to the last 100 messages (history[-100:]). Slicing a validated message list can orphan a leading role: "tool" message: its matching assistant message with tool_calls lies just before the window boundary. The API then rejects the request because a tool message must follow an assistant-with-tool_calls.

Reproduced with real data: the aitokenpool task session has 172 validated messages ending in assistant; the [-100:] window starts with role: "tool".

Fix

  • Add session.last_n_messages(messages, n) — slices the last n messages and drops leading window-boundary orphan role: "tool" messages (the remaining window keeps its original order).
  • Use it in _task_vibe_check instead of history[-100:].
  • 6 new tests: 5 unit tests in tests/test_session.py (including a reproduction of the real 172-message session) + 1 e2e regression in tests/test_ws_e2e.py (a 180-message session whose 100-window starts at a tool message).
  • Agent.md documented Python count synced 977 → 983.

Verification

  • Full local suite: 982 passed, 1 skipped
  • Import + CLI checks green
  • Vibe-check e2e tests: 6/6 passed (incl. new regression)

…ssages
Slicing a validated session history to the last 100 messages can orphan a
leading role:'tool' message whose matching assistant(tool_calls) lies before
the window, so the LLM rejects every task_vibe_check with 400 'Messages with
role tool must be a response to a preceding message with tool_calls' (48x on
aitokenpool-opensource-task, 18x on emrg-task since 2026-08-19T18:51).
Add session.last_n_messages() which drops leading window-boundary orphan
tool messages, and use it in _task_vibe_check. 6 new tests: 5 unit
(test_session.py) + 1 e2e regression (test_ws_e2e.py, 180-message session
whose [-100:] window starts at a tool message). Agent.md count 977 -> 983.

@argszeroargszero left a comment

Copy link
Copy Markdown
OwnerAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — cycle (1/3). Verified locally: full suite 982 passed + 1 skipped, import + CLI green, vibe-check e2e 6/6. Root cause reproduced with the real aitokenpool session (172-message validated history whose [-100:] window starts with role:'tool'); last_n_messages drops boundary orphans. Agent.md count synced 977 → 983.

@argszeroargszero left a comment

Copy link
Copy Markdown
OwnerAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — cycle (2/3). Re-verified: head 7742dfa unchanged, MERGEABLE/CLEAN, test + test-windows both PASS (run 32248439433). Root cause + fix reviewed — session.last_n_messages drops window-boundary orphan tool messages so task_vibe_check stops failing with LLM 400 for long sessions. Agent.md count synced.

@argszeroargszero left a comment

Copy link
Copy Markdown
OwnerAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — cycle (3/3). Re-verified: head 7742dfa unchanged, MERGEABLE/CLEAN, CI test + test-windows PASS. Three consecutive approvals from independent cycles (936/937/938), no ❌ — merging.

@argszero
argszero merged commit f889715 into masterAug 19, 2026
2 checks passed
@argszero
argszero deleted the feature/vibe-check-window-tool-orphan branch August 19, 2026 11:47
argszero added a commit that referenced this pull request Aug 19, 2026
Co-authored-by: EMRG Evolution <emrg@argszero.dev>
Sign up for freeto 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

@argszero