Skip to content

The agent that grows with you

fix(gateway): recover a stale whole-policy config stamp per fingerprint

liuhao10241cd1f5935465
diff --git a/gateway/session_policy_credentials.py b/gateway/session_policy_credentials.py
index 0e889bd985f..74cf98f8868 100644
--- a/gateway/session_policy_credentials.py
+++ b/gateway/session_policy_credentials.py
@@ -112,9 +112,20 @@ def recover_config_secrets(authority, policy, skip=None):
try:
source = json.loads(ref[len(PREFIX):])
home = Path(authority.db.db_path).resolve().parent
- if (source['home'] != str(home) or source['profile'] != authority.profile_id
- or source['policy'] != policy_identity(policy)):
- raise ValueError('scope mismatch')
+ if source['home'] != str(home) or source['profile'] != authority.profile_id:
+ raise RuntimeStoreError('launch_credentials_unavailable',
+ field='home' if source['home'] != str(home) else 'profile',
+ detail='receipt scope mismatch: the frozen config reference belongs to another '
+ + ('home' if source['home'] != str(home) else 'profile'))
+ if source['policy'] != policy_identity(policy):
+ # The stamp covers the whole frozen config_json, secret fields and not: a profile edit
+ # that rewrote the snapshot after the reference froze (a repointed base_url, keys
+ # unchanged) fails it without touching a single secret, stranding the session on every
+ # turn -- and on the /model re-stamp and the policy refresh that would repair it. The
+ # per-entry fingerprints below still gate every value against the live config.yaml and
+ # still refuse a changed route credential, so a stale stamp degrades to that check.
+ logger.warning('Session %s: frozen config reference predates the current policy snapshot; '
+ 'verifying its entries per fingerprint', source.get('session'))
cached = getattr(authority, '_local_config_secrets', {}).get(ref)
if cached is not None:
return {path: value for path, value in cached.items() if path[0] not in skip}
diff --git a/tests/gateway/test_config_policy_recovery.py b/tests/gateway/test_config_policy_recovery.py
index e9e72c5e5fd..a338fefee85 100644
--- a/tests/gateway/test_config_policy_recovery.py
+++ b/tests/gateway/test_config_policy_recovery.py
@@ -23,13 +23,20 @@ def test_config_credentials_recover_only_exact_source(tmp_path, monkeypatch):
policy = bind_launch_key(owner(instance='old'), 'session', policy, None, config_secrets=private)
assert policy.config(owner())['model']['api_key'] == cfg['model']['api_key']
assert cfg['model']['api_key'] not in json.dumps(asdict(policy))
- for wrong in (owner(profile='wrong'), owner(path=tmp_path)):
- with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable'):
+ for wrong, mismatched in ((owner(profile='wrong'), 'profile'), (owner(path=tmp_path), 'home')):
+ with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable') as refused:
policy.config(wrong)
- with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable'):
- replace(policy, model='other-policy').config(owner())
+ assert refused.value.field == mismatched
+ # A whole-policy stamp that predates the current snapshot (a profile edit rewrote the frozen
+ # config after the reference froze -- a repointed base_url, keys unchanged) degrades to
+ # per-entry verification instead of stranding the session: the recovered values are still
+ # fingerprint-gated against the live config.yaml.
+ stale = replace(policy, model='other-policy')
+ assert stale.config(owner())['model']['api_key'] == cfg['model']['api_key']
cfg['model']['api_key'] = 'changed-unrelated-token'
(home / 'config.yaml').write_text(json.dumps(cfg))
+ with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable'):
+ stale.config(owner())
with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable'):
policy.config(owner())