feat(rag): pass chat history with prior thinking to the LLM
Build and Push Containers / build-and-push-app (push) Successful in 1m39s
Build and Push Containers / build-and-push-db (push) Successful in 11s

Phase 74 (TODO.md L4): a follow-up question now reaches the model WITH
the conversation so far — every prior user/brain turn and the prior
thinking blocks on brain turns (preserve-thinking) — while
POST /api/chat stays stateless (A10): the client provides the history
in the request body and the server stores nothing new.

Server (task 01):
- ChatRequest.history: optional list[HistoryTurn] (who: user|brain,
  text, optional thinking) — absent/empty keeps the request
  byte-identical to pre-phase-74 (the two-message [system, user]
  request; the kill-switch semantics are pinned in the integration
  suite).
- app.rag.prompts.history_to_messages: pure mapper — walks the turns
  newest-first against the settings budgets (history_max_turns=40 /
  history_max_chars=24000, BOR_HISTORY_MAX_TURNS /
  BOR_HISTORY_MAX_CHARS); a capped turn is dropped WHOLE (never cut
  mid-answer); the kept window is returned oldest-first; brain turns
  carry their thinking as reasoning_content (A4) only when
  non-empty.
- Both branches feed it: the deflected path splices it between the
  system prompt and the current user message (the phase-71 recovery
  still rebuilds from messages[1:]), the grounded agent receives
  run_agent(..., history=hist); llm.py's message params widen to
  list[dict[str, Any]] (string-only messages stay byte-identical on
  the wire — the SDK passes message dicts through verbatim).
- The per-turn log line (PLAN §9) gains history_msgs=N after
  kb_chars=N.
- Pins: tests/unit/test_history.py (mapper: mapping, reasoning
  gating, both budgets, drop-whole, ordering, empty default),
  tests/unit/test_config.py (the two settings + env overrides),
  tests/unit/test_agent.py (the history splice + the default),
  tests/integration/test_chat_api.py (deflected AND grounded forward
  the history incl. reasoning_content, no-history byte-identity, 422
  pins, the log field).

Client (task 02):
- runTurn — the single funnel for fresh send / phase-49 retry /
  phase-53 stale-regen — sends history = the conversation record
  minus the current question, with thinking only on brain records
  that streamed one (undefined drops the key from the JSON, the
  record's convention); the question is never duplicated into the
  history.

Wire proof (task 03):
- The mock's echo my history marker (HISTORY_TRIGGER) answers with
  the deterministic history echo — history: N prior messages; last
  answer tail: <last 24 chars>; thinking: yes|no — checked BEFORE
  the DEFLECT_MODE branch (like TABLE_TRIGGER), so it fires on both
  turn branches whatever the gate says; the module docstring records
  the user/assistant-only history invariant that keeps every
  existing (tool-result-classified) marker flow unaffected.
- tests/e2e/test_llm_history.py (isolated): a grounded follow-up and
  a deflected follow-up both receive history: 2 prior messages +
  thinking: yes + the byte-exact tail of turn 1's answer (derived
  from the persisted bor.chat.v1 record — the same array the client
  maps into the body); a cold start receives history: 0 prior
  messages / last answer tail: none / thinking: no.
- Regressions green in isolation: chat_rag, chat_history (phase 50),
  agent_document_tools, harness_aligned_tools, stop_generation,
  retry_answer, response_to_docs.
This commit is contained in:
2026-09-05 16:04:40 -04:00
parent a16130c71d
commit 055c0b5d85
29 changed files with 1418 additions and 18 deletions
+77 -1
View File
@@ -30,7 +30,7 @@ import asyncio
import json
import logging
import uuid
from collections.abc import AsyncGenerator, AsyncIterator
from collections.abc import AsyncGenerator, AsyncIterator, Sequence
from copy import deepcopy
from typing import TYPE_CHECKING, Any, cast
@@ -115,7 +115,11 @@ async def _run(
holder: AgentHolder,
settings: Settings,
seed_docs: list[Document] | None = None,
history: Sequence[dict[str, Any]] = (),
) -> list[StreamPiece | ToolCallPiece | RetryPiece]:
"""Consume one ``run_agent`` turn; *history* (phase 74) is the
client's prior turns spliced between system and user (default
``()`` — the pre-phase-74 two-message request)."""
out: list[StreamPiece | ToolCallPiece | RetryPiece] = []
async for piece in run_agent(
cast("LLMClient", llm),
@@ -125,6 +129,7 @@ async def _run(
seed_docs=seed_docs or [],
settings=settings,
holder=holder,
history=history,
):
out.append(piece)
return out
@@ -458,6 +463,77 @@ def test_content_and_tool_call_in_one_stream_keeps_both(
assert llm.requests[1][0][3]["content"] == "0 documents:\n"
# ---------- phase 74: client history between system and user ----------
def test_run_agent_default_history_keeps_two_message_request() -> None:
"""No *history* (the default ``()``) → the model sees exactly the
pre-phase-74 two-message request ``[system, user]`` — byte-identical
behavior (owner-locked A2)."""
llm = ScriptedLLM([StreamPiece("content", "the answer")])
asyncio.run(_run(llm, AgentHolder(), _settings()))
(messages, _tools) = llm.requests[0]
assert messages == [
{"role": "system", "content": "SYSTEM_PROMPT"},
{"role": "user", "content": "QUESTION"},
]
def test_run_agent_places_history_between_system_and_user() -> None:
"""A non-empty *history* (the client's prior turns, already mapped by
``history_to_messages``) is spliced between the system prompt and the
CURRENT user message — oldest-first, with the assistant turn's prior
thinking riding on ``reasoning_content`` (A4). The current question
stays LAST."""
history = [
{"role": "user", "content": "old question"},
{
"role": "assistant",
"content": "old answer",
"reasoning_content": "old thinking",
},
]
llm = ScriptedLLM([StreamPiece("content", "the answer")])
pieces = asyncio.run(
_run(llm, AgentHolder(), _settings(), history=history)
)
assert [p for p in pieces if isinstance(p, StreamPiece)] == [
StreamPiece("content", "the answer")
]
(messages, _tools) = llm.requests[0]
assert messages == [
{"role": "system", "content": "SYSTEM_PROMPT"},
{"role": "user", "content": "old question"},
{
"role": "assistant",
"content": "old answer",
"reasoning_content": "old thinking",
},
{"role": "user", "content": "QUESTION"},
]
def test_run_agent_history_survives_a_tool_round(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The tool rounds append assistant/tool messages to the SAME
``messages`` list — the prior history stays in place between the
system prompt and the current question on the SECOND request too."""
monkeypatch.setattr(agent, "list_catalog", lambda db: [])
llm = ScriptedLLM(
[ToolCallPiece(id="call_1", name="ls", arguments={})],
[StreamPiece("content", "the answer")],
)
history = [{"role": "assistant", "content": "old answer"}]
asyncio.run(_run(llm, AgentHolder(), _settings(), history=history))
_first, second = llm.requests
assert second[0][:3] == [
{"role": "system", "content": "SYSTEM_PROMPT"},
{"role": "assistant", "content": "old answer"},
{"role": "user", "content": "QUESTION"},
]
# ---------- ls: full catalog + scoping ----------