@stackmemoryai/stackmemory 0.3.22 → 0.3.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/cli/commands/ralph.js +294 -0
  2. package/dist/cli/commands/ralph.js.map +7 -0
  3. package/dist/cli/index.js +2 -0
  4. package/dist/cli/index.js.map +2 -2
  5. package/dist/integrations/ralph/bridge/ralph-stackmemory-bridge.js +586 -0
  6. package/dist/integrations/ralph/bridge/ralph-stackmemory-bridge.js.map +7 -0
  7. package/dist/integrations/ralph/context/context-budget-manager.js +297 -0
  8. package/dist/integrations/ralph/context/context-budget-manager.js.map +7 -0
  9. package/dist/integrations/ralph/context/stackmemory-context-loader.js +356 -0
  10. package/dist/integrations/ralph/context/stackmemory-context-loader.js.map +7 -0
  11. package/dist/integrations/ralph/index.js +14 -0
  12. package/dist/integrations/ralph/index.js.map +7 -0
  13. package/dist/integrations/ralph/learning/pattern-learner.js +397 -0
  14. package/dist/integrations/ralph/learning/pattern-learner.js.map +7 -0
  15. package/dist/integrations/ralph/lifecycle/iteration-lifecycle.js +444 -0
  16. package/dist/integrations/ralph/lifecycle/iteration-lifecycle.js.map +7 -0
  17. package/dist/integrations/ralph/orchestration/multi-loop-orchestrator.js +459 -0
  18. package/dist/integrations/ralph/orchestration/multi-loop-orchestrator.js.map +7 -0
  19. package/dist/integrations/ralph/performance/performance-optimizer.js +354 -0
  20. package/dist/integrations/ralph/performance/performance-optimizer.js.map +7 -0
  21. package/dist/integrations/ralph/ralph-integration-demo.js +178 -0
  22. package/dist/integrations/ralph/ralph-integration-demo.js.map +7 -0
  23. package/dist/integrations/ralph/state/state-reconciler.js +400 -0
  24. package/dist/integrations/ralph/state/state-reconciler.js.map +7 -0
  25. package/dist/integrations/ralph/swarm/swarm-coordinator.js +487 -0
  26. package/dist/integrations/ralph/swarm/swarm-coordinator.js.map +7 -0
  27. package/dist/integrations/ralph/types.js +1 -0
  28. package/dist/integrations/ralph/types.js.map +7 -0
  29. package/dist/integrations/ralph/visualization/ralph-debugger.js +581 -0
  30. package/dist/integrations/ralph/visualization/ralph-debugger.js.map +7 -0
  31. package/package.json +1 -1
  32. package/scripts/deploy-ralph-swarm.sh +365 -0
  33. package/scripts/ralph-integration-test.js +274 -0
  34. package/scripts/ralph-loop-implementation.js +404 -0
  35. package/scripts/swarm-monitor.js +509 -0
  36. package/scripts/test-parallel-swarms.js +443 -0
  37. package/scripts/testing/ralph-cli-test.js +88 -0
  38. package/scripts/testing/ralph-integration-validation.js +727 -0
  39. package/scripts/testing/ralph-swarm-test-scenarios.js +613 -0
