A job that finishes reaches the page you are looking at

Three complaints, all downstream of background commands.

A finished job woke the model and not the browser. `jobs.wake` writes the
completion and calls `generation.ensure`, and nothing tells the page: the only
stream here is per-message, opened by the `sse-connect` on an incomplete
assistant bubble -- which is a bubble this page has not got, because the reply
that created it began somewhere else. `_queue_frames` proves the swap works and
can only ride a stream already open. So the reader sat on the chat, watched the
sidebar dot light up for the chat in front of them, and had to click it or
reload to see a reply that had been there for minutes.

`GET /api/chats/{id}/tail?after=` and a five-second poller is the answer, polled
for the reason `/unread` is: a second always-on connection per tab is a lot of
machinery for something that happens a few times a day. A cursor it cannot place
-- absent, from another chat, naming a row a rewind deleted -- is answered with
204 and never with the transcript, which the page still holds every bubble of.
The cut is read from the row so `_inject`'s restamp moves it too, and compared in
SQL, a row read back from SQLite being naive where one still in the session is
aware; the `id >` tie-break is not decoration, since under a bare `>` a row
sharing the cut's microsecond is skipped for ever.

The cursor comes from the DOM, because the DOM is the honest answer to what the
page has -- the composer's POST, the `done` frame and the last poll all move it,
and a variable would have to be updated by each of them, correctly, for ever. On
`htmx:configRequest` rather than `hx-vals="js:…"`: two of the three things that
handler does are cancellations, which `hx-vals` cannot express. Not
`article.msg:last-of-type` either -- that is per-parent, so on a compacted chat
it answers with the last article inside the `<details>` and the poll re-appends
half the conversation. It is silent while a reply streams, since that reply
delivers its own bubbles in the one frame that can get the order right, and a
`htmx:beforeSwap` listener drops any answer holding a bubble already on the page:
the race `hx-sync` cannot reach, and a duplicate there is a second `sse-connect`
for one message rather than a cosmetic one. The route clears `unread` on every
tick including the 204, because `_persist` marks a reply unread whenever
`followers == 0` and that is true of a job-woken reply with somebody watching it.

The completion also claimed the reader had sent it. The role is load-bearing --
`_inject` sends a queued turn verbatim and `build_messages` must keep seeing a
user turn -- so `Message.machine` marks the bubble instead and the request is
untouched. Their initial, their name and a pencil offering to rewrite what a
machine reported: the route refuses the edit too, a hidden button being a
courtesy. `_completion_text` is deliberately unchanged, `tool.background` quoting
its opening sentence to the model, and there is now a test holding the two
together.

And the panel. `.jobs__row` had no horizontal padding while `.picker__menu` has
none either, so every row ran flush into the border under a header inset by
--sp-3. `jobs__row--open` had been emitted since the panel shipped with no rule
anywhere, so the row whose log was on screen looked like the ones that were not.
The dot was keyed on `status`, and `done` is exit 0 and exit 2 alike -- green
beside the row's own "Failed, exit 2" -- so `JobView.tone` answers the colour and
the template goes on answering the wording, which is the half a class name cannot
carry. `duration` is empty for a running job on purpose: this panel is fetched
when somebody opens it and never polled, so a live figure would freeze the
instant it painted. Its stamps are normalised before subtracting, a job started
before a restart and finished after it having one naive and one aware.

Driven under the DOM stub before committing, per the standing rule: two listeners
on document.body for events dispatched at a requesting element are exactly the
shape a regex cannot check.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaroslav Beneš
2026-08-05 12:19:07 +02:00
parent a63723713f
commit 08fec2cb64
12 changed files with 1023 additions and 9 deletions
+97
View File
@@ -689,6 +689,103 @@ def test_cancelling_an_edit_restores_the_bubble(client: TestClient, db, register
assert "edit-form" not in page
# --- A turn nobody typed ------------------------------------------------------
def _machine_turn(db, chat_id: str, content: str = "A background job you started has finished"):
"""What `jobs.wake` writes: a user-role turn the application produced."""
from lembas.db.models import Chat
from lembas.services import chat as chat_service
chat = db.get(Chat, chat_id)
return chat_service.create_message(db, chat, "user", content, machine=True)
def test_a_machine_turn_is_not_shown_as_the_readers_own(
client: TestClient, db, registered, make_chat
):
"""A background job finishing is a user turn because the request needs it to
be, not because the reader said it. Rendering it under their name with their
initial beside it is the application putting words in their mouth."""
_add_connection(db)
chat_id = make_chat()
_machine_turn(db, chat_id)
page = client.get(f"/chat/{chat_id}").text
assert "msg--machine" in page
assert "Background job" in page
assert "msg__initial" not in page, "no initial in the gutter for a turn nobody typed"
def test_a_machine_turn_offers_no_pencil(client: TestClient, db, registered, make_chat):
"""Editing rewinds and re-sends under the reader's own authority, and what a
machine reported is not theirs to rewrite."""
_add_connection(db)
chat_id = make_chat()
event = _machine_turn(db, chat_id)
page = client.get(f"/chat/{chat_id}").text
assert f"/messages/{event.id}/edit" not in page
def test_a_machine_turn_cannot_be_edited(client: TestClient, db, registered, make_chat):
"""The hidden button is a courtesy; the route is the rule."""
_add_connection(db)
chat_id = make_chat()
event = _machine_turn(db, chat_id)
assert client.get(f"/api/chats/{chat_id}/messages/{event.id}/edit").status_code == 404
assert (
client.post(
f"/api/chats/{chat_id}/messages/{event.id}/edit",
data={"content": "something I would rather it had said"},
).status_code
== 404
)
def test_a_machine_turn_keeps_its_output(client: TestClient, db, registered, make_chat):
"""The fenced log is most of why somebody reads one of these at all."""
_add_connection(db)
chat_id = make_chat()
_machine_turn(
db,
chat_id,
"[job abc] `pytest -q`\nIt finished successfully.\n\n```\n1529 passed\n```",
)
page = client.get(f"/chat/{chat_id}").text
assert "1529 passed" in page
def test_a_machine_turn_still_reaches_the_model_as_a_user_turn(db, registered, make_chat):
"""The wire role is load-bearing: `_inject` sends a queued turn verbatim and
every template requires the first non-system message to be `user`. `machine`
changes the bubble and nothing else."""
from lembas.db.models import Chat
from lembas.services import chat as chat_service
chat_id = make_chat()
_machine_turn(db, chat_id, "a job finished")
sent = chat_service.build_messages(db, db.get(Chat, chat_id))
assert [(m["role"], m["content"]) for m in sent] == [("user", "a job finished")]
def test_an_ordinary_turn_is_not_a_machine_event(db, registered, make_chat):
"""Every row written before the column reads the same way, because
`sync_schema` adds a NOT NULL boolean with a literal default of 0."""
from lembas.db.models import Chat
from lembas.services import chat as chat_service
chat_id = make_chat()
chat = db.get(Chat, chat_id)
assert chat_service.create_message(db, chat, "user", "hello").machine is False
# --- Background generation ---------------------------------------------------
def test_sending_launches_the_generation_immediately(
client: TestClient, db, registered, make_chat