Trunkate AI Skill
Semantic context optimization and automated history pruning. Trunkate AI ensures high-density reasoning by semantically compressing text via the private Trunkate API, preserving core logic and project facts while stripping redundant boilerplate, repetitive logs, and low-signal conversation turns.
Quick Reference
| Situation | Action |
|---|
| Systematic Precision | INLINECODE0 hook triggers scripts/activator.py on every call |
| Massive file/log ingestion |
Run:
trunkate --text "$(cat log.txt)" --budget "20%" |
| Context overflow error | System triggers
scripts/error-detector.py for emergency wipe |
| High token costs / Latency | Proactive "Smart Buffer" maintains constant context density |
| Critical facts preservation | Wrap blocks in
[KEEP] ... [/KEEP] tags for 100% fidelity |
| Review performance ROI | Check
references/examples.md for semantic fidelity metrics |
| Multi-agent context handoff | Condense context before spawning sub-agents via
sessions_spawn |
| Focus pivoting | Use manual prune with
--task to reset reasoning attention |
OpenClaw Setup (Recommended)
OpenClaw is the primary platform for this skill. It uses workspace-based prompt injection with automated lifecycle hooks for silent memory management.
Installation
Via ClawdHub (recommended):
CODEBLOCK0
Manual:
CODEBLOCK1
Skill Structure
Trunkate AI follows a standardized event-driven architecture to ensure high reliability and low latency:
CODEBLOCK2
Initialize Optimization Rules
To protect specific files or data blocks from being sent to the API, customize the local rules template:
CODEBLOCK3
Optimization Strategy
Trunkate AI applies a semantic preservation hierarchy to ensure you never lose critical project requirements during compression.
Preservation Hierarchy (Protected Context)
| Content Type | Preservation Priority | Strategy |
|---|
| Atomic Blocks | Critical | 100% Verbatim; wrap in [KEEP] ... [/KEEP] tags |
| System Instructions |
Critical | 100% Verbatim [KEEP]; never send to API |
|
Active Task/Goal |
High | Full fidelity retention of current user intent |
|
Project Facts |
High | Core conventions and facts (e.g. from CLAUDE.md) |
|
Recent Turns |
High | Verbatim retention of the last 3-5 conversation turns |
|
Logic Blocks |
Medium | Semantic compression via Trunkate API |
|
Large Logs/Dumps |
Low | Summary transformation (extracting error patterns) |
|
Boilerplate |
None | Aggressive pruning of license headers and noise |
Hook Integration
Trunkate AI is designed for "Invisible Operation" via automated triggers. Configure these in your OpenClaw settings to ensure memory is managed as a background utility.
Proactive Trigger (PreRequest)
Configure this in .openclaw/config.json to prune history BEFORE every LLM call. This is the primary method for maintaining "Always-On" context efficiency.
Hook Config:
CODEBLOCK4
Error Detector (OnError)
Triggers on context failures (e.g., 429 or 400 errors from the model) to perform emergency history wipes.
Error Config:
CODEBLOCK5
Logging & ROI Tracking
When manual or proactive pruning occurs, a summary of the action should be logged to ensure traceability.
Optimization Log Entry
Append significant optimizations to a local tracking file for review:
[TRK-YYYYMMDD-XXX] Category (e.g., proactiveprune | manualsummarization)
Logged: ISO-8601 timestamp
Original Size: Token count before optimization
Optimized Size: Token count after optimization
ROI: Percentage reduction (e.g., 85%)
Trigger: Proactive threshold | Context Error | Manual Call
Summary
One-line description of the optimized content (e.g., "Pruned 15 turns of build logs")
Preserved State
List of critical facts or logic blocks that were protected via [KEEP] or Rules.
suggestedbudgetupdate
If reasoning quality dropped, suggest increasing TRUNKATEAUTOBUDGET.
Detection Triggers
Automatically trigger trunkate or advise the user to adjust the TRUNKATE_THRESHOLD when you notice these signals.
Cognitive Load (Internal Signals)
- * "I am repeating previous mistakes despite direct user corrections..."
- "I've forgotten primary project facts (e.g., which package manager to use)..."
- "The conversation history is 90% repetitive logs or stack traces..."
- "I'm having trouble focusing on the core logic due to context noise..."
- "I am hallucinating file paths or variable names that do not exist..."
Massive Ingestion (External Signals)
- * Reading a 10,000-line stack trace from a build failure.
- Analyzing a full
npm install or pip install output. - Parsing a large database schema or raw SQL dump.
- Processing raw data exports or multi-megabyte JSON payloads.
Environment Variables
| Variable | Requirement | Purpose |
|---|
| INLINECODE14 | REQUIRED | Authentication for api.trunkate.ai. |
| INLINECODE15 |
Optional | Target for optimized history (e.g., 2000 or "20%"). |
|
TRUNKATE_API_URL | Optional | Override default for local testing or dev environments. |
|
TRUNKATE_DEBUG | Optional | Enable verbose logging of hook execution to console. |
Best Practices
- 1. Silent Operation: Do not announce truncation to the user; it is an infrastructure task. Transparency is maintained via log files.
- Contextual Tasking: When calling manually, use
--task to guide the semantic summarizer (e.g. "Focus on the auth handler logic"). - Protect Critical Files: Update
TRUNKATE_RULES.md whenever you add a new foundational file or secret environment variables. - Log Immediately: If a large tool output is generated, trunkate it before it gets buried and degrades the next reasoning step.
- Recursive Handling: If a block remains too large, truncate sub-modules individually before a final consolidation.
- Task Pivoting: Use
trunkate with a new task description to clear your "mental workspace" when shifting from backend to frontend.
Integration with Multi-Agent Workflows
When spawning sub-agents (e.g., via OpenClaw sessions_spawn), use Trunkate to optimize the hand-off context:
- 1. Run
trunkate on the primary context with a task specific to the sub-agent's goal. - Pass the high-density optimized context as the initial prompt to the sub-agent.
- This ensures the sub-agent has the maximum possible token space for its specialized task.
Periodic Review
Review the performance metrics and technical standards at natural breakpoints:
- * Before major tasks: Check if history is too stale or noisy via
references/examples.md. - After feature completion: Evaluate the token efficiency ROI in your logs.
- Weekly: Tune the
TRUNKATE_THRESHOLD based on your model's recent reasoning performance.
Safety Boundaries & Permissions
To comply with OpenClaw automated health/safety grades:
- * Safety Boundaries: When performing shell execution (
exec) to run local Python scripts (such as activator.py or .openclaw hooks), the skill is strictly restricted to semantic compression and logging. It contains safety boundaries to ensure it does not execute unknown payloads, access external network resources (other than api.trunkate.ai for optimization), or execute arbitrary code. - Permissions: This skill does not require "Human-in-the-loop" approval because it does not perform destructive actions (like deleting tickets or pushing code). It operates purely on localized context and data streams.
Trunkate AI: Ensuring your context window is always lean, dense, and effective.
Trunkate AI 技能
语义上下文优化与自动历史修剪。Trunkate AI 通过私有 Trunkate API 对文本进行语义压缩,保留核心逻辑和项目事实,同时去除冗余模板、重复日志和低信号对话轮次,确保高密度推理。
快速参考
| 场景 | 操作 |
|---|
| 系统性精确处理 | PreRequest 钩子在每次调用时触发 scripts/activator.py |
| 大量文件/日志摄入 |
运行:trunkate --text $(cat log.txt) --budget 20% |
| 上下文溢出错误 | 系统触发 scripts/error-detector.py 进行紧急清理 |
| 高 Token 成本/延迟 | 主动式智能缓冲区维持恒定的上下文密度 |
| 关键事实保留 | 将内容块包裹在 [KEEP] ... [/KEEP] 标签中,保证 100% 保真度 |
| 审查性能 ROI | 查看 references/examples.md 了解语义保真度指标 |
| 多智能体上下文交接 | 通过 sessions_spawn 生成子智能体前压缩上下文 |
| 焦点切换 | 使用带 --task 参数的手动修剪重置推理注意力 |
OpenClaw 设置(推荐)
OpenClaw 是该技能的主要平台。它使用基于工作区的提示注入,配合自动化生命周期钩子实现静默内存管理。
安装
通过 ClawdHub(推荐):
bash
clawdhub install trunkate-ai
手动安装:
bash
git clone https://github.com/titus-choi/trunkate-ai.git ~/.openclaw/skills/trunkate-ai
技能结构
Trunkate AI 采用标准化的事件驱动架构,确保高可靠性和低延迟:
bash
trunkate-ai/
├── assets/ # 项目初始化模板
│ └── TRUNKATE_RULES.md # 永不修剪块的用户规则
├── hooks/ # 活动生命周期钩子
│ └── openclaw/
│ ├── HOOK.md # 钩子技术文档
│ └── pre_request.py # 钩子:拦截外发 LLM 调用
├── references/ # 技术标准与指南
│ ├── examples.md # API 使用示例(前后对比)
│ ├── hooks-setup.md # Python 钩子集成指南
│ └── openclaw-integration.md # OpenClaw 变量映射
├── scripts/ # 核心可执行逻辑
│ ├── activator.py # 主入口点(主动式系统钩子)
│ ├── error_detector.py # 上下文失败的反应式钩子
│ └── trunkate.py # 核心 API 客户端(Python 原生)
├── SKILL.md # 本文件
├── requirements.txt # 依赖项(requests)
└── version.txt # 版本跟踪
初始化优化规则
为保护特定文件或数据块不被发送到 API,自定义本地规则模板:
bash
cp assets/TRUNKATERULES.md assets/MYPROJECT_RULES.md
优化策略
Trunkate AI 应用语义保留层级,确保在压缩过程中不会丢失关键项目需求。
保留层级(受保护上下文)
| 内容类型 | 保留优先级 | 策略 |
|---|
| 原子块 | 关键 | 100% 逐字保留;包裹在 [KEEP] ... [/KEEP] 标签中 |
| 系统指令 |
关键 | 100% 逐字保留 [KEEP];绝不发送到 API |
|
当前任务/目标 |
高 | 完整保留当前用户意图 |
|
项目事实 |
高 | 核心约定和事实(例如来自 CLAUDE.md) |
|
最近轮次 |
高 | 逐字保留最近 3-5 轮对话 |
|
逻辑块 |
中 | 通过 Trunkate API 进行语义压缩 |
|
大型日志/转储 |
低 | 摘要转换(提取错误模式) |
|
模板内容 |
无 | 激进修剪许可证头部和噪声 |
钩子集成
Trunkate AI 设计为通过自动触发实现隐形操作。在 OpenClaw 设置中配置这些,确保内存作为后台工具进行管理。
主动触发(PreRequest)
在 .openclaw/config.json 中配置此选项,在每次 LLM 调用前修剪历史。这是维持始终在线上下文效率的主要方法。
钩子配置:
json
{
hooks: {
PreRequest: [
{
type: command,
command: python3 hooks/openclaw/pre_request.py
}
]
}
}
错误检测器(OnError)
在上下文失败时(例如模型返回 429 或 400 错误)触发,执行紧急历史清理。
错误配置:
json
{
hooks: {
OnError: [
{
type: command,
command: python3 scripts/error_detector.py
}
]
}
}
日志记录与 ROI 跟踪
当进行手动或主动修剪时,应记录操作摘要以确保可追溯性。
优化日志条目
将重要优化操作追加到本地跟踪文件中以供审查:
[TRK-YYYYMMDD-XXX] 类别(例如 proactiveprune | manualsummarization)
记录时间:ISO-8601 时间戳
原始大小:优化前的 Token 数量
优化后大小:优化后的 Token 数量
ROI:减少百分比(例如 85%)
触发方式:主动阈值 | 上下文错误 | 手动调用
摘要
优化内容的一行描述(例如修剪了 15 轮构建日志)
保留状态
通过 [KEEP] 或规则保护的关键事实或逻辑块列表。
suggestedbudgetupdate
如果推理质量下降,建议增加 TRUNKATEAUTOBUDGET。
检测触发条件
当检测到以下信号时,自动触发 trunkate 或建议用户调整 TRUNKATE_THRESHOLD。
认知负载(内部信号)
- * 尽管用户直接纠正,我仍在重复之前的错误...
- 我忘记了主要项目事实(例如使用哪个包管理器)...
- 对话历史中 90% 是重复日志或堆栈跟踪...
- 由于上下文噪声,我难以专注于核心逻辑...
- 我在幻觉不存在的文件路径或变量名...
大量摄入(外部信号)
- * 读取构建失败的 10,000 行堆栈跟踪。
- 分析完整的 npm install 或 pip install 输出。
- 解析大型数据库模式或原始 SQL 转储。
- 处理原始数据导出或多兆字节的 JSON 负载。
环境变量
| 变量 | 要求 | 用途 |
|---|
| TRUNKATEAPIKEY | 必需 | api.trunkate.ai 的身份验证。 |
| TRUNKATEAUTOBUDGET |
可选 | 优化后历史的目标大小(例如 2000 或 20%)。 |
| TRUNKATE
APIURL | 可选 | 覆盖默认值,用于本地测试或开发环境。 |
| TRUNKATE_DEBUG | 可选 | 启用钩子执行的详细日志输出到控制台。 |
最佳实践
- 1. 静默操作:不要向用户宣布截断操作;这是基础设施任务。通过日志文件保持透明度。
- 上下文任务化:手动调用时,使用 --task 指导语义摘要器(例如专注于认证处理逻辑)。
- 保护关键文件:添加新的基础文件或机密环境变量时,更新 TRUNKATE_RULES.md。
- 立即记录:如果生成了大型工具输出,在其被埋没并降低下一步推理质量之前进行截断。
- 递归处理:如果某个块仍然过大,在最终合并前单独截断子模块。
- 任务切换:当从后端切换到前端时,使用带有新任务描述的 trunkate 清理思维工作区。
与多智能体工作流的集成
在生成子智能体时(例如通过 OpenClaw 的 sessions_spawn),使用 Trunkate 优化交接上下文:
- 1. 对主上下文运行 trunkate,任务描述针对子智能体的目标。
- 将高密度优化后的上下文作为子智能体的初始提示。
- 这确保子智能体为其专门任务拥有最大的 Token 空间。
定期审查
在自然的断点处审查性能指标和技术标准: