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

+11 -11
View File
@@ -309,7 +309,7 @@ private func threeDecimals(_ value: String) -> Bool {
let withForeign = try String(contentsOf: url, encoding: .utf8)
.replacingOccurrences(of: "---\ntype: post", with: "---\nforeign_key: keep-me\ntype: post")
try withForeign.write(to: url, atomically: true, encoding: .utf8)
let bodyBefore = PostStore.splitFrontmatter(withForeign).body
let bodyBefore = PostFrontmatter.split(withForeign).body
let captured = PostStore.captureFirstDraftStats(
id: post.id, body: "# Draft\n\nOne two three. Four five! Six?", home: home,
@@ -317,7 +317,7 @@ private func threeDecimals(_ value: String) -> Bool {
#expect(captured)
let after = try String(contentsOf: url, encoding: .utf8)
let matter = Post.frontmatter(of: after) // the trend-view readiness read
let matter = PostFrontmatter.parse(after) // the trend-view readiness read
#expect(matter["foreign_key"] == "keep-me")
#expect(matter["stats_captured"]?.count == 19) // yyyy-MM-dd'T'HH:mm:ss, lexically sortable
#expect(matter["stats_burstiness"].map(threeDecimals) == true)
@@ -328,7 +328,7 @@ private func threeDecimals(_ value: String) -> Bool {
#expect(matter["pass_state"] == "First Draft")
#expect(matter["origin"] == "writer")
// Body byte-identical — the stamp never touches content (#31 in-place proof).
#expect(PostStore.splitFrontmatter(after).body == bodyBefore)
#expect(PostFrontmatter.split(after).body == bodyBefore)
}
@Test func `capture refuses an ai origin piece untouched`() throws {
@@ -369,7 +369,7 @@ private func threeDecimals(_ value: String) -> Bool {
#expect(PostStore.captureFirstDraftStats(id: post.id, body: "One two three. Four five! Six?", home: home))
#expect(PostStore.captureFirstDraftStats(id: post.id, body: "a b c. a b c.", home: home))
let matter = try Post.frontmatter(of: String(
let matter = try PostFrontmatter.parse(String(
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
))
#expect(matter["stats_burstiness"] == "-1.000") // σ = 0 over [3, 3] — the second body won
@@ -394,7 +394,7 @@ private func threeDecimals(_ value: String) -> Bool {
let after = try String(
contentsOf: home.appendingPathComponent("projects/posts/bare-legacy/post.md"), encoding: .utf8,
)
let matter = Post.frontmatter(of: after)
let matter = PostFrontmatter.parse(after)
#expect(matter["stats_captured"] != nil)
#expect(after.contains("# Bare")) // the body survived the fresh-block prepend
}
@@ -416,14 +416,14 @@ private func threeDecimals(_ value: String) -> Bool {
#expect(advanced == .pass1Done)
let after = try String(contentsOf: url, encoding: .utf8)
let matter = Post.frontmatter(of: after)
let matter = PostFrontmatter.parse(after)
#expect(matter["pass_state"] == "Pass-1 Done")
#expect(matter["stats_captured"] != nil) // survived the advance
#expect(matter["stats_trigram_repetition"] != nil)
#expect(matter["foreign_key"] == "keep-me") // foreign keys too — the latent #24 loss
#expect(matter["origin"] == "writer")
#expect(matter["type"] == "post")
#expect(PostStore.splitFrontmatter(after).body == "# Kept\n\nBody stays.")
#expect(PostFrontmatter.split(after).body == "# Kept\n\nBody stays.")
}
@Test func `overwrite preserves stats and foreign keys while swapping the body`() throws {
@@ -442,13 +442,13 @@ private func threeDecimals(_ value: String) -> Bool {
try PostStore.overwrite(id: post.id, body: "# Draft\n\nEdited mid-pass words.", home: home)
let after = try String(contentsOf: url, encoding: .utf8)
let matter = Post.frontmatter(of: after)
let matter = PostFrontmatter.parse(after)
#expect(matter["stats_captured"] != nil) // survived the editor save
#expect(matter["foreign_key"] == "keep-me")
#expect(matter["pass_state"] == "First Draft")
#expect(matter["origin"] == "writer")
#expect(matter["type"] == "post")
#expect(PostStore.splitFrontmatter(after).body == "# Draft\n\nEdited mid-pass words.") // the save still landed
#expect(PostFrontmatter.split(after).body == "# Draft\n\nEdited mid-pass words.") // the save still landed
}
@Test func `overwrite normalizes a bare post with the full stamp`() throws {
@@ -465,9 +465,9 @@ private func threeDecimals(_ value: String) -> Bool {
let after = try String(
contentsOf: home.appendingPathComponent("projects/posts/bare-overwrite/post.md"), encoding: .utf8,
)
let matter = Post.frontmatter(of: after)
let matter = PostFrontmatter.parse(after)
#expect(matter["type"] == "post")
#expect(matter["pass_state"] == "First Draft")
#expect(matter["origin"] == "writer")
#expect(PostStore.splitFrontmatter(after).body == "Bare body, edited.")
#expect(PostFrontmatter.split(after).body == "Bare body, edited.")
}