Skip to content

long running tasks memory

sherry-wiki edited this page Nov 17, 2026 · 8 revisions

🧠 Memory & Continuity

Sherry Wiki · English · 中文 · 日本語 · 한국어

← Back to index

Part of Long-Running Tasks: the two-layer memory system, summary ↔ TaskFlow coordination, subagent memory backflow, one-line tool-output summaries, session continuity, and TaskFlow auto-resume.


🧠 Layered Memory

Two layers, distinguished by how they reach the model:

Layer Store Location In the prompt?
L1 — curated memory memory_entries rows (targets: memory agent notes, user profile, facts system pitfalls) MesMemory SQLite (mes_memory.db), memory_entries table Yes — a frozen snapshot, always injected
L2 — raw history mes_memory.db (SQLite, WAL, FTS5) src/store/mes_memory/mes_memory.db No — retrieved by context_engine / message_search

An entry is one SQLite row in memory_entries; the § delimiter survives only in the legacy-import and GET/PUT /memory compatibility paths. Entries may be multiline, with no YAML frontmatter and no bullet prefixing.

Layer 1 is managed by MemoryStore (agent/tools/memory.py::load_from_store): per-target character limits 2200 (memory), 1375 (user) and 1375 (facts) plus a 200-active-entry cap per target (tiny rows would otherwise accumulate under the char budget), the injection scan (_MEMORY_THREAT_PATTERNS) that rejects prompt-injection and credential-exfiltration content, exact-match dedup, and SQLite WAL instead of file locks and atomic rewrites. Rows carry origin_session, usage_count and last_used; the rendered block appends [origin: …, created: …, used: Nx] only when provenance is known, and memory(action="read") increments usage. The live rows mutate immediately, while the prompt uses a frozen snapshot captured at load_from_store() to keep the prefix cache stable for the session. The legacy MEMORY.md/USER.md/FACTS.md files are imported once at boot (context_engine/store/memory_migration.py) and renamed to *.md.bak; any existing row makes that import a no-op.

The memory tool is tagged scope="main_only", so subagents never see it.

Graph-state checkpoint store. Sessions also persist their LangGraph state to src/checkpoints/sqlite.db, separate from the two layers above. The global keep-latest prune runs ONCE at boot (server/service/checkpoint_maintenance.py), not on every built_agent() call (that put a competing writer on the shared DB every turn); per-thread prunes still run on child release and session purge. With auto_vacuum=0 a DELETE only frees pages, so the maintenance pass reads PRAGMA freelist_count × page_size and runs VACUUM only when the freed space exceeds _VACUUM_THRESHOLD_BYTES (10 MB, agent/checkpointer/thread_safe_checkpointer.py) — fail-open: a VACUUM error is logged and the prune result stands.

🔎 Semantic Recall & Consolidation

Both are opt-in and fail-open. With semantic_recall_enabled on (memory settings → Semantic recall), the system prompt carries only a compact index (MemoryStore.format_index_for_system_prompt) and a per-turn memory_recall reminder (priority 88, cooldown 3) injects the entries relevant to the current request. context_engine/store/memory_recall.py self-heals the memory_entries.embedding index with the bge-m3 backend, re-ranks the cosine shortlist with bge-reranker, applies semantic_recall_char_budget, skips entries older than the session's current context_epoch baseline, and increments usage_count. memory(action="recall") and POST /memory/search use the same path; search falls back to substring when the switch is off.

POST /memory/consolidate starts an async task (GET /memory/consolidate/:task_id): it always runs the deterministic prune_unused aging pass, then — only with at least consolidation_min_entries (10) active entries — asks the main model to merge duplicates, resolve contradictions, re-route misfiled facts and tighten wording through the memory tool. Compression runs the same pass after the memory review (15 turns); session deletion runs it after both salvage waves (5 turns). Every failure is logged and never blocks compression or deletion.

🔗 Summary ↔ TaskFlow Coordination

When compression builds its LLM prompt, _get_taskflow_context_sync(session_id) (agent/middlewares/summarization/core.py:122) renders this session's active flows and appends them as the last part of the summary prompt (_build_summary_prompt, summarization/summary_generation.py:554):

