Build loop

From xoai/sage · View interactive version

Build loop is an AI agent skill from the GitHub repository xoai/sage. Drives task-by-task execution from an approved plan with quality gates between each task. Reads the plan, finds the next incomplete task, dispatches implementation, validates, updates progress, and continues. Use after a plan is approved and the user says "go", "start building", "execute the plan",

How to use this skill

Skill file

<!-- sage-metadata

cost-tier: sonnet

activation: auto

tags: [execution, orchestration, build, loop]

inputs: [plan, codebase-context, constitution]

outputs: [implementation, test-suite, updated-plan]

requires: [implement, tdd, coding-principles, quality-review, spec-review, verify-completion, scope-guard]

-->

Build Loop

Execute the plan, task by task, with quality gates between each one.

This is the main execution engine for BUILD and ARCHITECT modes.

Core Principle: Autonomous but supervised. The agent works through tasks

independently, but pauses at checkpoints and escalates when stuck. A beginner

should be able to say "go" and watch tasks get completed, knowing they'll be

asked before anything surprising happens.

When to Use

After a plan is approved. The user says "go", "start building", "implement",

or approves the plan at the checkpoint.

Process

Step 1: Read the Plan

Load .sage/work/<active-feature>/plan.md. Parse:

If ALL tasks are done → skip to Final Review.

If NO plan exists → error: "No approved plan found. Run the plan skill first."

Step 2: Show Status and Start

Sage: Plan: [feature name]

Tasks: [total] tasks in plan. Next: Task [N] — [task name].

Starting Task [N]. I'll commit after each task and check quality.

Say "pause" anytime to stop between tasks.

Step 3: Execute Task

For the current task:

  1. Load coding principles — read

sage/core/capabilities/execution/coding-principles/SKILL.md.

Apply the 7 universal principles to every line written during

this task. Announce: "Loading coding principles for implementation."

  1. Read the task spec from the plan (files, action, test, verify)
  2. Implement using TDD (tdd skill):

(on resume, a test the prior session already recorded as

written-and-failing is not re-watched — tdd § "Inherited red")

  1. Self-review using the implement skill's checklist
  2. Commit with semantic message — on the initiative branch

(git-discipline: created at the workflow's Branch Setup step;

commits never land on the default branch unless the user

declined branching there)

The scope-guard skill monitors throughout — flags if implementation

drifts beyond the task spec.

Step 4: Run Quality Gates

The deterministic script gates run after EVERY task. This is NOT optional.

Do not skip them because:

Script gates run. Every task. No exceptions.

After each task, run the quality gate sub-workflow:

Gate 1: spec-review → Does implementation match the task spec?

Gate 2: constitution → Does it violate any project principles?

Gate 3: quality-review → Clean code, security, maintainability?

Gate 4: hallucination → All imports, APIs, versions real and correct?

Gate 5: verification → Tests pass? Feature works as expected?

The judgment gates (1–3) reach their independent verdict via one combined

reviewer by default (gate_review); the script gates (1-script, 4, 5) run

--quiet. See sage/core/workflows/sub-workflows/quality-gates.workflow.md.

On a RESUMED cycle's close-out (this session started from `manifest.py

resume`), follow the resume close-out economy

(sage/core/workflows/_shared/cycle-protocol.md § "Resume close-out economy"):

run the deterministic script gates per remaining task for fast failure, but

**defer the adversarial sub-agent review to ONE combined review over the whole

cycle diff at close-out** — the first session already reviewed everything it

built; re-running per-task adversarial dispatches over a small delta is the cost

the profile targeted. On a first-session build, run the full per-task sequence

below.

If gates pass: Move to next task. Plan checkboxes updated in bulk

at the completion checkpoint (Rule 7).

If a gate fails:

When changing approach, log it in the initiative's scratch notes:

Append to .sage/work/[initiative]/scratch.md:

```

approach-[N]: [what was tried] — [why it failed]

```

If scratch.md has 3+ approaches for the same task, this is a

gotcha trigger — the sage-self-learning skill MUST store the finding

with WHEN/CHECK/BECAUSE format before continuing.

Sage: Task [N] failed quality gates:

Gate 3: Security — SQL injection risk in [file]

Gate 5: Verification — test_user_create fails

[1] Fix and continue

[2] Discuss the approach

Sage: Task [N] has failed Gate [X] three times.

The task spec may be ambiguous or contradictory.

Issue: [specific problem]

[1] Revise the task spec and retry

[2] Skip this task and continue with others

[3] Pause and discuss the approach

Step 5: Inter-Task Checkpoint

After every task (or every 3 tasks for long plans), show brief progress:

Sage: Task [N] complete. Continuing to Task [N+1] — [name].

Say "pause" to stop.

In Desktop mode, the session's task list is the progress display: mark the

task done there and skip this line

(sage/core/system-skills/sage-checkpoints/references/desktop-mode.md).

Batch the bookkeeping (batch_bookkeeping, default true): defer memory

writes and non-essential prose to the completion/session-break checkpoint rather

than emitting them per task — and when that checkpoint arrives, apply ALL of it

with one command, not incremental edits:

python3 sage/runtime/tools/manifest.py close-out <manifest.md> \
  --summary "..." --next-step "..." --decision "..." --complete-task N

gate_state and updated: are mechanical (the sync hook and every

advance/sync/close-out write own them — never hand-edit either). Keep the

one-line progress note above; everything else waits for the close-out command.

The manifest bridge at an [N]/context-budget break is NOT bookkeeping and is

never deferred (cycle-protocol.md § Session-break contract) — write it with

the same one command.

On a resume close-out, two more of the economy's levers apply here (see

cycle-protocol.md § Resume close-out economy): with resume_memory: skip

(default) the memory store/search is skipped — the brief already carries the

context and L2 measured its value at this horizon as null; and with

resume_test_cadence: lean (default) each step's GREEN runs the targeted test,

with the full suite run once at close-out (Gate 5) rather than per task.

For tasks marked [P] (parallelizable), note: "Tasks [N] and [M] are

marked parallel-safe. Running sequentially — concurrent dispatch happens

ONLY through --subagents --parallel lanes (subagent-execution

sub-workflow), never ad-hoc from the inline loop: lanes bring the

scheduler, worktrees, caps, and single-writer bookkeeping that ad-hoc

concurrency lacks."

Step 6: Final Review

After all tasks complete:

  1. Run full quality gates on the COMPLETE implementation (integration check)
  2. Verify all tests pass together (not just individually)
  3. Check for any TODO/FIXME markers that shouldn't be there

Present the result:

Sage: Implementation complete.

Feature: [name]

Tests: [count] passing

Commits: [count]

Quality: All 5 gates passed on final review.

[A] Approve — accept the work; branch stays unmerged

[M] Merge to [default] — user-gated merge per git-discipline

[R] Revise — needs changes

[P] PR — draft the pull-request description from the spec

🔒 MANDATORY CHECKPOINT: Wait for human decision.

[M] is the ONLY merge path — [A] never merges. Omit [M] when not a

git repository, when no initiative branch exists, or in a worktree session

(git-discipline, "Worktree sessions" — the branch goes to review instead).

Sage never pushes or opens the PR itself: [P] writes the description for

the user (or their tool) to open it with.

Step 7: Wrap Up

Based on human's choice:

sage/core/capabilities/execution/git-discipline/SKILL.md —

preconditions, the merge itself, conflict handling, and the

deletion offer all live there; do not restate them here. Omit

[M] when not a git repository, when no initiative branch exists, or

in a worktree session (git-discipline, "Worktree sessions").

to open the PR with — Sage never pushes or opens it (git-discipline)

Update the initiative's decisions.md

(.sage/work/[initiative]/decisions.md) if significant —

cross-initiative decisions go to the global .sage/decisions.md:

# Progress

Mode: idle
Feature: [YYYYMMDD-slug] (completed)
Phase: done
Next: "Tell me what to build next"
Updated: <timestamp>

Fallbacks

| Situation | Action |

|-----------|--------|

| Implementation fails to compile | Run systematic-debug, fix, retry |

| Test can't be written (untestable design) | Flag to human, suggest interface simplification |

| Task is larger than expected | Report: "Task [N] is bigger than planned. Split into [A] and [B]?" |

| Discovered bug in existing code | Report: "Found unrelated bug in [X]. Note for later or fix now?" |

| Context window getting full | Commit current work, save state, suggest new session |

| All tasks done but integration broken | Create an "integration fix" task, add to plan, execute |

Rules

MUST (violation = lost work or broken trust):

the adversarial review into one combined dispatch on resume close-out is not

skipping — the deterministic script gates still run every task and the

whole-change independent review still happens; what stops is re-reviewing, from

scratch, work a prior session already reviewed.

SHOULD (violation = suboptimal experience):

MAY (context-dependent):

review already covered everything.

by the --parallel lane machinery (opt-in, code-scheduled).

Failure Modes

a spec exists, offer to regenerate the plan from it. If neither exists,

suggest starting with elicitation.

write a session bridge note, save the plan with current progress, and

suggest a new session. Never lose work to context limits.

than planned, STOP. Report to the user: "Task N is bigger than expected.

Split into subtasks?" Don't silently expand scope.

and report: "Gate [X] keeps failing on task [N]. The issue is [description].

Should I try a different approach or skip this gate with your approval?"

approval, wait. Don't proceed to the next task. The checkpoint exists for

human verification.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.