Skip to content

fix(memory): harden correctness and privacy#1920

Merged
zerob13 merged 2 commits into
devfrom
fix/agent-memory-correctness-privacy-hardening
Jul 10, 2026
Merged

fix(memory): harden correctness and privacy#1920
zerob13 merged 2 commits into
devfrom
fix/agent-memory-correctness-privacy-hardening

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

Harden Agent Memory correctness, privacy, concurrency, and native-resource lifecycle behavior.

This change prevents extraction data loss, stale semantic writes, privacy-sensitive injection races, invalid conflict aggregates, late destructive-operation side effects, and unbounded provider or vector-store operations.

Changes

  • Add message-aligned, CJK-aware extraction chunks with Unicode-safe oversized-message fragmentation.
  • Advance extraction cursors only across successfully processed message boundaries and preserve exact lineage.
  • Add authoritative retrieval revalidation, read epochs, working-memory finalization, and a fail-closed injection gate.
  • Add per-agent destructive operation generations so clear, agent deletion, and dispose cancel stale asynchronous work.
  • Add conflict participant guards, transactional challenge creation, integrity repair, and indexed participant lookup.
  • Add the v41 decision_revision migration and revision-checked UPDATE, SUPERSEDE, CHALLENGE, and maintenance merge operations.
  • Add manager-owned vector-store leases, recoverable reset behavior, generation-checked writeback, and bounded disposal.
  • Route Memory provider requests through rate-limit admission, purpose-specific deadlines, cancellation, and unsettled-request bounds.
  • Replace new provenance writes with case-preserving SHA-256 v2 keys while retaining verified legacy lookup and lazy re-keying.
  • Add an isolated memory-native-validation GitHub Actions job for native binding, FTS, fresh-schema, and migration validation.

Summary by CodeRabbit

  • New Features
    • Improved memory extraction to chunk/window-based processing for long conversations.
    • Added decision_revision optimistic concurrency and provenance v2 keying (with legacy compatibility).
    • Introduced provider execution gateway with deadlines/throttling, vector-store lease-based lifecycle, and conflict integrity repair.
  • Bug Fixes
    • Prevented stale/late background work from overwriting newer memory changes via operation fencing and epoch/generation checks.
    • Conflicted memories are now excluded from automatic archiving.
  • Documentation
    • Added architecture specs, plans, tasks, and refreshed the agent memory system specification.
  • Tests
    • Expanded chunking, gateway, lineage, schema/migration, and native SQLite validation coverage (including native smoke checks in CI).

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e2def2b3-dd75-48b3-b3aa-2000b69b28d8

📥 Commits

Reviewing files that changed from the base of the PR and between f1052ce and 346f325.

📒 Files selected for processing (5)
  • docs/architecture/agent-memory-correctness-privacy-hardening/spec.md
  • docs/architecture/agent-memory-correctness-privacy-hardening/tasks.md
  • src/main/presenter/memoryPresenter/infra/providerGateway.ts
  • test/main/presenter/memoryPresenter.test.ts
  • test/main/presenter/memoryProviderGateway.test.ts
✅ Files skipped from review due to trivial changes (2)
  • docs/architecture/agent-memory-correctness-privacy-hardening/spec.md
  • docs/architecture/agent-memory-correctness-privacy-hardening/tasks.md
🚧 Files skipped from review as they are similar to previous changes (3)
  • test/main/presenter/memoryProviderGateway.test.ts
  • src/main/presenter/memoryPresenter/infra/providerGateway.ts
  • test/main/presenter/memoryPresenter.test.ts

📝 Walkthrough

Walkthrough

This PR hardens agent memory across chunked extraction, authoritative retrieval, revisioned persistence, conflict handling, provider cancellation, vector-store leases, provenance migration, disposal, and native SQLite validation. It also adds architecture specifications, implementation tasks, and regression coverage.

Changes

Agent memory correctness and lifecycle hardening

