#!/usr/bin/env python3
"""
Context Window Optimization Script
Based on IndyDevDan's Elite Context Engineering

Usage:
    uv run context-check.py [--threshold 80]

This script helps manage agent context windows by:
1. Checking current usage
2. Suggesting optimizations
3. Identifying bloat sources
"""

# /// script
# dependencies = [
#   "rich",
# ]
# ///

import argparse
from rich.console import Console
from rich.table import Table
from rich.panel import Panel

console = Console()

def analyze_context(threshold: int = 80):
    """
    Analyze context window usage and provide recommendations.

    Based on IndyDevDan's R&D Framework:
    - R: Reduce (remove unnecessary context)
    - D: Delegate (offload to sub-agents)
    """

    console.print(Panel.fit(
        "[bold cyan]Context Window Optimization[/bold cyan]\n"
        "Based on IndyDevDan's R&D Framework",
        border_style="cyan"
    ))

    # R&D Framework recommendations
    recommendations = Table(title="R&D Framework Actions")
    recommendations.add_column("Action", style="cyan")
    recommendations.add_column("Description", style="white")
    recommendations.add_column("Command", style="green")

    recommendations.add_row(
        "REDUCE",
        "Turn off autocompact to reclaim 22% context",
        "/config autocompact false"
    )
    recommendations.add_row(
        "REDUCE",
        "Check current context usage",
        "/context"
    )
    recommendations.add_row(
        "DELEGATE",
        "Offload search to scout sub-agents",
        "/scout [task]"
    )
    recommendations.add_row(
        "DELEGATE",
        "Use sub-agents for isolated tasks",
        "Task tool with isolated context"
    )
    recommendations.add_row(
        "REDUCE",
        "Delete agents when job complete",
        "Delete all agents"
    )

    console.print(recommendations)

    # Context management tips
    tips = """
[bold]Key Context Management Principles:[/bold]

1. [cyan]Focused > Bloated[/cyan]
   - 200k context is plenty if focused
   - Don't force agent to context switch

2. [cyan]Progressive Disclosure[/cyan]
   - Only load what's needed when needed
   - Skills use 3 levels: metadata → instructions → resources

3. [cyan]Agent as Deletable Resource[/cyan]
   - Spin up, do work, delete
   - Don't keep stale agents around

4. [cyan]The 50% Rule[/cyan]
   - If context > 50%, consider sub-agents
   - Scout-Plan-Build keeps planner clean
"""
    console.print(Panel(tips, title="Context Tips", border_style="green"))

def main():
    parser = argparse.ArgumentParser(description="Context Window Optimizer")
    parser.add_argument("--threshold", type=int, default=80,
                       help="Warning threshold for context usage (default: 80)")
    args = parser.parse_args()

    analyze_context(args.threshold)

    console.print("\n[bold green]Run /context in Claude Code to see actual usage[/bold green]")

if __name__ == "__main__":
    main()
