Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141/** * CLI managed-bootstrap authority (saga u4uo9m.3 plan item 1). * * A daemon-managed subagent is spawned with `--managed-manifest <path>` + `--daemon-socket * <path>`. The CLI loads + validates the manifest BEFORE model resolution/bootstrap and treats * its pinned values as AUTHORITATIVE — managed `session/cwd/model/provider/mode/prompt/tools/ * skills/depth/policy/timeout` override any conflicting direct flag. Direct/test paths * (no `--managed-manifest`) are unchanged and backward compatible. * * This pure helper turns a validated {@link AgentManifestV1} into the override set the CLI * feeds (a) into the model resolver (so pinned model/provider win over flags) and (b) into * `createDefaultCore` as the `managed` {@link ManagedBootstrapOptions} + the pinned system * prompt + cwd/mode/agentId overrides. Prompt/task/config never appear on argv (saga * u4uo9m non-disclosure). * * Layer L4 leaf (CLI): imports L1 contracts + the L4 daemon manifest helpers; never imported * by L2 core — the core consumes only the plain `ManagedContext`/`CapabilityPolicy` data. */import { homedir } from 'node:os';import { join } from 'node:path';import type { AgentManifestV1 } from '@/daemon/agent-catalog';import { ROOT_DEPTH, ROOT_LAUNCH_POLICY } from '@/daemon/agent-catalog';import type { ManagedContext, CapabilityPolicy } from '@/contracts/managed-context';import type { Rule } from '@/contracts/decision';import type { DefaultMode } from '@/core/default';
/** * The manifest-derived overrides the CLI applies when `--managed-manifest` is present. Each * field is `undefined` when the manifest omits it (preserving the direct path's default for * that field). `managed` is always present (the {@link ManagedBootstrapOptions} consumed by * `createDefaultCore`); `systemPrompt` is the pinned definition body. */export interface ManagedAuthority { /** Pinned model name override (wins over `--model`). */ modelOverride?: string; /** Pinned provider-key override (wins over `--provider`). */ providerOverride?: string; /** Pinned mode override (`plan`|`build`); wins over the build default. */ modeOverride?: DefaultMode; /** Pinned cwd override (wins over `--cwd`). */ cwdOverride?: string; /** Pinned agent id (the manifest's durable `agent_id`; wins over `--id`). */ agentIdOverride?: string; /** Durable session identity owned by the manifest. */ sessionIdOverride?: string; /** Pinned per-run deadline seconds (saga u4uo9m.1; consumed by the later coordinator). */ timeout?: number; /** The pinned definition body (base identity; restart/relaunch determinism). */ systemPrompt?: string; /** Optional pinned permission rules (evaluated first by the child gate). */ rules?: Rule[]; /** The full managed-bootstrap options (context + capability + child grant dir). */ managed: { context: ManagedContext; capability: CapabilityPolicy; childGrantBaseDir?: string; rules?: Rule[]; };}
/** * Build the {@link ManagedAuthority} from a validated manifest + the daemon socket path. The * `is_child` capability flag follows the durable parent link: a manifest with no * `parent_agent_id` is a root (depth pinned `0`, fixed `{maxTasks:8,maxDepth:1}`); a child * (has `parent_agent_id`) never exposes `subagent_launch` unless its explicit tool allowlist * names it. * * The child grant directory defaults to the historic per-agent dir * (`~/.shard/agents/<agentId>/`) so a managed child's `allow_always` grants are isolated * from any direct-run agent sharing that id (which never happens in practice — managed ids * are daemon-preallocated and durable). The daemon may pin a daemon-home-relative path via * the manifest in a later slice; the bootstrap honors `childGrantBaseDir` when supplied. * * Pure; does not touch the filesystem or argv. */export function buildManagedAuthority( manifest: AgentManifestV1, daemonSocket: string, rules?: Rule[],): ManagedAuthority { const isChild = manifest.spawn_options.parent_agent_id !== undefined; const depth = manifest.depth ?? ROOT_DEPTH; const launchPolicy = manifest.launch_policy ?? ROOT_LAUNCH_POLICY; // Pinned permission rules: the manifest is the configuration authority, so its // `permissions.rules` are authoritative (evaluated first by the child gate). An optional // caller-supplied `rules` (e.g. test injection) is appended after; production never // passes it. This is the seam that lets a worker definition suppress per-call prompts. const pinnedRules: Rule[] = manifest.permissions?.rules ?? []; const allRules: Rule[] = [...pinnedRules, ...(rules ?? [])];
const context: ManagedContext = { daemon_socket: daemonSocket, agent_id: manifest.agent_id, ...(manifest.spawn_options.parent_agent_id !== undefined ? { parent_agent_id: manifest.spawn_options.parent_agent_id } : {}), depth, launch_policy: launchPolicy, };
const capability: CapabilityPolicy = { is_child: isChild, ...(manifest.spawn_options.tools !== undefined ? { tool_allowlist: manifest.spawn_options.tools } : {}), ...(manifest.spawn_options.skills !== undefined ? { skill_allowlist: manifest.spawn_options.skills } : {}), };
const modeOverride: DefaultMode | undefined = manifest.spawn_options.mode === 'plan' || manifest.spawn_options.mode === 'build' ? manifest.spawn_options.mode : undefined;
const authority: ManagedAuthority = { ...(manifest.spawn_options.model !== undefined ? { modelOverride: manifest.spawn_options.model } : {}), ...(manifest.spawn_options.provider !== undefined ? { providerOverride: manifest.spawn_options.provider } : {}), ...(modeOverride !== undefined ? { modeOverride } : {}), ...(manifest.cwd !== undefined ? { cwdOverride: manifest.cwd } : {}), agentIdOverride: manifest.agent_id, sessionIdOverride: manifest.session_id, ...(manifest.timeout !== undefined ? { timeout: manifest.timeout } : {}), ...(manifest.system_prompt !== undefined ? { systemPrompt: manifest.system_prompt } : {}), ...(allRules.length > 0 ? { rules: allRules } : {}), managed: { context, capability, childGrantBaseDir: join(homedir(), '.shard', 'agents', manifest.agent_id), ...(allRules.length > 0 ? { rules: allRules } : {}), }, }; return authority;}