xiaohongshu
xiaohongshu
moai-alfred-session-state
Enterprise session state management, token budget optimization, runtime tracking, session handoff protocols, context continuity for Claude Sonnet 4.5 and Haiku 4.5 with context awareness features
Full skill instructions
| Field | Value |
|---|---|
| Skill Name | moai-alfred-session-state |
| Version | 4.0.0 (Enterprise) |
| Updated | 2025-11-12 |
| Status | Active |
| Tier | Alfred |
| Supported Models | Claude Sonnet 4.5, Claude Haiku 4.5 |
| Context Window | 200K tokens (Sonnet/Haiku), 500K tokens (Enterprise), 1M tokens (beta) |
| Key Features | Context Awareness, Token Budget Tracking, Session Persistence, Adaptive Recovery |
Provides enterprise-grade session state management for extended workflows, token budget optimization, runtime tracking, and handoff protocols to maintain context continuity across Alfred workflows and session boundaries.
Enterprise v4.0.0 Capabilities:
Automatic triggers:
Manual reference:
Claude Sonnet 4.5 and Haiku 4.5 feature built-in context awareness, enabling these models to:
Key Advantage: Models now self-regulate context usage in real time.
Token Allocation Strategy (200K Sonnet context):
├── System Prompt & Instructions: ~15K tokens (7.5%)
│ ├── CLAUDE.md: ~8K
│ ├── Command definitions: ~4K
│ └── Skill metadata: ~3K
├── Active Conversation: ~80K tokens (40%)
│ ├── Recent messages: ~50K
│ ├── Context cache: ~20K
│ └── Active references: ~10K
├── Reference Context (Progressive Disclosure): ~50K (25%)
│ ├── Project structure: ~15K
│ ├── Related Skills: ~20K
│ └── Tool definitions: ~15K
└── Reserve (Emergency Recovery): ~55K tokens (27.5%)
├── Session state snapshot: ~10K
├── TAGs and cross-references: ~15K
├── Error recovery context: ~20K
└── Free buffer: ~10K
Technique 1: Progressive Summarization
Step 1: Original context (50K tokens)
Step 2: Compress to summary (15K tokens)
Step 3: Add pointers to original → 35K tokens saved
Step 4: Carry forward summary only across handoffs
Technique 2: Context Tagging with Unique Identifiers
❌ Bad (high token cost):
"The user configuration from the previous 20 messages..."
✅ Good (efficient reference):
Technique 3: Structured Context Architecture
├── Critical Context (ALWAYS keep)
│ ├── Current task objectives
│ ├── User preferences & expertise level
│ └── Active constraints
├── Supporting Context (keep if space allows)
│ ├── Related history
│ ├── Reference documentation
│ └── Tool availability
└── Temporary Context (discard when not needed)
├── Raw tool outputs
├── Intermediate calculations
└── Debug information
Technique 4: MCP Server Context Budget
# Check MCP server context consumption
/context
# Result: Each enabled MCP server adds tool definitions
# Example: context7 MCP = ~2K tokens for tool definitions
# Optimization: Disable unused servers before critical tasks
# Typical savings: 5-10K tokens per unused MCP server
Technique 5: Task-Based Session Management
Strategy: Start new conversation for distinct tasks
Benefits:
- Fresh 200K token budget per task
- Eliminates stale context accumulation
- Enables parallel session forking
- Improves recovery speed
Implementation:
1. Complete current task in Session A
2. Save session snapshot to .moai/sessions/
3. Start Session B for new task with fresh context
4. Resume Session A later if needed via session ID
Session State Stack (Enterprise v4.0.0):
├── L1: Context-Aware Layer (Claude 4.5+ feature)
│ ├── Token budget tracking
│ ├── Context window position
│ ├── Auto-summarization triggers
│ └── Model-specific optimizations
├── L2: Active Context (current task, variables, scope)
├── L3: Session History (recent actions, decisions)
├── L4: Project State (SPEC progress, milestones)
├── L5: User Context (preferences, language, expertise)
└── L6: System State (tools, permissions, environment)
Agent SDK Session Management (November 2025 API):
{
"session_id": "sess_uuid_v4",
"model": "claude-sonnet-4-5-20250929",
"created_at": "2025-11-12T10:30:00Z",
"context_window": {
"total": 200000,
"used": 85000,
"available": 115000,
"position_percent": 42.5
},
"persistence": {
"auto_load_history": true,
"context_preservation": "critical_only",
"cache_enabled": true
},
"forking": {
"enabled": true,
"fork_session_id": "sess_fork_uuid",
"checkpoint_timestamp": "2025-11-12T10:30:00Z"
}
}
# Capture session ID from initial response
session_id = extract_session_id(response)
# Save for later use
save_session_checkpoint({
'session_id': session_id,
'timestamp': now(),
'model': 'claude-sonnet-4-5',
'context_state': current_context_snapshot()
})
# Later: Resume conversation
response = claude.messages.create(
model="claude-sonnet-4-5-20250929",
resume=session_id, # Continue from checkpoint
messages=[new_message]
)
# Or: Fork session for parallel exploration
response = claude.messages.create(
model="claude-sonnet-4-5-20250929",
fork_session=session_id, # Branch from checkpoint
messages=[alternative_message]
)
Workflow State Transitions:
pending → in_progress → blocked (waiting) → completed/failed
↓ ↓
[monitor token budget] [save checkpoint]
[track elapsed time] [update history]
[check for recovery] [archive state]
Task Lifecycle States:
pending - Queued but not startedin_progress - Currently executing (monitor tokens)blocked - Waiting for dependencies or inputtoken_warning - Approaching context limit (v4.0.0)context_switch - Model change or session forkcompleted - Finished successfullyfailed - Error occurred, initiating recoveryrecovered - Resumed from checkpointdef token_budget_callback(context):
"""
Called automatically when token budget changes.
Model provides real-time context awareness.
"""
remaining_tokens = context.available_tokens
used_percent = context.token_usage_percent
if used_percent > 85:
# Activate emergency summarization
compress_context_window()
archive_old_context()
elif used_percent > 75:
# Start progressive disclosure
defer_non_critical_context()
elif used_percent > 60:
# Monitor for safety
track_context_growth()
{
"handoff_id": "uuid-v4",
"timestamp": "2025-11-12T10:30:00Z",
"from_agent": "spec-builder",
"to_agent": "tdd-implementer",
"session_context": {
"session_id": "sess_uuid",
"model": "claude-sonnet-4-5-20250929",
"context_position": 42.5,
"available_tokens": 115000,
"user_language": "ko",
"expertise_level": "intermediate",
"current_project": "MoAI-ADK"
},
"task_context": {
"spec_id": "SPEC-001",
"current_phase": "implementation",
"completed_steps": ["spec_complete", "architecture_defined"],
"next_step": "write_tests",
"constraints": ["must_use_pytest", "coverage_85"]
},
"context_snapshot": {
"critical_context": "...compressed...",
"session_checkpoints": [...],
"active_todos": [...],
"token_budget_strategy": "progressive_summarization"
},
"recovery_info": {
"last_checkpoint": "2025-11-12T10:25:00Z",
"recovery_tokens_reserved": 55000,
"session_fork_available": true
}
}
def validate_handoff(handoff_package):
"""Enterprise validation with token budget check"""
required_fields = [
'handoff_id', 'from_agent', 'to_agent',
'session_context', 'task_context', 'context_snapshot'
]
for field in required_fields:
if field not in handoff_package:
raise HandoffError(f"Missing required field: {field}")
# NEW v4.0.0: Validate token budget
context = handoff_package['session_context']
available = context['available_tokens']
if available < 30000: # Minimum safe buffer
trigger_context_compression()
# Validate agent compatibility
if not can_agents_cooperate(
handoff_package['from_agent'],
handoff_package['to_agent']
):
raise AgentCompatibilityError("Agents cannot cooperate")
return True
Checkpoint Triggers:
Checkpoint Structure:
{
"checkpoint_id": "ckpt_uuid",
"timestamp": "2025-11-12T10:30:00Z",
"phase": "GREEN",
"token_usage": {
"used": 85000,
"available": 115000
},
"context_snapshot": "...compressed snapshot...",
"session_id": "sess_uuid",
"recovery_tokens_reserved": 55000
}
Files:
.moai/sessions/session-state.json - Current session metadata.moai/sessions/context-cache.json - Cached context for performance.moai/sessions/checkpoints/ - Saved recovery checkpoints.moai/sessions/token-usage.log - Token budget historyactive-tasks.md - TodoWrite task trackingSynchronization Protocol:
def sync_memory_files(session_state):
"""Ensure memory files reflect current session state"""
# Update session metadata with token info
update_session_metadata({
'session_id': session_state.id,
'token_usage': session_state.token_budget,
'context_position': session_state.context_position,
'last_sync': timestamp()
})
# Sync TodoWrite tasks
sync_todowrite_tasks(session_state.active_tasks)
# Update context cache (compressed)
update_context_cache(compress_context(session_state.context))
# Log token usage for analytics
log_token_usage({
'timestamp': timestamp(),
'used': session_state.tokens_used,
'available': session_state.tokens_available,
'percent': session_state.usage_percent
})
# Archive old checkpoints (>7 days)
archive_old_checkpoints()
✅ DO:
❌ DON'T:
✅ DO:
❌ DON'T:
Location: .moai/config/config.json
{
"session": {
"persistence_enabled": true,
"auto_checkpoint": true,
"checkpoint_interval_minutes": 10,
"recovery_strategy": "progressive_summarization",
"context_budget": {
"warning_threshold_percent": 75,
"emergency_threshold_percent": 85,
"reserve_tokens": 55000
},
"forking_enabled": true,
"max_parallel_sessions": 3
}
}
# View current session state
/alfred:debug --show-session-state
# Check context window position
/alfred:debug --show-token-budget
# View all checkpoints
/alfred:debug --list-checkpoints
# Validate memory synchronization
/alfred:debug --check-memory-sync
# Show token usage history
/alfred:debug --show-token-usage-log
| Issue | Symptoms | Solution |
|---|---|---|
| Lost session | Cannot resume conversation | Check .moai/sessions/ for session IDs |
| Token budget exceeded | Model stops responding | Use /context to identify heavy consumers, create new session |
| Handoff failed | Agent has wrong context | Verify handoff package completeness before transfer |
| Recovery stuck | Cannot continue after interruption | Restore from earlier checkpoint or start new session |
| Memory drift | Inconsistent information | Run sync_memory_files() or check cache integrity |
| Version | Date | Key Changes |
|---|---|---|
| 4.0.0 | 2025-11-12 | Enterprise context awareness, token budget tracking, Session SDK integration, Git 2.47+ support |
| 1.1.0 | 2025-11-05 | Session state management foundation |
| 1.0.0 | 2025-10-01 | Initial release |
moai-alfred-context-budget - Token optimization deep divemoai-alfred-agent-guide - Multi-agent coordinationmoai-foundation-trust - State validation principlesmoai-foundation-git - Git session state trackingLearn more in reference.md for detailed implementation guides, recovery procedures, advanced coordination patterns, and November 2025 API examples.
Skill Status: Production Ready | Last Updated: 2025-11-12 | Model Support: Sonnet 4.5, Haiku 4.5 | Enterprise v4.0.0
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience