Skip to content

The agent that grows with you

fix(cli,tui): a refused delete releases its read with a plain detach under the resolved id

yoniebans737d1d9e8158
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index 71c5f063b2a..33911f3b75f 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -283,16 +283,22 @@ class GatewaySessionStore:
def delete_session(self, session_id, sessions_dir=None):
self.snapshots.pop(session_id, None)
- state = self.connection.states.get(session_id)
- viewing = state is not None and state.subscription_id is not None
snapshot = self.call("session.resume", session_id=session_id)
+ # The owner answers and subscribes under the id it resolved the read to (a compression
+ # segment maps to its logical owner), and reuses a subscription this viewer already holds.
+ resolved = snapshot.get("session_id") or session_id
+ state = self.connection.states.get(resolved)
+ held = state is not None and state.subscription_id is not None
try:
self._mutate(session_id, "delete", {}, snapshot)
except GatewayClientError:
- # The revision read attached this viewer; a session it was not viewing goes back closed.
- if not viewing and snapshot.get("subscription_id"):
- self.call("session.detach", session_id=session_id, subscription_id=snapshot["subscription_id"],
- end_reason="cli_close")
+ # Hand back only the attachment the read made, with a plain detach: an end_reason would
+ # run the owner's viewer close (fencing the session's delegations, ending it at idle).
+ if not held and snapshot.get("subscription_id"):
+ try:
+ self.call("session.detach", session_id=resolved, subscription_id=snapshot["subscription_id"])
+ except GatewayClientError as exc:
+ logger.debug("session.detach after a refused delete failed: %s", exc)
raise
self._leave(session_id)
return True
diff --git a/ui-tui/src/app/slash/canonicalSessionControls.ts b/ui-tui/src/app/slash/canonicalSessionControls.ts
index 8a9a0541567..cfbe3616b1b 100644
--- a/ui-tui/src/app/slash/canonicalSessionControls.ts
+++ b/ui-tui/src/app/slash/canonicalSessionControls.ts
@@ -64,6 +64,8 @@ export interface MutationSnapshot {
execution_generation: number
/** The viewer attachment the revision read made (`session.resume` attaches). */
subscription_id?: string
+ /** The id the owner resolved the read to and subscribed under; may differ from the one sent. */
+ session_id?: string
}
/** The CAS tuple read (`session.resume`, which attaches this client as a viewer). */
diff --git a/ui-tui/src/components/activeSessionSwitcher.tsx b/ui-tui/src/components/activeSessionSwitcher.tsx
index ec3b905dbfe..ab92b804a1b 100644
--- a/ui-tui/src/components/activeSessionSwitcher.tsx
+++ b/ui-tui/src/components/activeSessionSwitcher.tsx
@@ -550,7 +550,8 @@ export function ActiveSessionSwitcher({
[history.length, items.length]
)
- const { deletePending, deleting, performDelete } = useSessionDelete(gw, removeDeleted, setActionErr)
+ const keepView = useCallback((resolved: string) => resolved === currentSessionId, [currentSessionId])
+ const { deletePending, deleting, performDelete } = useSessionDelete(gw, removeDeleted, setActionErr, keepView)
const handleRowClick = useCallback(
(index: number) => (event: { stopImmediatePropagation?: () => void }) => {
diff --git a/ui-tui/src/hooks/useSessionDelete.ts b/ui-tui/src/hooks/useSessionDelete.ts
index b4643a71a61..211fb306f37 100644
--- a/ui-tui/src/hooks/useSessionDelete.ts
+++ b/ui-tui/src/hooks/useSessionDelete.ts
@@ -8,11 +8,13 @@ import { messages } from '../i18n/runtime.js'
import { asRpcResult, rpcErrorMessage } from '../lib/rpc.js'
/** The picker's history delete: one request at a time, and a refused delete hands back the viewer
- * attachment its revision read made. Settles through `onDeleted` or `onError`. */
+ * attachment its revision read made unless `keepView` says this view already held the session.
+ * Settles through `onDeleted` or `onError`. */
export function useSessionDelete(
gw: GatewayClient,
onDeleted: (id: string) => void,
- onError: (message: string) => void
+ onError: (message: string) => void,
+ keepView: (resolvedId: string) => boolean
) {
const [deleting, setDeleting] = useState(false)
// Set synchronously (one stdin chunk can carry the confirming key and a click before a re-render);
@@ -35,11 +37,18 @@ export function useSessionDelete(
onError('')
// The shared gateway has no `session.delete`: its delete is the revision-fenced
// `session.mutate operation=delete` (the whole lineage), as the classic CLI sends it.
- let readSubscription: string | undefined
+ let release: undefined | { session_id: string; subscription_id: string }
const deleted = gw.isCanonical
? mutateCanonical(gw, target.id, 'delete', {}, undefined, snapshot => {
- readSubscription = snapshot.subscription_id
+ // The owner subscribes under the id it resolved the read to, and reuses a subscription
+ // this view already holds: release only an attachment the read itself made.
+ const resolved = snapshot.session_id || target.id
+
+ release =
+ snapshot.subscription_id && !keepView(resolved)
+ ? { session_id: resolved, subscription_id: snapshot.subscription_id }
+ : undefined
}).then(mutation => Boolean(mutation?.result))
: gw
.request<SessionDeleteResponse>('session.delete', { session_id: target.id })
@@ -56,23 +65,17 @@ export function useSessionDelete(
settle()
})
.catch(rejection => {
- // The revision read attached this view to a session it was not showing; a refused
- // delete hands it back closed so it does not reappear in the live section.
- if (readSubscription) {
- void gw
- .request('session.detach', {
- session_id: target.id,
- subscription_id: readSubscription,
- end_reason: 'tui_close'
- })
- .catch(() => undefined)
+ // A refused delete hands the read's attachment back so the closed session does not reappear
+ // in the live section. A plain detach: an end reason would run the owner's viewer close.
+ if (release) {
+ void gw.request('session.detach', release).catch(() => undefined)
}
onError(rpcErrorMessage(rejection))
settle()
})
},
- [gw, onDeleted, onError]
+ [gw, keepView, onDeleted, onError]
)
const pending = useCallback(() => deleting || deletingRef.current, [deleting])