refactor(posts): route every library write through one rail
CI / ci (push) Canceled after 0s

#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]
This commit is contained in:
AlexKucera committed 2026-11-10 08:45:04 +01:00
1 parent 627ecf515e
commit 5bf09552f6
27 files changed
+572 -345

No files matched your search

+20 -20
View File
@@ -23,7 +23,7 @@ import Testing
/// hop-back instead of asserting synchronously (PostsModelTests convention).
/// The post-loop expect makes a silent timeout a failure, not a pass.
@MainActor
private func pollUntil(_ condition: @MainActor () -> Bool) async throws {
func pollUntil(_ condition: @MainActor () -> Bool) async throws {
for _ in 0 ..< 250 where !condition() {
try await Task.sleep(nanoseconds: 20_000_000)
}
@@ -51,7 +51,7 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
let session = try #require(model.session)
let generation = session.loadGeneration
@@ -59,7 +59,7 @@ struct FixCardApplyTests {
// buffer first (flush-first, like every op that reads the piece).
session.edit("# Carded\n\nThe anchor sentence.\nPlus a typed line.\n")
#expect(model.applyCard(written[0].card, home: home) == .applied)
#expect(model.applyCard(written[0].card) == .applied)
// Disk carries the replacement over the flushed buffer...
#expect(PostStore.readBody(id: post.id, home: home) == "# Carded\n\nThe replaced sentence.\nPlus a typed line.\n")
@@ -80,14 +80,14 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
// The Post changed after the card was issued — the quote is gone.
model.session?.edit("# Carded\n\nThe sentence changed.\n")
model.session?.flush()
let postURL = folder.appendingPathComponent("post.md")
let before = try Data(contentsOf: postURL)
#expect(model.applyCard(written[0].card, home: home) == .stale)
#expect(model.applyCard(written[0].card) == .stale)
#expect(try Data(contentsOf: postURL) == before) // nothing selected, nothing changed
let diskCard = try #require(FixCardStore.scan(pieceURL: folder).first)
@@ -104,11 +104,11 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(pattern: "Demo", fix: "The replaced sentence.", quote: "The anchor sentence.", lane: .demonstration)],
)
model.select(post.id, home: home)
model.select(post.id)
let postURL = folder.appendingPathComponent("post.md")
let before = try Data(contentsOf: postURL)
let outcome = model.applyCard(written[0].card, home: home)
let outcome = model.applyCard(written[0].card)
guard case let .refused(why) = outcome else {
Issue.record("expected refusal, got \(outcome)")
return
@@ -127,7 +127,7 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
// A foreign write lands on disk underneath the writer (#67) — the
// reload question is up and unanswered.
let postURL = folder.appendingPathComponent("post.md")
@@ -135,7 +135,7 @@ struct FixCardApplyTests {
model.session?.diskDidChange(diskText: "foreign text on disk")
let before = try Data(contentsOf: postURL)
let outcome = model.applyCard(written[0].card, home: home)
let outcome = model.applyCard(written[0].card)
guard case let .refused(why) = outcome else {
Issue.record("expected refusal, got \(outcome)")
return
@@ -157,15 +157,15 @@ struct FixCardApplyTests {
card(pattern: "Telling", quote: "The anchor sentence."),
],
)
model.select(post.id, home: home)
model.select(post.id)
let postURL = folder.appendingPathComponent("post.md")
let before = try Data(contentsOf: postURL)
model.skipCard(written[0].card, home: home)
model.skipCard(written[0].card)
try await pollUntil { model.openPieceCards.first(where: { $0.id == written[0].card.id })?.status == .skipped }
#expect(try FixCardStore.scan(pieceURL: folder).first(where: { $0.id == written[0].card.id })?.status == .skipped)
model.doneCard(written[1].card, home: home)
model.doneCard(written[1].card)
try await pollUntil { model.openPieceCards.first(where: { $0.id == written[1].card.id })?.status == .done }
#expect(try FixCardStore.scan(pieceURL: folder).first(where: { $0.id == written[1].card.id })?.status == .done)
@@ -241,7 +241,7 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
model.session?.edit("# Carded\n\nThe sentence changed.\n") // stale the anchor
model.session?.flush()
let postURL = folder.appendingPathComponent("post.md")
@@ -252,7 +252,7 @@ struct FixCardApplyTests {
try FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: cardsDir.path)
defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: cardsDir.path) }
#expect(model.applyCard(written[0].card, home: home) == .stale) // the primary contract holds…
#expect(model.applyCard(written[0].card) == .stale) // the primary contract holds…
#expect(model.lastError?.contains("card close failed") == true) // …and the flag failure is loud
#expect(try Data(contentsOf: postURL) == before) // still nothing selected or changed
}
@@ -270,12 +270,12 @@ struct FixCardApplyTests {
)
try FileManager.default.removeItem(at: folder.appendingPathComponent("post.md")) // the read will fail
model.select(post.id, home: home)
model.select(post.id)
#expect(model.session?.loadFailed == true)
// The writer typed the anchor into the empty failed-session buffer.
model.session?.edit("The anchor sentence. typed nowhere durable.")
let outcome = model.applyCard(written[0].card, home: home)
let outcome = model.applyCard(written[0].card)
guard case .refused = outcome else {
Issue.record("expected refusal, got \(outcome)")
return
@@ -295,9 +295,9 @@ struct FixCardApplyTests {
let post = try PostStore.writeNew(body: "# Carded\n\nThe anchor sentence.\n", home: home)
let folder = try #require(PostStore.folder(of: post.id, home: home))
let written = try FixCardStore.submit(piecePath: folder.path, cards: [card()])
model.select(post.id, home: home)
model.select(post.id)
#expect(model.promoteCard(written[0].card, home: home) == .promoted)
#expect(model.promoteCard(written[0].card) == .promoted)
let learnings = home.appendingPathComponent("learnings/posts", isDirectory: true)
#expect(try FileManager.default.contentsOfDirectory(atPath: learnings.path).filter { $0.hasSuffix(".md") }.count == 1)
@@ -306,7 +306,7 @@ struct FixCardApplyTests {
// Already promoted → refused, no second file (one confirm = one entry).
let scanned = try #require(FixCardStore.scan(pieceURL: folder).first)
#expect(model.promoteCard(scanned, home: home) == .refused("card already promoted"))
#expect(model.promoteCard(scanned) == .refused("card already promoted"))
#expect(try FileManager.default.contentsOfDirectory(atPath: learnings.path).filter { $0.hasSuffix(".md") }.count == 1)
}
@@ -316,7 +316,7 @@ struct FixCardApplyTests {
let folder = try #require(PostStore.folder(of: post.id, home: home))
let written = try FixCardStore.submit(piecePath: folder.path, cards: [card()])
#expect(model.promoteCard(written[0].card, home: home) == .refused("open the piece to work its cards"))
#expect(model.promoteCard(written[0].card) == .refused("open the piece to work its cards"))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("learnings").path))
#expect(model.lastError == nil)
}