feat(cold-turkey): pure goal engine (#161)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Wayfinder map #158, Build S1: the pure logic slice the writing lock consumes — no chrome, no clock of its own. WritingGoal (words | minutes, one goal per block, fixed at arm) + WritingGoalRun: update(text:) takes whole-document text and netNewWords falls out of the delta (deleting costs — net, not gross; a big paste counts). now is injected into every call, so the countdown cannot pause by construction; the minutes deadline is arm+minutes (inclusive), isMet is live truth, unlatched (delete below target un-mets — complete on the first true tick), and progress(now:) is the clamped 0…1 the readout hairline rides. Word counting goes through the new internal DraftStats.words(in:) — net-words and first-draft stats share one tokenizer and can never drift. Finish-audit fanout: the Int() conversion trap in secondsRemaining fixed at the seam (isFinite + Double(Int.max).nextDown cap — the naive cap is itself off-by-one at 2^63; flip-verified pin), two unpinnable > 0 guards in progress deleted, baselineWords narrowed to let. Equatable and the ?? armedAt fallback kept deliberately (S2 consumes the former; removing the latter costs formula duplication). S2 throttle note landed on #162. Suite 602/602 (xcresult count), swiftformat + swiftlint clean in app and test targets. Two traps filed to the swift wiki (bare @Suite stripped by swiftformat; the Double(Int.max) cap).
This commit is contained in:
1 parent
d6c4712aaa
commit
e3195d68fd
10 files changed
+389
-5
No files matched your search
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// WritingGoal.swift
|
||||
// Museion
|
||||
//
|
||||
// #161 (Cold Turkey S1): the pure goal engine the writing lock
|
||||
// consumes. One goal per block, fixed at arm time — net-new-words
|
||||
// or a never-pausing countdown. No UI, no clock of its own: the
|
||||
// lock feeds whole-document text and `now`, this struct answers.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// The one goal a Cold Turkey block was armed with (#161). Fixed at
|
||||
/// arm time; the arm-bar validates the value, the engine just counts.
|
||||
nonisolated enum WritingGoal: Equatable {
|
||||
/// Net new words in the open Piece before the block completes.
|
||||
case words(Int)
|
||||
/// Minutes from arm until the block completes. Never pauses.
|
||||
case minutes(Int)
|
||||
}
|
||||
|
||||
/// One armed Cold Turkey block (#161): the goal plus the arm-time
|
||||
/// facts. Pure value — `update(text:)` records the live editor text
|
||||
/// (typing, deleting, pasting, undo all land as whole-document
|
||||
/// text), and `now` is injected, so the countdown can never be
|
||||
/// paused or deferred from inside the engine. Word counting rides
|
||||
/// `DraftStats.words` — the one tokenizer all text metrics share, so
|
||||
/// net-words can never drift from first-draft stats ("writer's" is
|
||||
/// two words there, headings are body text; frontmatter is not body
|
||||
/// text — the editor never shows it, so it never counts).
|
||||
nonisolated struct WritingGoalRun: Equatable {
|
||||
let goal: WritingGoal
|
||||
/// When the block armed. The minutes deadline hangs off this and
|
||||
/// nothing can move it.
|
||||
let armedAt: Date
|
||||
/// Whole-document word count at arm.
|
||||
let baselineWords: Int
|
||||
/// Whole-document word count as of the last `update(text:)`.
|
||||
private(set) var currentWords: Int
|
||||
|
||||
init(goal: WritingGoal, armedAt: Date, body: String) {
|
||||
self.goal = goal
|
||||
self.armedAt = armedAt
|
||||
let words = DraftStats.words(in: body)
|
||||
baselineWords = words
|
||||
currentWords = words
|
||||
}
|
||||
|
||||
/// Record the live editor text. The caller passes the whole
|
||||
/// document every time; the net falls out of the delta.
|
||||
mutating func update(text: String) {
|
||||
currentWords = DraftStats.words(in: text)
|
||||
}
|
||||
|
||||
/// Net new words since arm. Deleting costs — net, not gross; a
|
||||
/// 2000-word paste counts (gaming yourself is your business,
|
||||
/// #158 settled).
|
||||
var netNewWords: Int {
|
||||
currentWords - baselineWords
|
||||
}
|
||||
|
||||
/// Whole seconds left on a minutes goal (0 once past). Nil for a
|
||||
/// words goal, which has no clock.
|
||||
func secondsRemaining(now: Date) -> Int? {
|
||||
guard let deadline else { return nil }
|
||||
let seconds = deadline.timeIntervalSince(now).rounded()
|
||||
// ponytail: clamp at the Int seam — Int() traps past Int.max (an
|
||||
// absurd-but-legal .minutes payload would kill the editor mid-
|
||||
// session); NaN clock (unconstructible from Date()) counts expired
|
||||
guard seconds.isFinite else { return 0 }
|
||||
return Int(min(max(seconds, 0), Double(Int.max).nextDown))
|
||||
}
|
||||
|
||||
/// Goal met at `now`? Words: net ≥ target. Minutes: `now` has
|
||||
/// passed the deadline — inclusive, the boundary tick completes.
|
||||
/// Live truth, unlatched: deleting below target un-mets a words
|
||||
/// goal; the lock completes on the first tick this reads true.
|
||||
func isMet(now: Date) -> Bool {
|
||||
switch goal {
|
||||
case let .words(target): netNewWords >= target
|
||||
case .minutes: now >= deadline ?? armedAt
|
||||
}
|
||||
}
|
||||
|
||||
/// 0…1 toward the goal for the readout hairline — words by net
|
||||
/// count, minutes by elapsed time. Clamped at both ends (degenerate
|
||||
/// zero targets fall out to 1 through the Double math).
|
||||
func progress(now: Date) -> Double {
|
||||
switch goal {
|
||||
case let .words(target):
|
||||
min(1, Double(max(netNewWords, 0)) / Double(target))
|
||||
case let .minutes(minutes):
|
||||
min(1, max(0, now.timeIntervalSince(armedAt)) / (Double(minutes) * 60))
|
||||
}
|
||||
}
|
||||
|
||||
/// The never-moving minutes deadline; nil for a words goal.
|
||||
private var deadline: Date? {
|
||||
guard case let .minutes(minutes) = goal else { return nil }
|
||||
return armedAt.addingTimeInterval(Double(minutes) * 60)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user