Skip to content

fix(gateway): follow async delegation completions across compression - #69312

Merged
teknium1 merged 3 commits into
mainfrom
salvage/64530-delegation-compression
Jul 22, 2026
Merged

fix(gateway): follow async delegation completions across compression#69312
teknium1 merged 3 commits into
mainfrom
salvage/64530-delegation-compression

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

Async delegation completions now reach the user after a context-compression rotation instead of being silently dropped, and undeliverable completions get an honest durable disposition instead of being falsely marked delivered. Root cause: the #55578 fail-closed pinning guard treated a compression-ended parent session like an explicitly-ended one (dropping the completion without consulting end_reason/get_compression_tip), while _deliver_completion_notification acknowledged the durable row as delivered on mere adapter acceptance — so drops inside the pipeline were unrecoverable.

Changes

  • gateway/run.py: new _resolve_async_delegation_session() — resolves a pinned parent to its verified live compression tip (live → keep pin; compression-ended → follow SessionDB.get_compression_tip to a verified live continuation; anything else fails closed), with a lineage-ownership check so a late completion can never override an unrelated /new or restored route; also covers the pinned==current stale-route bypass.
  • gateway/session.py: new SessionStore.advance_compression_session() — CAS route-advance along an already-verified compression lineage; deliberately NOT switch_session, whose end/reopen side effects are wrong for a compression heal.
  • gateway/run.py: pre-flight _classify_completion_target() in _deliver_completion_notification — deliver / terminal / retry verdict before adapter acceptance, so the durable ack stays honest.
  • tools/async_delegation.py: new drop_completion_delivery() terminal disposition for permanently-gone targets (explicit /new-reset boundaries); release_completion_delivery() now converges to a terminal dropped state after _MAX_DELIVERY_ATTEMPTS (8) so an undeliverable row can't replay on every gateway restart forever.
  • Tests: 9 resolver scenarios + a real-SessionDB lineage test (test_async_delegation_session_binding.py), 3 CAS route-advance tests (test_session_store_runtime_stale_guard.py), 4 ack-semantics tests incl. attempt-cap churn (test_completion_delivery.py).

Validation

Before After
Delegation completes after compression rotation Completion silently dropped (parent has ended_at) Delivered to the verified live continuation session
Fail-closed drop of a claimed durable completion Falsely marked delivered on adapter acceptance Terminal dropped (explicit reset) or pending retry with attempt cap
Undeliverable completion across restarts Replayed and re-dropped on every boot Converges to terminal dropped after 8 attempts

Targeted tests: tests/gateway/ -k 'delegation or completion or compression' → 160 passed, 0 failed; async-delegation consumer suites (12 files) → 921 passed, 0 failed.

Credit

Salvaged from #64530 by @richkapp (first submitter). Ack-semantics hardening adapted from #65838 by @henrynguyeninfo1. Root-cause diagnosis credit: @allenkaplan (#65779, #65780). Also fixes the same bug reported in #65820 by @heyparth1. Fixes #65779.

Infographic

delegation-completions-across-compression

richkapp and others added 3 commits July 22, 2026 05:09
…mpletions

Adapter acceptance is not proof of delivery: the inner #55578 resolver can
still fail closed inside the message pipeline after the adapter accepted the
synthetic event, which falsely acknowledged the durable row as delivered and
silently discarded the delegation result.

Pre-flight the delivery target in _deliver_completion_notification before
adapter acceptance (adapted from #65838 by @henrynguyeninfo1):
- live parent or verified live compression tip -> deliver (inner resolver
  still owns the actual route retarget)
- explicit-reset / unknown parent -> terminal 'dropped' disposition via new
  drop_completion_delivery() (not falsely 'delivered', not eternally
  'pending')
- transient uncertainty (DB error, mid-flight rotation without a visible
  continuation) -> release the claim for retry

release_completion_delivery() now converges to a terminal 'dropped' state
once _MAX_DELIVERY_ATTEMPTS is exhausted, so an undeliverable completion
cannot replay on every gateway restart forever.
@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

running on dc2342b

CI timings

CI timings · View job

Wall time 8m27s vs 7m46s (+8.8%). 5 job(s) slower, 13 faster, 2 unchanged.

  • Build&Test Docker image / build (amd64, ubuntu-latest, linux/amd64, type=gha,scope=docker-amd64, type=gha,mode=max,scope=do...: +66.0s
  • Python tests / Run tests slice 2/8: -15.0s
  • Python tests / Run tests slice 7/8: +13.0s
  • OSV scan / Scan lockfiles / osv-scan: -13.0s
  • Python tests / Run tests slice 6/8: -11.0s

@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery tool/delegate Subagent delegation area/compression Context compression and continuation sessions P2 Medium — degraded but workaround exists needs-decision Awaiting maintainer decision before any implementation sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 22, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Related to open #64530 and #55578. This current diff contains the same compression-tip/CAS routing repair as #64530 but also adds target classification and durable terminal/retry handling; it is broader competing work rather than a redundant same-policy patch.

@teknium1
teknium1 merged commit b120121 into main Jul 22, 2026
39 checks passed
@teknium1
teknium1 deleted the salvage/64530-delegation-compression branch July 22, 2026 13:56
@richkapp

Copy link
Copy Markdown
Contributor

Big thank you for preserving the credit and improving the original fix instead of replacing it. <3 I'm officially a contributor. Feels good to help something that you use every day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/compression Context compression and continuation sessions comp/gateway Gateway runner, session dispatch, delivery needs-decision Awaiting maintainer decision before any implementation P2 Medium — degraded but workaround exists sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/delegate Subagent delegation type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async delegation completion is dropped after context compression and marked delivered

3 participants