Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

docs: reorganize PLAN.md and extract completed work to feature docs

Adam Eivy158e494b2b82
diff --git a/PLAN.md b/PLAN.md
index 08c4e1b391..6788621f4c 100644
--- a/PLAN.md
+++ b/PLAN.md
@@ -1,7 +1,5 @@
# Port OS - Implementation Plan
-See full plan at: `~/.claude/plans/mutable-inventing-eagle.md`
-
## Quick Reference
### Tech Stack
@@ -25,40 +23,55 @@ pm2 start ecosystem.config.cjs
pm2 logs
```
-### Milestones
-- [x] M0-M3: Bootstrap, app registry, PM2 integration, log viewer - Core infrastructure complete
-- [x] M4: App Wizard - Register existing apps or create from templates. See [App Wizard](./docs/features/app-wizard.md)
-- [x] M5: AI Providers - Multi-provider AI execution with headless Claude CLI
-- [x] M6: Dev Tools - Command runner with history and execution tracking
-- [ ] M7: App Templates - Template management and app scaffolding (planned)
-- [x] M8: Prompt Manager - Customizable AI prompts with variables and stages
-- [x] M9: Streaming Import - Real-time websocket updates during app detection
-- [x] M10: Enhanced DevTools - Provider/model selection, screenshots, git status, usage metrics
-- [x] M11: AI Agents Page - Process detection and management with colorful UI
-- [x] M12: History Improvements - Expandable entries with runtime/output capture
-- [x] M13: Autofixer - Autonomous crash detection and repair. See [Autofixer](./docs/features/autofixer.md)
-- [x] M14: Chief of Staff - Autonomous agent manager with task orchestration. See [Chief of Staff](./docs/features/chief-of-staff.md)
-- [x] M15: Error Handling - Graceful error handling with auto-fix. See [Error Handling](./docs/features/error-handling.md)
-- [x] M16: Memory System - Semantic memory with LLM classification. See [Memory System](./docs/features/memory-system.md)
-- [x] M17: PM2 Config Enhancement - Per-process port detection and CDP_PORT support
-- [x] M18: PM2 Standardization - LLM-powered config refactoring
-- [x] M19: CoS Agent Runner - Isolated PM2 process for agent spawning
-- [x] M20: AI Error Handling - Enhanced error extraction and CoS integration
-- [x] M21: Usage Metrics - Comprehensive AI usage tracking and mobile UI
-- [x] M22: Orphan Auto-Retry - Automatic retry for orphaned agents
-- [x] M23: Self-Improvement - Automated UI/security/code analysis with Playwright
-- [x] M24: Goal-Driven Mode - COS-GOALS.md mission file and always-working behavior
-- [x] M25: Task Learning - Completion tracking and success rate analysis
-- [x] M26: Scheduled Scripts - Cron-based automation with agent triggering
-- [x] M28: Weekly Digest UI - Visual digest with insights and comparisons
-- [x] M29: App Improvement - Comprehensive analysis extended to managed apps
-- [x] M30: Configurable Intervals - Per-task-type scheduling (daily, weekly, once, on-demand)
-- [x] M31: LLM Memory Classification - Intelligent memory extraction with quality filtering
-- [x] M32: Brain System - Second-brain capture and classification. See [Brain System](./docs/features/brain-system.md)
-- [x] M33: Soul System - Digital twin identity scaffold management
-- [x] M35: Chief of Staff Enhancement - Proactive autonomous agent with hybrid memory, missions, LM Studio, thinking levels
+---
+
+## Milestones
+
+### Completed
+
+- [x] **M0-M3**: Bootstrap, app registry, PM2 integration, log viewer - Core infrastructure
+- [x] **M4**: App Wizard - Register existing apps or create from templates. See [App Wizard](./docs/features/app-wizard.md)
+- [x] **M5**: AI Providers - Multi-provider AI execution with headless Claude CLI
+- [x] **M6**: Dev Tools - Command runner with history and execution tracking
+- [x] **M8**: Prompt Manager - Customizable AI prompts with variables and stages. See [Prompt Manager](./docs/features/prompt-manager.md)
+- [x] **M9**: Streaming Import - Real-time websocket updates during app detection
+- [x] **M10**: Enhanced DevTools - Provider/model selection, screenshots, git status, usage metrics
+- [x] **M11**: AI Agents Page - Process detection and management with colorful UI
+- [x] **M12**: History Improvements - Expandable entries with runtime/output capture
+- [x] **M13**: Autofixer - Autonomous crash detection and repair. See [Autofixer](./docs/features/autofixer.md)
+- [x] **M14**: Chief of Staff - Autonomous agent manager with task orchestration. See [Chief of Staff](./docs/features/chief-of-staff.md)
+- [x] **M15**: Error Handling - Graceful error handling with auto-fix. See [Error Handling](./docs/features/error-handling.md)
+- [x] **M16**: Memory System - Semantic memory with LLM classification. See [Memory System](./docs/features/memory-system.md)
+- [x] **M17**: PM2 Config Enhancement - Per-process port detection and CDP_PORT support
+- [x] **M18**: PM2 Standardization - LLM-powered config refactoring
+- [x] **M19**: CoS Agent Runner - Isolated PM2 process for agent spawning. See [CoS Agent Runner](./docs/features/cos-agent-runner.md)
+- [x] **M20**: AI Error Handling - Enhanced error extraction and CoS integration
+- [x] **M21**: Usage Metrics - Comprehensive AI usage tracking and mobile UI
+- [x] **M22**: Orphan Auto-Retry - Automatic retry for orphaned agents
+- [x] **M23**: Self-Improvement - Automated UI/security/code analysis with Playwright
+- [x] **M24**: Goal-Driven Mode - COS-GOALS.md mission file and always-working behavior
+- [x] **M25**: Task Learning - Completion tracking and success rate analysis
+- [x] **M26**: Scheduled Scripts - Cron-based automation with agent triggering
+- [x] **M27**: CoS Capability Enhancements - Dependency updates, performance tracking, learning insights
+- [x] **M28**: Weekly Digest UI - Visual digest with insights and comparisons
+- [x] **M29**: App Improvement - Comprehensive analysis extended to managed apps
+- [x] **M30**: Configurable Intervals - Per-task-type scheduling (daily, weekly, once, on-demand)
+- [x] **M31**: LLM Memory Classification - Intelligent memory extraction with quality filtering
+- [x] **M32**: Brain System - Second-brain capture and classification. See [Brain System](./docs/features/brain-system.md)
+- [x] **M33**: Soul System - Digital twin identity scaffold management. See [Soul System](./docs/features/soul-system.md)
+- [x] **M34 P1-P2,P4**: Digital Twin - Quantitative personality modeling and confidence scoring. See [Digital Twin](./docs/features/digital-twin.md)
+- [x] **M35**: Chief of Staff Enhancement - Proactive autonomous agent with hybrid memory, missions, LM Studio, thinking levels. See [CoS Enhancement](./docs/features/cos-enhancement.md)
+
+### Planned
+
+- [ ] **M7**: App Templates - Template management and app scaffolding from templates
+- [ ] **M34 P3,P5-P7**: Digital Twin - Behavioral feedback loop, multi-modal capture, advanced testing, personas
+
+---
+
+## Documentation
-### Documentation
+### Architecture & Guides
- [Architecture Overview](./docs/ARCHITECTURE.md) - System design, data flow
- [API Reference](./docs/API.md) - REST endpoints, WebSocket events
- [Contributing Guide](./docs/CONTRIBUTING.md) - Code guidelines, git workflow
@@ -68,23 +81,18 @@ pm2 logs
- [GitHub Actions](./docs/GITHUB_ACTIONS.md) - CI/CD workflow patterns
- [Troubleshooting](./docs/TROUBLESHOOTING.md) - Common issues and solutions
-#### Feature Documentation
+### Feature Documentation
- [App Wizard](./docs/features/app-wizard.md) - Register apps and create from templates
- [Autofixer](./docs/features/autofixer.md) - Autonomous crash detection and repair
+- [Brain System](./docs/features/brain-system.md) - Second-brain capture and classification
- [Chief of Staff](./docs/features/chief-of-staff.md) - Autonomous agent orchestration
+- [CoS Agent Runner](./docs/features/cos-agent-runner.md) - Isolated agent process management
+- [CoS Enhancement](./docs/features/cos-enhancement.md) - M35 hybrid memory, missions, thinking levels
+- [Digital Twin](./docs/features/digital-twin.md) - Quantitative personality modeling
- [Error Handling](./docs/features/error-handling.md) - Graceful error handling with auto-fix
- [Memory System](./docs/features/memory-system.md) - Semantic memory with LLM classification
-- [Brain System](./docs/features/brain-system.md) - Second-brain capture and classification
-- [Soul System](#m33-soul-system) - Digital twin identity scaffold management
-
-### Error Handling
-The server implements comprehensive error handling:
-- **asyncHandler**: All routes wrapped with error handler that catches uncaught errors
-- **ServerError**: Custom error class with status, code, severity, and context
-- **Socket.IO Events**: Errors broadcast to UI via `error:occurred` event
-- **Process Handlers**: Unhandled rejections and uncaught exceptions emit socket events
-- **Logging**: Errors logged with emoji prefixes, no server crashes
-- See [Error Handling](./docs/features/error-handling.md)
+- [Prompt Manager](./docs/features/prompt-manager.md) - Customizable AI prompts
+- [Soul System](./docs/features/soul-system.md) - Digital twin identity scaffold
---
@@ -93,398 +101,30 @@ The server implements comprehensive error handling:
Based on recent work and incomplete milestones:
1. **Complete M7: App Templates** - Implement template management UI and app scaffolding from templates
-2. **Vision API Polish** - Continue refining LM Studio vision integration based on test results
-3. **Post-Execution Validation** - Extend agent task validation to verify commits and changes
-4. **Shared AI Library** - Extract common AI patterns into reusable library (see [docs/SHARED_AI_LIBRARY_PLAN.md](./docs/SHARED_AI_LIBRARY_PLAN.md))
-5. **CoS Goals Refinement** - Review and update COS-GOALS.md based on recent self-improvement runs
-6. **Memory Consolidation** - Implement automatic memory consolidation for similar memories
-7. **Brain Weekly Review** - Test and refine weekly review generation
-8. **Documentation Updates** - Keep ARCHITECTURE.md and API.md current with recent features
-
----
-
-## Detailed Milestone Documentation
-
-### M7: App Templates (Planned)
-
-Templates allow creating new apps from pre-configured project structures.
-
-#### Built-in Template: PortOS Stack
-The default template mirrors this application's architecture:
-- Express.js API server
-- React + Vite frontend
-- Tailwind CSS styling
-- Collapsible navigation layout
-- PM2 ecosystem configuration
-- GitHub Actions CI/CD workflows
-- Auto-versioning system
-
-#### Features
-1. **Template Selection**: Browse available templates with feature descriptions
-2. **App Creation**: Scaffold new project with chosen name and target directory
-3. **Custom Templates**: Register additional templates from local paths
-4. **Template Management**: View, edit, delete custom templates
-
-#### Pages
-- `/templates` - Template browser and app creation
-- `/templates/new` - Register custom template
-
-#### Template Structure
-Templates are stored in `./data/templates/` with:
-- `manifest.json` - Template metadata and feature list
-- Source files to copy when creating new app
-
-#### API Endpoints
-| Route | Description |
-|-------|-------------|
-| GET /api/templates | List all templates |
-| POST /api/templates | Add custom template |
-| POST /api/templates/create | Create app from template |
-| DELETE /api/templates/:id | Remove custom template |
-
----
-
-### M8: Prompt Manager
-
-Customizable AI prompts for all backend AI operations. Inspired by void-private's prompt management architecture.
-
-#### Architecture
-- **File-based prompts**: Prompts stored as `.md` files for easy editing and version control
-- **Variable system**: Reusable variables with metadata, categories, and usage tracking
-- **Stage configuration**: Each prompt stage defines provider, model type, and execution options
-- **Template rendering**: Mustache-like syntax with conditionals, arrays, and nested data
-
-#### Directory Structure
-```
-./data/prompts/
-├── stages/ # Individual prompt templates (.md files)
-│ ├── app-detection.md
-│ ├── code-analysis.md
-│ └── ...
-├── variables.json # Reusable prompt variables
-└── stage-config.json # Stage metadata and provider config
-```
-
-#### Features
-1. **Prompt Stages**: Define different prompts for different AI tasks (detection, analysis, etc.)
-2. **Variables**: Reusable content blocks (personas, formats, constraints)
-3. **Per-Stage Provider Config**: Each stage can use different AI providers/models
-4. **Web UI**: Edit prompts, variables, and preview rendered output
-5. **Template Syntax**: `{{variable}}`, `{{#condition}}...{{/condition}}`, arrays
-
-#### UI Pages
-- `/prompts` - Prompt Manager with tabs for Stages, Variables, Elements
-- Live preview with test variables
-- Insert variable references
-
-#### API Endpoints
-| Route | Description |
-|-------|-------------|
-| GET /api/prompts | List all prompt stages |
-| GET /api/prompts/:stage | Get stage template |
-| PUT /api/prompts/:stage | Update stage/template |
-| POST /api/prompts/:stage/preview | Preview compiled prompt |
-| GET /api/prompts/variables | List all variables |
-| PUT /api/prompts/variables/:key | Update variable |
-| POST /api/prompts/variables | Create variable |
-| DELETE /api/prompts/variables/:key | Delete variable |
-
----
-
-### M9: Streaming Import Detection
-
-Enhanced app import with real-time websocket updates as AI discovers project configuration.
-
-#### Features
-1. **Progressive Discovery**: Form fields update in real-time as AI analyzes the project
-2. **Status Animations**: Visual indicators showing detection progress
-3. **PM2 Detection**: Check if app is already running in PM2
-4. **Websocket Updates**: Server streams discoveries to UI as they happen
-
-#### Discovery Steps (streamed to UI)
-1. Validating directory path
-2. Reading package.json
-3. Detecting project type (React, Express, monorepo, etc.)
-4. Finding configuration files
-5. Extracting ports from configs
-6. Detecting start commands
-7. Checking PM2 process status
-8. AI-powered analysis for name, description
-9. Generating PM2 process names
-
-#### UI/UX
-- Progress indicator with current step
-- Form fields animate in as values are discovered
-- Already-running indicator if detected in PM2
-- Expandable "Detection Log" showing raw discovery output
-
-#### API
-| Route | Description |
-|-------|-------------|
-| WS /api/detect/stream | Websocket for streaming detection |
-| GET /api/detect/pm2-status | Check if app running in PM2 |
-
----
-
-### M17: PM2 Ecosystem Config Enhancement
-
-Enhanced app management to detect and track all PM2 processes with their ports.
-
-#### Features
-- **Per-process port detection**: Extracts ports for each PM2 process from ecosystem.config.js/cjs
-- **CDP_PORT support**: Detects Chrome DevTools Protocol ports for browser processes
-- **Constant resolution**: Handles variable references like `CDP_PORT: CDP_PORT` by parsing top-level constants
-- **Comment handling**: Properly skips `//` and `/* */` comments when parsing config files
-- **Refresh button**: UI button to re-scan ecosystem config and update app data
-
-#### Schema Extension
-```javascript
-// Added to server/lib/validation.js
-export const processSchema = z.object({
- name: z.string().min(1),
- port: z.number().int().min(1).max(65535).nullable().optional(),
- description: z.string().optional()
-});
-
-// Added to appSchema
-processes: z.array(processSchema).optional()
-```
-
----
-
-### M18: PM2 Standardization
-
-LLM-powered refactoring to standardize app configurations to follow PM2 best practices.
-
-#### Features
-1. **Standardize PM2 Button**: Available in each app's expanded details row
-2. **LLM Analysis**: Uses configured AI provider to analyze project structure
-3. **Auto-apply Changes**: Automatically modifies files with git backup
-4. **Git Backup**: Creates backup branch before any modifications
-5. **Port Consolidation**: Moves all ports to ecosystem.config.cjs env blocks
-6. **Stray Port Removal**: Removes PORT from .env files, comments out port in vite.config
-
-#### PM2 Standard
-- All ports defined in `ecosystem.config.cjs` env blocks
-- PM2 configured with watch for live-reload on server processes
-- Vite processes use `npx vite --host --port XXXX` in args
-- No stray port references in .env or vite.config
-
----
-
-### M19: CoS Agent Runner
-
-Isolated PM2 process for spawning Claude CLI agents, preventing orphaned processes when portos-server restarts.
-
-#### Problem
-When multiple CoS agents are running and the main portos-server restarts (due to code changes, crashes, or manual restart), child processes spawned via `child_process.spawn()` become orphaned. The parent loses track of them because the `activeAgents` Map is in memory.
-
-#### Solution
-A separate `portos-cos` PM2 process that:
-1. Runs independently from `portos-server`
-2. Manages agent spawning via HTTP/Socket.IO bridge
-3. Doesn't restart when `portos-server` restarts
-4. Maintains its own state file for PID tracking
-
-#### Architecture
-```
-┌─────────────────┐ HTTP/Socket.IO ┌─────────────────┐
-│ portos-server │ ──────────────────► │ portos-cos │
-│ (5554) │ spawn/terminate │ (5558) │
-│ │ ◄────────────────── │ │
-│ subAgentSpawner│ events/output │ cos-runner │
-└─────────────────┘ └────────┬────────┘
- │
- │ spawn
- ▼
- ┌───────────────┐
- │ Claude CLI │
- │ Processes │
- └───────────────┘
-```
-
----
-
-### M20: AI Provider Error Handling
-
-Enhanced error extraction and display for AI provider execution failures, with automatic CoS investigation task creation.
-
-#### Problem
-When AI provider executions fail (e.g., invalid model, API errors), the devtools/runner UI only showed exit codes without meaningful error information, making debugging difficult.
-
-#### Solution
-1. **Error Extraction**: Extract meaningful error details from CLI output
-2. **Error Categorization**: Classify errors by type (model_not_found, auth_error, rate_limit, etc.)
-3. **Suggested Fixes**: Provide actionable suggestions for each error category
-4. **CoS Integration**: Create investigation tasks for actionable failures
-5. **UI Enhancement**: Display error details, category, and suggestions in history list
-
-#### Error Categories
-| Category | Description | Actionable |
-|----------|-------------|------------|
-| model_not_found | Model doesn't exist in provider | Yes |
-| auth_error | API key invalid or unauthorized | Yes |
-| api_error | Generic API request failure | Yes |
-| rate_limit | Rate limiting triggered | No (transient) |
-| quota_exceeded | Billing/quota issue | Yes |
-| network_error | Connection refused/timeout | No (transient) |
-| timeout | Process timeout (SIGTERM) | No |
-| command_not_found | CLI tool not installed | Yes |
-| permission_denied | File/directory access denied | Yes |
-| unknown | Unrecognized error pattern | No |
-
----
-
-### M21: Usage Metrics Integration
-
-Enhanced usage tracking to capture all AI provider executions across CoS agents and DevTools runner.
-
-#### Problem
-The Usage page existed but displayed zero data because the tracking functions were never called when runs executed.
-
-#### Solution
-1. **Usage Service Integration**: Added `recordSession` and `recordMessages` calls to runner.js and subAgentSpawner.js
-2. **Mobile Responsive Design**: Redesigned usage page with responsive Tailwind breakpoints
-3. **Unified Tracking**: Both manual DevTools runs and CoS agent executions now record usage
-
-#### Data Tracked
-| Metric | Source | Description |
-|--------|--------|-------------|
-| Sessions | createRun, createAgentRun | Incremented when any AI execution starts |
-| Messages | run completion | Recorded on successful run completion |
-| Tokens | Output-based estimate | ~4 characters per token estimation |
-| Provider Stats | Per provider | Sessions, messages, tokens by provider |
-| Model Stats | Per model | Sessions, messages, tokens by model |
-| Daily Activity | Per day | Sessions, messages, tokens by date |
-| Hourly Activity | 24-hour array | Session counts by hour of day |
-
----
-
-### M22: Orphan Auto-Retry
-
-Automatic retry for orphaned agents with investigation task creation after repeated failures.
-
-#### Problem
-When agents become orphaned (server restart, runner crash, etc.), the task remained stuck and the system didn't automatically retry or investigate the issue.
-
-#### Solution
-1. **Auto-Retry**: Reset orphaned tasks to pending for automatic retry (up to 3 attempts)
-2. **Retry Tracking**: Track retry count in task metadata (orphanRetryCount)
-3. **Investigation Task**: After max retries, create auto-approved investigation task
-4. **Evaluation Trigger**: Trigger task evaluation immediately after orphan cleanup
-
-#### Retry Flow
-```
-Agent Orphaned
- └─► cleanupOrphanedAgents() marks agent as completed
- └─► handleOrphanedTask() checks retry count
- ├─► retryCount < 3: Reset task to pending, trigger evaluation
- └─► retryCount >= 3: Mark task blocked, create investigation task
-```
-
----
-
-### M23: Self-Improvement System
-
-Automated self-analysis and improvement system that uses Playwright and Opus to continuously improve PortOS.
-
-See [docs/features/chief-of-staff.md](./docs/features/chief-of-staff.md#self-improvement) for details on analysis types and rotation system.
-
----
-
-### M24: Goal-Driven Proactive Mode
-
-Makes CoS proactive and goal-driven, spending more time working and less time idle.
-
-#### Solution
-
-1. **COS-GOALS.md Mission File**: Created `data/COS-GOALS.md` with mission statement, active goals, task priorities
-2. **Always-Working Behavior**: CoS ALWAYS returns work - falls back to self-improvement if apps are on cooldown
-3. **Expanded Task Types**: Added 4 new self-improvement types (cos-enhancement, test-coverage, documentation, feature-ideas)
-4. **Configuration Changes**: Shorter intervals (1m eval, 30m cooldown), MEDIUM priority for idle reviews
-
----
-
-### M25: Task Learning System
-
-Tracks patterns from completed tasks to improve future task execution and model selection.
-
-#### Features
-1. **Completion Tracking**: Records success/failure for every agent task completion
-2. **Success Rate Analysis**: Calculates success rates by task type and model tier
-3. **Duration Tracking**: Tracks average execution time per task type
-4. **Error Pattern Analysis**: Identifies recurring error categories
-5. **Model Effectiveness**: Compares performance across model tiers (light/medium/heavy)
-6. **Recommendations**: Generates actionable insights based on historical data
-
----
-
-### M26: Scheduled Scripts
-
-Cron-based script scheduling with optional agent triggering for automated health checks and maintenance.
-
-#### Features
-1. **Schedule Presets**: Common intervals (5min, 15min, hourly, daily, weekly, on-demand)
-2. **Custom Cron**: Full cron expression support for complex schedules
-3. **Command Allowlist**: Security-first design using same allowlist as command runner
-4. **Agent Triggering**: Scripts can spawn CoS agents when issues are detected
-5. **Run History**: Track last execution, output, and exit codes
-6. **Enable/Disable**: Toggle scripts without deleting them
-
----
-
-### M28: Weekly Digest UI
-
-Added a visual "Digest" tab to the Chief of Staff page that displays weekly activity summaries with insights, accomplishments, and week-over-week comparisons.
-
-#### Features
-1. **Weekly Summary View**: Visual dashboard showing tasks completed, success rate, work time, and issue count
-2. **Week-over-Week Comparison**: Shows percentage changes from previous week with trend indicators
-3. **Live Week Progress**: Real-time view of current week progress with projected totals
-4. **Top Accomplishments**: Lists most significant completed tasks sorted by duration
-5. **Task Type Breakdown**: Table view of performance metrics by task type
-6. **Error Patterns**: Highlights recurring errors that need attention
-7. **Actionable Insights**: Auto-generated insights like "Star Performer", "Needs Attention", "Recurring Issue"
-8. **Historical Navigation**: Dropdown to view digests from previous weeks
+2. **Digital Twin P3: Behavioral Feedback Loop** - Add "sounds like me" response validation and adaptive weighting
+3. **Vision API Polish** - Continue refining LM Studio vision integration based on test results
+4. **Memory Consolidation** - Implement automatic memory consolidation for similar memories
+5. **Documentation Updates** - Keep ARCHITECTURE.md and API.md current with M35 features
---
-### M29: Comprehensive App Improvement
-
-Extended the CoS self-improvement system to apply the same comprehensive analysis to managed apps (not just PortOS itself).
-
-#### Problem
-The CoS had a sophisticated self-improvement system with 12 different analysis types for PortOS itself, but managed apps only received simple idle reviews.
-
-#### Solution
-Created an app-agnostic self-improvement system that applies comprehensive analysis to any managed app:
-
-1. **10 Analysis Types for Apps**: Security audit, code quality, test coverage, performance, accessibility, console errors, dependency updates, documentation, error handling, and TypeScript typing
-2. **Rotation System**: Each app tracks its last improvement type and rotates through all 10 types
-3. **App Activity Tracking**: Extended `appActivity.js` to track `lastImprovementType` per app
-4. **Configuration Toggle**: New `comprehensiveAppImprovement` config option (default: true)
+## Error Handling Summary
----
-
-### M30: Configurable Task Intervals
-
-Per-task-type scheduling: daily, weekly, once, on-demand; schedule UI tab; execution history tracking.
-
----
-
-### M31: LLM-based Memory Classification
-
-Replaced pattern-based memory extraction with intelligent LLM-based evaluation using local LM Studio.
-
-See [Memory System](./docs/features/memory-system.md#llm-based-classification-m31) for details.
+The server implements comprehensive error handling:
+- **asyncHandler**: All routes wrapped with error handler that catches uncaught errors
+- **ServerError**: Custom error class with status, code, severity, and context
+- **Socket.IO Events**: Errors broadcast to UI via `error:occurred` event
+- **Process Handlers**: Unhandled rejections and uncaught exceptions emit socket events
+- **Logging**: Errors logged with emoji prefixes, no server crashes
+- See [Error Handling](./docs/features/error-handling.md) for details
---
-### Security Audit (2026-01-08)
+## Security Audit (2026-01-08)
Comprehensive security audit performed by CoS Self-Improvement agent.
-#### Vulnerabilities Found and Fixed
+### Vulnerabilities Found and Fixed
1. **Command Injection in Git Service** (CRITICAL - FIXED)
- File: `server/services/git.js`
@@ -494,7 +134,7 @@ Comprehensive security audit performed by CoS Self-Improvement agent.
- File: `server/routes/screenshots.js`
- Fix: Added `sanitizeFilename()` and path validation
-#### No Issues Found (Secure Patterns)
+### Secure Patterns (No Issues Found)
- Command execution uses allowlist
- PM2 operations use spawn with shell: false
- Input validation with Zod schemas
@@ -504,556 +144,34 @@ Comprehensive security audit performed by CoS Self-Improvement agent.
---
-### M27: CoS Capability Enhancements (2026-01-08)
-
-Enhancements to the Chief of Staff system for better task learning, smarter prioritization, and expanded self-improvement capabilities.
-
-#### Features
-
-1. **New Self-Improvement Task Type: Dependency Updates** - Runs `npm audit`, checks for outdated packages, updates safely
-2. **Enhanced Performance Tracking** - `getPerformanceSummary()` function provides overall success rate, top performers, tasks needing attention
-3. **Learning Insights System** - `recordLearningInsight()` and `getRecentInsights()` for storing and retrieving observations
-4. **Periodic Performance Logging** - Every 10 evaluations, CoS logs success rate and task performance stats
-
----
-
-### M33: Soul System
-
-Digital twin identity scaffold management for creating and testing aligned AI personas.
-
-#### Problem
-LLMs can embody specific personas, but creating comprehensive identity documents and testing alignment across different models is manual and error-prone.
-
-#### Solution
-A dedicated Soul page (`/soul`) with five tabs:
-
-1. **Overview Tab**: Dashboard showing soul health score, document counts, test scores, enrichment progress, and quick actions
-2. **Documents Tab**: Sidebar-based document editor for managing soul markdown files by category (core, audio, behavioral, enrichment)
-3. **Test Tab**: Multi-model behavioral testing against 14 predefined tests, with side-by-side result comparison
-4. **Enrich Tab**: Guided questionnaire across 10 categories that generates soul document content from answers
-5. **Export Tab**: Export soul for use in external LLMs (System Prompt, CLAUDE.md, JSON, individual files)
-
-#### Directory Structure
-```
-data/soul/
-├── meta.json # Document metadata, test history, settings
-├── SOUL.md # Core identity
-├── Expanded.md # High-fidelity spec
-├── BEHAVIORAL_TEST_SUITE.md # 14 behavioral tests
-├── AUDIO*.md # Audio preferences
-├── MEMORIES.md # Generated via enrichment
-├── FAVORITES.md # Generated via enrichment
-└── PREFERENCES.md # Generated via enrichment
-```
-
-#### CoS Integration
-- Soul context automatically injected into agent prompts when enabled
-- Settings control `autoInjectToCoS` and `maxContextTokens`
-- Prompt template `cos-agent-briefing.md` includes `{{soulSection}}`
-
-#### Enrichment Categories
-| Category | Description |
-|----------|-------------|
-| Core Memories | Formative experiences |
-| Favorite Books | Books that shaped thinking |
-| Favorite Movies | Films that resonate |
-| Music Taste | Cognitive infrastructure |
-| Communication | How to give/receive info |
-| Decision Making | Approach to choices |
-| Values | Core principles |
-| Aesthetics | Visual preferences |
-| Daily Routines | Structure habits |
-| Career/Skills | Professional expertise |
-
-#### API Endpoints
-| Route | Description |
-|-------|-------------|
-| GET /api/soul | Status summary |
-| GET /api/soul/documents | List documents |
-| POST /api/soul/documents | Create document |
-| PUT /api/soul/documents/:id | Update document |
-| DELETE /api/soul/documents/:id | Delete document |
-| GET /api/soul/tests | Get test suite |
-| POST /api/soul/tests/run | Run single-model tests |
-| POST /api/soul/tests/run-multi | Run multi-model tests |
-| GET /api/soul/enrich/categories | List enrichment categories |
-| POST /api/soul/enrich/question | Get next question |
-| POST /api/soul/enrich/answer | Submit answer |
-| POST /api/soul/export | Export soul |
-| GET /api/soul/validate/completeness | Check soul completeness |
-| POST /api/soul/validate/contradictions | Detect contradictions |
-| POST /api/soul/tests/generate | Generate dynamic tests |
-| POST /api/soul/analyze-writing | Analyze writing samples |
-
-#### Soul Enhancement: Reliable Digital Twins (M33.1)
-
-Additional features to create more reliable digital twins through improved data collection, validation, testing, and user guidance.
-
-##### New Enrichment Categories
-Added 3 new categories with 3 preset questions each:
-- **non_negotiables**: Principles and boundaries that define your limits
-- **decision_heuristics**: Mental models and shortcuts for making choices
-- **error_intolerance**: What your digital twin should never do
-
-##### Validation & Analysis
-1. **Completeness Validator**: Checks for 6 required sections (identity, values, communication, decision making, non-negotiables, error intolerance), shows percentage complete with actionable suggestions
-2. **Contradiction Detector**: AI-powered analysis to find inconsistencies between soul documents, with severity levels and resolution suggestions
-
-##### Dynamic Testing
-- **Generate Tests**: AI generates behavioral tests based on soul content, targeting values, communication style, non-negotiables, and decision patterns
-- Returns structured tests with prompts, expected behaviors, and failure signals
-
-##### Writing Sample Analysis
-- Paste writing samples to extract authentic voice patterns
-- Analyzes: sentence structure, vocabulary, formality, tone, distinctive markers
-- Generates WRITING_STYLE.md document content
-
-##### User Guidance
-1. **Soul Creation Wizard**: 5-step guided wizard for new users (identity, values, communication, decisions, boundaries)
-2. **Best Practices Documentation**: SOUL_GUIDE.md with comprehensive guidance on creating effective soul documents
-
-##### Context Optimization
-- **Document Weighting**: Priority slider (1-10) on each document
-- Higher weighted documents preserved first when context limits force truncation
-
----
-
-## M34: Digital Twin Personality Enhancement
-
-### Vision
-
-Transform the Digital Twin from a **document capture system** into a **quantitative personality modeling and prediction system** that can accurately embody a human's values, decision patterns, and communication style.
-
-### Current State Assessment
-
-| Strength | Gap |
-|----------|-----|
-| Rich document storage (14 categories) | No quantitative personality dimensions |
-| Multi-method enrichment | No automated extraction from behavior |
-| Behavioral testing framework | No feedback loop from real usage |
-| Writing sample analysis | No multi-modal capture (voice, video) |
-| Contradiction detection | No personality confidence scoring |
-| CoS integration | No external data source integration |
-
-### Improvement Phases
-
-#### Phase 1: Quantitative Personality Modeling (Foundation)
-
-**Goal**: Add structured personality trait scoring alongside unstructured documents.
-
-**1.1 Big Five Trait Scoring**
-- Add quantified OCEAN scores (Openness, Conscientiousness, Extraversion, Agreeableness, Neuroticism)
-- Infer scores from existing documents using LLM analysis
-- Allow manual override/adjustment
-- Store in `meta.json` under `traits.bigFive`
-
-**1.2 Values Hierarchy**
-- Extract explicit values from VALUES.md and NON_NEGOTIABLES.md
-- Create ranked values list with conflict resolution rules
-- Store in `meta.json` under `traits.valuesHierarchy`
-
-**1.3 Communication Fingerprint**
-- Quantify writing style: formality (1-10), verbosity (1-10), emoji usage, sentence length avg
-- Extract from WRITING_STYLE.md and writing samples
-- Store in `meta.json` under `traits.communicationProfile`
-
-**Data Structure**:
-```javascript
-traits: {
- bigFive: { O: 0.75, C: 0.82, E: 0.45, A: 0.68, N: 0.32 },
- valuesHierarchy: ["authenticity", "growth", "family", ...],
- communicationProfile: {
- formality: 6,
- verbosity: 4,
- avgSentenceLength: 18,
- emojiUsage: "rare",
- preferredTone: "direct-but-warm"
- },
- lastAnalyzed: "2026-01-21T..."
-}
-```
-
-**API Endpoints**:
-| Route | Description |
-|-------|-------------|
-| GET /api/digital-twin/traits | Get all trait scores |
-| POST /api/digital-twin/traits/analyze | Analyze documents → extract traits |
-| PUT /api/digital-twin/traits/:category | Manual override trait scores |
-
----
-
-#### Phase 2: Personality Confidence Scoring
-
-**Goal**: Measure how well-defined each aspect of personality is, guiding enrichment.
-
-**2.1 Coverage Metrics**
-- For each Big Five dimension: evidence count from documents
-- For each value: supporting document count + specificity score
-- For communication: sample diversity, consistency across samples
-
-**2.2 Confidence Algorithm**
-```
-confidence(aspect) = min(1.0,
- (evidence_count / required_evidence) *
- (consistency_score) *
- (recency_weight)
-)
-```
-
-**2.3 Gap Recommendations**
-- Identify lowest-confidence aspects
-- Generate specific questions to fill gaps
-- Prioritize enrichment categories by confidence gap
-
-**UI Enhancement**:
-- Add "Personality Map" visualization showing confidence per dimension
-- Color-code: green (>80%), yellow (50-80%), red (<50%)
-- Click dimension → suggested enrichment questions
-
----
-
-#### Phase 3: Behavioral Feedback Loop
-
-**Goal**: Learn from real-world twin usage to improve accuracy.
-
-**3.1 Response Validation Interface**
-- When CoS uses twin context, log the interaction
-- User can mark responses: "sounds like me" / "not quite me" / "definitely not me"
-- Store feedback in `data/digital-twin/feedback.json`
-
-**3.2 Feedback Analysis**
-- Periodically analyze feedback patterns
-- Identify which aspects of twin are underperforming
-- Generate document improvement suggestions
-
-**3.3 Adaptive Weighting**
-- Documents that correlate with "sounds like me" responses → increase weight
-- Documents that correlate with misses → flag for review
-- Auto-adjust document weights based on feedback patterns
-
-**Data Structure**:
-```javascript
-feedback: [
- {
- id: "uuid",
- timestamp: "...",
- context: "summary of interaction",
- response: "what the twin said",
- rating: "sounds_like_me" | "not_quite" | "definitely_not",
- documentsUsed: ["doc-id-1", "doc-id-2"],
- traits: { /* which traits were relevant */ }
- }
-]
-```
-
----
-
-#### Phase 4: External Data Integration
-
-**Goal**: Reduce manual input by importing from external sources.
-
-**4.1 Reading List Import**
-- Goodreads CSV import → analyze for personality insights
-- Extract: genres preferred, themes that resonate, reading patterns
-
-**4.2 Music Profile Import**
-- Spotify listening history → infer emotional patterns, energy levels
-- Last.fm scrobbles → genre preferences, listening habits
-
-**4.3 Social Media Analysis (Optional)**
-- Twitter/X export → communication style, topics of interest
-- LinkedIn export → professional values, career trajectory
-
-**4.4 Calendar Pattern Analysis**
-- iCal export → routine patterns, priorities by time allocation
-- Meeting patterns → social preferences
-
-**Implementation Notes**:
-- All imports are user-initiated, explicit consent
-- Data processed locally, not stored raw
-- Only personality inferences stored, not source data
-
----
-
-#### Phase 5: Multi-Modal Personality Capture
-
-**Goal**: Capture personality from voice and video, not just text.
-
-**5.1 Voice Analysis**
-- Record voice samples describing self
-- Extract: speech pace, pitch variation, pause patterns
-- Infer: confidence level, emotional expressiveness
-
-**5.2 Video Interview**
-- Guided self-interview with webcam
-- Analyze: facial expressions, gestures, eye contact patterns
-- Generate insights about authentic presentation vs stated identity
-
-**5.3 Audio Transcript Enrichment**
-- Transcribe voice samples → analyze as writing samples
-- Compare spoken vs written style differences
-
-**Technical Approach**:
-- Use Whisper for transcription
-- Use open-source models for facial expression analysis
-- Store only derived insights, not raw recordings
-
----
-
-#### Phase 6: Advanced Behavioral Testing
-
-**Goal**: More sophisticated twin validation beyond Q&A tests.
-
-**6.1 Scenario Simulation Tests**
-- Complex multi-turn conversations
-- Role-play scenarios (negotiation, conflict, mentoring)
-- Evaluate: consistency across turns, appropriate boundary-setting
-
-**6.2 Ethical Dilemma Tests**
-- Present moral dilemmas aligned with stated values
-- Check if twin's reasoning matches user's value hierarchy
-- Detect values actually held vs values aspired to
-
-**6.3 Communication Style Tests**
-- Generate responses, measure against communication fingerprint
-- Check: formality match, verbosity match, tone match
-- Quantitative scoring instead of pass/fail
-
-**6.4 Adversarial Testing**
-- Prompts designed to elicit non-me responses
-- Check if twin maintains boundaries (error_intolerance)
-- Verify non-negotiables are enforced
-
----
-
-#### Phase 7: Twin Personas & Context Switching
-
-**Goal**: Support multiple personality variants for different contexts.
-
-**7.1 Persona System**
-- Create named personas (Professional, Casual, Family, Creative)
-- Each persona: subset of documents + weight overrides
-- Context-aware persona selection
-
-**7.2 Blending Rules**
-- Define which traits change per persona vs remain constant
-- Example: formality varies, core values stay constant
-
-**7.3 Persona Testing**
-- Test each persona independently
-- Ensure personas don't violate core identity boundaries
-
----
-
-### Implementation Status
-
-| Phase | Status | Complexity | Impact |
-|-------|--------|------------|--------|
-| P1: Quantitative Modeling | ✅ COMPLETE | Medium | High - enables all other phases |
-| P2: Confidence Scoring | ✅ COMPLETE | Low | High - guides enrichment |
-| P3: Feedback Loop | 🔲 Planned | Medium | Very High - enables learning |
-| P4: External Integration | ✅ COMPLETE | High | Medium - convenience |
-| P5: Multi-Modal | 🔲 Planned | Very High | Medium - nice to have |
-| P6: Advanced Testing | 🔲 Planned | Medium | High - validation quality |
-| P7: Personas | 🔲 Planned | Medium | Medium - power users |
-
-### Completed: Phase 1 & 2 (2026-01-21)
-
-Implemented quantitative personality modeling and confidence scoring:
-- Big Five trait extraction via AI analysis
-- Values hierarchy with conflict detection
-- Communication profile quantification
-- Per-dimension confidence scoring
-- Gap recommendations for low-confidence areas
-- PersonalityMap radar chart component
-- ConfidenceGauge visualization component
-- GapRecommendations prioritized list component
-- API endpoints: `/traits`, `/traits/analyze`, `/confidence`, `/confidence/calculate`, `/gaps`
-
-### Success Metrics
-
-| Metric | Current | Target |
-|--------|---------|--------|
-| Behavioral test pass rate | ~70% | >90% |
-| Enrichment category coverage | Manual | Confidence-guided |
-| User feedback: "sounds like me" | N/A | >85% |
-| Time to usable twin | Hours | <30 min |
-| Trait confidence coverage | 0% | >80% across all dimensions |
-
----
-
-### Technical Considerations
-
-**New Prompt Templates Needed**:
-- `twin-trait-extractor` - Analyze documents → Big Five scores
-- `twin-values-ranker` - Extract and rank values with conflict rules
-- `twin-confidence-analyzer` - Score document evidence per dimension
-- `twin-feedback-analyzer` - Learn from usage feedback patterns
-- `twin-scenario-generator` - Create multi-turn test scenarios
-- `twin-dilemma-generator` - Create ethical dilemma tests
-
-**Schema Extensions** (`lib/digitalTwinValidation.js`):
-```javascript
-traitsSchema = z.object({
- bigFive: z.object({
- O: z.number().min(0).max(1),
- C: z.number().min(0).max(1),
- E: z.number().min(0).max(1),
- A: z.number().min(0).max(1),
- N: z.number().min(0).max(1)
- }).optional(),
- valuesHierarchy: z.array(z.string()).optional(),
- communicationProfile: communicationProfileSchema.optional(),
- lastAnalyzed: z.string().datetime().optional()
-});
-
-confidenceSchema = z.object({
- overall: z.number().min(0).max(1),
- dimensions: z.record(z.string(), z.number().min(0).max(1)),
- gaps: z.array(z.object({
- dimension: z.string(),
- confidence: z.number(),
- suggestedQuestions: z.array(z.string())
- }))
-});
-```
-
-**New UI Components**:
-- `PersonalityMap.jsx` - Radar chart of Big Five + confidence coloring
-- `ConfidenceGauge.jsx` - Per-dimension confidence indicator
-- `FeedbackRater.jsx` - "Sounds like me" rating interface
-- `GapRecommendations.jsx` - Prioritized enrichment suggestions
-- `TraitEditor.jsx` - Manual trait override interface
-
----
-
-## M35: Chief of Staff Enhancement
-
-Comprehensive upgrade to COS from reactive task executor to proactive autonomous agent.
-
-### Implementation Status
-
-| Phase | Component | Status | Files Created |
-|-------|-----------|--------|---------------|
-| **Phase 1: Memory** | BM25 Algorithm | ✅ Complete | `server/lib/bm25.js` |
-| | BM25 Index Manager | ✅ Complete | `server/services/memoryBM25.js` |
-| | Session Delta Tracker | ✅ Complete | `server/services/sessionDelta.js` |
-| | Hybrid Search (BM25+Vector) | ✅ Complete | Modified `memory.js`, `memoryRetriever.js` |
-| **Phase 5: Architecture** | Tool State Machine | ✅ Complete | `server/services/toolStateMachine.js` |
-| | Agent Gateway | ✅ Complete | `server/services/agentGateway.js` |
-| | Error Recovery | ✅ Complete | `server/services/errorRecovery.js` |
-| | Agent Run Cache | ✅ Complete | `server/services/agentRunCache.js` |
-| | Spawner Integration | ✅ Complete | Modified `subAgentSpawner.js` |
-| **Phase 2: Execution** | Event Scheduler | ✅ Complete | `server/services/eventScheduler.js` |
-| | Execution Lanes | ✅ Complete | `server/services/executionLanes.js` |
-| | Missions Service | ✅ Complete | `server/services/missions.js` |
-| | Mission Data Files | ✅ Complete | `data/cos/missions/*.json` |
-| | COS Event-Driven Updates | ✅ Complete | Modified `cos.js` |
-| | Mission Task Generation | ✅ Complete | Modified `cos.js` |
-| **Phase 3: LM Studio** | LM Studio Manager | ✅ Complete | `server/services/lmStudioManager.js` |
-| | Local Thinking | ✅ Complete | `server/services/localThinking.js` |
-| | LM Studio Routes | ✅ Complete | `server/routes/lmstudio.js` |
-| **Phase 4: Thinking** | Thinking Levels | ✅ Complete | `server/services/thinkingLevels.js` |
-| | Context Upgrader | ✅ Complete | `server/services/contextUpgrader.js` |
-| | COS Evolution | ✅ Complete | `server/services/cosEvolution.js` |
-| | Spawner Thinking Levels | ✅ Complete | Modified `subAgentSpawner.js` |
-
-### Features Added
-
-**Phase 1: Hybrid Memory Search**
-- BM25 algorithm with IDF weighting and inverted index
-- Reciprocal Rank Fusion (RRF) combining BM25 + vector search
-- 40% BM25 / 60% vector weighting for optimal retrieval
-- Session delta tracking for pending bytes/messages
+## Planned Feature Details
-**Phase 5: Agent Architecture**
-- Tool execution state machine (IDLE → START → RUNNING → UPDATE → END → ERROR)
-- Agent gateway with request deduplication and 10-minute cache
-- Error recovery with 6 strategies: retry, escalate, fallback, decompose, defer, investigate
-- Agent run cache for outputs, tool results, and contexts
+### M7: App Templates
-**Phase 2: Proactive Execution**
-- Event scheduler with cron expressions and timeout-safe timers (clamps to 2^31-1)
-- Execution lanes: critical (1), standard (2), background (3) concurrent slots
-- Mission system for long-term goals with sub-tasks
-- Initial missions: external project and solana-accounts improvements
+Templates allow creating new apps from pre-configured project structures.
-**Phase 3: Local Model Integration**
-- LM Studio availability checking and model discovery
-- Quick completions for local thinking without cloud costs
-- Memory classification using local models
-- Embeddings via local LM Studio
+**Built-in Template: PortOS Stack**
+- Express.js API server
+- React + Vite frontend
+- Tailwind CSS styling
+- PM2 ecosystem configuration
+- GitHub Actions CI/CD workflows
+- Auto-versioning system
-**Phase 4: Dynamic Model Selection**
-- Thinking levels: off, minimal, low, medium, high, xhigh
-- Level resolution hierarchy: task → hooks → agent → provider
-- Context upgrader with complexity analysis
-- COS self-evolution with automatic model changes
+**Features**
+1. Template Selection - Browse available templates with feature descriptions
+2. App Creation - Scaffold new project with chosen name and target directory
+3. Custom Templates - Register additional templates from local paths
+4. Template Management - View, edit, delete custom templates
-### New API Endpoints
+**Pages**
+- `/templates` - Template browser and app creation
+- `/templates/new` - Register custom template
+**API Endpoints**
| Route | Description |
|-------|-------------|
-| GET /api/lmstudio/status | Check LM Studio availability |
-| GET /api/lmstudio/models | List loaded models |
-| POST /api/lmstudio/completion | Local model completion |
-| POST /api/lmstudio/analyze-task | Analyze task complexity |
-| POST /api/lmstudio/classify-memory | Classify memory content |
-
-### Design Decisions
-
-1. **Mission Autonomy**: Full autonomy - COS can implement changes, run tests, commit without approval for managed apps
-2. **Model Usage**: Local-first with LM Studio, no cloud API costs for thinking
-3. **Self-Modification**: Full autonomy - COS can change its own base thinking model without user approval
-
-### Test Suite
-
-All COS Enhancement modules have comprehensive unit tests:
-
-| Test File | Module | Tests |
-|-----------|--------|-------|
-| `server/lib/bm25.test.js` | BM25 Algorithm | Tokenization, IDF, indexing, search, serialization |
-| `server/services/toolStateMachine.test.js` | Tool State Machine | State transitions, execution lifecycle, error recovery |
-| `server/services/thinkingLevels.test.js` | Thinking Levels | Level resolution, model selection, upgrade/downgrade |
-| `server/services/executionLanes.test.js` | Execution Lanes | Lane acquisition, release, capacity, promotion |
-| `server/services/errorRecovery.test.js` | Error Recovery | Error analysis, strategy selection, execution |
-| `server/services/agentRunCache.test.js` | Agent Run Cache | Caching, TTL, invalidation, statistics |
-| `server/services/missions.test.js` | Missions Service | CRUD, sub-tasks, proactive generation, archiving |
-
-Run all tests:
-```bash
-cd server && npm test
-# 24 test files, 722 tests
-```
-
-### Integration Complete
-
-All M35 phases have been integrated into the main codebase:
-
-1. ✅ Tool state machine integrated into `subAgentSpawner.js`
- - Creates execution tracking per agent spawn
- - Releases lanes on completion/error
- - Completes executions with success/failure status
-
-2. ✅ Event scheduler integrated into `cos.js`
- - Replaced setInterval with `scheduleEvent()` for evaluation loop
- - Replaced setInterval with `scheduleEvent()` for health check loop
- - Events use timeout-safe timers (clamps to 2^31-1)
-
-3. ✅ Thinking levels integrated into `subAgentSpawner.js`
- - `resolveThinkingLevel()` checks task → agent → provider hierarchy
- - `getModelForLevel()` maps thinking levels to actual models
- - `isLocalPreferred()` identifies local-first execution opportunities
-
-4. ✅ Execution lanes integrated into `subAgentSpawner.js`
- - `determineLane()` assigns lane based on task priority
- - `acquire()` and `waitForLane()` manage lane slots
- - `release()` frees lane on agent completion
-
-5. ✅ Missions connected to proactive task generation
- - `generateMissionTasks()` generates tasks from active missions
- - Mission tasks inserted after user/system tasks, before idle review
- - Tasks respect `autonomyLevel` for approval requirements
-3. Update `subAgentSpawner.js` to use thinking levels and context upgrader
-4. Add lane acquisition before agent spawning
-5. Connect missions to proactive task generation
+| GET /api/templates | List all templates |
+| POST /api/templates | Add custom template |
+| POST /api/templates/create | Create app from template |
+| DELETE /api/templates/:id | Remove custom template |
diff --git a/docs/SHARED_AI_LIBRARY_PLAN.md b/docs/SHARED_AI_LIBRARY_PLAN.md
deleted file mode 100644
index 4a3a948759..0000000000
--- a/docs/SHARED_AI_LIBRARY_PLAN.md
+++ /dev/null
@@ -1,533 +0,0 @@
-# Shared AI Library Plan
-
-This document outlines a plan for creating a shared library to DRY up AI provider, model, and prompt template patterns across PortOS-style Express/React/Tailwind projects.
-
-## Problem Statement
-
-Most PortOS-style apps use the same patterns for:
-1. **AI Provider Configuration**: CLI and API provider types with models, endpoints, API keys
-2. **Model Selection**: Tiered model selection (light/medium/heavy) based on task complexity
-3. **Prompt Templates**: Mustache-like templating with variable substitution
-4. **Run Execution**: Dual execution system (CLI spawn vs API fetch)
-5. **Provider UI Components**: Dropdowns, forms, settings panels
-
-This creates significant code duplication across projects, making maintenance difficult and bug fixes inconsistent.
-
-## Current Implementation Analysis
-
-### Files That Would Move to Shared Library
-
-#### Server-Side (Express)
-| File | Purpose | Lines (approx) |
-|------|---------|----------------|
-| `server/services/providers.js` | Provider CRUD, testing, model refresh | ~300 |
-| `server/services/runner.js` | CLI/API run execution, streaming | ~400 |
-| `server/lib/validation.js` (partial) | Provider/Run Zod schemas | ~100 |
-
-#### Client-Side (React)
-| File | Purpose | Lines (approx) |
-|------|---------|----------------|
-| `client/src/pages/AIProviders.jsx` | Provider management UI | ~500 |
-| `client/src/services/api.js` (partial) | Provider API client functions | ~100 |
-
-#### Shared Patterns
-| Pattern | Location | Description |
-|---------|----------|-------------|
-| Provider Schema | `data.sample/providers.json` | JSON structure for provider configs |
-| Prompt Templates | `data/prompts/` | File-based prompt system |
-| Model Tiering | `server/services/subAgentSpawner.js` | Task-to-model routing logic |
-
-## Proposed Library Architecture
-
-### Package: `@portos/ai-toolkit`
-
-A modular npm package with the following structure:
-
-```
-@portos/ai-toolkit/
-├── package.json
-├── src/
-│ ├── index.js # Main exports
-│ │
-│ ├── server/ # Express middleware and services
-│ │ ├── index.js
-│ │ ├── providers.js # Provider service (CRUD, test, refresh)
-│ │ ├── runner.js # Run execution (CLI/API)
-│ │ ├── prompts.js # Prompt template service
-│ │ ├── modelSelector.js # Tiered model selection logic
-│ │ ├── routes/
-│ │ │ ├── providers.js # Express routes for providers
-│ │ │ ├── runs.js # Express routes for runs
-│ │ │ └── prompts.js # Express routes for prompts
-│ │ └── validation.js # Zod schemas
-│ │
-│ ├── client/ # React components and hooks
-│ │ ├── index.js
-│ │ ├── components/
-│ │ │ ├── ProviderList.jsx
-│ │ │ ├── ProviderForm.jsx
-│ │ │ ├── ProviderDropdown.jsx
-│ │ │ ├── ModelDropdown.jsx
-│ │ │ ├── RunPanel.jsx
-│ │ │ └── PromptEditor.jsx
-│ │ ├── hooks/
-│ │ │ ├── useProviders.js
-│ │ │ ├── useRuns.js
-│ │ │ └── usePrompts.js
-│ │ └── api.js # API client functions
-│ │
-│ └── shared/ # Code used by both server and client
-│ ├── types.js # TypeScript/JSDoc type definitions
-│ ├── constants.js # Shared constants
-│ └── utils.js # Utility functions
-│
-├── templates/ # Sample data files
-│ ├── providers.sample.json
-│ └── prompts/
-│ └── stage-config.json
-│
-└── README.md
-```
-
-### Key Design Decisions
-
-#### 1. Modular Imports
-Allow importing only what you need:
-
-```javascript
-// Server usage
-import { providerService, createProviderRoutes } from '@portos/ai-toolkit/server';
-
-// Client usage
-import { ProviderDropdown, useProviders } from '@portos/ai-toolkit/client';
-
-// Full bundle
-import { server, client } from '@portos/ai-toolkit';
-```
-
-#### 2. Configuration-First Design
-The library should be configurable via a single config object:
-
-```javascript
-// server/index.js
-import { createAIToolkit } from '@portos/ai-toolkit/server';
-
-const aiToolkit = createAIToolkit({
- dataDir: './data', // Where to store providers.json, prompts/
- providersFile: 'providers.json', // Provider storage filename
- promptsDir: 'prompts', // Prompt templates directory
-
- // Socket.IO integration (optional)
- io: socketIOInstance,
-
- // Custom model selection logic (optional override)
- modelSelector: (task, provider) => {
- // Custom logic
- return { model: 'custom-model', tier: 'custom', reason: 'custom-logic' };
- },
-
- // Event hooks (optional)
- hooks: {
- onProviderCreated: (provider) => {},
- onRunStarted: (run) => {},
- onRunCompleted: (run, output) => {},
- onRunFailed: (run, error) => {}
- }
-});
-
-// Mount routes
-app.use('/api/providers', aiToolkit.routes.providers);
-app.use('/api/runs', aiToolkit.routes.runs);
-app.use('/api/prompts', aiToolkit.routes.prompts);
-
-// Access services directly if needed
-const provider = await aiToolkit.services.providers.getById('my-provider');
-```
-
-#### 3. Tailwind Design Token Support
-Components should accept a theme prop for custom design tokens:
-
-```jsx
-import { ProviderDropdown } from '@portos/ai-toolkit/client';
-
-<ProviderDropdown
- theme={{
- bg: 'port-card',
- border: 'port-border',
- accent: 'port-accent',
- text: 'white',
- textMuted: 'gray-400'
- }}
- value={selectedProvider}
- onChange={setSelectedProvider}
-/>
-```
-
-Or use CSS variables:
-
-```css
-:root {
- --ai-toolkit-bg: #1a1a1a;
- --ai-toolkit-border: #2a2a2a;
- --ai-toolkit-accent: #3b82f6;
-}
-```
-
-#### 4. Dependency Injection for Storage
-Allow custom storage backends:
-
-```javascript
-const aiToolkit = createAIToolkit({
- storage: {
- // Default: JSON file storage
- type: 'json',
- dataDir: './data'
- }
- // Or custom implementation
- storage: {
- type: 'custom',
- adapter: myDatabaseAdapter // Must implement ProviderStorage interface
- }
-});
-```
-
-#### 5. No Try/Catch (Per Project Guidelines)
-All functions should let errors bubble up. The consuming app handles errors via its own middleware.
-
-## Component Specifications
-
-### Server Components
-
-#### `providerService`
-
-```javascript
-// Core CRUD
-getAllProviders() // Returns { activeProvider, providers }
-getProviderById(id) // Returns provider or null
-getActiveProvider() // Returns active provider
-setActiveProvider(id) // Sets active provider
-createProvider(data) // Creates new provider
-updateProvider(id, data) // Updates provider
-deleteProvider(id) // Deletes provider (with fallback logic)
-
-// Testing & Refresh
-testProvider(id) // Tests CLI command or API endpoint
-refreshProviderModels(id) // Fetches models from API endpoint
-
-// Vision
-checkVisionHealth(id) // Tests vision capability
-```
-
-#### `runnerService`
-
-```javascript
-createRun(options) // Creates run metadata
-executeCliRun(run, io) // Spawns CLI provider
-executeApiRun(run, io) // Calls API endpoint
-stopRun(runId) // Terminates running process
-getRuns(limit, offset, source) // Lists run history
-getRunOutput(runId) // Gets run output
-getRunPrompt(runId) // Gets run prompt
-```
-
-#### `promptService`
-
-```javascript
-getStages() // Lists all prompt stages
-getStage(name) // Gets stage config + template
-updateStage(name, data) // Updates stage
-previewStage(name, variables) // Renders template with variables
-getVariables() // Lists all variables
-createVariable(key, data) // Creates variable
-updateVariable(key, data) // Updates variable
-deleteVariable(key) // Deletes variable
-```
-
-#### `modelSelector`
-
-```javascript
-selectModelForTask(task, provider) // Returns { model, tier, reason }
-
-// Default tiers:
-// - heavy: Critical, visual, complex reasoning, long context
-// - medium: Standard coding tasks (default)
-// - light: Documentation-only tasks (never for coding)
-```
-
-### Client Components
-
-#### `<ProviderDropdown />`
-
-```jsx
-<ProviderDropdown
- value={providerId}
- onChange={(id) => setProviderId(id)}
- filter={(p) => p.enabled} // Optional filter
- showType={true} // Show CLI/API badge
- placeholder="Select provider..."
-/>
-```
-
-#### `<ModelDropdown />`
-
-```jsx
-<ModelDropdown
- providerId={providerId}
- value={model}
- onChange={(m) => setModel(m)}
- showTiers={true} // Show light/medium/heavy badges
-/>
-```
-
-#### `<ProviderForm />`
-
-```jsx
-<ProviderForm
- provider={editingProvider} // null for create mode
- onSubmit={(data) => saveProvider(data)}
- onCancel={() => setEditing(null)}
-/>
-```
-
-#### `<RunPanel />`
-
-```jsx
-<RunPanel
- providerId={selectedProvider}
- model={selectedModel}
- onRunComplete={(run) => refreshHistory()}
-/>
-```
-
-#### `<PromptEditor />`
-
-```jsx
-<PromptEditor
- stage={selectedStage}
- variables={variables}
- onSave={(template) => saveStage(template)}
-/>
-```
-
-### Hooks
-
-#### `useProviders()`
-
-```javascript
-const {
- providers, // Provider list
- activeProvider, // Currently active provider
- isLoading,
- error,
- refetch,
- setActive, // Set active provider
- testProvider, // Test connectivity
- refreshModels // Refresh API models
-} = useProviders();
-```
-
-#### `useRuns(options)`
-
-```javascript
-const {
- runs,
- isLoading,
- error,
- createRun,
- stopRun,
- deleteRun,
- refetch
-} = useRuns({ limit: 50, source: 'devtools' });
-```
-
-## Migration Strategy
-
-### Phase 1: Extract & Package (Week 1-2)
-1. Create new npm package repository
-2. Extract server services (providers, runner, prompts)
-3. Extract validation schemas
-4. Write unit tests for all services
-5. Publish as `@portos/[email protected]`
-
-### Phase 2: Client Components (Week 2-3)
-1. Extract React components
-2. Create hooks for data fetching
-3. Add Storybook for component documentation
-4. Ensure Tailwind theme compatibility
-5. Publish `@portos/[email protected]`
-
-### Phase 3: Integration & Testing (Week 3-4)
-1. Update PortOS to use the package
-2. Test all AI-related functionality
-3. Document breaking changes
-4. Create migration guide
-5. Publish `@portos/[email protected]`
-
-### Phase 4: Multi-Project Adoption (Week 4+)
-1. Update other PortOS-style projects
-2. Collect feedback
-3. Iterate on API design
-4. Maintain backward compatibility
-
-## API Compatibility
-
-The library maintains API compatibility with the current PortOS implementation:
-
-| Current Endpoint | Library Route | Notes |
-|------------------|---------------|-------|
-| `GET /api/providers` | Same | No change |
-| `PUT /api/providers/active` | Same | No change |
-| `POST /api/providers/:id/test` | Same | No change |
-| `POST /api/providers/:id/refresh-models` | Same | No change |
-| `GET /api/runs` | Same | No change |
-| `POST /api/runs` | Same | No change |
-| `GET /api/prompts` | Same | No change |
-
-## Configuration Schema
-
-```javascript
-const defaultConfig = {
- // Data storage
- dataDir: './data',
- providersFile: 'providers.json',
- promptsDir: 'prompts',
- runsDir: 'runs',
-
- // Defaults
- defaultTimeout: 300000, // 5 minutes
- maxConcurrentRuns: 5,
-
- // Model selection
- modelSelector: defaultModelSelector,
-
- // Prompt templating
- templateEngine: 'mustache', // or 'handlebars'
-
- // API settings
- defaultTemperature: 0.1,
- streamingEnabled: true,
-
- // Vision support
- visionEnabled: true,
- maxImageSize: 10 * 1024 * 1024, // 10MB
-
- // Logging
- logLevel: 'info',
- logPrefix: '[AI-Toolkit]'
-};
-```
-
-## Extensibility Points
-
-### Custom Provider Types
-Projects can register custom provider types beyond CLI and API:
-
-```javascript
-aiToolkit.registerProviderType('mcp', {
- execute: async (provider, prompt, options) => {
- // Custom MCP server execution
- },
- test: async (provider) => {
- // Test MCP connection
- },
- refreshModels: async (provider) => {
- // Fetch available tools/models
- }
-});
-```
-
-### Custom Model Selection
-Override the default tiered selection:
-
-```javascript
-const aiToolkit = createAIToolkit({
- modelSelector: (task, provider) => {
- // Always use opus for this project
- return { model: 'claude-opus-4-5-20251101', tier: 'heavy', reason: 'project-default' };
- }
-});
-```
-
-### Event Hooks
-Subscribe to lifecycle events:
-
-```javascript
-aiToolkit.on('run:started', (run) => {
- analytics.track('ai_run_started', { provider: run.providerId });
-});
-
-aiToolkit.on('run:completed', (run, output) => {
- usage.recordTokens(run.providerId, estimateTokens(output));
-});
-```
-
-## Testing Strategy
-
-### Unit Tests
-- Each service function tested in isolation
-- Mock file system operations
-- Mock HTTP requests for API providers
-- Mock child_process for CLI providers
-
-### Integration Tests
-- Full route testing with supertest
-- Real file system operations in temp directory
-- Actual CLI execution with mock provider
-
-### Component Tests
-- React Testing Library for all components
-- Hook testing with @testing-library/react-hooks
-- Snapshot tests for UI consistency
-
-## Documentation
-
-### README.md
-- Quick start guide
-- Installation instructions
-- Basic usage examples
-- Configuration reference
-
-### API Reference
-- Full JSDoc comments on all exports
-- TypeScript declarations for IDE support
-- Inline code examples
-
-### Storybook
-- Interactive component playground
-- Props documentation
-- Theme customization examples
-
-### Migration Guide
-- Step-by-step migration from current implementation
-- Breaking changes list
-- Code diff examples
-
-## Success Metrics
-
-1. **Code Reduction**: 50%+ reduction in AI-related code per project
-2. **Bug Fix Propagation**: Fixes in library apply to all projects
-3. **Onboarding Time**: New projects get AI features in <30 minutes
-4. **Test Coverage**: >80% coverage on library code
-5. **Documentation**: Complete API reference and examples
-
-## Risks & Mitigations
-
-| Risk | Mitigation |
-|------|------------|
-| Breaking changes affect multiple projects | Semantic versioning, deprecation warnings |
-| Performance overhead from abstraction | Benchmark critical paths, optimize hot paths |
-| Tailwind class conflicts | Use CSS custom properties, scoped class prefixes |
-| Different project requirements | Extensible configuration, escape hatches |
-| Maintenance burden | Clear ownership, automated testing, documentation |
-
-## Next Steps
-
-1. **Review this plan** with stakeholders
-2. **Create the package repository** at `github.com/atomantic/portos-ai-toolkit`
-3. **Start Phase 1** with server-side extraction
-4. **Track progress** in the package's PLAN.md
-
----
-
-*Created: 2026-01-10*
-*Status: Draft - Awaiting Review*
diff --git a/docs/features/cos-agent-runner.md b/docs/features/cos-agent-runner.md
new file mode 100644
index 0000000000..40bd60ee83
--- /dev/null
+++ b/docs/features/cos-agent-runner.md
@@ -0,0 +1,45 @@
+# CoS Agent Runner
+
+Isolated PM2 process for spawning Claude CLI agents, preventing orphaned processes when portos-server restarts.
+
+## Problem
+
+When multiple CoS agents are running and the main portos-server restarts (due to code changes, crashes, or manual restart), child processes spawned via `child_process.spawn()` become orphaned. The parent loses track of them because the `activeAgents` Map is in memory.
+
+## Solution
+
+A separate `portos-cos` PM2 process that:
+1. Runs independently from `portos-server`
+2. Manages agent spawning via HTTP/Socket.IO bridge
+3. Doesn't restart when `portos-server` restarts
+4. Maintains its own state file for PID tracking
+
+## Architecture
+
+```
+┌─────────────────┐ HTTP/Socket.IO ┌─────────────────┐
+│ portos-server │ ──────────────────► │ portos-cos │
+│ (5554) │ spawn/terminate │ (5558) │
+│ │ ◄────────────────── │ │
+│ subAgentSpawner│ events/output │ cos-runner │
+└─────────────────┘ └────────┬────────┘
+ │
+ │ spawn
+ ▼
+ ┌───────────────┐
+ │ Claude CLI │
+ │ Processes │
+ └───────────────┘
+```
+
+## Features
+
+- **Process Isolation**: Agent processes survive server restarts
+- **State Persistence**: PIDs tracked in state file for recovery
+- **Bridge Communication**: HTTP/Socket.IO for cross-process messaging
+- **Orphan Detection**: Automatic cleanup of orphaned agent processes
+
+## Related Features
+
+- [Chief of Staff](./chief-of-staff.md) - Main orchestration system
+- [Error Handling](./error-handling.md) - Agent error recovery
diff --git a/docs/features/cos-enhancement.md b/docs/features/cos-enhancement.md
new file mode 100644
index 0000000000..64e15d23cb
--- /dev/null
+++ b/docs/features/cos-enhancement.md
@@ -0,0 +1,126 @@
+# Chief of Staff Enhancement (M35)
+
+Comprehensive upgrade from reactive task executor to proactive autonomous agent with hybrid memory, missions, local model integration, and dynamic thinking levels.
+
+## Architecture
+
+### New Services
+
+| Service | Purpose |
+|---------|---------|
+| `server/lib/bm25.js` | BM25 algorithm with IDF weighting |
+| `server/services/memoryBM25.js` | BM25 index manager for memory search |
+| `server/services/sessionDelta.js` | Session delta tracking for pending bytes/messages |
+| `server/services/toolStateMachine.js` | Tool execution state machine |
+| `server/services/agentGateway.js` | Request deduplication and caching |
+| `server/services/errorRecovery.js` | Error analysis and recovery strategies |
+| `server/services/agentRunCache.js` | Agent output caching with TTL |
+| `server/services/eventScheduler.js` | Cron-based event scheduling |
+| `server/services/executionLanes.js` | Concurrent execution lane management |
+| `server/services/missions.js` | Long-term goal and mission management |
+| `server/services/lmStudioManager.js` | LM Studio model discovery and health |
+| `server/services/localThinking.js` | Local model completions |
+| `server/services/thinkingLevels.js` | Dynamic model selection |
+| `server/services/contextUpgrader.js` | Complexity analysis for model upgrade |
+| `server/services/cosEvolution.js` | Self-evolution and model changes |
+
+## Features
+
+### Phase 1: Hybrid Memory Search
+
+- BM25 algorithm with IDF weighting and inverted index
+- Reciprocal Rank Fusion (RRF) combining BM25 + vector search
+- 40% BM25 / 60% vector weighting for optimal retrieval
+- Session delta tracking for pending bytes/messages
+
+### Phase 2: Proactive Execution
+
+- Event scheduler with cron expressions and timeout-safe timers (clamps to 2^31-1)
+- Execution lanes: critical (1), standard (2), background (3) concurrent slots
+- Mission system for long-term goals with sub-tasks
+- Mission-driven task generation in evaluation loop
+
+### Phase 3: Local Model Integration
+
+- LM Studio availability checking and model discovery
+- Quick completions for local thinking without cloud costs
+- Memory classification using local models
+- Embeddings via local LM Studio
+
+### Phase 4: Dynamic Model Selection
+
+- Thinking levels: off, minimal, low, medium, high, xhigh
+- Level resolution hierarchy: task → hooks → agent → provider
+- Context upgrader with complexity analysis
+- COS self-evolution with automatic model changes
+
+### Phase 5: Agent Architecture
+
+- Tool execution state machine (IDLE → START → RUNNING → UPDATE → END → ERROR)
+- Agent gateway with request deduplication and 10-minute cache
+- Error recovery with 6 strategies: retry, escalate, fallback, decompose, defer, investigate
+- Agent run cache for outputs, tool results, and contexts
+
+## Execution Lanes
+
+| Lane | Concurrent Slots | Purpose |
+|------|------------------|---------|
+| critical | 1 | Emergency fixes, blocking issues |
+| standard | 2 | Normal development tasks |
+| background | 3 | Self-improvement, documentation |
+
+## Thinking Levels
+
+| Level | Description | Model Tier |
+|-------|-------------|------------|
+| off | No extended thinking | light |
+| minimal | Brief analysis | light |
+| low | Standard analysis | medium |
+| medium | Thorough analysis | medium |
+| high | Deep analysis | heavy |
+| xhigh | Maximum analysis | heavy |
+
+## Error Recovery Strategies
+
+| Strategy | When Used |
+|----------|-----------|
+| retry | Transient errors (rate limit, timeout) |
+| escalate | Persistent failures requiring human review |
+| fallback | Model unavailable, try alternative |
+| decompose | Complex task, break into subtasks |
+| defer | Non-urgent, schedule for later |
+| investigate | Unknown error, create investigation task |
+
+## API Endpoints
+
+| Route | Description |
+|-------|-------------|
+| GET /api/lmstudio/status | Check LM Studio availability |
+| GET /api/lmstudio/models | List loaded models |
+| POST /api/lmstudio/completion | Local model completion |
+| POST /api/lmstudio/analyze-task | Analyze task complexity |
+| POST /api/lmstudio/classify-memory | Classify memory content |
+
+## Design Decisions
+
+1. **Mission Autonomy**: Full autonomy - COS can implement changes, run tests, commit without approval for managed apps
+2. **Model Usage**: Local-first with LM Studio, no cloud API costs for thinking
+3. **Self-Modification**: Full autonomy - COS can change its own base thinking model without user approval
+
+## Test Coverage
+
+| Test File | Module |
+|-----------|--------|
+| `server/lib/bm25.test.js` | BM25 Algorithm |
+| `server/services/toolStateMachine.test.js` | Tool State Machine |
+| `server/services/thinkingLevels.test.js` | Thinking Levels |
+| `server/services/executionLanes.test.js` | Execution Lanes |
+| `server/services/errorRecovery.test.js` | Error Recovery |
+| `server/services/agentRunCache.test.js` | Agent Run Cache |
+| `server/services/missions.test.js` | Missions Service |
+
+## Related Features
+
+- [Chief of Staff](./chief-of-staff.md) - Core orchestration
+- [Memory System](./memory-system.md) - Memory search integration
+- [Error Handling](./error-handling.md) - Error recovery integration
diff --git a/docs/features/digital-twin.md b/docs/features/digital-twin.md
new file mode 100644
index 0000000000..1e2604ce53
--- /dev/null
+++ b/docs/features/digital-twin.md
@@ -0,0 +1,134 @@
+# Digital Twin Personality Enhancement
+
+Quantitative personality modeling and prediction system that accurately embodies a human's values, decision patterns, and communication style.
+
+## Vision
+
+Transform the Digital Twin from a document capture system into a quantitative personality modeling and prediction system.
+
+## Architecture
+
+- **Digital Twin Service** (`server/services/digitalTwin.js`): Trait analysis, confidence scoring, gap recommendations
+- **Digital Twin Routes** (`server/routes/digitalTwin.js`): REST API endpoints
+- **Digital Twin Validation** (`server/lib/digitalTwinValidation.js`): Zod schemas for trait data
+
+## Features
+
+### Phase 1: Quantitative Personality Modeling (Complete)
+
+**Big Five Trait Scoring**
+- Quantified OCEAN scores (Openness, Conscientiousness, Extraversion, Agreeableness, Neuroticism)
+- Infer scores from existing documents using LLM analysis
+- Allow manual override/adjustment
+- Store in `meta.json` under `traits.bigFive`
+
+**Values Hierarchy**
+- Extract explicit values from VALUES.md and NON_NEGOTIABLES.md
+- Create ranked values list with conflict resolution rules
+- Store in `meta.json` under `traits.valuesHierarchy`
+
+**Communication Fingerprint**
+- Quantify writing style: formality (1-10), verbosity (1-10), emoji usage, sentence length avg
+- Extract from WRITING_STYLE.md and writing samples
+- Store in `meta.json` under `traits.communicationProfile`
+
+### Phase 2: Personality Confidence Scoring (Complete)
+
+**Coverage Metrics**
+- For each Big Five dimension: evidence count from documents
+- For each value: supporting document count + specificity score
+- For communication: sample diversity, consistency across samples
+
+**Confidence Algorithm**
+```
+confidence(aspect) = min(1.0,
+ (evidence_count / required_evidence) *
+ (consistency_score) *
+ (recency_weight)
+)
+```
+
+**Gap Recommendations**
+- Identify lowest-confidence aspects
+- Generate specific questions to fill gaps
+- Prioritize enrichment categories by confidence gap
+
+### Phase 4: External Data Integration (Complete)
+
+Import from external sources to reduce manual input:
+- Goodreads CSV import for reading preferences
+- Spotify/Last.fm for music profile
+- Calendar pattern analysis for routines
+
+## Data Structure
+
+```javascript
+traits: {
+ bigFive: { O: 0.75, C: 0.82, E: 0.45, A: 0.68, N: 0.32 },
+ valuesHierarchy: ["authenticity", "growth", "family", ...],
+ communicationProfile: {
+ formality: 6,
+ verbosity: 4,
+ avgSentenceLength: 18,
+ emojiUsage: "rare",
+ preferredTone: "direct-but-warm"
+ },
+ lastAnalyzed: "2026-01-21T..."
+}
+```
+
+## UI Components
+
+- `PersonalityMap.jsx` - Radar chart of Big Five with confidence coloring
+- `ConfidenceGauge.jsx` - Per-dimension confidence indicator
+- `GapRecommendations.jsx` - Prioritized enrichment suggestions
+- `TraitEditor.jsx` - Manual trait override interface
+
+## API Endpoints
+
+| Route | Description |
+|-------|-------------|
+| GET /api/digital-twin/traits | Get all trait scores |
+| POST /api/digital-twin/traits/analyze | Analyze documents to extract traits |
+| PUT /api/digital-twin/traits/:category | Manual override trait scores |
+| GET /api/digital-twin/confidence | Get confidence scores |
+| POST /api/digital-twin/confidence/calculate | Recalculate confidence |
+| GET /api/digital-twin/gaps | Get gap recommendations |
+
+## Planned Phases
+
+### Phase 3: Behavioral Feedback Loop
+- Response validation: "sounds like me" / "not quite me" ratings
+- Feedback analysis and document improvement suggestions
+- Adaptive document weighting based on feedback patterns
+
+### Phase 5: Multi-Modal Personality Capture
+- Voice analysis for speech patterns
+- Video interview for facial expressions and gestures
+- Comparison of spoken vs written style
+
+### Phase 6: Advanced Behavioral Testing
+- Complex multi-turn conversation scenarios
+- Ethical dilemma tests aligned with stated values
+- Quantitative scoring of communication style match
+- Adversarial testing of boundaries
+
+### Phase 7: Twin Personas & Context Switching
+- Named personas (Professional, Casual, Family, Creative)
+- Blending rules for trait variation per context
+- Per-persona testing
+
+## Success Metrics
+
+| Metric | Current | Target |
+|--------|---------|--------|
+| Behavioral test pass rate | ~70% | >90% |
+| Enrichment category coverage | Manual | Confidence-guided |
+| User feedback: "sounds like me" | N/A | >85% |
+| Time to usable twin | Hours | <30 min |
+| Trait confidence coverage | 0% | >80% across all dimensions |
+
+## Related Features
+
+- [Soul System](./soul-system.md) - Document-based identity management
+- [Chief of Staff](./chief-of-staff.md) - Uses twin context in agent prompts
diff --git a/docs/features/prompt-manager.md b/docs/features/prompt-manager.md
new file mode 100644
index 0000000000..cfb7c3c85c
--- /dev/null
+++ b/docs/features/prompt-manager.md
@@ -0,0 +1,61 @@
+# Prompt Manager
+
+Customizable AI prompts for all backend AI operations with file-based storage and template rendering.
+
+## Architecture
+
+- **Prompt Service** (`server/services/prompts.js`): Template loading, variable substitution, stage configuration
+- **Prompt Routes** (`server/routes/prompts.js`): REST API endpoints
+- **Prompt Page** (`client/src/pages/Prompts.jsx`): Stages, Variables, Elements tabs with live preview
+
+## Features
+
+1. **Prompt Stages**: Define different prompts for different AI tasks (detection, analysis, etc.)
+2. **Variables**: Reusable content blocks (personas, formats, constraints)
+3. **Per-Stage Provider Config**: Each stage can use different AI providers/models
+4. **Web UI**: Edit prompts, variables, and preview rendered output
+5. **Template Syntax**: `{{variable}}`, `{{#condition}}...{{/condition}}`, arrays
+
+## Directory Structure
+
+```
+./data/prompts/
+├── stages/ # Individual prompt templates (.md files)
+│ ├── app-detection.md
+│ ├── code-analysis.md
+│ └── ...
+├── variables.json # Reusable prompt variables
+└── stage-config.json # Stage metadata and provider config
+```
+
+## Template Syntax
+
+Templates use Mustache-like syntax:
+
+- `{{variable}}` - Simple variable substitution
+- `{{#condition}}...{{/condition}}` - Conditional blocks
+- `{{#array}}...{{/array}}` - Array iteration
+
+## API Endpoints
+
+| Route | Description |
+|-------|-------------|
+| GET /api/prompts | List all prompt stages |
+| GET /api/prompts/:stage | Get stage template |
+| PUT /api/prompts/:stage | Update stage/template |
+| POST /api/prompts/:stage/preview | Preview compiled prompt |
+| GET /api/prompts/variables | List all variables |
+| PUT /api/prompts/variables/:key | Update variable |
+| POST /api/prompts/variables | Create variable |
+| DELETE /api/prompts/variables/:key | Delete variable |
+
+## UI
+
+- `/prompts` - Prompt Manager with tabs for Stages, Variables, Elements
+- Live preview with test variables
+- Insert variable references
+
+## Related Features
+
+- [Chief of Staff](./chief-of-staff.md) - Uses prompts for agent briefings
+- [Memory System](./memory-system.md) - Uses prompts for memory evaluation
diff --git a/docs/features/soul-system.md b/docs/features/soul-system.md
new file mode 100644
index 0000000000..b3cd4ad10d
--- /dev/null
+++ b/docs/features/soul-system.md
@@ -0,0 +1,112 @@
+# Soul System
+
+Digital twin identity scaffold management for creating and testing aligned AI personas.
+
+## Overview
+
+LLMs can embody specific personas, but creating comprehensive identity documents and testing alignment across different models is manual and error-prone. The Soul System provides a structured approach to capturing, validating, and deploying personality models.
+
+## Architecture
+
+- **Soul Service** (`server/services/soul.js`): Document management, testing, enrichment
+- **Soul Routes** (`server/routes/soul.js`): REST API endpoints
+- **Soul Page** (`client/src/pages/Soul.jsx`): Overview, Documents, Test, Enrich, Export tabs
+
+## Features
+
+### Five-Tab Interface
+
+1. **Overview Tab**: Dashboard showing soul health score, document counts, test scores, enrichment progress, and quick actions
+2. **Documents Tab**: Sidebar-based document editor for managing soul markdown files by category (core, audio, behavioral, enrichment)
+3. **Test Tab**: Multi-model behavioral testing against 14 predefined tests, with side-by-side result comparison
+4. **Enrich Tab**: Guided questionnaire across 10 categories that generates soul document content from answers
+5. **Export Tab**: Export soul for use in external LLMs (System Prompt, CLAUDE.md, JSON, individual files)
+
+### CoS Integration
+
+- Soul context automatically injected into agent prompts when enabled
+- Settings control `autoInjectToCoS` and `maxContextTokens`
+- Prompt template `cos-agent-briefing.md` includes `{{soulSection}}`
+
+## Directory Structure
+
+```
+data/soul/
+├── meta.json # Document metadata, test history, settings
+├── SOUL.md # Core identity
+├── Expanded.md # High-fidelity spec
+├── BEHAVIORAL_TEST_SUITE.md # 14 behavioral tests
+├── AUDIO*.md # Audio preferences
+├── MEMORIES.md # Generated via enrichment
+├── FAVORITES.md # Generated via enrichment
+└── PREFERENCES.md # Generated via enrichment
+```
+
+## Enrichment Categories
+
+| Category | Description |
+|----------|-------------|
+| Core Memories | Formative experiences |
+| Favorite Books | Books that shaped thinking |
+| Favorite Movies | Films that resonate |
+| Music Taste | Cognitive infrastructure |
+| Communication | How to give/receive info |
+| Decision Making | Approach to choices |
+| Values | Core principles |
+| Aesthetics | Visual preferences |
+| Daily Routines | Structure habits |
+| Career/Skills | Professional expertise |
+
+### Additional Categories (M33.1)
+
+- **non_negotiables**: Principles and boundaries that define your limits
+- **decision_heuristics**: Mental models and shortcuts for making choices
+- **error_intolerance**: What your digital twin should never do
+
+## Validation & Analysis
+
+1. **Completeness Validator**: Checks for 6 required sections (identity, values, communication, decision making, non-negotiables, error intolerance), shows percentage complete with actionable suggestions
+2. **Contradiction Detector**: AI-powered analysis to find inconsistencies between soul documents, with severity levels and resolution suggestions
+
+## Dynamic Testing
+
+- AI generates behavioral tests based on soul content
+- Targets values, communication style, non-negotiables, and decision patterns
+- Returns structured tests with prompts, expected behaviors, and failure signals
+
+## Writing Sample Analysis
+
+- Paste writing samples to extract authentic voice patterns
+- Analyzes: sentence structure, vocabulary, formality, tone, distinctive markers
+- Generates WRITING_STYLE.md document content
+
+## Context Optimization
+
+- **Document Weighting**: Priority slider (1-10) on each document
+- Higher weighted documents preserved first when context limits force truncation
+
+## API Endpoints
+
+| Route | Description |
+|-------|-------------|
+| GET /api/soul | Status summary |
+| GET /api/soul/documents | List documents |
+| POST /api/soul/documents | Create document |
+| PUT /api/soul/documents/:id | Update document |
+| DELETE /api/soul/documents/:id | Delete document |
+| GET /api/soul/tests | Get test suite |
+| POST /api/soul/tests/run | Run single-model tests |
+| POST /api/soul/tests/run-multi | Run multi-model tests |
+| GET /api/soul/enrich/categories | List enrichment categories |
+| POST /api/soul/enrich/question | Get next question |
+| POST /api/soul/enrich/answer | Submit answer |
+| POST /api/soul/export | Export soul |
+| GET /api/soul/validate/completeness | Check soul completeness |
+| POST /api/soul/validate/contradictions | Detect contradictions |
+| POST /api/soul/tests/generate | Generate dynamic tests |
+| POST /api/soul/analyze-writing | Analyze writing samples |
+
+## Related Features
+
+- [Digital Twin](./digital-twin.md) - Quantitative personality modeling
+- [Chief of Staff](./chief-of-staff.md) - Uses soul context in agent prompts