App: open a chat with one round trip

The thread controller asks chat_subscribe to carry the newest history page
(history: true, limit 200) and applies it from the reply; a server that
leaves the page out is asked with chat_messages as before. Deltas arriving
before the reply are recorded from the start of the subscribe, and settle
against the page's live_seq exactly as for a separate read.

Also documents the subscribe history field and benchmarks the indexed page
at limits 20 and 100.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 15:09:02 +00:00
parent 686690f7b4
commit 608b6b8edb
7 changed files with 201 additions and 61 deletions
@@ -52,6 +52,9 @@ class ChatThreadController extends ChangeNotifier {
final ApiGateway gateway;
final String sessionId;
/// Messages asked for per history page (servers may serve fewer).
static const _pageLimit = 200;
late final ChatRepository _repository = ChatRepository(gateway);
late final ChatThreadCards _cards = ChatThreadCards(
repository: _repository,
@@ -212,9 +215,18 @@ class ChatThreadController extends ChangeNotifier {
_loading = true;
_error = null;
_notify();
await _attach();
if (!_owned(token)) return;
await _readNewest(token, replaceHeldTurn: false);
final frames = _live.beginRead();
final page = await _attach();
if (!_owned(token)) {
_live.abandon(frames);
return;
}
await _readNewest(
token,
replaceHeldTurn: false,
frames: frames,
page: page,
);
}
/// Reads the next older page. Only offered while the server says `has_more`;
@@ -229,7 +241,7 @@ class ChatThreadController extends ChangeNotifier {
final page = await _repository.history(
sessionId,
cursor: _cursor,
limit: 200,
limit: _pageLimit,
);
if (!_owned(token)) return;
turnDiffs.apply(page['turnDiffs'], sessionId);
@@ -276,17 +288,27 @@ class ChatThreadController extends ChangeNotifier {
_awaitingResponseOperation = null;
_authoritative = false;
_notify();
await _attach(force: true);
if (!_owned(token)) return;
await _readNewest(token, replaceHeldTurn: true);
final frames = _live.beginRead();
final page = await _attach(force: true);
if (!_owned(token)) {
_live.abandon(frames);
return;
}
await _readNewest(token, replaceHeldTurn: true, frames: frames, page: page);
}
Future<void> _readNewest(int token, {required bool replaceHeldTurn}) async {
// Deltas of a reply streaming during the read: the page holds the reply
// up to its own point, and those after it are folded back on top.
final frames = _live.beginRead();
/// Applies the newest history page: [page] when the subscribe reply carried
/// it, else one read now. [frames] records the deltas of a reply streaming
/// meanwhile: the page holds the reply up to its own point, and those after
/// it are folded back on top.
Future<void> _readNewest(
int token, {
required bool replaceHeldTurn,
required List<JsonMap> frames,
JsonMap? page,
}) async {
try {
final page = await _repository.history(sessionId, limit: 200);
page ??= await _repository.history(sessionId, limit: _pageLimit);
if (!_owned(token)) {
_live.abandon(frames);
return;
@@ -338,22 +360,29 @@ class ChatThreadController extends ChangeNotifier {
_notify();
}
/// Attaches the session stream once per connection. A refusal leaves history
/// Attaches the session stream once per connection, asking the reply to
/// carry the newest history page, which is returned (null when the server
/// left it out or the stream was already attached). A refusal leaves history
/// usable (the live stream is simply not attached) and is retried on resync.
Future<void> _attach({bool force = false}) async {
if (_disposed || (_subscribed && !force)) return;
Future<JsonMap?> _attach({bool force = false}) async {
if (_disposed || (_subscribed && !force)) return null;
try {
final reply = await _repository.subscribe(sessionId);
if (_disposed) return;
final reply = await _repository.subscribe(
sessionId,
historyLimit: _pageLimit,
);
if (_disposed) return null;
_subscribed = true;
_subscriptionError = null;
_applyQueue(reply['chat_queue']);
_applyShown(reply['chat_shown'], replace: true);
return ChatRepository.historyPageOf(reply);
} catch (error) {
// History stays usable, but the UI must report that live updates failed.
if (_disposed) return;
if (_disposed) return null;
_subscribed = false;
_subscriptionError = '$error';
return null;
}
}