WeKnora/docs
lyingbug 03d9d66a1f
feat(agent): size agent history by the window and calibrate token estimates (#3413)
* feat(agent): size agent history by the window and calibrate token estimates

Agent history was capped at history_turns (default 5) because compaction
could not survive a turn. With checkpoints persisted, load history by
tokens instead, and let compaction bound the session.

Budget. The loader drops the oldest turns that do not fit, and what it
drops is lost: neither replayed nor summarized. A budget equal to the
compaction threshold (the earlier attempt, since reverted) let the loader
trim first, so whenever one turn was larger than the system prompt plus
the new question the request never crossed the threshold; nothing was
summarized and the session became a sliding window with no checkpoint.
agent.HistoryTokenBudget is now the whole window: overflow reaches the
first round's compaction, which summarizes it into a new checkpoint.
A turn-by-turn regression test drives the real loader, budget and
compactor and fails as soon as a stored turn after the checkpoint is
missing from the loaded history; restoring the old budget fails it.

Summarizer input. History can now reach the window, so the part being
summarized can outgrow one request. An overflowing request degrades to a
raw archive that is never persisted, so every later turn would fail the
same way. Prepare keeps the newest messages that fit, the prompt states
how many were left out, and file paths still come from all of them.

Calibration. cl100k counts Chinese at about a token a character where
Qwen and DeepSeek spend about 0.6. The estimator now carries a scale
(clamped to 0.5-1.5, text only), learned from consecutive requests in a
turn: they share tools, system prompt and earlier messages, so the
provider's growth between them prices exactly the appended messages,
without mixing in how a provider renders tool schemas. The turn's scale
is stored on its usage (context_token_scale); the next turn's loader
prices history with it and hands it to the engine. Loading, the first
compaction check and compaction itself share one estimator, so they
agree; calibrating only the trigger would let the loader drop turns
again, which the regression test covers.

Also: history is priced as the engine sends it (redacted KB/Wiki results
cost one line); a checkpoint older than the rows read is matched by the
assistant rows' (created_at, id) order; a read ending on a turn's
question keeps that turn; a compaction discarded for freeing under 5%
still persists its checkpoint; a checkpoint write matching no row is an
error; a partial index backs the newest-checkpoint lookup. Agent mode no
longer reads history_turns: the field leaves AgentConfig, the editor
hides it in smart-reasoning mode, and quick-answer keeps its turn cap.

* fix(agent): address review of window-sized history loading

Summarization. The summarizer call had a fixed 60s total timeout. With
history loaded up to the window, a request could carry ~175k tokens and be
cut off while prefilling or writing normally; the degraded result left no
checkpoint, so the next turn reloaded the same history and failed the same
way. Summarization now streams and is cancelled only after no output for the
engine's stall timeout (120s by default, LLMCallTimeout overrides), with the
overall ceiling left to the transport, as for the engine's own rounds and as
in Codex and Pi. A stopped turn is not retried. When the summarizer still
fails, the raw archive is persisted as a checkpoint marked degraded so the
loop is broken, and the next compaction refines it as the previous summary.
The archive is held to the summary budget, newest messages first, so a
degraded compaction always frees room.

Loading. The loader priced turns as sent but returned and held them as
stored, so wiki pages stored in full and sent as one line could pile up in
memory. It now returns the as-sent history and releases each turn's stored
rows once replayed, keeping about a page of rows at a time.

Index. The partial checkpoint index is replaced by a composite
(session_id, created_at DESC, id DESC) index serving the backwards page,
the newest-checkpoint lookup and the recent-messages reads. It is built with
CREATE INDEX CONCURRENTLY so writes to messages are not blocked; the
migration holds that single statement, since a multi-statement query runs as
an implicit transaction. A PostgreSQL integration test, skipped without
WEKNORA_MIGRATION_TEST_POSTGRES_DSN, applies the chain, runs 000106 down and
up through golang-migrate, and checks both queries use the index without a
sort; the SQLite migration test checks the same plans.

Calibration. It no longer waits for a total token count, and a usage with
only a scale is persisted. A turn that measures nothing carries forward the
scale it started from, so the newest turn always has the latest one.
2026-09-18 22:51:31 +08:00
..
api fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
assets fix(milvus): support multilingual BM25 retrieval (#3235) 2026-09-14 19:18:24 +08:00
cloud-image docs(cloud-image): add cloud-agnostic image packaging scripts 2026-05-11 12:25:19 +08:00
dev fix: validate vector store connection addresses against SSRF policy 2026-06-04 15:26:56 +08:00
images fix(browser): align BrowserSkill 0.3.0 and repair task lifecycle 2026-09-17 11:54:12 +08:00
plans feat(faq): filter entries by enabled status (#3157) 2026-09-10 14:42:59 +08:00
poc/docker-sandbox feat(sandbox): Docker 后端支持会话级长驻容器与远程守护进程 2026-08-24 12:22:58 +08:00
wiki feat(datasource): add DingTalk Docs integration with safe sync and localization (#2313) 2026-09-14 19:53:29 +08:00
zh refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
BUILTIN_MCP_SERVICES.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
BUILTIN_MODELS.md feat(agent): keep long sandbox turns from truncating, overflowing, or rewriting files 2026-09-02 22:09:09 +08:00
CHUNKING.md fix(chunking): post-review polish — error surfacing + doc accuracy 2026-05-06 17:17:07 +08:00
IM集成开发文档.md feat(im): support reverse proxy for feishu websocket long connection (#2550) 2026-08-12 16:18:27 +08:00
KnowledgeGraph.md docs: fix 登陆 typo to 登录 2026-08-07 17:04:49 +08:00
LITE.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
Langfuse集成.md feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
MCP功能使用说明.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
OIDC认证调用流程.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
QA.md feat(website): add product homepage and unified docs deployment (#3297) 2026-09-16 11:58:37 +08:00
RBAC说明.md docs: clarify workspace owner cardinality 2026-07-23 16:13:35 +08:00
ROADMAP.md feat(roadmap): update progress on document parsing and IM integration 2026-03-31 20:22:34 +08:00
agent-prompt-assembly.md feat(browser): polish connection settings, open tasks in a new window, and streamline prompts (#3193) 2026-09-11 21:28:58 +08:00
agent-skills.md feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
agent-tools-design.md feat(sandbox): run sandboxed execs as root by default (#3067) 2026-09-07 13:03:35 +08:00
browser-skill-integration.md fix(browser): align BrowserSkill 0.3.0 and repair task lifecycle 2026-09-17 11:54:12 +08:00
browser-skill-production.md fix(browser): align BrowserSkill 0.3.0 and repair task lifecycle 2026-09-17 11:54:12 +08:00
chat-steering.md fix(chat): preserve steering continuity and composer focus 2026-09-09 20:15:23 +08:00
client-integration-upgrade-notes.md fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
code-slimming-audit.md refactor(frontend): consolidate knowledge tag filters and remove stale styles 2026-09-17 16:46:27 +08:00
docs.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
embed-secure-mode.md fix(embed): enforce host origin allowlists on framed pages 2026-09-14 18:03:57 +08:00
embed-subdomain.md fix(embed): enforce host origin allowlists on framed pages 2026-09-14 18:03:57 +08:00
mcp-tool-directory.md feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
migration-troubleshooting.md feat(system-info): surface DB migration errors with troubleshooting links 2026-05-14 16:34:50 +08:00
paradedb-upgrade.md fix(docker): upgrade ParadeDB for CPUs without AVX2 2026-09-17 17:17:04 +08:00
sandbox-cluster.md feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox-desktop.md feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox-docker-backend.md feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox-protocol.md feat(sandbox): run sandboxed execs as root by default (#3067) 2026-09-07 13:03:35 +08:00
swagger.json feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
swagger.yaml feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
swagger_contract_test.go feat(faq): filter entries by enabled status (#3157) 2026-09-10 14:42:59 +08:00
worker-pool-governance.md feat(worker): replace aggregate asynq concurrency with per-pool governance 2026-07-13 15:51:06 +08:00
使用其他向量数据库.md feat(retriever): configure Tencent VectorDB replicas via env 2026-06-22 20:21:25 +08:00
共享空间说明.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
开发指南.md fix: update dev script checks and Air docs 2026-07-01 11:52:42 +08:00
开启知识图谱功能.md feat: Update VLM configuration in knowledge base and API documentation 2025-11-20 20:08:24 +08:00
快速开发模式说明.md docs: update Air installation command from cosmtrek/air to air-verse/air 2026-02-04 19:27:44 +08:00
数据源导入开发文档.md feat(datasource): add DingTalk Docs integration with safe sync and localization (#2313) 2026-09-14 19:53:29 +08:00
日志配置.md refactor(logger): support LOG_FORMAT template and harden level coloring 2026-05-22 20:31:54 +08:00
添加新的网络搜索引擎.md feat(web-search): refactor web search provider 2026-03-31 20:22:34 +08:00