-
Validate preconditions. Abort before any filesystem write if:
- the
agent-files skill (tool agent_files_init) is not reachable at runtime
- any
children[i].relative_dir is absolute, contains / or \, contains .., or collides with another child's relative_dir
- any resolved child path escapes
parent_dir
parent_mission is empty, a literal placeholder, or mixes in workflow rules/current state/next steps
- any child
mission is empty, a literal placeholder, outside the parent mission, or mixes in workflow rules/current state/next steps
parent_objective or any child objective reads like permanent mission, scope, policy, or success criteria
parent_dir already has a four-file contract and overwrite is false, but the existing parent files fail upstream agent-files compatibility or cannot safely accept/verify the parent hierarchy changes without modifying immutable AGENT.md
-
Scaffold or verify parent. Invoke agent_files_init with target_dir = parent_dir, mission = parent_mission, objective = parent_objective (if provided), and overwrite. If overwrite=false and compatible parent files already exist, record them in files_skipped under the parent's absolute path and continue with hierarchy patch verification.
-
Patch or verify parent AGENT_HARNESS.md. Under ## Reusable Preferences, replace the placeholder bullet - _(populate as rules emerge)_ with the verbatim progress-scope rule from examples/parent-harness.progress-scope.snippet. If the bullet is already present verbatim, treat it as verified and emit a warning. If neither the placeholder nor the rule is present and overwrite=false, abort rather than guessing where to place parent-scope policy.
-
For each child (in input order):
a. Resolve child_dir = join(parent_dir, child.relative_dir).
b. Invoke agent_files_init with target_dir = child_dir, mission = child.mission, objective = child.objective (if provided), and overwrite. If overwrite=false and existing child files fail the upstream compatibility checks, abort before applying or verifying child hierarchy changes.
c. Install or verify child AGENT.md as immutable control. If the child AGENT.md was created in this same invocation, or overwrite=true explicitly authorizes replacement, install the rendered examples/child-AGENT.md.template as the child's initial immutable control file before reporting the child scaffold complete. If overwrite=false and AGENT.md already existed, do not replace or patch it; verify that it already contains the hierarchy contract and immutability clauses: parent-first read order, ## Precedence, ## Update Routing, an explicit reference to {{ parent_name }}/ as the outer boundary, agent-immutable control contract, control-contract replacement, migration, or scaffold overwrite, and Child skills, sidecars. If any clause is missing, abort rather than creating a mixed-boundary child.
d. Patch or verify child AGENT_GOAL.md. Locate ## Constraints; prepend the rendered examples/child-AGENT_GOAL.constraint.snippet as the first constraint block. If the same block is already present, treat the patch as verified. If ## Constraints is absent, abort rather than leaving the child without an outer-boundary constraint.
e. Leave the child AGENT_HARNESS.md and AGENT_PROGRESS.md as scaffolded by agent-files. The progress-scope rule lives in the parent harness and governs the child by inheritance; no per-child mirror is needed.
-
Seed or verify parent AGENT_PROGRESS.md after every child succeeds. Under ## Completed Changes, replace the neutral upstream bullet - No completed changes recorded yet. with the seed from examples/parent-progress.seed.snippet. The seed renders: a header sentence naming every child's AGENT_PROGRESS.md path, one [Scaffold] bullet for the parent itself, and one [Scaffold] bullet per child using the generic phrase "as a nested workspace with its own four-file agent contract". Substitute {{ parent_name }}, {{ child_progress_paths_csv }} (comma-separated list like `child_a/AGENT_PROGRESS.md`, `child_b/AGENT_PROGRESS.md`), and {{ child_relative_dir }} per child. If the rendered seed is already present verbatim, treat it as verified. If neither the neutral bullet nor the rendered seed is present and overwrite=false, abort rather than overwriting real parent progress. Do not seed parent progress before the child contracts are installed or verified.
-
Return parent_dir, parent_name, children_created, files_created per node, files_skipped per node, patches_applied per node, and warnings. patches_applied is a legacy output key for applied-or-verified hierarchy changes; it does not mean an existing immutable AGENT.md was patched.