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 -31
View File
@@ -179,15 +179,14 @@ nonisolated struct Post: Identifiable, Equatable {
/// kept this a pure function of (text, location); the cache memoizes
/// its output, nothing else).
private static func parse(text: String, id: String, project: String, isArchived: Bool) -> Post {
let frontmatter = frontmatter(of: text)
let origin = Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer
let type = ContentType(rawValue: frontmatter["type"] ?? "") ?? .post
let frontmatter = PostFrontmatter.parse(text)
let stamp = PostFrontmatter.decoded(frontmatter)
return Post(
id: id,
title: title(of: text),
passState: PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
origin: origin,
type: type,
passState: stamp.passState,
origin: stamp.origin,
type: stamp.type,
project: project,
isArchived: isArchived,
// #33: the remembered Micro.blog draft URL —
@@ -199,37 +198,18 @@ nonisolated struct Post: Identifiable, Equatable {
micropubURL: frontmatter["micropub_url"].flatMap {
Micropub.trustworthyURL($0) ? $0 : nil
},
// #32 read-back gate: the capture gate's immutable
// pair (origin writer, type post) — NOT pass_state,
// which legitimately advances after the capture.
// `stats_*` keys can arrive by import (external YAML
// rides verbatim), so an imported ai piece or outline
// #32/#180 read-back gate: the named immutable pair
// (origin writer, type post) — NOT pass_state, which
// legitimately advances after the capture. `stats_*`
// keys can arrive by import (external YAML rides
// verbatim), so an imported ai piece or outline
// carrying hand-stamped keys still never renders;
// a migrated writer piece keeps its real arc.
firstDraftStats: origin == .writer && type == .post
firstDraftStats: PostFrontmatter.isUnaidedFirstDraftPair(frontmatter)
? Post.FirstDraftStats(frontmatter: frontmatter) : nil,
)
}
/// The `---`-delimited frontmatter block if present; empty otherwise.
/// Quoted scalar values (`origin: "ai"`) are unquoted — lenient YAML.
static func frontmatter(of text: String) -> [String: String] {
let lines = text.split(separator: "\n", omittingEmptySubsequences: false)
guard lines.first(where: { !$0.trimmed().isEmpty })?.trimmed() == "---" else { return [:] }
var map: [String: String] = [:]
for line in lines.drop(while: { $0.trimmed() != "---" }).dropFirst() {
let trimmed = line.trimmed()
guard trimmed != "---" else { break }
guard let colon = trimmed.firstIndex(of: ":") else { continue }
let key = trimmed[..<colon].trimmingCharacters(in: .whitespaces)
let value = trimmed[trimmed.index(after: colon)...]
.trimmingCharacters(in: .whitespaces)
.trimmingCharacters(in: CharacterSet(charactersIn: "\"'"))
map[key] = value
}
return map
}
/// The Post's Title: first non-empty line after any frontmatter block,
/// `#` heading markers stripped; `untitled` when nothing readable remains.
static func title(of text: String) -> String {