chat: keep how far each session is shown, and take the stream up from there #68

Merged
mikluko merged 1 commit from shown-cursor into main 2026-11-16 09:50:42 +00:00
Owner

The chat side keeps how far it has shown each session, in the registry's shown (schema 2, #64), and takes the stream up from there.

  • A session is followed from Info.Shown in every state. A new session is shown from its first event, which a subscription "from now" could miss (its role prompt, "From gc"); one shown before a restart is not shown again.
  • The renderer reports a settled point: the last event after which no answer text, no thoughts and no plan edit waits unsent. The service records it at most every 200 ms, after the sends. A restart in the middle of a paragraph shows that paragraph again; nothing is lost, and nothing before the settled point is shown twice.
  • Closes the known limitation on spec-bb1.25 (what a session said while the homeserver was down): a message or reaction the room does not take ends the reading, the cursor stays where it was, and the stream is read again from it after five seconds. A test has the homeserver down while frames arrive: nothing is counted as shown, and the frames are shown once when it is back.
  • That outage is logged once when it starts, once a minute while it lasts, and once when it ends (a fixed minute, not a growing interval).
  • After a long outage the room does not replay hours at once: events older than [chat] catch_up (a duration, default one hour) are left out, and the room says so in one notice that links the session's transcript, also when only old events are left.
  • Duration moves from config to chat, with config.Duration as its alias, so that [chat] can have one.

