fix: buffer exec output for next user turn; alternation for strict templates

User-test surfaced the bug: with `deep` (mistral-nemo-12b) active,
running `list files` -> y on `CMD: ls` -> `Are there directory entries
beginning with "lor"?` returned a Jinja exception:

    api: ... Error: Jinja Exception: After the optional system message,
    conversation roles must alternate user/assistant/user/assistant/...

Cause: §6 specified "exec output injected into context uses role 'user'
with a prefix tag '[exec output]'." This works for permissive templates
(qwen2.5-coder-1.5b, the `fast` preset) but produces a back-to-back
user/user pair on strict templates that enforce the OpenAI alternation
contract — `[exec output]` user turn followed by the user's actual
follow-up question.

Fix:

context.lua:
  - new field `pending_exec_output` (initially nil)
  - new method `:append_exec_output(out)` buffers (concat on subsequent
    captures so multi-shell-then-ai still merges everything)
  - new method `:append_user(content)` flushes buffered exec output as
    a `[exec output]\n...\n\n` prefix and appends a user turn
  - `:reset()` also clears the buffer

repl.lua:
  - run_shell calls ctx:append_exec_output(out) instead of
    ctx:append({role="user", content="[exec output]\n"..out})
  - ask_ai calls ctx:append_user(text) instead of raw :append; saves
    prev_pending so a broker error can restore the buffer for retry

PHASE0.md §6:
  - amended the role-injection paragraph to describe the buffer-and-
    prepend policy; the §3 invariants list is untouched (this was a §6
    design detail, not a locked invariant)

Verification:
  - context unit tests cover: alternation after the failing sequence,
    multi-shell merge, reset clears buffer, broker-error retry path
  - live reproduction against `deep` (mistral-nemo) of the exact
    user-reported sequence succeeds; model responds with a sensible
    `CMD: ls | grep '^lor'` instead of a Jinja exception

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-10 18:41:21 +00:00
parent 8870eb0451
commit 16490e6905
3 changed files with 42 additions and 10 deletions
+9 -5
View File
@@ -46,7 +46,10 @@ function M.run(config)
end
-- Run a shell command, framing output and (per config.shell.capture_output)
-- injecting it back into context as a `[exec output]`-tagged user turn.
-- buffering it for the NEXT user turn — context.append_exec_output keeps
-- a [exec output] block pending until ask_ai flushes it via append_user.
-- Direct user-role injection violated chat-template alternation (mistral-
-- nemo's Jinja rejects user/user back-to-back); see PHASE0.md §6.
local function run_shell(cmd)
local chd, err = executor.maybe_chdir(cmd)
if chd ~= nil then
@@ -61,19 +64,20 @@ function M.run(config)
local out, code = executor.exec(cmd)
renderer.exec_output(out, code)
if config.shell and config.shell.capture_output then
ctx:append({ role = "user", content = "[exec output]\n" .. out })
status_evictions(ctx:enforce_budget())
ctx:append_exec_output(out)
end
end
-- Send user text to the active model, render the response, and (per
-- §6 + config.shell.confirm_cmd) optionally execute extracted CMD: lines.
local function ask_ai(text)
ctx:append({ role = "user", content = text })
local prev_pending = ctx.pending_exec_output
ctx:append_user(text) -- flushes any pending [exec output] as prefix
local resp, err = broker.chat(active_cfg, ctx:to_messages())
if not resp then
renderer.status("broker error: " .. tostring(err))
table.remove(ctx.turns) -- back out the user turn we just added
table.remove(ctx.turns) -- back out the merged user turn
ctx.pending_exec_output = prev_pending -- restore buffered exec output
return
end
ctx:append({ role = "assistant", content = resp })