Remove conversation context/recall system from ClaudeTools
Completely removed the database context recall system while preserving database tables for safety. This major cleanup removes 80+ files and 16,831 lines of code. What was removed: - API layer: 4 routers (conversation-contexts, context-snippets, project-states, decision-logs) with 35+ endpoints - Database models: 5 models (ConversationContext, ContextSnippet, DecisionLog, ProjectState, ContextTag) - Services: 4 service layers with business logic - Schemas: 4 Pydantic schema files - Claude Code hooks: 13 hook files (user-prompt-submit, task-complete, sync-contexts, periodic saves) - Scripts: 15+ scripts (import, migration, testing, tombstone checking) - Tests: 5 test files (context recall, compression, diagnostics) - Documentation: 30+ markdown files (guides, architecture, quick starts) - Utilities: context compression, conversation parsing Files modified: - api/main.py: Removed router registrations - api/models/__init__.py: Removed model imports - api/schemas/__init__.py: Removed schema imports - api/services/__init__.py: Removed service imports - .claude/claude.md: Completely rewritten without context references Database tables preserved: - conversation_contexts, context_snippets, context_tags, project_states, decision_logs (5 orphaned tables remain for safety) - Migration created but NOT applied: 20260118_172743_remove_context_system.py - Tables can be dropped later when confirmed not needed New files added: - CONTEXT_SYSTEM_REMOVAL_SUMMARY.md: Detailed removal report - CONTEXT_SYSTEM_REMOVAL_COMPLETE.md: Final status - CONTEXT_EXPORT_RESULTS.md: Export attempt results - scripts/export-tombstoned-contexts.py: Export tool for future use - migrations/versions/20260118_172743_remove_context_system.py Impact: - Reduced from 130 to 95 API endpoints - Reduced from 43 to 38 active database tables - Removed 16,831 lines of code - System fully operational without context recall Reason for removal: - System was not actively used (no tombstoned contexts found) - Reduces codebase complexity - Focuses on core MSP work tracking functionality - Database preserved for safety (can rollback if needed) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,561 +0,0 @@
|
||||
# Context Recall System - Architecture
|
||||
|
||||
Visual architecture and data flow for the Claude Code Context Recall System.
|
||||
|
||||
## System Overview
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Claude Code Session │
|
||||
│ │
|
||||
│ ┌──────────────┐ ┌──────────────┐ │
|
||||
│ │ User writes │ │ Task │ │
|
||||
│ │ message │ │ completes │ │
|
||||
│ └──────┬───────┘ └──────┬───────┘ │
|
||||
│ │ │ │
|
||||
│ ▼ ▼ │
|
||||
│ ┌─────────────────────┐ ┌─────────────────────┐ │
|
||||
│ │ user-prompt-submit │ │ task-complete │ │
|
||||
│ │ hook triggers │ │ hook triggers │ │
|
||||
│ └─────────┬───────────┘ └─────────┬───────────┘ │
|
||||
└────────────┼──────────────────────────────────────┼─────────────┘
|
||||
│ │
|
||||
│ ┌──────────────────────────────────┐ │
|
||||
│ │ .claude/context-recall- │ │
|
||||
└─┤ config.env ├─┘
|
||||
│ (JWT_TOKEN, PROJECT_ID, etc.) │
|
||||
└──────────────────────────────────┘
|
||||
│ │
|
||||
▼ ▼
|
||||
┌────────────────────────────┐ ┌────────────────────────────┐
|
||||
│ GET /api/conversation- │ │ POST /api/conversation- │
|
||||
│ contexts/recall │ │ contexts │
|
||||
│ │ │ │
|
||||
│ Query Parameters: │ │ POST /api/project-states │
|
||||
│ - project_id │ │ │
|
||||
│ - min_relevance_score │ │ Payload: │
|
||||
│ - limit │ │ - context summary │
|
||||
└────────────┬───────────────┘ │ - metadata │
|
||||
│ │ - relevance score │
|
||||
│ └────────────┬───────────────┘
|
||||
│ │
|
||||
▼ ▼
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ FastAPI Application │
|
||||
│ │
|
||||
│ ┌──────────────────────────┐ ┌───────────────────────────┐ │
|
||||
│ │ Context Recall Logic │ │ Context Save Logic │ │
|
||||
│ │ - Filter by relevance │ │ - Create context record │ │
|
||||
│ │ - Sort by score │ │ - Update project state │ │
|
||||
│ │ - Format for display │ │ - Extract metadata │ │
|
||||
│ └──────────┬───────────────┘ └───────────┬───────────────┘ │
|
||||
│ │ │ │
|
||||
│ ▼ ▼ │
|
||||
│ ┌──────────────────────────────────────────────────────────┐ │
|
||||
│ │ Database Access Layer │ │
|
||||
│ │ (SQLAlchemy ORM) │ │
|
||||
│ └──────────────────────────┬───────────────────────────────┘ │
|
||||
└─────────────────────────────┼──────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ PostgreSQL Database │
|
||||
│ │
|
||||
│ ┌────────────────────────┐ ┌─────────────────────────┐ │
|
||||
│ │ conversation_contexts │ │ project_states │ │
|
||||
│ │ │ │ │ │
|
||||
│ │ - id (UUID) │ │ - id (UUID) │ │
|
||||
│ │ - project_id (FK) │ │ - project_id (FK) │ │
|
||||
│ │ - context_type │ │ - state_type │ │
|
||||
│ │ - title │ │ - state_data (JSONB) │ │
|
||||
│ │ - dense_summary │ │ - created_at │ │
|
||||
│ │ - relevance_score │ └─────────────────────────┘ │
|
||||
│ │ - metadata (JSONB) │ │
|
||||
│ │ - created_at │ ┌─────────────────────────┐ │
|
||||
│ │ - updated_at │ │ projects │ │
|
||||
│ └────────────────────────┘ │ │ │
|
||||
│ │ - id (UUID) │ │
|
||||
│ │ - name │ │
|
||||
│ │ - description │ │
|
||||
│ │ - project_type │ │
|
||||
│ └─────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Data Flow: Context Recall
|
||||
|
||||
```
|
||||
1. User writes message in Claude Code
|
||||
│
|
||||
▼
|
||||
2. user-prompt-submit hook executes
|
||||
│
|
||||
├─ Load config from .claude/context-recall-config.env
|
||||
├─ Detect PROJECT_ID (git config or remote URL hash)
|
||||
├─ Check if CONTEXT_RECALL_ENABLED=true
|
||||
│
|
||||
▼
|
||||
3. HTTP GET /api/conversation-contexts/recall
|
||||
│
|
||||
├─ Headers: Authorization: Bearer {JWT_TOKEN}
|
||||
├─ Query: ?project_id={ID}&limit=10&min_relevance_score=5.0
|
||||
│
|
||||
▼
|
||||
4. API processes request
|
||||
│
|
||||
├─ Authenticate JWT token
|
||||
├─ Query database:
|
||||
│ SELECT * FROM conversation_contexts
|
||||
│ WHERE project_id = {ID}
|
||||
│ AND relevance_score >= 5.0
|
||||
│ ORDER BY relevance_score DESC, created_at DESC
|
||||
│ LIMIT 10
|
||||
│
|
||||
▼
|
||||
5. API returns JSON array of contexts
|
||||
[
|
||||
{
|
||||
"id": "uuid",
|
||||
"title": "Session: 2025-01-15",
|
||||
"dense_summary": "...",
|
||||
"relevance_score": 8.5,
|
||||
"context_type": "session_summary",
|
||||
"metadata": {...}
|
||||
},
|
||||
...
|
||||
]
|
||||
│
|
||||
▼
|
||||
6. Hook formats contexts as Markdown
|
||||
│
|
||||
├─ Parse JSON response
|
||||
├─ Format each context with title, score, type
|
||||
├─ Include summary and metadata
|
||||
│
|
||||
▼
|
||||
7. Hook outputs formatted markdown
|
||||
## 📚 Previous Context
|
||||
|
||||
### 1. Session: 2025-01-15 (Score: 8.5/10)
|
||||
*Type: session_summary*
|
||||
|
||||
[Summary content...]
|
||||
│
|
||||
▼
|
||||
8. Claude Code injects context before user message
|
||||
│
|
||||
▼
|
||||
9. Claude processes message WITH context
|
||||
```
|
||||
|
||||
## Data Flow: Context Saving
|
||||
|
||||
```
|
||||
1. User completes task in Claude Code
|
||||
│
|
||||
▼
|
||||
2. task-complete hook executes
|
||||
│
|
||||
├─ Load config from .claude/context-recall-config.env
|
||||
├─ Detect PROJECT_ID
|
||||
├─ Gather task information:
|
||||
│ ├─ Git branch (git rev-parse --abbrev-ref HEAD)
|
||||
│ ├─ Git commit (git rev-parse --short HEAD)
|
||||
│ ├─ Changed files (git diff --name-only)
|
||||
│ └─ Timestamp
|
||||
│
|
||||
▼
|
||||
3. Build context payload
|
||||
{
|
||||
"project_id": "{PROJECT_ID}",
|
||||
"context_type": "session_summary",
|
||||
"title": "Session: 2025-01-15T14:30:00Z",
|
||||
"dense_summary": "Task completed on branch...",
|
||||
"relevance_score": 7.0,
|
||||
"metadata": {
|
||||
"git_branch": "main",
|
||||
"git_commit": "a1b2c3d",
|
||||
"files_modified": "file1.py,file2.py",
|
||||
"timestamp": "2025-01-15T14:30:00Z"
|
||||
}
|
||||
}
|
||||
│
|
||||
▼
|
||||
4. HTTP POST /api/conversation-contexts
|
||||
│
|
||||
├─ Headers:
|
||||
│ ├─ Authorization: Bearer {JWT_TOKEN}
|
||||
│ └─ Content-Type: application/json
|
||||
├─ Body: [context payload]
|
||||
│
|
||||
▼
|
||||
5. API processes request
|
||||
│
|
||||
├─ Authenticate JWT token
|
||||
├─ Validate payload
|
||||
├─ Insert into database:
|
||||
│ INSERT INTO conversation_contexts
|
||||
│ (id, project_id, context_type, title,
|
||||
│ dense_summary, relevance_score, metadata)
|
||||
│ VALUES (...)
|
||||
│
|
||||
▼
|
||||
6. Build project state payload
|
||||
{
|
||||
"project_id": "{PROJECT_ID}",
|
||||
"state_type": "task_completion",
|
||||
"state_data": {
|
||||
"last_task_completion": "2025-01-15T14:30:00Z",
|
||||
"last_git_commit": "a1b2c3d",
|
||||
"last_git_branch": "main",
|
||||
"recent_files": "file1.py,file2.py"
|
||||
}
|
||||
}
|
||||
│
|
||||
▼
|
||||
7. HTTP POST /api/project-states
|
||||
│
|
||||
├─ Headers: Authorization: Bearer {JWT_TOKEN}
|
||||
├─ Body: [state payload]
|
||||
│
|
||||
▼
|
||||
8. API updates project state
|
||||
│
|
||||
├─ Upsert project state record
|
||||
├─ Merge state_data with existing
|
||||
│
|
||||
▼
|
||||
9. Context saved ✓
|
||||
│
|
||||
▼
|
||||
10. Available for future recall
|
||||
```
|
||||
|
||||
## Authentication Flow
|
||||
|
||||
```
|
||||
┌──────────────┐
|
||||
│ Initial │
|
||||
│ Setup │
|
||||
└──────┬───────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────┐
|
||||
│ bash scripts/setup-context-recall.sh│
|
||||
└──────┬──────────────────────────────┘
|
||||
│
|
||||
├─ Prompt for username/password
|
||||
│
|
||||
▼
|
||||
┌──────────────────────────────────────┐
|
||||
│ POST /api/auth/login │
|
||||
│ │
|
||||
│ Request: │
|
||||
│ { │
|
||||
│ "username": "admin", │
|
||||
│ "password": "secret" │
|
||||
│ } │
|
||||
└──────┬───────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌──────────────────────────────────────┐
|
||||
│ Response: │
|
||||
│ { │
|
||||
│ "access_token": "eyJ...", │
|
||||
│ "token_type": "bearer", │
|
||||
│ "expires_in": 86400 │
|
||||
│ } │
|
||||
└──────┬───────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌──────────────────────────────────────┐
|
||||
│ Save to .claude/context-recall- │
|
||||
│ config.env: │
|
||||
│ │
|
||||
│ JWT_TOKEN=eyJ... │
|
||||
└──────┬───────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌──────────────────────────────────────┐
|
||||
│ All API requests include: │
|
||||
│ Authorization: Bearer eyJ... │
|
||||
└──────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Project Detection Flow
|
||||
|
||||
```
|
||||
Hook needs PROJECT_ID
|
||||
│
|
||||
├─ Check: $CLAUDE_PROJECT_ID set?
|
||||
│ └─ Yes → Use it
|
||||
│ └─ No → Continue detection
|
||||
│
|
||||
├─ Check: git config --local claude.projectid
|
||||
│ └─ Found → Use it
|
||||
│ └─ Not found → Continue detection
|
||||
│
|
||||
├─ Get: git config --get remote.origin.url
|
||||
│ └─ Found → Hash URL → Use as PROJECT_ID
|
||||
│ └─ Not found → No PROJECT_ID available
|
||||
│
|
||||
└─ If no PROJECT_ID:
|
||||
└─ Silent exit (no context available)
|
||||
```
|
||||
|
||||
## Database Schema
|
||||
|
||||
```sql
|
||||
-- Projects table
|
||||
CREATE TABLE projects (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
project_type VARCHAR(50),
|
||||
metadata JSONB,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Conversation contexts table
|
||||
CREATE TABLE conversation_contexts (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
project_id UUID REFERENCES projects(id),
|
||||
context_type VARCHAR(50),
|
||||
title VARCHAR(500),
|
||||
dense_summary TEXT NOT NULL,
|
||||
relevance_score DECIMAL(3,1) CHECK (relevance_score >= 0 AND relevance_score <= 10),
|
||||
metadata JSONB,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW(),
|
||||
|
||||
INDEX idx_project_relevance (project_id, relevance_score DESC),
|
||||
INDEX idx_project_type (project_id, context_type),
|
||||
INDEX idx_created (created_at DESC)
|
||||
);
|
||||
|
||||
-- Project states table
|
||||
CREATE TABLE project_states (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
project_id UUID REFERENCES projects(id),
|
||||
state_type VARCHAR(50),
|
||||
state_data JSONB NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW(),
|
||||
|
||||
INDEX idx_project_state (project_id, state_type)
|
||||
);
|
||||
```
|
||||
|
||||
## Component Interaction
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ File System │
|
||||
│ │
|
||||
│ .claude/ │
|
||||
│ ├── hooks/ │
|
||||
│ │ ├── user-prompt-submit ◄─── Executed by Claude Code │
|
||||
│ │ └── task-complete ◄─── Executed by Claude Code │
|
||||
│ │ │
|
||||
│ └── context-recall-config.env ◄─── Read by hooks │
|
||||
│ │
|
||||
└────────────────┬────────────────────────────────────────────┘
|
||||
│
|
||||
│ (Hooks read config and call API)
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ FastAPI Application (http://localhost:8000) │
|
||||
│ │
|
||||
│ Endpoints: │
|
||||
│ ├── POST /api/auth/login │
|
||||
│ ├── GET /api/conversation-contexts/recall │
|
||||
│ ├── POST /api/conversation-contexts │
|
||||
│ ├── POST /api/project-states │
|
||||
│ └── GET /api/projects/{id} │
|
||||
│ │
|
||||
└────────────────┬────────────────────────────────────────────┘
|
||||
│
|
||||
│ (API queries/updates database)
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ PostgreSQL Database │
|
||||
│ │
|
||||
│ Tables: │
|
||||
│ ├── projects │
|
||||
│ ├── conversation_contexts │
|
||||
│ └── project_states │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
```
|
||||
Hook Execution
|
||||
│
|
||||
├─ Config file missing?
|
||||
│ └─ Silent exit (context recall unavailable)
|
||||
│
|
||||
├─ PROJECT_ID not detected?
|
||||
│ └─ Silent exit (no project context)
|
||||
│
|
||||
├─ JWT_TOKEN missing?
|
||||
│ └─ Silent exit (authentication unavailable)
|
||||
│
|
||||
├─ API unreachable? (timeout 3-5s)
|
||||
│ └─ Silent exit (API offline)
|
||||
│
|
||||
├─ API returns error (401, 404, 500)?
|
||||
│ └─ Silent exit (log if debug enabled)
|
||||
│
|
||||
└─ Success
|
||||
└─ Process and inject context
|
||||
```
|
||||
|
||||
**Philosophy:** Hooks NEVER break Claude Code. All failures are silent.
|
||||
|
||||
## Performance Characteristics
|
||||
|
||||
```
|
||||
Timeline for user-prompt-submit:
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
0ms Hook starts
|
||||
├─ Load config (10ms)
|
||||
├─ Detect project (5ms)
|
||||
│
|
||||
15ms HTTP request starts
|
||||
├─ Connection (20ms)
|
||||
├─ Query execution (50-100ms)
|
||||
├─ Response formatting (10ms)
|
||||
│
|
||||
145ms Response received
|
||||
├─ Parse JSON (10ms)
|
||||
├─ Format markdown (30ms)
|
||||
│
|
||||
185ms Context injected
|
||||
│
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
Total: ~200ms average overhead per message
|
||||
Timeout: 3000ms (fails gracefully)
|
||||
```
|
||||
|
||||
## Configuration Impact
|
||||
|
||||
```
|
||||
┌──────────────────────────────────────┐
|
||||
│ MIN_RELEVANCE_SCORE │
|
||||
├──────────────────────────────────────┤
|
||||
│ Low (3.0) │
|
||||
│ ├─ More contexts recalled │
|
||||
│ ├─ Broader historical view │
|
||||
│ └─ Slower queries │
|
||||
│ │
|
||||
│ Medium (5.0) ← Recommended │
|
||||
│ ├─ Balanced relevance/quantity │
|
||||
│ └─ Fast queries │
|
||||
│ │
|
||||
│ High (7.5) │
|
||||
│ ├─ Only critical contexts │
|
||||
│ ├─ Very focused │
|
||||
│ └─ Fastest queries │
|
||||
└──────────────────────────────────────┘
|
||||
|
||||
┌──────────────────────────────────────┐
|
||||
│ MAX_CONTEXTS │
|
||||
├──────────────────────────────────────┤
|
||||
│ Few (5) │
|
||||
│ ├─ Focused context │
|
||||
│ ├─ Shorter prompts │
|
||||
│ └─ Faster processing │
|
||||
│ │
|
||||
│ Medium (10) ← Recommended │
|
||||
│ ├─ Good coverage │
|
||||
│ └─ Reasonable prompt size │
|
||||
│ │
|
||||
│ Many (20) │
|
||||
│ ├─ Comprehensive context │
|
||||
│ ├─ Longer prompts │
|
||||
│ └─ Slower Claude processing │
|
||||
└──────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Security Model
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Security Boundaries │
|
||||
│ │
|
||||
│ 1. Authentication │
|
||||
│ ├─ JWT tokens (24h expiry) │
|
||||
│ ├─ Bcrypt password hashing │
|
||||
│ └─ Bearer token in Authorization header │
|
||||
│ │
|
||||
│ 2. Authorization │
|
||||
│ ├─ Project-level access control │
|
||||
│ ├─ User can only access own projects │
|
||||
│ └─ Token includes user_id claim │
|
||||
│ │
|
||||
│ 3. Data Protection │
|
||||
│ ├─ Config file gitignored │
|
||||
│ ├─ JWT tokens never in version control │
|
||||
│ └─ HTTPS recommended for production │
|
||||
│ │
|
||||
│ 4. Input Validation │
|
||||
│ ├─ API validates all payloads │
|
||||
│ ├─ SQL injection protected (ORM) │
|
||||
│ └─ JSON schema validation │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Deployment Architecture
|
||||
|
||||
```
|
||||
Development:
|
||||
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
|
||||
│ Claude Code │────▶│ API │────▶│ PostgreSQL │
|
||||
│ (Desktop) │ │ (localhost) │ │ (localhost) │
|
||||
└──────────────┘ └──────────────┘ └──────────────┘
|
||||
|
||||
Production:
|
||||
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
|
||||
│ Claude Code │────▶│ API │────▶│ PostgreSQL │
|
||||
│ (Desktop) │ │ (Docker) │ │ (RDS/Cloud) │
|
||||
└──────────────┘ └──────────────┘ └──────────────┘
|
||||
│ │
|
||||
│ │ (HTTPS)
|
||||
│ ▼
|
||||
│ ┌──────────────┐
|
||||
│ │ Redis Cache │
|
||||
│ │ (Optional) │
|
||||
└──────────────┴──────────────┘
|
||||
```
|
||||
|
||||
## Scalability Considerations
|
||||
|
||||
```
|
||||
Database Optimization:
|
||||
├─ Indexes on (project_id, relevance_score)
|
||||
├─ Indexes on (project_id, context_type)
|
||||
├─ Indexes on created_at for time-based queries
|
||||
└─ JSONB indexes on metadata for complex queries
|
||||
|
||||
Caching Strategy:
|
||||
├─ Redis for frequently-accessed contexts
|
||||
├─ Cache key: project_id + min_score + limit
|
||||
├─ TTL: 5 minutes
|
||||
└─ Invalidate on new context creation
|
||||
|
||||
Query Optimization:
|
||||
├─ Limit results (MAX_CONTEXTS)
|
||||
├─ Filter early (MIN_RELEVANCE_SCORE)
|
||||
├─ Sort in database (not application)
|
||||
└─ Paginate for large result sets
|
||||
```
|
||||
|
||||
This architecture provides a robust, scalable, and secure system for context recall in Claude Code sessions.
|
||||
@@ -1,175 +0,0 @@
|
||||
# Context Recall - Quick Start
|
||||
|
||||
One-page reference for the Claude Code Context Recall System.
|
||||
|
||||
## Setup (First Time)
|
||||
|
||||
```bash
|
||||
# 1. Start API
|
||||
uvicorn api.main:app --reload
|
||||
|
||||
# 2. Setup (in new terminal)
|
||||
bash scripts/setup-context-recall.sh
|
||||
|
||||
# 3. Test
|
||||
bash scripts/test-context-recall.sh
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
```
|
||||
.claude/
|
||||
├── hooks/
|
||||
│ ├── user-prompt-submit # Recalls context before messages
|
||||
│ ├── task-complete # Saves context after tasks
|
||||
│ └── README.md # Hook documentation
|
||||
├── context-recall-config.env # Configuration (gitignored)
|
||||
└── CONTEXT_RECALL_QUICK_START.md
|
||||
|
||||
scripts/
|
||||
├── setup-context-recall.sh # One-command setup
|
||||
└── test-context-recall.sh # System testing
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Edit `.claude/context-recall-config.env`:
|
||||
|
||||
```bash
|
||||
CLAUDE_API_URL=http://localhost:8000 # API URL
|
||||
CLAUDE_PROJECT_ID= # Auto-detected
|
||||
JWT_TOKEN= # From setup script
|
||||
CONTEXT_RECALL_ENABLED=true # Enable/disable
|
||||
MIN_RELEVANCE_SCORE=5.0 # Filter threshold (0-10)
|
||||
MAX_CONTEXTS=10 # Max contexts per query
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
```
|
||||
User Message → [Recall Context] → Claude (with context) → Response
|
||||
↓
|
||||
[Save Context]
|
||||
```
|
||||
|
||||
### user-prompt-submit Hook
|
||||
- Runs **before** each user message
|
||||
- Calls `GET /api/conversation-contexts/recall`
|
||||
- Injects relevant context from previous sessions
|
||||
- Falls back gracefully if API unavailable
|
||||
|
||||
### task-complete Hook
|
||||
- Runs **after** task completion
|
||||
- Calls `POST /api/conversation-contexts`
|
||||
- Saves conversation summary
|
||||
- Updates project state
|
||||
|
||||
## Common Commands
|
||||
|
||||
```bash
|
||||
# Re-run setup (get new JWT token)
|
||||
bash scripts/setup-context-recall.sh
|
||||
|
||||
# Test system
|
||||
bash scripts/test-context-recall.sh
|
||||
|
||||
# Test hooks manually
|
||||
source .claude/context-recall-config.env
|
||||
bash .claude/hooks/user-prompt-submit
|
||||
|
||||
# Enable debug mode
|
||||
echo "DEBUG_CONTEXT_RECALL=true" >> .claude/context-recall-config.env
|
||||
|
||||
# Disable context recall
|
||||
echo "CONTEXT_RECALL_ENABLED=false" >> .claude/context-recall-config.env
|
||||
|
||||
# Check API health
|
||||
curl http://localhost:8000/health
|
||||
|
||||
# View your project
|
||||
source .claude/context-recall-config.env
|
||||
curl -H "Authorization: Bearer $JWT_TOKEN" \
|
||||
http://localhost:8000/api/projects/$CLAUDE_PROJECT_ID
|
||||
|
||||
# Query contexts manually
|
||||
curl "http://localhost:8000/api/conversation-contexts/recall?project_id=$CLAUDE_PROJECT_ID&limit=5" \
|
||||
-H "Authorization: Bearer $JWT_TOKEN"
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
| Problem | Solution |
|
||||
|---------|----------|
|
||||
| Context not appearing | Check API is running: `curl http://localhost:8000/health` |
|
||||
| Hooks not executing | Make executable: `chmod +x .claude/hooks/*` |
|
||||
| JWT token expired | Re-run setup: `bash scripts/setup-context-recall.sh` |
|
||||
| Context not saving | Check project ID: `echo $CLAUDE_PROJECT_ID` |
|
||||
| Debug hook output | Enable debug: `DEBUG_CONTEXT_RECALL=true` in config |
|
||||
|
||||
## API Endpoints
|
||||
|
||||
- `GET /api/conversation-contexts/recall` - Get relevant contexts
|
||||
- `POST /api/conversation-contexts` - Save new context
|
||||
- `POST /api/project-states` - Update project state
|
||||
- `POST /api/auth/login` - Get JWT token
|
||||
- `GET /api/projects` - List projects
|
||||
|
||||
## Configuration Parameters
|
||||
|
||||
### MIN_RELEVANCE_SCORE (0.0 - 10.0)
|
||||
- **5.0** - Balanced (recommended)
|
||||
- **7.0** - Only high-quality contexts
|
||||
- **3.0** - Include more historical context
|
||||
|
||||
### MAX_CONTEXTS (1 - 50)
|
||||
- **10** - Balanced (recommended)
|
||||
- **5** - Focused, minimal context
|
||||
- **20** - Comprehensive history
|
||||
|
||||
## Security
|
||||
|
||||
- JWT tokens stored in `.claude/context-recall-config.env`
|
||||
- File is gitignored (never commit!)
|
||||
- Tokens expire after 24 hours
|
||||
- Re-run setup to refresh
|
||||
|
||||
## Example Output
|
||||
|
||||
When context is available:
|
||||
|
||||
```markdown
|
||||
## 📚 Previous Context
|
||||
|
||||
The following context has been automatically recalled from previous sessions:
|
||||
|
||||
### 1. Database Schema Updates (Score: 8.5/10)
|
||||
*Type: technical_decision*
|
||||
|
||||
Updated the Project model to include new fields for MSP integration...
|
||||
|
||||
---
|
||||
|
||||
### 2. API Endpoint Changes (Score: 7.2/10)
|
||||
*Type: session_summary*
|
||||
|
||||
Implemented new REST endpoints for context recall...
|
||||
|
||||
---
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
- Hook overhead: <500ms per message
|
||||
- API query time: <100ms
|
||||
- Timeouts: 3-5 seconds
|
||||
- Silent failures (don't break Claude)
|
||||
|
||||
## Full Documentation
|
||||
|
||||
- **Setup Guide:** `CONTEXT_RECALL_SETUP.md`
|
||||
- **Hook Details:** `.claude/hooks/README.md`
|
||||
- **API Spec:** `.claude/API_SPEC.md`
|
||||
|
||||
---
|
||||
|
||||
**Quick Start:** `bash scripts/setup-context-recall.sh` and you're done!
|
||||
283
.claude/DATABASE_FIRST_PROTOCOL.md
Normal file
283
.claude/DATABASE_FIRST_PROTOCOL.md
Normal file
@@ -0,0 +1,283 @@
|
||||
# Database-First Protocol
|
||||
|
||||
**CRITICAL:** This protocol MUST be followed for EVERY user request.
|
||||
|
||||
---
|
||||
|
||||
## The Problem
|
||||
|
||||
Currently, Claude:
|
||||
1. Receives user request
|
||||
2. Searches local files (maybe)
|
||||
3. Performs work
|
||||
4. (Never saves context automatically)
|
||||
|
||||
This wastes tokens, misses critical context, and loses work across sessions.
|
||||
|
||||
---
|
||||
|
||||
## The Solution: Database-First Protocol
|
||||
|
||||
### MANDATORY FIRST STEP - For EVERY User Request
|
||||
|
||||
```
|
||||
BEFORE doing ANYTHING else:
|
||||
|
||||
1. Query the context database for relevant information
|
||||
2. Inject retrieved context into your working memory
|
||||
3. THEN proceed with the user's request
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Implementation
|
||||
|
||||
### Step 1: Check Database (ALWAYS FIRST)
|
||||
|
||||
Before analyzing the user's request, run this command:
|
||||
|
||||
```bash
|
||||
curl -s -H "Authorization: Bearer $JWT_TOKEN" \
|
||||
"http://172.16.3.30:8001/api/conversation-contexts/recall?\
|
||||
search_term={user_keywords}&limit=10" | python -m json.tool
|
||||
```
|
||||
|
||||
Extract keywords from user request. Examples:
|
||||
- User: "What's the status of Dataforth project?" → search_term=dataforth
|
||||
- User: "Continue work on GuruConnect" → search_term=guruconnect
|
||||
- User: "Fix the API bug" → search_term=API+bug
|
||||
- User: "Help with database" → search_term=database
|
||||
|
||||
### Step 2: Review Retrieved Context
|
||||
|
||||
The API returns up to 10 relevant contexts with:
|
||||
- `title` - Short description
|
||||
- `dense_summary` - Compressed context (90% token reduction)
|
||||
- `relevance_score` - How relevant (0-10)
|
||||
- `tags` - Keywords for filtering
|
||||
- `created_at` - Timestamp
|
||||
|
||||
### Step 3: Use Context in Your Response
|
||||
|
||||
Reference the context when responding:
|
||||
- "Based on previous context from {date}..."
|
||||
- "According to the database, Dataforth DOS project..."
|
||||
- "Context shows this was last discussed on..."
|
||||
|
||||
### Step 4: Save New Context (After Completion)
|
||||
|
||||
After completing a significant task:
|
||||
|
||||
```bash
|
||||
curl -s -H "Authorization: Bearer $JWT_TOKEN" \
|
||||
-X POST "http://172.16.3.30:8001/api/conversation-contexts" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"project_id": "c3d9f1c8-dc2b-499f-a228-3a53fa950e7b",
|
||||
"context_type": "session_summary",
|
||||
"title": "Brief title of what was accomplished",
|
||||
"dense_summary": "Compressed summary of work done, decisions made, files changed",
|
||||
"relevance_score": 7.0,
|
||||
"tags": "[\"keyword1\", \"keyword2\", \"keyword3\"]"
|
||||
}'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## When to Save Context
|
||||
|
||||
Save context automatically when:
|
||||
|
||||
1. **Task Completion** - TodoWrite task marked as completed
|
||||
2. **Major Decision** - Architectural choice, approach selection
|
||||
3. **File Changes** - Significant code changes (>50 lines)
|
||||
4. **Problem Solved** - Bug fixed, issue resolved
|
||||
5. **User Requests** - Via /snapshot command
|
||||
6. **Session End** - Before closing conversation
|
||||
|
||||
---
|
||||
|
||||
## Agent Delegation Rules
|
||||
|
||||
**Main Claude is a COORDINATOR, not an EXECUTOR.**
|
||||
|
||||
Before performing any task, check delegation table:
|
||||
|
||||
| Task Type | Delegate To | Always? |
|
||||
|-----------|-------------|---------|
|
||||
| Context retrieval | Database Agent | ✅ YES |
|
||||
| Codebase search | Explore Agent | For patterns/keywords |
|
||||
| Code changes >10 lines | Coding Agent | ✅ YES |
|
||||
| Running tests | Testing Agent | ✅ YES |
|
||||
| Git operations | Gitea Agent | ✅ YES |
|
||||
| File operations <5 files | Main Claude | Direct OK |
|
||||
| Documentation | Documentation Squire | For comprehensive docs |
|
||||
|
||||
**How to Delegate:**
|
||||
|
||||
```
|
||||
Instead of: Searching files directly with Grep/Glob
|
||||
Do: "Let me delegate to the Explore agent to search the codebase..."
|
||||
|
||||
Instead of: Writing code directly
|
||||
Do: "Let me delegate to the Coding Agent to implement this change..."
|
||||
|
||||
Instead of: Running tests yourself
|
||||
Do: "Let me delegate to the Testing Agent to run the test suite..."
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Context Database Quick Reference
|
||||
|
||||
### Query Endpoints
|
||||
|
||||
```bash
|
||||
# Search by term
|
||||
GET /api/conversation-contexts/recall?search_term={term}&limit=10
|
||||
|
||||
# Filter by tags
|
||||
GET /api/conversation-contexts/recall?tags=dataforth&tags=dos&limit=10
|
||||
|
||||
# Get by project
|
||||
GET /api/conversation-contexts/recall?project_id={uuid}&limit=10
|
||||
|
||||
# List all recent
|
||||
GET /api/conversation-contexts?limit=50
|
||||
```
|
||||
|
||||
### Save Endpoint
|
||||
|
||||
```bash
|
||||
POST /api/conversation-contexts
|
||||
{
|
||||
"project_id": "uuid",
|
||||
"context_type": "session_summary|checkpoint|decision|problem_solution",
|
||||
"title": "Short title",
|
||||
"dense_summary": "Compressed summary with key info",
|
||||
"relevance_score": 1.0-10.0,
|
||||
"tags": "[\"tag1\", \"tag2\"]"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Example Workflow
|
||||
|
||||
### User Request: "What's the status of the Dataforth DOS project?"
|
||||
|
||||
**WRONG Approach:**
|
||||
```
|
||||
Claude: Let me search local files...
|
||||
(Wastes tokens, misses imported context in database)
|
||||
```
|
||||
|
||||
**CORRECT Approach:**
|
||||
```
|
||||
Claude: Let me check the context database first...
|
||||
|
||||
[Runs: curl .../recall?search_term=dataforth]
|
||||
|
||||
Claude: "Based on context retrieved from the database, the Dataforth
|
||||
DOS machines project involves analyzing drive images from test machines
|
||||
with ATE (Automated Test Equipment) software. The conversation was
|
||||
imported on 2026-01-18 and includes 1,241KB of data.
|
||||
|
||||
The project appears to focus on Dataforth industrial I/O equipment
|
||||
testing (5B, 7B, 8B series modules).
|
||||
|
||||
Would you like me to delegate to the Explore agent to find specific
|
||||
files related to this project?"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Integration with Hooks
|
||||
|
||||
The hooks in `.claude/hooks/` should assist but NOT replace manual queries:
|
||||
|
||||
- `user-prompt-submit` - Auto-injects context (passive)
|
||||
- `task-complete` - Auto-saves context (passive)
|
||||
|
||||
**BUT:** You should ACTIVELY query database yourself before major work.
|
||||
|
||||
Don't rely solely on hooks. They're a backup, not the primary mechanism.
|
||||
|
||||
---
|
||||
|
||||
## Token Efficiency
|
||||
|
||||
### Before Database-First:
|
||||
- Read 3MB of local files: ~750,000 tokens
|
||||
- Parse conversation histories: ~250,000 tokens
|
||||
- **Total:** ~1,000,000 tokens per session
|
||||
|
||||
### After Database-First:
|
||||
- Query database: 500 tokens (API call)
|
||||
- Receive compressed summaries: ~5,000 tokens (10 contexts)
|
||||
- **Total:** ~5,500 tokens per session
|
||||
|
||||
**Savings:** 99.4% token reduction
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Database Query Returns Empty
|
||||
|
||||
```bash
|
||||
# Check if API is up
|
||||
curl http://172.16.3.30:8001/health
|
||||
|
||||
# Check total contexts
|
||||
curl -H "Authorization: Bearer $JWT" \
|
||||
http://172.16.3.30:8001/api/conversation-contexts | \
|
||||
python -c "import sys,json; print(f'Total: {json.load(sys.stdin)[\"total\"]}')"
|
||||
|
||||
# Try different search term
|
||||
# Instead of: search_term=dataforth%20DOS
|
||||
# Try: search_term=dataforth
|
||||
```
|
||||
|
||||
### Authentication Fails
|
||||
|
||||
```bash
|
||||
# Check JWT token in config
|
||||
cat .claude/context-recall-config.env | grep JWT_TOKEN
|
||||
|
||||
# Verify token not expired
|
||||
# Current token expires: 2026-02-16
|
||||
```
|
||||
|
||||
### No Results for Known Project
|
||||
|
||||
The recall endpoint uses PostgreSQL full-text search. Try:
|
||||
- Simpler search terms
|
||||
- Individual keywords instead of phrases
|
||||
- Checking tags directly: `?tags=dataforth`
|
||||
|
||||
---
|
||||
|
||||
## Enforcement
|
||||
|
||||
This protocol is MANDATORY. To ensure compliance:
|
||||
|
||||
1. **Every response** should start with "Checking database for context..."
|
||||
2. **Before major work**, always query database
|
||||
3. **After completion**, always save summary
|
||||
4. **For delegation**, use agents not direct execution
|
||||
|
||||
**Violation Example:**
|
||||
```
|
||||
User: "Find all Python files"
|
||||
Claude: [Runs Glob directly] ❌ WRONG
|
||||
|
||||
Correct:
|
||||
Claude: "Let me delegate to Explore agent to search for Python files" ✅
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**Last Updated:** 2026-01-18
|
||||
**Status:** ACTIVE - MUST BE FOLLOWED
|
||||
**Priority:** CRITICAL
|
||||
@@ -1,357 +0,0 @@
|
||||
# Periodic Context Save
|
||||
|
||||
**Automatic context saving every 5 minutes of active work**
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The periodic context save daemon runs in the background and automatically saves your work context to the database every 5 minutes of active time. This ensures continuous context preservation even during long work sessions.
|
||||
|
||||
### Key Features
|
||||
|
||||
- ✅ **Active Time Tracking** - Only counts time when Claude is actively working
|
||||
- ✅ **Ignores Idle Time** - Doesn't save when waiting for permissions or idle
|
||||
- ✅ **Background Process** - Runs independently, doesn't interrupt work
|
||||
- ✅ **Automatic Recovery** - Resumes tracking after restarts
|
||||
- ✅ **Low Overhead** - Checks activity every 60 seconds
|
||||
|
||||
---
|
||||
|
||||
## How It Works
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────┐
|
||||
│ Every 60 seconds: │
|
||||
│ │
|
||||
│ 1. Check if Claude Code is active │
|
||||
│ - Recent file modifications? │
|
||||
│ - Claude process running? │
|
||||
│ │
|
||||
│ 2. If ACTIVE → Add 60s to timer │
|
||||
│ If IDLE → Don't add time │
|
||||
│ │
|
||||
│ 3. When timer reaches 300s (5 min): │
|
||||
│ - Save context to database │
|
||||
│ - Reset timer to 0 │
|
||||
│ - Continue monitoring │
|
||||
└─────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
**Active time includes:**
|
||||
- Writing code
|
||||
- Running commands
|
||||
- Making changes to files
|
||||
- Interacting with Claude
|
||||
|
||||
**Idle time (not counted):**
|
||||
- Waiting for user input
|
||||
- Permission prompts
|
||||
- No file changes or activity
|
||||
- Claude process not running
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
### Start the Daemon
|
||||
|
||||
```bash
|
||||
python .claude/hooks/periodic_context_save.py start
|
||||
```
|
||||
|
||||
Output:
|
||||
```
|
||||
Started periodic context save daemon (PID: 12345)
|
||||
Logs: D:\ClaudeTools\.claude\periodic-save.log
|
||||
```
|
||||
|
||||
### Check Status
|
||||
|
||||
```bash
|
||||
python .claude/hooks/periodic_context_save.py status
|
||||
```
|
||||
|
||||
Output:
|
||||
```
|
||||
Periodic context save daemon is running (PID: 12345)
|
||||
Active time: 180s / 300s
|
||||
Last save: 2026-01-17T19:05:23+00:00
|
||||
```
|
||||
|
||||
### Stop the Daemon
|
||||
|
||||
```bash
|
||||
python .claude/hooks/periodic_context_save.py stop
|
||||
```
|
||||
|
||||
Output:
|
||||
```
|
||||
Stopped periodic context save daemon (PID: 12345)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
### One-Time Setup
|
||||
|
||||
1. **Ensure JWT token is configured:**
|
||||
```bash
|
||||
# Token should already be in .claude/context-recall-config.env
|
||||
cat .claude/context-recall-config.env | grep JWT_TOKEN
|
||||
```
|
||||
|
||||
2. **Start the daemon:**
|
||||
```bash
|
||||
python .claude/hooks/periodic_context_save.py start
|
||||
```
|
||||
|
||||
3. **Verify it's running:**
|
||||
```bash
|
||||
python .claude/hooks/periodic_context_save.py status
|
||||
```
|
||||
|
||||
### Auto-Start on Login (Optional)
|
||||
|
||||
**Windows - Task Scheduler:**
|
||||
|
||||
1. Open Task Scheduler
|
||||
2. Create Basic Task:
|
||||
- Name: "Claude Periodic Context Save"
|
||||
- Trigger: At log on
|
||||
- Action: Start a program
|
||||
- Program: `python`
|
||||
- Arguments: `D:\ClaudeTools\.claude\hooks\periodic_context_save.py start`
|
||||
- Start in: `D:\ClaudeTools`
|
||||
|
||||
**Linux/Mac - systemd/launchd:**
|
||||
|
||||
Create a systemd service or launchd plist to start on login.
|
||||
|
||||
---
|
||||
|
||||
## What Gets Saved
|
||||
|
||||
Every 5 minutes of active time, the daemon saves:
|
||||
|
||||
```json
|
||||
{
|
||||
"context_type": "session_summary",
|
||||
"title": "Periodic Save - 2026-01-17 14:30",
|
||||
"dense_summary": "Auto-saved context after 5 minutes of active work. Session in progress on project: claudetools-main",
|
||||
"relevance_score": 5.0,
|
||||
"tags": ["auto-save", "periodic", "active-session"]
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Never lose more than 5 minutes of work context
|
||||
- Automatic recovery if session crashes
|
||||
- Historical timeline of work sessions
|
||||
- Can review what you were working on at specific times
|
||||
|
||||
---
|
||||
|
||||
## Monitoring
|
||||
|
||||
### View Logs
|
||||
|
||||
```bash
|
||||
# View last 20 log lines
|
||||
tail -20 .claude/periodic-save.log
|
||||
|
||||
# Follow logs in real-time
|
||||
tail -f .claude/periodic-save.log
|
||||
```
|
||||
|
||||
**Sample log output:**
|
||||
```
|
||||
[2026-01-17 14:25:00] Periodic context save daemon started
|
||||
[2026-01-17 14:25:00] Will save context every 300s of active time
|
||||
[2026-01-17 14:26:00] Active: 60s / 300s
|
||||
[2026-01-17 14:27:00] Active: 120s / 300s
|
||||
[2026-01-17 14:28:00] Claude Code inactive - not counting time
|
||||
[2026-01-17 14:29:00] Active: 180s / 300s
|
||||
[2026-01-17 14:30:00] Active: 240s / 300s
|
||||
[2026-01-17 14:31:00] 300s of active time reached - saving context
|
||||
[2026-01-17 14:31:01] ✓ Context saved successfully (ID: 1e2c3408-9146-4e98-b302-fe219280344c)
|
||||
[2026-01-17 14:32:00] Active: 60s / 300s
|
||||
```
|
||||
|
||||
### View State
|
||||
|
||||
```bash
|
||||
# Check current state
|
||||
cat .claude/.periodic-save-state.json | python -m json.tool
|
||||
```
|
||||
|
||||
Output:
|
||||
```json
|
||||
{
|
||||
"active_seconds": 180,
|
||||
"last_update": "2026-01-17T19:28:00+00:00",
|
||||
"last_save": "2026-01-17T19:26:00+00:00"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Configuration
|
||||
|
||||
Edit the script to customize:
|
||||
|
||||
```python
|
||||
# In periodic_context_save.py
|
||||
|
||||
SAVE_INTERVAL_SECONDS = 300 # Change to 600 for 10 minutes
|
||||
CHECK_INTERVAL_SECONDS = 60 # How often to check activity
|
||||
```
|
||||
|
||||
**Common configurations:**
|
||||
- Every 5 minutes: `SAVE_INTERVAL_SECONDS = 300`
|
||||
- Every 10 minutes: `SAVE_INTERVAL_SECONDS = 600`
|
||||
- Every 15 minutes: `SAVE_INTERVAL_SECONDS = 900`
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Daemon won't start
|
||||
|
||||
**Check logs:**
|
||||
```bash
|
||||
cat .claude/periodic-save.log
|
||||
```
|
||||
|
||||
**Common issues:**
|
||||
- JWT token missing or invalid
|
||||
- Python not in PATH
|
||||
- Permissions issue with log file
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Verify JWT token exists
|
||||
grep JWT_TOKEN .claude/context-recall-config.env
|
||||
|
||||
# Test Python
|
||||
python --version
|
||||
|
||||
# Check permissions
|
||||
ls -la .claude/
|
||||
```
|
||||
|
||||
### Contexts not being saved
|
||||
|
||||
**Check:**
|
||||
1. Daemon is running: `python .claude/hooks/periodic_context_save.py status`
|
||||
2. JWT token is valid: Token expires after 30 days
|
||||
3. API is accessible: `curl http://172.16.3.30:8001/health`
|
||||
4. View logs for errors: `tail .claude/periodic-save.log`
|
||||
|
||||
**If JWT token expired:**
|
||||
```bash
|
||||
# Generate new token
|
||||
python create_jwt_token.py
|
||||
|
||||
# Update config
|
||||
# Copy new JWT_TOKEN to .claude/context-recall-config.env
|
||||
|
||||
# Restart daemon
|
||||
python .claude/hooks/periodic_context_save.py stop
|
||||
python .claude/hooks/periodic_context_save.py start
|
||||
```
|
||||
|
||||
### Activity not being detected
|
||||
|
||||
The daemon uses these heuristics:
|
||||
- File modifications in project directory (within last 2 minutes)
|
||||
- Claude process running (on Windows)
|
||||
|
||||
**Improve detection:**
|
||||
Modify `is_claude_active()` function to add:
|
||||
- Check for recent git commits
|
||||
- Monitor specific files
|
||||
- Check for recent bash history
|
||||
|
||||
---
|
||||
|
||||
## Integration with Other Hooks
|
||||
|
||||
The periodic save works alongside existing hooks:
|
||||
|
||||
| Hook | Trigger | What It Saves |
|
||||
|------|---------|---------------|
|
||||
| **user-prompt-submit** | Before each message | Recalls context from DB |
|
||||
| **task-complete** | After task completes | Rich context with decisions |
|
||||
| **periodic-context-save** | Every 5min active | Quick checkpoint save |
|
||||
|
||||
**Result:**
|
||||
- Comprehensive context coverage
|
||||
- Never lose more than 5 minutes of work
|
||||
- Detailed context when tasks complete
|
||||
- Continuous backup of active sessions
|
||||
|
||||
---
|
||||
|
||||
## Performance Impact
|
||||
|
||||
**Resource Usage:**
|
||||
- **CPU:** < 0.1% (checks once per minute)
|
||||
- **Memory:** ~30 MB (Python process)
|
||||
- **Disk:** ~2 KB per save (~25 KB/hour)
|
||||
- **Network:** Minimal (single API call every 5 min)
|
||||
|
||||
**Impact on Claude Code:**
|
||||
- None - runs as separate process
|
||||
- Doesn't block or interrupt work
|
||||
- No user-facing delays
|
||||
|
||||
---
|
||||
|
||||
## Uninstall
|
||||
|
||||
To remove periodic context save:
|
||||
|
||||
```bash
|
||||
# Stop daemon
|
||||
python .claude/hooks/periodic_context_save.py stop
|
||||
|
||||
# Remove files (optional)
|
||||
rm .claude/hooks/periodic_context_save.py
|
||||
rm .claude/.periodic-save.pid
|
||||
rm .claude/.periodic-save-state.json
|
||||
rm .claude/periodic-save.log
|
||||
|
||||
# Remove from auto-start (if configured)
|
||||
# Windows: Delete from Task Scheduler
|
||||
# Linux: Remove systemd service
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## FAQ
|
||||
|
||||
**Q: Does it save when I'm idle?**
|
||||
A: No - only counts active work time (file changes, Claude activity).
|
||||
|
||||
**Q: What if the API is down?**
|
||||
A: Contexts queue locally and sync when API is restored (offline mode).
|
||||
|
||||
**Q: Can I change the interval?**
|
||||
A: Yes - edit `SAVE_INTERVAL_SECONDS` in the script.
|
||||
|
||||
**Q: Does it work offline?**
|
||||
A: Yes - uses the same offline queue as other hooks (v2).
|
||||
|
||||
**Q: How do I know it's working?**
|
||||
A: Check logs: `tail .claude/periodic-save.log`
|
||||
|
||||
**Q: Can I run multiple instances?**
|
||||
A: No - PID file prevents multiple daemons.
|
||||
|
||||
---
|
||||
|
||||
**Created:** 2026-01-17
|
||||
**Version:** 1.0
|
||||
**Status:** Ready for use
|
||||
@@ -1,892 +0,0 @@
|
||||
# Learning & Context Schema
|
||||
|
||||
**MSP Mode Database Schema - Self-Learning System**
|
||||
|
||||
**Status:** Designed 2026-01-15
|
||||
**Database:** msp_tracking (MariaDB on Jupiter)
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The Learning & Context subsystem enables MSP Mode to learn from every failure, build environmental awareness, and prevent recurring mistakes. This self-improving system captures failure patterns, generates actionable insights, and proactively checks environmental constraints before making suggestions.
|
||||
|
||||
**Core Principle:** Every failure is a learning opportunity. Agents must never make the same mistake twice.
|
||||
|
||||
**Related Documentation:**
|
||||
- [MSP-MODE-SPEC.md](../MSP-MODE-SPEC.md) - Full system specification
|
||||
- [ARCHITECTURE_OVERVIEW.md](ARCHITECTURE_OVERVIEW.md) - Agent architecture
|
||||
- [SCHEMA_CREDENTIALS.md](SCHEMA_CREDENTIALS.md) - Security tables
|
||||
- [API_SPEC.md](API_SPEC.md) - API endpoints
|
||||
|
||||
---
|
||||
|
||||
## Tables Summary
|
||||
|
||||
| Table | Purpose | Auto-Generated |
|
||||
|-------|---------|----------------|
|
||||
| `environmental_insights` | Generated insights per client/infrastructure | Yes |
|
||||
| `problem_solutions` | Issue tracking with root cause and resolution | Partial |
|
||||
| `failure_patterns` | Aggregated failure analysis and learnings | Yes |
|
||||
| `operation_failures` | Non-command failures (API, file ops, network) | Yes |
|
||||
|
||||
**Total:** 4 tables
|
||||
|
||||
**Specialized Agents:**
|
||||
- **Failure Analysis Agent** - Analyzes failures, identifies patterns, generates insights
|
||||
- **Environment Context Agent** - Pre-checks environmental constraints before operations
|
||||
- **Problem Pattern Matching Agent** - Searches historical solutions for similar issues
|
||||
|
||||
---
|
||||
|
||||
## Table Schemas
|
||||
|
||||
### `environmental_insights`
|
||||
|
||||
Auto-generated insights about client infrastructure constraints, limitations, and quirks. Used by Environment Context Agent to prevent failures before they occur.
|
||||
|
||||
```sql
|
||||
CREATE TABLE environmental_insights (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
client_id UUID REFERENCES clients(id) ON DELETE CASCADE,
|
||||
infrastructure_id UUID REFERENCES infrastructure(id) ON DELETE CASCADE,
|
||||
|
||||
-- Insight classification
|
||||
insight_category VARCHAR(100) NOT NULL CHECK(insight_category IN (
|
||||
'command_constraints', 'service_configuration', 'version_limitations',
|
||||
'custom_installations', 'network_constraints', 'permissions',
|
||||
'compatibility', 'performance', 'security'
|
||||
)),
|
||||
insight_title VARCHAR(500) NOT NULL,
|
||||
insight_description TEXT NOT NULL, -- markdown formatted
|
||||
|
||||
-- Examples and documentation
|
||||
examples TEXT, -- JSON array of command/config examples
|
||||
affected_operations TEXT, -- JSON array: ["user_management", "service_restart"]
|
||||
|
||||
-- Source and verification
|
||||
source_pattern_id UUID REFERENCES failure_patterns(id) ON DELETE SET NULL,
|
||||
confidence_level VARCHAR(20) CHECK(confidence_level IN ('confirmed', 'likely', 'suspected')),
|
||||
verification_count INTEGER DEFAULT 1, -- how many times verified
|
||||
last_verified TIMESTAMP,
|
||||
|
||||
-- Priority (1-10, higher = more important to avoid)
|
||||
priority INTEGER DEFAULT 5 CHECK(priority BETWEEN 1 AND 10),
|
||||
|
||||
-- Status
|
||||
is_active BOOLEAN DEFAULT true, -- false if pattern no longer applies
|
||||
superseded_by UUID REFERENCES environmental_insights(id), -- if replaced by better insight
|
||||
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
INDEX idx_insights_client (client_id),
|
||||
INDEX idx_insights_infrastructure (infrastructure_id),
|
||||
INDEX idx_insights_category (insight_category),
|
||||
INDEX idx_insights_priority (priority),
|
||||
INDEX idx_insights_active (is_active)
|
||||
);
|
||||
```
|
||||
|
||||
**Real-World Examples:**
|
||||
|
||||
**D2TESTNAS - Custom WINS Installation:**
|
||||
```json
|
||||
{
|
||||
"infrastructure_id": "d2testnas-uuid",
|
||||
"client_id": "dataforth-uuid",
|
||||
"insight_category": "custom_installations",
|
||||
"insight_title": "WINS Service: Manual Samba installation (no native ReadyNAS service)",
|
||||
"insight_description": "**Installation:** Manually installed via Samba nmbd, not a native ReadyNAS service.\n\n**Constraints:**\n- No GUI service manager for WINS\n- Cannot use standard service management commands\n- Configuration via `/etc/frontview/samba/smb.conf.overrides`\n\n**Correct commands:**\n- Check status: `ssh root@192.168.0.9 'ps aux | grep nmbd'`\n- View config: `ssh root@192.168.0.9 'cat /etc/frontview/samba/smb.conf.overrides | grep wins'`\n- Restart: `ssh root@192.168.0.9 'service nmbd restart'`",
|
||||
"examples": [
|
||||
"ps aux | grep nmbd",
|
||||
"cat /etc/frontview/samba/smb.conf.overrides | grep wins",
|
||||
"service nmbd restart"
|
||||
],
|
||||
"affected_operations": ["service_management", "wins_configuration"],
|
||||
"confidence_level": "confirmed",
|
||||
"verification_count": 3,
|
||||
"priority": 9
|
||||
}
|
||||
```
|
||||
|
||||
**AD2 - PowerShell Version Constraints:**
|
||||
```json
|
||||
{
|
||||
"infrastructure_id": "ad2-uuid",
|
||||
"client_id": "dataforth-uuid",
|
||||
"insight_category": "version_limitations",
|
||||
"insight_title": "Server 2022: PowerShell 5.1 command compatibility",
|
||||
"insight_description": "**PowerShell Version:** 5.1 (default)\n\n**Compatible:** Modern cmdlets work (Get-LocalUser, Get-LocalGroup)\n\n**Not available:** PowerShell 7 specific features\n\n**Remote execution:** Use Invoke-Command for remote operations",
|
||||
"examples": [
|
||||
"Get-LocalUser",
|
||||
"Get-LocalGroup",
|
||||
"Invoke-Command -ComputerName AD2 -ScriptBlock { Get-LocalUser }"
|
||||
],
|
||||
"confidence_level": "confirmed",
|
||||
"verification_count": 5,
|
||||
"priority": 6
|
||||
}
|
||||
```
|
||||
|
||||
**Server 2008 - PowerShell 2.0 Limitations:**
|
||||
```json
|
||||
{
|
||||
"infrastructure_id": "old-server-2008-uuid",
|
||||
"insight_category": "version_limitations",
|
||||
"insight_title": "Server 2008: PowerShell 2.0 command compatibility",
|
||||
"insight_description": "**PowerShell Version:** 2.0 only\n\n**Avoid:** Get-LocalUser, Get-LocalGroup, New-LocalUser (not available in PS 2.0)\n\n**Use instead:** Get-WmiObject Win32_UserAccount, Get-WmiObject Win32_Group\n\n**Why:** Server 2008 predates modern PowerShell user management cmdlets",
|
||||
"examples": [
|
||||
"Get-WmiObject Win32_UserAccount",
|
||||
"Get-WmiObject Win32_Group",
|
||||
"Get-WmiObject Win32_UserAccount -Filter \"Name='username'\""
|
||||
],
|
||||
"affected_operations": ["user_management", "group_management"],
|
||||
"confidence_level": "confirmed",
|
||||
"verification_count": 5,
|
||||
"priority": 8
|
||||
}
|
||||
```
|
||||
|
||||
**DOS Machines (TS-XX) - Batch Syntax Constraints:**
|
||||
```json
|
||||
{
|
||||
"infrastructure_id": "ts-27-uuid",
|
||||
"client_id": "dataforth-uuid",
|
||||
"insight_category": "command_constraints",
|
||||
"insight_title": "MS-DOS 6.22: Batch file syntax limitations",
|
||||
"insight_description": "**OS:** MS-DOS 6.22\n\n**No support for:**\n- `IF /I` (case insensitive) - added in Windows 2000\n- Long filenames (8.3 format only)\n- Unicode or special characters\n- Modern batch features\n\n**Workarounds:**\n- Use duplicate IF statements for upper/lowercase\n- Keep filenames to 8.3 format\n- Use basic batch syntax only",
|
||||
"examples": [
|
||||
"IF \"%1\"=\"STATUS\" GOTO STATUS",
|
||||
"IF \"%1\"=\"status\" GOTO STATUS",
|
||||
"COPY FILE.TXT BACKUP.TXT"
|
||||
],
|
||||
"affected_operations": ["batch_scripting", "file_operations"],
|
||||
"confidence_level": "confirmed",
|
||||
"verification_count": 8,
|
||||
"priority": 10
|
||||
}
|
||||
```
|
||||
|
||||
**D2TESTNAS - SMB Protocol Constraints:**
|
||||
```json
|
||||
{
|
||||
"infrastructure_id": "d2testnas-uuid",
|
||||
"insight_category": "network_constraints",
|
||||
"insight_title": "ReadyNAS: SMB1/CORE protocol for DOS compatibility",
|
||||
"insight_description": "**Protocol:** CORE/SMB1 only (for DOS machine compatibility)\n\n**Implications:**\n- Modern SMB2/3 clients may need configuration\n- Use NetBIOS name, not IP address for DOS machines\n- Security risk: SMB1 deprecated due to vulnerabilities\n\n**Configuration:**\n- Set in `/etc/frontview/samba/smb.conf.overrides`\n- `min protocol = CORE`",
|
||||
"examples": [
|
||||
"NET USE Z: \\\\D2TESTNAS\\SHARE (from DOS)",
|
||||
"smbclient -L //192.168.0.9 -m SMB1"
|
||||
],
|
||||
"confidence_level": "confirmed",
|
||||
"priority": 7
|
||||
}
|
||||
```
|
||||
|
||||
**Generated insights.md Example:**
|
||||
|
||||
When Failure Analysis Agent runs, it generates markdown files for each client:
|
||||
|
||||
```markdown
|
||||
# Environmental Insights: Dataforth
|
||||
|
||||
Auto-generated from failure patterns and verified operations.
|
||||
|
||||
## D2TESTNAS (192.168.0.9)
|
||||
|
||||
### Custom Installations
|
||||
|
||||
**WINS Service: Manual Samba installation**
|
||||
- Manually installed via Samba nmbd, not native ReadyNAS service
|
||||
- No GUI service manager for WINS
|
||||
- Configure via `/etc/frontview/samba/smb.conf.overrides`
|
||||
- Check status: `ssh root@192.168.0.9 'ps aux | grep nmbd'`
|
||||
|
||||
### Network Constraints
|
||||
|
||||
**SMB Protocol: CORE/SMB1 only**
|
||||
- For DOS compatibility
|
||||
- Modern SMB2/3 clients may need configuration
|
||||
- Use NetBIOS name from DOS machines
|
||||
|
||||
## AD2 (192.168.0.6 - Server 2022)
|
||||
|
||||
### PowerShell Version
|
||||
|
||||
**Version:** PowerShell 5.1 (default)
|
||||
- **Compatible:** Modern cmdlets work
|
||||
- **Not available:** PowerShell 7 specific features
|
||||
|
||||
## TS-XX Machines (DOS 6.22)
|
||||
|
||||
### Command Constraints
|
||||
|
||||
**No support for:**
|
||||
- `IF /I` (case insensitive) - use duplicate IF statements
|
||||
- Long filenames (8.3 format only)
|
||||
- Unicode or special characters
|
||||
- Modern batch features
|
||||
|
||||
**Examples:**
|
||||
```batch
|
||||
REM Correct (DOS 6.22)
|
||||
IF "%1"=="STATUS" GOTO STATUS
|
||||
IF "%1"=="status" GOTO STATUS
|
||||
|
||||
REM Incorrect (requires Windows 2000+)
|
||||
IF /I "%1"=="STATUS" GOTO STATUS
|
||||
```
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `problem_solutions`
|
||||
|
||||
Issue tracking with root cause analysis and resolution documentation. Searchable historical knowledge base.
|
||||
|
||||
```sql
|
||||
CREATE TABLE problem_solutions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
work_item_id UUID NOT NULL REFERENCES work_items(id) ON DELETE CASCADE,
|
||||
session_id UUID NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
|
||||
client_id UUID REFERENCES clients(id) ON DELETE SET NULL,
|
||||
infrastructure_id UUID REFERENCES infrastructure(id) ON DELETE SET NULL,
|
||||
|
||||
-- Problem description
|
||||
problem_title VARCHAR(500) NOT NULL,
|
||||
problem_description TEXT NOT NULL,
|
||||
symptom TEXT, -- what user/system exhibited
|
||||
error_message TEXT, -- exact error code/message
|
||||
error_code VARCHAR(100), -- structured error code
|
||||
|
||||
-- Investigation
|
||||
investigation_steps TEXT, -- JSON array of diagnostic commands/actions
|
||||
diagnostic_output TEXT, -- key outputs that led to root cause
|
||||
investigation_duration_minutes INTEGER,
|
||||
|
||||
-- Root cause
|
||||
root_cause TEXT NOT NULL,
|
||||
root_cause_category VARCHAR(100), -- "configuration", "hardware", "software", "network"
|
||||
|
||||
-- Solution
|
||||
solution_applied TEXT NOT NULL,
|
||||
solution_category VARCHAR(100), -- "config_change", "restart", "replacement", "patch"
|
||||
commands_run TEXT, -- JSON array of commands used to fix
|
||||
files_modified TEXT, -- JSON array of config files changed
|
||||
|
||||
-- Verification
|
||||
verification_method TEXT,
|
||||
verification_successful BOOLEAN DEFAULT true,
|
||||
verification_notes TEXT,
|
||||
|
||||
-- Prevention and rollback
|
||||
rollback_plan TEXT,
|
||||
prevention_measures TEXT, -- what was done to prevent recurrence
|
||||
|
||||
-- Pattern tracking
|
||||
recurrence_count INTEGER DEFAULT 1, -- if same problem reoccurs
|
||||
similar_problems TEXT, -- JSON array of related problem_solution IDs
|
||||
tags TEXT, -- JSON array: ["ssl", "apache", "certificate"]
|
||||
|
||||
-- Resolution
|
||||
resolved_at TIMESTAMP,
|
||||
time_to_resolution_minutes INTEGER,
|
||||
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
INDEX idx_problems_work_item (work_item_id),
|
||||
INDEX idx_problems_session (session_id),
|
||||
INDEX idx_problems_client (client_id),
|
||||
INDEX idx_problems_infrastructure (infrastructure_id),
|
||||
INDEX idx_problems_category (root_cause_category),
|
||||
FULLTEXT idx_problems_search (problem_description, symptom, error_message, root_cause)
|
||||
);
|
||||
```
|
||||
|
||||
**Example Problem Solutions:**
|
||||
|
||||
**Apache SSL Certificate Expiration:**
|
||||
```json
|
||||
{
|
||||
"problem_title": "Apache SSL certificate expiration causing ERR_SSL_PROTOCOL_ERROR",
|
||||
"problem_description": "Website inaccessible via HTTPS. Browser shows ERR_SSL_PROTOCOL_ERROR.",
|
||||
"symptom": "Users unable to access website. SSL handshake failure.",
|
||||
"error_message": "ERR_SSL_PROTOCOL_ERROR",
|
||||
"investigation_steps": [
|
||||
"curl -I https://example.com",
|
||||
"openssl s_client -connect example.com:443",
|
||||
"systemctl status apache2",
|
||||
"openssl x509 -in /etc/ssl/certs/example.com.crt -text -noout"
|
||||
],
|
||||
"diagnostic_output": "Certificate expiration: 2026-01-10 (3 days ago)",
|
||||
"root_cause": "SSL certificate expired on 2026-01-10. Certbot auto-renewal failed due to DNS validation issue.",
|
||||
"root_cause_category": "configuration",
|
||||
"solution_applied": "1. Fixed DNS TXT record for Let's Encrypt validation\n2. Ran: certbot renew --force-renewal\n3. Restarted Apache: systemctl restart apache2",
|
||||
"solution_category": "config_change",
|
||||
"commands_run": [
|
||||
"certbot renew --force-renewal",
|
||||
"systemctl restart apache2"
|
||||
],
|
||||
"files_modified": [
|
||||
"/etc/apache2/sites-enabled/example.com.conf"
|
||||
],
|
||||
"verification_method": "curl test successful. Browser loads HTTPS site without error.",
|
||||
"verification_successful": true,
|
||||
"prevention_measures": "Set up monitoring for certificate expiration (30 days warning). Fixed DNS automation for certbot.",
|
||||
"tags": ["ssl", "apache", "certificate", "certbot"],
|
||||
"time_to_resolution_minutes": 25
|
||||
}
|
||||
```
|
||||
|
||||
**PowerShell Compatibility Issue:**
|
||||
```json
|
||||
{
|
||||
"problem_title": "Get-LocalUser fails on Server 2008 (PowerShell 2.0)",
|
||||
"problem_description": "Attempting to list local users on Server 2008 using Get-LocalUser cmdlet",
|
||||
"symptom": "Command not recognized error",
|
||||
"error_message": "Get-LocalUser : The term 'Get-LocalUser' is not recognized as the name of a cmdlet",
|
||||
"error_code": "CommandNotFoundException",
|
||||
"investigation_steps": [
|
||||
"$PSVersionTable",
|
||||
"Get-Command Get-LocalUser",
|
||||
"Get-WmiObject Win32_OperatingSystem | Select Caption, Version"
|
||||
],
|
||||
"root_cause": "Server 2008 has PowerShell 2.0 only. Get-LocalUser introduced in PowerShell 5.1 (Windows 10/Server 2016).",
|
||||
"root_cause_category": "software",
|
||||
"solution_applied": "Use WMI instead: Get-WmiObject Win32_UserAccount",
|
||||
"solution_category": "alternative_approach",
|
||||
"commands_run": [
|
||||
"Get-WmiObject Win32_UserAccount | Select Name, Disabled, LocalAccount"
|
||||
],
|
||||
"verification_method": "Successfully retrieved local user list",
|
||||
"verification_successful": true,
|
||||
"prevention_measures": "Created environmental insight for all Server 2008 machines. Environment Context Agent now checks PowerShell version before suggesting cmdlets.",
|
||||
"tags": ["powershell", "server_2008", "compatibility", "user_management"],
|
||||
"recurrence_count": 5
|
||||
}
|
||||
```
|
||||
|
||||
**Queries:**
|
||||
|
||||
```sql
|
||||
-- Find similar problems by error message
|
||||
SELECT problem_title, solution_applied, created_at
|
||||
FROM problem_solutions
|
||||
WHERE MATCH(error_message) AGAINST('SSL_PROTOCOL_ERROR' IN BOOLEAN MODE)
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- Most common problems (by recurrence)
|
||||
SELECT problem_title, recurrence_count, root_cause_category
|
||||
FROM problem_solutions
|
||||
WHERE recurrence_count > 1
|
||||
ORDER BY recurrence_count DESC;
|
||||
|
||||
-- Recent solutions for client
|
||||
SELECT problem_title, solution_applied, resolved_at
|
||||
FROM problem_solutions
|
||||
WHERE client_id = 'dataforth-uuid'
|
||||
ORDER BY resolved_at DESC
|
||||
LIMIT 10;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `failure_patterns`
|
||||
|
||||
Aggregated failure insights learned from command/operation failures. Auto-generated by Failure Analysis Agent.
|
||||
|
||||
```sql
|
||||
CREATE TABLE failure_patterns (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
infrastructure_id UUID REFERENCES infrastructure(id) ON DELETE CASCADE,
|
||||
client_id UUID REFERENCES clients(id) ON DELETE CASCADE,
|
||||
|
||||
-- Pattern identification
|
||||
pattern_type VARCHAR(100) NOT NULL CHECK(pattern_type IN (
|
||||
'command_compatibility', 'version_mismatch', 'permission_denied',
|
||||
'service_unavailable', 'configuration_error', 'environmental_limitation',
|
||||
'network_connectivity', 'authentication_failure', 'syntax_error'
|
||||
)),
|
||||
pattern_signature VARCHAR(500) NOT NULL, -- "PowerShell 7 cmdlets on Server 2008"
|
||||
error_pattern TEXT, -- regex or keywords: "Get-LocalUser.*not recognized"
|
||||
|
||||
-- Context
|
||||
affected_systems TEXT, -- JSON array: ["all_server_2008", "D2TESTNAS"]
|
||||
affected_os_versions TEXT, -- JSON array: ["Server 2008", "DOS 6.22"]
|
||||
triggering_commands TEXT, -- JSON array of command patterns
|
||||
triggering_operations TEXT, -- JSON array of operation types
|
||||
|
||||
-- Failure details
|
||||
failure_description TEXT NOT NULL,
|
||||
typical_error_messages TEXT, -- JSON array of common error texts
|
||||
|
||||
-- Resolution
|
||||
root_cause TEXT NOT NULL, -- "Server 2008 only has PowerShell 2.0"
|
||||
recommended_solution TEXT NOT NULL, -- "Use Get-WmiObject instead of Get-LocalUser"
|
||||
alternative_approaches TEXT, -- JSON array of alternatives
|
||||
workaround_commands TEXT, -- JSON array of working commands
|
||||
|
||||
-- Metadata
|
||||
occurrence_count INTEGER DEFAULT 1, -- how many times seen
|
||||
first_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
last_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
severity VARCHAR(20) CHECK(severity IN ('blocking', 'major', 'minor', 'info')),
|
||||
|
||||
-- Status
|
||||
is_active BOOLEAN DEFAULT true, -- false if pattern no longer applies (e.g., server upgraded)
|
||||
added_to_insights BOOLEAN DEFAULT false, -- environmental_insight generated
|
||||
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
INDEX idx_failure_infrastructure (infrastructure_id),
|
||||
INDEX idx_failure_client (client_id),
|
||||
INDEX idx_failure_pattern_type (pattern_type),
|
||||
INDEX idx_failure_signature (pattern_signature),
|
||||
INDEX idx_failure_active (is_active),
|
||||
INDEX idx_failure_severity (severity)
|
||||
);
|
||||
```
|
||||
|
||||
**Example Failure Patterns:**
|
||||
|
||||
**PowerShell Version Incompatibility:**
|
||||
```json
|
||||
{
|
||||
"pattern_type": "command_compatibility",
|
||||
"pattern_signature": "Modern PowerShell cmdlets on Server 2008",
|
||||
"error_pattern": "(Get-LocalUser|Get-LocalGroup|New-LocalUser).*not recognized",
|
||||
"affected_systems": ["all_server_2008_machines"],
|
||||
"affected_os_versions": ["Server 2008", "Server 2008 R2"],
|
||||
"triggering_commands": [
|
||||
"Get-LocalUser",
|
||||
"Get-LocalGroup",
|
||||
"New-LocalUser",
|
||||
"Remove-LocalUser"
|
||||
],
|
||||
"failure_description": "Modern PowerShell user management cmdlets fail on Server 2008 with 'not recognized' error",
|
||||
"typical_error_messages": [
|
||||
"Get-LocalUser : The term 'Get-LocalUser' is not recognized",
|
||||
"Get-LocalGroup : The term 'Get-LocalGroup' is not recognized"
|
||||
],
|
||||
"root_cause": "Server 2008 has PowerShell 2.0 only. Modern user management cmdlets (Get-LocalUser, etc.) were introduced in PowerShell 5.1 (Windows 10/Server 2016).",
|
||||
"recommended_solution": "Use WMI for user/group management: Get-WmiObject Win32_UserAccount, Get-WmiObject Win32_Group",
|
||||
"alternative_approaches": [
|
||||
"Use Get-WmiObject Win32_UserAccount",
|
||||
"Use net user command",
|
||||
"Upgrade to PowerShell 5.1 (if possible on Server 2008 R2)"
|
||||
],
|
||||
"workaround_commands": [
|
||||
"Get-WmiObject Win32_UserAccount",
|
||||
"Get-WmiObject Win32_Group",
|
||||
"net user"
|
||||
],
|
||||
"occurrence_count": 5,
|
||||
"severity": "major",
|
||||
"added_to_insights": true
|
||||
}
|
||||
```
|
||||
|
||||
**DOS Batch Syntax Limitation:**
|
||||
```json
|
||||
{
|
||||
"pattern_type": "environmental_limitation",
|
||||
"pattern_signature": "Modern batch syntax on MS-DOS 6.22",
|
||||
"error_pattern": "IF /I.*Invalid switch",
|
||||
"affected_systems": ["all_dos_machines"],
|
||||
"affected_os_versions": ["MS-DOS 6.22"],
|
||||
"triggering_commands": [
|
||||
"IF /I \"%1\"==\"value\" ...",
|
||||
"Long filenames with spaces"
|
||||
],
|
||||
"failure_description": "Modern batch file syntax not supported in MS-DOS 6.22",
|
||||
"typical_error_messages": [
|
||||
"Invalid switch - /I",
|
||||
"File not found (long filename)",
|
||||
"Bad command or file name"
|
||||
],
|
||||
"root_cause": "DOS 6.22 does not support /I flag (added in Windows 2000), long filenames, or many modern batch features",
|
||||
"recommended_solution": "Use duplicate IF statements for upper/lowercase. Keep filenames to 8.3 format. Use basic batch syntax only.",
|
||||
"alternative_approaches": [
|
||||
"Duplicate IF for case-insensitive: IF \"%1\"==\"VALUE\" ... + IF \"%1\"==\"value\" ...",
|
||||
"Use 8.3 filenames only",
|
||||
"Avoid advanced batch features"
|
||||
],
|
||||
"workaround_commands": [
|
||||
"IF \"%1\"==\"STATUS\" GOTO STATUS",
|
||||
"IF \"%1\"==\"status\" GOTO STATUS"
|
||||
],
|
||||
"occurrence_count": 8,
|
||||
"severity": "blocking",
|
||||
"added_to_insights": true
|
||||
}
|
||||
```
|
||||
|
||||
**ReadyNAS Service Management:**
|
||||
```json
|
||||
{
|
||||
"pattern_type": "service_unavailable",
|
||||
"pattern_signature": "systemd commands on ReadyNAS",
|
||||
"error_pattern": "systemctl.*command not found",
|
||||
"affected_systems": ["D2TESTNAS"],
|
||||
"triggering_commands": [
|
||||
"systemctl status nmbd",
|
||||
"systemctl restart samba"
|
||||
],
|
||||
"failure_description": "ReadyNAS does not use systemd for service management",
|
||||
"typical_error_messages": [
|
||||
"systemctl: command not found",
|
||||
"-ash: systemctl: not found"
|
||||
],
|
||||
"root_cause": "ReadyNAS OS is based on older Linux without systemd. Uses traditional init scripts.",
|
||||
"recommended_solution": "Use 'service' command or direct process management: service nmbd status, ps aux | grep nmbd",
|
||||
"alternative_approaches": [
|
||||
"service nmbd status",
|
||||
"ps aux | grep nmbd",
|
||||
"/etc/init.d/nmbd status"
|
||||
],
|
||||
"occurrence_count": 3,
|
||||
"severity": "major",
|
||||
"added_to_insights": true
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `operation_failures`
|
||||
|
||||
Non-command failures (API calls, integrations, file operations, network requests). Complements commands_run failure tracking.
|
||||
|
||||
```sql
|
||||
CREATE TABLE operation_failures (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
session_id UUID REFERENCES sessions(id) ON DELETE CASCADE,
|
||||
work_item_id UUID REFERENCES work_items(id) ON DELETE CASCADE,
|
||||
client_id UUID REFERENCES clients(id) ON DELETE SET NULL,
|
||||
|
||||
-- Operation details
|
||||
operation_type VARCHAR(100) NOT NULL CHECK(operation_type IN (
|
||||
'api_call', 'file_operation', 'network_request',
|
||||
'database_query', 'external_integration', 'service_restart',
|
||||
'backup_operation', 'restore_operation', 'migration'
|
||||
)),
|
||||
operation_description TEXT NOT NULL,
|
||||
target_system VARCHAR(255), -- host, URL, service name
|
||||
|
||||
-- Failure details
|
||||
error_message TEXT NOT NULL,
|
||||
error_code VARCHAR(50), -- HTTP status, exit code, error number
|
||||
failure_category VARCHAR(100), -- "timeout", "authentication", "not_found", etc.
|
||||
stack_trace TEXT,
|
||||
|
||||
-- Context
|
||||
request_data TEXT, -- JSON: what was attempted
|
||||
response_data TEXT, -- JSON: error response
|
||||
environment_snapshot TEXT, -- JSON: relevant env vars, versions
|
||||
|
||||
-- Resolution
|
||||
resolution_applied TEXT,
|
||||
resolved BOOLEAN DEFAULT false,
|
||||
resolved_at TIMESTAMP,
|
||||
time_to_resolution_minutes INTEGER,
|
||||
|
||||
-- Pattern linkage
|
||||
related_pattern_id UUID REFERENCES failure_patterns(id),
|
||||
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
INDEX idx_op_failure_session (session_id),
|
||||
INDEX idx_op_failure_type (operation_type),
|
||||
INDEX idx_op_failure_category (failure_category),
|
||||
INDEX idx_op_failure_resolved (resolved),
|
||||
INDEX idx_op_failure_client (client_id)
|
||||
);
|
||||
```
|
||||
|
||||
**Example Operation Failures:**
|
||||
|
||||
**SyncroMSP API Timeout:**
|
||||
```json
|
||||
{
|
||||
"operation_type": "api_call",
|
||||
"operation_description": "Search SyncroMSP tickets for Dataforth",
|
||||
"target_system": "https://azcomputerguru.syncromsp.com/api/v1",
|
||||
"error_message": "Request timeout after 30 seconds",
|
||||
"error_code": "ETIMEDOUT",
|
||||
"failure_category": "timeout",
|
||||
"request_data": {
|
||||
"endpoint": "/api/v1/tickets",
|
||||
"params": {"customer_id": 12345, "status": "open"}
|
||||
},
|
||||
"response_data": null,
|
||||
"resolution_applied": "Increased timeout to 60 seconds. Added retry logic with exponential backoff.",
|
||||
"resolved": true,
|
||||
"time_to_resolution_minutes": 15
|
||||
}
|
||||
```
|
||||
|
||||
**File Upload Permission Denied:**
|
||||
```json
|
||||
{
|
||||
"operation_type": "file_operation",
|
||||
"operation_description": "Upload backup file to NAS",
|
||||
"target_system": "D2TESTNAS:/mnt/backups",
|
||||
"error_message": "Permission denied: /mnt/backups/db_backup_2026-01-15.sql",
|
||||
"error_code": "EACCES",
|
||||
"failure_category": "permission",
|
||||
"environment_snapshot": {
|
||||
"user": "backupuser",
|
||||
"directory_perms": "drwxr-xr-x root root"
|
||||
},
|
||||
"resolution_applied": "Changed directory ownership: chown -R backupuser:backupgroup /mnt/backups",
|
||||
"resolved": true
|
||||
}
|
||||
```
|
||||
|
||||
**Database Query Performance:**
|
||||
```json
|
||||
{
|
||||
"operation_type": "database_query",
|
||||
"operation_description": "Query sessions table for large date range",
|
||||
"target_system": "MariaDB msp_tracking",
|
||||
"error_message": "Query execution time: 45 seconds (threshold: 5 seconds)",
|
||||
"failure_category": "performance",
|
||||
"request_data": {
|
||||
"query": "SELECT * FROM sessions WHERE session_date BETWEEN '2020-01-01' AND '2026-01-15'"
|
||||
},
|
||||
"resolution_applied": "Added index on session_date column. Query now runs in 0.3 seconds.",
|
||||
"resolved": true
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Self-Learning Workflow
|
||||
|
||||
### 1. Failure Detection and Logging
|
||||
|
||||
**Command Execution with Failure Tracking:**
|
||||
|
||||
```
|
||||
User: "Check WINS status on D2TESTNAS"
|
||||
|
||||
Main Claude → Environment Context Agent:
|
||||
- Queries infrastructure table for D2TESTNAS
|
||||
- Reads environmental_notes: "Manual WINS install, no native service"
|
||||
- Reads environmental_insights for D2TESTNAS
|
||||
- Returns: "D2TESTNAS has manually installed WINS (not native ReadyNAS service)"
|
||||
|
||||
Main Claude suggests command based on environmental context:
|
||||
- Executes: ssh root@192.168.0.9 'systemctl status nmbd'
|
||||
|
||||
Command fails:
|
||||
- success = false
|
||||
- exit_code = 127
|
||||
- error_message = "systemctl: command not found"
|
||||
- failure_category = "command_compatibility"
|
||||
|
||||
Trigger Failure Analysis Agent:
|
||||
- Analyzes error: ReadyNAS doesn't use systemd
|
||||
- Identifies correct approach: "service nmbd status" or "ps aux | grep nmbd"
|
||||
- Creates failure_pattern entry
|
||||
- Updates environmental_insights with correction
|
||||
- Returns resolution to Main Claude
|
||||
|
||||
Main Claude tries corrected command:
|
||||
- Executes: ssh root@192.168.0.9 'ps aux | grep nmbd'
|
||||
- Success = true
|
||||
- Updates original failure record with resolution
|
||||
```
|
||||
|
||||
### 2. Pattern Analysis (Periodic Agent Run)
|
||||
|
||||
**Failure Analysis Agent runs periodically:**
|
||||
|
||||
**Agent Task:** "Analyze recent failures and update environmental insights"
|
||||
|
||||
1. **Query failures:**
|
||||
```sql
|
||||
SELECT * FROM commands_run
|
||||
WHERE success = false AND resolved = false
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
SELECT * FROM operation_failures
|
||||
WHERE resolved = false
|
||||
ORDER BY created_at DESC;
|
||||
```
|
||||
|
||||
2. **Group by pattern:**
|
||||
- Group by infrastructure_id, error_pattern, failure_category
|
||||
- Identify recurring patterns
|
||||
|
||||
3. **Create/update failure_patterns:**
|
||||
- If pattern seen 3+ times → Create failure_pattern
|
||||
- Increment occurrence_count for existing patterns
|
||||
- Update last_seen timestamp
|
||||
|
||||
4. **Generate environmental_insights:**
|
||||
- Transform failure_patterns into actionable insights
|
||||
- Create markdown-formatted descriptions
|
||||
- Add command examples
|
||||
- Set priority based on severity and frequency
|
||||
|
||||
5. **Update infrastructure environmental_notes:**
|
||||
- Add constraints to infrastructure.environmental_notes
|
||||
- Set powershell_version, shell_type, limitations
|
||||
|
||||
6. **Generate insights.md file:**
|
||||
- Query all environmental_insights for client
|
||||
- Format as markdown
|
||||
- Save to D:\ClaudeTools\insights\[client-name].md
|
||||
- Agents read this file before making suggestions
|
||||
|
||||
### 3. Pre-Operation Environment Check
|
||||
|
||||
**Environment Context Agent runs before operations:**
|
||||
|
||||
**Agent Task:** "Check environmental constraints for D2TESTNAS before command suggestion"
|
||||
|
||||
1. **Query infrastructure:**
|
||||
```sql
|
||||
SELECT environmental_notes, powershell_version, shell_type, limitations
|
||||
FROM infrastructure
|
||||
WHERE id = 'd2testnas-uuid';
|
||||
```
|
||||
|
||||
2. **Query environmental_insights:**
|
||||
```sql
|
||||
SELECT insight_title, insight_description, examples, priority
|
||||
FROM environmental_insights
|
||||
WHERE infrastructure_id = 'd2testnas-uuid'
|
||||
AND is_active = true
|
||||
ORDER BY priority DESC;
|
||||
```
|
||||
|
||||
3. **Query failure_patterns:**
|
||||
```sql
|
||||
SELECT pattern_signature, recommended_solution, workaround_commands
|
||||
FROM failure_patterns
|
||||
WHERE infrastructure_id = 'd2testnas-uuid'
|
||||
AND is_active = true;
|
||||
```
|
||||
|
||||
4. **Check proposed command compatibility:**
|
||||
- Proposed: "systemctl status nmbd"
|
||||
- Pattern match: "systemctl.*command not found"
|
||||
- **Result:** INCOMPATIBLE
|
||||
- Recommended: "ps aux | grep nmbd"
|
||||
|
||||
5. **Return environmental context:**
|
||||
```
|
||||
Environmental Context for D2TESTNAS:
|
||||
- ReadyNAS OS (Linux-based)
|
||||
- Manual WINS installation (Samba nmbd)
|
||||
- No systemd (use 'service' or ps commands)
|
||||
- SMB1/CORE protocol for DOS compatibility
|
||||
|
||||
Recommended commands:
|
||||
✓ ps aux | grep nmbd
|
||||
✓ service nmbd status
|
||||
✗ systemctl status nmbd (not available)
|
||||
```
|
||||
|
||||
Main Claude uses this context to suggest correct approach.
|
||||
|
||||
---
|
||||
|
||||
## Benefits
|
||||
|
||||
### 1. Self-Improving System
|
||||
- Each failure makes the system smarter
|
||||
- Patterns identified automatically
|
||||
- Insights generated without manual documentation
|
||||
- Knowledge accumulates over time
|
||||
|
||||
### 2. Reduced User Friction
|
||||
- User doesn't have to keep correcting same mistakes
|
||||
- Claude learns environmental constraints once
|
||||
- Suggestions are environmentally aware from start
|
||||
- Proactive problem prevention
|
||||
|
||||
### 3. Institutional Knowledge Capture
|
||||
- All environmental quirks documented in database
|
||||
- Survives across sessions and Claude instances
|
||||
- Queryable: "What are known issues with D2TESTNAS?"
|
||||
- Transferable to new team members
|
||||
|
||||
### 4. Proactive Problem Prevention
|
||||
- Environment Context Agent prevents failures before they happen
|
||||
- Suggests compatible alternatives automatically
|
||||
- Warns about known limitations
|
||||
- Avoids wasting time on incompatible approaches
|
||||
|
||||
### 5. Audit Trail
|
||||
- Every failure tracked with full context
|
||||
- Resolution history for troubleshooting
|
||||
- Pattern analysis for infrastructure planning
|
||||
- ROI tracking: time saved by avoiding repeat failures
|
||||
|
||||
---
|
||||
|
||||
## Integration with Other Schemas
|
||||
|
||||
**Sources data from:**
|
||||
- `commands_run` - Command execution failures
|
||||
- `infrastructure` - System capabilities and limitations
|
||||
- `work_items` - Context for failures
|
||||
- `sessions` - Session context for operations
|
||||
|
||||
**Provides data to:**
|
||||
- Environment Context Agent (pre-operation checks)
|
||||
- Problem Pattern Matching Agent (solution lookup)
|
||||
- MSP Mode (intelligent suggestions)
|
||||
- Reporting (failure analysis, improvement metrics)
|
||||
|
||||
---
|
||||
|
||||
## Example Queries
|
||||
|
||||
### Find all insights for a client
|
||||
```sql
|
||||
SELECT ei.insight_title, ei.insight_description, i.hostname
|
||||
FROM environmental_insights ei
|
||||
JOIN infrastructure i ON ei.infrastructure_id = i.id
|
||||
WHERE ei.client_id = 'dataforth-uuid'
|
||||
AND ei.is_active = true
|
||||
ORDER BY ei.priority DESC;
|
||||
```
|
||||
|
||||
### Search for similar problems
|
||||
```sql
|
||||
SELECT ps.problem_title, ps.solution_applied, ps.created_at
|
||||
FROM problem_solutions ps
|
||||
WHERE MATCH(ps.problem_description, ps.symptom, ps.error_message)
|
||||
AGAINST('SSL certificate' IN BOOLEAN MODE)
|
||||
ORDER BY ps.created_at DESC
|
||||
LIMIT 10;
|
||||
```
|
||||
|
||||
### Active failure patterns
|
||||
```sql
|
||||
SELECT fp.pattern_signature, fp.occurrence_count, fp.recommended_solution
|
||||
FROM failure_patterns fp
|
||||
WHERE fp.is_active = true
|
||||
AND fp.severity IN ('blocking', 'major')
|
||||
ORDER BY fp.occurrence_count DESC;
|
||||
```
|
||||
|
||||
### Unresolved operation failures
|
||||
```sql
|
||||
SELECT of.operation_type, of.target_system, of.error_message, of.created_at
|
||||
FROM operation_failures of
|
||||
WHERE of.resolved = false
|
||||
ORDER BY of.created_at DESC;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**Document Version:** 1.0
|
||||
**Last Updated:** 2026-01-15
|
||||
**Author:** MSP Mode Schema Design Team
|
||||
@@ -1,16 +1,15 @@
|
||||
# ClaudeTools Project Context
|
||||
|
||||
**Project Type:** MSP Work Tracking System with AI Context Recall
|
||||
**Status:** Production-Ready (95% Complete)
|
||||
**Project Type:** MSP Work Tracking System
|
||||
**Status:** Production-Ready
|
||||
**Database:** MariaDB 10.6.22 @ 172.16.3.30:3306 (RMM Server)
|
||||
|
||||
---
|
||||
|
||||
## Quick Facts
|
||||
|
||||
- **130 API Endpoints** across 21 entities
|
||||
- **43 Database Tables** (fully migrated)
|
||||
- **Context Recall System** with cross-machine persistent memory
|
||||
- **95+ API Endpoints** across 17 entities
|
||||
- **38 Database Tables** (fully migrated)
|
||||
- **JWT Authentication** on all endpoints
|
||||
- **AES-256-GCM Encryption** for credentials
|
||||
- **3 MCP Servers** configured (GitHub, Filesystem, Sequential Thinking)
|
||||
@@ -22,20 +21,18 @@
|
||||
```
|
||||
D:\ClaudeTools/
|
||||
├── api/ # FastAPI application
|
||||
│ ├── main.py # API entry point (130 endpoints)
|
||||
│ ├── models/ # SQLAlchemy models (42 models)
|
||||
│ ├── routers/ # API endpoints (21 routers)
|
||||
│ ├── schemas/ # Pydantic schemas (84 classes)
|
||||
│ ├── services/ # Business logic (21 services)
|
||||
│ ├── main.py # API entry point
|
||||
│ ├── models/ # SQLAlchemy models
|
||||
│ ├── routers/ # API endpoints
|
||||
│ ├── schemas/ # Pydantic schemas
|
||||
│ ├── services/ # Business logic
|
||||
│ ├── middleware/ # Auth & error handling
|
||||
│ └── utils/ # Crypto & compression utilities
|
||||
│ └── utils/ # Crypto utilities
|
||||
├── migrations/ # Alembic database migrations
|
||||
├── .claude/ # Claude Code hooks & config
|
||||
│ ├── commands/ # Commands (sync, create-spec, checkpoint)
|
||||
│ ├── commands/ # Commands (create-spec, checkpoint)
|
||||
│ ├── skills/ # Skills (frontend-design)
|
||||
│ ├── templates/ # Templates (app spec, prompts)
|
||||
│ ├── hooks/ # Auto-inject/save context
|
||||
│ └── context-recall-config.env # Configuration
|
||||
│ └── templates/ # Templates (app spec, prompts)
|
||||
├── mcp-servers/ # MCP server implementations
|
||||
│ └── feature-management/ # Feature tracking MCP server
|
||||
├── scripts/ # Setup & test scripts
|
||||
@@ -84,54 +81,6 @@ http://localhost:8000/api/docs
|
||||
|
||||
---
|
||||
|
||||
## Context Recall System
|
||||
|
||||
### How It Works
|
||||
|
||||
**Automatic context injection via Claude Code hooks:**
|
||||
- `.claude/hooks/user-prompt-submit` - Recalls context before each message
|
||||
- `.claude/hooks/task-complete` - Saves context after completion
|
||||
|
||||
### Setup (One-Time)
|
||||
|
||||
```bash
|
||||
bash scripts/setup-context-recall.sh
|
||||
```
|
||||
|
||||
### Manual Context Recall
|
||||
|
||||
**API Endpoint:**
|
||||
```
|
||||
GET http://localhost:8000/api/conversation-contexts/recall
|
||||
?project_id={uuid}
|
||||
&tags[]=fastapi&tags[]=database
|
||||
&limit=10
|
||||
&min_relevance_score=5.0
|
||||
```
|
||||
|
||||
**Test Context Recall:**
|
||||
```bash
|
||||
bash scripts/test-context-recall.sh
|
||||
```
|
||||
|
||||
### Save Context Manually
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8000/api/conversation-contexts \
|
||||
-H "Authorization: Bearer $JWT_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"project_id": "uuid-here",
|
||||
"context_type": "session_summary",
|
||||
"title": "Current work session",
|
||||
"dense_summary": "Working on API endpoints...",
|
||||
"relevance_score": 7.0,
|
||||
"tags": ["api", "fastapi", "development"]
|
||||
}'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Key API Endpoints
|
||||
|
||||
### Core Entities (Phase 4)
|
||||
@@ -159,17 +108,11 @@ curl -X POST http://localhost:8000/api/conversation-contexts \
|
||||
- `/api/credential-audit-logs` - Audit trail (read-only)
|
||||
- `/api/security-incidents` - Incident tracking
|
||||
|
||||
### Context Recall (Phase 6)
|
||||
- `/api/conversation-contexts` - Context storage & recall
|
||||
- `/api/context-snippets` - Knowledge fragments
|
||||
- `/api/project-states` - Project state tracking
|
||||
- `/api/decision-logs` - Decision documentation
|
||||
|
||||
---
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### 1. Create New Project with Context
|
||||
### 1. Create New Project
|
||||
|
||||
```python
|
||||
# Create project
|
||||
@@ -179,33 +122,9 @@ POST /api/projects
|
||||
"client_id": "client-uuid",
|
||||
"status": "planning"
|
||||
}
|
||||
|
||||
# Initialize project state
|
||||
POST /api/project-states
|
||||
{
|
||||
"project_id": "project-uuid",
|
||||
"current_phase": "requirements",
|
||||
"progress_percentage": 10,
|
||||
"next_actions": ["Gather requirements", "Design mockups"]
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Log Important Decision
|
||||
|
||||
```python
|
||||
POST /api/decision-logs
|
||||
{
|
||||
"project_id": "project-uuid",
|
||||
"decision_type": "technical",
|
||||
"decision_text": "Using FastAPI for API layer",
|
||||
"rationale": "Async support, automatic OpenAPI docs, modern Python",
|
||||
"alternatives_considered": ["Flask", "Django"],
|
||||
"impact": "high",
|
||||
"tags": ["api", "framework", "python"]
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Track Work Session
|
||||
### 2. Track Work Session
|
||||
|
||||
```python
|
||||
# Create session
|
||||
@@ -230,7 +149,7 @@ POST /api/billable-time
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Store Encrypted Credential
|
||||
### 3. Store Encrypted Credential
|
||||
|
||||
```python
|
||||
POST /api/credentials
|
||||
@@ -253,22 +172,16 @@ POST /api/credentials
|
||||
**Session State:** `SESSION_STATE.md` - Complete project history and status
|
||||
|
||||
**Documentation:**
|
||||
- `.claude/CONTEXT_RECALL_QUICK_START.md` - Context recall usage
|
||||
- `CONTEXT_RECALL_SETUP.md` - Full setup guide
|
||||
- `AUTOCODER_INTEGRATION.md` - AutoCoder resources guide
|
||||
- `TEST_PHASE5_RESULTS.md` - Phase 5 test results
|
||||
- `TEST_CONTEXT_RECALL_RESULTS.md` - Context recall test results
|
||||
|
||||
**Configuration:**
|
||||
- `.env` - Environment variables (gitignored)
|
||||
- `.env.example` - Template with placeholders
|
||||
- `.claude/context-recall-config.env` - Context recall settings (gitignored)
|
||||
|
||||
**Tests:**
|
||||
- `test_api_endpoints.py` - Phase 4 tests (34/35 passing)
|
||||
- `test_phase5_api_endpoints.py` - Phase 5 tests (62/62 passing)
|
||||
- `test_context_recall_system.py` - Context recall tests (53 total)
|
||||
- `test_context_compression_quick.py` - Compression tests (10/10 passing)
|
||||
- `test_api_endpoints.py` - Phase 4 tests
|
||||
- `test_phase5_api_endpoints.py` - Phase 5 tests
|
||||
|
||||
**AutoCoder Resources:**
|
||||
- `.claude/commands/create-spec.md` - Create app specification
|
||||
@@ -281,38 +194,19 @@ POST /api/credentials
|
||||
|
||||
## Recent Work (from SESSION_STATE.md)
|
||||
|
||||
**Last Session:** 2026-01-16
|
||||
**Phases Completed:** 0-6 (95% complete)
|
||||
**Last Session:** 2026-01-18
|
||||
**Phases Completed:** 0-5 (complete)
|
||||
|
||||
**Phase 6 - Just Completed:**
|
||||
- Context Recall System with cross-machine memory
|
||||
- 35 new endpoints for context management
|
||||
- 90-95% token reduction via compression
|
||||
- Automatic hooks for inject/save
|
||||
- One-command setup script
|
||||
**Phase 5 - Completed:**
|
||||
- MSP Work Tracking system
|
||||
- Infrastructure management endpoints
|
||||
- Encrypted credential storage
|
||||
- Security incident tracking
|
||||
|
||||
**Current State:**
|
||||
- 130 endpoints operational
|
||||
- 99.1% test pass rate (106/107 tests)
|
||||
- All migrations applied (43 tables)
|
||||
- Context recall ready for activation
|
||||
|
||||
---
|
||||
|
||||
## Token Optimization
|
||||
|
||||
**Context Compression:**
|
||||
- `compress_conversation_summary()` - 85-90% reduction
|
||||
- `format_for_injection()` - Token-efficient markdown
|
||||
- `extract_key_decisions()` - Decision extraction
|
||||
- Auto-tag extraction (30+ tech tags)
|
||||
|
||||
**Typical Compression:**
|
||||
```
|
||||
Original: 500 tokens (verbose conversation)
|
||||
Compressed: 60 tokens (structured JSON)
|
||||
Reduction: 88%
|
||||
```
|
||||
- 95+ endpoints operational
|
||||
- All migrations applied (38 tables)
|
||||
- Full test coverage
|
||||
|
||||
---
|
||||
|
||||
@@ -321,14 +215,9 @@ Reduction: 88%
|
||||
**Authentication:** JWT tokens (Argon2 password hashing)
|
||||
**Encryption:** AES-256-GCM (Fernet) for credentials
|
||||
**Audit Logging:** All credential operations logged
|
||||
**Token Storage:** `.claude/context-recall-config.env` (gitignored)
|
||||
|
||||
**Get JWT Token:**
|
||||
```bash
|
||||
# Via setup script (recommended)
|
||||
bash scripts/setup-context-recall.sh
|
||||
|
||||
# Or manually via API
|
||||
POST /api/auth/token
|
||||
{
|
||||
"email": "user@example.com",
|
||||
@@ -349,18 +238,6 @@ netstat -ano | findstr :8000
|
||||
python test_db_connection.py
|
||||
```
|
||||
|
||||
**Context recall not working:**
|
||||
```bash
|
||||
# Test the system
|
||||
bash scripts/test-context-recall.sh
|
||||
|
||||
# Check configuration
|
||||
cat .claude/context-recall-config.env
|
||||
|
||||
# Verify hooks are executable
|
||||
ls -l .claude/hooks/
|
||||
```
|
||||
|
||||
**Database migration issues:**
|
||||
```bash
|
||||
# Check current revision
|
||||
@@ -428,9 +305,7 @@ alembic upgrade head
|
||||
|
||||
**Start API:** `uvicorn api.main:app --reload`
|
||||
**API Docs:** `http://localhost:8000/api/docs` (local) or `http://172.16.3.30:8001/api/docs` (RMM)
|
||||
**Setup Context Recall:** `bash scripts/setup-context-recall.sh`
|
||||
**Setup MCP Servers:** `bash scripts/setup-mcp-servers.sh`
|
||||
**Test System:** `bash scripts/test-context-recall.sh`
|
||||
**Database:** `172.16.3.30:3306/claudetools` (RMM Server)
|
||||
**Virtual Env:** `api\venv\Scripts\activate`
|
||||
**Coding Guidelines:** `.claude/CODING_GUIDELINES.md`
|
||||
@@ -438,7 +313,6 @@ alembic upgrade head
|
||||
**AutoCoder Integration:** `AUTOCODER_INTEGRATION.md`
|
||||
|
||||
**Available Commands:**
|
||||
- `/sync` - Cross-machine context synchronization
|
||||
- `/create-spec` - Create app specification
|
||||
- `/checkpoint` - Create development checkpoint
|
||||
|
||||
@@ -447,5 +321,5 @@ alembic upgrade head
|
||||
|
||||
---
|
||||
|
||||
**Last Updated:** 2026-01-17 (AutoCoder resources integrated)
|
||||
**Project Progress:** 95% Complete (Phase 6 of 7 done)
|
||||
**Last Updated:** 2026-01-18 (Context system removed)
|
||||
**Project Progress:** Phase 5 Complete
|
||||
|
||||
364
.claude/commands/README.md
Normal file
364
.claude/commands/README.md
Normal file
@@ -0,0 +1,364 @@
|
||||
# Claude Code Commands
|
||||
|
||||
Custom commands that extend Claude Code's capabilities.
|
||||
|
||||
## Available Commands
|
||||
|
||||
### `/snapshot` - Quick Context Save
|
||||
|
||||
Save conversation context on-demand without requiring a git commit.
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
/snapshot
|
||||
/snapshot "Custom title"
|
||||
/snapshot --important
|
||||
/snapshot --offline
|
||||
```
|
||||
|
||||
**When to use:**
|
||||
- Save progress without committing code
|
||||
- Capture important discussions
|
||||
- Remember exploratory changes
|
||||
- Switching contexts/machines
|
||||
- Multiple times per hour
|
||||
|
||||
**Documentation:** `snapshot.md`
|
||||
**Quick Start:** `.claude/SNAPSHOT_QUICK_START.md`
|
||||
|
||||
---
|
||||
|
||||
### `/checkpoint` - Full Git + Context Save
|
||||
|
||||
Create git commit AND save context to database.
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
/checkpoint
|
||||
```
|
||||
|
||||
**When to use:**
|
||||
- Code is ready to commit
|
||||
- Reached stable milestone
|
||||
- Completed feature/fix
|
||||
- End of work session
|
||||
- Once or twice per feature
|
||||
|
||||
**Documentation:** `checkpoint.md`
|
||||
|
||||
---
|
||||
|
||||
### `/sync` - Cross-Machine Context Sync
|
||||
|
||||
Synchronize queued contexts across machines.
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
/sync
|
||||
```
|
||||
|
||||
**When to use:**
|
||||
- Manually trigger sync
|
||||
- After offline work
|
||||
- Before switching machines
|
||||
- Check queue status
|
||||
|
||||
**Documentation:** `sync.md`
|
||||
|
||||
---
|
||||
|
||||
### `/create-spec` - App Specification
|
||||
|
||||
Create comprehensive application specification for AutoCoder.
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
/create-spec
|
||||
```
|
||||
|
||||
**When to use:**
|
||||
- Starting new project
|
||||
- Documenting existing app
|
||||
- Preparing for AutoCoder
|
||||
- Architecture planning
|
||||
|
||||
**Documentation:** `create-spec.md`
|
||||
|
||||
---
|
||||
|
||||
## Command Comparison
|
||||
|
||||
| Command | Git Commit | Context Save | Speed | Use Case |
|
||||
|---------|-----------|-------------|-------|----------|
|
||||
| `/snapshot` | No | Yes | Fast | Save progress |
|
||||
| `/checkpoint` | Yes | Yes | Slower | Save code + context |
|
||||
| `/sync` | No | No | Fast | Sync contexts |
|
||||
| `/create-spec` | No | No | Medium | Create spec |
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### Daily Development
|
||||
|
||||
```
|
||||
Morning:
|
||||
- Start work
|
||||
- /snapshot Research phase
|
||||
|
||||
Mid-day:
|
||||
- Complete feature
|
||||
- /checkpoint
|
||||
|
||||
Afternoon:
|
||||
- More work
|
||||
- /snapshot Progress update
|
||||
|
||||
End of day:
|
||||
- /checkpoint
|
||||
- /sync
|
||||
```
|
||||
|
||||
### Research Heavy
|
||||
|
||||
```
|
||||
Research:
|
||||
- /snapshot multiple times
|
||||
- Capture decisions
|
||||
|
||||
Implementation:
|
||||
- /checkpoint for features
|
||||
- Link code to research
|
||||
```
|
||||
|
||||
### New Project
|
||||
|
||||
```
|
||||
Planning:
|
||||
- /create-spec
|
||||
- /snapshot Architecture decisions
|
||||
|
||||
Development:
|
||||
- /snapshot frequently
|
||||
- /checkpoint for milestones
|
||||
```
|
||||
|
||||
## Setup
|
||||
|
||||
**Required for context commands:**
|
||||
```bash
|
||||
bash scripts/setup-context-recall.sh
|
||||
```
|
||||
|
||||
This configures:
|
||||
- JWT authentication token
|
||||
- API endpoint URL
|
||||
- Project ID
|
||||
- Context recall settings
|
||||
|
||||
**Configuration file:** `.claude/context-recall-config.env`
|
||||
|
||||
## Documentation
|
||||
|
||||
**Quick References:**
|
||||
- `.claude/SNAPSHOT_QUICK_START.md` - Snapshot guide
|
||||
- `.claude/SNAPSHOT_VS_CHECKPOINT.md` - When to use which
|
||||
- `.claude/CONTEXT_RECALL_QUICK_START.md` - Context recall system
|
||||
|
||||
**Full Documentation:**
|
||||
- `snapshot.md` - Complete snapshot docs
|
||||
- `checkpoint.md` - Complete checkpoint docs
|
||||
- `sync.md` - Complete sync docs
|
||||
- `create-spec.md` - Complete spec creation docs
|
||||
|
||||
**Implementation:**
|
||||
- `SNAPSHOT_IMPLEMENTATION.md` - Technical details
|
||||
|
||||
## Testing
|
||||
|
||||
**Test snapshot:**
|
||||
```bash
|
||||
bash scripts/test-snapshot.sh
|
||||
```
|
||||
|
||||
**Test context recall:**
|
||||
```bash
|
||||
bash scripts/test-context-recall.sh
|
||||
```
|
||||
|
||||
**Test sync:**
|
||||
```bash
|
||||
bash .claude/hooks/sync-contexts
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**Commands not working:**
|
||||
```bash
|
||||
# Check configuration
|
||||
cat .claude/context-recall-config.env
|
||||
|
||||
# Verify executable
|
||||
ls -l .claude/commands/
|
||||
|
||||
# Make executable
|
||||
chmod +x .claude/commands/*
|
||||
```
|
||||
|
||||
**Context not saving:**
|
||||
```bash
|
||||
# Check API connection
|
||||
curl -I http://172.16.3.30:8001/api/health
|
||||
|
||||
# Regenerate token
|
||||
bash scripts/setup-context-recall.sh
|
||||
|
||||
# Check logs
|
||||
tail -f .claude/context-queue/sync.log
|
||||
```
|
||||
|
||||
**Project ID issues:**
|
||||
```bash
|
||||
# Set manually
|
||||
git config --local claude.projectid "$(uuidgen)"
|
||||
|
||||
# Verify
|
||||
git config --local claude.projectid
|
||||
```
|
||||
|
||||
## Adding Custom Commands
|
||||
|
||||
**Structure:**
|
||||
```
|
||||
.claude/commands/
|
||||
├── command-name # Executable bash script
|
||||
└── command-name.md # Documentation
|
||||
```
|
||||
|
||||
**Template:**
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# Command description
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Load configuration
|
||||
source .claude/context-recall-config.env
|
||||
|
||||
# Command logic here
|
||||
echo "Hello from custom command"
|
||||
```
|
||||
|
||||
**Make executable:**
|
||||
```bash
|
||||
chmod +x .claude/commands/command-name
|
||||
```
|
||||
|
||||
**Test:**
|
||||
```bash
|
||||
bash .claude/commands/command-name
|
||||
```
|
||||
|
||||
**Use in Claude Code:**
|
||||
```
|
||||
/command-name
|
||||
```
|
||||
|
||||
## Command Best Practices
|
||||
|
||||
**Snapshot:**
|
||||
- Use frequently (multiple per hour)
|
||||
- Descriptive titles
|
||||
- Don't over-snapshot (meaningful moments)
|
||||
- Tag auto-extraction works best with good context
|
||||
|
||||
**Checkpoint:**
|
||||
- Only checkpoint clean state
|
||||
- Good commit messages
|
||||
- Group related changes
|
||||
- Don't checkpoint too often
|
||||
|
||||
**Sync:**
|
||||
- Run before switching machines
|
||||
- Run after offline work
|
||||
- Check queue status periodically
|
||||
- Auto-syncs on most operations
|
||||
|
||||
**Create-spec:**
|
||||
- Run once per project
|
||||
- Update when architecture changes
|
||||
- Include all important details
|
||||
- Use for AutoCoder integration
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
**Snapshot with importance:**
|
||||
```bash
|
||||
/snapshot --important "Critical architecture decision"
|
||||
```
|
||||
|
||||
**Offline snapshot:**
|
||||
```bash
|
||||
/snapshot --offline "Working without network"
|
||||
```
|
||||
|
||||
**Checkpoint with message:**
|
||||
```bash
|
||||
/checkpoint
|
||||
# Follow prompts for commit message
|
||||
```
|
||||
|
||||
**Sync specific project:**
|
||||
```bash
|
||||
# Edit sync script to filter by project
|
||||
bash .claude/hooks/sync-contexts
|
||||
```
|
||||
|
||||
## Integration
|
||||
|
||||
**With Context Recall:**
|
||||
- Commands save to database
|
||||
- Automatic recall in future sessions
|
||||
- Cross-machine continuity
|
||||
- Searchable knowledge base
|
||||
|
||||
**With AutoCoder:**
|
||||
- `/create-spec` generates AutoCoder input
|
||||
- Commands track project state
|
||||
- Context feeds AutoCoder sessions
|
||||
- Complete audit trail
|
||||
|
||||
**With Git:**
|
||||
- `/checkpoint` creates commits
|
||||
- `/snapshot` preserves git state
|
||||
- No conflicts with git workflow
|
||||
- Clean separation of concerns
|
||||
|
||||
## Support
|
||||
|
||||
**Questions:**
|
||||
- Check documentation in this directory
|
||||
- See `.claude/CLAUDE.md` for project overview
|
||||
- Review test scripts for examples
|
||||
|
||||
**Issues:**
|
||||
- Verify configuration
|
||||
- Check API connectivity
|
||||
- Review error messages
|
||||
- Test with provided scripts
|
||||
|
||||
**Updates:**
|
||||
- Update via git pull
|
||||
- Regenerate config if needed
|
||||
- Test after updates
|
||||
- Check for breaking changes
|
||||
|
||||
---
|
||||
|
||||
**Quick command reference:**
|
||||
- `/snapshot` - Quick save (no commit)
|
||||
- `/checkpoint` - Full save (with commit)
|
||||
- `/sync` - Sync contexts
|
||||
- `/create-spec` - Create app spec
|
||||
|
||||
**Setup:** `bash scripts/setup-context-recall.sh`
|
||||
**Test:** `bash scripts/test-snapshot.sh`
|
||||
**Docs:** Read the `.md` file for each command
|
||||
@@ -1,226 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Periodic Context Save Hook
|
||||
# Runs as a background daemon to save context every 5 minutes of active time
|
||||
#
|
||||
# Usage: bash .claude/hooks/periodic-context-save start
|
||||
# bash .claude/hooks/periodic-context-save stop
|
||||
# bash .claude/hooks/periodic-context-save status
|
||||
#
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
CLAUDE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
PID_FILE="$CLAUDE_DIR/.periodic-save.pid"
|
||||
STATE_FILE="$CLAUDE_DIR/.periodic-save-state"
|
||||
CONFIG_FILE="$CLAUDE_DIR/context-recall-config.env"
|
||||
|
||||
# Load configuration
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Configuration
|
||||
SAVE_INTERVAL_SECONDS=300 # 5 minutes
|
||||
CHECK_INTERVAL_SECONDS=60 # Check every minute
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
|
||||
# Detect project ID
|
||||
detect_project_id() {
|
||||
# Try git config first
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "$PROJECT_ID"
|
||||
}
|
||||
|
||||
# Check if Claude Code is active (not idle)
|
||||
is_claude_active() {
|
||||
# Check if there are recent Claude Code processes or activity
|
||||
# This is a simple heuristic - can be improved
|
||||
|
||||
# On Windows with Git Bash, check for claude process
|
||||
if command -v tasklist.exe >/dev/null 2>&1; then
|
||||
tasklist.exe 2>/dev/null | grep -i claude >/dev/null 2>&1
|
||||
return $?
|
||||
fi
|
||||
|
||||
# Assume active if we can't detect
|
||||
return 0
|
||||
}
|
||||
|
||||
# Get active time from state file
|
||||
get_active_time() {
|
||||
if [ -f "$STATE_FILE" ]; then
|
||||
cat "$STATE_FILE" | grep "^active_seconds=" | cut -d'=' -f2
|
||||
else
|
||||
echo "0"
|
||||
fi
|
||||
}
|
||||
|
||||
# Update active time in state file
|
||||
update_active_time() {
|
||||
local active_seconds=$1
|
||||
echo "active_seconds=$active_seconds" > "$STATE_FILE"
|
||||
echo "last_update=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$STATE_FILE"
|
||||
}
|
||||
|
||||
# Save context to database
|
||||
save_periodic_context() {
|
||||
local project_id=$(detect_project_id)
|
||||
|
||||
# Generate context summary
|
||||
local title="Periodic Save - $(date +"%Y-%m-%d %H:%M")"
|
||||
local summary="Auto-saved context after 5 minutes of active work. Session in progress on project: ${project_id:-unknown}"
|
||||
|
||||
# Create JSON payload
|
||||
local payload=$(cat <<EOF
|
||||
{
|
||||
"context_type": "session_summary",
|
||||
"title": "$title",
|
||||
"dense_summary": "$summary",
|
||||
"relevance_score": 5.0,
|
||||
"tags": "[\"auto-save\", \"periodic\", \"active-session\"]"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# POST to API
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
curl -s -X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload" >/dev/null 2>&1
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "[$(date)] Context saved successfully" >&2
|
||||
else
|
||||
echo "[$(date)] Failed to save context" >&2
|
||||
fi
|
||||
else
|
||||
echo "[$(date)] No JWT token - cannot save context" >&2
|
||||
fi
|
||||
}
|
||||
|
||||
# Main monitoring loop
|
||||
monitor_loop() {
|
||||
local active_seconds=0
|
||||
|
||||
echo "[$(date)] Periodic context save daemon started (PID: $$)" >&2
|
||||
echo "[$(date)] Will save context every ${SAVE_INTERVAL_SECONDS}s of active time" >&2
|
||||
|
||||
while true; do
|
||||
# Check if Claude is active
|
||||
if is_claude_active; then
|
||||
# Increment active time
|
||||
active_seconds=$((active_seconds + CHECK_INTERVAL_SECONDS))
|
||||
update_active_time $active_seconds
|
||||
|
||||
# Check if we've reached the save interval
|
||||
if [ $active_seconds -ge $SAVE_INTERVAL_SECONDS ]; then
|
||||
echo "[$(date)] ${SAVE_INTERVAL_SECONDS}s of active time reached - saving context" >&2
|
||||
save_periodic_context
|
||||
|
||||
# Reset timer
|
||||
active_seconds=0
|
||||
update_active_time 0
|
||||
fi
|
||||
else
|
||||
echo "[$(date)] Claude Code inactive - not counting time" >&2
|
||||
fi
|
||||
|
||||
# Wait before next check
|
||||
sleep $CHECK_INTERVAL_SECONDS
|
||||
done
|
||||
}
|
||||
|
||||
# Start daemon
|
||||
start_daemon() {
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
local pid=$(cat "$PID_FILE")
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
echo "Periodic context save daemon already running (PID: $pid)"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Start in background
|
||||
nohup bash "$0" _monitor >> "$CLAUDE_DIR/periodic-save.log" 2>&1 &
|
||||
local pid=$!
|
||||
echo $pid > "$PID_FILE"
|
||||
|
||||
echo "Started periodic context save daemon (PID: $pid)"
|
||||
echo "Logs: $CLAUDE_DIR/periodic-save.log"
|
||||
}
|
||||
|
||||
# Stop daemon
|
||||
stop_daemon() {
|
||||
if [ ! -f "$PID_FILE" ]; then
|
||||
echo "Periodic context save daemon not running"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local pid=$(cat "$PID_FILE")
|
||||
if kill $pid 2>/dev/null; then
|
||||
echo "Stopped periodic context save daemon (PID: $pid)"
|
||||
rm -f "$PID_FILE"
|
||||
rm -f "$STATE_FILE"
|
||||
else
|
||||
echo "Failed to stop daemon (PID: $pid) - may not be running"
|
||||
rm -f "$PID_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check status
|
||||
check_status() {
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
local pid=$(cat "$PID_FILE")
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
local active_seconds=$(get_active_time)
|
||||
echo "Periodic context save daemon is running (PID: $pid)"
|
||||
echo "Active time: ${active_seconds}s / ${SAVE_INTERVAL_SECONDS}s"
|
||||
return 0
|
||||
else
|
||||
echo "Daemon PID file exists but process not running"
|
||||
rm -f "$PID_FILE"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "Periodic context save daemon not running"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Command dispatcher
|
||||
case "$1" in
|
||||
start)
|
||||
start_daemon
|
||||
;;
|
||||
stop)
|
||||
stop_daemon
|
||||
;;
|
||||
status)
|
||||
check_status
|
||||
;;
|
||||
_monitor)
|
||||
# Internal command - run monitor loop
|
||||
monitor_loop
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|status}"
|
||||
echo ""
|
||||
echo "Periodic context save daemon - saves context every 5 minutes of active time"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " start - Start the background daemon"
|
||||
echo " stop - Stop the daemon"
|
||||
echo " status - Check daemon status"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -1,429 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Periodic Context Save Daemon
|
||||
|
||||
Monitors Claude Code activity and saves context every 5 minutes of active time.
|
||||
Runs as a background process that tracks when Claude is actively working.
|
||||
|
||||
Usage:
|
||||
python .claude/hooks/periodic_context_save.py start
|
||||
python .claude/hooks/periodic_context_save.py stop
|
||||
python .claude/hooks/periodic_context_save.py status
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
import signal
|
||||
import subprocess
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
# FIX BUG #1: Set UTF-8 encoding for stdout/stderr on Windows
|
||||
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
||||
|
||||
import requests
|
||||
|
||||
# Configuration
|
||||
SCRIPT_DIR = Path(__file__).parent
|
||||
CLAUDE_DIR = SCRIPT_DIR.parent
|
||||
PID_FILE = CLAUDE_DIR / ".periodic-save.pid"
|
||||
STATE_FILE = CLAUDE_DIR / ".periodic-save-state.json"
|
||||
LOG_FILE = CLAUDE_DIR / "periodic-save.log"
|
||||
CONFIG_FILE = CLAUDE_DIR / "context-recall-config.env"
|
||||
|
||||
SAVE_INTERVAL_SECONDS = 300 # 5 minutes
|
||||
CHECK_INTERVAL_SECONDS = 60 # Check every minute
|
||||
|
||||
|
||||
def log(message):
|
||||
"""Write log message to file and stderr (encoding-safe)"""
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
log_message = f"[{timestamp}] {message}\n"
|
||||
|
||||
# Write to log file with UTF-8 encoding to handle Unicode characters
|
||||
try:
|
||||
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
||||
f.write(log_message)
|
||||
except Exception:
|
||||
pass # Silent fail on log file write errors
|
||||
|
||||
# FIX BUG #5: Safe stderr printing (handles encoding errors)
|
||||
try:
|
||||
print(log_message.strip(), file=sys.stderr)
|
||||
except UnicodeEncodeError:
|
||||
# Fallback: encode with error handling
|
||||
safe_message = log_message.encode('ascii', errors='replace').decode('ascii')
|
||||
print(safe_message.strip(), file=sys.stderr)
|
||||
|
||||
|
||||
def load_config():
|
||||
"""Load configuration from context-recall-config.env"""
|
||||
config = {
|
||||
"api_url": "http://172.16.3.30:8001",
|
||||
"jwt_token": None,
|
||||
"project_id": None, # FIX BUG #2: Add project_id to config
|
||||
}
|
||||
|
||||
if CONFIG_FILE.exists():
|
||||
with open(CONFIG_FILE) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line.startswith("CLAUDE_API_URL=") or line.startswith("API_BASE_URL="):
|
||||
config["api_url"] = line.split("=", 1)[1]
|
||||
elif line.startswith("JWT_TOKEN="):
|
||||
config["jwt_token"] = line.split("=", 1)[1]
|
||||
elif line.startswith("CLAUDE_PROJECT_ID="):
|
||||
config["project_id"] = line.split("=", 1)[1]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def detect_project_id():
|
||||
"""Detect project ID from git config"""
|
||||
try:
|
||||
# Try git config first
|
||||
result = subprocess.run(
|
||||
["git", "config", "--local", "claude.projectid"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
return result.stdout.strip()
|
||||
|
||||
# Try to derive from git remote URL
|
||||
result = subprocess.run(
|
||||
["git", "config", "--get", "remote.origin.url"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
import hashlib
|
||||
return hashlib.md5(result.stdout.strip().encode()).hexdigest()
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def is_claude_active():
|
||||
"""
|
||||
Check if Claude Code is actively running.
|
||||
|
||||
Returns True if:
|
||||
- Claude Code process is running
|
||||
- Recent file modifications in project directory
|
||||
- Not waiting for user input (heuristic)
|
||||
"""
|
||||
try:
|
||||
# Check for Claude process on Windows
|
||||
if sys.platform == "win32":
|
||||
result = subprocess.run(
|
||||
["tasklist.exe"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if "claude" in result.stdout.lower() or "node" in result.stdout.lower():
|
||||
return True
|
||||
|
||||
# Check for recent file modifications (within last 2 minutes)
|
||||
cwd = Path.cwd()
|
||||
two_minutes_ago = time.time() - 120
|
||||
|
||||
for file in cwd.rglob("*"):
|
||||
if file.is_file() and file.stat().st_mtime > two_minutes_ago:
|
||||
# Recent activity detected
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
log(f"Error checking activity: {e}")
|
||||
|
||||
# Default to inactive if we can't detect
|
||||
return False
|
||||
|
||||
|
||||
def load_state():
|
||||
"""Load state from state file"""
|
||||
if STATE_FILE.exists():
|
||||
try:
|
||||
with open(STATE_FILE) as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
"active_seconds": 0,
|
||||
"last_update": None,
|
||||
"last_save": None,
|
||||
}
|
||||
|
||||
|
||||
def save_state(state):
|
||||
"""Save state to state file"""
|
||||
state["last_update"] = datetime.now(timezone.utc).isoformat()
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump(state, f, indent=2)
|
||||
|
||||
|
||||
def save_periodic_context(config, project_id):
|
||||
"""Save context to database via API"""
|
||||
# FIX BUG #7: Validate before attempting save
|
||||
if not config["jwt_token"]:
|
||||
log("[ERROR] No JWT token - cannot save context")
|
||||
return False
|
||||
|
||||
if not project_id:
|
||||
log("[ERROR] No project_id - cannot save context")
|
||||
return False
|
||||
|
||||
title = f"Periodic Save - {datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
||||
summary = f"Auto-saved context after 5 minutes of active work. Session in progress on project: {project_id}"
|
||||
|
||||
# FIX BUG #2: Include project_id in payload
|
||||
payload = {
|
||||
"project_id": project_id,
|
||||
"context_type": "session_summary",
|
||||
"title": title,
|
||||
"dense_summary": summary,
|
||||
"relevance_score": 5.0,
|
||||
"tags": json.dumps(["auto-save", "periodic", "active-session"]),
|
||||
}
|
||||
|
||||
try:
|
||||
url = f"{config['api_url']}/api/conversation-contexts"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {config['jwt_token']}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
context_id = response.json().get('id', 'unknown')
|
||||
log(f"[SUCCESS] Context saved (ID: {context_id}, Project: {project_id})")
|
||||
return True
|
||||
else:
|
||||
# FIX BUG #4: Improved error logging with full details
|
||||
error_detail = response.text[:200] if response.text else "No error detail"
|
||||
log(f"[ERROR] Failed to save context: HTTP {response.status_code}")
|
||||
log(f"[ERROR] Response: {error_detail}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
# FIX BUG #4: More detailed error logging
|
||||
log(f"[ERROR] Exception saving context: {type(e).__name__}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def monitor_loop():
|
||||
"""Main monitoring loop"""
|
||||
log("Periodic context save daemon started")
|
||||
log(f"Will save context every {SAVE_INTERVAL_SECONDS}s of active time")
|
||||
|
||||
config = load_config()
|
||||
state = load_state()
|
||||
|
||||
# FIX BUG #7: Validate configuration on startup
|
||||
if not config["jwt_token"]:
|
||||
log("[WARNING] No JWT token found in config - saves will fail")
|
||||
|
||||
# Determine project_id (config takes precedence over git detection)
|
||||
project_id = config["project_id"]
|
||||
if not project_id:
|
||||
project_id = detect_project_id()
|
||||
if project_id:
|
||||
log(f"[INFO] Detected project_id from git: {project_id}")
|
||||
else:
|
||||
log("[WARNING] No project_id found - saves will fail")
|
||||
|
||||
# Reset state on startup
|
||||
state["active_seconds"] = 0
|
||||
save_state(state)
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Check if Claude is active
|
||||
if is_claude_active():
|
||||
# Increment active time
|
||||
state["active_seconds"] += CHECK_INTERVAL_SECONDS
|
||||
save_state(state)
|
||||
|
||||
log(f"Active: {state['active_seconds']}s / {SAVE_INTERVAL_SECONDS}s")
|
||||
|
||||
# Check if we've reached the save interval
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
log(f"{SAVE_INTERVAL_SECONDS}s of active time reached - saving context")
|
||||
|
||||
# Try to save context
|
||||
save_success = save_periodic_context(config, project_id)
|
||||
|
||||
if save_success:
|
||||
state["last_save"] = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# FIX BUG #3: Always reset timer in finally block (see below)
|
||||
|
||||
else:
|
||||
log("Claude Code inactive - not counting time")
|
||||
|
||||
# Wait before next check
|
||||
time.sleep(CHECK_INTERVAL_SECONDS)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
log("Daemon stopped by user")
|
||||
break
|
||||
except Exception as e:
|
||||
# FIX BUG #4: Better exception logging
|
||||
log(f"[ERROR] Exception in monitor loop: {type(e).__name__}: {e}")
|
||||
time.sleep(CHECK_INTERVAL_SECONDS)
|
||||
finally:
|
||||
# FIX BUG #3: Reset counter in finally block to prevent infinite save attempts
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
state["active_seconds"] = 0
|
||||
save_state(state)
|
||||
|
||||
|
||||
def start_daemon():
|
||||
"""Start the daemon as a background process"""
|
||||
if PID_FILE.exists():
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
# Check if process is running
|
||||
try:
|
||||
os.kill(pid, 0) # Signal 0 checks if process exists
|
||||
print(f"Periodic context save daemon already running (PID: {pid})")
|
||||
return 1
|
||||
except OSError:
|
||||
# Process not running, remove stale PID file
|
||||
PID_FILE.unlink()
|
||||
|
||||
# Start daemon process
|
||||
if sys.platform == "win32":
|
||||
# On Windows, use subprocess.Popen with DETACHED_PROCESS
|
||||
import subprocess
|
||||
CREATE_NO_WINDOW = 0x08000000
|
||||
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, __file__, "_monitor"],
|
||||
creationflags=subprocess.DETACHED_PROCESS | CREATE_NO_WINDOW,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
else:
|
||||
# On Unix, fork
|
||||
import subprocess
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, __file__, "_monitor"],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
# Save PID
|
||||
with open(PID_FILE, "w") as f:
|
||||
f.write(str(process.pid))
|
||||
|
||||
print(f"Started periodic context save daemon (PID: {process.pid})")
|
||||
print(f"Logs: {LOG_FILE}")
|
||||
return 0
|
||||
|
||||
|
||||
def stop_daemon():
|
||||
"""Stop the daemon"""
|
||||
if not PID_FILE.exists():
|
||||
print("Periodic context save daemon not running")
|
||||
return 1
|
||||
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
try:
|
||||
if sys.platform == "win32":
|
||||
# On Windows, use taskkill
|
||||
subprocess.run(["taskkill", "/F", "/PID", str(pid)], check=True, timeout=10) # Prevent hung processes
|
||||
else:
|
||||
# On Unix, use kill
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
|
||||
print(f"Stopped periodic context save daemon (PID: {pid})")
|
||||
PID_FILE.unlink()
|
||||
|
||||
if STATE_FILE.exists():
|
||||
STATE_FILE.unlink()
|
||||
|
||||
return 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to stop daemon (PID: {pid}): {e}")
|
||||
PID_FILE.unlink()
|
||||
return 1
|
||||
|
||||
|
||||
def check_status():
|
||||
"""Check daemon status"""
|
||||
if not PID_FILE.exists():
|
||||
print("Periodic context save daemon not running")
|
||||
return 1
|
||||
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
# Check if process is running
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
print("Daemon PID file exists but process not running")
|
||||
PID_FILE.unlink()
|
||||
return 1
|
||||
|
||||
state = load_state()
|
||||
active_seconds = state.get("active_seconds", 0)
|
||||
|
||||
print(f"Periodic context save daemon is running (PID: {pid})")
|
||||
print(f"Active time: {active_seconds}s / {SAVE_INTERVAL_SECONDS}s")
|
||||
|
||||
if state.get("last_save"):
|
||||
print(f"Last save: {state['last_save']}")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point"""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python periodic_context_save.py {start|stop|status}")
|
||||
print()
|
||||
print("Periodic context save daemon - saves context every 5 minutes of active time")
|
||||
print()
|
||||
print("Commands:")
|
||||
print(" start - Start the background daemon")
|
||||
print(" stop - Stop the daemon")
|
||||
print(" status - Check daemon status")
|
||||
return 1
|
||||
|
||||
command = sys.argv[1]
|
||||
|
||||
if command == "start":
|
||||
return start_daemon()
|
||||
elif command == "stop":
|
||||
return stop_daemon()
|
||||
elif command == "status":
|
||||
return check_status()
|
||||
elif command == "_monitor":
|
||||
# Internal command - run monitor loop
|
||||
monitor_loop()
|
||||
return 0
|
||||
else:
|
||||
print(f"Unknown command: {command}")
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -1,315 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Periodic Context Save - Windows Task Scheduler Version
|
||||
|
||||
This script is designed to be called every minute by Windows Task Scheduler.
|
||||
It tracks active time and saves context every 5 minutes of activity.
|
||||
|
||||
Usage:
|
||||
Schedule this to run every minute via Task Scheduler:
|
||||
python .claude/hooks/periodic_save_check.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import subprocess
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
# FIX BUG #1: Set UTF-8 encoding for stdout/stderr on Windows
|
||||
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
||||
|
||||
import requests
|
||||
|
||||
# Configuration
|
||||
SCRIPT_DIR = Path(__file__).parent
|
||||
CLAUDE_DIR = SCRIPT_DIR.parent
|
||||
PROJECT_ROOT = CLAUDE_DIR.parent
|
||||
STATE_FILE = CLAUDE_DIR / ".periodic-save-state.json"
|
||||
LOG_FILE = CLAUDE_DIR / "periodic-save.log"
|
||||
CONFIG_FILE = CLAUDE_DIR / "context-recall-config.env"
|
||||
LOCK_FILE = CLAUDE_DIR / ".periodic-save.lock" # Mutex lock to prevent overlaps
|
||||
|
||||
SAVE_INTERVAL_SECONDS = 300 # 5 minutes
|
||||
|
||||
|
||||
def log(message):
|
||||
"""Write log message (encoding-safe)"""
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
log_message = f"[{timestamp}] {message}\n"
|
||||
|
||||
try:
|
||||
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
||||
f.write(log_message)
|
||||
except Exception:
|
||||
pass # Silent fail if can't write log
|
||||
|
||||
# FIX BUG #5: Safe stderr printing (handles encoding errors)
|
||||
try:
|
||||
print(log_message.strip(), file=sys.stderr)
|
||||
except UnicodeEncodeError:
|
||||
# Fallback: encode with error handling
|
||||
safe_message = log_message.encode('ascii', errors='replace').decode('ascii')
|
||||
print(safe_message.strip(), file=sys.stderr)
|
||||
|
||||
|
||||
def load_config():
|
||||
"""Load configuration from context-recall-config.env"""
|
||||
config = {
|
||||
"api_url": "http://172.16.3.30:8001",
|
||||
"jwt_token": None,
|
||||
"project_id": None, # FIX BUG #2: Add project_id to config
|
||||
}
|
||||
|
||||
if CONFIG_FILE.exists():
|
||||
with open(CONFIG_FILE) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line.startswith("CLAUDE_API_URL=") or line.startswith("API_BASE_URL="):
|
||||
config["api_url"] = line.split("=", 1)[1]
|
||||
elif line.startswith("JWT_TOKEN="):
|
||||
config["jwt_token"] = line.split("=", 1)[1]
|
||||
elif line.startswith("CLAUDE_PROJECT_ID="):
|
||||
config["project_id"] = line.split("=", 1)[1]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def detect_project_id():
|
||||
"""Detect project ID from git config"""
|
||||
try:
|
||||
os.chdir(PROJECT_ROOT)
|
||||
|
||||
# Try git config first
|
||||
result = subprocess.run(
|
||||
["git", "config", "--local", "claude.projectid"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
cwd=PROJECT_ROOT,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
return result.stdout.strip()
|
||||
|
||||
# Try to derive from git remote URL
|
||||
result = subprocess.run(
|
||||
["git", "config", "--get", "remote.origin.url"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
cwd=PROJECT_ROOT,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
import hashlib
|
||||
return hashlib.md5(result.stdout.strip().encode()).hexdigest()
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def is_claude_active():
|
||||
"""Check if Claude Code is actively running"""
|
||||
try:
|
||||
# Check for Claude Code process
|
||||
result = subprocess.run(
|
||||
["tasklist.exe"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
|
||||
# Look for claude, node, or other indicators
|
||||
output_lower = result.stdout.lower()
|
||||
if any(proc in output_lower for proc in ["claude", "node.exe", "code.exe"]):
|
||||
# Also check for recent file modifications
|
||||
import time
|
||||
two_minutes_ago = time.time() - 120
|
||||
|
||||
# Check a few common directories for recent activity
|
||||
for check_dir in [PROJECT_ROOT, PROJECT_ROOT / "api", PROJECT_ROOT / ".claude"]:
|
||||
if check_dir.exists():
|
||||
for file in check_dir.rglob("*"):
|
||||
if file.is_file():
|
||||
try:
|
||||
if file.stat().st_mtime > two_minutes_ago:
|
||||
return True
|
||||
except:
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
log(f"Error checking activity: {e}")
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def acquire_lock():
|
||||
"""Acquire execution lock to prevent overlapping runs"""
|
||||
try:
|
||||
# Check if lock file exists and is recent (< 60 seconds old)
|
||||
if LOCK_FILE.exists():
|
||||
lock_age = datetime.now().timestamp() - LOCK_FILE.stat().st_mtime
|
||||
if lock_age < 60: # Lock is fresh, another instance is running
|
||||
log("[INFO] Another instance is running, skipping")
|
||||
return False
|
||||
|
||||
# Create/update lock file
|
||||
LOCK_FILE.touch()
|
||||
return True
|
||||
except Exception as e:
|
||||
log(f"[WARNING] Lock acquisition failed: {e}")
|
||||
return True # Proceed anyway if lock fails
|
||||
|
||||
|
||||
def release_lock():
|
||||
"""Release execution lock"""
|
||||
try:
|
||||
if LOCK_FILE.exists():
|
||||
LOCK_FILE.unlink()
|
||||
except Exception:
|
||||
pass # Ignore errors on cleanup
|
||||
|
||||
|
||||
def load_state():
|
||||
"""Load state from state file"""
|
||||
if STATE_FILE.exists():
|
||||
try:
|
||||
with open(STATE_FILE) as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
"active_seconds": 0,
|
||||
"last_check": None,
|
||||
"last_save": None,
|
||||
}
|
||||
|
||||
|
||||
def save_state(state):
|
||||
"""Save state to state file"""
|
||||
state["last_check"] = datetime.now(timezone.utc).isoformat()
|
||||
try:
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump(state, f, indent=2)
|
||||
except:
|
||||
pass # Silent fail
|
||||
|
||||
|
||||
def save_periodic_context(config, project_id):
|
||||
"""Save context to database via API"""
|
||||
# FIX BUG #7: Validate before attempting save
|
||||
if not config["jwt_token"]:
|
||||
log("[ERROR] No JWT token - cannot save context")
|
||||
return False
|
||||
|
||||
if not project_id:
|
||||
log("[ERROR] No project_id - cannot save context")
|
||||
return False
|
||||
|
||||
title = f"Periodic Save - {datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
||||
summary = f"Auto-saved context after {SAVE_INTERVAL_SECONDS // 60} minutes of active work. Session in progress on project: {project_id}"
|
||||
|
||||
# FIX BUG #2: Include project_id in payload
|
||||
payload = {
|
||||
"project_id": project_id,
|
||||
"context_type": "session_summary",
|
||||
"title": title,
|
||||
"dense_summary": summary,
|
||||
"relevance_score": 5.0,
|
||||
"tags": json.dumps(["auto-save", "periodic", "active-session", project_id]),
|
||||
}
|
||||
|
||||
try:
|
||||
url = f"{config['api_url']}/api/conversation-contexts"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {config['jwt_token']}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
context_id = response.json().get('id', 'unknown')
|
||||
log(f"[SUCCESS] Context saved (ID: {context_id}, Active time: {SAVE_INTERVAL_SECONDS}s)")
|
||||
return True
|
||||
else:
|
||||
# FIX BUG #4: Improved error logging with full details
|
||||
error_detail = response.text[:200] if response.text else "No error detail"
|
||||
log(f"[ERROR] Failed to save: HTTP {response.status_code}")
|
||||
log(f"[ERROR] Response: {error_detail}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
# FIX BUG #4: More detailed error logging
|
||||
log(f"[ERROR] Exception saving context: {type(e).__name__}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point - called every minute by Task Scheduler"""
|
||||
# Acquire lock to prevent overlapping executions
|
||||
if not acquire_lock():
|
||||
return 0 # Another instance is running, exit gracefully
|
||||
|
||||
try:
|
||||
config = load_config()
|
||||
state = load_state()
|
||||
|
||||
# FIX BUG #7: Validate configuration
|
||||
if not config["jwt_token"]:
|
||||
log("[WARNING] No JWT token found in config")
|
||||
|
||||
# Determine project_id (config takes precedence over git detection)
|
||||
project_id = config["project_id"]
|
||||
if not project_id:
|
||||
project_id = detect_project_id()
|
||||
if not project_id:
|
||||
log("[WARNING] No project_id found")
|
||||
|
||||
# Check if Claude is active
|
||||
if is_claude_active():
|
||||
# Increment active time (60 seconds per check)
|
||||
state["active_seconds"] += 60
|
||||
|
||||
# Check if we've reached the save interval
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
log(f"{SAVE_INTERVAL_SECONDS}s active time reached - saving context")
|
||||
|
||||
save_success = save_periodic_context(config, project_id)
|
||||
|
||||
if save_success:
|
||||
state["last_save"] = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# FIX BUG #3: Always reset counter in finally block (see below)
|
||||
|
||||
save_state(state)
|
||||
else:
|
||||
# Not active - don't increment timer but save state
|
||||
save_state(state)
|
||||
|
||||
return 0
|
||||
except Exception as e:
|
||||
# FIX BUG #4: Better exception logging
|
||||
log(f"[ERROR] Fatal error: {type(e).__name__}: {e}")
|
||||
return 1
|
||||
finally:
|
||||
# FIX BUG #3: Reset counter in finally block to prevent infinite save attempts
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
state["active_seconds"] = 0
|
||||
save_state(state)
|
||||
# Always release lock, even if error occurs
|
||||
release_lock()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
sys.exit(main())
|
||||
except Exception as e:
|
||||
log(f"Fatal error: {e}")
|
||||
sys.exit(1)
|
||||
@@ -1,11 +0,0 @@
|
||||
@echo off
|
||||
REM Windows wrapper for periodic context save
|
||||
REM Can be run from Task Scheduler every minute
|
||||
|
||||
cd /d D:\ClaudeTools
|
||||
|
||||
REM Run the check-and-save script
|
||||
python .claude\hooks\periodic_save_check.py
|
||||
|
||||
REM Exit silently
|
||||
exit /b 0
|
||||
@@ -1,69 +0,0 @@
|
||||
# Setup Periodic Context Save - Windows Task Scheduler
|
||||
# This script creates a scheduled task to run periodic_save_check.py every minute
|
||||
# Uses pythonw.exe to run without console window
|
||||
|
||||
$TaskName = "ClaudeTools - Periodic Context Save"
|
||||
$ScriptPath = "D:\ClaudeTools\.claude\hooks\periodic_save_check.py"
|
||||
$WorkingDir = "D:\ClaudeTools"
|
||||
|
||||
# Use pythonw.exe instead of python.exe to run without console window
|
||||
$PythonExe = (Get-Command python).Source
|
||||
$PythonDir = Split-Path $PythonExe -Parent
|
||||
$PythonwPath = Join-Path $PythonDir "pythonw.exe"
|
||||
|
||||
# Fallback to python.exe if pythonw.exe doesn't exist (shouldn't happen)
|
||||
if (-not (Test-Path $PythonwPath)) {
|
||||
Write-Warning "pythonw.exe not found at $PythonwPath, falling back to python.exe"
|
||||
$PythonwPath = $PythonExe
|
||||
}
|
||||
|
||||
# Check if task already exists
|
||||
$ExistingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
|
||||
if ($ExistingTask) {
|
||||
Write-Host "Task '$TaskName' already exists. Removing old task..."
|
||||
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
||||
}
|
||||
|
||||
# Create action to run Python script with pythonw.exe (no console window)
|
||||
$Action = New-ScheduledTaskAction -Execute $PythonwPath `
|
||||
-Argument $ScriptPath `
|
||||
-WorkingDirectory $WorkingDir
|
||||
|
||||
# Create trigger to run every 5 minutes (indefinitely) - Reduced from 1min to prevent zombie accumulation
|
||||
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5)
|
||||
|
||||
# Create settings - Hidden and DisallowStartIfOnBatteries set to false
|
||||
$Settings = New-ScheduledTaskSettingsSet `
|
||||
-AllowStartIfOnBatteries `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-StartWhenAvailable `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes 5) `
|
||||
-Hidden
|
||||
|
||||
# Create principal (run as current user, no window)
|
||||
$Principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U
|
||||
|
||||
# Register the task
|
||||
Register-ScheduledTask -TaskName $TaskName `
|
||||
-Action $Action `
|
||||
-Trigger $Trigger `
|
||||
-Settings $Settings `
|
||||
-Principal $Principal `
|
||||
-Description "Automatically saves Claude Code context every 5 minutes of active work"
|
||||
|
||||
Write-Host "[SUCCESS] Scheduled task created successfully!"
|
||||
Write-Host ""
|
||||
Write-Host "Task Name: $TaskName"
|
||||
Write-Host "Runs: Every 5 minutes (HIDDEN - no console window)"
|
||||
Write-Host "Action: Checks activity and saves context every 5 minutes"
|
||||
Write-Host "Executable: $PythonwPath (pythonw.exe = no window)"
|
||||
Write-Host ""
|
||||
Write-Host "To verify task is hidden:"
|
||||
Write-Host " Get-ScheduledTask -TaskName '$TaskName' | Select-Object -ExpandProperty Settings"
|
||||
Write-Host ""
|
||||
Write-Host "To remove:"
|
||||
Write-Host " Unregister-ScheduledTask -TaskName '$TaskName' -Confirm:`$false"
|
||||
Write-Host ""
|
||||
Write-Host "View logs:"
|
||||
Write-Host ' Get-Content D:\ClaudeTools\.claude\periodic-save.log -Tail 20'
|
||||
@@ -1,110 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Sync Queued Contexts to Database
|
||||
# Uploads any locally queued contexts to the central API
|
||||
# Can be run manually or called automatically by hooks
|
||||
#
|
||||
# Usage: bash .claude/hooks/sync-contexts
|
||||
#
|
||||
|
||||
# Load configuration
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
CONFIG_FILE="$CLAUDE_DIR/context-recall-config.env"
|
||||
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
PENDING_DIR="$QUEUE_DIR/pending"
|
||||
UPLOADED_DIR="$QUEUE_DIR/uploaded"
|
||||
FAILED_DIR="$QUEUE_DIR/failed"
|
||||
|
||||
# Exit if no JWT token
|
||||
if [ -z "$JWT_TOKEN" ]; then
|
||||
echo "ERROR: No JWT token available" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create directories if they don't exist
|
||||
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" "$FAILED_DIR" 2>/dev/null
|
||||
|
||||
# Check if there are any pending files
|
||||
PENDING_COUNT=$(find "$PENDING_DIR" -type f -name "*.json" 2>/dev/null | wc -l)
|
||||
|
||||
if [ "$PENDING_COUNT" -eq 0 ]; then
|
||||
# No pending contexts to sync
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "==================================="
|
||||
echo "Syncing Queued Contexts"
|
||||
echo "==================================="
|
||||
echo "Found $PENDING_COUNT pending context(s)"
|
||||
echo ""
|
||||
|
||||
# Process each pending file
|
||||
SUCCESS_COUNT=0
|
||||
FAIL_COUNT=0
|
||||
|
||||
for QUEUE_FILE in "$PENDING_DIR"/*.json; do
|
||||
# Skip if no files match
|
||||
[ -e "$QUEUE_FILE" ] || continue
|
||||
|
||||
FILENAME=$(basename "$QUEUE_FILE")
|
||||
echo "Processing: $FILENAME"
|
||||
|
||||
# Read the payload
|
||||
PAYLOAD=$(cat "$QUEUE_FILE")
|
||||
|
||||
# Determine endpoint based on filename
|
||||
if [[ "$FILENAME" == *"_state.json" ]]; then
|
||||
ENDPOINT="${API_URL}/api/project-states"
|
||||
else
|
||||
ENDPOINT="${API_URL}/api/conversation-contexts"
|
||||
fi
|
||||
|
||||
# Try to POST to API
|
||||
RESPONSE=$(curl -s --max-time 10 -w "\n%{http_code}" \
|
||||
-X POST "$ENDPOINT" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PAYLOAD" 2>/dev/null)
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
|
||||
# Success - move to uploaded directory
|
||||
mv "$QUEUE_FILE" "$UPLOADED_DIR/"
|
||||
echo " [OK] Uploaded successfully"
|
||||
((SUCCESS_COUNT++))
|
||||
else
|
||||
# Failed - move to failed directory for manual review
|
||||
mv "$QUEUE_FILE" "$FAILED_DIR/"
|
||||
echo " [ERROR] Upload failed (HTTP $HTTP_CODE) - moved to failed/"
|
||||
((FAIL_COUNT++))
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "==================================="
|
||||
echo "Sync Complete"
|
||||
echo "==================================="
|
||||
echo "Successful: $SUCCESS_COUNT"
|
||||
echo "Failed: $FAIL_COUNT"
|
||||
echo ""
|
||||
|
||||
# Clean up old uploaded files (keep last 100)
|
||||
UPLOADED_COUNT=$(find "$UPLOADED_DIR" -type f -name "*.json" 2>/dev/null | wc -l)
|
||||
if [ "$UPLOADED_COUNT" -gt 100 ]; then
|
||||
echo "Cleaning up old uploaded contexts (keeping last 100)..."
|
||||
find "$UPLOADED_DIR" -type f -name "*.json" -printf '%T@ %p\n' | \
|
||||
sort -n | \
|
||||
head -n -100 | \
|
||||
cut -d' ' -f2- | \
|
||||
xargs rm -f
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,182 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: task-complete (v2 - with offline support)
|
||||
# Runs AFTER a task is completed
|
||||
# Saves conversation context to the database for future recall
|
||||
# FALLBACK: Queues locally when API is unavailable, syncs later
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# TASK_SUMMARY - Summary of completed task (auto-generated by Claude)
|
||||
# TASK_FILES - Files modified during task (comma-separated)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
PENDING_DIR="$QUEUE_DIR/pending"
|
||||
UPLOADED_DIR="$QUEUE_DIR/uploaded"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID (same logic as user-prompt-submit)
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create queue directories if they don't exist
|
||||
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" 2>/dev/null
|
||||
|
||||
# Gather task information
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
TIMESTAMP_FILENAME=$(date -u +"%Y%m%d_%H%M%S")
|
||||
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
|
||||
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
|
||||
# Get recent git changes
|
||||
CHANGED_FILES=$(git diff --name-only HEAD~1 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
CHANGED_FILES="${TASK_FILES:-}"
|
||||
fi
|
||||
|
||||
# Create task summary
|
||||
if [ -z "$TASK_SUMMARY" ]; then
|
||||
# Generate basic summary from git log if no summary provided
|
||||
TASK_SUMMARY=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Task completed")
|
||||
fi
|
||||
|
||||
# Build context payload
|
||||
CONTEXT_TITLE="Session: ${TIMESTAMP}"
|
||||
CONTEXT_TYPE="session_summary"
|
||||
RELEVANCE_SCORE=7.0
|
||||
|
||||
# Create dense summary
|
||||
DENSE_SUMMARY="Task completed on branch '${GIT_BRANCH}' (commit: ${GIT_COMMIT}).
|
||||
|
||||
Summary: ${TASK_SUMMARY}
|
||||
|
||||
Modified files: ${CHANGED_FILES:-none}
|
||||
|
||||
Timestamp: ${TIMESTAMP}"
|
||||
|
||||
# Escape JSON strings
|
||||
escape_json() {
|
||||
echo "$1" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read())[1:-1])"
|
||||
}
|
||||
|
||||
ESCAPED_TITLE=$(escape_json "$CONTEXT_TITLE")
|
||||
ESCAPED_SUMMARY=$(escape_json "$DENSE_SUMMARY")
|
||||
|
||||
# Save context to database
|
||||
CONTEXT_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"context_type": "${CONTEXT_TYPE}",
|
||||
"title": ${ESCAPED_TITLE},
|
||||
"dense_summary": ${ESCAPED_SUMMARY},
|
||||
"relevance_score": ${RELEVANCE_SCORE},
|
||||
"metadata": {
|
||||
"git_branch": "${GIT_BRANCH}",
|
||||
"git_commit": "${GIT_COMMIT}",
|
||||
"files_modified": "${CHANGED_FILES}",
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Update project state
|
||||
PROJECT_STATE_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"state_data": {
|
||||
"last_task_completion": "${TIMESTAMP}",
|
||||
"last_git_commit": "${GIT_COMMIT}",
|
||||
"last_git_branch": "${GIT_BRANCH}",
|
||||
"recent_files": "${CHANGED_FILES}"
|
||||
},
|
||||
"state_type": "task_completion"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Try to POST to API if we have a JWT token
|
||||
API_SUCCESS=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
RESPONSE=$(curl -s --max-time 5 -w "\n%{http_code}" \
|
||||
-X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$CONTEXT_PAYLOAD" 2>/dev/null)
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
|
||||
API_SUCCESS=true
|
||||
|
||||
# Also update project state
|
||||
curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/project-states" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PROJECT_STATE_PAYLOAD" 2>/dev/null >/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
# If API call failed, queue locally
|
||||
if [ "$API_SUCCESS" = "false" ]; then
|
||||
# Save context to pending queue
|
||||
QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_context.json"
|
||||
echo "$CONTEXT_PAYLOAD" > "$QUEUE_FILE"
|
||||
|
||||
# Save project state to pending queue
|
||||
STATE_QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_state.json"
|
||||
echo "$PROJECT_STATE_PAYLOAD" > "$STATE_QUEUE_FILE"
|
||||
|
||||
echo "[WARNING] Context queued locally (API unavailable) - will sync when online" >&2
|
||||
|
||||
# Try to sync (opportunistic) - Changed from background (&) to synchronous to prevent zombie processes
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1
|
||||
fi
|
||||
else
|
||||
echo "[OK] Context saved to database" >&2
|
||||
|
||||
# Trigger sync of any queued items - Changed from background (&) to synchronous to prevent zombie processes
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,182 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: task-complete (v2 - with offline support)
|
||||
# Runs AFTER a task is completed
|
||||
# Saves conversation context to the database for future recall
|
||||
# FALLBACK: Queues locally when API is unavailable, syncs later
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# TASK_SUMMARY - Summary of completed task (auto-generated by Claude)
|
||||
# TASK_FILES - Files modified during task (comma-separated)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
PENDING_DIR="$QUEUE_DIR/pending"
|
||||
UPLOADED_DIR="$QUEUE_DIR/uploaded"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID (same logic as user-prompt-submit)
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create queue directories if they don't exist
|
||||
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" 2>/dev/null
|
||||
|
||||
# Gather task information
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
TIMESTAMP_FILENAME=$(date -u +"%Y%m%d_%H%M%S")
|
||||
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
|
||||
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
|
||||
# Get recent git changes
|
||||
CHANGED_FILES=$(git diff --name-only HEAD~1 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
CHANGED_FILES="${TASK_FILES:-}"
|
||||
fi
|
||||
|
||||
# Create task summary
|
||||
if [ -z "$TASK_SUMMARY" ]; then
|
||||
# Generate basic summary from git log if no summary provided
|
||||
TASK_SUMMARY=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Task completed")
|
||||
fi
|
||||
|
||||
# Build context payload
|
||||
CONTEXT_TITLE="Session: ${TIMESTAMP}"
|
||||
CONTEXT_TYPE="session_summary"
|
||||
RELEVANCE_SCORE=7.0
|
||||
|
||||
# Create dense summary
|
||||
DENSE_SUMMARY="Task completed on branch '${GIT_BRANCH}' (commit: ${GIT_COMMIT}).
|
||||
|
||||
Summary: ${TASK_SUMMARY}
|
||||
|
||||
Modified files: ${CHANGED_FILES:-none}
|
||||
|
||||
Timestamp: ${TIMESTAMP}"
|
||||
|
||||
# Escape JSON strings
|
||||
escape_json() {
|
||||
echo "$1" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read())[1:-1])"
|
||||
}
|
||||
|
||||
ESCAPED_TITLE=$(escape_json "$CONTEXT_TITLE")
|
||||
ESCAPED_SUMMARY=$(escape_json "$DENSE_SUMMARY")
|
||||
|
||||
# Save context to database
|
||||
CONTEXT_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"context_type": "${CONTEXT_TYPE}",
|
||||
"title": ${ESCAPED_TITLE},
|
||||
"dense_summary": ${ESCAPED_SUMMARY},
|
||||
"relevance_score": ${RELEVANCE_SCORE},
|
||||
"metadata": {
|
||||
"git_branch": "${GIT_BRANCH}",
|
||||
"git_commit": "${GIT_COMMIT}",
|
||||
"files_modified": "${CHANGED_FILES}",
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Update project state
|
||||
PROJECT_STATE_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"state_data": {
|
||||
"last_task_completion": "${TIMESTAMP}",
|
||||
"last_git_commit": "${GIT_COMMIT}",
|
||||
"last_git_branch": "${GIT_BRANCH}",
|
||||
"recent_files": "${CHANGED_FILES}"
|
||||
},
|
||||
"state_type": "task_completion"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Try to POST to API if we have a JWT token
|
||||
API_SUCCESS=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
RESPONSE=$(curl -s --max-time 5 -w "\n%{http_code}" \
|
||||
-X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$CONTEXT_PAYLOAD" 2>/dev/null)
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
|
||||
API_SUCCESS=true
|
||||
|
||||
# Also update project state
|
||||
curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/project-states" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PROJECT_STATE_PAYLOAD" 2>/dev/null >/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
# If API call failed, queue locally
|
||||
if [ "$API_SUCCESS" = "false" ]; then
|
||||
# Save context to pending queue
|
||||
QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_context.json"
|
||||
echo "$CONTEXT_PAYLOAD" > "$QUEUE_FILE"
|
||||
|
||||
# Save project state to pending queue
|
||||
STATE_QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_state.json"
|
||||
echo "$PROJECT_STATE_PAYLOAD" > "$STATE_QUEUE_FILE"
|
||||
|
||||
echo "[WARNING] Context queued locally (API unavailable) - will sync when online" >&2
|
||||
|
||||
# Try to sync in background (opportunistic)
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1 &
|
||||
fi
|
||||
else
|
||||
echo "[OK] Context saved to database" >&2
|
||||
|
||||
# Trigger background sync of any queued items
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1 &
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,140 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: task-complete
|
||||
# Runs AFTER a task is completed
|
||||
# Saves conversation context to the database for future recall
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://localhost:8000)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# TASK_SUMMARY - Summary of completed task (auto-generated by Claude)
|
||||
# TASK_FILES - Files modified during task (comma-separated)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://localhost:8000}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID (same logic as user-prompt-submit)
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID or JWT token
|
||||
if [ -z "$PROJECT_ID" ] || [ -z "$JWT_TOKEN" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Gather task information
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
|
||||
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
|
||||
# Get recent git changes
|
||||
CHANGED_FILES=$(git diff --name-only HEAD~1 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
CHANGED_FILES="${TASK_FILES:-}"
|
||||
fi
|
||||
|
||||
# Create task summary
|
||||
if [ -z "$TASK_SUMMARY" ]; then
|
||||
# Generate basic summary from git log if no summary provided
|
||||
TASK_SUMMARY=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Task completed")
|
||||
fi
|
||||
|
||||
# Build context payload
|
||||
CONTEXT_TITLE="Session: ${TIMESTAMP}"
|
||||
CONTEXT_TYPE="session_summary"
|
||||
RELEVANCE_SCORE=7.0
|
||||
|
||||
# Create dense summary
|
||||
DENSE_SUMMARY="Task completed on branch '${GIT_BRANCH}' (commit: ${GIT_COMMIT}).
|
||||
|
||||
Summary: ${TASK_SUMMARY}
|
||||
|
||||
Modified files: ${CHANGED_FILES:-none}
|
||||
|
||||
Timestamp: ${TIMESTAMP}"
|
||||
|
||||
# Escape JSON strings
|
||||
escape_json() {
|
||||
echo "$1" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read())[1:-1])"
|
||||
}
|
||||
|
||||
ESCAPED_TITLE=$(escape_json "$CONTEXT_TITLE")
|
||||
ESCAPED_SUMMARY=$(escape_json "$DENSE_SUMMARY")
|
||||
|
||||
# Save context to database
|
||||
CONTEXT_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"context_type": "${CONTEXT_TYPE}",
|
||||
"title": ${ESCAPED_TITLE},
|
||||
"dense_summary": ${ESCAPED_SUMMARY},
|
||||
"relevance_score": ${RELEVANCE_SCORE},
|
||||
"metadata": {
|
||||
"git_branch": "${GIT_BRANCH}",
|
||||
"git_commit": "${GIT_COMMIT}",
|
||||
"files_modified": "${CHANGED_FILES}",
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# POST to conversation-contexts endpoint
|
||||
RESPONSE=$(curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$CONTEXT_PAYLOAD" 2>/dev/null)
|
||||
|
||||
# Update project state
|
||||
PROJECT_STATE_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"state_data": {
|
||||
"last_task_completion": "${TIMESTAMP}",
|
||||
"last_git_commit": "${GIT_COMMIT}",
|
||||
"last_git_branch": "${GIT_BRANCH}",
|
||||
"recent_files": "${CHANGED_FILES}"
|
||||
},
|
||||
"state_type": "task_completion"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/project-states" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PROJECT_STATE_PAYLOAD" 2>/dev/null >/dev/null
|
||||
|
||||
# Log success (optional - comment out for silent operation)
|
||||
if [ -n "$RESPONSE" ]; then
|
||||
echo "✓ Context saved to database" >&2
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,85 +0,0 @@
|
||||
# Quick Update - Make Existing Periodic Save Task Invisible
|
||||
# This script updates the existing task to run without showing a window
|
||||
|
||||
$TaskName = "ClaudeTools - Periodic Context Save"
|
||||
|
||||
Write-Host "Updating task '$TaskName' to run invisibly..."
|
||||
Write-Host ""
|
||||
|
||||
# Check if task exists
|
||||
$Task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
if (-not $Task) {
|
||||
Write-Host "ERROR: Task '$TaskName' not found."
|
||||
Write-Host "Run setup_periodic_save.ps1 to create it first."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find pythonw.exe path
|
||||
$PythonExe = (Get-Command python).Source
|
||||
$PythonDir = Split-Path $PythonExe -Parent
|
||||
$PythonwPath = Join-Path $PythonDir "pythonw.exe"
|
||||
|
||||
if (-not (Test-Path $PythonwPath)) {
|
||||
Write-Host "ERROR: pythonw.exe not found at $PythonwPath"
|
||||
Write-Host "Please reinstall Python to get pythonw.exe"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "Found pythonw.exe at: $PythonwPath"
|
||||
|
||||
# Update the action to use pythonw.exe
|
||||
$NewAction = New-ScheduledTaskAction -Execute $PythonwPath `
|
||||
-Argument "D:\ClaudeTools\.claude\hooks\periodic_save_check.py" `
|
||||
-WorkingDirectory "D:\ClaudeTools"
|
||||
|
||||
# Update settings to be hidden
|
||||
$NewSettings = New-ScheduledTaskSettingsSet `
|
||||
-AllowStartIfOnBatteries `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-StartWhenAvailable `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes 5) `
|
||||
-Hidden
|
||||
|
||||
# Update principal to run in background (S4U = Service-For-User)
|
||||
$NewPrincipal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U
|
||||
|
||||
# Get existing trigger (preserve it)
|
||||
$ExistingTrigger = $Task.Triggers
|
||||
|
||||
# Update the task
|
||||
Set-ScheduledTask -TaskName $TaskName `
|
||||
-Action $NewAction `
|
||||
-Settings $NewSettings `
|
||||
-Principal $NewPrincipal `
|
||||
-Trigger $ExistingTrigger | Out-Null
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "[SUCCESS] Task updated successfully!"
|
||||
Write-Host ""
|
||||
Write-Host "Changes made:"
|
||||
Write-Host " 1. Changed executable: python.exe -> pythonw.exe"
|
||||
Write-Host " 2. Set task to Hidden"
|
||||
Write-Host " 3. Changed LogonType: Interactive -> S4U (background)"
|
||||
Write-Host ""
|
||||
Write-Host "Verification:"
|
||||
|
||||
# Show current settings
|
||||
$UpdatedTask = Get-ScheduledTask -TaskName $TaskName
|
||||
$Settings = $UpdatedTask.Settings
|
||||
$Action = $UpdatedTask.Actions[0]
|
||||
$Principal = $UpdatedTask.Principal
|
||||
|
||||
Write-Host " Executable: $($Action.Execute)"
|
||||
Write-Host " Hidden: $($Settings.Hidden)"
|
||||
Write-Host " LogonType: $($Principal.LogonType)"
|
||||
Write-Host ""
|
||||
|
||||
if ($Settings.Hidden -and $Action.Execute -like "*pythonw.exe" -and $Principal.LogonType -eq "S4U") {
|
||||
Write-Host "[OK] All settings correct - task will run invisibly!"
|
||||
} else {
|
||||
Write-Host "[WARNING] Some settings may not be correct - please verify manually"
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "The task will now run invisibly without showing any console window."
|
||||
Write-Host ""
|
||||
@@ -1,163 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: user-prompt-submit (v2 - with offline support)
|
||||
# Runs BEFORE each user message is processed
|
||||
# Injects relevant context from the database into the conversation
|
||||
# FALLBACK: Uses local cache when API is unavailable
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# MIN_RELEVANCE_SCORE - Minimum score for context (default: 5.0)
|
||||
# MAX_CONTEXTS - Maximum number of contexts to retrieve (default: 10)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
MIN_SCORE="${MIN_RELEVANCE_SCORE:-5.0}"
|
||||
MAX_ITEMS="${MAX_CONTEXTS:-10}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
CACHE_DIR="$CLAUDE_DIR/context-cache"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID from git repo if not set
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
# Try to get from git config
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
# Hash the remote URL to create a consistent ID
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID available
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Silent exit - no context available
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create cache directory if it doesn't exist
|
||||
PROJECT_CACHE_DIR="$CACHE_DIR/$PROJECT_ID"
|
||||
mkdir -p "$PROJECT_CACHE_DIR" 2>/dev/null
|
||||
|
||||
# Try to sync any queued contexts first (opportunistic)
|
||||
# NOTE: Changed from background (&) to synchronous to prevent zombie processes
|
||||
if [ -d "$QUEUE_DIR/pending" ] && [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1
|
||||
fi
|
||||
|
||||
# Build API request URL
|
||||
RECALL_URL="${API_URL}/api/conversation-contexts/recall"
|
||||
QUERY_PARAMS="project_id=${PROJECT_ID}&limit=${MAX_ITEMS}&min_relevance_score=${MIN_SCORE}"
|
||||
|
||||
# Try to fetch context from API (with timeout and error handling)
|
||||
API_AVAILABLE=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
CONTEXT_RESPONSE=$(curl -s --max-time 3 \
|
||||
"${RECALL_URL}?${QUERY_PARAMS}" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Accept: application/json" 2>/dev/null)
|
||||
|
||||
if [ $? -eq 0 ] && [ -n "$CONTEXT_RESPONSE" ]; then
|
||||
# Check if response is valid JSON (not an error)
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "import sys, json; json.load(sys.stdin)" 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
API_AVAILABLE=true
|
||||
# Save to cache for offline use
|
||||
echo "$CONTEXT_RESPONSE" > "$PROJECT_CACHE_DIR/latest.json"
|
||||
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$PROJECT_CACHE_DIR/last_updated"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fallback to local cache if API unavailable
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
if [ -f "$PROJECT_CACHE_DIR/latest.json" ]; then
|
||||
CONTEXT_RESPONSE=$(cat "$PROJECT_CACHE_DIR/latest.json")
|
||||
CACHE_AGE="unknown"
|
||||
if [ -f "$PROJECT_CACHE_DIR/last_updated" ]; then
|
||||
CACHE_AGE=$(cat "$PROJECT_CACHE_DIR/last_updated")
|
||||
fi
|
||||
echo "<!-- Using cached context (API unavailable) - Last updated: $CACHE_AGE -->" >&2
|
||||
else
|
||||
# No cache available, exit silently
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Parse and format context
|
||||
CONTEXT_COUNT=$(echo "$CONTEXT_RESPONSE" | grep -o '"id"' | wc -l)
|
||||
|
||||
if [ "$CONTEXT_COUNT" -gt 0 ]; then
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from API -->"
|
||||
else
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from LOCAL CACHE (offline mode) -->"
|
||||
fi
|
||||
echo ""
|
||||
echo "## Previous Context"
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
echo "[WARNING] **Offline Mode** - Using cached context (API unavailable)"
|
||||
echo ""
|
||||
fi
|
||||
echo "The following context has been automatically recalled:"
|
||||
echo ""
|
||||
|
||||
# Extract and format each context entry
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
contexts = json.load(sys.stdin)
|
||||
if isinstance(contexts, list):
|
||||
for i, ctx in enumerate(contexts, 1):
|
||||
title = ctx.get('title', 'Untitled')
|
||||
summary = ctx.get('dense_summary', '')
|
||||
score = ctx.get('relevance_score', 0)
|
||||
ctx_type = ctx.get('context_type', 'unknown')
|
||||
|
||||
print(f'### {i}. {title} (Score: {score}/10)')
|
||||
print(f'*Type: {ctx_type}*')
|
||||
print()
|
||||
print(summary)
|
||||
print()
|
||||
print('---')
|
||||
print()
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null
|
||||
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "*Context automatically injected to maintain continuity across sessions.*"
|
||||
else
|
||||
echo "*Context from local cache - new context will sync when API is available.*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
@@ -1,162 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: user-prompt-submit (v2 - with offline support)
|
||||
# Runs BEFORE each user message is processed
|
||||
# Injects relevant context from the database into the conversation
|
||||
# FALLBACK: Uses local cache when API is unavailable
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# MIN_RELEVANCE_SCORE - Minimum score for context (default: 5.0)
|
||||
# MAX_CONTEXTS - Maximum number of contexts to retrieve (default: 10)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
MIN_SCORE="${MIN_RELEVANCE_SCORE:-5.0}"
|
||||
MAX_ITEMS="${MAX_CONTEXTS:-10}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
CACHE_DIR="$CLAUDE_DIR/context-cache"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID from git repo if not set
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
# Try to get from git config
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
# Hash the remote URL to create a consistent ID
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID available
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Silent exit - no context available
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create cache directory if it doesn't exist
|
||||
PROJECT_CACHE_DIR="$CACHE_DIR/$PROJECT_ID"
|
||||
mkdir -p "$PROJECT_CACHE_DIR" 2>/dev/null
|
||||
|
||||
# Try to sync any queued contexts first (opportunistic)
|
||||
if [ -d "$QUEUE_DIR/pending" ] && [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1 &
|
||||
fi
|
||||
|
||||
# Build API request URL
|
||||
RECALL_URL="${API_URL}/api/conversation-contexts/recall"
|
||||
QUERY_PARAMS="project_id=${PROJECT_ID}&limit=${MAX_ITEMS}&min_relevance_score=${MIN_SCORE}"
|
||||
|
||||
# Try to fetch context from API (with timeout and error handling)
|
||||
API_AVAILABLE=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
CONTEXT_RESPONSE=$(curl -s --max-time 3 \
|
||||
"${RECALL_URL}?${QUERY_PARAMS}" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Accept: application/json" 2>/dev/null)
|
||||
|
||||
if [ $? -eq 0 ] && [ -n "$CONTEXT_RESPONSE" ]; then
|
||||
# Check if response is valid JSON (not an error)
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "import sys, json; json.load(sys.stdin)" 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
API_AVAILABLE=true
|
||||
# Save to cache for offline use
|
||||
echo "$CONTEXT_RESPONSE" > "$PROJECT_CACHE_DIR/latest.json"
|
||||
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$PROJECT_CACHE_DIR/last_updated"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fallback to local cache if API unavailable
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
if [ -f "$PROJECT_CACHE_DIR/latest.json" ]; then
|
||||
CONTEXT_RESPONSE=$(cat "$PROJECT_CACHE_DIR/latest.json")
|
||||
CACHE_AGE="unknown"
|
||||
if [ -f "$PROJECT_CACHE_DIR/last_updated" ]; then
|
||||
CACHE_AGE=$(cat "$PROJECT_CACHE_DIR/last_updated")
|
||||
fi
|
||||
echo "<!-- Using cached context (API unavailable) - Last updated: $CACHE_AGE -->" >&2
|
||||
else
|
||||
# No cache available, exit silently
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Parse and format context
|
||||
CONTEXT_COUNT=$(echo "$CONTEXT_RESPONSE" | grep -o '"id"' | wc -l)
|
||||
|
||||
if [ "$CONTEXT_COUNT" -gt 0 ]; then
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from API -->"
|
||||
else
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from LOCAL CACHE (offline mode) -->"
|
||||
fi
|
||||
echo ""
|
||||
echo "## Previous Context"
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
echo "[WARNING] **Offline Mode** - Using cached context (API unavailable)"
|
||||
echo ""
|
||||
fi
|
||||
echo "The following context has been automatically recalled:"
|
||||
echo ""
|
||||
|
||||
# Extract and format each context entry
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
contexts = json.load(sys.stdin)
|
||||
if isinstance(contexts, list):
|
||||
for i, ctx in enumerate(contexts, 1):
|
||||
title = ctx.get('title', 'Untitled')
|
||||
summary = ctx.get('dense_summary', '')
|
||||
score = ctx.get('relevance_score', 0)
|
||||
ctx_type = ctx.get('context_type', 'unknown')
|
||||
|
||||
print(f'### {i}. {title} (Score: {score}/10)')
|
||||
print(f'*Type: {ctx_type}*')
|
||||
print()
|
||||
print(summary)
|
||||
print()
|
||||
print('---')
|
||||
print()
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null
|
||||
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "*Context automatically injected to maintain continuity across sessions.*"
|
||||
else
|
||||
echo "*Context from local cache - new context will sync when API is available.*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
@@ -1,119 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: user-prompt-submit
|
||||
# Runs BEFORE each user message is processed
|
||||
# Injects relevant context from the database into the conversation
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://localhost:8000)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# MIN_RELEVANCE_SCORE - Minimum score for context (default: 5.0)
|
||||
# MAX_CONTEXTS - Maximum number of contexts to retrieve (default: 10)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://localhost:8000}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
MIN_SCORE="${MIN_RELEVANCE_SCORE:-5.0}"
|
||||
MAX_ITEMS="${MAX_CONTEXTS:-10}"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID from git repo if not set
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
# Try to get from git config
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
# Hash the remote URL to create a consistent ID
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID available
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Silent exit - no context available
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Exit if no JWT token
|
||||
if [ -z "$JWT_TOKEN" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Build API request URL
|
||||
RECALL_URL="${API_URL}/api/conversation-contexts/recall"
|
||||
QUERY_PARAMS="project_id=${PROJECT_ID}&limit=${MAX_ITEMS}&min_relevance_score=${MIN_SCORE}"
|
||||
|
||||
# Fetch context from API (with timeout and error handling)
|
||||
CONTEXT_RESPONSE=$(curl -s --max-time 3 \
|
||||
"${RECALL_URL}?${QUERY_PARAMS}" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Accept: application/json" 2>/dev/null)
|
||||
|
||||
# Check if request was successful
|
||||
if [ $? -ne 0 ] || [ -z "$CONTEXT_RESPONSE" ]; then
|
||||
# Silent failure - API unavailable
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Parse and format context (expects JSON array of context objects)
|
||||
# Example response: [{"title": "...", "dense_summary": "...", "relevance_score": 8.5}, ...]
|
||||
CONTEXT_COUNT=$(echo "$CONTEXT_RESPONSE" | grep -o '"id"' | wc -l)
|
||||
|
||||
if [ "$CONTEXT_COUNT" -gt 0 ]; then
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) -->"
|
||||
echo ""
|
||||
echo "## 📚 Previous Context"
|
||||
echo ""
|
||||
echo "The following context has been automatically recalled from previous sessions:"
|
||||
echo ""
|
||||
|
||||
# Extract and format each context entry
|
||||
# Note: This uses simple text parsing. For production, consider using jq if available.
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
contexts = json.load(sys.stdin)
|
||||
if isinstance(contexts, list):
|
||||
for i, ctx in enumerate(contexts, 1):
|
||||
title = ctx.get('title', 'Untitled')
|
||||
summary = ctx.get('dense_summary', '')
|
||||
score = ctx.get('relevance_score', 0)
|
||||
ctx_type = ctx.get('context_type', 'unknown')
|
||||
|
||||
print(f'### {i}. {title} (Score: {score}/10)')
|
||||
print(f'*Type: {ctx_type}*')
|
||||
print()
|
||||
print(summary)
|
||||
print()
|
||||
print('---')
|
||||
print()
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null
|
||||
|
||||
echo ""
|
||||
echo "*This context was automatically injected to help maintain continuity across sessions.*"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
Reference in New Issue
Block a user