Memory and multi-agent systems · Chapter 6 of 16
Updating memory
By the end of this chapter you can keep every value a fact was ever given with who said it and when, decide the current value by time and by who may decide it, keep requests that nobody has agreed to as pending, and tell a family both what stands and what was asked. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Memory
- 5 tasks
- Python
- In the all labs plan
The first 2 chapters of Memory and multi-agent systems are free. This one comes with the all labs plan.
Why you need this
Chapter 4's store keeps the last value it is given. For most facts that is right: the bride moved the mehndi, the mehndi moved. Then Rekha brought two messages to Monday's meeting.
On 2 September, Neelam Mehta, the groom's mother, wrote: "Rekha ji, our side has grown, please make it 400 guests in total." For two days the assistant told the caterer 400. Neelam ji does not decide the count; the two fathers do, and on the 4th they kept it at 320. And on 5 October Kabir Singh wrote "Let's make the budget 70 lakh, I want the fireworks." The fathers decide WD1057's budget, and neither has agreed. The newest message is not always the truth.
Sameer added his own: an import that ran the sessions in the wrong order once quoted a family an old budget. Pooja: "When they ask why we think something, we must be able to say who told us, and when."
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
history.py: a history that never forgets
Write a class FactHistory in history.py. FactHistory(path) opens or creates a SQLite file with a table fact_history with the columns wedding_id, key, value, said_by, said_at, message_id, accepted and why. add(wedding_id, key, value, said_by, said_at, message_id, accepted=True, why=None) adds a row and never updates or deletes one; values are stored as JSON text. history(wedding_id, key) returns every value of that fact, oldest first by said_at, each a dict with value, said_by, said_at, message_id, accepted (a bool) and why. keys(wedding_id) returns the wedding's keys, sorted. current(wedding_id) returns a dictionary of each key and its newest accepted value, leaving out a key with no accepted value.
The check adds rows out of time order, with times in two time zones, and opens the file again with a new FactHistory.
- Task 2
changes.py: what changed
In changes.py, start a fresh history.db (delete the file first) and add every fact of every WD1042 message to a FactHistory (task 1), from the data's facts labels, with the message's wedding id, sender, at and id.
Then print one line for each key given at least two different values, sorted by key: the key, a colon, a space, then every value in time order, each followed by a space and, in brackets, who said it, a comma, a space and the date (the first 10 characters of said_at), the values joined by a space, an arrow made of a hyphen and a greater-than sign, and a space. For example: theme: emerald and gold (Fatima Qureshi, 2026-08-11) -> ivory and gold (Fatima Qureshi, 2026-09-01)
- Task 3
authority.py: who may change what
Write accept(wedding, key, value, by, tentative=False) in authority.py: wedding is a record from baraat's weddings(), by is who said the value. Return (True, None) when policy.md section 1 lets the value stand, or (False, why) with a reason in your own words: something said tentatively is never accepted; the budget only from someone in the record's decides budget list; the guest count from someone in its decides guests list, the bride or the groom (couple), or the planner (planner), whose call notes count as theirs; anything else from anyone.
Under if __name__ == "__main__":, add every labelled fact of all four weddings to a FactHistory on a fresh authority.db with accept's answer (a message's tentative label says whether it was tentative), and print one line per wedding in the order of weddings(): the id, a colon, a space, the number of current facts, then facts, equal to facts.json or facts, different from facts.json, comparing current with baraat's current, for example WD1000: 12 facts, equal to facts.json
- Task 4
pending.py: requests still open
A value that was not accepted is open when it was said after the key's newest accepted value (or the key has no accepted value): nobody who decides has answered it. Write pending(store, wedding_id) in pending.py: store is a FactHistory; return the wedding's open values sorted by key, then oldest first, each a dict with key, value, said_by, said_at, message_id, accepted and why.
Under if __name__ == "__main__":, fill a FactHistory on a fresh pending.db as task 3 does and print every open value, weddings in the order of weddings(), one per line: the wedding id, a colon, a space, the key, a space, the value, from, who said it, a comma, then pending: and why. Then ask lab-chat at temperature 0 What is the budget? for WD1057, with a system message holding an instruction to answer from the notes, a blank line, Notes: on its own line, WD1057's current facts as dash, key, colon, value lines sorted by key, and one more line per open value starting with a dash, a space, pending, a space and the key, holding its value. Print the reply.
- Task 5 · interview question
Interview: fix_me.py, the newest message or the last one read?
LatestStore in fix_me.py keeps the latest value of every fact. Run it: it loads WD1063's sessions in the order the nightly import found them, the newest export first and an old backup after it, and the budget comes out wrong.
Fix put so that a value replaces the stored one only when it was said later, by the message's time, whatever order messages arrive in; times may be in different time zones. Keep the class's name and its methods.
What you will learn
- Changes and conflicts: a fact has a history, not just a value.
- An append-only history with provenance: who, when, which message.
- Newest by time, not by the order messages were read.
- Authority: who may change what (policy.md, section 1).
- Pending requests: what is asked but not decided, in the answer too.
More in Memory
Build "Updating memory" yourself
Baraat Bliss, a Jaipur wedding planner, must remember months of family decisions across many agents. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Memory and multi-agent systems are free. This one comes with the all labs plan.