oficina / equipo Public

Conversation: a reply commits after its draft draws; sends keep order

After main merged in, stream-markdown's long reply sometimes committed
before the draft drew its last words. The committed message replaced a
render about 50 characters short. On main e0595b5 it fails 1 run in 4,
and on the merge 2 in 2. Nothing makes the commit wait for the draft
renderer (one POST /conversation/draft at a time), so whichever of the
page refresh and the last draft answers first wins.

- A refresh that would settle a reply into its line waits until the
  draft has drawn all it was given (no draft request out, no frame
  pending, no hold). It waits up to two seconds, then the next refresh
  settles it.
- An own-message event for a send still waiting for its answer names
  that element in place, matched by text. Before, it drew a second
  element below, the first was removed when its answer came, and two
  quick sends could swap order. That flaked
  TestBrowserInstantThinkingQueued's note-under-the-latest check.

TestBrowserDraftStreamsFormatted passed 7 runs in a row.
TestBrowserThinkingLinesClear and TestBrowserInstantThinking* pass, and
so do ThinkingAndCards, RefreshRace, ReceiptsDesktop,
DraftIsTheMessage, EmptyExplore and TestPlan3ReleaseBoundaries.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 7ec9e4b6e283d695c0714609e100027d02840ecb parent 9918393
1 file changed, +36 −2
  • internal/equipo/static/chat-stream.js +36 −2

CI failed — run details

diff --git a/internal/equipo/static/chat-stream.js b/internal/equipo/static/chat-stream.js
index 54e90087..25886291 100644
--- a/internal/equipo/static/chat-stream.js
+++ b/internal/equipo/static/chat-stream.js
@@ -420,6 +420,22 @@
gone.remove();
}
}
+ // drawn: the draft shows the whole text it has been given (none asked
+ // for or waiting for a frame). The committed reply waits for that, up to
+ // two seconds, so the end of a long stream is drawn before it commits.
+ function drawn(live) {
+ var body = live.querySelector("[data-stream-text]"), d = body && body.draft;
+ return !live.holding && (!d || !d.busy && d.asked === d.want && !d.frame);
+ }
+ function whenDrawn(live, then) {
+ if (live.drawWait) return;
+ live.drawWait = true;
+ (function wait() {
+ if (live.isConnected && !drawn(live) && Date.now() < live.settleBy) return setTimeout(wait, 50);
+ live.drawWait = false;
+ then();
+ })();
+ }
// The committed reply takes over its draft in place: the same element,
// now with the server's attributes, time and markdown. No reflow.
function settle(live, server) {
@@ -717,7 +733,15 @@
}
if (!old) return el;
delete keep[turn];
- return el.hasAttribute("equipo-thinking") ? old : settle(old, el);
+ if (el.hasAttribute("equipo-thinking")) return old;
+ old.settleBy = old.settleBy || Date.now() + 2000;
+ if (!drawn(old) && Date.now() < old.settleBy) {
+ // The draft is still drawing its last words: the reply
+ // settles on the refresh after it has.
+ whenDrawn(old, refreshSoon);
+ return old;
+ }
+ return settle(old, el);
}).filter(Boolean));
// A stale page lacks lines for turns that started since: they stay.
if (stale) Object.keys(keep).forEach(function (turn) { if (!ended[turn] && !queued[turn]) feed.append(keep[turn]); });
@@ -789,6 +813,15 @@
refreshSoon();
return;
}
+ // A send from this page still waiting for its answer: the event
+ // names it in place, so the messages keep the order they were sent.
+ var mine = Array.from(local).find(function (el) { return !el.dataset.message && el.isConnected && el.querySelector("[equipo-message-body]").textContent === v.text; });
+ if (mine) {
+ mine.dataset.message = v.id;
+ mine.dataset.turnId = v.turn_id || "";
+ notes();
+ return;
+ }
var own = template("equipo-own-template");
own.dataset.message = v.id;
own.dataset.turnId = v.turn_id || "";
@@ -981,7 +1014,8 @@
// A conversation send names its turn; a thread question is its own.
claim(line, thread ? sent.message.id : sent.turn && sent.turn.id);
notes();
- if (feed.querySelector('[data-message="' + CSS.escape(sent.message.id) + '"]')) {
+ var shown = feed.querySelector('[data-message="' + CSS.escape(sent.message.id) + '"]');
+ if (shown && shown !== el) {
el.remove();
notes();
return;