Not done here: a restart in the middle of a turn does not restore what the renderer remembers (the plan notice's id, the reply-to links of tool results); those notices come again without them.

The chat side keeps how far it has shown each session, in the registry's `shown` (schema 2, #64), and takes the stream up from there. - A session is followed from `Info.Shown` in every state. A new session is shown from its first event, which a subscription "from now" could miss (its role prompt, "From gc"); one shown before a restart is not shown again. - The renderer reports a settled point: the last event after which no answer text, no thoughts and no plan edit waits unsent. The service records it at most every 200 ms, after the sends. A restart in the middle of a paragraph shows that paragraph again; nothing is lost, and nothing before the settled point is shown twice. - Closes the known limitation on spec-bb1.25 (what a session said while the homeserver was down): a message or reaction the room does not take ends the reading, the cursor stays where it was, and the stream is read again from it after five seconds. A test has the homeserver down while frames arrive: nothing is counted as shown, and the frames are shown once when it is back. - That outage is logged once when it starts, once a minute while it lasts, and once when it ends (a fixed minute, not a growing interval). - After a long outage the room does not replay hours at once: events older than `[chat] catch_up` (a duration, default one hour) are left out, and the room says so in one notice that links the session's transcript, also when only old events are left. - `Duration` moves from `config` to `chat`, with `config.Duration` as its alias, so that `[chat]` can have one. Not done here: a restart in the middle of a turn does not restore what the renderer remembers (the plan notice's id, the reply-to links of tool results); those notices come again without them.
chat: keep how far each session is shown, and take the stream up from there
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 16s
test / go (pull_request) Successful in 54s
c218d17ef5
Assisted-By: Claude <noreply@anthropic.com>
Author
Owner

Read at c218d17. The suite is green here under -race, internal/chat ten times with GOMAXPROCS=1. The shape is right: one cursor, moved only where nothing waits unsent, the stream read again from it. Three things stop it from merging, each a case where the room gets the same thing twice or without end.

1. A refusal is treated as an outage, and then never ends. Any error of Send or React ends the reading and the stream is read again from the cursor after five seconds. That is right for a homeserver out of reach. It is wrong for a homeserver that answered and refused, because the same event is refused again at every reading, and everything between the cursor and that event is sent again each time.

One way there with nothing unusual: the owner writes while the agent is in the middle of a paragraph. The prompt waits, the renderer puts ⏳ on the owner's message, and the cursor is behind it because text is pending. Now one send fails for any reason. The stream is read again in the same process, the room's record of the owner's message is still there (promptMessage), and the renderer puts ⏳ on it a second time. Tuwunel 2.0.0 answers that with

{"errcode":"M_DUPLICATE_ANNOTATION","error":"M_DUPLICATE_ANNOTATION: Duplicate reactions are not allowed."}

(tried here: the same reaction twice by one user). That is an error of React, so the reading ends, and five seconds later it does the same, with the paragraphs since the cursor sent once more each time. A message the homeserver will not take for itself (too large, forbidden) does the same.

So: tell "out of reach" (no answer, a 5xx, a 429) from "refused" (the homeserver answered with an error of its own). Read again only for the first. A refusal is logged, as before this change, and the event counts as shown; a duplicate reaction is the reaction being there already.

2. A tool notice and a permission request can be behind the cursor. settled is false while a plan edit waits, and the plan's edits are throttled (1.2 s), while Claude Code's usual order is a plan update and then the tool call at once. Tried on the renderer: a plan, a second plan 100 ms later, a tool call and a permission request for it; after the permission message and its reaction are sent, settled() is false. A restart, or a re-reading after a failed send, in that window shows the tool notice and the permission request a second time, the first request left with nothing behind it. The coordinator has this as the one thing that may not happen.

Make every event that sends what may not come twice a settled point: finish the plan's edit before a tool notice, a tool result and a permission request are sent, as the answer and the thoughts are already flushed before them. Then only text is ever behind the cursor.

3. The cursors of a city that ran 0.6.0 are stale, and the first start on this version replays up to an hour. 0.6.0 has the column and does not move it: in the city today guest stands at 1096 of 1166 events and host at 631, both where the migration put them at 09:16:26Z, and everything since was shown live. This code follows a session from Info.Shown, so at the roll each room is shown again whatever is younger than catch_up, permission requests and their answers among it, and told that the rest was left out.

The version that begins to keep the cursor has to begin it at the last event. That is a migration of the registry (schema 3: shown to each session's last event, as the migration to 2 set it), with roomcity-b8; say in the pull request which of you does it, and it lands with this or before it.

Two things to say in the pull request and the README, not to change:

  • The cost of a restart in the middle of an answer is not one paragraph. The cursor moves only at an event after which no text is pending, which for text is a chunk that ends exactly where a paragraph does; otherwise it stays at the last tool call or prompt, and every paragraph since comes again, sent or not.
  • A permission request that is open when the server restarts cannot be answered from the room afterwards: the renderer that knew which message it was is gone, and a reaction on that message is taken for nothing. !stop withdraws it. That was so before this change; the description's "an answer to a request the room never saw" covers the answer and not this.

It conflicts with main since #70: rebase when you take these up.

Read at c218d17. The suite is green here under `-race`, `internal/chat` ten times with `GOMAXPROCS=1`. The shape is right: one cursor, moved only where nothing waits unsent, the stream read again from it. Three things stop it from merging, each a case where the room gets the same thing twice or without end. **1. A refusal is treated as an outage, and then never ends.** Any error of `Send` or `React` ends the reading and the stream is read again from the cursor after five seconds. That is right for a homeserver out of reach. It is wrong for a homeserver that answered and refused, because the same event is refused again at every reading, and everything between the cursor and that event is sent again each time. One way there with nothing unusual: the owner writes while the agent is in the middle of a paragraph. The prompt waits, the renderer puts ⏳ on the owner's message, and the cursor is behind it because text is pending. Now one send fails for any reason. The stream is read again in the same process, the room's record of the owner's message is still there (`promptMessage`), and the renderer puts ⏳ on it a second time. Tuwunel 2.0.0 answers that with {"errcode":"M_DUPLICATE_ANNOTATION","error":"M_DUPLICATE_ANNOTATION: Duplicate reactions are not allowed."} (tried here: the same reaction twice by one user). That is an error of `React`, so the reading ends, and five seconds later it does the same, with the paragraphs since the cursor sent once more each time. A message the homeserver will not take for itself (too large, forbidden) does the same. So: tell "out of reach" (no answer, a 5xx, a 429) from "refused" (the homeserver answered with an error of its own). Read again only for the first. A refusal is logged, as before this change, and the event counts as shown; a duplicate reaction is the reaction being there already. **2. A tool notice and a permission request can be behind the cursor.** `settled` is false while a plan edit waits, and the plan's edits are throttled (1.2 s), while Claude Code's usual order is a plan update and then the tool call at once. Tried on the renderer: a plan, a second plan 100 ms later, a tool call and a permission request for it; after the permission message and its reaction are sent, `settled()` is false. A restart, or a re-reading after a failed send, in that window shows the tool notice and the permission request a second time, the first request left with nothing behind it. The coordinator has this as the one thing that may not happen. Make every event that sends what may not come twice a settled point: finish the plan's edit before a tool notice, a tool result and a permission request are sent, as the answer and the thoughts are already flushed before them. Then only text is ever behind the cursor. **3. The cursors of a city that ran 0.6.0 are stale, and the first start on this version replays up to an hour.** 0.6.0 has the column and does not move it: in the city today `guest` stands at 1096 of 1166 events and `host` at 631, both where the migration put them at 09:16:26Z, and everything since was shown live. This code follows a session from `Info.Shown`, so at the roll each room is shown again whatever is younger than `catch_up`, permission requests and their answers among it, and told that the rest was left out. The version that begins to keep the cursor has to begin it at the last event. That is a migration of the registry (schema 3: `shown` to each session's last event, as the migration to 2 set it), with roomcity-b8; say in the pull request which of you does it, and it lands with this or before it. Two things to say in the pull request and the README, not to change: - The cost of a restart in the middle of an answer is not one paragraph. The cursor moves only at an event after which no text is pending, which for text is a chunk that ends exactly where a paragraph does; otherwise it stays at the last tool call or prompt, and every paragraph since comes again, sent or not. - A permission request that is open when the server restarts cannot be answered from the room afterwards: the renderer that knew which message it was is gone, and a reaction on that message is taken for nothing. `!stop` withdraws it. That was so before this change; the description's "an answer to a request the room never saw" covers the answer and not this. It conflicts with main since #70: rebase when you take these up.
mikluko force-pushed shown-cursor from c218d17ef5
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 16s
test / go (pull_request) Successful in 54s
to 1211172b05
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 17s
test / go (pull_request) Successful in 48s
2026-11-16 09:37:04 +00:00
Compare
mikluko force-pushed shown-cursor from 1211172b05
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 17s
test / go (pull_request) Successful in 48s
to 91506374b7
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 17s
test / go (pull_request) Successful in 42s
2026-11-16 09:44:06 +00:00
Compare
mikluko force-pushed shown-cursor from 91506374b7
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 17s
test / go (pull_request) Successful in 42s
to 342991916d
All checks were successful
test / changelog (pull_request) Successful in 6s
image / image (pull_request) Successful in 19s
test / go (pull_request) Successful in 1m6s
2026-11-16 09:49:16 +00:00
Compare
mikluko deleted branch shown-cursor 2026-11-16 09:50:42 +00:00
Commenting is not possible because the repository is archived.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
mikluko/roomcity!68
No description provided.