Repository navigation
fix(memory-core): restore MEMORY.md when the in-place fallback write fails midway - #126486
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 23, 2026, 12:04 AM ET / 04:04 UTC. ClawSweeper reviewWhat this changesThe PR restores the verified original MEMORY.md content when the permission-triggered in-place promotion fallback fails partway through writing. Merge readinessKeep open: current main still lacks recovery when the in-place MEMORY.md fallback partially writes and then fails. This is a focused owner-boundary repair with strong terminal proof; rebase/current-main validation is the remaining merge precaution. Priority: P1 Review scores
Verification
Live VerificationCommand: Result: PASS (completed) Assertions:
How this fits togetherMemory Core promotes recalled short-term entries into an agent’s MEMORY.md through manual promotion and scheduled dreaming. It prefers an atomic replacement, then uses an existing-file fallback only when directory permissions block replacement; successful writes are then recorded as promoted. flowchart LR
A[Promotion candidates] --> B[Promotion apply flow]
B --> C[Atomic MEMORY.md replacement]
C -->|Directory permission failure| D[Existing-file fallback]
D -->|Write succeeds| E[Mark entries promoted]
D -->|Write fails| F[Restore verified original bytes]
F --> G[Surface write failure]
E --> H[Updated long-term memory]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +25, Tests +127. Total +152 across 2 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the existing ACL-compatible fallback and land this preimage recovery at its sole write owner after current-main validation. Do we have a high-confidence way to reproduce the issue? Yes—source-reproducible: current main has a direct r+ write with no recovery catch, and the PR supplies before/after terminal evidence for a permission-triggered EFBIG path. Is this the best way to solve the issue? Yes. The writer is the only layer holding the verified preimage; removing the shipped fallback would require a separate compatibility decision, while downstream recovery cannot restore lost bytes. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 91fb03223afd. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
|
@clawsweeper re-review Addressed the P1 finding: the restore now advances an explicit offset by each bytesWritten result until the full preimage is written, fails on zero progress, and only then truncates and syncs. Added a focused regression that makes the first restore write report short bytesWritten and asserts the file equals the original preimage; it fails against the single-write restore with the exact spliced-content corruption the review predicted. Proof regenerated on pristine main c5fd625 and head 4377cd7cc5c; PR body updated. |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
…fails midway The append-only promotion fallback writes the new content directly into the existing MEMORY.md when directory ACLs block atomic replacement. A mid-write failure such as EFBIG or ENOSPC left the file partially overwritten and the original entries destroyed. The fallback now rewrites the verified original content and truncates back to its length before rethrowing, so a failed in-place write leaves MEMORY.md exactly as it was.
FileHandle.write can report fewer bytes than requested without throwing. Advance an explicit offset by each bytesWritten result until the whole preimage is restored, and fail the restore on zero progress, so a short restore write can never splice failed-promotion bytes into MEMORY.md.
Use the real file handle without type laundering, skip the POSIX-only permission harness on Windows, and rely on the existing append-path integration for successful fallback coverage. Co-authored-by: Ayaan Zaidi <[email protected]>
4377cd7 to
c3f3e80
Compare
|
Thanks @yetval! Bug: When MEMORY.md could only be updated in place, a partial disk-limit failure could overwrite the user’s saved memory. |
…claw#126486) Preserve the verified MEMORY.md preimage when the ACL-compatible in-place promotion fallback fails after a partial write. Complete short restore writes before truncating and syncing, then return the original write error. Co-authored-by: Yuval Dinodia <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
Problem
When Memory Core cannot replace
MEMORY.mdatomically because the directory blocks temporary files or rename, it falls back to updating the existing writable file in place. If that write fails partway withEFBIG,ENOSPC, or another write error, the old file is left partially overwritten.This is user-data corruption. The recall store does not mark the promotion as complete, but the lost
MEMORY.mdbytes are not recoverable downstream.Root cause
writeExistingMemoryInPlaceverifies the current file againstexpectedContent, opens it withr+, and callswriteFile. The old implementation closed the file after a partial write error without restoring the verified preimage.Only the append-only promotion path enables this ACL-compatible fallback. Consolidation uses atomic replacement and does not write in place.
Fix
The in-place writer now restores the verified preimage before it returns the original write error. The restore uses positional writes and advances by
bytesWrittenuntil every byte is back. It then truncates to the original byte length and synchronizes the file.The offset loop is required because a restore write can make partial progress without throwing. A zero-progress write stops with an explicit recovery error instead of looping forever.
Proof
Real kernel
EFBIGproof used the productionwriteMemoryContententry point, a real read-only directory, a writable existingMEMORY.md, andulimit -f 2.2a76736fcb77440e98b2569a63d27a46f5683482:EFBIG; 55-byte preimage became a 1,024-byte partial file;afterEqualsOriginal=false.c3f3e8008ab5491b79a6ff24bebfaa9b4d8d5af8:EFBIG; file stayed 55 bytes;afterEqualsOriginal=true.node scripts/run-vitest.mjs extensions/memory-core/src/short-term-promotion-memory-write.test.ts extensions/memory-core/src/short-term-promotion.test.ts: 2 files passed, 93 tests passed.node_modules/.bin/oxfmt --check ...: passed.node_modules/.bin/oxlint ...: passed.The existing
short-term-promotion.test.tsintegration covers the successful append-only ACL fallback through the full promotion owner. Telegram cannot observe this filesystem failure, so the required default direct-message probe is collateral only; the kernelEFBIGrun is authoritative.Scope
Telegram collateral
The generic default direct-message probe passed on the final head. The dedicated QA user sent
Please answer with OPENCLAW_E2E_BASIC only.; the TDLib timeline recorded the user message, two typing events, and the bot replyOPENCLAW_E2E_BASIC. The gateway log recorded readiness and clean shutdown. The mock-provider log recorded onePOST /v1/responsesrequest.This filesystem durability bug is not Telegram-observable. The real kernel
EFBIGwriter proof above is authoritative.