AgentSkillsCN

workflow

用于会话生命周期管理——涵盖会话的开始与结束协议、检查点工作流,以及错误修复流程。

SKILL.md
--- frontmatter
name: workflow
description: Use for session lifecycle management - covers session start/end protocols, checkpoint workflow, and error remediation flow

contextd Workflow

Session Start Protocol

Step 1: Search for relevant memories

json
{
  "project_id": "contextd",
  "query": "current task context",
  "limit": 5
}

Step 2: Check for checkpoints

json
{
  "tenant_id": "fyrsmithlabs",
  "project_path": "/path/to/project",
  "limit": 5
}

Step 3: If checkpoint found

  • Ask user: "Found previous work: '[summary]'. Resume?"
  • If yes: checkpoint_resume(checkpoint_id, tenant_id, level)
  • Levels: summary (minimal), context (balanced), full (complete)

Checkpoint Workflow

When to checkpoint:

  • Context >= 70% capacity
  • End of work session
  • Before risky operations
  • Switching tasks

checkpoint_save

json
{
  "session_id": "session_abc123",
  "tenant_id": "fyrsmithlabs",
  "project_path": "/path/to/project",
  "name": "Feature implementation checkpoint",
  "summary": "Completed: spec, 2 of 4 skills. Next: remaining skills.",
  "context": "Working on plugin consolidation...",
  "full_state": "Complete conversation state...",
  "token_count": 45000,
  "threshold": 0.7,
  "auto_created": false
}

Summary must include:

  • What was accomplished
  • What's in progress
  • What's next
  • Key decisions made

Resume Levels

LevelTokensContent
summary~100-200Name + summary only
context~500-1000Summary + context + decisions
fullCompleteEntire conversation history

Error Remediation Flow

When encountering ANY error:

code
1. DIAGNOSE the error
   troubleshoot_diagnose(error_message, error_context)

2. SEARCH for past fixes
   remediation_search(query, tenant_id)

3. APPLY fix (use diagnosis + history)

4. RECORD the solution
   remediation_record(title, problem, root_cause, solution, category)

troubleshoot_diagnose

json
{
  "error_message": "cannot use vectorStore as vectorstore.Store value",
  "error_context": "Running go test after adding new interface method"
}

Returns: root_cause, hypotheses, recommendations, confidence

remediation_record

json
{
  "title": "Mock missing interface method after interface change",
  "problem": "Test fails with 'missing method X'",
  "symptoms": ["go test fails", "cannot use X as Y value"],
  "root_cause": "Mock implementations don't get new interface methods",
  "solution": "Add new method to all mock implementations",
  "category": "syntax",
  "tenant_id": "fyrsmithlabs",
  "scope": "org"
}

Categories: syntax, runtime, logic, config, dependency, network, auth, data, performance

Scopes: project (this project), team (team members), org (entire organization)

Session End Protocol

Before /clear or ending work:

  1. Re-index repository

    json
    { "path": "." }
    

    Captures code changes and updates branch metadata.

  2. Record learnings

    json
    {
      "project_id": "contextd",
      "title": "Implemented session lifecycle hooks",
      "content": "Used TDD with Registry pattern...",
      "outcome": "success",
      "tags": ["hooks", "lifecycle", "tdd"]
    }
    
  3. Checkpoint if resuming later

    json
    { "auto_created": true, "threshold": 0.7, ... }
    

Git Commit Re-index

After every git commit:

json
{ "path": "." }

Why: Captures changes for semantic search, updates branch metadata.

Quick Reference

WhenAction
Session startmemory_search + checkpoint_list
After git commitrepository_index
Context >= 70%checkpoint_save then /clear
Error encounteredtroubleshoot_diagnose -> remediation_search -> fix -> remediation_record
Before /clearmemory_record + checkpoint_save

Common Mistakes

MistakeFix
Skipping memory search at startAlways search first
Vague checkpoint summariesInclude completed/in-progress/next
Waiting until context overflowSave at 70%, not 95%
Not recording before /clearCall memory_record first
Skipping error diagnosistroubleshoot_diagnose first, always

Input Validation (contextd v1.5+)

ID Format Requirements

tenant_id and project_id must be lowercase alphanumeric with underscores:

  • Valid: my_project, contextd, org123
  • Invalid: My-Project, org/repo, project..name
  • Length: 1-64 characters

Path Validation

All project_path parameters are validated:

  • No directory traversal (../ is rejected)
  • Use absolute paths or paths within the current project

Validation Errors

