diff -ruN '--exclude=__pycache__' base/README.md head/README.md --- base/README.md 2026-11-12 18:36:31.000000000 +0000 +++ head/README.md 2026-11-12 19:16:07.513668397 +0000 @@ -80,6 +80,7 @@ | `memory["asked"]` | the fact the last recall question asked about, `{"slot", "turn"}`, so an elided ask ("what did I say it was again?") is answered about the same fact | the conversation lane | | `memory["topic"]` | what the conversation is about now | the conversation lane | | `memory["facts_log"]` | every fact the user stated, in order | the conversation lane | +| `memory["task"]` | what the chat is doing now: `{"goal", "topic", "subject", "lines", "details" ({"numbers", "not", "named", "corrected"}), "offered" ({"items", "turn"}: the titles the bot named in its last reply), "mood", "opened", "updated", "before"}`, or `None`. Opened by an ask that names a thing of its own; a follow-up, a bare detail or a statement inside it keeps it (`bot/memory/task.py`, written from `memory_store.observe`). A follow-up that names no thing of its own is about `task["subject"]`, two or ten lines up as well as one. `python3 tools/task_trace.py "line" ...` prints it per turn | the conversation lane | Helpers in `bot/memory/` for modules that answer a follow-up: `bot.memory.subject(memory)` is the noun phrase the conversation is about now ("birthday cake"), `bot.memory.gist(text)` is that diff -ruN '--exclude=__pycache__' base/bot/memory/task.py head/bot/memory/task.py --- base/bot/memory/task.py 1970-01-01 00:00:00.000000000 +0000 +++ head/bot/memory/task.py 2026-11-12 19:23:24.674195911 +0000 @@ -0,0 +1,186 @@ +"""What the conversation is doing: one shared key, `memory["task"]`, kept across turns. + +A follow-up is read against the line before it (`understood["leans_on"]`), and that is one line of +memory: "a cocker spaniel" then "how do i stop her biting" leans on the spaniel line, and the puppy +the chat is about - said two lines up - is gone. The lookup then matches the line on its own words +and returns the insect-bite card. The chat knew it was about a puppy; nothing kept that. + +This module keeps it. One dict, written in `observe` from readings that already exist, and read by +whoever needs to know what the chat is doing: + + {"goal": "getting a puppy", "topic": "pets", "subject": "puppy", "lines": [...], + "details": {"numbers": [...], "not": [...], "named": [...]}, + "offered": {"items": ["Arrival", "Contact"], "turn": 3}, + "mood": None, "opened": 1, "updated": 3} + +It changes no reply by itself. The rules: +- A line that names a thing of its own and asks or plans something opens a new task. +- A line that names no new thing (a follow-up, a bare detail, "any tips to cut back") keeps the task + and adds what it says to `details` and `lines`. +- "changing subject, ..." and a goodbye close it. +- What the bot offered is read from its last reply in `memory["turns"]`: titles it set in italics, + so "seen that one" and "another" can be resolved against them. +""" +import re + +KEY = "task" +_KEEP_LINES = 12 +_ITALIC = re.compile(r"\*([^*]{2,60})\*") +# Words a person uses to give a detail of the task in hand rather than a new thing to talk about. +_DETAIL_SUBJECTS = frozenset(( + "budget", "price", "cost", "time", "age", "size", "weight", "date", "day", "week", "month", + "year", "kids", "children", "people", "guests", "oven", "temperature", "temp", +)) +_ASKING = frozenset(("question", "request")) + + +def get(memory): + """The task in hand, or None.""" + task = memory.get(KEY) if isinstance(memory, dict) else None + return task if isinstance(task, dict) else None + + +def _turn_index(memory): + turns = memory.get("turns") if isinstance(memory, dict) else None + return len(turns) if isinstance(turns, list) else 0 + + +def _offered(memory): + """Titles the bot set in italics in its last reply, in order.""" + turns = memory.get("turns") or [] + if not turns: + return [] + said = str((turns[-1] or {}).get("bot") or "") + # A title made of the person's own words is the bot saying their line back ("I don't know + # *It Long* well enough"), not a thing it offered. + asked = set(re.findall(r"[a-z0-9']+", str((turns[-1] or {}).get("user") or "").lower())) + seen, out = set(), [] + for title in _ITALIC.findall(said): + title = title.strip() + words = set(re.findall(r"[a-z0-9']+", title.lower())) + if words and words <= asked: + continue + if title.lower() not in seen: + seen.add(title.lower()) + out.append(title) + return out + + +def _new_thing(understood, task): + """The thing this line names of its own, when it is not part of the task in hand.""" + subject = understood.get("ask_subject") or understood.get("subject") + if not isinstance(subject, str) or not subject.strip(): + return None + subject = subject.strip().lower() + for entity in understood.get("entities") or []: + if isinstance(entity, dict) and str(entity.get("text") or "").lower() == subject \ + and entity.get("type") == "person" and task: + return None # inside a task, a person is who it is for or with, not a new thing + head = subject.split()[-1] + if head in _DETAIL_SUBJECTS: + return None + if task: + known = " ".join([str(task.get("subject") or ""), str(task.get("goal") or "")] + + [str(line) for line in task.get("lines") or []]).lower() + if head in known.split(): + return None + return subject + + +def _opens(understood, task): + """True when this line starts a task of its own.""" + if understood.get("followup") and task: + return False + thing = _new_thing(understood, task) + if thing is None: + return task is None and bool(understood.get("topic_own")) \ + and str(understood.get("intent") or "") in _ASKING + if task is None: + return True + # With a task in hand only an ask opens another: a statement that names a thing is more + # about the task ("i keep checking my phone" after a break-up), and a correction puts the + # task right rather than starting one ("sorry i meant porto not lisbon"). + intent = str(understood.get("intent") or "") + if intent == "correction": + return False + return intent in _ASKING or str(understood.get("kind") or "") == "question" + + +def _parent(task, now, reach=6): + if not task or now - int(task.get("updated") or 0) > reach: + return None + return {key: task.get(key) for key in ("goal", "topic", "subject", "details")} + + +def _details(task, understood): + details = task.setdefault("details", {}) + for number in understood.get("numbers") or []: + if isinstance(number, dict) and number.get("text"): + details.setdefault("numbers", []).append( + {"value": number.get("value"), "unit": number.get("unit"), "text": number["text"]}) + if str(understood.get("intent") or "") == "correction": + details.setdefault("corrected", []).append( + str(understood.get("clean") or understood.get("text") or "")) + for thing in understood.get("excludes") or []: + details.setdefault("not", []).append(thing) + for entity in understood.get("entities") or []: + if isinstance(entity, dict) and entity.get("text"): + named = details.setdefault("named", []) + if entity["text"] not in named: + named.append(entity["text"]) + + +def observe(memory): + """Bring `memory["task"]` up to date with this turn's reading. Never raises.""" + try: + _observe(memory) + except Exception: # noqa: BLE001 - a reader of the chat must never cost the reply + pass + + +def _observe(memory): + understood = memory.get("understood") + if not isinstance(understood, dict): + return + now = _turn_index(memory) + task = get(memory) + offered = _offered(memory) + if task is not None and offered: + task["offered"] = {"items": offered, "turn": now - 1} + intent = str(understood.get("intent") or "") + if intent == "farewell" or understood.get("change_of_subject"): + task = None + memory[KEY] = None + line = str(understood.get("clean") or understood.get("text") or "") + if intent in ("about_bot", "presence", "empty") or understood.get("about_bot"): + return + if intent in ("greeting", "thanks", "laugh", "ack", "affirm") and not \ + understood.get("topic_own"): + return + if _opens(understood, task): + task = { + "goal": " ".join(str(word) for word in understood.get("focus") or []) or line, + "topic": understood.get("topic_own") or understood.get("topic"), + "subject": _new_thing(understood, None), + "lines": [line], + "details": {}, + "offered": {"items": offered, "turn": now - 1} if offered else None, + "mood": None, + "opened": now, + "updated": now, + # The task this one was asked inside, for a few turns: "whats the food like" on a + # trip is a question of its own and still about the trip. + "before": _parent(task, now), + } + memory[KEY] = task + elif task is not None: + task["lines"] = (task.get("lines") or [])[-(_KEEP_LINES - 1):] + [line] + task["updated"] = now + if not task.get("subject"): + task["subject"] = _new_thing(understood, None) + else: + return + _details(task, understood) + if understood.get("polarity") == -1 and understood.get("state_owner") in ("self", None) \ + and intent in ("news", "feeling", "statement"): + task["mood"] = {"low": True, "turn": now} diff -ruN '--exclude=__pycache__' base/bot/modules/memory_store.py head/bot/modules/memory_store.py --- base/bot/modules/memory_store.py 2026-11-12 18:36:34.000000000 +0000 +++ head/bot/modules/memory_store.py 2026-11-12 18:58:38.748341380 +0000 @@ -655,6 +655,8 @@ _the_limit_they_share(text, memory) except Exception: # noqa: BLE001 - the store never crashes a turn pass + from ..memory import task as _task # what the chat is doing, for every reader + _task.observe(memory) # The ages in words, for saying a group of children back the way the store says them elsewhere diff -ruN '--exclude=__pycache__' base/tests/test_the_task_is_kept.py head/tests/test_the_task_is_kept.py --- base/tests/test_the_task_is_kept.py 1970-01-01 00:00:00.000000000 +0000 +++ head/tests/test_the_task_is_kept.py 2026-11-12 19:16:40.144458362 +0000 @@ -0,0 +1,46 @@ +"""memory["task"]: what the chat is doing is kept across turns, and a follow-up does not lose it.""" +import io +import contextlib +import unittest + +from bot import runtime + + +def chat(*lines): + memory = {} + for line in lines: + with contextlib.redirect_stdout(io.StringIO()): + runtime.turn(line, memory) + return memory.get("task") or {} + + +class TaskTest(unittest.TestCase): + def test_a_follow_up_keeps_the_subject_two_lines_up(self): + task = chat("we just adopted a kitten", "a little tabby", "how do i stop her scratching") + self.assertEqual(task.get("subject"), "kitten") + self.assertEqual(task.get("topic"), "pets") + + def test_a_line_with_no_thing_of_its_own_keeps_the_task(self): + task = chat("im saving up for a new car", "about 3000 by next summer", "any tips to cut back") + self.assertEqual(task.get("subject"), "car") + self.assertIn("3000", [n["text"] for n in task["details"]["numbers"]]) + + def test_a_statement_inside_a_task_does_not_start_another(self): + task = chat("my boyfriend and i split up on sunday", "i keep checking my phone") + self.assertEqual(task.get("topic"), "friends-and-relationships") + + def test_a_new_ask_with_a_thing_of_its_own_opens_a_task(self): + task = chat("my printer keeps jamming", "whats a good recipe for banana bread") + self.assertNotEqual(task.get("subject"), "printer") + + def test_what_the_bot_offered_is_kept(self): + task = chat("recommend a film?", "seen them") + self.assertTrue((task.get("offered") or {}).get("items")) + + def test_a_change_of_subject_closes_it(self): + task = chat("my printer keeps jamming", "changing subject, how do i stop my bin smelling") + self.assertNotEqual(task.get("subject"), "printer") + + +if __name__ == "__main__": + unittest.main() diff -ruN '--exclude=__pycache__' base/tools/task_trace.py head/tools/task_trace.py --- base/tools/task_trace.py 1970-01-01 00:00:00.000000000 +0000 +++ head/tools/task_trace.py 2026-11-12 19:15:48.233202208 +0000 @@ -0,0 +1,25 @@ +"""Print memory["task"] after every turn: python3 tools/task_trace.py "line" ... or --jsonl FILE +or with --jsonl FILE to trace every chat in a file.""" +import sys, io, json, contextlib +sys.path.insert(0, ".") +from bot import runtime +def trace(lines): + m = {} + for line in lines: + with contextlib.redirect_stdout(io.StringIO()): + out = runtime.turn(line, m) + t = m.get("task") or {} + det = t.get("details") or {} + print(f"U: {line}\n B[{out['module']}]: {out['reply'][:110]}") + if t: + print(f" task: goal={t.get('goal')!r} topic={t.get('topic')} subject={t.get('subject')!r} opened={t.get('opened')} " + f"nums={[n['text'] for n in det.get('numbers', [])]} not={det.get('not', [])} named={det.get('named', [])} " + f"offered={(t.get('offered') or {}).get('items')} mood={t.get('mood')}") + else: + print(" task: None") +if sys.argv[1] == "--jsonl": + for l in open(sys.argv[2]): + if l.strip(): + r = json.loads(l); print(f"## {r['id']}"); trace(r["turns"]); print() +else: + trace(sys.argv[1:])