Skip to content

Show a distinct no-permission state for approvals#3318

Open
dmarticus wants to merge 2 commits into
mainfrom
posthog-code/approvals-no-permission-state
Open

Show a distinct no-permission state for approvals#3318
dmarticus wants to merge 2 commits into
mainfrom
posthog-code/approvals-no-permission-state

Conversation

@dmarticus

Copy link
Copy Markdown
Contributor

Problem

Tool approvals are org-admin-only, and the backend deliberately answers a 404 (not a 403) when the caller isn't an organization admin. The console's approvals surfaces treated that 404 like any other failure and showed "Couldn't load approvals", so non-admins read the feature as broken — and the panes kept polling the endpoint every 10s even though it could never succeed.

Changes

  • packages/api-client/src/fetcher.ts: non-2xx responses now throw a typed ApiRequestError with a .status field. The message keeps the exact legacy Failed request: [<status>] <body> format, so all existing catch sites that string-match on it are unaffected. Added a requestErrorStatus() helper.
  • useAgentFleetApprovals / useAgentApplicationApprovals expose isPermissionError, skip react-query retries on the 404 admin gate, and stop the 10s refetchInterval once the permission error is known (this also stops the wasted polling behind the pending-count strip on the applications list).
  • AgentFleetApprovalsPane / AgentApprovalsPane render a dedicated calm empty state — "You need organization admin access" with no retry affordance — while genuine failures keep the "Couldn't load approvals" state. Fixed the copy, which previously said "team-admin"; the actual gate is organization-membership admin. For the per-agent pane, 404→no-permission is safe because AgentDetailLayout only renders the pane's content after the application itself has loaded.
  • DeepLinkApprovalModal and AgentApprovalDetail need no changes: the modal uses the session-principal-scoped ingress endpoint (not the admin gate) and already handles 404/403, and the detail view is prop-driven from the list.

How did you test this?

  • New unit tests: fetcher.test.ts (typed status + legacy message format preserved on both throw sites, requestErrorStatus) and useAgentFleetApprovals.test.tsx (404 → isPermissionError with no retry, 500 → genuine error with retries, success path, predicate cases).
  • pnpm typecheck for @posthog/api-client and @posthog/ui; full test suites for both packages pass (160 files / 1433 tests in ui, 75 in api-client); Biome clean on changed files.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code

The approvals endpoints are org-admin gated and answer 404 for
non-admins, which previously rendered as a generic "Couldn't load
approvals" failure. The fetcher now throws a typed ApiRequestError
(same message format, so existing string-matching catch sites keep
working), the approvals hooks flag the 404 as a permission error and
stop polling/retrying it, and both approvals panes render a calm
"You need organization admin access" empty state instead.

Generated-By: PostHog Code
Task-Id: 8713c915-6ca4-444b-824f-343ec148e128
@trunk-io

trunk-io Bot commented Jul 9, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 4101201.

@dmarticus dmarticus marked this pull request as ready for review July 9, 2026 19:12
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(approvals): show a distinct no-perm..." | Re-trigger Greptile

Comment on lines +23 to +25
// Stop polling once we know the viewer lacks org-admin access.
refetchInterval: (query) =>
isApprovalsPermissionError(query.state.error) ? false : 10_000,

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.

P2 The refetchInterval callback parameter is named query, which shadows the outer const query assigned from useAuthenticatedQuery. The inner query is the TanStack Query<…> object, not the hook's return value — so this is harmless at runtime, but the two meanings in close proximity can mislead readers into thinking the callback accesses the hook's own state. The same shadowing is present in useAgentApplicationApprovals.ts.

Suggested change
// Stop polling once we know the viewer lacks org-admin access.
refetchInterval: (query) =>
isApprovalsPermissionError(query.state.error) ? false : 10_000,
// Stop polling once we know the viewer lacks org-admin access.
refetchInterval: (q) =>
isApprovalsPermissionError(q.state.error) ? false : 10_000,

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 4101201 — renamed the callback parameter to q in both hooks.

Comment on lines +37 to +73
describe("useAgentFleetApprovals", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("flags a 404 as a permission error without retrying", async () => {
mockListAgentFleetApprovals.mockRejectedValue(
new ApiRequestError(404, '{"detail":"Not found"}'),
);
const { result } = renderApprovalsHook();

await waitFor(() => expect(result.current.isError).toBe(true));
expect(result.current.isPermissionError).toBe(true);
// The admin gate never clears on retry, so the hook must not retry.
expect(mockListAgentFleetApprovals).toHaveBeenCalledTimes(1);
});

it("treats non-404 failures as genuine errors and retries them", async () => {
mockListAgentFleetApprovals.mockRejectedValue(
new ApiRequestError(500, '{"error":"boom"}'),
);
const { result } = renderApprovalsHook();

await waitFor(() => expect(result.current.isError).toBe(true));
expect(result.current.isPermissionError).toBe(false);
expect(mockListAgentFleetApprovals).toHaveBeenCalledTimes(4);
});

it("returns approvals on success", async () => {
mockListAgentFleetApprovals.mockResolvedValue([{ id: "approval-1" }]);
const { result } = renderApprovalsHook();

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data).toEqual([{ id: "approval-1" }]);
expect(result.current.isPermissionError).toBe(false);
});
});

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.

P2 useAgentApplicationApprovals contains identical retry/refetchInterval logic but has no dedicated test file. The shared isApprovalsPermissionError helper is exercised here, but the hook-level behaviour (no retry on 404, retry count on 500, isPermissionError flag propagation) would benefit from the same coverage — particularly because useAgentApplicationApprovals also gates on !!idOrSlug, a condition that isn't exercised by the fleet hook tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added useAgentApplicationApprovals.test.tsx in 4101201 — covers the 404 no-retry path, 500 retries, success, and the !!idOrSlug enabled gate (query stays pending, client never called).

…-agent hook

Rename the refetchInterval callback parameter from `query` to `q` in both
approvals hooks so it no longer shadows the outer `const query`, and add a
dedicated test file for useAgentApplicationApprovals covering the 404
no-retry path, 500 retry path, success, and the !!idOrSlug enabled gate.

Generated-By: PostHog Code
Task-Id: 8713c915-6ca4-444b-824f-343ec148e128
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.

1 participant