Skip to content

The agent that grows with you

fix(desktop): note Desktop's turns at the canonical client, by profile, in either order

yoniebans478738e6b3a8
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 4d47f6acb93..3f596f6ea06 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -9,6 +9,16 @@ export const canonicalProfile = (profile: unknown): string =>
export const canonicalSessionKey = (sessionId: unknown, profile?: unknown): string =>
JSON.stringify([canonicalProfile(profile), sessionId])
+const admissionObservers = new Set<(admissionId: string, status: string) => void>()
+
+/** Every `prompt.submit` this Desktop's canonical clients send (composer, edit and regenerate,
+ * Quick Entry, bots) reports its accepted admission here, whichever surface sent it. */
+export function observeCanonicalAdmissions(observer: (admissionId: string, status: string) => void): () => void {
+ admissionObservers.add(observer)
+
+ return () => admissionObservers.delete(observer)
+}
+
/** Live events and retained snapshots must describe the same answerable card. */
export function canonicalPromptParams(kind: unknown, sessionId: string, prompt: Record<string, unknown>): Record<string, unknown> {
const choices = Array.isArray(prompt.choices) ? prompt.choices : []
@@ -650,6 +660,8 @@ export class CanonicalDesktopProtocol {
throw new Error('Canonical admission receipt destination mismatch')
}
+ for (const observe of admissionObservers) { observe(value.admission_id, String(value.status ?? '')) }
+
return { ...value, session_id: value.ref.session_id, submission_id: params.submission_id ?? params.input_id }
}
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 4378ddc607b..f9739d12bd0 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
@@ -119,9 +119,9 @@ 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) {
+function lightOwnMessagingTurn(runtimeId: string, admissionId: string | undefined, profile?: string) {
const storedId = $sessionStates.get()[runtimeId]?.storedSessionId ?? runtimeId
- lightDesktopMessagingCompletion(storedId, admissionId, storedId === $focusedStoredSessionId.get())
+ lightDesktopMessagingCompletion(storedId, admissionId, storedId === $focusedStoredSessionId.get(), profile)
}
export function handleMessageStreamEvent(ctx: GatewayEventContext): boolean {
@@ -382,7 +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)
+ lightOwnMessagingTurn(sessionId, event.admission_id, event.profile)
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 580d6fad7d9..df0fc7b7c36 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,7 +41,6 @@ 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'
@@ -213,19 +212,6 @@ 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,
@@ -1249,11 +1235,12 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
}
if ((result?.submission_id ?? result?.admission_id) === submissionId) {
- trackAdmittedSubmission(
- targetStoredSessionId ?? liveSessionId,
- { id: submissionId, text, displayText: options?.displayText, status: result.status },
- result.admission_id
- )
+ trackPendingSubmission(targetStoredSessionId ?? liveSessionId, {
+ id: submissionId,
+ text,
+ displayText: options?.displayText,
+ status: result.status
+ })
// 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 7c040d20d5b..3b1917b30a4 100644
--- a/apps/desktop/src/store/session-unread.ts
+++ b/apps/desktop/src/store/session-unread.ts
@@ -1,3 +1,4 @@
+import { observeCanonicalAdmissions } from '@/api/canonical-protocol'
import { Codecs, persistentAtom } from '@/lib/persisted'
import { isMessagingSource } from '@/lib/session-source'
import { stableArray } from '@/lib/stable-array'
@@ -219,14 +220,7 @@ export function noteDesktopAdmission(admissionId: string | undefined): void {
}
desktopAdmissions.add(admissionId)
-
- for (const oldest of desktopAdmissions) {
- if (desktopAdmissions.size <= DESKTOP_ADMISSIONS_CAP) {
- break
- }
-
- desktopAdmissions.delete(oldest)
- }
+ trimOldest(desktopAdmissions)
}
/** A messaging turn Desktop submitted has completed (`admissionId`, the owner's
@@ -236,27 +230,72 @@ export function noteDesktopAdmission(admissionId: string | undefined): void {
export function lightDesktopMessagingCompletion(
storedSessionId: string,
admissionId: string | undefined,
- focused: boolean
+ focused: boolean,
+ profile?: string
) {
- if (!admissionId || !desktopAdmissions.delete(admissionId) || focused) {
+ if (!admissionId) {
+ return
+ }
+
+ if (!desktopAdmissions.delete(admissionId)) {
+ // Its receipt may still be in flight: keep the completion until the admission is noted.
+ earlyCompletions.set(admissionId, { storedSessionId, focused, profile })
+ trimOldest(earlyCompletions)
+
return
}
- if (isMessagingSource(resolveLoadedRow(storedSessionId)?.source)) {
- markSessionUnreadFinished(storedSessionId)
+ // The delivering socket's profile, when it names one, picks the row: another profile's same-id
+ // session must not decide this completion. The row's own id is marked, so a compressed
+ // conversation's listed segment paints at once.
+ const row = resolveOwnedRow(storedSessionId, profile)
+
+ if (!focused && isMessagingSource(row?.source)) {
+ markSessionUnreadFinished(row!.id, profile)
}
}
-/** A messaging conversation delivers its completion to its own platform; a
- * 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
+/** Stamped completions that arrived before their admission was noted, keyed by admission. */
+const earlyCompletions = new Map<string, { storedSessionId: string; focused: boolean; profile?: string }>()
+
+function trimOldest(entries: Set<string> | Map<string, unknown>) {
+ for (const oldest of entries.keys()) {
+ if (entries.size <= DESKTOP_ADMISSIONS_CAP) {
+ break
+ }
+
+ entries.delete(oldest)
+ }
+}
+
+// A turn will run for a queued or started admission; a deduplicated or lost one starts none.
+observeCanonicalAdmissions((admissionId, status) => {
+ if (status !== 'queued' && status !== 'started') {
+ return
+ }
+
+ noteDesktopAdmission(admissionId)
+ const early = earlyCompletions.get(admissionId)
+
+ if (early) {
+ earlyCompletions.delete(admissionId)
+ lightDesktopMessagingCompletion(early.storedSessionId, admissionId, early.focused, early.profile)
+ }
+})
+
+/** The row of a known owner profile; with no owner, the bare-id tie-break. */
+function resolveOwnedRow(storedSessionId: string, profile?: string): SessionInfo | undefined {
+ return profile
? rowsFor([$sessions.get(), $cronSessions.get(), $messagingSessions.get()]).find(
r => sessionMatchesStoredId(r, storedSessionId) && profileKeyForRow(r) === normalizeProfileKey(profile)
)
: resolveLoadedRow(storedSessionId)
+}
- return isMessagingSource(row?.source)
+/** A messaging conversation delivers its completion to its own platform; a
+ * Desktop observer is not an unread recipient of it at the busy-to-idle edge. */
+export function isMessagingCompletion(storedSessionId: string, profile?: string): boolean {
+ return isMessagingSource(resolveOwnedRow(storedSessionId, profile)?.source)
}
/** UNREAD WRITER — the live busy→idle edge (session-states.ts) and any surface