Skip to content

Commit d3f9e6f

Browse files
authored
feat: add hierarchical context manager [Story 447.1]
Implements the hierarchical short/long-term SYNAPSE context manager for Epic 447, including concurrent mutation serialization, long-term summary preservation under hard token limits, registry/manifest updates, and regression coverage.
1 parent 1d2ab55 commit d3f9e6f

8 files changed

Lines changed: 1165 additions & 6 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SYNAPSE Context Runtime
2+
3+
This directory contains bounded-context helpers for the SYNAPSE runtime.
4+
5+
## HierarchicalContextManager
6+
7+
`HierarchicalContextManager` keeps an LLM-ready message array under a configured
8+
token budget by moving older short-term messages into long-term summaries.
9+
Summarization is injectable and has a deterministic local fallback, so the
10+
manager can run in tests, CLI flows and offline agent loops without a live LLM.
11+
12+
```javascript
13+
const { HierarchicalContextManager } = require('./.aiox-core/core/synapse/context');
14+
15+
const contextManager = new HierarchicalContextManager({
16+
maxTokens: 8192,
17+
summarizationThreshold: 0.75,
18+
tokenizer: text => Math.ceil(text.length / 4),
19+
summarizer: async ({ messages }) => {
20+
return messages.map(message => `${message.role}: ${message.content}`).join('\n');
21+
},
22+
});
23+
24+
contextManager.on('swap:complete', data => {
25+
console.log(`Compacted ${data.messagesRemoved} message(s).`);
26+
});
27+
28+
await contextManager.addMessage({ role: 'user', content: 'Build the next task.' });
29+
const safeContext = contextManager.getContext();
30+
```
31+
32+
The manager does not call OpenAI, Anthropic, Claude, Gemini, Kimi or OpenRouter
33+
directly. Inject a summarizer when an agent loop wants model-based compression.
34+
If that summarizer fails, the manager emits `swap:error` and falls back to a
35+
local extractive summary instead of crashing the loop.

0 commit comments

Comments
 (0)