Skip to content

fix(plugins): reject backend choices without match - #809

Merged
uniqueg merged 5 commits into
devfrom
fix/808-unknown-backend-choices
Nov 21, 2026
Merged

uniqueg merged 5 commits into
devfrom
fix/808-unknown-backend-choices

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Closes #808

Summary

A client's gateway.backends {"type": "allow", ...} that can never match ended without a backend, which apps answer as an outage (proTES: 503), and the client couldn't learn why, as the request isn't stored.

  • PipelineRun.route() now refuses an allow of identifiers only, none of which names a backend of the app, with InvalidSelection (400, invalid-backends, "None of the chosen backends is known"), before the route phase. The message names no client values; the items are recorded as discarded, and the error carries the run (InvalidSelection(run=)), so that apps can store failed runs.
  • Selections with a URL are never refused: discovered backends have their URL as identifier and may be missing while a registry is unavailable, which must stay a 503. order and forbid are unchanged.
  • route() takes known: backends that the app left out of the candidates (e.g., proTES's TES instances that can't run the task), so that choosing them still ends without a backend, which the app can explain. Backends at the gateway's own URL never count.
  • selection.selects_unknown_only() shares the matcher with apply_backend_selection().
  • Docs: docs/plugins.md (per-request choices, using the engine in apps, problem types) and a section in docs/migration-0.23.md. The 400 reveals which identifiers exist, also of backends that routing would keep from a caller; documented as accepted, as the service info usually lists them.

