Documentation Index
Fetch the complete documentation index at: https://simplellmfunc.cn/llms.txt
Use this file to discover all available pages before exploring further.
SelfRef
SelfRef is SimpleLLMFunc’s system for durable, self-modifying context — letting agents remember across turns, compress their history, and delegate work to child agents.Two Components, Sharp Separation
SelfReference (durable backend)
The stateful storage layer. Lives across invocations. Holds:- History per memory key — the full conversation transcript
- Experiences — durable remembered facts/lessons
- Summaries — compaction checkpoints
- Fork state — child agent handles and results
SelfReference is implemented as a small public facade over focused internal components:
| Module | Responsibility |
|---|---|
state.py | Public facade, lifecycle, compatibility exports |
store.py | Durable history/source storage |
active_turn.py | Active memory key, fork context, runtime toolkit, template params, active ReAct state |
mutations.py | Pending compaction/context/destructive mutation queues |
memory_api.py | self_reference.memory[...] proxy and handle API |
context_memory.py | Context snapshots, experience CRUD, compaction commit, direct memory editing |
agent_binding.py | Bound recursive agent callable state |
fork_manager.py | Fork/spawn/gather lifecycle and result materialization |
fork_utils.py | Fork helper functions and compatibility constants |
SelfReference API; it makes the durable backend easier to maintain and reason about.
SelfRefSession (invocation-scoped plugin)
The per-call lifecycle adapter. Created fresh for each@llm_chat invocation. Implements ReAct hooks:
collect_context_mutations()— provides selfref-originated internal transcript patches before each compilefinalize()— persists final state back to the SelfReference backend after the turn ends
How They Connect
DataFromSelfRef: The Snapshot
When SelfRef is active, the compile pipeline receives aDataFromSelfRef snapshot:
- The system prompt (base + rendered experiences)
- What messages the LLM sees (working_messages after compaction)
- What experiences are active
The 6 Runtime Primitives
When a@llm_chat agent has SelfRef enabled and uses PyRepl, these primitives are available inside execute_code:
Context Primitives
| Primitive | What It Does |
|---|---|
runtime.selfref.context.inspect() | Returns read-only snapshot: active key, experiences, summary, messages, has_pending_compaction |
runtime.selfref.context.remember(text) | Stores a durable experience through an internal experience patch |
runtime.selfref.context.forget(experience_id) | Removes an experience through an internal experience patch |
runtime.selfref.context.compact(goal, instruction, discoveries, completed, current_status, likely_next_work, relevant_files_directories, remember=[]) | Queues context compaction → later applies an internal summary patch |
Fork Primitives
| Primitive | What It Does |
|---|---|
runtime.selfref.fork.spawn(task, instruction, ...) | Spawns a child agent with pre-fork context snapshot. Returns {fork_id, status} |
runtime.selfref.fork.gather_all(include_history=False) | Waits for all spawned children. Returns dict[fork_id → ForkResult] |
Experience Lifecycle
Experiences are durable facts stored in the system prompt:remember("...")→ records an experience through the runtime patch boundaryforget("exp_001")→ removes it- Experiences survive compaction — they’re stored in the system prompt, not the working transcript
- They’re rendered by
render_system_prompt_with_experiences()during compile Stage 2
Compaction Lifecycle
When context grows too large, the agent can compact:- Compaction is queued (not applied immediately)
- After the current tool batch completes, the runtime applies a summary patch
- The system prompt is preserved
- Working transcript is replaced with the summary message
- Items in
rememberbecome durable experiences - SelfReference backend stores the new state
Fork Lifecycle
Forks let an agent delegate work to child agents that inherit context:- Children inherit the pre-fork context snapshot (not the parent’s in-flight state)
- Children cannot modify the parent’s context
gather_all()blocks until all children complete- Results contain
status,response, and optionallyhistory
Activation
SelfRef is activated on@llm_chat via self_reference_key:
- Creates/retrieves the
SelfReferencebackend for this key - Wraps each invocation in a
SelfRefSession - Injects selfref primitives into the PyRepl runtime
- Handles finalization (persist updated history) after each turn
Building Guide: llm_chat
How to use @llm_chat with SelfRef in practice.
Advanced: SelfRef Engineering
Advanced patterns: multi-key memory, compaction strategies, fork orchestration.