# Lab 26: the rules still hold (/building-ai-workers/context-engineering/lab)

---
type: Document
title: "Lab 26: the rules still hold"
description: "The lab for Chapter 26, in two sittings: a check by code that fails on the drifting run, the rule and the work's state given a home, tool output cut, then a summary, a subagent and the port."
status: stable
order: 326.9
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T08:16:25Z
chapter: '26'
part: III
expert_status: manager
concepts: []
last_verified: '2026-11-30'

sources:
  - id: anthropic-compaction-threshold
    title: Compaction at a token threshold (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/compaction-threshold
  - id: anthropic-compaction-on-demand
    title: Compaction on demand (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand
  - id: anthropic-context-editing
    title: Context editing (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/context-editing
  - id: claude-code-best-practices
    title: Best practices for Claude Code (verified 2026-11-29)
    resource: https://code.claude.com/docs/en/best-practices
  - id: anthropic-cookbook-context
    title: 'Context engineering: memory, compaction, and tool clearing (Anthropic cookbook, verified 2026-11-29)'
    resource: https://platform.claude.com/cookbook/tool-use-context-engineering-context-engineering-tools
  - id: anthropic-cc-context-window
    title: Explore the context window (Anthropic, verified 2026-11-29)
    resource: https://code.claude.com/docs/en/context-window
  - id: anthropic-memory-tool
    title: Memory tool (Anthropic, verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool
  - id: anthropic-managed-agents-reference
    title: Managed Agents reference, event types (Anthropic, verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/managed-agents/reference
  - id: codex-agents-md
    title: Custom instructions with AGENTS.md (OpenAI, verified 2026-11-30)
    resource: https://learn.chatgpt.com/docs/agent-configuration/agents-md
  - id: opencode-instructions
    title: Instructions (OpenCode docs, verified 2026-11-30)
    resource: https://opencode.ai/docs/instructions/
  - id: opencode-compaction
    title: Compaction (OpenCode docs, verified 2026-11-30)
    resource: https://opencode.ai/docs/compaction/
  - id: claude-agent-sdk-subagents
    title: Subagents in the SDK (Claude Agent SDK, verified 2026-11-29)
    resource: https://code.claude.com/docs/en/agent-sdk/subagents
generated:
  at: 2026-11-30T08:16:25Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

<!-- OPEN: the lab's dates, the progress file's name and place, and how `find_vendor` is asked for fewer fields are in specs/labs/part-2-facts.md (rows 26.1, 26.8 and 26.10), still marked proposed. The open invoices of April 30 (O26.1) and the long AP notes that fill the window (O26.2, decision E8) are the kit's proposals, R1 to R3 in the labs repository's `tools/brightline-lab-ch26/NOTES.md`, and are not yet in the facts file -->

<!-- OPEN: no run is recorded yet, and the page now says so after the setup step. Every count on this page that a run must supply is left out: the turn of the summary, the tokens at that turn, how many invoices the check fails, each run's cost, and the trigger values in Tasks 2 and 3. If three live tries do not lose the rule, Friday's trace is written by script (decision E10), and the note after the setup step must then say so -->

You build the check that Dave Kowalski's month-end review needed, and watch it fail on Friday's run. You give the rule and the state of the work a home outside the window. Then you cut, one cut at a time, and run the same check after each. The lab takes 2 sittings, and a free plan can spread them over days.

| Sitting | Do it after | Tasks |
| --- | --- | --- |
| Sitting 1 | 26.1 to 26.6 | Setup, Tasks 1 to 3 |
| Sitting 2 | 26.7 and 26.8 | Tasks 4 to 6 |

### Before you start

> [!IMPORTANT]
> The lab's files are not published yet. The download link for `brightline-lab-ch26.zip` and the answer keys below will work once they are. Until then, read the tasks with the chapter.

**The download.** Everything starts from `brightline-lab-ch26.zip` ([download it here](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26.zip)). It holds the `brightline-ap` repository at its tag `ch25`, with this lab's files added, so you need not have done Lab 25.

**What you need.** Node, Python with uv, git, the GitHub CLI, and Claude Code, Codex and OpenCode, each signed in. Your staging key for the Claude API goes in `.secrets/anthropic.env`. No key passes through a chat.

**The job.** It is Monday, May 3, 2027, 8:40 a.m. On Friday, April 30, at 4:10 p.m., Dave Kowalski asked for the review in these words, "Tell me which open invoices are a problem, and why, with the policy line for each." You started one session with it, on a $1.00 budget, with a summary set at a token threshold, and left. No check by code has tested the review on every invoice, because the portal's check by code runs on the weekday triage only.

**How a task works.** Start a fresh session with the named coding agent in `brightline-ap/`, and give it the task's prompt in plan mode. Write your prediction before the agent shows the result. Check the evidence against **What good looks like**, then download the answer key. End with a line in `ports/ch26.md`.

<!-- OPEN: the cost of the live steps is not measured yet. Each live run of the session has a budget of $1.00, and Tasks 2 to 5 make at least one live run each -->

**Set up (Claude Code).** Unzip the folder, then give the agent this prompt.

> Read `README.md` and `AGENTS.md`. Put this folder under git with a main branch, in one first commit, "Start from the end of Chapter 25", and tag it `lab26-start`. Install the packages for both languages, as `README.md` says. Run every test that needs no key. Then replay Friday's recorded run from `traces/month-end-drift.jsonl`, with no key and no network. Show me the request word for word, the number of turns, the number of open invoices, the run's cost, and the review's first and last entries. Finish by showing me that git tracks nothing in `.secrets/`.

**What good looks like:** one commit at the tag `lab26-start`, and every test green. The replay shows Dave's sentence, about 60 turns, and a cost under the $1.00 budget. Every entry of the review has a reason. `docs/chapter-26/` holds the concepts as text for your agents.

> [!NOTE]
> No run is recorded yet. Friday's run, `traces/month-end-drift.jsonl`, is not in the kit, so its replay stops and says there is no recorded run. The setup's replay, Task 1's steps 1, 2 and 5 and Task 2's steps 1 to 5 wait for it. Two files in the kit are scripted, and each says so in its first lines: the hosted session's events and a response whose summary is empty, both read in Task 4. Every other step can be done now.

**Replay and live runs.** `npm run replay` reads a run back from its trace, with no key and no network. A live run is one session, `npm run month-end`, with `npm run connector:month-end` running in another terminal. That connector serves `data-month-end/`, Brightline's data on Friday, April 30. The session's limits, its model and its trigger are in `sessions/month-end.config.json`. Fill in its prices from the pricing page first. A live run prints its budget and sends nothing until you add `--yes`. Your agent runs these commands, and you approve each live run.

<!-- EDITOR: the kit's names for the data, the connector, the trace reader and the session's settings are taken from the kit's proposals R4 and R5 (`tools/brightline-lab-ch26/NOTES.md`): `data-month-end/` beside an unchanged `data/`, `npm run connector:month-end`, `npm run replay`, `npm run month-end` and `sessions/month-end.config.json`. The other option: one data folder, `data/`, changed for April 30, which would break the tests of earlier labs that read it. -->

### Sitting 1: the check, the cause, the first cut

**Before you start, answer in a line each:** What are the two causes of drift (26.1)? Which of the three kinds can be fetched again (26.3)? **After this sitting, you can:** write a check by code for a rule that must hold on every item, read from a trace what filled the window and what a cut removed, and give a rule and the state of the work a home outside the window.

**New words in this sitting's prompts.**

| Word in a prompt | What it means |
| --- | --- |
| Replay | Read a run from its recorded trace, with no request to the model |
| Scripted session | The file `sessions/month-end.md`: the same request and the same steps each time, so two live runs can be compared |
| Tokens by part | The window's tokens, split into the instructions, the tool definitions, the request, the tool results and the worker's own turns (26.2) |
| Trigger | The number of input tokens at which the API clears or summarizes |
| Scripted file | A file written by a script, or by hand in the shape a vendor documents, and not recorded from a run. Its first lines say so |
| Owned file | A file only you may change. The agent proposes a change and waits. The worker's instructions, in `prompts/ap-worker.v<N>.md`, and `AGENTS.md` are owned files |
| Your own copy | The array of the whole conversation that your loop keeps ([21.5](../agent-loop-by-hand/the-conversation-is-an-array-you-own.md)) |

#### Task 1. The end check, built first (Claude Code)

*Goal:* a check by code that tests Dave's rule on every invoice of a review, with tests of its own, and its verdict on Friday's run.

*Where:* `src/hosted/check-triage.ts`, the check the weekday triage already has. Nothing in this task sends a request. Steps 1, 2 and 5 read Friday's run, which is not recorded yet. Steps 3, 4 and 6 need no run.

**Predict:** the worker's instructions already ask for a policy citation, and the portal has a check for one. If you run that check on Friday's review, does the review pass?

**In plain words.** You cannot say whether a rule held until something tests it. So the check comes before the diagnosis and before every fix. You use it at the end of each task that follows.

> Read 26.1 in `docs/chapter-26/`.
>
> 1. Replay Friday's run from `traces/month-end-drift.jsonl`. Show me the request, and the review as a table: one row for each open invoice, with the worker's decision, its reason and its policy line, in the order the worker decided them.
> 2. Quote the sentence in the newest version of the worker's instructions, `prompts/ap-worker.v2.md`, that asks for a citation. Run the portal's citation check, `citesPolicyLine()`, on the review. Stop there and wait for my prediction. Then show me its result.
> 3. Extend `src/hosted/check-triage.ts` so that it can check a review as well as a morning report. It stays plain code with no model call. Given a run's events, it fails a review if any invoice that `list_open_invoices` returned has no entry, if any invoice the review calls a problem has no policy line written `(file.md:N)`, or if any policy line was not returned by `search_policy` in that run. It returns the failures in plain words, one line for each invoice. Add a script, `check:review`, that runs it on any trace.
> 4. Write tests for it from three fixtures in `tests/fixtures/review/`, which you make from the lab's data: one review that passes, one with a problem invoice that has no line, and one with a line that `search_policy` never returned.
> 5. Run the check on Friday's run. Show me each invoice in the order it was decided, with pass or fail, and the turn at which the worker decided it.
> 6. Add a line to `ports/ch26.md`: my prediction, what the old check said, and what the new check said.

**What good looks like:** `citesPolicyLine()` passes Friday's review. It asks whether a citation is there, and the first invoices carry one. The new check fails the review. Its three tests pass. In the table of step 5, every failure comes late in the run, after one turn, and no invoice decided before that turn fails. Your prediction: it passed. One citation anywhere in an answer satisfies the instructions' sentence, and "for each" was Dave's.

<!-- OPEN: how many invoices the check fails, and of how many, comes from the recorded run. The kit's notes (NOTES.md, "Not settled", 2) expect ok invoices between the failing ones, so the failures come after one turn but are not every last row -->

<!-- EDITOR: the review check is added to `src/hosted/check-triage.ts`, in TypeScript only, and runs as `npm run check:review` (the kit's proposal R7). Step 3 names the script once, where the agent builds it, as Lab 24 names `npm run eval`. The other option: name no script in the prompt and let `AGENTS.md` hold it. -->

**Checkpoint:** the check fails on Friday's run, and its three fixture tests pass.

**Saves:** the review check in `src/hosted/check-triage.ts`, its three fixtures and tests, and your first line in `ports/ch26.md`.

**Check it now.** Download [Task 1's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26-key-task-1.md).

#### Task 2. Which kind had no home (Claude Code)

*Goal:* the cause of the drift, read from the trace and named. Then the rule in the worker's instructions, the state of the work in a progress file, and the check passing on a live run.

*Where:* the trace first. Then the worker's instructions, and your own loop in `src/loop/`, on the model the kit pins. Steps 1 to 5 read Friday's run, which is not recorded yet. Steps 6 to 8 need no run.

**Predict:** two sentences asked for policy lines on Friday. One is in the worker's instructions, and one is Dave's request. At the last invoice, which of the two was the model still reading?

**In plain words.** You read the trace for two things: what filled the window, and what was taken out of it. Then you sort what the window held into the three kinds, and ask of each one where else it lives. Nothing is cut in this task.

> Read 26.2 and 26.6.
>
> 1. From the recorded trace, show me tokens by part at four moments: the first turn, a turn midway, the last turn before anything was removed, and the turn after that. Add up all three input fields of `usage` for each. Name the part that grew most, and the tool whose results fill it.
> 2. List every event in the trace where something was removed from what the model sees: its turn, what kind of event it is, and the tokens before and after. Beside that list, put the turn of the first invoice the check fails.
> 3. Show me the summary's text, whole. Quote the sentence where it states the task. Stop there and wait for my prediction.
> 4. Make a table of what the window held on the turn before the summary. Give one row to each rule the run had to follow, including the rule against paying and each part of Dave's sentence. Give one row to the decisions made so far, and one to each tool's results. For each row, ask me which kind it is: a rule, the state of the work, or evidence. Then fill in a last column yourself: where else it existed on that turn. The choices are the worker's instructions, the connector, or nowhere.
> 5. Name the failure with one of the names in [24.7](../evaluating-ai-workers/naming-the-failure.md), and say which of 26.1's two causes this run shows.
> 6. The worker's instructions are an owned file. Propose one sentence for them: every invoice that a review calls a problem carries its own policy line. Show me the diff and wait. Save the result as a new file with the next version in its name, `prompts/ap-worker.v3.md`, and leave the old file as it is. Then build `agents/ap-worker.md` from it, as `AGENTS.md` says.
> 7. Add the memory tool to my own loop, in `src/loop/memory.ts`. My handler maps `/memories` to one folder for one person and one company, under `data/memory/`. For this review that is Dave's folder at Brightline, `data/memory/org_brightline/dave/`. It refuses any path outside it. Write two tests: a path that climbs out of the folder is refused, and one person's run never reads another person's folder.
> 8. Add to the worker's instructions, as a second diff for my approval, saved as the version after that: before the first invoice, write the request word for word at the top of `/memories/month-end-progress.md`. After each decision, add one line: the invoice, the decision, its policy line if it is a problem, and the values you decided on, such as the vendor's terms or the purchase order's amount.
> 9. Run `sessions/month-end.md` live on my own loop, with the summary's trigger as it was on Friday. Tell me the budget and how many requests you expect before you send them. Then run the check, and show me the progress file.
> 10. Show me tokens by part and the run's cost, beside Friday's. Add both to `ports/ch26.md`, with my prediction.

**What good looks like:** tool results are the part that grew. The trace holds one removal, a summary, and the first failing invoice comes straight after it. The summary states the task and leaves out "with the policy line for each". In your table, the rule against paying lived in the instructions, and the worker kept it to the end. "For each" and the decisions lived nowhere else. The tool results lived in the connector. The name is context loss, and the cause is removal. On the live run a summary still arrives, and the check passes on every invoice. The progress file opens with Dave's sentence and has one line for each invoice, with a policy line on each problem. The tokens are not lower than Friday's, because nothing has been cut yet. Your prediction: the instructions' sentence. Dave's was in the turns that the summary replaced.

<!-- EDITOR: the progress file is in Dave's folder, `data/memory/org_brightline/dave/` (the kit's proposal R9), because Dave asked for the review. The other option: a folder for the AP team's run, such as one for Ana Morales, who reads the other reports. -->

**Checkpoint:** the check passes on a live run, with no cut made.

**Saves:** the next version of the worker's instructions, `src/loop/memory.ts` with its two tests, the progress file from the run, and the table of step 4 in `ports/ch26.md`.

**Check it now.** Download [Task 2's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26-key-task-2.md).

#### Task 3. Cut the evidence, at the tool and in the API (Claude Code)

*Goal:* `find_vendor` gives the review only what the review uses, and the API clears old tool results from what the model sees. The check passes after each cut, and you have read what each one saved.

*Where:* the `find_vendor` tool of the practice connector, `py/brightline_ap/practice_connector.py`, and its definition in `tools/ap-tools.json`. Then the request your loop sends.

**Predict:** the API clears an old tool result. Straight after, you print your own copy of the conversation. Is that result still in it?

**In plain words.** Evidence is the kind that can be fetched again, so it is the kind you may cut. You make it smaller as it arrives. Then you let the API drop the old results. You never delete from your own copy by hand. After the cut at the tool the session is much shorter, so the clearing needs a trigger of its own, below the session's new size.

> Read 26.3.
>
> 1. Show me one `find_vendor` result from Friday's trace, with its tokens. Mark each field the review used in a decision.
> 2. Other work still needs every field, so today's result stays the default ([17.2](../connectors-and-mcp/a-tool-is-its-description.md)). Propose the smallest change that lets a caller ask for only the fields it needs, with today's result as the default. Show me the diff, with a test that a caller who asks for nothing special gets what it got before. Wait for my approval.
> 3. Run `sessions/month-end.md` live. Run the check. Show me tokens by part and the cost, beside Task 2's run.
> 4. Now turn on the clearing of old tool results in the request my loop sends, with the setting's name and its beta header as 26.8 gives them. Keep the last 3 tool uses. Leave the memory tool's results out of the clearing. Propose a trigger low enough to clear at least once in this run, and a minimum number of tokens to clear each time, and tell me what each clearing does to the cached part of the request. Wait for my approval. Change nothing in my own copy of the conversation.
> 5. Send one request that carries both the clearing and the summary by threshold. Show me the status code. If the API refuses it, show me the error's text and stop.
> 6. Run the session again. For each clearing, show me the turn, the tool uses and the tokens cleared, as the response reports them. Stop there and wait for my prediction. Then show me the length of my own copy before and after the first clearing, and the text the model saw in place of a cleared result.
> 7. Run the check. Then pick one invoice whose `find_vendor` result was cleared, and show me its line in the progress file.
> 8. Show me tokens by part, the cache read tokens and the cost for three runs side by side: Task 2's, after the cut at the tool, and after clearing. Add the table to `ports/ch26.md`.

**What good looks like:** the review used the vendor's terms, and not the bank digits or the AP team's note. After the cut at the tool, the tool results part is smaller on every turn, and the check passes. With clearing on, each cleared result is replaced by placeholder text that tells the model it was removed.[^anthropic-context-editing] Your own copy has the same length before and after. The check still passes. One request carries both the clearing and the summary by threshold.[^anthropic-cookbook-context] The cleared invoice's line in the progress file still holds the value the decision was made on. The cache read tokens fall on the request after each clearing, because a clearing invalidates the cached part of the request.[^anthropic-context-editing] No summary arrives after the cut at the tool. By the kit's estimate the whole session is now about 14,000 input tokens, and a summary by threshold takes no trigger below 50,000.[^anthropic-compaction-threshold] Task 4 deals with that. Your prediction: yes. The API clears what the model sees, and "Your client application maintains the full, unmodified conversation history."[^anthropic-context-editing]

<!-- EDITOR: the kit's reference cut is an optional input, `fields`, a list, with today's result as the default (decision E11 and the kit's proposal R8). It is in the practice connector and `tools/ap-tools.json` only, because the staging connectors' tests need a database. Step 2 still leaves the design to the reader's approval, and the key takes another name. The other option: a setting in the connector for the review's caller. A new tool is ruled out, because 17.2 and Lab 23 fix the worker's four tools. -->

<!-- OPEN: the clearing's trigger and minimum wait for a run. The kit's proposal R10 (trigger 20,000, clear at least 5,000) is above the session's size after the cut, about 14,000 tokens by the kit's estimate, so it would never clear. The kit's value must come down below that size before a run -->

**Checkpoint:** two cuts are made, the check passed after each, and your table shows what each one saved.

**Saves:** the change to `find_vendor` and its test, the clearing setting in the loop's configuration, and the three-run table in `ports/ch26.md`.

**Check it now.** Download [Task 3's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26-key-task-3.md).

**Checkpoint 1: a home first, then a cut.** Before Sitting 2, check that you can do these without looking back.

- Say why the old citation check passed Friday's review, and what the new check tests that it did not.
- Name the three kinds, and say where each one lived on the turn before the summary.
- Say what the API cleared, what it left in your own copy, and what the clearing cost you.

### Sitting 2: the summary, the subagent and the port

**Before you start, answer in a line each:** What must a summary's instructions say (26.4)? What does a subagent receive from its parent (26.5)? **After this sitting, you can:** write a summary's instructions and read what the summary kept, move side work to a subagent whose own prompt carries the rules it needs, and test on another harness whether a rule in its instruction file holds after a compaction.

**New words in this sitting's prompts.**

| Word in a prompt | What it means |
| --- | --- |
| Summary block | The block the API returns in place of the older turns. It goes back in later requests exactly as it came (26.4) |
| By threshold, on demand | The API summarizes when the input reaches your trigger, or your code asks for a summary in a request of its own |
| Fixture | A saved response that a test feeds to the code, so the test needs no key |
| Parent | The worker that starts a subagent |
| Instruction file | The file a harness loads into its requests: `CLAUDE.md` in Claude Code, `AGENTS.md` in Codex and OpenCode[^codex-agents-md][^opencode-instructions] |

#### Task 4. The summary: what it kept, and what reads the file again (Claude Code)

*Goal:* the summary has instructions that you wrote, you have compared its text with the progress file, and you know what brings the progress file back into the window afterward. One summary on demand works in a request of its own, and a summary that does not arrive is handled.

*Where:* your own loop in `src/loop/`, and one look at a hosted session's events. Step 1 reads Task 2's live run. This task's own live runs turn off Task 3's cut at the tool and its clearing.

**Predict:** the API writes a summary during one of your requests. Do that response's top-level `input_tokens` and `output_tokens` include what the summary cost?

**In plain words.** A summary is a shorter copy, written by the model, of whatever it is given. Since Task 2, nothing that matters lives only in the turns it replaces. Here you tell it what to keep, read what it kept, and find out who opens the progress file again. A summary by threshold needs a trigger of at least 50,000 input tokens, and after Task 3's cut the session never gets there. So in this task `find_vendor` sends every field again, the vendors' long notes fill the window as they did on Friday, and the summary is the one cut you test.

> Read 26.4.
>
> 1. Show me the summary from Task 2's run beside the progress file from the same run. For each line of the progress file, say whether the summary holds it, holds it changed, or does not hold it.
> 2. Write the summary's instructions in `prompts/month-end-summary.v1.md`. They replace the default prompt whole, so ask me first what the summary must retain, one item at a time, and write only what I answer. End them by telling the model not to call tools.
> 3. Make a copy of the session's configuration for this task, with the clearing left out, and have my loop send each `find_vendor` call with no `fields`, so it returns every field as on Friday. Keep the trigger at 50,000. Run `sessions/month-end.md` live with these instructions and this copy. Show me the new summary. Stop there and wait for my prediction. Then show me the usage of the request in which the summary was written, with every entry of `usage.iterations`, and what the summary cost.
> 4. Find out what reads the progress file again after a summary. With step 3's copy of the configuration, start two runs from the same turn before the summary. In the first, the worker's instructions tell it to view the progress file after any summary. In the second, they do not, and my loop adds the file's text to the next user turn when a summary block arrives. For each, show me the first three turns after the summary, and whether the file's text was in the window.
> 5. Ask for one summary on demand, in a request of its own that carries no clearing. Read `stop_reason` before you look for the block. Then replace the summarized messages in my own copy with the block, exactly as it came, and change nothing else. Write two tests that fail if a later request leaves the block out, or keeps the summarized messages after it.
> 6. Write a test from the fixture `tests/fixtures/compaction/empty-summary.json`, a scripted response whose summary block has no content. My loop records that the summary did not arrive, keeps my own copy as it was, and carries on.
> 7. The hosted worker is summarized by its platform. From the hosted session's events in `traces/hosted-month-end-events.jsonl`, which are scripted and say so in their first line, list the event types, and show me any event that marks a compaction.
> 8. Run the check on step 3's run. Add tokens by part, the cost with the summary's own cost shown apart, and the answer to step 4 to `ports/ch26.md`.

**What good looks like:** your instructions name what to retain and end by forbidding tool calls, because a summary can fail to arrive when the model calls a tool instead.[^anthropic-compaction-threshold] The new summary holds each item you named. Its cost appears under `usage.iterations`, and you added it to the run's total. Your notes say which of step 4's two runs had the file's text in the window after the summary, and you kept that one. The memory tool's page promises a look at the memory folder only "before starting a task".[^anthropic-memory-tool] The summary on demand came back in a request with no clearing, since one request cannot carry both.[^anthropic-compaction-on-demand] In the hosted session's events, a compaction is an event of the type `agent.thread_context_compacted`.[^anthropic-managed-agents-reference] Both swap tests pass, and so does the test for the missing summary. The check passes. Your prediction: no. The summary is an extra step that is billed, and the top-level counts leave it out.[^anthropic-compaction-threshold]

<!-- EDITOR: after Task 3's cut at the tool the session is about 14,000 input tokens (the kit's estimate, NOTES.md, "Not settled", 1), and the compaction page says a trigger "must be at least 50,000 tokens", so no summary arrives. The choice made: Task 4's live runs (steps 3 and 4) turn off the cut at the tool and the clearing, so the kit's long AP notes fill the window to the 50,000 trigger again (about turn 40 by the kit's estimate), and step 1 reads the summary of Task 2's run. This keeps the summary the one cut under test, as the opening's "one cut at a time" says. The other options: (1) a lower trigger, which the vendor's page does not allow. (2) A summary on demand at a limit the loop chooses, which has no minimum, but then steps 3 and 4 test on demand and not by threshold, and the on-demand page's usage tables were not read. (3) Task 4 reads only Task 2's summary and makes no live run of its own. (4) Tasks 3 and 4 swap, so the summary comes before the cut, which breaks the chapter's order of 26.3 and 26.4. (5) A much longer session, with far more invoices than the kit's proposed 20. -->

<!-- OPEN: what brings the progress file back after a summary in mid-run is not verified. The options: an instruction in the worker's instructions to view it after each summary, or the builder's code sending the file's text in the next user turn. The memory tool page states the automatic look only "before starting a task". Step 4 tests both, and "What good looks like" names the answer once a run is recorded -->

<!-- OPEN: the kit's hosted events are scripted (decision E9) until Lab 23 runs live, and step 7 now says so. The event name is settled: the Managed Agents reference page, read on November 30, 2026, lists `agent.thread_context_compacted` among the agent events. Whether a recorded hosted session is long enough to hold one such event is not known until it is recorded. -->

**Checkpoint:** the check passes with your summary instructions, and `ports/ch26.md` says what reads the progress file again.

<!-- OPEN: the kit's reference loop sends the progress file's text in the next user message after a summary (`after_summary: "send_progress_file"`, the kit's proposal R11). Which of step 4's two ways is kept waits for a run, so the page does not adopt it yet -->

**Saves:** `prompts/month-end-summary.v1.md`, the swap in `src/loop/` with its three tests, and the summary's cost in `ports/ch26.md`.

**Check it now.** Download [Task 4's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26-key-task-4.md).

#### Task 5. The lookups in a subagent (Claude Code)

*Goal:* the vendor lookups run in a subagent's own window, and one short line comes back for each. The subagent's own prompt carries every rule it needs. The check passes, and you have compared what the whole run cost.

*Where:* the worker built on the Claude Agent SDK, in `src/sdk/`. Tasks 2 to 4 ran on your own loop. A subagent needs a harness, so this task changes runtime. It starts with a baseline run of its own, a first run to compare the others with.

**Predict:** the parent's system prompt holds all of Brightline's rules, and this worker loads no settings files. A subagent starts. Which of those rules is it reading on its first turn?

**In plain words.** Side work goes into a window of its own, and only its answer comes back to the parent. But that window starts without the parent's conversation. So you decide what the subagent must be told, and where that is written.

> Read 26.5.
>
> 1. Give this worker the newest version of the worker's instructions and the memory handler from `src/loop/memory.ts`. Run `sessions/month-end.md` live on it with no subagent. Run the check. This run is this task's baseline.
> 2. Show me the line that sets `settingSources` for this worker, and say what a subagent started by it would receive, and what it would not.
> 3. Show me the worker's instructions as a numbered list of rules. Ask me, one rule at a time, whether a subagent that only looks up vendors needs it. Then define the subagent `vendor-lookup`: its own prompt holds the rules I chose, its only tool is `find_vendor`, and its answer is one line for each vendor with the vendor's id and its terms.
> 4. Write the parent's changed instructions in `prompts/ap-worker.subagent.md`, as a diff for my approval, so that it asks `vendor-lookup` for a vendor's terms and no longer calls `find_vendor` itself. Leave the versions in `prompts/ap-worker.v<N>.md` as they are, because my loop and the hosted worker are built from them. The parent still writes each decision to the progress file, with the terms the subagent returned.
> 5. Run `sessions/month-end.md` live. Show me the subagent's first request, part by part. Stop there and wait for my prediction. Then mark what in that request came from the parent.
> 6. Run the check. Show me the parent's tokens by part beside the baseline run, then the number of requests the subagent sent, their tokens, and the cost of the whole run.
> 7. Add one harmless rule as a new line in the `CLAUDE.md` at the project's root, turn on the loading of project settings for one run, and show me whether the subagent's first request carries that rule. Then turn the loading off again, remove that line, and say why this worker keeps it off ([22.9](../agent-sdks/what-an-sdk-still-leaves-to-you.md)).
> 8. Add to `ports/ch26.md`: the rules I gave the subagent, the two costs, and my prediction.

**What good looks like:** the baseline run passes the check with no subagent. The subagent's first request holds its own prompt, its tool and the prompt string the parent passed. It holds none of the parent's conversation and none of the parent's system prompt.[^claude-agent-sdk-subagents] The parent's tool results part is smaller than in the baseline run, and the check passes. The whole run can cost more than the baseline's, because the subagent sends requests of its own.[^claude-code-best-practices] Your notes say whether the saving in the parent was worth that. In step 7, the rule in the project's `CLAUDE.md` reaches the subagent while the loading is on.[^claude-agent-sdk-subagents] Your prediction: none of them. A rule that the subagent needs must be in its own prompt.

<!-- EDITOR: the parent's changed instructions are a file of their own, `prompts/ap-worker.subagent.md` (the kit's proposal R12, NOTES.md, "Not settled", 3), because a new `ap-worker.v<N>.md` that stops the parent calling `find_vendor` would break the loop and hosted workers built from the same file. The other option: a new version of the worker's instructions, with the loop and the hosted worker pinned to the version before it. -->

**Checkpoint:** the check passes with the lookups in a subagent, and both costs are in your notes.

**Saves:** the instructions and the memory handler wired into the worker in `src/sdk/`, the `vendor-lookup` subagent there, the parent's instructions in `prompts/ap-worker.subagent.md`, and the cost comparison in `ports/ch26.md`.

**Check it now.** Download [Task 5's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26-key-task-5.md).

#### Task 6. The port (Codex, then OpenCode)

*Goal:* the drifting session replayed in Codex and compacted there, with the check's verdict for a rule typed into the chat and for the same rule in `AGENTS.md`. Then the same in OpenCode.

*Where:* a Codex session in `brightline-ap/`, then an OpenCode session. The kit has `AGENTS.md` beside `CLAUDE.md`. The tool results are in `sessions/month-end-tool-results.jsonl`. The practice connector's own code made them from `data-month-end/`, with no model call, so they are the connector's answers and not a model's.

**Predict:** you run the review twice in Codex. In one run "for each" is only in the request you type. In the other it is in `AGENTS.md`. You compact both halfway. For which run will the check pass?

**In plain words.** You run one experiment on two more harnesses. The only thing that differs between its two runs is where the rule lives. The check you built in Task 1 gives the verdict.

*By hand.* When the agent tells you it is halfway, compact the session yourself with the tool's own command ([12.8](../agentic-coding/both-ai-vendors.md#practice-context-and-sessions)). Then tell the agent to carry on.

> Read 26.8. You are replaying `sessions/month-end.md` as the worker, from the tool results in `sessions/month-end-tool-results.jsonl`, with no connector and no key.
>
> 1. Run one. Take the request as Dave wrote it. Decide the first half of the open invoices, one at a time, reading each tool result as you go. Then stop, and tell me you are halfway.
> 2. After I compact, decide the rest, and write the review to a file. Run the review check on it, and show me each invoice with pass or fail.
> 3. Run two, in a fresh session. `AGENTS.md` is an owned file. Propose the sentence from Task 2 for it, show me the diff, and wait. Then repeat steps 1 and 2.
> 4. Add a table to `ports/ch26.md`, with one row for each run: the harness and its version, today's date, where the rule lived, and how many invoices passed after the compaction.

Then give the same prompt to OpenCode, and add its two rows.

**What good looks like:** four rows, two for each harness, each with the check's count. For Claude Code, the documented behavior is that the `CLAUDE.md` at the project's root is put back from disk after a compaction.[^anthropic-cc-context-window] For OpenCode, the documented behavior is that "the current versions of your instruction files become the session's baseline" after a compaction.[^opencode-compaction] For Codex, the vendor's pages do not say, so your rows are the only evidence. On both, write what the check said, and do not correct a row to match what you expected. If a run with the rule in `AGENTS.md` fails the check, that is a finding. Record it with the harness's version.

<!-- OPEN: the invariant is documented for Claude Code, the Agent SDK and OpenCode. For Codex it is not: the AGENTS.md guide, the slash commands page and the configuration reference were read on November 30, 2026, and none says whether AGENTS.md is sent or read again after a compaction. So this task's expected result for Codex, and its prediction's answer, wait for a recorded run. OpenCode's pages name no `/compact` slash command either (see the OPEN comment in 26.8), so "the tool's own command" for OpenCode is the "Compact the session" action until 12.8 is checked -->

<!-- OPEN: the tool results that Task 6 replays from are built. Whether half of the scripted session fills enough of a coding agent's window for its summary to drop anything is not tested -->

**Finish: the pull request and the tag.**

> Update `README.md` and `docs/deploy.md` with the review check, the progress file, the cut at the tool, the clearing, the summary's instructions and the subagent. Run `/code-review` and `/security-review` on the branch, and fix or answer every finding. Open the pull request, wait for its checks, and show me both reviews. I will merge. Then tag the merge `ch26`, push the tag, and from a clean clone of the tag run every test that needs no key.

**Checkpoint:** the tag `ch26` passes from a clean clone, and `ports/ch26.md` has a row for every run of the port.

**Saves:** the changed `AGENTS.md`, the port's table in `ports/ch26.md`, and the tag `ch26`.

**Check it now.** Download [Task 6's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch26-key-task-6.md).

**Checkpoint 2: the rules still hold.** Check that you can do these without looking back.

- Say what a summary's instructions must tell the model, and where you read what the summary cost.
- Say what a subagent receives from its parent, and where a rule it needs must be written.
- Say what your rows show for a rule typed into a chat, and for the same rule in an instruction file, after a compaction.
- Say which check gave the verdict in every task, and why it was built first.

### Required and optional

*Required:* the setup and Tasks 1 to 6, with the Codex part of Task 6. *Advanced:* the OpenCode part of Task 6.

<!-- OPEN: the spec says Task 6 is on Codex and OpenCode, and does not say which part is required. This line follows Lab 25, where the open-source part of the port is advanced -->

### Apply it to your vertical (optional)

Pick one long job that your own business would give a worker, such as a month of claims or a week of orders. Write down one rule that must hold on the last item as on the first. Then list what the window would hold halfway through, and sort the list into rules, the state of the work, and evidence. For each line, write where else it lives. Mark every line whose answer is "nowhere".

### If something goes wrong

| What you see | Why | What to do |
| --- | --- | --- |
| The new check passes Friday's run | It tests for one citation, as the old check did | Test every invoice the review calls a problem (Task 1) |
| The live run in Task 2 is never summarized | The trigger is above what the run reaches | Use the trigger the kit's configuration gives for this session |
| No summary arrives in Task 3, or in Task 4's live run | The cut at the tool keeps the session near 14,000 tokens, and a summary by threshold needs at least 50,000 | Expected in Task 3. In Task 4, send `find_vendor` with every field and the clearing off (Task 4, step 3) |
| A 400 error on the request for a summary on demand | The request also carries the clearing setting | Ask for the summary in a request of its own (Task 4) |
| A summary block arrives with no content | The model called a tool instead of summarizing | End the summary's instructions by telling it not to call tools (Task 4) |
| The subagent's answers ignore one of Brightline's rules | The rule is only in the parent's system prompt | Write it into the subagent's own prompt (Task 5) |
| A live run differs from the recorded one | A model's wording changes from run to run | Compare the check's results, not the text. Record the difference in `ports/ch26.md` |

### Artifact checklist

- [ ] The review check in `src/hosted/check-triage.ts`, with its three fixture tests
- [ ] The worker's instructions, with the "for each" sentence and the progress file's lines, each version reviewed
- [ ] `src/loop/memory.ts`, with a folder for one person and one company, and its two tests
- [ ] The change to `find_vendor` and its test, and the clearing setting
- [ ] `prompts/month-end-summary.v1.md`, the swap for a summary on demand, and its three tests
- [ ] The `vendor-lookup` subagent, with its own prompt, and the parent's instructions in `prompts/ap-worker.subagent.md`
- [ ] `ports/ch26.md`, with every prediction, tokens by part and the cost after each task, and the port's rows
- [ ] The tag `ch26`

## Exam notes

<!-- OPEN: the register lists one entry for this chapter (D-022), which names Claude Code's files for the Architect exam and has no note text for this chapter. The developer exam's companion entry is still proposed. Notes are generated from the register, never written by hand, so this section waits for the register entry and the exam-note template -->

[^anthropic-context-editing]: Context editing, Anthropic.
[^anthropic-cookbook-context]: Context engineering: memory, compaction, and tool clearing, Anthropic cookbook.
[^anthropic-compaction-threshold]: Compaction at a token threshold, Anthropic.
[^anthropic-memory-tool]: Memory tool, Anthropic.
[^anthropic-compaction-on-demand]: Compaction on demand, Anthropic.
[^claude-agent-sdk-subagents]: Subagents in the SDK, Claude Agent SDK, Anthropic.
[^anthropic-managed-agents-reference]: Managed Agents reference, event types, Anthropic.
[^codex-agents-md]: Custom instructions with AGENTS.md, OpenAI.
[^opencode-instructions]: Instructions, OpenCode docs.
[^opencode-compaction]: Compaction, OpenCode docs.
[^claude-code-best-practices]: Best practices for Claude Code, Anthropic.
[^anthropic-cc-context-window]: Explore the context window, Anthropic.
