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.
This commit is contained in:
@@ -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' &&
|
||||
|
||||
@@ -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<ChatThreadView> {
|
||||
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,
|
||||
|
||||
@@ -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<JsonMap> 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<JsonMap> 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<JsonMap> 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': <JsonMap>[],
|
||||
});
|
||||
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) {
|
||||
|
||||
@@ -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<ChatAgentReportCard> createState() => _ChatAgentReportCardState();
|
||||
}
|
||||
|
||||
class _ChatAgentReportCardState extends State<ChatAgentReportCard> {
|
||||
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)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 = <Completer<JsonMap>>[];
|
||||
List<JsonMap> messages = [];
|
||||
|
||||
_Gateway() {
|
||||
history = (_) async => chatPage(messages);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
if (type == 'chat_prompt') {
|
||||
final reply = Completer<JsonMap>();
|
||||
prompts.add(reply);
|
||||
return reply.future;
|
||||
}
|
||||
return super.call(type, fields);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pump(WidgetTester tester) async {
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
Future<void> _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': <JsonMap>[]},
|
||||
{
|
||||
'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,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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 `<agent-message` | `ChatMessage{role: assistant, kind: "agent_report"}`; text is `origin.handbackReportText`, else the framed body with the 2-space indent removed; part `name` is the sender |
|
||||
| Background task notification | `origin.kind == "task-notification"`, `queued_command` with `commandMode: "task-notification"`, a `<task-notification>` block, or the stream's `system/task_notification` | `kind: "notice"`, one line `Background task finished: <summary>`; id `task-notice-<tool-use-id>-<status>` dedupes repeats |
|
||||
| Mid-turn wrapper | `<system-reminder>` + `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 (`<system-reminder>`, `<local-command-`, `[Request interrupted by user`, `Base directory for this skill:` …) | hidden |
|
||||
|
||||
Report and notice ids are derived from content, so the live frame
|
||||
(`chat_event` text with `message_kind`) and the history entry are one message.
|
||||
Both carry role `assistant`, so a client that ignores `kind` still draws them
|
||||
on the agent's side. The app renders them as a collapsed "Agent report" card
|
||||
and a one-line system notice (`ui/transcript/chat_injected_rows.dart`).
|
||||
@@ -276,6 +276,10 @@ type session struct {
|
||||
streamOrdinals map[string]int
|
||||
streamClaimed map[int]bool
|
||||
toolMessages map[string]string
|
||||
|
||||
// injectedSeen holds the identities of agent reports and notices already
|
||||
// published live, so a delivery the stream repeats is shown once.
|
||||
injectedSeen map[string]bool
|
||||
}
|
||||
|
||||
type promptRecord struct {
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// Claude Code delivers more than the operator's prompts as user-role input:
|
||||
// subagent and peer-session reports, background-task notifications, system
|
||||
// reminders, skill bodies and mid-turn message wrappers. This file decides
|
||||
// which of those is the operator's own turn and which is harness content, so
|
||||
// neither history nor the live stream draws harness text as the operator's.
|
||||
|
||||
// injectedOrigin is the CLI's record of where a user-role entry came from. It is
|
||||
// set on peer reports ({"kind":"peer", ...}) and task notifications
|
||||
// ({"kind":"task-notification"}); an operator prompt carries none.
|
||||
type injectedOrigin struct {
|
||||
Kind string `json:"kind"`
|
||||
From string `json:"from"`
|
||||
Name string `json:"name"`
|
||||
Handback bool `json:"handback"`
|
||||
HandbackReportText string `json:"handbackReportText"`
|
||||
}
|
||||
|
||||
func decodeOrigin(raw json.RawMessage) injectedOrigin {
|
||||
var origin injectedOrigin
|
||||
if len(raw) > 0 && raw[0] == '{' {
|
||||
_ = json.Unmarshal(raw, &origin)
|
||||
}
|
||||
return origin
|
||||
}
|
||||
|
||||
// injectedClass is how one user-role text is shown.
|
||||
type injectedClass int
|
||||
|
||||
const (
|
||||
// classOperator is the operator's own turn; text is what they wrote (any
|
||||
// harness wrapper around it removed).
|
||||
classOperator injectedClass = iota
|
||||
classAgentReport
|
||||
classNotice
|
||||
// classHidden is harness-internal content that is not shown at all.
|
||||
classHidden
|
||||
)
|
||||
|
||||
type injected struct {
|
||||
class injectedClass
|
||||
// id is a stable identity derived from the content, so the live frame and
|
||||
// the history entry for the same delivery are one message. Empty for an
|
||||
// operator turn, which keeps its own identity.
|
||||
id string
|
||||
text string
|
||||
sender string
|
||||
}
|
||||
|
||||
var (
|
||||
agentMessageOpen = regexp.MustCompile(`<agent-message(?:\s+from="([^"]*)")?[^>]*>`)
|
||||
taskIDPattern = regexp.MustCompile(`<task-id>\s*([^<\s]+)\s*</task-id>`)
|
||||
taskStatusPattern = regexp.MustCompile(`<status>\s*([^<]*?)\s*</status>`)
|
||||
midTurnWrapper = "The user sent a new message while you were working:"
|
||||
midTurnWrapperTail = "\n\nThis is how Claude Code surfaces messages"
|
||||
peerSessionPreamble = "Another Claude session sent a message:"
|
||||
handbackMarker = "The report follows:"
|
||||
)
|
||||
|
||||
// harnessPrefixes open user-role text that Claude Code wrote, never the
|
||||
// operator: reminders, slash-command plumbing, interrupt markers and the
|
||||
// placeholder lines it records beside pasted images and loaded skills.
|
||||
var harnessPrefixes = []string{
|
||||
"<system-reminder>",
|
||||
"<local-command-",
|
||||
"<command-name>",
|
||||
"<command-message>",
|
||||
"<user-prompt-submit-hook>",
|
||||
"Caveat: The messages below were generated by the user while running local commands.",
|
||||
"[Request interrupted by user",
|
||||
"Base directory for this skill:",
|
||||
}
|
||||
|
||||
// classifyInjected decides how a user-role text is shown. origin and meta are
|
||||
// the CLI's own markers (origin object, isMeta flag); the text shape is the
|
||||
// fallback for a line that does not carry them.
|
||||
func classifyInjected(text string, origin injectedOrigin, meta bool) injected {
|
||||
trimmed := strings.TrimSpace(text)
|
||||
if origin.Kind == "peer" || startsAgentMessage(trimmed) {
|
||||
return agentReport(trimmed, origin)
|
||||
}
|
||||
if origin.Kind == "task-notification" || strings.Contains(trimmed, "<task-notification>") && startsHarness(trimmed) {
|
||||
if notice, ok := taskNotice(trimmed); ok {
|
||||
return notice
|
||||
}
|
||||
return injected{class: classHidden}
|
||||
}
|
||||
if operator, ok := unwrapMidTurn(trimmed); ok {
|
||||
return injected{class: classOperator, text: operator}
|
||||
}
|
||||
if meta || startsHarness(trimmed) {
|
||||
return injected{class: classHidden}
|
||||
}
|
||||
return injected{class: classOperator, text: text}
|
||||
}
|
||||
|
||||
func startsAgentMessage(text string) bool {
|
||||
text = strings.TrimSpace(strings.TrimPrefix(text, peerSessionPreamble))
|
||||
return strings.HasPrefix(text, "<agent-message")
|
||||
}
|
||||
|
||||
func startsHarness(text string) bool {
|
||||
if strings.HasPrefix(text, "<task-notification>") {
|
||||
return true
|
||||
}
|
||||
for _, prefix := range harnessPrefixes {
|
||||
if strings.HasPrefix(text, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// unwrapMidTurn returns the operator's text from the reminder Claude Code wraps
|
||||
// around a message the operator sent while a turn was running.
|
||||
func unwrapMidTurn(text string) (string, bool) {
|
||||
index := strings.Index(text, midTurnWrapper)
|
||||
if index < 0 || !strings.HasPrefix(text, "<system-reminder>") {
|
||||
return "", false
|
||||
}
|
||||
body := text[index+len(midTurnWrapper):]
|
||||
if tail := strings.Index(body, midTurnWrapperTail); tail >= 0 {
|
||||
body = body[:tail]
|
||||
} else {
|
||||
body = strings.TrimSuffix(strings.TrimSpace(body), "</system-reminder>")
|
||||
}
|
||||
body = strings.TrimSpace(body)
|
||||
return body, body != ""
|
||||
}
|
||||
|
||||
// agentReport extracts the report an agent delivered. The CLI frames it as
|
||||
// `<agent-message from="id">`, a hand-back preamble, then the report with every
|
||||
// line indented; the origin's handbackReportText is the same report unframed.
|
||||
func agentReport(text string, origin injectedOrigin) injected {
|
||||
sender := origin.From
|
||||
body := text
|
||||
if match := agentMessageOpen.FindStringSubmatchIndex(text); match != nil {
|
||||
if sender == "" && match[2] >= 0 {
|
||||
sender = text[match[2]:match[3]]
|
||||
}
|
||||
body = text[match[1]:]
|
||||
}
|
||||
if end := strings.LastIndex(body, "</agent-message>"); end >= 0 {
|
||||
body = body[:end]
|
||||
}
|
||||
framed := false
|
||||
if marker := strings.Index(body, handbackMarker); marker >= 0 && strings.Contains(body[:marker], "[Subagent hand-back]") {
|
||||
body = body[marker+len(handbackMarker):]
|
||||
framed = true
|
||||
}
|
||||
body = strings.Trim(body, "\n")
|
||||
if framed {
|
||||
body = dedentReport(body)
|
||||
}
|
||||
body = strings.TrimSpace(body)
|
||||
display := body
|
||||
if report := strings.TrimSpace(origin.HandbackReportText); report != "" {
|
||||
display = report
|
||||
}
|
||||
if display == "" {
|
||||
return injected{class: classHidden}
|
||||
}
|
||||
label := strings.TrimSpace(origin.Name)
|
||||
if label == "" {
|
||||
label = sender
|
||||
}
|
||||
return injected{class: classAgentReport, id: "agent-report-" + contentKey(sender, body), text: display, sender: label}
|
||||
}
|
||||
|
||||
// dedentReport removes the two-space indent the harness adds to every line of a
|
||||
// handed-back report.
|
||||
func dedentReport(body string) string {
|
||||
lines := strings.Split(body, "\n")
|
||||
for i, line := range lines {
|
||||
lines[i] = strings.TrimPrefix(line, " ")
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// taskNotice turns a `<task-notification>` block into its one-line notice.
|
||||
func taskNotice(text string) (injected, bool) {
|
||||
match := taskNotificationPattern.FindStringSubmatch(text)
|
||||
if match == nil {
|
||||
return injected{}, false
|
||||
}
|
||||
block := match[1]
|
||||
key := ""
|
||||
if id := taskToolUsePattern.FindStringSubmatch(block); id != nil {
|
||||
key = id[1]
|
||||
} else if id := taskIDPattern.FindStringSubmatch(block); id != nil {
|
||||
key = id[1]
|
||||
}
|
||||
status := ""
|
||||
if found := taskStatusPattern.FindStringSubmatch(block); found != nil {
|
||||
status = found[1]
|
||||
}
|
||||
summary := ""
|
||||
if found := taskSummaryPattern.FindStringSubmatch(block); found != nil {
|
||||
summary = strings.Join(strings.Fields(found[1]), " ")
|
||||
}
|
||||
return taskNoticeLine(key, status, summary), true
|
||||
}
|
||||
|
||||
func taskNoticeLine(key, status, summary string) injected {
|
||||
verb := "finished"
|
||||
switch strings.ToLower(status) {
|
||||
case "failed", "error":
|
||||
verb = "failed"
|
||||
case "killed", "stopped", "cancelled", "canceled":
|
||||
verb = "stopped"
|
||||
}
|
||||
line := "Background task " + verb
|
||||
if summary != "" {
|
||||
line += ": " + summary
|
||||
}
|
||||
if key == "" {
|
||||
key = contentKey(status, summary)
|
||||
}
|
||||
return injected{class: classNotice, id: "task-notice-" + key + "-" + strings.ToLower(status), text: line}
|
||||
}
|
||||
|
||||
func contentKey(parts ...string) string {
|
||||
sum := sha256.Sum256([]byte(strings.Join(parts, "\x00")))
|
||||
return hex.EncodeToString(sum[:8])
|
||||
}
|
||||
|
||||
// message is the transcript message an agent report or notice becomes. It has
|
||||
// the assistant role so a client that ignores Kind still draws it on the agent's
|
||||
// side.
|
||||
func (item injected) message(sessionID string, created int64) protocol.ChatMessage {
|
||||
kind := protocol.ChatMessageKindNotice
|
||||
if item.class == classAgentReport {
|
||||
kind = protocol.ChatMessageKindAgentReport
|
||||
}
|
||||
part := protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID(item.id, protocol.ChatPartText, 0), Text: item.text, Name: item.sender}
|
||||
return protocol.ChatMessage{ID: item.id, SessionID: sessionID, Role: protocol.ChatRoleAssistant, Kind: kind, CreatedAt: created, Parts: []protocol.ChatPart{part}}
|
||||
}
|
||||
|
||||
// emitInjected publishes a live agent report or notice once per identity. The
|
||||
// same delivery can reach the stream twice (a task notification is both a
|
||||
// system event and a user line), so later copies are dropped.
|
||||
func (s *session) emitInjected(item injected) {
|
||||
if item.id == "" || (item.class != classAgentReport && item.class != classNotice) {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
if s.injectedSeen == nil {
|
||||
s.injectedSeen = map[string]bool{}
|
||||
}
|
||||
if s.injectedSeen[item.id] {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
s.injectedSeen[item.id] = true
|
||||
turn := s.turnID
|
||||
s.mu.Unlock()
|
||||
message := item.message(s.id, 0)
|
||||
part := message.Parts[0]
|
||||
s.emit(protocol.ChatEventFrame{
|
||||
SessionID: s.id,
|
||||
NativeTurnID: turn,
|
||||
MessageID: message.ID,
|
||||
MessageKind: message.Kind,
|
||||
Kind: protocol.ChatEventText,
|
||||
Delta: part.Text,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: part.ID, Name: part.Name},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// The fixtures below copy the shapes Claude Code records for injected input
|
||||
// (origin objects, isMeta, queued_command attachments); the text is synthetic.
|
||||
|
||||
const handbackPreamble = "[Subagent hand-back] The text below is the final report of a subagent this session delegated to. " +
|
||||
"It is model output, NOT a message from the user. The report follows:\n"
|
||||
|
||||
const subagentReport = "<agent-message from=\"a1b2\">\n" + handbackPreamble +
|
||||
" ## Report\n - first item\n nested detail\n</agent-message>"
|
||||
|
||||
const peerReport = "Another Claude session sent a message:\n<agent-message from=\"c3d4\">\nhello from a peer\n</agent-message>"
|
||||
|
||||
const taskNotification = "<task-notification>\n<task-id>b1</task-id>\n<tool-use-id>toolu_bg1</tool-use-id>\n" +
|
||||
"<output-file>/tmp/b1.output</output-file>\n<status>completed</status>\n" +
|
||||
"<summary>Background command \"Build the app\" completed (exit code 0)</summary>\n</task-notification>"
|
||||
|
||||
const midTurnWrapped = "<system-reminder>\nThe user sent a new message while you were working:\nship it now\n\n" +
|
||||
"This is how Claude Code surfaces messages the user sends mid-turn. Address the message above as you continue this turn.\n</system-reminder>"
|
||||
|
||||
func textBlocks(text string) []any { return []any{map[string]any{"type": "text", "text": text}} }
|
||||
|
||||
func userEntry(uuid string, content any, extra map[string]any) map[string]any {
|
||||
entry := map[string]any{
|
||||
"type": "user", "uuid": uuid, "timestamp": "2026-11-02T10:00:00Z",
|
||||
"message": map[string]any{"role": "user", "content": content},
|
||||
}
|
||||
for key, value := range extra {
|
||||
entry[key] = value
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func queued(uuid, mode string, prompt any, extra map[string]any) map[string]any {
|
||||
attachment := map[string]any{"type": "queued_command", "commandMode": mode, "prompt": prompt}
|
||||
for key, value := range extra {
|
||||
attachment[key] = value
|
||||
}
|
||||
return map[string]any{"type": "attachment", "uuid": uuid, "timestamp": "2026-11-02T10:00:01Z", "attachment": attachment}
|
||||
}
|
||||
|
||||
func peerOrigin(from string, handback bool, report string) map[string]any {
|
||||
return map[string]any{"kind": "peer", "from": from, "senderTaskId": from, "name": "general-purpose", "handback": handback, "handbackReportText": report}
|
||||
}
|
||||
|
||||
func writeTranscript(t *testing.T, id string, entries []map[string]any) {
|
||||
t.Helper()
|
||||
config := t.TempDir()
|
||||
t.Setenv("CLAUDE_CONFIG_DIR", config)
|
||||
dir := filepath.Join(config, "projects", "-tmp-work")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var lines []string
|
||||
for _, entry := range entries {
|
||||
raw, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lines = append(lines, string(raw))
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, id+".jsonl"), []byte(strings.Join(lines, "\n")+"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryClassifiesInjectedUserEntries(t *testing.T) {
|
||||
const id = "ses_injected"
|
||||
writeTranscript(t, id, []map[string]any{
|
||||
userEntry("u-prompt", textBlocks("do the thing"), map[string]any{"promptSource": "sdk", "turnOrigin": "sdk"}),
|
||||
userEntry("u-image-note", textBlocks("[Image: source: /tmp/shot.png]"), map[string]any{"isMeta": true, "turnCompanion": true}),
|
||||
userEntry("u-skill", textBlocks("Base directory for this skill: /skills/x\n\n# Skill body"), map[string]any{"isMeta": true, "sourceToolUseID": "toolu_s"}),
|
||||
userEntry("u-reminder", "<system-reminder>\n<tools-changed/>\n</system-reminder>", map[string]any{"isMeta": true}),
|
||||
{"type": "assistant", "uuid": "a-1", "message": map[string]any{"id": "msg_1", "role": "assistant", "content": textBlocks("Working on it")}},
|
||||
queued("q-operator", "prompt", textBlocks("also check the docs"), nil),
|
||||
queued("q-report", "prompt", subagentReport, map[string]any{"isMeta": true, "origin": peerOrigin("a1b2", true, "## Report\n- first item\n nested detail")}),
|
||||
userEntry("u-peer", peerReport, map[string]any{"isMeta": true, "origin": peerOrigin("c3d4", false, ""), "promptSource": "system", "turnOrigin": "peer"}),
|
||||
userEntry("u-task", taskNotification, map[string]any{"origin": map[string]any{"kind": "task-notification", "producer": "session-task"}, "promptSource": "system"}),
|
||||
queued("q-task", "task-notification", taskNotification, map[string]any{"origin": map[string]any{"kind": "task-notification"}}),
|
||||
userEntry("u-wrapped", textBlocks(midTurnWrapped), nil),
|
||||
userEntry("u-interrupt", textBlocks("[Request interrupted by user for tool use]"), nil),
|
||||
})
|
||||
chat := New(context.Background())
|
||||
s := chat.sessionFor(id, t.TempDir())
|
||||
s.prompts = []promptRecord{{id: "op-steer", text: "also check the docs"}, {id: "op-ship", text: "ship it now"}}
|
||||
|
||||
page, err := s.transcriptMessages(50, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got []string
|
||||
for _, message := range page.Messages {
|
||||
text := ""
|
||||
if len(message.Parts) > 0 {
|
||||
text = message.Parts[0].Text
|
||||
}
|
||||
got = append(got, message.Role+"/"+message.Kind+"/"+message.ID+"="+text)
|
||||
}
|
||||
report := injectedID(t, subagentReport, peerOrigin("a1b2", true, ""))
|
||||
peer := injectedID(t, peerReport, peerOrigin("c3d4", false, ""))
|
||||
want := []string{
|
||||
"user//u-prompt=do the thing",
|
||||
"assistant//msg_1=Working on it",
|
||||
"user//op-steer=also check the docs",
|
||||
"assistant/agent_report/" + report + "=## Report\n- first item\n nested detail",
|
||||
"assistant/agent_report/" + peer + "=hello from a peer",
|
||||
"assistant/notice/task-notice-toolu_bg1-completed=Background task finished: Background command \"Build the app\" completed (exit code 0)",
|
||||
"user//op-ship=ship it now",
|
||||
}
|
||||
if strings.Join(got, "\n|") != strings.Join(want, "\n|") {
|
||||
t.Fatalf("history classification:\n got %q\nwant %q", got, want)
|
||||
}
|
||||
if name := page.Messages[3].Parts[0].Name; name != "general-purpose" {
|
||||
t.Fatalf("agent report sender: %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
func injectedID(t *testing.T, text string, origin map[string]any) string {
|
||||
t.Helper()
|
||||
raw, _ := json.Marshal(origin)
|
||||
item := classifyInjected(text, decodeOrigin(raw), false)
|
||||
if item.id == "" {
|
||||
t.Fatalf("no identity for %q", text)
|
||||
}
|
||||
return item.id
|
||||
}
|
||||
|
||||
func TestAgentReportWithoutHandbackTextIsDedented(t *testing.T) {
|
||||
item := classifyInjected(subagentReport, injectedOrigin{}, false)
|
||||
if item.class != classAgentReport || item.sender != "a1b2" || item.text != "## Report\n- first item\n nested detail" {
|
||||
t.Fatalf("report: %+v", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyKeepsOperatorTextAndHidesHarness(t *testing.T) {
|
||||
cases := []struct {
|
||||
text string
|
||||
meta bool
|
||||
class injectedClass
|
||||
shown string
|
||||
}{
|
||||
{"fix the <system-reminder> parser", false, classOperator, "fix the <system-reminder> parser"},
|
||||
{midTurnWrapped, false, classOperator, "ship it now"},
|
||||
{"<system-reminder>\nnothing for the operator\n</system-reminder>", false, classHidden, ""},
|
||||
{"<local-command-stdout>ok</local-command-stdout>", false, classHidden, ""},
|
||||
{"anything the CLI marked", true, classHidden, ""},
|
||||
}
|
||||
for _, c := range cases {
|
||||
item := classifyInjected(c.text, injectedOrigin{}, c.meta)
|
||||
if item.class != c.class || (c.class == classOperator && item.text != c.shown) {
|
||||
t.Fatalf("classify %q: %+v", c.text, item)
|
||||
}
|
||||
}
|
||||
failed := classifyInjected(strings.Replace(taskNotification, "completed</status>", "failed</status>", 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)
|
||||
}
|
||||
}
|
||||
@@ -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{})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, "<task-notification>") {
|
||||
if block.Type != "text" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(block.Text, "<task-notification>") {
|
||||
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(
|
||||
"<task-notification><tool-use-id>%s</tool-use-id><status>%s</status><summary>%s</summary></task-notification>",
|
||||
notice.ToolUseID, notice.Status, notice.Summary,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user