The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui,cli): a refused delete hands back the attachment its revision read made
yoniebans9566b51a00dd
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.pyindex fb31667a63f..71c5f063b2a 100644--- a/hermes_cli/gateway_session_store.py+++ b/hermes_cli/gateway_session_store.py@@ -198,8 +198,8 @@ class GatewaySessionStore:self._adopt(self.attach(child))return child- def _mutate(self, session_id, operation, payload):- snapshot = self.call("session.resume", session_id=session_id)+ def _mutate(self, session_id, operation, payload, snapshot=None):+ snapshot = snapshot or self.call("session.resume", session_id=session_id)fence = {}if operation in _GENERATION_FENCED:fence["expected_generation"] = snapshot["execution_generation"]@@ -283,7 +283,17 @@ class GatewaySessionStore:def delete_session(self, session_id, sessions_dir=None):self.snapshots.pop(session_id, None)- self._mutate(session_id, "delete", {})+ 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)+ 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")+ raiseself._leave(session_id)return Truediff --git a/ui-tui/src/app/slash/canonicalSessionControls.ts b/ui-tui/src/app/slash/canonicalSessionControls.tsindex 367fef8991c..8a9a0541567 100644--- a/ui-tui/src/app/slash/canonicalSessionControls.ts+++ b/ui-tui/src/app/slash/canonicalSessionControls.ts@@ -62,6 +62,19 @@ export interface MutationSnapshot {stored_messages?: Array<Record<string, unknown>>revision: numberexecution_generation: number+ /** The viewer attachment the revision read made (`session.resume` attaches). */+ subscription_id?: string+}++/** The CAS tuple read (`session.resume`, which attaches this client as a viewer). */+async function readCasTuple(gw: MutationGateway, sid: string, onRead?: (snapshot: MutationSnapshot) => void) {+ const snapshot = asRpcResult<MutationSnapshot>(await gw.request('session.resume', { session_id: sid }))++ if (snapshot) {+ onRead?.(snapshot)+ }++ return snapshot}/** One revision-fenced `session.mutate` (`gateway/session_mutations.py`). The CAS tuple comes from a@@ -84,7 +97,8 @@ export async function mutateCanonical(sid: string,operation: CanonicalOperation,payload: Record<string, unknown> | ((snapshot: MutationSnapshot) => null | Record<string, unknown>),- stale: () => boolean = () => false+ stale: () => boolean = () => false,+ onRead?: (snapshot: MutationSnapshot) => void) {let requests = pending.get(gw)@@ -100,7 +114,7 @@ export async function mutateCanonical(let params = requests.get(key)if (!params) {- const snapshot = asRpcResult<MutationSnapshot>(await gw.request('session.resume', { session_id: sid }))+ const snapshot = await readCasTuple(gw, sid, onRead)if (stale()) {returndiff --git a/ui-tui/src/components/activeSessionSwitcher.tsx b/ui-tui/src/components/activeSessionSwitcher.tsxindex 41690fa008f..d188eeaa9ac 100644--- a/ui-tui/src/components/activeSessionSwitcher.tsx+++ b/ui-tui/src/components/activeSessionSwitcher.tsx@@ -559,8 +559,12 @@ export function ActiveSessionSwitcher({setActionErr('')// 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+const deleted = gw.isCanonical- ? mutateCanonical(gw, target.id, 'delete', {}).then(mutation => Boolean(mutation?.result))+ ? mutateCanonical(gw, target.id, 'delete', {}, undefined, snapshot => {+ readSubscription = snapshot.subscription_id+ }).then(mutation => Boolean(mutation?.result)): gw.request<SessionDeleteResponse>('session.delete', { session_id: target.id }).then(raw => asRpcResult<SessionDeleteResponse>(raw)?.deleted === target.id)@@ -582,6 +586,18 @@ export function ActiveSessionSwitcher({setDeleting(false)}).catch((e: unknown) => {+ // 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)+ }+setActionErr(rpcErrorMessage(e))setDeleting(false)})