feat(data-os): Source Registry Postgres persistence + full HTTP route surface

Replaces the in-memory (per-instance, not even per-process-shared) Maps in
SourceService/SourceRegistryService with real Postgres persistence, and adds
the register/query/describe/health/rights/provenance/freshness HTTP routes
that never existed (grep for SourceRegistryService in app/ previously
returned nothing).

- sql/002_source_registry.sql: opt_sources, opt_source_rights,
  opt_source_provenance, opt_source_health_checks, opt_source_freshness.
  Numbered 002 (not 006) because 004's opt_source_rate_policies already
  forward-references opt_sources, which no sql/ file defined until now —
  fixes 004's pre-existing broken migration ordering (opt_datasets remains
  a separate, unrelated, still-open gap in 004, flagged not fixed).
- src/repositories/*: SourceRepository / SourceRegistryRepository seams,
  each with an in-memory implementation (business logic extracted verbatim)
  and a Postgres implementation, chosen via resolveStoreMode — production
  still fails hard with no DATABASE_URL (Master-DoD §3), but a configured
  production now actually works instead of always being blocked.
- SourceService / SourceRegistryService: storage moved behind the
  repository seam; getHealth/getFreshness are now async and tenant-scoped
  at the accessor itself (needed once they're reachable directly over HTTP,
  not only after an earlier caller's tenant check).
- src/services/source-registry.ts: shared singleton instances for the
  sources/* route group, fixing the "each route module owned its own
  private registry" defect (source-rate-policies and .../query previously
  couldn't see a source registered via POST /api/0pt/sources).
- New routes: POST/GET /api/0pt/sources, GET /api/0pt/sources/[id],
  GET+PUT .../rights, GET+PUT .../provenance, GET+POST .../health,
  GET .../freshness.
- Business logic unchanged: fail-closed purpose gate, cross-tenant
  NOT_FOUND (no existence oracle, §50), bounded 100-row health history.

Tested against a real Postgres container (not mocked) —
tests/p0/source-registry-postgres.realinfra.test.ts, env-gated on
SOURCE_REGISTRY_TEST_DATABASE_URL, skips honestly when unset. All 249
pre-existing P0 tests + 7 new realinfra tests pass; also smoke-tested the
full HTTP route surface end-to-end against next dev + live Postgres.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Simon Axelsson
2026-09-01 20:42:41 +02:00
co-authored by Claude Sonnet 5
parent f8bb0d5ab9
commit 07b13bd833
26 changed files with 1578 additions and 177 deletions
+58
View File
@@ -95,6 +95,64 @@ export const createSourceRatePolicySchema = z.object({
created_by: z.string().min(1).optional(),
});
/** Nested `rights` block on POST /api/0pt/sources, and the body of PUT /api/0pt/sources/[sourceId]/rights -- mirrors SourceRights (minus source_id/tenant_id, which come from the URL/outer body). */
const sourceRightsInputSchema = z.object({
license_id: z.string().min(1),
license_url: z.string().min(1).optional(),
allowed_purposes: z.array(z.string().min(1)).default([]),
pii_allowed: z.boolean(),
redistribution: z.enum(['internal', 'customer_deliverable', 'public', 'prohibited']),
attribution: z.string().min(1).optional(),
verified_at: z.string().min(1),
verified_by: z.string().min(1),
notes: z.string().min(1).optional(),
});
/** Nested `provenance` block on POST /api/0pt/sources, and the body of PUT /api/0pt/sources/[sourceId]/provenance -- mirrors SourceProvenance (minus source_id/tenant_id/updated_at, which are set by the service). */
const sourceProvenanceInputSchema = z.object({
method: z.enum(['official_api', 'bulk_download', 'authorized_web', 'licensed_feed', 'manual_upload']),
authority: z.string().min(1).optional(),
compliance_evidence_ref: z.string().min(1).optional(),
cadence: z.string().min(1).optional(),
freshness_sla_hours: z.number().nonnegative().optional(),
});
/** POST /api/0pt/sources -- source.register. */
export const registerSourceSchema = z.object({
source_id: z.string().min(1),
tenant_id: z.string().min(1),
project_id: z.string().min(1),
source_type: z.enum(['database', 'file', 'web', 'api', 'stream', 'manual']),
name: z.string().min(1),
adapter_id: z.string().min(1).optional(),
access: z.enum(['api', 'bulk', 'authorized_web', 'licensed']).optional(),
description: z.string().min(1).optional(),
status: z.enum(['active', 'paused', 'archived', 'deprecated']).optional(),
correlation_id: z.string().min(1).optional(),
created_by: z.string().min(1).optional(),
rights: sourceRightsInputSchema.optional(),
provenance: sourceProvenanceInputSchema.optional(),
});
/** PUT /api/0pt/sources/[sourceId]/rights -- source.set_rights. */
export const setSourceRightsSchema = sourceRightsInputSchema.extend({
tenant_id: z.string().min(1),
});
/** PUT /api/0pt/sources/[sourceId]/provenance -- source.set_provenance. */
export const setSourceProvenanceSchema = sourceProvenanceInputSchema.extend({
tenant_id: z.string().min(1),
});
/** POST /api/0pt/sources/[sourceId]/health -- source.health (record a health check). */
export const recordSourceHealthCheckSchema = z.object({
tenant_id: z.string().min(1),
status: z.enum(['up', 'degraded', 'down', 'unknown']),
checked_at: z.string().min(1).optional(),
latency_ms: z.number().nonnegative().optional(),
detail: z.string().min(1).optional(),
});
/** POST /api/0pt/lakehouse/mappings -- mirrors MapDatasetToIcebergTableInput. */
export const mapDatasetToIcebergTableSchema = z.object({
tenant_id: z.string().min(1),