Sub topic 11
Collecting and merging subagent results
This chapter assumes you know how an orchestrator starts subagents, what a delegation brief is, and basic Git branches. It also assumes you can read short Python.
Why aggregation is its own step
Each subagent works in its own context and returns a short report. The orchestrator now holds several reports and possibly several sets of changes. It must decide which parts are usable, whether they fit together, and what to tell the user. Skipping this step is a common error. The orchestrator forwards the first report it reads, or concatenates all of them without checking.
Ask for a structured return
Aggregation is easier when every subagent replies in the same shape. The brief should name the fields. A useful set is:
status:ok,failed, orpartial.summary: two or three sentences on what changed.files: the paths the subagent touched.evidence: the command it ran to check its work and the result.
The orchestrator can then process the replies as data. It does not need to interpret free-form prose.
Separate results by status
Handle successes, failures, and partial results differently. A failed task should be reported or retried. It should never be silently dropped, because the user would then believe the work was done. The orchestrator in the earlier example already records failed for any task that raised an exception. The next step is to look at the results as a group.
from collections import defaultdict
def aggregate(results: list[dict]) -> dict:
ok = [r for r in results if r["status"] == "ok"]
not_ok = [r for r in results if r["status"] != "ok"]
owners = defaultdict(list)
for r in ok:
for path in r.get("files", []):
owners[path].append(r["name"])
conflicts = {p: names for p, names in owners.items() if len(names) > 1}
return {"ok": ok, "not_ok": not_ok, "conflicts": conflicts}
if __name__ == "__main__":
results = [
{"name": "update-docs", "status": "ok", "files": ["README.md"]},
{"name": "add-tests", "status": "ok", "files": ["tests/test_cli.py", "README.md"]},
{"name": "lint-fix", "status": "failed", "files": []},
]
print(aggregate(results))Here README.md appears in two reports, so it is listed under conflicts. The brief for add-tests limited its scope to tests/test_cli.py. Editing the README means the subagent went outside its scope, and the orchestrator should look at that edit before accepting it.
Merge the work
If the subagents worked in separate branches or worktrees, merge them one at a time. Run the tests after each merge, not only at the end. When a test fails, you then know which branch caused it.
Report back
The final message to the user should state three things: what was done, what failed or was skipped, and what you checked. Quote the evidence from the reports rather than restating it as a claim. A line such as "pytest passed after merging both branches" is checkable. "Everything looks good" is not.
Exercise
Two subagents both report ok, and both list src/cli.py under files. Their briefs gave each of them a different file to edit. What should the orchestrator do before merging, and why?
Show answer
Treat the overlap as a signal that at least one subagent went outside its scope. The orchestrator should inspect both changes to src/cli.py, decide which one to keep or how to combine them, and then merge and run the tests on the combined result. Merging blindly could let one edit overwrite or break the other.