Bruno-Cunha-Souza/ValarMindSkills / skills/context-optimization
context-optimization
UnverifiedLifecycle context audit/optimize/plan. Audit mode (Phase 0..6) + Guide mode (catalog lookup). Targets long-conv agents, RAG pipelines, sub-agents, large-doc workflows. Detects bloat/dedup/cache/masking/partition opportunities. Emits findings (SAFE/REVIEW/BREAKING) + optimization plan + token delta. Read-only — LGTM if sound. Triggers: 'optimize context', 'compact context', 'auditar contexto', 'otimizar contexto', '/context-optimization'.
| File | Kind | Size |
|---|---|---|
| EXAMPLE.md | markdown | 13.7 KB |
| SKILL.md | skill md | 10.5 KB |
| references/CHECKLIST.md | markdown | 5.7 KB |
| references/HARNESS_NOTES.md | markdown | 7.8 KB |
| references/SEVERITY_RUBRIC.md | markdown | 9.0 KB |
| references/TECHNIQUES.md | markdown | 19.6 KB |
| references/USE_CASES.md | markdown | 13.8 KB |
| scripts/.gitignore | other | 139 B |
| scripts/README.md | markdown | 6.8 KB |
| scripts/ctxopt/Cargo.lock | manifest | 27.2 KB |
| scripts/ctxopt/Cargo.toml | manifest | 676 B |
| scripts/ctxopt/src/cli.rs | script | 3.3 KB |
| scripts/ctxopt/src/count.rs | script | 5.5 KB |
| scripts/ctxopt/src/dedup.rs | script | 5.5 KB |
| scripts/ctxopt/src/emit.rs | script | 4.4 KB |
| scripts/ctxopt/src/finding.rs | script | 2.3 KB |
| scripts/ctxopt/src/main.rs | script | 7.4 KB |
| scripts/ctxopt/src/report.rs | script | 11.8 KB |
| scripts/ctxopt/src/scan.rs | script | 5.1 KB |
| scripts/ctxopt/src/toon.rs | script | 5.6 KB |
| scripts/ctxopt/tests/integration.rs | script | 3.2 KB |
| scripts/run-all.shexecutable | script | 1.3 KB |
scripts/ctxopt/src/scan.rs
View on GitHub//! `ctxopt scan` — file-size scanner. Equivalent to legacy 00-context-scan.sh.
use crate::cli::SizeThresholds;
use crate::emit::{log_finding, FindingWriter};
use crate::finding::{Category, Finding, Severity};
use anyhow::{Context, Result};
use serde_json::json;
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};
const TARGET_EXTENSIONS: &[&str] = &["md", "json", "jsonl", "txt"];
const EXCLUDED_PARTS: &[&str] = &[".git", "node_modules", ".venv", "__pycache__", "out", "target"];
fn is_excluded(entry: &DirEntry) -> bool {
entry
.path()
.components()
.any(|c| EXCLUDED_PARTS.iter().any(|ex| c.as_os_str() == *ex))
}
fn has_target_extension(path: &Path) -> bool {
path.extension()
.and_then(|e| e.to_str())
.map(|e| TARGET_EXTENSIONS.contains(&e))
.unwrap_or(false)
}
pub fn run(
project_root: &Path,
thresholds: &SizeThresholds,
writer: &mut dyn FindingWriter,
quiet: bool,
) -> Result<()> {
let project_root = project_root
.canonicalize()
.with_context(|| format!("project_root not readable: {}", project_root.display()))?;
if !quiet {
eprintln!(
"[INFO] Scanning {} for context candidates ...",
project_root.display()
);
}
let mut candidates: Vec<(PathBuf, u64)> = Vec::new();
for entry in WalkDir::new(&project_root).into_iter().filter_entry(|e| !is_excluded(e)) {
let entry = match entry {
Ok(e) => e,
Err(_) => continue,
};
if !entry.file_type().is_file() {
continue;
}
if !has_target_extension(entry.path()) {
continue;
}
let size = entry.metadata().map(|m| m.len()).unwrap_or(0);
candidates.push((entry.into_path(), size));
}
candidates.sort_by(|a, b| b.1.cmp(&a.1));
let total_files = candidates.len();
let total_bytes: u64 = candidates.iter().map(|(_, s)| s).sum();
if !quiet {
eprintln!("[INFO] Found {total_files} candidate files.");
}
if total_files == 0 {
if !quiet {
eprintln!("[OK] No context candidates found in {}", project_root.display());
}
return Ok(());
}
for (rank, (path, size)) in candidates.iter().take(20).enumerate() {
let rank = rank + 1;
let est_tokens = size / 4;
let rel = path
.strip_prefix(&project_root)
.unwrap_or(path)
.to_string_lossy()
.to_string();
let (severity, impact, remediation) = if *size >= thresholds.threshold_high {
(
Severity::High,
format!(
"File alone consumes ~{est_tokens} tokens (~{}KB). On a 200k window, this is {}% of total budget.",
size / 1024,
est_tokens * 100 / 200_000
),
"Apply §3 observation masking if this file is a tool output, or §6 verbatim deletion if citation-bound. For source code in audit scope, consider §7 partitioning.".to_string(),
)
} else if *size >= thresholds.threshold_medium {
(
Severity::Medium,
format!(
"File consumes ~{est_tokens} tokens. Cumulative bloat across multiple files this size adds up."
),
"Review for dedup opportunities (§10) and unused content.".to_string(),
)
} else if *size >= thresholds.threshold_low {
(
Severity::Low,
format!(
"File consumes ~{est_tokens} tokens — within typical budget but contributes to total."
),
"Track across runs; flag if this category grows.".to_string(),
)
} else {
continue;
};
let evidence = json!({
"path": rel,
"size_bytes": size,
"estimated_tokens": est_tokens,
"rank": rank,
});
let finding = Finding::new(
format!("CTX-SCAN-{rank:03}"),
"scan",
severity,
Category::Bloat,
rel,
impact,
remediation,
evidence,
);
log_finding(&finding, quiet);
writer.write(&finding)?;
}
let total_kb = total_bytes / 1024;
let total_est_tokens = total_bytes / 4;
let summary = Finding::new(
"CTX-SCAN-SUM",
"scan",
Severity::Info,
Category::Bloat,
project_root.to_string_lossy(),
format!(
"Scanned {total_files} files totalling ~{total_kb} KB (~{total_est_tokens} tokens)"
),
"Use this as the Phase 1 inventory baseline. Compare to use-case ceiling per Phase 5.3.",
json!({
"total_files": total_files,
"total_bytes": total_bytes,
"total_kb": total_kb,
"estimated_total_tokens": total_est_tokens,
}),
);
log_finding(&summary, quiet);
writer.write(&summary)?;
if !quiet {
eprintln!("[OK] Context scan complete.");
}
Ok(())
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |