From 07d4740d0c98cd72e1322ea8a747b67d823792ba Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 2 Nov 2026 16:44:07 +0000 Subject: [PATCH] Show injected Claude input as agent reports and notices, not operator bubbles Claude Code records subagent/peer reports, task notifications, reminders and skill bodies as user-role entries. Classify them server-side (history and live stream) by origin/isMeta/queued_command mode: reports become kind agent_report, task notifications kind notice, harness meta is hidden, and the mid-turn wrapper is unwrapped to the operator's text. The app draws a collapsed Agent report card and a one-line system notice on the agent's side. --- .../conversation/chat_thread_controller.dart | 3 +- .../chat/conversation/chat_thread_view.dart | 5 + .../features/chat/domain/chat_transcript.dart | 52 +++- .../ui/transcript/chat_injected_rows.dart | 244 +++++++++++++++ .../conversation/chat_injected_rows_test.dart | 260 ++++++++++++++++ docs/notes/chat-injected-content.md | 18 ++ internal/claude/claude.go | 4 + internal/claude/injected.go | 279 ++++++++++++++++++ internal/claude/injected_test.go | 205 +++++++++++++ internal/claude/transcript.go | 66 ++++- internal/claude/translate.go | 15 +- internal/protocol/chat.go | 27 +- internal/server/chat_hub.go | 13 +- internal/server/chat_hub_kind_test.go | 29 ++ internal/server/chat_reply_attention.go | 4 + 15 files changed, 1208 insertions(+), 16 deletions(-) create mode 100644 app/lib/features/chat/ui/transcript/chat_injected_rows.dart create mode 100644 app/test/features/chat/conversation/chat_injected_rows_test.dart create mode 100644 docs/notes/chat-injected-content.md create mode 100644 internal/claude/injected.go create mode 100644 internal/claude/injected_test.go create mode 100644 internal/server/chat_hub_kind_test.go diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index 8d33c0e9..1b7dcdff 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -373,7 +373,8 @@ class ChatThreadController extends ChangeNotifier { _running = next['running'] == true; if (!_running) { _liveAssistantMessageId = null; - } else if (const ['text', 'reasoning', 'tool'].contains(body['kind'])) { + } else if (const ['text', 'reasoning', 'tool'].contains(body['kind']) && + chatInjectedKind({'kind': body['message_kind']}) == null) { _liveAssistantMessageId = body['message_id'] as String?; } if (body['kind'] == 'step' && diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 219f4eaa..79b7fc97 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -16,6 +16,7 @@ import '../ui/sessions/chat_adaptive.dart'; import '../ui/sessions/session_actions.dart'; import '../ui/transcript/chat_markdown_view.dart'; import '../ui/transcript/chat_message_view.dart'; +import '../ui/transcript/chat_injected_rows.dart'; import 'chat_background_tasks.dart'; import 'chat_command_actions.dart'; import 'chat_composer_bar.dart'; @@ -1160,6 +1161,10 @@ class _ChatThreadViewState extends State { if (kind == 'activity') { return _ChatActivityLine(row: row, onDismiss: onDismissCompaction); } + if (kind == 'agent_report') { + return ChatAgentReportCard(row: row, textBuilder: chatMarkdownText); + } + if (kind == 'system_notice') return ChatSystemNoticeLine(row: row); if (kind == 'permission' || kind == 'question') { return ChatTranscriptCard( row: row, diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index 039d1cfe..51b04aed 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -70,6 +70,11 @@ JsonMap refreshCachedChatHistory( /// the latest record and that record's lifecycle state. Several updates of one /// compaction are therefore one line, never one line per update. /// +/// A message the server marks with a `kind` is content the harness delivered, +/// not a turn: an `agent_report` becomes its own `agent_report` row and a +/// `notice` its own `system_notice` row, on the agent's side, never folded into +/// an answer and never drawn as the operator's bubble (see [chatInjectedKind]). +/// /// [trailing] rows are conversation cards (permission requests and questions), /// not messages. They keep the server's order after the transcript, the way the /// web renders approvals and forms at the end of the conversation. @@ -124,6 +129,18 @@ List transcriptRows( } for (final message in messages) { + final injected = chatInjectedKind(message); + if (injected != null) { + flush(); + rows.add({ + 'kind': injected == chatKindAgentReport + ? 'agent_report' + : 'system_notice', + 'key': message['id'], + 'message': message, + }); + continue; + } if (message['role'] == 'assistant') { final id = '${message['id'] ?? ''}'.trim(); key ??= id; @@ -149,10 +166,14 @@ List transcriptRows( // Prefer the assistant identity that actually streamed: a queued follow-up // can be later in the transcript while this same reply is still arriving. // Before a first delta, do not reopen an older reply behind a new user turn. + final answers = [ + for (final message in messages) + if (chatInjectedKind(message) == null) message, + ]; final streamingId = liveMessageId ?? - (messages.isNotEmpty && messages.last['role'] == 'assistant' - ? '${messages.last['id'] ?? ''}' + (answers.isNotEmpty && answers.last['role'] == 'assistant' + ? '${answers.last['id'] ?? ''}' : null); var hasStreamingRow = false; if (running && streamingId != null) { @@ -180,6 +201,19 @@ List transcriptRows( return rows; } +/// `ChatMessage.Kind` of a report another agent delivered into the session. +const String chatKindAgentReport = 'agent_report'; + +/// `ChatMessage.Kind` of a one-line harness notice (a background task ended). +const String chatKindNotice = 'notice'; + +/// The injected-content kind the server recorded on [message], or null for an +/// ordinary turn. An unknown future kind keeps the message's own role. +String? chatInjectedKind(JsonMap message) { + final kind = '${message['kind'] ?? ''}'; + return kind == chatKindAgentReport || kind == chatKindNotice ? kind : null; +} + /// Whether [part] is a recorded activity record rather than answer content. /// /// Both spellings occur: `compaction` is the native lifecycle record and @@ -307,6 +341,9 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { final incoming = frame['part']; if (incoming is! Map || incoming['id'] == null) return thread; final messageId = frame['message_id']; + // An agent report or notice the harness delivered: drawn on its own, and + // never evidence that the agent's turn is running. + final injected = chatInjectedKind({'kind': frame['message_kind']}); var index = messages.indexWhere( (m) => m['id'] == messageId && m['role'] == 'assistant', ); @@ -315,6 +352,7 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { 'id': messageId, 'session_id': frame['session_id'], 'role': 'assistant', + 'kind': ?injected, 'parts': [], }); index = messages.length - 1; @@ -331,7 +369,12 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { final delta = frame['delta']; if (delta is! String || delta.isEmpty) return thread; if (partIndex < 0) { - parts.add({'type': kind, 'id': incoming['id'], 'text': delta}); + parts.add({ + 'type': kind, + 'id': incoming['id'], + 'text': delta, + if (incoming['name'] is String) 'name': incoming['name'], + }); } else { parts[partIndex] = { ...parts[partIndex], @@ -354,6 +397,9 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { parts[partIndex] = merged; } messages[index] = {...message, 'parts': parts}; + if (chatInjectedKind(message) != null) { + return {...thread, 'messages': messages}; + } return {...thread, 'messages': messages, 'running': true}; } if (kind == 'step' && frame['step'] is Map) { diff --git a/app/lib/features/chat/ui/transcript/chat_injected_rows.dart b/app/lib/features/chat/ui/transcript/chat_injected_rows.dart new file mode 100644 index 00000000..2283da73 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_injected_rows.dart @@ -0,0 +1,244 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../core/api_gateway.dart'; +import '../sessions/chat_adaptive.dart'; +import 'chat_message_bubble.dart'; +import 'chat_message_view.dart'; + +/// Content the harness delivered into the session, drawn on the agent's side: +/// an `agent_report` row (a delegated agent's report) and a `system_notice` +/// row (a one-line harness notice such as a background task finishing). Neither +/// is the operator's turn, so neither uses the operator's bubble. + +/// The text and sender a row's message recorded. +({String text, String sender}) _content(JsonMap row) { + final message = row['message']; + final parts = message is Map ? message['parts'] : null; + final buffer = StringBuffer(); + var sender = ''; + if (parts is List) { + for (final part in parts) { + if (part is! Map || part['type'] != 'text') continue; + final text = '${part['text'] ?? ''}'; + if (text.trim().isEmpty) continue; + if (buffer.isNotEmpty) buffer.write('\n\n'); + buffer.write(text); + final name = '${part['name'] ?? ''}'.trim(); + if (sender.isEmpty && name.isNotEmpty) sender = name; + } + } + return (text: buffer.toString().trim(), sender: sender); +} + +/// The report's first line, with markdown markers removed, as its summary. +String chatAgentReportSummary(String text) { + for (final raw in text.split('\n')) { + final line = raw + .trim() + .replaceFirst(RegExp(r'^(#{1,6}\s+|[-*+]\s+|>\s*)'), '') + .replaceAll('**', '') + .trim(); + if (line.isNotEmpty) return line; + } + return ''; +} + +/// A delegated agent's report: collapsed to its first line, expanded to the +/// whole report rendered as markdown. +class ChatAgentReportCard extends StatefulWidget { + const ChatAgentReportCard({super.key, required this.row, this.textBuilder}); + + final JsonMap row; + final ChatMessageTextBuilder? textBuilder; + + @override + State createState() => _ChatAgentReportCardState(); +} + +class _ChatAgentReportCardState extends State { + bool _expanded = false; + + void _toggle() => setState(() => _expanded = !_expanded); + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final content = _content(widget.row); + final title = content.sender.isEmpty + ? 'Agent report' + : 'Agent report · ${content.sender}'; + final summary = chatAgentReportSummary(content.text); + final secondary = cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + final header = Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsetsDirectional.only(end: 12, top: 2), + child: Icon( + cupertino ? CupertinoIcons.person_crop_rectangle : Icons.assignment, + color: secondary, + ), + ), + Expanded( + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + title, + style: cupertino + ? CupertinoTheme.of(context).textTheme.textStyle.copyWith( + fontWeight: FontWeight.w600, + ) + : Theme.of(context).textTheme.titleSmall, + ), + if (summary.isNotEmpty) + Text( + summary, + maxLines: _expanded ? null : 1, + overflow: _expanded ? null : TextOverflow.ellipsis, + style: + (cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.bodyMedium) + ?.copyWith(color: secondary), + ), + ], + ), + ), + Icon( + cupertino + ? (_expanded + ? CupertinoIcons.chevron_up + : CupertinoIcons.chevron_down) + : (_expanded ? Icons.expand_less : Icons.expand_more), + color: secondary, + ), + ], + ); + final toggle = Semantics( + container: true, + button: true, + expanded: _expanded, + label: title, + value: summary, + onTap: _toggle, + child: ExcludeSemantics( + child: cupertino + ? CupertinoButton( + padding: const EdgeInsets.all(12), + onPressed: _toggle, + child: DefaultTextStyle.merge( + style: CupertinoTheme.of(context).textTheme.textStyle, + child: header, + ), + ) + : InkWell( + onTap: _toggle, + child: Padding( + padding: const EdgeInsets.all(12), + child: header, + ), + ), + ), + ); + final body = _expanded + ? Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12), + child: DefaultTextStyle.merge( + style: chatMessageTextStyle(context, cupertino), + child: + widget.textBuilder?.call(context, content.text) ?? + SelectableText(content.text), + ), + ) + : null; + final column = Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [toggle, ?body], + ); + return Semantics( + container: true, + explicitChildNodes: true, + label: 'Agent report', + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 6), + child: Align( + alignment: AlignmentDirectional.centerStart, + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 720), + child: cupertino + ? DecoratedBox( + decoration: BoxDecoration( + color: CupertinoColors.secondarySystemGroupedBackground + .resolveFrom(context), + border: Border.all( + color: CupertinoColors.separator.resolveFrom(context), + ), + borderRadius: const BorderRadius.all(Radius.circular(12)), + ), + child: column, + ) + : Card.outlined( + margin: EdgeInsets.zero, + clipBehavior: Clip.antiAlias, + child: column, + ), + ), + ), + ), + ); + } +} + +/// A one-line harness notice in the agent's column, in the secondary style. +class ChatSystemNoticeLine extends StatelessWidget { + const ChatSystemNoticeLine({super.key, required this.row}); + + final JsonMap row; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final text = _content(row).text; + if (text.isEmpty) return const SizedBox.shrink(); + final color = cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + final style = + (cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.bodySmall) + ?.copyWith(color: color); + return Semantics( + container: true, + label: 'System notice', + value: text, + child: ExcludeSemantics( + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 6), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsetsDirectional.only(end: 8), + child: Icon( + cupertino ? CupertinoIcons.info_circle : Icons.info_outline, + size: style?.fontSize == null ? 16 : style!.fontSize! * 1.2, + color: color, + ), + ), + Expanded(child: Text(text, style: style)), + ], + ), + ), + ), + ); + } +} diff --git a/app/test/features/chat/conversation/chat_injected_rows_test.dart b/app/test/features/chat/conversation/chat_injected_rows_test.dart new file mode 100644 index 00000000..c7db1bfc --- /dev/null +++ b/app/test/features/chat/conversation/chat_injected_rows_test.dart @@ -0,0 +1,260 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_injected_rows.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart'; + +import 'chat_thread_fixtures.dart'; + +/// Content the harness delivered (agent reports, task notices) carries the +/// server's message kind; it is drawn on the agent's side and never as the +/// operator's bubble, and it does not disturb the operator's delivery line. + +const _report = + '## Segmented bar report\n\n- first finding\n - nested detail\n\n' + 'The **whole** report.'; + +JsonMap _reportMessage() => { + 'id': 'agent-report-1', + 'role': 'assistant', + 'kind': 'agent_report', + 'parts': [ + { + 'type': 'text', + 'id': 'agent-report-1#text#0', + 'text': _report, + 'name': 'general-purpose', + }, + ], +}; + +JsonMap _noticeMessage() => { + 'id': 'task-notice-toolu_1-completed', + 'role': 'assistant', + 'kind': 'notice', + 'parts': [ + { + 'type': 'text', + 'text': 'Background task finished: Background command "Build" completed', + }, + ], +}; + +class _Gateway extends FakeChatGateway { + final prompts = >[]; + List messages = []; + + _Gateway() { + history = (_) async => chatPage(messages); + } + + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_prompt') { + final reply = Completer(); + prompts.add(reply); + return reply.future; + } + return super.call(type, fields); + } +} + +Future _pump(WidgetTester tester) async { + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + await tester.pump(); +} + +Future _host( + WidgetTester tester, + _Gateway gateway, { + bool cupertino = false, +}) async { + var sequence = 0; + final thread = ChatThreadView( + gateway: gateway, + sessionId: 's', + onBack: () {}, + drafts: MemoryChatDraftStore(), + newOperationId: () => 'op-${++sequence}', + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: thread)) + : MaterialApp(home: Scaffold(body: thread)), + ); + await _pump(tester); +} + +void main() { + test('transcriptRows gives injected kinds their own rows', () { + final rows = transcriptRows([ + chatMessage( + 'u1', + 'user', + parts: [ + {'type': 'text', 'text': 'hi'}, + ], + ), + chatMessage( + 'a1', + 'assistant', + parts: [ + {'type': 'text', 'id': 'a1#text#0', 'text': 'answer'}, + ], + ), + _reportMessage(), + _noticeMessage(), + chatMessage( + 'a2', + 'assistant', + parts: [ + {'type': 'text', 'id': 'a2#text#0', 'text': 'next'}, + ], + ), + ]); + expect(rows.map((row) => row['kind']), [ + 'user', + 'assistant', + 'agent_report', + 'system_notice', + 'assistant', + ]); + expect(rows[1]['messageIds'], ['a1']); + expect(rows[4]['messageIds'], ['a2']); + }); + + test('a live report frame keeps its kind and does not start a turn', () { + final thread = applyChatEvent( + {'sessionId': 's', 'messages': []}, + { + 'session_id': 's', + 'message_id': 'agent-report-1', + 'message_kind': 'agent_report', + 'kind': 'text', + 'part': {'id': 'agent-report-1#text#0', 'name': 'general-purpose'}, + 'delta': _report, + }, + ); + final message = (thread['messages'] as List).single as Map; + expect(message['kind'], 'agent_report'); + expect(message['role'], 'assistant'); + expect((message['parts'] as List).single['name'], 'general-purpose'); + expect(thread['running'], isNot(true)); + }); + + test('the summary is the first line without markdown markers', () { + expect(chatAgentReportSummary(_report), 'Segmented bar report'); + expect(chatAgentReportSummary('\n- **Done**: all\n'), 'Done: all'); + }); + + for (final cupertino in [false, true]) { + testWidgets('a report is a collapsed card on the agent side, a notice is ' + 'one line, a prompt stays a bubble ($cupertino)', (tester) async { + tester.view.physicalSize = const Size(1000, 1600); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final gateway = _Gateway() + ..messages = [ + chatMessage( + 'u1', + 'user', + parts: [ + {'type': 'text', 'text': 'Please delegate this'}, + ], + ), + _reportMessage(), + _noticeMessage(), + ]; + addTearDown(gateway.close); + await _host(tester, gateway, cupertino: cupertino); + + final prompt = find.text('Please delegate this'); + expect(prompt, findsOneWidget); + expect( + find.ancestor(of: prompt, matching: find.byType(ChatUserBubble)), + findsOneWidget, + ); + + final card = find.byType(ChatAgentReportCard); + expect(card, findsOneWidget); + expect(find.text('Agent report · general-purpose'), findsOneWidget); + expect(find.text('Segmented bar report'), findsOneWidget); + // Collapsed: the rest of the report is not laid out yet. + expect(find.textContaining('nested detail'), findsNothing); + expect( + find.ancestor(of: card, matching: find.byType(ChatUserBubble)), + findsNothing, + ); + // The card starts on the agent's (leading) side; the prompt ends on + // the operator's (trailing) side. + final width = tester.getSize(find.byType(ChatThreadView)).width; + expect(tester.getTopLeft(card).dx, lessThan(width * 0.2)); + expect( + tester.getTopRight(find.byType(ChatUserBubble)).dx, + greaterThan(width * 0.8), + ); + + final notice = find.text( + 'Background task finished: Background command "Build" completed', + ); + expect(notice, findsOneWidget); + expect( + find.ancestor(of: notice, matching: find.byType(ChatUserBubble)), + findsNothing, + ); + expect(find.bySemanticsLabel('System notice'), findsOneWidget); + + await tester.tap(find.text('Agent report · general-purpose')); + await _pump(tester); + expect(find.textContaining('nested detail'), findsOneWidget); + expect(find.textContaining('first finding'), findsOneWidget); + // Markdown is rendered, not shown raw. + expect(find.textContaining('## Segmented'), findsNothing); + expect(find.textContaining('**whole**'), findsNothing); + }); + + testWidgets('a live report leaves the prompt\'s delivery line alone ' + '($cupertino)', (tester) async { + final gateway = _Gateway(); + addTearDown(gateway.close); + await _host(tester, gateway, cupertino: cupertino); + + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'Hello', + ); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await _pump(tester); + gateway.prompts.single.complete({}); + await _pump(tester); + expect(find.byKey(const Key('chat-delivery-sent')), findsOneWidget); + + gateway.emitEvent({ + 'session_id': 's', + 'message_id': 'agent-report-1', + 'message_kind': 'agent_report', + 'kind': 'text', + 'part': {'id': 'agent-report-1#text#0', 'name': 'general-purpose'}, + 'delta': _report, + }); + await _pump(tester); + expect(find.byType(ChatAgentReportCard), findsOneWidget); + expect(find.byKey(const Key('chat-delivery-sent')), findsOneWidget); + expect( + find.ancestor( + of: find.text('Hello'), + matching: find.byType(ChatUserBubble), + ), + findsOneWidget, + ); + }); + } +} diff --git a/docs/notes/chat-injected-content.md b/docs/notes/chat-injected-content.md new file mode 100644 index 00000000..91da9bed --- /dev/null +++ b/docs/notes/chat-injected-content.md @@ -0,0 +1,18 @@ +# Claude chat: harness-injected user entries + +Claude Code records more than the operator's prompts as user-role input. The +server (`internal/claude/injected.go`) classifies each one for history +(`transcript.go`) and the live stream (`translate.go`): + +| Entry | Identified by | Shown as | +|---|---|---| +| Subagent / peer report | `origin.kind == "peer"` (user entry or `queued_command` attachment, often `isMeta`), or text opening `` block, or the stream's `system/task_notification` | `kind: "notice"`, one line `Background task finished: `; id `task-notice--` dedupes repeats | +| Mid-turn wrapper | `` + `The user sent a new message while you were working:` | the operator's text only, as a user turn | +| Reminders, skill bodies, image placeholders, interrupt markers, local commands | `isMeta`, or a harness prefix (``, `", "failed", 1), injectedOrigin{}, false) + if failed.class != classNotice || !strings.HasPrefix(failed.text, "Background task failed: ") { + t.Fatalf("failed task notice: %+v", failed) + } +} + +func TestLiveStreamPublishesReportsAndNoticesOnce(t *testing.T) { + _, s := backgroundSession(t) + frames := s.subscribe() + s.handleLine(nil, line(t, map[string]any{"type": "user", "message": map[string]any{"role": "user", "content": "do the thing"}})) + s.handleLine(nil, line(t, map[string]any{ + "type": "user", "isMeta": true, "origin": peerOrigin("a1b2", true, ""), + "message": map[string]any{"role": "user", "content": subagentReport}, + })) + s.handleLine(nil, line(t, map[string]any{ + "type": "system", "subtype": "task_notification", "task_id": "b1", "tool_use_id": "toolu_bg1", + "status": "completed", "summary": "Background command \"Build the app\" completed (exit code 0)", + })) + s.handleLine(nil, line(t, map[string]any{"type": "user", "message": map[string]any{"role": "user", "content": textBlocks(taskNotification)}})) + s.handleLine(nil, line(t, map[string]any{"type": "user", "message": map[string]any{"role": "user", "content": textBlocks(midTurnWrapped)}})) + + var published []protocol.ChatEventFrame + for len(frames) > 0 { + frame := <-frames + if frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventUser { + published = append(published, frame) + } + } + if len(published) != 2 { + t.Fatalf("want one report and one notice, got %+v", published) + } + report, notice := published[0], published[1] + if report.MessageKind != protocol.ChatMessageKindAgentReport || report.Delta != "## Report\n- first item\n nested detail" || + report.MessageID != injectedID(t, subagentReport, peerOrigin("a1b2", true, "")) || report.Part.Name != "general-purpose" { + t.Fatalf("live report: %+v", report) + } + if notice.MessageKind != protocol.ChatMessageKindNotice || notice.MessageID != "task-notice-toolu_bg1-completed" || + notice.Delta != "Background task finished: Background command \"Build the app\" completed (exit code 0)" { + t.Fatalf("live notice: %+v", notice) + } +} diff --git a/internal/claude/transcript.go b/internal/claude/transcript.go index 6cf2bc38..40f2ad60 100644 --- a/internal/claude/transcript.go +++ b/internal/claude/transcript.go @@ -42,7 +42,15 @@ type transcriptEntry struct { Type string `json:"type"` Prompt json.RawMessage `json:"prompt"` CommandMode string `json:"commandMode"` + IsMeta bool `json:"isMeta"` + Origin json.RawMessage `json:"origin"` } `json:"attachment"` + // IsMeta marks a user entry the CLI wrote itself (a skill body, an image + // placeholder, a reminder). Origin says where injected input came from: a + // peer or subagent report, or a task notification. An operator prompt has + // neither. + IsMeta bool `json:"isMeta"` + Origin json.RawMessage `json:"origin"` } func readTranscript(path string) ([]transcriptEntry, error) { @@ -174,6 +182,15 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis parts = append(parts, files...) messages = append(messages, protocol.ChatMessage{ID: id, SessionID: s.id, Role: protocol.ChatRoleUser, CreatedAt: created, Parts: parts}) } + injectedSeen := map[string]bool{} + addInjected := func(entry transcriptEntry, item injected) { + if item.id == "" || injectedSeen[item.id] { + return + } + injectedSeen[item.id] = true + created, _ := transcriptTime(entry.Timestamp) + messages = append(messages, item.message(s.id, created)) + } for _, entry := range entries { if entry.IsSidechain { continue @@ -232,9 +249,17 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis } } case "user": + origin := decodeOrigin(entry.Origin) if len(entry.Message.Content) > 0 && entry.Message.Content[0] == '"' { - // A string user entry is harness feedback (local command output or - // a tool protocol message), never a turn the operator sent. + // A string user entry is harness input (local command output, a + // reminder, a task notification or an agent's report), never a + // turn the operator sent. Reports and notices are shown as such. + var raw string + if json.Unmarshal(entry.Message.Content, &raw) == nil { + if item := classifyInjected(raw, origin, entry.IsMeta); item.class == classAgentReport || item.class == classNotice { + addInjected(entry, item) + } + } continue } blocks := decodeBlocks(entry.Message.Content) @@ -263,14 +288,32 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis if hasResult || (text == "" && len(files) == 0) { continue } + if text != "" { + switch item := classifyInjected(text, origin, entry.IsMeta); item.class { + case classAgentReport, classNotice: + addInjected(entry, item) + continue + case classHidden: + continue + default: + text = item.text + } + } else if entry.IsMeta { + continue + } addUser(entry, text, files) case "attachment": // A message the operator sent while the turn was running is recorded // as a queued_command attachment where the CLI surfaced it to the // model, so it is the user's turn at that point of the transcript. - // Other queued commands (task notifications, agent messages) are - // harness input, not the operator's. - if entry.Attachment.Type != "queued_command" || entry.Attachment.CommandMode != "prompt" { + // Other queued commands (task notifications, and agent reports the + // CLI also queues as "prompt" with an origin) are harness input: + // they are shown as notices and report cards, never as the operator's. + if entry.Attachment.Type != "queued_command" { + continue + } + mode := entry.Attachment.CommandMode + if mode != "prompt" && mode != "task-notification" { continue } var text string @@ -285,6 +328,19 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis if strings.TrimSpace(text) == "" { continue } + origin := decodeOrigin(entry.Attachment.Origin) + if mode == "task-notification" && origin.Kind == "" { + origin.Kind = "task-notification" + } + switch item := classifyInjected(text, origin, entry.Attachment.IsMeta); item.class { + case classAgentReport, classNotice: + addInjected(entry, item) + continue + case classHidden: + continue + default: + text = item.text + } addUser(entry, text, []protocol.ChatPart{}) } } diff --git a/internal/claude/translate.go b/internal/claude/translate.go index c3ae8a1e..bc60102e 100644 --- a/internal/claude/translate.go +++ b/internal/claude/translate.go @@ -34,6 +34,10 @@ type lineEnvelope struct { IsError bool `json:"is_error"` TotalCostUSD float64 `json:"total_cost_usd"` Usage json.RawMessage `json:"usage"` + // Origin and IsMeta mark a user line the harness injected (an agent report, + // a task notification, a reminder); see classifyInjected. + Origin json.RawMessage `json:"origin"` + IsMeta bool `json:"isMeta"` } type contentBlock struct { @@ -470,10 +474,18 @@ func (s *session) handleUser(envelope lineEnvelope) { return } blocks := decodeBlocks(body.Content) + origin := decodeOrigin(envelope.Origin) for _, block := range blocks { - if block.Type == "text" && strings.Contains(block.Text, "") { + if block.Type != "text" { + continue + } + if strings.Contains(block.Text, "") { s.finishBackgroundTasks(block.Text) } + // The operator's own prompts are published when they are sent, so the + // stream echo is never drawn again. Harness input that the operator did + // not send is drawn as a report card or a notice, never as a prompt. + s.emitInjected(classifyInjected(block.Text, origin, envelope.IsMeta)) } for _, block := range blocks { if block.Type != "tool_result" || block.ToolUseID == "" { @@ -768,6 +780,7 @@ func (s *session) finishNotifiedTask(line []byte) { if json.Unmarshal(line, ¬ice) != nil || notice.ToolUseID == "" { return } + s.emitInjected(taskNoticeLine(notice.ToolUseID, notice.Status, strings.Join(strings.Fields(notice.Summary), " "))) s.finishBackgroundTasks(fmt.Sprintf( "%s%s%s", notice.ToolUseID, notice.Status, notice.Summary, diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 43709ce4..a6f10a5d 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -184,6 +184,21 @@ const ( ChatRoleAssistant = "assistant" ) +// ChatMessage.Kind (and ChatEventFrame.MessageKind) values. A message with a +// kind is content the harness delivered into the session, never a turn the +// operator sent: it carries role assistant so an older client that ignores the +// kind still draws it on the agent's side, never as the operator's bubble. +// +// ChatMessageKindAgentReport is a report another agent (a delegated subagent or +// a peer session) delivered; its one text part is the report, and the part's +// Name carries the sender's label when the harness recorded one. +// ChatMessageKindNotice is a one-line harness notice, such as a background task +// finishing; its one text part is the line. +const ( + ChatMessageKindAgentReport = "agent_report" + ChatMessageKindNotice = "notice" +) + // ChatModel is one model the current chat harness can switch to. ID is the // provider's model id, Provider its provider id, Name the harness's display name. type ChatModel struct { @@ -436,9 +451,12 @@ type ChatMessage struct { // Client is the sending device's label on a user turn the server echoed, so a // resumed transcript still says who sent the message. It is empty on an // assistant turn. - Client string `json:"client,omitempty"` - OperationID string `json:"operation_id,omitempty"` - Parts []ChatPart `json:"parts"` + Client string `json:"client,omitempty"` + OperationID string `json:"operation_id,omitempty"` + // Kind is empty for an ordinary user or assistant turn, or one of the + // ChatMessageKind values for content the harness injected. + Kind string `json:"kind,omitempty"` + Parts []ChatPart `json:"parts"` } // ChatDeltaPartID is the stable part id an incremental text or reasoning delta @@ -539,6 +557,9 @@ type ChatEventFrame struct { // transcript can say who sent the message. It is empty on assistant frames. Client string `json:"client,omitempty"` OperationID string `json:"operation_id,omitempty"` + // MessageKind is the ChatMessage.Kind of the message a text frame starts, + // so a live agent report or notice is never drawn as an answer or a prompt. + MessageKind string `json:"message_kind,omitempty"` } // ChatCompaction is session activity, never a transcript message or tool. diff --git a/internal/server/chat_hub.go b/internal/server/chat_hub.go index 662fb4e7..6dc964f4 100644 --- a/internal/server/chat_hub.go +++ b/internal/server/chat_hub.go @@ -79,8 +79,11 @@ type chatMessage struct { role string client string operationID string - order []string - parts map[string]*chatPart + // kind is the ChatMessage.Kind of harness-injected content (an agent + // report or a notice); empty for an ordinary turn. + kind string + order []string + parts map[string]*chatPart } type chatTurn struct { @@ -232,6 +235,9 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) { if message.operationID == "" && frame.Kind == protocol.ChatEventUser { message.operationID = frame.OperationID } + if message.kind == "" && frame.MessageKind != "" && frame.Kind != protocol.ChatEventUser { + message.kind = frame.MessageKind + } switch frame.Kind { case protocol.ChatEventUser: if frame.Part == nil || frame.Part.ID == "" || frame.Part.Text == "" || turn.overflow { @@ -260,7 +266,7 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) { } part, ok := message.parts[id] if !ok { - part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id}} + part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id, Name: frame.Part.Name}} message.order = append(message.order, id) message.parts[id] = part } @@ -553,6 +559,7 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage { } out.Client = message.client out.OperationID = message.operationID + out.Kind = message.kind for _, partID := range message.order { if part, ok := message.parts[partID]; ok { piece := part.part diff --git a/internal/server/chat_hub_kind_test.go b/internal/server/chat_hub_kind_test.go new file mode 100644 index 00000000..ed803678 --- /dev/null +++ b/internal/server/chat_hub_kind_test.go @@ -0,0 +1,29 @@ +package server + +import ( + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// A live agent report keeps its kind and sender across a resume snapshot, and +// is never mistaken for the agent's final reply. +func TestHubKeepsInjectedMessageKind(t *testing.T) { + h := newChatHub() + h.apply(protocol.ChatEventFrame{SessionID: "s", MessageID: "msg_1", Kind: protocol.ChatEventText, Delta: "The answer", Part: &protocol.ChatPart{ID: "msg_1#text#0"}}) + h.apply(protocol.ChatEventFrame{ + SessionID: "s", MessageID: "agent-report-x", MessageKind: protocol.ChatMessageKindAgentReport, Kind: protocol.ChatEventText, + Delta: "## Report", Part: &protocol.ChatPart{ID: "agent-report-x#text#0", Name: "general-purpose"}, + }) + messages := h.snapshot("s") + if len(messages) != 2 || messages[0].Kind != "" || messages[1].Kind != protocol.ChatMessageKindAgentReport || + messages[1].Role != protocol.ChatRoleAssistant || messages[1].Parts[0].Name != "general-purpose" || messages[1].Parts[0].Text != "## Report" { + t.Fatalf("snapshot: %+v", messages) + } + h.mu.Lock() + reply := h.finalReplyTextLocked("s") + h.mu.Unlock() + if reply != "The answer" { + t.Fatalf("final reply must skip the report, got %q", reply) + } +} diff --git a/internal/server/chat_reply_attention.go b/internal/server/chat_reply_attention.go index 75c239c3..98abaa6b 100644 --- a/internal/server/chat_reply_attention.go +++ b/internal/server/chat_reply_attention.go @@ -62,6 +62,10 @@ func (h *chatHub) finalReplyTextLocked(sessionID string) string { if message.role == protocol.ChatRoleUser { return "" } + if message.kind != "" { + // An agent report or notice the harness delivered is not the reply. + continue + } var text strings.Builder for _, id := range message.order { part := message.parts[id]