fix: chat_template, keep tool-call reasoning across later user turns

#45
by dimondev - opened

Summary

An assistant turn that called a tool loses its reasoning_content as soon as a later user message exists. On an agent loop that is every turn after the first tool call, and it invalidates ~27% of the previously cached prefix.

Patched chat_template.jinja attached. One-line change.

Hub main this PR
ChatLint errors 3 2
think_then_tool history_rewrite fail (27.5% prefix lost) pass

The remaining 2 are the preamble-hoist issue (assistant text after <tool_response|>), unchanged.

Fix

- {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}
+ {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or message.get('tool_calls') or preserve_thinking -%}

Keep reasoning whenever the message itself made tool calls. preserve_thinking still opts into keeping plain (non-tool) reasoning across the whole history.

The Jul 15 commit was titled "reasoning preservation"; this is the remaining hole. Identical one-liner applies to gemma-4-31B-it / 12B / 26B-A4B-it (separate PRs, same patch files under the contribution).

Reproduce

pip install chatlint
chatlint check chat_template.jinja

Ask

A ChatLint CI workflow on template changes would have flagged this. Happy to open that as a follow-up.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment