Skip to content

refactor(cloud): Changes a few things - #51

Merged
SteakFisher merged 2 commits into
mainfrom
refactor/cloud
May 21, 2026
Merged

refactor(cloud): Changes a few things#51
SteakFisher merged 2 commits into
mainfrom
refactor/cloud

Conversation

@SteakFisher

@SteakFisher SteakFisher commented May 21, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor
    • Authentication context handling consolidated across pricing and event query flows so all price calculations and event queries now run with an explicit authenticated context, improving access control consistency and reliability.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7575b036-06a5-41a4-bbf3-6d8ee1b8d06f

📥 Commits

Reviewing files that changed from the base of the PR and between 95319c2 and a3d5615.

📒 Files selected for processing (1)
  • src/routes/gRPC/query/queryEvents.ts

📝 Walkthrough

Walkthrough

This PR refactors the storage adapter call chain to propagate AuthContext instead of a mode string parameter through pricing and event query operations. All adapters and handlers are updated to accept auth context and extract the mode from it when building queries.

Changes

Authentication Context Propagation

Layer / File(s) Summary
Storage adapter interface contract
src/interface/storage/Storage.ts
StorageAdapter.price and StorageAdapter.query methods now require auth: AuthContext instead of mode: "production" | "test", establishing the contract for all implementations.
Route handlers and pricing service
src/routes/gRPC/query/queryEvents.ts, src/routes/gRPC/payment/createCheckoutLink.ts, src/services/pricingService.ts
Entry points extract auth context from gRPC/API context and pass it downstream. queryEvents reads AuthContext from apiKeyContextKey and calls adapter.query(request, auth). createCheckoutLink passes auth to calculatePrice, which forwards it to calculatePaymentPrice. Pricing service updates both sdkAdapter.price and aiAdapter.price calls to use auth.
ClickHouse adapter and handlers
src/storage/adapter/clickhouse/ClickHouseAdapter.ts, src/storage/adapter/clickhouse/handlers/priceRequestBasicUsage.ts, src/storage/adapter/clickhouse/handlers/priceRequestAiTokenUsage.ts, src/storage/adapter/clickhouse/handlers/queryEvents.ts
Adapter methods and handlers accept auth: AuthContext as a parameter. Handlers extract auth.mode for ClickHouse query execution. handleQueryEvents signature updated to accept auth (body unchanged).
Postgres adapter and handlers
src/storage/adapter/postgres/postgres.ts, src/storage/adapter/postgres/handlers/priceRequest.ts, src/storage/adapter/postgres/handlers/priceRequestBasicUsage.ts, src/storage/adapter/postgres/handlers/priceRequestAiTokenUsage.ts, src/storage/adapter/postgres/handlers/queryEvents.ts
Adapter methods and handlers accept auth: AuthContext. Core handlePriceRequest uses auth.mode when constructing SQL conditions. Higher-level handlers forward auth through the call chain.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • ScrawnDotDev/Scrawn#42: Both PRs touch the BASIC_USAGE pricing flow in the same handlers/adapters and rename/adjust handler plumbing.
  • ScrawnDotDev/Scrawn#37: Modifies the pricing/query plumbing for mode propagation; related to replacing mode with AuthContext.
  • ScrawnDotDev/Scrawn#35: Also touches ClickHouse pricing paths around PAYMENT events and adapter wiring.

Poem

🐰 I threaded auth through every call,
No loose mode strings tripping the hall,
Adapters hum with context clear,
Queries and prices hop right here,
🥕✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title is vague and overly generic, using "Changes a few things" which doesn't convey what was actually refactored. Consider a more descriptive title that reflects the core change, such as: 'refactor(cloud): Replace mode parameter with AuthContext in storage adapters'
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/cloud

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/storage/adapter/clickhouse/handlers/queryEvents.ts (1)

199-213: ⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

Enforce auth.mode in all query paths.

auth is accepted but never applied to query predicates, so list/aggregation results can mix production/test data. Please thread auth into query builders and add a mandatory mode filter for each table query (including total-count paths).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/storage/adapter/clickhouse/handlers/queryEvents.ts` around lines 199 -
213, The code accepts AuthContext but never applies it to queries; update
handleQueryEvents to thread auth (AuthContext.auth.mode) into all query-building
paths by passing auth into handleAggregationQuery and handleListQuery and into
any total/count helpers (e.g., functions that compute totals for tables), and
enforce a mandatory mode predicate per-table (e.g., add WHERE mode = auth.mode
or equivalent) so every table query and total-count path filters by auth.mode;
locate getTablesForRequest, handleAggregationQuery, handleListQuery and any
total-count builders and modify their signatures to accept auth and apply the
mode filter when composing predicates.
src/storage/adapter/postgres/handlers/queryEvents.ts (1)

189-203: ⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

Apply auth.mode as a required SQL filter in event queries.

The new auth parameter is unused, so query/list/aggregation/count operations are not scoped by mode. Add an enforced mode predicate to every generated subquery to keep production/test data isolated.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/storage/adapter/postgres/handlers/queryEvents.ts` around lines 189 - 203,
handleQueryEvents currently ignores the auth parameter so queries aren’t scoped
by mode; update it to enforce auth.mode as a required SQL filter by adding the
mode predicate into every generated subquery (either by injecting it into the
QueryRequest.where before calling or by passing auth down). Specifically, ensure
handleQueryEvents appends "mode = auth.mode" (or equivalent predicate) to the
request filter and propagate auth or the mode value into handleAggregationQuery,
handleListQuery and any count/subquery builders so every generated SQL includes
the mode constraint; also validate auth.mode is present/non-empty before running
queries to avoid accidental unscoped access.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/routes/gRPC/query/queryEvents.ts`:
- Around line 36-39: The handler currently uses auth! when calling adapter.query
which can pass undefined; add a null check similar to createCheckoutLink.ts:
retrieve auth from call[apiKeyContextKey], if (!auth) throw or return an
AuthError (ensure you import AuthError) and terminate the request before calling
StorageAdapterFactory.getEventStorageAdapter or adapter.query; specifically,
update the logic around auth, reference auth and call[apiKeyContextKey], and
ensure adapter.query(queryRequest, auth) is only called with a defined auth
after importing AuthError from the errors module.

---

Outside diff comments:
In `@src/storage/adapter/clickhouse/handlers/queryEvents.ts`:
- Around line 199-213: The code accepts AuthContext but never applies it to
queries; update handleQueryEvents to thread auth (AuthContext.auth.mode) into
all query-building paths by passing auth into handleAggregationQuery and
handleListQuery and into any total/count helpers (e.g., functions that compute
totals for tables), and enforce a mandatory mode predicate per-table (e.g., add
WHERE mode = auth.mode or equivalent) so every table query and total-count path
filters by auth.mode; locate getTablesForRequest, handleAggregationQuery,
handleListQuery and any total-count builders and modify their signatures to
accept auth and apply the mode filter when composing predicates.

In `@src/storage/adapter/postgres/handlers/queryEvents.ts`:
- Around line 189-203: handleQueryEvents currently ignores the auth parameter so
queries aren’t scoped by mode; update it to enforce auth.mode as a required SQL
filter by adding the mode predicate into every generated subquery (either by
injecting it into the QueryRequest.where before calling or by passing auth
down). Specifically, ensure handleQueryEvents appends "mode = auth.mode" (or
equivalent predicate) to the request filter and propagate auth or the mode value
into handleAggregationQuery, handleListQuery and any count/subquery builders so
every generated SQL includes the mode constraint; also validate auth.mode is
present/non-empty before running queries to avoid accidental unscoped access.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c15f319d-b209-42de-80b0-b1c1ad6ec426

📥 Commits

Reviewing files that changed from the base of the PR and between 6976ce9 and 95319c2.

📒 Files selected for processing (13)
  • src/interface/storage/Storage.ts
  • src/routes/gRPC/payment/createCheckoutLink.ts
  • src/routes/gRPC/query/queryEvents.ts
  • src/services/pricingService.ts
  • src/storage/adapter/clickhouse/ClickHouseAdapter.ts
  • src/storage/adapter/clickhouse/handlers/priceRequestAiTokenUsage.ts
  • src/storage/adapter/clickhouse/handlers/priceRequestBasicUsage.ts
  • src/storage/adapter/clickhouse/handlers/queryEvents.ts
  • src/storage/adapter/postgres/handlers/priceRequest.ts
  • src/storage/adapter/postgres/handlers/priceRequestAiTokenUsage.ts
  • src/storage/adapter/postgres/handlers/priceRequestBasicUsage.ts
  • src/storage/adapter/postgres/handlers/queryEvents.ts
  • src/storage/adapter/postgres/postgres.ts

Comment thread src/routes/gRPC/query/queryEvents.ts Outdated
@SteakFisher
SteakFisher merged commit f61ad41 into main May 21, 2026
3 checks passed
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