Merge branch 'worktree-agent-afe96053a37e02d6f'

This commit is contained in:
2026-11-26 03:27:28 +00:00
23 changed files with 1198 additions and 110 deletions
@@ -55,6 +55,117 @@ void main() {
},
);
test('output after a quiet spell is shown at once; streaming output at '
'most every show gap', () async {
var shown = 0;
final writes = <String>[];
final feed = ChatShellTerminalFeed(
(text, _) => writes.add(text),
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: const Duration(milliseconds: 300),
deferShows: true,
);
feed.add('abc', 3);
// The first output, written whole at once: shown at once.
expect(writes, ['abc']);
expect(shown, 1);
feed.add('x' * 35, 38);
await turn();
await turn();
// Behind, within the gap: not shown again.
expect(writes.length, 4);
expect(shown, 1);
await turn();
await turn();
// Caught up within the gap: shown when the gap has passed.
expect(writes.skip(1).join(), 'x' * 35);
expect(feed.idle, isTrue);
expect(shown, 1);
await Future<void>.delayed(const Duration(milliseconds: 400));
expect(shown, 2);
// After a quiet spell: at once again.
await Future<void>.delayed(const Duration(milliseconds: 400));
feed.add('q', 39);
expect(shown, 3);
// An action waiting behind output sees it shown first.
feed.add('y' * 25, 64);
var shownForAction = -1;
feed.then(() => shownForAction = shown);
for (var i = 0; i < 5; i++) {
await turn();
}
expect(shownForAction, 4);
expect(shown, 4);
feed.dispose();
});
test('without deferred shows (natively) written output is shown at once; '
'only a burst still behind waits for the gap', () async {
var shown = 0;
final feed = ChatShellTerminalFeed(
(_, _) {},
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: const Duration(hours: 1),
deferShows: false,
);
feed.add('abc', 3);
feed.add('def', 6);
expect(shown, 2);
feed.add('x' * 35, 41);
await turn();
await turn();
expect(shown, 2);
for (var i = 0; i < 3; i++) {
await turn();
}
expect(feed.idle, isTrue);
expect(shown, 3);
});
test('every turn shows its state with no show gap', () async {
var shown = 0;
final feed = ChatShellTerminalFeed(
(_, _) {},
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: Duration.zero,
);
feed.add('x' * 30, 30);
for (var i = 0; i < 5; i++) {
await turn();
}
expect(feed.idle, isTrue);
expect(shown, 3);
});
test('slices follow the measured speed within the slice', () async {
final sizes = <int>[];
final feed = ChatShellTerminalFeed(
(text, _) {
sizes.add(text.length);
// About 1 ms per 1000 units.
final until = Stopwatch()..start();
while (until.elapsedMicroseconds < text.length) {}
},
budget: const Duration(milliseconds: 6),
slice: 16 * 1024,
);
feed.add('y' * 100000, 100000);
while (!feed.idle) {
await turn();
}
expect(sizes.reduce((a, b) => a + b), 100000);
expect(sizes.every((s) => s <= 16 * 1024), isTrue);
// After the first slice measured the speed, no write takes much more
// than a turn's budget (6 ms, about 6000 units here).
expect(sizes.skip(1).every((s) => s <= 8000), isTrue, reason: '$sizes');
});
test('a surrogate pair is never split between two writes', () {
final writes = <String>[];
final feed = ChatShellTerminalFeed((text, _) => writes.add(text), slice: 3);