@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,650 @@
1
+ import type { PoolColumn, PoolTable } from "@milaboratories/pl-middle-layer";
2
+ import {
3
+ asBlockId,
4
+ createColumnDiscoveredId,
5
+ createGlobalPObjectId,
6
+ type AxisSpec,
7
+ type ColumnUniversalId,
8
+ type PColumnSpec,
9
+ type PTableColumnSpec,
10
+ type PTableDefV2,
11
+ type ProjectId,
12
+ } from "@milaboratories/pl-model-common";
13
+ import { afterEach, beforeEach, describe, expect, test } from "vitest";
14
+ import { AgentTables } from "../agent-tables";
15
+ import {
16
+ createTableDef,
17
+ createTableResult,
18
+ tableFilterInput,
19
+ tableSortInput,
20
+ TableRequestError,
21
+ type PoolTableComputable,
22
+ type TableColumnLookup,
23
+ type TableDriver,
24
+ type TableProject,
25
+ type TableRequest,
26
+ } from "./agent-tables";
27
+ import { queryTableResult, type DataQueryDriver } from "./data-query";
28
+ import { asPTableHandle, columnIdInput } from "./ids";
29
+
30
+ /** The JSON of a tool result, or its error text prefixed with `ERROR: `. */
31
+ function out(result: { content: { text: string }[]; isError?: boolean }): any {
32
+ const text = result.content[0].text;
33
+ return result.isError ? `ERROR: ${text}` : JSON.parse(text);
34
+ }
35
+
36
+ const PROJECT = "p1" as ProjectId;
37
+ const HANDLE = asPTableHandle("b".repeat(64));
38
+
39
+ const SAMPLE: AxisSpec = { name: "sample", type: "String" };
40
+ const CLONE: AxisSpec = { name: "clone", type: "Long" };
41
+
42
+ function column(blockId: string, name: string, axes: AxisSpec[], label?: string): PoolColumn {
43
+ const spec: PColumnSpec = {
44
+ kind: "PColumn",
45
+ name,
46
+ valueType: "Double",
47
+ axesSpec: axes,
48
+ ...(label !== undefined ? { annotations: { "pl7.app/label": label } } : {}),
49
+ };
50
+ return {
51
+ columnId: createGlobalPObjectId(blockId, name),
52
+ ref: { __isRef: true, blockId, name },
53
+ exportName: name,
54
+ producedBy: asBlockId(blockId),
55
+ spec,
56
+ };
57
+ }
58
+
59
+ const READS = column("qc", "reads", [SAMPLE], "Reads");
60
+ const QUALITY = column("qc", "quality", [SAMPLE], "Quality");
61
+ const COUNT = column("clones", "count", [SAMPLE, CLONE], "Count");
62
+ const OTHER = column("other", "other", [{ name: "sample", type: "String", domain: { d: "x" } }]);
63
+ const POOL = [READS, QUALITY, COUNT, OTHER];
64
+
65
+ // A column of a second block with the same spec as READS: the pool listing leaves it out.
66
+ const HIDDEN = column("qc2", "reads", [SAMPLE], "Reads");
67
+
68
+ // `description` is over `donor`, which a linker column maps to `sample`; the discovered id of
69
+ // `description` reached through the linker joins onto columns over `sample`.
70
+ const DONOR: AxisSpec = { name: "donor", type: "String" };
71
+ const LINK = column("meta", "link", [SAMPLE, DONOR]);
72
+ const DESCRIPTION = column("meta", "description", [DONOR], "Description");
73
+ const DISCOVERED: PoolColumn = {
74
+ columnId: createColumnDiscoveredId({
75
+ column: DESCRIPTION.columnId,
76
+ path: [{ type: "linker", column: LINK.columnId }],
77
+ }),
78
+ exportName: "description",
79
+ path: [LINK.columnId],
80
+ producedBy: DESCRIPTION.producedBy,
81
+ spec: DESCRIPTION.spec,
82
+ };
83
+
84
+ // `regionName` is over `region`, reached from `name` through two linkers: name -> group, then
85
+ // group -> region. Each linker join projects out the side it joins on, so the rows of its
86
+ // discovered id are over `name`, not over `region`.
87
+ const NAME: AxisSpec = { name: "name", type: "String" };
88
+ const GROUP: AxisSpec = { name: "group", type: "String" };
89
+ const REGION: AxisSpec = { name: "region", type: "String" };
90
+ const VALUE = column("t", "value", [NAME], "Value");
91
+ const NAME_GROUP = column("t", "nameGroup", [NAME, GROUP]);
92
+ const GROUP_REGION = column("t", "groupRegion", [GROUP, REGION]);
93
+ const REGION_NAME = column("t", "regionName", [REGION], "Region name");
94
+ const REGION_SIZE = column("t", "regionSize", [REGION], "Region size");
95
+ const REGION_BY_NAME: PoolColumn = {
96
+ columnId: createColumnDiscoveredId({
97
+ column: REGION_NAME.columnId,
98
+ path: [
99
+ { type: "linker", column: NAME_GROUP.columnId },
100
+ { type: "linker", column: GROUP_REGION.columnId },
101
+ ],
102
+ }),
103
+ exportName: "regionName",
104
+ path: [NAME_GROUP.columnId, GROUP_REGION.columnId],
105
+ producedBy: REGION_NAME.producedBy,
106
+ spec: REGION_NAME.spec,
107
+ };
108
+
109
+ /** Every column the fake project finds by id, those the listing leaves out included. */
110
+ const BY_ID = [
111
+ ...POOL,
112
+ HIDDEN,
113
+ LINK,
114
+ DESCRIPTION,
115
+ DISCOVERED,
116
+ VALUE,
117
+ NAME_GROUP,
118
+ GROUP_REGION,
119
+ REGION_NAME,
120
+ REGION_SIZE,
121
+ REGION_BY_NAME,
122
+ ];
123
+ const lookUp = (id: ColumnUniversalId) => BY_ID.find((c) => c.columnId === id);
124
+
125
+ /** A request over pool columns given whole. */
126
+ type Request = Omit<TableRequest, "columns" | "secondary"> & {
127
+ columns: PoolColumn[];
128
+ secondary?: PoolColumn[];
129
+ };
130
+
131
+ /** The definition `create_table` builds for `request`. */
132
+ function defineByIds(request: TableRequest, find: TableColumnLookup = lookUp) {
133
+ return createTableDef(request, find);
134
+ }
135
+
136
+ function define(request: Request) {
137
+ return defineByIds({
138
+ ...request,
139
+ columns: request.columns.map((c) => c.columnId),
140
+ secondary: request.secondary?.map((c) => c.columnId),
141
+ });
142
+ }
143
+
144
+ function refusal(request: Request) {
145
+ try {
146
+ define(request);
147
+ } catch (err) {
148
+ if (err instanceof TableRequestError) return `${err.message} | ${err.hint}`;
149
+ throw err;
150
+ }
151
+ throw new Error("Expected the request to be refused");
152
+ }
153
+
154
+ const colEntry = (c: PoolColumn) => ({ entry: { type: "column", column: c.columnId } });
155
+
156
+ describe("the definition of a table", () => {
157
+ test("joins the columns inner by default, as createPTableDefV3 does", () => {
158
+ expect(define({ columns: [READS, COUNT] }).def).toEqual({
159
+ query: { type: "innerJoin", entries: [colEntry(READS), colEntry(COUNT)] },
160
+ });
161
+ expect(define({ columns: [READS], join: "full" }).def.query.type).toBe("fullJoin");
162
+ });
163
+
164
+ test("outer-joins secondary columns onto the primary join", () => {
165
+ expect(define({ columns: [COUNT], secondary: [READS] }).def).toEqual({
166
+ query: {
167
+ type: "outerJoin",
168
+ primary: { entry: { type: "innerJoin", entries: [colEntry(COUNT)] } },
169
+ secondary: [colEntry(READS)],
170
+ },
171
+ });
172
+ });
173
+
174
+ test("a filter becomes a predicate over column and axis references", () => {
175
+ const { def } = define({
176
+ columns: [READS, QUALITY],
177
+ filter: {
178
+ type: "and",
179
+ filters: [
180
+ { type: "greaterThan", column: { column: READS.columnId }, x: 10 },
181
+ { type: "not", filter: { type: "inSet", column: { axis: "sample" }, value: ["s1"] } },
182
+ {
183
+ type: "lessThanColumn",
184
+ column: { column: QUALITY.columnId },
185
+ rhs: { column: READS.columnId },
186
+ },
187
+ ],
188
+ },
189
+ });
190
+ expect(def.query).toEqual({
191
+ type: "filter",
192
+ input: { type: "innerJoin", entries: [colEntry(READS), colEntry(QUALITY)] },
193
+ predicate: {
194
+ type: "and",
195
+ input: [
196
+ {
197
+ type: "numericComparison",
198
+ operand: "gt",
199
+ left: { type: "columnRef", value: READS.columnId },
200
+ right: { type: "constant", value: 10 },
201
+ },
202
+ {
203
+ type: "not",
204
+ input: {
205
+ type: "isIn",
206
+ input: { type: "axisRef", value: { name: "sample" } },
207
+ set: ["s1"],
208
+ negate: false,
209
+ },
210
+ },
211
+ {
212
+ type: "numericComparison",
213
+ operand: "lt",
214
+ left: { type: "columnRef", value: QUALITY.columnId },
215
+ right: { type: "columnRef", value: READS.columnId },
216
+ },
217
+ ],
218
+ },
219
+ });
220
+ });
221
+
222
+ test("sort keys come last, ascending and missing values last by default", () => {
223
+ const { def } = define({
224
+ columns: [COUNT],
225
+ sort: [
226
+ { column: COUNT.columnId, ascending: false },
227
+ { axis: "clone", nullsFirst: true },
228
+ ],
229
+ });
230
+ expect(def.query).toEqual({
231
+ type: "sort",
232
+ input: { type: "innerJoin", entries: [colEntry(COUNT)] },
233
+ sortBy: [
234
+ {
235
+ expression: { type: "columnRef", value: COUNT.columnId },
236
+ ascending: false,
237
+ nullsFirst: false,
238
+ },
239
+ {
240
+ expression: { type: "axisRef", value: { name: "clone" } },
241
+ ascending: true,
242
+ nullsFirst: true,
243
+ },
244
+ ],
245
+ });
246
+ });
247
+
248
+ test("a PlRef object and an escaped id find their column", () => {
249
+ const plRef = columnIdInput.parse({ __isRef: true, blockId: "qc", name: "reads" });
250
+ const escaped = columnIdInput.parse(JSON.stringify(QUALITY.columnId));
251
+ const filter = tableFilterInput.parse({
252
+ type: "isNA",
253
+ column: { column: { __isRef: true, blockId: "qc", name: "quality" } },
254
+ });
255
+ const sort = tableSortInput.parse([{ column: JSON.stringify(READS.columnId) }]);
256
+ const { columns, def } = defineByIds({ columns: [plRef, escaped], filter, sort });
257
+ expect(columns).toEqual([READS, QUALITY]);
258
+ expect(def.query).toMatchObject({
259
+ type: "sort",
260
+ sortBy: [{ expression: { type: "columnRef", value: READS.columnId } }],
261
+ input: {
262
+ type: "filter",
263
+ predicate: { type: "isNull", input: { type: "columnRef", value: QUALITY.columnId } },
264
+ },
265
+ });
266
+ });
267
+
268
+ test("a discovered id builds its entry by id and refers to it in filter and sort", () => {
269
+ const id = DISCOVERED.columnId;
270
+ const { def, columns } = define({
271
+ columns: [READS],
272
+ secondary: [DISCOVERED],
273
+ filter: { type: "patternEquals", column: { column: id }, value: "x" },
274
+ sort: [{ column: id }],
275
+ });
276
+ expect(columns).toEqual([READS, DISCOVERED]);
277
+ expect(def.query).toEqual({
278
+ type: "sort",
279
+ input: {
280
+ type: "filter",
281
+ input: {
282
+ type: "outerJoin",
283
+ primary: { entry: { type: "innerJoin", entries: [colEntry(READS)] } },
284
+ secondary: [colEntry(DISCOVERED)],
285
+ },
286
+ predicate: {
287
+ type: "stringEquals",
288
+ input: { type: "columnRef", value: id },
289
+ value: "x",
290
+ caseInsensitive: false,
291
+ },
292
+ },
293
+ sortBy: [
294
+ { expression: { type: "columnRef", value: id }, ascending: true, nullsFirst: false },
295
+ ],
296
+ });
297
+ });
298
+
299
+ test("refuses unknown, missing and repeated columns, saying what to do", () => {
300
+ const unknown = createGlobalPObjectId("nope", "x");
301
+ expect(() => defineByIds({ columns: [unknown] })).toThrow(/No column .*nope/);
302
+ expect(refusal({ columns: [{ ...READS, columnId: unknown }] })).toMatch(
303
+ /list_pool_columns again/,
304
+ );
305
+ expect(refusal({ columns: [] })).toMatch(/`columns` is empty/);
306
+ expect(refusal({ columns: [READS], secondary: [READS] })).toMatch(/named twice/);
307
+ });
308
+
309
+ test("refuses references to columns outside the table; an axis is a selector for the engine", () => {
310
+ expect(
311
+ refusal({ columns: [READS], filter: { type: "isNA", column: { column: QUALITY.columnId } } }),
312
+ ).toMatch(/is not in the table.*\| Add the column to `secondary`/);
313
+ const { def } = define({
314
+ columns: [READS],
315
+ sort: [{ axis: "sample", domain: { d: "x" }, contextDomain: { side: "b" } }],
316
+ });
317
+ expect(def.query).toMatchObject({
318
+ sortBy: [
319
+ {
320
+ expression: {
321
+ type: "axisRef",
322
+ value: { name: "sample", domain: { d: "x" }, contextDomain: { side: "b" } },
323
+ },
324
+ },
325
+ ],
326
+ });
327
+ });
328
+
329
+ test("refuses a filter over the size limit", () => {
330
+ const leaf = { type: "isNA" as const, column: { column: READS.columnId } };
331
+ expect(
332
+ refusal({
333
+ columns: [READS],
334
+ filter: { type: "or", filters: Array.from({ length: 300 }, () => leaf) },
335
+ }),
336
+ ).toMatch(/301 nodes/);
337
+ });
338
+ });
339
+
340
+ describe("the filter and sort inputs", () => {
341
+ test("take FilterSpec trees and refuse what a query cannot express", () => {
342
+ const c = READS.columnId;
343
+ const ok = tableFilterInput.safeParse({
344
+ type: "or",
345
+ filters: [
346
+ { type: "patternMatchesRegularExpression", column: { axis: "sample" }, value: "^s" },
347
+ { type: "equal", column: { column: c }, x: 1 },
348
+ ],
349
+ });
350
+ expect(ok.success).toBe(true);
351
+ expect(tableFilterInput.safeParse({ type: "topN", column: { column: c }, n: 3 }).success).toBe(
352
+ false,
353
+ );
354
+ expect(tableFilterInput.safeParse({ type: "and", filters: [] }).success).toBe(false);
355
+ expect(
356
+ tableFilterInput.safeParse({
357
+ type: "patternMatchesRegularExpression",
358
+ column: { column: c },
359
+ value: "(",
360
+ }).success,
361
+ ).toBe(false);
362
+ expect(tableSortInput.safeParse([{ column: c, ascending: false }]).success).toBe(true);
363
+ expect(tableSortInput.safeParse([{ index: 1 }]).success).toBe(false);
364
+ });
365
+
366
+ test("refuse a column reference that is not a column id", () => {
367
+ const filter = tableFilterInput.safeParse({ type: "isNA", column: { column: "reads" } });
368
+ expect(filter.error?.message).toMatch(/not a column id/);
369
+ expect(tableSortInput.safeParse([{ column: "reads" }]).success).toBe(false);
370
+ });
371
+ });
372
+
373
+ // create_table
374
+
375
+ /**
376
+ * A project whose pool columns, found by id, are {@link BY_ID}, and whose tables are what `build`
377
+ * says. It has no listing of the pool: create_table looks each column up by its id.
378
+ */
379
+ function fakeProject(
380
+ build: () => Promise<PoolTable> = async () => ({ type: "ok", handle: HANDLE }),
381
+ ) {
382
+ const built: { def: PTableDefV2<ColumnUniversalId>; computable: FakeComputable }[] = [];
383
+ const lookedUp: ColumnUniversalId[][] = [];
384
+ class FakeComputable implements PoolTableComputable {
385
+ resets = 0;
386
+ getValue = build;
387
+ resetState(): void {
388
+ this.resets++;
389
+ }
390
+ isChanged(): boolean {
391
+ return false;
392
+ }
393
+ }
394
+ const project: TableProject = {
395
+ getPoolColumnsById: (ids) => {
396
+ lookedUp.push([...ids]);
397
+ return { getValue: async () => ids.map(lookUp) };
398
+ },
399
+ createPoolTable: (def) => {
400
+ const computable = new FakeComputable();
401
+ built.push({ def, computable });
402
+ return computable;
403
+ },
404
+ };
405
+ return { project, built, lookedUp };
406
+ }
407
+
408
+ const SPEC: PTableColumnSpec[] = [
409
+ { type: "axis", id: { name: "sample", type: "String" }, spec: SAMPLE },
410
+ { type: "column", id: READS.columnId, spec: READS.spec },
411
+ ];
412
+
413
+ function fakeDriver(shape: (signal?: AbortSignal) => Promise<number>): TableDriver {
414
+ return {
415
+ getSpec: async () => SPEC,
416
+ getShape: async (_h, signal) => ({ columns: 2, rows: await shape(signal) }),
417
+ };
418
+ }
419
+
420
+ /** A row count that never comes, until the signal aborts. */
421
+ function never(signal?: AbortSignal): Promise<number> {
422
+ return new Promise((_resolve, reject) => {
423
+ if (signal?.aborted) reject(new Error("aborted"));
424
+ signal?.addEventListener("abort", () => reject(new Error("aborted")));
425
+ });
426
+ }
427
+
428
+ describe("createTableResult", () => {
429
+ let tables: AgentTables;
430
+
431
+ beforeEach(() => {
432
+ tables = new AgentTables({ idleMs: 180_000, maxTables: 1 });
433
+ });
434
+
435
+ afterEach(() => {
436
+ tables.releaseAll();
437
+ });
438
+
439
+ test("builds the table, keeps it and answers its handle, rows and columns", async () => {
440
+ const { project, built } = fakeProject();
441
+ const result = out(
442
+ await createTableResult(
443
+ project,
444
+ PROJECT,
445
+ { columns: [READS.columnId] },
446
+ {
447
+ driver: fakeDriver(async () => 7),
448
+ tables,
449
+ },
450
+ ),
451
+ );
452
+ expect(result).toEqual({
453
+ pTableHandle: HANDLE,
454
+ rows: 7,
455
+ expiresInSec: 180,
456
+ columns: [
457
+ { index: 0, type: "axis", name: "sample" },
458
+ { index: 1, type: "column", name: "reads", label: "Reads", columnId: READS.columnId },
459
+ ],
460
+ });
461
+ expect(built).toHaveLength(1);
462
+ expect(tables.use(HANDLE)).toMatchObject({ projectId: PROJECT });
463
+ tables.releaseAll();
464
+ expect(built[0].computable.resets).toBe(1);
465
+ });
466
+
467
+ test("answers without the row count when counting takes too long", async () => {
468
+ const { project } = fakeProject();
469
+ const result = out(
470
+ await createTableResult(
471
+ project,
472
+ PROJECT,
473
+ { columns: [READS.columnId] },
474
+ {
475
+ driver: fakeDriver(never),
476
+ tables,
477
+ rowsTimeoutMs: 10,
478
+ },
479
+ ),
480
+ );
481
+ expect(result).toMatchObject({ pTableHandle: HANDLE, rows: null });
482
+ expect(result.note).toMatch(/took too long.*query_table/);
483
+ expect(tables.size).toBe(1);
484
+ });
485
+
486
+ test("a cancelled call says so", async () => {
487
+ const { project } = fakeProject();
488
+ const abort = new AbortController();
489
+ const pending = createTableResult(
490
+ project,
491
+ PROJECT,
492
+ { columns: [READS.columnId] },
493
+ { driver: fakeDriver(never), tables },
494
+ abort.signal,
495
+ );
496
+ abort.abort();
497
+ expect(out(await pending)).toMatch(/cancelled/);
498
+ });
499
+
500
+ test("a column hidden by dedup is used by its id", async () => {
501
+ const { project, built } = fakeProject();
502
+ const result = out(
503
+ await createTableResult(
504
+ project,
505
+ PROJECT,
506
+ { columns: [HIDDEN.columnId] },
507
+ { driver: fakeDriver(async () => 1), tables },
508
+ ),
509
+ );
510
+ expect(result.pTableHandle).toBe(HANDLE);
511
+ expect(built[0].def.query).toEqual({ type: "innerJoin", entries: [colEntry(HIDDEN)] });
512
+ });
513
+
514
+ test("names the columns the middle layer refused, with what to do, and keeps nothing", async () => {
515
+ const { project, built } = fakeProject(async () => ({
516
+ type: "refused",
517
+ refused: [
518
+ { columnId: DISCOVERED.columnId, issue: "notReady" },
519
+ { columnId: COUNT.columnId, issue: "noData" },
520
+ ],
521
+ }));
522
+ const result = out(
523
+ await createTableResult(
524
+ project,
525
+ PROJECT,
526
+ { columns: [READS.columnId, COUNT.columnId], secondary: [DISCOVERED.columnId] },
527
+ { driver: fakeDriver(async () => 1), tables },
528
+ undefined,
529
+ (id) => `Block ${id.toUpperCase()}`,
530
+ ),
531
+ );
532
+ expect(result).toMatch(/No table was built/);
533
+ expect(result).toMatch(
534
+ /Description .*__isDiscovered.*still being calculated by block Block META/,
535
+ );
536
+ expect(result).toMatch(/Count .*has no data; block Block CLONES has not run/);
537
+ expect(result).toMatch(/await_block_done.*run_block/s);
538
+ expect(built[0].computable.resets).toBe(1);
539
+ expect(tables.size).toBe(0);
540
+ });
541
+
542
+ test("a table the engine refuses is given back, with its error", async () => {
543
+ const { project, built } = fakeProject(async () => {
544
+ throw new Error("axes are incompatible");
545
+ });
546
+ const result = out(
547
+ await createTableResult(
548
+ project,
549
+ PROJECT,
550
+ { columns: [READS.columnId] },
551
+ {
552
+ driver: fakeDriver(async () => 1),
553
+ tables,
554
+ },
555
+ ),
556
+ );
557
+ expect(result).toMatch(/could not be built: axes are incompatible/);
558
+ expect(built[0].computable.resets).toBe(1);
559
+ });
560
+
561
+ test("a filter or sort builds the table once, its axes named by selectors", async () => {
562
+ const { project, built } = fakeProject();
563
+ const request = { columns: [DISCOVERED.columnId], sort: [{ axis: "sample" }] };
564
+ const result = out(
565
+ await createTableResult(project, PROJECT, request, {
566
+ driver: fakeDriver(async () => 3),
567
+ tables,
568
+ }),
569
+ );
570
+ expect(result).toMatchObject({ pTableHandle: HANDLE, rows: 3 });
571
+ expect(built.map((b) => b.def.query)).toEqual([
572
+ {
573
+ type: "sort",
574
+ input: { type: "innerJoin", entries: [colEntry(DISCOVERED)] },
575
+ sortBy: [
576
+ {
577
+ expression: { type: "axisRef", value: { name: "sample" } },
578
+ ascending: true,
579
+ nullsFirst: false,
580
+ },
581
+ ],
582
+ },
583
+ ]);
584
+ });
585
+
586
+ test("names the tables the cap dropped", async () => {
587
+ const other = asPTableHandle("c".repeat(64));
588
+ const { project } = fakeProject(async () => ({ type: "ok", handle: other }));
589
+ const first = fakeProject();
590
+ await createTableResult(
591
+ first.project,
592
+ PROJECT,
593
+ { columns: [READS.columnId] },
594
+ {
595
+ driver: fakeDriver(async () => 1),
596
+ tables,
597
+ },
598
+ );
599
+ const result = out(
600
+ await createTableResult(
601
+ project,
602
+ PROJECT,
603
+ { columns: [QUALITY.columnId] },
604
+ {
605
+ driver: fakeDriver(async () => 1),
606
+ tables,
607
+ },
608
+ ),
609
+ );
610
+ expect(result.evicted).toEqual([HANDLE]);
611
+ expect(result.evictedNote).toMatch(/least recently used/);
612
+ expect(first.built[0].computable.resets).toBe(1);
613
+ });
614
+ });
615
+
616
+ describe("query_table on a table of the agent's own", () => {
617
+ const driver: DataQueryDriver = {
618
+ getSpec: async () => SPEC,
619
+ getShape: async () => ({ columns: 2, rows: 1 }),
620
+ getData: async () => [
621
+ { type: "String", data: ["s1"], isNA: new Uint8Array(1) },
622
+ { type: "Double", data: new Float64Array([3]), isNA: new Uint8Array(1) },
623
+ ],
624
+ listColumns: async () => [],
625
+ getUniqueValues: async () => {
626
+ throw new Error("not used");
627
+ },
628
+ };
629
+ const input = {
630
+ pTableHandle: HANDLE,
631
+ offset: 0,
632
+ limit: 10,
633
+ transformTimeout: 1000,
634
+ };
635
+
636
+ test("says when the table is stale, and only then", async () => {
637
+ const stale = out(await queryTableResult(driver, input, undefined, { stale: true }));
638
+ expect(stale).toMatchObject({ stale: true, rows: [["s1", 3]] });
639
+ expect(stale.staleNote).toMatch(/create_table again/);
640
+ const fresh = out(await queryTableResult(driver, input, undefined, { stale: false }));
641
+ expect(fresh.stale).toBeUndefined();
642
+ });
643
+
644
+ test("points to create_table for the column indices", async () => {
645
+ const result = out(
646
+ await queryTableResult(driver, { ...input, columns: [5] }, undefined, { stale: false }),
647
+ );
648
+ expect(result).toMatch(/create_table listed the table's columns/);
649
+ });
650
+ });