import type { SourceRatePolicy, SourceRatePolicyStatus, CreateSourceRatePolicyInput, } from "../types/source-rate-policy"; import { createAud0Client, type Aud0Client } from "../clients/aud0-client"; import { SourceService } from "./source-service"; interface SourceRatePolicyRecord { source_rate_policy_id: string; tenant_id: string; project_id: string; source_id: string; rate_limit_per_minute: number; crawl_budget: number; compliance_notes: string | null; domain: string | null; robots_ok: boolean | null; access: string | null; status: string; correlation_id: string | null; created_at: string; updated_at: string | null; created_by: string | null; } /** * Owns 0pt.source_rate_policy.schema.v1 -- the genuine ownership repair: * source/crawl-domain rate limiting is 0pt's own compliance policy, moved * OUT of N0D's ComputePolicy.sourceRatePolicy (deprecated there per * ADR-0030's audit). See docs/ICEBERG_WRITE_BOUNDARY.md. */ export class SourceRatePolicyService { private policies: Map = new Map(); private aud0: Aud0Client; private sourceService: SourceService; constructor(opts?: { aud0BaseUrl?: string; sourceService?: SourceService }) { this.aud0 = createAud0Client(opts?.aud0BaseUrl); this.sourceService = opts?.sourceService ?? new SourceService({ aud0BaseUrl: opts?.aud0BaseUrl }); } async createSourceRatePolicy(input: CreateSourceRatePolicyInput): Promise { if (this.policies.has(input.source_rate_policy_id)) { throw new Error(`Source rate policy ${input.source_rate_policy_id} already exists`); } if (input.rate_limit_per_minute <= 0) { throw new Error("rate_limit_per_minute must be > 0"); } if (input.crawl_budget <= 0) { throw new Error("crawl_budget must be > 0"); } // Verify the source exists -- a rate policy with no source is meaningless. try { await this.sourceService.getSource(input.source_id); } catch { throw new Error(`Source ${input.source_id} not found`); } const now = new Date().toISOString(); const record: SourceRatePolicyRecord = { source_rate_policy_id: input.source_rate_policy_id, tenant_id: input.tenant_id, project_id: input.project_id, source_id: input.source_id, rate_limit_per_minute: input.rate_limit_per_minute, crawl_budget: input.crawl_budget, compliance_notes: input.compliance_notes ?? null, domain: input.domain ?? null, robots_ok: input.robots_ok ?? null, access: input.access ?? null, status: input.status ?? "active", correlation_id: input.correlation_id ?? null, created_at: now, updated_at: null, created_by: input.created_by ?? null, }; this.policies.set(input.source_rate_policy_id, record); // 0pt.source_rate_policy_created -- docs/11-lakehouse-write-plan.md event list, // docs/ICEBERG_WRITE_BOUNDARY.md. Gives AUD0/CL0UD an evented trail of 0pt's // crawl-compliance posture without polling 0pt's DB. await this.aud0.ingestEvent({ event_id: `evt_0pt_source_rate_policy_created_${input.source_rate_policy_id}`, event_type: "0pt.source_rate_policy_created", tenant_id: input.tenant_id, project_id: input.project_id, actor_id: input.created_by ?? "system", app_id: "0pt", capability_id: "0pt.create_source_rate_policy", resource_type: "source_rate_policy", resource_id: input.source_rate_policy_id, risk_level: "L2", payload: { source_id: input.source_id, rate_limit_per_minute: input.rate_limit_per_minute, crawl_budget: input.crawl_budget, }, created_at: now, correlation_id: input.correlation_id ?? "", }); return this.toPolicy(record); } // P014: `tenantId` is optional so existing internal callers that already // resolved a policy from a tenant-scoped list (or are trusted, in-process // callers) are unaffected — but the HTTP route now always passes it, which // closes a real gap: this lookup used to be reachable by rate-policy ID // alone with NO tenant check anywhere, so any authenticated caller could // read another tenant's crawl-budget/rate-limit/compliance-notes record // just by guessing or enumerating IDs. A mismatched tenantId is reported // identically to "not found" (no existence oracle), matching the // convention SourceRegistryService.assertSourceTenant already established. async getSourceRatePolicy(sourceRatePolicyId: string, tenantId?: string): Promise { const record = this.policies.get(sourceRatePolicyId); if (!record || (tenantId && record.tenant_id !== tenantId)) { throw new Error(`Source rate policy ${sourceRatePolicyId} not found`); } return this.toPolicy(record); } async listSourceRatePolicies(filter?: { tenant_id?: string; project_id?: string; source_id?: string; status?: SourceRatePolicyStatus; }): Promise { let results = Array.from(this.policies.values()); if (filter?.tenant_id) results = results.filter((r) => r.tenant_id === filter.tenant_id); if (filter?.project_id) results = results.filter((r) => r.project_id === filter.project_id); if (filter?.source_id) results = results.filter((r) => r.source_id === filter.source_id); if (filter?.status) results = results.filter((r) => r.status === filter.status); return results.map((r) => this.toPolicy(r)); } private toPolicy(r: SourceRatePolicyRecord): SourceRatePolicy { return { source_rate_policy_id: r.source_rate_policy_id, tenant_id: r.tenant_id, project_id: r.project_id, source_id: r.source_id, rate_limit_per_minute: r.rate_limit_per_minute, crawl_budget: r.crawl_budget, compliance_notes: r.compliance_notes ?? undefined, domain: r.domain ?? undefined, robots_ok: r.robots_ok ?? undefined, access: (r.access as SourceRatePolicy["access"]) ?? undefined, status: r.status as SourceRatePolicyStatus, correlation_id: r.correlation_id ?? undefined, created_at: r.created_at, updated_at: r.updated_at ?? undefined, created_by: r.created_by ?? undefined, }; } }