From 232d606de09ba15629f4fae8ec7b85f15d67bd34 Mon Sep 17 00:00:00 2001 From: ChandanVarshney031 Date: Sun, 15 Nov 2026 22:05:39 +0530 Subject: [PATCH 1/2] ci(db): fail the build when the Prisma schema and migrations drift apart (#1691) --- .changeset/prisma-schema-drift-check-1691.md | 5 + .env.example | 2 + .github/workflows/ci.yml | 5 +- CONTRIBUTING.md | 6 + package.json | 1 + prisma/README.md | 5 + ...eb3-security-explanations.fallback.test.ts | 2 +- tests/prisma-drift-check.test.ts | 147 ++++++++++++++++++ 8 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 .changeset/prisma-schema-drift-check-1691.md create mode 100644 tests/prisma-drift-check.test.ts diff --git a/.changeset/prisma-schema-drift-check-1691.md b/.changeset/prisma-schema-drift-check-1691.md new file mode 100644 index 00000000..bf121d27 --- /dev/null +++ b/.changeset/prisma-schema-drift-check-1691.md @@ -0,0 +1,5 @@ +--- +"secureflow": minor +--- + +ci(db): fail build on schema-migration drift, add `npm run db:check-drift` script, and document in CONTRIBUTING.md (#1691) diff --git a/.env.example b/.env.example index 2cd2fcf5..b4c4b512 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ DATABASE_URL="postgresql://neondb_owner:@ep-lingering-thunder-ate1tkym.c-9.us-east-1.aws.neon.tech/neondb?sslmode=verify-full&channel_binding=require" DATABASE_POOL_URL="postgresql://neondb_owner:@ep-lingering-thunder-ate1tkym-pooler.c-9.us-east-1.aws.neon.tech/neondb?sslmode=verify-full&channel_binding=require" +# Optional: Shadow database for local migration drift checks (npm run db:check-drift) +# SHADOW_DATABASE_URL="postgresql://user:password@localhost:5432/secureflow_shadow" DB_POOL_MAX=10 GROQ_API_KEY=gsk_your_actual_api_key_here GITHUB_APP_ID="your_app_id" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be37a444..016bcf84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -235,10 +235,7 @@ jobs: PGPASSWORD=ci psql -h localhost -U ci -d secureflow \ -c 'CREATE DATABASE secureflow_shadow' set +e - npx prisma migrate diff \ - --from-migrations prisma/migrations \ - --to-schema prisma/schema.prisma \ - --exit-code + npm run db:check-drift code=$? if [ "$code" -eq 2 ]; then echo "::error::Schema drift detected. Run 'npx prisma migrate dev --name ' and commit the new migration." diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b24b3822..d7aa4d6d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -202,8 +202,13 @@ npm run db:migrate # Seed default data npm run db:seed + +# Check for schema drift (ensures schema.prisma and migrations are in sync) +npm run db:check-drift ``` +> 💡 **Tip**: Running `npm run db:check-drift` locally requires `SHADOW_DATABASE_URL` configured in your environment to diff against a shadow database (e.g. `postgresql://user:password@localhost:5432/secureflow_shadow`). In CI, this is provided automatically. + ### 5. Run Development Server ```bash @@ -234,6 +239,7 @@ Open [http://localhost:9002](http://localhost:9002) with your browser to see the - [ ] Run `npm run lint` and fix any issues - [ ] Run `npm run typecheck` and ensure no TypeScript errors - [ ] Run `npm run format:check` to verify code adheres to Prettier formatting +- [ ] Run `npm run db:check-drift` to verify Prisma schema and migrations are in sync - [ ] Run `npm test` and ensure all test suites pass - [ ] Run `npm run build` and confirm it builds - [ ] Test your changes thoroughly diff --git a/package.json b/package.json index de4411f2..29e1a390 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "db:migrate": "npx prisma migrate dev", "db:push": "npx prisma db push", "db:seed": "npx prisma db seed", + "db:check-drift": "prisma migrate diff --from-migrations prisma/migrations --to-schema prisma/schema.prisma --exit-code", "postinstall": "prisma generate", "worker": "tsx --env-file=.env scripts/start-worker.ts", "worker:render": "tsx scripts/start-worker.ts", diff --git a/prisma/README.md b/prisma/README.md index 5b53a057..572ffcee 100644 --- a/prisma/README.md +++ b/prisma/README.md @@ -215,6 +215,11 @@ Opens at `http://localhost:5555`. If your local database schema gets out of sync or encounters drift: +- **Check for schema vs. migration drift**: + ```bash + npm run db:check-drift + ``` + _Diffs migrations against `schema.prisma` using `prisma migrate diff --exit-code`. Exits with code 0 when in sync or code 2 when drift is detected. Requires `SHADOW_DATABASE_URL`._ - **Check migration status**: ```bash npx prisma migrate status diff --git a/src/ai/flows/web3-security-explanations.fallback.test.ts b/src/ai/flows/web3-security-explanations.fallback.test.ts index 46aede50..f44512b1 100644 --- a/src/ai/flows/web3-security-explanations.fallback.test.ts +++ b/src/ai/flows/web3-security-explanations.fallback.test.ts @@ -111,7 +111,7 @@ describe("web3SecurityExplanation - provider fallback (#1525)", () => { it("handles CircuitBreakerError by fast-failing with circuit breaker explanation", async () => { const { CircuitBreakerError } = await import("@/ai/resilience"); - mockGenerate.mockRejectedValue(new CircuitBreakerError("test-breaker", 60000)); + mockGenerate.mockRejectedValue(new CircuitBreakerError("test-breaker")); const result = await web3SecurityExplanation(input); diff --git a/tests/prisma-drift-check.test.ts b/tests/prisma-drift-check.test.ts new file mode 100644 index 00000000..fd44c0d3 --- /dev/null +++ b/tests/prisma-drift-check.test.ts @@ -0,0 +1,147 @@ +import { describe, it, expect } from "vitest"; +import fs from "node:fs"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; + +describe("Prisma Schema Drift Check (#1691)", () => { + const rootDir = process.cwd(); + const packageJsonPath = path.join(rootDir, "package.json"); + const ciWorkflowPath = path.join(rootDir, ".github", "workflows", "ci.yml"); + const contributingPath = path.join(rootDir, "CONTRIBUTING.md"); + const prismaReadmePath = path.join(rootDir, "prisma", "README.md"); + const schemaPath = path.join(rootDir, "prisma", "schema.prisma"); + + describe("npm scripts configuration", () => { + it("defines the db:check-drift script in package.json", () => { + expect(fs.existsSync(packageJsonPath)).toBe(true); + const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); + + expect(pkg.scripts).toHaveProperty("db:check-drift"); + const script = pkg.scripts["db:check-drift"]; + expect(script).toContain("prisma migrate diff"); + expect(script).toContain("--from-migrations prisma/migrations"); + expect(script).toContain("--to-schema prisma/schema.prisma"); + expect(script).toContain("--exit-code"); + }); + }); + + describe("CI workflow configuration (.github/workflows/ci.yml)", () => { + it("configures the prisma-drift-check job with PostgreSQL 16 service and shadow db", () => { + expect(fs.existsSync(ciWorkflowPath)).toBe(true); + const ciContent = fs.readFileSync(ciWorkflowPath, "utf8"); + + expect(ciContent).toContain("prisma-drift-check:"); + expect(ciContent).toContain("name: Prisma Schema Drift Check"); + expect(ciContent).toContain("image: postgres:16"); + expect(ciContent).toContain( + "SHADOW_DATABASE_URL: postgresql://ci:ci@localhost:5432/secureflow_shadow", + ); + expect(ciContent).toContain("CREATE DATABASE secureflow_shadow"); + expect(ciContent).toContain("npm run db:check-drift"); + expect(ciContent).toContain( + "Schema drift detected. Run 'npx prisma migrate dev --name ' and commit the new migration.", + ); + }); + + it("ensures release job depends on prisma-drift-check", () => { + const ciContent = fs.readFileSync(ciWorkflowPath, "utf8"); + const releaseJobIndex = ciContent.indexOf("release:"); + expect(releaseJobIndex).toBeGreaterThan(-1); + + const releaseSection = ciContent.slice(releaseJobIndex); + expect(releaseSection).toContain("prisma-drift-check"); + }); + }); + + describe("Documentation", () => { + it("documents db:check-drift in CONTRIBUTING.md setup and PR checklist", () => { + expect(fs.existsSync(contributingPath)).toBe(true); + const contributingContent = fs.readFileSync(contributingPath, "utf8"); + + expect(contributingContent).toContain("npm run db:check-drift"); + expect(contributingContent).toContain("SHADOW_DATABASE_URL"); + expect(contributingContent).toMatch(/- \[[ x]\] Run `npm run db:check-drift`/); + }); + + it("documents db:check-drift in prisma/README.md", () => { + expect(fs.existsSync(prismaReadmePath)).toBe(true); + const prismaReadmeContent = fs.readFileSync(prismaReadmePath, "utf8"); + + expect(prismaReadmeContent).toContain("npm run db:check-drift"); + }); + }); + + describe("Prisma migrate diff exit code contract", () => { + it("exits with 0 when comparing schema to itself (no drift)", () => { + expect(fs.existsSync(schemaPath)).toBe(true); + const isWindows = process.platform === "win32"; + const cmd = isWindows ? "cmd.exe" : "npx"; + const args = isWindows + ? [ + "/c", + "npx", + "prisma", + "migrate", + "diff", + "--from-schema", + "prisma/schema.prisma", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ] + : [ + "prisma", + "migrate", + "diff", + "--from-schema", + "prisma/schema.prisma", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ]; + + const res = spawnSync(cmd, args, { + cwd: rootDir, + encoding: "utf8", + }); + + expect(res.status).toBe(0); + expect(res.stdout).toContain("No difference detected"); + }); + + it("exits with 2 when diff detects unmigrated changes", () => { + expect(fs.existsSync(schemaPath)).toBe(true); + const isWindows = process.platform === "win32"; + const cmd = isWindows ? "cmd.exe" : "npx"; + const args = isWindows + ? [ + "/c", + "npx", + "prisma", + "migrate", + "diff", + "--from-empty", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ] + : [ + "prisma", + "migrate", + "diff", + "--from-empty", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ]; + + const res = spawnSync(cmd, args, { + cwd: rootDir, + encoding: "utf8", + }); + + expect(res.status).toBe(2); + expect(res.stdout).toContain("[+] Added"); + }); + }); +}); From 90dd8bc5c200828068cfa74f6005a8315216ec1c Mon Sep 17 00:00:00 2001 From: ChandanVarshney031 Date: Sun, 15 Nov 2026 22:36:50 +0530 Subject: [PATCH 2/2] fix(ci): ensure armoriq-verify exits cleanly and reset model circuit breakers in AI test suites --- scripts/armoriq-verify.ts | 24 +++++++++++++------ ...generate-remediation-patch.backoff.test.ts | 8 ++++++- .../generate-remediation-patch.vram.test.ts | 8 ++++++- .../web3-security-explanations.vram.test.ts | 8 ++++++- vitest.config.ts | 8 ++++++- 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/scripts/armoriq-verify.ts b/scripts/armoriq-verify.ts index 26a7eb7b..906ee347 100644 --- a/scripts/armoriq-verify.ts +++ b/scripts/armoriq-verify.ts @@ -180,12 +180,18 @@ export async function runArmorIQCheck(options: CliCheckOptions = {}) { if (options.inspectDlq) { console.log("🔍 Inspecting ArmorIQ Dead-Letter Queue Payloads & Telemetry (#1765)..."); const dlqStatus = await inspectRemediationDlqPayloads(); - console.log(`📊 DLQ Total: ${dlqStatus.total} | Queued: ${dlqStatus.queued.length} | Quarantined: ${dlqStatus.quarantined.length} | Failed: ${dlqStatus.failed.length}`); - console.log(`📈 Telemetry Metrics: Total Queued: ${dlqStatus.telemetry.totalQueued}, Replayed: ${dlqStatus.telemetry.totalReplayed}, Retries: ${dlqStatus.telemetry.totalRetries}, Quarantined: ${dlqStatus.telemetry.totalQuarantined}`); + console.log( + `📊 DLQ Total: ${dlqStatus.total} | Queued: ${dlqStatus.queued.length} | Quarantined: ${dlqStatus.quarantined.length} | Failed: ${dlqStatus.failed.length}`, + ); + console.log( + `📈 Telemetry Metrics: Total Queued: ${dlqStatus.telemetry.totalQueued}, Replayed: ${dlqStatus.telemetry.totalReplayed}, Retries: ${dlqStatus.telemetry.totalRetries}, Quarantined: ${dlqStatus.telemetry.totalQuarantined}`, + ); if (dlqStatus.quarantined.length > 0) { console.log("🚨 Quarantined Events:"); for (const q of dlqStatus.quarantined) { - console.log(` - [${q.id}] ${q.quarantineReason || "Quarantined"} (retries: ${q.retryCount})`); + console.log( + ` - [${q.id}] ${q.quarantineReason || "Quarantined"} (retries: ${q.retryCount})`, + ); } } } @@ -264,8 +270,12 @@ export async function runArmorIQCheck(options: CliCheckOptions = {}) { const isMain = process.argv[1] && /armoriq-verify(\.ts|\.js)?$/.test(process.argv[1]); if (isMain) { const options = parseCliArgs(process.argv.slice(2)); - runArmorIQCheck(options).catch((err) => { - console.error("Fatal error during ArmorIQ verification:", err); - process.exit(1); - }); + runArmorIQCheck(options) + .then(() => { + process.exit(0); + }) + .catch((err) => { + console.error("Fatal error during ArmorIQ verification:", err); + process.exit(1); + }); } diff --git a/src/ai/flows/generate-remediation-patch.backoff.test.ts b/src/ai/flows/generate-remediation-patch.backoff.test.ts index 63351e6b..92220062 100644 --- a/src/ai/flows/generate-remediation-patch.backoff.test.ts +++ b/src/ai/flows/generate-remediation-patch.backoff.test.ts @@ -1,4 +1,5 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { resetModelCircuitBreakers } from "@/ai/resilience"; const mockGenerate = vi.fn(); @@ -30,11 +31,16 @@ const rateLimit = () => Object.assign(new Error("429 Too Many Requests"), { stat describe("generateRemediationPatch - backoff and retry (#1513)", () => { beforeEach(() => { + resetModelCircuitBreakers(); mockGenerate.mockReset(); vi.spyOn(console, "warn").mockImplementation(() => {}); vi.spyOn(console, "error").mockImplementation(() => {}); }); + afterEach(() => { + resetModelCircuitBreakers(); + }); + it("retries a rate limit and returns the patch once the provider recovers", async () => { mockGenerate .mockRejectedValueOnce(rateLimit()) diff --git a/src/ai/flows/generate-remediation-patch.vram.test.ts b/src/ai/flows/generate-remediation-patch.vram.test.ts index 18eef840..2732c35a 100644 --- a/src/ai/flows/generate-remediation-patch.vram.test.ts +++ b/src/ai/flows/generate-remediation-patch.vram.test.ts @@ -1,4 +1,5 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { resetModelCircuitBreakers } from "@/ai/resilience"; const mockGenerate = vi.fn(); const PRIMARY = "openai/llama3"; @@ -32,11 +33,16 @@ const goodOutput = { describe("generateRemediationPatch - insufficient VRAM (#1525)", () => { beforeEach(() => { + resetModelCircuitBreakers(); mockGenerate.mockReset(); vi.spyOn(console, "warn").mockImplementation(() => {}); vi.spyOn(console, "error").mockImplementation(() => {}); }); + afterEach(() => { + resetModelCircuitBreakers(); + }); + it("does not crash when the local model does not fit in memory", async () => { mockGenerate.mockRejectedValue(OOM); diff --git a/src/ai/flows/web3-security-explanations.vram.test.ts b/src/ai/flows/web3-security-explanations.vram.test.ts index 5aa6a226..3bc0c434 100644 --- a/src/ai/flows/web3-security-explanations.vram.test.ts +++ b/src/ai/flows/web3-security-explanations.vram.test.ts @@ -1,4 +1,5 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { resetModelCircuitBreakers } from "@/ai/resilience"; const mockGenerate = vi.fn(); const PRIMARY = "openai/llama3"; @@ -36,12 +37,17 @@ const goodReply = { describe("web3SecurityExplanation - insufficient VRAM (#1525)", () => { beforeEach(() => { + resetModelCircuitBreakers(); mockGenerate.mockReset(); mockSetCached.mockReset(); vi.spyOn(console, "warn").mockImplementation(() => {}); vi.spyOn(console, "error").mockImplementation(() => {}); }); + afterEach(() => { + resetModelCircuitBreakers(); + }); + it("does not crash when the local model does not fit in memory", async () => { mockGenerate.mockRejectedValue(OOM); diff --git a/vitest.config.ts b/vitest.config.ts index 35c96f21..88cadcc6 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -19,7 +19,13 @@ export default defineConfig({ "tests/**/*.test.ts", "scripts/**/*.test.ts", ], - exclude: ["**/node_modules/**", "**/dist/**", "**/*.redteam.test.ts", "**/*.redteam.test.tsx"], + exclude: [ + "**/node_modules/**", + "**/dist/**", + ...(process.env.npm_lifecycle_event === "test:redteam" || process.env.REDTEAM === "true" + ? [] + : ["**/*.redteam.test.ts", "**/*.redteam.test.tsx"]), + ], setupFiles: ["./vitest.setup.ts"], testTimeout: 20000, coverage: {