Follow-up for proTES (its FOCA 0.23 bump): pass known= (the TES instances left out as ineligible) in TaskRuns.create_task(), and update its pinning how-to (protes#508 / protes#509) and test_pin_to_unknown_tes_instance for identifiers (400).

Verification

  • pytest tests/plugins without MongoDB: 886 passed, 1 skipped; the MongoDB store tests need a server (they run in CI).
  • New tests in tests/plugins/test_plugin_selection.py: refusal after a request-phase plugin with no route plugin run, error carries the run, provenance; URL and mixed items not refused; known adds to candidates; no backends at all; own URL; selects_unknown_only parametrized. Coverage of selection.py and errors.py: 100%.
  • ruff check, ruff format --check, mypy, ast-grep scan, mkdocs build --strict: clean. semgrep runs in the push hook.
  • CI: all checks passed (tests on Python 3.11 to 3.15 with MongoDB 7.0 and 8.0, lowest dependencies, integration, chart, image scans, docs, Codecov patch and project).
  • Reviews: adversarial and quality reviewers, two rounds; round-1 medium findings fixed (run on the error, registry outages, wording "before the route phase", union semantics, direct tests).

Declined review findings

  • Plugin test harness run_route() can't pass known (low): plugin authors test plugins, not the app's refusal; can follow if needed.
  • run: Any in InvalidSelection (low): as in its sibling errors; typing them all needs a TYPE_CHECKING import across the module, a separate change.
  • Own-URL filter in both route() and _refuse_unknown_selection() (low): kept so that the helper is correct on its own.
  • Docs paragraph length (low): kept as one paragraph, in the style of the section.
  • Sourcery: an allow with an unmatched URL isn't refused (high): deliberate and documented; a URL may name a discovered backend of a registry that is unavailable for now, which must stay "no backend available", as the issue asks for transient cases.
  • Sourcery: a pin of only a backend at the gateway's own URL is refused (medium): deliberate and tested (test_route_allow_of_own_url_is_unknown); the gateway never uses such a backend, so it is unknown, not removed by routing.

Sourcery's third finding (docs/configuration/plugins.md) is fixed in 73d8927 (docs only); its threads are answered and resolved.

🤖 Generated with Claude Code

Summary by Sourcery

Reject client backend selections that can never match a gateway backend while preserving transient availability behavior and giving applications enough context to explain valid-but-unavailable choices.

Bug Fixes:

  • Reject backend allow selections containing only unknown identifiers with a clear 400 invalid-backends error instead of allowing them to fall through as unavailable backends.
  • Preserve transient handling for URL-based selections and retain existing behavior for order, forbid, and known backends that routing later removes.

Enhancements:

  • Allow routing applications to provide known backends omitted from candidates and attach the failed run to invalid-selection errors.
  • Share backend matching logic between selection application and unknown-selection validation.

Documentation:

  • Document unknown backend selection behavior, application use of the known routing argument, and the resulting migration requirements.

Tests:

  • Add coverage for unknown selections, URL and mixed selections, omitted known backends, routing removals, own-URL backends, empty candidate sets, and selection matching.

uniqueg and others added 2 commits November 20, 2026 19:16
A client's `gateway.backends` `allow` that names none of the gateway's
backends ended without a backend, which apps answer as an outage (e.g.,
proTES: 503), and the client couldn't learn why, as the request isn't
stored. `run.route()` now refuses it with `400` (`invalid-backends`)
before any plugin runs, without naming the client's items, and records
them as discarded. `order` and `forbid` are unchanged.

`run.route()` takes `known`, all backends of the app, so that an
`allow` of a backend that the app left out of the candidates (e.g., as
it can't run the request) still ends without a backend. Backends at the
gateway's own URL never count as known.

Closes #808

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review fixes: a URL may name a backend of a registry that is unavailable
for now, so only an `allow` of identifiers alone is refused; `known`
adds to the candidates instead of replacing them; the refusal carries
the run (`InvalidSelection(run=)`), so that apps can store failed runs;
docs say it happens before the `route` phase, not before any plugin, and
that the 400 tells which identifiers exist.

Refs #808

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg self-assigned this Nov 20, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Extend routing to accept backends the app knows but omitted from candidates, and carry the run on invalid-selection errors so callers can record refusals.

  • foca/plugins/engine.py L1599-1603, L1644-1667 (+5 small edits)
  • foca/plugins/errors.py L114-116, L123-127

Implementation

Share backend matching between selection application and the predicate that identifies allow selections consisting only of unknown identifiers.

  • foca/plugins/selection.py L21, L462-470, L476-533

Documentation

Explain the refused backend-choice behavior, migration impact, app use of known backends, and updated problem type.

  • docs/configuration/plugins.md L153-154
  • docs/migration-0.23.md L17-20, L259-280
  • docs/plugins.md L108-120, L960-968, L1274

Tests

Cover refusal of unknown-only identifier selections and verify URL, known, routed-away, omitted, failed, empty-candidate, and own-URL cases.

  • tests/plugins/test_plugin_selection.py L16, L24-26, L346-501

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (eff9eb9) to head (ec33575).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #809   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           76        76           
  Lines        10550     10568   +18     
=========================================
+ Hits         10550     10568   +18     
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 20, 2026 21:14
@sourcery-ai

sourcery-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit ec33575

#808 fix(plugins): reject backend choices without match · 5 of 6 met · 1 to check by hand

# Requirement Status Notes
1 Reject an allow containing only unknown identifiers with 400 invalid-backends, without echoing client values, before the route phase ✅ Met The route check raises InvalidSelection with the generic message before _run_phase("route", ...); tests check the status, slug, message, discarded items, and that no route plugin ran.
2 Keep URL selections, order, and forbid unchanged ✅ Met Selections containing a URL are not refused, and selects_unknown_only() excludes order and forbid; tests cover these cases.
3 Keep known selections that routing removes or whose backend fails as unavailable rather than refusing them ✅ Met The routing-removal and failed-attempt tests confirm the existing no-backend behavior is preserved.
4 Add tests for unknown selections and known selections that routing removes or whose backend fails ✅ Met Tests cover refusal of unknown identifiers, routing removal, and a known backend whose attempt fails.
5 Document the behavior in docs/plugins.md and docs/configuration/plugins.md, and add a changelog or release-notes entry ✅ Met Both documentation files describe the behavior, and docs/migration-0.23.md includes a migration-note section.
6 Have proTES pick up the change with its next FOCA bump and update its pinning how-to 👀 Check by hand (external system) Check the referenced proTES follow-up issues (#508/#509) to confirm the FOCA bump, known= integration, and pinning how-to update.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="foca/plugins/selection.py" line_range="497" />
<code_context>
+        Whether the selection is `allow`, has no URLs, and none of its items
+        matches a known backend (as in :py:func:`apply_backend_selection`).
+    """
+    if selection.type != "allow" or any("://" in item for item in selection.backends):
+        return False
+    match = _matcher(known)
</code_context>
<issue_to_address>
**Unmatched URLs remain unrefused**

The issue asks to refuse an `allow` when none of its items matches a candidate, including a URL in another form. This guard exempts any selection containing a URL, so an unmatched URL still reaches the no-backend outcome.
</issue_to_address>

### Comment 2
<location path="foca/plugins/engine.py" line_range="1657" />
<code_context>
+        """
+        selection = self.selection.backends
+        if selection is None or not selects_unknown_only(
+            [backend for backend in known if not self.host.is_own_url(backend.url)],
+            selection,
+        ):
</code_context>
<issue_to_address>
**Matching backend is refused**

When an `allow` matches a known backend that routing removes, `selects_unknown_only` receives only backends that pass `not self.host.is_own_url(backend.url)`, so it treats the selection as unknown-only and refuses it; the tests do not cover this unchanged outcome.

Check the selection against the unfiltered known backends and add a test asserting the unchanged outcome when routing removes a matching backend.

Also at `tests/plugins/test_plugin_selection.py:386-387`.
</issue_to_address>

### Comment 3
<location path="docs/plugins.md" line_range="108-121" />
<code_context>
+  [provenance](#provenance)), never added. An `allow` of identifiers only,
</code_context>
<issue_to_address>
**Configuration guide not updated**

The change documents the behavior in `docs/plugins.md` and the migration notes, but the issue also asks for `docs/configuration/plugins.md` under Per-request choices to be updated.
</issue_to_address>

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: foca/plugins/selection.py:497, foca/plugins/engine.py:1657


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread foca/plugins/selection.py Resolved
Comment thread foca/plugins/engine.py Resolved
Comment thread docs/plugins.md Resolved
The configuration reference (per-request choices) now says, as the
issue asks, that an `allow` of only unknown identifiers is refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

New gaps against the linked issues

Comment thread tests/plugins/test_plugin_selection.py Resolved
A candidate that the route phase removes isn't unknown: the selection
ends without candidates, as before (review finding of Sourcery).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

New gaps against the linked issues

Comment thread foca/plugins/selection.py Resolved
Comment thread tests/plugins/test_plugin_selection.py Resolved
A known backend that fails its attempt still ends with "no backend
available" (503), not a refusal (review finding of Sourcery).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 4f5412d into dev Nov 21, 2026
58 checks passed
@uniqueg
uniqueg deleted the fix/808-unknown-backend-choices branch November 21, 2026 00:08
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.

fix(plugins): reject backend choices without match

1 participant