Search⌘ K
AI Features

Part 2: Isolation, Verification, and Security

Build a complete research session with planner, researcher, coder, validator, and auditor roles. Learn to enforce shared-state contracts, verify retrieval and citations, separate source fields from provenance, quarantine risky memory writes, protect team boundaries, roll back errors, and produce a checked incident report.

We'll cover the following...

In the last lesson, we built the project’s prefix file, retrieval file, and sandboxed runner, and each one passed the check that proved its original version. The project still has nothing that runs a whole session. The workers from Chapter 6 and the memory defenses from Chapter 7 exist, but only in their own lessons, on their own examples.

In this lesson, we’ll write the last two building files. agents.py runs five roles (a planner, a researcher, a coder, a validator, and an auditor) over the four-key shared state from Lesson 6.2, using the real retrieval and the real runner. memory.py decides which of the facts they produce may be saved for later sessions, and where. Then we’ll rerun the Chapter 6 and Chapter 7 checks against the assembled session.

Write the workers

The planner, researcher, and coder do the work. Each one is a function that reads and writes the shared state and returns a response in the handoff format from Lesson 6.2. Sharing only this state between workers is the Isolate move from Chapter 1. They now call the project files instead of hardcoded data:

  • Planner: We write the plan into full_plan, overwriting it in place.

  • Researcher: We run retrieve() from Lesson 8.2 and record clues only for text that was actually loaded, plus the deploy-log change.

  • Coder: We send the impact analysis to the sandboxed runner and record its two RESULT numbers, with the log path as their source.

Here are the three workers.

workers.py · Python 3.14.0
from check_runner import ANALYSIS, CSV
from retrieval import retrieve
from roles import respond
from runner import write_and_execute
from shared_state import add_clue, set_plan
QUESTION = "Checkout latency spiked at 14:05. What changed, and what should we roll back?"
QUOTES = {"runbook.first_step": ("note-1", "turn off the newest checkout feature flag"),
"postmortem.recovery": ("note-2", "brought latency back to normal in six minutes")}
def planner(state, indexes):
set_plan(state, "1 research notes and changes; 2 compute impact on the runner; 3 validate; 4 audit")
return respond("plan: research, compute, validate, audit")
def researcher(state, indexes):
found = retrieve(indexes, QUESTION, service="checkout", since="14:00")
for key, (doc_id, phrase) in QUOTES.items():
if phrase in found["loaded"].get(doc_id, ""): # quote only what was actually loaded
add_clue(state, key, phrase, doc_id, "researcher")
time, _, detail = found["changes"][0]
add_clue(state, "change.deploy", f"{time} {detail}", "change_log", "researcher")
return respond(f"{len(found['ordered'])} notes, {len(found['changes'])} changes", list(QUOTES) + ["change.deploy"])
def coder(state, indexes):
result = write_and_execute(ANALYSIS, files={"metrics.csv": CSV}) # the sandboxed runner from 8.2
for pair in result["summary"].split(", "):
key, value = pair.split("=")
add_clue(state, key, value, result["offloaded_to"], "coder")
return result # already in the handoff format

In the code above:

  • Lines 8–9: We list the two quotes the review needs and the note each one should come from.

  • Lines 12–14: We define planner(), which sets the plan and returns a short response.

  • Lines 17–24: We define ...