Files
museion/Museion/FixCardsWindow.swift
T
AlexKucera 5bf09552f6
CI / ci (push) Canceled after 0s
refactor(posts): route every library write through one rail
#179: every library-reflecting store write (draft save, session-born
post, rename, move to project, archive/restore, trash, convert,
project creation, import batch, pass advance) now rides one internal
seam — performWrite pairs the write with exactly one rescan and owns
the error strip (cleared BEFORE the write so importFiles' per-file
failure messages survive the success tail; a nil-Home refusal writes
nothing). The model owns the Home: watchedHome is private(set), set
only by bind (construction + the Settings repoint); no public method
takes a Home parameter — 14 view call sites and ~115 test call sites
swept, dead home plumbing cut. The editor's debounced flush, the card
closes, the pass substrate and the micropub publish stay off the rail
by design (own error semantics, no library regroup).

A converged review finding is closed with a guard: the pass advance
re-resolves watchedHome after the engine's minutes-long turn, so a
mid-pass Settings repoint now refuses the advance with an honest strip
message instead of silently stamping pass state into the repointed
library (wrong-piece write on a slug collision). createProject
failures now surface in the sheet AND the strip — the rail-owns-the-
strip posture supersedes the #108 sheet-only rule.

Five rail pins (PostsModelWriteRailTests, suite members): write → one
library refresh + strip cleared, failed write owns the strip, nil-Home
refusal, import batch keeps per-file messages, mid-pass repoint
refuses the advance — the last two flip-verified against their guards.
ScriptedEngine gains endHeldPrompt (release a held turn as ended).

Verification: suite 657/657 the evening this landed; the next-morning
re-run could not start — both test runners hung before establishing
connection (#134 testmanagerd wedge; an untouched control suite hangs
identically — machine state, not this diff; reboot pending). Rail pins
5/5 isolated after the last test-only edit; swiftlint at baseline
parity; swiftformat clean.

Closes #179

[Generated by AI]
2026-11-10 08:45:04 +01:00

321 lines
11 KiB
Swift

//
// FixCardsWindow.swift
// Museion
//
// #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
/// Palette body: the open piece's stored fix cards (`<piece>/cards/`, any
/// MCP client's submissions — PostsModel re-derives on open/rescan, so this
/// stays current via @Observable). Re-contextualizes as the piece changes.
struct FixCardsPalette: View {
@Environment(AppGateState.self) private var gate
var body: some View {
let posts = gate.posts
VStack(alignment: .leading, spacing: 0) {
if let title = openPieceTitle {
Text(title)
.font(.headline)
.padding(.horizontal, 12)
.padding(.top, 12)
.padding(.bottom, 4)
}
if posts.openPieceCards.isEmpty {
EmptyCardsState(hasPiece: posts.session?.postID != nil)
} else {
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)
// The window reads as app chrome, not a document — the same warm
// cream the workspace root/rail paints (DesignTokens.background).
.background(DesignTokens.background)
}
/// The pass label's title source — PostsModel's shared lookup.
private var openPieceTitle: String? {
guard let id = gate.posts.session?.postID else { return nil }
return gate.posts.title(of: id)
}
/// 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)
Text(hasPiece
? "Run a pass — cards land here as the mentor files them."
: "Open a post — its fix cards collect here.")
.font(.callout)
.foregroundStyle(.secondary)
}
.padding(12)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
// 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(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
CardRow(card: card)
}
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
/// 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
/// #26: the promotion ask — Promote arms it, its sheet answers it.
@State private var promoting = false
var body: some View {
let stale = card.anchorStatus == .stale
let open = card.status == .open
return VStack(alignment: .leading, spacing: 6) {
Label(card.pattern, systemImage: "rectangle.and.pencil.and.ellipsis")
.font(.callout.weight(.semibold))
.foregroundStyle(stale ? .orange : DesignTokens.foreground)
if !card.problem.isEmpty {
Text(card.problem).font(.callout)
}
if !card.fix.isEmpty {
Text("Fix: \(card.fix)").font(.callout)
}
Text(meta)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(stale ? .orange : DesignTokens.muted)
if open || card.promotedAt == nil {
// The actions this card offers (#25 S3.3, #26 S4.1):
// mechanical + valid → Apply (the blocking ask) and Skip;
// demonstration → Done and Skip (never Apply, ADR-0006);
// stale anchor → Skip only. Promote (its own ask) sits
// between the primary action and Skip, and outlives the
// card — closed cards keep it until promoted (#26: the
// Learning is the durable artifact).
HStack(spacing: 8) {
if open, !stale {
if card.lane == .mechanical {
Button("Apply") { asking = true }
.buttonStyle(.bordered)
.controlSize(.small)
} else {
Button("Done") {
gate.posts.doneCard(card)
}
.buttonStyle(.bordered)
.controlSize(.small)
}
}
if card.promotedAt == nil {
Button("Promote") { promoting = true }
.buttonStyle(.bordered)
.controlSize(.small)
}
if open {
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)
}
// #26: Promote is its own blocking ask — one confirm, one Learning.
.sheet(isPresented: $promoting) {
PromoteAskSheet(card: card, posts: gate.posts)
}
}
/// rank · pass · lane · emitter, plus the card's truth markers: the
/// stale anchor, the trust level on appliable mechanical cards (S2.5),
/// the close status, and the promotion stamp (#26).
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)"
}
if card.promotedAt != nil {
value += " · promoted"
}
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)
}
.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
@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) {
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))
}
}
}