The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webhook): validate physical routes when finalizing compressed sessions
unsupportedpastels9660198b9e2e
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.pyindex 5f62466ef38..cde918a4f1c 100644--- a/gateway/platforms/webhook_ingress.py+++ b/gateway/platforms/webhook_ingress.py@@ -97,6 +97,7 @@ def finalize_webhook(authority, receipt):async def recover_webhook_finalizations(authority):"""Close settled original webhooks only after current route authorization succeeds."""from gateway.session_envelope import check_native_route+ from gateway.session_contract import SessionReffrom hermes_state_runtime import RuntimeStoreError, get_session_admissionrows = authority.db._read_all("""@@ -120,7 +121,8 @@ async def recover_webhook_finalizations(authority):if entry is None or authority.logical_owner(entry.session_id) != sid:raise RuntimeStoreError('admission_conflict')adapter = authority.runner._adapter_for_source(entry.origin) if entry.origin is not None else None- await check_native_route(authority.runner, row['payload'], sid, entry.origin, adapter)+ target = authority.physical_target(SessionRef(authority.profile_id, sid))+ await check_native_route(authority.runner, row['payload'], target, entry.origin, adapter)results[sid] = 'finalized' if finalize_webhook(authority, authority._receipt(row)) else 'unchanged'except (KeyError, RuntimeStoreError):diff --git a/tests/gateway/test_webhook_finalization_recovery.py b/tests/gateway/test_webhook_finalization_recovery.pyindex 39ab48a8956..6d789470b28 100644--- a/tests/gateway/test_webhook_finalization_recovery.py+++ b/tests/gateway/test_webhook_finalization_recovery.py@@ -16,7 +16,7 @@ from tests.gateway.fixtures.webhook_route_authority import mount_authority@asynccontextmanager-async def settled_before_finalizer():+async def settled_before_finalizer(*, compress=False):config = PlatformConfig(enabled=True, extra={"secret": "owned-secret","routes": {"fixture": {"prompt": "{text}", "deliver": "log"}},@@ -37,10 +37,30 @@ async def settled_before_finalizer():assert response.status == 202, await response.text()runner = adapter._message_handler.__self__authority = runner.session_authority- row = authority.db._read_one("SELECT * FROM session_admissions")+ row = dict(authority.db._read_one("SELECT * FROM session_admissions"))claimed = claim_session_input(authority.db, epoch=authority.epoch, session_id=row["target_session_id"])+ if compress:+ logical = row["target_session_id"]+ entry = runner.session_store.lookup_by_session_id(logical)+ physical = logical + "-compressed"+ authority.db.publish_compression_child(+ parent_session_id=logical,+ child_session_id=physical,+ source="webhook",+ messages=[{"role": "user", "content": "retained summary"}],+ require_compression_lease=False,+ )+ assert runner.session_store.advance_compression_session(+ entry.session_key, logical, physical+ ) is not None+ # The logical webhook still owns terminal settlement/finalization+ # after its physical transcript rotates.+ authority.db._write_sql(+ "UPDATE sessions SET ended_at=NULL WHERE id=?", (logical,)+ )+ row["physical_session_id"] = physicalsettle_session_input(authority.db,epoch=authority.epoch,@@ -87,6 +107,56 @@ async def test_restart_finalizes_settled_webhook_without_provider_retry(monkeypaassert len(authority.db._read_all("SELECT * FROM session_admissions")) == 1+async def test_restart_reauthorizes_compressed_route_but_finalizes_logical_webhook():+ async with settled_before_finalizer(compress=True) as (+ _client, _body, _headers, runner, authority, row+ ):+ from gateway.platforms.webhook_ingress import recover_webhook_finalizations+ from gateway.session_authority import initialize_session_authority++ logical = row["target_session_id"]+ physical = row["physical_session_id"]+ entry = runner.session_store.lookup_by_session_id(physical)+ unrelated = physical + "-branch"+ authority.db.create_session(+ unrelated,+ source="webhook",+ parent_session_id=physical,+ model_config={"_branched_from": physical},+ )+ unrelated_before = authority.db.get_session(unrelated)++ restarted = await initialize_session_authority(+ runner,+ profile_id=authority.profile_id,+ instance_id="replacement-owner",+ db=authority.db,+ )+ scheduled = []+ restarted._schedule = scheduled.append+ sent = []++ async def forbid_send(*args, **kwargs):+ sent.append((args, kwargs))++ adapter = runner._adapter_for_source(entry.origin)+ adapter.send = forbid_send++ results = await recover_webhook_finalizations(restarted)++ assert results == {logical: "finalized"}+ assert authority.db.get_session(logical)["end_reason"] == "webhook_complete"+ assert authority.db.get_session(physical)["ended_at"] is None+ assert authority.db.get_session(unrelated) == unrelated_before+ assert scheduled == []+ assert sent == []+ admissions = authority.db._read_all("SELECT * FROM session_admissions")+ assert len(admissions) == 1+ assert admissions[0]["target_session_id"] == logical+ assert admissions[0]["status"] == "terminal"++@pytest.mark.asyncioasync def test_duplicate_request_finalizes_settled_webhook_without_reexecution():async with settled_before_finalizer() as (client, body, headers, _runner, authority, row):