The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(cli,tui): a refused delete detaches by the resolved id and keeps a view the client holds (currently failing)
Teknium9db77087cb49
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.pyindex 0c2a08a61a9..59ad2dc1b54 100644--- a/tests/hermes_cli/test_gateway_session_store.py+++ b/tests/hermes_cli/test_gateway_session_store.py@@ -113,27 +113,67 @@ def test_title_rename_delete_and_detach_are_gateway_verbs(store):assert delete["operation"] == "delete" and delete["expected_generation"] == 0-def test_a_refused_delete_of_a_closed_session_leaves_it_closed(store):- """The delete reads its revision through ``session.resume``, which attaches this viewer again;- a refusal must hand that attachment back, or the closed session stays in the live list with- this CLI as its viewer."""+def _refusing_delete(client, reason="revision_conflict"):+ original = client.rpc++ async def rpc(method, *, _timeout=None, **params):+ if method == "session.mutate" and params.get("operation") == "delete":+ client.calls.append((method, params))+ raise GatewayClientError(reason)+ return await original(method, _timeout=_timeout, **params)+ client.rpc = rpc+++def test_a_refused_delete_detaches_the_subscription_its_read_attached(store):+ """#134889 TUI-19: the delete's CAS read (``session.resume``) attaches a viewer; a refused delete+ must detach it, or the closed session stays in the live list until the CLI exits. A plain detach:+ an ``end_reason`` would run the owner's viewer close on the session."""db, client = storedb.open_session(cli("local-old", resumed=True))db.end_session("local-old", "new_session")- real_rpc = client.rpc+ _refusing_delete(client)+ with pytest.raises(GatewayClientError, match="revision_conflict"):+ db.delete_session("local-old")+ assert client.calls[-1] == ("session.detach", {"session_id": "local-old", "subscription_id": "sub-local-old"})- async def refusing(method, *, _timeout=None, **params):- if method == "session.mutate":++def test_a_refused_delete_detaches_by_the_id_the_read_resolved_to(store):+ """A compressed session's history row names a segment the owner resolves to its logical owner+ and subscribes under that id; the release must detach by the resolved id, or it is a no-op."""+ db, client = store+ original = client.rpc++ async def rpc(method, *, _timeout=None, **params):+ if method == "session.resume" and params["session_id"] == "segment-1":client.calls.append((method, params))- raise GatewayRPCError("session_busy")- return await real_rpc(method, _timeout=_timeout, **params)+ return snapshot("local-owner")+ return await original(method, _timeout=_timeout, **params)+ client.rpc = rpc+ _refusing_delete(client)+ with pytest.raises(GatewayClientError, match="revision_conflict"):+ db.delete_session("segment-1")+ assert client.calls[-1] == ("session.detach", {"session_id": "local-owner", "subscription_id": "sub-local-owner"})- client.rpc = refusing- with pytest.raises(GatewayClientError):- db.delete_session("local-old")- # cli_close: the stamp main's classic CLI ends a session with; a row already ended keeps its own reason.- assert client.calls[-1] == ("session.detach", {"session_id": "local-old", "subscription_id": "sub-local-old",- "end_reason": "cli_close"})++def test_a_refused_delete_of_a_segment_keeps_the_resolved_session_this_viewer_watches(store):+ """The held check uses the resolved id too: the CLI watches the logical owner, the read of its+ segment reuses that subscription, and a refusal must not end the CLI's own view."""+ db, client = store+ client.known.add("local-owner")+ db.open_session(cli("local-owner", resumed=True))+ original = client.rpc++ async def rpc(method, *, _timeout=None, **params):+ if method == "session.resume" and params["session_id"] == "segment-1":+ client.calls.append((method, params))+ return snapshot("local-owner")+ return await original(method, _timeout=_timeout, **params)+ client.rpc = rpc+ _refusing_delete(client, "permission_denied")+ with pytest.raises(GatewayClientError, match="permission_denied"):+ db.delete_session("segment-1")+ assert "session.detach" not in [m for m, _ in client.calls]+ assert db.connection.state("local-owner").subscription_id == "sub-local-owner"def test_a_refused_delete_keeps_a_session_this_cli_is_viewing(store):diff --git a/ui-tui/src/__tests__/canonicalPickerDelete.test.tsx b/ui-tui/src/__tests__/canonicalPickerDelete.test.tsxindex 9465d26846c..9996658a65f 100644--- a/ui-tui/src/__tests__/canonicalPickerDelete.test.tsx+++ b/ui-tui/src/__tests__/canonicalPickerDelete.test.tsx@@ -13,19 +13,26 @@ import { DEFAULT_THEME } from '../theme.js'const stored = { id: 'old', session_id: 'old', title: 'Retained old conversation', source: 'cli', started_at: 1, message_count: 2 }const live = { id: 'live', session_id: 'live', title: 'Current conversation', started_at: 2, message_count: 1, running: false }-function mount(mutate: (params: any) => Promise<unknown>) {+function mount(+ mutate: (params: any) => Promise<unknown>,+ resolve: (sid: string) => string = sid => sid,+ current = 'live'+) {const request = vi.fn(async (method: string, params: any) => {if (method === 'session.list') {return { scope: params.scope ?? 'live', sessions: params.scope === 'stored' ? [stored] : [live] }}if (method === 'session.resume') {+ // The owner answers (and subscribes) under the id it resolved the read to.+ const resolved = resolve(params.session_id)+return {- session_id: params.session_id,+ session_id: resolved,revision: 4,execution_generation: 2,messages: [],- subscription_id: 'cas-read'+ subscription_id: `sub-${resolved}`}}@@ -48,7 +55,7 @@ function mount(mutate: (params: any) => Promise<unknown>) {})const element = (<ActiveSessionSwitcher- currentSessionId="live"+ currentSessionId={current}gw={{ isCanonical: true, request } as any}onCancel={vi.fn()}onClose={vi.fn()}@@ -99,6 +106,8 @@ it('deletes a stored session through session.mutate with the resume CAS tuple',payload: {}})expect(request.mock.calls.map(([method]) => method)).not.toContain('session.delete')+ // A committed delete retires the session on the owner: nothing to detach.+ expect(request.mock.calls.map(([method]) => method)).not.toContain('session.detach')await vi.waitFor(() => {out.text = ''instance.rerender(React.cloneElement(element))@@ -131,22 +140,62 @@ it('keeps a refused delete on screen past the live-status poll', async () => {}})-it('hands back the attachment its revision read made when the delete is refused', async () => {- // The CAS read is a `session.resume`, which attaches this view to the closed session; left- // attached, a refusal puts the closed session back in the live list until Ink exits.+it('detaches the viewer its CAS read attached when the delete is refused (#134889 TUI-19)', async () => {const { deleteStored, instance, request } = mount(async () => {- throw Object.assign(new Error('session_busy'), { data: { reason: 'session_busy' } })+ throw Object.assign(new Error('revision_conflict'), { data: { reason: 'revision_conflict' } })})+ try {+ await deleteStored()+ // Without it the closed session stays subscribed, so the owner lists it live until Ink exits. A plain+ // detach: an end reason would run the owner's viewer close on whatever session the row names.+ await vi.waitFor(() =>+ expect(request).toHaveBeenCalledWith('session.detach', { session_id: 'old', subscription_id: 'sub-old' })+ )+ const methods = request.mock.calls.map(([method]) => method)+ expect(methods.indexOf('session.detach')).toBeGreaterThan(methods.indexOf('session.mutate'))+ } finally {+ instance.unmount()+ }+})++it('detaches by the id the read resolved to, not the history row id (#134889 TUI-19)', async () => {+ // A compressed session: the row names a segment, the owner resolves it to the logical owner.+ const { deleteStored, instance, request } = mount(+ async () => {+ throw Object.assign(new Error('revision_conflict'), { data: { reason: 'revision_conflict' } })+ },+ sid => (sid === 'old' ? 'old-owner' : sid)+ )+try {await deleteStored()await vi.waitFor(() =>expect(request).toHaveBeenCalledWith('session.detach', {- session_id: 'old',- subscription_id: 'cas-read',- end_reason: 'tui_close'+ session_id: 'old-owner',+ subscription_id: 'sub-old-owner'}))+ expect(request).not.toHaveBeenCalledWith('session.detach', expect.objectContaining({ session_id: 'old' }))+ } finally {+ instance.unmount()+ }+})++it('keeps the view of the session this picker watches when the read resolves to it', async () => {+ const { deleteStored, instance, request } = mount(+ async () => {+ throw Object.assign(new Error('revision_conflict'), { data: { reason: 'revision_conflict' } })+ },+ sid => (sid === 'old' ? 'live' : sid),+ 'live'+ )++ try {+ await deleteStored()+ await vi.waitFor(() => expect(request.mock.calls.map(([method]) => method)).toContain('session.mutate'))+ await new Promise(resolve => setTimeout(resolve, 50))+ expect(request.mock.calls.map(([method]) => method)).not.toContain('session.detach')} finally {instance.unmount()}