feat(app): show a session's running background tasks

The conversation reads the session's recorded chat_shells snapshot and folds
pushed shell frames, so only tasks the server still reports as running appear.
The list is capped at three visible rows and scrolls beyond, and a task that
exits leaves it.

Selecting a task opens its recorded command, identity and running status plus
one bounded chat_shell_output page (cursor 0, 64 KiB). Empty, unavailable and
refused reads are reported with native Retry/Refresh controls, and a refused
snapshot is shown with its reason instead of claiming there are no tasks.
Both reads carry the session id, linking every task to its conversation.
This commit is contained in:
2026-10-22 17:14:56 +01:00
parent cf6da4acaa
commit 2cb4a0c555
6 changed files with 1370 additions and 0 deletions
@@ -0,0 +1,160 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_shells.dart';
JsonMap _shell(String id, String status, {String command = ''}) => {
'id': id,
'status': status,
'command': command,
};
void main() {
group('recorded running tasks', () {
test('only a non-blank id with status running is a task', () {
final shells = [
_shell('sh1', 'running', command: 'go test ./...'),
_shell('sh2', 'exited', command: 'pnpm build'),
_shell('', 'running', command: 'no identity'),
{'status': 'running', 'command': 'no id'},
_shell('sh3', 'pending', command: 'not started'),
];
expect(runningShells(shells).map((shell) => shell['id']), ['sh1']);
});
test('a shell frame merges by id and keeps the command already known', () {
final first = mergeShellFrame(
const [],
_shell('sh1', 'running', command: 'go test ./...'),
);
final exited = mergeShellFrame(first, _shell('sh1', 'exited'));
final other = mergeShellFrame(
exited,
_shell('sh2', 'running', command: 'sleep 1'),
);
expect(other, hasLength(2));
expect(other.first['status'], 'exited');
expect(other.first['command'], 'go test ./...');
expect(runningShells(other).map((shell) => shell['id']), ['sh2']);
expect(mergeShellFrame(other, null), same(other));
expect(mergeShellFrame(other, _shell('', 'running')), same(other));
});
test('the retained observation tail is bounded', () {
var shells = const <JsonMap>[];
for (var index = 0; index < observedShellLimit + 4; index++) {
shells = mergeShellFrame(shells, _shell('sh$index', 'running'));
}
expect(shells, hasLength(observedShellLimit));
expect(shells.first['id'], 'sh4');
expect(shells.last['id'], 'sh${observedShellLimit + 3}');
});
test('a snapshot keeps observed exits and drops shells never seen', () {
final observed = [
_shell('exited', 'exited', command: 'pnpm build'),
_shell('running', 'running', command: 'go test ./...'),
_shell('gone', 'running', command: 'old fact'),
];
final loaded = shellsLoaded(observed, [
_shell('running', 'running', command: 'go test ./...'),
_shell('new', 'running', command: 'tail -f log'),
]);
expect(loaded.map((shell) => '${shell['id']}:${shell['status']}'), [
'exited:exited',
'running:running',
'new:running',
]);
expect(runningShells(loaded).map((shell) => shell['id']), [
'running',
'new',
]);
});
});
group('shell wording', () {
test('the command collapses whitespace and names a blank one', () {
expect(shellCommandSummary(' go test\n ./... '), 'go test ./...');
expect(shellCommandSummary(' '), 'background process');
expect(shellCommandSummary(null), 'background process');
});
test('a long command is elided to the tool-detail width', () {
final summary = shellCommandSummary('x' * 200);
expect(summary.length, shellCommandSummaryLength);
expect(summary.endsWith('…'), isTrue);
expect(summary, '${'x' * (shellCommandSummaryLength - 1)}…');
});
test('the heading keeps the recorded count', () {
expect(backgroundTasksTitle, 'Background tasks');
expect(backgroundTasksRunningLabel(0), '0 running');
expect(backgroundTasksRunningLabel(3), '3 running');
});
});
group('bounded output page', () {
test('a page carries its output, bound and reported shell', () {
final page = ChatShellOutputPage.fromReply({
'chat_shell_output': {
'shell': {'id': 'sh1', 'status': 'running', 'command': 'go test'},
'output': 'ok example\n',
'cursor': 0,
'size': 11,
'truncated': true,
},
});
expect(page.output, 'ok example\n');
expect(page.truncated, isTrue);
expect(page.unavailable, isFalse);
expect(page.shell?['command'], 'go test');
expect(chatShellOutputLimit, 64 * 1024);
});
test('an unknown reply parses as no page, never invented output', () {
final page = ChatShellOutputPage.fromReply(const {});
expect(page.output, '');
expect(page.shell, isNull);
expect(
const ChatShellOutputSnapshot(page: ChatShellOutputPage()).state,
ChatShellOutputState.empty,
);
});
test(
'the pane state follows loading, error, unavailable, empty, ready',
() {
expect(
const ChatShellOutputSnapshot().state,
ChatShellOutputState.none,
);
expect(
const ChatShellOutputSnapshot(loading: true).state,
ChatShellOutputState.loading,
);
expect(
const ChatShellOutputSnapshot(error: 'refused').state,
ChatShellOutputState.error,
);
expect(
const ChatShellOutputSnapshot(
page: ChatShellOutputPage(unavailable: true),
).state,
ChatShellOutputState.unavailable,
);
expect(
const ChatShellOutputSnapshot(
page: ChatShellOutputPage(output: 'ready'),
).state,
ChatShellOutputState.ready,
);
},
);
});
}