replay-latest ValueStream for session state streams (T-386, T-274)

Broadcast streams drop the current value for late subscribers — the
shape behind T-274: the init event fires while spawn() is still
awaiting the transcript-tail read, before the pane subscribes, so the
status bar stayed blank. New pure-Dart ValueStream<T> (no rxdart —
prefer-zero-deps) replays the latest value to each new subscriber;
statusStream, busyStream, and pendingPromptStream in the claude
builtin now use it. busyStream subscribers see the current state
first (seeded false), which the busy test now asserts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 01:05:08 +02:00
co-authored by Claude Fable 5
parent 0e7353bf9c
commit 5f9c054420
8 changed files with 244 additions and 4 deletions
@@ -201,6 +201,21 @@ void main() {
expect(statuses, hasLength(1));
});
// T-274 root cause: the init event fired before the pane subscribed and
// the plain broadcast stream dropped it — the status bar stayed blank.
test('subscribing AFTER the init event still yields the status (T-274/T-386)', () async {
proc.emit(initEvent());
await Future<void>.delayed(Duration.zero);
final late = <SessionStatus>[];
session.statusStream.listen(late.add);
await Future<void>.delayed(Duration.zero);
expect(late, hasLength(1), reason: 'replay-latest delivers the current status to late binders');
expect(late.single.model, 'claude-opus-4-7');
expect(late.single.permissionMode, 'default');
});
test('captures the claude session id from the first event carrying it (T-185)', () async {
final ids = <String>[];
session.sessionIdResolved.listen(ids.add);
@@ -647,7 +662,9 @@ void main() {
proc.emit(jsonEncode({'type': 'result', 'subtype': 'success'}));
await Future<void>.delayed(Duration.zero);
expect(session.busy, isFalse);
expect(busy, [true, false]);
// Leading false is the replayed seed — busyStream tells a new
// subscriber the CURRENT state before the live updates (T-386).
expect(busy, [false, true, false]);
});
test('dispose kills the process', () async {
+117
View File
@@ -0,0 +1,117 @@
/// Tests for [ValueStream] — the replay-latest state holder (T-386).
library;
import 'package:clide/src/util/value_stream.dart';
import 'package:test/test.dart';
void main() {
test('a late subscriber receives the latest value immediately', () async {
final v = ValueStream<int>();
v.add(1);
v.add(2);
final got = <int>[];
v.stream.listen(got.add);
await Future<void>.delayed(Duration.zero);
expect(got, [2]);
});
test('an unseeded holder replays nothing until the first add', () async {
final v = ValueStream<int>();
final got = <int>[];
v.stream.listen(got.add);
await Future<void>.delayed(Duration.zero);
expect(got, isEmpty);
v.add(7);
await Future<void>.delayed(Duration.zero);
expect(got, [7]);
});
test('seeded constructor provides the initial value', () async {
final v = ValueStream<bool>.seeded(false);
expect(v.hasValue, isTrue);
expect(v.value, isFalse);
final got = <bool>[];
v.stream.listen(got.add);
await Future<void>.delayed(Duration.zero);
expect(got, [false]);
});
test('live updates flow to existing subscribers', () async {
final v = ValueStream<String>();
final got = <String>[];
v.stream.listen(got.add);
v.add('a');
v.add('b');
await Future<void>.delayed(Duration.zero);
expect(got, ['a', 'b']);
});
test('each stream access gives every subscriber its own replay', () async {
final v = ValueStream<int>.seeded(5);
final a = <int>[];
final b = <int>[];
v.stream.listen(a.add);
v.stream.listen(b.add);
v.add(6);
await Future<void>.delayed(Duration.zero);
expect(a, [5, 6]);
expect(b, [5, 6]);
});
test('value throws before the first add; valueOrNull is null', () {
final v = ValueStream<int>();
expect(() => v.value, throwsStateError);
expect(v.valueOrNull, isNull);
expect(v.hasValue, isFalse);
});
test('a nullable type can hold null as a real value', () async {
final v = ValueStream<String?>.seeded(null);
expect(v.hasValue, isTrue);
expect(v.valueOrNull, isNull);
final got = <String?>[];
v.stream.listen(got.add);
await Future<void>.delayed(Duration.zero);
expect(got, [null]);
});
test('close ends derived streams; a post-close subscriber still gets the replay', () async {
final v = ValueStream<int>();
v.add(3);
final done = <String>[];
v.stream.listen((_) {}, onDone: () => done.add('a'));
await v.close();
await Future<void>.delayed(Duration.zero);
expect(done, ['a']);
expect(v.isClosed, isTrue);
final got = <int>[];
var closed = false;
v.stream.listen(got.add, onDone: () => closed = true);
await Future<void>.delayed(Duration.zero);
expect(got, [3], reason: 'the last value survives close for late readers');
expect(closed, isTrue);
});
test('add after close updates the value without throwing', () {
final v = ValueStream<int>.seeded(1);
v.close();
v.add(2);
expect(v.value, 2);
});
test('pause/resume on a derived stream buffers updates', () async {
final v = ValueStream<int>();
final got = <int>[];
final sub = v.stream.listen(got.add);
v.add(1);
await Future<void>.delayed(Duration.zero);
sub.pause();
v.add(2);
await Future<void>.delayed(Duration.zero);
expect(got, [1]);
sub.resume();
await Future<void>.delayed(Duration.zero);
expect(got, [1, 2]);
});
}