Skip to content

perf(agent-state): replace repeated (owner, session) strings with integer namespace_id - #7288

Open
M4n5ter wants to merge 1 commit into
perf/7272-recall-dedupfrom
perf/7272-namespace-surrogate-keys
Open

M4n5ter wants to merge 1 commit into
perf/7272-recall-dedupfrom
perf/7272-namespace-surrogate-keys

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Nov 25, 2026 •

Copy link
Copy Markdown
Member
English

PR 4/8 in the #7272 storage optimization stack (stacked on #7287; 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

Introduces an interned agent_namespaces(id, owner, session) dictionary in migration 0061_namespace_surrogate_keys.sql and replaces repeated (owner, session) strings on agent_recall_messages, agent_session_history, agent_usage_calls, and agent_usage_results with compact integer namespace_id foreign keys:

  • Eliminates repeated ~150+ byte owner/session text across primary tables and all secondary B-tree indexes.
  • Incorporates review and rebase fixes across usage/index.rs, session_history/metrics.rs (u.namespace_id = h.namespace_id), and session_history/source_tree/queries.rs (TOOL_IMAGE_BINDINGS, CompletedWorkAt, ImageSourcesAt).
  • Preserves hot-path index plans and verifies migration 0060 invariants in migration_tests and upgrade_compat.rs.

Offline Real-DB Verification (24.34 GiB CoW Clone)

  • m0061: 51.22 s, live size 19,441.60 MiB -> 16,846.02 MiB (-2,595.58 MiB / -2.53 GiB)
  • agent_session_history* (post-stack): 2,308.06 MiB -> 663.75 MiB (-1,644.31 MiB / -71.2%)

Refs #7272

中文

#7272 存储优化系列第 4/8 个 PR(基于 #7287,已同步 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 迁移)

改动内容

在迁移 0061_namespace_surrogate_keys.sql 中引入 agent_namespaces(id, owner, session) 字典表,用整数 namespace_id 替换 agent_recall_messages、agent_session_history、agent_usage_calls 与 agent_usage_results 及所有二级索引中的重复 (owner, session) 字符串,并合入评审与 rebase 修复(含 usage/index.rs、session_history/metrics.rs 及 source_tree/queries.rs 中的 TOOL_IMAGE_BINDINGS / ImageSourcesAt)。

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

  • m0061:51.22 s,活跃数据 19,441.60 MiB -> 16,846.02 MiB(-2,595.58 MiB / -2.53 GiB)
  • 整栈后 agent_session_history*:2,308.06 MiB -> 663.75 MiB(-1,644.31 MiB / -71.2%)

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

This delivers the single cleanest structural win in the stack (agent_session_history down 70.8%, -2.34 GiB on your DB), and the SQL coverage plus removal.rs guarded cleanup of agent_namespaces look thorough.

Issues to address

  1. Multi-hop behavior change in the 0055 migration hook (recall/index.rs:1295-1360): Because rebuild_session moved to namespace_id, migrate_external_effect_observations was rewritten to incrementally insert missing rows instead of rebuilding the affected session's Recall index. Unlike the original v55 hook, this no longer clears agent_recall_gaps for affected sessions, so upgrading <v55 -> v59 behaves differently from <v55 -> v55..v58. Either keep clearing agent_recall_gaps for those sessions inside the v55 hook or verify that gap state stays consistent across the multi-hop path in tests.
  2. Stacked triple rebuild inside one SAVEPOINT: On top of #7286 (DROP COLUMN folded) and #7287 (full agent_recall_messages rebuild), #7288 rebuilds agent_recall_messages a third time plus agent_session_history and 8 child tables inside a single uncommitted SAVEPOINT. Combining the folded removal (and any Recall schema change) into one agent_recall_messages rebuild will save tens of seconds of startup lock and gigabytes of peak WAL.
  3. Minor query-plan note (session_catalog/query.rs:112-117): STALE_CATALOG_SQL uses CROSS JOIN to drive from agent_session_history_historical before filtering agent_namespaces by (owner, session prefix). When historical=1 rows are sparse (0 rows on my local DB) this is fast, but on multi-owner DBs with many imported historical snapshots, driving from agent_namespaces (UNIQUE(owner, session)) and probing agent_session_history_historical(namespace_id, sequence DESC) scales better.

