From 4d665680ad882e0db9b3f7090cedc46fa91c5c2c Mon Sep 17 00:00:00 2001 From: Dawid Buszta <344369893+MF-DEEV@users.noreply.github.com> Date: Thu, 22 Oct 2026 12:31:31 +0200 Subject: [PATCH 1/2] fix(sessions): a recap is not a finished turn Writing a recap, on its own when a session is returned to or with /recap, makes Claude Code say busy for a moment, and /recap, like other slash commands, writes a user line that no reply follows. The island counted both as a turn and announced it done. Now a turn known only from the registry, with nothing typed, said or run, finishes nothing; slash command lines begin no turn here or in AI Agents; and going back to a result already shown is not announced again. --- .../AgentUsage/Harness/ClaudeReader.swift | 14 +++++++-- .../ClaudeSessions/ClaudeSessionEngine.swift | 25 +++++++++++++-- .../ClaudeTranscriptReader.swift | 4 +++ Tests/ClaudeSessionEngineTests.swift | 31 +++++++++++++++++++ Tests/ClaudeSessionReaderTests.swift | 3 ++ Tests/HarnessReaderContractTests.swift | 3 ++ 6 files changed, 76 insertions(+), 4 deletions(-) diff --git a/Sources/Vorssaint/Services/AgentUsage/Harness/ClaudeReader.swift b/Sources/Vorssaint/Services/AgentUsage/Harness/ClaudeReader.swift index 268671fa3..5fae3c1d5 100644 --- a/Sources/Vorssaint/Services/AgentUsage/Harness/ClaudeReader.swift +++ b/Sources/Vorssaint/Services/AgentUsage/Harness/ClaudeReader.swift @@ -54,10 +54,12 @@ struct ClaudeReader: AgentHarnessReader { // tool output can be large: no need to decode it to know that. if AgentLogParser.contains(line, #""isSidechain":true"#) { return [] } // Claude Code compacts an idle session on its own, writing the summary - // as a user line that no reply follows; it begins no work. + // as a user line that no reply follows; it begins no work. Nor does a + // slash command, like /recap, whose output now goes to a system line: + // one that asks the model, like a skill, begins with the reply. guard let json = AgentLogParser.object(line), json["type"] as? String == "user", json["isMeta"] as? Bool != true, json["isSidechain"] as? Bool != true, - json["isCompactSummary"] as? Bool != true else { return [] } + json["isCompactSummary"] as? Bool != true, !isCommand(json) else { return [] } adopt(json, into: &state) state.turnOpen = true state.runningCommands = [] @@ -115,6 +117,14 @@ struct ClaudeReader: AgentHarnessReader { return entries } + /// A slash command the person typed, which is not a prompt by itself. + private func isCommand(_ json: [String: Any]) -> Bool { + let content = (json["message"] as? [String: Any])?["content"] + let text = (content as? String) + ?? (content as? [[String: Any]])?.first { $0["type"] as? String == "text" }?["text"] as? String ?? "" + return text.trimmingCharacters(in: .whitespacesAndNewlines).hasPrefix("") + } + /// The interruption or local command output the person's message holds, /// never text inside a tool result. private func endsTurn(_ json: [String: Any]) -> Bool { diff --git a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift index 90cff95e9..661c688a9 100644 --- a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift +++ b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift @@ -23,6 +23,10 @@ final class ClaudeSessionEngine { struct Activity { var turnOpen = false var turnStarted: Date? + /// The open turn is known only from the registry saying busy, with + /// nothing typed, said or run since; and when the turn before began. + var registryOnly = false + var startedBefore: Date? var turnEnded: Date? var completed = true /// The newest moment a hook or transcript line told of. @@ -96,14 +100,26 @@ final class ClaudeSessionEngine { // ended even when its last lines were not read yet. if var entry = activity[id], entry.turnOpen, record.status == .idle || record.status == .shell, let changed = record.statusUpdated, changed > entry.lastFact ?? .distantPast { - end(at: changed, completed: true, in: &entry) + if entry.registryOnly { + // Busy a moment with nothing typed, said or run, as while + // Claude Code writes a recap of the session: no turn ran, + // and nothing finished. + entry.turnOpen = false + entry.turnStarted = entry.startedBefore + entry.registryOnly = false + } else { + end(at: changed, completed: true, in: &entry) + } activity[id] = entry } // And it went to work before its prompt was read. if record.status == .busy, let changed = record.statusUpdated { var entry = activity[id] ?? Activity(firstSeen: now) if !entry.turnOpen, changed > entry.lastFact ?? .distantPast { + let before = entry.turnStarted begin(at: changed, in: &entry) + entry.registryOnly = true + entry.startedBefore = before activity[id] = entry } } @@ -188,7 +204,10 @@ final class ClaudeSessionEngine { events.append(.needsPermission(id: id, tool: entry.permissions.first?.tool ?? entry.openTool?.name ?? "")) case .waitingInput: events.append(.needsInput(id: id)) - case .finished where previous.phase.isActive || previous.phase.isWaiting: + // Only a turn that ended after the work was seen begin is news; + // going back to a result already shown, as after a recap, is not. + case .finished where (previous.phase.isActive || previous.phase.isWaiting) + && (entry.turnEnded ?? .distantPast) >= previous.since: let duration = entry.turnStarted.flatMap { start in entry.turnEnded.map { $0.timeIntervalSince(start) } } events.append(.finished(id: id, duration: duration)) default: break @@ -491,5 +510,7 @@ final class ClaudeSessionEngine { private func touch(_ date: Date, in entry: inout Activity) { entry.lastFact = max(entry.lastFact ?? date, date) + // Something was typed, said or run: the turn is real. + entry.registryOnly = false } } diff --git a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift index 5b50c4f4a..91c404f42 100644 --- a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift +++ b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift @@ -113,6 +113,10 @@ struct ClaudeTranscriptReader { ?? blocks.compactMap { $0["type"] as? String == "text" ? $0["text"] as? String : nil } if texts.contains(where: { $0.hasPrefix("[Request interrupted by user") || $0.hasPrefix("") }) { + // A slash command typed in the terminal, like /recap or /cost, + // asks the model nothing by itself; one that does, like a + // skill, begins its turn with the reply or the registry. } else if !texts.isEmpty { facts.append(.turnBegan(date)) // Task notifications and the summary a compaction leaves diff --git a/Tests/ClaudeSessionEngineTests.swift b/Tests/ClaudeSessionEngineTests.swift index bad15035b..b94de624d 100644 --- a/Tests/ClaudeSessionEngineTests.swift +++ b/Tests/ClaudeSessionEngineTests.swift @@ -76,6 +76,37 @@ enum ClaudeSessionEngineTests { suite.expect(only(waiting, 6)?.phase == .waitingInput, "other prompts wait for input") suite.expect(only(waiting, 6)?.waitingFor == "input needed", "the reason is kept while it waits") + let recap = ClaudeSessionEngine() + recap.ingest(records: [record(status: .busy, changed: 0)], now: at(0)) + recap.ingest(sessionID: "s1", facts: [.turnBegan(at(1)), .text("Done.", at: at(2)), .turnEnded(at(3), completed: true)], now: at(3)) + recap.ingest(records: [record(status: .idle, changed: 3)], now: at(3)) + recap.acknowledge("s1", now: at(4)) + _ = recap.snapshot(now: at(700)) + recap.ingest(records: [record(status: .busy, changed: 900)], now: at(900)) + _ = recap.snapshot(now: at(901)) + recap.ingest(records: [record(status: .idle, changed: 902)], now: at(902)) + let recapped = recap.snapshot(now: at(903)) + suite.expect(recapped.sessions.first?.phase == .idle && recapped.sessions.first?.turnStarted == at(0) + && !recapped.events.contains { if case .finished = $0 { return true }; return false }, + "busy a moment with nothing typed or said, as while a recap is written, finishes nothing") + let fresh = ClaudeSessionEngine() + fresh.ingest(records: [record(status: .busy, changed: 0)], now: at(0)) + fresh.ingest(sessionID: "s1", facts: [.turnBegan(at(1)), .turnEnded(at(3), completed: true)], now: at(3)) + fresh.ingest(records: [record(status: .idle, changed: 3)], now: at(3)) + _ = fresh.snapshot(now: at(4)) + fresh.ingest(records: [record(status: .busy, changed: 20)], now: at(20)) + _ = fresh.snapshot(now: at(21)) + fresh.ingest(records: [record(status: .idle, changed: 22)], now: at(22)) + let back = fresh.snapshot(now: at(23)) + suite.expect(back.sessions.first?.phase == .finished && back.events.isEmpty, + "a recap over a result not yet looked at shows it again without a second notice") + recap.ingest(records: [record(status: .busy, changed: 1000)], now: at(1000)) + _ = recap.snapshot(now: at(1001)) + recap.ingest(sessionID: "s1", facts: [.turnBegan(at(1000)), .text("Fixed.", at: at(1001))], now: at(1001)) + recap.ingest(records: [record(status: .idle, changed: 1003)], now: at(1003)) + suite.expect(recap.snapshot(now: at(1004)).events.contains { if case .finished = $0 { return true }; return false }, + "a turn the transcript shows still finishes when the registry goes idle") + let render = ClaudeSessionEngine() render.ingest(records: [record(status: .busy, changed: 0)], now: at(0)) render.ingest(sessionID: "s1", facts: [.turnBegan(at(1))], now: at(1)) diff --git a/Tests/ClaudeSessionReaderTests.swift b/Tests/ClaudeSessionReaderTests.swift index b12d8aa88..1caeece1f 100644 --- a/Tests/ClaudeSessionReaderTests.swift +++ b/Tests/ClaudeSessionReaderTests.swift @@ -133,6 +133,9 @@ enum ClaudeSessionReaderTests { } suite.expect(!parse([summary], previews: false).contains { if case .turnBegan = $0 { return true }; return false }, "an idle compaction's summary begins no turn") + let slash = #"{"type":"user","message":{"role":"user","content":"/recap\nrecap"},"timestamp":"2026-10-22T10:26:11.114Z"}"# + suite.expect(!parse([slash], previews: true).contains { if case .turnBegan = $0 { return true }; return false }, + "a slash command like /recap begins no turn") suite.expect(said.first == "Also run lint" && said.count == 2, "a prompt typed while Claude works counts; summaries and commands do not: \(said.map { $0.prefix(20) })") suite.expect(said.last?.count == ClaudeTranscriptReader.replyLimit && said.last?.hasSuffix("…") == true, diff --git a/Tests/HarnessReaderContractTests.swift b/Tests/HarnessReaderContractTests.swift index 06736e019..d3783c188 100644 --- a/Tests/HarnessReaderContractTests.swift +++ b/Tests/HarnessReaderContractTests.swift @@ -34,6 +34,9 @@ enum HarnessReaderContractTests { let compacted = line(#"{"type":"user","isCompactSummary":true,"timestamp":1790087000,"sessionId":"s","message":{"content":"This session is being continued from a previous conversation"}}"#) suite.expect(reader.parse(compacted, state: &idle, now: now).isEmpty && !idle.turnOpen, "the summary Claude Code writes when it compacts an idle session begins no work") + let command = line(#"{"type":"user","timestamp":1790087100,"sessionId":"s","message":{"content":"/recap\nrecap"}}"#) + suite.expect(reader.parse(command, state: &idle, now: now).isEmpty && !idle.turnOpen, + "a slash command like /recap begins no work by itself") let began = Date(timeIntervalSince1970: 1_790_088_000) let prompt = line(#"{"type":"user","timestamp":1790088000,"sessionId":"s","cwd":"/Users/me/code/app","message":{"content":"work"}}"#) suite.expect(reader.parse(prompt, state: &state, now: now) == [.turnBegan(began)], From 324796bd06564c9c58fcc568209ce063fc5edf31 Mon Sep 17 00:00:00 2001 From: Dawid Buszta <344369893+MF-DEEV@users.noreply.github.com> Date: Thu, 22 Oct 2026 13:04:37 +0200 Subject: [PATCH 2/2] feat(sessions): set each turn's summary apart In the enlarged page the reply a turn ended on sits under a rule of its own, marked Summary with its time, and the replies written on the way to it are dimmed. Every turn gets its own, so one begun by a slash command, a finished task or a timer never takes the summary from the turn before. A refusal or a cut-off reply is not marked. --- .../Core/NotchClaudeSessionStrings.swift | 2 + .../ClaudeSessions/ClaudeSessionEngine.swift | 14 ++++--- .../ClaudeSessions/ClaudeSessionModels.swift | 6 ++- .../ClaudeTranscriptReader.swift | 7 ++++ .../Notch/NotchClaudeSessionsSupport.swift | 20 +++++++++ .../Notch/NotchClaudeSessionLargeView.swift | 36 ++++++++++++++-- Tests/ClaudeSessionEngineTests.swift | 42 +++++++++++++++++++ Tests/ClaudeSessionReaderTests.swift | 15 ++++++- 8 files changed, 131 insertions(+), 11 deletions(-) diff --git a/Sources/Vorssaint/Core/NotchClaudeSessionStrings.swift b/Sources/Vorssaint/Core/NotchClaudeSessionStrings.swift index 2fafb49d2..442b75fc9 100644 --- a/Sources/Vorssaint/Core/NotchClaudeSessionStrings.swift +++ b/Sources/Vorssaint/Core/NotchClaudeSessionStrings.swift @@ -28,6 +28,7 @@ struct NotchClaudeSessionStrings { let renamePlaceholder: String let conversation: String let noMessages: String + let answer: String let showAllFormat: String let showLess: String let shrink: String @@ -136,6 +137,7 @@ extension NotchClaudeSessionStrings { renamePlaceholder: "Name this session", conversation: "Conversation", noMessages: "Nothing said yet.", + answer: "Summary", showAllFormat: "Show all %d", showLess: "Show less", shrink: "Shrink", diff --git a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift index 661c688a9..40acc71ba 100644 --- a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift +++ b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionEngine.swift @@ -298,9 +298,9 @@ final class ClaudeSessionEngine { case .turnEnded(let date, let completed): end(at: date, completed: completed, in: &entry) case .toolStarted(let tool): start(tool, in: &entry) case .toolFinished(let id, let date, let failed): finish(id, at: date, failed: failed, in: &entry) - case .text(let text, let date): + case .text(let text, let date, let endsTurn): if policy.previews { entry.lastText = text } - say(text, as: .assistant, at: date, in: &entry) + say(text, as: .assistant, at: date, endsTurn: endsTurn, in: &entry) touch(date, in: &entry) case .prompt(let text, let date): say(text, as: .user, at: date, in: &entry) @@ -364,11 +364,15 @@ final class ClaudeSessionEngine { /// The conversation keeps its latest messages. static let chatLength = 150 - private func say(_ text: String, as role: ClaudeChatMessage.Role, at date: Date, in entry: inout Activity) { + private func say(_ text: String, as role: ClaudeChatMessage.Role, at date: Date, endsTurn: Bool = false, + in entry: inout Activity) { guard policy.previews else { return } - if let last = entry.chat.last, last.role == role, last.text == text { return } + if let last = entry.chat.last, last.role == role, last.text == text { + if endsTurn { entry.chat[entry.chat.count - 1].endsTurn = true } + return + } entry.said += 1 - entry.chat.append(ClaudeChatMessage(id: entry.said, role: role, text: text, at: date)) + entry.chat.append(ClaudeChatMessage(id: entry.said, role: role, text: text, at: date, endsTurn: endsTurn)) if entry.chat.count > Self.chatLength { entry.chat.removeFirst(entry.chat.count - Self.chatLength) } } diff --git a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionModels.swift b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionModels.swift index 999a821ea..b1de4eb24 100644 --- a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionModels.swift +++ b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeSessionModels.swift @@ -214,6 +214,9 @@ struct ClaudeChatMessage: Equatable, Identifiable { let role: Role let text: String let at: Date + /// The reply the turn ended on, which answers what was asked; the + /// replies before it tell of the work on the way. + var endsTurn = false } struct ClaudeSessionCounts: Equatable { @@ -259,7 +262,8 @@ enum ClaudeTranscriptFact: Equatable { case turnEnded(Date, completed: Bool) case toolStarted(ClaudeToolUse) case toolFinished(id: String, at: Date, failed: Bool) - case text(String, at: Date) + /// What the model wrote; the text a turn ends on ends that turn. + case text(String, at: Date, endsTurn: Bool = false) /// A prompt the person typed, read only when previews are allowed. case prompt(String, at: Date) case title(String) diff --git a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift index 91c404f42..8ad6e2de0 100644 --- a/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift +++ b/Sources/Vorssaint/Services/ClaudeSessions/ClaudeTranscriptReader.swift @@ -88,6 +88,13 @@ struct ClaudeTranscriptReader { switch message["stop_reason"] as? String { case "end_turn", "stop_sequence", "max_tokens", "refusal": let failed = json["isApiErrorMessage"] as? Bool == true || model.hasPrefix("<") + // The last text of a turn that ended as it should sums it up; + // a refusal or a cut-off reply stops short of that. + if !failed, ["end_turn", "stop_sequence"].contains(message["stop_reason"] as? String), + let index = facts.lastIndex(where: { if case .text = $0 { true } else { false } }), + case .text(let text, let at, _) = facts[index] { + facts[index] = .text(text, at: at, endsTurn: true) + } facts.append(.turnEnded(date, completed: !failed)) default: break } diff --git a/Sources/Vorssaint/Services/Notch/NotchClaudeSessionsSupport.swift b/Sources/Vorssaint/Services/Notch/NotchClaudeSessionsSupport.swift index f82195865..2df34c33a 100644 --- a/Sources/Vorssaint/Services/Notch/NotchClaudeSessionsSupport.swift +++ b/Sources/Vorssaint/Services/Notch/NotchClaudeSessionsSupport.swift @@ -186,6 +186,26 @@ enum NotchClaudeSessionsSupport { return thinkingGlyphs[place < thinkingGlyphs.count ? place : cycle - place] } + /// What the conversation sets apart: each reply a turn ended on, which + /// sums the turn up, and the replies written on the way to one. A turn + /// needs no prompt of its own, as one a slash command, a finished task or + /// a timer began; replies of a turn still running are neither. + static func answers(in chat: [ClaudeChatMessage]) -> (answers: Set, onTheWay: Set) { + var answers = Set(), onTheWay = Set() + var answered = false + for message in chat.reversed() { + if message.role == .user { + answered = false + } else if message.endsTurn { + answers.insert(message.id) + answered = true + } else if answered { + onTheWay.insert(message.id) + } + } + return (answers, onTheWay) + } + /// Text sizes a session's enlarged page offers. static let largeTextSizes: ClosedRange = 12...24 static func largeTextSize(_ size: Double) -> Double { diff --git a/Sources/Vorssaint/UI/Notch/NotchClaudeSessionLargeView.swift b/Sources/Vorssaint/UI/Notch/NotchClaudeSessionLargeView.swift index 238e897d9..1d8fec265 100644 --- a/Sources/Vorssaint/UI/Notch/NotchClaudeSessionLargeView.swift +++ b/Sources/Vorssaint/UI/Notch/NotchClaudeSessionLargeView.swift @@ -209,11 +209,14 @@ struct NotchClaudeSessionLargeView: View { } else if session.chat.isEmpty && !session.phase.isWorking { Text(text.noMessages).font(.system(size: size - 1)).foregroundStyle(.tertiary) } else { + let marks = NotchClaudeSessionsSupport.answers(in: session.chat) ScrollViewReader { proxy in ScrollView { LazyVStack(alignment: .leading, spacing: size) { ForEach(session.chat) { message in - bubble(message).id(message.id) + bubble(message, answer: marks.answers.contains(message.id), + onTheWay: marks.onTheWay.contains(message.id)) + .id(message.id) } if session.phase.isWorking { NotchClaudeThinkingRow(session: session, size: size) @@ -243,7 +246,7 @@ struct NotchClaudeSessionLargeView: View { } } - @ViewBuilder private func bubble(_ message: ClaudeChatMessage) -> some View { + @ViewBuilder private func bubble(_ message: ClaudeChatMessage, answer: Bool, onTheWay: Bool) -> some View { switch message.role { case .user: VStack(alignment: .trailing, spacing: 4) { @@ -259,12 +262,39 @@ struct NotchClaudeSessionLargeView: View { } .padding(.leading, 60) .frame(maxWidth: .infinity, alignment: .trailing) + case .assistant where answer: + // The reply a turn ended on stands apart from the ones written + // on the way, under a rule of its own. + VStack(alignment: .leading, spacing: size * 0.6) { + answerRule(message) + NotchClaudeRichText(text: NotchClaudeSessionFormat.markdown(message.text), size: size, + color: .white.withAlphaComponent(0.96), lineSpacing: size * 0.25) + } + .padding(.top, size * 0.5) + .frame(maxWidth: .infinity, alignment: .leading) case .assistant: - NotchClaudeRichText(text: NotchClaudeSessionFormat.markdown(message.text), size: size, lineSpacing: size * 0.25) + NotchClaudeRichText(text: NotchClaudeSessionFormat.markdown(message.text), size: size, + color: .white.withAlphaComponent(onTheWay ? 0.6 : 0.88), lineSpacing: size * 0.25) .frame(maxWidth: .infinity, alignment: .leading) } } + private func answerRule(_ message: ClaudeChatMessage) -> some View { + HStack(spacing: 8) { + Text(text.answer.uppercased()) + .font(.system(size: max(10, size - 4), weight: .semibold)) + .tracking(0.8) + .foregroundStyle(AgentProvider.claude.tint) + Text(message.at, style: .time) + .font(.system(size: max(10, size - 4))) + .foregroundStyle(.tertiary) + Rectangle() + .fill(AgentProvider.claude.tint.opacity(0.35)) + .frame(height: 1) + } + .accessibilityElement(children: .combine) + } + // MARK: Sidebar private func sidebar(now: Date) -> some View { diff --git a/Tests/ClaudeSessionEngineTests.swift b/Tests/ClaudeSessionEngineTests.swift index b94de624d..9e0290d52 100644 --- a/Tests/ClaudeSessionEngineTests.swift +++ b/Tests/ClaudeSessionEngineTests.swift @@ -401,6 +401,48 @@ enum ClaudeSessionEngineTests { && chat.map(\.role) == [.user, .assistant, .user, .assistant], "the conversation keeps prompts and replies in order, once each: \(chat.map(\.text))") suite.expect(Set(chat.map(\.id)).count == chat.count, "each message has its own id") + suite.expect(chat.allSatisfy { !$0.endsTurn }, "a reply ends no turn unless the transcript says so") + talk.ingest(sessionID: "s1", facts: [.text("Both pass.", at: at(4), endsTurn: true)], now: at(4)) + let ended = only(talk, 5)?.chat ?? [] + suite.expect(ended.count == 4 && ended.last?.endsTurn == true, + "a reply read again as the one its turn ended on is marked, not repeated") + talk.ingest(sessionID: "s1", facts: [.text("Both pass.", at: at(4))], now: at(4)) + suite.expect(only(talk, 5)?.chat.last?.endsTurn == true, "and reading it again plainly keeps the mark") + talk.ingest(sessionID: "s1", facts: [ + .prompt("Ship it", at: at(5)), .text("Shipping.", at: at(6)), .text("Shipped.", at: at(7), endsTurn: true), + ], now: at(7)) + let shipped = only(talk, 8)?.chat ?? [] + suite.expect(shipped.map(\.text).suffix(3) == ["Ship it", "Shipping.", "Shipped."] + && shipped.suffix(3).map(\.endsTurn) == [false, false, true], + "a new reply its turn ended on joins the conversation marked: \(shipped.map(\.text))") + let answers = NotchClaudeSessionsSupport.answers(in: [ + ClaudeChatMessage(id: 1, role: .user, text: "Ship it", at: at(1)), + ClaudeChatMessage(id: 2, role: .assistant, text: "Building.", at: at(2)), + ClaudeChatMessage(id: 3, role: .assistant, text: "Back in 10 minutes.", at: at(3), endsTurn: true), + ClaudeChatMessage(id: 4, role: .assistant, text: "Uploading.", at: at(4)), + ClaudeChatMessage(id: 5, role: .assistant, text: "Sent for review.", at: at(5), endsTurn: true), + ClaudeChatMessage(id: 6, role: .user, text: "And the poster?", at: at(6)), + ClaudeChatMessage(id: 7, role: .assistant, text: "Looking.", at: at(7)), + ClaudeChatMessage(id: 8, role: .user, text: "Thanks", at: at(8)), + ]) + suite.expect(answers.answers == [3, 5] && answers.onTheWay == [2, 4], + "each turn's last reply sums it up, a timer's turn too, and what came before it was on the way") + let command = NotchClaudeSessionsSupport.answers(in: [ + ClaudeChatMessage(id: 1, role: .user, text: "Fix the build", at: at(1)), + ClaudeChatMessage(id: 2, role: .assistant, text: "Fixed; tests pass.", at: at(2), endsTurn: true), + ClaudeChatMessage(id: 3, role: .assistant, text: "Reviewing the diff.", at: at(3)), + ClaudeChatMessage(id: 4, role: .assistant, text: "No issues found.", at: at(4), endsTurn: true), + ]) + suite.expect(command.answers == [2, 4] && command.onTheWay == [3], + "a turn begun with no prompt shown, as by /review, keeps the answer before it as it was") + let running = NotchClaudeSessionsSupport.answers(in: [ + ClaudeChatMessage(id: 1, role: .user, text: "Ship it", at: at(1)), + ClaudeChatMessage(id: 2, role: .assistant, text: "Done.", at: at(2), endsTurn: true), + ClaudeChatMessage(id: 3, role: .assistant, text: "Checking again.", at: at(3)), + ]) + suite.expect(running.answers == [2] && running.onTheWay.isEmpty, + "a reply after the answer, from a turn still running, stays as it is") + suite.expect(NotchClaudeSessionsSupport.answers(in: []).answers.isEmpty, "an empty conversation has no answer") talk.ingest(sessionID: "s1", facts: (0..<(ClaudeSessionEngine.chatLength + 10)).map { .text("m\($0)", at: at(10)) }, now: at(10)) let long = only(talk, 11)?.chat ?? [] diff --git a/Tests/ClaudeSessionReaderTests.swift b/Tests/ClaudeSessionReaderTests.swift index 1caeece1f..6b90d3a23 100644 --- a/Tests/ClaudeSessionReaderTests.swift +++ b/Tests/ClaudeSessionReaderTests.swift @@ -112,8 +112,19 @@ enum ClaudeSessionReaderTests { suite.expect(usage.count == 2 && usage.first?.output == 103, "usage is read for each reply") let open = parse([toolCall, reply, title], previews: true) - suite.expect(open.contains(.text("I ran it. SENTINEL-reply", at: date("2026-10-21T17:50:52.900Z"))) + suite.expect(open.contains(.text("I ran it. SENTINEL-reply", at: date("2026-10-21T17:50:52.900Z"), endsTurn: true)) && open.contains(.title("SENTINEL-title")), "with previews on replies and titles are read") + let onTheWay = #"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"Checking the build."}],"stop_reason":"tool_use"},"timestamp":"2026-10-21T17:50:43.000Z"}"# + let twoTexts = #"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"First."},{"type":"text","text":"Last."}],"stop_reason":"end_turn"},"timestamp":"2026-10-21T17:50:53.000Z"}"# + let failed = #"{"type":"assistant","isApiErrorMessage":true,"message":{"role":"assistant","model":"","content":[{"type":"text","text":"API Error: 529"}],"stop_reason":"stop_sequence"},"timestamp":"2026-10-21T17:50:54.000Z"}"# + let refused = #"{"type":"assistant","message":{"role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"Now the calibration."}],"stop_reason":"refusal"},"timestamp":"2026-10-21T17:50:55.000Z"}"# + let cut = #"{"type":"assistant","message":{"role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"The list goes"}],"stop_reason":"max_tokens"},"timestamp":"2026-10-21T17:50:56.000Z"}"# + let ends = parse([onTheWay, twoTexts, failed, refused, cut], previews: true).compactMap { fact -> String? in + if case .text(let text, _, let endsTurn) = fact { return "\(text)\(endsTurn ? "!" : "")" } + return nil + } + suite.expect(ends == ["Checking the build.", "First.", "Last.!", "API Error: 529", "Now the calibration.", "The list goes"], + "only the last text of a turn that ended as it should ends it, not a refusal or a cut-off: \(ends)") suite.expect(open.contains { if case .toolStarted(let tool) = $0 { return tool.summary == "echo SPIKE_OK > spike.txt" }; return false }, "and tool commands") @@ -127,7 +138,7 @@ enum ClaudeSessionReaderTests { let longReply = #"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"\#(String(repeating: "a", count: 7000))"}]},"timestamp":"2026-10-21T17:55:00.000Z"}"# let said = parse([midTurn, summary, command, longReply], previews: true).compactMap { fact -> String? in switch fact { - case .prompt(let text, _), .text(let text, _): return text + case .prompt(let text, _), .text(let text, _, _): return text default: return nil } }