…ever imported (#2689)
`portal_auth_exchange` read the sliding-session idle window through a name
the module does not import:
idle_s, _ = settings_service.get_portal_session_policy()
`grep -n settings_service src/backend/client_portal/router.py` returned
exactly one line — the call. So every request to the ent#163 trusted-issuer
seam raised `NameError` and answered 500. Present on `dev` AND on `main`
(line 308 there): released, not a dev-only regression.
Introduced by ent#375 (#2099), which added the idle-window read to the
response. `dependencies.py` performs the identical read correctly, and for
two reasons that are both load-bearing: the import is function-local
because `settings_service` imports `db` and a module-level import would
make a cycle, and it is wrapped in a degrade to the shipped policy because
this is an auth path and a settings hiccup must not 500 it. The router
copied the call and neither property.
The fix reuses that reader rather than re-importing `settings_service`
here. A second copy of the call would be a second chance to omit the
degrade — and the degrade is the half that matters on an auth route.
Importing an underscore-private name across modules follows existing
practice (`from ._common import _norm_ts`, `from services.git_service
import _detect_git_dir`, `from services.template_service import
_is_platform_injected`).
Blast radius, measured on a live stack in one minute: platform login,
platform JWT, the portal OTP flow, portal session tokens, MCP keys and
agent auth all answered normally; only this route was down. The failure is
reachable only WITH a valid `portal_delegate` key — every other principal
is refused 403 by the scope fence before reaching the line — so it is not
externally probeable, and it is total for exactly the callers the route
exists for.
WHY EIGHT TESTS MISSED IT. `test_ent163_portal_delegated_identity.py` and
`test_163_portal_delegate_scope.py` cover the mint, the access rule, scope
fencing, disjoint identities and revocation — all through
`service.portal_exchange(...)`. None executes the ROUTE HANDLER, and the
NameError is in the handler body three lines after the service returns.
Proven rather than asserted: with the broken line reinstated, only the
three new tests fail and all 26 pre-existing ones still pass.
`tests/unit/test_2689_portal_exchange_route_executes.py` drives the handler.
It pins that the route answers at all, that `expires_in` is the real idle
window (compared against `_portal_session_policy()`, not a literal, so the
policy stays single-sourced), and that a raising settings backend degrades
instead of 500ing. It also re-pins the two gates the route already owned —
the delegate-scope 403 and the no-share 403 — which were previously
asserted only against the service.
An AST scan for the same shape (a module using a service singleton it never
binds) across `src/backend` found no other instance.
Related to #2689
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01RSjVEjay9ztC1oDXXkh9uN
Fixes #2689.
The bug
portal_auth_exchangeread the sliding-session idle window through a name the module does not import:grep -n settings_service src/backend/client_portal/router.pyreturned exactly one line — the call itself. So every request to the ent#163 trusted-issuer seam — how a licensee runs their own customer portal against Trinity while keeping their own IdP — raisedNameErrorand answered 500.Present on
devand onmain(line 308 there). Released, not a dev-only regression. Introduced by ent#375 (#2099), which added the idle-window read to the response.Blast radius, measured
Every other auth path on the same stack in the same minute:
Login, the Workspace, MCP keys and agent auth were all fine. And the failure is reachable only with a valid
portal_delegatekey — every other principal is refused 403 by the scope fence before reaching line 350 — so it is not externally probeable, and it is total for exactly the callers the route exists for. Which is why nobody had hit it: minting one of those keys is the first thing that reaches the line.The fix
Reuse
dependencies._portal_session_policy()instead of re-importingsettings_servicehere.dependencies.pyperforms the identical read correctly, and for two reasons that are both load-bearing:settings_serviceimportsdband a module-level import here would make a cycle out of one settings read;The router copied the call and neither property. A second copy of that call would be a second chance to omit the degrade — and on an auth route the degrade is the half that matters. Importing an underscore-private name across modules follows existing practice in this tree (
from ._common import _norm_ts,from services.git_service import _detect_git_dir,from services.template_service import _is_platform_injected).Two lines of code; the rest of the diff is why.
Why eight tests missed it
test_ent163_portal_delegated_identity.pyandtest_163_portal_delegate_scope.pycarry 8 tests between them — the mint, the access rule, scope fencing, disjoint identities, revocation. Every one of them goes throughservice.portal_exchange(...). None executes the route handler, and theNameErroris in the handler body three lines after the service returns. A test that never runs the code under test cannot fail when that code is broken.Proven rather than asserted — with the broken line reinstated:
Only the three new tests fail. All 26 pre-existing ones still pass.
The new test
tests/unit/test_2689_portal_exchange_route_executes.py— 5 tests that drive the handler:expires_inis the real idle window, compared against_portal_session_policy()rather than a literal, so the policy stays single-sourced instead of gaining a second home in a test;Its docstring states the narrow contract on purpose: it asserts almost nothing the other files already own. Its job is to be the test that goes red when the endpoint cannot answer.
Systemic check
An AST scan across
src/backendfor the same shape — a module calling.method()on a service singleton (settings_service,platform_audit_service,idempotency_service,rate_limiter,docker_service,heartbeat_service,capacity_manager,effect_guard) that it never binds — found no other instance.Testing
tests/unit/test_2689_portal_exchange_route_executes.py— 5 new tests, each proven load-bearing by reinstating the defect.Related to #2689
🤖 Generated with Claude Code
https://claude.ai/code/session_01RSjVEjay9ztC1oDXXkh9uN