test(goal): pin zero word-rail trips for minutes updates (RED)
CI / ci (push) Canceled after 0s
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.
This commit is contained in:
1 parent
5915178ce8
commit
dde99c8a75
3 files changed
+77
-2
No files matched your search
@@ -48,8 +48,11 @@ nonisolated struct WritingGoalRun: Equatable {
|
|||||||
|
|
||||||
/// Record the live editor text. The caller passes the whole
|
/// Record the live editor text. The caller passes the whole
|
||||||
/// document every time; the net falls out of the delta.
|
/// document every time; the net falls out of the delta.
|
||||||
mutating func update(text: String) {
|
/// `countWords` is the injectable word rail — the pins count
|
||||||
currentWords = DraftStats.words(in: text)
|
/// rail trips through this seam (a global counter would race
|
||||||
|
/// the hosted suite's parallel DraftStats calls).
|
||||||
|
mutating func update(text: String, countWords: (String) -> Int = DraftStats.words(in:)) {
|
||||||
|
currentWords = countWords(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Net new words since arm. Deleting costs — net, not gross; a
|
/// Net new words since arm. Deleting costs — net, not gross; a
|
||||||
|
|||||||
@@ -75,3 +75,40 @@ import Testing
|
|||||||
#expect(abs(DraftStats.compute(body: "Dog dog DOG").typeTokenRatio - 1.0 / 3.0) < 0.001)
|
#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)
|
#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)",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -103,6 +103,41 @@ struct WritingGoalTests {
|
|||||||
#expect(block.secondsRemaining(now: Self.arm.addingTimeInterval(60)) == 29 * 60)
|
#expect(block.secondsRemaining(now: Self.arm.addingTimeInterval(60)) == 29 * 60)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func `a minutes update takes zero word-rail trips`() {
|
||||||
|
// Opt pass 2026-11-09 (unticketed digest finding): a minutes
|
||||||
|
// verdict rides the clock alone, so the per-keystroke feed must
|
||||||
|
// not count — let alone tokenize — the whole document. One long
|
||||||
|
// feed, zero rail trips through the injectable seam.
|
||||||
|
var block = Self.run(.minutes(30))
|
||||||
|
var railTrips = 0
|
||||||
|
block.update(
|
||||||
|
text: String(repeating: "word ", count: 2000),
|
||||||
|
countWords: { _ in
|
||||||
|
railTrips += 1
|
||||||
|
return 0
|
||||||
|
},
|
||||||
|
)
|
||||||
|
#expect(railTrips == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func `a words update takes exactly one word-rail trip`() {
|
||||||
|
// The skip boundary: words goals keep live accounting — exactly
|
||||||
|
// one rail trip per update, and the count lands.
|
||||||
|
var block = Self.run(.words(500))
|
||||||
|
var railTrips = 0
|
||||||
|
block.update(
|
||||||
|
text: "one two three four five six seven",
|
||||||
|
countWords: { _ in
|
||||||
|
railTrips += 1
|
||||||
|
return 7
|
||||||
|
},
|
||||||
|
)
|
||||||
|
#expect(railTrips == 1)
|
||||||
|
#expect(block.currentWords == 7)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
func `an absurd minutes goal never traps the countdown`() {
|
func `an absurd minutes goal never traps the countdown`() {
|
||||||
// Int() traps past Int.max — the clamp at the conversion seam is the pin
|
// Int() traps past Int.max — the clamp at the conversion seam is the pin
|
||||||
|
|||||||
Reference in new issue
Block a user