Skip to content

Repository files navigation

Token Optimizer

Pipeline de otimizacao de tokens para agentes Cursor/IA. Corta ~50k tokens de log bruto para ~300 tokens, elimina loops infinitos de correcao, e prioriza modelo barato (Flash) para erros simples.

Funciona com QUALQUER comando shell: Gradle, Flutter, Cargo, Python, Docker, kubectl, npm, etc.

Uso rapido

# Interceptar build com filtro de log
python3 smart_build.py "npm run build"

# Comprimir arquivo fonte (skeletonize)
python3 smart_build.py --compress src/main.py --level 2

# AST fold: so metodo alterado + dependencias (tree-sitter)
python3 smart_build.py --compress src/Main.java --level 2 --diff-aware

# Ver diff em vez de reler arquivo
python3 smart_build.py --diff src/main.py

# Resetar circuit breaker
python3 smart_build.py --reset

Componentes

Arquivo Funcao
smart_build.py Interceptor universal + log filter + circuit breaker + compress + AST fold
smart_build_dart_compress.dart Compressor Dart nativo (brace-depth tracking)
.venv/ Virtualenv com tree-sitter (opcional, para --diff-aware)
.cursor/rules/execution-cost.mdc Regras always-apply para Cursor

Como funciona

Build falha (50k tokens de log)
    │
    ▼
smart_build.py filtra:
    ├── Remove ANSI, timestamps, progresso, warnings
    ├── Remove paths de terceiros (node_modules, .cargo)
    ├── Mantem so Error:/Exception/Panic + contexto
    ├── Cap adaptativo: 10/25/40 linhas por tentativa
    ├── Header fixo "=== SMART BUILD ERROR ==="
    └── Timer: 3 tentativas max → circuit breaker
    │
    ▼
Agente recebe ~300 tokens em vez de ~50k

Resultados reais

Linguagem Compressao (--level 2) Metodo
Python 86.6% AST nativo (stdlib)
Dart 79.2% Dart nativo (brace-depth)
Rust 82.0% Regex
Java (regex) ~50% Regex
Java (tree-sitter --diff-aware) 63-99% AST tree-sitter
Logs de terminal 97-99% Regex filter

Instalacao

1. Scripts (obrigatorio)

cp smart_build.py /seu/projeto/scripts/
chmod +x scripts/smart_build.py

Para compressao Dart:

cp smart_build_dart_compress.dart /seu/projeto/scripts/

1b. AST-Based Code Folding (opcional, recomendado)

Para --diff-aware usar tree-sitter:

python3 -m venv .venv
.venv/bin/pip install tree-sitter tree-sitter-java tree-sitter-python tree-sitter-rust

O script auto-deteta .venv/ no mesmo diretorio ou no pai.

2. Regras Cursor (recomendado)

cp -r .cursor /seu/projeto/

3. Usar em vez de comandos brutos

# Em vez de:
./gradlew test

# Use:
python3 scripts/smart_build.py "./gradlew test"

Para forcar o Cursor a sempre usar o interceptor, copie .cursor/rules/execution-cost.mdc para o projeto.

Linguagens suportadas

Sistema Comando exemplo
Gradle/Java smart_build.py "cd backend && ./gradlew test"
Flutter/Dart smart_build.py "flutter analyze"
Cargo/Rust smart_build.py "cargo check"
Python smart_build.py "python -m pytest"
npm/Node smart_build.py "npm run build"
Docker smart_build.py "docker compose up"
kubectl smart_build.py "kubectl apply -f manifest.yaml"
Qualquer shell smart_build.py "bash deploy.sh"

Circuit Breaker

Apos 3 falhas consecutivas no mesmo comando, o script bloqueia:

CIRCUIT BREAKER TRIPPED
3 consecutive build failures on the same command.
=== MANUAL INTERVENTION REQUIRED ===

Reset: python3 smart_build.py --reset

AST-Based Code Folding (--diff-aware)

Combina tree-sitter + git diff para fold cirurgico.

O que faz: em vez de skeletonizar o arquivo todo, ele:

  1. Parse o AST com tree-sitter (Java, Python, Rust)
  2. Le o git diff para achar linhas alteradas
  3. Mapeia linhas alteradas para metodos na AST
  4. Mantem o metodo alterado com corpo completo
  5. Mantem assinaturas das dependencias diretas (metodos chamados)
  6. Remove 100% das classes e metodos nao relevantes

Resultado: um arquivo Java de 3.000 linhas vira ~40 linhas (economia de 99%).

Uso:

python3 smart_build.py --compress src/Main.java --level 2 --diff-aware

Dependencia: tree-sitter (opcional, auto-deteta .venv/).

O pipeline foi desenhado para maximizar cache hit:

  • Header fixo === SMART BUILD ERROR === → prefixo identico = cache hit
  • Sem timestamps → erros identicos geram texto identico
  • Sem variacao textual → prompts mais estaveis entre execucoes

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages