Skip to content

core: markdown writes use three different mechanisms and two of them are not atomic #1355

Closed

Description

@schlessera

Part of #1341.

Context

Markdown is the source of truth, and it is written three ways:

  • plain writeFileSync, not atomic: brain_update (packages/core/src/mcp-server.ts:629), ingestion append (packages/core/src/lib/ingestion.ts:213), archiver fallback (packages/core/src/lib/archiver.ts:123);
  • staged write then hard link: archiver (packages/core/src/lib/archiver.ts:109);
  • the writeFileSafely helper (packages/core/src/lib/safe-path.ts:146).

A crash or full disk during a plain writeFileSync can truncate a user's document.

Scope

  • Route every write of an existing user document through one atomic writer (write to a temp file in the same directory, preserve mode, rename), extending writeFileSafely if needed.
  • Keep exclusive-create (flag: "wx", packages/core/src/lib/ingestion.ts:245) semantics where used.

Out of scope

  • Writes to brain.db, scratch files or generated caches.

Acceptance criteria

  • No writeFileSync to a user document path remains outside the shared writer; a lint or test enforces it.
  • A test simulates a failed write (for example a throwing write after the temp file is created) and proves the original file is intact, failing before the change for brain_update.
  • File mode is preserved, with a test.

Verification

bun run test packages/core

Contract impact

None.

Activity

  1. added
    type: fix
    area: core
    area: mcp
    priority: p2
    agent-ready
    on Oct 9, 2026
  2. self-assigned this
    on Oct 9, 2026
  3. schlessera commented on Oct 9, 2026

    @schlessera
    OwnerAuthor

    Claiming this (claim token w1355-a7). Branch: fix/1355-atomic-markdown-writes.

  4. added this to the 0.41.0 milestone on Oct 9, 2026
  5. added
    blocked
    on Oct 10, 2026
  6. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Status, 2026-10-10: implemented in #1435 (head 22b009461a26f6c1a3ed09a22fa2fc97f703a1be). The PR is not merged yet.

    • CI run 38007229014, attempt 1, on merge checkout df6f4a6d (head 22b00946 merged into main bae10edc): changeset, verify, runtime, pack, captures, browser (1), browser (2), layout (1), layout (2) and unit (2)–unit (4) passed, and so did contract. The new document-writes and safe-path tests passed in CI.
    • unit (1) (job 114078933756) failed only at tests/audit-review-artifacts.test.ts:21, where the paid-extra child exits 1. proof fails because it aggregates unit. The cause is test: keep native audit offline controls independent of a frozen detection day #1437: DETECTION_DAY is frozen to 2026-10-09 (scripts/evals/audit-capabilities/benchmark.ts:20), and review-native.ts:35 refuses the request on any later UTC day. The same test fails locally on plain main bae10edc and on 21f146d1, so this change did not cause it.

    Marked blocked on #1437. To resume once #1437 merges: update #1435 onto main, confirm every selected job passes for the new head, then squash-merge with --match-head-commit. Milestone 0.41.0 is set on both this issue and the PR. #1350 and #1397 stay blocked until this issue closes.

    Releasing my claim; the PR stays open and ready.

  7. removed their assignment
    on Oct 10, 2026
  8. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Unblocked: #1437 is closed, so scripts/sync-project.ts is removing the blocked label.

  9. removed
    blocked
    on Oct 10, 2026
  10. self-assigned this
    on Oct 10, 2026
  11. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Re-claiming to finish #1435 now that #1437 has merged. I removed the stale Blocked by #1437 line and its status note from the body. Branch: fix/1355-atomic-markdown-writes.

  12. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Merged in #1435 as squash 20557bf54936a05f31684cbe0f8757104b30b5a3 (parent d57dc62b). The commit is on main.

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

Metadata

Metadata

Assignees

Labels

agent-readyarea: corearea: mcppriority: p2type: fix

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions