Skip to content

fix(agent-runtime): persist evidence ledger - #2426

Open
zvzuola wants to merge 1 commit into
GCWing:mainfrom
zvzuola:codex/persist-evidence-ledger
Open

fix(agent-runtime): persist evidence ledger#2426
zvzuola wants to merge 1 commit into
GCWing:mainfrom
zvzuola:codex/persist-evidence-ledger

Conversation

@zvzuola

@zvzuola zvzuola commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Persist EvidenceLedger events across session unloads, restarts, and restores to prevent long-running tasks from losing checkpoints, compression evidence, and partial subagent results.

The ledger is stored as a versioned evidence-ledger.json sidecar under the resolved session directory.

Type and Areas

Type: Bug fix / reliability

Areas: Rust core, Agent Runtime, session persistence, remote workspace

Motivation / Impact

Previously, EvidenceLedger existed only in process memory. When a session was unloaded or the host restarted, the ledger was lost. This could cause long-running tasks to lose important execution evidence and drift away from their original objective.

This change:

  • Persists durable session evidence to a versioned JSON sidecar.
  • Uses session-level serialization, cross-process file locking, and strict atomic replacement.
  • Makes event appends idempotent by event_id.
  • Rejects conflicting payloads for duplicate event IDs.
  • Loads and validates the ledger before publishing restored runtime state.
  • Preserves the original sidecar when the data is corrupt, from a newer schema, or belongs to another session.
  • Keeps legacy payloads readable through additive defaults and unknown-enum fallbacks.
  • Publishes evidence to in-memory state only after successful persistence.
  • Makes checkpoint persistence fail closed for mutating Bash, file, and Git tools.
  • Persists partial subagent timeout evidence while retaining already-produced partial results if persistence fails.
  • Uses the resolved remote session mirror for remote workspaces instead of reusing controller-side paths.

No new user-facing strings, locale changes, or UI changes are required.

Verification

  • cargo check --locked -p bitfun-agent-runtime --no-default-features --features agent-runtime — passed.
  • cargo test --locked -p bitfun-agent-runtime --no-default-features --features agent-runtime --lib evidence_ledger — 11 passed.
  • cargo test --locked -p bitfun-core --no-default-features --features agent-runtime --lib evidence_ledger — 4 passed.
  • cargo test --locked -p bitfun-core --no-default-features --features agent-runtime --lib concurrent_evidence_appends_keep_disk_and_memory_complete — passed.
  • cargo test --locked -p bitfun-core --no-default-features --features agent-runtime,remote-workspace --lib remote_workspace_evidence_uses_the_resolved_session_mirror — passed.
  • cargo check --locked -p bitfun-core --no-default-features --features product-full — passed.
  • pnpm run check:core-boundaries — passed.
  • pnpm run fmt:rs — passed.
  • git diff --check — passed.

The remote workspace scenario was exercised. Remote control, Peer Device Mode, and Detached Dispatch protocols were not changed by this PR and were not modified in the tests.

Reviewer Notes

  • The persistence format is currently schema version 1.
  • The sidecar is created only for durable sessions; transient sessions retain their existing in-memory behavior.
  • Restore is fail-closed for invalid or incompatible ledger data and does not delete or overwrite the original sidecar.
  • Concurrent appends are serialized in-process and protected across processes by the JSON file lock.
  • Existing repository feature-conditional warnings remain, but all targeted tests and checks pass.

Checklist

  • This PR is focused and does not include secrets, temporary prompts, generated scratch files, or unrelated artifacts.
  • Relevant verification is recorded above.
  • User-facing strings, docs, and locales are updated where applicable.

@limityan limityan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

需要修改:回退会话后,已撤销的证据会在重启后重新出现

发生场景

  1. 某个会话 turn 产生了文件修改前的 checkpoint、失败命令,或 subagent 的部分结果。
  2. 用户把会话回退到这个 turn 之前,表示这些后续执行已经被撤销。
  3. 用户关闭并重新打开会话,或应用重启后恢复该会话。

当前行为

恢复代码会正确隐藏回退点之后的对话 turn,但随后仍会从 evidence-ledger.json 加载全部历史证据。永久提交回退时,代码也只删除对话 turn、上下文快照和压缩产物,没有同步删除被回退 turn 对应的 ledger 记录。

造成的后果

重启后,已经被用户撤销的文件、失败命令和 subagent 结果会再次进入上下文压缩与 Deep Review。系统会把旧工作误认为当前事实,可能让 Agent 继续处理已经撤销的文件或问题。换句话说:界面中的会话已经回退,但 Agent 使用的证据没有一起回退。

修复要求

  • staged revert 和永久回退都应按仍然保留的 turn ID,同步裁剪磁盘 sidecar 与内存 ledger。
  • 恢复会话时,只应发布当前可见 turn 对应的证据。
  • 增加回归测试:先产生 evidence,再 rollback,随后 unload/restore,确认 compression contract 不再包含被撤销 turn 的证据。

该问题会破坏本 PR 要保证的恢复一致性,因此修复前建议不要合并。

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