Skip to content

refactor: consolidate duplicated cross-cutting constants#34

Merged
tianzhou merged 1 commit into
mainfrom
refactor/consolidate-constants
Jun 26, 2026
Merged

refactor: consolidate duplicated cross-cutting constants#34
tianzhou merged 1 commit into
mainfrom
refactor/consolidate-constants

Conversation

@tianzhou

Copy link
Copy Markdown
Contributor

What

Adds a dependency-free src/lib/constants.ts as the single source of truth for constants that are duplicated or span the client/server boundary, and replaces the scattered magic numbers with named imports (task #10).

Constant Value Was
AUDIT_LOG_FETCH_LIMIT 1000 client AUDIT_LOG_LIMIT and server clamp Math.min(req.limit, 1000) ×2 — these had already drifted into the 500-vs-1000 bug
AUDIT_LOG_DEFAULT_LIMIT 100 inline : 100 default in both audit handlers
LIVE_QUERY_REFETCH_INTERVAL_MS 5000 inlined refetchInterval: 5000 ×3 (active sessions + both audit hooks)
MCP_MAX_RESULT_ROWS 1000 MAX_RESULT_ROWS in mcp.ts
MCP_CATALOG_PAGE_SIZE 100 PAGE_SIZE in mcp.ts

mcp.ts keeps its local MAX_RESULT_ROWS / PAGE_SIZE names (now sourced from the shared module) so existing internal usages and tests/mcp.test.ts's import stay intact.

Scope (deliberate)

Limited to duplicated / cross-tier / contract constants. Well-named feature-local constants (panel widths, MOBILE_BREAKPOINT, MAX_AUTO_RETRIES, AI token caps, cookie maxAge, …) are intentionally left next to their feature — a global "constants drawer" hurts locality and review-ability. Scope approved by @tianzhou and @PG-Codex in the task thread.

Verification

  • pnpm build — passed (existing Vite/Rolldown warnings only)
  • pnpm test tests/audit.test.ts tests/mcp.test.ts — 53 passed
  • eslint on changed lines — clean (the 2 pre-existing query-service.ts lint errors — tableInfo prefer-const, unused definitionLower — are unrelated and left out of scope, consistent with prior PRs)

🤖 Generated with Claude Code

Introduce a dependency-free src/lib/constants.ts as the single source of
truth for constants that are duplicated or span the client/server
boundary, and replace the scattered magic numbers with named imports:

- AUDIT_LOG_FETCH_LIMIT (1000): the audit-log fetch limit, used by both
  the client request (useQuery) and the server-side clamp (query-service).
  These previously drifted (client 1000 vs server 500); now they can't.
- AUDIT_LOG_DEFAULT_LIMIT (100): server default when no limit is given.
- LIVE_QUERY_REFETCH_INTERVAL_MS (5000): polling cadence shared by the
  active-sessions and audit-log queries (was inlined 3x).
- MCP_MAX_RESULT_ROWS (1000) / MCP_CATALOG_PAGE_SIZE (100): MCP row cap
  and catalog page size (mcp.ts keeps its local MAX_RESULT_ROWS/PAGE_SIZE
  names, now sourced from the shared module, so importers/tests are intact).

Scope is intentionally limited to duplicated / cross-tier / contract
constants; well-named feature-local constants (panel widths, breakpoints,
retry counts, etc.) are left next to their feature.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 26, 2026 08:29
@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pgconsole Ready Ready Preview, Comment Jun 26, 2026 8:29am

Copilot AI left a comment

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.

Pull request overview

This PR introduces a dependency-free shared constants module (src/lib/constants.ts) to eliminate duplicated cross-tier “magic numbers” and ensure client/server runtime contracts (notably audit-log limits and MCP row caps) stay in sync.

Changes:

  • Added src/lib/constants.ts as a single source of truth for shared audit-log, live polling, and MCP constants.
  • Updated client query hooks to use named constants for audit-log fetch limits and polling intervals.
  • Updated server audit-log handlers and MCP server constants to source values from the shared module while preserving existing local constant names where needed.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/lib/constants.ts Adds centralized, dependency-free shared constants for cross-tier contracts and duplicated values.
src/hooks/useQuery.ts Replaces inline polling intervals and audit-log limit constant with shared imports.
server/services/query-service.ts Replaces hardcoded audit-log limit clamp/default values with shared constants.
server/mcp.ts Sources MCP page size and max result rows from shared constants while keeping existing exported names.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown

Greptile Summary

This PR introduces src/lib/constants.ts as a single source of truth for five constants that were either duplicated across files or spanned the client/server boundary, replacing scattered magic numbers with named imports.

  • Audit log: removes the client-side AUDIT_LOG_LIMIT = 1000 local constant and the two inline Math.min(req.limit, 1000) : 100 server expressions, replacing both with AUDIT_LOG_FETCH_LIMIT and AUDIT_LOG_DEFAULT_LIMIT so the client request limit and server hard-clamp can never drift apart again.
  • Live polling: consolidates three independent refetchInterval: 5000 inline values (active processes, audit log, system audit log) into LIVE_QUERY_REFETCH_INTERVAL_MS.
  • MCP: promotes MAX_RESULT_ROWS = 1000 and PAGE_SIZE = 100 in mcp.ts to named shared constants while keeping the local aliases intact so existing internal usages and tests are unaffected.

Confidence Score: 4/5

Safe to merge — all value changes are no-ops (same numbers, different names) and tests confirm no regressions.

The refactor is mechanically correct and tests pass. The one thing worth a follow-up is that server/ now imports from src/lib/, a boundary enforced only by a comment. If that invariant is ever violated by accident the server build breaks, but since the file is currently four literal export const declarations the risk is low today.

server/mcp.ts — the ../src/lib/constants import path crosses the server/client directory boundary without a lint or tsconfig guard.

Important Files Changed

Filename Overview
src/lib/constants.ts New file — clean, dependency-free, well-documented module holding five named constants; correctly scoped to cross-cutting/duplicated values only.
server/mcp.ts Imports MCP_CATALOG_PAGE_SIZE and MCP_MAX_RESULT_ROWS from src/lib/constants; keeps local PAGE_SIZE and exported MAX_RESULT_ROWS aliases for backwards compatibility — but the cross-src import is not enforced by tooling.
server/services/query-service.ts Both audit-log handlers now use AUDIT_LOG_FETCH_LIMIT and AUDIT_LOG_DEFAULT_LIMIT from the shared module; values are unchanged (1000 and 100) and the diff is a straightforward replacement.
src/hooks/useQuery.ts Removes local AUDIT_LOG_LIMIT constant and replaces three inline 5000 refetchInterval values; now aligned with server through shared constants.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    C["src/lib/constants.ts\n(single source of truth)"]

    C -->|AUDIT_LOG_FETCH_LIMIT\nAUDIT_LOG_DEFAULT_LIMIT| QS["server/services/query-service.ts\ngetAuditLogEntries\ngetSystemAuditLogEntries"]

    C -->|MCP_MAX_RESULT_ROWS\nMCP_CATALOG_PAGE_SIZE| MCP["server/mcp.ts\nMAX_RESULT_ROWS\nPAGE_SIZE"]

    C -->|AUDIT_LOG_FETCH_LIMIT\nLIVE_QUERY_REFETCH_INTERVAL_MS| UQ["src/hooks/useQuery.ts\nuseAuditLogEntries\nuseSystemAuditLogEntries\nuseActiveProcesses"]
Loading
%%{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"}}}%%
flowchart TD
    C["src/lib/constants.ts\n(single source of truth)"]

    C -->|AUDIT_LOG_FETCH_LIMIT\nAUDIT_LOG_DEFAULT_LIMIT| QS["server/services/query-service.ts\ngetAuditLogEntries\ngetSystemAuditLogEntries"]

    C -->|MCP_MAX_RESULT_ROWS\nMCP_CATALOG_PAGE_SIZE| MCP["server/mcp.ts\nMAX_RESULT_ROWS\nPAGE_SIZE"]

    C -->|AUDIT_LOG_FETCH_LIMIT\nLIVE_QUERY_REFETCH_INTERVAL_MS| UQ["src/hooks/useQuery.ts\nuseAuditLogEntries\nuseSystemAuditLogEntries\nuseActiveProcesses"]
Loading

Reviews (1): Last reviewed commit: "refactor: consolidate duplicated cross-c..." | Re-trigger Greptile

Comment thread server/mcp.ts
import { detectRequiredPermissions } from './lib/sql-permissions'
import { buildExecutableSql, formatExecutionError } from './lib/execute-sql'
import { auditSQL } from './lib/audit'
import { MCP_CATALOG_PAGE_SIZE, MCP_MAX_RESULT_ROWS } from '../src/lib/constants'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Server importing from src/lib/ without tooling guard

server/ code now depends on a path inside src/lib/, which is typically the client-side tree. The file's own comment asks it to stay dependency-free, but there is no ESLint boundary rule or separate tsconfig path alias enforcing that invariant. If a future contributor adds any browser-only import to src/lib/constants.ts (or even accidentally imports from another src/lib/ module that does), the server will break at startup — and the error would only surface at runtime or in CI, not at edit time. A shared/ or common/ directory at the root, or a lint rule banning server/** from importing src/**, would make the constraint machine-checked rather than comment-enforced.

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.

Reasonable hardening idea, but worth noting this isn't a new boundary: server/ already imports from src/lib/ today — e.g. server/lib/sql-permissions.ts imports src/lib/sql/core and src/lib/sql/pg-system-functions. So this PR follows the established pattern rather than introducing the cross-tree dependency, and src/lib/constants.ts is pure literal export consts (no imports of its own), so the dependency-free invariant holds today.

Machine-enforcing it (an ESLint no-restricted-imports rule banning browser-only imports from server-reachable src/lib modules, or relocating the genuinely-shared pieces to a root shared/ dir) is a good idea — but it's a repo-wide change that should also cover the existing sql/* imports, so I'd do it as a separate follow-up rather than scope-creep this constants PR. Leaving as-is here.

@tianzhou tianzhou merged commit 60bfcf8 into main Jun 26, 2026
6 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.

2 participants