fix(memory): repair a dead index entry, and remove local blocks archived elsewhere
Two changes to workspace memory sync. They are related — the first is what makes the second's failure mode survivable — but they are separable, and the second is destructive. See Scope at the end.
1. A record deleted out from under us is re-created, not updated forever
The mirror index names the cloud record for a block. When the record set came back without that record — deleted in the web app, by another client, or by a wipe — absence read as "still ours":
const indexUsable = existing?.memoryId && (!indexed || !isArchived(indexed))
!indexed is truthy exactly when the record is gone, so match stayed pointing at a dead id and the sweep issued MemoryApi.update(deadId, …). runQueue counts that as one failed task and moves on without clearing the entry, so every later sweep retried the same dead id.
Absence now falls through to the identity search, which creates a fresh record, and the create rewrites the stale entry. Only a complete read counts as evidence of absence — a truncated set may simply not reach the record, and creating a second one for a block that already has one is the duplicate this path works hardest to avoid.
Known limit: the self-heal runs on the next save, not the next load. push returns at the content-hash guard for a block whose payload is already indexed, so an unchanged block whose record was deleted is not re-created until it is edited, and pendingCount reports 0 meanwhile. Pinned by an UNCHANGED block is not re-created when its record disappears. Recovering it would mean invalidating index entries a complete load shows to be absent; that is not in this change.
2. A record archived elsewhere removes its local block
Removal was one-directional. Deleting a block locally archives its cloud record (MemoryStore.remove → archiveBlock), but archiving from the web app left the block on disk — where prompt injection keeps reading it, and where the next edit re-creates the record. "Deleted in the web app" meant "hidden from one web table" and nothing else.
A record this client mirrored and archived now removes the block it mirrors, on the read path — which already holds the whole record set, a settled binding, and the directory they belong to, and runs on every hydrate and refresh.
It deletes files, so every rule is a refusal
| Rule | Why |
|---|---|
| decided per block, never per record | push archives split-create extras with the same identity metadata as the primary it keeps; reaping on the extra deleted a block whose memory was live, and the removal then archived the primary too |
archived_at must parse | absent or malformed, the only guard against deleting a recent edit does not exist |
>= keeps the block | equal timestamps cannot order the two, and "cannot tell" must not delete |
| removal is conditional on the block still being what was read | write renames a newer file into place; narrowed, not closed — only a lock shared with write closes it |
| binding fenced per iteration | commitLoad drops a load's result on a relink, which is no help once files are gone |
| this client's records only, matching workspace and project key | two projects in one workspace may hold the same block id |
| bounded at 25 blocks per load | the archived set only grows, and this runs inside the hydration budget |
archiveNow also returns early on an already-archived record. It prefers the index entry, so a reap on a machine that had one re-stamped archived_at to now — moving the moment every other machine's edited-after-archive guard compares against, so a block recreated after the original archive looked older than the tombstone and was deleted everywhere.
MemoryStore.remove threads its directory to the audit entry as well as the path and the archive. With no instance context that line threw from outside the best-effort try, with the file already unlinked: no audit entry, no telemetry, and a successful delete reported as a failure.
Verification
bun test test/altimate/workspace/ test/memory/ — 1126 pass, plus the pre-existing flushPendingSyncs timeout this repo carries on main. Typecheck and tracker-leak check clean.
Every guard is pinned by a test whose mutation was confirmed to fail it: the split-create case, an index-named tombstone, missing and unparseable archived_at, the equal-timestamp boundary, the conditional removal, a relink mid-reap, and both absence tests for part 1.
Six tests run against the real MemoryStore, because the reaper's seams stub removal out entirely — a wrong-directory unlink or a missing audit entry could not fail there. The reaper's double now records scope, directory and expectUpdated rather than only the id.
Not run against a live workspace. This deletes files on people's machines; a real end-to-end — archive in the web app, confirm the block disappears locally — is worth doing before merge.
Scope
Part 2 is a new destructive behaviour reached from cloud state, and it is fair to want it separable. It is a distinct commit (aac99dab, on top of a7e4d390) and reverts cleanly on its own, leaving part 1 intact. Happy to split it into its own PR if that is preferred — say the word and I will.
Related
Part of the memory-delete work. The web half is AltimateAI/altimate-frontend#3415, whose confirm copy is written to be true both with this change and without it, since the CLI reaches users only when they upgrade.
Summary by CodeRabbit
- Bug Fixes
- Memory sync recovers missing remote records only after a complete listing confirms they are gone; incomplete listings won’t trigger recovery.
- Refresh removes local memory blocks only when the matching remote record is archived, the block is older and unchanged, and the workspace and project still match. Active, newer, or out-of-scope blocks are preserved.
- Re-archiving a record no longer changes its original archive timestamp.
- Memory removal and its audit record can use an explicit project directory, even without an active project context.