Repository navigation
fix(storage): bound batched clear purges by generation instead of time - #498
Merged
qxcnm merged 1 commit intoOct 9, 2026
Merged
Conversation
A clear marker doomed rows with rowid <= max_rowid AND created_at <= cleared_at. created_at has one-second resolution and SQLite reuses the highest rowid once that row is deleted, so a payload row written in the same second as the clear could match both conditions, be hidden from the detail view and then be deleted by the background purge. Migration 143 adds a nullable generation column to the four purged payload tables and to request_log_payload_purges. Every write stores the clear generation current in its own statement, and a clear stores the generation it bumped to. A clear marker now dooms rows of a lower generation (plus rows written before the migration), which no row written after the clear can have. Markers left by an older version keep the time-based condition, restricted to rows written before the upgrade. Deleting and reading use the same condition. Add regressions for rowid reuse with created_at == cleared_at in every purged table, for legacy markers, and for re-running the migration when the columns already exist.
This was referenced Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
#494 合并后的数据安全跟进,对应 #494 下的评论。
#494 的清理标记用秒级
now_ts()区分清空前后的数据,删除条件是rowid <= max_rowid AND created_at <= cleared_at。SQLite 在最高 rowid 那一行被删除后会复用这个 rowid,所以清空提交后、同一秒内写入的新行可能同时满足两个条件:它会立刻被详情页隐藏,随后被后台分批删除删掉。原有的reused_rowid_after_clear_is_not_purged只覆盖了cleared_at + 5,没有覆盖同一秒。本 PR 改为用清理代次严格区分清空前后的数据,不再依赖时间。
改动
request_log_payload_purges各加一个可空的generation列。只是ALTER TABLE ADD COLUMN,不重写已有数据。迁移中途中断后重跑时,会补齐还没加上的列。(SELECT generation FROM request_log_payload_state WHERE id = 1)),与另一连接上的清空保持原子;预览覆盖写入时也会更新代次。改动范围
主要文件
crates/core/migrations/143_request_log_payload_purge_generation.sql(新)crates/core/src/storage/request_log_payload_purge.rs、request_log_payload_store.rs、request_logs.rs、mod.rscrates/core/src/storage/request_log_payload_purge_tests.rs验证
本机 2 vCPU / 3.6 GB RAM,构建与测试以单核、低优先级、串行方式运行,基于当前 main(
86aa0108)。cargo fmt --all -- --check,WebSocket 依赖 pin 检查cargo test -p codexmanager-core:lib 487(新增 3 个)、集成测试 48,全部通过same_second_rowid_reuse_after_clear_is_not_purged:四张表都先写入一行,清空,删掉这些行让 rowid 被复用,再在与清空同一秒(created_at == cleared_at)写入新行;断言新行确实复用了清空范围内的 rowid、写入时带上了当前代次、清空后立刻可读、分批删除后仍然保留。把判断条件临时改回旧的时间条件后,这个测试会失败(新行在删除前就已不可见)legacy_clear_marker_only_purges_rows_written_before_the_upgrade:升级前留下的无代次标记只删除迁移前写入的行,同秒、同 rowid 范围内的新行保留generation_migration_finishes_when_columns_already_exist:迁移在列已存在时也能完成并记录cargo check --workspace --all-targets,改动文件无新增 warningcargo test --workspace:未执行。本机链接 service 测试二进制会耗尽内存,CI 的Test SQLite storage maintenance步骤会跑 core 全量测试风险与影响面
备注