Merge branch 'worktree-agent-a86b9c8a03da895c2'

This commit is contained in:
2026-11-22 18:24:30 +00:00
9 changed files with 698 additions and 199 deletions
@@ -0,0 +1,321 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_permission_card_model.dart';
import 'package:pipelines_app/features/chat/conversation/chat_permission_group.dart';
import 'package:pipelines_app/features/chat/conversation/chat_question_group.dart';
import 'package:pipelines_app/features/chat/conversation/chat_question_card_model.dart';
import 'package:pipelines_app/features/chat/conversation/chat_row_keys.dart';
import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'package:pipelines_app/features/chat/domain/chat_turn_diffs.dart';
/// The thread view's inputs to its row derivation.
class _State {
List<JsonMap> messages = [];
List<JsonMap> cards = [];
List<JsonMap> shown = [];
bool hasMore = false;
bool running = false;
String? live;
final diffs = ChatTurnDiffs();
}
typedef _Rows =
List<JsonMap> Function(
List<JsonMap> messages, {
List<JsonMap> trailing,
bool running,
bool awaitingResponse,
String? liveMessageId,
});
/// The chain the thread view runs, with [rows] as its first stage.
List<JsonMap> _derive(_Rows rows, _State state) => attachChatTurnDiffs(
groupSettledQuestions(
groupSettledPermissions(
withChatShownItems(
rows(
state.messages,
trailing: state.cards,
running: state.running,
awaitingResponse: false,
liveMessageId: state.live,
),
state.shown,
hasMore: state.hasMore,
),
),
),
state.diffs,
);
/// Comparable form of a row list: permission cards are plain objects.
Object? _plain(Object? value) => switch (value) {
ChatPermissionCardData card => {
'requestId': card.requestId,
'action': card.action,
'state': '${card.state}',
'decision': card.decision,
'headline': card.headline,
'statusLine': card.statusLine,
},
Map map => {for (final e in map.entries) e.key: _plain(e.value)},
List list => [for (final item in list) _plain(item)],
_ => value,
};
const _base = 1700000000000;
List<JsonMap> _history(int from, int to) => [
for (var i = from; i < to; i++) ...[
{
'id': 'u$i',
'role': 'user',
'created_at': _base + i * 1000,
'parts': [
{'type': 'text', 'text': 'question $i'},
],
},
{
'id': 'a$i',
'role': 'assistant',
'model': 'm',
'created_at': _base + i * 1000 + 100,
'parts': [
{'type': 'reasoning', 'id': 'r$i', 'text': 'thinking $i'},
{
'type': 'tool',
'id': 'call-$i',
'name': i % 5 == 0 ? 'mcp__pipelines__show_file' : 'Bash',
'state': {'status': 'completed'},
},
if (i % 7 == 0)
{'type': 'compaction', 'id': 'c$i', 'state': 'completed'},
],
},
// A second message in the same turn.
if (i.isOdd)
{
'id': 'b$i',
'role': 'assistant',
'parts': [
{'type': 'text', 'id': 't$i', 'text': 'answer $i'},
],
},
if (i % 9 == 0) ...[
for (final n in [1, 2])
{
'id': 'n$i-$n',
'role': 'assistant',
'kind': 'notice',
'created_at': _base + i * 1000 + 500 + n,
'parts': [
{'type': 'text', 'text': 'Background task finished: job $n'},
],
},
],
if (i % 11 == 0)
{
'id': 'r$i',
'role': 'assistant',
'kind': 'agent_report',
'parts': [
{'type': 'text', 'text': 'report $i'},
],
},
],
];
JsonMap _thread(_State state) => {
'sessionId': 's1',
'messages': state.messages,
'running': state.running,
};
void _apply(_State state, JsonMap frame) {
final next = applyChatEvent(_thread(state), {'session_id': 's1', ...frame});
state.messages = chatMessageList(next['messages']);
state.running = next['running'] == true;
}
void _delta(_State state, String messageId, String text) {
_apply(state, {
'kind': 'text',
'message_id': messageId,
'part': {'id': 'live-$messageId'},
'delta': text,
});
state.live = messageId;
}
void main() {
late _State state;
late ChatTranscriptRows memo;
late ChatRowKeys memoKeys;
late ChatRowKeys fullKeys;
late List<JsonMap> previous;
/// Derives through the memo and from scratch, and checks they agree on rows
/// and on the keys a long-lived [ChatRowKeys] assigns them.
List<JsonMap> check() {
final incremental = _derive(memo.rows, state);
final full = _derive(transcriptRows, state);
expect(_plain(incremental), _plain(full));
expect(memoKeys.forRows(incremental), fullKeys.forRows(full));
return previous = incremental;
}
/// Rows of [now] that are the very objects [before] held for the same key.
int reused(List<JsonMap> before, List<JsonMap> now) {
final held = {for (final row in before) '${row['key']}': row};
return now.where((row) => identical(held['${row['key']}'], row)).length;
}
setUp(() {
state = _State()..messages = _history(10, 40);
state.shown = [
{'id': 'f1', 'kind': 'file', 'created_at': _base + 20 * 1000 + 50},
{'id': 'f0', 'kind': 'file', 'created_at': _base + 1000},
];
state.hasMore = true;
memo = ChatTranscriptRows();
memoKeys = ChatRowKeys();
fullKeys = ChatRowKeys();
check();
});
test('streaming into the last message re-derives only the live turn', () {
_apply(state, {
'kind': 'user',
'message_id': 'u-live',
'part': {'type': 'text', 'text': 'go'},
});
check();
for (var i = 0; i < 20; i++) {
final before = previous;
_delta(state, 'a-live', 'chunk $i ');
final now = check();
expect(now.last['streaming'], isTrue);
// Every row but the live turn is the object the last build held.
if (i > 0) expect(reused(before, now), now.length - 1);
}
// A second message joins the live turn.
_delta(state, 'b-live', 'more');
check();
_apply(state, {
'kind': 'step',
'step': {'status': 'ended'},
});
state.live = null;
final now = check();
expect(now.any((row) => row['streaming'] == true), isFalse);
});
test('a new message appended keeps the settled rows', () {
final before = previous;
state.messages = [
...state.messages,
{
'id': 'u-new',
'role': 'user',
'created_at': _base + 99000,
'parts': [
{'type': 'text', 'text': 'next'},
],
},
];
final now = check();
expect(reused(before, now), greaterThan(before.length - 3));
});
test('a shown item arriving is placed as a full derivation places it', () {
state.shown = mergeChatShownItems(state.shown, [
{'id': 'f2', 'kind': 'image', 'created_at': _base + 30 * 1000 + 10},
]);
final now = check();
expect(now.where((row) => row['kind'] == chatShownRowKind), hasLength(2));
// An older one waits for its page while history remains.
state.shown = mergeChatShownItems(state.shown, [
{'id': 'f3', 'kind': 'notify', 'created_at': _base},
]);
check();
});
test('permission cards added, then settled into their turn', () {
state.cards = [
{
'kind': 'permission',
'key': 'chat-permission-p1',
'frame': {
'request_id': 'p1',
'action': 'Bash',
'source': {'type': 'tool', 'id': 'call-38'},
},
'answer': '',
'error': null,
},
{
'kind': 'question',
'key': 'chat-question-q1',
'form': {'id': 'q1', chatQuestionRaisedIn: 'a37', 'resolved': true},
'error': null,
},
];
var now = check();
expect(now.where((row) => row['kind'] == 'permission'), hasLength(1));
state.cards = [
{...state.cards.first, 'answer': 'once'},
state.cards.last,
];
now = check();
expect(now.where((row) => row['kind'] == 'permission'), isEmpty);
expect(now.where((row) => row['permissions'] != null), hasLength(1));
expect(now.where((row) => row['questions'] != null), hasLength(1));
_delta(state, 'a-live', 'after the card');
check();
});
test('turn diffs arriving attach as a full derivation attaches them', () {
state.diffs.apply({
'session_id': 's1',
'turn_diffs': [
{
'turn_id': 't1',
'message_ids': ['a31'],
'files': 2,
},
],
}, 's1');
final now = check();
expect(now.where((row) => row['turnDiff'] != null), hasLength(1));
_delta(state, 'a-live', 'x');
check();
});
test('a prepended history page re-derives only the runs it touches', () {
final before = previous;
// The older page ends in an assistant message that folds into the
// first loaded turn's neighbour, and the last page drops hasMore.
state.messages = [..._history(0, 10), ...state.messages];
state.hasMore = false;
final now = check();
expect(reused(before, now), greaterThan(before.length ~/ 2));
state.messages = [
{
'id': 'older',
'role': 'assistant',
'parts': [
{'type': 'text', 'id': 'o', 'text': 'older'},
],
},
...state.messages,
];
check();
});
test('a cached transcript reused with the same list returns equal rows', () {
final before = previous;
final now = check();
expect(reused(before, now), now.length);
});
}