Skip to content

The agent that grows with you

fix(desktop): a turn Desktop sends into a messaging session lights the unread dot

yoniebans4b12ad037e79
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
index a03b4706b14..4378ddc607b 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
@@ -17,6 +17,9 @@ import { clearAllPrompts } from '@/store/prompts'
import { providerWaitText, setSessionProviderWait } from '@/store/provider-wait'
import { setCurrentUsage, setTurnStartedAt } from '@/store/session'
import { refreshSupportedSessionControlAfterTurn } from '@/store/session-control'
+import { $focusedStoredSessionId } from '@/store/session-focus'
+import { $sessionStates } from '@/store/session-states'
+import { lightDesktopMessagingCompletion } from '@/store/session-unread'
import { pruneFinishedSessionSubagents } from '@/store/subagents'
import { clearActiveSessionTodos } from '@/store/todos'
@@ -115,6 +118,12 @@ function appendMoaReference(ctx: GatewayEventContext, sessionId: string): void {
/** The message/reasoning/MoA streaming family: message.start → deltas →
* interim → complete, thinking/reasoning deltas, moa.* progress, reaction. */
+/** The stamped completion of a messaging turn Desktop sent decides its unread dot. */
+function lightOwnMessagingTurn(runtimeId: string, admissionId: string | undefined) {
+ const storedId = $sessionStates.get()[runtimeId]?.storedSessionId ?? runtimeId
+ lightDesktopMessagingCompletion(storedId, admissionId, storedId === $focusedStoredSessionId.get())
+}
+
export function handleMessageStreamEvent(ctx: GatewayEventContext): boolean {
const { deps, event, payload, sessionId, isActiveEvent, occurredAt } = ctx
@@ -373,6 +382,7 @@ export function handleMessageStreamEvent(ctx: GatewayEventContext): boolean {
// Keyed by session so only one window beeps when several are open.
playCompletionSound(sessionId)
+ lightOwnMessagingTurn(sessionId, event.admission_id)
const finalText = coerceGatewayText(payload?.text) || coerceGatewayText(payload?.rendered)
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index df0fc7b7c36..580d6fad7d9 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
@@ -41,6 +41,7 @@ import {
} from '@/store/session'
import { profileScopeForSessionOwner } from '@/store/session-request-router'
import { $sessionStates, $sessionTiles } from '@/store/session-states'
+import { noteDesktopAdmission } from '@/store/session-unread'
import type { SessionInfo } from '@/types/hermes'
import { cronRunWriteGateApplies, refreshCronRunWriteGate } from '../../../cron/open-cron-run'
@@ -212,6 +213,19 @@ function isPreAdmissionRefusal(error: unknown): boolean {
return code === 4094 || (code === 4000 && typeof message === 'string' && /submission_id/.test(message))
}
+/** Track an admitted submission, and note its admission as this Desktop's own turn when one will run. */
+function trackAdmittedSubmission(
+ storedId: string,
+ entry: Parameters<typeof trackPendingSubmission>[1],
+ admissionId?: string
+) {
+ trackPendingSubmission(storedId, entry)
+
+ if (entry.status === 'queued' || entry.status === 'started') {
+ noteDesktopAdmission(admissionId)
+ }
+}
+
export function useSubmitPrompt(deps: SubmitPromptDeps) {
const {
activeSessionIdRef,
@@ -1235,12 +1249,11 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
}
if ((result?.submission_id ?? result?.admission_id) === submissionId) {
- trackPendingSubmission(targetStoredSessionId ?? liveSessionId, {
- id: submissionId,
- text,
- displayText: options?.displayText,
- status: result.status
- })
+ trackAdmittedSubmission(
+ targetStoredSessionId ?? liveSessionId,
+ { id: submissionId, text, displayText: options?.displayText, status: result.status },
+ result.admission_id
+ )
// Queued is also the initial receipt for an idle session's first
// turn. Keep its input and any start event that raced this ACK;
diff --git a/apps/desktop/src/store/session-unread.ts b/apps/desktop/src/store/session-unread.ts
index a36147fe6ed..7c040d20d5b 100644
--- a/apps/desktop/src/store/session-unread.ts
+++ b/apps/desktop/src/store/session-unread.ts
@@ -206,8 +206,49 @@ function setMarkerBucket(profile: string, ids: readonly string[]): void {
$unreadFinishedMarkers.set(next)
}
+/** Admission ids of turns this Desktop submitted, oldest first, until their
+ * stamped completion consumes them. Per turn, never per session; bounded so ids
+ * of turns that never complete here cannot accumulate. */
+const desktopAdmissions = new Set<string>()
+const DESKTOP_ADMISSIONS_CAP = 256
+
+/** Desktop's own submit was admitted as `admissionId`: that turn's completion is the user's. */
+export function noteDesktopAdmission(admissionId: string | undefined): void {
+ if (!admissionId) {
+ return
+ }
+
+ desktopAdmissions.add(admissionId)
+
+ for (const oldest of desktopAdmissions) {
+ if (desktopAdmissions.size <= DESKTOP_ADMISSIONS_CAP) {
+ break
+ }
+
+ desktopAdmissions.delete(oldest)
+ }
+}
+
+/** A messaging turn Desktop submitted has completed (`admissionId`, the owner's
+ * stamp on its `message.complete`): mark it unread unless it is on screen. The
+ * busy-to-idle edge never lights messaging sessions; this stamped completion is
+ * the one place their unread is decided, so no settle path can misattribute it. */
+export function lightDesktopMessagingCompletion(
+ storedSessionId: string,
+ admissionId: string | undefined,
+ focused: boolean
+) {
+ if (!admissionId || !desktopAdmissions.delete(admissionId) || focused) {
+ return
+ }
+
+ if (isMessagingSource(resolveLoadedRow(storedSessionId)?.source)) {
+ markSessionUnreadFinished(storedSessionId)
+ }
+}
+
/** A messaging conversation delivers its completion to its own platform; a
- * Desktop observer is not an unread recipient of it. */
+ * Desktop observer is not an unread recipient of it at the busy-to-idle edge. */
export function isMessagingCompletion(storedSessionId: string, profile?: string): boolean {
const row = profile
? rowsFor([$sessions.get(), $cronSessions.get(), $messagingSessions.get()]).find(
diff --git a/apps/shared/src/gateway-events.ts b/apps/shared/src/gateway-events.ts
index 1cc23c4e008..10c32e3264a 100644
--- a/apps/shared/src/gateway-events.ts
+++ b/apps/shared/src/gateway-events.ts
@@ -68,6 +68,8 @@ export interface GatewayEvent<K extends GatewayEventName = GatewayEventName> {
* generation, spread onto the params beside `type`/`payload` (`gateway/session_events.py`). */
authority_epoch?: number
execution_generation?: number
+ /** The owner's admission id for the turn the frame belongs to (same stamp). */
+ admission_id?: string
/** Session-scoped replay generation on canonical gateways. */
replay_epoch?: string
payload?: GatewayEventMap[K]