# Mission 17.2: The briefing came with yesterday’s calendar

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/einsatz-17-2

Belongs to [Chapter 17: Autonomous agents](https://llm.yoga/en/17-selbststaendige-agenten.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Lukas**, account manager:

> “My morning briefing came right on time at seven today, just with yesterday’s appointments. I prepared for a call that had already happened and almost missed the customer meeting at ten. According to monitoring, everything ran without errors.”

Automatic reports and briefings are among the first things teams build with AI; whether they can be trusted is then a question for you. The briefing is a routine: a small script that starts on its own every morning. A sync at 5 a.m. fetches the calendar through the calendar service’s API, the way programs request data, and puts it in a cache. At the end, every script reports an exit code, a number for “worked” (0) or “error” (anything else).

Yesterday 5:00 a.m.: sync successful. Today 5:00 a.m.: API unreachable, the cache stays at yesterday’s state.

Today 7:00 a.m.: the briefing takes the cache, 26 hours old, and ends with exit code 0. Monitoring reports nothing. (Example)

**Question 1 (decision):** What should the script have done at 7 a.m. today?

- A) Send yesterday’s appointments, with the note “As of: yesterday.”
- B) Send nothing at all and try the sync again tomorrow.
- C) Report that the calendar is outdated and end with an error code.

## What’s behind it

Picture a weather report on the radio. The weather station goes down, so the announcer simply reads yesterday’s report, in the same calm voice. It sounds right, and nobody takes an umbrella.

That’s exactly how routines fail when they carry on with old data. They look successful and still deliver something wrong. The rule **“no stale data”** guards against this: if the source data is missing or outdated, the routine writes a line saying what’s missing and ends with an error code. It never carries on with the last known state.

For this, the routine asks itself three questions, in this order. Is there data? Is it newer than the allowed limit, about 24 hours for the briefing? And if it’s too old: what does it do then? The first two checks cost nothing and come before any actual work.

The error code isn’t a detail. A long-runner’s monitoring only sees whether a run ended with 0 or with an error. With 0, everything is green, the briefing is wrong, and nobody notices. Only the error code turns a silent error into a visible one.

For Lukas this means: from now on, the script checks the cache’s age. If it’s too old, it reports “Calendar outdated” and ends with an error code. A partial briefing with mail and weather can still go out, but only with a clear note at the top that the calendar is missing.

### In more detail: missed slots and the right limit

If the routine runs as a scheduled service on macOS, then according to its man page launchd runs a job that the computer slept through when it next wakes up. It merges several missed runs into one. cron, by contrast, skips missed runs. So the routine has to expect to run at 11 a.m. instead of 7 a.m., and the limit has to fit that: a calendar synced at 5 a.m. is still usable at 11 a.m., one from yesterday isn’t.

The Mac exercise builds exactly this check into a small Python script and tests all three cases: fresh, outdated, missing.

### How tybo does it: rule, wrapper and watch

In tybo the rule is spelled out in the docs on routines (in German; here translated): “If the source data is missing or outdated, the routine writes a line saying what’s missing and ends with exit code 1; never carry on with old data.” The line belongs on the error output; the wrapper then takes it into its notification as the error line.

Every routine starts through a shared wrapper (`src/routine-run.ts`). It passes output and exit code through and records the last 20 runs per routine: start, duration, exit code, last lines. launchd itself only knows the last exit code. If a routine ends with 1, the wrapper reports it right away. Alongside that, a routine watch in the bot checks every ten minutes whether a run didn’t happen at all.

tybo’s morning briefing puts its text together from data sources without a model, and notifications take a shared path to Telegram and the browser. Source: `docs/routinen.md` and `docs/architecture.md` in the tybo repo.

## Try it

**Lukas’s morning, question by question**

**Task:** Play through today’s 7 a.m. run the way the script should have handled it under the “no stale data” rule. If you end up somewhere else, tap a different answer further up.

**Step k1: Is there calendar data?**
- No, neither from the API nor in the cache. (continue at e1)
- Yes, there’s a state in the cache. (continue at k2)

**Step k2: Is it less than 24 hours old?**
- Yes, this morning’s sync worked. (continue at e2)
- No, it’s from yesterday morning, 26 hours old. (continue at k3)

**Step k3: What does the script do with yesterday’s data?**
- It builds the briefing from it and ends with exit code 0. (continue at e4)
- It writes “Calendar outdated” and ends with exit code 1. (continue at e3)

**End e1: Result: calendar missing** Line “Calendar missing,” exit code 1. Monitoring reports the failure. Correct for a morning with no state at all.

**End e2: Result: fresh data** Briefing with today’s appointments, exit code 0. That’s what a normal morning looks like.

**End e3: Result: outdated, and everyone knows it** Line “Calendar outdated: 26 hours old,” exit code 1. Lukas learns that his appointments are missing before he plans, and monitoring reports the failed sync.

**End e4: Result: old briefing, all green** Yesterday’s appointments, exit code 0. Monitoring sees success, and Lukas prepares for a call that has already happened.

## Check

**Question 2 (check question):** The script now writes “Calendar outdated.” Why does it also need the error code?

- A) Without an error code, the computer won’t start the script again.
- B) The error code deletes the old cache so it won’t be used again.
- C) Monitoring only sees the code; the line alone might go unread.

**Question 3 (check question):** Lukas: “Then on days like that, at least send me mail and weather, without the calendar.” Does that work?

- A) No, a briefing with a gap is always worse than none at all.
- B) Yes, if it says clearly at the top that the calendar is missing.
- C) Yes, the gap stands out anyway, because there are no appointments in it.

## Done

**What you can do now**

- You can explain the “no stale data” rule: report missing or outdated sources and end with an error code.
- You can go through a routine’s three questions in the right order: is there data, is it fresh enough, what otherwise.
- When someone shows you an automatic report that “always runs through,” you ask what it does when its source fails, and whether it then ends with an error code.

### Two cards for review

**Card 1:** True or false? “A routine that runs through without errors has worked.”

**Card 2:** The calendar source fails. Can the morning briefing still send mail and weather?

- [Next mission: Since yesterday, only the fallback model answers](https://llm.yoga/en/einsatz-17-3.md)
- [Try it on your Mac](https://llm.yoga/en/mac-17-2.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [A day in the life of a long-runner](https://llm.yoga/en/17-selbststaendige-agenten-2.md)
- [What sets a long-runner apart from a chat](https://llm.yoga/en/17-selbststaendige-agenten-1.md)
- [What makes building one yourself hard](https://llm.yoga/en/17-selbststaendige-agenten-3.md)
- [Exercise: a routine with an age check](https://llm.yoga/en/17-selbststaendige-agenten-3.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is C.
- A: That seems honest, but it’s still a briefing with old data. Lukas plans with appointments that are no longer right anyway, and monitoring sees success again.
- B: Then the briefing is missing without explanation. If the script ends with 0, monitoring doesn’t notice anything either. A silent failure is the most dangerous kind.
- C, correct: The line tells Lukas what’s missing before he plans his day. The error code makes monitoring react, so someone fixes the sync.

The case leads to End e3.
- Step k1, „No, neither from the API nor in the cache.“: Then there would be nothing to check at all.
- Step k1, „Yes, there’s a state in the cache.“: That’s how it was today: the API was down, the cache wasn’t empty. The next question settles whether that state is any good.
- Step k2, „Yes, this morning’s sync worked.“: That would be a normal morning.
- Step k2, „No, it’s from yesterday morning, 26 hours old.“: The 5 a.m. sync failed today, so the state is from 5 a.m. yesterday.
- Step k3, „It builds the briefing from it and ends with exit code 0.“: That’s the easy path, and it’s exactly what spoiled Lukas’s morning.
- Step k3, „It writes “Calendar outdated” and ends with exit code 1.“: That’s what the rule demands: say what’s missing and make the error visible.
- End e1: Doesn’t fit the case: there was a state in the cache, just an old one.
- End e2: Doesn’t fit the case: today the API was unreachable, and the state is from yesterday morning.
- End e4: That’s exactly how it went today. Pick the other answer at the last question.

**Question 2:** The correct answer is C.
- A: A scheduled service starts at the next time anyway. This isn’t about repeating, it’s about someone seeing the error.
- B: An exit code doesn’t change any files. It’s just the number the script uses to report whether the run worked.
- C, correct: With exit code 0, everything stays green, no matter what the output says. Only the error code makes the silent error visible, and the line then says what’s missing.

**Question 3:** The correct answer is B.
- A: A partial briefing can be useful. The rule forbids old data, not fresh parts.
- B, correct: Mail and weather are fresh and can be sent. The note keeps Lukas from taking an empty calendar for a free day.
- C: On a day without appointments, the briefing would look exactly the same. Without a note, Lukas can’t tell the failure from a free day.

**Card 1:** **False.** It can run through with outdated data and still deliver something wrong, such as a briefing with yesterday’s calendar. Only the “no stale data” rule makes the error visible: report missing or outdated sources and end with an error code.

**Card 2:** Yes, fresh parts can go out, but only with a clear note at the top that the calendar is missing. Otherwise the reader takes an empty calendar for a free day. The routine never uses old calendar data, and it reports the failure with an error code.
