Files
pipelines/app/tool/perf/perf_fixture.dart
T
alan ad4233f7b8 Chat: /new continues the same chat on a fresh session
The composer's /new used to create a separate chat. It now sends
chat_new_session. The server starts a fresh native session on the chat's
own harness (OpenCode, Codex or Claude Code) and re-keys the
operator_sessions row in one transaction. The chat keeps its title,
directory, backend, colour, star, model and mode. The old row is kept
unlisted with replaced_by, and its native session stays resumable.

The reply and the chat_session_changed push carry previous_id, so every
client swaps the row in place and an open conversation follows it to the
fresh, empty transcript with a notice.
2026-11-03 14:06:44 +00:00

289 lines
9.4 KiB
Dart

// Extra fixture data for the interaction bench (tool/perf/interaction_bench.mjs)
// over the full-app probe (tool/search_selection_probe_entry.dart). Every
// option defaults to off, so other captures see the fixture unchanged.
//
// `?hits=N` N more `release` run hits in Search;
// `?chats=N` N chat sessions in the chat list, each opening a short
// markdown transcript;
// `?graph=1` run 7 carries its pinned graph (plan -> build);
// `?quota=claude|opencode` (with `?chats=N`) pushes a usage frame with
// that harness's account quota after a chat subscribes.
// `?colors=1` (with `?chats=N`) names the chats, stars and colours some of
// them, and keeps colours picked with chat_color;
// `?msgs=`, `?rich=1`, `?older=1`, `?live=N` vary the chat transcripts for
// scroll captures (tool/perf/chat_scroll_fixture.dart).
// `/new` in a fixture chat moves it to a fresh, empty session
// (tool/perf/chat_new_session_fixture.dart).
import 'package:pipelines_app/core/api_gateway.dart';
import 'chat_new_session_fixture.dart';
import 'chat_scroll_fixture.dart';
Map<String, String> _params = const {};
/// Reads the options from the page URL's query, before the app rewrites it.
void configurePerfFixture(Map<String, String> params) {
_params = params;
configureChatScrollFixture(params);
}
int get _hits => int.tryParse(_params['hits'] ?? '') ?? 0;
int get _chats => int.tryParse(_params['chats'] ?? '') ?? 0;
bool get perfRunGraph => _params['graph'] == '1';
bool get _colors => _params['colors'] == '1';
/// `?colors=1`: titles, directories, stars and colours per chat index.
const _colorTitles = [
'Release notes',
'api refactor',
'Billing bug',
'Docs pass',
'Flaky test',
'Onboarding',
'Perf sweep',
'Search UX',
];
const _colorStarred = {0, 1, 3};
const _colorNames = ['teal', 'purple', null, 'amber', 'red', null, 'blue'];
final Map<String, String?> _pickedColors = {};
JsonMap _chatRow(int i) {
final id = perfChatId(i);
if (!_colors) {
return {
'id': id,
'title': 'Fixture chat ${i + 1}',
'directory': '/workspace/demo',
// Server-shaped handoff facts (chat_sessions rows carry them).
if (_harness != 'opencode') 'backend': _harness,
'resume_command': _resumeCommand(id),
'native_export': _harness == 'claude',
};
}
final color = _pickedColors.containsKey(id)
? _pickedColors[id]
: _colorNames[i % _colorNames.length];
return {
'id': id,
'title': _colorTitles[i % _colorTitles.length],
'directory': i.isEven ? '/workspace/demo' : '/workspace/site',
'model': 'claude-opus',
'backend': 'claude',
'updated_at': DateTime.now().millisecondsSinceEpoch - (i + 1) * 600000,
if (_colorStarred.contains(i)) 'starred': true,
'color': ?color,
};
}
/// `?harness=claude|codex` makes the chats that harness's (default OpenCode),
/// with the resume command the server would send for it.
String get _harness => _params['harness'] ?? 'opencode';
String _resumeCommand(String id) => switch (_harness) {
'claude' => 'cd /workspace/demo && claude --resume $id',
'codex' => 'cd /workspace/demo && codex resume $id',
_ => 'cd /workspace/demo && opencode --session $id',
};
/// Whether a chat scroll option (tool/perf/chat_scroll_fixture.dart) shapes
/// the transcripts instead of the bench's short markdown history.
bool get _scrollVariety => ['msgs', 'rich', 'older'].any(_params.containsKey);
/// Unsolicited envelopes that follow a reply: the `?quota=` usage frame.
List<JsonMap> perfChatFollowUps(JsonMap request) {
final quota = _params['quota'];
if (_chats == 0 || quota == null || request['type'] != 'chat_subscribe') {
return const [];
}
final now = DateTime.now();
int at(Duration offset) => now.add(offset).millisecondsSinceEpoch;
final account = quota == 'opencode'
? {'harness': 'OpenCode', 'unsupported': true}
: {
'harness': 'Claude Code',
'source': 'rate_limit_event',
'plan': 'max',
'as_of': at(const Duration(minutes: -4)),
'windows': [
{
'id': 'five_hour',
'label': '5-hour',
'used_percent': 42,
'resets_at': at(
const Duration(hours: 2, minutes: 14, seconds: 30),
),
},
{
'id': 'seven_day',
'label': 'Weekly',
'used_percent': 93,
'resets_at': at(const Duration(days: 3, hours: 5)),
},
],
};
return [
{
'type': 'chat_event',
'chat_event': {
'session_id': (request['chat_subscribe'] as Map?)?['session_id'],
'kind': 'usage',
'usage': {
'scope': 'session',
'total_tokens': 132408,
'context_tokens': 42000,
'context_window': 200000,
'account': account,
},
},
},
];
}
/// Run 7's pinned graph for `?graph=1`.
JsonMap perfRunGraphFixture() => {
'name': 'Release train',
'stages': [
{
'key': 'plan',
'type': 'model',
'edges': {'success': 'build', 'error': '#'},
},
{
'key': 'build',
'type': 'model',
'edges': {'success': 'plan', 'error': '#failure'},
},
],
};
/// Filler run hits for a query that `release` text matches.
List<JsonMap> perfSearchHits(String query, String kind) {
if (_hits == 0 || (kind.isNotEmpty && kind != 'run')) return const [];
if (query.isNotEmpty && !'release filler run'.contains(query)) {
return const [];
}
return [
for (var i = 0; i < _hits; i++)
{
'kind': 'run',
'target': 'run:${1000 + i}',
'score': 1,
'recency': '2026-10-18T08:42:00Z',
'run': {
'id': 1000 + i,
'pipeline_name': 'Release filler $i',
'status': i % 3 == 0 ? 'failed' : 'succeeded',
'workspace': '/srv/probe/release',
'created_at': '2026-10-18T08:12:00Z',
'finished_at': '2026-10-18T08:42:00Z',
},
},
];
}
String _answer(int i) =>
'## Answer $i\n\nSome **bold** text with `code`.\n\n'
'```dart\n${List.generate(8, (l) => 'final v$l = $i * $l;').join('\n')}'
'\n```\n\n- one\n- two\n';
/// Chat replies for `?chats=N`, or null to fall through to the fixture.
JsonMap? perfChatReply(JsonMap request) {
if (_chats == 0) return null;
final id = request['id'];
switch ('${request['type']}') {
case 'chat_sessions':
return {
'id': id,
'type': 'chat_sessions',
'chat_sessions': [for (var i = 0; i < _chats; i++) _chatRow(i)],
};
case 'chat_color':
final query = request['chat_query'];
final session = query is Map ? '${query['session_id'] ?? ''}' : '';
final color = query is Map ? query['color'] as String? : null;
_pickedColors[session] = color;
final row = _chatRow(perfChatIndex(session));
return {
'id': id,
'type': 'chat_color',
'chat_session': row,
'chat_sessions': [row],
};
case 'chat_export':
final body = request['chat_export'];
final format = body is Map ? '${body['format'] ?? ''}' : '';
final json = format == 'json';
return {
'id': id,
'type': 'chat_export',
'chat_export_file': {
'session_id': body is Map ? body['session_id'] : '',
'format': format,
'name': json ? 'Fixture-chat.json' : 'Fixture-chat.md',
'mime': json ? 'application/json' : 'text/markdown',
'content': json ? '{"messages":[]}\n' : '# Fixture chat\n',
},
};
case 'chat_new_session':
return perfChatNewSessionReply(request, _chatRow);
case 'chat_subscribe':
return {'id': id, 'type': 'chat_subscribed'};
case 'chat_shells':
return {'id': id, 'type': 'chat_shells', 'chat_shells': const []};
case 'chat_messages':
final query = request['chat_query'];
final session = query is Map ? '${query['session_id'] ?? ''}' : '';
if (perfChatIsFresh(session)) {
return {
'id': id,
'type': 'chat_messages',
'chat_history': {
'messages': const [],
'next_cursor': '',
'has_more': false,
},
};
}
if (_scrollVariety) {
return {
'id': id,
'type': 'chat_messages',
'chat_history': chatScrollHistory(
session,
perfChatIndex(session),
query is Map ? '${query['cursor'] ?? ''}' : '',
),
};
}
return {
'id': id,
'type': 'chat_messages',
'chat_history': {
'messages': [
for (var i = 0; i < 20; i++)
{
'id': 'perf-m$i',
'role': i.isEven ? 'user' : 'assistant',
'parts': [
{
'type': 'text',
'text': i.isEven ? 'Question $i' : _answer(i),
},
],
},
],
'next_cursor': '',
'has_more': false,
},
};
}
return null;
}
/// The `?chats=N` list rows, as chat_sessions serves them.
List<JsonMap> perfChatRows() => [for (var i = 0; i < _chats; i++) _chatRow(i)];
/// The text of message `perf-m<m>` in the bench's short transcript, newest
/// last (20 messages).
String perfChatMessageText(int m) => m.isEven ? 'Question $m' : _answer(m);