#!/usr/bin/env python3
from pathlib import Path
import sys

ROOT = Path(__file__).resolve().parents[1]
LIMITS = {
    'AGENTS.md': 8 * 1024,
    'CLAUDE.md': 4 * 1024,
    'GEMINI.md': 4 * 1024,
    '.github/copilot-instructions.md': 4 * 1024,
    '.windsurfrules': 3 * 1024,
    # Loaded whole on every skill trigger — the single biggest per-task cost.
    'skills/adghost-lily/SKILL.md': 24 * 1024,
}
# Always-on dirs also get a TOTAL cap so file-count creep can't re-bloat the boot context.
DIR_TOTAL_LIMITS = {
    '.claude/rules': 10 * 1024,
}
# Scoped rules should remain compact. References may be long because they are loaded on demand.
SCOPED_DIR_LIMITS = {
    '.cursor/rules': 3 * 1024,
    '.claude/rules': 3 * 1024,
    '.clinerules': 3 * 1024,
    '.roo/rules': 3 * 1024,
    '.continue/rules': 3 * 1024,
    '.github/instructions': 3 * 1024,
}

failed = False
for rel, limit in LIMITS.items():
    p = ROOT / rel
    if p.exists():
        size = len(p.read_bytes())
        status = 'OK' if size <= limit else 'TOO LARGE'
        print(f'{status:9} {rel:45} {size:6} / {limit}')
        failed = failed or size > limit

for rel, limit in SCOPED_DIR_LIMITS.items():
    d = ROOT / rel
    if d.exists():
        for p in sorted(d.rglob('*')):
            if p.is_file():
                size = len(p.read_bytes())
                status = 'OK' if size <= limit else 'TOO LARGE'
                print(f'{status:9} {p.relative_to(ROOT).as_posix():45} {size:6} / {limit}')
                failed = failed or size > limit

for rel, limit in DIR_TOTAL_LIMITS.items():
    d = ROOT / rel
    if d.exists():
        total = sum(len(p.read_bytes()) for p in d.rglob('*') if p.is_file())
        status = 'OK' if total <= limit else 'TOO LARGE'
        print(f'{status:9} {rel + "/ (total)":45} {total:6} / {limit}')
        failed = failed or total > limit

sys.exit(1 if failed else 0)