taskflow_ctx = _get_taskflow_context_sync(session_id)
if taskflow_ctx:
    parts.append(taskflow_ctx)

The block is headed ## Current TaskFlow State (authoritative) (summarization/core.py:137) and, for up to three flows owned by the session (the store read is scoped by session_id in SQL — no post-filtering), lists the flow id/status, description, done/total progress with the status breakdown, the last two completed steps, the first two pending steps, and any wait reason. It reuses the DAG helpers step_status and steps_summary, and is fully fail-open (except Exception → ""). The deterministic fallback summary (_build_static_fallback_summary) does not include this block; it is an LLM-prompt-only addition.

🧠 Subagent Memory Backflow

SubagentCompletionDrainMiddleware (agent/middlewares/subagent_completion_drain/core.py) is the parent-turn ingestion point for queued subagent completions: at before_model it rehydrates and drains the session's SteeringQueue and injects the rebuilt completion-carrier messages. When the drain is non-empty it also reconciles the shared memory with the parent's in-memory view:

# subagent_completion_drain/core.py:93-117
def _backflow_shared_memory() -> None:
    from agent.tools.memory import memory_store
    memory_store.load_from_store()

Parent and children share one process-wide MemoryStore over the SQLite store, so a child's writes are already durable. What can drift is the parent's in-memory view — the live rows plus the frozen snapshot the system prompt was built from. Reloading is load-bearing and sufficient: SQLite is the source of truth, so there is no save-back and a reload cannot clobber a concurrent writer (the old load → persist reconcile could).

Like the drain, the backflow is fail-open — a memory-I/O failure is logged and swallowed, and the completion carrier still reaches the parent turn. The drain keeps the parent honest about completions: when the session has no passing evidence, it appends a mandatory-verification gate message after the carriers, so the parent treats a completion as a DoneClaim, not a verified result (verify via todoread, check acceptance criteria, and probe for stale state before marking a todo complete).

✂️ Tool Output Summarization

Older oversized ToolMessage content is normally cleared to a marker during non-LLM pruning. pub/func/message/tool_output_prune.py replaces the bare _PRUNE_MARKER = "[Old tool result content cleared]" (tool_output_prune.py:22) with a one-line, tool-specific summary so the model retains a hint of what was in the result:

# _TOOL_SUMMARY_TEMPLATES (tool_output_prune.py:43-65)
"read"/"read_file"   -> "[read] read file, {len} chars, {lines} lines"
"write"/"write_file" -> "[write] wrote file, {len} chars"
"edit"/"edit_file"   -> "[edit] edited file, {len} chars"
"grep"               -> "[grep] search done, {lines} matches"
"glob"               -> "[glob] matched {n} files"
"bash"/"shell"       -> "[bash] exit_code={code}, output {len} chars"
"taskflow_flow"      -> "[taskflow_flow] {len} chars"
"taskflow_step"      -> "[taskflow_step] {len} chars"
"taskflow_inspect"   -> "[taskflow_inspect] {len} chars"
"memory"             -> "[memory] {first 80 chars}..."
default              -> "[tool] output {len} chars, first 100: ..."

prune_tool_outputs(messages, protect_tokens=…, min_reduction_tokens=…, protected_tools=None, estimator=None) (tool_output_prune.py:104) walks messages newest→oldest, stops at the first summary message, protects the newest prune_protect_tokens (40 000), skips protected tools ({"memory", "skill_view", "skill_list"}), and only commits when the freed tokens reach prune_min_reduction_tokens (5 000). Replaced messages are model_copy clones carrying additional_kwargs["status"] = "compacted" and ["original_length"]. Summaries are capped at 200 chars; any template exception falls back to the marker. It is called from Summarization._run_non_llm_strategies (summarization/compression.py:314).

🔄 Session Continuity

