fix(db): первое MCP-восстановление под карантином и JIT в захватах каталога

Отметкой попытки при отсутствующем журнале считаются только .used и
квитанции applied/unknown. Квитанции blocked и running без .used не доходят
до записи журнала, поэтому больше не блокируют первое MCP-восстановление
под карантином и старт backend после отклонённой попытки.

Захваты каталога выключают JIT: на PostgreSQL 18 компиляция запроса
unsupported занимала ~5,4 с при лимите 5 с (выполнение 0,2 с). Runtime —
SET LOCAL jit = off в снимке каталога и контролируемой схеме, оператор —
-c jit=off в STARTUP_OPTIONS.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
DenozordecandClaude Opus 5.5 committed 2026-11-21 00:51:14 +07:00
1 parent 2c5890f9d6
commit 11537c9944
9 files changed
+49 -6

No files matched your search

+1 -1
View File
@@ -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",
@@ -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}))
})
@@ -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))
@@ -102,6 +102,7 @@ async function snapshot(client: PoolClient): Promise<CapturedMigrationSchema> {
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 }
@@ -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]
@@ -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")
+2 -1
View File
@@ -28,7 +28,8 @@ export async function withRuntimeCatalogSnapshot<T>(
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'",
@@ -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')
@@ -119,7 +119,9 @@ async function vaultFacts(stateDirectory:string):Promise<VaultFacts>{
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