ErrorCauseFix
invalid tenant_idInvalid charactersUse lowercase, underscores only
invalid project_pathDirectory traversalUse absolute or relative-to-project paths
invalid patternsShell injection charsRemove ;, |, `, $

Checkpoint Compression (Deltas)

Delta-Based Storage

Instead of storing full state each time, checkpoints can store deltas:

json
{
  "checkpoint_id": "cp_002",
  "parent_id": "cp_001",
  "storage_mode": "delta",
  "delta": {
    "added_memories": ["mem_123", "mem_124"],
    "completed_tasks": ["#42", "#43"],
    "new_decisions": ["Use Registry pattern for DI"],
    "files_changed": ["api/handler.go", "internal/store.go"]
  },
  "delta_size_tokens": 150,
  "full_size_tokens": 2500
}

Compression Modes

ModeStorageRestore SpeedUse Case
fullComplete stateInstantShort sessions, critical points
deltaChanges onlyRequires chainLong sessions, frequent saves
hybridFull every 5thBalancedDefault recommendation

Checkpoint Chain

code
cp_001 (full) -> cp_002 (delta) -> cp_003 (delta) -> cp_004 (delta) -> cp_005 (full)
                                                                        ^
                                                              Auto-consolidate at 5

Resume from delta:

code
checkpoint_resume(checkpoint_id: "cp_003")
  -> Loads cp_001 (full base)
  -> Applies cp_002 delta
  -> Applies cp_003 delta
  -> Returns reconstructed state

Checkpoint Branching

Parallel Work Streams

Create checkpoint branches for exploration:

json
{
  "checkpoint_id": "cp_main_005",
  "branch": "experiment/new-arch",
  "parent_checkpoint": "cp_main_003",
  "description": "Exploring event-sourcing architecture"
}

Branch Operations

OperationPurpose
checkpoint_branch(parent_id, branch_name)Create exploration branch
checkpoint_merge(branch_id, target_id)Merge learnings back
checkpoint_abandon(branch_id)Discard failed experiment

Visualizing Branches

code
cp_main_001 -> cp_main_002 -> cp_main_003 -> cp_main_004 -> cp_main_005
                                    \
                                     -> cp_exp_001 -> cp_exp_002 (merged at cp_main_005)

Auto-Error Capture via Hooks

PostToolUse Hook for Error Detection

Automatically capture errors when tools fail:

json
{
  "hook_type": "PostToolUse",
  "matcher": "Bash|Task",
  "condition": "tool_output.exit_code != 0 OR tool_output.contains('error')",
  "prompt": "An error occurred. Automatically:\n1. Call troubleshoot_diagnose with the error\n2. Search remediation_search for past fixes\n3. If novel error, prepare remediation_record after fix"
}

PreToolUse Hook for Dangerous Operations

Checkpoint before risky operations:

json
{
  "hook_type": "PreToolUse",
  "matcher": "Bash",
  "condition": "command.matches('rm|drop|delete|truncate|reset --hard')",
  "prompt": "Risky operation detected. Auto-checkpoint before proceeding:\ncheckpoint_save(name: 'pre-risky-op', auto_created: true)"
}

Stop Hook for Session End

Auto-capture learnings when session ends:

json
{
  "hook_type": "Stop",
  "prompt": "Session ending. Before final response:\n1. Record any unrecorded learnings with memory_record\n2. Save checkpoint if work is in progress\n3. Re-index repository if files changed"
}

Unified Memory Type References

All workflow operations should tag with standard types:

TypeTagPurpose
Learningtype:learningKnowledge gained
Remediationtype:remediationError -> fix
Decisiontype:decisionArchitecture choice
Failuretype:failureWhat NOT to do
Patterntype:patternReusable code
Policytype:policySTRICT constraint

Example with type:

json
{
  "title": "Session lifecycle implementation",
  "tags": ["type:learning", "category:workflow", "component:hooks"]
}

Hierarchical Namespace Guidance

Checkpoint Namespaces

Structure checkpoint names for multi-project work:

code
<org>/<project>/<branch>/<checkpoint>

Examples:
  fyrsmithlabs/contextd/main/feature-complete
  fyrsmithlabs/contextd/experiment/event-sourcing
  fyrsmithlabs/marketplace/main/v1.5-release

Cross-Project Checkpoints

Link related checkpoints across projects:

json
{
  "checkpoint_id": "cp_contextd_005",
  "related_checkpoints": [
    "fyrsmithlabs/marketplace/main/contextd-integration"
  ],
  "cross_project_context": "Marketplace integration depends on contextd v1.2.0"
}

Audit Fields

All workflow operations record:

FieldDescriptionAuto-set
created_byAgent/session IDYes
created_atISO timestampYes
updated_atLast modificationYes
usage_countTimes checkpoint resumedYes
last_resumed_atLast resume timestampYes

Claude Code 2.1 Patterns

Background Checkpoint Save

Save checkpoints without blocking:

code
Task(
  subagent_type: "general-purpose",
  prompt: "Save checkpoint with current state summary",
  run_in_background: true,
  description: "Background checkpoint save"
)

// Continue work immediately

Task Dependencies for Workflow

Chain workflow operations:

code
diagnose_task = Task(prompt: "Diagnose error: {{error}}")
search_task = Task(prompt: "Search remediations", addBlockedBy: [diagnose_task.id])
fix_task = Task(prompt: "Apply fix", addBlockedBy: [search_task.id])
record_task = Task(prompt: "Record remediation", addBlockedBy: [fix_task.id])

PostToolUse Hook Example

Auto-checkpoint after successful operations:

json
{
  "hook_type": "PostToolUse",
  "tool_name": "Bash",
  "condition": "command.contains('git commit') AND exit_code == 0",
  "prompt": "Commit successful. Auto-checkpoint and re-index repository."
}

Event-Driven State Sharing

Workflow emits events for other skills:

json
{
  "event": "checkpoint_saved",
  "payload": {
    "checkpoint_id": "cp_005",
    "project_id": "fyrsmithlabs/contextd",
    "summary": "Feature complete",
    "token_count": 45000
  },
  "notify": ["orchestration", "self-reflection"]
}

Subscribe to workflow events:

  • session_started - New session began
  • checkpoint_saved - State preserved
  • checkpoint_resumed - Previous state loaded
  • error_captured - Auto-captured error
  • remediation_applied - Fix successfully applied