Skip to content

The agent that grows with you

fix(desktop): host.sessions.reorder writes live ids; add reorderPinned; move verbs to sdk/sessions.ts

teknium14f3426e7ca1b
diff --git a/apps/desktop/src/sdk/index.test.ts b/apps/desktop/src/sdk/index.test.ts
index cd166960512..796bb948e09 100644
--- a/apps/desktop/src/sdk/index.test.ts
+++ b/apps/desktop/src/sdk/index.test.ts
@@ -402,13 +402,21 @@ describe('host.sessions session-list mutations', () => {
expect($pinnedSessionIds.get()).toEqual(['root-9'])
})
- it('reorder persists the manual order the drag path writes', async () => {
+ it('reorder persists the manual order the drag path writes, in the LIVE id space', async () => {
const { $sidebarSessionOrderIds, $sidebarSessionOrderManual } = await import('@/store/layout')
+ const { $sessions } = await import('@/store/session')
+ const { makeSessionInfo } = await import('@/test/session-info')
+
+ // `c` was compressed: the row slot hands a plugin its durable root `c`,
+ // but the order store (and the sidebar's reconcile effect) key rows by
+ // the live id `c-tip`. Feeding the durable id back verbatim would drop
+ // the row from the order and flip the manual flag off on the next render.
+ $sessions.set([makeSessionInfo({ _lineage_root_id: 'c', id: 'c-tip' }), makeSessionInfo({ id: 'a' })])
host.sessions.reorder(['c', 'a', 'b'])
expect($sidebarSessionOrderManual.get()).toBe(true)
- expect($sidebarSessionOrderIds.get()).toEqual(['c', 'a', 'b'])
+ expect($sidebarSessionOrderIds.get()).toEqual(['c-tip', 'a', 'b'])
// Empty list = clear the manual order, back to the default sort.
host.sessions.reorder([])
@@ -417,6 +425,20 @@ describe('host.sessions session-list mutations', () => {
expect($sidebarSessionOrderIds.get()).toEqual([])
})
+ it('reorderPinned permutes the Pinned section through the same setter the drag uses', async () => {
+ const { $pinnedSessionIds } = await import('@/store/layout')
+ const { $sessions } = await import('@/store/session')
+ const { makeSessionInfo } = await import('@/test/session-info')
+
+ $sessions.set([makeSessionInfo({ _lineage_root_id: 'p1', id: 'p1-tip' })])
+ $pinnedSessionIds.set(['p1', 'p2', 'unloaded'])
+
+ // Durable ids (the slot's) and live ids both resolve; an unmentioned pin keeps its slot.
+ host.sessions.reorderPinned(['p2', 'p1-tip'])
+
+ expect($pinnedSessionIds.get()).toEqual(['p2', 'p1', 'unloaded'])
+ })
+
it('setColor writes the durable-keyed colour override and clears with null', async () => {
const { $sessionColorOverrides } = await import('@/store/session-color')
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index e2c5e06f990..29a05b2d922 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -60,12 +60,6 @@ import {
retireLocalProfileGateways,
type SpawnPriority
} from '@/store/gateway'
-import {
- pinSession,
- setSidebarSessionOrderIds,
- setSidebarSessionOrderManual,
- unpinSession
-} from '@/store/layout'
import { notify, notifyError } from '@/store/notifications'
import {
$activeGatewayProfile,
@@ -96,11 +90,9 @@ import {
rememberedSessionProfile,
requestSessionResume,
sessionMatchesStoredId,
- sessionPinId,
setResumeExhaustedSessionId,
setSessionOwnerHint
} from '@/store/session'
-import { setSessionColorOverride } from '@/store/session-color'
import {
$focusedRuntimeId,
$focusedSessionState,
@@ -116,6 +108,7 @@ import type { PaginatedSessions, UsageStats } from '@/types/hermes'
import { composerHost } from './composer'
import { planPluginOpenSession } from './plugin-open-session-plan'
+import { sessionsHost } from './sessions'
import { desktopSettings } from './settings'
export type { DesktopSettingKey, DesktopSettingValues } from './settings'
@@ -655,17 +648,6 @@ async function awaitProfileActivation(
// by mutation: removing it changed no test outcome.
}
-/** Pins and colours are keyed by the DURABLE (lineage-root) id so they survive
- * compression's session-id rotation; a row's live id resolves through
- * `$sessions` (the app's own lineage matcher), and an id that resolves to
- * nothing is passed through as-is (the stores tolerate ids for rows this
- * window hasn't loaded). */
-function durableSessionPinId(storedSessionId: string): string {
- const session = $sessions.get().find(s => sessionMatchesStoredId(s, storedSessionId))
-
- return session ? sessionPinId(session) : storedSessionId
-}
-
export const host = {
state: {
/** Runtime id of the active chat session (null on a fresh draft). */
@@ -937,46 +919,8 @@ export const host = {
ensureAgent: async (connectionId: null | string | undefined, profile: string): Promise<void> =>
ensureGatewayAgent(connectionId ?? null, (profile ?? '').trim() || 'default'),
- /** Session-list mutations a plugin may perform on the user's behalf. Every
- * method writes the SAME stores the app's own controls write, so a plugin
- * action and a hand click can never disagree — the sidebar and the tab
- * strip re-render from those stores immediately. Ids are stored (durable)
- * session ids as a sidebar row carries them (`session.id`); a live id is
- * resolved to its durable lineage root before writing. */
- sessions: {
- /** Pin or unpin a session — the row's ⇧-click / context-menu action. A
- * pinned session moves into the Pinned section on the next render.
- * `index` slots the pin at that position in the Pinned list (a drop
- * target between two pins); omitted = append, like the ⇧-click. */
- pin: (storedSessionId: string, pinned = true, index?: number): void => {
- const id = durableSessionPinId(storedSessionId)
-
- if (pinned) {
- pinSession(id, index)
- } else {
- unpinSession(id)
- }
- },
-
- /** Replace the manual session order with `ids` (what a drag persists).
- * Ids the window hasn't loaded reconcile on the next render, exactly
- * like the app's own reorder. An EMPTY list clears the manual order and
- * returns Recents to the default sort — the sidebar's own reconcile
- * effect reaches that state one render after a drag empties the list;
- * the verb states it directly so a plugin reset never depends on a
- * mounted effect. */
- reorder: (ids: string[]): void => {
- setSidebarSessionOrderManual(ids.length > 0)
- setSidebarSessionOrderIds(ids)
- },
-
- /** Set a session's colour override (or clear it with `null`) — the same
- * per-session colour the app's own picker writes, so a plugin swatch and
- * a hand-picked colour are one value. */
- setColor: (storedSessionId: string, color: null | string): void => {
- setSessionColorOverride(durableSessionPinId(storedSessionId), color)
- }
- },
+ /** Session-list mutations (pin, reorder, colour) — see `./sessions`. */
+ sessions: sessionsHost,
/** Open a stored session the way core surfaces do. A plugin/Bot Mode open
* is navigation, not a workspace or chrome API-home switch —
diff --git a/apps/desktop/src/sdk/sessions.ts b/apps/desktop/src/sdk/sessions.ts
new file mode 100644
index 00000000000..4dd7562dc78
--- /dev/null
+++ b/apps/desktop/src/sdk/sessions.ts
@@ -0,0 +1,79 @@
+import {
+ pinSession,
+ setPinnedSessionOrder,
+ setSidebarSessionOrderIds,
+ setSidebarSessionOrderManual,
+ unpinSession
+} from '@/store/layout'
+import { $sessions, sessionMatchesStoredId, sessionPinId } from '@/store/session'
+import { setSessionColorOverride } from '@/store/session-color'
+
+/** Pins and colours are keyed by the DURABLE (lineage-root) id so they survive
+ * compression's session-id rotation; a row's live id resolves through
+ * `$sessions` (the app's own lineage matcher), and an id that resolves to
+ * nothing is passed through as-is (the stores tolerate ids for rows this
+ * window hasn't loaded). */
+function durableSessionPinId(storedSessionId: string): string {
+ const session = $sessions.get().find(s => sessionMatchesStoredId(s, storedSessionId))
+
+ return session ? sessionPinId(session) : storedSessionId
+}
+
+/** The Recents order store is keyed by the LIVE id (`session.id`) — the drag
+ * path persists `reorderableRowIds` and the sidebar's reconcile effect keeps
+ * only ids present in `unpinnedAgentSessions.map(s => s.id)`. A plugin holds
+ * the durable id from the row slot, so map it back to the loaded row's live id
+ * before writing; a durable id written verbatim would be dropped by the next
+ * reconcile and, if nothing else survived, flip the manual flag off. */
+function liveSessionId(storedSessionId: string): string {
+ const session = $sessions.get().find(s => sessionMatchesStoredId(s, storedSessionId))
+
+ return session ? session.id : storedSessionId
+}
+
+/** Session-list mutations a plugin may perform on the user's behalf. Every
+ * method writes the SAME stores the app's own controls write, so a plugin
+ * action and a hand click can never disagree — the sidebar and the tab
+ * strip re-render from those stores immediately. Ids are stored (durable)
+ * session ids as a sidebar row slot carries them; a live id is resolved to
+ * the same row through the app's lineage matcher. */
+export const sessionsHost = {
+ /** Pin or unpin a session — the row's ⇧-click / context-menu action. A
+ * pinned session moves into the Pinned section on the next render.
+ * `index` slots the pin at that position in the Pinned list (a drop
+ * target between two pins); omitted = append, like the ⇧-click. */
+ pin: (storedSessionId: string, pinned = true, index?: number): void => {
+ const id = durableSessionPinId(storedSessionId)
+
+ if (pinned) {
+ pinSession(id, index)
+ } else {
+ unpinSession(id)
+ }
+ },
+
+ /** Replace the manual Recents order with `ids` (what a drag persists).
+ * Ids the window hasn't loaded reconcile on the next render, exactly
+ * like the app's own reorder. An EMPTY list clears the manual order and
+ * returns Recents to the default sort — the sidebar's own reconcile
+ * effect reaches that state one render after a drag empties the list;
+ * the verb states it directly so a plugin reset never depends on a
+ * mounted effect. */
+ reorder: (ids: string[]): void => {
+ setSidebarSessionOrderManual(ids.length > 0)
+ setSidebarSessionOrderIds(ids.map(liveSessionId))
+ },
+
+ /** Permute the Pinned section — the sidebar's own pinned-drag path. Only
+ * pins the list names move; a pin it omits (row not loaded) keeps its slot. */
+ reorderPinned: (ids: string[]): void => {
+ setPinnedSessionOrder(ids.map(durableSessionPinId))
+ },
+
+ /** Set a session's colour override (or clear it with `null`) — the same
+ * per-session colour the app's own picker writes, so a plugin swatch and
+ * a hand-picked colour are one value. */
+ setColor: (storedSessionId: string, color: null | string): void => {
+ setSessionColorOverride(durableSessionPinId(storedSessionId), color)
+ }
+}
diff --git a/website/docs/developer-guide/desktop-plugin-sdk.md b/website/docs/developer-guide/desktop-plugin-sdk.md
index 188b5e89f65..57a297700c3 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -521,14 +521,20 @@ controls write (so a plugin action and a hand click can never disagree):
```ts
host.sessions.pin(storedSessionId: string, pinned?: boolean, index?: number): void
-host.sessions.reorder(storedSessionIds: string[]): void // [] = clear manual order → default sort
+host.sessions.reorder(storedSessionIds: string[]): void // Recents; [] = clear manual order → default sort
+host.sessions.reorderPinned(storedSessionIds: string[]): void // Pinned section; omitted pins keep their slot
host.sessions.setColor(storedSessionId: string, color: string | null): void
```
Ids are STORED session ids: a live id is resolved to its durable lineage root,
so pins and colours survive compression's id rotation — and the row-decoration
slots hand your render that same durable id (`_lineage_root_id ?? id`), never
-the live one. `pin(id, true, index)` slots the pin at that position in the
+the live one. Resolution goes through the rows this window has loaded; an id
+that matches no loaded row is written as given, so pass the slot's durable id
+(not a live id you remembered) for a session that may have scrolled out of the
+list. `reorder` accepts the same ids and maps each to its row's live id
+internally — the Recents order store is keyed by the live id, like the drag
+path. `pin(id, true, index)` slots the pin at that position in the
Pinned list (a drop target between two pins); without `index` it appends, like
the row's ⇧-click.
@@ -551,7 +557,8 @@ Migration for the held catalog plugins:
(`render: ({ sessionId }) => …` under `SESSION_ROW_AREAS.leading` gives you
the durable id per row), then `host.sessions.pin(sessionId, true, dropIndex)`
for a drop into the Pinned section, `host.sessions.pin(sessionId, false)` for
- a drop back into Recents, and `host.sessions.reorder([])` for its
+ a drop back into Recents, `host.sessions.reorderPinned(ids)` for a drag
+ within the Pinned section, and `host.sessions.reorder([])` for its
"reset manual order" path.
- **better-session-appearance** — replace the `localStorage`
`hermes.desktop.sessionColors` write and the fiber-harvested `onChange` with
@@ -692,7 +699,8 @@ host.requestProfile<T>(profile, method, params?) // legacy v1/local overload
host.request<T>(method, params?) // active-gateway JSON-RPC — the real power
host.sessions.pin(storedSessionId, pinned?, index?) // pin/unpin (default pinned=true); index = slot in Pinned;
// same store the row's ⇧-click / drop writes
-host.sessions.reorder(ids) // replace the manual session order (what a drag persists); [] resets
+host.sessions.reorder(ids) // replace the manual Recents order (what a drag persists); [] resets
+host.sessions.reorderPinned(ids) // permute the Pinned section (the pinned drag path)
host.sessions.setColor(storedSessionId, color | null) // per-session colour override; null clears
```
@@ -1195,8 +1203,8 @@ pipeline as a trust boundary.
|----------|---------|
| Host | `host` (`.state.*`, `.settings`, `.notify`, `.notifyError`, `.navigate`, `.onEvent`, `.logs`, `.status`, `.restartGateway`, `.request`, `.composer`, `.sessions`) |
| Plugin contract | `HermesPlugin`, `PluginContext`, `PluginContribution`, `PluginStorage`, `PluginOs`, `PluginRestOptions`, `PluginNativeNotificationInput`, `PluginNotificationAction`, `HermesOpenTarget`, `Contribution` |
-| Area constants | `PANES_AREA`, `ROUTES_AREA`, `SIDEBAR_NAV_AREA`, `STATUSBAR_AREAS`, `TITLEBAR_AREAS`, `WORKSPACE_PAGE_HEADER_AREA`, `PALETTE_AREA`, `KEYBINDS_AREA`, `THEMES_AREA`, `COMPOSER_AREAS` |
-| Area payloads | `RouteContribution`, `SidebarNavContribution`, `StatusbarItem`, `TitlebarTool`, `PaletteContribution`, `KeybindContribution`, `ComposerMiddleware`, `ComposerAttachmentProvider` |
+| Area constants | `PANES_AREA`, `ROUTES_AREA`, `SIDEBAR_NAV_AREA`, `STATUSBAR_AREAS`, `TITLEBAR_AREAS`, `WORKSPACE_PAGE_HEADER_AREA`, `PALETTE_AREA`, `KEYBINDS_AREA`, `THEMES_AREA`, `COMPOSER_AREAS`, `SESSION_ROW_AREAS` |
+| Area payloads | `RouteContribution`, `SidebarNavContribution`, `StatusbarItem`, `TitlebarTool`, `PaletteContribution`, `KeybindContribution`, `ComposerMiddleware`, `ComposerAttachmentProvider`, `SessionRowSlotContribution` |
| React / state | `useValue`, `atom`, `computed`, `useQuery`, `useMutation`, `useQueryClient`, `queryClient`, `Contribute` |
| Theming | `useTheme`, `requestTheme`, `setAccentOverride`, `$accentOverride`, `retintTheme`, `themeHue`, `DesktopTheme`, `DesktopThemeColors`, plus OKLCH math (`hexToOklch`, `oklchToHex`, `oklchToSrgb255`, `mixOklab`, `maxChroma`, `hueDelta`, `normalizeHex`) and sRGB measures (`contrastRatio` — `number | null`, null for unparseable input — `readableOn`) |
| UI kit | `Button`, `Input`, `Textarea`, `Select*`, `Switch`, `Checkbox`, `SegmentedControl`, `Tabs*`, `Dialog*`, `ConfirmDialog`, `DropdownMenu*`, `ContextMenu*`, `Popover*`, `Tip`/`Tooltip*`, `Badge`, `Kbd`/`KbdGroup`, `SearchField`, `ScrollArea`, `Separator`, `Skeleton`, `GlyphSpinner`, `Loader`, `EmptyState`, `ErrorState`, `CopyButton`, `StatusDot`, `LogView`, `Codicon`, `DecodeText` |