The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(vault): session teardown still fences a late unlock after the TTL dropped its token
Teknium6400c96deb54
diff --git a/agent/vault_backends/unlock.py b/agent/vault_backends/unlock.pyindex 9016760f271..31ff7d5714f 100644--- a/agent/vault_backends/unlock.py+++ b/agent/vault_backends/unlock.py@@ -94,7 +94,6 @@ _Key = tuple[str, str] # (profile home, backend)class _Token:value: str = field(repr=False)last_used: float- owner: Optional[str] # the gateway session that unlocked; released when THAT session endsclass _TokenOps(NamedTuple):@@ -112,14 +111,20 @@ def _token_store() -> _TokenOps:The lock generation per key is bumped by every lock, and an unlock that started before the bumpmust not commit its token afterwards (a slow `bw unlock` child would otherwise silently undo anacknowledged Lock). Sibling sessions in a profile never release each other's tokens.++ ``owners`` (which gateway session unlocked a key) is revocation bookkeeping and outlives the token:+ the TTL drops only the secret, so a session ending after its token expired still bumps the+ generation and fences a replacement unlock that is in flight."""guard = threading.Lock()tokens: dict[_Key, _Token] = {}+ owners: dict[_Key, Optional[str]] = {}generations: dict[_Key, int] = {}timer: Optional[threading.Timer] = Nonedef forget(key: _Key) -> None:tokens.pop(key, None)+ owners.pop(key, None)generations[key] = generations.get(key, 0) + 1def arm(now: float) -> None:@@ -171,7 +176,8 @@ def _token_store() -> _TokenOps:if generation is not None and generation != generations.get(key, 0):return Falsenow = time.monotonic()- tokens[key] = _Token(value, now, owner)+ tokens[key] = _Token(value, now)+ owners[key] = ownerarm(now)return True@@ -179,7 +185,7 @@ def _token_store() -> _TokenOps:with guard:# Bump the generation for every key the lock names (not only the ones holding a token):# an unlock that is still running for this backend must see the lock when it returns.- keys = {k for k in [*tokens, *generations] if k[0] == home and (backend is None or k[1] == backend)}+ keys = {k for k in [*tokens, *owners, *generations] if k[0] == home and (backend is None or k[1] == backend)}if backend is not None:keys.add((home, backend))for key in keys:@@ -188,13 +194,13 @@ def _token_store() -> _TokenOps:def release(session_id: str) -> None:with guard:- for key in [k for k, t in tokens.items() if t.owner == session_id]:+ for key in [k for k, owner in owners.items() if owner == session_id]:forget(key)settle()def lock_everything() -> None:with guard:- for key in list(tokens):+ for key in {*tokens, *owners}:forget(key)settle()diff --git a/tests/agent/test_vault_unlock_tokens.py b/tests/agent/test_vault_unlock_tokens.pyindex afe3d38a165..5adc3ca5750 100644--- a/tests/agent/test_vault_unlock_tokens.py+++ b/tests/agent/test_vault_unlock_tokens.py@@ -78,6 +78,37 @@ def test_idle_token_is_dropped_at_the_ttl_without_another_vault_call(monkeypatchwhat="an expired token released without waiting for the next vault call")+def test_session_teardown_fences_a_late_unlock_after_its_token_expired(monkeypatch):+ """The TTL drops the token bytes, not the record of which session owned that login: a replacement+ unlock still running when the owning session ends must not commit afterwards."""+ monkeypatch.setattr(unlock, "_IDLE_TTL_S", 0.05)+ unlock.set_current_session_id("dummy-session")+ try:+ assert unlock.store_session_token("bitwarden", f"dummy-old-{uuid.uuid4().hex}")+ generation = unlock.begin_unlock("bitwarden") # e.g. a slow `bw unlock` child+ wait_until(lambda: not unlock.is_unlocked("bitwarden"), timeout=5, what="the old token expires")+ unlock.release_session("dummy-session")+ assert not unlock.store_session_token("bitwarden", f"dummy-late-{uuid.uuid4().hex}", generation)+ assert not unlock.is_unlocked("bitwarden")+ finally:+ unlock.set_current_session_id(None)+++def test_sibling_session_teardown_keeps_a_login_after_an_earlier_token_expired(monkeypatch):+ monkeypatch.setattr(unlock, "_IDLE_TTL_S", 0.05)+ try:+ unlock.set_current_session_id("dummy-first")+ assert unlock.store_session_token("bitwarden", f"dummy-first-{uuid.uuid4().hex}")+ wait_until(lambda: not unlock.is_unlocked("bitwarden"), timeout=5, what="the first token expires")+ monkeypatch.setattr(unlock, "_IDLE_TTL_S", 60)+ unlock.set_current_session_id("dummy-second")+ assert unlock.store_session_token("bitwarden", f"dummy-second-{uuid.uuid4().hex}")+ unlock.release_session("dummy-first")+ assert unlock.is_unlocked("bitwarden"), "the first session ending must not revoke the second's login"+ finally:+ unlock.set_current_session_id(None)++# A stand-in `bw` that logs the environment of every invocation beside itself._FAKE_BW = r'''#!/usr/bin/env python3import json, os, sys