Context Fragmentation in Multi-Agent Orchestration
session-context-manager.ts passes too much context (entire session state)Lightweight Context Index
✅ DO THIS:
Orchestrator → Agent: "Here's the ONE file you need: .tmp/architecture/auth-system/contexts.json"
❌ NOT THIS:
Orchestrator → Agent: "Here's the entire session context with everything from all previous agents (5000 lines)"
{
feature: "auth-system",
created: "2026-02-15T10:00:00Z",
updated: "2026-02-15T10:30:00Z",
agents: {
"ArchitectureAnalyzer": {
outputs: [".tmp/architecture/auth-system/contexts.json"],
metadata: {
boundedContext: "authentication",
module: "auth-service"
},
timestamp: "2026-02-15T10:05:00Z"
},
"StoryMapper": {
outputs: [".tmp/story-maps/auth-system/map.json"],
metadata: {
verticalSlice: "user-login"
},
timestamp: "2026-02-15T10:15:00Z"
}
},
contextFiles: [
".opencode/context/core/standards/code-quality.md",
".opencode/context/security/auth-patterns.md"
],
referenceFiles: [
"src/auth/old-auth.ts"
]
}
Lightweight (paths and metadata only):
NOT stored (content stays in files):
createContextIndex(feature, options)Initialize a new context index for a feature.
import { createContextIndex } from './context-index';
const result = createContextIndex('auth-system', {
contextFiles: [
'.opencode/context/core/standards/code-quality.md',
'.opencode/context/security/auth-patterns.md'
],
referenceFiles: [
'src/auth/old-auth.ts'
]
});
// Result: { success: true }
// Creates: .tmp/context-index/auth-system.json
addAgentOutput(feature, agent, outputPath, metadata)Track what each agent produced.
import { addAgentOutput } from './context-index';
// After ArchitectureAnalyzer completes
addAgentOutput(
'auth-system',
'ArchitectureAnalyzer',
'.tmp/architecture/auth-system/contexts.json',
{
boundedContext: 'authentication',
module: 'auth-service'
}
);
// After StoryMapper completes
addAgentOutput(
'auth-system',
'StoryMapper',
'.tmp/story-maps/auth-system/map.json',
{
verticalSlice: 'user-login'
}
);
getContextForAgent(feature, agentType)Get ONLY the files needed for a specific agent type.
import { getContextForAgent } from './context-index';
// StoryMapper needs: ArchitectureAnalyzer output + story context
const result = getContextForAgent('auth-system', 'StoryMapper');
// Returns:
{
success: true,
context: {
feature: "auth-system",
agentType: "StoryMapper",
contextFiles: [
".opencode/context/core/story-mapping/guide.md"
],
referenceFiles: [],
agentOutputs: [
".tmp/architecture/auth-system/contexts.json"
],
metadata: {
boundedContext: "authentication",
module: "auth-service"
}
}
}
getFullContext(feature)Orchestrator can see everything (for coordination).
import { getFullContext } from './context-index';
const result = getFullContext('auth-system');
// Returns complete index with all agent outputs
Each agent type gets a minimal, focused context:
Needs:
Gets:
{
contextFiles: [
".opencode/context/architecture/patterns.md"
],
referenceFiles: [
"src/auth/old-auth.ts"
],
agentOutputs: [],
metadata: {}
}
Needs:
Gets:
{
contextFiles: [
".opencode/context/core/story-mapping/guide.md"
],
referenceFiles: [],
agentOutputs: [
".tmp/architecture/auth-system/contexts.json"
],
metadata: {
boundedContext: "authentication",
module: "auth-service"
}
}
Needs:
Gets:
{
contextFiles: [
".opencode/context/core/prioritization/scoring.md"
],
referenceFiles: [],
agentOutputs: [
".tmp/story-maps/auth-system/map.json"
],
metadata: {
verticalSlice: "user-login"
}
}
Needs:
Gets:
{
contextFiles: [
".opencode/context/core/task-management/navigation.md",
".opencode/context/core/standards/code-quality.md"
],
referenceFiles: [],
agentOutputs: [
".tmp/architecture/auth-system/contexts.json",
".tmp/story-maps/auth-system/map.json",
".tmp/planning/prioritized.json"
],
metadata: {
ArchitectureAnalyzer: { boundedContext: "authentication" },
StoryMapper: { verticalSlice: "user-login" }
}
}
Needs:
Gets:
{
contextFiles: [
".opencode/context/core/standards/code-quality.md",
".opencode/context/security/auth-patterns.md"
],
referenceFiles: [],
agentOutputs: [
".tmp/tasks/auth-system/subtask_01.json"
],
metadata: {}
}
import {
createContextIndex,
addAgentOutput,
getContextForAgent
} from './context-index';
// 1. Initialize context index
createContextIndex('auth-system', {
contextFiles: [
'.opencode/context/core/standards/code-quality.md',
'.opencode/context/security/auth-patterns.md',
'.opencode/context/architecture/patterns.md'
],
referenceFiles: [
'src/auth/old-auth.ts'
]
});
// 2. Delegate to ArchitectureAnalyzer
const archContext = getContextForAgent('auth-system', 'ArchitectureAnalyzer');
// Pass ONLY: archContext.contextFiles + archContext.referenceFiles
// 3. ArchitectureAnalyzer completes → Read output and update index
addAgentOutput(
'auth-system',
'ArchitectureAnalyzer',
'.tmp/architecture/auth-system/contexts.json',
{ boundedContext: 'authentication', module: 'auth-service' }
);
// 4. Delegate to StoryMapper
const storyContext = getContextForAgent('auth-system', 'StoryMapper');
// Pass ONLY:
// - storyContext.contextFiles (story mapping guide)
// - storyContext.agentOutputs (ArchitectureAnalyzer output path)
// 5. StoryMapper completes → Update index
addAgentOutput(
'auth-system',
'StoryMapper',
'.tmp/story-maps/auth-system/map.json',
{ verticalSlice: 'user-login' }
);
// 6. Delegate to PrioritizationEngine
const prioContext = getContextForAgent('auth-system', 'PrioritizationEngine');
// Pass ONLY:
// - prioContext.contextFiles (prioritization guide)
// - prioContext.agentOutputs (StoryMapper output path)
// 7. Continue pattern through all agents...
// For each agent:
const context = getContextForAgent(feature, agentType);
// Delegate with minimal context
task(
subagent_type=agentType,
description="...",
prompt=`
Context files to load:
${context.contextFiles.map(f => `- ${f}`).join('\n')}
Previous agent outputs to read:
${context.agentOutputs.map(f => `- ${f}`).join('\n')}
Metadata from previous agents:
${JSON.stringify(context.metadata, null, 2)}
Your task: ...
`
);
// After agent completes, update index
addAgentOutput(feature, agentType, outputPath, metadata);
Use session-context-manager.ts when:
Example: Interactive feature development with user feedback loops
Use context-index.ts when:
Example: Automated feature pipeline (Architecture → Stories → Tasks → Code)
| Feature | Session Context | Context Index |
|---|---|---|
| Format | Markdown (human-readable) | JSON (machine-readable) |
| Size | Large (full content) | Small (paths only) |
| Audience | Humans + Agents | Agents only |
| Context Passing | Everything to everyone | Minimal per agent |
| Use Case | Interactive sessions | Automated pipelines |
| Performance | Slower (large files) | Faster (lightweight) |
| Tracking | Decisions, progress, narrative | Outputs, metadata, pointers |
Yes! They solve different problems:
// Create session context for human tracking
createSession('auth-system', 'Implement JWT authentication', {
contextFiles: [...],
exitCriteria: [...]
});
// Create context index for agent coordination
createContextIndex('auth-system', {
contextFiles: [...],
referenceFiles: [...]
});
// Session context = human-readable audit trail
// Context index = efficient agent coordination
// BAD: Agent gets everything
const index = getFullContext('auth-system');
task(subagent_type="StoryMapper", prompt=JSON.stringify(index));
Why bad: Agent wastes time parsing irrelevant data
// BAD: Storing full file contents
addAgentOutput('auth-system', 'ArchitectureAnalyzer', outputPath, {
fullContent: fs.readFileSync(outputPath, 'utf-8') // DON'T DO THIS
});
Why bad: Index becomes bloated, defeats the purpose
// BAD: Agent completes but orchestrator doesn't update index
task(subagent_type="ArchitectureAnalyzer", ...);
// ... agent completes ...
// (orchestrator forgets to call addAgentOutput)
Why bad: Next agent won't know about previous outputs
// BAD: Expecting humans to read JSON index
const index = getFullContext('auth-system');
console.log("Review this:", JSON.stringify(index));
Why bad: Index is for machines, use session-context for humans
// Agent completes
const result = await task(subagent_type="ArchitectureAnalyzer", ...);
// Immediately update index
addAgentOutput(
'auth-system',
'ArchitectureAnalyzer',
'.tmp/architecture/auth-system/contexts.json',
{ boundedContext: 'authentication' }
);
// GOOD: Small metadata that helps next agent
addAgentOutput('auth-system', 'ArchitectureAnalyzer', outputPath, {
boundedContext: 'authentication',
module: 'auth-service',
complexity: 'medium'
});
// BAD: Large data that should stay in files
addAgentOutput('auth-system', 'ArchitectureAnalyzer', outputPath, {
fullAnalysis: { /* 1000 lines of JSON */ }
});
// GOOD: Agent reads the file
const context = getContextForAgent('auth-system', 'StoryMapper');
task(
subagent_type="StoryMapper",
prompt=`Read architecture analysis: ${context.agentOutputs[0]}`
);
// BAD: Orchestrator reads and passes content
const archOutput = fs.readFileSync('.tmp/architecture/...', 'utf-8');
task(
subagent_type="StoryMapper",
prompt=`Here's the full architecture: ${archOutput}`
);
// GOOD: Only relevant context files
createContextIndex('auth-system', {
contextFiles: [
'.opencode/context/security/auth-patterns.md',
'.opencode/context/core/standards/code-quality.md'
]
});
// BAD: Every context file in the project
createContextIndex('auth-system', {
contextFiles: glob('.opencode/context/**/*.md') // Too much!
});
npx ts-node context-index.ts create auth-system
# ✅ Context index created for: auth-system
# Location: .tmp/context-index/auth-system.json
npx ts-node context-index.ts add-output \
auth-system \
ArchitectureAnalyzer \
.tmp/architecture/auth-system/contexts.json \
'{"boundedContext":"authentication","module":"auth-service"}'
# ✅ Added ArchitectureAnalyzer output: .tmp/architecture/auth-system/contexts.json
npx ts-node context-index.ts get-context auth-system StoryMapper
# {
# "feature": "auth-system",
# "agentType": "StoryMapper",
# "contextFiles": [".opencode/context/core/story-mapping/guide.md"],
# "agentOutputs": [".tmp/architecture/auth-system/contexts.json"],
# "metadata": {"boundedContext":"authentication"}
# }
npx ts-node context-index.ts show auth-system
# {
# "feature": "auth-system",
# "created": "2026-02-15T10:00:00Z",
# "agents": { ... }
# }
Lightweight Context Handoff Pattern:
Use this pattern when:
Use session-context when: