The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): owner holds + $projectTree leaf out of session-states, breaking the projects cycle
Austin Pickett73f7625c592c
diff --git a/apps/desktop/src/store/project-tree.ts b/apps/desktop/src/store/project-tree.tsnew file mode 100644index 00000000000..9053969721f--- /dev/null+++ b/apps/desktop/src/store/project-tree.ts@@ -0,0 +1,11 @@+import { atom } from 'nanostores'++import type { SidebarProjectTree } from '@/app/chat/sidebar/projects/workspace-groups'++// The project tree atom lives in this leaf so store/session-states.ts can+// read it (a focused tile's session is often older than the paginated recents+// page, so the tree is the only loaded copy of its row, #76535) without+// pulling store/projects.ts — and through it store/gateway.ts — into every+// suite that partially mocks the gateway. store/projects.ts owns all writes+// and re-exports the atom so existing call sites keep their import path.+export const $projectTree = atom<SidebarProjectTree[]>([])diff --git a/apps/desktop/src/store/projects.ts b/apps/desktop/src/store/projects.tsindex d3f4145b8b3..f765e1d995e 100644--- a/apps/desktop/src/store/projects.ts+++ b/apps/desktop/src/store/projects.ts@@ -63,13 +63,17 @@ import { recordFeatureUse } from './desktop-metrics'// membership; these atoms are the renderer's cached view.export const $projects = atom<ProjectInfo[]>([])++// The atom itself lives in ./project-tree (a leaf); re-exported here so every+// existing import path keeps working — projects.ts remains its only writer.+import { $projectTree } from './project-tree'+export { $projectTree }export const $activeProjectId = atom<null | string>(null)// The authoritative project -> repo -> lane tree (overview), served by// `projects.tree`. Lanes carry counts + structure; per-project session rows are// fetched lazily on drill-in via `fetchProjectSessions`. This is the single// source of project membership — the desktop no longer derives it.-export const $projectTree = atom<SidebarProjectTree[]>([])export const $projectTreeLoading = atom(false)// Backend-resolved session -> project owner, the ONE authority the row// classifiers (filter, bucket, color, label) and the lane overlay share, so adiff --git a/apps/desktop/src/store/session-owner-holds.ts b/apps/desktop/src/store/session-owner-holds.tsnew file mode 100644index 00000000000..d5ee789e199--- /dev/null+++ b/apps/desktop/src/store/session-owner-holds.ts@@ -0,0 +1,121 @@+import { registryBackendScopeKey } from '@hermes/shared'+import { atom } from 'nanostores'++import { normalizeProfileKey } from './profile'+import type { SessionOwnerScope } from './session-request-router'++// ── Owner hold across the create → foreground gap ───────────────────────────+// A routed session.create returns a stored id on the owner's socket, but the+// surface that will PIN that socket (the selected primary thread, or a tile)+// is published later and asynchronously: navigate → route effect →+// $selectedStoredSessionId, or openSessionTile → $sessionTiles. In that gap+// the entry has no active request, is not yet foreground-bound and, if the+// user switched source meanwhile, is not the active key either — so the+// live-work pruner or a refcount-0 lease release could close the socket that+// holds the just-minted runtime before the first prompt.submit. The hold+// names the owner in foregroundSessionScopes from the moment the create+// returns until the foreground publication takes over (the stored id becomes+// selected or tiled), the caller releases it (failed create / drift close),+// or a bounded TTL expires — nothing latches.+const SESSION_OWNER_HOLD_TTL_MS = 60_000++const sessionOwnerHolds = new Map<+ string,+ { owner: SessionOwnerScope; timer: ReturnType<typeof setTimeout>; until: number }+>()++export const $sessionOwnerHoldRevision = atom(0)++function bumpSessionOwnerHoldRevision(): void {+ $sessionOwnerHoldRevision.set($sessionOwnerHoldRevision.get() + 1)+}++function forgetSessionOwnerHold(storedSessionId: string, publish: boolean): boolean {+ const hold = sessionOwnerHolds.get(storedSessionId)++ if (!hold) {+ return false+ }++ clearTimeout(hold.timer)+ sessionOwnerHolds.delete(storedSessionId)++ if (publish) {+ bumpSessionOwnerHoldRevision()+ }++ return true+}++export function holdSessionOwnerUntilForeground(storedSessionId: string, owner: SessionOwnerScope): () => void {+ const id = storedSessionId.trim()++ if (!id || !owner) {+ return () => undefined+ }++ forgetSessionOwnerHold(id, false)+ const until = Date.now() + SESSION_OWNER_HOLD_TTL_MS+ const timer = setTimeout(() => releaseSessionOwnerHold(id), SESSION_OWNER_HOLD_TTL_MS)++ sessionOwnerHolds.set(id, { owner, timer, until })+ bumpSessionOwnerHoldRevision()++ return () => releaseSessionOwnerHold(id)+}++export function releaseSessionOwnerHold(storedSessionId: string): void {+ forgetSessionOwnerHold(storedSessionId.trim(), true)+}++/** @internal Tests. */+export function _resetSessionOwnerHoldsForTests(): void {+ const hadHolds = sessionOwnerHolds.size > 0++ for (const hold of sessionOwnerHolds.values()) {+ clearTimeout(hold.timer)+ }++ sessionOwnerHolds.clear()++ if (hadHolds) {+ bumpSessionOwnerHoldRevision()+ }+}++/** Retire every hold whose scope `known` already covers, plus every expired+ * one, and return the scopes the surviving holds still name. Called by+ * foregroundSessionScopes (session-states.ts) as its create → foreground+ * rung; a hold already covered by the publication (or past its TTL) retires+ * without a recursive revision publish. */+export function sweepSessionOwnerHolds(known: Set<string>): Set<string> {+ const scopes = new Set<string>()+ const now = Date.now()++ for (const [storedSessionId, hold] of [...sessionOwnerHolds]) {+ const scope = holdScope(hold.owner)++ if (!scope || hold.until <= now || known.has(scope)) {+ // This recompute was already triggered by the covering publication (or+ // is itself observing expiry), so avoid recursively publishing.+ forgetSessionOwnerHold(storedSessionId, false)++ continue+ }++ scopes.add(scope)+ }++ return scopes+}++/** The backend scope key a hold's owner names, when it names one. */+export function holdScope(owner: SessionOwnerScope): string | null {+ if (typeof owner === 'string') {+ return normalizeProfileKey(owner) || null+ }++ const connectionId = owner?.connectionId?.trim()++ return connectionId ? registryBackendScopeKey(connectionId, normalizeProfileKey(owner?.profile ?? '')) : null+}diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.tsindex 4b3b960226c..ee144a11994 100644--- a/apps/desktop/src/store/session-states.ts+++ b/apps/desktop/src/store/session-states.ts@@ -54,7 +54,7 @@ import {import { forgetPendingRuntimeTabs } from './preview-ownership'import { dropPreviewArtifactsForProfile, migratePreviewArtifactsForProfile } from './preview-status'import { $activeGatewayProfile, normalizeProfileKey } from './profile'-import { $projectTree } from './projects'+import { $projectTree } from './project-tree'import { clearAllProviderWaits, clearSessionProviderWait } from './provider-wait'import {$activeSessionId,@@ -216,84 +216,17 @@ export function liveSessionScopes(): Set<string> {return scopes}-// ── Owner hold across the create → foreground gap ───────────────────────────-// A routed session.create returns a stored id on the owner's socket, but the-// surface that will PIN that socket (the selected primary thread, or a tile)-// is published later and asynchronously: navigate → route effect →-// $selectedStoredSessionId, or openSessionTile → $sessionTiles. In that gap-// the entry has no active request, is not yet foreground-bound and, if the-// user switched source meanwhile, is not the active key either — so the-// live-work pruner or a refcount-0 lease release could close the socket that-// holds the just-minted runtime before the first prompt.submit. The hold-// names the owner in foregroundSessionScopes from the moment the create-// returns until the foreground publication takes over (the stored id becomes-// selected or tiled), the caller releases it (failed create / drift close),-// or a bounded TTL expires — nothing latches.-const SESSION_OWNER_HOLD_TTL_MS = 60_000--const sessionOwnerHolds = new Map<- string,- { owner: SessionOwnerScope; timer: ReturnType<typeof setTimeout>; until: number }->()--export const $sessionOwnerHoldRevision = atom(0)--function bumpSessionOwnerHoldRevision(): void {- $sessionOwnerHoldRevision.set($sessionOwnerHoldRevision.get() + 1)-}--function forgetSessionOwnerHold(storedSessionId: string, publish: boolean): boolean {- const hold = sessionOwnerHolds.get(storedSessionId)-- if (!hold) {- return false- }-- clearTimeout(hold.timer)- sessionOwnerHolds.delete(storedSessionId)-- if (publish) {- bumpSessionOwnerHoldRevision()- }-- return true-}--export function holdSessionOwnerUntilForeground(storedSessionId: string, owner: SessionOwnerScope): () => void {- const id = storedSessionId.trim()-- if (!id || !owner) {- return () => undefined- }-- forgetSessionOwnerHold(id, false)- const until = Date.now() + SESSION_OWNER_HOLD_TTL_MS- const timer = setTimeout(() => releaseSessionOwnerHold(id), SESSION_OWNER_HOLD_TTL_MS)-- sessionOwnerHolds.set(id, { owner, timer, until })- bumpSessionOwnerHoldRevision()-- return () => releaseSessionOwnerHold(id)-}--export function releaseSessionOwnerHold(storedSessionId: string): void {- forgetSessionOwnerHold(storedSessionId.trim(), true)-}--/** @internal Tests. */-export function _resetSessionOwnerHoldsForTests(): void {- const hadHolds = sessionOwnerHolds.size > 0-- for (const hold of sessionOwnerHolds.values()) {- clearTimeout(hold.timer)- }-- sessionOwnerHolds.clear()-- if (hadHolds) {- bumpSessionOwnerHoldRevision()- }-}+// The owner hold itself (the map, TTL, hold/release/reset, and the+// foreground-scope sweep) lives in ./session-owner-holds; re-exported here+// because session-states.ts is where the rest of the ownership ladder lives+// and most call sites already import from it.+export {+ $sessionOwnerHoldRevision,+ _resetSessionOwnerHoldsForTests,+ holdSessionOwnerUntilForeground,+ releaseSessionOwnerHold+} from './session-owner-holds'+import { sweepSessionOwnerHolds } from './session-owner-holds'/*** Registry scopes owned by an open foreground surface, when known.@@ -370,24 +303,7 @@ export function foregroundSessionScopes(): Set<string> {// Create → foreground holds. A hold whose scope the rungs above already// name (the runtime's event scope once selected, a mounted tile's route) is// covered and retires; an expired one retires too.- const now = Date.now()-- for (const [storedSessionId, hold] of [...sessionOwnerHolds]) {- const scope =- typeof hold.owner === 'string'- ? normalizeProfileKey(hold.owner)- : hold.owner?.connectionId?.trim()- ? registryBackendScopeKey(hold.owner.connectionId.trim(), normalizeProfileKey(hold.owner.profile))- : null-- if (!scope || hold.until <= now || scopes.has(scope)) {- // This recompute was already triggered by the covering publication (or- // is itself observing expiry), so avoid recursively publishing.- forgetSessionOwnerHold(storedSessionId, false)-- continue- }-+ for (const scope of sweepSessionOwnerHolds(scopes)) {scopes.add(scope)}