@@ -0,0 +1,297 @@
1
+ import { logger } from "../../../core/monitoring/logger.js";
2
+ class ContextBudgetManager {
3
+ config;
4
+ tokenUsage = /* @__PURE__ */ new Map();
5
+ DEFAULT_MAX_TOKENS = 4e3;
6
+ TOKEN_CHAR_RATIO = 0.25;
7
+ // Rough estimate: 1 token ≈ 4 chars
8
+ constructor(config) {
9
+ this.config = {
10
+ maxTokens: config?.maxTokens || this.DEFAULT_MAX_TOKENS,
11
+ priorityWeights: {
12
+ task: config?.priorityWeights?.task || 0.3,
13
+ recentWork: config?.priorityWeights?.recentWork || 0.25,
14
+ feedback: config?.priorityWeights?.feedback || 0.2,
15
+ gitHistory: config?.priorityWeights?.gitHistory || 0.15,
16
+ dependencies: config?.priorityWeights?.dependencies || 0.1
17
+ },
18
+ compressionEnabled: config?.compressionEnabled ?? true,
19
+ adaptiveBudgeting: config?.adaptiveBudgeting ?? true
20
+ };
21
+ }
22
+ /**
23
+ * Estimate tokens for a given text
24
+ */
25
+ estimateTokens(text) {
26
+ if (!text) return 0;
27
+ const baseTokens = text.length * this.TOKEN_CHAR_RATIO;
28
+ const codeMultiplier = this.detectCodeContent(text) ? 1.2 : 1;
29
+ const jsonMultiplier = this.detectJsonContent(text) ? 0.9 : 1;
30
+ return Math.ceil(baseTokens * codeMultiplier * jsonMultiplier);
31
+ }
32
+ /**
33
+ * Allocate token budget across different context categories
34
+ */
35
+ allocateBudget(context) {
36
+ const currentTokens = this.calculateCurrentTokens(context);
37
+ if (currentTokens <= this.config.maxTokens) {
38
+ logger.debug("Context within budget", {
39
+ used: currentTokens,
40
+ max: this.config.maxTokens
41
+ });
42
+ return context;
43
+ }
44
+ logger.info("Context exceeds budget, optimizing...", {
45
+ current: currentTokens,
46
+ max: this.config.maxTokens
47
+ });
48
+ if (this.config.adaptiveBudgeting) {
49
+ return this.adaptiveBudgetAllocation(context, currentTokens);
50
+ }
51
+ return this.priorityBasedAllocation(context, currentTokens);
52
+ }
53
+ /**
54
+ * Compress context to fit within budget
55
+ */
56
+ compressContext(context) {
57
+ if (!this.config.compressionEnabled) {
58
+ return context;
59
+ }
60
+ const compressed = {
61
+ ...context,
62
+ task: this.compressTaskContext(context.task),
63
+ history: this.compressHistoryContext(context.history),
64
+ environment: this.compressEnvironmentContext(context.environment),
65
+ memory: this.compressMemoryContext(context.memory),
66
+ tokenCount: 0
67
+ };
68
+ compressed.tokenCount = this.calculateCurrentTokens(compressed);
69
+ logger.debug("Context compressed", {
70
+ original: context.tokenCount,
71
+ compressed: compressed.tokenCount,
72
+ reduction: `${Math.round((1 - compressed.tokenCount / context.tokenCount) * 100)}%`
73
+ });
74
+ return compressed;
75
+ }
76
+ /**
77
+ * Get current token usage statistics
78
+ */
79
+ getUsage() {
80
+ const categories = {};
81
+ let totalUsed = 0;
82
+ for (const [category, tokens] of this.tokenUsage) {
83
+ categories[category] = tokens;
84
+ totalUsed += tokens;
85
+ }
86
+ return {
87
+ used: totalUsed,
88
+ available: this.config.maxTokens - totalUsed,
89
+ categories
90
+ };
91
+ }
92
+ /**
93
+ * Calculate current token count for context
94
+ */
95
+ calculateCurrentTokens(context) {
96
+ this.tokenUsage.clear();
97
+ const taskTokens = this.estimateTokens(JSON.stringify(context.task));
98
+ const historyTokens = this.estimateTokens(JSON.stringify(context.history));
99
+ const envTokens = this.estimateTokens(JSON.stringify(context.environment));
100
+ const memoryTokens = this.estimateTokens(JSON.stringify(context.memory));
101
+ this.tokenUsage.set("task", taskTokens);
102
+ this.tokenUsage.set("history", historyTokens);
103
+ this.tokenUsage.set("environment", envTokens);
104
+ this.tokenUsage.set("memory", memoryTokens);
105
+ return taskTokens + historyTokens + envTokens + memoryTokens;
106
+ }
107
+ /**
108
+ * Adaptive budget allocation based on iteration phase
109
+ */
110
+ adaptiveBudgetAllocation(context, currentTokens) {
111
+ const reductionRatio = this.config.maxTokens / currentTokens;
112
+ const phase = this.determinePhase(context.task.currentIteration);
113
+ const adjustedWeights = this.getPhaseAdjustedWeights(phase);
114
+ return this.applyWeightedReduction(context, reductionRatio, adjustedWeights);
115
+ }
116
+ /**
117
+ * Priority-based allocation using fixed weights
118
+ */
119
+ priorityBasedAllocation(context, currentTokens) {
120
+ const reductionRatio = this.config.maxTokens / currentTokens;
121
+ return this.applyWeightedReduction(context, reductionRatio, this.config.priorityWeights);
122
+ }
123
+ /**
124
+ * Apply weighted reduction to context
125
+ */
126
+ applyWeightedReduction(context, reductionRatio, weights) {
127
+ const reduced = { ...context };
128
+ if (weights.recentWork < 1) {
129
+ const keepCount = Math.ceil(
130
+ context.history.recentIterations.length * reductionRatio * weights.recentWork
131
+ );
132
+ reduced.history = {
133
+ ...context.history,
134
+ recentIterations: context.history.recentIterations.slice(-keepCount)
135
+ };
136
+ }
137
+ if (weights.gitHistory < 1) {
138
+ const keepCount = Math.ceil(
139
+ context.history.gitCommits.length * reductionRatio * weights.gitHistory
140
+ );
141
+ reduced.history.gitCommits = context.history.gitCommits.slice(-keepCount);
142
+ }
143
+ if (weights.dependencies < 1) {
144
+ const keepCount = Math.ceil(
145
+ context.memory.relevantFrames.length * reductionRatio * weights.dependencies
146
+ );
147
+ reduced.memory = {
148
+ ...context.memory,
149
+ relevantFrames: context.memory.relevantFrames.sort((a, b) => (b.created_at || 0) - (a.created_at || 0)).slice(0, keepCount)
150
+ };
151
+ }
152
+ reduced.tokenCount = this.calculateCurrentTokens(reduced);
153
+ return reduced;
154
+ }
155
+ /**
156
+ * Compress task context
157
+ */
158
+ compressTaskContext(task) {
159
+ return {
160
+ ...task,
161
+ description: this.truncateWithEllipsis(task.description, 500),
162
+ criteria: task.criteria.slice(0, 5),
163
+ // Keep top 5 criteria
164
+ feedback: task.feedback ? this.truncateWithEllipsis(task.feedback, 300) : void 0
165
+ };
166
+ }
167
+ /**
168
+ * Compress history context
169
+ */
170
+ compressHistoryContext(history) {
171
+ return {
172
+ ...history,
173
+ recentIterations: history.recentIterations.slice(-5).map((iter) => ({
174
+ ...iter,
175
+ summary: this.truncateWithEllipsis(iter.summary, 100)
176
+ })),
177
+ gitCommits: history.gitCommits.slice(-10).map((commit) => ({
178
+ ...commit,
179
+ message: this.truncateWithEllipsis(commit.message, 80),
180
+ files: commit.files.slice(0, 5)
181
+ // Keep top 5 files
182
+ })),
183
+ changedFiles: history.changedFiles.slice(0, 20),
184
+ // Keep top 20 files
185
+ testResults: history.testResults.slice(-3)
186
+ // Keep last 3 test runs
187
+ };
188
+ }
189
+ /**
190
+ * Compress environment context
191
+ */
192
+ compressEnvironmentContext(env) {
193
+ return {
194
+ ...env,
195
+ dependencies: this.compressObject(env.dependencies, 20),
196
+ // Keep top 20 deps
197
+ configuration: this.compressObject(env.configuration, 10)
198
+ // Keep top 10 config items
199
+ };
200
+ }
201
+ /**
202
+ * Compress memory context
203
+ */
204
+ compressMemoryContext(memory) {
205
+ return {
206
+ ...memory,
207
+ relevantFrames: memory.relevantFrames.slice(0, 5),
208
+ // Keep top 5 frames
209
+ decisions: memory.decisions.filter((d) => d.impact !== "low").slice(-5),
210
+ // Keep last 5
211
+ patterns: memory.patterns.filter((p) => p.successRate > 0.7).slice(0, 3),
212
+ // Keep top 3
213
+ blockers: memory.blockers.filter((b) => !b.resolved)
214
+ // Keep unresolved only
215
+ };
216
+ }
217
+ /**
218
+ * Determine iteration phase
219
+ */
220
+ determinePhase(iteration) {
221
+ if (iteration <= 3) return "early";
222
+ if (iteration <= 10) return "middle";
223
+ return "late";
224
+ }
225
+ /**
226
+ * Get phase-adjusted weights
227
+ */
228
+ getPhaseAdjustedWeights(phase) {
229
+ switch (phase) {
230
+ case "early":
231
+ return {
232
+ task: 0.4,
233
+ recentWork: 0.1,
234
+ feedback: 0.2,
235
+ gitHistory: 0.2,
236
+ dependencies: 0.1
237
+ };
238
+ case "middle":
239
+ return this.config.priorityWeights;
240
+ case "late":
241
+ return {
242
+ task: 0.2,
243
+ recentWork: 0.35,
244
+ feedback: 0.25,
245
+ gitHistory: 0.15,
246
+ dependencies: 0.05
247
+ };
248
+ }
249
+ }
250
+ /**
251
+ * Detect if text contains code
252
+ */
253
+ detectCodeContent(text) {
254
+ const codePatterns = [
255
+ /function\s+\w+\s*\(/,
256
+ /class\s+\w+/,
257
+ /const\s+\w+\s*=/,
258
+ /import\s+.*from/,
259
+ /\{[\s\S]*\}/
260
+ ];
261
+ return codePatterns.some((pattern) => pattern.test(text));
262
+ }
263
+ /**
264
+ * Detect if text contains JSON
265
+ */
266
+ detectJsonContent(text) {
267
+ try {
268
+ JSON.parse(text);
269
+ return true;
270
+ } catch {
271
+ return text.includes('"') && text.includes(":") && text.includes("{");
272
+ }
273
+ }
274
+ /**
275
+ * Truncate text with ellipsis
276
+ */
277
+ truncateWithEllipsis(text, maxLength) {
278
+ if (text.length <= maxLength) return text;
279
+ return text.substring(0, maxLength - 3) + "...";
280
+ }
281
+ /**
282
+ * Compress object by keeping only top N entries
283
+ */
284
+ compressObject(obj, maxEntries) {
285
+ const entries = Object.entries(obj);
286
+ if (entries.length <= maxEntries) return obj;
287
+ const compressed = {};
288
+ entries.slice(0, maxEntries).forEach(([key, value]) => {
289
+ compressed[key] = value;
290
+ });
291
+ return compressed;
292
+ }
293
+ }
294
+ export {
295
+ ContextBudgetManager
296
+ };
297
+ //# sourceMappingURL=context-budget-manager.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/integrations/ralph/context/context-budget-manager.ts"],
4
+ "sourcesContent": ["/**\n * Context Budget Manager for Ralph-StackMemory Integration\n * Manages token allocation and context prioritization to prevent overwhelming iterations\n */\n\nimport { logger } from '../../../core/monitoring/logger.js';\nimport {\n IterationContext,\n TaskContext,\n HistoryContext,\n EnvironmentContext,\n MemoryContext,\n RalphStackMemoryConfig,\n TokenEstimate,\n IterationSummary,\n} from '../types.js';\n\nexport class ContextBudgetManager {\n private config: RalphStackMemoryConfig['contextBudget'];\n private tokenUsage: Map<string, number> = new Map();\n private readonly DEFAULT_MAX_TOKENS = 4000;\n private readonly TOKEN_CHAR_RATIO = 0.25; // Rough estimate: 1 token \u2248 4 chars\n\n constructor(config?: Partial<RalphStackMemoryConfig['contextBudget']>) {\n this.config = {\n maxTokens: config?.maxTokens || this.DEFAULT_MAX_TOKENS,\n priorityWeights: {\n task: config?.priorityWeights?.task || 0.3,\n recentWork: config?.priorityWeights?.recentWork || 0.25,\n feedback: config?.priorityWeights?.feedback || 0.2,\n gitHistory: config?.priorityWeights?.gitHistory || 0.15,\n dependencies: config?.priorityWeights?.dependencies || 0.1,\n },\n compressionEnabled: config?.compressionEnabled ?? true,\n adaptiveBudgeting: config?.adaptiveBudgeting ?? true,\n };\n }\n\n /**\n * Estimate tokens for a given text\n */\n estimateTokens(text: string): number {\n if (!text) return 0;\n \n // More accurate estimation based on common patterns\n const baseTokens = text.length * this.TOKEN_CHAR_RATIO;\n \n // Adjust for code content (typically more dense)\n const codeMultiplier = this.detectCodeContent(text) ? 1.2 : 1.0;\n \n // Adjust for JSON content (typically less dense)\n const jsonMultiplier = this.detectJsonContent(text) ? 0.9 : 1.0;\n \n return Math.ceil(baseTokens * codeMultiplier * jsonMultiplier);\n }\n\n /**\n * Allocate token budget across different context categories\n */\n allocateBudget(context: IterationContext): IterationContext {\n const currentTokens = this.calculateCurrentTokens(context);\n \n if (currentTokens <= this.config.maxTokens) {\n logger.debug('Context within budget', { \n used: currentTokens, \n max: this.config.maxTokens \n });\n return context;\n }\n\n logger.info('Context exceeds budget, optimizing...', {\n current: currentTokens,\n max: this.config.maxTokens,\n });\n\n // Apply adaptive budgeting if enabled\n if (this.config.adaptiveBudgeting) {\n return this.adaptiveBudgetAllocation(context, currentTokens);\n }\n\n // Apply fixed priority-based budgeting\n return this.priorityBasedAllocation(context, currentTokens);\n }\n\n /**\n * Compress context to fit within budget\n */\n compressContext(context: IterationContext): IterationContext {\n if (!this.config.compressionEnabled) {\n return context;\n }\n\n const compressed: IterationContext = {\n ...context,\n task: this.compressTaskContext(context.task),\n history: this.compressHistoryContext(context.history),\n environment: this.compressEnvironmentContext(context.environment),\n memory: this.compressMemoryContext(context.memory),\n tokenCount: 0,\n };\n\n compressed.tokenCount = this.calculateCurrentTokens(compressed);\n \n logger.debug('Context compressed', {\n original: context.tokenCount,\n compressed: compressed.tokenCount,\n reduction: `${Math.round((1 - compressed.tokenCount / context.tokenCount) * 100)}%`,\n });\n\n return compressed;\n }\n\n /**\n * Get current token usage statistics\n */\n getUsage(): { used: number; available: number; categories: Record<string, number> } {\n const categories: Record<string, number> = {};\n let totalUsed = 0;\n\n for (const [category, tokens] of this.tokenUsage) {\n categories[category] = tokens;\n totalUsed += tokens;\n }\n\n return {\n used: totalUsed,\n available: this.config.maxTokens - totalUsed,\n categories,\n };\n }\n\n /**\n * Calculate current token count for context\n */\n private calculateCurrentTokens(context: IterationContext): number {\n this.tokenUsage.clear();\n \n const taskTokens = this.estimateTokens(JSON.stringify(context.task));\n const historyTokens = this.estimateTokens(JSON.stringify(context.history));\n const envTokens = this.estimateTokens(JSON.stringify(context.environment));\n const memoryTokens = this.estimateTokens(JSON.stringify(context.memory));\n\n this.tokenUsage.set('task', taskTokens);\n this.tokenUsage.set('history', historyTokens);\n this.tokenUsage.set('environment', envTokens);\n this.tokenUsage.set('memory', memoryTokens);\n\n return taskTokens + historyTokens + envTokens + memoryTokens;\n }\n\n /**\n * Adaptive budget allocation based on iteration phase\n */\n private adaptiveBudgetAllocation(\n context: IterationContext,\n currentTokens: number\n ): IterationContext {\n const reductionRatio = this.config.maxTokens / currentTokens;\n \n // Determine phase based on iteration number\n const phase = this.determinePhase(context.task.currentIteration);\n \n // Adjust weights based on phase\n const adjustedWeights = this.getPhaseAdjustedWeights(phase);\n \n return this.applyWeightedReduction(context, reductionRatio, adjustedWeights);\n }\n\n /**\n * Priority-based allocation using fixed weights\n */\n private priorityBasedAllocation(\n context: IterationContext,\n currentTokens: number\n ): IterationContext {\n const reductionRatio = this.config.maxTokens / currentTokens;\n return this.applyWeightedReduction(context, reductionRatio, this.config.priorityWeights);\n }\n\n /**\n * Apply weighted reduction to context\n */\n private applyWeightedReduction(\n context: IterationContext,\n reductionRatio: number,\n weights: Record<string, number>\n ): IterationContext {\n const reduced: IterationContext = { ...context };\n\n // Reduce history based on weight\n if (weights.recentWork < 1.0) {\n const keepCount = Math.ceil(\n context.history.recentIterations.length * reductionRatio * weights.recentWork\n );\n reduced.history = {\n ...context.history,\n recentIterations: context.history.recentIterations.slice(-keepCount),\n };\n }\n\n // Reduce git history based on weight\n if (weights.gitHistory < 1.0) {\n const keepCount = Math.ceil(\n context.history.gitCommits.length * reductionRatio * weights.gitHistory\n );\n reduced.history.gitCommits = context.history.gitCommits.slice(-keepCount);\n }\n\n // Reduce memory frames based on weight\n if (weights.dependencies < 1.0) {\n const keepCount = Math.ceil(\n context.memory.relevantFrames.length * reductionRatio * weights.dependencies\n );\n reduced.memory = {\n ...context.memory,\n relevantFrames: context.memory.relevantFrames\n .sort((a, b) => (b.created_at || 0) - (a.created_at || 0))\n .slice(0, keepCount),\n };\n }\n\n reduced.tokenCount = this.calculateCurrentTokens(reduced);\n return reduced;\n }\n\n /**\n * Compress task context\n */\n private compressTaskContext(task: TaskContext): TaskContext {\n return {\n ...task,\n description: this.truncateWithEllipsis(task.description, 500),\n criteria: task.criteria.slice(0, 5), // Keep top 5 criteria\n feedback: task.feedback ? this.truncateWithEllipsis(task.feedback, 300) : undefined,\n };\n }\n\n /**\n * Compress history context\n */\n private compressHistoryContext(history: HistoryContext): HistoryContext {\n return {\n ...history,\n recentIterations: history.recentIterations\n .slice(-5) // Keep last 5 iterations\n .map(iter => ({\n ...iter,\n summary: this.truncateWithEllipsis(iter.summary, 100),\n })),\n gitCommits: history.gitCommits\n .slice(-10) // Keep last 10 commits\n .map(commit => ({\n ...commit,\n message: this.truncateWithEllipsis(commit.message, 80),\n files: commit.files.slice(0, 5), // Keep top 5 files\n })),\n changedFiles: history.changedFiles.slice(0, 20), // Keep top 20 files\n testResults: history.testResults.slice(-3), // Keep last 3 test runs\n };\n }\n\n /**\n * Compress environment context\n */\n private compressEnvironmentContext(env: EnvironmentContext): EnvironmentContext {\n return {\n ...env,\n dependencies: this.compressObject(env.dependencies, 20), // Keep top 20 deps\n configuration: this.compressObject(env.configuration, 10), // Keep top 10 config items\n };\n }\n\n /**\n * Compress memory context\n */\n private compressMemoryContext(memory: MemoryContext): MemoryContext {\n return {\n ...memory,\n relevantFrames: memory.relevantFrames.slice(0, 5), // Keep top 5 frames\n decisions: memory.decisions\n .filter(d => d.impact !== 'low') // Remove low impact decisions\n .slice(-5), // Keep last 5\n patterns: memory.patterns\n .filter(p => p.successRate > 0.7) // Keep successful patterns only\n .slice(0, 3), // Keep top 3\n blockers: memory.blockers.filter(b => !b.resolved), // Keep unresolved only\n };\n }\n\n /**\n * Determine iteration phase\n */\n private determinePhase(iteration: number): 'early' | 'middle' | 'late' {\n if (iteration <= 3) return 'early';\n if (iteration <= 10) return 'middle';\n return 'late';\n }\n\n /**\n * Get phase-adjusted weights\n */\n private getPhaseAdjustedWeights(phase: 'early' | 'middle' | 'late'): Record<string, number> {\n switch (phase) {\n case 'early':\n // Early phase: Focus on task understanding\n return {\n task: 0.4,\n recentWork: 0.1,\n feedback: 0.2,\n gitHistory: 0.2,\n dependencies: 0.1,\n };\n case 'middle':\n // Middle phase: Balance all aspects\n return this.config.priorityWeights;\n case 'late':\n // Late phase: Focus on recent work and feedback\n return {\n task: 0.2,\n recentWork: 0.35,\n feedback: 0.25,\n gitHistory: 0.15,\n dependencies: 0.05,\n };\n }\n }\n\n /**\n * Detect if text contains code\n */\n private detectCodeContent(text: string): boolean {\n const codePatterns = [\n /function\\s+\\w+\\s*\\(/,\n /class\\s+\\w+/,\n /const\\s+\\w+\\s*=/,\n /import\\s+.*from/,\n /\\{[\\s\\S]*\\}/,\n ];\n return codePatterns.some(pattern => pattern.test(text));\n }\n\n /**\n * Detect if text contains JSON\n */\n private detectJsonContent(text: string): boolean {\n try {\n JSON.parse(text);\n return true;\n } catch {\n return text.includes('\"') && text.includes(':') && text.includes('{');\n }\n }\n\n /**\n * Truncate text with ellipsis\n */\n private truncateWithEllipsis(text: string, maxLength: number): string {\n if (text.length <= maxLength) return text;\n return text.substring(0, maxLength - 3) + '...';\n }\n\n /**\n * Compress object by keeping only top N entries\n */\n private compressObject(obj: Record<string, any>, maxEntries: number): Record<string, any> {\n const entries = Object.entries(obj);\n if (entries.length <= maxEntries) return obj;\n \n const compressed: Record<string, any> = {};\n entries.slice(0, maxEntries).forEach(([key, value]) => {\n compressed[key] = value;\n });\n \n return compressed;\n }\n}"],
5
+ "mappings": "AAKA,SAAS,cAAc;AAYhB,MAAM,qBAAqB;AAAA,EACxB;AAAA,EACA,aAAkC,oBAAI,IAAI;AAAA,EACjC,qBAAqB;AAAA,EACrB,mBAAmB;AAAA;AAAA,EAEpC,YAAY,QAA2D;AACrE,SAAK,SAAS;AAAA,MACZ,WAAW,QAAQ,aAAa,KAAK;AAAA,MACrC,iBAAiB;AAAA,QACf,MAAM,QAAQ,iBAAiB,QAAQ;AAAA,QACvC,YAAY,QAAQ,iBAAiB,cAAc;AAAA,QACnD,UAAU,QAAQ,iBAAiB,YAAY;AAAA,QAC/C,YAAY,QAAQ,iBAAiB,cAAc;AAAA,QACnD,cAAc,QAAQ,iBAAiB,gBAAgB;AAAA,MACzD;AAAA,MACA,oBAAoB,QAAQ,sBAAsB;AAAA,MAClD,mBAAmB,QAAQ,qBAAqB;AAAA,IAClD;AAA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AQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACrD,iBAAW,GAAG,IAAI;AAAA,IACpB,CAAC;AAED,WAAO;AAAA,EACT;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,356 @@
1
+ import { logger } from "../../../core/monitoring/logger.js";
2
+ import { FrameManager } from "../../../core/context/frame-manager.js";
3
+ import { sharedContextLayer } from "../../../core/context/shared-context-layer.js";
4
+ import { ContextRetriever } from "../../../core/retrieval/context-retriever.js";
5
+ import { sessionManager } from "../../../core/session/index.js";
6
+ import { ContextBudgetManager } from "./context-budget-manager.js";
7
+ class StackMemoryContextLoader {
8
+ frameManager;
9
+ contextRetriever;
10
+ budgetManager;
11
+ config;
12
+ constructor(config) {
13
+ this.config = {
14
+ maxTokens: 3200,
15
+ // Leave room for task description
16
+ lookbackDays: 30,
17
+ similarityThreshold: 0.7,
18
+ patternDetectionEnabled: true,
19
+ includeFailedAttempts: true,
20
+ crossSessionSearch: true,
21
+ ...config
22
+ };
23
+ this.budgetManager = new ContextBudgetManager({
24
+ maxTokens: this.config.maxTokens,
25
+ priorityWeights: {
26
+ task: 0.15,
27
+ recentWork: 0.3,
28
+ patterns: 0.25,
29
+ decisions: 0.2,
30
+ dependencies: 0.1
31
+ }
32
+ });
33
+ logger.info("StackMemory context loader initialized", {
34
+ maxTokens: this.config.maxTokens,
35
+ lookbackDays: this.config.lookbackDays,
36
+ patternDetection: this.config.patternDetectionEnabled
37
+ });
38
+ }
39
+ async initialize() {
40
+ try {
41
+ await sessionManager.initialize();
42
+ await sharedContextLayer.initialize();
43
+ const session = await sessionManager.getOrCreateSession({});
44
+ if (session.database) {
45
+ this.frameManager = new FrameManager(session.database, session.projectId);
46
+ this.contextRetriever = new ContextRetriever(session.database);
47
+ }
48
+ logger.info("Context loader initialized successfully");
49
+ } catch (error) {
50
+ logger.error("Failed to initialize context loader", error);
51
+ throw error;
52
+ }
53
+ }
54
+ /**
55
+ * Load context for Ralph loop initialization
56
+ */
57
+ async loadInitialContext(request) {
58
+ logger.info("Loading initial context for Ralph loop", {
59
+ task: request.task.substring(0, 100),
60
+ usePatterns: request.usePatterns,
61
+ useSimilarTasks: request.useSimilarTasks
62
+ });
63
+ const sources = [];
64
+ let totalTokens = 0;
65
+ try {
66
+ if (request.useSimilarTasks) {
67
+ const similarTasks2 = await this.findSimilarTasks(request.task);
68
+ if (similarTasks2.length > 0) {
69
+ const tasksContext = await this.extractTaskContext(similarTasks2);
70
+ sources.push({
71
+ type: "similar_tasks",
72
+ weight: 0.3,
73
+ content: tasksContext,
74
+ tokens: this.budgetManager.estimateTokens(tasksContext)
75
+ });
76
+ totalTokens += sources[sources.length - 1].tokens;
77
+ }
78
+ }
79
+ if (request.usePatterns) {
80
+ const patterns2 = await this.extractRelevantPatterns(request.task);
81
+ if (patterns2.length > 0) {
82
+ const patternsContext = await this.formatPatterns(patterns2);
83
+ sources.push({
84
+ type: "historical_patterns",
85
+ weight: 0.25,
86
+ content: patternsContext,
87
+ tokens: this.budgetManager.estimateTokens(patternsContext)
88
+ });
89
+ totalTokens += sources[sources.length - 1].tokens;
90
+ }
91
+ }
92
+ const decisions = await this.loadRecentDecisions();
93
+ if (decisions.length > 0) {
94
+ const decisionsContext = this.formatDecisions(decisions);
95
+ sources.push({
96
+ type: "recent_decisions",
97
+ weight: 0.2,
98
+ content: decisionsContext,
99
+ tokens: this.budgetManager.estimateTokens(decisionsContext)
100
+ });
101
+ totalTokens += sources[sources.length - 1].tokens;
102
+ }
103
+ const projectContext = await this.loadProjectContext(request.task);
104
+ if (projectContext) {
105
+ sources.push({
106
+ type: "project_context",
107
+ weight: 0.15,
108
+ content: projectContext,
109
+ tokens: this.budgetManager.estimateTokens(projectContext)
110
+ });
111
+ totalTokens += sources[sources.length - 1].tokens;
112
+ }
113
+ const budgetedSources = this.budgetManager.allocateBudget({ sources });
114
+ const synthesizedContext = this.synthesizeContext(budgetedSources.sources);
115
+ logger.info("Context loaded successfully", {
116
+ totalSources: sources.length,
117
+ totalTokens,
118
+ budgetedTokens: budgetedSources.sources.reduce((sum, s) => sum + s.tokens, 0)
119
+ });
120
+ return {
121
+ context: synthesizedContext,
122
+ sources: budgetedSources.sources,
123
+ metadata: {
124
+ totalTokens: budgetedSources.sources.reduce((sum, s) => sum + s.tokens, 0),
125
+ sourcesCount: budgetedSources.sources.length,
126
+ patterns: request.usePatterns ? patterns : [],
127
+ similarTasks: request.useSimilarTasks ? similarTasks : []
128
+ }
129
+ };
130
+ } catch (error) {
131
+ logger.error("Failed to load context", error);
132
+ throw error;
133
+ }
134
+ }
135
+ /**
136
+ * Find similar tasks from StackMemory history
137
+ */
138
+ async findSimilarTasks(taskDescription) {
139
+ if (!this.frameManager || !this.contextRetriever) {
140
+ return [];
141
+ }
142
+ try {
143
+ const searchResults = await this.contextRetriever.search(taskDescription, {
144
+ maxResults: 10,
145
+ types: ["task", "subtask"],
146
+ timeFilter: {
147
+ days: this.config.lookbackDays
148
+ }
149
+ });
150
+ const similarities = [];
151
+ for (const result of searchResults) {
152
+ const similarity = this.calculateTaskSimilarity(taskDescription, result.content);
153
+ if (similarity >= this.config.similarityThreshold) {
154
+ similarities.push({
155
+ frameId: result.frameId,
156
+ task: result.content,
157
+ similarity,
158
+ outcome: await this.determineTaskOutcome(result.frameId),
159
+ createdAt: result.timestamp,
160
+ sessionId: result.sessionId || "unknown"
161
+ });
162
+ }
163
+ }
164
+ return similarities.sort((a, b) => {
165
+ const aScore = a.similarity * (a.outcome === "success" ? 1.2 : 1);
166
+ const bScore = b.similarity * (b.outcome === "success" ? 1.2 : 1);
167
+ return bScore - aScore;
168
+ }).slice(0, 5);
169
+ } catch (error) {
170
+ logger.error("Failed to find similar tasks", error);
171
+ return [];
172
+ }
173
+ }
174
+ /**
175
+ * Extract relevant patterns from historical data
176
+ */
177
+ async extractRelevantPatterns(taskDescription) {
178
+ try {
179
+ const context = await sharedContextLayer.getSharedContext();
180
+ if (!context) return [];
181
+ const relevantPatterns = [];
182
+ for (const pattern of context.globalPatterns) {
183
+ const relevance = this.calculatePatternRelevance(taskDescription, pattern.pattern);
184
+ if (relevance >= 0.5) {
185
+ relevantPatterns.push({
186
+ pattern: pattern.pattern,
187
+ type: pattern.type,
188
+ frequency: pattern.frequency,
189
+ lastSeen: pattern.lastSeen,
190
+ relevance,
191
+ resolution: pattern.resolution,
192
+ examples: await this.getPatternExamples(pattern.pattern)
193
+ });
194
+ }
195
+ }
196
+ return relevantPatterns.sort((a, b) => b.relevance * Math.log(b.frequency + 1) - a.relevance * Math.log(a.frequency + 1)).slice(0, 8);
197
+ } catch (error) {
198
+ logger.error("Failed to extract patterns", error);
199
+ return [];
200
+ }
201
+ }
202
+ /**
203
+ * Load recent decisions that might be relevant
204
+ */
205
+ async loadRecentDecisions() {
206
+ try {
207
+ const context = await sharedContextLayer.getSharedContext();
208
+ if (!context) return [];
209
+ const cutoff = Date.now() - 7 * 24 * 60 * 60 * 1e3;
210
+ return context.decisionLog.filter((d) => d.timestamp >= cutoff && d.outcome === "success").sort((a, b) => b.timestamp - a.timestamp).slice(0, 5);
211
+ } catch (error) {
212
+ logger.error("Failed to load recent decisions", error);
213
+ return [];
214
+ }
215
+ }
216
+ /**
217
+ * Load project-specific context
218
+ */
219
+ async loadProjectContext(taskDescription) {
220
+ try {
221
+ if (!this.contextRetriever) return null;
222
+ const projectInfo = await this.contextRetriever.search(taskDescription, {
223
+ maxResults: 3,
224
+ types: ["task"],
225
+ projectSpecific: true
226
+ });
227
+ if (projectInfo.length === 0) return null;
228
+ const contextParts = [];
229
+ for (const info of projectInfo) {
230
+ contextParts.push(`Project context: ${info.content}`);
231
+ }
232
+ return contextParts.join("\n\n");
233
+ } catch (error) {
234
+ logger.error("Failed to load project context", error);
235
+ return null;
236
+ }
237
+ }
238
+ /**
239
+ * Calculate similarity between task descriptions
240
+ */
241
+ calculateTaskSimilarity(task1, task2) {
242
+ const words1 = new Set(task1.toLowerCase().split(/\s+/));
243
+ const words2 = new Set(task2.toLowerCase().split(/\s+/));
244
+ const intersection = new Set([...words1].filter((x) => words2.has(x)));
245
+ const union = /* @__PURE__ */ new Set([...words1, ...words2]);
246
+ return intersection.size / union.size;
247
+ }
248
+ /**
249
+ * Calculate pattern relevance to current task
250
+ */
251
+ calculatePatternRelevance(taskDescription, pattern) {
252
+ const taskWords = taskDescription.toLowerCase().split(/\s+/);
253
+ const patternWords = pattern.toLowerCase().split(/\s+/);
254
+ let matches = 0;
255
+ for (const word of taskWords) {
256
+ if (patternWords.some((p) => p.includes(word) || word.includes(p))) {
257
+ matches++;
258
+ }
259
+ }
260
+ return matches / taskWords.length;
261
+ }
262
+ /**
263
+ * Extract context from similar tasks
264
+ */
265
+ async extractTaskContext(similarities) {
266
+ const contextParts = [];
267
+ contextParts.push("Similar tasks from history:");
268
+ for (const sim of similarities) {
269
+ contextParts.push(`
270
+ Task: ${sim.task}
271
+ Outcome: ${sim.outcome}
272
+ Similarity: ${Math.round(sim.similarity * 100)}%
273
+ ${sim.outcome === "success" ? "\u2705 Successfully completed" : "\u274C Had issues"}
274
+ `.trim());
275
+ }
276
+ return contextParts.join("\n\n");
277
+ }
278
+ /**
279
+ * Format patterns for context inclusion
280
+ */
281
+ async formatPatterns(patterns2) {
282
+ const contextParts = [];
283
+ contextParts.push("Relevant patterns from experience:");
284
+ for (const pattern of patterns2) {
285
+ contextParts.push(`
286
+ Pattern: ${pattern.pattern}
287
+ Type: ${pattern.type}
288
+ Frequency: ${pattern.frequency} occurrences
289
+ ${pattern.resolution ? `Resolution: ${pattern.resolution}` : ""}
290
+ Relevance: ${Math.round(pattern.relevance * 100)}%
291
+ `.trim());
292
+ }
293
+ return contextParts.join("\n\n");
294
+ }
295
+ /**
296
+ * Format decisions for context inclusion
297
+ */
298
+ formatDecisions(decisions) {
299
+ const contextParts = [];
300
+ contextParts.push("Recent successful decisions:");
301
+ for (const decision of decisions) {
302
+ contextParts.push(`
303
+ Decision: ${decision.decision}
304
+ Reasoning: ${decision.reasoning}
305
+ Date: ${new Date(decision.timestamp).toLocaleDateString()}
306
+ `.trim());
307
+ }
308
+ return contextParts.join("\n\n");
309
+ }
310
+ /**
311
+ * Synthesize all context sources into coherent input
312
+ */
313
+ synthesizeContext(sources) {
314
+ if (sources.length === 0) {
315
+ return "No relevant historical context found.";
316
+ }
317
+ const contextParts = [];
318
+ contextParts.push("Context from StackMemory:");
319
+ const sortedSources = sources.sort((a, b) => b.weight - a.weight);
320
+ for (const source of sortedSources) {
321
+ contextParts.push(`
322
+ --- ${source.type.replace("_", " ").toUpperCase()} ---`);
323
+ contextParts.push(source.content);
324
+ }
325
+ contextParts.push("\nUse this context to inform your approach to the current task.");
326
+ return contextParts.join("\n");
327
+ }
328
+ /**
329
+ * Determine task outcome from frame history
330
+ */
331
+ async determineTaskOutcome(frameId) {
332
+ try {
333
+ if (!this.frameManager) return "unknown";
334
+ const frame = await this.frameManager.getFrame(frameId);
335
+ if (!frame) return "unknown";
336
+ if (frame.state === "closed" && frame.outputs) {
337
+ return "success";
338
+ }
339
+ return frame.state === "closed" ? "failure" : "unknown";
340
+ } catch {
341
+ return "unknown";
342
+ }
343
+ }
344
+ /**
345
+ * Get examples of a specific pattern
346
+ */
347
+ async getPatternExamples(pattern) {
348
+ return [];
349
+ }
350
+ }
351
+ const stackMemoryContextLoader = new StackMemoryContextLoader();
352
+ export {
353
+ StackMemoryContextLoader,
354
+ stackMemoryContextLoader
355
+ };
356
+ //# sourceMappingURL=stackmemory-context-loader.js.map