refactor(posts): one frontmatter read/write codec
CI / ci (push) Canceled after 0s

#180: one PostFrontmatter module now owns the lenient read
(parse/split/decoded) and every write posture — fresh stamp,
merge-import, update-keys — behind one known-key table (type,
pass_state, origin; one insertion order). The three near-twin walkers
are deleted byte-identically: PostStore.stamped/splitFrontmatter/
mergedFrontmatter, LearningDecay.stampingFrontmatter,
Post.frontmatter(of:) — 21 call sites swept across seven production
files and eight test files.

The Unaided First Draft eligibility is named once and consumed by both
gates: isUnaidedFirstDraftPair (writer ∧ post — the scan's read-back
gate) and isCaptureEligible (pair ∧ First Draft — the capture gate at
pass invocation). The lenient ?? default stamp decode is extracted
with it (the third copy's deferred extraction).

One deliberate divergence from the absorbed writer, doc-commented and
pinned: mergingImport now preserves hand-edit lines above the opening
--- on import where the old split-first shape silently dropped them —
lossless through every posture is the ticket's contract. Seven codec
pins (PostFrontmatterTests) cover the posture round-trip (foreign keys
verbatim through import → save → advance → micropub), the lenient
unknown-type decode, and the predicates' truth table.

Existing pieces are untouched on disk: every posture moved verbatim
and the full suite pins the byte shapes (664/664 green; the one
under-load flake in the #179 repoint pin is pre-existing on main,
proven by a stashed-tree control run).

Closes #180
This commit is contained in:
AlexKucera committed 2026-11-10 10:21:32 +01:00
1 parent 5bf09552f6
commit 218e2db397
22 files changed
+479 -221

No files matched your search

+13 -13
View File
@@ -74,7 +74,7 @@ struct LearningDecayTests {
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file, now: now)
let stampedText = try readLearning(home, file)
let frontmatter = Post.frontmatter(of: stampedText)
let frontmatter = PostFrontmatter.parse(stampedText)
#expect(frontmatter["dismissed"] == FixCardStore.formatted(now, "yyyy-MM-dd"))
#expect(frontmatter["dismiss_count"] == "1")
#expect(frontmatter["dismissed_at_pass"] == "1")
@@ -85,7 +85,7 @@ struct LearningDecayTests {
#expect(frontmatter["promoted"] == "2026-10-21")
#expect(frontmatter["from_card"] == "6D9DB4F2-82F9-4C64-9C2B-1A21B4E9B5D0")
// The body is byte-identical.
#expect(PostStore.splitFrontmatter(stampedText).body == PostStore.splitFrontmatter(learning).body)
#expect(PostFrontmatter.split(stampedText).body == PostFrontmatter.split(learning).body)
// Hidden from the pass rail.
#expect(try FixCardStore.learnings(piecePath: piece.path).isEmpty)
}
@@ -125,7 +125,7 @@ struct LearningDecayTests {
#expect(try FixCardStore.learnings(piecePath: piece.path).count == 1) // resurfaced
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file)
let frontmatter = try Post.frontmatter(of: readLearning(home, file))
let frontmatter = try PostFrontmatter.parse(readLearning(home, file))
#expect(frontmatter["dismiss_count"] == "2")
#expect(frontmatter["dismissed_at_pass"] == "10") // re-anchored at the current clock
@@ -221,7 +221,7 @@ struct LearningDecayTests {
#expect(throws: FixCardStore.CardError.self) {
try LearningDecay.dismiss(home: hugeHome, typeDir: "posts", file: "20261021-huge.md")
}
#expect(try Post.frontmatter(of: readLearning(hugeHome, "20261021-huge.md"))["dismiss_count"] == String(Int.max))
#expect(try PostFrontmatter.parse(readLearning(hugeHome, "20261021-huge.md"))["dismiss_count"] == String(Int.max))
}
/// Scenario: dismiss is a write-path trust boundary — traversal-shaped
@@ -251,7 +251,7 @@ struct LearningDecayTests {
// Nothing escaped the learnings tree; the real learning is untouched.
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("x.md").path))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("learnings/x.md").path))
#expect(try Post.frontmatter(of: readLearning(home, file))["dismiss_count"] == nil)
#expect(try PostFrontmatter.parse(readLearning(home, file))["dismiss_count"] == nil)
}
/// Scenario: a pre-planted symlinked learnings component must not
@@ -326,7 +326,7 @@ struct LearningDecayTests {
#expect(throws: FixCardStore.CardError.self) {
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file)
}
#expect(try Post.frontmatter(of: readLearning(home, file))["dismiss_count"] == "1")
#expect(try PostFrontmatter.parse(readLearning(home, file))["dismiss_count"] == "1")
// A negative hand-edited count clamps to 0 at parse → stamp writes 1.
let negHome = try toolsHome()
@@ -338,7 +338,7 @@ struct LearningDecayTests {
---
""")
try LearningDecay.dismiss(home: negHome, typeDir: "posts", file: file)
#expect(try Post.frontmatter(of: readLearning(negHome, file))["dismiss_count"] == "1")
#expect(try PostFrontmatter.parse(readLearning(negHome, file))["dismiss_count"] == "1")
}
/// Scenario: decay is defined per pass-clocked content type — a foreign
@@ -360,11 +360,11 @@ struct LearningDecayTests {
contentsOf: home.appendingPathComponent("learnings/essays/\(file)"),
encoding: .utf8,
)
#expect(Post.frontmatter(of: text)["dismiss_count"] == nil)
#expect(PostFrontmatter.parse(text)["dismiss_count"] == nil)
}
/// Scenario: a Learning with NO frontmatter (a hand-made file) still
/// dismisses — stampingFrontmatter prepends a fresh block and the body
/// dismisses — the update walk prepends a fresh block and the body
/// rides verbatim below it (#27 audit: the prepend branch pinned).
@Test func `dismiss prepends a fresh block on a frontmatter-less file`() throws {
let home = try toolsHome()
@@ -375,14 +375,14 @@ struct LearningDecayTests {
try LearningDecay.dismiss(home: home, typeDir: "posts", file: file)
let stampedText = try readLearning(home, file)
let frontmatter = Post.frontmatter(of: stampedText)
let frontmatter = PostFrontmatter.parse(stampedText)
#expect(frontmatter["dismiss_count"] == "1")
#expect(frontmatter["dismissed_at_pass"] == "0")
#expect(PostStore.splitFrontmatter(stampedText).body == body)
#expect(PostFrontmatter.split(stampedText).body == body)
}
/// Scenario: lines ABOVE the opening `---` (a hand-edit blank —
/// Post.frontmatter tolerates one) belong to neither block nor body;
/// PostFrontmatter.parse tolerates any count) belong to neither block nor body;
/// the stamp keeps them — the "every other line verbatim" contract
/// (#27 review pin).
@Test func `dismiss preserves lines above the frontmatter block`() throws {
@@ -394,7 +394,7 @@ struct LearningDecayTests {
let stampedText = try readLearning(home, file)
#expect(stampedText.hasPrefix("\n---\n")) // the leading blank survived
#expect(Post.frontmatter(of: stampedText)["dismiss_count"] == "1")
#expect(PostFrontmatter.parse(stampedText)["dismiss_count"] == "1")
#expect(stampedText.hasSuffix("\n\n# Blank\n")) // body untouched
}
}