claude-flow 3.62.0 → 3.63.1
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/.claude/.proven-config-version +1 -0
- package/.claude/helpers/.helpers-version +1 -0
- package/.claude/helpers/helpers.manifest.json +13 -0
- package/.claude/proven-config.json +42 -0
- package/.claude/settings.local.json +8 -0
- package/.claude-plugin/marketplace.json +3 -3
- package/README.md +2 -2
- package/node_modules/@claude-flow/codex/dist/cli.js +0 -0
- package/node_modules/@claude-flow/plugin-agent-federation/dist/bin.js +0 -0
- package/node_modules/@claude-flow/security/dist/input-validator.d.ts +6 -6
- package/package.json +1 -1
- package/v3/@claude-flow/cli/README.md +2 -2
- package/v3/@claude-flow/cli/catalog-manifest.json +3 -3
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentbbs-tools.js +7 -2
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-agent-tools.d.ts +1 -20
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-agent-tools.js +347 -108
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-learning-tool.d.ts +4 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-learning-tool.js +46 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/memory-tools.js +6 -3
- package/v3/@claude-flow/cli/dist/src/missions/schemas.d.ts +12 -12
- package/v3/@claude-flow/cli/dist/src/ruvector/diskann-backend.d.ts +78 -0
- package/v3/@claude-flow/cli/dist/src/ruvector/diskann-backend.js +310 -0
- package/v3/@claude-flow/cli/dist/src/services/flywheel-transaction.js +5 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-loop.d.ts +79 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-loop.js +76 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-store.d.ts +187 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-store.js +238 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning.d.ts +108 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning.js +197 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-client.d.ts +72 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-client.js +236 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-worker.d.ts +56 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-worker.js +161 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-collector.d.ts +93 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-collector.js +181 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-store.d.ts +52 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-store.js +119 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-workflows.d.ts +101 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-workflows.js +183 -0
- package/v3/@claude-flow/cli/package.json +1 -1
- package/v3/@claude-flow/shared/dist/events/event-store.test.d.ts +8 -0
- package/v3/@claude-flow/shared/dist/events/event-store.test.js +293 -0
- package/v3/@claude-flow/shared/dist/hooks/hooks.test.d.ts +9 -0
- package/v3/@claude-flow/shared/dist/hooks/hooks.test.js +322 -0
- package/v3/@claude-flow/shared/dist/hooks/verify-exports.test.d.ts +9 -0
- package/v3/@claude-flow/shared/dist/hooks/verify-exports.test.js +93 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/** Provider event semantics for ADR-115 / dynamic workflows. No model calls. */
|
|
2
|
+
export interface ManagedEvent {
|
|
3
|
+
id: string;
|
|
4
|
+
type: string;
|
|
5
|
+
processed_at?: string | null;
|
|
6
|
+
content?: Array<{
|
|
7
|
+
type: string;
|
|
8
|
+
text?: string;
|
|
9
|
+
}>;
|
|
10
|
+
name?: string;
|
|
11
|
+
input?: unknown;
|
|
12
|
+
stop_reason?: {
|
|
13
|
+
type: string;
|
|
14
|
+
};
|
|
15
|
+
workflow_run_id?: string | null;
|
|
16
|
+
session_thread_id?: string;
|
|
17
|
+
result?: {
|
|
18
|
+
type: string;
|
|
19
|
+
} | string;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
export interface EventPage {
|
|
23
|
+
data: ManagedEvent[];
|
|
24
|
+
next_page?: string | null;
|
|
25
|
+
}
|
|
26
|
+
/** Validate untrusted pages before deriving lifecycle claims. */
|
|
27
|
+
export declare function eventPage(value: unknown): EventPage;
|
|
28
|
+
/** Reads complete history, bounded and deduplicated. Cursor is opaque, never a URL. */
|
|
29
|
+
export declare function readManagedEvents(read: (query: string) => Promise<unknown>, since?: string): Promise<ManagedEvent[]>;
|
|
30
|
+
/** Incremental reducer. Provider completion is always unverified. */
|
|
31
|
+
export declare class ManagedProgress {
|
|
32
|
+
private seen;
|
|
33
|
+
private text;
|
|
34
|
+
private tools;
|
|
35
|
+
private runs;
|
|
36
|
+
private failures;
|
|
37
|
+
private status;
|
|
38
|
+
private stopReason;
|
|
39
|
+
private cost;
|
|
40
|
+
private since;
|
|
41
|
+
private bytes;
|
|
42
|
+
private finished;
|
|
43
|
+
private interrupted;
|
|
44
|
+
accept(events: ManagedEvent[]): void;
|
|
45
|
+
watermark(): string | undefined;
|
|
46
|
+
snapshot(): {
|
|
47
|
+
assistantText: string;
|
|
48
|
+
toolUses: {
|
|
49
|
+
name: string;
|
|
50
|
+
input: unknown;
|
|
51
|
+
}[];
|
|
52
|
+
status: string;
|
|
53
|
+
stopReason: string | null;
|
|
54
|
+
eventCount: number;
|
|
55
|
+
workflowRuns: {
|
|
56
|
+
id: string;
|
|
57
|
+
status: string;
|
|
58
|
+
result?: string;
|
|
59
|
+
}[];
|
|
60
|
+
openRuns: number;
|
|
61
|
+
finished: boolean;
|
|
62
|
+
interrupted: boolean;
|
|
63
|
+
blocked: boolean;
|
|
64
|
+
observedFailures: number;
|
|
65
|
+
listCost: unknown;
|
|
66
|
+
verification: "unverified";
|
|
67
|
+
needsThreadInspection: boolean;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** Build only the supported configuration; do not pass arbitrary multiagent JSON. */
|
|
71
|
+
export declare function dynamicConfig(input: Record<string, unknown>): {
|
|
72
|
+
enabled: boolean;
|
|
73
|
+
budget: {
|
|
74
|
+
type: string;
|
|
75
|
+
max_list_cost: {
|
|
76
|
+
amount: string;
|
|
77
|
+
currency: string;
|
|
78
|
+
};
|
|
79
|
+
} | undefined;
|
|
80
|
+
multiagent: {
|
|
81
|
+
type: string;
|
|
82
|
+
subagents: {
|
|
83
|
+
type: string;
|
|
84
|
+
};
|
|
85
|
+
workflows: {
|
|
86
|
+
inline_agents?: {
|
|
87
|
+
type: string;
|
|
88
|
+
} | undefined;
|
|
89
|
+
predefined_agents?: any[] | undefined;
|
|
90
|
+
type: string;
|
|
91
|
+
};
|
|
92
|
+
} | undefined;
|
|
93
|
+
networking: {
|
|
94
|
+
type: string;
|
|
95
|
+
allowed_hosts: any[];
|
|
96
|
+
} | {
|
|
97
|
+
type: {};
|
|
98
|
+
allowed_hosts?: undefined;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=managed-workflows.d.ts.map
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/** Validate untrusted pages before deriving lifecycle claims. */
|
|
2
|
+
export function eventPage(value) {
|
|
3
|
+
if (!value || typeof value !== 'object')
|
|
4
|
+
throw new Error('Invalid Managed Agents event page');
|
|
5
|
+
const page = value;
|
|
6
|
+
if (!Array.isArray(page.data) || page.data.some(e => !e || typeof e.id !== 'string' || !e.id || typeof e.type !== 'string')) {
|
|
7
|
+
throw new Error('Invalid Managed Agents events');
|
|
8
|
+
}
|
|
9
|
+
for (const e of page.data) {
|
|
10
|
+
if (e.processed_at != null && (typeof e.processed_at !== 'string' || !Number.isFinite(Date.parse(e.processed_at))))
|
|
11
|
+
throw new Error('Invalid event timestamp');
|
|
12
|
+
if (e.type === 'agent.message' && e.content !== undefined && (!Array.isArray(e.content) || e.content.some(b => !b || typeof b.type !== 'string' || (b.text !== undefined && typeof b.text !== 'string'))))
|
|
13
|
+
throw new Error('Invalid event content');
|
|
14
|
+
if (e.stop_reason != null && typeof e.stop_reason.type !== 'string')
|
|
15
|
+
throw new Error('Invalid event stop reason');
|
|
16
|
+
if (e.type === 'workflow_run.status_ended' && (!e.result || typeof e.result !== 'object' || typeof e.result.type !== 'string'))
|
|
17
|
+
throw new Error('Invalid workflow result');
|
|
18
|
+
if (e.type.startsWith('workflow_run.') && e.type !== 'workflow_run.error' && (typeof e.workflow_run_id !== 'string' || !e.workflow_run_id))
|
|
19
|
+
throw new Error('Invalid workflow ID');
|
|
20
|
+
if (e.workflow_run_id != null && typeof e.workflow_run_id !== 'string')
|
|
21
|
+
throw new Error('Invalid workflow ID');
|
|
22
|
+
}
|
|
23
|
+
if (page.next_page != null && (typeof page.next_page !== 'string' || !page.next_page || page.next_page.length > 4096)) {
|
|
24
|
+
throw new Error('Invalid Managed Agents cursor');
|
|
25
|
+
}
|
|
26
|
+
return page;
|
|
27
|
+
}
|
|
28
|
+
/** Reads complete history, bounded and deduplicated. Cursor is opaque, never a URL. */
|
|
29
|
+
export async function readManagedEvents(read, since) {
|
|
30
|
+
const events = new Map();
|
|
31
|
+
const cursors = new Set();
|
|
32
|
+
let bytes = 0;
|
|
33
|
+
let cursor;
|
|
34
|
+
for (let count = 0; count < 100; count++) {
|
|
35
|
+
const query = new URLSearchParams({ order: 'asc', limit: '100' });
|
|
36
|
+
if (cursor)
|
|
37
|
+
query.set('page', cursor);
|
|
38
|
+
if (since)
|
|
39
|
+
query.set('created_at[gte]', since);
|
|
40
|
+
const page = eventPage(await read(query.toString()));
|
|
41
|
+
bytes += Buffer.byteLength(JSON.stringify(page), 'utf8');
|
|
42
|
+
if (bytes > 16_000_000)
|
|
43
|
+
throw new Error('Managed Agents transcript limit exceeded');
|
|
44
|
+
for (const e of page.data)
|
|
45
|
+
events.set(e.id, e);
|
|
46
|
+
if (events.size > 10_000)
|
|
47
|
+
throw new Error('Managed Agents event limit exceeded');
|
|
48
|
+
if (!page.next_page)
|
|
49
|
+
return [...events.values()];
|
|
50
|
+
if (cursors.has(page.next_page))
|
|
51
|
+
throw new Error('Repeated Managed Agents cursor');
|
|
52
|
+
cursors.add(page.next_page);
|
|
53
|
+
cursor = page.next_page;
|
|
54
|
+
}
|
|
55
|
+
throw new Error('Managed Agents page limit exceeded');
|
|
56
|
+
}
|
|
57
|
+
/** Incremental reducer. Provider completion is always unverified. */
|
|
58
|
+
export class ManagedProgress {
|
|
59
|
+
seen = new Set();
|
|
60
|
+
text = [];
|
|
61
|
+
tools = [];
|
|
62
|
+
runs = new Map();
|
|
63
|
+
failures = 0;
|
|
64
|
+
status = 'unknown';
|
|
65
|
+
stopReason = null;
|
|
66
|
+
cost = null;
|
|
67
|
+
since;
|
|
68
|
+
bytes = 0;
|
|
69
|
+
finished = false;
|
|
70
|
+
interrupted = false;
|
|
71
|
+
accept(events) {
|
|
72
|
+
for (const e of events) {
|
|
73
|
+
// Queued user messages may be returned again after processing.
|
|
74
|
+
if (!e.processed_at)
|
|
75
|
+
continue;
|
|
76
|
+
if (this.seen.has(e.id))
|
|
77
|
+
continue;
|
|
78
|
+
if (this.seen.size >= 10_000)
|
|
79
|
+
throw new Error('Managed Agents event limit exceeded');
|
|
80
|
+
this.bytes += Buffer.byteLength(JSON.stringify(e), 'utf8');
|
|
81
|
+
if (this.bytes > 16_000_000)
|
|
82
|
+
throw new Error('Managed Agents transcript limit exceeded');
|
|
83
|
+
this.seen.add(e.id);
|
|
84
|
+
if (!this.since || e.processed_at > this.since)
|
|
85
|
+
this.since = e.processed_at;
|
|
86
|
+
if (e.type === 'user.message') {
|
|
87
|
+
this.finished = false;
|
|
88
|
+
this.interrupted = false;
|
|
89
|
+
}
|
|
90
|
+
else if (e.type === 'user.interrupt') {
|
|
91
|
+
this.finished = false;
|
|
92
|
+
this.interrupted = true;
|
|
93
|
+
}
|
|
94
|
+
else if (e.type === 'agent.message' && Array.isArray(e.content)) {
|
|
95
|
+
this.text.push(e.content.filter(b => b?.type === 'text' && typeof b.text === 'string').map(b => b.text).join(''));
|
|
96
|
+
}
|
|
97
|
+
else if (e.type === 'agent.tool_use' || e.type === 'agent.mcp_tool_use') {
|
|
98
|
+
this.tools.push({ name: e.name ?? '?', input: e.input });
|
|
99
|
+
}
|
|
100
|
+
else if (e.type === 'session.status_running') {
|
|
101
|
+
this.status = 'running';
|
|
102
|
+
this.stopReason = null;
|
|
103
|
+
this.finished = false;
|
|
104
|
+
}
|
|
105
|
+
else if (e.type === 'session.status_idle') {
|
|
106
|
+
this.status = 'idle';
|
|
107
|
+
this.stopReason = e.stop_reason?.type ?? null;
|
|
108
|
+
this.finished = this.stopReason === 'end_turn' && !this.interrupted && [...this.runs.values()].every(r => r.status === 'ended');
|
|
109
|
+
}
|
|
110
|
+
else if (e.type === 'session.status_terminated') {
|
|
111
|
+
this.status = 'terminated';
|
|
112
|
+
this.finished = false;
|
|
113
|
+
}
|
|
114
|
+
else if (e.type === 'session.error' || e.type === 'session.status_error' || e.type === 'session.status_failed') {
|
|
115
|
+
this.failures++;
|
|
116
|
+
}
|
|
117
|
+
else if (e.type === 'session.usage') {
|
|
118
|
+
this.cost = e.usage && typeof e.usage === 'object' ? e.usage.list_cost ?? null : null;
|
|
119
|
+
}
|
|
120
|
+
if (e.type === 'workflow_run.error')
|
|
121
|
+
this.failures++;
|
|
122
|
+
if (e.workflow_run_id && e.type.startsWith('workflow_run.')) {
|
|
123
|
+
this.finished = false;
|
|
124
|
+
const run = this.runs.get(e.workflow_run_id) ?? { id: e.workflow_run_id, status: 'running' };
|
|
125
|
+
if (e.type === 'workflow_run.status_ended') {
|
|
126
|
+
run.status = 'ended';
|
|
127
|
+
run.result = typeof e.result === 'object' ? e.result.type : 'unknown';
|
|
128
|
+
if (run.result !== 'completed')
|
|
129
|
+
this.failures++;
|
|
130
|
+
}
|
|
131
|
+
else if (e.type === 'workflow_run.status_idle' && run.status !== 'ended')
|
|
132
|
+
run.status = 'idle';
|
|
133
|
+
else if (e.type === 'workflow_run.status_running' && run.status !== 'ended')
|
|
134
|
+
run.status = 'running';
|
|
135
|
+
this.runs.set(run.id, run);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
watermark() { return this.since; }
|
|
140
|
+
snapshot() {
|
|
141
|
+
const workflowRuns = [...this.runs.values()].map(r => ({ ...r }));
|
|
142
|
+
const openRuns = workflowRuns.filter(r => r.status !== 'ended').length;
|
|
143
|
+
const blocked = this.status === 'idle' && ['budget_reached', 'requires_action'].includes(this.stopReason ?? '');
|
|
144
|
+
return {
|
|
145
|
+
assistantText: this.text.join(''), toolUses: [...this.tools], status: this.status,
|
|
146
|
+
stopReason: this.stopReason, eventCount: this.seen.size, workflowRuns, openRuns,
|
|
147
|
+
finished: this.finished, interrupted: this.interrupted, blocked, observedFailures: this.failures, listCost: this.cost,
|
|
148
|
+
verification: 'unverified',
|
|
149
|
+
needsThreadInspection: workflowRuns.length > 0,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Build only the supported configuration; do not pass arbitrary multiagent JSON. */
|
|
154
|
+
export function dynamicConfig(input) {
|
|
155
|
+
if (input.dynamicWorkflows !== undefined && typeof input.dynamicWorkflows !== 'boolean')
|
|
156
|
+
throw new Error('dynamicWorkflows must be boolean');
|
|
157
|
+
const enabled = input.dynamicWorkflows === true;
|
|
158
|
+
if (input.budgetCents !== undefined || enabled) {
|
|
159
|
+
if (typeof input.budgetCents !== 'string' || !/^[1-9][0-9]{0,8}$/.test(input.budgetCents)) {
|
|
160
|
+
throw new Error('budgetCents must be a positive integer cents string (maximum 999999999)');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (enabled && input.networking !== undefined && input.networking !== 'limited')
|
|
164
|
+
throw new Error('Dynamic workflows require limited networking');
|
|
165
|
+
const hosts = input.allowedHosts ?? [];
|
|
166
|
+
if (!Array.isArray(hosts) || hosts.length > 100 || hosts.some(h => typeof h !== 'string' || h.length > 253 || !/^[a-z0-9]+(?:[.-][a-z0-9]+)*$/i.test(h))) {
|
|
167
|
+
throw new Error('allowedHosts must contain at most 100 exact hostnames');
|
|
168
|
+
}
|
|
169
|
+
const agents = input.predefinedAgents;
|
|
170
|
+
if (agents !== undefined && (!Array.isArray(agents) || agents.length < 1 || agents.length > 20 || agents.some(a => typeof a !== 'string' || !/^agent_[A-Za-z0-9]+$/.test(a)))) {
|
|
171
|
+
throw new Error('predefinedAgents must contain 1 to 20 agent IDs');
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
enabled,
|
|
175
|
+
budget: input.budgetCents === undefined ? undefined : { type: 'limit', max_list_cost: { amount: input.budgetCents, currency: 'USD' } },
|
|
176
|
+
multiagent: enabled ? {
|
|
177
|
+
type: 'multiagent_20261001', subagents: { type: 'disabled' },
|
|
178
|
+
workflows: { type: 'enabled', ...(agents ? { inline_agents: { type: 'disabled' }, predefined_agents: agents } : {}) },
|
|
179
|
+
} : undefined,
|
|
180
|
+
networking: enabled || input.networking === 'limited' ? { type: 'limited', allowed_hosts: hosts } : { type: input.networking ?? 'unrestricted' },
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=managed-workflows.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@claude-flow/cli",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.63.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Ruflo CLI - Enterprise AI agent orchestration with 60+ specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event Store Tests
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive tests for the event sourcing implementation.
|
|
5
|
+
* Tests event storage, retrieval, filtering, snapshots, and projections.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
8
|
+
import { EventStore } from './event-store.js';
|
|
9
|
+
import { AgentStateProjection, TaskHistoryProjection, MemoryIndexProjection } from './projections.js';
|
|
10
|
+
import { createAgentSpawnedEvent, createAgentStartedEvent, createAgentTaskAssignedEvent, createAgentTaskCompletedEvent, createTaskCreatedEvent, createTaskStartedEvent, createTaskCompletedEvent, createMemoryStoredEvent, createMemoryRetrievedEvent, createMemoryDeletedEvent, } from './domain-events.js';
|
|
11
|
+
describe('EventStore', () => {
|
|
12
|
+
let eventStore;
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
eventStore = new EventStore({ databasePath: ':memory:', verbose: false });
|
|
15
|
+
await eventStore.initialize();
|
|
16
|
+
});
|
|
17
|
+
afterEach(async () => {
|
|
18
|
+
await eventStore.shutdown();
|
|
19
|
+
});
|
|
20
|
+
describe('Event Appending', () => {
|
|
21
|
+
it('should append events to the store', async () => {
|
|
22
|
+
const event = createAgentSpawnedEvent('agent-1', 'coder', 'core', ['coding', 'testing']);
|
|
23
|
+
await eventStore.append(event);
|
|
24
|
+
const events = await eventStore.getEvents('agent-1');
|
|
25
|
+
expect(events).toHaveLength(1);
|
|
26
|
+
expect(events[0].type).toBe('agent:spawned');
|
|
27
|
+
expect(events[0].version).toBe(1);
|
|
28
|
+
});
|
|
29
|
+
it('should increment version for each event', async () => {
|
|
30
|
+
const event1 = createAgentSpawnedEvent('agent-1', 'coder', 'core', []);
|
|
31
|
+
const event2 = createAgentStartedEvent('agent-1');
|
|
32
|
+
await eventStore.append(event1);
|
|
33
|
+
await eventStore.append(event2);
|
|
34
|
+
const events = await eventStore.getEvents('agent-1');
|
|
35
|
+
expect(events[0].version).toBe(1);
|
|
36
|
+
expect(events[1].version).toBe(2);
|
|
37
|
+
});
|
|
38
|
+
it('should maintain version per aggregate', async () => {
|
|
39
|
+
const agent1Event = createAgentSpawnedEvent('agent-1', 'coder', 'core', []);
|
|
40
|
+
const agent2Event = createAgentSpawnedEvent('agent-2', 'tester', 'quality', []);
|
|
41
|
+
await eventStore.append(agent1Event);
|
|
42
|
+
await eventStore.append(agent2Event);
|
|
43
|
+
const agent1Events = await eventStore.getEvents('agent-1');
|
|
44
|
+
const agent2Events = await eventStore.getEvents('agent-2');
|
|
45
|
+
expect(agent1Events[0].version).toBe(1);
|
|
46
|
+
expect(agent2Events[0].version).toBe(1);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe('Event Retrieval', () => {
|
|
50
|
+
beforeEach(async () => {
|
|
51
|
+
// Setup: Create multiple events
|
|
52
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
53
|
+
await eventStore.append(createAgentStartedEvent('agent-1'));
|
|
54
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Build feature', 'Description', 'high', []));
|
|
55
|
+
await eventStore.append(createTaskStartedEvent('task-1', 'agent-1'));
|
|
56
|
+
});
|
|
57
|
+
it('should retrieve events by aggregate ID', async () => {
|
|
58
|
+
const events = await eventStore.getEvents('agent-1');
|
|
59
|
+
expect(events).toHaveLength(2);
|
|
60
|
+
expect(events[0].type).toBe('agent:spawned');
|
|
61
|
+
expect(events[1].type).toBe('agent:started');
|
|
62
|
+
});
|
|
63
|
+
it('should retrieve events from specific version', async () => {
|
|
64
|
+
const events = await eventStore.getEvents('agent-1', 2);
|
|
65
|
+
expect(events).toHaveLength(1);
|
|
66
|
+
expect(events[0].type).toBe('agent:started');
|
|
67
|
+
});
|
|
68
|
+
it('should retrieve events by type', async () => {
|
|
69
|
+
const events = await eventStore.getEventsByType('task:created');
|
|
70
|
+
expect(events).toHaveLength(1);
|
|
71
|
+
expect(events[0].aggregateId).toBe('task-1');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe('Event Filtering', () => {
|
|
75
|
+
beforeEach(async () => {
|
|
76
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
77
|
+
await eventStore.append(createAgentSpawnedEvent('agent-2', 'tester', 'quality', []));
|
|
78
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task 1', 'Desc', 'high', []));
|
|
79
|
+
await eventStore.append(createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024));
|
|
80
|
+
});
|
|
81
|
+
it('should filter by aggregate types', async () => {
|
|
82
|
+
const events = await eventStore.query({ aggregateTypes: ['agent'] });
|
|
83
|
+
expect(events).toHaveLength(2);
|
|
84
|
+
expect(events.every((e) => e.aggregateType === 'agent')).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
it('should filter by event types', async () => {
|
|
87
|
+
const events = await eventStore.query({ eventTypes: ['agent:spawned'] });
|
|
88
|
+
expect(events).toHaveLength(2);
|
|
89
|
+
expect(events.every((e) => e.type === 'agent:spawned')).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
it('should filter by aggregate IDs', async () => {
|
|
92
|
+
const events = await eventStore.query({ aggregateIds: ['agent-1', 'task-1'] });
|
|
93
|
+
expect(events).toHaveLength(2);
|
|
94
|
+
});
|
|
95
|
+
it('should support pagination', async () => {
|
|
96
|
+
const page1 = await eventStore.query({ limit: 2, offset: 0 });
|
|
97
|
+
const page2 = await eventStore.query({ limit: 2, offset: 2 });
|
|
98
|
+
expect(page1).toHaveLength(2);
|
|
99
|
+
expect(page2).toHaveLength(2);
|
|
100
|
+
expect(page1[0].id).not.toBe(page2[0].id);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('Event Replay', () => {
|
|
104
|
+
it('should replay all events', async () => {
|
|
105
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
106
|
+
await eventStore.append(createAgentStartedEvent('agent-1'));
|
|
107
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task', 'Desc', 'high', []));
|
|
108
|
+
const events = [];
|
|
109
|
+
for await (const event of eventStore.replay()) {
|
|
110
|
+
events.push(event);
|
|
111
|
+
}
|
|
112
|
+
expect(events).toHaveLength(3);
|
|
113
|
+
});
|
|
114
|
+
it('should replay from specific version', async () => {
|
|
115
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
116
|
+
await eventStore.append(createAgentStartedEvent('agent-1'));
|
|
117
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task', 'Desc', 'high', []));
|
|
118
|
+
const events = [];
|
|
119
|
+
for await (const event of eventStore.replay(2)) {
|
|
120
|
+
events.push(event);
|
|
121
|
+
}
|
|
122
|
+
expect(events.length).toBeGreaterThanOrEqual(2);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
describe('Snapshots', () => {
|
|
126
|
+
it('should save snapshots', async () => {
|
|
127
|
+
const snapshot = {
|
|
128
|
+
aggregateId: 'agent-1',
|
|
129
|
+
aggregateType: 'agent',
|
|
130
|
+
version: 5,
|
|
131
|
+
state: { status: 'active', tasks: ['task-1'] },
|
|
132
|
+
timestamp: Date.now(),
|
|
133
|
+
};
|
|
134
|
+
await eventStore.saveSnapshot(snapshot);
|
|
135
|
+
const retrieved = await eventStore.getSnapshot('agent-1');
|
|
136
|
+
expect(retrieved).not.toBeNull();
|
|
137
|
+
expect(retrieved?.version).toBe(5);
|
|
138
|
+
expect(retrieved?.state).toEqual({ status: 'active', tasks: ['task-1'] });
|
|
139
|
+
});
|
|
140
|
+
it('should return latest snapshot', async () => {
|
|
141
|
+
const snapshot1 = {
|
|
142
|
+
aggregateId: 'agent-1',
|
|
143
|
+
aggregateType: 'agent',
|
|
144
|
+
version: 5,
|
|
145
|
+
state: { status: 'active' },
|
|
146
|
+
timestamp: Date.now(),
|
|
147
|
+
};
|
|
148
|
+
const snapshot2 = {
|
|
149
|
+
aggregateId: 'agent-1',
|
|
150
|
+
aggregateType: 'agent',
|
|
151
|
+
version: 10,
|
|
152
|
+
state: { status: 'completed' },
|
|
153
|
+
timestamp: Date.now(),
|
|
154
|
+
};
|
|
155
|
+
await eventStore.saveSnapshot(snapshot1);
|
|
156
|
+
await eventStore.saveSnapshot(snapshot2);
|
|
157
|
+
const retrieved = await eventStore.getSnapshot('agent-1');
|
|
158
|
+
expect(retrieved?.version).toBe(10);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
describe('Statistics', () => {
|
|
162
|
+
it('should provide event store statistics', async () => {
|
|
163
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
164
|
+
await eventStore.append(createAgentSpawnedEvent('agent-2', 'tester', 'quality', []));
|
|
165
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task', 'Desc', 'high', []));
|
|
166
|
+
const stats = await eventStore.getStats();
|
|
167
|
+
expect(stats.totalEvents).toBe(3);
|
|
168
|
+
expect(stats.eventsByType['agent:spawned']).toBe(2);
|
|
169
|
+
expect(stats.eventsByType['task:created']).toBe(1);
|
|
170
|
+
expect(stats.eventsByAggregate['agent-1']).toBe(1);
|
|
171
|
+
expect(stats.eventsByAggregate['agent-2']).toBe(1);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
describe('AgentStateProjection', () => {
|
|
176
|
+
let eventStore;
|
|
177
|
+
let projection;
|
|
178
|
+
beforeEach(async () => {
|
|
179
|
+
eventStore = new EventStore({ databasePath: ':memory:' });
|
|
180
|
+
await eventStore.initialize();
|
|
181
|
+
projection = new AgentStateProjection(eventStore);
|
|
182
|
+
});
|
|
183
|
+
afterEach(async () => {
|
|
184
|
+
await eventStore.shutdown();
|
|
185
|
+
});
|
|
186
|
+
it('should build agent state from events', async () => {
|
|
187
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', ['coding']));
|
|
188
|
+
await eventStore.append(createAgentStartedEvent('agent-1'));
|
|
189
|
+
await projection.initialize();
|
|
190
|
+
const agent = projection.getAgent('agent-1');
|
|
191
|
+
expect(agent).not.toBeNull();
|
|
192
|
+
expect(agent?.role).toBe('coder');
|
|
193
|
+
expect(agent?.status).toBe('active');
|
|
194
|
+
});
|
|
195
|
+
it('should track task assignments', async () => {
|
|
196
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
197
|
+
await eventStore.append(createAgentTaskAssignedEvent('agent-1', 'task-1', Date.now()));
|
|
198
|
+
await projection.initialize();
|
|
199
|
+
const agent = projection.getAgent('agent-1');
|
|
200
|
+
expect(agent?.currentTask).toBe('task-1');
|
|
201
|
+
});
|
|
202
|
+
it('should track completed tasks', async () => {
|
|
203
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
204
|
+
await eventStore.append(createAgentTaskCompletedEvent('agent-1', 'task-1', { success: true }, Date.now(), 5000));
|
|
205
|
+
await projection.initialize();
|
|
206
|
+
const agent = projection.getAgent('agent-1');
|
|
207
|
+
expect(agent?.completedTasks).toContain('task-1');
|
|
208
|
+
expect(agent?.taskCount).toBe(1);
|
|
209
|
+
});
|
|
210
|
+
it('should filter agents by status', async () => {
|
|
211
|
+
await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
|
|
212
|
+
await eventStore.append(createAgentSpawnedEvent('agent-2', 'tester', 'quality', []));
|
|
213
|
+
await eventStore.append(createAgentStartedEvent('agent-1'));
|
|
214
|
+
await projection.initialize();
|
|
215
|
+
const activeAgents = projection.getAgentsByStatus('active');
|
|
216
|
+
const idleAgents = projection.getAgentsByStatus('idle');
|
|
217
|
+
expect(activeAgents).toHaveLength(1);
|
|
218
|
+
expect(idleAgents).toHaveLength(1);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
describe('TaskHistoryProjection', () => {
|
|
222
|
+
let eventStore;
|
|
223
|
+
let projection;
|
|
224
|
+
beforeEach(async () => {
|
|
225
|
+
eventStore = new EventStore({ databasePath: ':memory:' });
|
|
226
|
+
await eventStore.initialize();
|
|
227
|
+
projection = new TaskHistoryProjection(eventStore);
|
|
228
|
+
});
|
|
229
|
+
afterEach(async () => {
|
|
230
|
+
await eventStore.shutdown();
|
|
231
|
+
});
|
|
232
|
+
it('should build task history from events', async () => {
|
|
233
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Build Feature', 'Description', 'high', []));
|
|
234
|
+
await eventStore.append(createTaskStartedEvent('task-1', 'agent-1'));
|
|
235
|
+
await eventStore.append(createTaskCompletedEvent('task-1', { success: true }, 5000));
|
|
236
|
+
await projection.initialize();
|
|
237
|
+
const task = projection.getTask('task-1');
|
|
238
|
+
expect(task).not.toBeNull();
|
|
239
|
+
expect(task?.title).toBe('Build Feature');
|
|
240
|
+
expect(task?.status).toBe('completed');
|
|
241
|
+
expect(task?.assignedAgent).toBe('agent-1');
|
|
242
|
+
});
|
|
243
|
+
it('should calculate average task duration', async () => {
|
|
244
|
+
await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task 1', 'Desc', 'high', []));
|
|
245
|
+
await eventStore.append(createTaskCompletedEvent('task-1', { success: true }, 5000));
|
|
246
|
+
await eventStore.append(createTaskCreatedEvent('task-2', 'implementation', 'Task 2', 'Desc', 'high', []));
|
|
247
|
+
await eventStore.append(createTaskCompletedEvent('task-2', { success: true }, 3000));
|
|
248
|
+
await projection.initialize();
|
|
249
|
+
const avgDuration = projection.getAverageTaskDuration();
|
|
250
|
+
expect(avgDuration).toBe(4000);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
describe('MemoryIndexProjection', () => {
|
|
254
|
+
let eventStore;
|
|
255
|
+
let projection;
|
|
256
|
+
beforeEach(async () => {
|
|
257
|
+
eventStore = new EventStore({ databasePath: ':memory:' });
|
|
258
|
+
await eventStore.initialize();
|
|
259
|
+
projection = new MemoryIndexProjection(eventStore);
|
|
260
|
+
});
|
|
261
|
+
afterEach(async () => {
|
|
262
|
+
await eventStore.shutdown();
|
|
263
|
+
});
|
|
264
|
+
it('should build memory index from events', async () => {
|
|
265
|
+
await eventStore.append(createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024));
|
|
266
|
+
await eventStore.append(createMemoryRetrievedEvent('mem-1', 'default', 'key1', 1));
|
|
267
|
+
await projection.initialize();
|
|
268
|
+
const memory = projection.getMemory('mem-1');
|
|
269
|
+
expect(memory).not.toBeNull();
|
|
270
|
+
expect(memory?.namespace).toBe('default');
|
|
271
|
+
expect(memory?.accessCount).toBe(1);
|
|
272
|
+
});
|
|
273
|
+
it('should track memory deletions', async () => {
|
|
274
|
+
await eventStore.append(createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024));
|
|
275
|
+
await eventStore.append(createMemoryDeletedEvent('mem-1', 'default', 'key1'));
|
|
276
|
+
await projection.initialize();
|
|
277
|
+
const memory = projection.getMemory('mem-1');
|
|
278
|
+
expect(memory?.isDeleted).toBe(true);
|
|
279
|
+
const activeMemories = projection.getActiveMemories();
|
|
280
|
+
expect(activeMemories).toHaveLength(0);
|
|
281
|
+
});
|
|
282
|
+
it('should calculate total size by namespace', async () => {
|
|
283
|
+
await eventStore.append(createMemoryStoredEvent('mem-1', 'ns1', 'key1', 'semantic', 1024));
|
|
284
|
+
await eventStore.append(createMemoryStoredEvent('mem-2', 'ns1', 'key2', 'semantic', 2048));
|
|
285
|
+
await eventStore.append(createMemoryStoredEvent('mem-3', 'ns2', 'key3', 'semantic', 512));
|
|
286
|
+
await projection.initialize();
|
|
287
|
+
const ns1Size = projection.getTotalSizeByNamespace('ns1');
|
|
288
|
+
const ns2Size = projection.getTotalSizeByNamespace('ns2');
|
|
289
|
+
expect(ns1Size).toBe(3072);
|
|
290
|
+
expect(ns2Size).toBe(512);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
//# sourceMappingURL=event-store.test.js.map
|