Repository navigation
Description
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
editwithold_string: "TOKEN"succeeds (BOM preserved). The identical call withreplace_all: truethrowsCould not find the exact text in …. - CRLF file
alpha TOKEN\r\nbeta TOKEN\r\ngamma\r\n, multi-line LFold_string: "beta TOKEN\ngamma": a single edit succeeds (file keeps CRLF). The same call withreplace_all: truethrowsreplace_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
- 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.
replace_all: trueshould accept exactly theold_stringvalues a singleeditaccepts, including on BOM and CRLF files.- A failure or crash during a read-path config rewrite must leave the original
config.yamlintact (atomic rename, or at least a.bak), instead of leaving a 0-byte file that silently resets to default settings on next load. - A v2 session with one torn JSONL line should be repaired on open (v1 already does this), instead of throwing
Malformed JSONLon every subsequent read and write forever. - Saving settings should preserve a symlinked
config.yaml(liketui-status-line-writealready 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
Ralle1976 commentedon Nov 20, 2026
Data-integrity findings #1 and #2 I could reproduce against
main— but #2 is already fixedupstream, and #3 has a Windows-specific failure mode that the proposed fix does not cover.
Versionsanker
mcode --version→0.7.8(npm, global)npm view @minimax-ai/code version→0.7.8upstream/main@3c21163(package version0.7.2), tagv0.7.2=c6c7f28v24.18.0, pnpm9.15.0alpine:latestin Docker (uid 1000), BusyBoxmv, gleiche MessfolgeReproduced 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:
Three U+FFFD replacement characters materialize where the GBK bytes were; the tool reports
success.
edit-capture.ts:29writes the whole file back with'utf8', so the loss ispermanent. The diff hides it, because both sides of the diff come from the same lossy decode
(
originalFileatedit-capture.ts:32is alsooriginal.toString('utf8')) — the U+FFFDs arealready present on the "before" side.
One detail in the suggested fix (
detectEncodingFromBufferfrompackages/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 stilldecoded as UTF-8. The
isValidUtf8check 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 onc4 e3 ba c3, whiledetectEncodingFromBufferalonedoes not. Suggest using both, or the UTF-16LE branch would also need to be handled: with a
UTF-16LE file,
pi's ownstripBomatedit.ts:345operates on the already-lossily-decodedstring, 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 inreplace_allis gone:packages/agent-tools/src/shared/replace-all-edit.ts:72-74now normalizes both sides beforecounting.
That change is not in the revision the report was written against (
da22f11, v0.6.5); itlanded in
698a986("feat: sync and release minimax Code 0.7.2", #566). The released0.7.8carries it — the shipped bundle contains the same
stripBom+normalizeToLFpair before theoccurrence 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:
detectLineEnding(third_party/pi-mono/.../core/tools/edit-diff.ts:11-17) picks the ending ofthe first line and
restoreLineEndings(:23-25) then applies it to the whole file, so theuntouched
cccline 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 aone-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-31truncates 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 theoriginal file has been truncated" — asserts
fs.readFileSync(file, "utf8")is""). I ran thatsuite 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 notunconditional, and the difference is measurable:
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, andmvovera target another process holds open succeeds there. On Windows the sharing mode matters: with
FileShare::ReadWriteDeletethe rename succeeds, withFileShare::None,::Read,::ReadWriteor
::Deleteit fails with EPERM. The read-only-attribute row has no POSIX counterpart at all —POSIX
mvover a mode-0444 target succeeds, since the permission bits of the target do notgate a rename.
Two consequences for the fix as proposed:
The failure is not transient in the way the existing retry helper assumes.
packages/shared/src/windows-file-system.ts:12(
retryWindowsFileSystemOperation) retriesEACCES/EBUSY/EPERMonwin32with a4-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:
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.
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-262catches the rename failure,unlinks the temp file and throws a
LocalModelProviderConfigWriteError— but thecatchblock has no binding, so the original
EPERMis swallowed and the error the caller seescarries no errno.
writeFileDurablyinpackages/local-runtime/src/channels/durable-yaml.ts:85-91instead unlinks the temp file ina
finallyand rethrows the original error, leaving the target untouched and intact. Thatdifference matters on Windows, where
EPERMis the expected outcome of a locked orread-only
config.yamland 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.yamlwrite pathsstill rename without any retry wrapper, while the auth/credential writers in the same repository
already use one:
packages/config/src/local-model-provider-write.ts:259await fs.promises.rename(tmpPath, filePath)packages/config/src/tui-status-line-write.ts:47await fs.rename(temporaryPath, targetPath)packages/local-runtime/src/config/update.ts:298await fs.promises.rename(tmpPath, filePath)packages/config/src/local-runtime-auth-context.ts:88retryWindowsFileSystemOperation(() => fs.renameSync(tmpPath, filePath))packages/tui/src/auth/storage.ts:285retryWindowsFileSystemOperation(() => fs.renameSync(...))packages/tui/src/auth/identity-storage.ts:149retryWindowsFileSystemOperation(() => fs.renameSync(...))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.yamlwriters. HoistingatomicWriteFilefrompackages/config/src/local-model-provider-write.ts:246into@mavis/configas 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.yamlintact but the new content unwritten, whereasthe 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.yamlhas already lost the settings.Making the read-path writers use temp + rename, and keeping the
.baksnapshot thatpackages/config/src/byok-config.ts:276(backupConfigForMigration) already performs for theBYOK migration, would preserve both properties.
I did not reproduce the
config.yamlrewrite end-to-end through the CLI: with an isolatedprofile (
MINIMAX_DATA_DIR/MAVIS_DATA_DIR/HOME/USERPROFILEredirected to a tempdirectory)
mcode --version,mcode initandmcode execdid not rewrite the file, andmcode execaborts with "Sign in to MiniMax to use Agent features" before the config istouched. The managed-runtime gate (
packages/config/src/config.ts:174-186,isManagedRuntime())requires
__MAVIS_RUNTIME_MANAGED=1, an Electron process, orMAVIS_BUILD_ENV=prod|staging;with that set the shipped
0.7.8still did not rewrite the file in my run, so I am reportingthe 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:60does throwMalformed JSONLwithout anobserver, and
packages/local-runtime/src/config/update.ts:298andpackages/config/src/local-model-provider-write.ts:259dorenamewithout resolvingrealpathfirst, so a symlinkedconfig.yamlis replaced by a regular file. On Windowssymlink 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.
syxscott commentedon Nov 20, 2026
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@ 3c21163retryWindowsFileSystemOperation(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): thecatchhas no binding and rethrows a bareLocalModelProviderConfigWriteError()— the original errno is dropped, so a caller cannot distinguish a retryableEPERM/EBUSYfrom a permanent failure. Your contrast withwriteFileDurably(durable-yaml.ts:85-91: unlink-in-finally+ rethrow the original error) is the better shape.config.yamlwriters 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.replace-all-edit.ts:71-73now doesnormalizeToLF(stripBom(rawContent).text)/normalizeToLF(oldString). The mixed-line-ending normalization observation remains reproducible and stays open as its own item.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.main(private-config-file.ts:27-31:ftruncateSync(fd, 0)+writeFileSync(fd, content)), andmanaged-preset-sync.test.ts:105-114still 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.yaml0 bytes or partial — and that property holds on Windows too: a failed rename (EPERMon 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)
private-config-file.tscallers: managed-preset sync, legacy BYOK migration) switch to temp-file + fsync + rename — no more truncate-then-write.retryWindowsFileSystemOperation, matching the credential writers, instead of hoisting the no-retryatomicWriteFileas-is.writeFileDurablyerror semantics: unlink the temp file in afinallyand rethrow the original error, soEPERM/EBUSY/EACCESreach the caller with their errno intact..baksnapshot (backupConfigForMigrationpattern) 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).managed-preset-sync.test.ts:105-114from asserting the empty-file end state to asserting crash consistency: after a mid-write failure the original content is still readable atconfigPath(or a.bakexists).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.