Skip to content

perf(agent-state): drop stored folded column from agent_recall_messages - #7286

Open
M4n5ter wants to merge 1 commit into
perf/7272-drop-duplicate-indexesfrom
perf/7272-drop-recall-folded
Open

M4n5ter wants to merge 1 commit into
perf/7272-drop-duplicate-indexesfrom
perf/7272-drop-recall-folded

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Nov 25, 2026 •

Copy link
Copy Markdown
Member
English

PR 2/8 in the #7272 storage optimization stack (stacked on #7285; rebased onto main after 0057_pinned_memory.sql).

Stack (#7272)

  1. perf(agent-state): drop redundant duplicate and prefix indexes on recall, runs, and interactions #7285 — Drop 6 redundant duplicate/prefix B-tree indexes (m0058)
  2. perf(agent-state): drop stored folded column from agent_recall_messages #7286 — Drop agent_recall_messages.folded (m0059)
  3. perf(agent-state): deduplicate Recall indexing at source and coalesce external deltas #7287 — Deduplicate Recall bodies & FTS5 postings at source + 2-tier immutable external delta sealing (m0060)
  4. perf(agent-state): replace repeated (owner, session) strings with integer namespace_id #7288 — Replace repeated (owner, session) strings with namespace_id (m0061)
  5. perf(event-log): add tiered/dictionary zstd compression and background recompression for contents #7289 — Tiered/dictionary zstd compression & background recompression for contents (m0062–m0063)
  6. perf(agent): deduplicate step metadata, tool contracts, external descriptors, and return payloads #7290 — Structural dedup for step metadata, tool contracts, external descriptors & returns
  7. perf(storage): compress application tables, agent_cells bodies, and intern usage JSON blobs #7318 — Compress application_* & agent_cells, intern agent_usage_calls JSON blobs (m0064–m0065)
  8. perf(agent): externalize and deduplicate media in frozen prompts (Layout v3) #7341 — Externalize & CAS-deduplicate media in frozen prompts (Layout v3, zero SQL migration)

Summary

Removes the redundant NFC/lowercased mirror column folded from agent_recall_messages in migration 0059_drop_recall_folded.sql:

  • Drops agent_recall_messages.folded via ALTER TABLE DROP COLUMN.
  • Folds already-masked message bodies on demand in recall/query.rs, with ASCII and is_nfc_quick fast paths in recall::fold() to avoid regressions on high-frequency and CJK queries.
  • Preserves migration 0055's historical data hook writing the v55 folded column for multi-hop upgrades while runtime indexing targets the current schema with zero legacy read branches.

Offline Real-DB Verification (24.34 GiB CoW Clone)

  • m0059: 70.78 s, live size 24,669.68 MiB -> 22,715.51 MiB (-1,954.17 MiB / -1.91 GiB)

Refs #7272

中文

#7272 存储优化系列第 2/8 个 PR(基于 #7285,已同步 rebase 到 main 0056 之后)。

系列栈(#7272)

  1. perf(agent-state): drop redundant duplicate and prefix indexes on recall, runs, and interactions #7285 — 删除 6 个重复/前缀冗余 B-tree 索引(m0058)
  2. perf(agent-state): drop stored folded column from agent_recall_messages #7286 — 删除 agent_recall_messages.folded 冗余列(m0059)
  3. perf(agent-state): deduplicate Recall indexing at source and coalesce external deltas #7287 — 从源头对 Recall 正文与 FTS5 postings 联合去重 + 两段不可变外部 delta 封口(m0060)
  4. perf(agent-state): replace repeated (owner, session) strings with integer namespace_id #7288 — 用整数 namespace_id 替换重复 (owner, session) 字符串(m0061)
  5. perf(event-log): add tiered/dictionary zstd compression and background recompression for contents #7289 — contents 分档/静态字典 zstd 压缩与后台存量重压(m0062–m0063)
  6. perf(agent): deduplicate step metadata, tool contracts, external descriptors, and return payloads #7290 — 新写 durable facts 结构去重(tool contracts、external descriptors/instructions、returns)
  7. perf(storage): compress application tables, agent_cells bodies, and intern usage JSON blobs #7318 — 压缩 application_* 与 agent_cells 并对 agent_usage_calls JSON 字典驻留(m0064–m0065)
  8. perf(agent): externalize and deduplicate media in frozen prompts (Layout v3) #7341 — Frozen prompt 媒体外置与 CAS 去重(Layout v3,零 SQL 迁移)

改动内容

在迁移 0059_drop_recall_folded.sql 中移除 agent_recall_messages.folded 冗余镜像列,改为在 recall/query.rs 按需折叠(recall::fold() 带 ASCII 与 is_nfc_quick 快路径),同时保持多跳升级经过 0055 时的数据钩子兼容,运行时零历史兼容分支。

真实库离线实测(24.34 GiB CoW 克隆)

  • m0059:70.78 s,活跃数据 24,669.68 MiB -> 22,715.51 MiB(-1,954.17 MiB / -1.91 GiB)

Refs #7272

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English

The fold() fast paths (is_ascii() and is_nfc_quick(s.chars()) == IsNormalized::Yes) are byte-for-byte equivalent to s.nfc().collect::<String>().to_lowercase() under UAX #15, and LegacyV55Folded keeps multi-hop upgrades from <v55 safe. Dropping folded also reduces SQLite page reads during search, offsetting on-demand folding.

One stack-level suggestion (non-blocking for this PR on its own): if this ships together with #7288, consider folding the folded column removal into #7288's agent_recall_messages table rebuild rather than running a separate ALTER TABLE ... DROP COLUMN ahead of it. Because migration::run wraps all pending migrations in a single outer SAVEPOINT, running 0057 + 0058 + 0059 back-to-back currently rewrites agent_recall_messages three times inside one uncommitted transaction (~221 s and multi-GiB WAL peak on a 20+ GiB database).

中文

fold() 新增的两个快速路径(is_ascii() 与 is_nfc_quick(s.chars()) == IsNormalized::Yes)按 UAX #15 与原实现 s.nfc().collect::<String>().to_lowercase() 逐字节等价,LegacyV55Folded 也保证了 <v55 跨级升级安全。删掉 folded 后搜索时减少了一倍 SQLite 页读取,足以抵消按需折叠的 CPU 开销。

顺带一个整个 stack 层面的建议(不阻塞本 PR 单独看):如果准备和 #7288 同批上线,可以考虑把删除 folded 列直接并入 #7288 的 agent_recall_messages 重建里,省掉前置的 ALTER TABLE ... DROP COLUMN。因为 migration::run 把所有待跑迁移包在同一个外层 SAVEPOINT 中,0057 + 0058 + 0059 连续跑会在同一个未提交事务里把 agent_recall_messages 整表重写三次(20+ GiB 库上合计约 221 秒,WAL 峰值可达十来 GiB)。

@M4n5ter

M4n5ter commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

@Astro-Han Thanks for verifying the UAX #15 equivalence and multi-hop LegacyV55Folded path! Rebased onto latest main (0058_drop_recall_folded.sql). On the stack-level rewrite/WAL cost: we kept 0058 isolated for review clarity and eliminated the primary SAVEPOINT bottleneck inside #7287 (m0059 now defers FTS5 writes to a single bulk INSERT ... SELECT pass instead of triggering per-row fts5SavepointMethod flushes). On a 24.34 GiB offline CoW clone, m0058 runs in 70.78 s (-1.91 GiB) and m0060 in 51.22 s.

Remove the redundant NFC/lowercased mirror column folded from
agent_recall_messages in migration 0059:
- Drop agent_recall_messages.folded via ALTER TABLE DROP COLUMN.
- Fold the already-masked message body on demand in recall/query.rs, with
  ASCII and is_nfc_quick fast paths in recall::fold() to avoid 3-7x latency
  regressions on high-frequency and CJK queries.
- Keep migration 0055's historical data hook (migrate_external_effect_observations)
  writing the v55 folded column so multi-hop upgrades from <v55 succeed cleanly
  before migration 0059 drops it, while runtime indexing targets the current
  schema without legacy branches.
- Add fold equivalence unit tests and verify the dropped column in
  migration_tests and upgrade_compat.rs.

Refs #7272

Generated-by: Antigravity
@M4n5ter
M4n5ter force-pushed the perf/7272-drop-recall-folded branch from 02f4a3c to 7e35a52 Compare November 27, 2026 06:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/M Under 500 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants