feat(stats): first-draft stats capture (S5.1) (#31)
CI / ci (push) Canceled after 0s
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:
1 parent
ae234350ef
commit
42bcfce9ba
13 files changed
+611
-20
No files matched your search
+73
-10
@@ -273,13 +273,25 @@ nonisolated enum PostStore {
|
||||
|
||||
/// Save an existing Post: body-only text in, file normalized out. The
|
||||
/// file's present `pass_state`/`origin`/`type` are preserved (origin
|
||||
/// immutable, CONTEXT.md; bare corpus posts normalize on save, #59). A
|
||||
/// vanished post throws — `writeNew` is the only creator; saving must
|
||||
/// never resurrect a deleted post with reset metadata.
|
||||
/// immutable, CONTEXT.md; bare corpus posts normalize on save, #59).
|
||||
/// **#31 audit fix: blocked files update the three owned keys IN PLACE
|
||||
/// (stampingFrontmatter) and swap in the caller's body — the old
|
||||
/// stamped() round-trip rebuilt the block from three parsed fields,
|
||||
/// erasing the first-draft stats captured at the pass invocation and
|
||||
/// any foreign frontmatter on every editor save.** A vanished post
|
||||
/// throws — `writeNew` is the only creator; saving must never
|
||||
/// resurrect a deleted post with reset metadata.
|
||||
static func overwrite(id: String, body: String, home: URL) throws {
|
||||
let piece = try existingStamp(id: id, home: home)
|
||||
try stamped(body: body, passState: piece.passState, origin: piece.origin, type: piece.type)
|
||||
.write(to: piece.dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
let raw = try String(contentsOf: piece.dir.appendingPathComponent("post.md"), encoding: .utf8)
|
||||
let text = try PostStore.splitFrontmatter(raw).block.isEmpty
|
||||
? stamped(body: body, passState: piece.passState, origin: piece.origin, type: piece.type)
|
||||
: PostStore.splitFrontmatter(LearningDecay.stampingFrontmatter(of: raw, with: [
|
||||
("type", piece.type.rawValue),
|
||||
("pass_state", piece.passState.rawValue),
|
||||
("origin", piece.origin.rawValue),
|
||||
])).block + body
|
||||
try text.write(to: piece.dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
}
|
||||
|
||||
/// #114: the shared read behind `overwrite` and `convert` — resolve the
|
||||
@@ -598,14 +610,21 @@ nonisolated extension PostStore {
|
||||
/// #24 (S3.1): advance `pass_state` when a pass completes. Monotonic:
|
||||
/// the new state is the higher of the file's current state and the
|
||||
/// pass's doneState — passes run in ANY order, so a Pass 1 completing
|
||||
/// on a Pass-2 Done piece never regresses it. Body/origin/type ride the
|
||||
/// existingStamp→stamped round-trip unchanged (same preservation as
|
||||
/// `overwrite`). Returns the state now stamped. A vanished piece throws.
|
||||
/// on a Pass-2 Done piece never regresses it. **#31 (S5.1): the state
|
||||
/// update is an IN-PLACE key stamp (stampingFrontmatter), not the
|
||||
/// existingStamp→stamped block rewrite — the rewrite carried only
|
||||
/// type/pass_state/origin and silently erased the first-draft stats
|
||||
/// captured at the invocation (and any foreign frontmatter).** Bare
|
||||
/// posts (no block) still normalize through the full #59 stamp.
|
||||
/// Returns the state now stamped. A vanished piece throws.
|
||||
static func advancePassState(id: String, kind: PassKind, home: URL) throws -> Post.PassState {
|
||||
let piece = try existingStamp(id: id, home: home)
|
||||
let advanced = higherState(piece.passState, kind.doneState)
|
||||
try stamped(body: piece.body, passState: advanced, origin: piece.origin, type: piece.type)
|
||||
.write(to: piece.dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
let raw = try String(contentsOf: piece.dir.appendingPathComponent("post.md"), encoding: .utf8)
|
||||
let text = try PostStore.splitFrontmatter(raw).block.isEmpty
|
||||
? stamped(body: piece.body, passState: advanced, origin: piece.origin, type: piece.type)
|
||||
: LearningDecay.stampingFrontmatter(of: raw, with: [("pass_state", advanced.rawValue)])
|
||||
try text.write(to: piece.dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
// The decay clock counts completed passes per content type (#27).
|
||||
// try?: a failed clock bump must never fail a completed pass, and
|
||||
// a lagging clock only delays resurfacing (safe direction).
|
||||
@@ -682,3 +701,47 @@ nonisolated extension PostStore {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: First-draft stats capture (#31)
|
||||
|
||||
/// #31 (S5.1): the unaided-first-draft capture write side (CONTEXT.md
|
||||
/// 'Unaided First Draft'). Same file as the pass substrate: the enum's
|
||||
/// private helpers (folder(of:)) stay reachable at file scope.
|
||||
nonisolated extension PostStore {
|
||||
/// Compute the four `DraftStats` over `body` and stamp them into the
|
||||
/// piece's frontmatter — runPass fires this at the invocation, before
|
||||
/// the prompt crosses the bridge. Eligibility is decided by the FILE's
|
||||
/// own frontmatter alone (same lenient defaults as Post.scan — a bare
|
||||
/// legacy post qualifies): origin writer, Pass State First Draft, type
|
||||
/// post. Not eligible → false, file untouched. The stamp is in place
|
||||
/// (stampingFrontmatter): every non-stats line byte-identical, so a
|
||||
/// re-invocation while still First Draft overwrites — latest draft
|
||||
/// state wins. Any read/write failure → false: a failed capture must
|
||||
/// never fail the pass (the decay-clock posture).
|
||||
@discardableResult
|
||||
static func captureFirstDraftStats(id: String, body: String, home: URL) -> Bool {
|
||||
guard let dir = folder(of: id, home: home),
|
||||
let text = try? String(contentsOf: dir.appendingPathComponent("post.md"), encoding: .utf8)
|
||||
else { return false }
|
||||
let matter = Post.frontmatter(of: text)
|
||||
// ponytail: third copy of the lenient `?? default` stamp decode
|
||||
// (Post.scan, existingStamp) — extract one shared decode when a
|
||||
// fourth caller needs it.
|
||||
guard Post.Origin(rawValue: matter["origin"] ?? "") ?? .writer == .writer,
|
||||
Post.PassState(rawValue: matter["pass_state"] ?? "") ?? .firstDraft == .firstDraft,
|
||||
Post.ContentType(rawValue: matter["type"] ?? "") ?? .post == .post
|
||||
else { return false }
|
||||
let stats = DraftStats.compute(body: body)
|
||||
let stamped = LearningDecay.stampingFrontmatter(of: text, with: [
|
||||
("stats_captured", FixCardStore.formatted(Date(), "yyyy-MM-dd'T'HH:mm:ss")), // lexically sortable
|
||||
("stats_burstiness", String(format: "%.3f", stats.burstiness)),
|
||||
("stats_type_token_ratio", String(format: "%.3f", stats.typeTokenRatio)),
|
||||
("stats_sentence_variation", String(format: "%.3f", stats.sentenceVariation)),
|
||||
("stats_trigram_repetition", String(format: "%.3f", stats.trigramRepetition)),
|
||||
])
|
||||
guard (try? stamped.write(
|
||||
to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8,
|
||||
)) != nil else { return false }
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user