Lab 9 — Subagents and background agents
← Back to labs index

Lab 9 — Subagents and background agents

This standalone snapshot teaches harness ask to delegate in two ways:

The exercise matches the Claude Code Lab 9.

What changes

harness/subagents.py keeps the earlier in-process model of a sub-agent task plan with isolated child transcripts.

Learner steps

Start from: labs/app/ at tag lab08-done (see the track guide).

  1. Install this lab and configure Foundry as before:

    python -m venv .venv
    . .venv/bin/activate          # Windows: .\.venv\Scripts\Activate.ps1
    pip install -e '.[dev]'
    cp .env.example .env
    az login
    cd ../app
    

Part A: subagents

  1. Read the two agent definitions in assets/.harness/agents/:

    Agent Tools Job
    security-reviewer list_files, read_file, git_status, git_log (read-only) Find input-validation, secret-handling and money bugs
    test-writer list_files, read_file, write_file, edit_file, run_tests Add missing tests; mark real app bugs xfail instead of “fixing” them

    Install and commit them:

    cp -R ../lab09-background-agents/assets/. .
    git add .harness && git commit -m "chore: add review and test subagents"
    
  2. Delegate:

    harness ask -n delegate
    

    Type /agents, then:

    Use the security-reviewer agent to review cli.py and pricing.py, and the test-writer agent to add tests for any untested CLI command. Then give me one combined summary.
    

    Observe:

    • two run_agent calls, each followed by lines such as Tool: security-reviewer:read_file(...).
    • your Lab 6 permission prompts (and Lab 2B hooks) still apply to the test-writer’s edits.
    • did the reviewer stay read-only? Any tool outside its list is shown as Hook: denied security-reviewer:....
    • /context: the main conversation holds the two summaries, not the files the agents read. Compare with Lab 7’s numbers. Each agent’s full transcript is in the .agents/ folder named in its result.

    /exit when done, review the new tests, and commit what you keep.

Part B: a background agent

  1. Commit your work first: the background agent’s worktree is created from your last commit, so it won’t see uncommitted changes. Keep the worktrees out of git:

    echo ".harness/worktrees/" >> .gitignore
    git add .gitignore && git commit -m "chore: ignore agent worktrees"
    
  2. Start it:

    harness ask --bg -n coverage "Measure which functions in this project have no direct test and write COVERAGE.md with a table of function, file and tested yes/no. Do not modify code or commit."
    harness agents
    

    Keep working in your terminal; check on it with:

    harness logs coverage
    

    Observe: git worktree list shows a second checkout under .harness/worktrees/coverage on a worktree-coverage branch. The agent writes COVERAGE.md there, not in your app/ folder. That isolation is what makes it safe to run alongside your own edits.

  3. When harness agents shows done, bring the result over and clean up:

    cp .harness/worktrees/coverage/COVERAGE.md .
    rm .harness/worktrees/coverage/COVERAGE.md
    harness rm coverage
    
    Copy-Item .harness\worktrees\coverage\COVERAGE.md .
    Remove-Item .harness\worktrees\coverage\COVERAGE.md
    harness rm coverage
    

    Try harness rm coverage before removing COVERAGE.md from the worktree: it refuses, so you don’t lose work by accident.

    Subagents and background agents multiply token cost. Check /cost, or harness trace on the background agent’s trace file (shown by harness logs), and keep tasks small.

  4. Record
    • Which tools each subagent actually used, and any that were denied.
    • Main-context size after delegation vs Lab 7’s /context numbers.
    • Whether the background agent finished, and how you’d know if it hadn’t.
  5. Checkpoint. Commit the useful results (new tests, COVERAGE.md), then in app/:

    python -m pytest -q
    git tag lab09-done
    
  6. Back in the lab folder, run the offline checks and inspect the declared capabilities:

    pytest checks/
    harness lab-info
    

harness ask keeps skills and MCP (Lab 8), tracing (Lab 7), permissions (Lab 6), file memory (Lab 5), plan mode and todos (Lab 4), sessions (Lab 3) and the Lab 2B tools, built-in policy, project hooks and rules. This is a teaching harness, not a sandbox: only use it on the practice app.