Loading video player…

Watch the First Steps of a Run

In this video, you watched the start of an example run of this section’s task for the escape-room builder: a list-rooms command that prints every room with its verdict. The session was written for the video rather than recorded. Your own run will differ, but you watch the same places.

Key points:

  • The task has the parts you give every task: the goal, where to look, the limits, and a done-when. Two limits matter here: use checkRoom(), the existing function that decides whether a room can be finished, for each verdict, and don’t change any room file.
  • First, the agent opened files. It read the project map and package.json, listed the rooms/ folder, and opened one room file. It didn’t open the checker in src/check-room.js, where checkRoom() lives, or the check-room.js command that runs it, though the task named both. That’s only a hint, because it may open them later. But an agent that never reads the checker is unlikely to reuse it.
  • Next, the agent stated a plan in three steps. Its first step was to write its own function to work out whether every puzzle can be reached. That’s a second checker, and deciding that is the job of checkRoom(). Steps two and three fit the task, since a script in package.json isn’t a new dependency. Step one is the first step that contradicts the task, and nothing is built on it yet. That makes it the moment to step in. How you do that is the next lesson.
  • A few seconds later, the agent made its first edit. It created a new file for the command, with its own function for whether a room can be finished. That function never reads a puzzle’s uses list, which says what solving that puzzle uses up. So a key that’s been used up still counts as found. For a room that can’t be finished once that key is gone, it could say “can be finished” when your checker says it can’t.
  • Once the wrong turn is in code, each step after it builds on it, such as a test for the new function and a script to run it. The done-when can still pass, because none of the three sample rooms is that kind of room.
  • Your own run may go another way. It might add a package to find files, change a room file to “fix” it, or change the output of the existing check-room.js command. Or it might follow the task. All of these are normal.

Watch three places at the start of a run: the files it opens, the plan it states, if it states one, and its first edit. Read each one against your task, and step in at the first one that contradicts it. That’s where steering a running agent starts.

Here’s the task from the video. It’s an example written for the course’s project, so your wording and file names may differ:

Language: Text
Add a list-rooms command that prints every room in
rooms/, one line each, with its verdict.

Read docs/map.md, then check-room.js and
src/check-room.js.

Use checkRoom() from src/check-room.js for each
verdict. Add no new dependencies. Don't change
check-room.js or its output, or any file in rooms/.

Done when the command prints one line per room, each
with the verdict check-room.js gives it, and npm test
passes. Then stop and list the files you changed.

And here’s the plan the example session stated:

Language: Text
Plan:
1. Create src/list-rooms.js. Read each JSON file in
   rooms/ and write a function that works out whether
   every puzzle can be reached.
2. Print each room's name and its verdict.
3. Add a list-rooms script to package.json, and a test.

The agent’s first edit was the file below, with canFinish(), its own copy of the reachability check, near the bottom. It’s a small example made for the video:

Language: JavaScript Filename: src/list-rooms.js
import { readdirSync, readFileSync } from "node:fs";

export function listRooms(folder) {
  return readdirSync(folder)
    .filter((file) => file.endsWith(".json"))
    .map((file) => {
      const text = readFileSync(`${folder}/${file}`, "utf8");
      const room = JSON.parse(text);
      const verdict = canFinish(room) ? "can" : "cannot";
      return `${room.name}: ${verdict} be finished.`;
    });
}

function canFinish(room) {
  const found = new Set();
  let left = room.puzzles;
  let solvedOne = true;
  while (solvedOne) {
    const ready = left.filter((puzzle) =>
      puzzle.needs.every((need) => found.has(need)),
    );
    for (const puzzle of ready) {
      puzzle.unlocks.forEach((item) => found.add(item));
    }
    left = left.filter((puzzle) => !ready.includes(puzzle));
    solvedOne = ready.length > 0;
  }
  return left.length === 0;
}

Next, in Interrupt and Redirect Early, you’ll run this task on your own project and steer your agent if it heads the wrong way.

00:00 Your escape-room builder checks a room, names its dead ends, and reports on play-tests. Your agent wrote most of that code. In this section, you’ll give it one new task and stay in charge while it runs.

00:14 The task is a list-rooms command that prints every room with its verdict. It has the parts you give every task: the goal, where to look, the limits, and a done-when.

00:26 Two limits matter in this video. Use checkRoom, the existing function that decides whether a room can be finished, for each verdict. And don’t change any room file.

00:40 Here’s how a run of this task might start, in an example written for this video. Your own run will differ, but you watch the same places.

00:49 First, the agent opens files. It reads the project map and package.json, lists the rooms folder, and opens one room file.

01:00 The task named two more files: the checker, where checkRoom lives, and the command that runs it. The agent hasn’t opened either of them. That’s a hint, not proof.

01:12 It may open them later. But an agent that never reads the checker is unlikely to reuse it.

01:20 Next, the agent states a plan, in three steps. Read it against the task beside it. Your turn: pause the video and find the step that goes against the task. It’s step one.

01:35 The agent plans to write its own function to work out whether every puzzle can be reached. That’s a second checker. Deciding that is checkRoom’s job. Steps two and three fit the task.

01:49 This is the first step that contradicts your task, and so far nothing is built on it. That makes it the moment to step in. How you do that is the next lesson.

02:01 A few seconds later, the agent makes its first edit. It creates a new file for the command. You don’t need to follow every line.

02:11 Look at its own function for whether a room can be finished. It never reads a puzzle’s uses list, which says what solving that puzzle uses up. So a key that’s been used up still counts as found.

02:24 For a room that can’t be finished once that key is gone, it could say “can be finished” when your checker says it can’t. Now the wrong turn is in code. Each step after this, like a test for the new function or a script to run it, builds on it.

02:40 And the done-when can still pass, because none of the three sample rooms is that kind of room. So, watch three places at the start of a run: the files it opens, the plan it states, if it states one, and its first edit.

02:57 Read each one against your task, and step in at the first one that contradicts it. That’s where steering a running agent starts.

03:06 Your own run may go another way. It might add a package to find files. It might change a room file to “fix” it. It might change the output of the existing check-room command.

03:18 Or it might follow the task. All of these are normal. Next, in Interrupt and Redirect Early, you’ll run this task on your own project and steer your agent if it heads the wrong way.

Become a Member to join the conversation.