Skip to content

[Bug]: Five data-integrity bugs — edit mangles non-UTF-8 files, replace_all mismatch, non-atomic config rewrites, bricked v2 session, symlinked config clobber #547

Open

Description

@syxscott

Product or interface

CLI - interactive TUI (core packages — affects the Desktop app and CLI alike; found by source review)

Version

main @ da22f11 (v0.6.5)

Platform

Windows

OS version and architecture

Windows 10 x64 (build 26200)

Issue area

File / workspace / path / encoding

Steps to reproduce

Five related data-integrity bugs found during a review of this repository. Findings 1 and 2 were reproduced first-hand by driving the repository's own tool implementations (byte-level, on temp files); findings 3–5 are code-verified with call paths traced end-to-end. None involve security-sensitive material. Per finding: symptom → root cause (file:line) → minimal fix → existing test that can be extended. Happy to provide reproduction scripts or fix patches for any of these.

# Severity Bug
1 High edit / replace_all silently destroy non-UTF-8 (GBK, Shift-JIS, Latin-1…) file content
2 Medium replace_all rejects old_string that a single edit accepts (BOM and CRLF files), with misleading errors
3 High Read-path config rewriters truncate-then-write config.yaml with no lock and no backup — a crash mid-write destroys all user settings
4 Medium-High One torn JSONL line permanently bricks a v2 session (strict reads, recovery machinery exists but is unwired)
5 Medium Settings writers replace a symlinked config.yaml with a regular file

1. edit silently destroys non-UTF-8 file content

Repro (first-hand, byte-level): a file containing "你好 world\n" in GBK (c4 e3 ba c3 20 77 6f 72 6c 64 0a), LocalEditTool.execute with old_string: "world" → new_string: "earth". The tool reports success, but the file becomes efbfbd efbfbd efbfbd 20 65 61 72 74 68 0a — "��� earth\n". The untouched GBK bytes are permanently replaced with U+FFFD replacement characters, and the diff returned to the user/model only shows world → earth, completely hiding the destruction. replace_all: true behaves the same way (it performs its own full-file decode + replacement in packages/agent-tools/src/shared/replace-all-edit.ts:54-55).

Root cause: LocalEditTool (packages/agent-tools/src/desktop/local-pi-tools.ts:296-299) injects only createCapturingEditOperations(); the injected readFile (packages/agent-tools/src/shared/edit-capture.ts:23-27) hands the raw Buffer to pi, which lossily decodes with buffer.toString("utf-8") (third_party/pi-mono/packages/coding-agent/src/core/tools/edit.ts:341) and writes the whole file back as UTF-8, materializing the U+FFFDs.

The gap is first-party: the Write path already handles encodings — write-capture.ts:97-130 preserves utf16le/BOM, and read-guards.ts:349-354 deliberately lets UTF-16/GBK through the read binary guard — but Edit has no equivalent guard.

Minimal fix: sniff the Buffer in createCapturingEditOperations().readFile and in buildReplaceAllEdit before decoding (reuse detectEncodingFromBuffer from write-capture.ts:106 plus a fatal TextDecoder("utf-8", { fatal: true }) validity check — the same pattern already exists as isValidUtf8 in packages/local-runtime/src/turns/file-changes.ts:709), and either fail with a clear "file is not UTF-8" error or transcode like write-capture does.

Test: packages/agent-tools/src/shared/replace-all-edit.test.ts already drives LocalEditTool end-to-end on temp files; add a GBK-bytes case asserting the edit is rejected and the bytes are unchanged.


2. replace_all is inconsistent with a single edit on BOM / CRLF files

Repro (first-hand):

  • UTF-8-BOM file: a single edit with old_string: "TOKEN" succeeds (BOM preserved). The identical call with replace_all: true throws Could not find the exact text in ….
  • CRLF file alpha TOKEN\r\nbeta TOKEN\r\ngamma\r\n, multi-line LF old_string: "beta TOKEN\ngamma": a single edit succeeds (file keeps CRLF). The same call with replace_all: true throws replace_all: old_string was not found.

