diff --git a/infra/deploy-runner/build-engine-mcp-execution-profile-decoder-artifact.mjs b/infra/deploy-runner/build-engine-mcp-execution-profile-decoder-artifact.mjs new file mode 100755 index 0000000..07e4f69 --- /dev/null +++ b/infra/deploy-runner/build-engine-mcp-execution-profile-decoder-artifact.mjs @@ -0,0 +1,176 @@ +#!/usr/bin/env node +import { createHash } from "node:crypto"; +import { spawnSync } from "node:child_process"; +import { copyFile, lstat, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +const here = dirname(fileURLToPath(import.meta.url)); +const workspaceRoot = resolve(here, "../../.."); +const engineRoot = resolve( + process.env.NODEDC_ENGINE_SOURCE_ROOT || join(workspaceRoot, "NODEDC_ENGINE_INFRA"), +); +const artifactRoot = resolve( + process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(here, "../deploy-artifacts"), +); +const [patchId = "", ...extra] = process.argv.slice(2); +if ( + extra.length + || !/^engine-mcp-execution-profile-decoder-\d{8}-\d{3}$/.test(patchId) +) { + throw new Error( + "usage: build-engine-mcp-execution-profile-decoder-artifact.mjs " + + "", + ); +} + +const descriptorPath = + "nodedc-source/server/deployTransitions/executionProfileDecoderV1.json"; +const targetSha256 = Object.freeze({ + "nodedc-source/server/routes/n8n.js": + "1c2427c1d5830c40b1e8ae05f3d683fc39d07d7e0fe2431b0f6efbbb1d3fcb88", + [descriptorPath]: + "93e431902e9bcd3b828a82ed6b42b48d939051f21bf8824dafcf2addac8a711c", +}); +const entries = Object.freeze(Object.keys(targetSha256)); +const artifact = join(artifactRoot, `nodedc-${patchId}.tgz`); + +await assertFresh(artifact); +await assertExactSources(); +const stage = await mkdtemp(join(tmpdir(), "nodedc-engine-mcp-profile-decoder-")); +try { + const payload = join(stage, "payload"); + for (const relativePath of entries) { + const destination = join(payload, relativePath); + await mkdir(dirname(destination), { recursive: true }); + await copyFile(join(engineRoot, relativePath), destination); + } + await writeFile( + join(stage, "manifest.env"), + `id=${patchId}\ncomponent=engine\ntype=app-overlay\n`, + { encoding: "utf8", flag: "wx", mode: 0o644 }, + ); + await writeFile(join(stage, "files.txt"), `${entries.join("\n")}\n`, { + encoding: "utf8", + flag: "wx", + mode: 0o644, + }); + await mkdir(artifactRoot, { recursive: true }); + run("python3", ["-c", canonicalTarScript(), artifact, stage]); + console.log(JSON.stringify({ + ok: true, + patchId, + artifact, + sha256: digest(await readFile(artifact)), + entries, + targetSha256, + services: ["nodedc-backend"], + transition: "exact-flatted-numeric-string-preservation", + mcpSurface: "external-codex", + mcpTool: "engine_get_node_output_profile", + valuesIncluded: false, + rawExecutionDataIncluded: false, + untouched: [ + "L2 graph", + "n8n", + "L1", + "Engine UI", + "databases", + "MCP Nginx", + "embedded AI Workspace", + ], + }, null, 2)); +} finally { + await rm(stage, { recursive: true, force: true }); +} + +async function assertExactSources() { + for (const [relativePath, expected] of Object.entries(targetSha256)) { + const sourcePath = join(engineRoot, relativePath); + const info = await lstat(sourcePath); + if (!info.isFile() || info.isSymbolicLink()) { + throw new Error(`engine_mcp_profile_decoder_source_unsafe:${relativePath}`); + } + const actual = digest(await readFile(sourcePath)); + if (actual !== expected) { + throw new Error( + `engine_mcp_profile_decoder_target_mismatch:${relativePath}:` + + `expected=${expected}:actual=${actual}`, + ); + } + } + + const source = await readFile( + join(engineRoot, "nodedc-source/server/routes/n8n.js"), + "utf8", + ); + for (const marker of [ + "const compactReference = Symbol('n8nCompactReference')", + "Top-level string entries are primitive values.", + "valuesIncluded: false,", + ]) { + if (!source.includes(marker)) { + throw new Error(`engine_mcp_profile_decoder_marker_missing:${marker}`); + } + } + + const descriptor = JSON.parse(await readFile(join(engineRoot, descriptorPath), "utf8")); + const expectedDescriptor = { + schemaVersion: "nodedc.engine.deploy-transition/v1", + id: "engine-mcp-execution-profile-decoder-v1", + component: "engine", + scope: "external-mcp-observability", + sourcePath: "nodedc-source/server/routes/n8n.js", + behavior: "preserve-top-level-numeric-string-primitives-in-flatted-execution-data", + acceptance: { + tool: "engine_get_node_output_profile", + valuesIncluded: false, + rawExecutionDataIncluded: false, + }, + }; + if (JSON.stringify(descriptor) !== JSON.stringify(expectedDescriptor)) { + throw new Error("engine_mcp_profile_decoder_descriptor_contract_mismatch"); + } +} + +async function assertFresh(path) { + try { + await lstat(path); + } catch (error) { + if (error?.code === "ENOENT") return; + throw error; + } + throw new Error("artifact_already_exists"); +} + +function canonicalTarScript() { + return [ + "import gzip,io,pathlib,sys,tarfile", + "root=pathlib.Path(sys.argv[2])", + "with open(sys.argv[1],'xb') as out:", + " with gzip.GzipFile(filename='',mode='wb',fileobj=out,compresslevel=9,mtime=0) as gz:", + " with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as tar:", + " for top in ('manifest.env','files.txt','payload'):", + " p=root/top; paths=[p]+(sorted(p.rglob('*')) if p.is_dir() else [])", + " for x in paths:", + " info=tar.gettarinfo(str(x),arcname=x.relative_to(root).as_posix())", + " info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644", + " with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info,src if info.isfile() else None)", + ].join("\n"); +} + +function digest(value) { + return createHash("sha256").update(value).digest("hex"); +} + +function run(command, args) { + const result = spawnSync(command, args, { + encoding: "utf8", + maxBuffer: 128 * 1024 * 1024, + stdio: ["ignore", "pipe", "pipe"], + }); + if (result.status !== 0) { + throw new Error(`${command}_failed:${result.stderr || result.stdout}`); + } +} diff --git a/infra/deploy-runner/nodedc-deploy b/infra/deploy-runner/nodedc-deploy index 8a72c46..ceeb68e 100755 --- a/infra/deploy-runner/nodedc-deploy +++ b/infra/deploy-runner/nodedc-deploy @@ -530,6 +530,23 @@ ENGINE_PROVIDER_TARGET_HOST_POLICY_PREDECESSOR_SHA256 = { ENGINE_PROVIDER_TARGET_HOST_POLICY_TARGET_SHA256 = { "nodedc-source/server/routes/n8n.js": "9bc3638e271102abec91bb80413329befb89d60c0e4dc0548f0dd11e93220d0a", } +ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL = ( + "nodedc-source/server/deployTransitions/executionProfileDecoderV1.json" +) +ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES = ( + "nodedc-source/server/routes/n8n.js", + ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL, +) +ENGINE_MCP_EXECUTION_PROFILE_DECODER_PREDECESSOR_SHA256 = { + "nodedc-source/server/routes/n8n.js": "752cb1524160adc1c95163c34e139b615c063d2ce8980f2a63879bddbd0f1e08", +} +ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256 = { + "nodedc-source/server/routes/n8n.js": "1c2427c1d5830c40b1e8ae05f3d683fc39d07d7e0fe2431b0f6efbbb1d3fcb88", + ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL: "93e431902e9bcd3b828a82ed6b42b48d939051f21bf8824dafcf2addac8a711c", +} +ENGINE_MCP_EXECUTION_PROFILE_DECODER_NEW_PATHS = ( + ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL, +) ENGINE_AGENT_FULL_GRANT_MIGRATION_ARTIFACT_ENTRIES = ( ENGINE_AGENT_FULL_GRANT_MIGRATION_STORE_REL, ) @@ -4307,6 +4324,41 @@ process.stdout.write('engine-provider-target-host-policy:exact-literal-host:v1') } +def accept_engine_mcp_execution_profile_decoder_runtime(): + root = component_root("engine") + validate_engine_mcp_execution_profile_decoder_slice( + root, + ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES, + ) + live = run_engine_backend_probe( + ( + "node", + "--input-type=module", + "-e", + """ +const module=await import('file:///app/server/routes/n8n.js'); +const execution={id:'profile-decoder-acceptance',data:JSON.stringify([{resultData:'1'},{runData:'2'},{Probe:'3'},['4'],{data:'5'},{main:'6'},[['7']],{json:'8'},{params:'9'},{VS_43:'10',mcc:'11',sats:9},'16','250'])}; +const profile=module.toSafeNodeOutputProfile(execution,'Probe'); +const paths=new Map(profile.paths.map((item)=>[item.path,item])); +if(!profile.found||profile.valuesIncluded!==false||paths.get('$.params.VS_43')?.types?.join(',')!=='string'||paths.get('$.params.VS_43')?.minLength!==2||paths.get('$.params.mcc')?.types?.join(',')!=='string'||paths.get('$.params.mcc')?.minLength!==3||paths.get('$.params.sats')?.types?.join(',')!=='number'||profile.paths.some((item)=>item.path.includes('.json.fact')))process.exit(2); +process.stdout.write('engine-mcp-execution-profile-decoder:flatted-numeric-strings:safe-profile:v1'); +""".strip(), + ), + "Engine MCP execution profile decoder", + container_id=engine_backend_container_id(), + ) + expected = ( + "engine-mcp-execution-profile-decoder:" + "flatted-numeric-strings:safe-profile:v1" + ) + if live != expected: + die("Engine MCP execution profile decoder live acceptance mismatch") + return { + "target_sha256": dict(ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256), + "live": live, + } + + def validate_no_lifecycle_scripts(payload_dir, label): forbidden = ("preinstall", "install", "postinstall", "prepare", "prepack", "postpack") for package_path in payload_dir.rglob("package.json"): @@ -4925,6 +4977,53 @@ def validate_engine_provider_target_host_policy_slice(payload_dir, entries): die("Engine provider target host policy contract mismatch") +def validate_engine_mcp_execution_profile_decoder_slice(payload_dir, entries): + if tuple(entries) != ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES: + die("Engine MCP execution profile decoder files.txt exact set/order mismatch") + for rel, expected_sha256 in ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256.items(): + path = payload_dir / rel + try: + path_stat = path.lstat() + except FileNotFoundError: + die(f"Engine MCP execution profile decoder target is missing: {rel}") + if ( + stat.S_ISLNK(path_stat.st_mode) + or not stat.S_ISREG(path_stat.st_mode) + or sha256_file(path) != expected_sha256 + ): + die(f"Engine MCP execution profile decoder target sha256 mismatch: {rel}") + descriptor = read_strict_json( + payload_dir / ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL, + "Engine MCP execution profile decoder descriptor", + max_bytes=16 * 1024, + ) + expected_descriptor = { + "schemaVersion": "nodedc.engine.deploy-transition/v1", + "id": "engine-mcp-execution-profile-decoder-v1", + "component": "engine", + "scope": "external-mcp-observability", + "sourcePath": "nodedc-source/server/routes/n8n.js", + "behavior": "preserve-top-level-numeric-string-primitives-in-flatted-execution-data", + "acceptance": { + "tool": "engine_get_node_output_profile", + "valuesIncluded": False, + "rawExecutionDataIncluded": False, + }, + } + if descriptor != expected_descriptor: + die("Engine MCP execution profile decoder descriptor contract mismatch") + source = (payload_dir / "nodedc-source/server/routes/n8n.js").read_text( + encoding="utf-8" + ) + required_markers = ( + "const compactReference = Symbol('n8nCompactReference')", + "Top-level string entries are primitive values.", + "valuesIncluded: false,", + ) + if any(marker not in source for marker in required_markers): + die("Engine MCP execution profile decoder source contract mismatch") + + def validate_engine_agent_full_grant_migration_slice(payload_dir, entries): if tuple(entries) != ENGINE_AGENT_FULL_GRANT_MIGRATION_ARTIFACT_ENTRIES: die("Engine agent full grant migration files.txt exact set/order mismatch") @@ -5370,6 +5469,11 @@ def load_artifact(artifact, work_dir): validate_engine_depttrans_zone_authority_v1_slice(payload_dir, entries) if is_engine_provider_target_host_policy_slice(manifest["component"], entries): validate_engine_provider_target_host_policy_slice(payload_dir, entries) + if is_engine_mcp_execution_profile_decoder_slice( + manifest["component"], + entries, + ): + validate_engine_mcp_execution_profile_decoder_slice(payload_dir, entries) if is_engine_provider_security_catalog_slice(manifest["component"], entries): validate_engine_provider_security_catalog_payload(payload_dir, entries) if touches_engine_credential_sink(manifest["component"], entries): @@ -5383,6 +5487,10 @@ def load_artifact(artifact, work_dir): and not is_engine_provider_authority_diagnostics_slice(manifest["component"], entries) and not is_engine_depttrans_zone_authority_v1_slice(manifest["component"], entries) and not is_engine_provider_target_host_policy_slice(manifest["component"], entries) + and not is_engine_mcp_execution_profile_decoder_slice( + manifest["component"], + entries, + ) and ( touches_engine_data_product_publish_grant(entries) or ENGINE_DATA_PRODUCT_PUBLISH_GRANT_OVERRIDE_REL in entries @@ -5561,6 +5669,14 @@ def is_engine_provider_target_host_policy_slice(component, entries): ) +def is_engine_mcp_execution_profile_decoder_slice(component, entries): + return ( + component == "engine" + and entries is not None + and tuple(entries) == ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES + ) + + def is_engine_agent_full_grant_migration_slice(component, entries): return ( component == "engine" @@ -6004,6 +6120,42 @@ def preflight_engine_provider_target_host_policy_predecessor(): } +def preflight_engine_mcp_execution_profile_decoder_predecessor(): + root = component_root("engine") + actual = {} + for rel, expected_sha256 in ( + ENGINE_MCP_EXECUTION_PROFILE_DECODER_PREDECESSOR_SHA256.items() + ): + path = root / rel + try: + path_stat = path.lstat() + except FileNotFoundError: + die(f"Engine MCP execution profile decoder predecessor is missing: {rel}") + if stat.S_ISLNK(path_stat.st_mode) or not stat.S_ISREG(path_stat.st_mode): + die(f"Engine MCP execution profile decoder predecessor is unsafe: {rel}") + actual_sha256 = sha256_file(path) + if actual_sha256 != expected_sha256: + die( + "Engine MCP execution profile decoder predecessor drift detected: " + f"path={rel} expected={expected_sha256} actual={actual_sha256}" + ) + actual[rel] = actual_sha256 + for rel in ENGINE_MCP_EXECUTION_PROFILE_DECODER_NEW_PATHS: + path = root / rel + if path.exists() or path.is_symlink(): + die(f"Engine MCP execution profile decoder new path already exists: {rel}") + backend = preflight_engine_credential_backend_runtime() + if backend["mode"] != "verified-derived-retry": + die("Engine MCP execution profile decoder requires the active immutable backend") + return { + "mode": "exact-flatted-numeric-string-preservation", + "predecessor_sha256": actual, + "target_sha256": dict(ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256), + "new_paths": tuple(ENGINE_MCP_EXECUTION_PROFILE_DECODER_NEW_PATHS), + "backend_mode": backend["mode"], + } + + def preflight_engine_agent_full_grant_migration_predecessor(): root = component_root("engine") store_path = root / ENGINE_AGENT_FULL_GRANT_MIGRATION_STORE_REL @@ -6057,6 +6209,7 @@ def component_services(component, entries=None): or is_engine_provider_authority_diagnostics_slice(component, entries) or is_engine_depttrans_zone_authority_v1_slice(component, entries) or is_engine_provider_target_host_policy_slice(component, entries) + or is_engine_mcp_execution_profile_decoder_slice(component, entries) or is_engine_provider_security_catalog_slice(component, entries) ): # This slice updates only the existing Engine backend control plane. @@ -8265,6 +8418,7 @@ def plan_artifact(artifact): provider_authority_diagnostics_preflight = None depttrans_zone_authority_v1_preflight = None provider_target_host_policy_preflight = None + mcp_execution_profile_decoder_preflight = None provider_catalog_preflight = None with tempfile.TemporaryDirectory(prefix="plan-", dir=TMP_DIR) as tmp: manifest, entries, payload_dir = load_artifact(artifact, Path(tmp)) @@ -8319,6 +8473,13 @@ def plan_artifact(artifact): provider_target_host_policy_preflight = ( preflight_engine_provider_target_host_policy_predecessor() ) + if is_engine_mcp_execution_profile_decoder_slice( + manifest["component"], + entries, + ): + mcp_execution_profile_decoder_preflight = ( + preflight_engine_mcp_execution_profile_decoder_predecessor() + ) if is_engine_provider_security_catalog_slice(manifest["component"], entries): provider_catalog_preflight = preflight_engine_provider_security_catalog_predecessor() @@ -8351,6 +8512,12 @@ def plan_artifact(artifact): component, entries, ) + touches_mcp_execution_profile_decoder = ( + is_engine_mcp_execution_profile_decoder_slice( + component, + entries, + ) + ) touches_agent_grant_migration = is_engine_agent_full_grant_migration_slice( component, entries, @@ -8366,6 +8533,7 @@ def plan_artifact(artifact): or touches_provider_authority_diagnostics or touches_depttrans_zone_authority_v1 or touches_provider_target_host_policy + or touches_mcp_execution_profile_decoder or touches_agent_grant_migration ): credential_backend_preflight = preflight_engine_credential_backend_runtime() @@ -8400,6 +8568,14 @@ def plan_artifact(artifact): die("Engine provider target host policy preflight is missing") if credential_backend_preflight["mode"] != "verified-derived-retry": die("Engine provider target host policy requires the active immutable credential backend") + if touches_mcp_execution_profile_decoder: + if mcp_execution_profile_decoder_preflight is None: + die("Engine MCP execution profile decoder preflight is missing") + if credential_backend_preflight["mode"] != "verified-derived-retry": + die( + "Engine MCP execution profile decoder requires the active " + "immutable credential backend" + ) if touches_agent_grant_migration: agent_grant_migration_predecessor_sha256 = ( preflight_engine_agent_full_grant_migration_predecessor() @@ -8667,6 +8843,50 @@ def plan_artifact(artifact): print("n8n_l1=untouched") print("engine_ui=untouched") print("engine_databases=untouched") + if mcp_execution_profile_decoder_preflight: + print( + "engine_mcp_observability_transition=" + "exact-flatted-numeric-string-preservation" + ) + print("engine_mcp_surface=external-codex") + print("engine_mcp_tool=engine_get_node_output_profile") + print("engine_mcp_profile_values_included=no") + print("engine_mcp_raw_execution_data_included=no") + for changed_path in ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES: + print(f"engine_mcp_profile_decoder_changed_path={changed_path}") + if changed_path in mcp_execution_profile_decoder_preflight[ + "predecessor_sha256" + ]: + print( + f"engine_mcp_profile_decoder_predecessor_sha256[{changed_path}]=" + f"{mcp_execution_profile_decoder_preflight['predecessor_sha256'][changed_path]}" + ) + elif changed_path in mcp_execution_profile_decoder_preflight["new_paths"]: + print( + f"engine_mcp_profile_decoder_predecessor_state[{changed_path}]=" + "absent" + ) + else: + die( + "Engine MCP execution profile decoder plan has no predecessor " + f"state: {changed_path}" + ) + print( + f"engine_mcp_profile_decoder_target_sha256[{changed_path}]=" + f"{mcp_execution_profile_decoder_preflight['target_sha256'][changed_path]}" + ) + print( + "backend_current_barrier=" + f"{mcp_execution_profile_decoder_preflight['backend_mode']}" + ) + print("backend_force_recreate=yes") + print("backend_pull=never") + print("l2_graph=untouched") + print("n8n_l1=untouched") + print("engine_ui=untouched") + print("engine_databases=untouched") + print("mcp_nginx=untouched") + print("embedded_ai_workspace=untouched") if transition_descriptor: print(f"n8n_transition={transition_descriptor['action']}") print(f"n8n_version={transition_descriptor['n8nVersion']}") @@ -9783,6 +10003,7 @@ def component_healthchecks(component, entries=None, services=None): or is_engine_provider_authority_diagnostics_slice(component, entries) or is_engine_depttrans_zone_authority_v1_slice(component, entries) or is_engine_provider_target_host_policy_slice(component, entries) + or is_engine_mcp_execution_profile_decoder_slice(component, entries) or is_engine_agent_full_grant_migration_slice(component, entries) or is_engine_mcp_control_plane_slice(component, entries) or is_engine_mcp_ontology_sdk_slice(component, entries) @@ -10190,6 +10411,12 @@ def run_healthchecks(component, entries=None, services=None): component, entries, ) + touches_mcp_execution_profile_decoder = ( + is_engine_mcp_execution_profile_decoder_slice( + component, + entries, + ) + ) touches_agent_grant_migration = is_engine_agent_full_grant_migration_slice( component, entries, @@ -10206,6 +10433,7 @@ def run_healthchecks(component, entries=None, services=None): or touches_provider_authority_diagnostics or touches_depttrans_zone_authority_v1 or touches_provider_target_host_policy + or touches_mcp_execution_profile_decoder or touches_agent_grant_migration or touches_mcp_control_plane or touches_mcp_ontology_sdk @@ -10226,6 +10454,7 @@ def run_healthchecks(component, entries=None, services=None): or touches_provider_authority_diagnostics or touches_depttrans_zone_authority_v1 or touches_provider_target_host_policy + or touches_mcp_execution_profile_decoder or touches_agent_grant_migration or touches_mcp_control_plane or touches_mcp_ontology_sdk @@ -10311,6 +10540,32 @@ def run_healthchecks(component, entries=None, services=None): accept_engine_provider_target_host_policy_runtime() elif installed_sha256 != ENGINE_PROVIDER_TARGET_HOST_POLICY_PREDECESSOR_SHA256: die("Engine provider target host policy installed state is neither target nor rollback predecessor") + if touches_mcp_execution_profile_decoder: + root = component_root("engine") + target_state = all( + (root / rel).is_file() + and not (root / rel).is_symlink() + and sha256_file(root / rel) == expected + for rel, expected in ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256.items() + ) + predecessor_state = all( + (root / rel).is_file() + and not (root / rel).is_symlink() + and sha256_file(root / rel) == expected + for rel, expected in ( + ENGINE_MCP_EXECUTION_PROFILE_DECODER_PREDECESSOR_SHA256.items() + ) + ) and all( + not (root / rel).exists() and not (root / rel).is_symlink() + for rel in ENGINE_MCP_EXECUTION_PROFILE_DECODER_NEW_PATHS + ) + if target_state: + accept_engine_mcp_execution_profile_decoder_runtime() + elif not predecessor_state: + die( + "Engine MCP execution profile decoder installed state is neither " + "target nor rollback predecessor" + ) container_name = COMPONENTS[component].get("health_container") if container_name: healthcheck_container(container_name) @@ -10402,6 +10657,19 @@ def apply_artifact(artifact): target_host_policy_backend_preflight = preflight_engine_credential_backend_runtime() if target_host_policy_backend_preflight["mode"] != "verified-derived-retry": die("Engine provider target host policy requires the active immutable credential backend") + if is_engine_mcp_execution_profile_decoder_slice(component, entries): + preflight_engine_mcp_execution_profile_decoder_predecessor() + profile_decoder_backend_preflight = ( + preflight_engine_credential_backend_runtime() + ) + if ( + profile_decoder_backend_preflight["mode"] + != "verified-derived-retry" + ): + die( + "Engine MCP execution profile decoder requires the " + "active immutable credential backend" + ) if is_engine_agent_full_grant_migration_slice(component, entries): preflight_engine_agent_full_grant_migration_predecessor() migration_backend_preflight = preflight_engine_credential_backend_runtime() @@ -10654,6 +10922,10 @@ def apply_artifact(artifact): or is_engine_provider_authority_diagnostics_slice(component, entries) or is_engine_depttrans_zone_authority_v1_slice(component, entries) or is_engine_provider_target_host_policy_slice(component, entries) + or is_engine_mcp_execution_profile_decoder_slice( + component, + entries, + ) or is_engine_agent_full_grant_migration_slice(component, entries) or is_engine_mcp_control_plane_slice(component, entries) or is_engine_mcp_ontology_sdk_slice(component, entries) diff --git a/infra/deploy-runner/test_engine_mcp_execution_profile_decoder.py b/infra/deploy-runner/test_engine_mcp_execution_profile_decoder.py new file mode 100644 index 0000000..7825b52 --- /dev/null +++ b/infra/deploy-runner/test_engine_mcp_execution_profile_decoder.py @@ -0,0 +1,325 @@ +import hashlib +import importlib.machinery +import importlib.util +import io +import json +import os +from pathlib import Path +import subprocess +import tarfile +import tempfile +import unittest +from contextlib import redirect_stdout +from unittest import mock + + +SCRIPT_DIR = Path(__file__).resolve().parent +RUNNER_PATH = SCRIPT_DIR / "nodedc-deploy" +BUILDER_PATH = ( + SCRIPT_DIR / "build-engine-mcp-execution-profile-decoder-artifact.mjs" +) +ENGINE_ROOT = SCRIPT_DIR.parent.parent.parent / "NODEDC_ENGINE_INFRA" +PATCH_ID = "engine-mcp-execution-profile-decoder-20991231-999" + + +def load_runner(): + loader = importlib.machinery.SourceFileLoader( + "nodedc_engine_mcp_execution_profile_decoder", + str(RUNNER_PATH), + ) + spec = importlib.util.spec_from_loader(loader.name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +RUNNER = load_runner() + + +class EngineMcpExecutionProfileDecoderTest(unittest.TestCase): + def require_current_target_source(self): + for relative_path, expected in ( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256.items() + ): + path = ENGINE_ROOT / relative_path + if ( + not path.is_file() + or hashlib.sha256(path.read_bytes()).hexdigest() != expected + ): + self.skipTest( + "execution profile decoder source has advanced " + "to its exact successor" + ) + + def build(self, artifact_dir): + environment = os.environ.copy() + environment["NODEDC_ENGINE_SOURCE_ROOT"] = str(ENGINE_ROOT) + environment["NODEDC_DEPLOY_ARTIFACT_DIR"] = str(artifact_dir) + completed = subprocess.run( + ["node", str(BUILDER_PATH), PATCH_ID], + check=True, + capture_output=True, + text=True, + env=environment, + ) + return json.loads(completed.stdout) + + def test_builder_emits_exact_deterministic_two_file_slice(self): + self.require_current_target_source() + with tempfile.TemporaryDirectory( + prefix="nodedc-engine-mcp-profile-decoder-" + ) as directory: + root = Path(directory) + first = self.build(root / "first") + second = self.build(root / "second") + first_artifact = Path(first["artifact"]) + second_artifact = Path(second["artifact"]) + self.assertEqual(first_artifact.read_bytes(), second_artifact.read_bytes()) + self.assertEqual( + first["sha256"], + hashlib.sha256(first_artifact.read_bytes()).hexdigest(), + ) + self.assertEqual(first["services"], ["nodedc-backend"]) + self.assertEqual(first["valuesIncluded"], False) + self.assertEqual(first["rawExecutionDataIncluded"], False) + self.assertEqual( + tuple(first["entries"]), + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES, + ) + + extract = root / "extract" + with tarfile.open(first_artifact, "r:gz") as archive: + archive.extractall(extract, filter="data") + entries = tuple( + (extract / "files.txt").read_text(encoding="utf-8").splitlines() + ) + payload = extract / "payload" + self.assertTrue( + RUNNER.is_engine_mcp_execution_profile_decoder_slice( + "engine", + entries, + ) + ) + self.assertFalse( + RUNNER.is_engine_provider_target_host_policy_slice( + "engine", + entries, + ) + ) + self.assertEqual( + RUNNER.component_services("engine", entries), + ("nodedc-backend",), + ) + self.assertEqual( + RUNNER.component_healthchecks( + "engine", + entries, + ("nodedc-backend",), + ), + ("http://127.0.0.1:3001/health",), + ) + RUNNER.validate_engine_mcp_execution_profile_decoder_slice( + payload, + entries, + ) + with tempfile.TemporaryDirectory( + prefix="nodedc-engine-mcp-profile-plan-" + ) as plan_directory: + loaded = RUNNER.load_artifact( + first_artifact, + Path(plan_directory), + ) + self.assertEqual(loaded[0]["component"], "engine") + self.assertEqual(tuple(loaded[1]), entries) + + def test_preflight_requires_exact_predecessor_and_absent_descriptor(self): + with tempfile.TemporaryDirectory( + prefix="nodedc-engine-mcp-profile-preflight-" + ) as directory: + root = Path(directory) + relative_path = next( + iter( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_PREDECESSOR_SHA256 + ) + ) + path = root / relative_path + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("predecessor\n", encoding="utf-8") + expected = ( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_PREDECESSOR_SHA256[ + relative_path + ] + ) + with ( + mock.patch.object(RUNNER, "component_root", return_value=root), + mock.patch.object(RUNNER, "sha256_file", return_value=expected), + mock.patch.object( + RUNNER, + "preflight_engine_credential_backend_runtime", + return_value={"mode": "verified-derived-retry"}, + ), + ): + result = ( + RUNNER.preflight_engine_mcp_execution_profile_decoder_predecessor() + ) + self.assertEqual( + result["mode"], + "exact-flatted-numeric-string-preservation", + ) + + descriptor = ( + root / RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL + ) + descriptor.parent.mkdir(parents=True, exist_ok=True) + descriptor.write_text("{}\n", encoding="utf-8") + with ( + mock.patch.object(RUNNER, "component_root", return_value=root), + mock.patch.object(RUNNER, "sha256_file", return_value=expected), + ): + with self.assertRaises(RUNNER.DeployError): + RUNNER.preflight_engine_mcp_execution_profile_decoder_predecessor() + + def test_plan_renders_exact_external_mcp_boundary(self): + self.require_current_target_source() + with tempfile.TemporaryDirectory( + prefix="nodedc-engine-mcp-profile-plan-" + ) as directory: + root = Path(directory) + built = self.build(root / "artifacts") + artifact = Path(built["artifact"]) + live_root = root / "live" + live_root.mkdir() + preflight = { + "mode": "exact-flatted-numeric-string-preservation", + "predecessor_sha256": dict( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_PREDECESSOR_SHA256 + ), + "target_sha256": dict( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256 + ), + "new_paths": tuple( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_NEW_PATHS + ), + "backend_mode": "verified-derived-retry", + } + output = io.StringIO() + with ( + mock.patch.object(RUNNER, "validate_artifact_location"), + mock.patch.object(RUNNER, "ensure_layout"), + mock.patch.object(RUNNER, "TMP_DIR", root), + mock.patch.object(RUNNER, "component_root", return_value=live_root), + mock.patch.object( + RUNNER, + "component_compose_root", + return_value=live_root, + ), + mock.patch.object( + RUNNER, + "preflight_engine_mcp_execution_profile_decoder_predecessor", + return_value=preflight, + ), + mock.patch.object( + RUNNER, + "preflight_engine_credential_backend_runtime", + return_value={"mode": "verified-derived-retry"}, + ), + mock.patch.object(RUNNER, "state_has_sha", return_value=False), + mock.patch.object(RUNNER, "state_has_patch_id", return_value=False), + redirect_stdout(output), + ): + RUNNER.plan_artifact(artifact) + plan = output.getvalue() + self.assertIn("services=nodedc-backend", plan) + self.assertIn( + "engine_mcp_observability_transition=" + "exact-flatted-numeric-string-preservation", + plan, + ) + self.assertIn("engine_mcp_surface=external-codex", plan) + self.assertIn("engine_mcp_profile_values_included=no", plan) + self.assertIn("mcp_nginx=untouched", plan) + self.assertIn("embedded_ai_workspace=untouched", plan) + self.assertIn( + "engine_mcp_profile_decoder_predecessor_state" + f"[{RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_DESCRIPTOR_REL}]=" + "absent", + plan, + ) + self.assertIn("state=new", plan) + + def test_healthchecks_dispatch_safe_profile_acceptance(self): + entries = RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_ARTIFACT_ENTRIES + with tempfile.TemporaryDirectory( + prefix="nodedc-engine-mcp-profile-health-" + ) as directory: + root = Path(directory) + for relative_path in entries: + path = root / relative_path + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("target\n", encoding="utf-8") + + def target_hash(path): + relative_path = Path(path).relative_to(root).as_posix() + return ( + RUNNER.ENGINE_MCP_EXECUTION_PROFILE_DECODER_TARGET_SHA256[ + relative_path + ] + ) + + with ( + mock.patch.object(RUNNER, "component_root", return_value=root), + mock.patch.object(RUNNER, "sha256_file", side_effect=target_hash), + mock.patch.object(RUNNER, "healthcheck_compose_service"), + mock.patch.object(RUNNER, "component_healthchecks", return_value=()), + mock.patch.object( + RUNNER, + "preflight_engine_credential_backend_runtime", + return_value={"mode": "verified-derived-retry"}, + ), + mock.patch.object( + RUNNER, + "accept_engine_mcp_execution_profile_decoder_runtime", + return_value={"live": "accepted"}, + ) as acceptance, + mock.patch.object(RUNNER, "healthcheck_container"), + ): + RUNNER.run_healthchecks( + "engine", + entries, + ("nodedc-backend",), + ) + acceptance.assert_called_once_with() + + def test_live_acceptance_uses_safe_profile_contract(self): + self.require_current_target_source() + expected_live = ( + "engine-mcp-execution-profile-decoder:" + "flatted-numeric-strings:safe-profile:v1" + ) + with ( + mock.patch.object(RUNNER, "component_root", return_value=ENGINE_ROOT), + mock.patch.object( + RUNNER, + "engine_backend_container_id", + return_value="backend", + ), + mock.patch.object( + RUNNER, + "run_engine_backend_probe", + return_value=expected_live, + ) as backend_probe, + ): + result = RUNNER.accept_engine_mcp_execution_profile_decoder_runtime() + self.assertEqual(result["live"], expected_live) + self.assertEqual( + backend_probe.call_args.args[1], + "Engine MCP execution profile decoder", + ) + probe_source = backend_probe.call_args.args[0][-1] + self.assertIn("module.toSafeNodeOutputProfile", probe_source) + self.assertIn("profile.valuesIncluded!==false", probe_source) + self.assertIn(".json.fact", probe_source) + + +if __name__ == "__main__": + unittest.main(verbosity=2)