@sparkleideas/shared 3.0.0-alpha.7
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.
- package/README.md +323 -0
- package/__tests__/hooks/bash-safety.test.ts +289 -0
- package/__tests__/hooks/file-organization.test.ts +335 -0
- package/__tests__/hooks/git-commit.test.ts +336 -0
- package/__tests__/hooks/index.ts +23 -0
- package/__tests__/hooks/session-hooks.test.ts +357 -0
- package/__tests__/hooks/task-hooks.test.ts +193 -0
- package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
- package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
- package/docs/EVENTS_README.md +352 -0
- package/package.json +39 -0
- package/src/core/config/defaults.ts +207 -0
- package/src/core/config/index.ts +15 -0
- package/src/core/config/loader.ts +271 -0
- package/src/core/config/schema.ts +188 -0
- package/src/core/config/validator.ts +209 -0
- package/src/core/event-bus.ts +236 -0
- package/src/core/index.ts +22 -0
- package/src/core/interfaces/agent.interface.ts +251 -0
- package/src/core/interfaces/coordinator.interface.ts +363 -0
- package/src/core/interfaces/event.interface.ts +267 -0
- package/src/core/interfaces/index.ts +19 -0
- package/src/core/interfaces/memory.interface.ts +332 -0
- package/src/core/interfaces/task.interface.ts +223 -0
- package/src/core/orchestrator/event-coordinator.ts +122 -0
- package/src/core/orchestrator/health-monitor.ts +214 -0
- package/src/core/orchestrator/index.ts +89 -0
- package/src/core/orchestrator/lifecycle-manager.ts +263 -0
- package/src/core/orchestrator/session-manager.ts +279 -0
- package/src/core/orchestrator/task-manager.ts +317 -0
- package/src/events/domain-events.ts +584 -0
- package/src/events/event-store.test.ts +387 -0
- package/src/events/event-store.ts +588 -0
- package/src/events/example-usage.ts +293 -0
- package/src/events/index.ts +90 -0
- package/src/events/projections.ts +561 -0
- package/src/events/state-reconstructor.ts +349 -0
- package/src/events.ts +367 -0
- package/src/hooks/INTEGRATION.md +658 -0
- package/src/hooks/README.md +532 -0
- package/src/hooks/example-usage.ts +499 -0
- package/src/hooks/executor.ts +379 -0
- package/src/hooks/hooks.test.ts +421 -0
- package/src/hooks/index.ts +131 -0
- package/src/hooks/registry.ts +333 -0
- package/src/hooks/safety/bash-safety.ts +604 -0
- package/src/hooks/safety/file-organization.ts +473 -0
- package/src/hooks/safety/git-commit.ts +623 -0
- package/src/hooks/safety/index.ts +46 -0
- package/src/hooks/session-hooks.ts +559 -0
- package/src/hooks/task-hooks.ts +513 -0
- package/src/hooks/types.ts +357 -0
- package/src/hooks/verify-exports.test.ts +125 -0
- package/src/index.ts +195 -0
- package/src/mcp/connection-pool.ts +438 -0
- package/src/mcp/index.ts +183 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/session-manager.ts +428 -0
- package/src/mcp/tool-registry.ts +566 -0
- package/src/mcp/transport/http.ts +557 -0
- package/src/mcp/transport/index.ts +294 -0
- package/src/mcp/transport/stdio.ts +324 -0
- package/src/mcp/transport/websocket.ts +484 -0
- package/src/mcp/types.ts +565 -0
- package/src/plugin-interface.ts +663 -0
- package/src/plugin-loader.ts +638 -0
- package/src/plugin-registry.ts +604 -0
- package/src/plugins/index.ts +34 -0
- package/src/plugins/official/hive-mind-plugin.ts +330 -0
- package/src/plugins/official/index.ts +24 -0
- package/src/plugins/official/maestro-plugin.ts +508 -0
- package/src/plugins/types.ts +108 -0
- package/src/resilience/bulkhead.ts +277 -0
- package/src/resilience/circuit-breaker.ts +326 -0
- package/src/resilience/index.ts +26 -0
- package/src/resilience/rate-limiter.ts +420 -0
- package/src/resilience/retry.ts +224 -0
- package/src/security/index.ts +39 -0
- package/src/security/input-validation.ts +265 -0
- package/src/security/secure-random.ts +159 -0
- package/src/services/index.ts +16 -0
- package/src/services/v3-progress.service.ts +505 -0
- package/src/types/agent.types.ts +144 -0
- package/src/types/index.ts +22 -0
- package/src/types/mcp.types.ts +300 -0
- package/src/types/memory.types.ts +263 -0
- package/src/types/swarm.types.ts +255 -0
- package/src/types/task.types.ts +205 -0
- package/src/types.ts +367 -0
- package/src/utils/secure-logger.d.ts +69 -0
- package/src/utils/secure-logger.d.ts.map +1 -0
- package/src/utils/secure-logger.js +208 -0
- package/src/utils/secure-logger.js.map +1 -0
- package/src/utils/secure-logger.ts +257 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
# Event Store Quick Reference
|
|
2
|
+
|
|
3
|
+
Fast reference guide for using the Event Store system.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
cd v3/@claude-flow/shared
|
|
9
|
+
npm install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import {
|
|
16
|
+
EventStore,
|
|
17
|
+
AgentStateProjection,
|
|
18
|
+
createAgentSpawnedEvent,
|
|
19
|
+
createTaskCompletedEvent,
|
|
20
|
+
} from '@claude-flow/shared/events';
|
|
21
|
+
|
|
22
|
+
// 1. Initialize
|
|
23
|
+
const store = new EventStore({ databasePath: './events.db' });
|
|
24
|
+
await store.initialize();
|
|
25
|
+
|
|
26
|
+
// 2. Record events
|
|
27
|
+
await store.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
28
|
+
|
|
29
|
+
// 3. Query events
|
|
30
|
+
const events = await store.getEvents('agent-1');
|
|
31
|
+
|
|
32
|
+
// 4. Build projections
|
|
33
|
+
const projection = new AgentStateProjection(store);
|
|
34
|
+
await projection.initialize();
|
|
35
|
+
|
|
36
|
+
// 5. Cleanup
|
|
37
|
+
await store.shutdown();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Common Operations
|
|
41
|
+
|
|
42
|
+
### Record Agent Events
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// Agent spawned
|
|
46
|
+
await store.append(
|
|
47
|
+
createAgentSpawnedEvent('agent-1', 'coder', 'core', ['coding'])
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
// Agent started
|
|
51
|
+
await store.append(createAgentStartedEvent('agent-1'));
|
|
52
|
+
|
|
53
|
+
// Agent stopped
|
|
54
|
+
await store.append(createAgentStoppedEvent('agent-1', 'completed'));
|
|
55
|
+
|
|
56
|
+
// Agent failed
|
|
57
|
+
await store.append(createAgentFailedEvent('agent-1', new Error('Timeout')));
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Record Task Events
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// Task created
|
|
64
|
+
await store.append(
|
|
65
|
+
createTaskCreatedEvent('task-1', 'implementation', 'Build Feature', 'Description', 'high', [])
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
// Task started
|
|
69
|
+
await store.append(createTaskStartedEvent('task-1', 'agent-1'));
|
|
70
|
+
|
|
71
|
+
// Task completed
|
|
72
|
+
await store.append(createTaskCompletedEvent('task-1', { success: true }, 5000));
|
|
73
|
+
|
|
74
|
+
// Task failed
|
|
75
|
+
await store.append(createTaskFailedEvent('task-1', new Error('Build failed'), 1));
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Record Memory Events
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Memory stored
|
|
82
|
+
await store.append(
|
|
83
|
+
createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024)
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Memory retrieved
|
|
87
|
+
await store.append(createMemoryRetrievedEvent('mem-1', 'default', 'key1', 1));
|
|
88
|
+
|
|
89
|
+
// Memory deleted
|
|
90
|
+
await store.append(createMemoryDeletedEvent('mem-1', 'default', 'key1'));
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Record Swarm Events
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Swarm initialized
|
|
97
|
+
await store.append(
|
|
98
|
+
createSwarmInitializedEvent('hierarchical-mesh', 15, { config: 'value' })
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// Swarm scaled
|
|
102
|
+
await store.append(createSwarmScaledEvent(10, 15, 'Increased capacity'));
|
|
103
|
+
|
|
104
|
+
// Swarm terminated
|
|
105
|
+
await store.append(createSwarmTerminatedEvent('Shutdown', { reason: 'Complete' }));
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Query Events
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// By aggregate ID
|
|
112
|
+
const agentEvents = await store.getEvents('agent-1');
|
|
113
|
+
|
|
114
|
+
// By event type
|
|
115
|
+
const spawned = await store.getEventsByType('agent:spawned');
|
|
116
|
+
|
|
117
|
+
// By aggregate type
|
|
118
|
+
const allAgentEvents = await store.query({ aggregateTypes: ['agent'] });
|
|
119
|
+
|
|
120
|
+
// By multiple filters
|
|
121
|
+
const recentTasks = await store.query({
|
|
122
|
+
aggregateTypes: ['task'],
|
|
123
|
+
eventTypes: ['task:completed', 'task:failed'],
|
|
124
|
+
afterTimestamp: Date.now() - 86400000, // Last 24 hours
|
|
125
|
+
limit: 10,
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Projections
|
|
130
|
+
|
|
131
|
+
### Agent State Projection
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const agentProj = new AgentStateProjection(store);
|
|
135
|
+
await agentProj.initialize();
|
|
136
|
+
|
|
137
|
+
// Get single agent
|
|
138
|
+
const agent = agentProj.getAgent('agent-1');
|
|
139
|
+
|
|
140
|
+
// Get all agents
|
|
141
|
+
const allAgents = agentProj.getAllAgents();
|
|
142
|
+
|
|
143
|
+
// Filter by status
|
|
144
|
+
const activeAgents = agentProj.getAgentsByStatus('active');
|
|
145
|
+
|
|
146
|
+
// Filter by domain
|
|
147
|
+
const coreAgents = agentProj.getAgentsByDomain('core');
|
|
148
|
+
|
|
149
|
+
// Get count
|
|
150
|
+
const activeCount = agentProj.getActiveAgentCount();
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Task History Projection
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const taskProj = new TaskHistoryProjection(store);
|
|
157
|
+
await taskProj.initialize();
|
|
158
|
+
|
|
159
|
+
// Get single task
|
|
160
|
+
const task = taskProj.getTask('task-1');
|
|
161
|
+
|
|
162
|
+
// Get all tasks
|
|
163
|
+
const allTasks = taskProj.getAllTasks();
|
|
164
|
+
|
|
165
|
+
// Filter by status
|
|
166
|
+
const completed = taskProj.getTasksByStatus('completed');
|
|
167
|
+
|
|
168
|
+
// Filter by agent
|
|
169
|
+
const agentTasks = taskProj.getTasksByAgent('agent-1');
|
|
170
|
+
|
|
171
|
+
// Get metrics
|
|
172
|
+
const avgDuration = taskProj.getAverageTaskDuration();
|
|
173
|
+
const completedCount = taskProj.getCompletedTaskCount();
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Memory Index Projection
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
const memProj = new MemoryIndexProjection(store);
|
|
180
|
+
await memProj.initialize();
|
|
181
|
+
|
|
182
|
+
// Get single memory
|
|
183
|
+
const memory = memProj.getMemory('mem-1');
|
|
184
|
+
|
|
185
|
+
// Get active memories
|
|
186
|
+
const active = memProj.getActiveMemories();
|
|
187
|
+
|
|
188
|
+
// Filter by namespace
|
|
189
|
+
const nsMemories = memProj.getMemoriesByNamespace('default');
|
|
190
|
+
|
|
191
|
+
// Get most accessed
|
|
192
|
+
const hot = memProj.getMostAccessedMemories(10);
|
|
193
|
+
|
|
194
|
+
// Get total size
|
|
195
|
+
const size = memProj.getTotalSizeByNamespace('default');
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Event Replay
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
// Replay all events
|
|
202
|
+
for await (const event of store.replay()) {
|
|
203
|
+
console.log(event.type, event.timestamp);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Replay from version
|
|
207
|
+
for await (const event of store.replay(100)) {
|
|
208
|
+
// Process events starting from version 100
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Snapshots
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// Save snapshot
|
|
216
|
+
await store.saveSnapshot({
|
|
217
|
+
aggregateId: 'agent-1',
|
|
218
|
+
aggregateType: 'agent',
|
|
219
|
+
version: 500,
|
|
220
|
+
state: { status: 'active', tasks: [] },
|
|
221
|
+
timestamp: Date.now(),
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Load snapshot
|
|
225
|
+
const snapshot = await store.getSnapshot('agent-1');
|
|
226
|
+
if (snapshot) {
|
|
227
|
+
// Resume from snapshot
|
|
228
|
+
const recentEvents = await store.getEvents('agent-1', snapshot.version);
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Statistics
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
const stats = await store.getStats();
|
|
236
|
+
|
|
237
|
+
console.log('Total events:', stats.totalEvents);
|
|
238
|
+
console.log('Events by type:', stats.eventsByType);
|
|
239
|
+
console.log('Events by aggregate:', stats.eventsByAggregate);
|
|
240
|
+
console.log('Time range:', stats.oldestEvent, '-', stats.newestEvent);
|
|
241
|
+
console.log('Snapshots:', stats.snapshotCount);
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Configuration
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
const store = new EventStore({
|
|
248
|
+
// Database path
|
|
249
|
+
databasePath: './events.db', // or ':memory:' for in-memory
|
|
250
|
+
|
|
251
|
+
// Logging
|
|
252
|
+
verbose: true,
|
|
253
|
+
|
|
254
|
+
// Auto-persist (milliseconds, 0 = disabled)
|
|
255
|
+
autoPersistInterval: 5000,
|
|
256
|
+
|
|
257
|
+
// Snapshot recommendation threshold
|
|
258
|
+
snapshotThreshold: 100,
|
|
259
|
+
|
|
260
|
+
// Custom WASM path (optional)
|
|
261
|
+
wasmPath: './sql-wasm.wasm',
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Event Types Reference
|
|
266
|
+
|
|
267
|
+
| Category | Event Type | Factory Function |
|
|
268
|
+
|----------|-----------|------------------|
|
|
269
|
+
| **Agent** | `agent:spawned` | `createAgentSpawnedEvent()` |
|
|
270
|
+
| | `agent:started` | `createAgentStartedEvent()` |
|
|
271
|
+
| | `agent:stopped` | `createAgentStoppedEvent()` |
|
|
272
|
+
| | `agent:failed` | `createAgentFailedEvent()` |
|
|
273
|
+
| **Task** | `task:created` | `createTaskCreatedEvent()` |
|
|
274
|
+
| | `task:started` | `createTaskStartedEvent()` |
|
|
275
|
+
| | `task:completed` | `createTaskCompletedEvent()` |
|
|
276
|
+
| | `task:failed` | `createTaskFailedEvent()` |
|
|
277
|
+
| **Memory** | `memory:stored` | `createMemoryStoredEvent()` |
|
|
278
|
+
| | `memory:retrieved` | `createMemoryRetrievedEvent()` |
|
|
279
|
+
| | `memory:deleted` | `createMemoryDeletedEvent()` |
|
|
280
|
+
| **Swarm** | `swarm:initialized` | `createSwarmInitializedEvent()` |
|
|
281
|
+
| | `swarm:scaled` | `createSwarmScaledEvent()` |
|
|
282
|
+
| | `swarm:terminated` | `createSwarmTerminatedEvent()` |
|
|
283
|
+
|
|
284
|
+
## Projection Properties
|
|
285
|
+
|
|
286
|
+
### AgentProjectionState
|
|
287
|
+
```typescript
|
|
288
|
+
{
|
|
289
|
+
id: AgentId;
|
|
290
|
+
role: string;
|
|
291
|
+
domain: string;
|
|
292
|
+
status: AgentStatus;
|
|
293
|
+
currentTask: TaskId | null;
|
|
294
|
+
completedTasks: TaskId[];
|
|
295
|
+
failedTasks: TaskId[];
|
|
296
|
+
totalTaskDuration: number;
|
|
297
|
+
taskCount: number;
|
|
298
|
+
errorCount: number;
|
|
299
|
+
spawnedAt: number;
|
|
300
|
+
startedAt: number | null;
|
|
301
|
+
stoppedAt: number | null;
|
|
302
|
+
lastActivityAt: number;
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### TaskProjectionState
|
|
307
|
+
```typescript
|
|
308
|
+
{
|
|
309
|
+
id: TaskId;
|
|
310
|
+
type: string;
|
|
311
|
+
title: string;
|
|
312
|
+
status: TaskStatus;
|
|
313
|
+
priority: string;
|
|
314
|
+
assignedAgent: AgentId | null;
|
|
315
|
+
dependencies: TaskId[];
|
|
316
|
+
blockedBy: TaskId[];
|
|
317
|
+
createdAt: number;
|
|
318
|
+
startedAt: number | null;
|
|
319
|
+
completedAt: number | null;
|
|
320
|
+
failedAt: number | null;
|
|
321
|
+
duration: number | null;
|
|
322
|
+
result: unknown;
|
|
323
|
+
error: string | null;
|
|
324
|
+
retryCount: number;
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### MemoryProjectionState
|
|
329
|
+
```typescript
|
|
330
|
+
{
|
|
331
|
+
id: string;
|
|
332
|
+
namespace: string;
|
|
333
|
+
key: string;
|
|
334
|
+
type: string;
|
|
335
|
+
size: number;
|
|
336
|
+
accessCount: number;
|
|
337
|
+
storedAt: number;
|
|
338
|
+
lastAccessedAt: number;
|
|
339
|
+
deletedAt: number | null;
|
|
340
|
+
isDeleted: boolean;
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Error Handling
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
try {
|
|
348
|
+
await store.append(event);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
console.error('Failed to append event:', error);
|
|
351
|
+
// Handle error
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Listen for errors
|
|
355
|
+
store.on('error', (error) => {
|
|
356
|
+
console.error('Event store error:', error);
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Event Listeners
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
// Event appended
|
|
364
|
+
store.on('event:appended', (event) => {
|
|
365
|
+
console.log('Event recorded:', event.type);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// Snapshot recommended
|
|
369
|
+
store.on('snapshot:recommended', ({ aggregateId, version }) => {
|
|
370
|
+
console.log(`Consider snapshot for ${aggregateId} at v${version}`);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// Persisted to disk
|
|
374
|
+
store.on('persisted', ({ size, path }) => {
|
|
375
|
+
console.log(`Persisted ${size} bytes to ${path}`);
|
|
376
|
+
});
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## Testing
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
383
|
+
|
|
384
|
+
describe('My Event Store Tests', () => {
|
|
385
|
+
let store: EventStore;
|
|
386
|
+
|
|
387
|
+
beforeEach(async () => {
|
|
388
|
+
store = new EventStore({ databasePath: ':memory:' });
|
|
389
|
+
await store.initialize();
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
afterEach(async () => {
|
|
393
|
+
await store.shutdown();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it('should record events', async () => {
|
|
397
|
+
await store.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
398
|
+
const events = await store.getEvents('agent-1');
|
|
399
|
+
expect(events).toHaveLength(1);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## Performance Tips
|
|
405
|
+
|
|
406
|
+
1. **Use Snapshots**: Save snapshots every 100-500 events for large aggregates
|
|
407
|
+
2. **Filter Early**: Use specific filters to reduce result sets
|
|
408
|
+
3. **Pagination**: Use `limit` and `offset` for large queries
|
|
409
|
+
4. **Indexes**: All common queries are indexed automatically
|
|
410
|
+
5. **Batch Writes**: Group related events together when possible
|
|
411
|
+
6. **In-Memory Mode**: Use `:memory:` for testing (faster)
|
|
412
|
+
7. **Auto-Persist**: Adjust interval based on write frequency
|
|
413
|
+
|
|
414
|
+
## Common Patterns
|
|
415
|
+
|
|
416
|
+
### Agent Lifecycle Tracking
|
|
417
|
+
```typescript
|
|
418
|
+
// Spawn → Start → Assign → Complete → Stop
|
|
419
|
+
await store.append(createAgentSpawnedEvent(id, role, domain, caps));
|
|
420
|
+
await store.append(createAgentStartedEvent(id));
|
|
421
|
+
await store.append(createAgentTaskAssignedEvent(id, taskId, Date.now()));
|
|
422
|
+
await store.append(createAgentTaskCompletedEvent(id, taskId, result, now, duration));
|
|
423
|
+
await store.append(createAgentStoppedEvent(id, 'completed'));
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
### Task Execution Flow
|
|
427
|
+
```typescript
|
|
428
|
+
// Create → Start → Complete
|
|
429
|
+
await store.append(createTaskCreatedEvent(id, type, title, desc, priority, deps));
|
|
430
|
+
await store.append(createTaskStartedEvent(id, agentId));
|
|
431
|
+
await store.append(createTaskCompletedEvent(id, result, duration));
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### Memory Lifecycle
|
|
435
|
+
```typescript
|
|
436
|
+
// Store → Retrieve (multiple times) → Delete
|
|
437
|
+
await store.append(createMemoryStoredEvent(id, ns, key, type, size));
|
|
438
|
+
await store.append(createMemoryRetrievedEvent(id, ns, key, 1));
|
|
439
|
+
await store.append(createMemoryRetrievedEvent(id, ns, key, 2));
|
|
440
|
+
await store.append(createMemoryDeletedEvent(id, ns, key));
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
## Troubleshooting
|
|
444
|
+
|
|
445
|
+
**Problem**: "EventStore not initialized"
|
|
446
|
+
**Solution**: Call `await eventStore.initialize()` before use
|
|
447
|
+
|
|
448
|
+
**Problem**: Events not persisting
|
|
449
|
+
**Solution**: Check `databasePath` is not `:memory:` and call `persist()`
|
|
450
|
+
|
|
451
|
+
**Problem**: Slow queries
|
|
452
|
+
**Solution**: Use filters, pagination, and snapshots
|
|
453
|
+
|
|
454
|
+
**Problem**: Version conflicts
|
|
455
|
+
**Solution**: Events are append-only, versions auto-increment per aggregate
|
|
456
|
+
|
|
457
|
+
## Resources
|
|
458
|
+
|
|
459
|
+
- Full Documentation: `README.md`
|
|
460
|
+
- Implementation Details: `IMPLEMENTATION_SUMMARY.md`
|
|
461
|
+
- Working Example: `example-usage.ts`
|
|
462
|
+
- Test Suite: `event-store.test.ts`
|
|
463
|
+
|
|
464
|
+
---
|
|
465
|
+
|
|
466
|
+
**Quick Links**:
|
|
467
|
+
- [README](./README.md) - Complete documentation
|
|
468
|
+
- [Example](./example-usage.ts) - Working code example
|
|
469
|
+
- [Tests](./event-store.test.ts) - Test suite
|
|
470
|
+
- [Summary](./IMPLEMENTATION_SUMMARY.md) - Implementation overview
|