Skip to content

fix(checkpoint): recalibrate counters after cleanup (#157) - #667

Draft
Guichena wants to merge 2 commits into
TencentCloud:mainfrom
Guichena:fix/checkpoint-recalibrate-157
Draft

fix(checkpoint): recalibrate counters after cleanup (#157)#667
Guichena wants to merge 2 commits into
TencentCloud:mainfrom
Guichena:fix/checkpoint-recalibrate-157

Conversation

@Guichena

Copy link
Copy Markdown

Description | 描述

修复 Issue #157 的 checkpoint 计数漂移:基于健康存储聚合计数进行启动校准,清理时使用原子增量扣减,并在存储不可用时对 JSONL 做受限恢复。

Related Issue | 关联 Issue

Fixes #157

Change Type | 修改类型

  • Bug fix | Bug 修复
  • Code optimization | 代码优化

Self-test Checklist | 自测清单

  • Verified locally | 本地验证通过
  • No existing features affected | 无影响现有功能

Additional Notes | 其他说明

  • SQLite/TencentDB 提供显式、可失败的聚合计数接口;健康存储计数失败时保留现有 checkpoint,避免不完整 JSONL 覆盖有效值。
  • 清理按实际删除的 L0/L1 记录原子扣减,并按 persona 时间水位精确扣减;L0 使用消息记录单位。
  • JSONL 仅在存储降级/缺失时作为恢复源:新 checkpoint 可重建,已有 checkpoint 只允许向下校准,避免 append-only 事件重复计数。
  • 保留 session cursor;pipeline 只修复 scenes_processed 的单调性。

Verification

  • npm test(78 tests)
  • npm run build
  • npm pack --dry-run

Guichena added 2 commits July 31, 2026 23:03
…entCloud#157)

l0_conversations_count and total_memories_extracted were increment-only:
nothing ever decremented them when records were removed by memory-cleaner
or manual JSONL pruning, so after any cleanup the counters permanently
overstated reality and skewed L2/L3 persona thresholds.

Add CheckpointManager.recalibrate(source?) which reconciles both counters
to live store counts (countL0/countL1), falling back to JSONL shard line
counts in degraded mode. It clamps NaN/negative counts, clamps
memories_since_last_persona down to the actual L1 total, preserves
unrelated fields, reuses the per-file lock, and is idempotent.

Wire it in at two non-fatal points:
- end of each LocalMemoryCleaner.runOnce, right after deletions;
- gateway startup in TdaiCore.ensureSchedulerStarted, before restoring
  pipeline state, so thresholds resume from accurate counts.

Add regression tests covering downward/upward correction, idempotence,
field preservation, memories clamping, JSONL fallback, store-source
failure fallback, and NaN/negative clamping.
@Maxwell-Code07

Copy link
Copy Markdown
Collaborator

Thank you for your interest and contributions! We will review your code and get back to you as soon as possible!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants