Skip to content

feat: add dbLogStats types and function for query performance#1118

Merged
jhaynie merged 3 commits intomainfrom
task/db-slow-queries
Mar 5, 2026
Merged

feat: add dbLogStats types and function for query performance#1118
jhaynie merged 3 commits intomainfrom
task/db-slow-queries

Conversation

@jhaynie
Copy link
Member

@jhaynie jhaynie commented Mar 5, 2026

Summary

  • Add Zod schemas and dbLogStats() API function for the new database query performance stats endpoint
  • New file packages/core/src/services/db/stats.ts following the existing logs.ts pattern

Details

New exports from @agentuity/core (and @agentuity/server via re-export)

Schemas: DbLogStatsSummarySchema, DbLogStatsTimeSeriesPointSchema, DbLogStatsQueryPatternSchema, DbLogStatsCommandBreakdownSchema, DbLogStatsResponseSchema, DbLogStatsRequestSchema, DbLogStatsAPIResponseSchema

Types: DbLogStatsSummary, DbLogStatsTimeSeriesPoint, DbLogStatsQueryPattern, DbLogStatsCommandBreakdown, DbLogStatsResponse

Function: dbLogStats(client, { database, orgId, region, startDate, endDate })Promise<DbLogStatsResponse>

Related PRs

  • Ion: agentuity/ion — ClickHouse aggregation endpoint
  • App: agentuity/app — Performance tab UI

Merge order

This PR should be merged and published before the App PR, since the App API imports dbLogStats from @agentuity/server.

Summary by CodeRabbit

  • New Features
    • Added a database log statistics API delivering summary metrics, time-series data, query-pattern analysis, and command-level breakdowns (counts, errors, durations, percentiles). Supports filtering by database, organization/region, and time range to improve visibility into query performance and errors for monitoring and diagnostics.

Add Zod schemas and API function for the new database query performance
stats endpoint. Includes types for summary stats, time series data,
query patterns, and command breakdown.
@agentuity-agent
Copy link

agentuity-agent bot commented Mar 5, 2026

The latest Agentuity deployment details. Learn more about Agentuity.

Project Deployment Preview Updated (UTC)
docs 🔴 Failed - Mar 05, 2026 5:32 AM

The build failed with the following:

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2026

📝 Walkthrough

Walkthrough

Adds a new DbLogStats module at packages/core/src/services/db providing request/response schemas, analytics data structures, and a typed dbLogStats API client; re-exports these schemas, types, and function from packages/core/src/services/db/index.ts.

Changes

Cohort / File(s) Summary
Database Statistics Implementation
packages/core/src/services/db/stats.ts
New DbLogStats module: defines request/response schemas (summary, time series, query patterns, command breakdown), related TypeScript types, and dbLogStats(client, request) client function. Validates required fields, builds date query params, calls API, and throws DbInvalidArgumentError or DbResponseError on failure.
Database Services Export
packages/core/src/services/db/index.ts
Adds re-exports from ./stats.ts: DbLogStatsAPIResponseSchema, request/response schemas, analytics schemas and types, and the dbLogStats function to the public service surface.
🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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


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

@github-actions
Copy link

github-actions bot commented Mar 5, 2026

📦 Canary Packages Published

version: 1.0.34-0e866e7

Packages
Package Version URL
@agentuity/drizzle 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-drizzle-1.0.34-0e866e7.tgz
@agentuity/cli 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-cli-1.0.34-0e866e7.tgz
@agentuity/opencode 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-opencode-1.0.34-0e866e7.tgz
@agentuity/core 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-core-1.0.34-0e866e7.tgz
@agentuity/pi 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-pi-1.0.34-0e866e7.tgz
@agentuity/server 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-server-1.0.34-0e866e7.tgz
@agentuity/claude-code 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-claude-code-1.0.34-0e866e7.tgz
@agentuity/workbench 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-workbench-1.0.34-0e866e7.tgz
@agentuity/frontend 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-frontend-1.0.34-0e866e7.tgz
@agentuity/runtime 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-runtime-1.0.34-0e866e7.tgz
@agentuity/auth 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-auth-1.0.34-0e866e7.tgz
@agentuity/react 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-react-1.0.34-0e866e7.tgz
@agentuity/schema 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-schema-1.0.34-0e866e7.tgz
@agentuity/evals 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-evals-1.0.34-0e866e7.tgz
@agentuity/postgres 1.0.34-0e866e7 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-postgres-1.0.34-0e866e7.tgz
Install

Add to your package.json:

{
  "dependencies": {
    "@agentuity/drizzle": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-drizzle-1.0.34-0e866e7.tgz",
    "@agentuity/cli": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-cli-1.0.34-0e866e7.tgz",
    "@agentuity/opencode": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-opencode-1.0.34-0e866e7.tgz",
    "@agentuity/core": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-core-1.0.34-0e866e7.tgz",
    "@agentuity/pi": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-pi-1.0.34-0e866e7.tgz",
    "@agentuity/server": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-server-1.0.34-0e866e7.tgz",
    "@agentuity/claude-code": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-claude-code-1.0.34-0e866e7.tgz",
    "@agentuity/workbench": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-workbench-1.0.34-0e866e7.tgz",
    "@agentuity/frontend": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-frontend-1.0.34-0e866e7.tgz",
    "@agentuity/runtime": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-runtime-1.0.34-0e866e7.tgz",
    "@agentuity/auth": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-auth-1.0.34-0e866e7.tgz",
    "@agentuity/react": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-react-1.0.34-0e866e7.tgz",
    "@agentuity/schema": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-schema-1.0.34-0e866e7.tgz",
    "@agentuity/evals": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-evals-1.0.34-0e866e7.tgz",
    "@agentuity/postgres": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-postgres-1.0.34-0e866e7.tgz"
  }
}

Or install directly:

bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-drizzle-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-cli-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-opencode-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-core-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-pi-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-server-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-claude-code-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-workbench-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-frontend-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-runtime-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-auth-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-react-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-schema-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-evals-1.0.34-0e866e7.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.34-0e866e7/agentuity-postgres-1.0.34-0e866e7.tgz

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/core/src/services/db/stats.ts`:
- Around line 83-86: The formatting in the dbLogStats function (export async
function dbLogStats) is inconsistent with the project's formatter (Biome); run
the project's formatter or reformat this file to match Biome rules so the CI
passes. Locate the dbLogStats function in packages/core/src/services/db/stats.ts
and apply the standard formatting tool (e.g., run the repo's formatter command
or Biome CLI) to fix whitespace/indentation and any minor syntax layout
differences until Biome no longer reports "File content differs from formatting
output."
- Around line 86-95: Validate that database, startDate, and endDate are present
in addition to orgId and region and throw DbInvalidArgumentError with a clear
message and the offending values (use the same DbInvalidArgumentError used
currently). After validation, encode path segments orgId, region, and database
with encodeURIComponent when building the URL (the existing URL construction
using `/resource/${orgId}/${region}/${database}/logs/stats?...` should use
encoded values). Keep using URLSearchParams for startDate and endDate but
validate they are non-empty before appending to params (include them in the
validation check). Ensure variable names referenced are orgId, region, database,
startDate, endDate, params, and url so the change is easy to locate.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7f238eb2-bce1-486d-94b2-4e8d7ddfb8ae

📥 Commits

Reviewing files that changed from the base of the PR and between 09effbd and d493c03.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • packages/core/src/services/db/index.ts
  • packages/core/src/services/db/stats.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (15)
  • GitHub Check: Agentuity Deployment
  • GitHub Check: Template Integration Tests
  • GitHub Check: Storage CLI Tests
  • GitHub Check: Queue SDK Tests
  • GitHub Check: Playwright E2E Smoke Test
  • GitHub Check: Queue CLI Tests
  • GitHub Check: Sandbox CLI Tests
  • GitHub Check: Framework Integration Tests (TanStack & Next.js)
  • GitHub Check: Build
  • GitHub Check: Cloud Deployment Tests
  • GitHub Check: Package Installation & Usage Test
  • GitHub Check: Standalone Agent Test
  • GitHub Check: SDK Integration Test Suite
  • GitHub Check: Postgres SSL Integration Test
  • GitHub Check: Pack & Upload
🧰 Additional context used
📓 Path-based instructions (4)
packages/core/src/**/*.ts

📄 CodeRabbit inference engine (packages/core/AGENTS.md)

packages/core/src/**/*.ts: Use TypeScript for all code in the @agentuity/core package
Prefer interfaces for public API definitions in @agentuity/core
Use generic types for reusable type utilities in @agentuity/core
Ensure no side effects in exports - all exports must be pure functions or types with no global mutations
Ensure code is compatible with both Browser and Node/Bun runtime environments

Files:

  • packages/core/src/services/db/index.ts
  • packages/core/src/services/db/stats.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use Biome as code formatter with tabs (width 3), single quotes, semicolons, lineWidth 100, and trailingCommas es5

Files:

  • packages/core/src/services/db/index.ts
  • packages/core/src/services/db/stats.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx}: Use TypeScript Strict mode with ESNext target and bundler moduleResolution
Use StructuredError from @agentuity/core for error handling

Files:

  • packages/core/src/services/db/index.ts
  • packages/core/src/services/db/stats.ts
**/index.ts

📄 CodeRabbit inference engine (AGENTS.md)

Use named exports from package index.ts files

Files:

  • packages/core/src/services/db/index.ts
🧠 Learnings (11)
📚 Learning: 2026-02-05T17:19:40.468Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/schema/AGENTS.md:0-0
Timestamp: 2026-02-05T17:19:40.468Z
Learning: Applies to packages/schema/src/index.ts : All public APIs must be exported from `src/index.ts`

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2026-01-02T15:01:46.349Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/auth/AGENTS.md:0-0
Timestamp: 2026-01-02T15:01:46.349Z
Learning: Applies to packages/auth/**/schema.ts : Export the Drizzle schema from 'agentuity/auth/schema' for merging with application schemas

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2026-03-03T16:56:23.099Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/server/AGENTS.md:0-0
Timestamp: 2026-03-03T16:56:23.099Z
Learning: Applies to packages/server/src/index.ts : Re-export `agentuity/core` types and `z` from zod in the main entry point

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2026-02-05T17:18:57.641Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/core/AGENTS.md:0-0
Timestamp: 2026-02-05T17:18:57.641Z
Learning: Applies to packages/core/src/index.ts : Export all public APIs from src/index.ts

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2026-03-03T16:56:23.099Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/server/AGENTS.md:0-0
Timestamp: 2026-03-03T16:56:23.099Z
Learning: Applies to packages/server/src/index.ts : All public APIs must be exported from `src/index.ts`

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2026-01-30T21:56:42.250Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/drizzle/AGENTS.md:0-0
Timestamp: 2026-01-30T21:56:42.250Z
Learning: Applies to packages/drizzle/**/*.{ts,tsx} : Define schemas using Drizzle's `pgTable` and column types rather than other schema definition methods

Applied to files:

  • packages/core/src/services/db/index.ts
  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-01-02T15:01:46.349Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/auth/AGENTS.md:0-0
Timestamp: 2026-01-02T15:01:46.349Z
Learning: Applies to packages/auth/**/schema.ts : Define Drizzle table schemas and relations in the schema.ts file

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2026-01-30T21:56:52.619Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/postgres/AGENTS.md:0-0
Timestamp: 2026-01-30T21:56:52.619Z
Learning: Applies to packages/postgres/**/*.{ts,tsx} : Use tagged template literals as the primary query interface for PostgreSQL queries in agentuity/postgres

Applied to files:

  • packages/core/src/services/db/index.ts
📚 Learning: 2025-12-21T00:31:41.858Z
Learnt from: jhaynie
Repo: agentuity/sdk PR: 274
File: packages/cli/src/cmd/build/vite/server-bundler.ts:12-41
Timestamp: 2025-12-21T00:31:41.858Z
Learning: In Bun runtime, BuildMessage and ResolveMessage are global types and are not exported from the bun module. Do not import { BuildMessage } from 'bun' or similar; these types are available globally and should be used without import. This applies to all TypeScript files that target the Bun runtime within the repository.

Applied to files:

  • packages/core/src/services/db/index.ts
  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-02-05T17:19:40.468Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/schema/AGENTS.md:0-0
Timestamp: 2026-02-05T17:19:40.468Z
Learning: Applies to packages/schema/src/**/*.ts : Implement fluent API with chainable methods where appropriate

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-01-30T21:56:52.619Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/postgres/AGENTS.md:0-0
Timestamp: 2026-01-30T21:56:52.619Z
Learning: Applies to packages/postgres/**/*.{ts,tsx} : Use StructuredError from agentuity/core for error handling in agentuity/postgres

Applied to files:

  • packages/core/src/services/db/stats.ts
🧬 Code graph analysis (1)
packages/core/src/services/db/stats.ts (3)
packages/core/src/services/db/index.ts (12)
  • DbLogStatsRequestSchema (16-16)
  • DbLogStatsSummarySchema (19-19)
  • DbLogStatsTimeSeriesPointSchema (21-21)
  • DbLogStatsQueryPatternSchema (14-14)
  • DbLogStatsCommandBreakdownSchema (12-12)
  • DbLogStatsResponseSchema (17-17)
  • DbLogStatsAPIResponseSchema (11-11)
  • DbLogStatsSummary (20-20)
  • DbLogStatsTimeSeriesPoint (22-22)
  • DbLogStatsQueryPattern (15-15)
  • DbLogStatsCommandBreakdown (13-13)
  • DbLogStatsResponse (18-18)
packages/core/src/services/api.ts (2)
  • APIResponseSchema (915-926)
  • request (308-370)
packages/schema/src/index.ts (1)
  • infer (182-182)
🪛 GitHub Actions: Biome
packages/core/src/services/db/stats.ts

[error] 83-86: File content differs from formatting output. Run the formatter to fix code style.

🪛 GitHub Check: Format & Lint
packages/core/src/services/db/stats.ts

[failure] 1-1: format
File content differs from formatting output

🔇 Additional comments (2)
packages/core/src/services/db/index.ts (1)

10-24: Looks good: stats API surface is re-exported cleanly.

This keeps the db service index consistent and side-effect free.

packages/core/src/services/db/stats.ts (1)

6-69: Schema layering and API response typing are well-structured.

Nice reuse of APIResponseSchema and clear separation of summary/time-series/pattern/breakdown models.

Comment on lines +86 to +95
if (!orgId || !region) {
throw new DbInvalidArgumentError({ message: 'orgId and region are required', orgId, region });
}

const params = new URLSearchParams();
params.append('startDate', startDate);
params.append('endDate', endDate);

const url = `/resource/${orgId}/${region}/${database}/logs/stats?${params.toString()}`;

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, check if the file exists and read the relevant section
cat -n packages/core/src/services/db/stats.ts | head -100 | tail -50

Repository: agentuity/sdk

Length of output: 2473


🏁 Script executed:

# Also search for the DbLogStatsRequest type definition
rg -n "DbLogStatsRequest" packages/core/src/services/db/stats.ts -A 10

Repository: agentuity/sdk

Length of output: 1215


🏁 Script executed:

# Search for the dbLogStats function to understand the full context
rg -n "export async function dbLogStats" packages/core/src/services/db/stats.ts -A 20

Repository: agentuity/sdk

Length of output: 810


🏁 Script executed:

# Verify if there are other similar functions in the same file with similar issues
rg -n "export async function" packages/core/src/services/db/stats.ts | head -10

Repository: agentuity/sdk

Length of output: 170


🏁 Script executed:

# Check if encodeURIComponent is used elsewhere in the codebase for URL construction
rg -n "encodeURIComponent" packages/core/src/ -A 1 | head -20

Repository: agentuity/sdk

Length of output: 1576


Validate all required inputs and encode path segments before URL construction.

The function schema requires all five fields (database, orgId, region, startDate, endDate), but only orgId and region are validated at line 86. Missing validation allows falsy values for database, startDate, and endDate to reach the URL construction. Additionally, path segments at line 94 are not URL-encoded; values containing reserved characters (e.g., /, ?, #) will break routing. This deviates from the codebase pattern seen in similar services where path segments use encodeURIComponent().

Suggested fix
 export async function dbLogStats(client: APIClient, request: DbLogStatsRequest): Promise<DbLogStatsResponse> {
 	const { database, orgId, region, startDate, endDate } = request;

-	if (!orgId || !region) {
-		throw new DbInvalidArgumentError({ message: 'orgId and region are required', orgId, region });
+	if (!database || !orgId || !region || !startDate || !endDate) {
+		throw new DbInvalidArgumentError({
+			message: 'database, orgId, region, startDate, and endDate are required',
+			database,
+			orgId,
+			region,
+			startDate,
+			endDate,
+		});
 	}

-	const url = `/resource/${orgId}/${region}/${database}/logs/stats?${params.toString()}`;
+	const url = `/resource/${encodeURIComponent(orgId)}/${encodeURIComponent(region)}/${encodeURIComponent(database)}/logs/stats?${params.toString()}`;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/core/src/services/db/stats.ts` around lines 86 - 95, Validate that
database, startDate, and endDate are present in addition to orgId and region and
throw DbInvalidArgumentError with a clear message and the offending values (use
the same DbInvalidArgumentError used currently). After validation, encode path
segments orgId, region, and database with encodeURIComponent when building the
URL (the existing URL construction using
`/resource/${orgId}/${region}/${database}/logs/stats?...` should use encoded
values). Keep using URLSearchParams for startDate and endDate but validate they
are non-empty before appending to params (include them in the validation check).
Ensure variable names referenced are orgId, region, database, startDate,
endDate, params, and url so the change is easy to locate.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
packages/core/src/services/db/stats.ts (1)

6-12: ⚠️ Potential issue | 🟠 Major

Input contract is still under-enforced, and path segments are not escaped.

DbLogStatsRequestSchema defines five required inputs, but runtime guards only check two. Also, orgId, region, and database are interpolated directly into the path without encoding. This can still produce malformed URLs and brittle routing for reserved characters.

💡 Suggested fix
 export const DbLogStatsRequestSchema = z.object({
-	database: z.string().describe('the database name'),
-	orgId: z.string().describe('the organization ID'),
-	region: z.string().describe('the region'),
-	startDate: z.string().describe('start date filter (ISO 8601)'),
-	endDate: z.string().describe('end date filter (ISO 8601)'),
+	database: z.string().min(1).describe('the database name'),
+	orgId: z.string().min(1).describe('the organization ID'),
+	region: z.string().min(1).describe('the region'),
+	startDate: z.string().min(1).describe('start date filter (ISO 8601)'),
+	endDate: z.string().min(1).describe('end date filter (ISO 8601)'),
 });
@@
 export async function dbLogStats(
 	client: APIClient,
 	request: DbLogStatsRequest
 ): Promise<DbLogStatsResponse> {
 	const { database, orgId, region, startDate, endDate } = request;
 
-	if (!orgId || !region) {
-		throw new DbInvalidArgumentError({ message: 'orgId and region are required', orgId, region });
+	if (!database || !orgId || !region || !startDate || !endDate) {
+		throw new DbInvalidArgumentError({
+			message: 'database, orgId, region, startDate, and endDate are required',
+			database,
+			orgId,
+			region,
+			startDate,
+			endDate,
+		});
 	}
@@
-	const url = `/resource/${orgId}/${region}/${database}/logs/stats?${params.toString()}`;
+	const url = `/resource/${encodeURIComponent(orgId)}/${encodeURIComponent(region)}/${encodeURIComponent(database)}/logs/stats?${params.toString()}`;
#!/bin/bash
# Read-only verification: confirm whether db service URL builders encode path segments.
# Expected: routes interpolating dynamic path segments should use encodeURIComponent.

rg -n -C2 "const url = \`/resource/\$\{.*\}/\$\{.*\}/\$\{.*\}" packages/core/src/services/db
rg -n -C2 "encodeURIComponent\(" packages/core/src/services/db

Also applies to: 89-97

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/core/src/services/db/stats.ts` around lines 6 - 12,
DbLogStatsRequestSchema currently declares required fields but the runtime
checks only validate a subset and path segments (database, orgId, region) are
interpolated unescaped; update the runtime validation to use
DbLogStatsRequestSchema.parse/parseAsync where requests are handled (or add an
explicit guard using DbLogStatsRequestSchema.parse) to enforce all five fields,
and when constructing route paths or URLs (the code that builds the path using
database, orgId, region—search for occurrences where those values are
interpolated into template literals) wrap each dynamic segment with
encodeURIComponent(database), encodeURIComponent(orgId),
encodeURIComponent(region) so path segments are properly escaped and runtime
input fully validated.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@packages/core/src/services/db/stats.ts`:
- Around line 6-12: DbLogStatsRequestSchema currently declares required fields
but the runtime checks only validate a subset and path segments (database,
orgId, region) are interpolated unescaped; update the runtime validation to use
DbLogStatsRequestSchema.parse/parseAsync where requests are handled (or add an
explicit guard using DbLogStatsRequestSchema.parse) to enforce all five fields,
and when constructing route paths or URLs (the code that builds the path using
database, orgId, region—search for occurrences where those values are
interpolated into template literals) wrap each dynamic segment with
encodeURIComponent(database), encodeURIComponent(orgId),
encodeURIComponent(region) so path segments are properly escaped and runtime
input fully validated.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3c9542a6-b1b5-4611-870f-975662bf7bbf

📥 Commits

Reviewing files that changed from the base of the PR and between d493c03 and e0d3487.

📒 Files selected for processing (1)
  • packages/core/src/services/db/stats.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Sandbox CLI Tests
  • GitHub Check: Playwright E2E Smoke Test
  • GitHub Check: Queue CLI Tests
  • GitHub Check: Storage CLI Tests
  • GitHub Check: Framework Integration Tests (TanStack & Next.js)
  • GitHub Check: Build
  • GitHub Check: Template Integration Tests
  • GitHub Check: Package Installation & Usage Test
  • GitHub Check: SDK Integration Test Suite
  • GitHub Check: Postgres SSL Integration Test
  • GitHub Check: Cloud Deployment Tests
🧰 Additional context used
📓 Path-based instructions (3)
packages/core/src/**/*.ts

📄 CodeRabbit inference engine (packages/core/AGENTS.md)

packages/core/src/**/*.ts: Use TypeScript for all code in the @agentuity/core package
Prefer interfaces for public API definitions in @agentuity/core
Use generic types for reusable type utilities in @agentuity/core
Ensure no side effects in exports - all exports must be pure functions or types with no global mutations
Ensure code is compatible with both Browser and Node/Bun runtime environments

Files:

  • packages/core/src/services/db/stats.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use Biome as code formatter with tabs (width 3), single quotes, semicolons, lineWidth 100, and trailingCommas es5

Files:

  • packages/core/src/services/db/stats.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx}: Use TypeScript Strict mode with ESNext target and bundler moduleResolution
Use StructuredError from @agentuity/core for error handling

Files:

  • packages/core/src/services/db/stats.ts
🧠 Learnings (8)
📓 Common learnings
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/auth/AGENTS.md:0-0
Timestamp: 2026-01-02T15:01:46.349Z
Learning: Applies to packages/auth/**/schema.ts : Export the Drizzle schema from 'agentuity/auth/schema' for merging with application schemas
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/auth/AGENTS.md:0-0
Timestamp: 2026-01-02T15:01:46.349Z
Learning: Applies to packages/auth/**/schema.ts : Define Drizzle table schemas and relations in the schema.ts file
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/server/AGENTS.md:0-0
Timestamp: 2026-03-03T16:56:23.099Z
Learning: Applies to packages/server/src/index.ts : Re-export `agentuity/core` types and `z` from zod in the main entry point
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/drizzle/AGENTS.md:0-0
Timestamp: 2026-01-30T21:56:42.250Z
Learning: Applies to packages/drizzle/**/*.{ts,tsx} : Define schemas using Drizzle's `pgTable` and column types rather than other schema definition methods
📚 Learning: 2026-03-02T21:30:56.436Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-03-02T21:30:56.436Z
Learning: Applies to **/*.{ts,tsx,js,jsx} : Use Biome as code formatter with tabs (width 3), single quotes, semicolons, lineWidth 100, and trailingCommas es5

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-02-06T06:54:11.483Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: apps/testing/webrtc-test/src/api/AGENTS.md:0-0
Timestamp: 2026-02-06T06:54:11.483Z
Learning: Applies to apps/testing/webrtc-test/src/api/src/api/**/route.ts : Use agentuity/schema or any Standard Schema compatible library for request validation in API routes

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-02-06T06:54:02.599Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: apps/testing/webrtc-test/src/agent/AGENTS.md:0-0
Timestamp: 2026-02-06T06:54:02.599Z
Learning: Applies to apps/testing/webrtc-test/src/agent/src/agent/**/route.ts : Routes must use agent.validator() for POST endpoints to validate input against agent schema

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2025-12-13T04:17:48.194Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: apps/testing/integration-suite/src/api/AGENTS.md:0-0
Timestamp: 2025-12-13T04:17:48.194Z
Learning: Applies to apps/testing/integration-suite/src/api/src/api/*/route.ts : Implement request validation using agentuity/schema or any Standard Schema compatible library with createRouter.validator()

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-01-31T00:58:50.803Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: apps/docs/src/api/AGENTS.md:0-0
Timestamp: 2026-01-31T00:58:50.803Z
Learning: Applies to apps/docs/src/api/src/api/**/route.ts : Use agentuity/schema or any Standard Schema compatible library for request validation in routes

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2026-01-02T15:02:45.328Z
Learnt from: CR
Repo: agentuity/sdk PR: 0
File: packages/runtime/AGENTS.md:0-0
Timestamp: 2026-01-02T15:02:45.328Z
Learning: Applies to packages/runtime/**/routes/**/*.{ts,tsx} : Use `agent.validator()` for automatic input validation in route handlers

Applied to files:

  • packages/core/src/services/db/stats.ts
📚 Learning: 2025-12-21T00:31:41.858Z
Learnt from: jhaynie
Repo: agentuity/sdk PR: 274
File: packages/cli/src/cmd/build/vite/server-bundler.ts:12-41
Timestamp: 2025-12-21T00:31:41.858Z
Learning: In Bun runtime, BuildMessage and ResolveMessage are global types and are not exported from the bun module. Do not import { BuildMessage } from 'bun' or similar; these types are available globally and should be used without import. This applies to all TypeScript files that target the Bun runtime within the repository.

Applied to files:

  • packages/core/src/services/db/stats.ts
🧬 Code graph analysis (1)
packages/core/src/services/db/stats.ts (2)
packages/core/src/services/db/index.ts (10)
  • DbLogStatsQueryPatternSchema (14-14)
  • DbLogStatsCommandBreakdownSchema (12-12)
  • DbLogStatsResponseSchema (17-17)
  • DbLogStatsAPIResponseSchema (11-11)
  • DbLogStatsSummary (20-20)
  • DbLogStatsTimeSeriesPoint (22-22)
  • DbLogStatsQueryPattern (15-15)
  • DbLogStatsCommandBreakdown (13-13)
  • DbLogStatsResponse (18-18)
  • dbLogStats (23-23)
packages/core/src/services/api.ts (2)
  • APIResponseSchema (915-926)
  • request (308-370)
🔇 Additional comments (1)
packages/core/src/services/db/stats.ts (1)

15-68: Clean schema layering and API wrapper composition.

The summary/time-series/query-pattern/command-breakdown schemas are well structured, and wrapping with APIResponseSchema(...) keeps the response contract consistent.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
packages/core/src/services/db/stats.ts (1)

90-94: ⚠️ Potential issue | 🟡 Minor

Include all validated fields in the invalid-argument error context.

The message at Line 91 references database, startDate, and endDate, but the error payload only includes orgId and region. Include all checked fields for consistent diagnostics.

Proposed fix
 	if (!orgId || !region || !database || !startDate || !endDate) {
 		throw new DbInvalidArgumentError({
 			message: 'orgId, region, database, startDate, and endDate are required',
+			database,
 			orgId,
 			region,
+			startDate,
+			endDate,
 		});
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/core/src/services/db/stats.ts` around lines 90 - 94, The thrown
DbInvalidArgumentError in stats.ts currently only includes orgId and region but
the message references database, startDate, and endDate; update the error
context passed to new DbInvalidArgumentError in the function that validates
inputs (the throw at the DbInvalidArgumentError instantiation) to include all
validated fields: orgId, region, database, startDate, and endDate so diagnostics
match the error message.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/core/src/services/db/stats.ts`:
- Around line 87-89: Guard against a missing request object before destructuring
so we throw the intended DbInvalidArgumentError instead of a native TypeError:
at the start of the function in packages/core/src/services/db/stats.ts check if
request is null/undefined (e.g. if (!request) throw new
DbInvalidArgumentError('request is required')), then perform the existing
destructuring const { database, orgId, region, startDate, endDate } = request;
and keep the subsequent checks that validate each field.

---

Duplicate comments:
In `@packages/core/src/services/db/stats.ts`:
- Around line 90-94: The thrown DbInvalidArgumentError in stats.ts currently
only includes orgId and region but the message references database, startDate,
and endDate; update the error context passed to new DbInvalidArgumentError in
the function that validates inputs (the throw at the DbInvalidArgumentError
instantiation) to include all validated fields: orgId, region, database,
startDate, and endDate so diagnostics match the error message.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: eb46f32d-9ca8-4e7a-ac8b-c166c79fd87d

📥 Commits

Reviewing files that changed from the base of the PR and between e0d3487 and 0e866e7.

📒 Files selected for processing (1)
  • packages/core/src/services/db/stats.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Sandbox CLI Tests
🧰 Additional context used
📓 Path-based instructions (3)
packages/core/src/**/*.ts

📄 CodeRabbit inference engine (packages/core/AGENTS.md)

packages/core/src/**/*.ts: Use TypeScript for all code in the @agentuity/core package
Prefer interfaces for public API definitions in @agentuity/core
Use generic types for reusable type utilities in @agentuity/core
Ensure no side effects in exports - all exports must be pure functions or types with no global mutations
Ensure code is compatible with both Browser and Node/Bun runtime environments

Files:

  • packages/core/src/services/db/stats.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use Biome as code formatter with tabs (width 3), single quotes, semicolons, lineWidth 100, and trailingCommas es5

Files:

  • packages/core/src/services/db/stats.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx}: Use TypeScript Strict mode with ESNext target and bundler moduleResolution
Use StructuredError from @agentuity/core for error handling

Files:

  • packages/core/src/services/db/stats.ts
🧠 Learnings (1)
📚 Learning: 2025-12-21T00:31:41.858Z
Learnt from: jhaynie
Repo: agentuity/sdk PR: 274
File: packages/cli/src/cmd/build/vite/server-bundler.ts:12-41
Timestamp: 2025-12-21T00:31:41.858Z
Learning: In Bun runtime, BuildMessage and ResolveMessage are global types and are not exported from the bun module. Do not import { BuildMessage } from 'bun' or similar; these types are available globally and should be used without import. This applies to all TypeScript files that target the Bun runtime within the repository.

Applied to files:

  • packages/core/src/services/db/stats.ts
🔇 Additional comments (1)
packages/core/src/services/db/stats.ts (1)

71-75: No refactor needed; Zod-inferred types should remain as type aliases.

The codebase consistently uses export type TypeName = z.infer<...> for all Zod schema types across the core package (e.g., packages/core/src/services/webhook/types.ts). The "prefer interfaces for public API definitions" guideline applies to abstract service contracts and structural types, not Zod-inferred types. Converting these to interfaces would create unnecessary inconsistency within the codebase.

Comment on lines +87 to +89
const { database, orgId, region, startDate, endDate } = request;

if (!orgId || !region || !database || !startDate || !endDate) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Guard request before destructuring to preserve structured error handling.

At Line 87, destructuring happens before any runtime guard. If a JS consumer passes null/undefined, this throws a native TypeError instead of DbInvalidArgumentError.

Proposed fix
 export async function dbLogStats(
 	client: APIClient,
 	request: DbLogStatsRequest
 ): Promise<DbLogStatsResponse> {
+	if (!request || typeof request !== 'object') {
+		throw new DbInvalidArgumentError({
+			message: 'request is required',
+		});
+	}
+
 	const { database, orgId, region, startDate, endDate } = request;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { database, orgId, region, startDate, endDate } = request;
if (!orgId || !region || !database || !startDate || !endDate) {
export async function dbLogStats(
client: APIClient,
request: DbLogStatsRequest
): Promise<DbLogStatsResponse> {
if (!request || typeof request !== 'object') {
throw new DbInvalidArgumentError({
message: 'request is required',
});
}
const { database, orgId, region, startDate, endDate } = request;
if (!orgId || !region || !database || !startDate || !endDate) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/core/src/services/db/stats.ts` around lines 87 - 89, Guard against a
missing request object before destructuring so we throw the intended
DbInvalidArgumentError instead of a native TypeError: at the start of the
function in packages/core/src/services/db/stats.ts check if request is
null/undefined (e.g. if (!request) throw new DbInvalidArgumentError('request is
required')), then perform the existing destructuring const { database, orgId,
region, startDate, endDate } = request; and keep the subsequent checks that
validate each field.

@jhaynie jhaynie merged commit 9f42dc3 into main Mar 5, 2026
16 of 18 checks passed
@jhaynie jhaynie deleted the task/db-slow-queries branch March 5, 2026 14:54
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