Thread Memory Protocol

August 21, 2026 · View on GitHub

The stable contract between Thread and any host base. A base "has Thread memory" when it satisfies the three weak capabilities below; everything else is implementation.

This document is the contract anchor for multi-base adapters (B4a). It describes current state, not process.

1. The three weak capabilities a base must provide

#CapabilityPurposeExamples
1Capture hookHand Thread every conversation event (user/assistant messages, tool calls/results) as it happensdsh session/event subscription; Qoder hooks; Claude Code hooks
2Context injectionLet Thread add context the model sees next turndsh agent.inject; Qoder additionalContext; MCP-visible instructions
3Compaction boundary signalTell Thread when the base compacts contextdsh compaction/summary event; Qoder PostCompact hook

Optional (capability-gated enhancements):

CapabilityEnablesAvailability
Native tool registrationQuery tool in the model's tool schema (strongest channel)dsh ctx.tools.register; MCP tools elsewhere
Skill catalog registrationBehavior contract discoverable via the base's skill loaderdsh ctx.skills.register; SKILL.md-style skills elsewhere
Programmable compactionThread-triggered silent compactiondsh ctx.compaction.compactNow

2. The query tool contract

Tool name: query_session_memory

One tool, two modes:

2.1 Semantic search (default)

ParameterTypeMeaning
querystringKeyword/phrase search over the indexed event stream
kindenumFilter: user_message / assistant_message / tool_call / tool_result / compact_checkpoint / goal / decision / feedback
session_idstringTarget session; defaults to the most recent active one
limitintMax result segments (default 20, max 50)
since / untilISOTime bounds (exact-query path)
orderenumasc / desc (default desc)
count_onlyboolReturn counts instead of rows
token_budgetintResult token budget

2.2 Navigation primitives (nav)

Filesystem-style navigation over the association structure (session → assets/documents, plus lineage edges):

navtargetReturns
lssession idThat session's outputs (assets) and pending todos
lsasset idThe asset's related edges
cdasset id / event id / doc pathNode detail: title, source event, related edges
catasset id / event id / doc pathFull content (asset file text or event body)
grep— (query = keyword)Search hits with context + matching asset index entries

All navigation responses use one envelope:

interface NavResult {
  kind: "list" | "node" | "content" | "hits";
  title: string;
  items: NavItem[];
  context?: { session_id?: string; asset_id?: number; evidence?: string[] };
}
interface NavItem {
  id: string;
  type: "session" | "asset" | "event" | "decision" | "todo";
  label: string;
  ref?: string;
}

2.3 Error contract

  • Nothing found → status: "not-found" plus a follow-up suggestion; never a fabricated answer.
  • Isolated content → visibility markers, never silent leaks.
  • Unreadable source → explicit [文件不可读: <path>] marker, never silent.

3. Storage model (contract surface)

TableMeaning
eventsLossless append-only event stream (the source of truth)
goals / decisions / feedbackStructured state tables with status machines
pending_candidatesStaged candidates; promoted to decisions via explicit update, nothing becomes a decision implicitly
knowledge_assetsRegistered outputs (documents/reports) with lineage edges
todosPending work sedimented from goals at closing
lineage_edgesDeterministic relations: produces, references, evidence, precedes, next_step
thread_metaDelivery watermarks (cross-agent delta)

4. Adapter conformance

An adapter conforms when:

  1. It forwards all conversation events through capability #1 with stable origins (idempotent capture).
  2. It injects the status card / anchors through capability #2.
  3. It forwards compaction boundaries through capability #3 and re-anchors state after them.
  4. It exposes query_session_memory with the contract in §2 (native registration when available, MCP otherwise).
  5. It honors session isolation and cross-agent delta semantics identically.