Repository navigation
fix(webhooks): public webhook won't fire schedules of soft-deleted agents (#1423) - #1433
Merged
Merged
Conversation
get_schedule_by_webhook_token() filtered only agent_schedules.deleted_at
IS NULL — it did NOT check the owning agent's soft-delete state
(agent_ownership.deleted_at). So POST /api/webhooks/{token} could still
fire a schedule of a soft-deleted (recoverable) agent throughout the
retention window (default 180d), re-opening the exact hole the cron path
(list_all_enabled_schedules) guards against and violating the #834
soft-delete invariant that schedules stop firing immediately.
Parity fix: JOIN agent_ownership and add
agent_ownership.c.deleted_at.is_(None), mirroring list_all_enabled_schedules.
A soft-deleted agent's token now 404s (before any scheduler dispatch, so no
schedule_executions row is created); admin recovery restores triggering.
Verified live against a running backend: live webhook -> 202; soft-delete
agent -> webhook 404; recover -> webhook 202 again.
- src/backend/db/schedules.py: agent-ownership join + deleted_at guard
- tests/test_webhook_triggers.py: regression test (soft-delete -> 404,
recover -> fires) on a throwaway agent
Related to #1423
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Resolve by running |
vybe
approved these changes
Jul 3, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
Validated via /validate-pr: webhook token lookup now applies the same two soft-delete filters as the cron path; named regression test covers 404-then-recover. All required checks green, security sweep clean.
vybe
pushed a commit
that referenced
this pull request
Jul 6, 2026
…1445) (#1453) * fix(webhooks): gate schedule/webhook creation on a live agent (#1445) Root cause: PR #1433 gave get_schedule_by_webhook_token an INNER JOIN on agent_ownership, so a schedule whose agent has no live ownership row now 404s at token lookup. can_user_access_agent returns True unconditionally for admins with no existence check, so admin callers could mint orphan schedules (with real webhook tokens) on never-created agents — tokens that then 404 deterministically. This is the source of the "intermittent under load" webhook 404s: a test-fixture artifact, not concurrency. Fix at the creation boundary, fail-loud, keeping #1433's INNER JOIN intact: - New db.is_agent_live(name) predicate: live agent_ownership row, deleted_at IS NULL, no users join — matches the token-lookup predicate exactly (get_agent_owner joins users → false-negative on a live agent with a missing owner row). - routers/schedules.create_schedule: access-check FIRST (uniform 403, no 404-vs-403 enumeration oracle), then is_agent_live 404, before the #929 timeout check. - routers/schedules.generate_webhook: is_agent_live 404 after the access check (defense-in-depth, not a pre-auth probe). - db/schedules.create_schedule: chokepoint guard (is_agent_live → None → 403) so the no-orphan invariant holds for every caller. - webhooks.py: static, neutral per-branch 404 log lines (no token interpolation) so the next occurrence is grep-attributable. Co-Authored-By: Claude Opus 4.8 <[email protected]> * test(webhooks): harden webhook tests off orphan agents; gate regression (#1445) - Move the WHOLE module off the name-only `test_agent_name` fixture (which creates no ownership row) onto a module-scoped `webhook_agent` fixture that POSTs a real agent (live ownership row, no flaky running-wait, mirrors the #1423 throwaway-agent pattern). Post-gate, `_create_test_schedule` asserts 201, so every test — generation AND trigger — needs a real agent. - New TestWebhookCreationGate: creating a schedule on a never-created agent now 404s (was 201 → orphan); generate_webhook on a soft-deleted agent 404s. - Fix the direct route-handler unit tests (test_929) to stub the new db.can_user_access_agent / db.is_agent_live gate so they reach the timeout-enforcement path under test. Co-Authored-By: Claude Opus 4.8 <[email protected]> * docs(webhooks): document the live-agent creation gate (#1445) - architecture.md WEBHOOK-001: creation gate note (is_agent_live, 404/403 ordering, chokepoint) — idempotency sentence untouched (#1437's territory) - requirements/public-access.md §15.1g: creation-gated-on-live-agent bullet - feature-flows/webhook-triggers.md: management-endpoint auth + creation-gate paragraph; failure-mode table notes (malformed/lookup-miss log lines, orphan schedules no longer creatable) - feature-flows.md index: Recent Updates row - user-docs/api-reference/webhook-triggers.md: add the missing WEBHOOK-001 schedule-webhook endpoints + the public creation precondition Co-Authored-By: Claude Opus 4.8 <[email protected]> * test(webhooks): unit-pin the #1445 access-first ordering + no-orphan 404 Two importlib route-handler tests (no live stack): - access-check-first => uniform 403 with is_agent_live never consulted (enumeration-oracle guard, plan Decision #5) - authorised caller on a dead agent => 404, db.create_schedule never reached Co-Authored-By: Claude Opus 4.8 <[email protected]> * docs(webhooks): sync flow doc — is_agent_live gate + 404-branch logs (#1445) /sync-feature-flows pass: DB Methods table gains the is_agent_live gate predicate and spells out the #1423 soft-delete-aware token-lookup INNER JOIN; the public-trigger steps note the static per-branch 404 log lines. Co-Authored-By: Claude Opus 4.8 <[email protected]> * test(webhooks): DB-layer #1445 gate unit tests; fix flow-index issue ref Add tests/unit/test_1445_schedule_creation_gate.py — the backend-agnostic (db_harness) coverage the router/integration tests can't reach: is_agent_live (no users-join, matches the token-lookup predicate; live agent with a missing owner-user row still reads live) and the create_schedule chokepoint (no-orphan for MCP / system-manifest-deploy / any non-router caller). Fix the feature-flows index row to cite #1423 (the soft-delete token-lookup issue) instead of #1433 (its PR) — matches the code docstring, architecture.md, requirements, and the flow doc. Co-Authored-By: Claude Opus 4.8 <[email protected]> --------- Co-authored-by: Andrii Pasternak <[email protected]> Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Eugene Vyborov <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_schedule_by_webhook_token()filtered onlyagent_schedules.deleted_at IS NULL— it did not check whether the schedule's agent was soft-deleted (agent_ownership.deleted_at). SoPOST /api/webhooks/{token}could still fire a schedule belonging to a soft-deleted (recoverable) agent throughout the retention window (default 180 days), re-opening exactly the hole the cron path guards against and violating the #834 soft-delete invariant that schedules stop firing immediately.The cron path (
list_all_enabled_schedules(), a few methods above) already JOINsagent_ownershipand filtersagent_ownership.deleted_at IS NULL; the webhook lookup just skipped that guard.Fix (
src/backend/db/schedules.py)One-method parity fix: add the
agent_ownershipjoin +agent_ownership.c.deleted_at.is_(None)predicate toget_schedule_by_webhook_token(), mirroringlist_all_enabled_schedules(). A soft-deleted agent's token now returns no schedule → the router 404s before any scheduler dispatch, so noschedule_executionsrow is created. Admin recovery (agent_ownership.deleted_at → NULL) restores triggering.Acceptance criteria
get_schedule_by_webhook_token()returns nothing when the owning agent is soft-deleted.POST /api/webhooks/{token}for a soft-deleted agent's schedule → 404, no execution row (404 is before dispatch).tests/test_webhook_triggers.py.Verification — live, against a running backend
Plus
tests/test_webhook_triggers.py::TestWebhookTrigger::test_soft_deleted_agent_webhook_returns_404_then_recovers(uses a throwaway agent so the shared fixture is untouched).Related to #1423
🤖 Generated with Claude Code
Closes #1423