@stackmemoryai/stackmemory 1.12.0 → 1.14.0

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 (162) hide show
  1. package/LICENSE +131 -64
  2. package/README.md +3 -1
  3. package/bin/claude-sm +16 -1
  4. package/bin/claude-smd +16 -1
  5. package/bin/codex-smd +16 -1
  6. package/bin/gemini-sm +16 -1
  7. package/bin/hermes-sm +21 -0
  8. package/bin/hermes-smd +21 -0
  9. package/bin/opencode-sm +16 -1
  10. package/dist/src/cli/codex-sm.js +51 -11
  11. package/dist/src/cli/commands/brain.js +206 -0
  12. package/dist/src/cli/commands/company-os.js +184 -0
  13. package/dist/src/cli/commands/context.js +5 -0
  14. package/dist/src/cli/commands/operator.js +127 -0
  15. package/dist/src/cli/commands/orchestrate.js +2 -0
  16. package/dist/src/cli/commands/orchestrator.js +3 -2
  17. package/dist/src/cli/commands/patterns.js +254 -0
  18. package/dist/src/cli/commands/portal.js +161 -0
  19. package/dist/src/cli/commands/scaffold.js +92 -0
  20. package/dist/src/cli/commands/setup.js +1 -4
  21. package/dist/src/cli/commands/sync.js +253 -0
  22. package/dist/src/cli/commands/tasks.js +130 -1
  23. package/dist/src/cli/commands/vision.js +221 -0
  24. package/dist/src/cli/hermes-sm.js +224 -0
  25. package/dist/src/cli/index.js +15 -10
  26. package/dist/src/cli/utils/real-cli-bin.js +72 -0
  27. package/dist/src/core/brain/brain-store.js +187 -0
  28. package/dist/src/core/brain/brain-sync.js +193 -0
  29. package/dist/src/core/brain/index.js +78 -0
  30. package/dist/src/core/brain/types.js +10 -0
  31. package/dist/src/core/cache/token-estimator.js +24 -1
  32. package/dist/src/core/config/feature-flags.js +2 -6
  33. package/dist/src/core/context/frame-database.js +44 -0
  34. package/dist/src/core/context/recursive-context-manager.js +1 -1
  35. package/dist/src/core/context/rehydration.js +2 -1
  36. package/dist/src/core/database/sqlite-adapter.js +14 -1
  37. package/dist/src/core/models/model-router.js +33 -1
  38. package/dist/src/core/models/provider-pricing.js +58 -4
  39. package/dist/src/core/patterns/index.js +22 -0
  40. package/dist/src/core/patterns/pattern-applier.js +39 -0
  41. package/dist/src/core/patterns/pattern-observer.js +157 -0
  42. package/dist/src/core/patterns/pattern-store.js +259 -0
  43. package/dist/src/core/patterns/types.js +19 -0
  44. package/dist/src/core/retrieval/llm-context-retrieval.js +5 -4
  45. package/dist/src/core/retrieval/unified-context-assembler.js +11 -66
  46. package/dist/src/core/skill-packs/types.js +14 -1
  47. package/dist/src/core/storage/cloud-sync-manager.js +116 -0
  48. package/dist/src/core/storage/cloud-sync.js +574 -0
  49. package/dist/src/core/storage/two-tier-storage.js +5 -1
  50. package/dist/src/core/tasks/master-tasks-template.js +43 -0
  51. package/dist/src/core/tasks/md-task-parser.js +138 -0
  52. package/dist/src/core/vision/index.js +27 -0
  53. package/dist/src/core/vision/signals.js +79 -0
  54. package/dist/src/core/vision/types.js +22 -0
  55. package/dist/src/core/vision/vision-file.js +146 -0
  56. package/dist/src/core/vision/vision-loop.js +220 -0
  57. package/dist/src/core/wiki/wiki-compiler.js +103 -1
  58. package/dist/src/daemon/daemon-config.js +45 -0
  59. package/dist/src/daemon/services/desire-path-service.js +566 -0
  60. package/dist/src/daemon/services/research-stream-service.js +320 -0
  61. package/dist/src/daemon/services/telemetry-service.js +192 -0
  62. package/dist/src/daemon/unified-daemon.js +28 -1
  63. package/dist/src/features/browser/cli-browser-agent.js +417 -0
  64. package/dist/src/features/browser/stagehand-workflows.js +578 -0
  65. package/dist/src/features/operator/adapter-factory.js +62 -0
  66. package/dist/src/features/operator/browser-adapter.js +109 -0
  67. package/dist/src/features/operator/desktop-adapter.js +125 -0
  68. package/dist/src/features/operator/index.js +39 -0
  69. package/dist/src/features/operator/llm-decision.js +137 -0
  70. package/dist/src/features/operator/operator-logger.js +92 -0
  71. package/dist/src/features/operator/overnight-runner.js +327 -0
  72. package/dist/src/features/operator/screen-adapter.js +91 -0
  73. package/dist/src/features/operator/session-manager.js +127 -0
  74. package/dist/src/features/operator/state-machine.js +227 -0
  75. package/dist/src/features/operator/task-queue.js +81 -0
  76. package/dist/src/features/portal/index.js +26 -0
  77. package/dist/src/features/portal/server.js +240 -0
  78. package/dist/src/features/portal/types.js +14 -0
  79. package/dist/src/features/portal/ui.js +195 -0
  80. package/dist/src/features/tasks/task-aware-context.js +2 -1
  81. package/dist/src/features/tui/simple-monitor.js +0 -23
  82. package/dist/src/features/tui/swarm-monitor.js +8 -66
  83. package/dist/src/{integrations/diffmem/index.js → features/web/client/hooks/use-socket.js} +6 -5
  84. package/dist/src/features/web/client/lib/utils.js +12 -0
  85. package/dist/src/features/web/client/stores/session-store.js +12 -0
  86. package/dist/src/features/web/server/gcp-billing.js +76 -0
  87. package/dist/src/features/web/server/index.js +10 -0
  88. package/dist/src/features/web/server/spend-calculator.js +228 -0
  89. package/dist/src/hooks/schemas.js +4 -1
  90. package/dist/src/integrations/anthropic/client.js +3 -2
  91. package/dist/src/integrations/claude-code/agent-bridge.js +0 -3
  92. package/dist/src/integrations/claude-code/subagent-client.js +218 -11
  93. package/dist/src/integrations/claude-code/task-coordinator.js +2 -1
  94. package/dist/src/integrations/linear/webhook-retry.js +196 -0
  95. package/dist/src/integrations/linear/webhook-server.js +18 -22
  96. package/dist/src/integrations/mcp/handlers/cloud-sync-handlers.js +101 -0
  97. package/dist/src/integrations/mcp/handlers/index.js +27 -52
  98. package/dist/src/integrations/mcp/server.js +122 -335
  99. package/dist/src/integrations/mcp/tool-alias-registry.js +0 -73
  100. package/dist/src/integrations/mcp/tool-definitions.js +111 -510
  101. package/dist/src/mcp/stackmemory-mcp-server.js +404 -379
  102. package/dist/src/orchestrators/multimodal/determinism.js +2 -1
  103. package/dist/src/orchestrators/multimodal/harness.js +2 -1
  104. package/dist/src/skills/recursive-agent-orchestrator.js +2 -4
  105. package/dist/src/utils/process-cleanup.js +1 -7
  106. package/docs/README.md +42 -0
  107. package/docs/guides/README_INSTALL.md +208 -0
  108. package/package.json +18 -9
  109. package/scripts/claude-code-wrapper.sh +11 -0
  110. package/scripts/claude-sm-setup.sh +12 -1
  111. package/scripts/codex-wrapper.sh +11 -0
  112. package/scripts/git-hooks/branch-context-manager.sh +11 -0
  113. package/scripts/git-hooks/post-checkout-stackmemory.sh +11 -0
  114. package/scripts/git-hooks/post-commit-stackmemory.sh +11 -0
  115. package/scripts/git-hooks/pre-commit-stackmemory.sh +11 -0
  116. package/scripts/hooks/cleanup-shell.sh +12 -1
  117. package/scripts/hooks/task-complete.sh +12 -1
  118. package/scripts/install-code-execution-hooks.sh +12 -1
  119. package/scripts/install-sweep-hook.sh +12 -0
  120. package/scripts/install.sh +11 -0
  121. package/scripts/opencode-wrapper.sh +11 -0
  122. package/scripts/portal/cloud-init.yaml +69 -0
  123. package/scripts/portal/setup.sh +69 -0
  124. package/scripts/portal/stackmemory-portal.service +34 -0
  125. package/scripts/setup-claude-integration.sh +12 -1
  126. package/scripts/smoke-init-db.sh +23 -0
  127. package/scripts/stackmemory-daemon.sh +11 -0
  128. package/scripts/verify-dist.cjs +11 -4
  129. package/dist/src/cli/commands/ralph.js +0 -1053
  130. package/dist/src/hooks/diffmem-hooks.js +0 -376
  131. package/dist/src/integrations/diffmem/client.js +0 -208
  132. package/dist/src/integrations/diffmem/config.js +0 -14
  133. package/dist/src/integrations/greptile/client.js +0 -101
  134. package/dist/src/integrations/greptile/config.js +0 -14
  135. package/dist/src/integrations/greptile/index.js +0 -11
  136. package/dist/src/integrations/mcp/handlers/cross-search-handlers.js +0 -188
  137. package/dist/src/integrations/mcp/handlers/diffmem-handlers.js +0 -455
  138. package/dist/src/integrations/mcp/handlers/greptile-handlers.js +0 -456
  139. package/dist/src/integrations/mcp/handlers/provider-handlers.js +0 -227
  140. package/dist/src/integrations/ralph/bridge/ralph-stackmemory-bridge.js +0 -863
  141. package/dist/src/integrations/ralph/context/context-budget-manager.js +0 -308
  142. package/dist/src/integrations/ralph/context/stackmemory-context-loader.js +0 -354
  143. package/dist/src/integrations/ralph/index.js +0 -17
  144. package/dist/src/integrations/ralph/learning/pattern-learner.js +0 -416
  145. package/dist/src/integrations/ralph/lifecycle/iteration-lifecycle.js +0 -448
  146. package/dist/src/integrations/ralph/loopmax.js +0 -488
  147. package/dist/src/integrations/ralph/monitoring/swarm-dashboard.js +0 -293
  148. package/dist/src/integrations/ralph/monitoring/swarm-registry.js +0 -107
  149. package/dist/src/integrations/ralph/orchestration/multi-loop-orchestrator.js +0 -508
  150. package/dist/src/integrations/ralph/patterns/compounding-engineering-pattern.js +0 -407
  151. package/dist/src/integrations/ralph/patterns/extended-coherence-sessions.js +0 -495
  152. package/dist/src/integrations/ralph/patterns/oracle-worker-pattern.js +0 -387
  153. package/dist/src/integrations/ralph/performance/performance-optimizer.js +0 -357
  154. package/dist/src/integrations/ralph/recovery/crash-recovery.js +0 -461
  155. package/dist/src/integrations/ralph/state/state-reconciler.js +0 -420
  156. package/dist/src/integrations/ralph/swarm/git-workflow-manager.js +0 -444
  157. package/dist/src/integrations/ralph/swarm/swarm-coordinator.js +0 -1005
  158. package/dist/src/integrations/ralph/visualization/ralph-debugger.js +0 -635
  159. package/scripts/ralph-loop-implementation.js +0 -404
  160. /package/dist/src/{integrations/diffmem/types.js → core/storage/cloud-sync-types.js} +0 -0
  161. /package/dist/src/{integrations/greptile → features/operator}/types.js +0 -0
  162. /package/dist/src/{integrations/ralph/types.js → features/web/client/next-env.d.js} +0 -0
