// One verdict per agent: is node9 really checking it on this machine? // // `node9 doctor` prints it and the add-ons report sends it to the dashboard // (docs roadmap/active/agents-on-machines-design.md, 3.1.1). Both call this // function, so the dashboard can never call an agent protected where doctor // does not. A wired hook is not always a running hook: Codex skips hooks it // was never asked to trust, and OpenCode 2 refuses a 1.x plugin file. import fs from 'fs'; import type { AgentWiringRow } from './agent-wiring'; import { assessCodexTrust, type CodexTrustAssessment } from './codex-trust'; /** `nothing-to-check`: a chat app with no MCP server set up. node9 checks * only the MCP tools such an app is given, and it has none: not a gap. */ export type AgentProtection = 'protected' | 'partly' | 'not-protected' | 'nothing-to-check'; /** Why an agent is not plainly protected. Fixed codes; the page words them. */ export type AgentGapReason = | 'codex-trust-unconfirmed' | 'codex-hooks-disabled' | 'codex-never-trusted' | 'opencode-plugin-outdated' | 'settings-invalid' | 'hook-missing' | 'not-set-up' | 'no-mcp-servers'; export interface AgentVerdict { protection: AgentProtection; reason?: AgentGapReason; /** Some hook (or plugin file) of the agent is wired. */ anyHookWired: boolean; /** Codex only, when its hooks are wired: what its trust looks like. */ codexTrust?: CodexTrustAssessment; } export function agentVerdict(row: AgentWiringRow, home: string): AgentVerdict { const anyHookWired = row.hooks.some((h) => h.wired); if (row.isProtected && row.id === 'codex' && anyHookWired) { // Codex runs a hook only after the user trusts it, trust is keyed to the // file's content, and node9 rewrites that file on every init/add/heal. const codexTrust = assessCodexTrust(home); const base = { anyHookWired, codexTrust }; if (codexTrust.state === 'observed') return { ...base, protection: 'protected' }; if (codexTrust.state === 'disabled') return { ...base, protection: 'not-protected', reason: 'codex-hooks-disabled' }; if (codexTrust.state === 'never-trusted') return { ...base, protection: 'not-protected', reason: 'codex-never-trusted' }; // Stale trust and "Codex not used since" look identical from here. return { ...base, protection: 'partly', reason: 'codex-trust-unconfirmed' }; } if (row.id === 'opencode' && !anyHookWired && fs.existsSync(row.settingsPath)) { // The file exists but is the 1.x shape: OpenCode 1 runs it, 2 refuses it. return { anyHookWired, protection: 'partly', reason: 'opencode-plugin-outdated' }; } if (row.isProtected) return { anyHookWired, protection: 'protected' }; if ( row.id === 'claudeDesktop' && (row.installed || row.present) && mcpServerCount(row.settingsPath) === 0 ) { // The Claude desktop chat runs no commands of its own; node9 can check // only the MCP servers it is given, and none is set up. return { anyHookWired, protection: 'nothing-to-check', reason: 'no-mcp-servers' }; } if (row.wireState === 'invalid') return { anyHookWired, protection: 'not-protected', reason: 'settings-invalid' }; if (row.wireState === 'unwired') return { anyHookWired, protection: 'not-protected', reason: 'hook-missing' }; return { anyHookWired, protection: 'not-protected', reason: 'not-set-up' }; } /** How many MCP servers a JSON config names (`mcpServers`); 0 when the file * is absent, null when it cannot be read or parsed (then nothing is assumed). */ function mcpServerCount(file: string): number | null { if (!file) return null; let text: string; try { text = fs.readFileSync(file, 'utf-8'); } catch (err) { return (err as NodeJS.ErrnoException).code === 'ENOENT' ? 0 : null; } try { const parsed = JSON.parse(text) as { mcpServers?: unknown }; const servers = parsed?.mcpServers; if (servers === undefined || servers === null) return 0; return typeof servers === 'object' && !Array.isArray(servers) ? Object.keys(servers).length : null; } catch { return null; } }