@milaboratories/pl-mcp-server 53.1.0 → 55.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.
Files changed (208) hide show
  1. package/README.md +30 -37
  2. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  3. package/dist/agent-tables.cjs +116 -0
  4. package/dist/agent-tables.cjs.map +1 -0
  5. package/dist/agent-tables.js +115 -0
  6. package/dist/agent-tables.js.map +1 -0
  7. package/dist/instructions.cjs +16 -12
  8. package/dist/instructions.cjs.map +1 -1
  9. package/dist/instructions.js +16 -12
  10. package/dist/instructions.js.map +1 -1
  11. package/dist/package.cjs +1 -1
  12. package/dist/package.js +1 -1
  13. package/dist/project-holds.cjs +122 -0
  14. package/dist/project-holds.cjs.map +1 -0
  15. package/dist/project-holds.js +122 -0
  16. package/dist/project-holds.js.map +1 -0
  17. package/dist/project-list.cjs +16 -0
  18. package/dist/project-list.cjs.map +1 -0
  19. package/dist/project-list.js +16 -0
  20. package/dist/project-list.js.map +1 -0
  21. package/dist/server-log.cjs +48 -18
  22. package/dist/server-log.cjs.map +1 -1
  23. package/dist/server-log.js +48 -19
  24. package/dist/server-log.js.map +1 -1
  25. package/dist/server.cjs +71 -18
  26. package/dist/server.cjs.map +1 -1
  27. package/dist/server.d.ts +37 -24
  28. package/dist/server.d.ts.map +1 -1
  29. package/dist/server.js +71 -19
  30. package/dist/server.js.map +1 -1
  31. package/dist/tools/agent-tables.cjs +424 -0
  32. package/dist/tools/agent-tables.cjs.map +1 -0
  33. package/dist/tools/agent-tables.js +417 -0
  34. package/dist/tools/agent-tables.js.map +1 -0
  35. package/dist/tools/await.cjs +216 -72
  36. package/dist/tools/await.cjs.map +1 -1
  37. package/dist/tools/await.js +212 -74
  38. package/dist/tools/await.js.map +1 -1
  39. package/dist/tools/block-action-context.cjs +14 -9
  40. package/dist/tools/block-action-context.cjs.map +1 -1
  41. package/dist/tools/block-action-context.js +14 -9
  42. package/dist/tools/block-action-context.js.map +1 -1
  43. package/dist/tools/block-api.cjs +17 -2
  44. package/dist/tools/block-api.cjs.map +1 -1
  45. package/dist/tools/block-api.js +17 -3
  46. package/dist/tools/block-api.js.map +1 -1
  47. package/dist/tools/block-params.cjs +275 -6
  48. package/dist/tools/block-params.cjs.map +1 -1
  49. package/dist/tools/block-params.js +273 -8
  50. package/dist/tools/block-params.js.map +1 -1
  51. package/dist/tools/block-state.cjs +343 -57
  52. package/dist/tools/block-state.cjs.map +1 -1
  53. package/dist/tools/block-state.js +337 -61
  54. package/dist/tools/block-state.js.map +1 -1
  55. package/dist/tools/block-storage.cjs +153 -0
  56. package/dist/tools/block-storage.cjs.map +1 -0
  57. package/dist/tools/block-storage.js +147 -0
  58. package/dist/tools/block-storage.js.map +1 -0
  59. package/dist/tools/block-updates.cjs +405 -0
  60. package/dist/tools/block-updates.cjs.map +1 -0
  61. package/dist/tools/block-updates.js +397 -0
  62. package/dist/tools/block-updates.js.map +1 -0
  63. package/dist/tools/blocks.cjs +199 -76
  64. package/dist/tools/blocks.cjs.map +1 -1
  65. package/dist/tools/blocks.js +193 -76
  66. package/dist/tools/blocks.js.map +1 -1
  67. package/dist/tools/data-query.cjs +470 -101
  68. package/dist/tools/data-query.cjs.map +1 -1
  69. package/dist/tools/data-query.js +465 -106
  70. package/dist/tools/data-query.js.map +1 -1
  71. package/dist/tools/errors.cjs +21 -0
  72. package/dist/tools/errors.cjs.map +1 -0
  73. package/dist/tools/errors.js +20 -0
  74. package/dist/tools/errors.js.map +1 -0
  75. package/dist/tools/files.cjs +643 -0
  76. package/dist/tools/files.cjs.map +1 -0
  77. package/dist/tools/files.js +613 -0
  78. package/dist/tools/files.js.map +1 -0
  79. package/dist/tools/ids.cjs +72 -0
  80. package/dist/tools/ids.cjs.map +1 -1
  81. package/dist/tools/ids.js +63 -2
  82. package/dist/tools/ids.js.map +1 -1
  83. package/dist/tools/imports.cjs +195 -0
  84. package/dist/tools/imports.cjs.map +1 -0
  85. package/dist/tools/imports.js +186 -0
  86. package/dist/tools/imports.js.map +1 -0
  87. package/dist/tools/logs.cjs +111 -30
  88. package/dist/tools/logs.cjs.map +1 -1
  89. package/dist/tools/logs.js +108 -31
  90. package/dist/tools/logs.js.map +1 -1
  91. package/dist/tools/paging.cjs +27 -0
  92. package/dist/tools/paging.cjs.map +1 -0
  93. package/dist/tools/paging.js +26 -0
  94. package/dist/tools/paging.js.map +1 -0
  95. package/dist/tools/params-merge.cjs +106 -0
  96. package/dist/tools/params-merge.cjs.map +1 -0
  97. package/dist/tools/params-merge.js +106 -0
  98. package/dist/tools/params-merge.js.map +1 -0
  99. package/dist/tools/project-template.cjs +75 -0
  100. package/dist/tools/project-template.cjs.map +1 -0
  101. package/dist/tools/project-template.js +72 -0
  102. package/dist/tools/project-template.js.map +1 -0
  103. package/dist/tools/projects.cjs +68 -38
  104. package/dist/tools/projects.cjs.map +1 -1
  105. package/dist/tools/projects.js +69 -40
  106. package/dist/tools/projects.js.map +1 -1
  107. package/dist/tools/readiness.cjs +104 -0
  108. package/dist/tools/readiness.cjs.map +1 -0
  109. package/dist/tools/readiness.js +99 -0
  110. package/dist/tools/readiness.js.map +1 -0
  111. package/dist/tools/result-pool.cjs +260 -0
  112. package/dist/tools/result-pool.cjs.map +1 -0
  113. package/dist/tools/result-pool.js +252 -0
  114. package/dist/tools/result-pool.js.map +1 -0
  115. package/dist/tools/sandbox.cjs +112 -14
  116. package/dist/tools/sandbox.cjs.map +1 -1
  117. package/dist/tools/sandbox.js +110 -15
  118. package/dist/tools/sandbox.js.map +1 -1
  119. package/dist/tools/sdk-types.cjs +24 -0
  120. package/dist/tools/sdk-types.cjs.map +1 -0
  121. package/dist/tools/sdk-types.js +24 -0
  122. package/dist/tools/sdk-types.js.map +1 -0
  123. package/dist/tools/settle.cjs +155 -0
  124. package/dist/tools/settle.cjs.map +1 -0
  125. package/dist/tools/settle.js +148 -0
  126. package/dist/tools/settle.js.map +1 -0
  127. package/dist/tools/tokens.cjs +49 -8
  128. package/dist/tools/tokens.cjs.map +1 -1
  129. package/dist/tools/tokens.js +45 -9
  130. package/dist/tools/tokens.js.map +1 -1
  131. package/dist/tools/types.cjs +62 -11
  132. package/dist/tools/types.cjs.map +1 -1
  133. package/dist/tools/types.js +60 -12
  134. package/dist/tools/types.js.map +1 -1
  135. package/dist/tools/value-outline.cjs +119 -0
  136. package/dist/tools/value-outline.cjs.map +1 -0
  137. package/dist/tools/value-outline.js +117 -0
  138. package/dist/tools/value-outline.js.map +1 -0
  139. package/package.json +9 -8
  140. package/src/agent-tables.test.ts +246 -0
  141. package/src/agent-tables.ts +145 -0
  142. package/src/instructions.ts +16 -12
  143. package/src/project-holds.test.ts +362 -0
  144. package/src/project-holds.ts +207 -0
  145. package/src/project-list.test.ts +38 -0
  146. package/src/project-list.ts +25 -0
  147. package/src/server-log.test.ts +71 -6
  148. package/src/server-log.ts +58 -27
  149. package/src/server.logging.test.ts +46 -51
  150. package/src/server.test.ts +166 -22
  151. package/src/server.ts +97 -37
  152. package/src/tools/agent-tables.test.ts +650 -0
  153. package/src/tools/agent-tables.ts +673 -0
  154. package/src/tools/await.test.ts +287 -0
  155. package/src/tools/await.ts +393 -122
  156. package/src/tools/block-action-context.ts +17 -13
  157. package/src/tools/block-api.ts +19 -4
  158. package/src/tools/block-params.test.ts +828 -0
  159. package/src/tools/block-params.ts +626 -10
  160. package/src/tools/block-state.test.ts +480 -0
  161. package/src/tools/block-state.ts +678 -83
  162. package/src/tools/block-storage.test.ts +270 -0
  163. package/src/tools/block-storage.ts +195 -0
  164. package/src/tools/block-updates.test.ts +605 -0
  165. package/src/tools/block-updates.ts +751 -0
  166. package/src/tools/blocks.test.ts +169 -22
  167. package/src/tools/blocks.ts +354 -113
  168. package/src/tools/data-query.test.ts +641 -0
  169. package/src/tools/data-query.ts +839 -145
  170. package/src/tools/errors.test.ts +33 -0
  171. package/src/tools/errors.ts +23 -0
  172. package/src/tools/files.test.ts +842 -0
  173. package/src/tools/files.ts +990 -0
  174. package/src/tools/ids.test.ts +87 -0
  175. package/src/tools/ids.ts +89 -1
  176. package/src/tools/imports.ts +304 -0
  177. package/src/tools/logs.test.ts +70 -0
  178. package/src/tools/logs.ts +154 -44
  179. package/src/tools/paging.test.ts +39 -0
  180. package/src/tools/paging.ts +39 -0
  181. package/src/tools/params-merge.test.ts +274 -0
  182. package/src/tools/params-merge.ts +149 -0
  183. package/src/tools/project-template.test.ts +230 -0
  184. package/src/tools/project-template.ts +135 -0
  185. package/src/tools/projects.test.ts +122 -0
  186. package/src/tools/projects.ts +136 -48
  187. package/src/tools/readiness.test.ts +195 -0
  188. package/src/tools/readiness.ts +189 -0
  189. package/src/tools/result-pool.test.ts +377 -0
  190. package/src/tools/result-pool.ts +483 -0
  191. package/src/tools/sandbox.test.ts +128 -10
  192. package/src/tools/sandbox.ts +132 -18
  193. package/src/tools/sdk-types.ts +54 -0
  194. package/src/tools/settle.test.ts +340 -0
  195. package/src/tools/settle.ts +312 -0
  196. package/src/tools/tokens.ts +74 -9
  197. package/src/tools/types.ts +84 -12
  198. package/src/tools/value-outline.ts +143 -0
  199. package/dist/tools/screenshot.cjs +0 -44
  200. package/dist/tools/screenshot.cjs.map +0 -1
  201. package/dist/tools/screenshot.js +0 -44
  202. package/dist/tools/screenshot.js.map +0 -1
  203. package/dist/tools/ui-interaction.cjs +0 -140
  204. package/dist/tools/ui-interaction.cjs.map +0 -1
  205. package/dist/tools/ui-interaction.js +0 -140
  206. package/dist/tools/ui-interaction.js.map +0 -1
  207. package/src/tools/screenshot.ts +0 -61
  208. package/src/tools/ui-interaction.ts +0 -184