Further improvements to push toward 3–5×

  1. Two remaining near-redundant indexes on agent_session_history (namespace.rs:388-398):
    • agent_session_history_result(namespace_id, fact_id) WHERE result_source=1: fact_id already has a table-wide UNIQUE index and nothing uses INDEXED BY agent_session_history_result.
    • agent_memory_run_termination(run) WHERE kind='run_terminated': agent_session_history_run(run, step, kind) already covers (run, NULL, 'run_terminated') since run_terminated always has step IS NULL.
  2. Next table for namespace_id — agent_usage_calls: On my 1.71 GiB DB, agent_usage_calls + agent_usage_results take 38.6 MiB (24.4 MiB in indexes alone; ~392 MiB on your 23.4 GiB DB), largely because (owner, session) repeats across the table and multiple secondary indexes. A follow-up migration giving agent_usage_calls a nullable namespace_id (to preserve standalone cells where owner IS NULL) will reclaim a large chunk of the remaining "everything else" bucket.
中文

这个 PR 是整套 stack 里收益最扎实的结构性改进(agent_session_history 降幅 70.8%,你那边直省 -2.34 GiB),全仓 SQL 切换和 removal.rs 带守卫的 agent_namespaces 清理都很完整。

需要处理的问题

  1. 已发布 0055 迁移 hook 的跨级行为分歧(recall/index.rs:1295-1360):因为 rebuild_session 改成了 namespace_id 版,migrate_external_effect_observations 被改写为增量插入缺失行而不再重建受影响 session 的 Recall 索引。和原版 v55 hook 不同,这不再清理受影响 session 的 agent_recall_gaps,导致 <v55 -> v59 与 <v55 -> v55..v58 的升级结果不一致。建议在 v55 hook 里保留对受影响 session 的 agent_recall_gaps 清理,并在跨级升级测试中覆盖断言。
  2. 同一外层 SAVEPOINT 下连续三次重建大表:叠在 #7286(DROP COLUMN folded)和 #7287(整表重建 agent_recall_messages)之上,#7288 在同一个未提交 SAVEPOINT 里第三次重建 agent_recall_messages,外加重建 agent_session_history 和 8 张子表。把删 folded(以及 Recall 结构改动)合并进同一次 agent_recall_messages 重建,能省下几十秒启动阻塞和数 GiB 的 WAL 峰值。
  3. 查询计划小提示(session_catalog/query.rs:112-117):STALE_CATALOG_SQL 用 CROSS JOIN 强制先扫 agent_session_history_historical 再按 (owner, session prefix) 过滤 agent_namespaces。historical=1 稀疏时(我本地为 0 行)很快,但在多 owner 导入大量历史快照的库上,先走 agent_namespaces 的 UNIQUE(owner, session) 范围扫描再去探测 agent_session_history_historical(namespace_id, sequence DESC) 扩展性更好。

进一步改进方向(继续压向 3–5×)

  1. agent_session_history 上还有 2 个近冗余索引可以精简(namespace.rs:388-398):
    • agent_session_history_result(namespace_id, fact_id) WHERE result_source=1:fact_id 已有全表 UNIQUE 索引,且全仓没有 INDEXED BY agent_session_history_result。
    • agent_memory_run_termination(run) WHERE kind='run_terminated':agent_session_history_run(run, step, kind) 已经能覆盖 (run, NULL, 'run_terminated')(run_terminated 恒有 step IS NULL)。
  2. 下一张最值得转 namespace_id 的表是 agent_usage_calls:我本地 1.71 GiB 库里 agent_usage_calls + agent_usage_results 占 38.6 MiB(光索引就占 24.4 MiB;你 23.4 GiB 库上约 392 MiB),大头就是主表和多个二级索引重复存 (owner, session)。后续给 agent_usage_calls 加一个可空的 namespace_id(兼容 owner IS NULL 的 standalone cell),能从“其余表”里再抠出一大块空间。

@M4n5ter

M4n5ter commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