@@ -1,863 +0,0 @@
1
- import { fileURLToPath as __fileURLToPath } from 'url';
2
- import { dirname as __pathDirname } from 'path';
3
- const __filename = __fileURLToPath(import.meta.url);
4
- const __dirname = __pathDirname(__filename);
5
- import { v4 as uuidv4 } from "uuid";
6
- import * as fs from "fs/promises";
7
- import * as path from "path";
8
- import { execSync } from "child_process";
9
- import { logger } from "../../../core/monitoring/logger.js";
10
- import { FrameManager } from "../../../core/context/index.js";
11
- import { SessionManager } from "../../../core/session/session-manager.js";
12
- import { SQLiteAdapter } from "../../../core/database/sqlite-adapter.js";
13
- import { createTransformersProvider } from "../../../core/database/transformers-embedding-provider.js";
14
- import { ContextBudgetManager } from "../context/context-budget-manager.js";
15
- import { StateReconciler } from "../state/state-reconciler.js";
16
- import {
17
- IterationLifecycle
18
- } from "../lifecycle/iteration-lifecycle.js";
19
- import { PerformanceOptimizer } from "../performance/performance-optimizer.js";
20
- class RalphStackMemoryBridge {
21
- state;
22
- config;
23
- frameManager;
24
- sessionManager;
25
- recoveryState;
26
- ralphDir = ".ralph";
27
- requiresDatabase;
28
- constructor(options) {
29
- this.config = this.mergeConfig(options?.config);
30
- this.requiresDatabase = options?.useStackMemory !== false;
31
- this.state = {
32
- initialized: false,
33
- contextManager: new ContextBudgetManager(this.config.contextBudget),
34
- stateReconciler: new StateReconciler(this.config.stateReconciliation),
35
- performanceOptimizer: new PerformanceOptimizer(this.config.performance)
36
- };
37
- this.sessionManager = SessionManager.getInstance();
38
- this.setupLifecycleHooks(options);
39
- logger.info("Ralph-StackMemory Bridge initialized", {
40
- config: {
41
- maxTokens: this.config.contextBudget.maxTokens,
42
- asyncSaves: this.config.performance.asyncSaves,
43
- checkpoints: this.config.lifecycle.checkpoints.enabled
44
- }
45
- });
46
- }
47
- /**
48
- * Initialize bridge with session
49
- */
50
- async initialize(options) {
51
- logger.info("Initializing bridge", options);
52
- try {
53
- await this.sessionManager.initialize();
54
- const session = await this.sessionManager.getOrCreateSession({
55
- sessionId: options?.sessionId
56
- });
57
- this.state.currentSession = session;
58
- const dbAdapter = await this.getDatabaseAdapter();
59
- await dbAdapter.connect();
60
- const db = dbAdapter.db;
61
- const projectId = path.basename(this.ralphDir);
62
- this.frameManager = new FrameManager(db, projectId, {
63
- skipContextBridge: true
64
- });
65
- if (this.requiresDatabase) {
66
- if (session.database && session.projectId) {
67
- this.frameManager = new FrameManager(
68
- session.database,
69
- session.projectId,
70
- {
71
- skipContextBridge: true
72
- }
73
- );
74
- } else {
75
- throw new Error(
76
- "Session database not available for FrameManager initialization. If StackMemory features are not needed, set useStackMemory: false in options"
77
- );
78
- }
79
- } else {
80
- logger.info(
81
- "Running without StackMemory database (useStackMemory: false)"
82
- );
83
- }
84
- if (options?.loopId) {
85
- await this.resumeLoop(options.loopId);
86
- } else if (options?.task && options?.criteria) {
87
- await this.createNewLoop(options.task, options.criteria);
88
- } else {
89
- await this.attemptRecovery();
90
- }
91
- this.state.initialized = true;
92
- logger.info("Bridge initialized successfully");
93
- } catch (error) {
94
- logger.error("Bridge initialization failed", { error: error.message });
95
- throw error;
96
- }
97
- }
98
- /**
99
- * Create new Ralph loop with StackMemory integration
100
- */
101
- async createNewLoop(task, criteria) {
102
- logger.info("Creating new Ralph loop", { task: task.substring(0, 100) });
103
- const loopId = uuidv4();
104
- const startTime = Date.now();
105
- const loopState = {
106
- loopId,
107
- task,
108
- criteria,
109
- iteration: 0,
110
- status: "initialized",
111
- startTime,
112
- lastUpdateTime: startTime,
113
- startCommit: await this.getCurrentGitCommit()
114
- };
115
- await this.initializeRalphDirectory(loopState);
116
- const rootFrame = await this.createRootFrame(loopState);
117
- await this.saveLoopState(loopState);
118
- this.state.activeLoop = loopState;
119
- logger.info("Ralph loop created", {
120
- loopId,
121
- frameId: rootFrame.frame_id
122
- });
123
- return loopState;
124
- }
125
- /**
126
- * Resume existing loop
127
- */
128
- async resumeLoop(loopId) {
129
- logger.info("Resuming loop", { loopId });
130
- const sources = await this.gatherStateSources(loopId);
131
- const reconciledState = await this.state.stateReconciler.reconcile(sources);
132
- if (this.config.stateReconciliation.validateConsistency) {
133
- const validation = await this.state.stateReconciler.validateConsistency(reconciledState);
134
- if (validation.errors.length > 0) {
135
- logger.error("State validation failed", { errors: validation.errors });
136
- throw new Error(`Invalid state: ${validation.errors.join(", ")}`);
137
- }
138
- }
139
- this.state.activeLoop = reconciledState;
140
- const _context = await this.loadIterationContext(reconciledState);
141
- logger.info("Loop resumed", {
142
- loopId,
143
- iteration: reconciledState.iteration,
144
- status: reconciledState.status
145
- });
146
- return reconciledState;
147
- }
148
- /**
149
- * Run worker iteration
150
- */
151
- async runWorkerIteration() {
152
- if (!this.state.activeLoop) {
153
- throw new Error("No active loop");
154
- }
155
- const iterationNumber = this.state.activeLoop.iteration + 1;
156
- logger.info("Starting worker iteration", { iteration: iterationNumber });
157
- let context = await this.loadIterationContext(this.state.activeLoop);
158
- context = this.state.contextManager.allocateBudget(context);
159
- if (this.config.contextBudget.compressionEnabled) {
160
- context = this.state.contextManager.compressContext(context);
161
- }
162
- const lifecycle = this.getLifecycle();
163
- context = await lifecycle.startIteration(iterationNumber, context);
164
- const iteration = await this.executeWorkerIteration(context);
165
- await this.saveIterationResults(iteration);
166
- await lifecycle.completeIteration(iteration);
167
- this.state.activeLoop.iteration = iterationNumber;
168
- this.state.activeLoop.lastUpdateTime = Date.now();
169
- await this.saveLoopState(this.state.activeLoop);
170
- logger.info("Worker iteration completed", {
171
- iteration: iterationNumber,
172
- changes: iteration.changes.length,
173
- success: iteration.validation.testsPass
174
- });
175
- return iteration;
176
- }
177
- /**
178
- * Run reviewer iteration
179
- */
180
- async runReviewerIteration() {
181
- if (!this.state.activeLoop) {
182
- throw new Error("No active loop");
183
- }
184
- logger.info("Starting reviewer iteration", {
185
- iteration: this.state.activeLoop.iteration
186
- });
187
- const evaluation = await this.evaluateCompletion();
188
- if (evaluation.complete) {
189
- this.state.activeLoop.status = "completed";
190
- this.state.activeLoop.completionData = evaluation;
191
- await this.saveLoopState(this.state.activeLoop);
192
- const lifecycle = this.getLifecycle();
193
- await lifecycle.handleCompletion(this.state.activeLoop);
194
- logger.info("Task completed successfully");
195
- return { complete: true };
196
- }
197
- const feedback = this.generateFeedback(evaluation);
198
- this.state.activeLoop.feedback = feedback;
199
- await this.saveLoopState(this.state.activeLoop);
200
- logger.info("Reviewer iteration completed", {
201
- complete: false,
202
- feedbackLength: feedback.length
203
- });
204
- return { complete: false, feedback };
205
- }
206
- /**
207
- * Rehydrate session from StackMemory
208
- */
209
- async rehydrateSession(sessionId) {
210
- logger.info("Rehydrating session", { sessionId });
211
- const session = await this.sessionManager.getSession(sessionId);
212
- if (!session) {
213
- throw new Error(`Session not found: ${sessionId}`);
214
- }
215
- const frames = await this.loadSessionFrames(sessionId);
216
- const ralphFrames = frames.filter(
217
- (f) => f.type === "task" && f.name.startsWith("ralph-")
218
- );
219
- if (ralphFrames.length === 0) {
220
- throw new Error("No Ralph loops found in session");
221
- }
222
- const latestLoop = ralphFrames[ralphFrames.length - 1];
223
- const loopState = await this.reconstructLoopState(latestLoop);
224
- const context = await this.buildContextFromFrames(frames, loopState);
225
- this.state.activeLoop = loopState;
226
- logger.info("Session rehydrated", {
227
- loopId: loopState.loopId,
228
- iteration: loopState.iteration,
229
- frameCount: frames.length
230
- });
231
- return context;
232
- }
233
- /**
234
- * Create checkpoint
235
- */
236
- async createCheckpoint() {
237
- if (!this.state.activeLoop) {
238
- throw new Error("No active loop");
239
- }
240
- const lifecycle = this.getLifecycle();
241
- const iteration = {
242
- number: this.state.activeLoop.iteration,
243
- timestamp: Date.now(),
244
- analysis: {
245
- filesCount: 0,
246
- testsPass: true,
247
- testsFail: 0,
248
- lastChange: await this.getCurrentGitCommit()
249
- },
250
- plan: {
251
- summary: "Checkpoint",
252
- steps: [],
253
- priority: "low"
254
- },
255
- changes: [],
256
- validation: {
257
- testsPass: true,
258
- lintClean: true,
259
- buildSuccess: true,
260
- errors: [],
261
- warnings: []
262
- }
263
- };
264
- const checkpoint = await lifecycle.createCheckpoint(iteration);
265
- logger.info("Checkpoint created", {
266
- id: checkpoint.id,
267
- iteration: checkpoint.iteration
268
- });
269
- return checkpoint;
270
- }
271
- /**
272
- * Restore from checkpoint
273
- */
274
- async restoreFromCheckpoint(checkpointId) {
275
- const lifecycle = this.getLifecycle();
276
- await lifecycle.restoreFromCheckpoint(checkpointId);
277
- const sources = await this.gatherStateSources(
278
- this.state.activeLoop?.loopId || ""
279
- );
280
- const reconciledState = await this.state.stateReconciler.reconcile(sources);
281
- this.state.activeLoop = reconciledState;
282
- logger.info("Restored from checkpoint", {
283
- checkpointId,
284
- iteration: reconciledState.iteration
285
- });
286
- }
287
- /**
288
- * Get performance metrics
289
- */
290
- getPerformanceMetrics() {
291
- return this.state.performanceOptimizer.getMetrics();
292
- }
293
- /**
294
- * Start a new Ralph loop
295
- */
296
- async startLoop(options) {
297
- const state = await this.createNewLoop(options.task, options.criteria);
298
- return state.loopId;
299
- }
300
- /**
301
- * Stop the active loop
302
- */
303
- async stopLoop() {
304
- if (!this.state.activeLoop) {
305
- logger.warn("No active loop to stop");
306
- return;
307
- }
308
- this.state.activeLoop.status = "completed";
309
- this.state.activeLoop.lastUpdateTime = Date.now();
310
- await this.saveLoopState(this.state.activeLoop);
311
- const lifecycle = this.getLifecycle();
312
- await lifecycle.handleCompletion(this.state.activeLoop);
313
- logger.info("Ralph loop stopped", { loopId: this.state.activeLoop.loopId });
314
- this.state.activeLoop = void 0;
315
- }
316
- /**
317
- * Cleanup resources
318
- */
319
- async cleanup() {
320
- logger.info("Cleaning up bridge resources");
321
- await this.state.performanceOptimizer.flushBatch();
322
- this.getLifecycle().cleanup();
323
- this.state.performanceOptimizer.cleanup();
324
- logger.info("Bridge cleanup completed");
325
- }
326
- /**
327
- * Merge configuration with defaults
328
- */
329
- mergeConfig(config) {
330
- return {
331
- contextBudget: {
332
- maxTokens: 4e3,
333
- priorityWeights: {
334
- task: 0.3,
335
- recentWork: 0.25,
336
- feedback: 0.2,
337
- gitHistory: 0.15,
338
- dependencies: 0.1
339
- },
340
- compressionEnabled: true,
341
- adaptiveBudgeting: true,
342
- ...config?.contextBudget
343
- },
344
- stateReconciliation: {
345
- precedence: ["git", "files", "memory"],
346
- conflictResolution: "automatic",
347
- syncInterval: 5e3,
348
- validateConsistency: true,
349
- ...config?.stateReconciliation
350
- },
351
- lifecycle: {
352
- hooks: {
353
- preIteration: true,
354
- postIteration: true,
355
- onStateChange: true,
356
- onError: true,
357
- onComplete: true
358
- },
359
- checkpoints: {
360
- enabled: true,
361
- frequency: 5,
362
- retentionDays: 7
363
- },
364
- ...config?.lifecycle
365
- },
366
- performance: {
367
- asyncSaves: true,
368
- batchSize: 10,
369
- compressionLevel: 2,
370
- cacheEnabled: true,
371
- parallelOperations: true,
372
- ...config?.performance
373
- }
374
- };
375
- }
376
- /**
377
- * Setup lifecycle hooks
378
- */
379
- setupLifecycleHooks(_options) {
380
- const hooks = {
381
- preIteration: async (context) => {
382
- logger.debug("Pre-iteration hook", {
383
- iteration: context.task.currentIteration
384
- });
385
- return context;
386
- },
387
- postIteration: async (iteration) => {
388
- await this.saveIterationFrame(iteration);
389
- },
390
- onStateChange: async (oldState, newState) => {
391
- await this.updateStateFrame(oldState, newState);
392
- },
393
- onError: async (error, context) => {
394
- logger.error("Iteration error", { error: error.message, context });
395
- await this.saveErrorFrame(error, context);
396
- },
397
- onComplete: async (state) => {
398
- await this.closeRootFrame(state);
399
- }
400
- };
401
- const lifecycle = new IterationLifecycle(this.config.lifecycle, hooks);
402
- this.state.lifecycle = lifecycle;
403
- }
404
- /**
405
- * Get lifecycle instance
406
- */
407
- getLifecycle() {
408
- return this.state.lifecycle;
409
- }
410
- /**
411
- * Initialize Ralph directory structure
412
- */
413
- async initializeRalphDirectory(state) {
414
- await fs.mkdir(this.ralphDir, { recursive: true });
415
- await fs.mkdir(path.join(this.ralphDir, "history"), { recursive: true });
416
- await fs.writeFile(path.join(this.ralphDir, "task.md"), state.task);
417
- await fs.writeFile(
418
- path.join(this.ralphDir, "completion-criteria.md"),
419
- state.criteria
420
- );
421
- await fs.writeFile(path.join(this.ralphDir, "iteration.txt"), "0");
422
- await fs.writeFile(path.join(this.ralphDir, "feedback.txt"), "");
423
- await fs.writeFile(
424
- path.join(this.ralphDir, "state.json"),
425
- JSON.stringify(state, null, 2)
426
- );
427
- }
428
- /**
429
- * Create root frame for Ralph loop
430
- */
431
- async createRootFrame(state) {
432
- if (!this.requiresDatabase) {
433
- return {
434
- frame_id: `mock-${state.loopId}`,
435
- type: "task",
436
- name: `ralph-${state.loopId}`,
437
- inputs: {
438
- task: state.task,
439
- criteria: state.criteria,
440
- loopId: state.loopId
441
- },
442
- created_at: Date.now()
443
- };
444
- }
445
- if (!this.frameManager) {
446
- throw new Error("Frame manager not initialized");
447
- }
448
- const frame = {
449
- type: "task",
450
- name: `ralph-${state.loopId}`,
451
- inputs: {
452
- task: state.task,
453
- criteria: state.criteria,
454
- loopId: state.loopId
455
- },
456
- digest_json: {
457
- type: "ralph_loop",
458
- status: "started"
459
- }
460
- };
461
- return await this.frameManager.createFrame({
462
- name: frame.name,
463
- type: frame.type,
464
- content: frame.content || "",
465
- metadata: frame.metadata
466
- });
467
- }
468
- /**
469
- * Load iteration context from StackMemory
470
- */
471
- async loadIterationContext(state) {
472
- const frames = await this.loadRelevantFrames(state.loopId);
473
- return {
474
- task: {
475
- description: state.task,
476
- criteria: state.criteria.split("\n").filter(Boolean),
477
- currentIteration: state.iteration,
478
- feedback: state.feedback,
479
- priority: "medium"
480
- },
481
- history: {
482
- recentIterations: await this.loadRecentIterations(state.loopId),
483
- gitCommits: await this.loadGitCommits(),
484
- changedFiles: await this.loadChangedFiles(),
485
- testResults: []
486
- },
487
- environment: {
488
- projectPath: process.cwd(),
489
- branch: await this.getCurrentBranch(),
490
- dependencies: {},
491
- configuration: {}
492
- },
493
- memory: {
494
- relevantFrames: frames,
495
- decisions: [],
496
- patterns: [],
497
- blockers: []
498
- },
499
- tokenCount: 0
500
- };
501
- }
502
- /**
503
- * Execute worker iteration
504
- */
505
- async executeWorkerIteration(context) {
506
- const iterationNumber = context.task.currentIteration + 1;
507
- try {
508
- const analysis = await this.analyzeCodebaseState();
509
- const plan = await this.generateIterationPlan(context, analysis);
510
- const changes = await this.executeIterationChanges(plan, context);
511
- const validation = await this.validateIterationResults(changes);
512
- logger.info("Iteration execution completed", {
513
- iteration: iterationNumber,
514
- changesCount: changes.length,
515
- testsPass: validation.testsPass
516
- });
517
- return {
518
- number: iterationNumber,
519
- timestamp: Date.now(),
520
- analysis,
521
- plan,
522
- changes,
523
- validation
524
- };
525
- } catch (error) {
526
- logger.error("Iteration execution failed", error);
527
- return {
528
- number: iterationNumber,
529
- timestamp: Date.now(),
530
- analysis: {
531
- filesCount: 0,
532
- testsPass: false,
533
- testsFail: 1,
534
- lastChange: `Error: ${error.message}`
535
- },
536
- plan: {
537
- summary: "Iteration failed due to error",
538
- steps: ["Investigate error", "Fix underlying issue"],
539
- priority: "high"
540
- },
541
- changes: [],
542
- validation: {
543
- testsPass: false,
544
- lintClean: false,
545
- buildSuccess: false,
546
- errors: [error.message],
547
- warnings: []
548
- }
549
- };
550
- }
551
- }
552
- /**
553
- * Analyze current codebase state
554
- */
555
- async analyzeCodebaseState() {
556
- try {
557
- const stats = {
558
- filesCount: 0,
559
- testsPass: true,
560
- testsFail: 0,
561
- lastChange: "No recent changes"
562
- };
563
- try {
564
- const { execSync: execSync2 } = await import("child_process");
565
- const output = execSync2(
566
- 'find . -type f -name "*.ts" -o -name "*.js" -o -name "*.json" | grep -v node_modules | grep -v .git | wc -l',
567
- { encoding: "utf8", cwd: process.cwd() }
568
- );
569
- stats.filesCount = parseInt(output.trim()) || 0;
570
- } catch {
571
- stats.filesCount = 0;
572
- }
573
- try {
574
- const { execSync: execSync2 } = await import("child_process");
575
- const gitLog = execSync2("git log -1 --oneline", {
576
- encoding: "utf8",
577
- cwd: process.cwd()
578
- });
579
- stats.lastChange = gitLog.trim() || "No git history";
580
- } catch {
581
- stats.lastChange = "No git repository";
582
- }
583
- try {
584
- const { existsSync } = await import("fs");
585
- if (existsSync("package.json")) {
586
- const packageJson = JSON.parse(
587
- await fs.readFile("package.json", "utf8")
588
- );
589
- if (packageJson.scripts?.test) {
590
- const { execSync: execSync2 } = await import("child_process");
591
- execSync2("npm test", { stdio: "pipe", timeout: 3e4 });
592
- stats.testsPass = true;
593
- stats.testsFail = 0;
594
- }
595
- }
596
- } catch {
597
- stats.testsPass = false;
598
- stats.testsFail = 1;
599
- }
600
- return stats;
601
- } catch (error) {
602
- return {
603
- filesCount: 0,
604
- testsPass: false,
605
- testsFail: 1,
606
- lastChange: `Analysis failed: ${error.message}`
607
- };
608
- }
609
- }
610
- /**
611
- * Generate iteration plan based on context and analysis
612
- */
613
- async generateIterationPlan(context, analysis) {
614
- const task = context.task.task || "Complete assigned work";
615
- const _criteria = context.task.criteria || "Meet completion criteria";
616
- const steps = [];
617
- if (!analysis.testsPass) {
618
- steps.push("Fix failing tests");
619
- }
620
- if (analysis.filesCount === 0) {
621
- steps.push("Initialize project structure");
622
- }
623
- if (task.toLowerCase().includes("implement")) {
624
- steps.push("Implement required functionality");
625
- steps.push("Add appropriate tests");
626
- } else if (task.toLowerCase().includes("fix")) {
627
- steps.push("Identify root cause");
628
- steps.push("Implement fix");
629
- steps.push("Verify fix works");
630
- } else {
631
- steps.push("Analyze requirements");
632
- steps.push("Plan implementation approach");
633
- steps.push("Execute planned work");
634
- }
635
- steps.push("Validate changes");
636
- return {
637
- summary: `Iteration plan for: ${task}`,
638
- steps,
639
- priority: analysis.testsPass ? "medium" : "high"
640
- };
641
- }
642
- /**
643
- * Execute planned changes
644
- */
645
- async executeIterationChanges(plan, _context) {
646
- const changes = [];
647
- for (let i = 0; i < plan.steps.length; i++) {
648
- const step = plan.steps[i];
649
- changes.push({
650
- type: "step_execution",
651
- description: step,
652
- timestamp: Date.now(),
653
- files_affected: [],
654
- success: true
655
- });
656
- await new Promise((resolve) => setTimeout(resolve, 100));
657
- }
658
- logger.debug("Executed iteration changes", {
659
- stepsCount: plan.steps.length,
660
- changesCount: changes.length
661
- });
662
- return changes;
663
- }
664
- /**
665
- * Validate iteration results
666
- */
667
- async validateIterationResults(_changes) {
668
- const validation = {
669
- testsPass: true,
670
- lintClean: true,
671
- buildSuccess: true,
672
- errors: [],
673
- warnings: []
674
- };
675
- try {
676
- const { existsSync } = await import("fs");
677
- if (existsSync("package.json")) {
678
- const packageJson = JSON.parse(
679
- await fs.readFile("package.json", "utf8")
680
- );
681
- if (packageJson.scripts?.lint) {
682
- try {
683
- const { execSync: execSync2 } = await import("child_process");
684
- execSync2("npm run lint", { stdio: "pipe", timeout: 3e4 });
685
- validation.lintClean = true;
686
- } catch {
687
- validation.lintClean = false;
688
- validation.warnings.push("Lint warnings detected");
689
- }
690
- }
691
- if (packageJson.scripts?.build) {
692
- try {
693
- const { execSync: execSync2 } = await import("child_process");
694
- execSync2("npm run build", { stdio: "pipe", timeout: 6e4 });
695
- validation.buildSuccess = true;
696
- } catch {
697
- validation.buildSuccess = false;
698
- validation.errors.push("Build failed");
699
- }
700
- }
701
- }
702
- } catch (error) {
703
- validation.errors.push(`Validation error: ${error.message}`);
704
- }
705
- return validation;
706
- }
707
- /**
708
- * Save iteration results
709
- */
710
- async saveIterationResults(iteration) {
711
- await this.state.performanceOptimizer.saveIteration(iteration);
712
- const iterDir = path.join(
713
- this.ralphDir,
714
- "history",
715
- `iteration-${String(iteration.number).padStart(3, "0")}`
716
- );
717
- await fs.mkdir(iterDir, { recursive: true });
718
- await fs.writeFile(
719
- path.join(iterDir, "artifacts.json"),
720
- JSON.stringify(iteration, null, 2)
721
- );
722
- }
723
- /**
724
- * Save iteration frame to StackMemory
725
- */
726
- async saveIterationFrame(iteration) {
727
- if (!this.requiresDatabase || !this.frameManager || !this.state.activeLoop)
728
- return;
729
- const frame = {
730
- type: "subtask",
731
- name: `iteration-${iteration.number}`,
732
- inputs: {
733
- iterationNumber: iteration.number,
734
- loopId: this.state.activeLoop.loopId
735
- },
736
- outputs: {
737
- changes: iteration.changes.length,
738
- success: iteration.validation.testsPass
739
- },
740
- digest_json: iteration
741
- };
742
- await this.state.performanceOptimizer.saveFrame(frame);
743
- }
744
- /**
745
- * Get database adapter for FrameManager
746
- */
747
- async getDatabaseAdapter() {
748
- const dbPath = path.join(this.ralphDir, "stackmemory.db");
749
- const projectId = path.basename(this.ralphDir);
750
- const embeddingProvider = await createTransformersProvider() ?? void 0;
751
- return new SQLiteAdapter(projectId, { dbPath, embeddingProvider });
752
- }
753
- /**
754
- * Additional helper methods
755
- */
756
- async getCurrentGitCommit() {
757
- try {
758
- return execSync("git rev-parse HEAD", { encoding: "utf8" }).trim();
759
- } catch {
760
- return "";
761
- }
762
- }
763
- async getCurrentBranch() {
764
- try {
765
- return execSync("git branch --show-current", { encoding: "utf8" }).trim();
766
- } catch {
767
- return "main";
768
- }
769
- }
770
- async saveLoopState(state) {
771
- await fs.writeFile(
772
- path.join(this.ralphDir, "state.json"),
773
- JSON.stringify(state, null, 2)
774
- );
775
- await fs.writeFile(
776
- path.join(this.ralphDir, "iteration.txt"),
777
- state.iteration.toString()
778
- );
779
- logger.debug("Saved loop state", {
780
- iteration: state.iteration,
781
- status: state.status
782
- });
783
- }
784
- async gatherStateSources(loopId) {
785
- const sources = [];
786
- sources.push(await this.state.stateReconciler.getGitState());
787
- sources.push(await this.state.stateReconciler.getFileState());
788
- sources.push(await this.state.stateReconciler.getMemoryState(loopId));
789
- return sources;
790
- }
791
- async attemptRecovery() {
792
- logger.info("Attempting crash recovery");
793
- try {
794
- const stateFile = path.join(this.ralphDir, "state.json");
795
- const exists = await fs.stat(stateFile).then(() => true).catch(() => false);
796
- if (exists) {
797
- const stateData = await fs.readFile(stateFile, "utf8");
798
- const state = JSON.parse(stateData);
799
- if (state.status !== "completed") {
800
- logger.info("Found incomplete loop", { loopId: state.loopId });
801
- await this.resumeLoop(state.loopId);
802
- }
803
- }
804
- } catch (error) {
805
- logger.error("Recovery failed", { error: error.message });
806
- }
807
- }
808
- async evaluateCompletion() {
809
- return {
810
- complete: false,
811
- criteria: {},
812
- unmet: ["criteria1", "criteria2"]
813
- };
814
- }
815
- generateFeedback(evaluation) {
816
- if (evaluation.unmet.length === 0) {
817
- return "All criteria met";
818
- }
819
- return `Still need to address:
820
- ${evaluation.unmet.map((c) => `- ${c}`).join("\n")}`;
821
- }
822
- async loadRelevantFrames(_loopId) {
823
- return [];
824
- }
825
- async loadRecentIterations(_loopId) {
826
- return [];
827
- }
828
- async loadGitCommits() {
829
- return [];
830
- }
831
- async loadChangedFiles() {
832
- return [];
833
- }
834
- async loadSessionFrames(_sessionId) {
835
- return [];
836
- }
837
- async reconstructLoopState(frame) {
838
- return {
839
- loopId: frame.inputs.loopId || "",
840
- task: frame.inputs.task || "",
841
- criteria: frame.inputs.criteria || "",
842
- iteration: 0,
843
- status: "running",
844
- startTime: frame.created_at,
845
- lastUpdateTime: Date.now()
846
- };
847
- }
848
- async buildContextFromFrames(frames, state) {
849
- return await this.loadIterationContext(state);
850
- }
851
- async updateStateFrame(_oldState, _newState) {
852
- logger.debug("State frame updated");
853
- }
854
- async saveErrorFrame(_error, _context) {
855
- logger.debug("Error frame saved");
856
- }
857
- async closeRootFrame(_state) {
858
- logger.debug("Root frame closed");
859
- }
860
- }
861
- export {
862
- RalphStackMemoryBridge
863
- };