When a session is cleared, context_engine/session_continuity.py persists an end-state so the next session can offer continuity. server/service/session_cleanup_service.py::purge_session calls auto_save_on_session_end(session_id) as step 0, before any deletion (server/service/session_cleanup_service.py step 0). That function:

  1. Resolves channel_id/chat_id through runtime.session.relation_register (_get_channel_chat_for_session, session_continuity.py:167).
  2. Reads the last 3 turns and clips the last AI reply to _MAX_SUMMARY_CHARS = 500 (session_continuity.py:28).
  3. Collects active flow ids for the session.
  4. Writes save_session_end_state(...) to a JSON file at src/data/session_continuity/{safe-key}.json (session_continuity.py:25), with fields last_session_id, ended_at, ended_ts, summary, taskflow_ids.

After the message-store deletion, clear_session also purges the session's planning stores — agent.tools.todolist.registry.store_sqlite.delete_todos_by_session(session_id) and agent.tools.taskflow.registry.store_sqlite.delete_flows_by_session(session_id) — so a cleared session leaves no todo or task-flow debris. The deletions are best-effort (a failure is logged and never blocks the rest of the purge), only rows whose session_id matches are deleted, and pre-isolation task-flow rows (session_id = '') are never matched.

The next session reads it via build_continuity_prompt(session_id) (session_continuity.py:80), called by _build_continuity_block in workspace/prompt_builder.py:169 and injected when the full prompt is built (prompt_builder.py:289-295):

## Last Session (continuity)
Last conversation ended with: <summary ≤ 500 chars>
Related tasks: <up to 3 flow ids>
If the user says 'continue' or doesn't specify a new task, refer to the above context first.

A session never receives its own state (last_session_id == session_id → ""). Because the lookup requires both a channel id and a chat id, pure WebSocket sessions with no channel binding get no continuity block. Storage is filesystem JSON (keyed by channel:chat, falling back to session_id), not a database.

🧹 Session-End Memory Salvage

purge_session also schedules a detached memory review before destroying a MAIN session (background_reviews.salvage_session_memory). It snapshots the post-compaction checkpoint transcript, model, system prompt and tool face, computes the normal compression cutoff read-only, and returns immediately after scheduling; the delete does not wait for the model. Wave 1 reviews the prefix a compression would have discarded with the captured (cache-warm) model/prompt/tool face; after it writes memory entries (stamped with the deleted session's id, not the derived wave key), wave 2 reloads memory, rebuilds the system prompt non-persistently, and reviews the preserved tail with a memory-only face. Both waves use the BACKGROUND_REVIEW lane, derived session keys (<id>::session-end-salvage:1|2), and no checkpointer; the background_review_memory metadata gate blocks knowledge/plan tools after deletion. Every failure is fail-open, a process exit may abandon an unfinished wave, and side chats/subagent children are not included. Continuity (above) stores the last AI reply for the next conversation; this salvage writes durable memory rows.

♻️ TaskFlow Auto-Resume

Active flows are re-surfaced as a per-turn taskflow_status system reminder (not into the cached system prompt) so a fresh session can pick up unfinished work. Three independent readers use the same recipe — the session-scoped get_active_flows_sync(session_id) reached through PromptDataProvider.get_active_flows(session_id):

Reader Location Purpose
_build_taskflow_block workspace/prompt_builder.py:145 ## Pending TaskFlows in the per-turn reminder block
_get_taskflow_context_sync agent/middlewares/summarization/core.py:122 TaskFlow block in the compression summary prompt
_get_active_taskflow_ids_sync context_engine/session_continuity.py:215 taskflow_ids in the persisted continuity state

creator_session_key is still stamped on the flow at creation (taskflow_create.py:38) as requester_session_key(session_id) = f"agent:main:session:{session_id}" (_shared.py:21) because child dispatch builds the requester key from it; it is no longer the isolation mechanism. get_active_flows_sync(session_id) returns only this session's running and waiting flows ordered by revision, using the stdlib sqlite3 path that works without an event loop; failures return [].

The system-prompt block (prompt_builder.py:140) looks like:

## Pending TaskFlows
- [running] flow-1: "<description>" | 2/5 steps done | next: step-3 "<task>"
Use taskflow_inspect(action="summary") to inspect a flow and continue execution.

It is capped at three flows and suppressed when the prompt is built with a file filter (selected_file_names is not None). Every read is fail-open.

Clone this wiki locally