Lesson 07 · memory

State is written to disk, surviving compression

"State that survives compression — because it's outside the conversation."

⏱ ~10 min · 📝 3 interactive widgets · 🧑‍💻 Based on shareAI-lab · s07_task_system.py

What is the difference between TodoWrite and Task?

TodoManager of s03 can also list tasks - but it is stored in memory. As soon as auto_compact mentioned in s06 is triggered, messages[] is replaced by summary, and the status of TodoManager is disappeared (because it is in-memory).

The Task of s07 is different: each task is a .tasks/task_42.json file. Whether the context is compressed, the process is restarted, or the agent is replaced - as long as the disk file still exists, the task still exists.

# .tasks/task_12.json
{
  "id": 12,
  "subject": "Refactor auth middleware",
  "description": "Extract JWT logic to shared module",
  "status": "pending",
  "blockedBy": [8, 11], # Must complete #8 and #11
  "owner": ""
}
Selection rules: Use todo for temporary to-dos (forget about it once you finish it); use task for persistent tasks (retained across sessions and dependent).

blockedBy · Dependency graph temper

blockedBy is a list of task ids - these must be completed before the current task is "executable".

The implementation of s07 has a neat detail: When a task is completed, it will automatically remove itself from the blockedBy list of all other tasks.

def _clear_dependency(self, completed_id: int):
    # Scan every task, remove completed_id from their blockedBy
    for f in self.dir.glob("task_*.json"):
        task = json.loads(f.read_text())
        if completed_id in task.get("blockedBy", []):
            task["blockedBy"].remove(completed_id)
            self._save(task)

In this way, the agent does not need to maintain a separate "what tasks are unblocked" table - as long as it traverses .tasks/, the one found status=="pending" and not blockedBy is now executable.

Dependency graph interaction

The widget below gives you a dependency graph of 5 tasks. Click "complete" to see blockedBy automatically updated and which tasks become "executable" (highlighted in green).

Can it still survive after being compressed?

s06 mentioned that auto_compact will replace messages[] with a summary. But the task is not affected because it is on disk. Test it: Let the agent do 5 tasks. After compressing the conversation, it scans .tasks/ and it can continue in the same place.

Interactive

Widget 1 · Dependency Graph · Click complete to see dependency updates

Topological relationship of 5 tasks. Click the button on any pending task to change its status and observe the changes in the blockedBy array and "next executable" list.

Task 列表(.tasks/ 目录)
当前可执行(status=pending, blockedBy=[])
依赖关系图
Interactive

Widget 2 · Compression Survival · task survive auto_compact

Let the agent create 3 tasks, then trigger auto_compact (messages are cleared), and then restore - see if the tasks are still there.

.tasks/ 目录
messages[]
Interactive

Widget 3 · Dependency Chain · Give you a picture to answer which tasks can be activated now

Dependencies of 5 tasks. Answer: In a given situation, what are the next steps that can be done (multiple choices possible).

答对 0 / 4