Skip to content

feat(memory): PR-1 registry, index, recover, doctor foundation - #592

Closed
prodempsey wants to merge 7 commits into
kunchenguid:mainfrom
prodempsey:fm/memory-pr1-registry-r1
Closed

prodempsey wants to merge 7 commits into
kunchenguid:mainfrom
prodempsey:fm/memory-pr1-registry-r1

Conversation

@prodempsey

Copy link
Copy Markdown

Memory PR-1 — registry / index / recovery foundation

Exact reviewed SHA: 746b46bbd77d8f50fb23fc1fc338e416f6475e31
Branch: fm/memory-pr1-registry-r1
Scope: PR-1 only (Milestone A foundation). This is not an operationally closed memory system.

Scope (PR-1 only)

Additive memory/ package:

  • JSONL canonical registry with exclusive lock and atomic append
  • Active-memory index rebuild
  • Automatic boundary snapshots + shared full validator
  • Mutation preflight that repairs/blocks on invalid completed snapshots
  • Corrupt-snapshot quarantine and exact-boundary reconstruction
  • Activity ledger, recover, doctor, CLI (mem)
  • Risk class / captain override rules (taskId-bound)

Not in this PR (PR-2+ gated): PGlite/FTS, vectors/embeddings, migration/cutover, recall, session packs, brief injection, workflow hooks, Fleet Bridge memory UI, reconciliation, closeout enforcement, graph work.

The package is additive and not yet consumed by FirstMate workflow scripts.

Implementation report

Local path (fleet): data/memory-pr1-registry-r1/report.md (also on the branch under that path if present).

Independent QA (five rounds)

Round Task Reviewed SHA Verdict
s1 memory-pr1-qa-s1 initial tip NOT READY / blockers found
s2 memory-pr1-qa-s2 4e684ad… FAIL — residual blockers
s3 memory-pr1-qa-s3 a0f3f96… FAIL — residual
s4 memory-pr1-qa-s4 322beb5… FAIL — corrupt completed snapshot trusted by mutation preflight
s5 memory-pr1-qa-s5 746b46bbd77d8f50fb23fc1fc338e416f6475e31 PASS — ready to land

Final QA report (fleet): data/memory-pr1-qa-s5/report.md
Captain export: D:\FirstMate-Reports\memory-pr1-qa-s5-report-2026-07-15.md

Final fix report (f4): data/memory-pr1-fix-f4/report.md

Test results (QA s5, isolated under /tmp)

  • Full committed package suite: 93/93 pass
  • Critical prior-regression group (concurrency, recovery, audit, state-machine, risk-class, activity): 53/53 pass
  • Exact QA4 corrupt-snapshotHash repro: repair-then-commit on event 501
  • Idempotent retry repair, explicit mem snapshot repair, shared-validator parity: pass
  • 12-process concurrent repair: one valid boundary snapshot, no lost writes
  • Final mem audit --json / mem doctor --json: green
  • Production $HOME/fleet/state/memory: absent before and after QA (no production records)
  • PR-2 surfaces: absent from the branch
  • Unresolved actionable review findings at pre-PR QA: 0

Rollback

# If merged via merge commit:
git revert -m 1 <merge-commit-sha>

# Or reset local consumers to pre-merge main and re-install without the memory/ package.

The package is additive; removing or not installing memory/ restores prior behavior for workflow paths that do not call it.

Captain authorization

Captain accepted QA s5 and authorized land at exact SHA 746b46bbd77d8f50fb23fc1fc338e416f6475e31. PR-2 remains gated pending a separate Captain order.

Corrective fixes on the PR-1 registry core against the exact-SHA QA findings.
All changes stay within the inert memory/ package (code + tests); no workflow,
migration, PGlite, vector, recall, or Fleet Bridge scope.

Blockers fixed:
- Cross-process lock: atomic mkdir mutex with an ownership token written via
  temp+rename; a freshly created lock with no visible owner is treated as
  initializing/busy for a bounded grace, never immediately stale; stale reclaim
  requires sufficient age plus proof the owner is absent, via an atomic
  rename-steal; release verifies the ownership token. Index projection now
  refreshes under the same mutation lock, with a monotonic compare-before-install
  guard so an older projection cannot overwrite a newer one.
- State machine: explicit transition table for every legal edge; illegal
  transitions refused without altering canonical state; high-impact activation
  requires an independent activator or captain authority; quarantine recovery via
  a defined revalidate transition; supersession requires an existing active
  successor and updates both sides of the lineage atomically; terminal states are
  terminal. guard_linked and factual/procedural memory type added to the schema
  with event-specific structural validation.
- Sparse, non-destructive mem update: only caller-supplied fields change.
- Byte-faithful, crash-durable recovery: raw-byte corrupt-suffix preservation,
  exact valid-prefix repair, fsynced backup/sidecar/repaired/replacement,
  failpoints at every stage, mandatory green final audit.
- mem audit verifies the full watermark, active ID set/count, per-record content
  and content hashes, and rejects any inactive/tampered/stale projection.
- Canonical shim resolves code only from $HOME/fleet/firstmate-runtime; FM_HOME no
  longer redirects executable code. Hardened installer (lstat, temp+fsync+atomic
  rename, executable-mode validation, safe pre-existing symlink handling).
- Fail-safe risk classification defaults upward on missing/uninspected/ambiguous
  inputs; captain override applies the downgrade, logs an opaque reference, and
  never leaks the raw token.
- mem doctor uses real semver (minor floor) and one stable JSON schema in every
  condition, including missing dependencies.
- Snapshots use safe hashed filenames (no path traversal), written atomically with
  fsync. Activity ledger gains cross-process locking, a segment manifest with row
  counts and content hashes, and corrupt-tail fold.

Tests: added subprocess concurrency stress, lock init-race/token-release/stale
reclaim, index non-regression, full transition matrix, high-impact activation,
quarantine revalidation, non-dangling supersession, sparse-update preservation,
binary sidecar fidelity, recovery failpoints, audit tamper matrix, stable doctor
JSON, semver floor, risk fail-safe/override, snapshot traversal/atomicity, and
activity concurrency/manifest. Fixed the A34 temp-dir self-copy fragility so the
suite passes under any TMPDIR, including one under the package tree.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 746b46bbd7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread memory/lib/registry.mjs
validTo: null,
recordedAt: event.ts,
verifiedAt: null,
confidence: 'unverified',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not apply passthrough fields to record internals

When an event carries an extra field such as fields.status, this assignment copies it straight onto the folded record because recordFieldsSchema is passthrough(). A valid retired event with fields.status: 'active' would pass schema validation, run the transition, and then leave the terminal record active, bypassing the state machine and active-index filtering; restrict application to the known mutable record fields or strip unknown keys before folding.

Useful? React with 👍 / 👎.

Comment on lines +54 to +60
const ok = Boolean(
compatible
&& parts.requiredDependencies.ok
&& parts.packageLock.present
&& parts.packageLock.current
&& registryOk
&& snapshotsOk

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Fail doctor when the active index is not current

When the registry is healthy but memory-index.json is missing or stale, auditRegistry() reports activeIndex.status !== 'current', but this health gate ignores parts.activeIndex entirely. That lets mem doctor exit 0 even though consumers would read an out-of-date or absent active-memory index, so the doctor result should include the active-index status in its ok calculation.

Useful? React with 👍 / 👎.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: closing this as stale. It has been waiting on a contributor update for 14+ days with no author push or comment. Reopen if you want to pick it back up.

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