Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
68 changes: 68 additions & 0 deletions .agents/skills/phi-loop/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
id: phi-loop
name: PHI LOOP
description: Execute 9-phase spec-first development workflow (Issue β†’ Spec β†’ TDD β†’ Code β†’ Gen β†’ Seal β†’ Verify β†’ Land β†’ Learn)
---

# PHI LOOP Skill

Execute the canonical t27 development workflow following all 7 invariant laws.

## Phases

| Phase | Name | Description | Artifacts |
|-------|------|-------------|------------|
| 1 | Issue | Define problem or requirement | #N ticket in repo |
| 2 | Spec | Write .t27 specification | `specs/ring-NNN-name.t27` |
| 3 | TDD | Write tests before code | test cases in .t27 |
| 4 | Code | Implement according to spec | src/ files (manual) |
| 5 | Gen | Run `tri gen` to generate | `gen/` files |
| 6 | Seal | Verify generated code, seal hash | seal hash |
| 7 | Verify | Run `tri test` for conformance | test results |
| 8 | Land | Merge changes to main | git merge |
| 9 | Learn | Capture learnings | episodes.jsonl entry |

## Commands

```bash
# Start PHI LOOP for ring N
tri phi-loop N

# Advance to next phase
tri next-phase

# Show current phase status
tri status

# Reset current ring
tri reset
```

## Law Compliance

Execute with these constraints:
- **L1 TRACEABILITY**: All commits must reference issue #N
- **L2 GENERATION**: Never edit files in gen/ manually
- **L3 PURITY**: Source files must be ASCII-only with English identifiers
- **L4 TESTABILITY**: Every .t27 spec must contain test/invariant/bench
- **L5 IDENTITY**: φ² + 1/φ² = 3; use tolerance for f64
- **L6 CEILING**: FORMAT-SPEC-001.json + gf16.t27 are numeric SSOT
- **L7 UNITY**: No new shell scripts; use tri/t27c

## Exit Condition

PHI LOOP is complete when:
1. All 9 phases have executed
2. Agent V (Verification) passes
3. Changes are landed to main branch
4. Episode is saved to experience log

## Output

On completion:
```
Phase complete: Learn
β†’ Next ring: N+1
```

This triggers creation of next ring branch.
107 changes: 107 additions & 0 deletions .agents/skills/tri-pipeline/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
id: tri-pipeline
name: TRI Pipeline
description: Execute tri commands (gen, test, verify, seal, verdict) for spec-first development
---

# TRI Pipeline Skill

Execute the canonical t27 toolchain commands.

## Commands

### `tri gen`
Generate code from .t27 specifications.

```bash
tri gen specs/ring-NNN-name.t27
```

**Output**: Generated files in `gen/` directory
**Laws**: L2 (GENERATION) - gen/ is read-only
**Verification**: Hash verification during seal phase

### `tri test`
Run conformance tests from specifications.

```bash
tri test specs/ring-NNN-name.t27
```

**Output**: Test results, pass/fail status
**Laws**: L4 (TESTABILITY) - specs must have tests
**Success Criteria**: All tests pass, invariants satisfied

### `tri verify`
Verify all 7 invariant laws.

```bash
tri verify
```

**Checks**:
- L1: Commits have issue references
- L2: No manual edits to gen/
- L3: ASCII-only source files
- L4: All specs have tests
- L5: Ο† identity constraints
- L6: FORMAT-SPEC-001.json authority
- L7: No new shell scripts on critical path

**Output**: Pass/fail for each law
**Block**: Non-compliant commits are blocked

### `tri seal`
Generate and verify seal hash.

```bash
tri seal specs/ring-NNN-name.t27
```

**Output**: Hash of generated artifacts
**Purpose**: Immutable snapshot for verification

### `tri verdict`
Generate formal pass/fail verdict.

```bash
tri verdict
```

**Output**:
- Overall status: PASS | FAIL
- Law compliance breakdown
- Required fixes for failures

### `tri experience save`
Save episode to experience log.

```bash
tri experience save --ring 72 --phase verify --outcome success
```

**Output**: Entry in `~/.trinity/experience/episodes.jsonl`

### `tri experience query`
Search past episodes.

```bash
tri experience query "how to fix L5 violation"
```

**Output**: Relevant past episodes with solutions

## Error Handling

If a command fails:
1. Log error with context
2. Suggest fix based on error type
3. Check experience for similar past issues
4. Retry with modified inputs if applicable

## Success Indicators

- Command exits with code 0
- Output contains expected patterns
- No law violations detected
- Artifacts are generated correctly
181 changes: 181 additions & 0 deletions .agents/skills/tri/doctor-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Doctor Loop β€” Watchdog, Self-Healing, and Safety

Doctor agent runs independently of task agents. Never takes regular PHI LOOP tasks.

## Agent Definition

```json
{
"agent_id": "tri-doctor",
"role": "Swarm Health & Recovery",
"type": "watchdog",
"runs_independently": true,
"takes_feature_tasks": false,
"schedule": "periodic: 60s"
}
```

## Scope

