feat(mentor): apply/skip fix cards (#25)
CI / ci (push) Canceled after 0s

Fix Cards become actionable. Mechanical cards offer Apply behind a
blocking ask-before sheet showing the exact edit (anchored quote ->
replacement, occurrence + context; empty fix cuts); the sheet is the
only path to a write, one card at a time, no apply-all. The edit
lands through the editing session (pending edits flush first, the
editor re-seeds via adoptText, the card closes applied); Skip and
Done close the card in place, recording nothing; demonstration
cards close Done/Skip only (ADR-0006). Cards arrive ranked
(store-stamped, scan-ordered, open-first palette) and capped at ten
per submit — the app keeps the ranked head and the wire reply
discloses the drop. Stale anchors re-validate at apply: no text
selected or changed, the card flags itself. Includes the
finish-audit round (11 fixes, 2 flip-verified pins); 316 tests
green.

Closes #25
This commit is contained in:
AlexKucera committed 2026-10-20 16:46:43 +02:00
1 parent 5700264628
commit 4d33676a08
15 files changed
+1012 -74

No files matched your search

+141 -12
View File
@@ -38,40 +38,55 @@ private func writePiece(
}
/// One wire-shaped card with test-friendly defaults. Internal — shared
/// with PostsModelTests (review: a hand-copy drifted here).
/// with PostsModelTests and FixCardApplyTests (review: a hand-copy drifted
/// here). #25: `fix`/`lane` feed the apply pins.
func card(
pattern: String = "Hedging",
problem: String = "The hedge weakens the claim.",
diagnosticQuestion: String? = nil,
fix: String? = nil,
quote: String = "repeated anchor line",
occurrence: Int = 1,
lane: FixCardStore.StoredFixCard.Lane = .mechanical,
) -> FixCardStore.Input {
FixCardStore.Input(
pattern: pattern,
problem: problem,
diagnosticQuestion: diagnosticQuestion,
fix: nil,
fix: fix,
anchor: .init(quote: quote, occurrence: occurrence),
lane: .mechanical,
lane: lane,
emitter: .lint,
provenance: nil,
)
}
/// A fully-stamped card for hand-written fixtures (scan tests).
private func storedCard(pattern: String) -> FixCardStore.StoredFixCard {
/// A fully-stamped card for hand-written fixtures (scan tests). Defaults
/// match the historical shape; apply tests override anchor/lane, the rank
/// pins override submittedAt/rank. Internal — FixCardApplyTests shares it
/// (the card()/makeModelHome cross-file convention).
func storedCard(
pattern: String = "P",
quote: String = "q",
occurrence: Int = 1,
fix: String = "",
lane: FixCardStore.StoredFixCard.Lane = .mechanical,
submittedAt: Date = Date(timeIntervalSince1970: 0),
rank: Int? = nil,
) -> FixCardStore.StoredFixCard {
FixCardStore.StoredFixCard(
id: UUID(),
submittedAt: Date(timeIntervalSince1970: 0),
submittedAt: submittedAt,
rank: rank,
pattern: pattern,
problem: "p",
diagnosticQuestion: "",
fix: "",
anchor: .init(quote: "q", occurrence: 1),
lane: .mechanical,
fix: fix,
anchor: .init(quote: quote, occurrence: occurrence),
lane: lane,
emitter: .lint,
provenance: nil,
status: "open",
status: .open,
anchorStatus: .valid,
)
}
@@ -103,7 +118,8 @@ struct MCPToolsTests {
/// #23 follow-up: pass differentiation — `provenance.pass` rides the
/// wire; pre-schema cards (no pass key) decode to nil, never error.
@Test func `provenance pass rides the wire and old cards decode without it`() throws {
let wire = #"{"pattern":"p","problem":"q","anchor":{"quote":"x"},"lane":"mechanical","emitter":"engine","provenance":{"piece":"posts/a","pass":2}}"#
let wire = #"{"pattern":"p","problem":"q","anchor":{"quote":"x"},"lane":"mechanical","emitter":"engine","provenance":"#
+ #"{"piece":"posts/a","pass":2}}"#
let input = try FixCardStore.decoder.decode(FixCardStore.Input.self, from: Data(wire.utf8))
#expect(input.provenance?.pass == 2)
@@ -122,7 +138,7 @@ struct MCPToolsTests {
#expect(written.count == 1)
#expect(written[0].card.anchorStatus == .valid)
#expect(written[0].card.status == "open")
#expect(written[0].card.status == .open)
#expect(written[0].file.hasSuffix("-hedging.json"))
let cardsDir = piece.appendingPathComponent("cards", isDirectory: true)
@@ -204,6 +220,119 @@ struct MCPToolsTests {
#expect(written[0].file != written[1].file)
}
/// #25 (S3.2): the pass ceiling — an attention budget, not infinite
/// scroll. The backstop keeps the ranked HEAD (submit order = rank,
/// pass-persona doctrine); tail cards drop before validation, so a
/// malformed 12th card never errors an otherwise-fine submit.
@Test func `submit caps at the ten-card pass ceiling keeping the ranked head`() throws {
let home = try toolsHome()
let piece = try writePiece(home)
var cards = (1 ... 11).map { card(pattern: "P\($0)") }
cards.append(card(pattern: " ")) // malformed — and beyond the ceiling
let written = try FixCardStore.submit(piecePath: piece.path, cards: cards)
#expect(written.map(\.card.pattern) == (1 ... 10).map { "P\($0)" })
let files = try FileManager.default.contentsOfDirectory(atPath: piece.appendingPathComponent("cards").path)
.filter { $0.hasSuffix(".json") }
#expect(files.count == 10)
}
/// #25 (S3.2): rank is the submit's array order (the persona's ranked
/// call) — the store stamps it 1-based like id/submittedAt. Optional on
/// decode: pre-#25 cards on disk carry no rank key.
@Test func `submit stamps rank in submit order and old cards decode without it`() throws {
let home = try toolsHome()
let piece = try writePiece(home)
let written = try FixCardStore.submit(
piecePath: piece.path,
cards: [card(pattern: "Top"), card(pattern: "Mid"), card(pattern: "Tail")],
)
#expect(written.map(\.card.rank) == [1, 2, 3])
// The disk round-trip carries it (snake_case "rank").
let json = try #require(
try JSONSerialization.jsonObject(
with: Data(contentsOf: piece.appendingPathComponent("cards/\(written[0].file)")),
) as? [String: Any],
)
#expect(json["rank"] as? Int == 1)
// A pre-schema card (no rank key) decodes with rank nil, never errors.
let oldData = try FixCardStore.encoder.encode(storedCard(pattern: "Old"))
let stripped = try #require(
try JSONSerialization.jsonObject(with: oldData) as? [String: Any],
).filter { $0.key != "rank" }
let decoded = try FixCardStore.decoder.decode(
FixCardStore.StoredFixCard.self,
from: JSONSerialization.data(withJSONObject: stripped),
)
#expect(decoded.rank == nil)
}
/// #25 (S3.2/S3.3): the pure Apply — the mechanical edit a card makes
/// in a text: the anchored occurrence replaced by the fix, nothing
/// else. nil = the card must not touch text: a demonstration lane
/// (only a mechanical fix is ever applied, ADR-0006) or an anchor that
/// no longer resolves (edits between issue and accept degrade to a
/// loud no-match, never a wrong-span patch).
@Test func `applied text replaces only the anchored occurrence`() {
let text = "alpha anchor beta anchor gamma"
// The nth occurrence, not the first.
#expect(
FixCardStore.appliedText(of: storedCard(quote: "anchor", occurrence: 2, fix: "FIX"), in: text)
== "alpha anchor beta FIX gamma",
)
#expect(
FixCardStore.appliedText(of: storedCard(quote: "anchor", fix: "FIX"), in: text)
== "alpha FIX beta anchor gamma",
)
// An empty fix cuts the span.
#expect(
FixCardStore.appliedText(of: storedCard(quote: "anchor ", occurrence: 2), in: text)
== "alpha anchor beta gamma",
)
// Beyond-count occurrence, vanished quote, empty quote: no edit.
#expect(FixCardStore.appliedText(of: storedCard(quote: "anchor", occurrence: 3, fix: "FIX"), in: text) == nil)
#expect(FixCardStore.appliedText(of: storedCard(quote: "gone", fix: "FIX"), in: text) == nil)
#expect(FixCardStore.appliedText(of: storedCard(quote: "", fix: "FIX"), in: text) == nil)
// Demonstration lane never applies (ADR-0006).
#expect(FixCardStore.appliedText(of: storedCard(quote: "anchor", fix: "FIX", lane: .demonstration), in: text) == nil)
}
/// #25 (S3.3): the in-place close — Skip/Done/Apply flip the card's
/// status in its own JSON file; every other field travels unchanged
/// (Skip records nothing beyond the flip, CONTEXT.md).
@Test func `close rewrites the card status in place`() throws {
let home = try toolsHome()
let piece = try writePiece(home)
let written = try FixCardStore.submit(piecePath: piece.path, cards: [card()])
let closed = try FixCardStore.close(pieceURL: piece, id: written[0].card.id, status: .skipped)
#expect(closed.status == .skipped)
#expect(closed.rank == 1)
#expect(closed.pattern == "Hedging")
// The SAME single file, status flipped, everything else intact.
let cardsDir = piece.appendingPathComponent("cards", isDirectory: true)
let files = try FileManager.default.contentsOfDirectory(atPath: cardsDir.path)
.filter { $0.hasSuffix(".json") }
#expect(files == [written[0].file])
let json = try #require(
try JSONSerialization.jsonObject(
with: Data(contentsOf: cardsDir.appendingPathComponent(written[0].file)),
) as? [String: Any],
)
#expect(json["status"] as? String == "skipped")
#expect(json["pattern"] as? String == "Hedging")
#expect(json["rank"] as? Int == 1)
// Scan sees the closed card; a foreign id errors, file untouched.
#expect(FixCardStore.scan(pieceURL: piece).first?.status == .skipped)
#expect(throws: Error.self) {
try FixCardStore.close(pieceURL: piece, id: UUID(), status: .done)
}
}
@Test func `scan returns newest first and skips undecodable`() throws {
let home = try toolsHome()
let piece = try writePiece(home)