@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,605 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { asBlockId, asProjectId } from "@milaboratories/pl-model-common";
3
+ import { ModelAPIVersionMismatchError } from "@milaboratories/pl-errors";
4
+ import type { BlockPackSpec, BlockPackSpecAny } from "@milaboratories/pl-middle-layer";
5
+ import { UNSUPPORTED_BLOCK_API_TEXT } from "./block-api";
6
+ import {
7
+ blockUpdatesOf,
8
+ listBlockUpdates,
9
+ type UpdateBlocksInput,
10
+ type UpdateBlocksOptions,
11
+ type UpdateBlocksProject,
12
+ UPDATE_BLOCKER_CHARS,
13
+ UPDATE_LABEL_CHARS,
14
+ UPDATE_READINESS_BLOCKERS,
15
+ UPDATE_REASON_CHARS,
16
+ updateBlocks,
17
+ type UpdatesOverviewBlock,
18
+ type UpdateSpec,
19
+ updateTargetInput,
20
+ } from "./block-updates";
21
+ import type { ReadinessOverviewBlock } from "./readiness";
22
+ import { progressReporter, type WaitContext } from "./settle";
23
+ import { updateBlockSpecInput } from "./blocks";
24
+ import { oneAtATime } from "./block-state";
25
+
26
+ const P = asProjectId("p");
27
+ const REGISTRY = "https://registry.test/";
28
+
29
+ type FakeBlock = UpdatesOverviewBlock & ReadinessOverviewBlock;
30
+
31
+ function registrySpec(version: string): BlockPackSpec {
32
+ return targetSpec(version);
33
+ }
34
+
35
+ /** A registry pack as update_blocks takes it in `to`. */
36
+ function targetSpec(version: string): UpdateSpec {
37
+ return {
38
+ type: "from-registry-v2",
39
+ registryUrl: REGISTRY,
40
+ id: { organization: "org", name: "block", version },
41
+ };
42
+ }
43
+
44
+ /** A block on 1.0.0 with the given suggestions; neutral defaults for the rest. */
45
+ function block(id: string, overrides: Partial<FakeBlock> = {}): FakeBlock {
46
+ return {
47
+ id,
48
+ title: `Block ${id}`,
49
+ label: `Block ${id}`,
50
+ currentBlockPack: registrySpec("1.0.0"),
51
+ updatedBlockPack: undefined,
52
+ updateSuggestions: [],
53
+ settings: {},
54
+ isIncompatibleWithRuntime: false,
55
+ calculationStatus: "NotCalculated",
56
+ stale: true,
57
+ canRun: true,
58
+ outputErrors: false,
59
+ missingReference: false,
60
+ upstreams: [],
61
+ ...overrides,
62
+ };
63
+ }
64
+
65
+ /** A block with patch 1.0.1, minor 1.1.0 and major 2.0.0 offered, 2.0.0 recommended. */
66
+ function upgradable(id: string, overrides: Partial<FakeBlock> = {}): FakeBlock {
67
+ return block(id, {
68
+ updatedBlockPack: registrySpec("2.0.0"),
69
+ updateSuggestions: [
70
+ { type: "major", update: registrySpec("2.0.0") },
71
+ { type: "minor", update: registrySpec("1.1.0") },
72
+ { type: "patch", update: registrySpec("1.0.1") },
73
+ ],
74
+ ...overrides,
75
+ });
76
+ }
77
+
78
+ type Call = { blockId: string; spec: BlockPackSpecAny; required: number | undefined };
79
+
80
+ /**
81
+ * A project of `blocks` whose `updateBlockPack` records each call and runs `outcome` for it: a
82
+ * throw is the middle layer refusing the pack. `refresh` is the overview's `refreshState`: the
83
+ * application's update check, in at once by default.
84
+ */
85
+ function fakeProject(
86
+ blocks: FakeBlock[],
87
+ outcome: (blockId: string) => Promise<void> | void = () => {},
88
+ refresh: () => Promise<void> = async () => {},
89
+ ) {
90
+ const calls: Call[] = [];
91
+ const project: UpdateBlocksProject = {
92
+ overview: { getValue: async () => ({ blocks }), refreshState: refresh },
93
+ updateBlockPack: async (blockId, spec, _resetArgs, _author, ops) => {
94
+ calls.push({ blockId, spec, required: ops?.requireModelAPIVersion });
95
+ await outcome(blockId);
96
+ },
97
+ getBlockReadiness: () => ({ getValue: async () => undefined }),
98
+ };
99
+ return { project, calls };
100
+ }
101
+
102
+ function options(overrides: Partial<UpdateBlocksOptions> = {}): UpdateBlocksOptions {
103
+ let version = 0;
104
+ return {
105
+ author: () => ({ authorId: "mcp", localVersion: ++version }),
106
+ timeoutMs: 50_000,
107
+ ...overrides,
108
+ };
109
+ }
110
+
111
+ type Outcome = {
112
+ blockId: string;
113
+ label?: string;
114
+ status: string;
115
+ reason?: string;
116
+ from?: unknown;
117
+ to?: unknown;
118
+ readiness?: { status: string; blockers?: { message: string }[]; moreBlockers?: number };
119
+ };
120
+ type Answer = {
121
+ blocks: Outcome[];
122
+ done: number;
123
+ failed: number;
124
+ skipped: number;
125
+ unchanged: number;
126
+ stoppedBy?: string;
127
+ note?: string;
128
+ };
129
+
130
+ async function run(
131
+ project: UpdateBlocksProject,
132
+ input: Omit<UpdateBlocksInput, "projectId">,
133
+ opts: Partial<UpdateBlocksOptions> = {},
134
+ ) {
135
+ const result: { content: { text: string }[]; isError?: boolean } = await updateBlocks(
136
+ project,
137
+ { projectId: P, ...input },
138
+ options(opts),
139
+ );
140
+ return result;
141
+ }
142
+
143
+ function answerOf(result: { content: { text: string }[] }): Answer {
144
+ return JSON.parse(result.content[0].text);
145
+ }
146
+
147
+ /** Holds the turn of block `blockId` with a change until the returned function is called. */
148
+ function holdQueue(project: UpdateBlocksProject, blockId: string): () => void {
149
+ let release = () => {};
150
+ void oneAtATime(project, asBlockId(blockId), async () => {
151
+ await new Promise<void>((resolve) => (release = resolve));
152
+ });
153
+ return () => release();
154
+ }
155
+
156
+ /** Resolves once `condition` holds, checking every millisecond. */
157
+ async function until(condition: () => boolean): Promise<void> {
158
+ while (!condition()) await new Promise((resolve) => setTimeout(resolve, 1));
159
+ }
160
+
161
+ describe("blockUpdatesOf", () => {
162
+ test("lists the suggestions with their level and the recommended one with its level", () => {
163
+ const updates = blockUpdatesOf(upgradable("a", { settings: { versionLock: "major" } }));
164
+ expect(updates).toEqual({
165
+ blockId: "a",
166
+ label: "Block a",
167
+ current: { source: "registry", registry: REGISTRY, version: "1.0.0" },
168
+ recommended: { version: "2.0.0", level: "major", spec: registrySpec("2.0.0") },
169
+ available: [
170
+ { version: "2.0.0", level: "major", spec: registrySpec("2.0.0") },
171
+ { version: "1.1.0", level: "minor", spec: registrySpec("1.1.0") },
172
+ { version: "1.0.1", level: "patch", spec: registrySpec("1.0.1") },
173
+ ],
174
+ versionLock: "major",
175
+ });
176
+ });
177
+
178
+ test("a dev block with a newer build is recommended as rebuilt", () => {
179
+ const dev: BlockPackSpec = { type: "dev-v2", folder: "/blocks/a", mtime: "1" };
180
+ const updates = blockUpdatesOf(
181
+ block("a", { currentBlockPack: dev, updatedBlockPack: { ...dev, mtime: "2" } }),
182
+ );
183
+ expect(updates.current).toEqual({ source: "dev", folder: "/blocks/a" });
184
+ expect(updates.recommended).toEqual({ level: "rebuilt", spec: { ...dev, mtime: "2" } });
185
+ expect(updates.available).toEqual([]);
186
+ });
187
+
188
+ test("says when a block is incompatible and leaves out what is not known", () => {
189
+ const updates = blockUpdatesOf(
190
+ block("a", { currentBlockPack: undefined, isIncompatibleWithRuntime: true }),
191
+ );
192
+ expect(updates).toEqual({
193
+ blockId: "a",
194
+ label: "Block a",
195
+ available: [],
196
+ incompatible: true,
197
+ });
198
+ });
199
+ });
200
+
201
+ /**
202
+ * An update check that is not in until `arrive` is called; then `blocks` become `after`, the way
203
+ * the overview offers a block's updates once the application's check is in.
204
+ */
205
+ function pendingCheck(blocks: FakeBlock[], after: FakeBlock[]) {
206
+ let arrive = () => {};
207
+ const arrived = new Promise<void>((resolve) => (arrive = resolve));
208
+ const refresh = async () => {
209
+ await arrived;
210
+ blocks.splice(0, blocks.length, ...after);
211
+ };
212
+ return { refresh, arrive };
213
+ }
214
+
215
+ /** Never in. */
216
+ const neverChecked = () => new Promise<void>(() => {});
217
+
218
+ describe("list_block_updates", () => {
219
+ const blocks = [block("a"), upgradable("b"), upgradable("c")];
220
+
221
+ test("lists only blocks with an update by default, every block with all", async () => {
222
+ const { project } = fakeProject(blocks);
223
+ const some = JSON.parse((await listBlockUpdates(project, { projectId: P })).content[0].text);
224
+ expect(some.blocks.map((b: { blockId: string }) => b.blockId)).toEqual(["b", "c"]);
225
+ expect(some).toMatchObject({ total: 2, offset: 0, hasMore: false });
226
+
227
+ const every = JSON.parse(
228
+ (await listBlockUpdates(project, { projectId: P, all: true })).content[0].text,
229
+ );
230
+ expect(every.blocks.map((b: { blockId: string }) => b.blockId)).toEqual(["a", "b", "c"]);
231
+ });
232
+
233
+ test("pages, and names the asked-for ids the project lacks", async () => {
234
+ const { project } = fakeProject(blocks);
235
+ const result = JSON.parse(
236
+ (
237
+ await listBlockUpdates(project, {
238
+ projectId: P,
239
+ blockIds: [asBlockId("c"), asBlockId("b"), asBlockId("x")],
240
+ limit: 1,
241
+ })
242
+ ).content[0].text,
243
+ );
244
+ expect(result.blocks.map((b: { blockId: string }) => b.blockId)).toEqual(["b"]);
245
+ expect(result).toMatchObject({ total: 2, hasMore: true, notFound: ["x"] });
246
+ });
247
+
248
+ test("waits for the application's update check before it lists", async () => {
249
+ const blocks = [block("a")];
250
+ const { refresh, arrive } = pendingCheck(blocks, [upgradable("a")]);
251
+ const { project } = fakeProject(blocks, undefined, refresh);
252
+ const answer = listBlockUpdates(project, { projectId: P });
253
+ setTimeout(arrive, 5);
254
+ const result = JSON.parse((await answer).content[0].text);
255
+ expect(result.blocks.map((b: { blockId: string }) => b.blockId)).toEqual(["a"]);
256
+ expect(result.blocks[0].recommended).toMatchObject({ version: "2.0.0" });
257
+ expect(result.updateCheck).toBeUndefined();
258
+ });
259
+
260
+ test("says the update check is pending when it is not in, never that there is nothing", async () => {
261
+ const { project } = fakeProject([block("a")], undefined, neverChecked);
262
+ const result = JSON.parse(
263
+ (await listBlockUpdates(project, { projectId: P }, { updateCheckMs: 10 })).content[0].text,
264
+ );
265
+ expect(result).toMatchObject({ blocks: [], updateCheck: "pending" });
266
+ expect(result.note).toContain("has not finished");
267
+ });
268
+ });
269
+
270
+ describe("update_blocks", () => {
271
+ test("updates the blocks top to bottom, whatever order they are named in", async () => {
272
+ const { project, calls } = fakeProject([upgradable("a"), upgradable("b"), upgradable("c")]);
273
+ const result = await run(project, {
274
+ updates: [
275
+ { blockId: asBlockId("c"), to: "patch" },
276
+ { blockId: asBlockId("a"), to: "minor" },
277
+ ],
278
+ });
279
+ expect(calls).toEqual([
280
+ { blockId: "a", spec: registrySpec("1.1.0"), required: 2 },
281
+ { blockId: "c", spec: registrySpec("1.0.1"), required: 2 },
282
+ ]);
283
+ const answer = answerOf(result);
284
+ expect(answer).toMatchObject({ done: 2, failed: 0, skipped: 0, unchanged: 0 });
285
+ expect(answer.blocks[0]).toMatchObject({
286
+ blockId: "a",
287
+ status: "done",
288
+ from: { source: "registry", version: "1.0.0" },
289
+ to: { source: "registry", version: "1.1.0" },
290
+ readiness: { status: "ready" },
291
+ });
292
+ });
293
+
294
+ test("all: recommended takes every block with a recommended update", async () => {
295
+ const { project, calls } = fakeProject([upgradable("a"), block("b"), upgradable("c")]);
296
+ const answer = answerOf(await run(project, { all: "recommended" }));
297
+ expect(calls.map((c) => c.blockId)).toEqual(["a", "c"]);
298
+ expect(calls[0].spec).toEqual(registrySpec("2.0.0"));
299
+ expect(answer.done).toBe(2);
300
+ });
301
+
302
+ test("all: recommended leaves out what Update All leaves out", async () => {
303
+ const dev: BlockPackSpec = { type: "dev-v2", folder: "/blocks/b", mtime: "1" };
304
+ const pack: BlockPackSpec = { type: "from-pack-v2", packUrl: "file:///blocks/c" };
305
+ const { project, calls } = fakeProject([
306
+ upgradable("a"),
307
+ block("b", { currentBlockPack: dev, updatedBlockPack: { ...dev, mtime: "2" } }),
308
+ block("c", { currentBlockPack: pack, updatedBlockPack: pack }),
309
+ upgradable("d", { isIncompatibleWithRuntime: true }),
310
+ ]);
311
+ const answer = answerOf(await run(project, { all: "recommended" }));
312
+ expect(calls.map((c) => c.blockId)).toEqual(["a"]);
313
+ expect(answer.blocks.map((b) => b.blockId)).toEqual(["a"]);
314
+ });
315
+
316
+ test("a level the block does not have fails that block only", async () => {
317
+ const onlyPatch = block("a", {
318
+ updatedBlockPack: registrySpec("1.0.1"),
319
+ updateSuggestions: [{ type: "patch", update: registrySpec("1.0.1") }],
320
+ });
321
+ const { project, calls } = fakeProject([onlyPatch, upgradable("b")]);
322
+ const answer = answerOf(
323
+ await run(project, {
324
+ updates: [
325
+ { blockId: asBlockId("a"), to: "major" },
326
+ { blockId: asBlockId("b"), to: "major" },
327
+ ],
328
+ }),
329
+ );
330
+ expect(answer.blocks[0]).toMatchObject({ blockId: "a", status: "failed" });
331
+ expect(answer.blocks[0].reason).toContain("No major update is available");
332
+ expect(answer.blocks[0].reason).toContain("patch 1.0.1");
333
+ expect(answer.blocks[1]).toMatchObject({ blockId: "b", status: "done" });
334
+ expect(calls.map((c) => c.blockId)).toEqual(["b"]);
335
+ });
336
+
337
+ test("a block without a recommended update, or already on the version, is unchanged", async () => {
338
+ const { project, calls } = fakeProject([
339
+ block("a", { settings: { versionLock: "patch" } }),
340
+ upgradable("b"),
341
+ ]);
342
+ const answer = answerOf(
343
+ await run(project, {
344
+ updates: [
345
+ { blockId: asBlockId("a"), to: "recommended" },
346
+ { blockId: asBlockId("b"), to: targetSpec("1.0.0") },
347
+ ],
348
+ }),
349
+ );
350
+ expect(answer.blocks.map((b) => b.status)).toEqual(["unchanged", "unchanged"]);
351
+ expect(answer.blocks[0].reason).toContain("versionLock is patch");
352
+ expect(answer.unchanged).toBe(2);
353
+ expect(calls).toEqual([]);
354
+ });
355
+
356
+ test("a pack on block API 1 is refused through the shared guard; the rest go on", async () => {
357
+ const { project } = fakeProject([upgradable("a"), upgradable("b")], (blockId) => {
358
+ if (blockId === "a") throw new ModelAPIVersionMismatchError("a", 2, 1);
359
+ });
360
+ const answer = answerOf(await run(project, { all: "recommended" }));
361
+ expect(answer.blocks[0]).toMatchObject({
362
+ status: "failed",
363
+ reason: UNSUPPORTED_BLOCK_API_TEXT,
364
+ });
365
+ expect(answer.blocks[1]).toMatchObject({ status: "done" });
366
+ });
367
+
368
+ test("any other failure of the middle layer fails the block with its message", async () => {
369
+ const { project } = fakeProject([upgradable("a"), upgradable("b")], (blockId) => {
370
+ if (blockId === "a") throw new Error("runtime too old");
371
+ });
372
+ const answer = answerOf(await run(project, { all: "recommended" }));
373
+ expect(answer.blocks[0].status).toBe("failed");
374
+ expect(answer.blocks[0].reason).toContain("runtime too old");
375
+ expect(answer.blocks[1].status).toBe("done");
376
+ });
377
+
378
+ test("a cancel lets the block under way finish and skips the ones waiting", async () => {
379
+ const controller = new AbortController();
380
+ const { project, calls } = fakeProject(
381
+ [upgradable("a"), upgradable("b"), upgradable("c")],
382
+ () => controller.abort(),
383
+ );
384
+ const answer = answerOf(
385
+ await run(project, { all: "recommended" }, { signal: controller.signal }),
386
+ );
387
+ expect(calls.map((c) => c.blockId)).toEqual(["a"]);
388
+ expect(answer.blocks.map((b) => b.status)).toEqual(["done", "skipped", "skipped"]);
389
+ expect(answer.stoppedBy).toBe("aborted");
390
+ });
391
+
392
+ test("after the timeout no block starts", async () => {
393
+ const { project, calls } = fakeProject([upgradable("a"), upgradable("b")], async () => {
394
+ await new Promise((resolve) => setTimeout(resolve, 30));
395
+ });
396
+ const answer = answerOf(await run(project, { all: "recommended" }, { timeoutMs: 10 }));
397
+ expect(calls.map((c) => c.blockId)).toEqual(["a"]);
398
+ expect(answer.blocks.map((b) => b.status)).toEqual(["done", "skipped"]);
399
+ expect(answer.blocks[1].reason).toContain("time ran out");
400
+ expect(answer.stoppedBy).toBe("timeout");
401
+ });
402
+
403
+ test("an update waits for a change of the same block queued before it", async () => {
404
+ const order: string[] = [];
405
+ const { project } = fakeProject([upgradable("a")], () => {
406
+ order.push("update");
407
+ });
408
+ let release = () => {};
409
+ const earlier = oneAtATime(project, asBlockId("a"), async () => {
410
+ await new Promise<void>((resolve) => (release = resolve));
411
+ order.push("earlier write");
412
+ });
413
+ const updating = run(project, { all: "recommended" });
414
+ await new Promise((resolve) => setTimeout(resolve, 10));
415
+ expect(order).toEqual([]);
416
+ release();
417
+ await Promise.all([earlier, updating]);
418
+ expect(order).toEqual(["earlier write", "update"]);
419
+ });
420
+
421
+ test("a cancel while a block waits for an earlier change of it skips it", async () => {
422
+ const controller = new AbortController();
423
+ const { project, calls } = fakeProject([upgradable("a"), upgradable("b"), upgradable("c")]);
424
+ const release = holdQueue(project, "b");
425
+ const updating = run(project, { all: "recommended" }, { signal: controller.signal });
426
+ await until(() => calls.length === 1);
427
+ await new Promise((resolve) => setTimeout(resolve, 10));
428
+ controller.abort();
429
+ release();
430
+ const answer = answerOf(await updating);
431
+ expect(calls.map((c) => c.blockId)).toEqual(["a"]);
432
+ expect(answer.blocks.map((b) => b.status)).toEqual(["done", "skipped", "skipped"]);
433
+ expect(answer.blocks[1].reason).toContain("cancelled while this block waited");
434
+ expect(answer.stoppedBy).toBe("aborted");
435
+ });
436
+
437
+ test("a block whose turn comes after the timeout is skipped", async () => {
438
+ const { project, calls } = fakeProject([upgradable("a"), upgradable("b")]);
439
+ const release = holdQueue(project, "a");
440
+ const updating = run(project, { all: "recommended" }, { timeoutMs: 10 });
441
+ await new Promise((resolve) => setTimeout(resolve, 30));
442
+ release();
443
+ const answer = answerOf(await updating);
444
+ expect(calls).toEqual([]);
445
+ expect(answer.blocks.map((b) => b.status)).toEqual(["skipped", "skipped"]);
446
+ expect(answer.blocks[0].reason).toContain("time ran out while this block waited");
447
+ expect(answer.stoppedBy).toBe("timeout");
448
+ });
449
+
450
+ test("labels and reasons are cut short", async () => {
451
+ const { project } = fakeProject([upgradable("a", { title: "label ".repeat(100) })], () => {
452
+ throw new Error(`refused ${"because ".repeat(100)}`);
453
+ });
454
+ const answer = answerOf(await run(project, { all: "recommended" }));
455
+ expect(answer.blocks[0].status).toBe("failed");
456
+ expect(answer.blocks[0].label?.length).toBeLessThanOrEqual(UPDATE_LABEL_CHARS);
457
+ expect(answer.blocks[0].reason?.length).toBeLessThanOrEqual(UPDATE_REASON_CHARS);
458
+ });
459
+
460
+ test("an updated block's readiness is its status and the first blockers, cut", async () => {
461
+ const { project: base } = fakeProject([upgradable("a")]);
462
+ const project: UpdateBlocksProject = {
463
+ ...base,
464
+ getBlockReadiness: () => ({
465
+ getValue: async () => ({
466
+ args: {
467
+ type: "blocked",
468
+ issues: Array.from({ length: 5 }, (_, n) => ({
469
+ severity: "error" as const,
470
+ message: `issue ${n} ${"x".repeat(1000)}`,
471
+ })),
472
+ },
473
+ prerun: { type: "staged", issues: [] },
474
+ missingReferences: [],
475
+ }),
476
+ }),
477
+ };
478
+ const answer = answerOf(await run(project, { all: "recommended" }));
479
+ const readiness = answer.blocks[0].readiness;
480
+ expect(readiness?.blockers).toHaveLength(UPDATE_READINESS_BLOCKERS);
481
+ expect(readiness?.moreBlockers).toBe(5 - UPDATE_READINESS_BLOCKERS);
482
+ for (const b of readiness?.blockers ?? [])
483
+ expect(b.message.length).toBeLessThanOrEqual(UPDATE_BLOCKER_CHARS);
484
+ });
485
+
486
+ test("progress counts the blocks and only grows", async () => {
487
+ const sent: { progress: number; total?: number }[] = [];
488
+ const ctx: WaitContext = {
489
+ mcpReq: {
490
+ signal: new AbortController().signal,
491
+ _meta: { progressToken: "t" },
492
+ notify: async (n) => {
493
+ sent.push(n.params);
494
+ },
495
+ },
496
+ };
497
+ const report = progressReporter(ctx, "Updating blocks", 1);
498
+ const { project } = fakeProject([upgradable("a"), block("b"), upgradable("c")]);
499
+ await run(
500
+ project,
501
+ {
502
+ updates: [
503
+ { blockId: asBlockId("a"), to: "recommended" },
504
+ { blockId: asBlockId("b"), to: "major" },
505
+ { blockId: asBlockId("c"), to: "recommended" },
506
+ ],
507
+ },
508
+ {
509
+ onProgress: (done, total, message) => {
510
+ report?.(done, total, message);
511
+ // a repeated count is not sent again
512
+ report?.(done, total, message);
513
+ },
514
+ },
515
+ );
516
+ expect(sent.map((p) => p.progress)).toEqual([1, 2, 3]);
517
+ expect(sent.every((p) => p.total === 3)).toBe(true);
518
+ });
519
+
520
+ test("refuses unclear input and unknown blocks before changing anything", async () => {
521
+ const { project, calls } = fakeProject([upgradable("a")]);
522
+ const both = await run(project, {
523
+ all: "recommended",
524
+ updates: [{ blockId: asBlockId("a"), to: "patch" }],
525
+ });
526
+ const neither = await run(project, {});
527
+ const empty = await run(project, { updates: [] });
528
+ const twice = await run(project, {
529
+ updates: [
530
+ { blockId: asBlockId("a"), to: "patch" },
531
+ { blockId: asBlockId("a"), to: "major" },
532
+ ],
533
+ });
534
+ const unknown = await run(project, { updates: [{ blockId: asBlockId("x"), to: "patch" }] });
535
+ for (const result of [both, neither, empty, twice, unknown]) {
536
+ expect(result.isError).toBe(true);
537
+ expect(result.content[0].text).toContain("Nothing was changed");
538
+ }
539
+ expect(calls).toEqual([]);
540
+ });
541
+
542
+ test("all: recommended waits for the update check, then updates what it recommends", async () => {
543
+ const blocks = [block("a")];
544
+ const { refresh, arrive } = pendingCheck(blocks, [upgradable("a")]);
545
+ const { project, calls } = fakeProject(blocks, undefined, refresh);
546
+ const answer = run(project, { all: "recommended" });
547
+ setTimeout(arrive, 5);
548
+ expect(answerOf(await answer)).toMatchObject({ done: 1 });
549
+ expect(calls.map((c) => c.spec)).toEqual([registrySpec("2.0.0")]);
550
+ });
551
+
552
+ test("while the update check is not in, all: recommended changes nothing and says why", async () => {
553
+ const { project, calls } = fakeProject([upgradable("a")], undefined, neverChecked);
554
+ const result = await run(project, { all: "recommended" }, { updateCheckMs: 10 });
555
+ expect(result.isError).toBe(true);
556
+ expect(result.content[0].text).toContain("has not finished");
557
+ expect(result.content[0].text).toContain("Nothing was changed");
558
+ expect(calls).toEqual([]);
559
+ });
560
+
561
+ test("while the update check is not in, only blocks named by a pack spec are updated", async () => {
562
+ const { project, calls } = fakeProject(
563
+ [upgradable("a"), upgradable("b")],
564
+ undefined,
565
+ neverChecked,
566
+ );
567
+ const answer = answerOf(
568
+ await run(
569
+ project,
570
+ {
571
+ updates: [
572
+ { blockId: asBlockId("a"), to: "recommended" },
573
+ { blockId: asBlockId("b"), to: targetSpec("1.1.0") },
574
+ ],
575
+ },
576
+ { updateCheckMs: 10 },
577
+ ),
578
+ );
579
+ expect(answer.blocks.map((b) => [b.blockId, b.status])).toEqual([
580
+ ["a", "skipped"],
581
+ ["b", "done"],
582
+ ]);
583
+ expect(answer.blocks[0].reason).toContain("has not finished");
584
+ expect(calls.map((c) => c.blockId)).toEqual(["b"]);
585
+ });
586
+
587
+ test("a candidate's channel survives the input schema and reaches the update", async () => {
588
+ const onBeta = { ...registrySpec("1.1.0"), channel: "beta" };
589
+ const parsed = updateTargetInput.parse(onBeta);
590
+ expect(parsed).toEqual(onBeta);
591
+ expect(updateBlockSpecInput.parse(onBeta)).toEqual(onBeta);
592
+ if (typeof parsed === "string") throw new Error("a spec parses as a spec");
593
+
594
+ const { project, calls } = fakeProject([upgradable("a")]);
595
+ await run(project, { updates: [{ blockId: asBlockId("a"), to: parsed }] });
596
+ expect(calls[0].spec).toEqual(onBeta);
597
+ });
598
+
599
+ test("says so when no block has a recommended update", async () => {
600
+ const { project } = fakeProject([block("a")]);
601
+ const answer = answerOf(await run(project, { all: "recommended" }));
602
+ expect(answer.blocks).toEqual([]);
603
+ expect(answer.note).toContain("No block");
604
+ });
605
+ });