Doctor monitors:
- **SacredPhysics** β€” TRINITY, G, ΩΛ, tpresent, Ξ³ conformance
- **Numeric** β€” GoldenFloat family, TF, IPS formats
- **Graph** β€” Dependency graph integrity, topology, tier violations
- **Compiler** β€” Parser, codegen, type checking
- **Runtime** β€” CLI commands, execution, validation
- **QueenLotus** β€” Swarm orchestration, task queue, claim ownership
- **Swarm** β€” Overall: toxic rate, repeat failures, stuck tasks

## Continuous Monitoring

Every 60 seconds, Doctor:

1. **Reads state:**
- `.trinity/state/queen-health.json`
- `.trinity/state/swarm-health.json`
- `.trinity/state/ownership-index.json`

2. **Analyzes events:**
- Scans `.trinity/events/akashic-log.jsonl` for anomalies
- Checks for toxic verdict trends
- Identifies stale claims (no heartbeat > TTL)
- Detects missing FUTURE in loop.handoff

3. **Emits health event:**
```json
{"event":"health.snapshot","queen_health":0.95,"swarm_health":0.92,"toxic_rate":0.02}
```

## Anomaly Detection

Doctor detects these anomalies:

| Type | Trigger | Severity | Action |
|-------|---------|----------|--------|
| `toxic_rate_increase` | Toxic rate > 5% | Warning: emit health.anomaly |
| `stale_claim_detected` | Claim expired, no heartbeat | Critical: emit health.anomaly + reclaim |
| `missing_future_handoff` | Loop completed without FUTURE | Warning: emit health.anomaly |
| `domain_below_threshold` | Domain health < 0.5 | Critical: emit health.anomaly + domain recovery |
| `state_divergence` | State != events.replay | Error: emit health.anomaly + emergency alert |
| `queen_below_critical` | queen_health < 0.5 | Critical: BLOCK all mutations |

## Self-Healing Procedures

### 1. Stale Claim Recovery

When stale claim detected:
```bash
# Reclaim claim
./scripts/replay-step.sh --reclaim-claim <claim_id>

# Write claim.reclaim event
echo '{"event":"claim.reclaim",...}' >> .trinity/events/akashic-log.jsonl
```

### 2. Domain Recovery

When domain health drops below threshold:
```bash
# Domain-specific recovery
./scripts/replay-step.sh --domain <affected>

# Emit recovery.start event
echo '{"event":"recovery.start","type":"domain","domain":"<domain>"}' >> .trinity/events/akashic-log.jsonl
```

### 3. Queen Recovery

When queen_health < 0.5:
```bash
# Full swarm recovery
./scripts/replay-step.sh --last-clean

# BLOCK all new mutations
echo "QUEEN CRITICAL: Only recovery allowed" >> .trinity/state/queen-health.json

# Notify agents
for agent in $(ls .trinity/agents/); do
# Send notification (implementation-dependent)
done
```

## Doctor Loop Schedule

```bash
# Every 60 seconds
while true; do
# Read state and events
read .trinity/state/*.json
tail -100 .trinity/events/akashic-log.jsonl

# Analyze for anomalies
# ... (analysis logic)

# If anomaly detected:
# - Emit health.anomaly event
# - Trigger appropriate recovery
# - Log action taken

sleep 60
done
```

## Integration with /tri

Doctor Loop should be integrated into tri CLI:

```bash
# Start Doctor Loop
tri doctor start

# Stop Doctor Loop
tri doctor stop

# Show Doctor status
tri doctor status

# Show recent anomalies
tri doctor anomalies
```

## Status Output

```
╔══════════════════════════════════════════════════════╗
β•‘ Tri Doctor Status β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Queen Health: 0.95 (GREEN)
Swarm Health: 0.92 (GREEN)
Toxic Rate: 0.02 (2% of 127 steps)
Stale Claims: 0
Missing FUTURE: 0

Recent Anomalies:
- [2026-04-04T13:00:00Z] health.anomaly: Numeric toxic_rate 15%↑ β†’ WARNING
- [2026-04-03T10:30:00Z] claim.reclaim: expired claim on specs/base/ops.t27 β†’ RECLAIMED

Active Recoveries:
- [recovery-001] Started at 12:00 (GF8 toxic recovery) β€” IN PROGRESS

─────────────────────────────────────
Doctor Loop: ACTIVE (running since 2026-04-04T12:00:00Z)
Next check in: 58 seconds
```

## Permissions

| Permission | Description |
|-----------|-------------|
| `read.trinity` | Read all .trinity files (events, claims, queue, state, experience) |
| `write.state` | Write .trinity/state/* files |
| `write.events` | Append to .trinity/events/akashic-log.jsonl |
| `write.claims` | Write .trinity/claims/{active,released}/* |
| `reclaim` | Reclaim stale claims (write + release) |
| `schedule` | Add recovery tasks to .trinity/queue/ |
| `emit.health` | Emit health events to .trinity/events |

Doctor NEVER needs:
- `write.spec` β€” Not allowed to modify specs
- `tri.skill.*` β€” Not allowed to run PHI LOOP
- Direct mutation β€” Only triggers recovery
Loading
Loading