feat(rag): pass chat history with prior thinking to the LLM
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:
@@ -80,6 +80,21 @@ class Settings(BaseSettings):
|
||||
#: Flat seconds to wait between attempts (phase 67,
|
||||
#: ``BOR_LLM_RETRY_DELAY``); the TODO-locked 5 s, no backoff.
|
||||
llm_retry_delay: float = 5.0
|
||||
# --- Chat history (phase 74, TODO L4: prior turns + prior thinking) ---
|
||||
#: Newest client-provided history turns kept per ``POST /api/chat``
|
||||
#: (phase 74, ``BOR_HISTORY_MAX_TURNS``): the request's ``history``
|
||||
#: (the client's prior turns, stateless per A10) is walked
|
||||
#: newest-first and the walk stops once this many turns are kept —
|
||||
#: the oldest turns are the ones dropped. ``0`` = no history (the
|
||||
#: pre-phase-74 two-message requests — the kill switch).
|
||||
history_max_turns: int = 40
|
||||
#: Total char budget for the kept history (phase 74,
|
||||
#: ``BOR_HISTORY_MAX_CHARS``) — ``len(text) + len(thinking or "")``
|
||||
#: per turn, so prior thinking blocks count against the same budget
|
||||
#: as the answer text. A turn that would overflow the remaining
|
||||
#: budget is dropped WHOLE (never cut mid-answer) and the walk stops
|
||||
#: there — the kept history is always a contiguous newest window.
|
||||
history_max_chars: int = 24_000
|
||||
|
||||
# --- RAG tuning ---
|
||||
embedding_dim: int = 768 # verified against aipi /v1 (embed model)
|
||||
@@ -294,6 +309,26 @@ class Settings(BaseSettings):
|
||||
raise ValueError("upload_max_mb must be > 0 (MiB)")
|
||||
return v
|
||||
|
||||
@field_validator("history_max_turns")
|
||||
@classmethod
|
||||
def _history_max_turns_non_negative(cls, v: int) -> int:
|
||||
"""``0`` is the no-history kill switch (pre-phase-74 two-message
|
||||
requests) — a negative value is a typo (the ``agent_max_rounds``
|
||||
pattern)."""
|
||||
if v < 0:
|
||||
raise ValueError("history_max_turns must be >= 0 (0 = no history)")
|
||||
return v
|
||||
|
||||
@field_validator("history_max_chars")
|
||||
@classmethod
|
||||
def _history_max_chars_non_negative(cls, v: int) -> int:
|
||||
"""``0`` is the no-history kill switch (pre-phase-74 two-message
|
||||
requests) — a negative value is a typo (the ``agent_max_rounds``
|
||||
pattern)."""
|
||||
if v < 0:
|
||||
raise ValueError("history_max_chars must be >= 0 (chars)")
|
||||
return v
|
||||
|
||||
@field_validator("docs_branch", "docs_base_branch")
|
||||
@classmethod
|
||||
def _docs_branch_tokens(cls, v: str, info: ValidationInfo) -> str:
|
||||
|
||||
Reference in New Issue
Block a user