Layer / File(s) Summary
Architecture and operational contracts
docs/architecture/..., .github/workflows/prcheck.yml, scripts/smoke-memory-native-sqlite.js, package.json
Documents the correctness model, updates native SQLite CI validation, adds an encrypted SQLite smoke test, and removes the former native test script.
Chunked extraction pipeline
src/main/presenter/agentRuntimePresenter/*, src/main/presenter/memoryPresenter/core/*, test/main/presenter/agentRuntimePresenter/*
Replaces span extraction with bounded message chunks, Unicode-safe fragments, lineage metadata, cursor commit boundaries, queued continuation, and full-chunk prompts.
Runtime, provider, and vector lifecycle
src/main/presenter/memoryPresenter/context.ts, infra/*, ports.ts, src/main/presenter/memoryPresenter/index.ts
Adds read epochs, destructive generations, provider deadlines and cancellation, generation-aware vector leases, working-memory dirty flushing, and bounded disposal.
Revisioned persistence and conflict integrity
src/main/presenter/memoryPresenter/types.ts, services/*, src/main/presenter/sqlitePresenter/*
Adds schema v41 with decision_revision, CAS-style semantic mutations, unresolved-conflict guards and repair, archive filtering, and transactional maintenance merges.
Provenance and authoritative injection
src/shared/lib/agentMemoryLineage.ts, core/scoring.ts, services/retrievalService.ts, services/workingMemoryService.ts
Adds shared lineage codecs, v2 SHA-256 provenance keys with legacy re-keying, authoritative row refresh, epoch-gated injection, and legacy working-memory reconciliation.
Validation and race coverage
test/main/presenter/*
Adds tests for chunking, migrations, provider limits, provenance compatibility, conflicts, stale writes, vector lifecycle, disposal, and asynchronous invalidation.

Estimated code review effort: 5 (Critical) | ~120 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.81% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main thrust of the PR: memory correctness and privacy hardening.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/agent-memory-correctness-privacy-hardening

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/presenter/memoryPresenter/infra/providerGateway.ts`:
- Around line 129-135: In the timeout callback that creates the deadline
promise, update the ordering so reject(createAbortError(`[Memory] ${purpose}
deadline exceeded (${deadline}ms)`)) executes before controller.abort(). Keep
the existing timer and cleanup behavior, ensuring the deadline rejection wins
the Promise.race instead of the abort handler.

In `@test/main/presenter/memoryPresenter.test.ts`:
- Around line 112-118: Update the return type of
conflict.repairConflictIntegrity in the test seam to include the clearedLinks
field, matching the actual returned object and existing assertions while
preserving the current fields.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5577a954-83fd-44ee-b2bb-19a0caec839c

📥 Commits

Reviewing files that changed from the base of the PR and between 49546ea and f1052ce.

📒 Files selected for processing (50)
  • .github/workflows/prcheck.yml
  • docs/architecture/agent-memory-correctness-privacy-hardening/plan.md
  • docs/architecture/agent-memory-correctness-privacy-hardening/spec.md
  • docs/architecture/agent-memory-correctness-privacy-hardening/tasks.md
  • docs/architecture/agent-memory-system/spec.md
  • package.json
  • scripts/smoke-memory-native-sqlite.js
  • src/main/presenter/agentRuntimePresenter/index.ts
  • src/main/presenter/agentRuntimePresenter/memoryExtractionChunks.ts
  • src/main/presenter/index.ts
  • src/main/presenter/memoryPresenter/context.ts
  • src/main/presenter/memoryPresenter/core/decision.ts
  • src/main/presenter/memoryPresenter/core/extraction.ts
  • src/main/presenter/memoryPresenter/core/injectionPort.ts
  • src/main/presenter/memoryPresenter/core/lifecycle.ts
  • src/main/presenter/memoryPresenter/core/scoring.ts
  • src/main/presenter/memoryPresenter/index.ts
  • src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts
  • src/main/presenter/memoryPresenter/infra/providerGateway.ts
  • src/main/presenter/memoryPresenter/infra/vectorStoreManager.ts
  • src/main/presenter/memoryPresenter/ports.ts
  • src/main/presenter/memoryPresenter/services/conflictService.ts
  • src/main/presenter/memoryPresenter/services/maintenanceService.ts
  • src/main/presenter/memoryPresenter/services/managementService.ts
  • src/main/presenter/memoryPresenter/services/personaService.ts
  • src/main/presenter/memoryPresenter/services/reflectionService.ts
  • src/main/presenter/memoryPresenter/services/retrievalService.ts
  • src/main/presenter/memoryPresenter/services/rowMutations.ts
  • src/main/presenter/memoryPresenter/services/workingMemoryService.ts
  • src/main/presenter/memoryPresenter/services/writeCoordinator.ts
  • src/main/presenter/memoryPresenter/types.ts
  • src/main/presenter/sqlitePresenter/index.ts
  • src/main/presenter/sqlitePresenter/schemaCatalog.ts
  • src/main/presenter/sqlitePresenter/tables/agentMemory.ts
  • src/main/routes/index.ts
  • src/shared/lib/agentMemoryLineage.ts
  • test/main/presenter/agentMemoryLineage.test.ts
  • test/main/presenter/agentMemoryTable.test.ts
  • test/main/presenter/agentRuntimePresenter/agentRuntimePresenter.test.ts
  • test/main/presenter/agentRuntimePresenter/memoryExtractionChunks.test.ts
  • test/main/presenter/fakes/memoryFakes.ts
  • test/main/presenter/memory-persona-eval.test.ts
  • test/main/presenter/memoryAdd.test.ts
  • test/main/presenter/memoryExtraction.test.ts
  • test/main/presenter/memoryLifecycle.test.ts
  • test/main/presenter/memoryNativeMigration.test.ts
  • test/main/presenter/memoryPresenter.test.ts
  • test/main/presenter/memoryProviderGateway.test.ts
  • test/main/presenter/memoryRetrieval.eval.test.ts
  • test/main/presenter/memoryUpdate.test.ts
💤 Files with no reviewable changes (1)
  • package.json

Comment thread src/main/presenter/memoryPresenter/infra/providerGateway.ts
Comment thread test/main/presenter/memoryPresenter.test.ts
@zerob13 zerob13 merged commit e148044 into dev Jul 10, 2026
3 checks passed
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