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

+218 -24
View File
@@ -2,11 +2,10 @@
// FixCardsWindow.swift
// Museion
//
// #23 follow-up (dogfood): the Fix Cards palette — the durable card
// store's own surface, a floating window (scene id "fix-cards") opened
// from the mentor pass bar. The transcript renders card tool calls as
// one-line events; the card DETAIL lives here while the verdict gets
// worked through. Show-only, never acts (Apply is #25).
// #25 (S3.3): the Fix Cards palette becomes the card work surface. Open
// cards lead with their actions — Apply (a blocking ask), Skip, Done —
// closed cards dim behind them. No apply-all, no batch, nothing applies
// without the ask (S3.3). #23's show-only surface kept its bones.
//
import SwiftUI
@@ -28,9 +27,18 @@ struct FixCardsPalette: View {
.padding(.bottom, 4)
}
if posts.openPieceCards.isEmpty {
emptyState(hasPiece: posts.session?.postID != nil)
EmptyCardsState(hasPiece: posts.session?.postID != nil)
} else {
cardList(posts.openPieceCards)
CardList(cards: ordered(posts.openPieceCards))
// The visible trust posture (S2.5): apply = ask-before, one
// card at a time. Never-do needs no control here — nothing
// auto-applies; the ask is the only path to a write.
Text("apply = ask-before · one card at a time")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
.padding(.horizontal, 12)
.padding(.bottom, 10)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
@@ -45,7 +53,19 @@ struct FixCardsPalette: View {
return gate.posts.title(of: id)
}
private func emptyState(hasPiece: Bool) -> some View {
/// Open cards first, closed after — both groups keep the scan's order
/// (newest pass first, rank ascending; #25 S3.2). Live attention stays
/// on open work.
private func ordered(_ cards: [FixCardStore.StoredFixCard]) -> [FixCardStore.StoredFixCard] {
cards.filter { $0.status == .open } + cards.filter { $0.status != .open }
}
}
/// Nothing filed yet — names how cards arrive.
private struct EmptyCardsState: View {
let hasPiece: Bool
var body: some View {
VStack(alignment: .leading, spacing: 6) {
Text(hasPiece ? "No fix cards yet." : "No piece open.")
.font(.headline)
@@ -58,36 +78,46 @@ struct FixCardsPalette: View {
.padding(12)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
/// The open piece's STORED fix cards (moved from MentorPaneView #23 S2.4
/// — uncapped here, the window owns its scroll).
private func cardList(_ cards: [FixCardStore.StoredFixCard]) -> some View {
// ponytail: eager VStack in the ScrollView — the pass ceiling caps this at
// ~tens of cards; LazyVStack if a piece ever accumulates hundreds.
/// The piece's stored cards, open-first. The header counts OPEN work —
/// closed cards must not read as pending (#25).
private struct CardList: View {
let cards: [FixCardStore.StoredFixCard]
var body: some View {
let openCount = cards.count(where: { $0.status == .open })
ScrollView {
VStack(alignment: .leading, spacing: 8) {
Text("FIX CARDS · \(cards.count)")
Text(openCount < cards.count ? "FIX CARDS · \(openCount) open" : "FIX CARDS · \(openCount)")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
ForEach(cards, id: \.id) { card in
row(card)
CardRow(card: card)
}
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
/// One card: pattern title, problem + fix body, lane · emitter mono meta
/// (ADR-0006). A stale anchor reads distinct — orange + caption — and is
/// never appliable: v1 shows, never acts.
private func row(_ card: FixCardStore.StoredFixCard) -> some View {
/// One card: pattern title, problem + fix body, rank · pass · lane ·
/// emitter mono meta (ADR-0006). A stale anchor reads distinct — orange —
/// and never offers Apply. Closed cards dim, record their status in the
/// meta, and offer no actions. The blocking Apply ask is the ROW's own
/// sheet: one card asks at a time, by construction (S3.3).
private struct CardRow: View {
let card: FixCardStore.StoredFixCard
@Environment(AppGateState.self) private var gate
/// The row's own blocking ask; Apply arms it, the sheet answers it.
@State private var asking = false
var body: some View {
let stale = card.anchorStatus == .stale
var meta = "\(card.lane.rawValue) · \(card.emitter.rawValue)"
if let pass = card.provenance?.pass {
meta = "pass \(pass) · " + meta
}
if stale {
meta += " · stale anchor"
}
let open = card.status == .open
return VStack(alignment: .leading, spacing: 6) {
Label(card.pattern, systemImage: "rectangle.and.pencil.and.ellipsis")
.font(.callout.weight(.semibold))
@@ -101,9 +131,173 @@ struct FixCardsPalette: View {
Text(meta)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(stale ? .orange : DesignTokens.muted)
if open {
// The actions this card offers (#25 S3.3): mechanical +
// valid → Apply (opens the blocking ask) and Skip;
// demonstration → Done and Skip (never Apply, ADR-0006);
// stale anchor → Skip only.
HStack(spacing: 8) {
if stale {
SkipButton(card: card)
} else if card.lane == .mechanical {
Button("Apply") { asking = true }
.buttonStyle(.bordered)
.controlSize(.small)
SkipButton(card: card)
} else {
Button("Done") {
gate.posts.doneCard(card, home: gate.museionHome)
}
.buttonStyle(.bordered)
.controlSize(.small)
SkipButton(card: card)
}
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
.padding(10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(.quaternary.opacity(0.3), in: RoundedRectangle(cornerRadius: 8))
.opacity(open ? 1 : 0.45)
// Apply = ask-before, a BLOCKING ask: the sheet is the only path
// from the Apply button to a write.
.sheet(isPresented: $asking) {
ApplyAskSheet(card: card, posts: gate.posts, home: gate.museionHome)
}
}
/// rank · pass · lane · emitter, plus the card's truth markers: the
/// stale anchor, the trust level on appliable mechanical cards (S2.5),
/// and the close status once closed.
private var meta: String {
var value = "\(card.lane.rawValue) · \(card.emitter.rawValue)"
if let pass = card.provenance?.pass {
value = "pass \(pass) · " + value
}
if let rank = card.rank {
value = "#\(rank) · " + value
}
if card.anchorStatus == .stale {
value += " · stale anchor"
}
if card.status == .open, card.lane == .mechanical, card.anchorStatus != .stale {
value += " · apply · ask-before"
}
if card.status != .open {
value += " · \(card.status.rawValue)"
}
return value
}
}
/// Skip — not this instance (CONTEXT.md): closes the card, records
/// nothing beyond the flip.
private struct SkipButton: View {
let card: FixCardStore.StoredFixCard
@Environment(AppGateState.self) private var gate
var body: some View {
Button("Skip") {
gate.posts.skipCard(card, home: gate.museionHome)
}
.buttonStyle(.borderless)
.controlSize(.small)
}
}
/// The blocking ask (#25 S3.3): the exact edit, in full, before any write.
/// No apply-all, no batch, nothing applies without the ask — Apply is the
/// sheet's only write path, Cancel writes nothing. Outcome states: stale
/// swaps the body (no retry — the anchor no longer resolves), a refusal
/// shows its reason with the buttons kept.
private struct ApplyAskSheet: View {
let card: FixCardStore.StoredFixCard
let posts: PostsModel
let home: URL
@Environment(\.dismiss) private var dismiss
@State private var wentStale = false
@State private var refusal: String?
var body: some View {
VStack(alignment: .leading, spacing: 12) {
if wentStale {
Text("The text changed since this card was issued — nothing was selected or changed.")
.font(.callout)
Button("Close") { dismiss() }
.frame(maxWidth: .infinity, alignment: .trailing)
} else {
// Untrusted text in bounded chrome: quote/fix are
// client-authored, so the ask scrolls instead of growing
// past the sheet and pushing the buttons out of reach.
ScrollView {
VStack(alignment: .leading, spacing: 12) {
Text(card.pattern)
.font(.headline)
MonoBlock(
label: card.anchor.occurrence > 1
? "Anchored · occurrence \(card.anchor.occurrence)" : "Anchored",
text: card.anchor.quote,
)
if let context = card.anchor.context {
Text(context)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
}
MonoBlock(label: "Replacement", text: card.fix.isEmpty ? "(cut — the span is removed)" : card.fix)
Text("Apply writes this edit to the piece · ask-before")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
if let refusal {
Text(refusal)
.font(.callout)
.foregroundStyle(.orange)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
HStack(spacing: 8) {
Button("Apply") { apply() }
.buttonStyle(.borderedProminent)
Button("Cancel") { dismiss() }
.buttonStyle(.bordered)
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
.padding(16)
.frame(width: 320, alignment: .leading)
}
private func apply() {
switch posts.applyCard(card, home: home) {
case .applied:
dismiss()
case .stale:
wentStale = true
case let .refused(reason):
refusal = reason
}
}
}
/// One labeled mono text block — the ask's edit halves.
private struct MonoBlock: View {
let label: String
let text: String
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(label)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
Text(text)
.font(.system(size: 12, design: DesignTokens.mono))
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(8)
.background(.quaternary.opacity(0.3), in: RoundedRectangle(cornerRadius: 6))
}
}
}