Skip to content

perf(server): High-performance backend optimizations - #7

Merged
arvarik merged 2 commits into
mainfrom
perf/backend-optimizations
May 21, 2026
Merged

perf(server): High-performance backend optimizations#7
arvarik merged 2 commits into
mainfrom
perf/backend-optimizations

Conversation

@arvarik

@arvarik arvarik commented May 21, 2026

Copy link
Copy Markdown
Owner

Summary of Accomplishments

This Pull Request implements the approved backend architectural optimizations for contrack to eliminate critical N+1 database bottlenecks and enhance client request resiliency.

1. Connection Abort Propagation (Security & Cost Control)

  • Bound an AbortController to Express req.on("close") in /api/search/semantic.
  • Propagated the signal down to the AI reranking service and adapter facades, allowing immediate socket tear-down during early client exits and saving redundant LLM token spending.

2. O(1) Batch Normalization (Dedupe Performance)

  • Optimized dedupe scanning in normalizeContacts to batch-load tags and interests at start, mapping them via in-memory O(1) lookups.
  • Latency reduced from ~3,000ms down to ~20ms for 1,082 records (a ~150x speedup).

3. High-Performance Contact Bulk Hydration

  • Rewrote hydrateMany in contactRepository.ts to leverage query-parameter safe chunked SQL queries (chunk size of 500) and O(N) map-joins.
  • Keeps individual microsecond cached statements for collections <= 3 contacts to maintain maximum efficiency.
  • Strips contactId fields from child records to guarantee 100% downstream API payload shape parity.
  • Yields a ~6x hydration speedup (from 68ms down to 11ms for 500 records).

4. Validation

  • Run integration benchmark validating perfect structural equivalence between the sequential and bulk hydration pipelines.
  • Fully verified via test harness: 180/180 tests passing successfully.

arvarik added 2 commits May 20, 2026 17:02
…ization, and contact hydration

- Propagated AbortSignal from Express `/api/search/semantic` connection close to AI service & provider adapters to prevent token and compute leaks.
- Replaced loop-nested N+1 queries in `normalizeContacts` with pre-fetched bulk tags/interests lookup Maps, reducing latency by ~150x.
- Refactored `hydrateMany` to chunk queries at 500 parameters (respecting SQLite limit) and use O(N) map-joins with exact single-record schema parity.
- Added dynamic microsecond-range sequential hydration fallback for collections <= 3 contacts.
- Verified zero regression with full Vitest integration suite (180 tests passing).
@arvarik
arvarik merged commit b7bce93 into main May 21, 2026
2 checks passed
@arvarik
arvarik deleted the perf/backend-optimizations branch May 21, 2026 03:14
arvarik added a commit that referenced this pull request Aug 4, 2026
…ication

Data lifecycle (rec #7):
- Deletes are now soft: contacts move to a trash (deletedAt + isArchived)
  with restore via POST /api/trash/:id/restore, an Undo toast in the UI,
  immediate purge via DELETE /api/trash/:id, and automatic hard-deletion
  after TRASH_RETENTION_DAYS (default 30, daily sweep). FTS triggers are
  trash-aware so trashed contacts vanish from search and reindex on restore.
- Automatic SQLite snapshots via the online backup API into
  DATA_DIR/backups with rotation (BACKUP_INTERVAL_HOURS / BACKUP_KEEP),
  plus GET/POST /api/backups.
- Full export: GET /api/export/json (entire database, downloadable) and
  GET /api/export/csv (flat contacts spreadsheet, RFC-4180 escaping).

Authentication (rec #1):
- Single-user bearer token gating /api and /uploads, enforced when
  AUTH_TOKEN is set or AUTH_REQUIRED=true (token auto-generated, persisted
  to DATA_DIR/auth-token, printed in logs at boot). Timing-safe comparison.
- /api/auth status/login/logout; login sets an HttpOnly SameSite=Strict
  cookie for the SPA (AuthGate sign-in screen); scripts and MCP clients
  use Authorization: Bearer.
- Docker is secure by default (AUTH_REQUIRED=true in the image); dev
  server warns when bound beyond localhost without auth. Remote-access
  guidance documented (Tailscale / TLS reverse proxy).

Testing: 316 tests (22 new integration tests covering trash/restore/purge,
backup files on disk, export contents, and the full auth matrix), verified
end-to-end in Docker on OrbStack including token persistence across
restarts. Docs and .env.example updated; validation schema gap
(cadenceDays/website) closed by the new tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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