feat(stats): first-draft stats capture (S5.1) (#31)
CI / ci (push) Canceled after 0s

Measure the writer, not the text. Running Pass 1 or Pass 2 on an
unaided draft (origin: writer, First Draft Pass State, post type -
lenient defaults, so bare legacy posts qualify) computes four style
metrics natively in Swift at the invocation - before the engine sees
anything - and stamps stats_captured plus four stats_* values into
the piece's frontmatter in place, so the S5.2 trend view is a plain
scan of Post.frontmatter. Capture re-fires while still First Draft
(latest draft wins); a cancelled pass keeps its capture; ai-origin
and advanced pieces never qualify.

The capture exposed an erasure class: the existingStamp-to-stamped
block rewrite rebuilt frontmatter from three parsed fields, silently
dropping everything else. advancePassState and overwrite now update
their owned keys in place (stampingFrontmatter, widened internal per
its own third-caller trigger; bare posts still normalize through
stamped), so captured stats and foreign YAML survive pass completion
AND mid-pass editor saves. Both fixes are flip-verified by pins.

DraftStats carries the four metrics (burstiness, type-token ratio,
sentence-length variation, trigram repetition) as a pure Foundation
model - full precision out, %.3f at store time. 16 new pins across
DraftStatsTests, PostStoreTests, and PassRunTests; full unit bundle
396/396 (sequential gate; parallel runs flake under external CPU
load per the #30 learning). Finish-audit fanout (ponytail/security/
review) ran on the diff: its MEDIUM (overwrite erasure) and the
converged deletions landed here.

Closes #31
This commit is contained in:
AlexKucera committed 2026-10-22 19:05:42 +02:00
1 parent ae234350ef
commit 42bcfce9ba
13 files changed
+611 -20

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
//
// DraftStatsTests.swift
// MuseionTests
//
// #31 (S5.1): the four unaided-first-draft stats, pinned over
// hand-derived expected values (like #24's estimateTokens pin — the
// expectations are derived here, not copied from the implementation).
//
// ## Scenario: first-draft stats capture
// From #31 (PRD S5.1)
// Given a saved origin: writer post still in First Draft Pass State
// When any pass (Pass 1 or Pass 2) is invoked on it
// Then the four stats are computed natively in Swift from the exact
// text the engine is about to see and stamped into the post's
// frontmatter before the prompt crosses the bridge — the pass proceeds
// untouched
// Given instead an ai-origin piece, or one already past First Draft
// When a pass is invoked
// Then nothing is stamped — origin and Pass State alone decide
//
import Foundation
@testable import Museion
import Testing
@Test func `draft stats over sentences of three two and one tokens`() {
// Derivation: "One two three. Four five! Six?" → sentences [3, 2, 1].
// μ = 2; σ = √(((3−2)² + 0 + (1−2)²)/3) = √(2/3) ≈ 0.81650.
// burstiness = (σ − μ)/(σ + μ) ≈ −0.42020; variation = σ/μ ≈ 0.40825.
// Tokens: one two three four five six → TTR = 6/6 = 1. Trigrams:
// [one two three][two three four][three four five][four five six] —
// 4 of 4 unique → repetition 0.
let stats = DraftStats.compute(body: "One two three. Four five! Six?")
#expect(abs(stats.burstiness - -0.42020) < 0.001)
#expect(abs(stats.sentenceVariation - 0.40825) < 0.001)
#expect(abs(stats.typeTokenRatio - 1.0) < 0.001)
#expect(abs(stats.trigramRepetition - 0.0) < 0.001)
}
@Test func `draft stats regular sentences pin burstiness at minus one`() {
// Derivation: "a b c. a b c." → sentences [3, 3]: σ = 0, μ = 3 →
// burstiness = (0 − 3)/(0 + 3) = −1 (maximally regular), variation 0.
// Tokens: a b c a b c → TTR = 3/6 = 0.5. Trigrams:
// [a b c][b c a][c a b][a b c] → 3 unique of 4 → 0.25.
let stats = DraftStats.compute(body: "a b c. a b c.")
#expect(abs(stats.burstiness - -1.0) < 0.001)
#expect(abs(stats.sentenceVariation - 0.0) < 0.001)
#expect(abs(stats.typeTokenRatio - 0.5) < 0.001)
#expect(abs(stats.trigramRepetition - 0.25) < 0.001)
}
@Test func `draft stats empty body is all zeros`() {
let stats = DraftStats.compute(body: "")
#expect(stats.burstiness == 0)
#expect(stats.typeTokenRatio == 0)
#expect(stats.sentenceVariation == 0)
#expect(stats.trigramRepetition == 0)
}
@Test func `draft stats single sentence pins burstiness at minus one`() {
// Derivation: "Only one sentence." → sentences [3]: σ = 0, μ = 3 →
// burstiness −1, variation 0; TTR 3/3 = 1; one trigram, unique → 0.
let stats = DraftStats.compute(body: "Only one sentence.")
#expect(abs(stats.burstiness - -1.0) < 0.001)
#expect(abs(stats.sentenceVariation - 0.0) < 0.001)
#expect(abs(stats.typeTokenRatio - 1.0) < 0.001)
#expect(abs(stats.trigramRepetition - 0.0) < 0.001)
}
@Test func `draft stats tokens case fold without apostrophe joining`() {
// Derivation: "Dog dog DOG" → dog dog dog → 1 unique of 3 → 1/3.
// No apostrophe joining: "writer's note writer" → writer s note writer
// → 3 unique of 4 → 0.75 (WITH joining it would be 3/3 = 1.0 — this
// pin discriminates the ceiling, the old all-unique body did not).
#expect(abs(DraftStats.compute(body: "Dog dog DOG").typeTokenRatio - 1.0 / 3.0) < 0.001)
#expect(abs(DraftStats.compute(body: "writer's note writer").typeTokenRatio - 3.0 / 4.0) < 0.001)
}