Skip to content

Add hull integrity monitoring and relief on station#16

Merged
harrymunro merged 3 commits intomainfrom
context-management
Feb 20, 2026
Merged

Add hull integrity monitoring and relief on station#16
harrymunro merged 3 commits intomainfrom
context-management

Conversation

@harrymunro
Copy link
Owner

@harrymunro harrymunro commented Feb 20, 2026

Summary

Ships in a Nelson squadron now have damage control for the one thing that actually sinks AI agents: running out of context window.

The admiral monitors hull integrity across the entire squadron by reading exact token counts from Claude Code session JSONL files. No estimation heuristics, no paid APIs, no external dependencies — the numbers come straight from the API usage data that Claude Code already records on every assistant turn.

Each subagent (ship) has its own JSONL file at {session-id}/subagents/agent-{agentId}.jsonl, separate from the flagship's session file. The admiral scans the whole directory in one pass to build a squadron readiness board — a single view of every ship's context health.

When a ship's hull integrity drops to Red, the admiral orchestrates relief on station — a tactical handover borrowed from Royal Navy patrol doctrine. The damaged ship writes a rich turnover brief to file (not a message, to keep the replacement's context clean), a fresh ship reads it and picks up exactly where the other left off. The flagship monitors itself too, and can hand over to a new session before it founders.

What's new

3 damage control procedures:

  • Hull integrity — Four-tier threshold system (Green/Amber/Red/Critical) with a squadron readiness board the admiral maintains at each quarterdeck checkpoint
  • Relief on station — Full handover sequence including flagship self-monitoring, chained reliefs (A → B → C with bounded cumulative briefs), and a crew variant for captain-level recovery
  • Session hygiene — Clean start procedure that clears stale damage reports and turnover briefs, with an archive option for previous sessions

2 admiralty templates:

  • Damage report — JSON format for hull integrity reporting, stored at .claude/nelson/damage-reports/{ship-name}.json
  • Turnover brief — Rich handover document with standard and flagship variants, including relief chain history

1 utility script:

  • count-tokens.py — Three modes of operation:
    • --session — Read a single JSONL file for exact token counts
    • --squadron — Scan a session directory for the flagship + all subagent JSONL files and produce a full readiness board
    • --file — Heuristic fallback for plain text (character count / 4)

SKILL.md integration:

  • Session hygiene hooked into Step 1 (Issue Sailing Orders)
  • Hull integrity checks added to Step 4 (Quarterdeck Rhythm)
  • Turnover brief reference added to Step 3 (Battle Plan)
  • Three new entries in the Damage Control table

The key insight

Claude Code already records exact token counts (input_tokens + cache_creation_input_tokens + cache_read_input_tokens) on every assistant turn in the session JSONL. We don't need to estimate anything — we just read the last assistant message's usage data.

Ships can't easily self-monitor (they don't know their own agent ID to locate their JSONL), but that's the correct pattern: the flagship monitors everyone. The admiral runs --squadron against the session directory at each quarterdeck checkpoint and gets the full picture.

Live readiness board from the session that built this PR:

Ship Tokens Hull Status
Flagship 104,365 47% Red
HMS Kent 26,952 86% Green
HMS Argyll 29,341 85% Green
HMS Daring 34,693 82% Green
HMS Astute 57,269 71% Amber

Test plan

  • --session produces correct JSON with exact token counts from JSONL
  • --squadron scans flagship + all subagent files and returns a readiness board
  • --file falls back to heuristic for plain text
  • Hull integrity thresholds trigger correctly (tested Green at 98%, Critical at 27%, Red at 47%)
  • Subagent JSONL files are separate from flagship — no cross-contamination
  • scripts/check-references.sh passes — all cross-references valid
  • No existing files broken — only additive changes to SKILL.md and CLAUDE.md
  • Live test in a Nelson mission with actual relief-on-station handover

Remove references to deleted index files (admiralty-templates.md,
damage-control.md, standing-orders.md) and add missing entries
(royal-marines.md, commendations.md, battalion-ashore.md,
marine-deployment-brief.md, scripts/) to README and CLAUDE.md.
Fix $nelson to /nelson in agent definition.
Ships can now monitor their context window consumption and file
structured damage reports. When a ship's hull integrity drops to
Red, the admiral orchestrates relief on station — a tactical
handover where the damaged ship writes a turnover brief to file
and a fresh replacement reads it to continue the mission.

New damage control procedures:
- hull-integrity.md: threshold definitions, squadron readiness board
- relief-on-station.md: planned ship replacement with chained reliefs
- session-hygiene.md: clean start procedure for new sessions

New admiralty templates:
- damage-report.md: JSON format for hull integrity reports
- turnover-brief.md: standard and flagship handover briefs

New script:
- count-tokens.py: reads exact token counts from Claude Code session
  JSONL files (zero external dependencies, no estimation needed)
The flagship monitors hull integrity across all ships by scanning the
session directory. Subagent JSONL files live at {session}/subagents/
and each contains its own API usage data, so the flagship reads every
ship's exact token count in one pass.

Ships cannot easily self-monitor (they do not know their own agent ID
to locate their JSONL file), but the admiral handles all hull integrity
checks at each quarterdeck checkpoint — which is the correct pattern.
@harrymunro harrymunro merged commit 7cd2bed into main Feb 20, 2026
5 checks passed
@harrymunro harrymunro deleted the context-management branch February 20, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant