forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction-executor.test.ts
More file actions
305 lines (257 loc) · 9.63 KB
/
Copy pathaction-executor.test.ts
File metadata and controls
305 lines (257 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { describe, it, expect } from "vitest";
import { ActionExecutor } from "../../src/actions/action-executor.js";
import { ToolRegistry } from "../../src/tools/tool-registry.js";
import {
AgentInstanceManager,
InMemoryMemoryStore,
InMemoryRuntimeStateStore,
UnconfiguredModelProvider
} from "../../src/index.js";
import type { RuntimeContext } from "../../src/index.js";
describe("ActionExecutor tool dispatch and payload passthrough (Issue #115)", () => {
const createMockContext = (taskId: string): RuntimeContext => ({
runtimeId: "test-runtime",
taskId,
agent: new AgentInstanceManager().getOrCreate("test-agent"),
memory: new InMemoryMemoryStore(),
modelProvider: new UnconfiguredModelProvider(),
state: new InMemoryRuntimeStateStore(),
now: new Date().toISOString()
});
it("dispatches to the correct registered tool by name", async () => {
const registry = new ToolRegistry();
registry.register({
name: "add",
description: "Adds two numbers",
execute: ({ payload }) => ({
sum: (payload as any).a + (payload as any).b
})
});
registry.register({
name: "multiply",
description: "Multiplies two numbers",
execute: ({ payload }) => ({
product: (payload as any).a * (payload as any).b
})
});
const executor = new ActionExecutor(registry);
const ctx = createMockContext("task-math");
const addResult = await executor.execute("add", { a: 3, b: 4 }, ctx);
expect(addResult).toEqual({ sum: 7 });
const mulResult = await executor.execute("multiply", { a: 3, b: 4 }, ctx);
expect(mulResult).toEqual({ product: 12 });
});
it("passes payload through to tool execute without modification", async () => {
const registry = new ToolRegistry();
let receivedPayload: unknown = null;
registry.register({
name: "capture",
description: "Captures payload for inspection",
execute: ({ payload }) => {
receivedPayload = payload;
return { ok: true };
}
});
const executor = new ActionExecutor(registry);
const complexPayload = {
nested: { arr: [1, 2, 3], flag: true },
label: "test"
};
await executor.execute("capture", complexPayload, createMockContext("task-payload"));
expect(receivedPayload).toBe(complexPayload);
});
it("passes context through to tool execute", async () => {
const registry = new ToolRegistry();
let receivedContext: any = null;
registry.register({
name: "ctxCapture",
description: "Captures context for inspection",
execute: ({ context }) => {
receivedContext = context;
return { ok: true };
}
});
const executor = new ActionExecutor(registry);
const mockContext = createMockContext("t1");
await executor.execute("ctxCapture", {}, mockContext);
expect(receivedContext).toBe(mockContext);
});
it("throws TOOL_NOT_FOUND for unregistered tool names", async () => {
const registry = new ToolRegistry();
const executor = new ActionExecutor(registry);
await expect(
executor.execute("nonexistent", {}, createMockContext("task-err"))
).rejects.toThrow(/not registered/);
});
it("does not increment tool call count when tool is not found (Issue #256)", async () => {
const registry = new ToolRegistry();
registry.register({
name: "valid-tool",
description: "A valid tool",
execute: () => ({ success: true })
});
const executor = new ActionExecutor(registry, 1);
const mockContext = { runtimeId: "r1", taskId: "task-quota-1" } as any;
expect(executor.getToolCallCount("task-quota-1")).toBe(0);
// Call unknown tool -> should fail with TOOL_NOT_FOUND and not consume budget
await expect(
executor.execute("missing-tool", {}, mockContext)
).rejects.toMatchObject({
name: "RuntimeError",
code: "TOOL_NOT_FOUND"
});
expect(executor.getToolCallCount("task-quota-1")).toBe(0);
// Subsequent valid call with maxToolCallsPerTask: 1 should still succeed
const result = await executor.execute("valid-tool", {}, mockContext);
expect(result).toEqual({ success: true });
expect(executor.getToolCallCount("task-quota-1")).toBe(1);
});
it("returns async tool results correctly", async () => {
const registry = new ToolRegistry();
registry.register({
name: "asyncTool",
description: "Returns a promise",
execute: async ({ payload }) => {
return { value: (payload as any).x * 10 };
}
});
const executor = new ActionExecutor(registry);
const result = await executor.execute("asyncTool", { x: 5 }, createMockContext("task-async"));
expect(result).toEqual({ value: 50 });
});
it("executes tools and tracks call counts per task", async () => {
const registry = new ToolRegistry();
registry.register({
name: "test-tool",
description: "Test tool",
execute({ payload }) {
return { handled: payload };
}
});
const executor = new ActionExecutor(registry);
const ctx = createMockContext("task-1");
expect(executor.getToolCallCount("task-1")).toBe(0);
const result1 = await executor.execute("test-tool", { count: 1 }, ctx);
expect(result1).toEqual({ handled: { count: 1 } });
expect(executor.getToolCallCount("task-1")).toBe(1);
const result2 = await executor.execute("test-tool", { count: 2 }, ctx);
expect(result2).toEqual({ handled: { count: 2 } });
expect(executor.getToolCallCount("task-1")).toBe(2);
});
it("enforces maxToolCallsPerTask policy limit", async () => {
const registry = new ToolRegistry();
registry.register({
name: "ping",
description: "Ping tool",
execute() {
return "pong";
}
});
const executor = new ActionExecutor(registry, 2);
const ctx = createMockContext("task-limited");
// Call 1: Allowed (count 0 -> 1)
await expect(executor.execute("ping", {}, ctx)).resolves.toBe("pong");
// Call 2: Allowed (count 1 -> 2)
await expect(executor.execute("ping", {}, ctx)).resolves.toBe("pong");
// Call 3: Exceeds limit (count 2 >= 2)
await expect(executor.execute("ping", {}, ctx)).rejects.toMatchObject({
name: "RuntimeError",
code: "MAX_TOOL_CALLS_EXCEEDED",
details: {
currentToolCalls: 2,
maxToolCalls: 2
}
});
});
it("isolates call limits per task ID", async () => {
const registry = new ToolRegistry();
registry.register({
name: "ping",
description: "Ping tool",
execute() {
return "pong";
}
});
const executor = new ActionExecutor(registry, 1);
const ctxA = createMockContext("task-A");
const ctxB = createMockContext("task-B");
// task-A first call succeeds
await expect(executor.execute("ping", {}, ctxA)).resolves.toBe("pong");
// task-A second call fails
await expect(executor.execute("ping", {}, ctxA)).rejects.toMatchObject({
code: "MAX_TOOL_CALLS_EXCEEDED"
});
// task-B has its own quota and succeeds
await expect(executor.execute("ping", {}, ctxB)).resolves.toBe("pong");
});
});
describe("ActionExecutor per-task tool call tracking and limits", () => {
const createMockContext = (taskId: string): RuntimeContext => ({
runtimeId: "test-runtime",
taskId,
agent: new AgentInstanceManager().getOrCreate("test-agent"),
memory: new InMemoryMemoryStore(),
modelProvider: new UnconfiguredModelProvider(),
state: new InMemoryRuntimeStateStore(),
now: new Date().toISOString()
});
it("executes tools and tracks call counts per task", async () => {
const registry = new ToolRegistry();
registry.register({
name: "test-tool",
description: "Test tool",
execute({ payload }) {
return { handled: payload };
}
});
const executor = new ActionExecutor(registry);
const ctx = createMockContext("task-1");
expect(executor.getToolCallCount("task-1")).toBe(0);
const result1 = await executor.execute("test-tool", { count: 1 }, ctx);
expect(result1).toEqual({ handled: { count: 1 } });
expect(executor.getToolCallCount("task-1")).toBe(1);
const result2 = await executor.execute("test-tool", { count: 2 }, ctx);
expect(result2).toEqual({ handled: { count: 2 } });
expect(executor.getToolCallCount("task-1")).toBe(2);
});
it("enforces maxToolCallsPerTask policy limit", async () => {
const registry = new ToolRegistry();
registry.register({
name: "ping",
description: "Ping tool",
execute() {
return "pong";
}
});
const executor = new ActionExecutor(registry, 2);
const ctx = createMockContext("task-limited");
await expect(executor.execute("ping", {}, ctx)).resolves.toBe("pong");
await expect(executor.execute("ping", {}, ctx)).resolves.toBe("pong");
await expect(executor.execute("ping", {}, ctx)).rejects.toMatchObject({
name: "RuntimeError",
code: "MAX_TOOL_CALLS_EXCEEDED",
details: {
currentToolCalls: 2,
maxToolCalls: 2
}
});
});
it("isolates call limits per task ID", async () => {
const registry = new ToolRegistry();
registry.register({
name: "ping",
description: "Ping tool",
execute() {
return "pong";
}
});
const executor = new ActionExecutor(registry, 1);
const ctxA = createMockContext("task-A");
const ctxB = createMockContext("task-B");
await expect(executor.execute("ping", {}, ctxA)).resolves.toBe("pong");
await expect(executor.execute("ping", {}, ctxA)).rejects.toMatchObject({
code: "MAX_TOOL_CALLS_EXCEEDED"
});
await expect(executor.execute("ping", {}, ctxB)).resolves.toBe("pong");
});
});