Skip to content

feat(workspace): an unread reply shows on the agent's row and in the tab title (ent#557) - #2648

Merged
vybe merged 5 commits into
devfrom
feat/ent557-workspace-unread
Sep 11, 2026
Merged

vybe merged 5 commits into
devfrom
feat/ent557-workspace-unread

Conversation

@dolho

@dolho dolho commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

An agent replied while the operator was elsewhere and nothing indicated it.

Most of the machinery already existed — the per-agent pill, the per-chat badge, the aggregate in the wordmark. Three things were missing, and the first one is the interesting one.

1. A chat you have never opened could not be unread

ent#359 defined unread relative to a read cursor and gave a cursorless thread a count of nothing — correctly: inventing a cursor at the beginning of time would have badged every historical conversation in every install on the day it shipped.

What it could not foresee is ent#523, which made Main the landing place for everything an agent starts — agent-initiated messages, asks raised outside a chat, scheduled briefs all resolve to it. A freshly minted Main has never been read by anyone, so the one case an unread badge exists for produced no badge anywhere.

A cursorless thread now counts agent messages newer than the viewer's account baseline: anything an agent has said to you since the first time you read anything here, in a chat you have never opened.

The baseline is stored, not derived — and that is the whole design

Both obvious derivations were tried and both fail the same test, because both move:

derivation reads as why it is wrong
MAX(last_read_at) "since you were last here" Advances every time the viewer reads anything, so an unread reply in a chat they have not opened is silently cleared by reading a different chat.
MIN(last_read_at) "since the first time you read anything" Looks stable and is not: mark_chat_read UPDATES the row it advances, so a viewer with one chat has MIN == MAX and inherits the identical bug.

MIN was implemented first. test_reading_one_chat_does_not_silently_clear_another failed it — which is why the stored row exists, and why that test is the centre of this PR rather than a footnote. Trading "no badge" for "a badge that another chat can silently clear" would have been a worse bug than the one reported.

The baseline is a row in the chat-state table under a reserved kind, written on the viewer's first ever mark_chat_read and never moved. Every read of that table excludes it, so it reaches neither the sidebar payload (as a phantom chat) nor either row cap. A viewer with no baseline still counts nothing — ent#359's property preserved rather than traded away, and it falls out of SQL's NULL comparison rather than a second branch.

2. It only appeared when you did something

refreshThreads() is event-driven — a send, a navigation, a turn finishing — so an agent-initiated reply reached the sidebar on the viewer's next action and not before, which is the same as never for someone in another tab. It now also runs on the ent#364 asks poll (20 s, visibility-aware).

Folded into that timer rather than given its own: the Workspace has no WebSocket a portal client is on (operator_queue_new is broadcast on the platform /ws), and a second cadence for one badge is a second thing to reason about. Since #2198 the thread half is one request for every agent rather than one per agent, which is what makes it cheap enough to ride there.

3. The tab said nothing

While anything is unread the title carries a compact prefix, returning to the plain title once everything is read and cleared when the Workspace unmounts — a count that outlives the surface leaves a tab reading (3) on a page with nothing to click.

utils/tabTitle.js owns the string because document.title now has two writers on independent schedules — the router's label (#1418) and the count. With both assigning directly, the last to fire erases the other's half: a navigation drops the count, a count update drops the label. Neither assigns now; both call in and the module renders the whole string. The count is a prefix on whatever the router computed, which is what lets ent#556 change the label without touching any of this, and it leads rather than trails because browsers truncate a tab from the right — a suffix would be the first thing to disappear on the tab that most needs it.

Acceptance criteria

  • A reply you have not seen shows on the agent's row, including in a never-opened chat; the rule is stated in the flow doc (with the two rejected derivations, so the next reader does not re-try them).
  • It appears while you are elsewhere, without a manual refresh — on the existing poll, the ent#532 shape.
  • Ask and unread stay distinct; nothing sums them (asserted).
  • The tab shows a compact marker while unread and returns to the plain title; it composes with the router's label rather than replacing it.
  • Reading clears it immediately — markRead already cleared optimistically and re-decorated threads, so the total recomputes synchronously and the tab follows with no reload.
  • Honest counts: the tab reads the same totalUnread(threads) the sidebar does, so every unit is a row the user can click; 99+ caps; zero renders nothing.
  • Design review with screenshots — needs a human, see below.
  • Both flow docs updated.

The one AC I could not complete

AC 7 asks for screenshots of the sidebar row and the tab title in light and dark at 1 / several / 99+, reviewed against the design system. I have no browser and the local stack is down; rendering the Workspace needs a live backend plus a signed-in portal session with unread state. I have not faked it.

