diff --git a/backend/package.json b/backend/package.json index 8d51dd52..0d91f54e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -67,7 +67,7 @@ "test:ospf-optimizer": "tsx src/services/ospf-cost-optimization.test.ts && tsx src/modules/mcp/ospf-legacy-source.test.ts && tsx src/services/ospf-legacy-optimization.test.ts && tsx src/services/ospf-template-flags.test.ts && tsx src/services/ospf-route-proof.test.ts && tsx src/services/ospf-link-catalog.test.ts && tsx src/services/ospf-identity-catalog.test.ts && tsx src/services/ospf-template-cost-write.test.ts && tsx src/services/ospf-template-cost-http.test.ts && tsx src/services/ospf-optimizer-identity.test.ts && tsx src/services/ospf-optimizer-metrics.test.ts && tsx src/services/ospf-optimizer-plan.test.ts && tsx src/services/ospf-effective-disabled.test.ts && tsx src/routes/ospf-optimizer.test.ts", "test:containers": "tsx src/services/containers-live.test.ts && tsx src/services/containers-state.test.ts && tsx src/services/container-removal.test.ts && tsx src/routes/containers-state.test.ts", "test:certificates": "tsx src/services/certificate-installation.test.ts && tsx src/services/certificate-renew-collector.test.ts && tsx src/services/certificate-issue-execution.test.ts && tsx src/services/certificate-issue-request.test.ts && tsx src/routes/certificates-issue.test.ts && tsx src/services/certificate-issue-dto.test.ts && tsx src/services/certificate-parse.test.ts && tsx src/services/certificates-state.test.ts && tsx src/routes/certificates-state.test.ts && tsx src/services/acme-settings-test.test.ts && tsx src/services/acme-settings-update-model.test.ts && tsx src/modules/mcp/acme-settings-actions.test.ts", - "test:system-database": "tsx src/services/bounded-database-dump.test.ts && tsx src/services/database-dump-connection.test.ts && tsx src/services/system-database-partition-restore.test.ts && tsx src/services/system-database-cache.test.ts && tsx src/services/database-background-work.test.ts && tsx src/services/database-request-gate.test.ts && tsx --test src/services/detached-database-admission.test.ts && tsx src/services/traffic-flow-worker-shutdown.test.ts && tsx src/services/system-database-restore-execution.test.ts && tsx src/services/system-database-operation.test.ts && tsx src/services/system-database-schema-readiness.test.ts && tsx --test src/routes/mcp-oauth-database-admission.test.ts && tsx src/routes/system-database.test.ts && tsx src/plugins/auth.system-database.test.ts && tsx src/services/database-control-archive-boundary.test.ts && tsx src/db/database-schema-control-model.test.ts && tsx src/db/database-schema-control.test.ts && tsx src/db/controlled-migrations.test.ts && tsx src/db/controlled-schema-verification.test.ts && tsx --test src/db/migration-data-inventory.test.ts && tsx --test src/services/database-maintenance-startup.test.ts && tsx --test src/services/database-restore-audit.test.ts && tsx --test src/services/database-maintenance-resolution.test.ts && tsx --test scripts/database-maintenance-resolution.test.mts", + "test:system-database": "tsx src/services/bounded-database-dump.test.ts && tsx src/services/database-dump-connection.test.ts && tsx src/services/system-database-partition-restore.test.ts && tsx src/services/system-database-cache.test.ts && tsx src/services/database-background-work.test.ts && tsx src/services/database-request-gate.test.ts && tsx --test src/services/detached-database-admission.test.ts && tsx src/services/traffic-flow-worker-shutdown.test.ts && tsx src/services/system-database-restore-execution.test.ts && tsx src/services/system-database-operation.test.ts && tsx src/services/system-database-schema-readiness.test.ts && tsx --test src/routes/mcp-oauth-database-admission.test.ts && tsx src/routes/system-database.test.ts && tsx src/plugins/auth.system-database.test.ts && tsx src/services/database-control-archive-boundary.test.ts && tsx src/db/database-schema-control-model.test.ts && tsx src/db/database-schema-control.test.ts && tsx src/db/controlled-migrations.test.ts && tsx src/db/controlled-schema-verification.test.ts && tsx --test src/db/migration-data-inventory.test.ts && tsx --test src/services/database-maintenance-startup.test.ts && tsx --test src/services/database-restore-audit.test.ts && tsx --test src/services/database-maintenance-resolution.test.ts && tsx --test scripts/database-maintenance-resolution.test.mts && tsx src/db/database-runtime-schema.test.ts", "test:all": "npm run test:logs && npm run test:probes && npm run test:exec && npm run test:alert-engine && npm run test:alerts-telegram && npm run test:auth && npm run test:wireguard && npm run test:ipsec && npm run test:traffic-rate && npm run test:traffic-flow && npm run test:users && npm run test:pg && npm run test:sidebar-counts && npm run test:backups && npm run test:scheduler && npm run test:live-maps && npm run test:ospf-optimizer && npm run test:recursive-routes && npm run test:config-sync && npm run test:ros-file && npm run test:containers && npm run test:certificates && npm run test:netflow-settings && npm run test:netflow-purge && npm run test:evolution && npm run test:system-database && npm run test:server-privacy && npm run test:mcp:module-completion && npm run test:mcp:loopback-agent && npm run test:server-probe-management && npm run test:server-poll-management && npm run test:mcp:routing-authority && npm run test:mcp:native-readers && npm run test:mcp:chr-acquisition && npm run test:mcp:shared-context && npm run test:mcp:telemetry-configuration", "test:server-privacy": "tsx ../packages/contracts/src/servers.test.ts && tsx src/routes/server-secrets-sql.test.ts && tsx --test src/services/server-connection-test.test.ts", "test:mcp:flow-rebuild:pg": "node scripts/test-mcp-postgres.mjs --flow-rebuild-mcp", diff --git a/backend/scripts/database-writer-profile-operator.test.mts b/backend/scripts/database-writer-profile-operator.test.mts index e24e3b5a..df40e2c4 100644 --- a/backend/scripts/database-writer-profile-operator.test.mts +++ b/backend/scripts/database-writer-profile-operator.test.mts @@ -34,7 +34,7 @@ test('actual pg ConnectionParameters cannot inherit malicious PGOPTIONS and nati try{ const ca=Buffer.from(rootCertificates[0]),request={roles:{publisher:{name:'installation_policy_signer'}},source:{databaseName:'owned'},tls:{serverName:'db.example.invalid',caSha256:hash(ca),spkiSha256:'a'.repeat(64)}} const config=poolOptions({host:'db.example.invalid',port:5432,user:'installation_policy_signer',password:'test-only',database:'owned'},request,ca) - const actual=new ConnectionParameters(config);assert.equal(actual.options,STARTUP_OPTIONS);assert(!actual.options.includes('attacker'));assert.equal(actual.user,'installation_policy_signer');assert.equal(actual.ssl.rejectUnauthorized,true) + const actual=new ConnectionParameters(config);assert.equal(actual.options,STARTUP_OPTIONS);assert(!actual.options.includes('attacker'));assert.match(actual.options,/(^| )-c jit=off( |$)/);assert.equal(actual.user,'installation_policy_signer');assert.equal(actual.ssl.rejectUnauthorized,true) }finally{if(original===undefined)delete process.env.PGOPTIONS;else process.env.PGOPTIONS=original} assert.doesNotThrow(()=>assertJsDriverEnvironment({}));for(const value of ['1','0','',undefined])assert.throws(()=>assertJsDriverEnvironment({NODE_PG_FORCE_NATIVE:value})) }) diff --git a/backend/scripts/writer-profile/operator-contract.mjs b/backend/scripts/writer-profile/operator-contract.mjs index 4f0fabc1..295ca0c8 100644 --- a/backend/scripts/writer-profile/operator-contract.mjs +++ b/backend/scripts/writer-profile/operator-contract.mjs @@ -3,7 +3,8 @@ import {X509Certificate} from 'node:crypto' import {checkServerIdentity,TLSSocket} from 'node:tls' import {sha} from './operator-files.mjs' export const roleKeys=Object.freeze(['runtime','operator','capture','publisher','control']) -export const STARTUP_OPTIONS='-c statement_timeout=10000 -c lock_timeout=5000 -c client_encoding=UTF8' +// jit=off: JIT compilation of the large catalogue queries alone can exceed the fixed capture limits (5 s plans on PostgreSQL 18). +export const STARTUP_OPTIONS='-c statement_timeout=10000 -c lock_timeout=5000 -c client_encoding=UTF8 -c jit=off' export function assertJsDriverEnvironment(environment){assert(!Object.hasOwn(environment,'NODE_PG_FORCE_NATIVE'),'Native pg driver selector refused')} const hex=v=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v) const exact=(v,keys)=>v&&typeof v==='object'&&!Array.isArray(v)&&Object.keys(v).length===keys.length&&keys.every(k=>Object.hasOwn(v,k)) diff --git a/backend/src/db/controlled-schema-reference-core.ts b/backend/src/db/controlled-schema-reference-core.ts index d0efdb10..836a2be7 100644 --- a/backend/src/db/controlled-schema-reference-core.ts +++ b/backend/src/db/controlled-schema-reference-core.ts @@ -102,6 +102,7 @@ async function snapshot(client: PoolClient): Promise { await client.query("SET LOCAL TimeZone='UTC'") await client.query("SET LOCAL DateStyle='ISO, YMD'") await client.query("SET LOCAL statement_timeout='5s'") + await client.query("SET LOCAL jit = off") // JIT of catalogue queries alone can exceed the 5 s limit const result = await captureMigrationSchema(client, { scope }) await client.query("ROLLBACK"); return result } catch (error) { await client.query("ROLLBACK").catch(() => {}); throw error } diff --git a/backend/src/db/controlled-schema-verification.ts b/backend/src/db/controlled-schema-verification.ts index 52250ce0..ebf7be74 100644 --- a/backend/src/db/controlled-schema-verification.ts +++ b/backend/src/db/controlled-schema-verification.ts @@ -75,7 +75,7 @@ export async function verifyControlledSchemaCurrentState() { await app.query("SELECT pg_catalog.pg_advisory_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY]) const state = (await control.read()).state await app.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") - for (const sql of ["SET LOCAL search_path = pg_catalog", "SET LOCAL TimeZone = 'UTC'", "SET LOCAL DateStyle = 'ISO, YMD'", "SET LOCAL standard_conforming_strings = on", "SET LOCAL statement_timeout = '5s'"]) await app.query(sql) + for (const sql of ["SET LOCAL search_path = pg_catalog", "SET LOCAL TimeZone = 'UTC'", "SET LOCAL DateStyle = 'ISO, YMD'", "SET LOCAL standard_conforming_strings = on", "SET LOCAL statement_timeout = '5s'", "SET LOCAL jit = off"]) await app.query(sql) const contextRows = (await app.query(CONTROLLED_SCHEMA_VERIFICATION_SQL.context)).rows if (contextRows.length !== 1 || Object.keys(contextRows[0]).sort().join(" ") !== "backend_pid transaction_started" || typeof contextRows[0].backend_pid !== "string" || !/^\d+$/.test(contextRows[0].backend_pid) || typeof contextRows[0].transaction_started !== "string" || !Number.isFinite(Date.parse(contextRows[0].transaction_started))) refuse("Read-only evidence transaction context unavailable") const context = contextRows[0] diff --git a/backend/src/db/database-runtime-schema.test.ts b/backend/src/db/database-runtime-schema.test.ts new file mode 100644 index 00000000..64f9ead2 --- /dev/null +++ b/backend/src/db/database-runtime-schema.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict" +import { withRuntimeCatalogSnapshot } from "./database-runtime-schema.js" + +// The catalogue snapshot disables JIT inside its own read-only transaction: on PostgreSQL 18 the compilation of the +// large catalogue queries alone exceeded the fixed 5 s limit while execution took milliseconds. +const seen: string[] = [] +const client = { + query: async (sql: string) => { + seen.push(sql) + if (sql.startsWith("BEGIN")) return { command: "BEGIN", rows: [] } + if (sql === "ROLLBACK") return { command: "ROLLBACK", rows: [] } + if (sql.includes("to_regnamespace")) return { rows: [{ present: true }] } + if (sql.includes("pg_advisory_unlock")) return { rows: [{ released: true }] } + return { rows: [] } + }, +} +let worked = false +await withRuntimeCatalogSnapshot(client as never, () => assert.fail("discarded"), async () => { + const begin = seen.findIndex(sql => sql.startsWith("BEGIN")), jit = seen.indexOf("SET LOCAL jit = off") + assert(begin >= 0 && jit > begin, "jit is disabled inside the snapshot transaction") + worked = true +}) +assert(worked) +assert.equal(seen.at(-2), "SELECT pg_catalog.pg_advisory_unlock($1::bigint) AS released") +console.log("runtime catalogue snapshot disables JIT inside its transaction") diff --git a/backend/src/db/database-runtime-schema.ts b/backend/src/db/database-runtime-schema.ts index 1c7e0c57..38c6918a 100644 --- a/backend/src/db/database-runtime-schema.ts +++ b/backend/src/db/database-runtime-schema.ts @@ -28,7 +28,8 @@ export async function withRuntimeCatalogSnapshot( inSnapshot = true for (const setting of [ "SET LOCAL search_path = pg_catalog", "SET LOCAL TimeZone = 'UTC'", "SET LOCAL DateStyle = 'ISO, YMD'", - "SET LOCAL standard_conforming_strings = on", "SET LOCAL statement_timeout = '5s'", + // JIT compilation of the catalogue queries alone can exceed the fixed 5 s limit; execution itself takes milliseconds. + "SET LOCAL standard_conforming_strings = on", "SET LOCAL statement_timeout = '5s'", "SET LOCAL jit = off", "SET LOCAL quote_all_identifiers = off", "SET LOCAL IntervalStyle = 'postgres'", "SET LOCAL extra_float_digits = 3", "SET LOCAL bytea_output = 'hex'", "SET LOCAL row_security = off", "SET LOCAL client_encoding = 'UTF8'", diff --git a/backend/src/services/database-maintenance-startup.test.ts b/backend/src/services/database-maintenance-startup.test.ts index f5f1bbe5..bfb9467e 100644 --- a/backend/src/services/database-maintenance-startup.test.ts +++ b/backend/src/services/database-maintenance-startup.test.ts @@ -144,6 +144,19 @@ test('missing journal with attempt markers is not a new clean install',()=>own(a await outer(root,'applied',true) const value=await inspectDatabaseMaintenanceStartup(root);assert.equal(value.classification,'inspection-required');assert.equal(value.reason,'JOURNAL_ABSENT_WITH_ATTEMPT_MARKERS') })) +test('unclaimed blocked or running receipts never strand a missing journal; claims and attempted receipts still refuse',()=>own(async root=>{ + // A refused first MCP restore and the in-flight first restore under quarantine both precede any claim or journal write. + for(const state of ['blocked','running']){ + await outer(root,state,false) + const value=await inspectDatabaseMaintenanceStartup(root);assert.equal(value.classification,'fresh-legacy');assert.equal(value.reason,'NONE') + } + const claimed=await outer(root,'running',true) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).reason,'JOURNAL_ABSENT_WITH_ATTEMPT_MARKERS') + await rm(path.join(claimed.vault,claimed.archiveRef+'.used')) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'fresh-legacy') + await outer(root,'unknown',false) + assert.equal((await inspectDatabaseMaintenanceStartup(root)).reason,'JOURNAL_ABSENT_WITH_ATTEMPT_MARKERS') +})) test('orphan used claim, unknown publication temporary file and blocked-with-used refuse',()=>own(async root=>{ await completed(root);const f=await outer(root,'blocked',true) assert.equal((await inspectDatabaseMaintenanceStartup(root)).classification,'inspection-required') diff --git a/backend/src/services/database-maintenance-startup.ts b/backend/src/services/database-maintenance-startup.ts index b57afe3d..8aa3ca80 100644 --- a/backend/src/services/database-maintenance-startup.ts +++ b/backend/src/services/database-maintenance-startup.ts @@ -119,7 +119,9 @@ async function vaultFacts(stateDirectory:string):Promise{ for(const [archiveRef,claim]of used){const receipt=receipts.get(claim.operationId);if(!receipt||receipt.archiveRef!==archiveRef||receipt.state!=='applied')unresolvedFiles.add(archiveRef+'.used')} if(await directory(root)!==before||(await readdir(root)).sort().join('\0')!==[...names].sort().join('\0'))refuse() for(const [name,signature]of signatures)if(identity(await lstat(path.join(root,name),{bigint:true}))!==signature)refuse() - return {markers:used.size+receipts.size,applied,blocked,unresolved:unresolvedFiles.size>0,auditSources:Object.freeze(auditSources),evidence:Object.freeze(evidence),unresolvedFiles:Object.freeze([...unresolvedFiles].sort())} + // Attempt markers: claims and attempted receipts. A blocked or unclaimed running receipt never reached a journal write (its claim comes first). + const attempted=[...receipts.values()].filter(receipt=>receipt.state==='applied'||receipt.state==='unknown').length + return {markers:used.size+attempted,applied,blocked,unresolved:unresolvedFiles.size>0,auditSources:Object.freeze(auditSources),evidence:Object.freeze(evidence),unresolvedFiles:Object.freeze([...unresolvedFiles].sort())} } /** Strict inspection facts; no acceptance decision and no SQL. Operator commands * use the same grammar, not a looser recovery parser. Existing single managed