macro-agent 0.0.16 → 0.0.17
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/dist/agent/agent-manager.d.ts.map +1 -1
- package/dist/agent/agent-manager.js +23 -5
- package/dist/agent/agent-manager.js.map +1 -1
- package/dist/map/adapter/acp-over-map.d.ts +10 -0
- package/dist/map/adapter/acp-over-map.d.ts.map +1 -1
- package/dist/map/adapter/acp-over-map.js +157 -5
- package/dist/map/adapter/acp-over-map.js.map +1 -1
- package/dist/store/event-store.d.ts.map +1 -1
- package/dist/store/event-store.js +92 -53
- package/dist/store/event-store.js.map +1 -1
- package/dist/store/instance.d.ts +0 -2
- package/dist/store/instance.d.ts.map +1 -1
- package/dist/store/instance.js +1 -24
- package/dist/store/instance.js.map +1 -1
- package/package.json +3 -3
- package/references/acp-factory-ref/package-lock.json +2 -2
- package/references/acp-factory-ref/package.json +2 -2
- package/references/claude-code-acp/package-lock.json +2 -2
- package/references/claude-code-acp/package.json +1 -1
- package/references/claude-code-acp/src/acp-agent.ts +3 -6
- package/src/agent/__tests__/agent-manager.test.ts +4 -6
- package/src/agent/agent-manager.ts +24 -5
- package/src/map/adapter/__tests__/acp-over-map-history.test.ts +664 -0
- package/src/map/adapter/__tests__/acp-over-map-persistence.e2e.test.ts +440 -0
- package/src/map/adapter/acp-over-map.ts +189 -5
- package/src/store/__tests__/event-store.test.ts +4 -12
- package/src/store/__tests__/instance.test.ts +5 -7
- package/src/store/event-store.ts +115 -57
- package/src/store/instance.ts +1 -29
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP-over-MAP History Persistence Tests
|
|
3
|
+
*
|
|
4
|
+
* Verifies that prompts sent through the ACP-over-MAP handler
|
|
5
|
+
* correctly record conversation turns in the EventStore, and that
|
|
6
|
+
* _macro/getHistory returns them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
10
|
+
import { ACPOverMAPHandler } from "../acp-over-map.js";
|
|
11
|
+
import type { ACPEnvelope } from "../acp-over-map.js";
|
|
12
|
+
import { createEventStore, type EventStore } from "../../../store/event-store.js";
|
|
13
|
+
import type { AgentManager } from "../../../agent/agent-manager.js";
|
|
14
|
+
import type { TaskManager } from "../../../task/task-manager.js";
|
|
15
|
+
import type { Agent, Task } from "../../../store/types/index.js";
|
|
16
|
+
import type { AgentId } from "../../../store/types/index.js";
|
|
17
|
+
|
|
18
|
+
// ─────────────────────────────────────────────────────────────────
|
|
19
|
+
// Helpers
|
|
20
|
+
// ─────────────────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
function createMockAgent(overrides: Partial<Agent> = {}): Agent {
|
|
23
|
+
return {
|
|
24
|
+
id: "agent-1" as AgentId,
|
|
25
|
+
session_id: "session-1",
|
|
26
|
+
state: "running",
|
|
27
|
+
task: "Test task",
|
|
28
|
+
task_id: "task-1",
|
|
29
|
+
parent: null,
|
|
30
|
+
lineage: [],
|
|
31
|
+
config: {},
|
|
32
|
+
cwd: "/test/cwd",
|
|
33
|
+
created_at: Date.now(),
|
|
34
|
+
started_at: Date.now(),
|
|
35
|
+
...overrides,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function createMockTask(overrides: Partial<Task> = {}): Task {
|
|
40
|
+
return {
|
|
41
|
+
id: "task-1",
|
|
42
|
+
description: "Test task",
|
|
43
|
+
status: "in_progress",
|
|
44
|
+
created_by: "agent-1",
|
|
45
|
+
created_at: Date.now(),
|
|
46
|
+
...overrides,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function createMockAgentManager(
|
|
51
|
+
promptUpdates: unknown[] = [],
|
|
52
|
+
): AgentManager {
|
|
53
|
+
const mockAgent = createMockAgent();
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
spawn: vi.fn().mockResolvedValue({
|
|
57
|
+
id: "agent-new",
|
|
58
|
+
session_id: "session-new",
|
|
59
|
+
agent: createMockAgent({ id: "agent-new" as AgentId, session_id: "session-new" }),
|
|
60
|
+
session: {},
|
|
61
|
+
}),
|
|
62
|
+
get: vi.fn().mockReturnValue(mockAgent),
|
|
63
|
+
list: vi.fn().mockReturnValue([mockAgent]),
|
|
64
|
+
listHeadManagers: vi.fn().mockReturnValue([mockAgent]),
|
|
65
|
+
getChildren: vi.fn().mockReturnValue([]),
|
|
66
|
+
getHierarchy: vi.fn().mockReturnValue({
|
|
67
|
+
root: { agent: mockAgent, children: [] },
|
|
68
|
+
depth: 1,
|
|
69
|
+
totalAgents: 1,
|
|
70
|
+
}),
|
|
71
|
+
getOrCreateHeadManager: vi.fn().mockResolvedValue({
|
|
72
|
+
id: "agent-1",
|
|
73
|
+
session_id: "session-1",
|
|
74
|
+
agent: mockAgent,
|
|
75
|
+
session: {},
|
|
76
|
+
}),
|
|
77
|
+
hasActiveSession: vi.fn().mockReturnValue(true),
|
|
78
|
+
resume: vi.fn().mockResolvedValue({
|
|
79
|
+
id: "agent-1",
|
|
80
|
+
session_id: "session-1",
|
|
81
|
+
agent: mockAgent,
|
|
82
|
+
session: {},
|
|
83
|
+
}),
|
|
84
|
+
terminate: vi.fn().mockResolvedValue(undefined),
|
|
85
|
+
prompt: vi.fn().mockReturnValue({
|
|
86
|
+
[Symbol.asyncIterator]: async function* () {
|
|
87
|
+
for (const update of promptUpdates) {
|
|
88
|
+
yield update;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
getSession: vi.fn().mockReturnValue(null),
|
|
93
|
+
onLifecycleEvent: vi.fn().mockReturnValue(() => {}),
|
|
94
|
+
close: vi.fn().mockResolvedValue(undefined),
|
|
95
|
+
respondToPermission: vi.fn().mockReturnValue(true),
|
|
96
|
+
cancelPermission: vi.fn().mockReturnValue(true),
|
|
97
|
+
} as unknown as AgentManager;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function createMockTaskManager(): TaskManager {
|
|
101
|
+
return {
|
|
102
|
+
get: vi.fn().mockReturnValue(createMockTask()),
|
|
103
|
+
list: vi.fn().mockReturnValue([createMockTask()]),
|
|
104
|
+
create: vi.fn().mockReturnValue(createMockTask()),
|
|
105
|
+
} as unknown as TaskManager;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Build an ACP envelope for processRequest */
|
|
109
|
+
function envelope(
|
|
110
|
+
streamId: string,
|
|
111
|
+
method: string,
|
|
112
|
+
params?: unknown,
|
|
113
|
+
sessionId?: string,
|
|
114
|
+
): ACPEnvelope {
|
|
115
|
+
return {
|
|
116
|
+
acp: {
|
|
117
|
+
jsonrpc: "2.0",
|
|
118
|
+
id: `${streamId}-${method}-${Date.now()}`,
|
|
119
|
+
method,
|
|
120
|
+
params,
|
|
121
|
+
},
|
|
122
|
+
acpContext: {
|
|
123
|
+
streamId,
|
|
124
|
+
sessionId,
|
|
125
|
+
direction: "client-to-agent",
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ─────────────────────────────────────────────────────────────────
|
|
131
|
+
// Tests
|
|
132
|
+
// ─────────────────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
describe("ACP-over-MAP history persistence", () => {
|
|
135
|
+
let eventStore: EventStore;
|
|
136
|
+
let handler: ACPOverMAPHandler;
|
|
137
|
+
|
|
138
|
+
afterEach(async () => {
|
|
139
|
+
await eventStore.close();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
async function setup(promptUpdates: unknown[] = []) {
|
|
143
|
+
eventStore = await createEventStore({ inMemory: true });
|
|
144
|
+
const agentManager = createMockAgentManager(promptUpdates);
|
|
145
|
+
const taskManager = createMockTaskManager();
|
|
146
|
+
|
|
147
|
+
handler = new ACPOverMAPHandler({
|
|
148
|
+
agentManager,
|
|
149
|
+
eventStore,
|
|
150
|
+
taskManager,
|
|
151
|
+
defaultCwd: "/test/cwd",
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return { agentManager, taskManager };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Register an agent in the EventStore so loadSession can resolve it */
|
|
158
|
+
function registerAgent(agentId: string, sessionId: string): void {
|
|
159
|
+
eventStore.emit({
|
|
160
|
+
type: "spawn",
|
|
161
|
+
source: { agent_id: agentId },
|
|
162
|
+
payload: {
|
|
163
|
+
agent_id: agentId,
|
|
164
|
+
session_id: sessionId,
|
|
165
|
+
task: "Test task",
|
|
166
|
+
task_id: "task-1",
|
|
167
|
+
cwd: "/test/cwd",
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
// Mark as running
|
|
171
|
+
eventStore.emit({
|
|
172
|
+
type: "lifecycle",
|
|
173
|
+
source: { agent_id: agentId },
|
|
174
|
+
payload: {
|
|
175
|
+
agent_id: agentId,
|
|
176
|
+
action: "started",
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Initialize a stream and create a session, returning the sessionId */
|
|
182
|
+
async function initAndCreateSession(
|
|
183
|
+
streamId: string,
|
|
184
|
+
targetAgentId: AgentId = "agent-1" as AgentId,
|
|
185
|
+
): Promise<string> {
|
|
186
|
+
// Initialize
|
|
187
|
+
await handler.processRequest(
|
|
188
|
+
targetAgentId,
|
|
189
|
+
envelope(streamId, "initialize", {
|
|
190
|
+
protocolVersion: 1,
|
|
191
|
+
capabilities: {},
|
|
192
|
+
clientInfo: { name: "test", version: "1.0" },
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
// Create session
|
|
197
|
+
const sessionResult = await handler.processRequest(
|
|
198
|
+
targetAgentId,
|
|
199
|
+
envelope(streamId, "session/new", {
|
|
200
|
+
cwd: "/test",
|
|
201
|
+
mcpServers: [],
|
|
202
|
+
}),
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const sessionId = (sessionResult.acp.result as { sessionId?: string })?.sessionId;
|
|
206
|
+
if (!sessionId) throw new Error("session/new did not return sessionId");
|
|
207
|
+
|
|
208
|
+
// Register the agent in EventStore so loadSession can resolve it later
|
|
209
|
+
registerAgent(targetAgentId, sessionId);
|
|
210
|
+
|
|
211
|
+
return sessionId;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
it("should record user and assistant text turns after prompt via ACP-over-MAP", async () => {
|
|
215
|
+
await setup([
|
|
216
|
+
{
|
|
217
|
+
sessionUpdate: "agent_message_chunk",
|
|
218
|
+
content: { type: "text", text: "Hello " },
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
sessionUpdate: "agent_message_chunk",
|
|
222
|
+
content: { type: "text", text: "world!" },
|
|
223
|
+
},
|
|
224
|
+
]);
|
|
225
|
+
|
|
226
|
+
const streamId = "test-stream-1";
|
|
227
|
+
const agentId = "agent-1" as AgentId;
|
|
228
|
+
const sessionId = await initAndCreateSession(streamId, agentId);
|
|
229
|
+
|
|
230
|
+
// Send prompt
|
|
231
|
+
await handler.processRequest(
|
|
232
|
+
agentId,
|
|
233
|
+
envelope(streamId, "session/prompt", {
|
|
234
|
+
prompt: [{ type: "text", text: "Say hello" }],
|
|
235
|
+
}, sessionId),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
// Get history via extension
|
|
239
|
+
const historyResult = await handler.processRequest(
|
|
240
|
+
agentId,
|
|
241
|
+
envelope(streamId, "_macro/getHistory", { sessionId }),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const turns = (historyResult.acp.result as { turns: { role: string; content: unknown }[] }).turns;
|
|
245
|
+
|
|
246
|
+
expect(turns).toHaveLength(2);
|
|
247
|
+
|
|
248
|
+
// User turn
|
|
249
|
+
expect(turns[0].role).toBe("user");
|
|
250
|
+
expect(turns[0].content).toBe("Say hello");
|
|
251
|
+
|
|
252
|
+
// Assistant turn — accumulated text chunks
|
|
253
|
+
expect(turns[1].role).toBe("assistant");
|
|
254
|
+
const content = turns[1].content as { parts: { type: string; text?: string }[] };
|
|
255
|
+
expect(content.parts[0].type).toBe("text");
|
|
256
|
+
expect(content.parts[0].text).toBe("Hello world!");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("should record tool calls in assistant turns", async () => {
|
|
260
|
+
await setup([
|
|
261
|
+
{
|
|
262
|
+
sessionUpdate: "agent_message_chunk",
|
|
263
|
+
content: { type: "text", text: "Let me check." },
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
sessionUpdate: "tool_call",
|
|
267
|
+
toolCallId: "tc-1",
|
|
268
|
+
title: "Read file",
|
|
269
|
+
status: "completed",
|
|
270
|
+
rawInput: { path: "/test.txt" },
|
|
271
|
+
output: "file contents",
|
|
272
|
+
},
|
|
273
|
+
]);
|
|
274
|
+
|
|
275
|
+
const streamId = "test-stream-2";
|
|
276
|
+
const agentId = "agent-1" as AgentId;
|
|
277
|
+
const sessionId = await initAndCreateSession(streamId, agentId);
|
|
278
|
+
|
|
279
|
+
await handler.processRequest(
|
|
280
|
+
agentId,
|
|
281
|
+
envelope(streamId, "session/prompt", {
|
|
282
|
+
prompt: [{ type: "text", text: "Read test.txt" }],
|
|
283
|
+
}, sessionId),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const historyResult = await handler.processRequest(
|
|
287
|
+
agentId,
|
|
288
|
+
envelope(streamId, "_macro/getHistory", { sessionId }),
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const turns = (historyResult.acp.result as { turns: { role: string; content: unknown }[] }).turns;
|
|
292
|
+
|
|
293
|
+
expect(turns).toHaveLength(2);
|
|
294
|
+
|
|
295
|
+
const assistantContent = turns[1].content as {
|
|
296
|
+
parts: { type: string; text?: string; toolCallId?: string; title?: string; output?: unknown }[];
|
|
297
|
+
};
|
|
298
|
+
expect(assistantContent.parts).toHaveLength(2);
|
|
299
|
+
expect(assistantContent.parts[0]).toEqual({ type: "text", text: "Let me check." });
|
|
300
|
+
expect(assistantContent.parts[1]).toMatchObject({
|
|
301
|
+
type: "tool",
|
|
302
|
+
toolCallId: "tc-1",
|
|
303
|
+
title: "Read file",
|
|
304
|
+
status: "completed",
|
|
305
|
+
output: "file contents",
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("should accumulate history across multiple prompts", async () => {
|
|
310
|
+
const { agentManager } = await setup([
|
|
311
|
+
{
|
|
312
|
+
sessionUpdate: "agent_message_chunk",
|
|
313
|
+
content: { type: "text", text: "Hello" },
|
|
314
|
+
},
|
|
315
|
+
]);
|
|
316
|
+
|
|
317
|
+
const streamId = "test-stream-3";
|
|
318
|
+
const agentId = "agent-1" as AgentId;
|
|
319
|
+
const sessionId = await initAndCreateSession(streamId, agentId);
|
|
320
|
+
|
|
321
|
+
// First prompt
|
|
322
|
+
await handler.processRequest(
|
|
323
|
+
agentId,
|
|
324
|
+
envelope(streamId, "session/prompt", {
|
|
325
|
+
prompt: [{ type: "text", text: "Hi" }],
|
|
326
|
+
}, sessionId),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
// Update mock for second prompt
|
|
330
|
+
(agentManager.prompt as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
331
|
+
[Symbol.asyncIterator]: async function* () {
|
|
332
|
+
yield {
|
|
333
|
+
sessionUpdate: "agent_message_chunk",
|
|
334
|
+
content: { type: "text", text: "Goodbye" },
|
|
335
|
+
};
|
|
336
|
+
},
|
|
337
|
+
} as any);
|
|
338
|
+
|
|
339
|
+
// Second prompt
|
|
340
|
+
await handler.processRequest(
|
|
341
|
+
agentId,
|
|
342
|
+
envelope(streamId, "session/prompt", {
|
|
343
|
+
prompt: [{ type: "text", text: "Bye" }],
|
|
344
|
+
}, sessionId),
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
const historyResult = await handler.processRequest(
|
|
348
|
+
agentId,
|
|
349
|
+
envelope(streamId, "_macro/getHistory", { sessionId }),
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
const turns = (historyResult.acp.result as { turns: { role: string; content: unknown }[] }).turns;
|
|
353
|
+
|
|
354
|
+
// 2 prompts × 2 turns = 4 turns total
|
|
355
|
+
expect(turns).toHaveLength(4);
|
|
356
|
+
expect(turns[0].role).toBe("user");
|
|
357
|
+
expect(turns[0].content).toBe("Hi");
|
|
358
|
+
expect(turns[1].role).toBe("assistant");
|
|
359
|
+
expect(turns[2].role).toBe("user");
|
|
360
|
+
expect(turns[2].content).toBe("Bye");
|
|
361
|
+
expect(turns[3].role).toBe("assistant");
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("should return empty turns for session with no history", async () => {
|
|
365
|
+
await setup();
|
|
366
|
+
|
|
367
|
+
const streamId = "test-stream-4";
|
|
368
|
+
const agentId = "agent-1" as AgentId;
|
|
369
|
+
const sessionId = await initAndCreateSession(streamId, agentId);
|
|
370
|
+
|
|
371
|
+
const historyResult = await handler.processRequest(
|
|
372
|
+
agentId,
|
|
373
|
+
envelope(streamId, "_macro/getHistory", { sessionId }),
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
const turns = (historyResult.acp.result as { turns: unknown[] }).turns;
|
|
377
|
+
expect(turns).toEqual([]);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("should only record completed tool calls, not running ones", async () => {
|
|
381
|
+
await setup([
|
|
382
|
+
{
|
|
383
|
+
sessionUpdate: "tool_call",
|
|
384
|
+
toolCallId: "tc-running",
|
|
385
|
+
title: "Running tool",
|
|
386
|
+
status: "running",
|
|
387
|
+
rawInput: {},
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
sessionUpdate: "tool_call",
|
|
391
|
+
toolCallId: "tc-done",
|
|
392
|
+
title: "Done tool",
|
|
393
|
+
status: "completed",
|
|
394
|
+
rawInput: { x: 1 },
|
|
395
|
+
output: "result",
|
|
396
|
+
},
|
|
397
|
+
]);
|
|
398
|
+
|
|
399
|
+
const streamId = "test-stream-5";
|
|
400
|
+
const agentId = "agent-1" as AgentId;
|
|
401
|
+
const sessionId = await initAndCreateSession(streamId, agentId);
|
|
402
|
+
|
|
403
|
+
await handler.processRequest(
|
|
404
|
+
agentId,
|
|
405
|
+
envelope(streamId, "session/prompt", {
|
|
406
|
+
prompt: [{ type: "text", text: "Run tools" }],
|
|
407
|
+
}, sessionId),
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
const historyResult = await handler.processRequest(
|
|
411
|
+
agentId,
|
|
412
|
+
envelope(streamId, "_macro/getHistory", { sessionId }),
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
const turns = (historyResult.acp.result as { turns: { role: string; content: unknown }[] }).turns;
|
|
416
|
+
const assistantContent = turns[1].content as {
|
|
417
|
+
parts: { type: string; toolCallId?: string }[];
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const toolParts = assistantContent.parts.filter((p) => p.type === "tool");
|
|
421
|
+
expect(toolParts).toHaveLength(1);
|
|
422
|
+
expect(toolParts[0].toolCallId).toBe("tc-done");
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("should restore history after reconnection via _resolve_ pattern (full TUI flow)", async () => {
|
|
426
|
+
// This test simulates the full TUI reconnection scenario:
|
|
427
|
+
// 1. Stream 1: Create session, send prompt (records turns)
|
|
428
|
+
// 2. Stream 2: Reconnect with _resolve_, loadSession returns sessionId, getHistory returns turns
|
|
429
|
+
|
|
430
|
+
const { agentManager } = await setup([
|
|
431
|
+
{
|
|
432
|
+
sessionUpdate: "agent_message_chunk",
|
|
433
|
+
content: { type: "text", text: "Hello from agent!" },
|
|
434
|
+
},
|
|
435
|
+
]);
|
|
436
|
+
|
|
437
|
+
const agentId = "agent-1" as AgentId;
|
|
438
|
+
|
|
439
|
+
// ── Stream 1: Initial session ──
|
|
440
|
+
const stream1 = "stream-reconnect-1";
|
|
441
|
+
const sessionId = await initAndCreateSession(stream1, agentId);
|
|
442
|
+
|
|
443
|
+
// Send a prompt (records turns in EventStore)
|
|
444
|
+
await handler.processRequest(
|
|
445
|
+
agentId,
|
|
446
|
+
envelope(stream1, "session/prompt", {
|
|
447
|
+
prompt: [{ type: "text", text: "Hello agent" }],
|
|
448
|
+
}, sessionId),
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
// Verify turns exist
|
|
452
|
+
const check = await handler.processRequest(
|
|
453
|
+
agentId,
|
|
454
|
+
envelope(stream1, "_macro/getHistory", { sessionId }),
|
|
455
|
+
);
|
|
456
|
+
expect((check.acp.result as { turns: unknown[] }).turns).toHaveLength(2);
|
|
457
|
+
|
|
458
|
+
// ── Stream 2: Simulates TUI restart / reconnection ──
|
|
459
|
+
const stream2 = "stream-reconnect-2";
|
|
460
|
+
|
|
461
|
+
// Initialize the new stream
|
|
462
|
+
await handler.processRequest(
|
|
463
|
+
agentId,
|
|
464
|
+
envelope(stream2, "initialize", {
|
|
465
|
+
protocolVersion: 1,
|
|
466
|
+
capabilities: {},
|
|
467
|
+
clientInfo: { name: "test-reconnect", version: "1.0" },
|
|
468
|
+
}),
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
// loadSession with _resolve_ pattern (TUI sends agentId in _meta)
|
|
472
|
+
// The mock agent has session_id: "session-1", so loadSession should resolve it
|
|
473
|
+
const loadResult = await handler.processRequest(
|
|
474
|
+
agentId,
|
|
475
|
+
envelope(stream2, "session/load", {
|
|
476
|
+
sessionId: "_resolve_",
|
|
477
|
+
cwd: "/test",
|
|
478
|
+
mcpServers: [],
|
|
479
|
+
_meta: { agentId },
|
|
480
|
+
}),
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
// The loadResult MUST include the resolved sessionId
|
|
484
|
+
const resolvedSessionId = (loadResult.acp.result as { sessionId?: string })?.sessionId;
|
|
485
|
+
expect(resolvedSessionId).toBeDefined();
|
|
486
|
+
expect(resolvedSessionId).toBe(sessionId);
|
|
487
|
+
expect(resolvedSessionId).not.toBe("_resolve_");
|
|
488
|
+
|
|
489
|
+
// Now the TUI uses the resolved sessionId to call getHistory
|
|
490
|
+
const historyResult = await handler.processRequest(
|
|
491
|
+
agentId,
|
|
492
|
+
envelope(stream2, "_macro/getHistory", { sessionId: resolvedSessionId }),
|
|
493
|
+
);
|
|
494
|
+
|
|
495
|
+
const turns = (historyResult.acp.result as { turns: { role: string; content: unknown }[] }).turns;
|
|
496
|
+
expect(turns).toHaveLength(2);
|
|
497
|
+
expect(turns[0].role).toBe("user");
|
|
498
|
+
expect(turns[0].content).toBe("Hello agent");
|
|
499
|
+
expect(turns[1].role).toBe("assistant");
|
|
500
|
+
const content = turns[1].content as { parts: { type: string; text: string }[] };
|
|
501
|
+
expect(content.parts[0].text).toBe("Hello from agent!");
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it("should return sessionId from loadSession on all code paths", async () => {
|
|
505
|
+
// Verify that loadSession always returns { sessionId } so the TUI
|
|
506
|
+
// never falls back to using agentId for history queries.
|
|
507
|
+
await setup();
|
|
508
|
+
|
|
509
|
+
const agentId = "agent-1" as AgentId;
|
|
510
|
+
const streamId = "stream-sessionid-test";
|
|
511
|
+
|
|
512
|
+
// Initialize
|
|
513
|
+
await handler.processRequest(
|
|
514
|
+
agentId,
|
|
515
|
+
envelope(streamId, "initialize", {
|
|
516
|
+
protocolVersion: 1,
|
|
517
|
+
capabilities: {},
|
|
518
|
+
clientInfo: { name: "test", version: "1.0" },
|
|
519
|
+
}),
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
// Create a session first so the agent exists
|
|
523
|
+
const newResult = await handler.processRequest(
|
|
524
|
+
agentId,
|
|
525
|
+
envelope(streamId, "session/new", {
|
|
526
|
+
cwd: "/test",
|
|
527
|
+
mcpServers: [],
|
|
528
|
+
}),
|
|
529
|
+
);
|
|
530
|
+
const sessionId = (newResult.acp.result as { sessionId?: string })?.sessionId;
|
|
531
|
+
expect(sessionId).toBeDefined();
|
|
532
|
+
|
|
533
|
+
// Register the agent in EventStore so loadSession can resolve it
|
|
534
|
+
registerAgent(agentId, sessionId!);
|
|
535
|
+
|
|
536
|
+
// Now loadSession on a new stream — should return the sessionId
|
|
537
|
+
const stream2 = "stream-sessionid-test-2";
|
|
538
|
+
await handler.processRequest(
|
|
539
|
+
agentId,
|
|
540
|
+
envelope(stream2, "initialize", {
|
|
541
|
+
protocolVersion: 1,
|
|
542
|
+
capabilities: {},
|
|
543
|
+
clientInfo: { name: "test", version: "1.0" },
|
|
544
|
+
}),
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
const loadResult = await handler.processRequest(
|
|
548
|
+
agentId,
|
|
549
|
+
envelope(stream2, "session/load", {
|
|
550
|
+
sessionId: "_resolve_",
|
|
551
|
+
cwd: "/test",
|
|
552
|
+
mcpServers: [],
|
|
553
|
+
_meta: { agentId },
|
|
554
|
+
}),
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
const resolved = (loadResult.acp.result as { sessionId?: string })?.sessionId;
|
|
558
|
+
expect(resolved).toBe(sessionId);
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it("should stream notifications back via emitNotification callback", async () => {
|
|
562
|
+
await setup([
|
|
563
|
+
{
|
|
564
|
+
sessionUpdate: "agent_message_chunk",
|
|
565
|
+
content: { type: "text", text: "Hello" },
|
|
566
|
+
},
|
|
567
|
+
]);
|
|
568
|
+
|
|
569
|
+
const streamId = "test-stream-6";
|
|
570
|
+
const agentId = "agent-1" as AgentId;
|
|
571
|
+
const sessionId = await initAndCreateSession(streamId, agentId);
|
|
572
|
+
|
|
573
|
+
const notifications: ACPEnvelope[] = [];
|
|
574
|
+
const emitNotification = (n: ACPEnvelope) => notifications.push(n);
|
|
575
|
+
|
|
576
|
+
await handler.processRequest(
|
|
577
|
+
agentId,
|
|
578
|
+
envelope(streamId, "session/prompt", {
|
|
579
|
+
prompt: [{ type: "text", text: "Say hello" }],
|
|
580
|
+
}, sessionId),
|
|
581
|
+
emitNotification,
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
// Should have at least the session update notification
|
|
585
|
+
const sessionUpdates = notifications.filter(
|
|
586
|
+
(n) => n.acp.method === "session/update",
|
|
587
|
+
);
|
|
588
|
+
expect(sessionUpdates.length).toBeGreaterThan(0);
|
|
589
|
+
|
|
590
|
+
// AND history should still be recorded
|
|
591
|
+
const historyResult = await handler.processRequest(
|
|
592
|
+
agentId,
|
|
593
|
+
envelope(streamId, "_macro/getHistory", { sessionId }),
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
const turns = (historyResult.acp.result as { turns: unknown[] }).turns;
|
|
597
|
+
expect(turns).toHaveLength(2);
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it("should resolve history via agentId when sessionId is different (restart scenario)", async () => {
|
|
601
|
+
// This test simulates the scenario where:
|
|
602
|
+
// 1. Session is created, prompt is sent (history recorded under sessionId)
|
|
603
|
+
// 2. Server restarts, resume() fails, TUI creates a NEW session
|
|
604
|
+
// 3. TUI calls _macro/getHistory with the NEW sessionId + agentId
|
|
605
|
+
// 4. Server uses agentId to resolve the OLD session and return its history
|
|
606
|
+
|
|
607
|
+
await setup([
|
|
608
|
+
{
|
|
609
|
+
sessionUpdate: "agent_message_chunk",
|
|
610
|
+
content: { type: "text", text: "I remember you!" },
|
|
611
|
+
},
|
|
612
|
+
]);
|
|
613
|
+
|
|
614
|
+
const agentId = "agent-1" as AgentId;
|
|
615
|
+
const streamId = "stream-agentid-lookup";
|
|
616
|
+
const originalSessionId = await initAndCreateSession(streamId, agentId);
|
|
617
|
+
|
|
618
|
+
// Send a prompt (records turns under originalSessionId)
|
|
619
|
+
await handler.processRequest(
|
|
620
|
+
agentId,
|
|
621
|
+
envelope(streamId, "session/prompt", {
|
|
622
|
+
prompt: [{ type: "text", text: "Remember me" }],
|
|
623
|
+
}, originalSessionId),
|
|
624
|
+
);
|
|
625
|
+
|
|
626
|
+
// Verify turns exist under original session
|
|
627
|
+
const check = await handler.processRequest(
|
|
628
|
+
agentId,
|
|
629
|
+
envelope(streamId, "_macro/getHistory", { sessionId: originalSessionId }),
|
|
630
|
+
);
|
|
631
|
+
expect((check.acp.result as { turns: unknown[] }).turns).toHaveLength(2);
|
|
632
|
+
|
|
633
|
+
// Simulate calling getHistory with a DIFFERENT sessionId + agentId
|
|
634
|
+
// (as happens when resume fails and TUI creates a new session).
|
|
635
|
+
// agentId takes priority — resolves to the original session with history.
|
|
636
|
+
const newSessionId = "completely-different-session-id";
|
|
637
|
+
const historyResult = await handler.processRequest(
|
|
638
|
+
agentId,
|
|
639
|
+
envelope(streamId, "_macro/getHistory", {
|
|
640
|
+
sessionId: newSessionId,
|
|
641
|
+
agentId,
|
|
642
|
+
}),
|
|
643
|
+
);
|
|
644
|
+
|
|
645
|
+
// agentId resolves to the OLD session, so we should get history
|
|
646
|
+
const turns = (historyResult.acp.result as { turns: { role: string; content: unknown }[] }).turns;
|
|
647
|
+
expect(turns).toHaveLength(2);
|
|
648
|
+
expect(turns[0].role).toBe("user");
|
|
649
|
+
expect(turns[0].content).toBe("Remember me");
|
|
650
|
+
expect(turns[1].role).toBe("assistant");
|
|
651
|
+
const content = turns[1].content as { parts: { text: string }[] };
|
|
652
|
+
expect(content.parts[0].text).toBe("I remember you!");
|
|
653
|
+
|
|
654
|
+
// Without agentId, only sessionId is used — new session has no turns
|
|
655
|
+
const historyNoAgent = await handler.processRequest(
|
|
656
|
+
agentId,
|
|
657
|
+
envelope(streamId, "_macro/getHistory", {
|
|
658
|
+
sessionId: newSessionId,
|
|
659
|
+
}),
|
|
660
|
+
);
|
|
661
|
+
const turnsNoAgent = (historyNoAgent.acp.result as { turns: unknown[] }).turns;
|
|
662
|
+
expect(turnsNoAgent).toHaveLength(0);
|
|
663
|
+
});
|
|
664
|
+
});
|