refactor(events): inline agent collapse-to-404 check; drop one-off helper#267
Open
dm36 wants to merge 1 commit into
Open
refactor(events): inline agent collapse-to-404 check; drop one-off helper#267dm36 wants to merge 1 commit into
dm36 wants to merge 1 commit into
Conversation
…lper Per @rpatel-scale's review on #257: ``check_agent_or_collapse_to_404`` had exactly one caller (``GET /events/{id}``) and duplicated the shape of ``_check_api_key_or_collapse_to_404`` and the in-flight ``check_task_or_collapse_to_404`` from #249. Rather than ship a third per-resource helper, inline the 4-line try/except in ``events.py`` and delete ``agent_authorization.py`` outright. Wire behavior is unchanged: one ``authz.check`` on the parent agent; denial surfaces as 404 (``ItemDoesNotExist``) so callers can't probe cross-tenant existence by comparing 403 vs 404. When #249 lands, a follow-up can fold the (then-three) inline/named copies into a single generic ``check_or_collapse_to_404(authz, resource, op)`` with full context across tasks / api_keys / events.
50d0437 to
4ac446b
Compare
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
Addresses @rpatel-scale's review comment on #257:
check_agent_or_collapse_to_404had exactly one caller (GET /events/{id}) and structurally duplicated:_check_api_key_or_collapse_to_404— already on main from feat(AGX1-263): agent_api_keys route migration to FGAC (404 collapse + two-factor) #252check_task_or_collapse_to_404— in flight in feat(tasks): per-RPC task permission rewire and 404/403 wrap #249Rather than ship a third per-resource helper, inline the 4-line
try/exceptinevents.pyand deleteagent_authorization.py.Wire behavior
Unchanged:
authorization.check(resource=AgentexResource.agent(event.agent_id), operation=read).AuthorizationError, raiseItemDoesNotExist→ FastAPI returns 404.Follow-up
When #249 lands, all three of (task, api_key, events) will have the same
try/exceptshape inline or in their own one-off helper. A small cleanup PR can then fold them into a single genericcheck_or_collapse_to_404(authorization, resource, operation)— with full context across all three callers in one place rather than piecemeal.Diff
Test plan
uv run pytest agentex/tests/integration/api/events/test_events_authz_api.py— the existing 5 cases assert wire behavior (resource type, operation, 200/404 status), so they should pass unchanged.Greptile Summary
This refactor inlines the single-caller
check_agent_or_collapse_to_404helper directly intoGET /events/{id}and deletesagent_authorization.py, eliminating an unnecessary abstraction. The wire behavior is unchanged: oneauthorization.checkcall on the parent agent, withAuthorizationErrorcollapsed toItemDoesNotExist(→ 404) to prevent cross-tenant probing.events.py: ImportsAgentexResource,AuthorizationError, andItemDoesNotExistdirectly; the inlinedtry/exceptblock is functionally identical to the helper. The error message now usesevent_idrather thanagent_id, which is a subtle correctness improvement — callers look up events byevent_id, so the 404 message should reference that ID.agent_authorization.py: Deleted. The file's TODO about restoring 403/404 semantics once agents carry tenant scope is dropped without a tracking ticket.Confidence Score: 5/5
Safe to merge — the change is a straightforward inline of a 4-line helper with no behavioral difference.
The inlined try/except block is structurally identical to the deleted helper. The only observable difference — using event_id instead of agent_id in the 404 message — is an improvement, not a regression. No auth logic has changed.
No files require special attention.
Important Files Changed
event_idinstead ofagent_id(subtle improvement over the original helper).Sequence Diagram
sequenceDiagram participant Client participant EventsRoute as GET /events/{id} participant EventUseCase participant Authorization Client->>EventsRoute: "GET /events/{event_id}" EventsRoute->>EventUseCase: get(event_id) EventUseCase-->>EventsRoute: event_entity EventsRoute->>Authorization: "check(resource=agent(event_entity.agent_id), operation=read)" alt Authorized Authorization-->>EventsRoute: OK EventsRoute-->>Client: 200 Event else AuthorizationError (denied or wrong tenant) Authorization-->>EventsRoute: AuthorizationError EventsRoute-->>Client: 404 ItemDoesNotExist(event_id) endPrompt To Fix All With AI
Reviews (2): Last reviewed commit: "refactor(events): inline agent collapse-..." | Re-trigger Greptile
Context used:
Learned From
scaleapi/scaleapi#127117