Skip to content

feat(session): add conflict-safe memory rollback - #4460

Open
Stamina9 wants to merge 1 commit into
volcengine:mainfrom
Stamina9:codex/feat-session-memory-rollback-4178
Open

feat(session): add conflict-safe memory rollback#4460
Stamina9 wants to merge 1 commit into
volcengine:mainfrom
Stamina9:codex/feat-session-memory-rollback-4178

Conversation

@Stamina9

@Stamina9 Stamina9 commented Aug 29, 2026

Copy link
Copy Markdown

Description

Session commits can mutate long-term memory, while deleting the session currently leaves those mutations behind. This PR adds a conflict-safe rollback path that replays every archived memory_diff.json in reverse commit and operation order before optionally deleting the session.

The owning implementation lives in openviking/service/session_memory_rollback.py. The server route, Python SDK, and Rust CLI transport the same dry_run, force, and delete_session safety options instead of reimplementing rollback rules.

The rollback service:

  • reverses adds, updates, and deletes under the session tree lock and exact memory URI locks;
  • compares current state with the recorded post-commit state and fails closed on conflicts;
  • supports dry-run planning and force mode while blocking dependent older operations on the same URI;
  • compensates already-applied memory changes if a later write or session deletion fails;
  • refreshes vectors and overviews after releasing storage locks;
  • rechecks running commit tasks inside the tree lock to close the check/lock race.

New audit records use schema v2 with canonical raw before/after/deleted snapshots and an explicit operation order. Legacy or incomplete snapshots are reported as conflicts rather than reconstructing metadata heuristically. Existing sessions remain readable; only rollback of records without sufficient raw snapshots is rejected safely.

The issue was not reproduced against a deployed server in this environment. The production execution paths were traced from commit audit creation through the service, REST route, SDK, and CLI, then validated with focused contract and failure-boundary tests.

Human Involvement

  • A human participated in the implementation or review loop
  • This PR was generated entirely by AI agents without human participation in the loop

The human selected the issue and risk standard, reviewed progress, provided the fork, and approved the release workflow. AI assistance was used for implementation and validation.

Related Issue

Fixes #4178

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • Add POST /api/v1/sessions/{session_id}/rollback, Python SDK methods, ov session rollback, and ov session delete --rollback-memories.
  • Persist complete, ordered raw memory audit snapshots for normal and training-generated memory operations.
  • Add conflict detection, dry-run, force handling, lock ordering, compensation, refresh, legacy fail-closed behavior, and English/Chinese documentation.

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

Validation performed on upstream main commit cd8580c6f8a50ec44593618b3102799ab0b553fd:

python -m pytest -o addopts='' \
  tests/service/test_session_memory_rollback.py \
  tests/session/memory/test_memory_diff.py \
  tests/session/train/test_train_components.py \
  sdk/python/tests/test_async_client_behaviors.py \
  tests/server/test_api_sessions.py::test_rollback_session_forwards_safety_options -q
# 112 passed

python -m ruff check <changed Python paths>
# All checks passed

python -m mypy --follow-imports=skip \
  openviking/service/session_memory_rollback.py \
  openviking/session/train/components/policy_optimizer.py
# Success: no issues found in 2 source files

cargo check --locked -p ov_cli
# passed

cargo test --locked -p ov_cli -- \
  --skip base_client::tests::plain_text_http_error_preserves_status \
  --skip config_wizard::wizard::tests::display_config_home_uses_tilde_for_current_home
# 473 passed; 0 failed; 2 filtered out

rustfmt --edition 2024 --check --config skip_children=true \
  crates/ov_cli/src/commands/session.rs crates/ov_cli/src/main.rs
# passed

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

No dependent changes are required.

Screenshots (if applicable)

Not applicable; this change exposes REST, SDK, and CLI behavior without a graphical UI.

Additional Notes

  • The default pytest addopts require pytest-cov, which was unavailable in the isolated runtime, so the exact focused command overrides addopts with -o addopts=''.
  • Running the entire tests/server/test_api_sessions.py file on this Windows runtime produced 51 setup errors because the installed x86 wheel does not package the native PersistStore backend. The rollback route contract test itself passes and is included above.
  • The two filtered Rust tests are unchanged environment/baseline tests: one requires a local HTTP behavior unavailable in this runner, and one depends on a conventional HOME value. All remaining 473 Rust tests pass.
  • The full change is larger than the repository's preferred review-size thresholds because the issue explicitly spans persisted audit data, rollback ownership, REST, SDK, CLI, documentation, and failure-boundary tests. The PR contains no unrelated cleanup or generated files.

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

[Feature]: 会话级记忆影响一键回滚(基于 memory_diff.json 审计日志反向操作)

1 participant