CI / ci (push) Canceled after 0s
The argus opt pass (2026-11-09, unticketed digest finding cold-turkey-full-tokenization): every editor keystroke feeds the whole document through WritingGoalRun.update, which counted words for ALL goal types — a minutes verdict ignores the count, so the feed paid whole-document tokenization (lowercased [String] allocation) per keystroke on the main actor for nothing (argus probe: ~16.6 ms/update at 50k words, linear in document size). Pins, RED-first where constructible: - WritingGoalTests 'a minutes update takes zero word-rail trips' — RED at base (base takes one trip per update). - WritingGoalTests 'a words update takes exactly one word-rail trip' — the skip boundary: words goals keep live accounting. - DraftStatsTests 'word rail equals the split tokenizer over a mixed corpus' — semantic equivalence pin (old split expression as inline reference over mixed scripts, emoji, punctuation, numbers, empties); green at base by design — it is the equivalence proof. Instrument: update(text:countWords:) gains an injectable word-rail seam (default DraftStats.words) — per-call counting instead of a global counter, which would race the hosted suite's parallel DraftStats calls. No behavior change in this commit; update still counts for every goal.
115 lines
4.6 KiB
Swift
115 lines
4.6 KiB
Swift
//
|
||
// 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)
|
||
}
|
||
|
||
@Test func `word rail equals the split tokenizer over a mixed corpus`() {
|
||
/// The count-only word rail (opt pass 2026-11-09) must answer exactly
|
||
/// what the lowercased-token array answered: split on
|
||
/// !(isLetter || isNumber), empty subsequences omitted. The reference
|
||
/// IS the old implementation — the corpus (mixed scripts, emoji,
|
||
/// punctuation, numbers, empties) makes any semantic drift (e.g. a
|
||
/// whitespace count) fail here.
|
||
func reference(_ text: String) -> Int {
|
||
text.split(whereSeparator: { !($0.isLetter || $0.isNumber) }).count
|
||
}
|
||
let corpus = [
|
||
"",
|
||
" \t\n ",
|
||
"\n\n\n",
|
||
"— — !!!",
|
||
"one two three",
|
||
"writer's note",
|
||
"## Chapter Two — draft 3",
|
||
"abc123def",
|
||
"3.14 and 2,718,281",
|
||
"$5,000 USD",
|
||
"naïve café Zürich",
|
||
"Привет мир",
|
||
"日本語のテキストです",
|
||
"مرحبا بالعالم",
|
||
"emoji 👨👩👧👦 are not words",
|
||
"word—word, word; word: word",
|
||
"One two three. Four five! Six?",
|
||
]
|
||
for text in corpus {
|
||
#expect(
|
||
DraftStats.words(in: text) == reference(text),
|
||
"corpus mismatch for \(text.debugDescription)",
|
||
)
|
||
}
|
||
}
|