import assert from "node:assert/strict" import test from "node:test" import { readFile } from "node:fs/promises" import ts from "typescript" import { fingerprint, McpError } from "./security.js" /** Execute the actual native guard body. Only authority/SQL adapters are inert; * these cases test identity comparison semantics, not signed inverse integration. */ async function fixture() { const enrolled: Record = { nativeId: "*1", candidateId: "a", targetProof: "target-a", dependencyProof: "dep", sourceProofs: [{ serverId: 1, proof: "source" }] } const authority = { packet: { profileId: "profile", profileVersion: 1, connectionId: "connection" } } const input: Record = { profile: { autoRecovery: false }, persistentProfileAuthority: authority, nativeId: "*1", originalCandidateId: "a", originalTargetProof: "target-a", dependencyProof: "dep", contextSources: enrolled.sourceProofs } const prepared: Record = { ...input, desiredCandidateId: "b" } const packet: Record = { nativeId: "*1", originalCandidateId: "a", desiredCandidateId: "b", originalTargetProof: "target-a", desiredTargetProof: "target-b", dependencyProof: "dep", contextSources: enrolled.sourceProofs } const source = { row: { id: "source-op", source_plan_id: "source-plan" }, input, configuration: { checkpointProof: "configuration-proof", packet }, sourceProof: "source-proof" } const row = { id: "profile", version: 1, connection_id: "connection", grant_expires_at: new Date(Date.now() + 600000), config: {}, state: { baseline: { proof: "inert-authenticated-baseline" } } } const metadata = { operationName: "routing.community.switch", operationVersion: 7, serviceName: "routing.community.scoped", phase: "forward", stepIndex: 0, ownerId: "owner", serviceInput: input, principal: {} } let nativeWrites = 0 const providers: Record = { "./routing-reservation-store.js": { assertPersistentRoutingForwardTransportCharge: async () => {} }, "./routing-main-transition.js": { loadRoutingMainTransitionPreparation: async () => prepared }, "./routing-community-scoped-request.js": { assertScopedCommunityRequest: async () => {} }, "@mmapp/contracts/mcp-routing": { routeAiProfileSchema: { parse: (value: unknown) => value } }, "../../db/index.js": { pool: { query: async () => ({ rows: [row] }) } }, "../../services/config-mutation-policy.js": { captureVerifiedMcpServiceExecution: async () => metadata, inspectVerifiedMcpServiceExecution: async () => metadata }, "./store.js": { effectiveMcpConnection: async () => ({}) }, "./routing-profile-authority.js": { routingProfileAuthorityBindingSchema: { parse: (value: unknown) => { assert.ok(value); return value } }, assertPersistentScopedProfileAuthority: async () => {} }, "./routing-community-scoped-recovery-source.js": { loadScopedRecoverySource: async () => source }, "./routing-controller-baseline.js": { routingControllerBaselinePrincipal: async () => ({}), authenticateRoutingControllerBaseline: async () => ({ value: { packet: enrolled } }) }, "./security.js": { fingerprint, McpError }, } const exports: { assertActiveRoutingProfileWriter?: (serverId: number) => Promise } = {} const body = await readFile(new URL("./routing-controller-active-writer.ts", import.meta.url), "utf8") const compiled = ts.transpileModule(body, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText new Function("exports", "require", compiled)(exports, (name: string) => { assert.ok(providers[name], name); return providers[name] }) assert.ok(exports.assertActiveRoutingProfileWriter) const guard = exports.assertActiveRoutingProfileWriter return { enrolled, input, prepared, source, metadata, run: async () => { await guard(1); nativeWrites++ }, writes: () => nativeWrites, recovery: () => { metadata.operationName = "operations.recover"; metadata.serviceName = "routing.community.scoped.recover" metadata.serviceInput = { sourceOperationId: "source-op", sourceProof: source.sourceProof, configurationProof: source.configuration.checkpointProof, sourceReceiptId: source.input.receiptId } }, } } test("scoped original enrolled identity is admitted", async () => { const value = await fixture(); await value.run(); assert.equal(value.writes(), 1) }) const drifts: Array<[string, string, unknown]> = [ ["native", "nativeId", "*other"], ["candidate", "originalCandidateId", "c"], ["target", "originalTargetProof", "other-target"], ["dependency", "dependencyProof", "other-dep"], ["sources", "contextSources", [{ serverId: 1, proof: "other-source" }]], ] for (const [name, key, bad] of drifts) test(`scoped ${name} drift is refused before native`, async () => { const value = await fixture(); value.input[key] = bad; await assert.rejects(value.run); assert.equal(value.writes(), 0) }) test("main fixed loaded original identity is admitted", async () => { const value = await fixture(); value.metadata.serviceName = "routing.community"; await value.run() }) for (const [name, key, bad] of drifts) test(`main decrypted ${name} drift is refused before native`, async () => { const value = await fixture(); value.metadata.serviceName = "routing.community"; value.prepared[key] = bad; await assert.rejects(value.run); assert.equal(value.writes(), 0) }) test("inverse with enrolled A awaiting its own B transition is admitted", async () => { const value = await fixture(); value.recovery(); await value.run() }) const sourceTransition = { operationId: "source-op", planId: "source-plan", checkpointProof: "configuration-proof" } test("inverse from this exact source advanced B baseline is admitted", async () => { const value = await fixture(); value.recovery(); Object.assign(value.enrolled, { candidateId: "b", targetProof: "target-b", transition: sourceTransition }); await value.run() }) test("inverse from this source can bind a separately authenticated B readback seal", async () => { const value = await fixture() value.recovery() Object.assign(value.enrolled, { candidateId: "b", targetProof: "observed-b", transition: { ...sourceTransition, sourceDesiredTargetProof: "target-b" } }) await value.run() assert.equal(value.writes(), 1) }) for (const [key, wrong] of Object.entries({ operationId: "other-source", planId: "other-plan", checkpointProof: "other-checkpoint", sourceDesiredTargetProof: "other-desired" })) { test(`inverse from observed B refuses mismatched ${key}`, async () => { const value = await fixture() value.recovery() Object.assign(value.enrolled, { candidateId: "b", targetProof: "observed-b", transition: { ...sourceTransition, sourceDesiredTargetProof: "target-b", [key]: wrong } }) await assert.rejects(value.run) assert.equal(value.writes(), 0) }) } test("new forward source must match the enrolled observed seal", async () => { const value = await fixture() Object.assign(value.enrolled, { candidateId: "b", targetProof: "observed-b", transition: { ...sourceTransition, sourceDesiredTargetProof: "target-b" } }) Object.assign(value.input, { originalCandidateId: "b", originalTargetProof: "target-b" }) await assert.rejects(value.run) assert.equal(value.writes(), 0) value.input.originalTargetProof = "observed-b" await value.run() }) test("inverse with B but no authenticated transition is refused", async () => { const value = await fixture(); value.recovery(); Object.assign(value.enrolled, { candidateId: "b", targetProof: "target-b" }); await assert.rejects(value.run) }) test("inverse with another configuration checkpoint is refused", async () => { const value = await fixture(); value.recovery(); Object.assign(value.enrolled, { candidateId: "b", targetProof: "target-b", transition: { ...sourceTransition, checkpointProof: "other-checkpoint" } }); await assert.rejects(value.run) }) test("inverse with external C baseline is refused", async () => { const value = await fixture(); value.recovery(); value.enrolled.candidateId = "c"; await assert.rejects(value.run) }) test("inverse with another authenticated source identity is refused", async () => { const value = await fixture(); value.recovery(); value.metadata.serviceInput.sourceProof = "other"; await assert.rejects(value.run) }) test("inverse with source dependency drift is refused", async () => { const value = await fixture(); value.recovery(); value.source.configuration.packet.dependencyProof = "other"; await assert.rejects(value.run) })