WeKnora/internal/models/chat
lyingbug 62f7da5eea
feat(agent): persist compaction checkpoints across turns (#3407)
* feat(agent): persist compaction checkpoints across turns

Agent compaction only lived inside the turn that ran it: the summary was
discarded at turn end and LoadAgentHistory rebuilt raw history from the
messages table, so every later turn past the threshold paid for another
summarization of nearly the same history.

A compaction whose history summary ends exactly on a stored turn is now
written onto that turn's assistant message (messages.context_checkpoint).
LoadAgentHistory starts from the newest checkpoint: the summary replaces
that turn and everything before it, and later turns are replayed verbatim.
The compactor already treats a leading summary as the previous summary, so
checkpoints fold forward incrementally.

History messages carry their turn's assistant message ID (chat.Message
TurnID, engine-internal) so compaction can tell whether its cut lands on a
stored-turn boundary. Nothing is persisted when the cut ends inside a stored
turn, covers only the live turn, or the history summary degraded to a raw
archive; a split turn's prefix summary is never persisted because that turn
is replayed whole next time. Writes are column-scoped and best-effort, and a
failed lookup falls back to history without a checkpoint.

Storing the checkpoint on the covered turn keeps the boundary implicit, so
session forks copy it with the turn and deleting the turn drops it. The
HistoryTurns cap still applies, now to the turns after the checkpoint.

* test(skill): wait for the background install in the name-conflict test

InstallSkill queues the install on a goroutine, and the fixture row already
starts at installing, so reading the status right after the call raced the
mock install and failed whenever it landed on ready first. Wait for the run
to finish on the reused row instead, as the other install tests do.

* feat(agent): size agent history by tokens instead of turn count

Agent history was capped at history_turns (default 5) because every turn
used to rebuild raw history and compaction could not persist. With
compaction checkpoints persisted, the cap is no longer what keeps a session
inside the window, and it threw away context that would have fit.

LoadAgentHistory now loads the newest checkpoint summary plus the newest
turns after it that fit a token budget: the run's compaction threshold
(agent.HistoryTokenBudget, the window less the reply reserve). History
that large already triggers the first round's compaction, which persists a
new checkpoint, so compaction rather than a turn count bounds the session.
Loading more would only hand that compaction a summarizer input larger
than the model can read.

Rows are paged backwards by (created_at, id) and the read stops at the
checkpoint or once the budget is full, so a long session is never read
whole. Kept turns stay contiguous, the newest turn is kept even when it
alone exceeds the budget (compaction splits it), and a turn cut by the read
is left out rather than replayed without its original question.

Agent mode no longer reads history_turns: AgentConfig.HistoryTurns is
removed, the editor hides the input in smart-reasoning mode and explains
the automatic behavior, and the smart-reasoning builtins drop the setting.
Quick-answer (KnowledgeQA) keeps its turn cap since it has no compaction.

* Revert "feat(agent): size agent history by tokens instead of turn count"

This reverts commit d05fb6e5f1f161813a88e9127609f7a059f38f12.

Loading history up to the compaction threshold lets the loader drop the
oldest turns before compaction can trigger: whenever a single turn's
replay is larger than the system prompt plus the new question, the
trimmed history stays under the threshold, nothing is summarized, and the
session degrades into a large sliding window that never gets a
checkpoint. A turn-by-turn simulation of LoadAgentHistory with the real
compactor showed zero compactions over 60 turns in those cases.

Keep the persisted checkpoints with the HistoryTurns cap for now; token
budgeted history returns in a separate change with a budget that leaves
the overflow to compaction.
2026-09-18 20:38:56 +08:00
..
anthropic.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
anthropic_test.go feat(chat): keep prompt prefixes stable and send provider cache markers 2026-09-03 00:45:52 +08:00
anthropic_tools.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
anthropic_tools_test.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
chat.go feat(agent): persist compaction checkpoints across turns (#3407) 2026-09-18 20:38:56 +08:00
completion_budget.go fix(chat): send one completion-token field per provider (#3082) 2026-09-07 19:16:27 +08:00
completion_budget_test.go fix(chat): send one completion-token field per provider (#3082) 2026-09-07 19:16:27 +08:00
concurrency_wrapper.go feat(system): expose model concurrency stats on runtime queues dashboard 2026-07-11 07:17:28 +08:00
concurrency_wrapper_test.go fix(models): harden background concurrency governor 2026-07-09 20:40:51 +08:00
config_from_model_test.go feat(models): support custom HTTP headers across all remote model calls 2026-04-23 23:36:40 +08:00
image_resolve.go feat(resource): add stable resource registry with LLM-context alias compaction 2026-07-15 17:02:43 +08:00
json_field_extractor.go feat: implement final_answer tool and enhance agent duration tracking 2026-03-09 11:46:56 +08:00
json_field_extractor_test.go feat: implement final_answer tool and enhance agent duration tracking 2026-03-09 11:46:56 +08:00
langfuse_wrapper.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
langfuse_wrapper_test.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
llm_debug.go fix(chat): send one completion-token field per provider (#3082) 2026-09-07 19:16:27 +08:00
llm_debug_wrapper.go feat(logger): implement LLM debug logging functionality 2026-04-16 23:15:02 +08:00
ollama.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
ollama_test.go fix(security): harden outbound requests and file previews 2026-07-02 19:15:46 +08:00
openai_request.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
openai_stream.go feat(agent): stream live +/- progress for sandbox write and edit 2026-09-02 23:36:58 +08:00
prompt_cache.go feat(chat): keep prompt prefixes stable and send provider cache markers 2026-09-03 00:45:52 +08:00
prompt_cache_apply_test.go feat(chat): keep prompt prefixes stable and send provider cache markers 2026-09-03 00:45:52 +08:00
provider.go feat: add LiteLLM as a model provider (#2923) 2026-09-01 12:21:03 +08:00
provider_test.go feat(chat): keep prompt prefixes stable and send provider cache markers 2026-09-03 00:45:52 +08:00
remote_api.go feat(chat): keep prompt prefixes stable and send provider cache markers 2026-09-03 00:45:52 +08:00
remote_api_test.go fix(chat): send one completion-token field per provider (#3082) 2026-09-07 19:16:27 +08:00
sandbox_file_progress.go feat(agent): stream live +/- progress for sandbox write and edit 2026-09-02 23:36:58 +08:00
sandbox_file_progress_test.go feat(agent): stream live +/- progress for sandbox write and edit 2026-09-02 23:36:58 +08:00
special_tokens.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
special_tokens_test.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
sse_reader.go fix: ReadEvent incompatible sse data format 2026-04-20 22:29:38 +08:00
stream_emit.go feat(thinkingControl): implement thinking mode configuration in model editor 2026-06-05 14:56:48 +08:00
stream_raw_dump.go refactor(chat): improve model name sanitization and path validation 2026-06-05 19:35:02 +08:00
thinking.go feat(models): add model test debugger for saved model configs 2026-06-25 17:05:33 +08:00
thinking_test.go feat(models): add model test debugger for saved model configs 2026-06-25 17:05:33 +08:00
timeout_test.go fix(chat): LLM 兜底超时仅在上层无 deadline 时生效,并修复 ChatStream raw HTTP 路径的 cancel 泄漏 2026-05-10 00:16:12 +08:00
transport.go fix(security): harden outbound URL SSRF protection (#2626) 2026-08-10 13:24:14 +08:00
usage.go feat(chat): keep prompt prefixes stable and send provider cache markers 2026-09-03 00:45:52 +08:00
usage_attribution_test.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00