This document is the agent-readable execution contract for Dark Factory. See the interactive system flow for the human visual.
| Component | Responsibility |
|---|---|
| Planning folder | Stores durable project context, the Task Graph, and dependencies. |
| Dark Factory skill | Teaches an operator agent how to start, monitor, recover, stop, and clean a run. |
| Dark Factory controller | Selects tasks, enforces run limits, supervises delivery, orders merges, reconciles state, and coordinates cleanup. |
| Agent Orchestrator | Creates or restores Codex workers, isolated worktrees, processes, and runtime state. |
| Archon | Runs the repeatable workflows inside each feature worktree. |
| GitHub | Provides pull request, review, CI, mergeability, and merge truth. |
The controller must not edit feature worktrees. Feature changes belong to the assigned Agent Orchestrator worker running Archon.
- A human in a Codex session asks the agent to use the Dark Factory skill.
- The skill inspects the target project and its planning folder.
- The planning folder provides project context and the Task Graph.
- The skill starts the controller with the requested limits and monitors it until the run finishes, stops, or reaches a real recovery boundary.
Dark Factory repeats this loop while work remains:
- Read the Task Graph and runtime state.
- Choose runnable tasks within the active run limits.
- Start or resume the selected work through Agent Orchestrator.
- Observe worker, Archon, pull request, and process evidence.
- Route ready pull requests through the ordered merge queue.
- Re-read project state after every merge or recovery transition.
A task is runnable only when its Task Graph dependencies are complete and it is not already active, completed, or blocked. Agent Orchestrator creates one Codex worker and one isolated worktree for each selected feature. Multiple features may run independently up to the concurrency limit.
Every selected feature follows the same lifecycle inside its assigned worktree.
- Start infrastructure with
wtc. - Start the project servers with the
dev-starthelper. - Plan the selected task with
superpowers:writing-plans. - Implement and validate with
superpowers:subagent-driven-development. - Run frontend QA with
vercel-browserwhen the task affects frontend behavior. - Mark the task complete in the feature branch Task Graph.
- Commit the completed feature with
commit. - Open the pull request with
ship.
The workflow then stops. The Agent Orchestrator worker invokes auto-merge in
prepare mode after auto-feature finishes.
The task remains [ ] on main until its pull request merges. The feature branch
must contain the same task marked [x] before it can enter the merge queue.
auto-merge owns the review-to-merge readiness loop. The caller selects its
mode:
| Caller | Mode | Result when merge-gate confirms readiness |
|---|---|---|
| Agent Orchestrator worker | prepare |
Publish readiness evidence and hand the pull request to Dark Factory. |
| Dark Factory merge queue | finalize |
Merge the pull request on GitHub. |
Both modes run auto-squash and then merge-gate. A retry returns to
auto-squash; it does not restart auto-feature.
- Wait for a 5-minute quiet window.
- Run the
auto-squashArchon workflow. - Check review comments and CI status.
- If nothing needs work, leave the loop and enter
merge-gate. - If work remains:
- Scan the pull request with
pr-scan. - Fix and validate the findings.
- Commit and push the fixes with
commit. - Resolve review threads with
pr-resolve. - Repeat from the quiet-window wait.
- Scan the pull request with
The gate first checks CI, reviews, branch freshness, and mergeability.
| State | Action | Next state |
|---|---|---|
| Ready | Confirm readiness. | Apply the caller-selected prepare or finalize result. |
| Not green | Return to auto-squash for review or CI repair. |
Retry the outer auto-merge loop. |
| Pending | Wait for required pull request checks. | Recheck readiness. |
| Stale | Update the branch. | Wait for pull request readiness, then run the final readiness check. |
| Conflict | Resolve conflicts with fix-merge-conflicts. |
Continue through conflict validation. |
| Unsafe | Preserve the workspace and stop for recovery. | Blocked. |
| Repair failure | Preserve the workspace and stop for recovery. | Blocked. |
Conflict validation follows these steps:
- Determine whether the conflict repair affects frontend behavior.
- Run browser QA with
vercel-browserwhen frontend behavior is affected. - Commit and push the conflict repair.
- Wait until the pull request is ready.
- Run the final readiness check.
The final readiness check routes ready work to the caller-selected mode, review
or pending work back to the outer auto-merge loop, and unsafe or failed work
to recovery.
Dark Factory owns the merge queue. It processes one candidate at a time so each pull request is checked against the latest main branch.
- Collect eligible pull requests. Rebuild the queue after every merge.
- Order candidates by task priority, then task ID.
- Verify the next candidate:
- Confirm the assigned worker completed the task.
- Confirm CI is green, the branch is fresh, and the pull request is mergeable.
- Confirm the task is
[ ]on main and[x]in the candidate pull request.
- Route the candidate:
- Ready: finalize the candidate through
auto-merge. - Recoverable: refresh its recorded pull request state or restore its worker
and resume
auto-merge, then verify the same candidate again. - Unsafe: preserve the queue and workspace, then stop for recovery.
- Ready: finalize the candidate through
- In
finalizemode,merge-gateperforms its final safety check andauto-mergemerges the pull request on GitHub. - Update project state:
- Mark the Agent Orchestrator session as merged.
- Refresh the main checkout.
- Re-read the Task Graph.
- Clean only the feature that just merged:
- development processes;
- temporary browser data;
- Docker resources and volumes;
- local feature branch;
- worktree;
- Agent Orchestrator session.
- If cleanup fails, stop the queue and retry cleanup during recovery.
- If queued pull requests remain, rebuild and reorder the queue before checking the next candidate.
- If the queue is empty, re-evaluate the Task Graph and runtime state.
| Condition | Recovery action | Resume point |
|---|---|---|
| A newer commit was pushed after readiness was recorded. | Refresh the saved pull request state. | Verify the same merge candidate again. |
auto-merge stopped before preparation finished. |
Restore the same Agent Orchestrator worker and resume auto-merge. |
Verify the same merge candidate after fresh readiness evidence is published. |
| A repairable review, CI, stale-branch, or conflict condition remains. | Resume the owning worker and let auto-merge run the required repair path. |
Re-enter the merge queue with fresh evidence. |
| The pull request, worker handoff, or Task Graph is unsafe or inconsistent. | Preserve the workspace and stop. | Resume only after the unsafe state is resolved. |
| Per-feature cleanup failed. | Keep the queue stopped and retry cleanup. | Continue with the next candidate only after cleanup succeeds. |
Recovery restores existing work. It must not create a fresh feature for a pull request that already has an owning worker or worktree.
After the queue is empty, Dark Factory re-reads the Task Graph and runtime state:
| Result | Action |
|---|---|
| Runnable or active work remains | Return to the controller loop and continue within the active run limits. |
| Every planned task is complete | Finish the run. |
| State requires recovery | Preserve state and stop for recovery. |
The run is complete only when all planned tasks are complete and no managed feature still requires scheduling, merge processing, recovery, or cleanup.