Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat(cos): add agent context compaction support for M40-P2

Adam Eivycf41dd61d281
diff --git a/.changelog/v0.12.x.md b/.changelog/v0.12.x.md
index fe4f97d6d5..b2a7926470 100644
--- a/.changelog/v0.12.x.md
+++ b/.changelog/v0.12.x.md
@@ -441,9 +441,9 @@ This release adds branch management and automated release workflow capabilities
- `client/src/components/city/CityScanlines.jsx` — CRT flicker, stronger vignette, edge glow lines
- `client/src/components/city/CityScene.jsx` — Integrated all 5 new scene layers, extended orbit range
-### M40: Agent Skill System — Planned
+### M40: Agent Skill System — In Progress
- **Task-type-specific prompt templates**: New `data/prompts/skills/` directory with specialized agent briefings per task category (bug-fix, feature, security-audit, refactor, documentation, mobile-responsive) — each with routing descriptions, embedded examples, and task-specific guidelines to improve agent accuracy
-- **Agent context compaction**: Output length tracking and compaction metadata for retries that hit context limits, plus output format constraints for verbose task types
+- **Agent context compaction** ✅: Context-limit and output-limit error patterns now include compaction metadata (output size, retry hints) that propagates through task retry metadata. When an agent retries after a context-limit failure, the briefing prompt includes a "Context Compaction Required" section with mandatory output constraints. Agent briefing template updated with task-type-specific output format constraints for documentation, refactors, security audits, and bug fixes.
- **Negative example routing**: Anti-pattern recording in task learning so failed model/task-type combinations are tracked and auto-avoided, with routing accuracy surfaced in the Learning tab
- **Deterministic workflow skills**: Recurring autonomous jobs (daily briefing, git maintenance, security audit, app improvement) converted from inline prompts to versioned, editable skill templates with defined steps, outputs, and success criteria
diff --git a/PLAN.md b/PLAN.md
index 3520d10f43..9d739756b7 100644
--- a/PLAN.md
+++ b/PLAN.md
@@ -130,12 +130,12 @@ Currently all agents use the same generic `cos-agent-briefing.md` prompt templat
- Update `buildAgentPrompt()` in `subAgentSpawner.js` to select the appropriate skill template based on task type/description keywords
- Each template loaded only when matched (avoids token inflation for unrelated tasks)
-### P2: Agent Context Compaction
+### P2: Agent Context Compaction ✅
Long-running agents can hit context limits causing failures. Add proactive context management:
- Pass `--max-turns` or equivalent context budget hints when spawning agents
- Track agent output length and detect when agents are approaching context limits
-- Add compaction metadata to agent error analysis so retries can include "compact context" instructions
-- Update the agent briefing to include explicit output format constraints for verbose task types
+- ✅ Add compaction metadata to agent error analysis so retries can include "compact context" instructions
+- ✅ Update the agent briefing to include explicit output format constraints for verbose task types
### P3: Negative Example Coverage for Task Routing
Improve task-to-model routing accuracy by adding negative examples to the model selection logic:
diff --git a/data.sample/prompts/stages/cos-agent-briefing.md b/data.sample/prompts/stages/cos-agent-briefing.md
index 6bd21abf35..bdb4f3553c 100644
--- a/data.sample/prompts/stages/cos-agent-briefing.md
+++ b/data.sample/prompts/stages/cos-agent-briefing.md
@@ -20,6 +20,10 @@ Your job is to complete the assigned task independently and efficiently.
{{task.metadata.app}}
{{/task.metadata.app}}
+{{#compactionSection}}
+{{{compactionSection}}}
+{{/compactionSection}}
+
## Instructions
1. **Analyze** the task requirements carefully before making changes
@@ -44,6 +48,14 @@ Your job is to complete the assigned task independently and efficiently.
## Output Format
+Keep your output concise throughout execution. Avoid reproducing full file contents — reference files by path and line number instead.
+
+**Task-type-specific constraints**:
+- **Documentation/changelog tasks**: Summarize changes concisely. Do not echo the full document — list sections modified and key additions only.
+- **Large refactors**: List only changed files with a one-line description per file. Do not reproduce before/after code blocks.
+- **Security audits**: Report findings as a compact table (file, line, severity, description). Skip files with no issues.
+- **Bug fixes**: State the root cause, the fix, and affected files. Do not narrate your entire debugging process.
+
At the end of your work, provide a summary in this format:
```
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 50c47060f0..a221249b70 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -58,6 +58,68 @@ function extractTaskTypeKey(task) {
return 'unknown';
}
+const SKILLS_DIR = join(ROOT_DIR, 'data/prompts/skills');
+
+/**
+ * Skill template keyword matchers
+ * Each entry maps a skill template filename to its trigger keywords.
+ * Order matters — first match wins, so more specific patterns come first.
+ */
+const SKILL_MATCHERS = [
+ {
+ skill: 'security-audit',
+ keywords: ['security', 'audit', 'vulnerability', 'xss', 'injection', 'owasp', 'cve', 'penetration', 'hardening', 'sanitize', 'authorization']
+ },
+ {
+ skill: 'mobile-responsive',
+ keywords: ['mobile', 'responsive', 'tablet', 'breakpoint', 'viewport', 'touch', 'swipe', 'small screen', 'media query', 'mobile-friendly', 'adaptive']
+ },
+ {
+ skill: 'bug-fix',
+ keywords: ['fix', 'bug', 'broken', 'error', 'crash', 'issue', 'not working', 'fails', 'regression', 'defect']
+ },
+ {
+ skill: 'refactor',
+ keywords: ['refactor', 'reorganize', 'restructure', 'clean up', 'simplify', 'extract', 'consolidate', 'decouple', 'modularize']
+ },
+ {
+ skill: 'documentation',
+ keywords: ['document', 'documentation', 'docs', 'readme', 'jsdoc', 'api docs', 'guide', 'tutorial', 'changelog']
+ },
+ {
+ skill: 'feature',
+ keywords: ['add', 'create', 'implement', 'build', 'new', 'feature', 'support', 'enable', 'integrate', 'endpoint', 'page', 'component']
+ }
+];
+
+/**
+ * Detect the best matching skill template for a task based on description keywords
+ * @param {Object} task - Task object with description
+ * @returns {string|null} Skill template name or null if no match
+ */
+function detectSkillTemplate(task) {
+ const desc = (task?.description || '').toLowerCase();
+ for (const matcher of SKILL_MATCHERS) {
+ if (matcher.keywords.some(kw => desc.includes(kw))) {
+ return matcher.skill;
+ }
+ }
+ return null;
+}
+
+/**
+ * Load a skill template from disk if it exists
+ * @param {string} skillName - Name of the skill template file (without .md)
+ * @returns {Promise<string|null>} Template content or null
+ */
+async function loadSkillTemplate(skillName) {
+ const templatePath = join(SKILLS_DIR, `${skillName}.md`);
+ if (!existsSync(templatePath)) return null;
+ const content = await readFile(templatePath, 'utf-8');
+ console.log(`🎯 Loaded skill template: ${skillName}`);
+ return content;
+}
+
/**
* Select optimal model for a task based on complexity analysis and historical performance
* User can override by specifying Model: and/or Provider: in task metadata
@@ -253,6 +315,9 @@ async function completeAgentRun(runId, output, exitCode, duration, errorAnalysis
metadata.errorDetails = errorInfo.details || metadata.error;
metadata.errorCategory = errorInfo.category || 'unknown';
metadata.suggestedFix = errorInfo.suggestedFix || null;
+ if (errorInfo.compaction) {
+ metadata.compaction = errorInfo.compaction;
+ }
}
await writeFile(metaPath, JSON.stringify(metadata, null, 2));
@@ -475,18 +540,39 @@ const ERROR_PATTERNS = [
pattern: /context.?length|max.?tokens|token.?limit|context.?window/i,
category: 'context-length',
actionable: true,
- extract: () => ({
+ extract: (match, output) => ({
message: 'Context length exceeded',
- suggestedFix: 'Task is too large for the context window. Break into smaller subtasks or use a model with larger context.'
+ suggestedFix: 'Task is too large for the context window. Break into smaller subtasks or use a model with larger context.',
+ compaction: {
+ needed: true,
+ reason: 'context-limit',
+ outputSize: Buffer.byteLength(output || ''),
+ retryHints: [
+ 'Summarize intermediate findings concisely instead of reproducing full file contents',
+ 'Use targeted reads (offset/limit) instead of reading entire files',
+ 'Avoid listing full directory trees — only reference files you modify',
+ 'Keep your Task Summary under 30 lines'
+ ]
+ }
})
},
{
pattern: /output.?length|max.?output|response.?too.?long/i,
category: 'output-length',
actionable: false,
- extract: () => ({
+ extract: (match, output) => ({
message: 'Output length exceeded',
- suggestedFix: 'Agent response exceeded output limit. Task may need to be scoped down.'
+ suggestedFix: 'Agent response exceeded output limit. Task may need to be scoped down.',
+ compaction: {
+ needed: true,
+ reason: 'output-limit',
+ outputSize: Buffer.byteLength(output || ''),
+ retryHints: [
+ 'Limit output to changed files and a brief summary only',
+ 'Do not echo file contents back — just reference file paths and line numbers',
+ 'Combine related changes into single descriptions'
+ ]
+ }
})
},
@@ -839,13 +925,18 @@ async function resolveFailedTaskUpdate(task, errorAnalysis, agentId) {
emitLog('info', `🔄 Task ${task.id} retry ${failureCount}/${MAX_TASK_RETRIES} (${lastErrorCategory})`, {
taskId: task.id, failureCount, maxRetries: MAX_TASK_RETRIES, category: lastErrorCategory
});
+
+ // Propagate compaction hints to task metadata for retry prompt injection
+ const compaction = errorAnalysis?.compaction || null;
+
return {
status: 'pending',
metadata: {
...task.metadata,
failureCount,
lastFailureAt: new Date().toISOString(),
- lastErrorCategory
+ lastErrorCategory,
+ ...(compaction && { compaction })
}
};
}
@@ -2011,6 +2102,32 @@ async function getClaudeMdContext(workspaceDir) {
* @param {string} workspaceDir - Working directory (may be a worktree)
* @param {Object|null} worktreeInfo - Worktree details if using a worktree ({ worktreePath, branchName })
*/
+/**
+ * Build a compaction instruction section for retries after context-limit failures.
+ * Provides explicit guidance to the agent on reducing output verbosity.
+ */
+function buildCompactionSection(task) {
+ const compaction = task.metadata?.compaction;
+ if (!compaction?.needed) return '';
+
+ const hints = compaction.retryHints || [];
+ const reason = compaction.reason === 'output-limit' ? 'output length limit' : 'context window limit';
+ const prevOutputKB = compaction.outputSize ? Math.round(compaction.outputSize / 1024) : 'unknown';
+
+ return `
+## Context Compaction Required
+
+**WARNING**: A previous attempt at this task failed because the agent exceeded the ${reason}.
+Previous output size: ~${prevOutputKB} KB. You MUST keep your output compact to avoid the same failure.
+
+**Mandatory output constraints**:
+${hints.map(h => `- ${h}`).join('\n')}
+- Do NOT reproduce entire file contents in your output
+- Reference files by path and line number instead of quoting them
+- Limit exploratory reads — plan your approach first, then make targeted changes
+`;
+}
+
async function buildAgentPrompt(task, config, workspaceDir, worktreeInfo = null) {
// Get relevant memories for context injection
const memorySection = await getMemorySection(task, {
@@ -2034,6 +2151,9 @@ async function buildAgentPrompt(task, config, workspaceDir, worktreeInfo = null)
return null;
});
+ // Build context compaction section if task is retrying after a context-limit failure
+ const compactionSection = task.metadata?.compaction?.needed ? buildCompactionSection(task) : '';
+
// Build worktree context section if applicable
const worktreeSection = worktreeInfo ? `
## Git Worktree Context
@@ -2044,6 +2164,15 @@ You are working in an **isolated git worktree** to avoid conflicts with other ag
**Important**: Commit your changes to this branch. Your commits will be automatically merged back to the main development branch when your task completes. Do NOT manually switch branches or modify the worktree configuration.
` : '';
+ // Detect and load task-type-specific skill template (only when matched)
+ const matchedSkill = detectSkillTemplate(task);
+ const skillSection = matchedSkill
+ ? await loadSkillTemplate(matchedSkill).catch(err => {
+ console.log(`⚠️ Skill template load failed for ${matchedSkill}: ${err.message}`);
+ return null;
+ })
+ : null;
+
// Try to use the prompt template system
const promptData = await buildPrompt('cos-agent-briefing', {
task,
@@ -2052,6 +2181,8 @@ You are working in an **isolated git worktree** to avoid conflicts with other ag
claudeMdSection,
digitalTwinSection,
worktreeSection,
+ compactionSection,
+ skillSection,
soulSection: digitalTwinSection, // Backwards compatibility for prompt templates
timestamp: new Date().toISOString()
}).catch(() => null);
@@ -2078,6 +2209,8 @@ ${task.metadata?.context ? `- **Context**: ${task.metadata.context}` : ''}
${task.metadata?.app ? `- **Target App**: ${task.metadata.app}` : ''}
${Array.isArray(task.metadata?.screenshots) && task.metadata.screenshots.length > 0 ? `- **Screenshots**: ${task.metadata.screenshots.join(', ')}` : ''}
${worktreeSection}
+${compactionSection}
+${skillSection ? `## Task-Type Skill Guidelines\n\n${skillSection}\n` : ''}
## Instructions
1. Analyze the task requirements carefully
2. Make necessary changes to complete the task