Skip to content

fix(logfmt): read pre-escaping values as written - #19

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/logfmt-legacy-escapes
Oct 21, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/logfmt-legacy-escapes

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Closes #14. This is the follow-up to #9, which caused the problem.

The issue left the choice to you, so treat this as a proposal. If you'd rather go with the document-only option, close this and I'll send that instead.

The problem: before v0.2.3, backslashes were stored raw. After #9, the reader decodes \n, \t, \r and \ as escapes everywhere, so an older C:\new\temp or a literal "\n" in prose reads back with a real newline or tab. Some write paths then save the wrong value: the deletions audit log, clearSuperseded, and the viewer's edit box.

Why a marker rather than a migration: there's no reliable way to tell an old line from a new one after the fact, and the store is shared between projects. A project that pins v0.2.2 keeps writing old-format lines into the same files after any one-time migration has run. A marker on new lines stays correct whatever mix of versions writes to the store.

Change

  • formatLine adds esc=1 at the end of a line only when one of its quoted values actually uses an escape (it contains a backslash, newline, CR or tab). Every other line is byte-for-byte what it was before.
  • parseLine unescapes only the fields before the marker. Lines without it, and any field an older version appended after it, are read the old way, where only \" is an escape. esc is never returned as a field.
  • markSuperseded now rewrites the whole line through formatLine instead of appending fields to the raw line. A legacy row it touches comes out in the new format with its values unchanged. (Appending new-format fields to an old line would have mixed the two formats in one line.)
  • Content can't fake the marker: it's quoted, so esc=1 inside a value is never read as a field. The single-line guarantee from fix: escape newlines and backslashes in logfmt values #9 is unchanged.

One known gap: rows written by v0.2.3 to v0.3.0, which use escapes but have no marker, are read the old way. Their \n shows as a literal backslash-n, and a backslash shows doubled. Only rows written in the day since v0.2.3 that contain a backslash, newline or tab are affected. The alternative would have misread every older row instead.

Tests (test/logfmt.test.ts)

  • The issue's examples (the UNC path, printf "%s\n") and C:\new\temp read back as written.
  • The marker appears only when needed and is never returned as a field.
  • A field appended after the marker is read the old way.
  • Content containing esc=1 round-trips.
  • Superseding a raw legacy row keeps its content and rewrites the line in the new format.
  • With the change reverted, four of the new tests fail.

Checks: bun test 195 tests, 0 fail, 4 skip. tsc --noEmit is clean. biome lint reports no new findings in the touched files. Only standard Bun and TypeScript are used, so it behaves the same on macOS.

🤖 Generated with Claude Code

WhollyMindless and others added 2 commits October 21, 2026 13:52
Lines written before v0.2.3 stored backslashes raw, so `C:\new` or a
literal `\n` in prose was decoded as a newline on read. A line whose
values use the escapes now ends with `esc=1`, and only fields before
that marker are unescaped. Lines without it, and fields an older
version appended after it, are read the old way, where only `\"` is an
escape. Lines with no backslashes, newlines or tabs read the same
either way and carry no marker, so most lines are unchanged.

markSuperseded rewrites the whole line instead of appending to it, so
a legacy row it touches comes out in the current format with the same
values.

Closes abogartz#14

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts: test/logfmt.test.ts (import lines only). Union kept: the
marker tests need config/memoryRoot + markSuperseded, main's abogartz#23
concurrency test needs appendLog/readLines. src/logfmt.ts auto-merged
(their esc=1 reader/writer + main's appendFile O_APPEND) and was
reviewed hunk-by-hunk; memory.ts likewise (appendMemory caps +
markSuperseded whole-line rewrite are independent).

Combined suite: 205 pass / 4 skip / 0 fail; tsc clean; biome lint exit 0.
@abogartz

Copy link
Copy Markdown
Owner

Merge conflict resolved — merged current main into the branch (096113c). Only test/logfmt.test.ts conflicted, on the import lines: kept the union (your memoryRoot/markSuperseded + main's appendLog/readLines from the #23 fix).

src/logfmt.ts and src/memory.ts auto-merged; I reviewed both hunk-by-hunk — your esc=1 reader/writer and main's appendFile O_APPEND + MAX_CONTENT_CHARS caps are independent and coexist correctly. Note the interaction your #14 fix benefits from: markSuperseded's whole-line rewrite now goes through formatLine on a main where supersede appends are already single-format, so legacy rows touched by supersede get normalized exactly as your tests describe.

Verified on the merged head: bun test 205 pass / 4 skip / 0 fail (your 6 new cases + main's concurrent-append storm), tsc --noEmit clean, biome lint exit 0.

@abogartz
abogartz merged commit 7a367a2 into abogartz:main Oct 21, 2026
2 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.3.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless
WhollyMindless deleted the fix/logfmt-legacy-escapes branch October 26, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logfmt: legacy values containing literal escape sequences (\\n, \\t, \\\\) re-decode on read — pick an explicit policy

2 participants