Skip to content

Failure events 3/4: Python SDK failure hook - #200

Draft
shawnburke wants to merge 1 commit into
shawn/failure-events-2-managerfrom
shawn/failure-events-3-python
Draft

shawnburke wants to merge 1 commit into
shawn/failure-events-2-managerfrom
shawn/failure-events-3-python

Conversation

@shawnburke

@shawnburke shawnburke commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Part 3 of 4, split from #190 (COR2-1098). This PR adds on_failure to the Python SDK. Based on #199.

Stack:

  1. Failure events 1/4: protocol, and one path for every invocation failure #198 Protocol and one failure path
  2. Failure events 2/4: FailureManager and the DispatchEvents stream #199 FailureManager and DispatchEvents
  3. Failure events 3/4: Python SDK failure hook #200 Python SDK failure hook (this PR)
  4. Failure events 4/4: Go SDK failure hook #201 Go SDK failure hook

The hook receives the original message and a reason that separates handler failures from Axon failures:

Scenario Detected by reason source
Handler raises SDK HANDLER_ERROR APPLICATION
Handler runs past its timeout SDK TIMEOUT APPLICATION, or RUNTIME if it was waiting on a Cortex API call or for a free handler
Invocation never answered Agent TIMEOUT RUNTIME
Agent queue full (webhook got 503) Agent NO_RESOURCES RUNTIME, caller_rejected=true
Expired in the agent queue Agent NO_RESOURCES RUNTIME

Usage

from cortex_axon import AxonClient, FailureAction, FailureSource

def on_failure(info, failure):
    dead_letter_topic.publish({
        "invocation_id": info.invocation_id,
        "handler": info.handler_name,
        "attempt": info.attempt,
        "reason": failure.reason.name,
        "source": failure.source.name,
        "caller_rejected": failure.caller_rejected,
        "message": info.args,
    })
    if failure.source == FailureSource.RUNTIME and info.attempt < 3:
        return FailureAction.RETRY
    return FailureAction.DROP

AxonClient(scope=globals(), on_failure=on_failure).run()

Behavior

  • The SDK only reports and answers. It classifies its own failures into ReportInvocationRequest.failure, and runs the hook only for events from the agent. The hook's return value is the answer. Returning None or raising means DROP.
  • Events are read on their own thread and run on their own pool, so they never wait for a handler slot.
  • At the deadline, the SDK reports the timeout itself and drops anything the handler does afterwards, since a thread can't be stopped. It does not start an invocation past its deadline.
  • With an older agent, DispatchEvents answers Unimplemented. The SDK logs once and stops asking.
  • The Python workflow now sets up Go, because the e2e tests build and run the real agent. Without Go, they skip.

Test plan

  • ruff clean; locally 28 passed and the 3 e2e tests skipped (no Go modules in my environment).
  • CI (with Go), including e2e against a real agent:
    • a handler error and a timeout each go SDK report → agent → hook → RETRY, and the retry succeeds with the original body;
    • saturation: one slot is stuck, the agent reports the queued invocations, the hook retries them, and each message succeeds exactly once.
  • Unit tests:
    • the reported reason/source mapping;
    • the hook is not run locally;
    • a slow handler is APPLICATION, and its late result is dropped;
    • an expired invocation is not started;
    • event → hook → answer, where nothing returned or a raise means DROP;
    • 20 events are answered while every handler slot is held;
    • an older agent is handled.

🤖 Generated with Claude Code

The third of four PRs split from #190.

AxonClient takes on_failure. The hook runs for each failure event the agent
sends on DispatchEvents, and returns FailureAction.DROP or RETRY, which is
sent back as the answer. Returning None or raising means DROP. Events are
read on their own thread and run on their own pool, so they arrive even
while every handler slot is busy.

The SDK does not run the hook for its own failures. It classifies them
into ReportInvocationRequest.failure (APPLICATION or RUNTIME; a timeout
during a Cortex API call or while waiting for a free handler is RUNTIME),
and the agent delivers them.

At the deadline the SDK reports the timeout itself and drops whatever the
handler does afterwards, since a Python thread can't be stopped. It does
not start an invocation past its deadline.

An agent without DispatchEvents answers Unimplemented; the SDK logs once
and does not ask again.

The end-to-end tests build and run the real agent, so the workflow now
sets up Go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@engrams-agent
engrams-agent Bot force-pushed the shawn/failure-events-3-python branch from 0102204 to 82c193f Compare November 20, 2026 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant