Skip to main content

Health Registry System - Unified Specification v1.0

Status: AUTHORITATIVE - Supersedes VERSION-GUARD-SPEC.md and VERSION-GUARD-FINAL-DESIGN.md Created: 2025-12-20 Based on: 5 challenge agent analyses + sequential thinking synthesis

Executive Summary

This specification defines a comprehensive Health Registry System for cleo that goes beyond simple version guards to provide:
  1. Central Project Registry - Track all initialized projects globally
  2. Health Monitoring - 36 checks across 6 dimensions with scoring
  3. LLM-Agent-First Design - JSON by default, structured errors, recovery hints
  4. Git Sync Support - Cross-machine identity via project UUID

Table of Contents

  1. Architecture Overview
  2. Central Project Registry
  3. Health Monitoring System
  4. Exit Code Taxonomy
  5. LLM-Agent-First Protocols
  6. Git Sync Support
  7. Implementation Roadmap
  8. Files to Create/Modify

1. Architecture Overview

What We’re Building

┌─────────────────────────────────────────────────────────────────────┐
│                    HEALTH REGISTRY SYSTEM                            │
├─────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  ~/.cleo/                                                     │
│  ├── registry/                                                       │
│  │   ├── registry.json      ← Central project registry              │
│  │   ├── registry.lock      ← Concurrent access lock                │
│  │   └── health-cache.json  ← TTL-based health cache                │
│  ├── lib/                                                            │
│  │   ├── registry.sh        ← Registry operations                   │
│  │   ├── health.sh          ← Health check engine                   │
│  │   └── validation.sh      ← Extended (existing)                   │
│  └── schemas/                                                        │
│      ├── registry.schema.json                                        │
│      └── health.schema.json                                          │
│                                                                      │
│  Per-Project (.cleo/)                                              │
│  ├── project.json           ← Project UUID + metadata               │
│  ├── todo.json              ← Extended with _meta.version           │
│  └── health-history.json    ← Local health trend data               │
│                                                                      │
└─────────────────────────────────────────────────────────────────────┘

Design Principles

  1. Extend, Don’t Replace - Build on existing lib/validation.sh, not new files
  2. Performance First - head+grep for fast path, jq fallback for reliability
  3. LLM-Agent-First - JSON output by default, structured errors always
  4. Backward Compatible - Old projects work without modification
  5. Fail Safe - Warn by default for additive changes, block for breaking

2. Central Project Registry

Registry Schema

Location: ~/.cleo/registry/registry.json
{
  "$schema": "https://cleo.dev/schemas/v1/registry.schema.json",
  "version": "1.0.0",
  "_meta": {
    "checksum": "a7b3c9d2e1f4a7b3",
    "createdAt": "2025-12-20T10:00:00Z",
    "lastUpdated": "2025-12-20T15:30:00Z",
    "totalProjects": 5,
    "healthSummary": {
      "healthy": 4,
      "degraded": 0,
      "orphaned": 1
    }
  },
  "config": {
    "storePlainPaths": true,
    "autoRegisterOnInit": true,
    "autoRegisterOnCommand": false,
    "trackAccessOnCommand": true,
    "orphanCheckDays": 30
  },
  "projects": {
    "a7b3c9d2e1f4": {
      "id": "a7b3c9d2e1f4",
      "pathHash": "a7b3c9d2e1f4",
      "path": "/home/user/projects/my-app",
      "name": "my-app",
      "displayName": "My Application",
      "initialized": {
        "at": "2025-12-01T10:00:00Z",
        "version": "0.23.1",
        "schemaVersion": "2.3.0"
      },
      "lastAccess": {
        "at": "2025-12-20T15:30:00Z",
        "command": "list",
        "version": "0.24.0"
      },
      "health": {
        "status": "healthy",
        "score": 95,
        "lastCheck": "2025-12-20T15:30:00Z",
        "issues": []
      },
      "stats": {
        "totalTasks": 45,
        "pendingTasks": 12,
        "activeTasks": 1,
        "completedTasks": 30,
        "archivedTasks": 150
      },
      "flags": {
        "isOrphan": false,
        "isFavorite": false
      }
    }
  },
  "recentlyAccessed": ["a7b3c9d2e1f4", "b8c4d0e3f5a6"],
  "favorites": ["a7b3c9d2e1f4"]
}

Project ID Generation

Deterministic SHA-256 hash of canonical absolute path, truncated to 12 hex chars:
calculate_project_id() {
    local path="$1"
    local canonical_path
    canonical_path=$(realpath "$path" 2>/dev/null || echo "$path")
    echo -n "$canonical_path" | sha256sum | cut -c1-12
}
# Example: /home/user/projects/my-app → "a7b3c9d2e1f4"

Project Identity (for Git Sync)

Each project gets a UUID stored in .cleo/project.json:
{
  "projectId": "550e8400-e29b-41d4-a716-446655440000",
  "projectName": "my-awesome-app",
  "createdAt": "2025-11-01T10:00:00Z",
  "knownPaths": [
    "/home/dev/myproject",
    "/Users/dev/work/myproject"
  ]
}
This enables cross-machine identity - same project recognized by UUID regardless of path.

Registration Flow

ct init


┌─────────────────────────────┐
│ 1. Create .cleo/ folder   │
│ 2. Generate project UUID    │
│ 3. Create project.json      │
│ 4. Calculate project ID     │
│    (SHA256 of path)         │
└─────────────────────────────┘


┌─────────────────────────────┐
│ 5. Lock registry            │
│ 6. Check existing entry     │
│    by projectId or pathHash │
│ 7. Create/update entry      │
│ 8. Atomic write registry    │
│ 9. Unlock                   │
└─────────────────────────────┘

Orphan Detection

Health Status Flow:

    Path exists? ─────────────────────────────────────────┐
         │                                                 │
         │ YES                                             │ NO
         ▼                                                 ▼
    .cleo/todo.json exists?                    Days since last access?
         │                                                 │
    ├─ YES → HEALTHY                              ├─ < 7 days → POSSIBLY_MOVED
    │                                              ├─ < 30 days → ORPHAN_SUSPECTED
    └─ NO → DEGRADED                               └─ >= 30 days → ORPHAN_CONFIRMED

Registry CLI Commands

# Query commands
ct registry list                    # List all registered projects
ct registry list --healthy          # Filter by health status
ct registry list --orphan           # Show only orphaned projects
ct registry show <id-or-path>       # Show detailed project info
ct registry show .                  # Current project's registry entry
ct registry stats                   # Aggregate stats across all

# Mutation commands
ct registry add [path]              # Register project
ct registry remove <id-or-path>     # Unregister project
ct registry clean                   # Remove confirmed orphans
ct registry clean --dry-run         # Preview removals
ct registry refresh [--all]         # Refresh cached stats

# Discovery
ct registry discover [base-path]    # Scan for unregistered projects
ct registry discover --register     # Auto-register found projects

# Aliases
ct projects                         # Alias for 'ct registry list'
ct proj                             # Short alias

3. Health Monitoring System

Health Dimensions (6 Categories, 36 Checks)

CategoryWeightDescription
Data Integrity (DI)40%JSON validity, checksums, schema versions, required fields
Referential Integrity (RI)25%Dependencies exist, parents exist, no orphans, no cycles
Semantic Integrity (SI)20%Business rules (single active task, status consistency)
Session/Operational (SO)5%Stale sessions, lock files, focus consistency
Backup Health (BH)5%Recent backups exist, backup validation
Configuration Health (CH)5%Config validity, version compatibility

Complete Health Check Registry

Category 1: Data Integrity (DI) - 40% Weight

IDCheck NameSeverityAuto-Fix
DI-001JSON Syntax ValidCRITICALNO
DI-002Checksum VerificationCRITICALSAFE
DI-003Config Version MatchCRITICALSAFE
DI-004Schema Version CompatibleCRITICALCONDITIONAL
DI-005Required Fields PresentCRITICALCONDITIONAL

Category 2: Referential Integrity (RI) - 25% Weight

IDCheck NameSeverityAuto-Fix
RI-001Task ID UniquenessCRITICALNO
RI-002Dependency ExistsCRITICALCONDITIONAL
RI-003No Circular DependenciesCRITICALNO
RI-004Parent Task ExistsCRITICALCONDITIONAL
RI-005Phase Slug ValidWARNINGSAFE
RI-006Focus Task ExistsWARNINGSAFE
RI-007Labels Index ConsistentINFOSAFE

Category 3: Semantic Integrity (SI) - 20% Weight

IDCheck NameSeverityAuto-Fix
SI-001Single Active TaskWARNINGCONDITIONAL
SI-002Single Active PhaseWARNINGCONDITIONAL
SI-003Done Tasks Have completedAtWARNINGSAFE
SI-004Blocked Tasks Have blockedByWARNINGNO
SI-005Active Phases Have startedAtWARNINGSAFE
SI-006Completed Phases Have completedAtWARNINGSAFE
SI-007Timestamp Ordering ValidWARNINGCONDITIONAL
SI-008No Future TimestampsWARNINGSAFE
SI-009Hierarchy Depth ValidWARNINGNO
SI-010Sibling Count ValidWARNINGNO
SI-011Status Transitions ValidINFON/A
SI-012CurrentPhase Matches ActiveWARNINGSAFE

Category 4: Session/Operational (SO) - 5% Weight

IDCheck NameSeverityAuto-Fix
SO-001No Stale SessionsWARNINGSAFE
SO-002Lock File StalenessWARNINGSAFE
SO-003Focus ConsistencyWARNINGSAFE
SO-004Session Note PresentINFONO
SO-005Last Session ID ValidINFOSAFE

Category 5: Backup Health (BH) - 5% Weight

IDCheck NameSeverityAuto-Fix
BH-001Recent Backup ExistsWARNINGSAFE
BH-002Backup Directory ValidINFOSAFE
BH-003Migration Backups PreservedINFOSAFE
BH-004Backup Metadata ValidINFOCONDITIONAL
BH-005Backup Checksums MatchWARNINGCONDITIONAL
BH-006Safety Backups Not ExpiredINFOSAFE

Category 6: Configuration Health (CH) - 5% Weight

IDCheck NameSeverityAuto-Fix
CH-001Config File ExistsWARNINGSAFE
CH-002Config Schema ValidWARNINGNO
CH-003Config Version CurrentINFOCONDITIONAL
CH-004Validation Settings SaneINFONO
CH-005Backup Settings ValidINFOSAFE
CH-006Phase Definitions CompleteINFOSAFE

Health Score Calculation

HEALTH_SCORE = Σ (category_weight × category_score)

Category Score = 100 - (critical_failures × 25) - (warning_failures × 10) - (info_failures × 3)
                 [minimum 0]
Grade Mapping:
ScoreGradeStatusAction
90-100AExcellentNo action needed
80-89BGoodOptional improvements
70-79CFairAddress warnings
60-69DPoorMust address issues
0-59FCriticalImmediate attention

Remediation Classification

  • SAFE (17 checks): Apply without confirmation - idempotent, non-destructive
  • CONDITIONAL (9 checks): Requires backup + user opt-in
  • MANUAL (9 checks): Cannot auto-fix, human judgment required

Health CLI Commands

# Health checks
ct health                           # Full health check (JSON output)
ct health --quick                   # Fast subset of critical checks
ct health --category schema         # Check specific category
ct health --fix                     # Preview fixes
ct health --fix --apply             # Apply safe fixes
ct health --fix --apply --force     # Apply conditional fixes (creates backup)
ct health --fix --interactive       # Prompt for each fix

# Registry-wide health
ct registry health --all            # Health check all registered projects
ct registry health --fix --all      # Batch fix all projects

4. Exit Code Taxonomy

Exit Code Ranges

RangeCategoryDescription
0-9GeneralStandard success/failure
10-19HierarchyParent/child, depth, sibling issues
20-29ConcurrencyLock, session, write conflicts (existing)
30-39Schema/VersionVersion mismatch, migration
50-59HealthHealth check failures
100+SpecialInternal errors

Schema/Version Exit Codes (30-39)

CodeConstantMeaningRecoverableRecovery Command
30EXIT_SCHEMA_OUTDATEDProject schema older than CLIYesct migrate run
31EXIT_SCHEMA_INCOMPATIBLEMajor version mismatchNoUpgrade CLI or downgrade project
32EXIT_SCHEMA_AHEADProject newer than CLIYesUpgrade CLI
33EXIT_SCHEMA_CORRUPTCannot parse versionNoManual fix
35EXIT_MIGRATION_IN_PROGRESSMigration lock heldYesWait/retry
36EXIT_MIGRATION_FAILEDMigration failedYesct restore

