Skip to content

fix(webhooks): public webhook won't fire schedules of soft-deleted agents (#1423) - #1433

Merged
vybe merged 1 commit into
devfrom
feature/1423-webhook-softdelete-guard
Jul 3, 2026
Merged

vybe merged 1 commit into
devfrom
feature/1423-webhook-softdelete-guard

Conversation

@dolho

@dolho dolho commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

get_schedule_by_webhook_token() filtered only agent_schedules.deleted_at IS NULL — it did not check whether the schedule's agent was soft-deleted (agent_ownership.deleted_at). So POST /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 JOINs agent_ownership and filters agent_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_ownership join + agent_ownership.c.deleted_at.is_(None) predicate to get_schedule_by_webhook_token(), mirroring list_all_enabled_schedules(). A soft-deleted agent's token now returns no schedule → the router 404s before any scheduler dispatch, so no schedule_executions row 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).
  • Recovering the agent restores webhook triggering.
  • Regression test in tests/test_webhook_triggers.py.

Verification — live, against a running backend

live webhook            -> 202
soft-delete agent       -> 200
webhook (soft-deleted)  -> 404   <-- the fix
recover agent           -> 200
webhook (recovered)     -> 202   <-- fires again

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

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]>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@dolho
dolho requested a review from vybe July 3, 2026 09:44

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
vybe merged commit 2907e7c into dev Jul 3, 2026
19 checks passed
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]>
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