Skip to content

Sessions with weft/thread

View as Markdown

The core is stateless: a run takes a transcript in and gives one back, and storing it between runs is your job (Continue a conversation). Package weft/thread is the layer above it. A session is a conversation stored as an append-only tree of entries in a backend, with one writer, turns that are durable step by step, and branching, compaction, approvals and child sessions built on the same tree.

Use weft/thread when a conversation must outlive the process, survive a crash mid-turn, or be read by another process while it is written. Stay with weft.Messages when each request carries its whole history and nothing needs to be durable.

What it adds over the core:

  • Durability. The prompt is written to the storage before the run starts, each step’s messages as they join the run, and a turn entry at the end. A crash loses at most the step in flight and the turn’s closing entry: the prompt and every finished step are already stored (thread rule T4).
  • One writer. A Session value is its session’s only writer until it closes; a second writer gets thread.ErrLocked (T1, T2).
  • A tree, not a list. You branch back, fork into a new session and label entries; nothing is rewritten or deleted in place.
  • Busy policies. A message sent while a turn runs is queued, rejected, steered into the turn, interrupts it, or rolls it back, and every message accepted that way is written to the storage first.
  • Compaction, approvals and pools. Long sessions are summarized without deleting anything, parked tool calls survive restarts, and child sessions run under a bound.

weft/thread is a package of the framework module, github.com/weftgo/weft, so it comes with go get github.com/weftgo/weft@v0.20.2. It is pre-1.0: its API and stored format may change between minor versions, each change recorded in the CHANGELOG.

The section has thirteen pages: this one and the twelve below, in reading order.

  1. What is a session?: the entry tree, one writer per session, and what is durable when.
  2. Store sessions: JSONL, SQLite or memory: pick a backend, list and delete sessions, tail one live.
  3. Write your own thread.Storage: the five methods, the optional capabilities, and threadtest.
  4. Send turns and wait for them: Send, Wait, events, outcomes and run options.
  5. What happens when you send while a turn runs?: the busy policies, receipts and Steer.
  6. Label, branch and fork a session: navigate the tree, fork, titles and public ids.
  7. Keep long sessions inside the context window (compaction): summaries that replace old entries in the context.
  8. Approvals in sessions: parked calls that survive restarts, grants and approvers.
  9. Sign approval decisions: decisions that cross a process boundary.
  10. Run bounded child sessions with a pool: delegation with receipts.
  11. Operate sessions in production: backups, locks, shutdown and crash recovery.
  12. The session log format: every entry kind and its fields.