The next morning
Dana should hear about her refund. A model drafts the email, the manager approves it, and the mail service sends it: the desk from Why Flow. In Why Flow, desk.py sent Dana an email nobody approved, because Tuesday's run started at the top and the top was a call to the model, and you saw the Flow version get it right.
This chapter is about why it got it right, because the same reason decides what you can and can't write in a Flow program. The morning's process runs main from the top, just as desk.py did. Every line of the program runs again. What stops the model from being asked again, and what else would have to be true for the morning's run to build the same question the evening's did?
The program
use "std@v1/clock"use "./tools"pub type Outcome { Sent(Text), Held }pub flow main(customer: Text, email: Text) -> Result<Outcome, ToolProblem> = { let draft = tools.draft_reply(email)? let drafted = clock.now()? match tools.ask_manager("Send this to ${customer}? Drafted ${drafted}.\n\n${draft}")? { Approve => Ok(Sent(tools.send_email(customer, draft)?)), Deny => Ok(Held), }}It's the desk with one line added, clock.now(), so the manager can see when the draft was written. In Flow, reading the clock is a Tool call like any other: clock is a library in the standard library, and the host answers it. That line matters later in the chapter.
Show tools/tools.py · 77 lines
# Stand-ins for a model, a manager and a mail service, as a Tool library.# The manager is a person: ask_manager writes the question to questions.txt and# holds its call until answer.py saves their answer and the run asks again.# Nothing leaves this directory: each service appends what it was asked to calls.log.import jsonfrom dataclasses import dataclassfrom pathlib import Pathfrom flow_toolkit import DecodeError, Hold, context, from_json, log, tool, variantDRAFTS = [ "Sorry, Dana. We have refunded the 40 dollars for order A-1042.", "Dana, we're sorry about the lamp. A new one ships today, free of charge.",]CALLS = Path("calls.log")def log_call(line): with CALLS.open("a") as calls: calls.write(line + "\n")@variant@dataclass(frozen=True)class Approve: pass@variant@dataclass(frozen=True)class Deny: passAnswer = Approve | Denydef answered(kind, question): """The answer answers.json holds for this call's key, read as kind. Until it holds one, write the question with the key to questions.txt and hold.""" key = str(context().key) questions = Path("questions.txt") if key not in (questions.read_text() if questions.exists() else ""): with questions.open("a") as file: file.write(f"{key} {question}\n") answers = Path("answers.json") saved = json.loads(answers.read_text()) if answers.exists() else {} if key not in saved: raise Hold() try: return from_json(kind, saved[key]) except DecodeError as error: log(f"{saved[key]} is not an answer: {error}") raise Hold()@tooldef draft_reply(email: str) -> str: """Draft a reply to a customer. Like a model, it rarely writes the same one twice.""" asked = len(CALLS.read_text().splitlines()) if CALLS.exists() else 0 log_call("draft_reply") return DRAFTS[asked % 2]@tooldef ask_manager(question: str) -> Answer: """Put a question to the manager. A person answers it, later.""" return answered(Answer, question)@tooldef send_email(to: str, body: str) -> str: """Send an email and return the mail service's message id.""" log_call("send_email") with Path("outbox.txt").open("a") as outbox: outbox.write(f"to {to}: {body}\n") return "msg-5512"calls.log; the manager's Tool posts its question to questions.txt.Show tools/answer.py · 11 lines
# The person's side of questions.txt: python3 tools/answer.py KEY ANSWER saves# ANSWER, written as JSON, for the question KEY names. The Tool reads it when# the run asks that call again, with flow resume RUN --resend CALL.import jsonimport sysfrom pathlib import Pathkey, answer = sys.argv[1], sys.argv[2]answers = Path(__file__).with_name("answers.json")saved = json.loads(answers.read_text()) if answers.exists() else {}answers.write_text(json.dumps({**saved, key: answer}, indent=2) + "\n")In the evening
In next-morning, start the run. ask_manager holds its call, as in Waiting for the manager, so the run waits there:
flow run main.flow customer=dana@example.com email='Order A-1042, the desk lamp, arrived broken.'run f0fa702fdde6f0fa702fdde6 waiting: every open call waits for a replyopen root#3, held by its implementation key: f0fa702fdde6f0fa702fdde6f0fa702f/root#3 tools.ask_manager arguments: {"question":"Send this to dana@example.com? Drafted 2026-11-19T10:18:49.078675Z.\n\nSorry, Dana. We have refunded the 40 dollars for order A-1042."}reply details: flow show f0fa702fdde6 'root#3'next: flow show f0fa702fdde6cat tools/calls.logdraft_replyThe run asked the model, read the clock, and is waiting at its third call, the question to the manager. The model was asked once.
In the morning
The manager approves, and the answer is saved under the question's key, as in Waiting for the manager:
python3 tools/answer.py 'f0fa702fdde6f0fa702fdde6f0fa702f/root#3' '"Approve"'A new process continues the run, and --resend asks the manager's call again. The model is still connected, so calls.log will show it if the model is asked again:
flow resume f0fa702fdde6 --resend 'root#3'Ok(Sent("msg-5512"))note: Open calls were selected for resend. The original call ID does not guarantee external deduplication; an external effect may happen again.f0fa702fdde6 completednext: flow replay f0fa702fdde6cat tools/calls.logdraft_replysend_emailcat tools/outbox.txtto dana@example.com: Sorry, Dana. We have refunded the 40 dollars for order A-1042.Here is what each process did with each of the four calls:
| Call | The evening's process | The morning's process |
|---|---|---|
draft_reply (root#1) |
asked the model | handed back the recorded draft |
clock.now (root#2) |
read the clock | handed back the recorded time |
ask_manager (root#3) |
the library held it | asked the library again, which returned the saved Approve |
send_email (root#4) |
not reached | sent it |
The morning's process ran main from the top. At each call it looked at the record. A call the record already had an answer for got that answer, with the arguments checked against what the evening's process had sent, and nothing was sent anywhere. The manager's call had no answer in the record, and you had named it with --resend, so it was asked again, under the same key and with the same question, and the library returned the manager's answer. After that the record had nothing more, so the run continued live, and send_email was the only other call the morning's process sent.
The record shows both days as one run:
flow show f0fa702fdde6run f0fa702fdde6state: completed11 of 11 facts · 0 hidden 1 Program f0fa702fdde6 ./main.main customer = "dana@example.com" email = "Order A-1042, the desk lamp, arrived broken." 2 Call root#1 tools.draft_reply email = "Order A-1042, the desk lamp, arrived broken." 3 Reply root#1 Ok value = "Sorry, Dana. We have refunded the 40 dollars for order A-1042." 4 Call root#2 std@v1/clock.now 5 Reply root#2 Ok value = "2026-11-19T10:18:49.078675Z" 6 Call root#3 tools.ask_manager question = "Send this to dana@example.com? Drafted 2026-11-19T10:18:49.078675Z.\n\nSorry, Dana. We have refunded the 40 dollars for order A-1042." 7 Hold root#3 held: its implementation promised to answer later 8 Reply root#3 Ok value = "Approve" 9 Call root#4 tools.send_email to = "dana@example.com" body = "Sorry, Dana. We have refunded the 40 dollars for order A-1042." 10 Reply root#4 Ok value = "msg-5512" 11 End Completed value = {"Ok":{"Sent":"msg-5512"}}host activity (14 records; not semantic facts):…full host records: flow show f0fa702fdde6 --jsonnext: flow replay f0fa702fdde6Fact 7 is the library's promise to answer later, and fact 8 the answer you gave. Nothing in the eleven numbered lines marks where the evening's process stopped and the morning's began; only the host's notes under the facts, left out here, show there were two processes. And since every answer is in the record, the finished run replays with nothing connected:
flow replay f0fa702fdde6Replay f0fa702fdde6: matchedNo tools were called.Why the question came out the same
Serving the draft from the record is only half of it. The morning's process also had to build the same question from that draft, character for character, or the manager's answer would belong to a question the run never asked, and the arguments check at the manager's call would fail. It did, because it computed the question from the same things the evening's had: the input, the draft, and the time, all from the record.
The time is the one to look at. Had main read the system clock directly, as desk.py could with datetime.now(), the morning's question would have carried the morning's time, and the run would no longer fit its record. A library that recorded calls for Python could ask you to send every clock read through it, and every file read, environment variable and random number. It can ask; it can't stop a dependency you didn't write from calling datetime.now() anyway. Temporal meets this with a sandbox for Python workflows and a static checker for Go, and its documentation is frank that neither catches everything.
Flow's answer is to have nothing to catch, which the Reference calls closed evaluation. The only way a Flow program can learn the time is the clock Tool, and the same goes for files, the network, the environment and randomness: each is a Tool call, so each answer is in the record. The rule from Why Flow does the work here, and it is the reason Flow is a language rather than a library you call from Python. Fixing a bug in a half-finished run leans on it too.
What resuming costs
Every resume runs your Flow code again from the start of the run, or from its latest checkpoint, a point the runtime saves so it needn't go back further. Flow code is ordinary computation, cheap next to a model call, but in a long run the re-reading adds up, and checkpoints are what keep it short.
? is a decision here as anywhere. If the mail service had come back with a NetworkError, ? would have made it main's result, the run would have ended Err, and whether Dana got her email would be for a person to find out. A payment that timed out shows what deciding it in the program looks like.
And resume only continues a run. A new flow run is a new run with an empty record, and it asks the model again.
Exercises
- Start a new run, answer it with
'"Deny"', and resume it. What is intools/calls.logafterwards, and what does the run return? - Start two runs for the same customer and email, then look at the drafts in the two questions. Why do they differ, and why doesn't that matter for either run?
- Move
let drafted = clock.now()?above the draft, start a run, and look at its history. Which call isroot#1now, and which call do you name with--resend?