What I can give without a browser — the exact strings, from the shipped pure function:

   0  |  "Trinity — Workspace"
   1  |  "(1) Trinity — Workspace"
   3  |  "(3) Trinity — Workspace"
  12  |  "(12) Trinity — Workspace"
  99  |  "(99) Trinity — Workspace"
 100  |  "(99+) Trinity — Workspace"
1500  |  "(99+) Trinity — Workspace"

truncated to 18 chars (a crowded tab bar):
   0  |  "Trinity — Workspac…"
   3  |  "(3) Trinity — Work…"
 100  |  "(99+) Trinity — Wo…"

The marker survives truncation at every count, which is the legibility half of that AC.

On the row half: this PR changes no sidebar markup at all. The badge, its cap and its token were already there (#2424 / ent#359); ent#557 only makes the number non-zero in a case where it was always zero. So "must not crowd the status chip or reflow it" is a property of markup already on dev. Worth knowing while reviewing: #2641 touches that same row — it stops the always-reserved 72px availability slot charging every row, which gives the badges more room, not less. No conflict, but they are best looked at together.

Verification

  • tests/unit/test_ent557_unread_never_opened_chat.py — 12 passed, against a throwaway sqlite carrying the real tables, so the SQL is the code under test rather than a mock: the agent-started Main case, a first-ever sign-in still counting nothing, a pre-baseline chat not retroactively unread, reading one chat not clearing another, the baseline frozen at the first read, the baseline row invisible to the sidebar and both caps, and cross-viewer isolation of the baseline subquery.
  • portalUnreadTabTitle.spec.js — 14 passed. The format is executed (prefix, cap, zero, non-numeric input, empty base) and the two-writer ordering is driven through the module's own API.
  • portalUnreadLiveness.spec.js — 10 passed.
  • ent#359's own 22 tests pass unchanged — the null-cursor property they were written for still holds.
  • Backend portal slice 587 passed; frontend 114 files / 2539 tests; both ratchets green; vite build clean.

Scope

OSS-core, deliberately ungated, per the standing Workspace ruling restated in the issue — so the code lands in the public repo with no requires_entitlement and no gated Vue.

Fixes abilityai/trinity-enterprise#557

🤖 Generated with Claude Code

https://claude.ai/code/session_01Bd71qsYbFodvofba8P69eP

…tab title (ent#557)

An agent replied while the operator was elsewhere and nothing indicated it.
Most of the machinery already existed — the per-agent pill, the per-chat badge,
the aggregate in the wordmark. Three things were missing.

**1. A chat you have never opened could not be unread.** ent#359 defined unread
relative to a read cursor and gave a cursorless thread a count of nothing,
correctly: inventing a cursor at the beginning of time would have badged every
historical conversation in every install on the day it shipped. What it could
not foresee is ent#523, which made Main the landing place for everything an
AGENT starts. A freshly minted Main has never been read by anyone, so the one
case an unread badge exists for produced no badge anywhere.

A cursorless thread now counts agent messages newer than the viewer's account
baseline — "anything an agent has said to you since the first time you read
anything here, in a chat you have never opened".

The baseline is a STORED, write-once row, not a derived value, and that is the
whole of the design. Both obvious derivations were tried and both fail the same
test:

* MAX(last_read_at) — "since you were last here" — advances every time the
  viewer reads anything, so an unread reply in a chat they have not opened is
  silently cleared by reading a DIFFERENT chat.
* MIN(last_read_at) — "since the first time you read anything" — looks stable
  and is not: `mark_chat_read` UPDATES the row it advances, so a viewer with one
  chat has MIN == MAX and inherits the identical bug.

MIN was implemented first and `test_reading_one_chat_does_not_silently_clear_
another` failed it, which is why the row exists. It lives in the chat-state
table under a reserved kind, written on the first ever read and never moved;
every read of that table excludes it, so it reaches neither the sidebar payload
nor either row cap. A viewer with no baseline still counts nothing — ent#359's
property preserved, and it falls out of SQL's NULL comparison rather than a
second branch.

**2. It only appeared when you did something.** `refreshThreads()` is
event-driven — a send, a navigation, a turn finishing — so an agent-initiated
reply reached the sidebar on the viewer's next action and not before, which is
the same as never for someone in another tab. It now also runs on the ent#364
asks poll (20s, visibility-aware). Folded into that timer rather than given its
own: the Workspace has no WebSocket a portal client is on, and since #2198 the
thread half is ONE request for every agent, which is what makes it cheap enough
to ride there.

**3. The tab said nothing.** While anything is unread the title carries
`(3) Trinity — Workspace`, returning to the plain title when everything is read
and cleared when the Workspace unmounts. `utils/tabTitle.js` owns it because
`document.title` now has two writers on independent schedules — the router's
label and the count — and with both assigning directly the last to fire would
erase the other's half. Neither assigns; both call in. The count is a PREFIX on
whatever the router computed, which is what lets ent#556 change the label
without touching any of this, and it leads rather than trails because browsers
truncate a tab from the right.

Reading clears it with no reload: `markRead` already cleared optimistically and
re-decorated `threads`, so the total recomputes synchronously and the tab
follows. Asks and unread stay separate (#2424) — this adds nothing that sums
them.

Tests: `test_ent557_unread_never_opened_chat.py` (12, against a real sqlite so
the SQL is the code under test), `portalUnreadTabTitle.spec.js` (14, the format
executed and the two-writer ordering), `portalUnreadLiveness.spec.js` (10).
Backend portal slice 587 passed; frontend 114 files / 2539 tests; ratchets green;
build clean.

OSS-core, deliberately ungated, per the standing Workspace ruling restated in
the issue.

Related to Abilityai/trinity-enterprise#557

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bd71qsYbFodvofba8P69eP
@dolho
dolho requested a review from vybe September 9, 2026 13:22
@vybe

vybe commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

merge-train 2026-09-09: not on this train. The feature does not reach the API — verified end to end.

service.get_chat_state discards the counts the new SQL computes — src/backend/client_portal/service.py:4620-4632

The loop iterates db.get_chat_state(email) and reads unread.get(cid, 0) off rows that already exist. A cursorless thread has no enterprise_portal_chat_state row, so it never appears in rows and never gets emitted. count_unread_by_session is the only function this PR changed, and get_chat_state is its only caller.

Net effect: the reported case — an agent replies into a freshly minted Main the viewer has never opened — still produces no badge, no per-agent pill, no wordmark total, no tab-title marker. AC 1 and AC 2 are not delivered. The case the change does reach is starred but never opened, which is not the reported bug.

The comment at :4633-4636 argues against the fix using the SQL this PR replaced. It states that count_unread_by_session INNER JOINs and requires last_read_at IS NOT NULL — but the diff changes that to LEFT JOIN and drops the IS NOT NULL. It reads as protection that exists, and it is exactly what would stop the next reader from restoring the fallback loop. Same shape as the ent#523 entry in learnings.md.

Also worth fixing while it is open: no test crosses the db→service boundary (all 12 call pdb.* directly), which is why this was invisible; and docs/memory/feature-flows/workspace-sidebar-ia.md:396 now marks the limitation "Fixed by ent#557".

The stored-baseline design, the SQL and the two-writer tab title are all sound — this is one layer short of working. Rides the next train once the service layer emits the cursorless sessions.

…QL (ent#557)

`count_unread_by_session` was widened to LEFT JOIN and count a never-opened
thread against the account baseline — and `service.get_chat_state` then
discarded every one of those rows. It built the payload by iterating the
viewer's state ROWS and reading the unread map off them, so a thread with no
cursor (the entire ent#557 case: an agent replies into a freshly minted Main
nobody has opened) never appeared. No badge, no per-agent pill, no wordmark
total, no tab title. AC 1 and AC 2 were undelivered; the only case the change
did reach was "starred but never opened", which is not the reported bug.

The service now runs a second pass over the unread map and emits the thread ids
the row loop did not, de-duplicated against them so one chat cannot be counted
twice in the wordmark total, and `starred: false` by construction. It is bounded
by the same read — `count_unread_by_session` is scoped to the caller's own
`enterprise_portal_messages`, so it cannot append a chat that is not theirs.

The comment where the fallback used to be argued against restoring it by citing
the INNER JOIN and the `last_read_at IS NOT NULL` filter — both of which this
same PR removed. It read as protection that exists and would have stopped the
next reader from putting the pass back (the ent#523 entry in `learnings.md`).
Replaced with what is actually true now.

Tests: five cases crossing the db→service boundary, which is the boundary all
twelve existing tests skip by calling `client_portal.db` directly — which is why
this was invisible. `test_the_api_reports_a_chat_that_has_no_state_row` fails
against the pre-fix service and passes after.

Related to Abilityai/trinity-enterprise#557

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01RSjVEjay9ztC1oDXXkh9uN
@dolho

dolho commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in cc93e834. You were right that it was one layer short — the SQL was widened and the service threw the extra rows away.

service.get_chat_state now runs a second pass. The row loop reaches every chat that has a state row; count_unread_by_session is, since this PR, the wider of the two sets, so the ids it returns that the loop did not emit are appended as {"kind": "thread", "starred": false}. De-duplicated against the first pass (two entries for one id would double the wordmark total) and bounded by the same read — that query is scoped to the caller's own enterprise_portal_messages, so it cannot append a chat that is not theirs. The account baseline is still excluded one layer down, so no phantom account/baseline chat.

The comment at :4633 is gone. It argued against the fallback using the INNER JOIN and the IS NOT NULL filter that this same PR replaced — exactly the ent#523 shape you named. Replaced with what is true after the change.

Tests now cross the db→service boundary. Five cases in a new section of test_ent557_unread_never_opened_chat.py, calling service.get_chat_state rather than pdb.*: the cursorless thread reaching the payload, a chat with a row emitted exactly once, a star surviving while gaining a count, the baseline never appearing as a chat, and a first-ever viewer still getting nothing. test_the_api_reports_a_chat_that_has_no_state_row fails against the pre-fix service and passes after — verified by stashing the service change and re-running.

39 passed on test_ent557_* + test_ent359_*; 534 passed, 2 skipped across every portal/chat-state test in the suite.

workspace-sidebar-ia.md gains the two-pass paragraph, so the written record says why the SQL alone delivers nothing. The Fixed by ent#557 limitation row is now true.

@dolho

dolho commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

Re-review after the fix. No blockers. Two observations, both recorded rather than objected to.

Re-read clean: the second pass in service.get_chat_state is de-duplicated against the row loop (seen_threads), skips n <= 0, and is scoped by count_unread_by_session's own client_email filter, so it cannot append a chat that is not the caller's. starred: False is right by construction — a thread with no state row has never been starred. The account baseline is excluded one layer down (db.get_chat_state filters chat_kind != BASELINE_KIND), so it cannot surface as a phantom chat, and there is a test for exactly that.

1. The payload is no longer bounded by MAX_CHAT_STATE_ROWS (1000). The first pass is; the second is bounded only by how many of the viewer's threads have unread agent messages. That is not a regression in order-of-magnitude — since #2198 the sidebar already returns every thread the viewer has in one call, so the payload was O(threads) before this — but the cap is now load-bearing for one half of the list and not the other, and the comment above _would_create_row_past_cap reads as though it bounds the whole thing. Worth a sentence there so the next reader does not infer a ceiling that only covers the first pass.

2. count_unread_by_session has no LIMIT and groups over the viewer's whole message history. Also pre-existing, and it rides the same request the sidebar already makes. Flagging it only because this change makes it the wider of the two reads — it is now the one that decides the payload's size, so if the thread count ever becomes the scaling problem, this is where it will show up first.

Both are for a follow-up at most. The delivery half is correct now and the db→service tests are the right place to have put the proof.

…y clear (ent#557)

Found re-reviewing my own fix. Emitting cursorless threads was the right
correction; it interacts badly with a cap I did not check.

`service.mark_chat_read` silently no-ops when the row would be a NEW one and the
viewer is at `MAX_CHAT_STATE_ROWS` (1000) — deliberately, on the stated ground
that "a read marker is incidental to what the user asked for (opening a chat)".
A cursorless thread is by definition a new row.

So ent#557 made that no-op load-bearing. Before it, a cursorless thread produced
no badge and the no-op was invisible, which is what made the justification true.
After it, a capped viewer gets a badge on the wordmark total, the per-agent pill
AND the browser tab title, and opening the chat cannot dismiss it — a permanent
unread marker with no user action that clears it.

The second pass is now gated on there being room. Not shown beats
shown-and-stuck: a capped viewer degrades to exactly the ent#359 behaviour,
which is the state they were in before this feature, rather than to a badge that
never goes away.

Three properties, each deliberate:

* the gate applies to the CURSORLESS pass only. A thread that already has a row
  can always be marked read (`_would_create_row_past_cap` allows updating a row
  the caller already owns), so capping its badge would hide unread the viewer
  can perfectly well clear;
* it fails OPEN — an unreadable count reports room, because refusing to show
  unread on a COUNT that could not be taken would hide real unread from every
  viewer on a transient DB error, and the write path is what enforces the cap;
* the COUNT is paid only when the pass would emit something, i.e. never on the
  ordinary sidebar load where every thread already has a row. Asserted, so it
  stays true.

Tests: the capped viewer (badge withheld, and the no-op demonstrated rather than
assumed — `mark_chat_read` is called and the count does not move), the
room-left case clearing to zero, a row-bearing thread still badging AT the cap,
the fail-open path, and the no-COUNT-on-ordinary-load cost note. Removing the
gate reds the first.

527 passed across portal / chat-state.

Related to Abilityai/trinity-enterprise#557

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01RSjVEjay9ztC1oDXXkh9uN
@dolho

dolho commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

Deeper self re-review — found a regression my own fix introduced. Fixed in 1b70a3c3.

Emitting the cursorless threads was the right correction. It interacts badly with a cap I did not check.

service.mark_chat_read silently no-ops when the row would be a new one and the viewer is at MAX_CHAT_STATE_ROWS (1000):

if _would_create_row_past_cap(email, kind, cid):
    # Silently no-op rather than erroring: a read marker is incidental to
    # what the user asked for (opening a chat) …
    return

A cursorless thread is, by definition, a new row. So ent#557 made that no-op load-bearing. Before it, a cursorless thread produced no badge and the no-op was invisible — which is exactly what made its justification true. After it, a capped viewer gets a badge on the wordmark total, the per-agent pill and the browser tab title, and opening the chat cannot dismiss it. A permanent unread marker with no user action that clears it, which is a worse experience than the bug this PR fixes.

Demonstrated rather than reasoned: the test calls svc.mark_chat_read at the cap and asserts count_unread_by_session does not move.

The second pass is now gated on there being room. Not shown beats shown-and-stuck — a capped viewer degrades to exactly the ent#359 behaviour, which is the state they were in before this feature, rather than to a badge that never goes away.

Three properties, each deliberate and each tested:

  • the gate applies to the cursorless pass only. A thread that already has a row can always be marked read (_would_create_row_past_cap allows updating a row the caller owns), so capping its badge would hide unread the viewer can perfectly well clear — asserted at a cap of 1;
  • it fails open. An unreadable count reports room: refusing to show unread because a COUNT could not be taken would hide real unread from every viewer on a transient DB error, and the write path is what actually enforces the cap;
  • the COUNT is paid only when the pass would emit something — never on the ordinary sidebar load, where every thread already has a row. Asserted so it stays true, since this rides bug: Agent Detail and Workspace over-fetch on load (21 redundant requests, N+1 roster) #2198's batch endpoint.

Removing the gate reds the capped-viewer case.

527 passed, 2 skipped across portal / chat-state. The earlier finding about the payload no longer being bounded by MAX_CHAT_STATE_ROWS now has a second-order answer: at the cap the extra entries are not emitted at all.

vybe and others added 2 commits September 11, 2026 10:24
…2648)

merge-train: mechanical, per the merge-train note on the PR. The two feature
flows were updated but the requirements entry still stated the ent#359 rule
this PR inverts for cursorless threads (RoE #1).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@vybe

vybe commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

merge-train 2026-09-10 (evening run): riding. Both 09-09 findings re-verified by execution, not by the description: the cursorless thread reaches service.get_chat_state's payload (test_the_api_reports_a_chat_that_has_no_state_row calls the service, not pdb.*), the second pass is de-duplicated against the first (ids.count("t-a") == 1), the cap gate holds (test_a_capped_viewer_is_not_shown_a_badge_they_cannot_clear), and the frontend half is live end to end — store → decorate() → sidebarThreads → per-agent pill / wordmark badge / PortalChatRow, and Portal.vue → setUnreadCount → tabTitle.js → document.title, with both writers calling in. 44/44 on the ent#557 + ent#359 suites, ratchets flat.

Two mechanical things pushed to your branch, both announced here so they are not a mystery commit:

  • 2c26b08e merges origin/dev (clean).
  • 860ca86a adds one bullet to docs/memory/requirements/core-agent.md §5.10. The two feature flows were updated but the requirements entry still read "a thread with no last_read_at reports nothing unread" — the exact half this PR inverts — and the requirements file is the stated source of truth (RoE Fix: Add missing Docker labels to system agent container #1). Wording is lifted from workspace-sidebar-ia.md; change it if it says something you did not mean.

The body's Related to abilityai/trinity-enterprise#557 is now Fixes abilityai/trinity-enterprise#557. Cross-tracker closes but does not relabel, so I will set status-in-dev on ent#557 by hand after the merge.

Non-blocking, for your judgement later: the tab total sums threads while the sidebar sums sidebarThreads (the is_main && !last_message_at filter); they agree today only because every assistant writer also touches the session, and a message landing between the two Promise.all responses can put a count on the tab that no visible row carries for one poll. totalUnread(sidebarThreads.value) would make the spec's "the SAME threads" claim true by construction.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: batch validated on train/20260911-0925 (#2701)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants