fix(ai): avoid constant oauth token ordering#503
Conversation
|
The latest updates on your projects. Learn more about Unkey Deploy
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: ASSERTIVE Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
No issues found across 3 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant AI as AI Tool
participant OAuth as OAuth Token Service
participant Ordering as getOAuthTokenOrderBy()
participant DB as Database
Note over AI,DB: OAuth Token Resolution Flow
AI->>OAuth: resolveOAuthToken(preferUserId?, providerId)
alt preferUserId provided
OAuth->>Ordering: getOAuthTokenOrderBy("user_123")
Ordering-->>OAuth: [preference CASE, role priority CASE]
else no preferUserId
OAuth->>Ordering: getOAuthTokenOrderBy(undefined)
Ordering-->>OAuth: [role priority CASE only]
end
OAuth->>DB: .select(...).orderBy(...orderBy).limit(MAX_CANDIDATES)
DB-->>OAuth: TokenCandidate[]
loop for each candidate
OAuth->>OAuth: Check token validity & TTL
alt valid token
OAuth-->>AI: ResolvedToken
else invalid/expired
OAuth->>OAuth: continue to next candidate
end
end
alt no valid candidate found
OAuth-->>AI: null
end
Note over Ordering: Key boundary: Ordering logic extracted<br/>ensures no ORDER BY constant (0)<br/>when preferUserId is missing
Shadow auto-approve: would auto-approve. Fix OAuth token ordering bug when no preferred user is set; extract ordering logic with test coverage.
Re-trigger cubic
Greptile SummaryThis PR fixes a bug where omitting
Confidence Score: 5/5Safe to merge — the change is narrow and well-tested, touching only the ORDER BY construction path for OAuth token lookup. The diff is a targeted extraction of ordering logic that also closes a real bug: ORDER BY 0 in Postgres is invalid (column references are 1-based) and would have errored on every token lookup where no preferred user was passed. The new utility is straightforward, the spread into .orderBy() is correct, and the regression tests render the actual SQL fragments against both branches. No pre-existing behavior is changed beyond removing the invalid constant from the ORDER BY clause. No files require special attention — all three are small, focused, and the tests directly validate the SQL output. Important Files Changed
Sequence Diagram%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant Caller
participant getOAuthToken
participant resolveOAuthToken
participant getOAuthTokenOrderBy
participant DB as Postgres DB
Caller->>getOAuthToken: (providerId, orgId, preferUserId?)
getOAuthToken->>resolveOAuthToken: (providerId, orgId, preferUserId?)
resolveOAuthToken->>getOAuthTokenOrderBy: (preferUserId?)
alt preferUserId is set
getOAuthTokenOrderBy-->>resolveOAuthToken: [user_preference_expr, ROLE_PRIORITY]
else no preferUserId
getOAuthTokenOrderBy-->>resolveOAuthToken: [ROLE_PRIORITY]
end
resolveOAuthToken->>DB: SELECT ... ORDER BY ...orderBy LIMIT 5
DB-->>resolveOAuthToken: TokenCandidate[]
resolveOAuthToken-->>getOAuthToken: "ResolvedToken | null"
getOAuthToken-->>Caller: "string | null"
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant Caller
participant getOAuthToken
participant resolveOAuthToken
participant getOAuthTokenOrderBy
participant DB as Postgres DB
Caller->>getOAuthToken: (providerId, orgId, preferUserId?)
getOAuthToken->>resolveOAuthToken: (providerId, orgId, preferUserId?)
resolveOAuthToken->>getOAuthTokenOrderBy: (preferUserId?)
alt preferUserId is set
getOAuthTokenOrderBy-->>resolveOAuthToken: [user_preference_expr, ROLE_PRIORITY]
else no preferUserId
getOAuthTokenOrderBy-->>resolveOAuthToken: [ROLE_PRIORITY]
end
resolveOAuthToken->>DB: SELECT ... ORDER BY ...orderBy LIMIT 5
DB-->>resolveOAuthToken: TokenCandidate[]
resolveOAuthToken-->>getOAuthToken: "ResolvedToken | null"
getOAuthToken-->>Caller: "string | null"
Reviews (1): Last reviewed commit: "fix(ai): avoid constant oauth token orde..." | Re-trigger Greptile |
Summary
ORDER BY 0.Validation
bun test src/ai/tools/utils/oauth-token.test.tsbun run test --filter @databuddy/aidotenv -- turbo run testdotenv -- turbo run check-typesandenforce-formatSummary by cubic
Fix OAuth token ordering to stop emitting ORDER BY 0 when no preferred user is set. We still prefer the requested user first, then owner/admin roles.
Bug Fixes
Refactors
getOAuthTokenOrderByand used spread.orderBy(...orderBy)for clarity.Written for commit 12cecfd. Summary will update on new commits.