refactor(session): type the transcript rail
CI / ci (push) Canceled after 0s

SessionRow text rows carry (body, speaker, notice) as data — agent/
user/system plus refused/interrupted/connectionLost/kickoff/failed/
plain — typed at every append site; no stored row holds a speaker
prefix (pinned). The two wire-only classifications are sniffed once at
the append seam: a replayed pass kick-off ('Pass Pass N…') and the
engine's 'Operation interrupted:' notice. The transcript view, the
writing-session filter, and the New Post prefill switch on typed data;
rendering is identical (refusal/connection-lost rows stay agent prose
by pinned render).

Fix Card and Research Note payloads decode through one shared
ToolPayloads decoder: lenient for the transcript rail (both the nested
card-rail shape and the flat #14-era fake kept), strict for the
museion-tools MCP trust boundary with named CardErrors. Three
intentional wire tightenings at the strict entry: empty cards/notes
arrays throw instead of no-op succeeding, the camelCase
diagnosticQuestion alias is rejected (schema is snake_case), and
decode failures answer short named errors instead of DecodingError
dumps. quote/note type parity restored (present-but-wrong-type throws).
Every wire-shape fact is pinned in ToolPayloadsTests.

Full suite 653/653, swiftformat/swiftlint clean. Audit flip-verified.

Closes #177
This commit is contained in:
AlexKucera committed 2026-11-09 16:01:53 +01:00
1 parent c2eb9573db
commit d14cb40e73
18 files changed
+912 -358

No files matched your search

+41 -88
View File
@@ -169,68 +169,18 @@ struct SessionTranscriptView: View {
}
}
/// #155: the editorial-voice classifier — the model's text rows carry
/// speaker prefixes ("Mentor: ", "You: "); the render seam maps them
/// onto the contract's three voices. Everything else (refusals, pass
/// kick-off labels, reveals) is the system's mono voice. Static,
/// nonisolated — pure, testable from any context.
/// Round-1 polish: engine-emitted operational notices (interruptions)
/// and replayed pass kick-offs are STATUS, not conversation — they map
/// to the system voice too (never serif prose, never a user capsule).
nonisolated static func speaker(of text: String) -> Speaker {
if text.hasPrefix("Mentor: ") {
let body = String(text.dropFirst("Mentor: ".count))
return body.hasPrefix("Operation interrupted:") ? .system : .agent
}
if text.hasPrefix("You: ") {
return isPassKickoff(String(text.dropFirst("You: ".count))) ? .system : .user
}
return .system
}
/// Round-1 polish: a replayed pass kick-off — the engine replays the
/// pass prompt as a user turn whose body opens with PassContext's fixed
/// "Pass Pass N — <level>." line. Static, nonisolated — pure.
nonisolated static func isPassKickoff(_ body: String) -> Bool {
body.hasPrefix("Pass Pass ")
}
/// Round-1 polish: the system row's display text — a replayed pass
/// kick-off shows its FIRST LINE only (the full engine prompt stays out
/// of the UI); every other status row renders verbatim. Static,
/// nonisolated — pure, testable from any context.
///
/// Round-2 polish (r1 regression): PassContext's first line already
/// carries "Pass " + the pass name, so the raw line reads "Pass Pass
/// 2 — …" — collapse the doubled leading "Pass Pass N" to "Pass N".
nonisolated static func statusDisplayText(_ text: String) -> String {
guard text.hasPrefix("You: ") else { return text }
let body = text.dropFirst("You: ".count)
guard isPassKickoff(String(body)) else { return text }
/// #177 (S2): a replayed pass kick-off shows its FIRST LINE only (the
/// full engine prompt stays out of the UI); PassContext's first line
/// already carries "Pass " + the pass name, so the raw line reads
/// "Pass Pass 2 — …" — collapse the doubled leading "Pass Pass N" to
/// "Pass N". The row arrives typed (.system, .kickoff) from the model's
/// append seam, so the prefix is unconditional — no re-sniff here.
/// Static, nonisolated — pure, test-pinnable.
nonisolated static func kickoffDisplayText(_ body: String) -> String {
let line = String(body.prefix(while: { $0 != "\n" }))
guard line.hasPrefix("Pass Pass ") else { return line }
return "Pass " + line.dropFirst("Pass Pass ".count)
}
/// Round-2 polish (argon#24/mercury#60/sol#9): operational warnings —
/// engine-emitted interruptions, connection losses, and the model's
/// failure rows — get the restrained terracotta accent in the status
/// voice. Refusals and cancellations stay neutral (they are outcomes,
/// not failures). Static, nonisolated — pure, test-pinnable.
nonisolated static func isWarningStatus(_ text: String) -> Bool {
let body = text.hasPrefix("Mentor: ") ? String(text.dropFirst("Mentor: ".count)) : text
return body.hasPrefix("Operation interrupted")
|| body.hasPrefix("connection lost")
|| body.contains(" failed — ")
}
/// The three contract voices for a text row.
nonisolated enum Speaker {
case agent
case user
case system
}
/// The composer's Return split: plain Return sends, Shift+Return breaks
/// the line, every other chord passes through to native handling. Caps
/// Lock is masked — an exact `==` turned Shift+Return under Caps Lock
@@ -255,12 +205,15 @@ struct SessionTranscriptView: View {
@ViewBuilder
private func rowView(_ row: SessionRow, previous: SessionRow?) -> some View {
switch row.content {
case let .text(text):
switch Self.speaker(of: text) {
case let .text(body, speaker, notice):
// #177 (S2): the render seam switches on typed data — speaker and
// notice arrive from the model's append sites, never re-derived
// from stored prefixes.
switch speaker {
case .agent:
agentProse(markdownText(String(text.dropFirst("Mentor: ".count))))
agentProse(markdownText(body))
case .user:
userCapsule(String(text.dropFirst("You: ".count)))
userCapsule(body)
case .system:
// Round-7 (mercury#21): the full warning treatment (tinted
// fill + border) renders ONLY while this row is the
@@ -269,13 +222,18 @@ struct SessionTranscriptView: View {
// Round-8 (argon#8): a historical warning keeps the
// terracotta ink + glyph (see `statusBox`) — distinct,
// just not shouting.
let current = Self.isWarningStatus(text) && isLastMeaningfulRow(row)
statusBox(Self.statusDisplayText(text), current: current)
// Round-7 (argon#39): 8pt of air when the warning
// banner directly follows the pass kick-off pill —
// at the stack's uniform 18pt the pair read as one
// fused block.
.padding(.top, current && Self.isKickoffPill(previous) ? 8 : 0)
let warning = notice == .interrupted || notice == .connectionLost || notice == .failed
let current = warning && isLastMeaningfulRow(row)
statusBox(
notice == .kickoff ? Self.kickoffDisplayText(body) : body,
warning: warning,
current: current,
)
// Round-7 (argon#39): 8pt of air when the warning
// banner directly follows the pass kick-off pill —
// at the stack's uniform 18pt the pair read as one
// fused block.
.padding(.top, current && Self.isKickoffPill(previous) ? 8 : 0)
}
case let .thought(text):
// #24 dogfood fix: collapsed — a pass's thought stream runs to
@@ -350,8 +308,8 @@ struct SessionTranscriptView: View {
/// column's full width, leading edge aligned (fit-content pills and
/// full-width banners read as two glued regions).
@ViewBuilder
private func statusBox(_ text: String, current: Bool = false) -> some View {
if Self.isWarningStatus(text) {
private func statusBox(_ text: String, warning: Bool, current: Bool = false) -> some View {
if warning {
let parts = Self.warningParts(text)
WarningStatusRow(
headline: parts.headline,
@@ -615,21 +573,15 @@ private struct WarningStatusRow: View {
// NOT private: ACPClientTests pins both seams via @testable.)
extension SessionTranscriptView {
/// Round-6 (all seats): the warning row's headline/detail split — drop
/// the speaker prefix, then cut at the first ": " ("Operation
/// interrupted: waiting for model response (61.6s elapsed)." → headline
/// "Operation interrupted", detail the rest). No ": " → the whole line
/// is the headline ("connection lost"). Static, nonisolated — pure.
/// Round-6 (all seats): the warning row's headline/detail split — cut
/// at the first ": " ("Operation interrupted: waiting for model
/// response (61.6s elapsed)." → headline "Operation interrupted",
/// detail the rest). Bodies arrive UNPREFIXED (#177 typed rows). No
/// ": " → the whole line is the headline ("connection lost").
/// Static, nonisolated — pure.
nonisolated static func warningParts(_ text: String) -> (headline: String, detail: String?) {
var body = text
if body.hasPrefix("Mentor: ") {
body = String(body.dropFirst("Mentor: ".count))
}
if body.hasPrefix("You: ") {
body = String(body.dropFirst("You: ".count))
}
guard let separator = body.range(of: ": ") else { return (body, nil) }
return (String(body[..<separator.lowerBound]), String(body[separator.upperBound...]))
guard let separator = text.range(of: ": ") else { return (text, nil) }
return (String(text[..<separator.lowerBound]), String(text[separator.upperBound...]))
}
/// Round-8 (argon#8 — calm copy): the engine's own interruption detail,
@@ -666,11 +618,12 @@ private extension SessionTranscriptView {
}
/// Round-7 (argon#39): a replayed pass kick-off row — the status pill
/// a warning banner must not hug (the 8pt-air rule). Static,
/// a warning banner must not hug (the 8pt-air rule). #177 (S2):
/// keyed on the typed notice, not a stored prefix. Static,
/// nonisolated — pure.
nonisolated static func isKickoffPill(_ row: SessionRow?) -> Bool {
guard let row, case let .text(text) = row.content, text.hasPrefix("You: ") else { return false }
return isPassKickoff(String(text.dropFirst("You: ".count)))
guard let row, case .text(_, _, .kickoff) = row.content else { return false }
return true
}
}