feat(stats): trend view — the writer's arc (#32)
CI / ci (push) Canceled after 0s

A Trends pane (rail destination after Learnings — measurement beside
memory): one line chart per style metric (burstiness, type-token ratio,
sentence variation, trigram repetition) across unaided-first-draft
CAPTURES over time. Points are capture moments, never draft grades —
the pane answers "am I getting better," not "is this draft good." Each
card carries a first-to-last read (improving/steady/slipping at ±0.01,
direction inverted for trigram repetition — the one falling-is-better
metric; one capture shows its value with no verdict), an up/down-better
hint, and a one-line plain-language gloss of the term.

The edited-text leak is closed by construction: Post.scan reads the
five stats_* frontmatter keys into firstDraftStats only when every key
parses to a finite double AND the piece carries the capture gate's
immutable pair (origin writer, type post — not pass_state, which
legitimately advances after the capture). Import carries external YAML
verbatim, so imported ai pieces or outlines with hand-stamped keys
never render, while a migrated writer piece keeps its real arc. The
finite gate also kills a deterministic crash: Double("nan") parses, and
a non-finite peak traps the chart's y-domain construction.

The pane rides PostsModel's watched scan — no scan of its own — so it
refreshes with the library. Fixed y-domains keep single captures
visible (sentence variation scales to its peak); offset-keyed chart
identity and a title tie-break make same-second capture twins
deterministic; the empty state names how the data arrives.

Finish-audit (3 reviewers) fixed the nan/inf crash, the import leak,
same-second ordering, and cut the injectable tolerance parameter and a
label-switch enum down to raw values. Suite 508; the non-finite and
read-back-gate pins are flip-verified.

Closes #32
This commit is contained in:
AlexKucera committed 2026-10-31 15:12:45 +01:00
1 parent 97e86cf62d
commit 3ee6756467
11 files changed
+679 -8

No files matched your search

+67 -2
View File
@@ -28,10 +28,64 @@ nonisolated struct Post: Identifiable, Equatable {
var project: String
/// True when the piece lives under archive/ (#107).
var isArchived: Bool
/// #32 (S5.2): the captured first-draft stats — present ONLY when
/// `captureFirstDraftStats` stamped the piece at a pass invocation
/// (writer origin, First Draft, post type, decided there against the
/// file's own frontmatter), and read back only for the same immutable
/// pair (scan's gate — see `Post.scan`). Presence is the trend view's
/// whole eligibility story: app-internal edit paths never grow these
/// keys, and non-writer or outline pieces never render them.
var firstDraftStats: FirstDraftStats?
/// New pieces land here when no project is selected (#107).
static let defaultProject = "posts"
/// One stats capture, read back from the five flat `stats_*`
/// frontmatter keys (#31 wrote them; #32 reads them back).
nonisolated struct FirstDraftStats: Equatable {
/// When the pass invocation captured the draft
/// (`yyyy-MM-dd'T'HH:mm:ss`, lexically sortable — #31's stamp).
let captured: Date
let stats: DraftStats
/// Nil unless EVERY key parses to a finite Double — a half-loaded
/// arc would lie, and `Double("nan")`/`Double("inf")` DO parse:
/// a non-finite peak traps the chart's y-domain construction and
/// reads "steady" in the verdict (finish-audit #32).
init?(frontmatter: [String: String]) {
guard
let captured = frontmatter["stats_captured"].flatMap(Self.parseCaptured),
let burstiness = frontmatter["stats_burstiness"].flatMap(Self.finite),
let typeTokenRatio = frontmatter["stats_type_token_ratio"].flatMap(Self.finite),
let sentenceVariation = frontmatter["stats_sentence_variation"].flatMap(Self.finite),
let trigramRepetition = frontmatter["stats_trigram_repetition"].flatMap(Self.finite)
else { return nil }
self.captured = captured
stats = DraftStats(
burstiness: burstiness,
typeTokenRatio: typeTokenRatio,
sentenceVariation: sentenceVariation,
trigramRepetition: trigramRepetition,
)
}
/// The symmetric read of the capture stamp (`FixCardStore.formatted`,
/// local time) — per-access formatter, the repo's no-static convention.
private static func parseCaptured(_ stamp: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter.date(from: stamp)
}
/// Frontmatter is external data (hand-edits, imports): reject the
/// non-finite doubles Swift's `Double(String)` happily parses.
private static func finite(_ raw: String) -> Double? {
guard let value = Double(raw), value.isFinite else { return nil }
return value
}
}
/// The two roots that hold pieces, in resolution order (#107): projects/
/// is searched before archive/.
static let containerRoots = ["projects", "archive"]
@@ -89,15 +143,26 @@ nonisolated struct Post: Identifiable, Equatable {
let fileURL = dir.appendingPathComponent("post.md")
guard let text = try? String(contentsOf: fileURL, encoding: .utf8), seen.insert(dir.lastPathComponent).inserted else { continue }
let frontmatter = frontmatter(of: text)
let origin = Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer
let type = ContentType(rawValue: frontmatter["type"] ?? "") ?? .post
posts.append(
Post(
id: dir.lastPathComponent,
title: title(of: text),
passState: PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
origin: Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer,
type: ContentType(rawValue: frontmatter["type"] ?? "") ?? .post,
origin: origin,
type: type,
project: projectDir.lastPathComponent,
isArchived: isArchived,
// #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
// carrying hand-stamped keys still never renders;
// a migrated writer piece keeps its real arc.
firstDraftStats: origin == .writer && type == .post
? Post.FirstDraftStats(frontmatter: frontmatter) : nil,
),
)
}