name: context-preservation-protocol description: | Context preservation patterns for storing work results and session context via an MCP-compatible context server. Provides patterns for documenting work, storing reports, and ensuring continuity between sessions. Use when you need to preserve work results or session context.
Context Preservation Best Practices
Storing work documentation and context before stopping is MANDATORY whenever you have context-server store capability and produced substantive work this session: the durable record is what survives a context reset or compaction, so an artifact left only in an ephemeral channel is lost. The patterns in this skill help you structure, store, and preserve your work results in the context server.
How to Obtain Thread ID
The thread ID is used as thread_id for context server queries. Obtain it using the following search chain:
- Already available -- If the thread ID is provided via context or prompt, use it directly
- Thread ID file -- Check
.context_server/.thread_idin the project working directory - Project directory name -- If no thread ID file exists, derive the thread identifier from the project directory basename using the git remote URL fallback chain described below. Using the project name ensures all agents working on the same project write to the same thread, which is essential for multi-agent coordination
Available Context Server Tools
Note: Not all tools listed below may be available in your environment. Tool availability depends on server configuration and how the server is connected to your MCP client. Use the tools that are available to you. If a recommended tool is unavailable, use an alternative from this table.
The tools below cover storage and update. For retrieval and search operations, the context server exposes a parallel set of tools (for example search_context, get_context_by_ids, hybrid_search_context, semantic_search_context, and fts_search_context) -- consult the retrieval section of the server's own documentation.
| Tool | Status | Use For |
|---|---|---|
store_context |
RECOMMENDED | Store NEW entry (typical for fresh work reports) |
update_context |
RECOMMENDED | Update EXISTING entry (for revisions/continuations) |
store_context_batch |
Optional | Store multiple entries at once (rarely needed) |
update_context_batch |
Optional | Update multiple entries at once (rarely needed) |
delete_context |
Use with caution | Delete specific context entries |
delete_context_batch |
Use with caution | Delete multiple context entries at once |
list_threads |
Optional | Discover available threads and their metadata |
get_statistics |
Optional | Check server health and usage metrics |
Key notes:
store_contextis the standard choice for fresh work reportsupdate_contextis used when revising a previously stored plan or continuing incomplete research
When to use store_context_batch:
- Use ONLY when storing multiple independent entries in a single operation
- Typical use cases: migrations, imports, or bulk data operations
- NOT needed for normal work reports (use
store_contextinstead)
Protocol requirements:
metadata: Recommended - enables filtering by agent_name, task_name, status, projecttags: Recommended - enables search and categorizationimages: optional
Context Update Strategy
When to Use update_context
Use update_context instead of store_context when:
- Revising a previously stored plan based on user feedback
- Continuing research that was marked INCOMPLETE
- Correcting errors in a prior report
- Updating status from "pending" to "done"
When to Use store_context
Use store_context (not update_context) when:
- Creating fresh research/implementation work
- No prior context_id exists for this task
- Starting a new research thread
update_context Parameters
| Parameter | Required | Description |
|---|---|---|
context_id |
YES | ID of the entry to update |
text |
NO | Complete revised text (replaces existing entirely) |
metadata |
NO | Full metadata replacement (replaces all metadata) |
metadata_patch |
NO | Partial metadata update (RFC 7396 merge semantics) |
tags |
NO | Updated tags (replaces existing tags entirely) |
Important: Use metadata_patch (not metadata) for revisions to preserve fields you do not want to change.
Update Protocol for Plan Revisions
When updating an existing entry for plan revision:
- Extract context_id from the prompt (e.g.,
PREVIOUS CONTEXT ID: 123) - Retrieve previous entry:
get_context_by_ids([context_id]) - Verify ownership: Check that
agent_namein metadata matches your agent identifier - Create revised content: Generate the updated plan
- Call update_context:
update_context( context_id=<extracted_id>, text=<revised_report>, metadata_patch={ "revision_count": <current + 1 or 1 if first revision>, "status": "done" }, tags=["report", "implementation-guide", "research", ...] ) - Return SAME context_id in status message
Metadata Merge Semantics (RFC 7396)
When using metadata_patch:
- New keys are ADDED
- Existing keys are UPDATED with new values
- Keys set to
nullare DELETED - Omitted keys are PRESERVED unchanged
Example:
# Original metadata: {"agent_name": "implementation-guide", "status": "pending", "revision_count": 0}
# Patch: {"status": "done", "revision_count": 1}
# Result: {"agent_name": "implementation-guide", "status": "done", "revision_count": 1}
Important Notes
textis REPLACED entirely (not appended)tagsare REPLACED entirely (not merged)updated_attimestamp is automatically set by the server- Embeddings are regenerated when text changes
Environment Integration Patterns
Context preservation operations can interact with environment-level hooks, validation gates, and orchestration workflows. The patterns below describe how to structure stored context for optimal integration.
Hook-Aware Preservation
In environments with event-driven hooks, context storage may trigger or be validated by environment logic:
- Post-storage validation: Environment hooks may verify that stored context includes required metadata fields, correct tagging, and proper references
- Storage auditing: Hooks may log storage operations for traceability, verifying that agents store work results before session completion
- Format enforcement: Validation gates may reject context entries that lack required structure (e.g., missing
status,agent_name, orreferences)
When operating in such environments, follow the metadata schema and compliance checklist rigorously to avoid validation failures.
Metadata Patterns for Multi-Agent Coordination
Structured metadata enables sophisticated workflows across multiple agents:
- Work chain linking: Always populate
references.context_idswith IDs of entries your work builds upon. This creates navigable chains that other agents and orchestrators can follow - Agent identification: Always set
agent_nameto enable filtering by agent role. This is critical for orchestrators that need to find specific agent outputs - Status signaling: Use
status: "pending"to signal that work requires continuation, andstatus: "done"to signal completion. Orchestrators use this to determine workflow progression - Report type classification: Use
report_typeconsistently to enable cross-agent discovery (e.g., finding all validation reports regardless of which validator produced them)
Preservation for Orchestrated Workflows
When storing context in multi-agent orchestrated environments:
- Handoff readiness: Structure reports so that another agent can understand the work without additional context. Include goals, work performed, results, and explicit next steps
- Reference completeness: Include all
context_idsthat informed your work. Incomplete references break the traceability chain - Tag consistency: Use consistent tags across related entries to enable grouped retrieval (e.g., all entries tagged with a specific feature or task name)
These patterns are generic and apply to any environment with multi-agent coordination capabilities.
Metadata Schema
Core Fields (Recommended)
| Field | Type | Recommended | Description |
|---|---|---|---|
agent_name |
string | Yes | Your agent identifier (defined in your instructions) |
task_name |
string | Yes | Human-readable task description |
status |
enum | Yes | Completion state: done or pending |
project |
string | Yes | Canonical project name (from git remote URL, see Worktree Metadata Fields section) |
technologies |
array | Yes | List of technologies involved |
report_type |
enum | Yes | Type of work report |
references |
object | Yes | Cross-system identifiers linking to external resources (use {} if none). See References Field for sub-fields. |
Standardized Values
status Field
| Value | Use When |
|---|---|
done |
Work complete, no follow-up required |
pending |
Work incomplete, requires continuation (e.g., Research Continuation) |
report_type Field
| Value | Description |
|---|---|
research |
Research, analysis, implementation planning |
implementation |
Code implementation work |
validation |
Quality validation, testing results |
documentation |
Documentation creation/updates |
technologies Field
Array of lowercase technology identifiers representing the SUBJECT MATTER of the task itself.
The following guidance is CRITICAL for understanding what to include vs exclude:
| Include (Task Subject) | Exclude (Execution Tools) |
|---|---|
| Technologies the task is ABOUT | Tools used to execute the task |
| Languages/frameworks being implemented | Linters, formatters, type checkers |
| Databases being configured or queried | Version control operations |
| APIs being developed or integrated | Testing frameworks (unless testing IS the task) |
| Infrastructure being designed | CI/CD tools (unless CI/CD IS the task) |
Examples of Correct Usage:
| Task Description | Correct technologies |
Why |
|---|---|---|
| Fix bug in FastAPI endpoint | ["python", "fastapi"] |
Task is about Python/FastAPI code |
| Update README documentation | ["markdown"] |
Task is about markdown content |
| Configure PostgreSQL connection pooling | ["postgresql"] |
Task is about database configuration |
| Write pytest tests for auth module | ["python", "pytest"] |
Testing IS the task subject |
| Set up GitHub Actions CI pipeline | ["github-actions"] |
CI/CD IS the task subject |
| Research LangGraph checkpointing | ["langchain", "langgraph"] |
Research topic is LangGraph |
Examples of INCORRECT Usage:
| Task Description | WRONG technologies |
Why Wrong |
|---|---|---|
| Fix markdown typo | ["python", "pre-commit", "markdown"] |
Python/pre-commit are execution tools, not task subject |
| Update hook logic | ["python", "pytest", "mypy"] |
pytest/mypy are validation tools, not task subject |
| Design API schema | ["python", "git", "vscode"] |
git/vscode are development tools, not task subject |
Example values (illustrative, not exhaustive): use any lowercase identifier that describes your task's subject matter -- e.g. languages (python, typescript, go, rust), frameworks (fastapi, react, django), databases (postgresql, mongodb, redis), infrastructure (docker, kubernetes, terraform).
Example:
"technologies": ["python", "fastapi", "postgresql"]
Optional Fields
| Field | Type | Description |
|---|---|---|
domain |
enum | Technical domain: backend, frontend, fullstack, devops, data, security, general |
Advanced: Worktree Metadata Fields
This section is relevant for environments that use git worktrees. If you are not using worktrees, you can skip this section.
When working in a git worktree environment, include these fields for proper context isolation:
| Field | Type | Required | Description |
|---|---|---|---|
worktree_id |
string | RECOMMENDED | Current worktree directory name |
worktree_path |
string | RECOMMENDED | Absolute path to worktree root |
is_linked_worktree |
boolean | OPTIONAL | True if linked worktree, false for main |
Project Name Derivation:
The project field MUST be derived using this fallback chain to ensure consistency across git worktrees:
Parse from git remote URL (PREFERRED)
- Try
originremote first:git remote get-url origin - Parse repository name from URL:
https://github.com/user/my-project.git->my-projectgit@github.com:user/my-project.git->my-project
- If
originunavailable, tryupstream, then first available remote
- Try
Git toplevel basename (FALLBACK for repos without remotes)
git rev-parse --show-toplevel-> extract last path component- Example:
/home/user/projects/my-project->my-project
Current directory basename (FALLBACK for non-git directories)
- Extract last directory name from working directory path
- Example:
/home/user/work/my-project->my-project
Why this matters:
- Different worktrees of the same repository have different directory names
- Using directory name causes context isolation to break across worktrees
- Remote URL provides true canonical identity across all worktrees and users
Example with worktree fields:
{
"agent_name": "developer",
"task_name": "Implement feature X",
"status": "done",
"project": "my-project",
"worktree_id": "feature-branch",
"worktree_path": "/home/user/projects/feature-branch",
"is_linked_worktree": true,
"technologies": ["python", "fastapi"],
"report_type": "implementation",
"references": {}
}
References Field
The references field stores cross-system identifiers for traceability. All values are arrays.
Core Reference Types:
| Key | Value Type | Format | Example |
|---|---|---|---|
context_ids |
integer[] | Numeric | [2322, 2325] |
urls |
string[] | Full URL | ["https://github.com/org/repo/pull/445"] |
git_commits |
string[] | Full SHA (40/64 hex chars) | ["abc1234def5678901234567890abcdef12345678"] |
Open for extension following {system}_{entity_type}s convention (e.g., github_prs, jira_issues).
Usage Guidelines:
- Include relevant references when storing context
- Use empty object
{}if no external references exist context_ids: Reference related entries in the same or other threadsurls: Store any external reference as a full URL (issues, PRs, documentation pages, commit URLs, etc.)git_commits: Use FULL SHA only (40 characters for SHA-1, 64 characters for SHA-256). Within a single-project session whereprojectmetadata identifies the repository, bare SHAs are unambiguous and directly usable withgit show
Cross-Repository Disambiguation:
When context spans multiple repositories (e.g., a task involving changes across a backend and frontend repo), bare SHAs in git_commits may be ambiguous because the same hash format provides no indication of which repository it belongs to. In such cases, supplement git_commits with platform URLs in urls to provide full context:
"references": {
"git_commits": ["abc1234def5678901234567890abcdef12345678"],
"urls": ["https://github.com/org/backend-repo/commit/abc1234def5678901234567890abcdef12345678"]
}
Both fields serve complementary purposes: git_commits provides typed, validated commit identifiers usable across any git platform (including local repos without hosting); urls provides human-readable, clickable links with full repository context.
Examples:
"references": {
"context_ids": [2322, 2325],
"urls": ["https://github.com/org/repo/pull/445", "https://docs.example.com/guide"],
"git_commits": ["abc1234def5678901234567890abcdef12345678"]
}
"references": {}
Preservation Strategy
When you have context-server store capability and produced substantive work this session, you MUST complete the following before stopping; if you already stored this report earlier in the same session and it is unchanged, do not store it again. Complete the following before stopping:
Create a comprehensive Markdown report of your work results:
FIRST CHECK: If you have a specific report structure defined in your own agent instructions, use your own STRUCTURE within the Markdown format.
ONLY IF NO SPECIFIC FORMAT EXISTS, use the following structure:
## Summary - Brief overview including key decisions, recommendations, and conclusions ## Goals - What goals you were tasked to achieve ## Work Performed - Detailed list of all tasks completed ## Results Achieved - Detailed documentation, outcomes, deliverables - Examples (code snippets, configurations) - URIs (URLs, file paths) - References (version numbers, filenames, entity names, line numbers) - Any other relevant informationFront-load critical information: Place key findings, decisions, recommendations, and conclusions in the opening section (Summary) of your stored entries. Search tools return truncated previews from the beginning of stored text -- information buried deep in an entry may be invisible during search-based discovery, causing other agents to misjudge relevance and skip retrieval of entries that contain important content.
Always use English to write the report, REGARDLESS of the language requested by the calling party.
Save the report using
store_contextwith these parameters:thread_id: Your thread ID (REQUIRED)source:agent(REQUIRED)text: Your complete Markdown report (REQUIRED)metadata: Recommended - include these fields for best discoverability:
Why these fields are recommended: each enables metadata filtering so other agents and sessions can find your context -- by agent ({ "agent_name": "[your agent name]", "task_name": "[human-readable task description, e.g., 'Implement authentication', 'Fix login bug']", "status": "done | pending", "project": "[current directory name]", "technologies": ["list", "of", "technologies"], "report_type": "research | implementation | validation | documentation", "references": {} }agent_name), task (task_name), completion state (status), project (project), tech stack (technologies, via thearray_containsoperator or tags), and work type (report_type).tags: Recommended -["report", ...relevant tags]for search and categorization
After successfully saving, capture the
context_idfrom thestore_contextresponse and include it in your brief completion status to the calling party:- Format:
"[Brief status summary]. Report ID: [context_id]" - Example:
"Implementation complete. 3 features implemented. Report ID: 2510" - The caller can use this ID to retrieve the full report via
get_context_by_ids([context_id])
- Format:
This ensures your work is documented, preserved, and retrievable by other agents who need to access your detailed findings. A structured-output return value or any other in-window reply to your caller is SEPARATE from this durable record and does NOT substitute for it; the ephemeral reply is lost on compaction, the stored entry is not. A dispatch instruction that forbids writing report files to disk (for example a swarm or deep-research "do not write files to disk" contract) governs on-disk files only and does NOT relieve you of storing the context-server entry.
Context Continuity Patterns
These patterns help agents preserve state across context window boundaries and long-running tasks. They are the storage-side patterns; the symmetric retrieval-side patterns (search, re-read after compaction, references navigation) belong to the retrieval workflow and follow the same principles described below applied to retrieval tools.
Basic Continuity (Default)
These patterns should be applied by default when storing context:
- Always set status: Mark entries as
status: "done"orstatus: "pending"to signal work state to future sessions and other agents - Session handoff notes: Before ending a session, store a summary entry describing: work completed, key decisions, unresolved issues, and recommended next steps. This serves as a briefing document for the next session
- Reference chain maintenance: Always populate
references.context_idswith the entries your work builds upon. This creates a navigable history that survives context window resets - Pre-compaction preservation: If approaching context window limits during extended work, proactively store current progress to the context server before compaction occurs. Critical details stored externally survive compaction intact
Advanced: Long-Running Task Continuity (Optional)
For tasks spanning multiple context windows or extended multi-step execution:
Checkpoint Storage:
At defined milestones during multi-step tasks, store a checkpoint entry containing:
- Summary of completed steps and remaining work
- Key decisions and their rationale
- Active blockers or dependencies
- List of modified files and their purpose
- Set
status: "pending"and includereferences.context_idslinking to the task plan
Progressive Summarization:
For tasks generating large volumes of context, periodically store condensed summary entries:
- Distill key findings, decisions, and progress into a structured summary
- Reference the original detailed entries via
references.context_ids - Tag summaries consistently (e.g., with task name) for easy retrieval
Multi-Agent Handoff Reports:
When completing work that another agent will continue:
- Store a comprehensive handoff report with explicit next steps
- Include all relevant
references.context_idsso the receiving agent can trace the full work chain - Set
report_typeandagent_nameaccurately to enable precise filtering - Structure the report with clear sections (Summary, Work Performed, Results, Next Steps, and others) so the receiving agent can parse it efficiently
Compliance Checklist
Before returning to the calling party, verify the following whenever you had store capability and produced substantive work:
- Report created: Comprehensive Markdown report documenting your work
- Report saved: Called
store_contextwith thread_id, source="agent", text, metadata, and tags - Metadata complete: Included agent_name, task_name, status, and project fields
- Technologies and report_type: Populated correctly per task subject (not execution tools)
- References included: Populated
referencesfield with relevant identifiers (use{}if none) - Tags included: Added "report" tag plus relevant categorization tags
- Storage verified: Confirmed
store_contextcall succeeded before returning - Report ID returned: Included
context_idfromstore_contextresponse in status message
Completing this checklist is mandatory for reliable context preservation whenever you had store capability and produced substantive work.
Behavioral Examples
Error Handling
Storage Failure Protocol
Context server storage is mandatory for substantive work when you have store capability. Failure to store means work results may be lost.
If context storage fails (network error, server unavailable, timeout):
- Retry once after 2 seconds for transient errors (timeout, 5xx)
- If retry fails or error is non-transient (4xx, connection refused):
- Return FULL REPORT to caller inline in your response (not just a status summary)
- Inform the caller of the storage failure so they can decide next steps
- Example fallback message:
WARNING: Context server storage failed. Full report included below. Error: [specific error message] Impact: Report not persisted to context server. Content preserved in this response only.
Rationale: When storage fails, preserving the report inline ensures work is not lost entirely. The caller can manually store it later or take other action.