Root cause: buildReplaceAllEdit (packages/agent-tools/src/shared/replace-all-edit.ts:54-55, 67, 84) matches old_string against the RAW file content (BOM and \r\n intact) and builds oldText including the BOM. pi's matching contract — used by single edits — strips the BOM and normalizes both sides to LF before matching (edit.ts:344-348, edit-diff.ts:322-325), then restores BOM/line-endings on write. So the replace_all-built oldText can never match a BOM file, and a multi-line LF-only old_string can never be counted in a CRLF file.

The failure is safe (no write occurs) but it breaks the read→edit loop on every BOM file and on most multi-line replace_all calls under CRLF, and the error text ("the file may have changed since — use the read tool to reload") actively misleads.

Minimal fix: in buildReplaceAllEdit, strip the BOM and LF-normalize both the content and old_string before counting/replacing (pi's pipeline re-adds the BOM and restores line endings on write). Test: extend replace-all-edit.test.ts with BOM and CRLF cases.

Related: files with mixed line endings are normalized wholesale — edit-diff.ts detects one dominant ending and restoreLineEndings rewrites every line, so editing one line flips the endings of untouched lines (verified: aaa\r\nbbb\nccc\n + edit bbb→B → aaa\r\nB\r\nccc\r\n).


3. Read-path config rewriters: truncate+write, no lock, no backup

Where: writePrivateConfigFileSync (packages/config/src/private-config-file.ts:23-34) does openSync(O_WRONLY|O_CREAT) → fchmodSync → ftruncateSync(fd, 0) → writeFileSync. No temp file, no rename, no lock, no backup. Its callers are the read-path rewriters run by prepareConfigFileForRead (packages/config/src/config.ts:1881-1891) on every config load: syncManagedPresetBaseUrl (write at config.ts:1617-1620) and migrateLegacyByokProvidersOnDisk (packages/config/src/byok-config.ts:277-281; that one at least calls backupConfigForMigration first — the preset sync does not).

The trigger is real, not theoretical: the managed preset sync fires whenever provider.minimax.options.baseURL is a managed preset URL that differs from the current build's preset. LEGACY_MANAGED_PRESET_BASE_URLS (config.ts:1545-1547) exists precisely because past gateway migrations put stale preset URLs into user configs — an app update that changes PRESET_BASE_URLS makes every install rewrite its full config on first boot.

Loss scenario: a crash/power loss between ftruncateSync and write completion leaves config.yaml at 0 bytes (or half-written). The next boot parses "" → null → returns {} with no warning; the app "works" but BYOK providers, API keys, and all preferences are gone with nothing on disk to recover. The repository's own test codifies the destroyed end state: packages/config/test/managed-preset-sync.test.ts:105-114 ("does not hide a failure after the original file has been truncated") asserts the file is "" after a mid-write failure.

Contrast: every settings writer uses the proper discipline — proper-lockfile + temp-file + rename (local-model-provider-write.ts:220, tui-status-line-write.ts:24); the atomic-write helper even already exists (local-model-provider-write.ts:246-264, duplicated in packages/local-runtime/src/config/update.ts:285-303).

Additional risk (same root): the unlocked read-modify-write races the locked settings writers across processes — a second window/CLI holding a stale snapshot can silently revert a just-saved setting.

Minimal fix: route these writes through the shared lock + temp + rename helper (hoist atomicWriteFile into @mavis/config); fsync before rename. At absolute minimum, snapshot to config.yaml.bak.<reason>.<ts> first (the exact pattern exists at byok-config.ts:537-545). Test: replace the truncated-to-empty assertion in managed-preset-sync.test.ts with a crash-consistency case (partial write then throw ⇒ original content still readable, or a .bak exists).


4. One torn JSONL line permanently bricks a v2 session

Where: readJsonl (packages/local-runtime-v2/src/infra/file/jsonl.ts:60) throws Malformed JSONL in <path> at line N when no onMalformedLine observer is supplied; the cached strict path throws identically (:96-98), and it also treats a trailing line without \n as a line (:88), so a torn tail from a crash between write() and sync() is a parse failure. The production provider builds its adapter without an observer (packages/local-runtime-v2/src/service/session-system/messages/history/canonical-history-provider.ts:214), so readSnapshot (:340), append (:253), replace (:272), compact (:293), and syncIndex (:404) all hit the strict throw.

Consequence: a power loss mid-append leaves a partial last line in messages.jsonl; on next startup nothing detects or repairs it, and every read and every write of that session throws forever. The messages are still on disk but permanently inaccessible from the app without hand-editing the file.

This is a gap, not a design choice: the v1 ledger handles exactly this scenario (per-line parse failures are skipped and the scan continues: packages/local-runtime/src/sessions/ledger/ledger-read.ts:176-195, 226-239; the next append starts on a fresh line boundary: ledger-append-recovery.ts:78-93). All the tolerant-decode and repair machinery already exists in v2 (readActive/reportActiveMalformedLine in canonical-history-jsonl.ts:423-427, 1228-1233; repairCanonicalHistory in canonical-history-recovery.ts) — only the wiring is missing.

Minimal fix: in the provider's read path, catch the malformed-line error, fall back to the tolerant decode, run repairCanonicalHistory(...), and persist the repaired prefix via the already-atomic files.replaceActive(...). Test: add a "messages.jsonl with a torn trailing line is repaired on open" case to test/history-processing.test.ts.


5. Settings writers replace a symlinked config.yaml with a regular file

Where: fs.rename(tmpPath, configPath) in packages/config/src/local-model-provider-write.ts:259 and packages/local-runtime/src/config/update.ts:298. rename does not follow symlinks, so a symlink at configPath is silently replaced by the temp file.

Why this matters: symlinked configs are a supported setup — tui-status-line-write.ts:20-22 deliberately resolves realpath first, commenting "Replace the real file, preserving any symlink used to manage profile settings", and config-file-permissions.test.ts:241-250 exercises a symlinked config. The other writers don't, so a dotfiles user (config.yaml → repo copy) who changes one setting in the TUI silently breaks the link; the dotfiles copy keeps stale settings and the next sync reverts the user's change.

Minimal fix: mirror tui-status-line-write.ts — resolve realpath before temp-file creation and rename onto the target. Test: add a symlinked-config case to config-update-preservation.test.ts.

Expected and actual behavior

  1. Editing a non-UTF-8 file should either preserve the file's encoding (like the Write path already does for UTF-16/BOM) or refuse with a clear error — not report success while replacing every non-UTF-8 byte in the file with U+FFFD.
  2. replace_all: true should accept exactly the old_string values a single edit accepts, including on BOM and CRLF files.
  3. A failure or crash during a read-path config rewrite must leave the original config.yaml intact (atomic rename, or at least a .bak), instead of leaving a 0-byte file that silently resets to default settings on next load.
  4. A v2 session with one torn JSONL line should be repaired on open (v1 already does this), instead of throwing Malformed JSONL on every subsequent read and write forever.
  5. Saving settings should preserve a symlinked config.yaml (like tui-status-line-write already does), instead of replacing the symlink with a regular file.

Redacted error summary

Finding 1 (first-hand, synthetic GBK file): before c4 e3 ba c3 20 77 6f 72 6c 64 0a → after a successful world→earth edit: ef bf bd ef bf bd ef bf bd 20 65 61 72 74 68 0a; tool result reported success, diff showed only the intended change.

Finding 2 (first-hand): Error: Could not find the exact text in <path>. The old text must match exactly… on a BOM file where a single edit with identical old_string succeeded; Error: replace_all: old_string was not found in <path>. on a CRLF file where a single multi-line edit with an LF old_string succeeded.

Finding 4: Malformed JSONL in <path> at line N thrown by every readSnapshot/append/compact after a crash left a partial last line.

Before submitting

  • I have searched existing issues.
  • I have included my version and removed sensitive information.

Activity

  1. Ralle1976 commented on Nov 20, 2026

    @Ralle1976
    Contributor

    Data-integrity findings #1 and #2 I could reproduce against main — but #2 is already fixed
    upstream, and #3 has a Windows-specific failure mode that the proposed fix does not cover.

    Versionsanker

    Installierte CLI mcode --version → 0.7.8 (npm, global)
    npm latest npm view @minimax-ai/code version → 0.7.8
    Quellstand upstream/main @ 3c21163 (package version 0.7.2), tag v0.7.2 = c6c7f28
    Toolchain Node v24.18.0, pnpm 9.15.0
    Platform Windows 11 x64, build 26200
    POSIX-Kontrolle alpine:latest in Docker (uid 1000), BusyBox mv, gleiche Messfolge

    Reproduced on main (3c21163), finding #1. Driving the real edit pipeline
    (packages/agent-tools/src/shared/edit-capture.ts:23-27 → third_party/pi-mono/.../core/tools/edit.ts:341-352)
    on a GBK file:

    before: c4 e3 ba c3 20 77 6f 72 6c 64 0a      ("你好 world\n" in GBK)
    after : ef bf bd ef bf bd ef bf bd 20 65 61 72 74 68 0a   ("��� earth\n")
    

    Three U+FFFD replacement characters materialize where the GBK bytes were; the tool reports
    success. edit-capture.ts:29 writes the whole file back with 'utf8', so the loss is
    permanent. The diff hides it, because both sides of the diff come from the same lossy decode
    (originalFile at edit-capture.ts:32 is also original.toString('utf8')) — the U+FFFDs are
    already present on the "before" side.

    One detail in the suggested fix (detectEncodingFromBuffer from packages/agent-tools/src/shared/write-capture.ts:106-113)
    does not cover the reported case. That helper only recognizes a UTF-8 BOM and a UTF-16LE BOM;
    GBK has no BOM, so it returns { encoding: 'utf-8', bom: false } and the bytes are still
    decoded as UTF-8. The isValidUtf8 check that also exists in the repo
    (packages/local-runtime/src/turns/file-changes.ts:709) is the half that actually catches it —
    a fatal TextDecoder('utf-8') throws on c4 e3 ba c3, while detectEncodingFromBuffer alone
    does not. Suggest using both, or the UTF-16LE branch would also need to be handled: with a
    UTF-16LE file, pi's own stripBom at edit.ts:345 operates on the already-lossily-decoded
    string, so the BOM is lost and the file is rewritten as mixed UTF-8/UTF-16LE.

    Finding #2 is already fixed on main. The BOM/CRLF mismatch in replace_all is gone:
    packages/agent-tools/src/shared/replace-all-edit.ts:72-74 now normalizes both sides before
    counting.

    const content = normalizeToLF(stripBom(rawContent).text);
    const needle = normalizeToLF(oldString);

    That change is not in the revision the report was written against (da22f11, v0.6.5); it
    landed in 698a986 ("feat: sync and release minimax Code 0.7.2", #566). The released 0.7.8
    carries it — the shipped bundle contains the same stripBom+normalizeToLF pair before the
    occurrence count. So #2 needs no further work.

    The related observation about mixed line endings, however, is still reproducible. One edit of a
    single line rewrites the endings of every untouched line:

    "aaa\r\nbbb\nccc\n"  +  edit "bbb" -> "B"   =>  "aaa\r\nB\r\nccc\r\n"
    

    detectLineEnding (third_party/pi-mono/.../core/tools/edit-diff.ts:11-17) picks the ending of
    the first line and restoreLineEndings (:23-25) then applies it to the whole file, so the
    untouched ccc line changes from LF to CRLF. The inverse also happens: "aaa\nbbb\r\nccc\r\n"
    becomes "aaa\nB\nccc\n", dropping 4 bytes. This is a whole-file rewrite triggered by a
    one-line edit, and it is the case where Windows matters most, because CRLF is the default
    on-disk ending there.

    Finding #3 — the read-path config rewrite — and the Windows dimension the proposed fix misses.

    packages/config/src/private-config-file.ts:27-31 truncates and rewrites the file in place,
    with no lock and no backup, and the repository's own test codifies the resulting empty file
    (packages/config/test/managed-preset-sync.test.ts:105-114, "does not hide a failure after the
    original file has been truncated" — asserts fs.readFileSync(file, "utf8") is ""). I ran that
    suite on this machine: 16/16 pass, so the empty-file end state is currently asserted behaviour
    rather than an accident.

    The suggested fix is to route these writes through the existing lock + temp-file + rename helper
    (packages/config/src/local-model-provider-write.ts:246-264). On Windows that rename is not
    unconditional, and the difference is measurable:

    fs.renameSync(tmpPath, configPath)
    
    situation Windows (this machine) POSIX (alpine, uid 1000)
    target exists, nobody holds it OK OK
    target held open by another process EPERM (rename) OK
    target carries the Windows read-only attribute EPERM (rename) OK

    The POSIX control was run in the same session with the same sequence
    (mv -f tmp target); POSIX replaces the target unconditionally in all three rows, and mv over
    a target another process holds open succeeds there. On Windows the sharing mode matters: with
    FileShare::ReadWriteDelete the rename succeeds, with FileShare::None, ::Read, ::ReadWrite
    or ::Delete it fails with EPERM. The read-only-attribute row has no POSIX counterpart at all —
    POSIX mv over a mode-0444 target succeeds, since the permission bits of the target do not
    gate a rename.

    Two consequences for the fix as proposed:

    1. The failure is not transient in the way the existing retry helper assumes. packages/shared/src/windows-file-system.ts:12
      (retryWindowsFileSystemOperation) retries EACCES/EBUSY/EPERM on win32 with a
      4-attempt, 50 ms-linear-backoff budget. The waits are 50 + 100 + 150 + 200 = 500 ms in total, so
      the last attempt happens at t ≈ 500 ms. Measured against a held lock, that budget covers a lock
      held for up to ~450 ms, but not for 500 ms or more:

      lock held  400 ms -> bare rename: EPERM (1 ms)   | with the repo's retry policy: OK (529 ms)
      lock held  450 ms -> bare rename: EPERM (1 ms)   | with the repo's retry policy: OK (528 ms)
      lock held  500 ms -> bare rename: EPERM (1 ms)   | with the repo's retry policy: EPERM (543 ms)
      lock held  600 ms -> bare rename: EPERM (1 ms)   | with the repo's retry policy: EPERM (528 ms)
      lock held 1200 ms -> bare rename: EPERM (1 ms)   | with the repo's retry policy: EPERM (528 ms)
      

      A lock held by an antivirus scanner or a file indexer can easily outlive 500 ms, so the
      helper raises the success rate but does not make the write reliable.

    2. The rename-based fix converts a silent failure into a loud one, which is an improvement.
      Note that the two helper variants differ in the debris they leave:
      packages/config/src/local-model-provider-write.ts:260-262 catches the rename failure,
      unlinks the temp file and throws a LocalModelProviderConfigWriteError — but the catch
      block has no binding, so the original EPERM is swallowed and the error the caller sees
      carries no errno. writeFileDurably in
      packages/local-runtime/src/channels/durable-yaml.ts:85-91 instead unlinks the temp file in
      a finally and rethrows the original error, leaving the target untouched and intact. That
      difference matters on Windows, where EPERM is the expected outcome of a locked or
      read-only config.yaml and the caller needs the errno to decide between "retry" and
      "give up".

    The part that is not yet covered on Windows: the three remaining config.yaml write paths
    still rename without any retry wrapper, while the auth/credential writers in the same repository
    already use one:

    site upstream/main retry wrapper
    packages/config/src/local-model-provider-write.ts:259 await fs.promises.rename(tmpPath, filePath) none
    packages/config/src/tui-status-line-write.ts:47 await fs.rename(temporaryPath, targetPath) none
    packages/local-runtime/src/config/update.ts:298 await fs.promises.rename(tmpPath, filePath) none
    packages/config/src/local-runtime-auth-context.ts:88 retryWindowsFileSystemOperation(() => fs.renameSync(tmpPath, filePath)) yes
    packages/tui/src/auth/storage.ts:285 retryWindowsFileSystemOperation(() => fs.renameSync(...)) yes
    packages/tui/src/auth/identity-storage.ts:149 retryWindowsFileSystemOperation(() => fs.renameSync(...)) yes

    So the repository already contains the Windows-correct primitive and already applies it to the
    credential stores — it just is not applied to the config.yaml writers. Hoisting
    atomicWriteFile from packages/config/src/local-model-provider-write.ts:246 into
    @mavis/config as suggested would carry that gap along with it.

    If the fix is applied to the read-path rewriters, one more detail needs deciding: on Windows a
    failed rename leaves the original config.yaml intact but the new content unwritten, whereas
    the current truncate-then-write path leaves the file at 0 bytes. Both are data loss from the
    user's point of view, but only one of them is recoverable — the read-only or locked file can be
    unlocked and the write retried, whereas a 0-byte config.yaml has already lost the settings.
    Making the read-path writers use temp + rename, and keeping the .bak snapshot that
    packages/config/src/byok-config.ts:276 (backupConfigForMigration) already performs for the
    BYOK migration, would preserve both properties.

    I did not reproduce the config.yaml rewrite end-to-end through the CLI: with an isolated
    profile (MINIMAX_DATA_DIR/MAVIS_DATA_DIR/HOME/USERPROFILE redirected to a temp
    directory) mcode --version, mcode init and mcode exec did not rewrite the file, and
    mcode exec aborts with "Sign in to MiniMax to use Agent features" before the config is
    touched. The managed-runtime gate (packages/config/src/config.ts:174-186, isManagedRuntime())
    requires __MAVIS_RUNTIME_MANAGED=1, an Electron process, or MAVIS_BUILD_ENV=prod|staging;
    with that set the shipped 0.7.8 still did not rewrite the file in my run, so I am reporting
    the write-path behaviour from the source and the repository's own test rather than from an
    observed CLI run. The truncation behaviour itself is directly asserted by
    packages/config/test/managed-preset-sync.test.ts:105-114, which passes here.

    I did not check findings #4 (torn JSONL line) and #5 (symlinked config.yaml) in detail —
    packages/local-runtime-v2/src/infra/file/jsonl.ts:60 does throw Malformed JSONL without an
    observer, and packages/local-runtime/src/config/update.ts:298 and
    packages/config/src/local-model-provider-write.ts:259 do rename without resolving
    realpath first, so a symlinked config.yaml is replaced by a regular file. On Windows
    symlink creation needs Developer Mode or elevation, so I could not exercise #5 here either.


    Edit 2026-11-19: the retry-budget figures in point 2 were wrong — re-measured, and the corrected numbers are in place. A 600 ms lock is not covered by the helper; the budget spans ~500 ms in total, with the last attempt at t ≈ 500 ms. The conclusion (the helper raises the success rate but does not make the write reliable) is unchanged.

  2. syxscott commented on Nov 20, 2026

    @syxscott
    Author

    Thanks for the reproductions, the version archaeology, and for catching the two details that materially change the fix plan. I have verified every factual claim against origin/main @ 3c21163 — all nine check out. Verdict: agreed with the Windows refinement; the fix plan below is amended accordingly, with one scoping note.

    What I verified on origin/main @ 3c21163

    • retryWindowsFileSystemOperation (packages/shared/src/windows-file-system.ts:1-31): win32-only, EACCES/EBUSY/EPERM, 4 retries with 50 ms linear backoff — five executions at t ≈ 0 / 50 / 150 / 300 / 500 ms. Your budget arithmetic (a ~400 ms lock is covered, ≥500 ms is not) matches the code.
    • atomicWriteFile (local-model-provider-write.ts:246-264): the catch has no binding and rethrows a bare LocalModelProviderConfigWriteError() — the original errno is dropped, so a caller cannot distinguish a retryable EPERM/EBUSY from a permanent failure. Your contrast with writeFileDurably (durable-yaml.ts:85-91: unlink-in-finally + rethrow the original error) is the better shape.
    • The three config.yaml writers rename without any retry wrapper (local-model-provider-write.ts:259, tui-status-line-write.ts:47, local-runtime/config/update.ts:298) while the credential writers all wrap theirs (local-runtime-auth-context.ts:88, tui/auth/storage.ts:285, auth/identity-storage.ts:149). Your table is the right checklist.
    • Finding 中国互联网烂透了 #2: confirmed fixed upstream — replace-all-edit.ts:71-73 now does normalizeToLF(stripBom(rawContent).text) / normalizeToLF(oldString). The mixed-line-ending normalization observation remains reproducible and stays open as its own item.
    • Finding Linux Support: please 🥺️ #1 refinement: confirmed — detectEncodingFromBuffer (write-capture.ts:106-113) recognizes only the UTF-8 and UTF-16LE BOMs, so for BOM-less GBK the fatal-UTF-8 validity check is the operative half. The original proposal paired both, but your ordering point stands: validity check first, BOM sniff second — and the UTF-16LE mixed-rewrite risk deserves an explicit test either way.
    • The truncate-then-write itself is unchanged on main (private-config-file.ts:27-31: ftruncateSync(fd, 0) + writeFileSync(fd, content)), and managed-preset-sync.test.ts:105-114 still asserts the empty-file end state.

    One scoping note

    "Does not cover Windows" is one step too strong for the temp+rename core. The goal of that change is crash consistency — at no instant is config.yaml 0 bytes or partial — and that property holds on Windows too: a failed rename (EPERM on a locked or read-only target) leaves the original file intact, and only the temp file needs cleanup. What temp+rename alone does not provide is write success under contention — which is exactly your point. The same event that truncate-then-write turns into silent unrecoverable loss becomes, with rename, a loud recoverable failure; your additions decide how often the loud path is taken and what the caller learns from it. Both layers belong in the fix, and neither substitutes for the other.

    Amended fix plan (adopting your refinements)

    1. Read-path rewriters (the private-config-file.ts callers: managed-preset sync, legacy BYOK migration) switch to temp-file + fsync + rename — no more truncate-then-write.
    2. The rename is wrapped in retryWindowsFileSystemOperation, matching the credential writers, instead of hoisting the no-retry atomicWriteFile as-is.
    3. The unified helper adopts the writeFileDurably error semantics: unlink the temp file in a finally and rethrow the original error, so EPERM/EBUSY/EACCES reach the caller with their errno intact.
    4. Keep the .bak snapshot (backupConfigForMigration pattern) for the read-path rewrites — belt and suspenders for the failure classes retry cannot cover (disk full between temp write and rename, crash before rename).
    5. Flip managed-preset-sync.test.ts:105-114 from asserting the empty-file end state to asserting crash consistency: after a mid-write failure the original content is still readable at configPath (or a .bak exists).

    On the end-to-end reproduction: same result here — the managed-runtime gate (isManagedRuntime()) is the trigger and an unmanaged profile never reaches the rewriter, which is why the finding was sourced from the code path plus the repository's own test. Whoever picks this up should treat the assertion flip in (5) as the acceptance criterion.

    Happy to provide the amended patch on my fork if the config team wants to pick it up, or to review if you take it — the reproduction work you did here is most of the way to the PR already.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions