Files
museion/docs/devlog/20261031-issue32-m4-trend-view-s5-2_log.md
AlexKucera 3ee6756467
CI / ci (push) Canceled after 0s
feat(stats): trend view — the writer's arc (#32)
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
2026-10-31 15:12:45 +01:00

108 lines
7.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# M4: Trend view (S5.2)
> **Date:** 2026-10-31
> **Type:** issue
> **Reference:** Gitea issue #32 (AlexKucera/Museion), PRD S5.2; blocked-by #31 (closed)
## Goal
The writer sees per-metric trends across unaided first drafts over time — the view
answers "am I getting better," never "is this draft good." Only stats-eligible
drafts feed it: the edited-text leak must be impossible by construction, not by
filtering.
## What Was Done
- `Museion/TrendsPaneView.swift` (new) — the Trends pane: one Swift Charts
`LineMark`+`PointMark` card per metric (adaptive ≥340 2-col grid), a mono-11
`first → last · improving/steady/slipping` readout at ±0.01 (direction
inverted for trigram repetition — the one ↓-better metric), per-metric ↑/↓
hint, a plain-language gloss line beneath each chart (user follow-up
request), fixed y-domains (burstiness −1…1, TTR + trigram 0…1,
sentenceVariation 1.2× peak, floor 1). Offset-keyed chart identity and a
title tie-break make same-second capture twins deterministic.
- `Museion/PostLibrary.swift` — `Post.firstDraftStats: FirstDraftStats?`:
`Post.scan` reads the five `stats_*` keys back, nil unless every key parses
to a FINITE Double, and only when the piece carries the capture gate's
immutable pair (origin writer, type post). Origin/type decode hoisted to
locals so the gate and the Post fields share one decode.
- `Museion/WorkspaceView.swift` — rail destination `Trends`
(`chart.xyaxis.line`, after Learnings — measurement beside memory).
- `Museion/DraftStats.swift` — `Equatable` re-added (#31 removed it as unused;
Post's synthesized equality rides `firstDraftStats`).
- Tests: `MuseionTests/TrendViewTests.swift` (new, `@Suite`) — 9 tests:
real-capture round-trip (%.3f grain + stamp-format date proof), absence by
construction, partial/malformed keys, non-finite values, read-back gate
(imported ai/outline pieces with hand-stamped keys scan nil; a Pass-1-Done
writer piece keeps its arc), verdict pins incl. the exact ±0.01 boundary,
y-domains, distinct glosses. Non-finite + gate pins flip-verified (each
fails on its own guard).
- Docs: `Museion/AGENTS.md`, `MuseionTests/AGENTS.md`, `CHANGELOG.md`.
- Finish-audit (3 reviewers: ponytail / security / general) — accepted and
fixed: the nan/inf crash (security MEDIUM, reproduced), the import leak
(read-back gate), same-second twin ordering, ponytail cuts (injectable
`tolerance` param, `TrendDirection.label` switch → String rawValue, dead
`?? .distantPast` fallbacks). Deferred: the x-axis ordering pin (comparator
lives inside the View; extraction for one comparator isn't worth it).
## Decisions & Rationale
| Decision | Rationale |
|----------|-----------|
| Read-back gate = origin writer + type post, NOT pass_state | Import carries external `stats_*` verbatim (reviewer finding), so presence alone overstated "by construction". The immutable pair mirrors the capture gate exactly; pass_state legitimately advances after the first capture, so gating on First Draft would hide every real arc. A migrated writer piece keeps its history. |
| `isFinite` on every parsed metric key | `Double("nan")`/`"inf")` parse; a non-finite sentenceVariation peak traps `0 ... max(nan, 1)` in `chartYScale` — a deterministic crash from one imported file. One guard at the parse point every consumer (chart, verdict, readout) routes through. |
| Pane rides `PostsModel.posts` (the watched scan) | Zero new scan/watcher machinery; refreshes with the library for free. |
| Verdict = first→last, one word, ±0.01 noise band | The AC asks for the arc, never a draft grade. Strict comparisons pinned at exactly the tolerance. |
| Gloss line below each chart, not tooltips | User-requested shape ("text below the graphs"); always visible beats hover-discovery for terms learned once. Copy pinned distinct + direction-bearing. |
| Fixed y-domains; variation scales to peak | A single capture's dot never flattens into an empty axis; scales stay comparable across sessions. |
| Per-access POSIX DateFormatter for the stamp parse | Repo convention (no non-Sendable statics); symmetric with `FixCardStore.formatted` on the write side. |
## Gotchas & Fixes
| Problem | Root Cause | Fix |
|---------|------------|-----|
| `Double?.filter(\.isFinite)` does not compile | Optional has no `filter` in the stdlib (reviewer-suggested shape) | Explicit `guard let value = Double(raw), value.isFinite`; reviewer remediations are guesses until probed |
| Type-checker timeout on the series chain | compactMap→sorted→map over tuples with an inline ternary comparator | Broken into `captured`/`ordered` sub-expressions with labeled tuple elements |
| Scripted flip-restore nested a function declaration inside itself | The restore replaced the flipped BODY line with the whole function decl | Repaired by patch; the build-first check caught it before a wasted suite run — exactly the failure mode finish-audit warns about |
| Executed-count pipes failing in background shells | The rtk wrapper mangles `ls`/xcresulttool output; `ls` prints dirs with trailing colons | Read counts in the foreground with an explicit xcresult path (`ls -td`) |
| Suite-log greps counting 0 passed | Swift Testing logs `Test "x" passed`, not XCTest's `Test case` | Authoritative counts always from `xcresulttool get test-results summary` |
## Files Changed
| File | Change Summary |
|------|---------------|
| `Museion/TrendsPaneView.swift` | New — the pane, TrendMetric, TrendDirection, MetricCard |
| `Museion/PostLibrary.swift` | `Post.FirstDraftStats` (finite-parse), scan read-back + origin/type gate |
| `Museion/WorkspaceView.swift` | `.trends` pane case + rail button |
| `Museion/DraftStats.swift` | `Equatable` re-added |
| `MuseionTests/TrendViewTests.swift` | New — 9 tests incl. 2 flip-verified audit pins |
| `Museion/AGENTS.md` / `MuseionTests/AGENTS.md` | DOX pass — file entries, gate/gloss facts, pin inventory |
| `CHANGELOG.md` | Unreleased → added: trend view |
| `AGENTS.md` (root) | GitNexus index-counter line (its own `analyze` output) |
## Verification
- Build: `xcodebuild … build` **SUCCEEDED**, no new warnings.
- Full unit suite (sequential, executed count from xcresult): **508/508**
(500 at #122 + 8 new; the 9th test folds into an existing pin set).
- SwiftFormat: 0/102 files require formatting. SwiftLint: all touched files
clean (the 2 WorkspaceView warnings predate the session — verified against
HEAD).
- Flip cycle: finiteness guard + read-back guard batched off → exactly the
two pins failed, each on its own guard; restored → green.
## Open Items & Next Steps
- [ ] X-axis ordering pin deferred — the comparator lives inside the View;
extract only if a second consumer ever needs the ordered series.
- [ ] Pre-existing lint ceilings (PostsModelTests/PostStoreTests file length,
WorkspaceView file length) — own extraction ticket.
- [ ] `MuseionUITests.testAppLaunches` still fails environmentally
("Failed to activate application") — pre-existing, per the #31 log.
- [ ] v2 parking lot: periodization — trends naming the dominant persistent
weakness + the matching drill (PRD v2 lot, idea ⑫).
---
*Log written by write-log skill*