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/toon.rs
View on GitHub//! TOON v3.0 thin emitter — Token-Oriented Object Notation.
//!
//! Spec: <https://github.com/toon-format/spec>
//!
//! This module supports the subset needed for ctxopt output:
//! - Uniform array of objects → tabular form: `key[N]{f1,f2,...}:` header + tab-separated rows.
//! - Scalar key-value lines: `key: value`.
//! - Nested key-value blocks via 2-space indentation.
//! - Multi-line string content via `|` literal blocks with indentation.
//!
//! Round-trip with TOON-format reference parsers is in scope of unit tests.
use crate::finding::Finding;
use std::io::{self, Write};
/// Sanitize a string for use as a tab-separated TOON cell.
/// Replaces tabs/newlines/carriage returns with spaces; trims trailing whitespace.
fn sanitize_cell(s: &str) -> String {
s.replace(['\t', '\n', '\r'], " ").trim().to_string()
}
/// Emit a uniform-array block header: `key[N]{f1,f2,...}:`
pub fn write_uniform_header<W: Write>(
w: &mut W,
key: &str,
count: usize,
fields: &[&str],
) -> io::Result<()> {
write!(w, "{key}[{count}]{{")?;
for (i, f) in fields.iter().enumerate() {
if i > 0 {
write!(w, ",")?;
}
w.write_all(f.as_bytes())?;
}
writeln!(w, "}}:")?;
Ok(())
}
/// Emit a single uniform-array row with 2-space indent + tab-separated cells.
pub fn write_uniform_row<W: Write>(w: &mut W, cells: &[String]) -> io::Result<()> {
write!(w, " ")?;
for (i, cell) in cells.iter().enumerate() {
if i > 0 {
w.write_all(b"\t")?;
}
w.write_all(sanitize_cell(cell).as_bytes())?;
}
writeln!(w)?;
Ok(())
}
/// Emit a scalar key-value line.
pub fn write_scalar<W: Write>(w: &mut W, key: &str, value: &str) -> io::Result<()> {
writeln!(w, "{key}: {value}")
}
/// Emit a list of findings as `findings[N]{...}:` uniform array (or `findings[]{}: empty`).
pub fn write_findings<W: Write>(w: &mut W, findings: &[Finding]) -> io::Result<()> {
let fields = [
"id",
"phase",
"severity",
"category",
"target",
"impact",
"remediation",
"timestamp",
];
write_uniform_header(w, "findings", findings.len(), &fields)?;
for f in findings {
let cells = vec![
f.id.clone(),
f.phase.clone(),
f.severity.to_string(),
f.category.to_string(),
f.target.clone(),
f.impact.clone(),
f.remediation.clone(),
f.timestamp.clone(),
];
write_uniform_row(w, &cells)?;
}
// Heterogeneous evidence goes in a secondary block (one record per finding).
let any_evidence = findings
.iter()
.any(|f| !f.evidence.is_null() && f.evidence != serde_json::json!({}));
if any_evidence {
writeln!(w)?;
writeln!(w, "evidence_blob:")?;
for f in findings {
if f.evidence.is_null() || f.evidence == serde_json::json!({}) {
continue;
}
writeln!(w, " - id: {}", f.id)?;
let json = serde_json::to_string(&f.evidence).unwrap_or_default();
writeln!(w, " json: {json}")?;
}
}
Ok(())
}
/// Emit a summary block grouping severity/category counts and a verdict scalar.
#[allow(clippy::too_many_arguments)]
pub fn write_summary<W: Write>(
w: &mut W,
total: usize,
severity_counts: &[(&str, usize)],
category_counts: &[(&str, usize)],
verdict_rework: bool,
verdict_review: bool,
verdict_pass_with_notes: bool,
) -> io::Result<()> {
write_scalar(w, "total", &total.to_string())?;
write_uniform_header(w, "counts", severity_counts.len(), &["severity", "count"])?;
for (sev, n) in severity_counts {
write_uniform_row(w, &[sev.to_string(), n.to_string()])?;
}
write_uniform_header(
w,
"by_category",
category_counts.len(),
&["category", "count"],
)?;
for (cat, n) in category_counts {
write_uniform_row(w, &[cat.to_string(), n.to_string()])?;
}
writeln!(w, "verdict:")?;
writeln!(w, " rework: {verdict_rework}")?;
writeln!(w, " review: {verdict_review}")?;
writeln!(w, " pass_with_notes: {verdict_pass_with_notes}")?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::finding::{Category, Finding, Severity};
use serde_json::json;
#[test]
fn uniform_header_writes_expected_form() {
let mut buf = Vec::new();
write_uniform_header(&mut buf, "items", 3, &["a", "b", "c"]).unwrap();
assert_eq!(String::from_utf8(buf).unwrap(), "items[3]{a,b,c}:\n");
}
#[test]
fn uniform_row_tab_separates() {
let mut buf = Vec::new();
write_uniform_row(&mut buf, &["x".into(), "y\twith tab".into(), "z\nnewline".into()])
.unwrap();
let out = String::from_utf8(buf).unwrap();
assert!(out.starts_with(" x\t"));
assert!(!out.contains("\ty\twith"));
assert!(!out.contains('\n') || out.ends_with('\n'));
}
#[test]
fn findings_block_round_trips_count() {
let f = Finding::new(
"T-001",
"scan",
Severity::High,
Category::Bloat,
"src/big.md",
"Big file",
"Apply masking",
json!({"path": "src/big.md", "size_bytes": 12345}),
);
let mut buf = Vec::new();
write_findings(&mut buf, &[f]).unwrap();
let out = String::from_utf8(buf).unwrap();
assert!(out.starts_with("findings[1]{id,phase,severity,category,"));
assert!(out.contains("T-001\tscan\thigh\tbloat\tsrc/big.md"));
assert!(out.contains("evidence_blob:"));
}
}
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 |