import { readFileSync } from "node:fs"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { isAgtGovernanceActive } from "./agt-governance-active.js"; import { isHarnessProjectEnabled } from "./harness-project-config.js"; const LOAD_GUARD_KEY = Symbol.for("ultimate-pi.extension-load-guard"); type LoadGuardRegistry = Set; function getRegistry(): LoadGuardRegistry { const state = globalThis as typeof globalThis & { [LOAD_GUARD_KEY]?: LoadGuardRegistry; }; if (!state[LOAD_GUARD_KEY]) { state[LOAD_GUARD_KEY] = new Set(); } return state[LOAD_GUARD_KEY]; } function isSourceRepo(): boolean { try { const pkg = JSON.parse( readFileSync(join(process.cwd(), "package.json"), "utf8"), ) as { name?: string }; return pkg.name === "ultimate-pi"; } catch { return false; } } export function claimExtensionLoad(key: string, moduleUrl: string): boolean { const registry = getRegistry(); const modulePath = fileURLToPath(moduleUrl); if (modulePath.includes("/node_modules/ultimate-pi/") && isSourceRepo()) { return false; } if (registry.has(key)) return false; registry.add(key); return true; } /** AGT subprocess governance for all subagents when project/harness policy is active. */ export function claimSubagentGovernanceLoad( key: string, moduleUrl: string, projectRoot?: string, ): boolean { if (!isAgtGovernanceActive(projectRoot)) return false; return claimExtensionLoad(key, moduleUrl); } /** Skip duplicate loads and skip harness-only governance when harness is disabled. */ export function claimHarnessGovernanceLoad( key: string, moduleUrl: string, ): boolean { if (!isHarnessProjectEnabled()) return false; return claimExtensionLoad(key, moduleUrl); }