@@ -0,0 +1,480 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { asBlockId, type BlockCodeFeatureFlags } from "@milaboratories/pl-model-common";
3
+ import { ModelAPIVersionMismatchError } from "@milaboratories/pl-errors";
4
+ import type {
5
+ BlockDataProject,
6
+ ChangeProject,
7
+ SetBlockDataInput,
8
+ WriteOutcome,
9
+ } from "./block-state";
10
+ import { blockStatus, settleAfterChange, writeAndSettle, writeBlockData } from "./block-state";
11
+ import { RESULT_CHAR_LIMIT, textResult } from "./types";
12
+ import { UNSUPPORTED_BLOCK_API_TEXT } from "./block-api";
13
+ import { asPluginHandle } from "./ids";
14
+
15
+ type Payload = { operation: string; value: unknown; pluginId?: string };
16
+
17
+ /** Raw block storage of a BlockModelV3 block, in the format the SDK writes. */
18
+ function storage(data: unknown, plugins: Record<string, unknown> = {}) {
19
+ return {
20
+ __pl_a7f3e2b9__: "v1",
21
+ __dataVersion: "v1",
22
+ __data: data,
23
+ __pluginRegistry: Object.fromEntries(Object.keys(plugins).map((h) => [h, `${h}Plugin`])),
24
+ __plugins: Object.fromEntries(
25
+ Object.entries(plugins).map(([h, d]) => [h, { __dataVersion: "v1", __data: d }]),
26
+ ),
27
+ };
28
+ }
29
+
30
+ /** The feature flags of a BlockModelV3 block, as the project overview gives them. */
31
+ const V3_FLAGS: BlockCodeFeatureFlags = { requiresModelAPIVersion: 2 };
32
+
33
+ /** A project with one block `b1` whose storage is `blockStorage`. It records the storage writes. */
34
+ function stubProject(
35
+ blockStorage: unknown,
36
+ options: {
37
+ mutate?: (payload: Payload) => void;
38
+ /**
39
+ * The block's feature flags in the overview; a BlockModelV3 block's when not given, none
40
+ * (the pack not loaded yet) when given as `undefined`.
41
+ */
42
+ featureFlags?: BlockCodeFeatureFlags | undefined;
43
+ } = {},
44
+ ) {
45
+ const writes: Payload[] = [];
46
+ const project: BlockDataProject = {
47
+ overview: {
48
+ getValue: async () => ({
49
+ blocks: [
50
+ { id: "b1", featureFlags: "featureFlags" in options ? options.featureFlags : V3_FLAGS },
51
+ ],
52
+ }),
53
+ },
54
+ getBlockState: () => ({ getValue: async () => ({ blockStorage }) }),
55
+ mutateBlockStorage: async (_blockId, payload) => {
56
+ options.mutate?.(payload);
57
+ writes.push(payload);
58
+ },
59
+ };
60
+ return { project, writes };
61
+ }
62
+
63
+ const author = { authorId: "test", localVersion: 1 };
64
+
65
+ function input(
66
+ overrides: Partial<SetBlockDataInput>,
67
+ ): Omit<SetBlockDataInput, "projectId" | "wait" | "timeout"> {
68
+ return { blockId: asBlockId("b1"), ...overrides };
69
+ }
70
+
71
+ /** What a written outcome says: whether the value was unchanged, and the changed paths. */
72
+ function written(outcome: WriteOutcome) {
73
+ if (outcome.kind !== "written") throw new Error(outcome.result.content[0].text);
74
+ return { unchanged: outcome.unchanged, ...outcome.changes };
75
+ }
76
+
77
+ /** The error text of a refused write. */
78
+ function errorText(outcome: WriteOutcome): string {
79
+ if (outcome.kind !== "refused") throw new Error("The write was not refused");
80
+ return outcome.result.content[0].text;
81
+ }
82
+
83
+ describe("set_block_data", () => {
84
+ test("a block on block API 1, refused by the middle layer, gets the shared error", async () => {
85
+ const { project, writes } = stubProject(storage({ a: 1 }), {
86
+ mutate: () => {
87
+ throw new ModelAPIVersionMismatchError("b1", 2, 1);
88
+ },
89
+ });
90
+ const result = await writeBlockData(project, author, input({ data: { a: 2 } }));
91
+ expect(errorText(result)).toBe(UNSUPPORTED_BLOCK_API_TEXT);
92
+ expect(writes).toEqual([]);
93
+ });
94
+
95
+ test("a block on block API 1 gets the shared error before its data is looked at", async () => {
96
+ // Storage of block API 1: the data the agent sends would drop its top-level keys, which is
97
+ // refused on its own; the block API is checked first.
98
+ let mutated = false;
99
+ const { project, writes } = stubProject(
100
+ { args: { a: 1 }, uiState: {} },
101
+ {
102
+ featureFlags: {},
103
+ mutate: () => {
104
+ mutated = true;
105
+ },
106
+ },
107
+ );
108
+ const result = await writeBlockData(project, author, input({ data: { a: 2 } }));
109
+ expect(errorText(result)).toBe(UNSUPPORTED_BLOCK_API_TEXT);
110
+ expect(writes).toEqual([]);
111
+ expect(mutated).toBe(false);
112
+ });
113
+
114
+ test("a block whose pack is not loaded yet is left to the middle layer's check", async () => {
115
+ const { project, writes } = stubProject(storage({ a: 1 }), { featureFlags: undefined });
116
+ const result = await writeBlockData(project, author, input({ data: { a: 2 } }));
117
+ expect(written(result).unchanged).toBe(false);
118
+ expect(writes).toHaveLength(1);
119
+ });
120
+
121
+ test("writes the data whole: an `args` key is not unwrapped", async () => {
122
+ const { project, writes } = stubProject(storage({ args: { a: 0 }, other: 1 }));
123
+ const result = await writeBlockData(
124
+ project,
125
+ author,
126
+ input({ data: { args: { a: 1 }, other: 2 } }),
127
+ );
128
+ expect(written(result)).toEqual({
129
+ unchanged: false,
130
+ changes: [
131
+ { path: "args.a", op: "replace", from: 0, to: 1 },
132
+ { path: "other", op: "replace", from: 1, to: 2 },
133
+ ],
134
+ });
135
+ expect(writes).toEqual([
136
+ { operation: "update-block-data", value: { args: { a: 1 }, other: 2 } },
137
+ ]);
138
+ });
139
+
140
+ test("refuses a write that drops top-level keys", async () => {
141
+ const { project, writes } = stubProject(storage({ numbers: [1], labels: [], description: "" }));
142
+ const result = await writeBlockData(project, author, input({ data: { numbers: [10, 20] } }));
143
+ const text = errorText(result);
144
+ expect(text).toContain("labels, description");
145
+ expect(text).toContain("removeKeys");
146
+ expect(writes).toEqual([]);
147
+ });
148
+
149
+ test("removeKeys lets the named keys go", async () => {
150
+ const { project, writes } = stubProject(storage({ a: 1, b: 2, c: 3 }));
151
+ const refused = await writeBlockData(
152
+ project,
153
+ author,
154
+ input({ data: { a: 1 }, removeKeys: ["b"] }),
155
+ );
156
+ expect(errorText(refused)).toContain(": c.");
157
+ const result = await writeBlockData(
158
+ project,
159
+ author,
160
+ input({ data: { a: 1 }, removeKeys: ["b", "c"] }),
161
+ );
162
+ expect(written(result).unchanged).toBe(false);
163
+ expect(writes).toEqual([{ operation: "update-block-data", value: { a: 1 } }]);
164
+ });
165
+
166
+ test("an equal value is not written", async () => {
167
+ const { project, writes } = stubProject(storage({ a: 1, b: [1, 2] }));
168
+ const result = await writeBlockData(project, author, input({ data: { b: [1, 2], a: 1 } }));
169
+ expect(written(result)).toEqual({ unchanged: true, changes: [] });
170
+ expect(writes).toEqual([]);
171
+ });
172
+
173
+ test("an update expression computes the new data from the current one", async () => {
174
+ const { project, writes } = stubProject(storage({ numbers: [1], labels: ["x"] }));
175
+ const result = await writeBlockData(
176
+ project,
177
+ author,
178
+ input({ update: "({ ...data, numbers: [...data.numbers, 2] })" }),
179
+ );
180
+ expect(written(result)).toMatchObject({
181
+ unchanged: false,
182
+ changes: [{ path: "numbers[1]", op: "add", to: 2 }],
183
+ });
184
+ expect(writes).toEqual([
185
+ { operation: "update-block-data", value: { numbers: [1, 2], labels: ["x"] } },
186
+ ]);
187
+ });
188
+
189
+ test("an update expression that changes `data` in place writes every change", async () => {
190
+ const { project, writes } = stubProject(
191
+ storage({ a: 1, gone: true, nested: { b: 1 }, list: [1] }),
192
+ );
193
+ const result = await writeBlockData(
194
+ project,
195
+ author,
196
+ input({
197
+ update:
198
+ "(() => { data.a = 2; data.nested.b = 2; delete data.gone; data.added = 3; " +
199
+ "data.list.push(2); return data; })()",
200
+ removeKeys: ["gone"],
201
+ }),
202
+ );
203
+ expect(written(result)).toMatchObject({ unchanged: false });
204
+ expect(writes).toEqual([
205
+ {
206
+ operation: "update-block-data",
207
+ value: { a: 2, nested: { b: 2 }, list: [1, 2], added: 3 },
208
+ },
209
+ ]);
210
+ });
211
+
212
+ test("data and update together, or neither, are refused", async () => {
213
+ const { project, writes } = stubProject(storage({ a: 1 }));
214
+ errorText(await writeBlockData(project, author, input({})));
215
+ errorText(await writeBlockData(project, author, input({ data: { a: 1 }, update: "data" })));
216
+ expect(writes).toEqual([]);
217
+ });
218
+
219
+ test("an object is not replaced by a value of another type", async () => {
220
+ const { project, writes } = stubProject(storage({ a: 1 }));
221
+ expect(errorText(await writeBlockData(project, author, input({ data: [1] })))).toContain(
222
+ "must be an object",
223
+ );
224
+ expect(writes).toEqual([]);
225
+ });
226
+
227
+ test("writes a plugin's data with its handle", async () => {
228
+ const { project, writes } = stubProject(
229
+ storage({ a: 1 }, { counter: { count: 10, lastIncrement: "x" } }),
230
+ );
231
+ const result = await writeBlockData(
232
+ project,
233
+ author,
234
+ input({ pluginId: asPluginHandle("counter"), data: { count: 11, lastIncrement: "x" } }),
235
+ );
236
+ expect(written(result)).toMatchObject({
237
+ unchanged: false,
238
+ changes: [{ path: "count", op: "replace", from: 10, to: 11 }],
239
+ });
240
+ expect(writes).toEqual([
241
+ {
242
+ operation: "update-plugin-data",
243
+ pluginId: asPluginHandle("counter"),
244
+ value: { count: 11, lastIncrement: "x" },
245
+ },
246
+ ]);
247
+ });
248
+
249
+ test("the plugin data is guarded too", async () => {
250
+ const { project, writes } = stubProject(storage({}, { counter: { count: 10, last: "x" } }));
251
+ const result = await writeBlockData(
252
+ project,
253
+ author,
254
+ input({ pluginId: asPluginHandle("counter"), data: { count: 11 } }),
255
+ );
256
+ expect(errorText(result)).toContain("last");
257
+ expect(writes).toEqual([]);
258
+ });
259
+
260
+ test("an unknown plugin handle is refused and nothing is written", async () => {
261
+ const { project, writes } = stubProject(storage({ a: 1 }, { counter: { count: 1 } }));
262
+ const result = await writeBlockData(
263
+ project,
264
+ author,
265
+ input({ pluginId: asPluginHandle("nope"), data: { count: 2 } }),
266
+ );
267
+ const text = errorText(result);
268
+ expect(text).toContain('no plugin "nope"');
269
+ expect(text).toContain("counter");
270
+ expect(writes).toEqual([]);
271
+ });
272
+
273
+ test.each(["toString", "constructor", "__proto__", "hasOwnProperty"])(
274
+ "a plugin handle %s that only the prototype has is refused and nothing is written",
275
+ async (handle) => {
276
+ const { project, writes } = stubProject(storage({ a: 1 }, { counter: { count: 1 } }));
277
+ for (const call of [{ data: { count: 2 } }, { update: "({ ...data, count: 2 })" }]) {
278
+ const result = await writeBlockData(
279
+ project,
280
+ author,
281
+ input({ pluginId: asPluginHandle(handle), ...call }),
282
+ );
283
+ const text = errorText(result);
284
+ expect(text).toContain(`no plugin "${handle}"`);
285
+ expect(text).toContain("counter");
286
+ }
287
+ expect(writes).toEqual([]);
288
+ },
289
+ );
290
+
291
+ test("a plugin whose handle is a prototype key is written as any other", async () => {
292
+ const { project, writes } = stubProject(storage({ a: 1 }, { constructor: { count: 1 } }));
293
+ const result = await writeBlockData(
294
+ project,
295
+ author,
296
+ input({ pluginId: asPluginHandle("constructor"), data: { count: 2 } }),
297
+ );
298
+ expect(written(result).unchanged).toBe(false);
299
+ expect(writes).toEqual([
300
+ { operation: "update-plugin-data", pluginId: "constructor", value: { count: 2 } },
301
+ ]);
302
+ });
303
+
304
+ test("an unknown block is refused", async () => {
305
+ const { project, writes } = stubProject(storage({ a: 1 }));
306
+ const result = await writeBlockData(project, author, {
307
+ ...input({ data: {} }),
308
+ blockId: asBlockId("zz"),
309
+ });
310
+ expect(errorText(result)).toContain("not found");
311
+ expect(writes).toEqual([]);
312
+ });
313
+ });
314
+
315
+ describe("blockStatus", () => {
316
+ test("derives the status from stored fields", () => {
317
+ const base = {
318
+ calculationStatus: "NotCalculated",
319
+ stale: true,
320
+ canRun: true,
321
+ outputErrors: false,
322
+ } as const;
323
+ expect(blockStatus(base)).toBe("ready");
324
+ expect(blockStatus({ ...base, canRun: false })).toBe("blocked");
325
+ expect(blockStatus({ ...base, calculationStatus: "Running" })).toBe("running");
326
+ expect(blockStatus({ ...base, calculationStatus: "Limbo" })).toBe("limbo");
327
+ expect(blockStatus({ ...base, calculationStatus: "Done", stale: false })).toBe("up-to-date");
328
+ expect(blockStatus({ ...base, calculationStatus: "Done", stale: true })).toBe("ready");
329
+ });
330
+
331
+ test.each([
332
+ { stale: false, outputErrors: false, canRun: false, status: "up-to-date" },
333
+ { stale: false, outputErrors: true, canRun: true, status: "failed" },
334
+ { stale: false, outputErrors: true, canRun: false, status: "failed" },
335
+ { stale: true, outputErrors: false, canRun: true, status: "ready" },
336
+ { stale: true, outputErrors: true, canRun: true, status: "ready" },
337
+ { stale: true, outputErrors: true, canRun: false, status: "blocked" },
338
+ ] as const)(
339
+ "a Done block, stale $stale, outputErrors $outputErrors, canRun $canRun, is $status",
340
+ ({ status, ...fields }) => {
341
+ expect(blockStatus({ calculationStatus: "Done", ...fields })).toBe(status);
342
+ },
343
+ );
344
+
345
+ test("outputErrors of a running block or one in limbo does not change its status", () => {
346
+ const failed = { stale: false, canRun: true, outputErrors: true } as const;
347
+ expect(blockStatus({ ...failed, calculationStatus: "Running" })).toBe("running");
348
+ expect(blockStatus({ ...failed, calculationStatus: "Limbo" })).toBe("limbo");
349
+ });
350
+ });
351
+
352
+ describe("tool results", () => {
353
+ test("binary data is replaced by its size", () => {
354
+ const result = textResult({
355
+ organization: { logo: { content: new Uint8Array(24439), mimeType: "image/png" } },
356
+ });
357
+ expect(JSON.parse(result.content[0].text)).toEqual({
358
+ organization: { logo: { content: { bytes: 24439 }, mimeType: "image/png" } },
359
+ });
360
+ });
361
+
362
+ test("a result over the limit is an outline with a hint, not an error", () => {
363
+ const big = { small: 1, rows: Array.from({ length: 20000 }, (_, i) => `row-${i}`) };
364
+ const result = textResult(big, { hint: "Use transform." });
365
+ const text = result.content[0].text;
366
+ expect(text.length).toBeLessThanOrEqual(RESULT_CHAR_LIMIT);
367
+ expect("isError" in result).toBe(false);
368
+ const parsed: unknown = JSON.parse(text);
369
+ expect(parsed).toMatchObject({
370
+ resultTooLarge: true,
371
+ limit: RESULT_CHAR_LIMIT,
372
+ hint: "Use transform.",
373
+ outline: {
374
+ type: "object",
375
+ fields: { small: { type: "number" }, rows: { type: "array", length: 20000 } },
376
+ },
377
+ });
378
+ });
379
+
380
+ test("a result under the limit is the JSON itself", () => {
381
+ expect(textResult({ a: 1 }).content[0].text).toBe('{"a":1}');
382
+ });
383
+ });
384
+
385
+ describe("set_block_data and import handles", () => {
386
+ const handle = (p: string) =>
387
+ `upload://upload/${encodeURIComponent(JSON.stringify({ localPath: p }))}`;
388
+ const good = handle("/data/good.fastq");
389
+ const changed = handle("/data/changed.fastq");
390
+ /** An ls driver for which only `good` names an unchanged file. */
391
+ const ls = {
392
+ getLocalFileSize: async (h: string) => {
393
+ if (h !== good) throw new Error("File has changed since the handle was created.");
394
+ return 1;
395
+ },
396
+ };
397
+
398
+ test("a new handle whose file changed is refused by its path; nothing is written", async () => {
399
+ const { project, writes } = stubProject(storage({ files: [] }));
400
+ const result = await writeBlockData(
401
+ project,
402
+ author,
403
+ input({ update: `({ ...data, files: [${JSON.stringify(changed)}] })` }),
404
+ ls,
405
+ );
406
+ expect(errorText(result)).toMatch(/files\[0\]: File has changed.*Nothing was changed/s);
407
+ expect(writes).toEqual([]);
408
+ });
409
+
410
+ test("a stored handle whose file changed does not block a write next to it", async () => {
411
+ const { project, writes } = stubProject(storage({ files: [changed], label: "a" }));
412
+ const result = await writeBlockData(
413
+ project,
414
+ author,
415
+ input({ data: { files: [changed, good], label: "b" } }),
416
+ ls,
417
+ );
418
+ expect(result.kind).toBe("written");
419
+ expect(writes).toHaveLength(1);
420
+ });
421
+ });
422
+
423
+ describe("a write whose block cannot be read afterwards", () => {
424
+ /** A block `b1` whose storage writes land, and whose state then fails to read. */
425
+ function projectFailingAfterWrite() {
426
+ const writes: Payload[] = [];
427
+ const block = {
428
+ id: "b1",
429
+ featureFlags: V3_FLAGS,
430
+ calculationStatus: "NotCalculated" as const,
431
+ stale: true,
432
+ canRun: true,
433
+ outputErrors: false,
434
+ missingReference: false,
435
+ upstreams: [],
436
+ };
437
+ const project: BlockDataProject & ChangeProject = {
438
+ overview: { getValue: async () => ({ blocks: [block] }) },
439
+ getBlockState: () => ({
440
+ getValue: async () => ({ blockStorage: storage({ a: 1 }) }),
441
+ getFullValue: async () => {
442
+ throw new Error("backend went away");
443
+ },
444
+ awaitChange: async () => {},
445
+ }),
446
+ getBlockReadiness: () => ({ getValue: async () => undefined }),
447
+ mutateBlockStorage: async (_blockId, payload) => {
448
+ writes.push(payload);
449
+ },
450
+ };
451
+ return { project, writes };
452
+ }
453
+
454
+ test("is ok with settled: false and the error, so the agent does not write again", async () => {
455
+ const { project, writes } = projectFailingAfterWrite();
456
+ const result = await writeAndSettle(project, author, {
457
+ blockId: asBlockId("b1"),
458
+ data: { a: 2 },
459
+ });
460
+ expect("isError" in result).toBe(false);
461
+ expect(JSON.parse(result.content[0].text)).toMatchObject({
462
+ ok: true,
463
+ changes: [{ path: "a", op: "replace", from: 1, to: 2 }],
464
+ settled: false,
465
+ settleError: "backend went away",
466
+ });
467
+ expect(writes).toHaveLength(1);
468
+ });
469
+
470
+ test("settleAfterChange never throws", async () => {
471
+ const { project } = projectFailingAfterWrite();
472
+ expect(
473
+ await settleAfterChange(project, asBlockId("b1"), {
474
+ wait: true,
475
+ timeoutMs: 1000,
476
+ notePrerun: false,
477
+ }),
478
+ ).toMatchObject({ settled: false, settleError: "backend went away" });
479
+ });
480
+ });