@Astro-Han Addressed every item from your review and rebased onto latest main (0060_namespace_surrogate_keys.sql):

  • 0055 multi-hop hook parity: migrate_external_effect_observations now calls rebuild_session(..., MessageLayout::LegacyV55Folded) so agent_recall_gaps is cleared identically across <v55 -> v60+ upgrades.
  • STALE_CATALOG_SQL join order: Rewrote session_catalog/query.rs to drive from agent_namespaces (UNIQUE(owner, session)) and probe agent_session_history_historical(namespace_id, sequence DESC).
  • Dropped both near-redundant indexes: Removed agent_session_history_result and agent_memory_run_termination during the 0060 rebuild in namespace.rs.
  • Migrated agent_usage_calls / agent_usage_results to namespace_id (nullable namespace_id for standalone cells) directly in this PR, and updated session_history/metrics.rs plus main's new TOOL_IMAGE_BINDINGS / ImageSourcesAt queries.
  • Verified on 24.34 GiB offline CoW clone: m0060 completes in 51.22 s (-2.53 GiB), bringing agent_session_history* down -71.2% (2,308.06 -> 663.75 MiB).

@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

Confirmed all previous review items are cleanly resolved on a42de7813c (0055 multi-hop rebuild_session parity, STALE_CATALOG_SQL join order, dropping the 2 redundant indexes, and migrating agent_usage_calls / agent_usage_results to namespace_id). On my 1.71 GiB DB clone, m0060 completed in 3.11 s and brought agent_session_history* down -71.9% (183.93 -> 51.62 MiB).

Two small items left after the rebase from v59 to v60:

  1. Off-by-one version check in products/maka/cli/tests/upgrade_compat.rs:745, 850: Both guards still check versions.contains(&59) (and some assertion messages in namespace.rs:510, 526, 549 and lib.rs:509, 521 still say 59) even though the migration is now registered at version 60. Updating the check to &60 prevents false failures on a database stopped at v59.
  2. Minor query-plan polish (plugins/agent/state/src/session_catalog/query.rs:604-611): STALE_CATALOG_SQL now scans agent_namespaces via UNIQUE(owner, session) in (owner, session) order, but GROUP BY ns.id still triggers USE TEMP B-TREE FOR GROUP BY. Changing it to GROUP BY ns.session (1:1 with ns.id under ns.owner = ?1) eliminates the temp sort.
中文

已核实 a42de7813c 干净解决了上次全部评审项(0055 跨级 rebuild_session 一致性、STALE_CATALOG_SQL 连接顺序、删除 2 个近冗余索引,以及将 agent_usage_calls / agent_usage_results 纳入 namespace_id)。在我本地 1.71 GiB 库克隆上,m0060 耗时仅 3.11 s,agent_session_history* 降幅达 -71.9%(183.93 -> 51.62 MiB)。

rebase 到 v60 后还有两处小问题:

  1. products/maka/cli/tests/upgrade_compat.rs:745, 850 版本号错位:两处仍用 versions.contains(&59)(namespace.rs:510, 526, 549 和 lib.rs:509, 521 的断言文案也还写着 59),但迁移已升至 60。停在 v59 的库会在此误报,改成 &60 即可。
  2. 查询计划微调(plugins/agent/state/src/session_catalog/query.rs:604-611):STALE_CATALOG_SQL 已改为按 UNIQUE(owner, session) 扫描 agent_namespaces,但 GROUP BY ns.id 仍会触发 USE TEMP B-TREE FOR GROUP BY;在 ns.owner = ?1 条件下 ns.session 与 ns.id 一一对应,改成 GROUP BY ns.session 即可省掉临时排序。

@M4n5ter
M4n5ter force-pushed the perf/7272-namespace-surrogate-keys branch from a42de78 to 65d9c6e Compare November 26, 2026 15:36
@M4n5ter

M4n5ter commented Nov 26, 2026

Copy link
Copy Markdown
Member Author
English

