Memory and multi-agent systems in TypeScript · 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
- TypeScript
- In the all labs plan
The first 2 chapters of Memory and multi-agent systems in TypeScript 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."
An upsert cannot do any of this. It keeps one value per key and throws the rest away, so it cannot tell a decision from a request, cannot say who said 38 lakh, and believes whichever message it happened to read last.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
history.ts: a history that never forgets
Write and export a class FactHistory in history.ts. new FactHistory(path) opens or creates a SQLite file with node:sqlite, with a table fact_history with the columns wedding_id, key, value, said_by, said_at, message_id, accepted and why.
add(entry) adds one row and never updates or deletes one: entry is an object with wedding_id, key, value, said_by, said_at, message_id, and optionally accepted (true when left out) and why (null when left out); values are stored as JSON text. history(weddingId, key) returns every value of that fact, oldest first by said_at, each an object with value, said_by, said_at, message_id, accepted (a boolean) and why. keys(weddingId) returns the wedding's keys, sorted. current(weddingId) returns an object 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.ts: what changed
In changes.ts, start a fresh history.db (delete the file first) and add every fact of every WD1042 message to a FactHistory (task 1), from the message'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.ts: who may change what
Write and export accept(wedding, key, value, by, tentative = false) in authority.ts: wedding is a record from baraat's weddings(), by is who said the value. Return [true, null] 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; the guest count from someone in its decides.guests, the bride or the groom (couple), or the planner (planner), whose call notes count as theirs; anything else from anyone.
Inside if (import.meta.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 field 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 your current with baraat's current, for example:
WD1000: 12 facts, equal to facts.json
- Task 4
pending.ts: 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 and export pending(store, weddingId) in pending.ts: store is a FactHistory; return the wedding's open values sorted by key, then oldest first, each an object with key, value, said_by, said_at, message_id, accepted and why.
Inside if (import.meta.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.ts, the newest message or the last one read?
LatestStore in fix-me.ts 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, its methods and the export.
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, and why comparing ISO strings as text goes wrong across time zones.
- 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. You build its assistant in TypeScript. 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 in TypeScript are free. This one comes with the all labs plan.