Skip to content

fix(triggers): bound host-executed trigger runs with a deadline (toby) - #4135

Merged
OliverBryant merged 2 commits into
xorbitsai:mainfrom
OliverBryant:fix/host-trigger-run-deadline
Dec 3, 2026
Merged

OliverBryant merged 2 commits into
xorbitsai:mainfrom
OliverBryant:fix/host-trigger-run-deadline

Conversation

@OliverBryant

Copy link
Copy Markdown
Contributor

Part of xorbitsai/xagent-saas#3607

The engine awaited a host trigger executor with no deadline, so a hung host run stayed RUNNING forever and held its release-ledger slot (found in review of xorbitsai/xagent-saas#3754).

  • Host executors now run under XAGENT_HOST_TRIGGER_RUN_TIMEOUT_SECONDS (default 1800s, the same as the host's scheduled-run deadline; must be positive and finite).
  • On expiry the executor coroutine is cancelled (one that ignores cancellation is abandoned after a 10s grace), the run is settled FAILED with TimeoutError: host executor exceeded the <n>s run deadline, the trigger's last_error is set, and the ledger slot is released as before.
  • A host that finishes late cannot overwrite the terminal state: the run is settled once, and the terminal write only moves RUNNING rows.
  • Triggers without a host executor take the unchanged path.

Tests: normal completion, hung run (cancelled + failed + lease released), and a host that swallows cancellation and finishes late. Mutation-checked: no deadline, no cancel, a removed status guard and awaiting an uncancellable host all fail the tests.

The engine awaited a host trigger executor with no limit, so a hung host
run stayed RUNNING forever and held its release-ledger slot. Run the
executor under XAGENT_HOST_TRIGGER_RUN_TIMEOUT_SECONDS (default 1800s,
matching the host's scheduled-run deadline). On expiry cancel the
coroutine (abandoning one that ignores cancellation after a short grace),
settle the run FAILED with a timeout reason, and release the ledger slot.
A late-finishing host cannot overwrite the terminal state: the terminal
write only moves RUNNING rows.

@rogercloud rogercloud left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major

  • src/xagent/web/services/triggers.py:2321 — Abandoning an executor that ignores cancellation (deadline or shutdown path) ends the lifecycle task, so release_hold.adopt frees the ledger slot while host code is still running and unsettled_total stays at 0; a planned release then reads the process as drained/settled. Call release_hold.record_unsettled(...) when abandoning (and keep a reference to the task, see inline).

Minor

  • src/xagent/web/services/triggers.py:2346 — A malformed XAGENT_HOST_TRIGGER_RUN_TIMEOUT_SECONDS passes boot and fails every host run as "Host executor failed". Validate it at startup alongside validate_activation_timeouts.
  • src/xagent/web/services/triggers.py:2362 — A second cancel during the deadline grace wait skips abandon/exception retrieval and records "Cancelled" instead of the timeout. The outer-cancel branch also never retrieves an exception raised during the grace. One _cancel_with_grace helper fixes both and removes the duplicated sequence.
  • src/xagent/web/services/trigger_executors.py:42 — The host-facing contract still says the callable "may run for a long time". Document the deadline, that cancellation must be honoured, the 10s abandon grace, and that the executor now runs in its own Task.
  • tests/web/api/test_host_executed_triggers.py:902 — The late-host test depends on sleep margins, it re-tests the pre-existing RUNNING guard by hand, and the shutdown path with a stubborn executor is untested.
  • example.env:2263 — The new entry sits inside the activation-bounds block; move it next to XAGENT_TRIGGER_DISPATCHER_*.

Simplification

  • tests/web/api/test_host_executed_triggers.py L886: shrink: the stubborn test inlines the fire_trigger + session open/close sequence. Call await _fire_host_run_async(trigger_id, "evt-late").
    net: -12 lines possible

Blocking: no — recommended event: APPROVE

Comment thread src/xagent/web/services/triggers.py Resolved
Comment thread src/xagent/web/services/triggers.py Resolved
Comment thread src/xagent/web/services/triggers.py Resolved
…ate the run timeout at boot

Abandoning an executor that ignores cancellation now records the run as
unsettled in the release ledger, the cancel-and-grace logic is shared by
the deadline and shutdown paths (always consuming the task outcome and
keeping the timeout reason), and the run timeout is validated at startup
with the activation timeouts.
@OliverBryant
OliverBryant added this pull request to the merge queue Dec 3, 2026
Merged via the queue into xorbitsai:main with commit 48faaa2 Dec 3, 2026
94 checks passed
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.

2 participants