@Astro-Han Thanks for catching both! Rebased onto latest main (porting #7315 recall/query.rs and #7317 source_tree/data_source.rs / store/fallback.rs queries onto namespace_id) and updated in 65d9c6ea65:

  1. upgrade_compat.rs version guards: Updated all three namespace_id / agent_namespaces guards from &59 to &60, and cleaned up stale "v59" strings in namespace.rs.
  2. STALE_CATALOG_SQL GROUP BY: Switched GROUP BY ns.id to GROUP BY ns.session in session_catalog/query.rs, eliminating USE TEMP B-TREE FOR GROUP BY while staying semantically equivalent under fixed ns.owner = ?1 and UNIQUE(owner, session).
  3. Additional audit fix (legacy queue-source validation): Hardened index_sources_with_layout in recall/index.rs so non-Current layout rebuilds fail closed with the same Event::Consumed, ownership, DeliveryTarget, and agent_queue_content_pins custody checks as session_history/sql.rs.
  4. fix(agent,workbench): resolve transcript anchors for session search jumps #7315 / fix(agent): prevent superseded compaction re-adoption and unbrick crossed timing reads #7317 integration: Ported all newly merged agent_session_history / agent_recall_messages queries (including data_source.rs::successful_closure) to namespace_id and verified with recall_jump_anchors_verify_for_executor_deltas_and_hidden_facts and historical_crossed_compaction_checkpoint_remains_readable_in_live_and_snapshot.
中文

@Astro-Han 感谢指正!已 rebase 到最新 main(将 #7315 与 #7317 新增查询一并迁移到 namespace_id),更新在 65d9c6ea65:

  1. upgrade_compat.rs 版本号守卫:三处 namespace_id / agent_namespaces 校验门槛全部从 &59 改为 &60,顺手清掉 namespace.rs 里过期的 "v59" 文案。
  2. STALE_CATALOG_SQL GROUP BY:session_catalog/query.rs 改为 GROUP BY ns.session,在 ns.owner = ?1 与 UNIQUE(owner, session) 约束下语义等价且消除 USE TEMP B-TREE FOR GROUP BY。
  3. 顺手修掉历史 queue-source 重建校验:recall/index.rs 的历史 layout 重建支路改为与 session_history/sql.rs 一致的 fail-closed 归属与 agent_queue_content_pins custody 校验,不再静默跳过。
  4. fix(agent,workbench): resolve transcript anchors for session search jumps #7315 / fix(agent): prevent superseded compaction re-adoption and unbrick crossed timing reads #7317 集成:已将合并进 main 的新查询(含 data_source.rs::successful_closure)全部切到 namespace_id,并通过对应集成回归测试。

@M4n5ter
M4n5ter force-pushed the perf/7272-namespace-surrogate-keys branch from 65d9c6e to 578a073 Compare November 27, 2026 02:56
@github-actions github-actions Bot added effort/XXL Over 2500 readable lines and removed effort/XL Under 2500 readable lines labels Nov 27, 2026

@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

Confirmed upgrade_compat.rs guards (&61), STALE_CATALOG_SQL (GROUP BY ns.session), and the 0057_pinned_memory.sql / #7315 / #7317 port onto namespace_id all check out on 578a0733a6.

Two blocking cold-upgrade issues in the newly added non-Current branch of index_sources_with_layout (plugins/agent/state/src/recall/index.rs:2597-2622):

  1. Cold migration crash on pre-#7115 RequestSpec rows (recall/index.rs:2615-2616):

    • I ran the full upgrade on an offline CoW clone of my 1.71 GiB database (max_ver=55), and migration 0060 aborted immediately with:
      Json(Error("unknown field systemPrompt, expected one of surface, dynamicContext, toolCatalogDigest, providerOptions, maxOutputTokens"))
    • My DB has 1,049 agent_request_specs rows from before #7115 (Nov 21) where composition still has systemPrompt/tools instead of surface, and RequestComposition carries #[serde(deny_unknown_fields)]. Because this branch only checks request_spec.steering.get(position) == Some(&frozen.source), deserializing the entire RequestSpec fails closed on historical DBs unnecessarily.
    • Fix: deserialize only a lightweight local struct (e.g. #[derive(Deserialize)] struct RequestSteeringOnly { #[serde(default)] steering: Vec<ContentRef> } without deny_unknown_fields) to verify steering.get(position) == Some(&frozen.source).
  2. frozen.spec != *spec breaks cold upgrades of v57+ (0057_pinned_memory) DBs with queued runs (recall/index.rs:2598):

    • #7184 populates RunSpec.memory_projection / updates memory_admission on admission after enqueueing, which is why plugins/agent/state/src/session_history/sql.rs:1087 checks !frozen.matches_run_input(spec) instead of strict equality. Any v57+ DB containing such a queued run will abort during migration 0060 with "original Run input differs from consumed request".
    • Fix: change if frozen.spec != *spec to if !frozen.matches_run_input(spec.clone()).

Minor cleanup: namespace.rs still has leftover "v59"/"v60" strings in temp table names, error text, and test assertions (actual migration is 61), and recall.rs:46-47 has a duplicate #[cfg(test)].

中文

已核验 578a0733a6 上 upgrade_compat.rs 版本守卫(&61)、STALE_CATALOG_SQL(GROUP BY ns.session)以及 0057_pinned_memory.sql / #7315 / #7317 向 namespace_id 的移植均无问题。

三轮在 index_sources_with_layout 非 Current 分支(plugins/agent/state/src/recall/index.rs:2597-2622)新增的校验引入了两个会阻断冷升级的问题:

  1. 遇到 #7115 前的历史 RequestSpec 直接导致冷迁移崩溃(recall/index.rs:2615-2616):

    • 在我本地 1.71 GiB 真实库副本(max_ver=55)上跑完整升级时,m0060 直接报错中止:Json(Error("unknown field systemPrompt, expected one of surface, dynamicContext, toolCatalogDigest, providerOptions, maxOutputTokens"))。
    • 库里有 1,049 条 #7115(11-21)之前的 agent_request_specs 记录,其 composition 仍是旧字段 systemPrompt/tools,而 RequestComposition 带有 #[serde(deny_unknown_fields)]。这里实际只需要核对 steering.get(position) == Some(&frozen.source)。
    • 修复:改为轻量结构体(如不带 deny_unknown_fields 的 struct RequestSteeringOnly { #[serde(default)] steering: Vec<ContentRef> })局部反序列化。
  2. frozen.spec != *spec 会阻断含 pinned-memory 排队 Run 的 v57+ 数据库冷升级(recall/index.rs:2598):

    • #7184 在 follow-up 入队后、接纳 Run 时才补写 memory_projection / memory_admission,因此 session_history/sql.rs:1087 专门用 !frozen.matches_run_input(spec) 而非直接判等。v57+ 库含此类 Run 时会在 m0060 报错 "original Run input differs from consumed request" 中止迁移。
    • 修复:将 if frozen.spec != *spec 改为 if !frozen.matches_run_input(spec.clone())。

小清理:namespace.rs 里仍有若干残留的 v59/v60 临时表名和报错/测试文案(当前迁移号为 61);recall.rs:46-47 有重复的 #[cfg(test)]。

@M4n5ter
M4n5ter force-pushed the perf/7272-namespace-surrogate-keys branch from 578a073 to 1a60f8d Compare November 27, 2026 06:29
@M4n5ter

M4n5ter commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

@Astro-Han Addressed both cold-upgrade blockers and the minor cleanup in 1a60f8d291:

  1. Pre-#7115 RequestSpec compatibility: the non-Current branch of index_sources_with_layout (recall/index.rs) now deserializes only #[serde(default)] steering: Vec<ContentRef> instead of full RequestSpec.
  2. #7184 v57+ pinned-memory queued runs: switched frozen.spec != *spec to !frozen.matches_run_input(spec.clone()), matching session_history/sql.rs.
  3. Cleanup & regression coverage: cleaned up the remaining v59/v60 temp table names and error/test strings in namespace.rs (and removed the duplicate #[cfg(test)] in recall.rs), and added cold_upgrade_handles_legacy_request_spec_and_pinned_memory_queued_run.

Verified locally with maka-agent-state --lib, node scripts/ci.mjs check migrations, cargo fmt --check, and cargo test -p maka-cli --test upgrade_compat.

@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

Confirmed both cold-upgrade blockers are fixed on 1a60f8d291 (RequestSteeringOnly for pre-#7115 RequestSpec rows and !frozen.matches_run_input(spec.clone()) for #7184 v57+ queued runs), and the new agent_recall_external_segments table is cleanly migrated to namespace_id across namespace.rs, store/removal.rs, and all recall queries.

I re-ran the full v55 -> v65 upgrade + background recompression end-to-end on a fresh CoW clone of my real 1.71 GiB database:

  • Migrations v55 -> v65 completed cleanly in 75.68 s (debug), recompression in 13.45 s (179,691 rows, -345.89 MiB raw payload), incremental_vacuum -> 740.38 MiB, VACUUM -> 667 MiB, with PRAGMA quick_check = ok, foreign_key_check = 0, and FTS5 integrity-check = OK.

One remaining blocker inherited from #7287:

  • plugins/agent/state/src/session_history.rs:153-159: ExecutorTranscript::mask_pieces still queries AND m.fact_id = s.fact on agent_recall_messages (which has no fact_id column). Once fixed in #7287, please port the JOIN agent_session_history h ON h.namespace_id = m.namespace_id AND h.sequence = m.sequence AND h.fact_id = s.fact update here as well.
中文

已确认 1a60f8d291 修好了两个冷升级阻塞问题(RequestSteeringOnly 兼容 #7115 前历史 RequestSpec,!frozen.matches_run_input(spec.clone()) 兼容 #7184 v57+ 排队 Run),新增的 agent_recall_external_segments 也完整迁移到了 namespace_id(namespace.rs、store/removal.rs 及全部 Recall 查询无遗漏)。

也在我本地 1.71 GiB 真实库新副本上重跑通了完整 v55 → v65 迁移与重压:

  • 迁移 75.68 s(debug)、后台重压 13.45 s(179,691 行,省 345.89 MiB 原始载荷)、incremental_vacuum 后 740.38 MiB、VACUUM 后 667 MiB,quick_check = ok、foreign_key_check = 0、FTS5 integrity-check = OK。

仅剩一处从 #7287 承接的阻塞项:

  • plugins/agent/state/src/session_history.rs:153-159:ExecutorTranscript::mask_pieces 同样查询了 agent_recall_messages 不存在的 m.fact_id 列,随 #7287 修复后请同步改为 JOIN agent_session_history h ON h.namespace_id = m.namespace_id AND h.sequence = m.sequence AND h.fact_id = s.fact。

…eger namespace_id

Introduce an interned agent_namespaces(id, owner, session) dictionary in
migration 0061 and replace repeated owner/session text columns on
agent_recall_messages, agent_session_history, agent_usage_calls, and
agent_usage_results with integer namespace_id foreign keys:
- Preserve all hot-path query plans across Recall, SessionHistory (including
  LivePositions, CompletedWorkAt, TOOL_IMAGE_BINDINGS / ImageSourcesAt, and
  metrics), and Usage queries.
- Eliminate ~150+ byte repeated owner/session strings from primary tables and
  secondary B-tree indexes.
- Verify migration 0061 invariants and query equivalence in migration_tests
  and upgrade_compat.rs.

Refs #7272

Generated-by: Antigravity
@M4n5ter
M4n5ter force-pushed the perf/7272-namespace-surrogate-keys branch from 1a60f8d to 5e3ca94 Compare November 27, 2026 09:30
@M4n5ter

M4n5ter commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

@Astro-Han Rebased onto #7287 (b49161c978) in 5e3ca94f4c and ported all three updates to namespace_id:

  • ExecutorTranscript::mask_pieces (session_history.rs): now joins agent_session_history h -> agent_namespaces ns -> agent_recall_messages m on namespace_id, ports the live straddling-cut query to namespace_id, and uses real table schemas in executor_tests.
  • unsealed_external_segment_headers (recall/index.rs): ported the pinned UNION ALL (agent_recall_external_segments_open + agent_recall_external_segments_sealed_seq) to namespace_id.
  • Verified locally with maka-agent-state --lib (97/97 on #7288, including the real-DB executor_view(..., true) and straddling-cut redaction checks), cargo fmt --check, and cargo test -p maka-cli --test upgrade_compat.

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/XXL Over 2500 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants