From e2a97ac3b2756b819dadb08426e7ce7f7cbd5a56 Mon Sep 17 00:00:00 2001 From: Jonathan Haas Date: Wed, 17 Jun 2026 20:49:18 -0700 Subject: [PATCH] Add operating layer backend persistence --- deploy/helm/maestro/templates/deployment.yaml | 8 ++++ deploy/helm/maestro/values.yaml | 7 ++++ src/db/health.ts | 10 +++++ .../0009_operating_layer_evidence.sql | 30 ++++++++++++++ src/db/migrations/meta/_journal.json | 7 ++++ src/db/schema.ts | 41 +++++++++++++++++++ ...hosted-session-storage.integration.test.ts | 2 +- test/db/migration-files.test.ts | 24 +++++++++++ test/deploy/helm-maestro.test.ts | 25 +++++++++++ test/web-health.test.ts | 4 ++ 10 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/db/migrations/0009_operating_layer_evidence.sql diff --git a/deploy/helm/maestro/templates/deployment.yaml b/deploy/helm/maestro/templates/deployment.yaml index 6f3975218..e4e2936e2 100644 --- a/deploy/helm/maestro/templates/deployment.yaml +++ b/deploy/helm/maestro/templates/deployment.yaml @@ -57,6 +57,14 @@ spec: - name: {{ $key }} value: {{ $value | quote }} {{- end }} + {{- if .Values.operatingLayer.enabled }} + - name: MAESTRO_OPERATING_LAYER_ENABLED + value: "1" + - name: MAESTRO_OPERATING_LAYER_EVIDENCE_STORE + value: {{ .Values.operatingLayer.evidenceStore | quote }} + - name: MAESTRO_OPERATING_LAYER_EVIDENCE_RETENTION_DAYS + value: {{ .Values.operatingLayer.evidenceRetentionDays | quote }} + {{- end }} {{- if .Values.writableTmp.enabled }} volumeMounts: - name: tmp diff --git a/deploy/helm/maestro/values.yaml b/deploy/helm/maestro/values.yaml index 69819d012..d07b0e6cd 100644 --- a/deploy/helm/maestro/values.yaml +++ b/deploy/helm/maestro/values.yaml @@ -14,6 +14,13 @@ headlessRuntime: # durable-owner when Platform routes by a durable runtime-owner record. mode: single-replica +operatingLayer: + # Enables production operating-layer evidence capture. Requires a configured + # MAESTRO_DATABASE_URL/DATABASE_URL secret so evidence persists in Postgres. + enabled: false + evidenceStore: database + evidenceRetentionDays: 90 + image: repository: ghcr.io/evalops/maestro # Defaults to Chart.appVersion when empty. Prefer immutable sha-* tags or digest. diff --git a/src/db/health.ts b/src/db/health.ts index 167ea0ab2..d069090bc 100644 --- a/src/db/health.ts +++ b/src/db/health.ts @@ -10,6 +10,7 @@ export const CRITICAL_DATABASE_TABLES = [ "distributed_locks", "usage_metrics", "execution_traces", + "operating_layer_evidence", "workspace_config", "revenue_attribution", ] as const; @@ -39,6 +40,15 @@ const CRITICAL_DATABASE_COLUMNS: Partial< "delivered_at", "created_at", ], + operating_layer_evidence: [ + "id", + "capability_id", + "evidence_kind", + "subject", + "status", + "evidence", + "recorded_at", + ], }; export async function checkCriticalTables( diff --git a/src/db/migrations/0009_operating_layer_evidence.sql b/src/db/migrations/0009_operating_layer_evidence.sql new file mode 100644 index 000000000..4cae62281 --- /dev/null +++ b/src/db/migrations/0009_operating_layer_evidence.sql @@ -0,0 +1,30 @@ +CREATE TABLE IF NOT EXISTS "operating_layer_evidence" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "org_id" uuid, + "session_id" varchar(255), + "capability_id" varchar(64) NOT NULL, + "evidence_kind" varchar(128) NOT NULL, + "subject" varchar(255) NOT NULL, + "status" varchar(32) DEFAULT 'recorded' NOT NULL, + "score" integer, + "blockers" jsonb DEFAULT '[]'::jsonb NOT NULL, + "warnings" jsonb DEFAULT '[]'::jsonb NOT NULL, + "evidence" jsonb NOT NULL, + "metadata" jsonb DEFAULT '{}'::jsonb, + "recorded_at" timestamp with time zone DEFAULT now() NOT NULL, + "expires_at" timestamp with time zone, + CONSTRAINT "operating_layer_evidence_org_id_organizations_id_fk" + FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE cascade +); +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "operating_layer_evidence_capability_recorded_idx" + ON "operating_layer_evidence" ("capability_id", "recorded_at"); +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "operating_layer_evidence_org_recorded_idx" + ON "operating_layer_evidence" ("org_id", "recorded_at"); +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "operating_layer_evidence_session_recorded_idx" + ON "operating_layer_evidence" ("session_id", "recorded_at"); +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "operating_layer_evidence_expiry_idx" + ON "operating_layer_evidence" ("expires_at"); diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json index 64cf4773d..ba22e747c 100644 --- a/src/db/migrations/meta/_journal.json +++ b/src/db/migrations/meta/_journal.json @@ -64,6 +64,13 @@ "when": 1777303800000, "tag": "0008_reconcile_legacy_webhook_deliveries", "breakpoints": true + }, + { + "idx": 9, + "version": "7", + "when": 1781749800000, + "tag": "0009_operating_layer_evidence", + "breakpoints": true } ] } diff --git a/src/db/schema.ts b/src/db/schema.ts index e94c5e333..4461752d7 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -415,6 +415,47 @@ export const hostedSessionEntries = pgTable( }), ); +// ============================================================================ +// OPERATING LAYER EVIDENCE +// ============================================================================ + +export const operatingLayerEvidence = pgTable( + "operating_layer_evidence", + { + id: uuid("id").primaryKey().defaultRandom(), + orgId: uuid("org_id").references(() => organizations.id, { + onDelete: "cascade", + }), + sessionId: varchar("session_id", { length: 255 }), + capabilityId: varchar("capability_id", { length: 64 }).notNull(), + evidenceKind: varchar("evidence_kind", { length: 128 }).notNull(), + subject: varchar("subject", { length: 255 }).notNull(), + status: varchar("status", { length: 32 }).default("recorded").notNull(), + score: integer("score"), + blockers: jsonb("blockers").$type().default([]).notNull(), + warnings: jsonb("warnings").$type().default([]).notNull(), + evidence: jsonb("evidence").$type().notNull(), + metadata: jsonb("metadata").$type>().default({}), + recordedAt: timestamp("recorded_at", { withTimezone: true }) + .defaultNow() + .notNull(), + expiresAt: timestamp("expires_at", { withTimezone: true }), + }, + (table) => ({ + capabilityRecordedIdx: index( + "operating_layer_evidence_capability_recorded_idx", + ).on(table.capabilityId, table.recordedAt), + orgRecordedIdx: index("operating_layer_evidence_org_recorded_idx").on( + table.orgId, + table.recordedAt, + ), + sessionRecordedIdx: index( + "operating_layer_evidence_session_recorded_idx", + ).on(table.sessionId, table.recordedAt), + expiryIdx: index("operating_layer_evidence_expiry_idx").on(table.expiresAt), + }), +); + // ============================================================================ // AUDIT LOGS // ============================================================================ diff --git a/test/db/hosted-session-storage.integration.test.ts b/test/db/hosted-session-storage.integration.test.ts index 6f164ae59..1945aaf06 100644 --- a/test/db/hosted-session-storage.integration.test.ts +++ b/test/db/hosted-session-storage.integration.test.ts @@ -19,7 +19,7 @@ vi.mock("../../src/session/session-memory.js", () => ({ const BASE_DB_URL = process.env.MAESTRO_DATABASE_URL || process.env.DATABASE_URL; const describeDb = BASE_DB_URL ? describe : describe.skip; -const EXPECTED_MIGRATION_COUNT = 9; +const EXPECTED_MIGRATION_COUNT = 10; const originalEnv = { ...process.env }; diff --git a/test/db/migration-files.test.ts b/test/db/migration-files.test.ts index 986bc7558..0da19036e 100644 --- a/test/db/migration-files.test.ts +++ b/test/db/migration-files.test.ts @@ -37,6 +37,9 @@ describe("database migration packaging", () => { expect(journal.entries.map((entry) => entry.tag)).toContain( "0008_reconcile_legacy_webhook_deliveries", ); + expect(journal.entries.map((entry) => entry.tag)).toContain( + "0009_operating_layer_evidence", + ); for (const entry of journal.entries) { expect( @@ -152,4 +155,25 @@ describe("database migration packaging", () => { ); expect(migration).toContain("webhook_delivery_retry_idx"); }); + + it("adds the operating layer evidence migration for production readiness", () => { + const migration = readFileSync( + path.join(migrationsDir, "0009_operating_layer_evidence.sql"), + "utf8", + ); + + expect(migration).toContain( + 'CREATE TABLE IF NOT EXISTS "operating_layer_evidence"', + ); + expect(migration).toContain('"capability_id" varchar(64) NOT NULL'); + expect(migration).toContain('"evidence_kind" varchar(128) NOT NULL'); + expect(migration).toContain('"evidence" jsonb NOT NULL'); + expect(migration).toContain( + "operating_layer_evidence_org_id_organizations_id_fk", + ); + expect(migration).toContain( + "operating_layer_evidence_capability_recorded_idx", + ); + expect(migration).toContain("operating_layer_evidence_expiry_idx"); + }); }); diff --git a/test/deploy/helm-maestro.test.ts b/test/deploy/helm-maestro.test.ts index 94328980f..c09807816 100644 --- a/test/deploy/helm-maestro.test.ts +++ b/test/deploy/helm-maestro.test.ts @@ -45,6 +45,7 @@ describe("maestro Helm chart", () => { expect(result.stdout).toMatch(/mountPath:\s+\/tmp/); expect(result.stdout).toContain("name: HOME"); expect(result.stdout).toContain("name: XDG_CACHE_HOME"); + expect(result.stdout).not.toContain("MAESTRO_OPERATING_LAYER_ENABLED"); expect(result.stdout).toContain("startupProbe:"); expect(result.stdout).toContain("preStop:"); expect(result.stdout).toContain("/.well-known/evalops/remote-runner/drain"); @@ -82,6 +83,30 @@ describe("maestro Helm chart", () => { }, ); + helmIt( + "renders production operating layer evidence settings when enabled", + () => { + const result = renderChart([ + "--set", + "operatingLayer.enabled=true", + "--set", + "operatingLayer.evidenceRetentionDays=120", + ]); + + expect(result.status).toBe(0); + expect(result.stdout).toContain("name: MAESTRO_OPERATING_LAYER_ENABLED"); + expect(result.stdout).toContain('value: "1"'); + expect(result.stdout).toContain( + "name: MAESTRO_OPERATING_LAYER_EVIDENCE_STORE", + ); + expect(result.stdout).toContain('value: "database"'); + expect(result.stdout).toContain( + "name: MAESTRO_OPERATING_LAYER_EVIDENCE_RETENTION_DAYS", + ); + expect(result.stdout).toContain('value: "120"'); + }, + ); + helmIt("can render an HPA without a fixed deployment replica count", () => { const result = renderChart([ "--set", diff --git a/test/web-health.test.ts b/test/web-health.test.ts index d4e2c9550..34a48936c 100644 --- a/test/web-health.test.ts +++ b/test/web-health.test.ts @@ -15,6 +15,7 @@ vi.mock("../src/db/health.js", () => ({ "distributed_locks", "usage_metrics", "execution_traces", + "operating_layer_evidence", "workspace_config", "revenue_attribution", ], @@ -67,6 +68,7 @@ describe("Health Checks", () => { { name: "distributed_locks", exists: true }, { name: "usage_metrics", exists: true }, { name: "execution_traces", exists: true }, + { name: "operating_layer_evidence", exists: true }, { name: "workspace_config", exists: true }, { name: "revenue_attribution", exists: true }, ]); @@ -102,6 +104,7 @@ describe("Health Checks", () => { { name: "distributed_locks", exists: false }, { name: "usage_metrics", exists: true }, { name: "execution_traces", exists: true }, + { name: "operating_layer_evidence", exists: true }, { name: "workspace_config", exists: true }, { name: "revenue_attribution", exists: true }, ]); @@ -132,6 +135,7 @@ describe("Health Checks", () => { { name: "distributed_locks", exists: true }, { name: "usage_metrics", exists: true }, { name: "execution_traces", exists: true }, + { name: "operating_layer_evidence", exists: true }, { name: "workspace_config", exists: true }, { name: "revenue_attribution", exists: true }, ]);