Skip to content

The agent that grows with you

fix(gateway): keep a branch's launch secrets while it still uses them

David Dudok de Witef5b071b0114
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index bdd170b5403..7fd3315e933 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -779,21 +779,43 @@ def rebind_launch_key(authority, session, api_key):
keys[_key_slot(ref)] = api_key
+def _secret_refs(authority, session_ids):
+ """The launch-key slots and config-secret references the live LOCAL policies of *session_ids* name."""
+ from gateway.config import Platform
+ sources = [getattr(authority.sessions.get(sid), 'source', None) for sid in session_ids]
+ chats = [source.chat_id for source in sources if source is not None and source.platform == Platform.LOCAL]
+ if not chats:
+ return set() # only a restored (live) LOCAL session has a policy in the adapter map
+ from gateway.session_local_recovery import local_adapter_map
+ policies = getattr(local_adapter_map(authority).get(Platform.LOCAL), 'policies', None) or {}
+ refs = set()
+ for policy in filter(None, map(policies.get, chats)):
+ refs.update(ref for ref in (policy.credential_ref and _key_slot(policy.credential_ref),
+ policy.config_secret_ref) if ref)
+ return refs
+
+
def release_launch_secrets(authority, session_ids):
"""Drop retired sessions' launch keys, frozen config secrets and editor MCP servers.
They live only in this authority's memory for the session's lifetime; a deleted session can
- never run again, so keeping them would hold its credentials until the process exits."""
+ never run again, so keeping them would hold its credentials until the process exits. A branch's
+ policy names its parent's slots, so a slot named after a retired session stays while a live
+ policy references it, and then until restart, which revokes every launch secret anyway. Only
+ slots named after *session_ids* are released, so the set never exceeds release by name: no later
+ sweep, and a binding not yet published always sits under its own live session's name. Call
+ before the retired sessions leave ``authority.sessions``."""
import hashlib
from gateway.session_policy_credentials import PREFIX
retired = {str(sid) for sid in session_ids}
if not retired:
return
+ kept = _secret_refs(authority, set(authority.sessions) - retired)
prefix = f'{authority.instance_id}:{authority.epoch}:'
keys = getattr(authority, '_local_launch_keys', None) or {}
# A key re-supplied after a restart sits in the slot of the epoch that minted it.
owners = getattr(authority, '_local_owner_key_requests', None) or {}
- for ref in [ref for ref in keys if ref.split(':', 2)[-1] in retired]:
+ for ref in [ref for ref in keys if ref.split(':', 2)[-1] in retired and ref not in kept]:
del keys[ref]
owners.pop(ref, None)
configs = getattr(authority, '_local_config_secrets', None) or {}
@@ -802,7 +824,7 @@ def release_launch_secrets(authority, session_ids):
owner = json.loads(ref[len(PREFIX):]).get('session')
else:
owner = ref[len(prefix):] if ref.startswith(prefix) else None
- if owner in retired:
+ if owner in retired and ref not in kept:
del configs[ref]
editors = getattr(authority, '_local_editor_mcp', None) or {}
for sid in retired:
diff --git a/tests/gateway/test_branch_after_compressed_root_delete.py b/tests/gateway/test_branch_after_compressed_root_delete.py
new file mode 100644
index 00000000000..8c1bc97846c
--- /dev/null
+++ b/tests/gateway/test_branch_after_compressed_root_delete.py
@@ -0,0 +1,99 @@
+"""#134889 NF4: deleting a compressed, branched conversation leaves its branch a working conversation."""
+import asyncio
+from contextlib import closing
+from http.server import ThreadingHTTPServer
+import json
+from pathlib import Path
+import sqlite3
+import threading
+
+from tests.gateway.fixtures.local_recovery_probe import child_env, daemon, rpc, websocket
+from tests.gateway.test_session_mutation_compress import SummaryModel
+from tests.waiting import async_wait_until
+
+KEY = 'launch-only-key-0123456789abcdef'
+
+
+class AuthModel(SummaryModel):
+ def do_POST(self):
+ self.server.auth.append(self.headers.get('Authorization'))
+ super().do_POST()
+
+
+def test_branch_keeps_its_launch_key_after_the_compressed_root_is_deleted(tmp_path):
+ root = Path(__file__).resolve().parents[2]
+ home, user = tmp_path / 'state', tmp_path / 'user'
+ home.mkdir(mode=0o700)
+ user.mkdir()
+ peer = ThreadingHTTPServer(('127.0.0.1', 0), AuthModel)
+ peer.requests, peer.auth = [], []
+ thread = threading.Thread(target=peer.serve_forever, daemon=True)
+ thread.start()
+ url = f'http://127.0.0.1:{peer.server_port}/v1'
+ (home / 'config.yaml').write_text(json.dumps({
+ 'gateway': {'multiplex_profiles': False, 'managed_workers': False},
+ 'model': {'provider': 'custom', 'default': 'original', 'base_url': url},
+ 'auxiliary': {'title_generation': {'enabled': False},
+ 'compression': {'provider': 'custom', 'model': 'summary', 'base_url': url}},
+ 'platform_toolsets': {'cli': []},
+ 'compression': {'protect_first_n': 1, 'protect_last_n': 2, 'min_tail_user_messages': 1, 'in_place': False}}))
+ (home / 'models_dev_cache.json').write_text(json.dumps({'custom': {'id': 'custom', 'models': {
+ name: {'id': name, 'name': name, 'limit': {'context': 1000000, 'output': 4000}}
+ for name in ('original', 'summary')}}}))
+ env = child_env()
+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),
+ OPENAI_API_KEY='profile-key', OPENAI_BASE_URL=url, PYTHONUNBUFFERED='1')
+
+ def db():
+ return closing(sqlite3.connect(f'file:{home / "state.db"}?mode=ro', uri=True))
+
+ async def run(desc):
+ async with websocket(home, desc) as ws:
+ async def call(method, **params):
+ response = await rpc(ws, method, **params)
+ assert 'result' in response, response
+ return response['result']
+
+ async def turn(sid, text):
+ await call('prompt.submit', session_id=sid, input_id=text, text=text)
+
+ def outcome():
+ with db() as conn:
+ row = conn.execute("SELECT outcome FROM session_admissions WHERE request_id=? "
+ "AND status='terminal'", (text,)).fetchone()
+ return row and row[0]
+ await async_wait_until(outcome, timeout=30, what=f'{text} terminal')
+ sent = [auth for auth, body in zip(peer.auth, peer.requests) if text in json.dumps(body['messages'][-1:])]
+ return outcome(), sent
+
+ async def mutate(sid, operation):
+ state = await call('session.resume', session_id=sid)
+ return await call('session.mutate', session_id=sid, request_id=operation + sid, operation=operation,
+ payload={}, expected_revision=state['revision'],
+ expected_generation=state['execution_generation'])
+
+ sid = (await call('session.create', source='cli', request_id='owner', toolsets=[], model='original',
+ provider='custom', base_url=url, api_key=KEY))['session_id']
+ for i in range(5):
+ await turn(sid, f'ROOT_{i}')
+ continuation = (await mutate(sid, 'compress'))['target_session_id']
+ branch = (await mutate(sid, 'branch'))['branched_session_id']
+ assert await turn(branch, 'BEFORE_DELETE') == ('completed', ['Bearer ' + KEY])
+ assert set((await mutate(sid, 'delete'))['deleted_ids']) == {sid, continuation}
+ with db() as conn:
+ # Independent conversation: unparented, its fork marker kept as provenance (prune does the same).
+ parent, config = conn.execute('SELECT parent_session_id,model_config FROM sessions WHERE id=?',
+ (branch,)).fetchone()
+ assert parent is None and json.loads(config)['_branched_from'] == continuation
+ assert await turn(branch, 'AFTER_DELETE') == ('completed', ['Bearer ' + KEY])
+ compressed = await mutate(branch, 'compress')
+ assert set((await mutate(branch, 'delete'))['deleted_ids']) == {branch, compressed['target_session_id']}
+ with db() as conn:
+ assert conn.execute('SELECT count(*) FROM sessions').fetchone() == (0,)
+ try:
+ with daemon(root, home, env, barrier=False) as (_, desc):
+ asyncio.run(run(desc))
+ finally:
+ peer.shutdown()
+ peer.server_close()
+ thread.join(timeout=5)
diff --git a/tests/gateway/test_launch_secret_retirement.py b/tests/gateway/test_launch_secret_retirement.py
index d07fd2e9c7e..f9ff6cd4d14 100644
--- a/tests/gateway/test_launch_secret_retirement.py
+++ b/tests/gateway/test_launch_secret_retirement.py
@@ -39,3 +39,200 @@ async def test_delete_prunes_launch_and_config_secrets(tmp_path, monkeypatch):
finally:
await connection.close()
owner.db.close()
+
+
+async def _branching_owner(tmp_path, monkeypatch):
+ """An owner whose sessions carry a config secret, with the mutate and next-turn key helpers."""
+ from gateway import run, session_mutations
+ from gateway.config import GatewayConfig
+ from gateway.session import SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_controls import AuthorityConnection
+ from gateway.session_policy import launch_key, policy_for_source
+ config = {'platform_toolsets': {'cli': []}, 'model': {
+ 'provider': 'custom', 'api_key': 'config-secret', 'base_url': 'http://127.0.0.1:9/v1'}}
+ monkeypatch.setattr(run, '_load_gateway_config', lambda *a, **k: config)
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ runner = SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False,
+ _cached_agent_for=lambda route: None, _evict_cached_agent=lambda route: None,
+ _set_session_reasoning_override=lambda route, value: None)
+ runner._adapter_for_source = lambda source: runner.adapters.get(source.platform)
+ owner = await initialize_session_authority(runner, profile_id=str(tmp_path), instance_id='fixture')
+ connection = AuthorityConnection(owner, object(), {'user_id': 'owner'})
+
+ async def mutate(ref, operation, payload=None, request_id=None):
+ handle = owner._handle(ref)
+ return await session_mutations.mutate_session(owner, connection.actor, ref, dict(
+ session_id=ref.session_id, request_id=request_id or operation + ref.session_id, operation=operation,
+ payload=payload or {}, expected_revision=handle.revision,
+ expected_generation=handle.execution_generation))
+
+ def key(ref):
+ return launch_key(owner, policy_for_source(runner, owner.sessions[ref.session_id].source))
+ return owner, connection, mutate, key
+
+
+def _create(owner, connection, tmp_path, api_key):
+ from gateway.session_local import create_local_session
+ return create_local_session(owner, connection.actor, {'request_id': 'parent', 'source': 'cli',
+ 'cwd': str(tmp_path), 'model': 'm', 'toolsets': [], **({'api_key': api_key} if api_key else {})})
+
+
[email protected]('first', ['parent', 'branch'])
+async def test_branches_keep_the_secrets_their_parent_bound_until_the_last_delete(tmp_path, monkeypatch, first):
+ """A branch's policy names its parent's launch-key slot and config-secret reference: deleting
+ either side keeps them for the survivors (#134889 RV-13). A delete releases only its own
+ sessions' slots, so the parent's go with the parent's delete when no branch is left, else at restart."""
+ from gateway.session_contract import SessionRef
+ owner, connection, mutate, key = await _branching_owner(tmp_path, monkeypatch)
+ try:
+ parent = _create(owner, connection, tmp_path, 'launch-parent')
+ child = SessionRef(parent.profile_id, (await mutate(parent, 'branch'))['branched_session_id'])
+ grandchild = SessionRef(parent.profile_id, (await mutate(child, 'branch'))['branched_session_id'])
+ secrets = dict(owner._local_config_secrets)
+ live = [parent, child, grandchild]
+ for ref in list(live) if first == 'parent' else live[::-1]:
+ await mutate(ref, 'delete')
+ live.remove(ref)
+ assert [key(survivor) for survivor in live] == ['launch-parent'] * len(live)
+ assert owner._local_config_secrets == secrets or not live
+ left = [*owner._local_launch_keys, *owner._local_config_secrets]
+ assert all(parent.session_id in ref for ref in left) and bool(left) == (first == 'parent')
+ finally:
+ await connection.close()
+ owner.db.close()
+
+
[email protected]('api_key', [None, 'launch-parent'], ids=['config-secret', 'launch-key'])
+async def test_a_branch_rebinds_after_its_parent_is_deleted(tmp_path, monkeypatch, api_key):
+ """A branch whose deleted parent's secrets it still uses rebinds them under its own name (here a
+ reasoning change) and keeps running; its own delete releases what it bound."""
+ import copy
+ from gateway import run
+ from gateway.session_contract import SessionRef
+ from gateway.session_policy import policy_for_source
+ owner, connection, mutate, key = await _branching_owner(tmp_path, monkeypatch)
+ try:
+ parent = _create(owner, connection, tmp_path, api_key)
+ child = SessionRef(parent.profile_id, (await mutate(parent, 'branch'))['branched_session_id'])
+ await mutate(parent, 'delete')
+ rotated = copy.deepcopy(run._load_gateway_config())
+ rotated['model']['api_key'] = 'rotated-config-secret' # only the frozen copy still answers
+ monkeypatch.setattr(run, '_load_gateway_config', lambda *a, **k: rotated)
+ await mutate(child, 'reasoning', {'effort': 'high'})
+ assert key(child) == api_key
+ assert policy_for_source(owner.runner, owner.sessions[child.session_id].source).config(owner)['model'][
+ 'api_key'] == 'config-secret'
+ await mutate(child, 'delete')
+ assert not owner.sessions
+ assert all(child.session_id not in ref for ref in (*owner._local_launch_keys, *owner._local_config_secrets))
+ finally:
+ await connection.close()
+ owner.db.close()
+
+
[email protected]('operation', ['reasoning', 'delete'])
+async def test_an_unpublished_edit_keeps_the_secret_it_reuses(tmp_path, monkeypatch, operation):
+ """Another session's edit or delete while an edit commits never revokes the secret that edit
+ rebound into (here the parent's own, which a deleted branch's descendant also names), or the
+ committed policy names a slot that is gone."""
+ import asyncio
+ import copy
+ import threading
+ from gateway import run, session_mutations
+ from gateway.session_contract import SessionRef
+ from gateway.session_policy import policy_for_source
+ owner, connection, mutate, _ = await _branching_owner(tmp_path, monkeypatch)
+ entered, release, pending = threading.Event(), threading.Event(), None
+
+ def policy(ref):
+ return policy_for_source(owner.runner, owner.sessions[ref.session_id].source)
+
+ async def reasoning(ref, effort):
+ return await mutate(ref, 'reasoning', {'effort': effort}, request_id='reasoning-' + effort)
+ try:
+ parent = _create(owner, connection, tmp_path, None)
+ await reasoning(parent, 'high')
+ frozen = policy(parent).config_secret_ref
+ child = SessionRef(parent.profile_id, (await mutate(parent, 'branch'))['branched_session_id'])
+ grandchild = SessionRef(parent.profile_id, (await mutate(child, 'branch'))['branched_session_id'])
+ await mutate(child, 'delete') # the grandchild still names the parent's `high` secret
+ await reasoning(parent, 'low')
+ rotated = copy.deepcopy(run._load_gateway_config())
+ rotated['model']['api_key'] = 'rotated-config-secret' # only the frozen copy still answers
+ monkeypatch.setattr(run, '_load_gateway_config', lambda *a, **k: rotated)
+ commit = session_mutations.mutate_runtime_session
+
+ def held_commit(*args, **kwargs):
+ if kwargs['request_id'] == 'reasoning-again' and not kwargs.get('_prepare_only'):
+ entered.set() # prepared and bound, not yet committed or published
+ assert release.wait(10)
+ return commit(*args, **kwargs)
+ monkeypatch.setattr(session_mutations, 'mutate_runtime_session', held_commit)
+ pending = asyncio.create_task(mutate(parent, 'reasoning', {'effort': 'high'}, request_id='reasoning-again'))
+ assert await asyncio.to_thread(entered.wait, 10)
+ await mutate(grandchild, operation, {'effort': 'medium'} if operation == 'reasoning' else None)
+ release.set()
+ await pending
+ assert policy(parent).config_secret_ref == frozen
+ assert policy(parent).config(owner)['model']['api_key'] == 'config-secret'
+ finally:
+ release.set()
+ if pending is not None:
+ await asyncio.gather(pending, return_exceptions=True)
+ await connection.close()
+ owner.db.close()
+
+
[email protected]('api_key', ['launch-parent', None], ids=['launch-key', 'config-secret'])
+async def test_a_prune_keeps_the_parents_secret_for_a_branch_not_yet_published(tmp_path, monkeypatch, api_key):
+ """A prune of a branch never releases its deleted parent's secrets: a grandchild committed off the
+ branch but not yet published (absent from ``authority.sessions``) still names them."""
+ import asyncio
+ import copy
+ import threading
+ from gateway import run, session_mutations
+ from gateway.session_contract import SessionRef
+ from gateway.session_policy import policy_for_source, rebind_launch_key
+ from gateway.session_prune import _apply
+ owner, connection, mutate, key = await _branching_owner(tmp_path, monkeypatch)
+ committed, release, pending = threading.Event(), threading.Event(), None
+ try:
+ parent = _create(owner, connection, tmp_path, api_key)
+ child = SessionRef(parent.profile_id, (await mutate(parent, 'branch'))['branched_session_id'])
+ await mutate(parent, 'delete')
+ if api_key:
+ rebind_launch_key(owner, child, api_key) # the resume re-supply, which main also honours
+ owner.db.end_session(child.session_id, 'tui_shutdown')
+ rotated = copy.deepcopy(run._load_gateway_config())
+ rotated['model']['api_key'] = 'rotated-config-secret' # only the frozen copy still answers
+ monkeypatch.setattr(run, '_load_gateway_config', lambda *a, **k: rotated)
+ commit = session_mutations.mutate_runtime_session
+
+ def held_publication(*args, **kwargs):
+ result = commit(*args, **kwargs)
+ if kwargs['request_id'] == 'held-branch' and not kwargs.get('_prepare_only'):
+ committed.set() # committed, not yet restored into the owner
+ assert release.wait(10)
+ return result
+ monkeypatch.setattr(session_mutations, 'mutate_runtime_session', held_publication)
+ pending = asyncio.create_task(mutate(child, 'branch', request_id='held-branch'))
+ assert await asyncio.to_thread(committed.wait, 10)
+ assert (await _apply(owner, {'older_than_days': None, 'chat_id': child.session_id}, None))['deleted'] == 1
+ release.set()
+ grandchild = SessionRef(parent.profile_id, (await pending)['branched_session_id'])
+ if api_key:
+ assert key(grandchild) == api_key
+ else:
+ assert policy_for_source(owner.runner, owner.sessions[grandchild.session_id].source).config(
+ owner)['model']['api_key'] == 'config-secret'
+ finally:
+ release.set()
+ if pending is not None:
+ await asyncio.gather(pending, return_exceptions=True)
+ await connection.close()
+ owner.db.close()