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

+28 -22
View File
@@ -132,14 +132,19 @@ struct SessionLifecycleTests {
}
let replayed = mentor.transcriptRows(of: resumedEntry)
#expect(settled)
#expect(texts(in: replayed).first == "You: earlier ask")
// #177 (S2): the replayed rows land typed at the append seam — a
// plain replayed user turn is (.user, .plain), the agent chunk
// (.agent, .plain); the stored bodies are unprefixed.
#expect(replayed.contains { $0.content == .text("earlier ask", .user, .plain) })
#expect(replayed.contains { $0.content == .text("earlier verdict", .agent, .plain) })
#expect(texts(in: replayed).first == "earlier ask")
#expect(replayed.contains { row in
if case let .thought(text) = row.content {
return text == "old thinking"
}
return false
})
#expect(texts(in: replayed).contains("Mentor: earlier verdict"))
#expect(texts(in: replayed).contains("earlier verdict"))
// The wire calls went out over session/load.
await drainStderr(mentor, containing: "load ghost")
#expect(mentor.stderrTail.contains { $0.contains("load ghost") })
@@ -304,7 +309,7 @@ struct SessionLifecycleTests {
// prompt/fork/compact never run against unloaded state.
let rows = try second.transcriptRows(of: #require(second.activeEntry))
#expect(rows.contains { row in
if case let .text(text) = row.content {
if case let .text(text, _, _) = row.content {
return text.contains("earlier ask")
}
return false
@@ -452,7 +457,7 @@ struct SessionLifecycleTests {
// marker proves the words came from the engine, not the local copy.
let rows = try mentor.transcriptRows(of: #require(mentor.activeEntry))
#expect(rows.contains { row in
if case let .text(text) = row.content {
if case let .text(text, _, _) = row.content {
return text.contains("earlier ask")
}
return false
@@ -460,17 +465,18 @@ struct SessionLifecycleTests {
}
}
// Scenario: draft candidate — from #30 (S2.7). Given a session
// transcript, when the sheet prefills, then the last mentor text row is
// the draft candidate; thought/card/research/raw rows never prefill.
// Scenario: draft candidate — from #30 (S2.7), retyped #177 (S2). Given a
// session transcript, when the sheet prefills, then the last agent
// conversation row (.agent, .plain) is the draft candidate; noticed agent
// rows, user rows, and thought/card/research/raw rows never prefill.
// File-scope extension — type_body_length posture; same suite, same file.
@MainActor
extension SessionLifecycleTests {
@Test func `draft candidate is the last mentor text row`() {
@Test func `draft candidate is the last agent plain text row`() {
let rows: [SessionRow] = [
SessionRow(content: .text("You: help me draft this")),
SessionRow(content: .text("help me draft this", .user, .plain)),
SessionRow(content: .thought("thinking hard about angles")),
SessionRow(content: .text("Mentor: first reply")),
SessionRow(content: .text("first reply", .agent, .plain)),
SessionRow(content: .researchNote(ResearchCandidate(
piecePath: "/tmp/piece", title: "T", url: "https://x", quote: "", note: "",
))),
@@ -479,32 +485,32 @@ extension SessionLifecycleTests {
anchor: "a", provenance: "engine", status: "open",
))),
SessionRow(content: .raw(kind: "tool call", json: "{}")),
SessionRow(content: .text("Mentor: second and latest reply")),
SessionRow(content: .text("second and latest reply", .agent, .plain)),
]
#expect(MentorSessionModel.draftCandidateText(in: rows) == "second and latest reply")
}
@Test func `no mentor text yields no draft candidate`() {
@Test func `no agent plain text yields no draft candidate`() {
// Empty transcript, user-only, and non-text lanes never prefill.
#expect(MentorSessionModel.draftCandidateText(in: []) == nil)
#expect(MentorSessionModel.draftCandidateText(in: [
SessionRow(content: .text("You: hello")),
SessionRow(content: .text("hello", .user, .plain)),
SessionRow(content: .thought("hm")),
]) == nil)
// A trailing blank mentor row doesn't blank the prefill — the
// A trailing blank agent body doesn't blank the prefill — the
// earlier real reply is still the candidate.
#expect(MentorSessionModel.draftCandidateText(in: [
SessionRow(content: .text("Mentor: the real reply")),
SessionRow(content: .text("Mentor: ")),
SessionRow(content: .text("the real reply", .agent, .plain)),
SessionRow(content: .text(" ", .agent, .plain)),
]) == "the real reply")
// Refusal/connection rows share the display prefix — an error is
// not the draft; an earlier reply still wins, error-only yields nil.
// #177 (S2): noticed agent rows are excluded BY NOTICE — an error
// is not the draft; an earlier reply still wins, error-only nil.
#expect(MentorSessionModel.draftCandidateText(in: [
SessionRow(content: .text("Mentor: the real reply")),
SessionRow(content: .text("Mentor: connection lost — broken pipe")),
SessionRow(content: .text("the real reply", .agent, .plain)),
SessionRow(content: .text("connection lost — broken pipe", .agent, .connectionLost)),
]) == "the real reply")
#expect(MentorSessionModel.draftCandidateText(in: [
SessionRow(content: .text("Mentor: (refused the prompt)")),
SessionRow(content: .text("(refused the prompt)", .agent, .refused)),
]) == nil)
}
}
@@ -512,7 +518,7 @@ extension SessionLifecycleTests {
@MainActor
private func texts(in rows: [SessionRow]) -> [String] {
rows.compactMap {
if case let .text(text) = $0.content {
if case let .text(text, _, _) = $0.content {
text
} else {
nil