Health Exit Codes (50-59)

CodeConstantMeaningRecoverable
50EXIT_HEALTH_CRITICALCritical health issuesDepends on issue
51EXIT_HEALTH_WARNINGWarning-level issuesYes
52EXIT_HEALTH_FIXABLEAuto-fixable issues foundYes (ct health --fix)

5. LLM-Agent-First Protocols

JSON Output Structure

All commands output JSON when piped (non-TTY). Standard envelope:
{
  "$schema": "https://cleo.dev/schemas/v1/output.schema.json",
  "_meta": {
    "format": "json",
    "version": "0.24.0",
    "command": "health",
    "timestamp": "2025-12-20T10:00:00Z",
    "executionMs": 45
  },
  "success": true,
  "data": { ... },
  "warnings": [],
  "errors": []
}

Error Response Structure

{
  "_meta": { ... },
  "success": false,
  "error": {
    "code": "E_SCHEMA_OUTDATED",
    "exitCode": 30,
    "message": "Project schema v2.2.0 is older than CLI v2.3.0",
    "category": "schema",
    "severity": "warning",
    "recoverable": true,
    "autoFix": true,
    "fixCommand": "ct migrate run",
    "context": {
      "projectVersion": "2.2.0",
      "cliVersion": "2.3.0",
      "breaking": false
    }
  }
}

Health Check Response

{
  "_meta": { ... },
  "success": true,
  "healthy": false,
  "summary": {
    "totalChecks": 36,
    "passed": 33,
    "failed": 3,
    "critical": 0,
    "warning": 2,
    "info": 1
  },
  "score": 87,
  "grade": "B",
  "trend": "stable",
  "categories": {
    "dataIntegrity": { "score": 100, "status": "pass", "checks": [...] },
    "referentialIntegrity": { "score": 95, "status": "warning", "checks": [...] }
  },
  "autoFixable": [
    {
      "checkId": "RI-006",
      "fixCommand": "ct focus clear",
      "description": "Clear invalid focus reference"
    }
  ],
  "nextAction": {
    "priority": "high",
    "command": "ct health --fix --apply",
    "reason": "2 auto-fixable issues found"
  }
}
┌─────────────────────────────────────────────────────────────────┐
│                    SESSION LIFECYCLE                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  1. SESSION START                                                │
│     ├── ct health --quick                                        │
│     │   └── Exit 0: Proceed                                      │
│     │   └── Exit 50+: Run auto-recovery                          │
│     │                                                            │
│  2. PRE-WRITE CHECK (write operations only)                      │
│     ├── ct health --category schema --quick                      │
│     │   └── Schema outdated? → ct migrate run                    │
│     │   └── Session conflict? → ct session end && start          │
│     │                                                            │
│  3. OPERATION                                                    │
│     ├── ct add/update/complete/...                               │
│     │   └── Success: Continue                                    │
│     │   └── Failure: Check error.recoverable                     │
│     │       └── true: Execute error.fixCommand                   │
│     │       └── false: Escalate to human                         │
│     │                                                            │
│  4. POST-FAILURE RECOVERY                                        │
│     ├── ct health --full                                         │
│     │   └── ct health --fix --apply (if safe)                    │
│     │   └── Retry original operation                             │
│     │                                                            │
│  5. SESSION END                                                  │
│     ├── ct health --quick (log warnings)                         │
│     ├── ct session end                                           │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

6. Git Sync Support

Project UUID for Cross-Machine Identity

Projects identified by UUID (in .cleo/project.json), not path:
{
  "projectId": "550e8400-e29b-41d4-a716-446655440000",
  "knownPaths": [
    "/home/dev/myproject",
    "/Users/dev/work/myproject"
  ]
}

Custom Git Merge Driver

# .gitattributes
.cleo/todo.json merge=cleo

# Git config
git config merge.cleo.driver "cleo merge %O %A %B"

Merge Strategy

Use version number + timestamp for conflict detection:
{
  "_meta": {
    "version": 47,
    "lastModifiedAt": "2025-12-20T14:30:00Z"
  }
}
On git merge conflict:
  1. Compare _meta.version - higher wins
  2. Tiebreak: compare _meta.lastModifiedAt - more recent wins
  3. If still tied: git’s standard conflict markers, user resolves
Future enhancement: Custom git merge driver for automatic resolution.

7. Implementation Roadmap

Phase 1: Foundation (v0.24.0)

TaskScopePriority
Add _meta.version (increment) to todo.jsonSmallP0
Add _meta.lastWriterVersion trackingSmallP0
Add exit codes 30-39 to lib/exit-codes.shSmallP0
Create ~/.cleo/registry/ structureSmallP0
Create lib/registry.sh with CRUD operationsMediumP0
Modify init.sh to register projectsSmallP0
Add ct registry list commandMediumP1

Phase 2: Health System (v0.25.0)

TaskScopePriority
Create lib/health.sh with 36 checksLargeP0
Add ct health command with JSON outputMediumP0
Implement health score calculationMediumP1
Add ct health --fix with safety levelsMediumP1
Add health caching with TTLSmallP2

Phase 3: Version Guards (v0.25.0)

TaskScopePriority
Add fast_version_check to wrapperMediumP0
Add validate_version_with_policy to validation.shMediumP0
Integrate version check in write scriptsMediumP0
Add CLEO_VERSION_CHECK env overrideSmallP1
Add config: migration.policy, migration.checkOnWriteSmallP1

Phase 4: Git Sync (v0.26.0)

TaskScopePriority
Add project UUID to .cleo/project.jsonSmallP1
Add custom git merge driverMediumP2
Add registry reconciliationMediumP2

Phase 5: Scalability (v0.27.0)

TaskScopePriority
Add sharded registry (50+ projects)HighP3
Add SQLite backend (500+ projects)HighP3
Add ct registry gc commandMediumP3

8. Files to Create/Modify

New Files

FilePurpose
lib/registry.shRegistry CRUD operations
lib/health.shHealth check engine
scripts/registry.shRegistry command handler
scripts/health.shHealth command handler
schemas/registry.schema.jsonRegistry JSON schema
schemas/health.schema.jsonHealth output JSON schema
templates/registry.template.jsonEmpty registry template
templates/project.template.jsonProject identity template

Modified Files

FileChanges
lib/exit-codes.shAdd exit codes 30-59
lib/validation.shAdd validate_version_with_policy()
scripts/init.shAdd project registration, create project.json
install.shCreate registry directory, add registry command
schemas/todo.schema.jsonAdd _meta.version, lastModifiedBy
schemas/config.schema.jsonAdd migration.policy, migration.checkOnWrite
scripts/add.shAdd version check after lock
scripts/update.shAdd version check after lock
scripts/complete.shAdd version check after lock
scripts/archive.shAdd version check after lock
Wrapper scriptAdd fast_version_check()

Appendix A: Gap Resolutions

Issues identified by Spec Challenge Agent and resolutions:
GapResolution
head+grep fragilityAdd jq fallback: grep ... || jq ...
Exit code 25 arbitraryUse semantic range 30-39
Multi-file version consistencyAdd cross-file check before archive ops
”Project ahead” should blockChanged to block, not warn
Incomplete WRITE_COMMANDSAudit: add restore, config set
lastWriterVersion uses jqEmbed in atomic write jq call
Error recovery missingAdded rollback via ct restore
Plugin integrationDocumented as “not supported in v1”

Appendix B: Superseded Documents

This specification supersedes:
  1. docs/archive/specs-md/VERSION-GUARD-SPEC.md (archived) - Original proposal (contradicted by challenge agents)
  2. docs/archive/specs-md/VERSION-GUARD-FINAL-DESIGN.md (archived) - Refined but narrow (version-only focus)
The above documents should be marked with:
> **SUPERSEDED**: This document has been superseded by [HEALTH-REGISTRY-SPEC.md](./HEALTH-REGISTRY-SPEC.md)

Appendix C: Success Criteria

  1. Performance: < 5ms for fast version check, < 100ms for full health
  2. Safety: No data corruption on version mismatch
  3. UX: Clear, actionable JSON messages for LLM consumption
  4. Compatibility: Existing scripts/automation continue to work
  5. Testability: All new code has unit + integration tests
  6. Coverage: 36 health checks across all data integrity dimensions

Document version: 1.0.0 Generated by: Sequential thinking + 5 challenge agents synthesis