1. Introduction

Week one asked who says what runs. Week two asked who decides what runs next. This week the graph is finished and the question turns to what it leaves behind.
Market Street sells things, and this shop stops three times in a normal day:
the pause | what it is waiting for | what you write |
the clerk asks you which one | the person already typing |
|
the courier is out | the world | a long-running tool |
the manager has to sign | somebody who is not in this conversation |
|
Every one of those pauses ends the turn. The run is gone; the process may be gone; a day may pass. And each one leaves a different name in the same ledger — out_for_delivery, a case with no stamp on it — which is the sentence this whole lab is built on:
A shop that can stop has to write down where it stopped.
The rule over the door is the other half: nothing may be sold twice.

You do not need weeks one or two. The story continues; the lab stands alone.
You will make two edits. Both are one line. One is in street/agent.py, one is in street/service.py, and each is marked with a 👉 comment in the code.
Three tabs, and they never change jobs: tab 1 is the terminal, tab 2 is the workbench (adk web), tab 3 is the shop. Every 👉 below says which.
2. 🧰 Setup
Everything in this lab runs in Cloud Shell — Python, uv and gcloud are already installed there, and Gemini is reached through Vertex AI using your own credentials. There is no API key anywhere in this lab, and nothing to install on your laptop.
There is one thing this lab cannot hand you: a Google Cloud project with billing on it. Step 1 gets you an account that can pay for it; step 4 turns that account into a project — or picks up the Agent Valley project you already made in week one or week two. Do them in that order — every step after 4 reads the project that step 4 settles on.
1 · Get an account with billing (~1 min)
🌐 Do this before anything else. Sign in with a Google account that has billing enabled. Any such account works. If yours does not, start a free trial; it gives you a billing account, which is all step 4 needs.
2 · Open Cloud Shell (~1 min)
🌐 Click Activate Cloud Shell at the top of the Google Cloud console — it is the terminal-shaped icon in the header.

🌐 Click Open Editor (the open-folder-with-a-pencil icon). This brings up the Cloud Shell Editor with a file explorer on the left — that is where you work whenever a step is marked 👉✏️.

🌐 If you are prompted to authorize at any point today, click Authorize to continue.
🌐 Open a terminal inside the editor — that is tab 1, and every 👉💻 step runs there unless it says otherwise. The Introduction named the other two already: tab 2 is the workbench (adk web) and tab 3 is the shop, and you open each of them later, right before the step that needs it. If the terminal is not already along the bottom of the screen, open it with View → Terminal:

💻 Check which account Cloud Shell is signed in as:
gcloud auth list
👀 Expect your own account marked with a *, and it must be the same account you signed in with in step 1.
3 · Get the lab (~1 min)
💻 Clone the repo — this is the Market Street repo, not week one's or week two's:
cd ~
git clone https://github.com/cuppibla/agent-valley-market
cd agent-valley-market
4 · Create your project — or reuse an earlier week's (~2 min)
💻 One script finds your billing account, creates a project on it, and points Cloud Shell at that project. If you did week one or week two, it finds that Agent Valley project first and offers to reuse it — say yes and there is no second billable project to think about:
cd ~/agent-valley-market
./setup_project.sh
👀 Expect a ✓ for the billing account it picked, a ✓ for the project it created or reused, and an ending like:
Project ready: agent-valley-4271 Next: ./setup_codelab.sh
agent-valley-4271 is your project ID — yours will have different digits. Four things happen in there:
billing | takes the newest | |
project | looks for an Agent Valley project you already own — week one's or week two's — and offers to reuse it; otherwise creates | |
selects | writes the ID to | |
waits | polls until the project is actually allowed to serve, for up to two minutes | |
🌐 The same project is now in the console — its ID sits in the Project info card on the Dashboard:

5 · Install and point at Vertex AI (~3 min)
💻 Install uv, export the three variables that send ADK to Vertex AI, and run setup:
cd ~/agent-valley-market
curl -LsSf https://astral.sh/uv/install.sh | sh
source ~/.local/bin/env
export GOOGLE_GENAI_USE_VERTEXAI=True
export GOOGLE_CLOUD_PROJECT=$(gcloud config get project)
export GOOGLE_CLOUD_LOCATION=global
./setup_codelab.sh
$(gcloud config get project) has something to return because step 4 set it. That is the whole reason step 4 comes first.
setup_codelab.sh is idempotent — safe to re-run any number of times. It does four things, in order:
api | reads the project step 4 selected and enables the Vertex AI API on it | |
uv env |
| |
.env | writes the variables the lab reads — no API key anywhere | |
model check | makes one real call to Gemini, so a bad project fails here at setup time, not later inside a chapter | |
6 · Prove it is green (~1 min)
💻 Run the preflight — every tick is a real probe, not a guess:
cd ~/agent-valley-market
uv run python scripts/preflight.py
👀 Expect a green board. The last two lines of the preflight are this week's two dials, read straight from your code:
✓ street · workflow · 12 nodes · no manager yet (chapter 3 adds one) ✓ ledger · InMemorySessionService · in memory (chapter 4 moves it to a file)
Both are true right now. Both will be false by the end.
3. 1 · One counter
- What — talk to the shop with no costume on, and meet the first pause.
- Why — because the State tab is this week's whole subject, and the workbench shows it raw.
- How — run it, buy one thing, change nothing.

👉💻
cd ~/agent-valley-market
uv run adk web . --allow_origins="*"
👉🔬 Open http://localhost:8000 (Cloud Shell: Web Preview → port 8000), pick street, and type:
buy the Star Lantern · order #k7f2
👀 A handful of bubbles for one sentence. desk read what you typed and filled in a slip; route sent the slip down the buy path; reserve took one off the shelf; dispatch handed the parcel to the courier. Then it stops. Look at the small grey chips under some of them: State: app:stock, orders. Those nodes wrote something down.
👉🔬 Click State in the left panel.
👀 The run is over. The state is still there — a ledger with one order in it, and that order says out_for_delivery. Nothing has been charged. This shop is cash on delivery, and chapter 2 is about what that costs you.
Open the file
👉✏️ Open street/agent.py and read the last statement:
root_agent = Workflow(
name="street",
edges=[
("START", desk, route, {"buy": reserve, "pick": clerk, "return": verify, "ask": twill}),
(clerk, route),
(reserve, dispatch, charge, grant, reply),
(verify,
refund, reply),
],
)

👀 Week two's shapes, all present: chains, a dict that routes on a word, and paths that share a node. The statement is the graph, and the picture above is nothing but that statement with boxes around the names.
👀 Now count what is in it: 4 agents and 7 plain functions. A node is a slot, not an agent. And this week that carries a second meaning worth saying out loud — look at which nodes touch money. charge, refund, reserve: every one of them is a plain Python function. The model fills in the form; the code moves the sparks. That is a safety argument, not a style preference.
The first pause
👉🔬 Start a new session in adk web and be vague on purpose:
I'd like to buy something

👀 Read the four events. desk filled the slip in with item: null, because it did not know. route read that and emitted route: pick. The clerk asked you which one. And then nothing. No reserve, no dispatch, no fifth event.
👀 The workflow is stopped at that one node, and it will stay stopped until you answer.
👉🔬 Answer the clerk — type this into the same session and send it:
the star lantern
👀 Watch it carry on from exactly where it stopped: reserve, then dispatch, then the events that never arrived a moment ago.
👉✏️ Find clerk in street/agent.py. One keyword does that:
clerk = Agent(name="clerk", ..., output_schema=Slip, mode="task")
👀 mode="task" lets an agent inside a workflow talk to the customer across several turns, right there inside its own box, and the graph does not move until it calls finish_task with something that fits its output_schema.

Now go to the shop
👉💻 Leave adk web running. Open a second terminal tab (tab 3 — the shop):
cd ~/agent-valley-market
bash valley.sh
👉🔬 Open http://localhost:3200 (Cloud Shell: Web Preview → port 3200), press ▶ Start, then Market Street, and pick whoever is shopping today.

👀 Left, you are the customer and you talk to Twill. Right is the back room — the same nodes you just saw in the workbench, drawn as the crew along the street. The customer never sees the back room. The staff never see the chat. It is one session.

4. 2 · Out for delivery
- What — a parcel that is still walking, and a run that ends before it arrives.
- Why — because the world does not answer when you ask it to, and the turn that asked is over long before the answer lands.
- How — nothing to edit here. Read
dispatchandcharge, then watch the delivery come back in a later turn.

👉🔬 Press Rain Cape ✦6.

👀 Read the back room. Twill is done, Nix is done, and then your familiar — the courier — is holding the street with a raised hand. charge and grant are dark. The ledger says:
#eqn9 Rain Cape ✦6 out for delivery
👀 And your purse has not moved. The run has ended. Nobody was asked anything; there is no question, no schema, no buttons. The shop is simply waiting on the world.
👉✏️ Look at dispatch in street/agent.py:
dispatch = Agent(name="dispatch", ..., tools=[LongRunningFunctionTool(hand_to_courier)])
👀 hand_to_courier returns a ticket and returns immediately. Wrapping it in LongRunningFunctionTool is what tells ADK the work is not finished when the function is: the run ends holding that ticket, and the answer will arrive later, out of band.

👀 Follow the numbers. 1 the order reaches dispatch. 2 it calls the tool, which hands the parcel over and comes straight back with a ticket. 3 the run ends, and the only trace of it is a row in the ledger saying out_for_delivery. 4 an hour later, in a process that may not be the same one, the delivery is reported on that ticket. 5 charge runs — and reads the ledger, because the edge it would have arrived on belongs to a turn that finished long ago.
👉🔬 Press 🔔 the courier knocks — you are the world, so the parcel arrives when you say it does. A minute from now, or an hour.
👀 charge runs, then grant, then Twill speaks. The purse moves at last.
The line that is the whole week
👉✏️ Read charge in street/agent.py. It is the most important node in this lab, and not for what it does:
ledger = dict(ctx.state.get(ORDERS, {}))
key = next((k for k, v in ledger.items() if v["status"] == OUT_FOR_DELIVERY), None)
👀 It never looks at
node_input
. It cannot. The parcel left in one turn and the delivery came back in another, so whatever was travelling on the edge is long gone.
After a pause, the edge carries nothing. The ledger carries everything.
That is not a slogan about durability. It is why this node is written the way it is, and you can delete the ledger and watch it fail.
5. 3 · The manager's stamp
- What — a pause that is answered by somebody who is not in the conversation.
- Why — because a refund is money leaving the shop.
- How — one word in the edges list.

👉🔬 Press return it.

👀 verify finds the order, refund puts the sparks back, Twill says thank you. Nobody looked. Sparks left the till because a customer asked nicely.
👉✏️ EDIT ONE. In street/agent.py, scroll to the bottom — the edges list. Before — note the empty line after the 👉 comment; that is where the word goes:
(verify,
# 👉 EDIT ONE — chapter 3. Add `approve,` on the next line. One word: the
# graph now stops here and waits for the back office before any sparks move.
refund, reply),
On that empty line, add this one word — the comma is part of it. Copy it as-is; the nine leading spaces are what line it up with refund on the line below:
approve,
After — street/agent.py now reads:
(verify,
# 👉 EDIT ONE — chapter 3. Add `approve,` on the next line. One word: the
# graph now stops here and waits for the back office before any sparks move.
approve,
refund, reply),
Save. No restart here either.
👀 That is the whole edit: one word, in a list. approve was already written and sitting in street/agent.py, doing nothing, because nothing pointed at it. A node that no edge mentions is not part of the graph.
👉🔬 Press return it again.

👀 Odo appears in the back room and raises a hand. But look at the customer's side: there are no buttons. Twill says this one needs the manager's stamp, and that is all the customer gets, because this question is not theirs to answer.
👉🔬 Open the back office in a second tab: http://localhost:3200/w3/back. Better, open it on your phone. You are the manager now.

👀 One row per case, and each row names two things: which session it belongs to and which interrupt it answers. That is not decoration. The shop may have several stopped runs at once, this page has never seen any of them, and an answer arriving from a different screen has to say what it is answering.
👉🔬 Press Stamp it ✓, then look back at the customer's tab. Nobody touched it, and the refund has landed.
Read the node
👉✏️ Open _approve in street/agent.py. It runs twice:
answer = ctx.resume_inputs.get(STAMP)
if answer is None:
yield RequestInput(interrupt_id=STAMP, message=..., payload=case, response_schema=Stamp)
return
The first time there is no answer, so it asks and returns — and the run ends. When the answer arrives on that id, the node runs again with ctx.resume_inputs filled in, which is what rerun_on_resume=True buys.
👀 And notice what it does with the answer. It does not route on it. It writes it into the case, and refund reads it there. The manager's decision travels the way everything travels this week: as state.

Three pauses, three pictures
👀 You have now seen all three, and the back room draws each one differently — it is simply how far the light got:
the clerk | the courier | the manager | |
how far the light got | one node in | up to | everything before |
who is waited for | the customer | the world | staff, elsewhere |
how the answer arrives | their next message | a delivery on a ticket | an interrupt id and a schema |
what you wrote |
| one long-running tool | one word in the edges |

Which one should you use?
Four questions. Ask them in order and stop at the first yes.
- Is anyone making a decision? No → a long-running tool. You are waiting for an event, not an answer.
- Is the decider the person already typing? Yes →
mode="task". Their answer is their next message on a channel that is already open. - Does one dangerous step sit inside an otherwise ordinary agent? Yes → tool confirmation,
FunctionTool(require_confirmation=True). You are gating a call, not restructuring a graph. - Otherwise →
RequestInput. The answer is coming from somebody who is not in this conversation, so it needs an id to find its way back.
A long-running tool waits for the world. Task mode waits for the person in front of you. An interrupt waits for someone who is somewhere else.
6. 4 · The night
- What — the process dies while the shop is holding something.
- Why — because everything you built in the last two chapters lives in one dictionary, and right now that dictionary lives inside a process.
- How — one line in
street/service.py.

What you are about to do, and why. You are going to kill the shop in the middle of an order — twice. Once now, with the ledger living in memory: the order is lost. Then you change one line so the ledger lives in a file, and you do the exact same thing again: the order survives. Same crash, two outcomes. That is the whole chapter, and it is the only way to prove a durable store actually does something.
Crash one — the ledger is in memory.
👉🔬 Buy something — press any item on the stall — and then leave the parcel where it is. A button labelled 🔔 the courier knocks appears in the back room, under the crew, beside the courier's portrait. Do not press it. Leaving it unpressed is the point: the parcel is out, the run has ended, and nothing has been charged, so the shop is now holding an unfinished order. That is the thing we are about to destroy.
👉🔬 Now press 💥. That icon kills the service process on the spot — it is a crash button, not a move in the game. It is the middle of the three small icons at the top right of the back room panel, circled in red in the screenshot below.

👉💻 The service is dead, so nothing is listening any more. Bring it back up — this is just restarting a crashed server, not part of the lesson. In tab 3, press Ctrl+C to clear the dead process, then:
cd ~/agent-valley-market
bash valley.sh
👉🔬 Do not reload the browser. The shop tab is polling for the service and comes back by itself. When it does, look at the back room before you type anything: read the ledger panel, and look for the 🔔 the courier knocks button you left alone a minute ago.

👀 Morning, and Twill has no idea who you are. The ledger panel says nothing ordered yet, and the doorbell is gone. Your order did not survive the crash. The charged guard in charge cannot help here — it reads a ledger that no longer exists.
That is the failure. Now fix it.

👉✏️ EDIT TWO — open street/service.py:
cd ~/agent-valley-market
cloudshell edit street/service.py
Search for EDIT TWO. Before — you are looking at this block, around line 53:
# ── where the shop keeps its memory ──────────────────────────────────────────
# 👉 EDIT TWO — chapter 4. This is the whole edit. Replace the line below with:
#
# _sessions = SqliteSessionService("market.db")
#
# In memory, the ledger lives inside this process and dies with it. In a file, it
# does not — and `adk web --session_service_uri=sqlite:///market.db .` can read it.
_sessions = InMemorySessionService()
After — only the last line changes. Everything above it stays exactly as it is:
# ── where the shop keeps its memory ──────────────────────────────────────────
# 👉 EDIT TWO — chapter 4. This is the whole edit. Replace the line below with:
#
# _sessions = SqliteSessionService("market.db")
#
# In memory, the ledger lives inside this process and dies with it. In a file, it
# does not — and `adk web --session_service_uri=sqlite:///market.db .` can read it.
_sessions = SqliteSessionService("market.db")
SqliteSessionService is already imported at the top of the file, so there is nothing else to add. Save.
👉💻 This restart is mandatory — the edit does nothing until you do it. In tab 3, press Ctrl+C, then:
cd ~/agent-valley-market
bash valley.sh
👉🔬 Confirm it actually took. Look at the header of the ledger panel in the back room: it names the store the service is really running. It should now read MARKET.DB. If it still names the in-memory store, the restart did not happen — go back and do it before going any further.
Crash two — the ledger is in a file.
👉🔬 First press ↺ Reset the shop — it is the middle of the three buttons under STARTING OVER at the bottom of the counter, circled in red below — and confirm. Do this before you buy anything.

👉🔬 Now run the exact same experiment as before. Three steps, the same three:
- Buy something, and do not press 🔔 the courier knocks.
- Press 💥 to kill the service again.
- In tab 3, Ctrl+C, then
bash valley.sh, and wait for the shop tab to come back on its own.

👀 This time the doorbell is still there. Ring it. The order is still out_for_delivery, the courier's knock lands, and the charge goes through. Same crash, opposite outcome — and the only thing you changed was where the ledger lives.
Exactly-once is not a delivery guarantee. It is two things the shop needs: a node that is safe to run twice, and a memory that outlives the process.
The workbench can read the same market.db
👉💻 In tab 2, stop adk web and start it again pointed at market.db, the database street/service.py now writes to:
cd ~/agent-valley-market
uv run adk web --session_service_uri=sqlite:///market.db . --allow_origins="*"
👉🔬 Reload localhost:8000, pick street, and open the session picker. Your shop conversation is in the list.

👀 Every knock, every charge, the faint and the morning. The shop and the workbench are two windows onto one file.
7. 5 · Whose drawer is it (optional)
- What — the four lifetimes hiding in one dictionary.
- Why — so
statestops meaning one thing. - How — nothing to edit. Open the Monocle, then open a second shop.

👉🔬 Press 🧐 — the Monocle.

👀 Left, every event with the keys it wrote. Right, the same keys the workbench's State tab shows, sorted into four drawers:

the drawer | the prefix | this shop's keys | how long it lives |
this conversation | (none) |
| until the conversation ends |
this customer |
|
| every conversation they ever have |
the shop |
|
| whoever walks in, forever |
the counter top |
| (nothing) | wiped when the turn ends |
👀 The fourth drawer is empty, and that is the design. Every pause in this shop ends the turn — the clerk's question, the courier's walk, the manager's stamp — so by the time an answer comes back, the turn that started the order is long over. Nothing this shop needs could survive in temp:.
One shelf, two customers
👉🔬 Open the shop in a second browser window — a private window, so it gets its own familiar. Buy Rain Capes in one window until it says sold out, then look at the first window's stall.
👀 app:stock is the only key two customers share, and it is the whole of what "app" scope means: one shelf, and everybody draws from it.
👉🔬 Back in the first window, press start a new conversation, then ask Twill:
what am I wearing, and how many sparks do I have?

👀 The chat is empty, the ledger is empty, and Twill still knows your purse and your collar. The conversation drawer was left behind. The customer drawer came with you.
👀 And who answered? Not reply — that is a function. route sent it to twill, whose instruction reads:
"Sparks in their purse: {user:sparks?}\n"
"On their collar right now: {user:inventory?}\n"
Those are the keys charge and grant wrote. A function wrote them; a model read them; neither knows the other exists. That is how a crew shares a memory: not by passing messages, but by agreeing on the names of the drawers.
👉✏️ All five of them are declared in one place — open street/state.py. Every key there is written by somebody and read by somebody else. A key that is only ever written is decoration, and there are none.
The rule no single node owns
👀 Look at the till one more time. charge and refund are careful with it. But "careful" is a property of the code you happened to write, and user:sparks is not a local variable — it follows this customer into every conversation they will ever have, and any node in the shop can reach it. The ones written today are polite. The one somebody adds in six months might not be.
You cannot fix that with an instruction, and you certainly cannot fix it with a prompt. Week one's sentence is the one that applies:
An instruction is a request. A callback is a rule.
👉✏️ Open street/auditor.py. It is a plugin, which is a callback that is not attached to any one agent — it sees every event from every node, agents and plain functions alike, on the way to being written down:
class Auditor(BasePlugin):
async def on_event_callback(self, *, invocation_context, event):
...
if SPARKS not in delta: # nothing to do with the till
return None
if node in ALLOWED: # charge and refund, and nobody else
return None
refused = delta.pop(SPARKS) # the session never learns about this
👀 on_event_callback runs before the event is persisted, and returning the event replaces it. So taking a key out of state_delta there is not a late correction: the write never happens at all.

What the auditor would stop
street/agent.py has a commented-out line inside grant (search for TRY IT) that reaches into the till:
# ctx.state[SPARKS] = 999
Uncomment it and grant tries to set the purse to ✦999. It never gets there. The auditor strips user:sparks out of the delta before the event is persisted, the node turns rose and is labelled REFUSED, and the chat carries 🛡 auditor · refused user:sparks from grant — that is not its till.

👀 Two things worth noticing about how that refusal behaves. grant's other write, user:inventory, still goes through — the auditor took one key out of the delta, not the whole event. And after refusing it puts the true figure back into the live state, so grant does not spend the rest of the run believing its own lie. A rule that only holds at the boundary is a rule with a hole in it.
👀 And notice what the policy actually is:
ALLOWED = {"charge", "refund"}
Two node names, in one file, enforced against nodes that do not exist yet. That is the difference between a careful codebase and a rule.
8. Close
Two lines, two chapters, and both about the same dictionary.
the edit | the line | what it bought |
ONE |
| a graph a person can stop |
TWO |
| a memory that outlives the process |
What each piece replaced
ADK 2 | what you would write instead | what that costs you |
| a dict you pass to every function | the merge rule scatters, and a model cannot read it |
| pickle the dict to disk yourself | you write the resume, the locking, the migration |
| a state machine around a chat loop | the conversation and the graph drift apart |
| a queue, a worker and a callback table | three services to hold one ticket |
| a webhook, a cron job and a lookup | three more to hold one question |
| three databases and a cache | deciding where a key belongs, every time |
a plugin's | a review comment asking people to be careful | it holds for the code you have read, and nothing else |
The one sentence
A run can end while still holding a promise. What changes between these three is only who is expected to keep it — the world, the person in front of you, or somebody somewhere else.
And a run that can stop is a run that has to remember.
👀 Your purse is where the ledger says it should be, and every spark that left it can be accounted for. The Market Street stamp is in the passport, and week four opens The Archive — where a shop that remembers this week has to remember everyone.