@milaboratories/pl-mcp-server 53.0.0 → 54.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -2
- package/dist/instructions.cjs +3 -2
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +3 -2
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +72 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +72 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/server-log.cjs +373 -0
- package/dist/server-log.cjs.map +1 -0
- package/dist/server-log.js +371 -0
- package/dist/server-log.js.map +1 -0
- package/dist/server.cjs +77 -22
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +31 -7
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +77 -23
- package/dist/server.js.map +1 -1
- package/dist/tools/await.cjs +59 -60
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +60 -61
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +11 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +11 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-params.cjs +4 -3
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +4 -3
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +46 -41
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +47 -42
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/blocks.cjs +30 -20
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +30 -20
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/connection.cjs.map +1 -1
- package/dist/tools/connection.js.map +1 -1
- package/dist/tools/data-query.cjs +111 -80
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +114 -83
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/ids.cjs +16 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +13 -1
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/logs.cjs +28 -26
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +28 -26
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/ping.cjs.map +1 -1
- package/dist/tools/ping.js.map +1 -1
- package/dist/tools/projects.cjs +10 -27
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +11 -28
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/sandbox.cjs +110 -8
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +107 -9
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/screenshot.cjs +9 -1
- package/dist/tools/screenshot.cjs.map +1 -1
- package/dist/tools/screenshot.js +9 -1
- package/dist/tools/screenshot.js.map +1 -1
- package/dist/tools/types.cjs +29 -0
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +27 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/ui-interaction.cjs.map +1 -1
- package/dist/tools/ui-interaction.js.map +1 -1
- package/package.json +6 -5
- package/src/instructions.ts +3 -2
- package/src/project-holds.test.ts +183 -0
- package/src/project-holds.ts +105 -0
- package/src/server-log.test.ts +871 -0
- package/src/server-log.ts +504 -0
- package/src/server.logging.test.ts +383 -0
- package/src/server.request-failure.test.ts +49 -0
- package/src/server.test.ts +15 -5
- package/src/server.ts +111 -26
- package/src/test/recording-logger.ts +33 -0
- package/src/tools/await.ts +96 -98
- package/src/tools/block-action-context.ts +16 -16
- package/src/tools/block-params.ts +6 -6
- package/src/tools/block-state.ts +61 -60
- package/src/tools/blocks.ts +49 -40
- package/src/tools/connection.ts +2 -3
- package/src/tools/data-query.ts +151 -108
- package/src/tools/ids.ts +22 -1
- package/src/tools/logs.ts +43 -43
- package/src/tools/ping.ts +2 -3
- package/src/tools/projects.ts +22 -45
- package/src/tools/sandbox.test.ts +183 -0
- package/src/tools/sandbox.ts +126 -10
- package/src/tools/screenshot.ts +15 -4
- package/src/tools/types.test.ts +4 -3
- package/src/tools/types.ts +73 -3
- package/src/tools/ui-interaction.ts +2 -3
|
@@ -0,0 +1,871 @@
|
|
|
1
|
+
import type { MiLogger } from "@milaboratories/helpers";
|
|
2
|
+
import { inspect } from "node:util";
|
|
3
|
+
import { describe, expect, test } from "vitest";
|
|
4
|
+
import type { McpInstanceId } from "./server";
|
|
5
|
+
import type { ServerLogOptions, TxCounters } from "./server-log";
|
|
6
|
+
import { fields, ServerLog } from "./server-log";
|
|
7
|
+
import { recordingLogger } from "./test/recording-logger";
|
|
8
|
+
import { safeEval } from "./tools/sandbox";
|
|
9
|
+
import {
|
|
10
|
+
errorResult,
|
|
11
|
+
isTimedOutResult,
|
|
12
|
+
textResult,
|
|
13
|
+
timedOutResult,
|
|
14
|
+
transformFailedResult,
|
|
15
|
+
} from "./tools/types";
|
|
16
|
+
|
|
17
|
+
const instanceId = "mcp-1a2b3c4d" as McpInstanceId;
|
|
18
|
+
|
|
19
|
+
/** A log writing to a recording logger, on a clock the test moves by hand. */
|
|
20
|
+
function logOnManualClock(options: Partial<ServerLogOptions> = {}) {
|
|
21
|
+
const recorded = recordingLogger();
|
|
22
|
+
const clock = { ms: 1000 };
|
|
23
|
+
const log = new ServerLog({
|
|
24
|
+
logger: recorded.logger,
|
|
25
|
+
instanceId,
|
|
26
|
+
now: () => clock.ms,
|
|
27
|
+
...options,
|
|
28
|
+
});
|
|
29
|
+
/** A handler that takes `ms` and then returns `result`. */
|
|
30
|
+
const returning =
|
|
31
|
+
<T>(result: T, ms = 0) =>
|
|
32
|
+
async (): Promise<T> => {
|
|
33
|
+
clock.ms += ms;
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
/** A handler that takes `ms` and then throws `error`. */
|
|
37
|
+
const throwing =
|
|
38
|
+
(error: unknown, ms = 0) =>
|
|
39
|
+
async (): Promise<never> => {
|
|
40
|
+
clock.ms += ms;
|
|
41
|
+
throw error;
|
|
42
|
+
};
|
|
43
|
+
return { ...recorded, log, clock, returning, throwing };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A promise settled from outside, to hold a call open. */
|
|
47
|
+
function deferred<T>() {
|
|
48
|
+
let resolve: (value: T) => void = () => {};
|
|
49
|
+
const promise = new Promise<T>((r) => {
|
|
50
|
+
resolve = r;
|
|
51
|
+
});
|
|
52
|
+
return { promise, resolve };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The size `resultBytes` gives for a result of text items: the UTF-8 bytes of the texts. */
|
|
56
|
+
function textBytes(result: { content: { text: string }[] }): number {
|
|
57
|
+
return result.content.reduce((sum, item) => sum + Buffer.byteLength(item.text), 0);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Reads the fields of a `key=value` line into a record; the first word is the kind of line. */
|
|
61
|
+
function parse(line: string): Record<string, string> {
|
|
62
|
+
const firstLine = line.split("\n", 1)[0] ?? "";
|
|
63
|
+
const parsed: Record<string, string> = {};
|
|
64
|
+
for (const match of firstLine.matchAll(/(\w+)=("(?:[^"\\]|\\.)*"|\S*)/g)) {
|
|
65
|
+
const [, key, value] = match;
|
|
66
|
+
if (key === undefined || value === undefined) continue;
|
|
67
|
+
parsed[key] = value.startsWith('"') ? String(JSON.parse(value)) : value;
|
|
68
|
+
}
|
|
69
|
+
return parsed;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
describe("a call that succeeds", () => {
|
|
73
|
+
test("writes one info line with everything about the call", async () => {
|
|
74
|
+
const { log, lines, returning } = logOnManualClock();
|
|
75
|
+
const args = { projectId: "NG:0x35B1EB", blockId: "b-1", label: "Enter Numbers" };
|
|
76
|
+
const result = textResult({ ok: true });
|
|
77
|
+
|
|
78
|
+
const returned = await log.run("add_block", args, returning(result, 331));
|
|
79
|
+
|
|
80
|
+
expect(returned).toBe(result);
|
|
81
|
+
expect(lines).toEqual([
|
|
82
|
+
{
|
|
83
|
+
level: "info",
|
|
84
|
+
text:
|
|
85
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=add_block project=NG:0x35B1EB block=b-1 " +
|
|
86
|
+
`argsBytes=${Buffer.byteLength(JSON.stringify(args))} ms=331 outcome=ok ` +
|
|
87
|
+
`resultBytes=${textBytes(result)} inflight=0`,
|
|
88
|
+
},
|
|
89
|
+
]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("leaves out the ids the arguments do not have", async () => {
|
|
93
|
+
const { log, at, returning } = logOnManualClock();
|
|
94
|
+
await log.run("list_projects", {}, returning(textResult([])));
|
|
95
|
+
await log.run("ping", undefined, returning(textResult({ status: "ok" })));
|
|
96
|
+
|
|
97
|
+
expect(at("info")).toEqual([
|
|
98
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=list_projects argsBytes=2 ms=0 outcome=ok resultBytes=2 inflight=0",
|
|
99
|
+
expect.stringMatching(/^mcp-call call=mcp-1a2b3c4d#2 tool=ping argsBytes=0 ms=0 outcome=ok /),
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("takes an id only from a string argument", async () => {
|
|
104
|
+
const { log, at, returning } = logOnManualClock();
|
|
105
|
+
await log.run(
|
|
106
|
+
"get_project_overview",
|
|
107
|
+
{ projectId: 5, blockId: { id: "b" } },
|
|
108
|
+
returning(textResult(1)),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(parse(at("info")[0] ?? "")).not.toHaveProperty("project");
|
|
112
|
+
expect(parse(at("info")[0] ?? "")).not.toHaveProperty("block");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("measures sizes in bytes, not characters", async () => {
|
|
116
|
+
const { log, at, returning } = logOnManualClock();
|
|
117
|
+
await log.run("set_block_title", { title: "é" }, returning(textResult("ü")));
|
|
118
|
+
|
|
119
|
+
const line = parse(at("info")[0] ?? "");
|
|
120
|
+
expect(line.argsBytes).toBe(String('{"title":"é"}'.length + 1));
|
|
121
|
+
expect(line.resultBytes).toBe(String('"ü"'.length + 1));
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("measures the content of a result, not the JSON around it", async () => {
|
|
125
|
+
const { log, at, returning } = logOnManualClock();
|
|
126
|
+
const image = "iVBORw0KGgo=";
|
|
127
|
+
const result = {
|
|
128
|
+
content: [
|
|
129
|
+
{ type: "text", text: 'a "quoted"\nline' },
|
|
130
|
+
{ type: "image", data: image, mimeType: "image/png" },
|
|
131
|
+
{ type: "resource", resource: { uri: "file:///a", text: "é" } },
|
|
132
|
+
{ type: "resource", resource: { uri: "file:///b", blob: image } },
|
|
133
|
+
],
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
await log.run("capture_screenshot", {}, returning(result));
|
|
137
|
+
await log.run("ping", undefined, returning("not a tool result"));
|
|
138
|
+
|
|
139
|
+
const [first, second] = at("info").map(parse);
|
|
140
|
+
expect(first?.resultBytes).toBe(
|
|
141
|
+
String(Buffer.byteLength('a "quoted"\nline') + image.length + 2 + image.length),
|
|
142
|
+
);
|
|
143
|
+
expect(second).not.toHaveProperty("resultBytes");
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("a call that returns an error result", () => {
|
|
148
|
+
test("writes the info line and an error line with the text the handler returned", async () => {
|
|
149
|
+
const { log, lines, returning } = logOnManualClock();
|
|
150
|
+
const args = { projectId: "p-1", blockId: "b-9", timeout: 100 };
|
|
151
|
+
const result = errorResult("Block b-9 not found in project p-1.", "Use get_project_overview.");
|
|
152
|
+
|
|
153
|
+
const returned = await log.run("await_block_done", args, returning(result, 12));
|
|
154
|
+
|
|
155
|
+
expect(returned).toBe(result);
|
|
156
|
+
expect(lines).toEqual([
|
|
157
|
+
{
|
|
158
|
+
level: "info",
|
|
159
|
+
text:
|
|
160
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=await_block_done project=p-1 block=b-9 " +
|
|
161
|
+
`argsBytes=${JSON.stringify(args).length} ms=12 outcome=error ` +
|
|
162
|
+
`resultBytes=${textBytes(result)} inflight=0`,
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
level: "error",
|
|
166
|
+
text:
|
|
167
|
+
"mcp-call-failed call=mcp-1a2b3c4d#1 tool=await_block_done project=p-1 block=b-9 " +
|
|
168
|
+
'outcome=error argKeys=projectId,blockId,timeout error="Block b-9 not found in project p-1."\n' +
|
|
169
|
+
"Block b-9 not found in project p-1.\n\nHint: Use get_project_overview.",
|
|
170
|
+
},
|
|
171
|
+
]);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe("a call that throws", () => {
|
|
176
|
+
test("throws the same error on, and writes it with its causes and stacks", async () => {
|
|
177
|
+
const { log, lines, at, throwing } = logOnManualClock();
|
|
178
|
+
const root = new TypeError("socket hang up");
|
|
179
|
+
const middle = new Error("transaction failed", { cause: root });
|
|
180
|
+
const thrown = new Error("Failed to add block", { cause: middle });
|
|
181
|
+
|
|
182
|
+
await expect(log.run("add_block", { projectId: "p-1" }, throwing(thrown, 40))).rejects.toBe(
|
|
183
|
+
thrown,
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
expect(lines.map((line) => line.level)).toEqual(["info", "error"]);
|
|
187
|
+
expect(at("info")[0]).toBe(
|
|
188
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=add_block project=p-1 argsBytes=19 ms=40 " +
|
|
189
|
+
"outcome=thrown resultBytes=0 inflight=0",
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const [head, ...rest] = (at("error")[0] ?? "").split("\n");
|
|
193
|
+
expect(head).toBe(
|
|
194
|
+
"mcp-call-failed call=mcp-1a2b3c4d#1 tool=add_block project=p-1 outcome=thrown " +
|
|
195
|
+
'argKeys=projectId error="Failed to add block"',
|
|
196
|
+
);
|
|
197
|
+
const details = rest.join("\n");
|
|
198
|
+
expect(details).toMatch(/^Error: Failed to add block\n\s+at /);
|
|
199
|
+
expect(details).toMatch(/\n\s+\[cause\]: Error: transaction failed\n\s+at /);
|
|
200
|
+
expect(details).toMatch(/\n\s+\[cause\]: TypeError: socket hang up\n\s+at /);
|
|
201
|
+
// The stacks, not only the messages: this file is where the three errors were made.
|
|
202
|
+
expect(details.match(/server-log\.test\.ts/g)?.length).toBeGreaterThanOrEqual(3);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("writes a thrown value that is not an error", async () => {
|
|
206
|
+
const { log, at, throwing } = logOnManualClock();
|
|
207
|
+
|
|
208
|
+
await expect(log.run("ping", undefined, throwing("just a string"))).rejects.toBe(
|
|
209
|
+
"just a string",
|
|
210
|
+
);
|
|
211
|
+
await expect(log.run("ping", undefined, throwing({ code: 7 }))).rejects.toEqual({ code: 7 });
|
|
212
|
+
|
|
213
|
+
expect(at("error")).toEqual([
|
|
214
|
+
'mcp-call-failed call=mcp-1a2b3c4d#1 tool=ping outcome=thrown argKeys="" error="just a string"\njust a string',
|
|
215
|
+
'mcp-call-failed call=mcp-1a2b3c4d#2 tool=ping outcome=thrown argKeys="" error="{\\"code\\":7}"\n{ code: 7 }',
|
|
216
|
+
]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("stops at a cause chain that loops", async () => {
|
|
220
|
+
const { log, at, throwing } = logOnManualClock();
|
|
221
|
+
const a = new Error("a");
|
|
222
|
+
const b = new Error("b", { cause: a });
|
|
223
|
+
a.cause = b;
|
|
224
|
+
|
|
225
|
+
await expect(log.run("ping", undefined, throwing(a))).rejects.toBe(a);
|
|
226
|
+
|
|
227
|
+
expect(at("error")[0]).toMatch(/\[Circular \*1\]/);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("stops at a cause chain that is too long", async () => {
|
|
231
|
+
const { log, at, throwing } = logOnManualClock();
|
|
232
|
+
let error = new Error("cause 0");
|
|
233
|
+
for (let i = 1; i <= 20; i++) error = new Error(`cause ${i}`, { cause: error });
|
|
234
|
+
|
|
235
|
+
await expect(log.run("ping", undefined, throwing(error))).rejects.toBe(error);
|
|
236
|
+
|
|
237
|
+
const written = at("error")[0] ?? "";
|
|
238
|
+
expect(written).toContain("cause 20");
|
|
239
|
+
expect(written).not.toContain("cause 0\n");
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("a slow call", () => {
|
|
244
|
+
test("is reported with a warning once it takes longer than the threshold", async () => {
|
|
245
|
+
const { log, lines, returning } = logOnManualClock();
|
|
246
|
+
|
|
247
|
+
await log.run(
|
|
248
|
+
"run_block",
|
|
249
|
+
{ projectId: "p-1", blockId: "b-1" },
|
|
250
|
+
returning(textResult(1), 5000),
|
|
251
|
+
);
|
|
252
|
+
await log.run(
|
|
253
|
+
"run_block",
|
|
254
|
+
{ projectId: "p-1", blockId: "b-1" },
|
|
255
|
+
returning(textResult(1), 5001),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
expect(lines.map((line) => line.level)).toEqual(["info", "info", "warn"]);
|
|
259
|
+
expect(lines[2]?.text).toBe(
|
|
260
|
+
"mcp-call-slow call=mcp-1a2b3c4d#2 tool=run_block project=p-1 block=b-1 ms=5001 thresholdMs=5000",
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("is reported when it failed or threw, after the failure", async () => {
|
|
265
|
+
const { log, lines, returning, throwing } = logOnManualClock({ slowCallMs: 10 });
|
|
266
|
+
|
|
267
|
+
await log.run("run_block", {}, returning(errorResult("no"), 11));
|
|
268
|
+
await expect(log.run("run_block", {}, throwing(new Error("no"), 11))).rejects.toThrow("no");
|
|
269
|
+
|
|
270
|
+
expect(lines.map((line) => line.level)).toEqual([
|
|
271
|
+
"info",
|
|
272
|
+
"error",
|
|
273
|
+
"warn",
|
|
274
|
+
"info",
|
|
275
|
+
"error",
|
|
276
|
+
"warn",
|
|
277
|
+
]);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("of the tool that waits for a block is not reported", async () => {
|
|
281
|
+
const { log, lines, returning } = logOnManualClock();
|
|
282
|
+
|
|
283
|
+
await log.run("await_block_done", { projectId: "p-1" }, returning(textResult(1), 120_000));
|
|
284
|
+
|
|
285
|
+
expect(lines.map((line) => line.level)).toEqual(["info"]);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
describe("a wait that ran out of time", () => {
|
|
290
|
+
test("is reported with a warning, and its result is the plain JSON", async () => {
|
|
291
|
+
const { log, lines, returning } = logOnManualClock();
|
|
292
|
+
const result = timedOutResult({ status: "Running", hint: "Call again." });
|
|
293
|
+
|
|
294
|
+
await log.run(
|
|
295
|
+
"await_block_done",
|
|
296
|
+
{ projectId: "p-1", blockId: "b-1" },
|
|
297
|
+
returning(result, 120_004),
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
expect(result).toEqual({
|
|
301
|
+
content: [
|
|
302
|
+
{ type: "text", text: '{"timedOut":true,"status":"Running","hint":"Call again."}' },
|
|
303
|
+
],
|
|
304
|
+
});
|
|
305
|
+
expect(lines.map((line) => line.level)).toEqual(["info", "warn"]);
|
|
306
|
+
expect(lines[0]?.text).toContain("outcome=ok");
|
|
307
|
+
expect(lines[1]?.text).toBe(
|
|
308
|
+
"mcp-call-timeout call=mcp-1a2b3c4d#1 tool=await_block_done project=p-1 block=b-1 ms=120004",
|
|
309
|
+
);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("is recognised by how the result was made, not by what it says", () => {
|
|
313
|
+
expect(isTimedOutResult(timedOutResult({ status: "Done" }))).toBe(true);
|
|
314
|
+
expect(isTimedOutResult(textResult({ timedOut: true, status: "Done" }))).toBe(false);
|
|
315
|
+
expect(isTimedOutResult(undefined)).toBe(false);
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
describe("calls served at the same time", () => {
|
|
320
|
+
test("are numbered in the order they started and say how many were in flight", async () => {
|
|
321
|
+
const { log, at } = logOnManualClock();
|
|
322
|
+
const first = deferred<string>();
|
|
323
|
+
const second = deferred<string>();
|
|
324
|
+
const third = deferred<string>();
|
|
325
|
+
|
|
326
|
+
const calls = [
|
|
327
|
+
log.run("list_projects", {}, () => first.promise),
|
|
328
|
+
log.run("get_block_state", {}, () => second.promise),
|
|
329
|
+
log.run("ping", undefined, () => third.promise),
|
|
330
|
+
];
|
|
331
|
+
// Finished out of order: a line is written when its call ends.
|
|
332
|
+
second.resolve("second");
|
|
333
|
+
await calls[1];
|
|
334
|
+
third.resolve("third");
|
|
335
|
+
await calls[2];
|
|
336
|
+
first.resolve("first");
|
|
337
|
+
expect(await Promise.all(calls)).toEqual(["first", "second", "third"]);
|
|
338
|
+
await log.run("ping", undefined, async () => "alone");
|
|
339
|
+
|
|
340
|
+
expect(at("info").map(parse)).toMatchObject([
|
|
341
|
+
{ call: "mcp-1a2b3c4d#2", tool: "get_block_state", inflight: "1" },
|
|
342
|
+
{ call: "mcp-1a2b3c4d#3", tool: "ping", inflight: "2" },
|
|
343
|
+
{ call: "mcp-1a2b3c4d#1", tool: "list_projects", inflight: "0" },
|
|
344
|
+
{ call: "mcp-1a2b3c4d#4", tool: "ping", inflight: "0" },
|
|
345
|
+
]);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test("a call that threw no longer counts as in flight", async () => {
|
|
349
|
+
const { log, at, throwing } = logOnManualClock();
|
|
350
|
+
|
|
351
|
+
await expect(log.run("ping", undefined, throwing(new Error("no")))).rejects.toThrow("no");
|
|
352
|
+
await log.run("ping", undefined, async () => "ok");
|
|
353
|
+
|
|
354
|
+
expect(parse(at("info")[1] ?? "")).toMatchObject({ call: "mcp-1a2b3c4d#2", inflight: "0" });
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
describe("commit conflicts of the backend client", () => {
|
|
359
|
+
/** A client whose conflict counter the test sets. */
|
|
360
|
+
function backend() {
|
|
361
|
+
const state = { client: {}, conflicts: 0 };
|
|
362
|
+
const txCounters = (): TxCounters => ({ client: state.client, conflicts: state.conflicts });
|
|
363
|
+
return { state, txCounters };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
test("that happened during the call are counted", async () => {
|
|
367
|
+
const { state, txCounters } = backend();
|
|
368
|
+
state.conflicts = 7;
|
|
369
|
+
const { log, at } = logOnManualClock({ txCounters });
|
|
370
|
+
|
|
371
|
+
await log.run("add_block", {}, async () => {
|
|
372
|
+
state.conflicts += 3;
|
|
373
|
+
return textResult(1);
|
|
374
|
+
});
|
|
375
|
+
await log.run("list_projects", {}, async () => textResult(1));
|
|
376
|
+
|
|
377
|
+
expect(at("info").map(parse)).toMatchObject([
|
|
378
|
+
{ tool: "add_block", txConflictsDuring: "3" },
|
|
379
|
+
{ tool: "list_projects", txConflictsDuring: "0" },
|
|
380
|
+
]);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
test("are counted for every call in flight at the time", async () => {
|
|
384
|
+
const { state, txCounters } = backend();
|
|
385
|
+
const { log, at } = logOnManualClock({ txCounters });
|
|
386
|
+
const reading = deferred<string>();
|
|
387
|
+
|
|
388
|
+
const read = log.run("get_block_state", {}, () => reading.promise);
|
|
389
|
+
await log.run("add_block", {}, async () => {
|
|
390
|
+
state.conflicts += 2;
|
|
391
|
+
return textResult(1);
|
|
392
|
+
});
|
|
393
|
+
reading.resolve("state");
|
|
394
|
+
await read;
|
|
395
|
+
|
|
396
|
+
expect(at("info").map(parse)).toMatchObject([
|
|
397
|
+
{ tool: "add_block", txConflictsDuring: "2" },
|
|
398
|
+
{ tool: "get_block_state", txConflictsDuring: "2" },
|
|
399
|
+
]);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test("are left out when there is no backend, or it changed during the call", async () => {
|
|
403
|
+
const { state, txCounters } = backend();
|
|
404
|
+
let connected = false;
|
|
405
|
+
const { log, at } = logOnManualClock({
|
|
406
|
+
txCounters: () => (connected ? txCounters() : undefined),
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
await log.run("ping", undefined, async () => textResult(1));
|
|
410
|
+
await log.run("connect_to_server", {}, async () => {
|
|
411
|
+
connected = true;
|
|
412
|
+
return textResult(1);
|
|
413
|
+
});
|
|
414
|
+
await log.run("connect_to_server", {}, async () => {
|
|
415
|
+
state.client = {};
|
|
416
|
+
return textResult(1);
|
|
417
|
+
});
|
|
418
|
+
await log.run("ping", undefined, async () => textResult(1));
|
|
419
|
+
|
|
420
|
+
const lines = at("info").map(parse);
|
|
421
|
+
expect(lines[0]).not.toHaveProperty("txConflictsDuring");
|
|
422
|
+
expect(lines[1]).not.toHaveProperty("txConflictsDuring");
|
|
423
|
+
expect(lines[2]).not.toHaveProperty("txConflictsDuring");
|
|
424
|
+
expect(lines[3]).toMatchObject({ txConflictsDuring: "0" });
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
describe("argument values", () => {
|
|
429
|
+
const args = {
|
|
430
|
+
projectId: "p-1",
|
|
431
|
+
blockId: "b-1",
|
|
432
|
+
addr: "https://pl.example.org:6345",
|
|
433
|
+
login: "someone@example.org",
|
|
434
|
+
password: "correct-horse-battery-staple",
|
|
435
|
+
data: { name: "sample-a", files: ["/data/reads_R1.fastq.gz"] },
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
test("are not written in the line of a call: only the ids, the size and the names", async () => {
|
|
439
|
+
const { log, everything, returning } = logOnManualClock();
|
|
440
|
+
|
|
441
|
+
await log.run("connect_to_server", args, returning(textResult({ status: "connected" }), 5));
|
|
442
|
+
|
|
443
|
+
const written = everything();
|
|
444
|
+
for (const value of [args.password, args.addr, args.login, "sample-a", "reads_R1"])
|
|
445
|
+
expect(written).not.toContain(value);
|
|
446
|
+
expect(written).toContain("project=p-1 block=b-1");
|
|
447
|
+
expect(written).toContain(`argsBytes=${Buffer.byteLength(JSON.stringify(args))}`);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test("an id that is not a plain token is quoted and cut, so the line stays one line", async () => {
|
|
451
|
+
const { log, at, returning } = logOnManualClock();
|
|
452
|
+
const odd = 'p 1"\noutcome=ok';
|
|
453
|
+
|
|
454
|
+
await log.run("get_project_overview", { projectId: odd }, returning(textResult(1)));
|
|
455
|
+
await log.run(
|
|
456
|
+
"get_project_overview",
|
|
457
|
+
{ projectId: "x".repeat(5000) },
|
|
458
|
+
returning(textResult(1)),
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
const [quoted, long] = at("info");
|
|
462
|
+
expect(quoted).not.toContain("\n");
|
|
463
|
+
expect(parse(quoted ?? "")).toMatchObject({ project: odd, outcome: "ok" });
|
|
464
|
+
expect(parse(long ?? "").project).toBe(`${"x".repeat(200)}…`);
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
describe("the text of a failure", () => {
|
|
469
|
+
test("is written as it is, argument values included", async () => {
|
|
470
|
+
const { log, at, returning, throwing } = logOnManualClock();
|
|
471
|
+
const folder = "/Users/someone/blocks/missing-dev-block";
|
|
472
|
+
|
|
473
|
+
await log.run(
|
|
474
|
+
"set_block_data",
|
|
475
|
+
{ data: { name: "sample-a" } },
|
|
476
|
+
returning(errorResult("No sample sample-a in the block.")),
|
|
477
|
+
);
|
|
478
|
+
await expect(
|
|
479
|
+
log.run(
|
|
480
|
+
"add_block",
|
|
481
|
+
{ spec: { type: "dev-v2", folder } },
|
|
482
|
+
throwing(new Error(`Block folder ${folder} does not exist`)),
|
|
483
|
+
),
|
|
484
|
+
).rejects.toThrow();
|
|
485
|
+
|
|
486
|
+
const [returned, thrown] = at("error");
|
|
487
|
+
expect(returned).toBe(
|
|
488
|
+
'mcp-call-failed call=mcp-1a2b3c4d#1 tool=set_block_data outcome=error argKeys=data error="No sample sample-a in the block."\n' +
|
|
489
|
+
"No sample sample-a in the block.",
|
|
490
|
+
);
|
|
491
|
+
expect(parse(thrown ?? "").error).toBe(`Block folder ${folder} does not exist`);
|
|
492
|
+
expect(thrown).toContain(`\nError: Block folder ${folder} does not exist\n`);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
test("of a transform is written whole, as the agent gets it", async () => {
|
|
496
|
+
const { log, at } = logOnManualClock();
|
|
497
|
+
const transform = "data.samples.sample_a.file";
|
|
498
|
+
let agentText = "";
|
|
499
|
+
|
|
500
|
+
await log.run("get_block_state", { projectId: "p-1", blockId: "b-1", transform }, async () => {
|
|
501
|
+
try {
|
|
502
|
+
return textResult(await safeEval(transform, { data: {} }, 5000));
|
|
503
|
+
} catch (e: unknown) {
|
|
504
|
+
const result = transformFailedResult(e, "Check your JS expression syntax.");
|
|
505
|
+
agentText = result.content[0]?.text ?? "";
|
|
506
|
+
return result;
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
expect(agentText).toMatch(/^Transform failed: TypeError: .*sample_a/);
|
|
511
|
+
expect(at("error")[0]).toContain(`\n${agentText}`);
|
|
512
|
+
expect(parse(at("error")[0] ?? "").error).toBe(agentText.split("\n", 1)[0]);
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
describe("a secret argument", () => {
|
|
517
|
+
const password = "correct-horse-battery-staple";
|
|
518
|
+
|
|
519
|
+
test("never reaches any line, whatever the outcome", async () => {
|
|
520
|
+
const { log, lines, everything, returning, throwing } = logOnManualClock({ slowCallMs: 1 });
|
|
521
|
+
const args = { projectId: "p-1", login: "someone", password };
|
|
522
|
+
|
|
523
|
+
await log.run("connect_to_server", args, returning(textResult({ status: "connected" }), 5));
|
|
524
|
+
await log.run(
|
|
525
|
+
"connect_to_server",
|
|
526
|
+
args,
|
|
527
|
+
returning(errorResult(`login someone with ${password} failed`), 5),
|
|
528
|
+
);
|
|
529
|
+
await expect(
|
|
530
|
+
log.run(
|
|
531
|
+
"connect_to_server",
|
|
532
|
+
args,
|
|
533
|
+
throwing(
|
|
534
|
+
new Error(`Cannot connect with ${password}`, {
|
|
535
|
+
cause: new Error(`refused: ${password}`),
|
|
536
|
+
}),
|
|
537
|
+
5,
|
|
538
|
+
),
|
|
539
|
+
),
|
|
540
|
+
).rejects.toThrow();
|
|
541
|
+
await log.run("await_block_done", args, returning(timedOutResult({ status: password }), 5));
|
|
542
|
+
|
|
543
|
+
// Every kind of line was written: one per call, two failures, three slow calls, one timeout.
|
|
544
|
+
expect(lines.map((line) => line.level).toSorted()).toEqual([
|
|
545
|
+
...Array<string>(2).fill("error"),
|
|
546
|
+
...Array<string>(4).fill("info"),
|
|
547
|
+
...Array<string>(4).fill("warn"),
|
|
548
|
+
]);
|
|
549
|
+
const written = everything();
|
|
550
|
+
expect(written).not.toContain(password);
|
|
551
|
+
expect(written).toContain("login someone with <secret> failed");
|
|
552
|
+
expect(written).toContain("Cannot connect with <secret>");
|
|
553
|
+
expect(written).toContain("refused: <secret>");
|
|
554
|
+
expect(written).toContain("argKeys=projectId,login,password");
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
test("is removed before the text is cut, so no part of it is left", async () => {
|
|
558
|
+
const { log, at, everything, returning } = logOnManualClock();
|
|
559
|
+
|
|
560
|
+
await log.run(
|
|
561
|
+
"connect_to_server",
|
|
562
|
+
{ password },
|
|
563
|
+
returning(errorResult(`${"z".repeat(290)}${password}`)),
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
expect(everything()).not.toContain(password.slice(0, 8));
|
|
567
|
+
expect(parse(at("error")[0] ?? "").error).toBe(`${"z".repeat(290)}<secret>`);
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
test("is found by its name; other arguments and nested keys are left", async () => {
|
|
571
|
+
const { log, everything, returning } = logOnManualClock();
|
|
572
|
+
|
|
573
|
+
await log.run(
|
|
574
|
+
"set_block_data",
|
|
575
|
+
{ token: "tok-123456", data: { password: "nested-value" }, password: 17 },
|
|
576
|
+
returning(errorResult("tok-123456 nested-value 17")),
|
|
577
|
+
);
|
|
578
|
+
|
|
579
|
+
expect(everything()).toContain("\n<secret> nested-value 17");
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
describe("a secret with a backslash or a quote", () => {
|
|
584
|
+
const passwords = ["back\\sl\\ash-pass", "it's-a-pass", 'say-"hi"-pass', `a\\b'c"d\`e-pass`];
|
|
585
|
+
|
|
586
|
+
test.each(passwords)(
|
|
587
|
+
"%s never reaches any line, escaped or not, from an error's properties and its cause",
|
|
588
|
+
async (password) => {
|
|
589
|
+
const { log, everything, returning, throwing } = logOnManualClock();
|
|
590
|
+
const args = { login: "someone", password };
|
|
591
|
+
|
|
592
|
+
await log.run(
|
|
593
|
+
"connect_to_server",
|
|
594
|
+
args,
|
|
595
|
+
returning(errorResult(JSON.stringify({ login: "someone", password }))),
|
|
596
|
+
);
|
|
597
|
+
await expect(
|
|
598
|
+
log.run(
|
|
599
|
+
"connect_to_server",
|
|
600
|
+
args,
|
|
601
|
+
throwing(
|
|
602
|
+
Object.assign(
|
|
603
|
+
new Error(`Cannot connect with ${password}`, {
|
|
604
|
+
cause: Object.assign(new Error(`refused: ${password}`), {
|
|
605
|
+
request: { password },
|
|
606
|
+
}),
|
|
607
|
+
}),
|
|
608
|
+
{ password, details: { password } },
|
|
609
|
+
),
|
|
610
|
+
),
|
|
611
|
+
),
|
|
612
|
+
).rejects.toThrow();
|
|
613
|
+
|
|
614
|
+
const written = everything();
|
|
615
|
+
for (const form of [
|
|
616
|
+
password,
|
|
617
|
+
JSON.stringify(password).slice(1, -1),
|
|
618
|
+
inspect(password).slice(1, -1),
|
|
619
|
+
])
|
|
620
|
+
expect(written).not.toContain(form);
|
|
621
|
+
// No part of it is left either.
|
|
622
|
+
expect(written).not.toContain("-pass");
|
|
623
|
+
expect(written).toContain("Cannot connect with <secret>");
|
|
624
|
+
expect(written).toContain("refused: <secret>");
|
|
625
|
+
},
|
|
626
|
+
);
|
|
627
|
+
|
|
628
|
+
test("given to the log is replaced in an error's properties too", () => {
|
|
629
|
+
const secret = "se\\cr'et\"value";
|
|
630
|
+
const { log, everything } = logOnManualClock({ secrets: [secret] });
|
|
631
|
+
|
|
632
|
+
log.sdkError(Object.assign(new Error(`bad /${secret}/mcp`), { url: `/${secret}/mcp` }));
|
|
633
|
+
|
|
634
|
+
expect(everything()).not.toContain("value");
|
|
635
|
+
expect(everything()).toContain("/<secret>/mcp");
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
describe("a secret given to the log", () => {
|
|
640
|
+
test("is replaced in every line, wherever it came from", async () => {
|
|
641
|
+
const secret = "0123456789abcdef0123456789abcdef";
|
|
642
|
+
const { log, lines, everything, returning, throwing } = logOnManualClock({
|
|
643
|
+
secrets: [secret],
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
await log.run(
|
|
647
|
+
"get_project_overview",
|
|
648
|
+
{ projectId: secret },
|
|
649
|
+
returning(errorResult(`at /${secret}/`)),
|
|
650
|
+
);
|
|
651
|
+
await expect(
|
|
652
|
+
log.run("ping", undefined, throwing(new Error(`http://127.0.0.1:1/${secret}/mcp`))),
|
|
653
|
+
).rejects.toThrow(secret);
|
|
654
|
+
log.sdkError(new Error(`bad request to /${secret}/mcp`));
|
|
655
|
+
log.requestFailed("POST", 500, new Error(`cannot serve /${secret}/mcp`));
|
|
656
|
+
|
|
657
|
+
expect(lines).toHaveLength(6);
|
|
658
|
+
expect(everything()).not.toContain(secret);
|
|
659
|
+
expect(everything()).toContain("<secret>");
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
test("is replaced before a text or an id is cut, so no part of it is left", async () => {
|
|
663
|
+
const secret = "5ec7e75ec7e75ec7e75ec7e75ec7e7aa";
|
|
664
|
+
const { log, everything, returning, throwing } = logOnManualClock({ secrets: [secret] });
|
|
665
|
+
const message = `${"x".repeat(280)} /${secret}/mcp`;
|
|
666
|
+
|
|
667
|
+
log.sdkError(new Error(message));
|
|
668
|
+
log.requestFailed("POST", 500, new Error(message));
|
|
669
|
+
await expect(log.run("ping", undefined, throwing(new Error(message)))).rejects.toThrow();
|
|
670
|
+
await log.run(
|
|
671
|
+
"get_project_overview",
|
|
672
|
+
{ projectId: `${"y".repeat(190)}${secret}` },
|
|
673
|
+
returning(textResult(1)),
|
|
674
|
+
);
|
|
675
|
+
|
|
676
|
+
expect(everything()).not.toContain(secret.slice(0, 8));
|
|
677
|
+
expect(everything()).toContain(`${"x".repeat(280)} /<secret>/mcp`);
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
describe("a logger that returns a promise", () => {
|
|
682
|
+
test("that rejects does not make a rejection nobody handles", async () => {
|
|
683
|
+
const unhandled: unknown[] = [];
|
|
684
|
+
const record = (reason: unknown) => unhandled.push(reason);
|
|
685
|
+
process.on("unhandledRejection", record);
|
|
686
|
+
try {
|
|
687
|
+
const rejecting = async () => {
|
|
688
|
+
throw new Error("the log file is gone");
|
|
689
|
+
};
|
|
690
|
+
const log = new ServerLog({
|
|
691
|
+
logger: { info: rejecting, warn: rejecting, error: rejecting },
|
|
692
|
+
instanceId,
|
|
693
|
+
slowCallMs: -1,
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
const ok = textResult({ ok: true });
|
|
697
|
+
expect(await log.run("ping", undefined, async () => ok)).toBe(ok);
|
|
698
|
+
await log.run("ping", undefined, async () => errorResult("no"));
|
|
699
|
+
log.sdkError(new Error("no"));
|
|
700
|
+
// Node reports an unhandled rejection after the microtasks of the turn have run.
|
|
701
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
702
|
+
|
|
703
|
+
expect(unhandled).toEqual([]);
|
|
704
|
+
} finally {
|
|
705
|
+
process.off("unhandledRejection", record);
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
describe("a logger that throws", () => {
|
|
711
|
+
const broken: MiLogger = {
|
|
712
|
+
info: () => {
|
|
713
|
+
throw new Error("the log file is gone");
|
|
714
|
+
},
|
|
715
|
+
warn: () => {
|
|
716
|
+
throw new Error("the log file is gone");
|
|
717
|
+
},
|
|
718
|
+
error: () => {
|
|
719
|
+
throw new Error("the log file is gone");
|
|
720
|
+
},
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
test("changes neither what a call returns nor what it throws", async () => {
|
|
724
|
+
const log = new ServerLog({ logger: broken, instanceId, slowCallMs: -1 });
|
|
725
|
+
const ok = textResult({ ok: true });
|
|
726
|
+
const failed = errorResult("no");
|
|
727
|
+
const thrown = new Error("thrown by the handler");
|
|
728
|
+
|
|
729
|
+
expect(await log.run("ping", undefined, async () => ok)).toBe(ok);
|
|
730
|
+
expect(await log.run("ping", undefined, async () => failed)).toBe(failed);
|
|
731
|
+
await expect(
|
|
732
|
+
log.run("ping", undefined, async () => {
|
|
733
|
+
throw thrown;
|
|
734
|
+
}),
|
|
735
|
+
).rejects.toBe(thrown);
|
|
736
|
+
expect(
|
|
737
|
+
await log.run("await_block_done", {}, async () => timedOutResult({ status: "Running" })),
|
|
738
|
+
).toEqual(timedOutResult({ status: "Running" }));
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
test("does not stop the lines of the other levels", async () => {
|
|
742
|
+
const recorded = recordingLogger();
|
|
743
|
+
const log = new ServerLog({
|
|
744
|
+
logger: { ...recorded.logger, info: broken.info },
|
|
745
|
+
instanceId,
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
expect(await log.run("ping", undefined, async () => errorResult("no"))).toEqual(
|
|
749
|
+
errorResult("no"),
|
|
750
|
+
);
|
|
751
|
+
|
|
752
|
+
expect(recorded.lines.map((line) => line.level)).toEqual(["error"]);
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
test("does not make the server's own lines throw", () => {
|
|
756
|
+
const log = new ServerLog({ logger: broken, instanceId });
|
|
757
|
+
|
|
758
|
+
expect(() => {
|
|
759
|
+
log.serverStarted(4200, "1.0.0");
|
|
760
|
+
log.sdkError(new Error("no"));
|
|
761
|
+
log.requestFailed("POST", 500, new Error("no"));
|
|
762
|
+
log.serverStopped(4200);
|
|
763
|
+
}).not.toThrow();
|
|
764
|
+
});
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
describe("what cannot be measured", () => {
|
|
768
|
+
test("costs a field, not the call", async () => {
|
|
769
|
+
const loop: Record<string, unknown> = { projectId: "p-1" };
|
|
770
|
+
loop.self = loop;
|
|
771
|
+
const { log, at } = logOnManualClock({
|
|
772
|
+
txCounters: () => {
|
|
773
|
+
throw new Error("the client is closed");
|
|
774
|
+
},
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
const returned = await log.run("set_block_data", loop, async () => ({ size: 10n }));
|
|
778
|
+
|
|
779
|
+
expect(returned).toEqual({ size: 10n });
|
|
780
|
+
expect(at("info")).toEqual([
|
|
781
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=set_block_data project=p-1 ms=0 outcome=ok inflight=0",
|
|
782
|
+
]);
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
test("counters that fail when the call ends cost only their field", async () => {
|
|
786
|
+
let reads = 0;
|
|
787
|
+
const { log, at } = logOnManualClock({
|
|
788
|
+
txCounters: () => {
|
|
789
|
+
if (reads++ > 0) throw new Error("the client is closed");
|
|
790
|
+
return { client: {}, conflicts: 0 };
|
|
791
|
+
},
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
await log.run("disconnect", {}, async () => "done");
|
|
795
|
+
|
|
796
|
+
expect(at("info")).toEqual([
|
|
797
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=disconnect argsBytes=2 ms=0 outcome=ok inflight=0",
|
|
798
|
+
]);
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
test("a clock that fails leaves the duration out", async () => {
|
|
802
|
+
const recorded = recordingLogger();
|
|
803
|
+
const log = new ServerLog({
|
|
804
|
+
logger: recorded.logger,
|
|
805
|
+
instanceId,
|
|
806
|
+
now: () => {
|
|
807
|
+
throw new Error("no clock");
|
|
808
|
+
},
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
expect(await log.run("ping", undefined, async () => "pong")).toBe("pong");
|
|
812
|
+
|
|
813
|
+
expect(recorded.at("info")).toEqual([
|
|
814
|
+
"mcp-call call=mcp-1a2b3c4d#1 tool=ping argsBytes=0 outcome=ok inflight=0",
|
|
815
|
+
]);
|
|
816
|
+
});
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
describe("the server's own lines", () => {
|
|
820
|
+
test("say where it listens without the secret, and how many calls it served", async () => {
|
|
821
|
+
const { log, lines } = logOnManualClock();
|
|
822
|
+
|
|
823
|
+
log.serverStarted(4200, "49.0.0");
|
|
824
|
+
await log.run("ping", undefined, async () => "pong");
|
|
825
|
+
log.serverStopped(4200);
|
|
826
|
+
|
|
827
|
+
expect(lines[0]).toEqual({
|
|
828
|
+
level: "info",
|
|
829
|
+
text: 'mcp-server-start id=mcp-1a2b3c4d port=4200 url="http://127.0.0.1:4200/<secret>/mcp" version=49.0.0',
|
|
830
|
+
});
|
|
831
|
+
expect(lines[2]).toEqual({
|
|
832
|
+
level: "info",
|
|
833
|
+
text: "mcp-server-stop id=mcp-1a2b3c4d port=4200 calls=1 inflight=0",
|
|
834
|
+
});
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
test("write a failure outside a tool handler with its causes", () => {
|
|
838
|
+
const { log, lines } = logOnManualClock();
|
|
839
|
+
const error = new Error("Cannot serve", { cause: new Error("stream closed") });
|
|
840
|
+
|
|
841
|
+
log.requestFailed("POST", 500, error);
|
|
842
|
+
log.requestFailed("POST", undefined, "torn down");
|
|
843
|
+
log.sdkError(error);
|
|
844
|
+
|
|
845
|
+
expect(lines.map((line) => line.level)).toEqual(["error", "error", "error"]);
|
|
846
|
+
expect(lines[0]?.text).toMatch(
|
|
847
|
+
/^mcp-request-failed id=mcp-1a2b3c4d method=POST status=500 error="Cannot serve"\nError: Cannot serve\n\s+at [^]*\n\s+\[cause\]: Error: stream closed\n\s+at /,
|
|
848
|
+
);
|
|
849
|
+
expect(lines[1]?.text).toBe(
|
|
850
|
+
'mcp-request-failed id=mcp-1a2b3c4d method=POST error="torn down"\ntorn down',
|
|
851
|
+
);
|
|
852
|
+
expect(lines[2]?.text).toMatch(
|
|
853
|
+
/^mcp-sdk-error id=mcp-1a2b3c4d error="Cannot serve"\nError: Cannot serve\n[^]*\n\s+\[cause\]: Error: stream closed\n/,
|
|
854
|
+
);
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
describe("fields", () => {
|
|
859
|
+
test("are written in order, without the undefined ones, quoted only when needed", () => {
|
|
860
|
+
expect(
|
|
861
|
+
fields({
|
|
862
|
+
call: "mcp-1a2b3c4d#57",
|
|
863
|
+
project: "NG:0x35B1EB",
|
|
864
|
+
block: undefined,
|
|
865
|
+
ms: 0,
|
|
866
|
+
error: 'not "found"\there',
|
|
867
|
+
keys: "",
|
|
868
|
+
}),
|
|
869
|
+
).toBe('call=mcp-1a2b3c4d#57 project=NG:0x35B1EB ms=0 error="not \\"found\\"\\there" keys=""');
|
|
870
|
+
});
|
|
871
|
+
});
|