@milaboratories/pl-mcp-server 54.0.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.
- package/README.md +15 -25
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +15 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +15 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +58 -8
- package/dist/project-holds.cjs.map +1 -1
- package/dist/project-holds.js +58 -8
- package/dist/project-holds.js.map +1 -1
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +40 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +40 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +48 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +21 -20
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +48 -18
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -73
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -75
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +3 -0
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +3 -0
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +272 -4
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +270 -6
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +323 -42
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +317 -46
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +183 -70
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +177 -70
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +451 -113
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +446 -118
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +56 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +51 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +98 -19
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +95 -20
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +66 -19
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +66 -20
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +15 -12
- package/src/project-holds.test.ts +179 -0
- package/src/project-holds.ts +114 -12
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +54 -1
- package/src/server-log.ts +49 -27
- package/src/server.logging.test.ts +38 -46
- package/src/server.test.ts +151 -17
- package/src/server.ts +62 -34
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -123
- package/src/tools/block-action-context.ts +3 -0
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +623 -8
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +659 -66
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +336 -105
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +807 -157
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +67 -0
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +130 -21
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +135 -25
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +77 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type { ProjectHolder } from "@milaboratories/pl-middle-layer";
|
|
2
|
+
import type { PTableHandle, ProjectId } from "@milaboratories/pl-model-common";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
4
|
+
import { AgentTables, projectHoldsWithTables, type HeldTable } from "./agent-tables";
|
|
5
|
+
import type { Hold, HoldSource, KeptHoldRelease } from "./project-holds";
|
|
6
|
+
|
|
7
|
+
const P1 = "p1" as ProjectId;
|
|
8
|
+
const P2 = "p2" as ProjectId;
|
|
9
|
+
const IDLE_MS = 1_000;
|
|
10
|
+
|
|
11
|
+
function handle(n: number): PTableHandle {
|
|
12
|
+
return n.toString(16).padStart(64, "0") as PTableHandle;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A table that counts its resets; `changed` is what `isChanged` says. */
|
|
16
|
+
class FakeTable implements HeldTable {
|
|
17
|
+
resets = 0;
|
|
18
|
+
changed = false;
|
|
19
|
+
/** Called on every reset, before it is counted. */
|
|
20
|
+
onReset = () => {};
|
|
21
|
+
resetState(): void {
|
|
22
|
+
this.onReset();
|
|
23
|
+
this.resets++;
|
|
24
|
+
}
|
|
25
|
+
isChanged(): boolean {
|
|
26
|
+
return this.changed;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("AgentTables", () => {
|
|
31
|
+
let tables: AgentTables;
|
|
32
|
+
let made: FakeTable[];
|
|
33
|
+
|
|
34
|
+
function table(): FakeTable {
|
|
35
|
+
const t = new FakeTable();
|
|
36
|
+
made.push(t);
|
|
37
|
+
return t;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
vi.useFakeTimers();
|
|
42
|
+
made = [];
|
|
43
|
+
tables = new AgentTables({ idleMs: IDLE_MS, maxTables: 3 });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
// No leak: every table given to the registry was given back exactly once.
|
|
48
|
+
tables.releaseAll();
|
|
49
|
+
for (const t of made) expect(t.resets).toBe(1);
|
|
50
|
+
vi.useRealTimers();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a table is found for reads, with its project", () => {
|
|
54
|
+
tables.add(P1, handle(1), table());
|
|
55
|
+
expect(tables.use(handle(1))).toEqual({ projectId: P1, stale: false });
|
|
56
|
+
expect(tables.use(handle(2))).toBeUndefined();
|
|
57
|
+
expect(tables.expiresInSec).toBe(1);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("says it is stale once what it was built from changed", () => {
|
|
61
|
+
const t = table();
|
|
62
|
+
tables.add(P1, handle(1), t);
|
|
63
|
+
t.changed = true;
|
|
64
|
+
expect(tables.use(handle(1))).toMatchObject({ stale: true });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("expires after the idle time; every read starts the idle time again", async () => {
|
|
68
|
+
const t = table();
|
|
69
|
+
tables.add(P1, handle(1), t);
|
|
70
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS * 0.8);
|
|
71
|
+
tables.use(handle(1));
|
|
72
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS * 0.8);
|
|
73
|
+
expect(t.resets).toBe(0);
|
|
74
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS * 0.3);
|
|
75
|
+
expect(t.resets).toBe(1);
|
|
76
|
+
expect(tables.use(handle(1))).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("drop gives the table back once; dropping again or an unknown handle says false", () => {
|
|
80
|
+
const t = table();
|
|
81
|
+
tables.add(P1, handle(1), t);
|
|
82
|
+
expect(tables.drop(handle(1))).toBe(true);
|
|
83
|
+
expect(t.resets).toBe(1);
|
|
84
|
+
expect(tables.drop(handle(1))).toBe(false);
|
|
85
|
+
expect(tables.drop(handle(9))).toBe(false);
|
|
86
|
+
expect(tables.use(handle(1))).toBeUndefined();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("over the cap, the least recently used goes and is named", () => {
|
|
90
|
+
const first = table();
|
|
91
|
+
tables.add(P1, handle(1), first);
|
|
92
|
+
tables.add(P1, handle(2), table());
|
|
93
|
+
tables.add(P1, handle(3), table());
|
|
94
|
+
tables.use(handle(1));
|
|
95
|
+
expect(tables.add(P1, handle(4), table())).toEqual([handle(2)]);
|
|
96
|
+
expect(tables.size).toBe(3);
|
|
97
|
+
expect(first.resets).toBe(0);
|
|
98
|
+
expect(tables.use(handle(2))).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("the tables of a project go with it", () => {
|
|
102
|
+
tables.add(P1, handle(1), table());
|
|
103
|
+
tables.add(P2, handle(2), table());
|
|
104
|
+
tables.releaseProject(P1);
|
|
105
|
+
expect(tables.use(handle(1))).toBeUndefined();
|
|
106
|
+
expect(tables.use(handle(2))).toMatchObject({ projectId: P2 });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("the same table built again is one entry; the newer build replaces the older", () => {
|
|
110
|
+
const first = table();
|
|
111
|
+
const second = table();
|
|
112
|
+
tables.add(P1, handle(1), first);
|
|
113
|
+
first.changed = true;
|
|
114
|
+
expect(tables.use(handle(1))).toMatchObject({ stale: true });
|
|
115
|
+
// The newer reference is in place before the older one is given back.
|
|
116
|
+
let atReset: unknown;
|
|
117
|
+
first.onReset = () => (atReset = tables.size);
|
|
118
|
+
expect(tables.add(P2, handle(1), second)).toEqual([]);
|
|
119
|
+
expect(atReset).toBe(1);
|
|
120
|
+
expect(first.resets).toBe(1);
|
|
121
|
+
expect(second.resets).toBe(0);
|
|
122
|
+
// The fresh build says whether the table is stale, and whose it is.
|
|
123
|
+
expect(tables.use(handle(1))).toEqual({ projectId: P2, stale: false });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("a handle built again after it went is open again", () => {
|
|
127
|
+
tables.add(P1, handle(1), table());
|
|
128
|
+
tables.drop(handle(1));
|
|
129
|
+
tables.add(P1, handle(1), table());
|
|
130
|
+
expect(tables.use(handle(1))).toMatchObject({ projectId: P1 });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("release all gives every table back", () => {
|
|
134
|
+
tables.add(P1, handle(1), table());
|
|
135
|
+
tables.add(P2, handle(2), table());
|
|
136
|
+
tables.releaseAll();
|
|
137
|
+
expect(tables.size).toBe(0);
|
|
138
|
+
expect(tables.use(handle(2))).toBeUndefined();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("projectHoldsWithTables", () => {
|
|
143
|
+
const HOLDER = "mcp:test" as ProjectHolder;
|
|
144
|
+
const HOLD_IDLE_MS = 100;
|
|
145
|
+
|
|
146
|
+
/** A middle layer whose holds end when given back, or when `end` is called. */
|
|
147
|
+
class Source implements HoldSource<ProjectId> {
|
|
148
|
+
readonly controllers: AbortController[] = [];
|
|
149
|
+
async acquireProject(id: ProjectId): Promise<Hold<ProjectId>> {
|
|
150
|
+
const controller = new AbortController();
|
|
151
|
+
this.controllers.push(controller);
|
|
152
|
+
return {
|
|
153
|
+
project: id,
|
|
154
|
+
signal: controller.signal,
|
|
155
|
+
get ended() {
|
|
156
|
+
return controller.signal.aborted;
|
|
157
|
+
},
|
|
158
|
+
release: async () => controller.abort(),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let tables: AgentTables;
|
|
164
|
+
let made: FakeTable[];
|
|
165
|
+
let released: [ProjectId, KeptHoldRelease][];
|
|
166
|
+
let ended: ProjectId[];
|
|
167
|
+
|
|
168
|
+
function holdsAndTables() {
|
|
169
|
+
return projectHoldsWithTables<ProjectId>(tables, {
|
|
170
|
+
holder: HOLDER,
|
|
171
|
+
idleMs: HOLD_IDLE_MS,
|
|
172
|
+
onRelease: (id, reason) => released.push([id, reason]),
|
|
173
|
+
onEnded: (id) => ended.push(id),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function table(): FakeTable {
|
|
178
|
+
const t = new FakeTable();
|
|
179
|
+
made.push(t);
|
|
180
|
+
return t;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
beforeEach(() => {
|
|
184
|
+
vi.useFakeTimers();
|
|
185
|
+
tables = new AgentTables({ idleMs: 60_000, maxTables: 3 });
|
|
186
|
+
made = [];
|
|
187
|
+
released = [];
|
|
188
|
+
ended = [];
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
afterEach(() => {
|
|
192
|
+
tables.releaseAll();
|
|
193
|
+
for (const t of made) expect(t.resets).toBe(1);
|
|
194
|
+
vi.useRealTimers();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("a project's tables stay while a call on it runs past the idle time, and go with the hold it keeps", async () => {
|
|
198
|
+
const source = new Source();
|
|
199
|
+
const holds = holdsAndTables();
|
|
200
|
+
await holds.use(source, P1, async () => {
|
|
201
|
+
tables.add(P1, handle(1), table());
|
|
202
|
+
});
|
|
203
|
+
let finish = () => {};
|
|
204
|
+
const running = holds.use(
|
|
205
|
+
source,
|
|
206
|
+
P1,
|
|
207
|
+
() =>
|
|
208
|
+
new Promise<void>((resolve) => {
|
|
209
|
+
finish = resolve;
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
// The hold the first call kept is given back while the second one runs.
|
|
213
|
+
await vi.advanceTimersByTimeAsync(HOLD_IDLE_MS * 2);
|
|
214
|
+
expect(released).toEqual([[P1, "idle"]]);
|
|
215
|
+
expect(tables.use(handle(1))).toMatchObject({ projectId: P1 });
|
|
216
|
+
|
|
217
|
+
finish();
|
|
218
|
+
await running;
|
|
219
|
+
expect(tables.use(handle(1))).toMatchObject({ projectId: P1 });
|
|
220
|
+
await vi.advanceTimersByTimeAsync(HOLD_IDLE_MS * 2);
|
|
221
|
+
expect(released).toEqual([
|
|
222
|
+
[P1, "idle"],
|
|
223
|
+
[P1, "idle"],
|
|
224
|
+
]);
|
|
225
|
+
expect(tables.use(handle(1))).toBeUndefined();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("with no call running, the tables go with the hold; a project ended takes its tables", async () => {
|
|
229
|
+
const source = new Source();
|
|
230
|
+
const holds = holdsAndTables();
|
|
231
|
+
await holds.use(source, P1, async () => {
|
|
232
|
+
tables.add(P1, handle(1), table());
|
|
233
|
+
});
|
|
234
|
+
await holds.use(source, P2, async () => {
|
|
235
|
+
tables.add(P2, handle(2), table());
|
|
236
|
+
});
|
|
237
|
+
await vi.advanceTimersByTimeAsync(HOLD_IDLE_MS / 2);
|
|
238
|
+
// The middle layer ends the hold kept on P2 (the project was deleted).
|
|
239
|
+
source.controllers[1].abort();
|
|
240
|
+
expect(ended).toEqual([P2]);
|
|
241
|
+
expect(tables.use(handle(2))).toBeUndefined();
|
|
242
|
+
|
|
243
|
+
await vi.advanceTimersByTimeAsync(HOLD_IDLE_MS);
|
|
244
|
+
expect(tables.use(handle(1))).toBeUndefined();
|
|
245
|
+
});
|
|
246
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { PTableHandle, ProjectId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import { ProjectHolds, type ProjectHoldsOptions } from "./project-holds";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What the server keeps of a table an agent built: the computable that holds the driver's
|
|
6
|
+
* reference to it (the middle layer's `Project.createPoolTable`).
|
|
7
|
+
*/
|
|
8
|
+
export interface HeldTable {
|
|
9
|
+
/** Gives the driver's reference back. */
|
|
10
|
+
resetState(): void;
|
|
11
|
+
/** Whether what the table was built from may have changed since it was built. */
|
|
12
|
+
isChanged(): boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AgentTablesOptions {
|
|
16
|
+
/** How long a table is kept after it was built or last read. */
|
|
17
|
+
idleMs: number;
|
|
18
|
+
/** How many tables are kept; the least recently used goes when one more is added. */
|
|
19
|
+
maxTables: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type Entry = {
|
|
23
|
+
readonly projectId: ProjectId;
|
|
24
|
+
readonly table: HeldTable;
|
|
25
|
+
readonly timer: NodeJS.Timeout;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The tables agents built with `create_table`, by handle.
|
|
30
|
+
*
|
|
31
|
+
* A table is given back (its computable reset, which releases the driver's reference) at the
|
|
32
|
+
* earliest of: {@link drop}; {@link AgentTablesOptions.idleMs} after it was built or last used;
|
|
33
|
+
* {@link releaseProject} of its project, when the server lets go of it; {@link releaseAll}; or
|
|
34
|
+
* when more than {@link AgentTablesOptions.maxTables} are open and it is the least recently used.
|
|
35
|
+
* A read of a table that went meanwhile fails as a read of any stale handle does.
|
|
36
|
+
*
|
|
37
|
+
* The same definition over the same data gives the same handle: building it again replaces the
|
|
38
|
+
* entry, and the newer reference is in place before the older one is given back.
|
|
39
|
+
*/
|
|
40
|
+
export class AgentTables {
|
|
41
|
+
/** The open tables; the map's order is the order of use, oldest first. */
|
|
42
|
+
private readonly open = new Map<PTableHandle, Entry>();
|
|
43
|
+
|
|
44
|
+
constructor(private readonly options: AgentTablesOptions) {}
|
|
45
|
+
|
|
46
|
+
/** How many tables are open. */
|
|
47
|
+
get size(): number {
|
|
48
|
+
return this.open.size;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The idle time of a table, in whole seconds. */
|
|
52
|
+
get expiresInSec(): number {
|
|
53
|
+
return Math.round(this.options.idleMs / 1000);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Keeps `table`, built over project `projectId`, under `handle`. Gives the handles of the tables
|
|
58
|
+
* let go to stay within the cap.
|
|
59
|
+
*/
|
|
60
|
+
add(projectId: ProjectId, handle: PTableHandle, table: HeldTable): PTableHandle[] {
|
|
61
|
+
const previous = this.open.get(handle);
|
|
62
|
+
const entry: Entry = {
|
|
63
|
+
projectId,
|
|
64
|
+
table,
|
|
65
|
+
timer: setTimeout(() => this.release(handle, entry), this.options.idleMs),
|
|
66
|
+
};
|
|
67
|
+
entry.timer.unref();
|
|
68
|
+
this.open.delete(handle);
|
|
69
|
+
this.open.set(handle, entry);
|
|
70
|
+
if (previous !== undefined) {
|
|
71
|
+
clearTimeout(previous.timer);
|
|
72
|
+
previous.table.resetState();
|
|
73
|
+
}
|
|
74
|
+
const evicted: PTableHandle[] = [];
|
|
75
|
+
for (const [oldest, old] of this.open) {
|
|
76
|
+
if (this.open.size <= this.options.maxTables) break;
|
|
77
|
+
evicted.push(oldest);
|
|
78
|
+
this.release(oldest, old);
|
|
79
|
+
}
|
|
80
|
+
return evicted;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The table under `handle`, counted as used: its idle time starts again. `stale`: what it was
|
|
85
|
+
* built from may have changed. `undefined` when no such table is open.
|
|
86
|
+
*/
|
|
87
|
+
use(handle: PTableHandle): { projectId: ProjectId; stale: boolean } | undefined {
|
|
88
|
+
const entry = this.open.get(handle);
|
|
89
|
+
if (entry === undefined) return undefined;
|
|
90
|
+
entry.timer.refresh();
|
|
91
|
+
this.open.delete(handle);
|
|
92
|
+
this.open.set(handle, entry);
|
|
93
|
+
return { projectId: entry.projectId, stale: entry.table.isChanged() };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Gives back the table under `handle`. False when no such table is open. */
|
|
97
|
+
drop(handle: PTableHandle): boolean {
|
|
98
|
+
const entry = this.open.get(handle);
|
|
99
|
+
if (entry === undefined) return false;
|
|
100
|
+
this.release(handle, entry);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Gives back every table built over `projectId`. */
|
|
105
|
+
releaseProject(projectId: ProjectId): void {
|
|
106
|
+
for (const [handle, entry] of this.open)
|
|
107
|
+
if (entry.projectId === projectId) this.release(handle, entry);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Gives back every table. */
|
|
111
|
+
releaseAll(): void {
|
|
112
|
+
for (const [handle, entry] of this.open) this.release(handle, entry);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private release(handle: PTableHandle, entry: Entry): void {
|
|
116
|
+
if (this.open.get(handle) !== entry) return;
|
|
117
|
+
clearTimeout(entry.timer);
|
|
118
|
+
this.open.delete(handle);
|
|
119
|
+
entry.table.resetState();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The server's holds on projects, wired to its agent tables: the tables built over a project go
|
|
125
|
+
* when the server's kept hold on it is given back or ended, but not while a call on the project is
|
|
126
|
+
* still running. Such a call ends by keeping the project again ({@link ProjectHolds}), and the
|
|
127
|
+
* release of that hold lets the tables go then.
|
|
128
|
+
*/
|
|
129
|
+
export function projectHoldsWithTables<P>(
|
|
130
|
+
tables: AgentTables,
|
|
131
|
+
options: ProjectHoldsOptions<P>,
|
|
132
|
+
): ProjectHolds<P> {
|
|
133
|
+
const holds: ProjectHolds<P> = new ProjectHolds<P>({
|
|
134
|
+
...options,
|
|
135
|
+
onRelease: (projectId, reason) => {
|
|
136
|
+
if (holds.callsInFlight(projectId) === 0) tables.releaseProject(projectId);
|
|
137
|
+
options.onRelease?.(projectId, reason);
|
|
138
|
+
},
|
|
139
|
+
onEnded: (projectId) => {
|
|
140
|
+
tables.releaseProject(projectId);
|
|
141
|
+
options.onEnded?.(projectId);
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
return holds;
|
|
145
|
+
}
|
package/src/instructions.ts
CHANGED
|
@@ -3,23 +3,26 @@
|
|
|
3
3
|
* the order the tools are used in. Clients that load tools on demand show the model this text and
|
|
4
4
|
* the tool names only, so the order of work has to be readable from here.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Claude Code cuts server instructions at 4096 characters. This text stays under 3600, so a new
|
|
7
|
+
* tool can be named in the order of work without pushing it over. It keeps only the order of work
|
|
8
|
+
* and the rules that hold for every tool; what one tool does, refuses or waits for is said in that
|
|
9
|
+
* tool's description, and nothing here repeats it.
|
|
8
10
|
*/
|
|
9
11
|
export const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis projects. A project is an ordered list of blocks; each block is one analysis step with its own configuration, and reads the results of blocks above it.
|
|
10
12
|
|
|
11
13
|
Order of work:
|
|
12
14
|
1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.
|
|
13
15
|
2. Project: list_projects finds a project, create_project makes a new one. Every tool that works on a project takes its projectId.
|
|
14
|
-
3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.
|
|
15
|
-
4. Configure a block:
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation (get_block_contract shows the params a kind accepts), or bare, in its default state. Add blocks in the order the data flows.
|
|
17
|
+
4. Configure a block: get_block_params reads its kind's params; write them whole with set_block_params. For what params do not cover (view state, plugin data, blocks with no params), or when set_block_params refuses: read the data with get_block_state and write it whole with set_block_data. Both writes wait for the outputs to settle and return the block's readiness. A block's choices for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs; get_column_values lists a column's values.
|
|
18
|
+
From scratch: add the block bare, read its option outputs, copy the references you need into its params or data. A block offers a result of a block above only once it exists, some only after a run: if an option is missing, run the block above first (run_block, await_block_done). Params and data can name things differently: read the data before writing it.
|
|
19
|
+
Files: list_files finds them, get_file_handles makes import handles to put in the block's params or data. read_file reads a local file or a block's output blob.
|
|
20
|
+
5. Check and run: the readiness (get_block_state and both writes) says whether a block can run and, if not, why, in the block's own words. get_project_overview shows the status of every block. run_block starts a block together with the stale blocks above it; await_block_done waits for it.
|
|
21
|
+
6. Results: get_block_outputs, then list_columns and query_table for tables, get_block_logs for logs. select_block shows a block to the user, only when they ask.
|
|
22
|
+
|
|
23
|
+
Result pool: the columns blocks export for the blocks below them. list_pool_columns lists them with their specs, as one block sees them or for the whole project, and tests a column selector of a block's model against them; get_pool_column gives one whole spec. A block's choices are in its own option outputs; a pool column's \`ref\`, or its \`columnId\` when it has a \`ref\`, fits a block field of that form (get_block_contract).
|
|
24
|
+
To filter or join large results (columns of many blocks), create_table makes a temporary table of pool columns for query_table; drop_table frees it.
|
|
18
25
|
|
|
19
26
|
Rules:
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
- Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.
|
|
23
|
-
- Only blocks on the current block API can be added.
|
|
24
|
-
- select_block shows a block to the user in the application; use it only when the user asks to be shown something.
|
|
25
|
-
- capture_screenshot, click, type_text, press_key, scroll and execute_js drive the application window. They are for testing the application; configuring and running blocks does not need them.`;
|
|
27
|
+
- Ids, references and handles come from tool results: copy them, do not compose them. A block param takes the form get_block_contract declares: a column's \`ref\` for a PlRef object, its \`columnId\` for an id string, \`valueIds\` for picked values. Tools that take a column (create_table, get_pool_column, get_column_values) take its \`columnId\`. The one exception: a reference to a result of a block above, {__isRef: true, blockId, name}, may be written by hand when get_block_contract declares that shape and you know the result's name, with the real id of a block above.
|
|
28
|
+
- Changing a block's params or data makes its results, and those of blocks that read them, stale until they run again.`;
|
|
@@ -174,10 +174,189 @@ describe("ProjectHolds", () => {
|
|
|
174
174
|
expect(released).toEqual([]);
|
|
175
175
|
});
|
|
176
176
|
|
|
177
|
+
test("a kept hold the middle layer ends is told about once; one the server gives back is not", async () => {
|
|
178
|
+
const ended: ProjectId[] = [];
|
|
179
|
+
holds = new ProjectHolds<FakeProject>({
|
|
180
|
+
holder: HOLDER,
|
|
181
|
+
idleMs: IDLE_MS,
|
|
182
|
+
onEnded: (id) => ended.push(id),
|
|
183
|
+
});
|
|
184
|
+
await holds.use(source, P1, async () => {});
|
|
185
|
+
await holds.use(source, P2, async () => {});
|
|
186
|
+
for (const hold of source.live) if (hold.project.id === P1) hold.end("deleted");
|
|
187
|
+
holds.releaseAll();
|
|
188
|
+
expect(ended).toEqual([P1]);
|
|
189
|
+
});
|
|
190
|
+
|
|
177
191
|
test("a call whose hold ended during it keeps nothing", async () => {
|
|
178
192
|
await holds.use(source, P1, async () => {
|
|
179
193
|
for (const hold of source.live) hold.end("deleted");
|
|
180
194
|
});
|
|
181
195
|
expect(source.live.size).toBe(0);
|
|
182
196
|
});
|
|
197
|
+
|
|
198
|
+
describe("while the project has pending work (Q-002)", () => {
|
|
199
|
+
let pending: Map<ProjectId, number>;
|
|
200
|
+
let bytes: Map<ProjectId, number>;
|
|
201
|
+
let checks: ProjectId[];
|
|
202
|
+
let kept: [ProjectId, string, number][];
|
|
203
|
+
|
|
204
|
+
beforeEach(() => {
|
|
205
|
+
pending = new Map();
|
|
206
|
+
bytes = new Map();
|
|
207
|
+
checks = [];
|
|
208
|
+
kept = [];
|
|
209
|
+
holds = new ProjectHolds<FakeProject>({
|
|
210
|
+
holder: HOLDER,
|
|
211
|
+
idleMs: IDLE_MS,
|
|
212
|
+
onRelease: (id, reason) => released.push([id, reason]),
|
|
213
|
+
pendingWork: async (id, project) => {
|
|
214
|
+
expect(project.id).toBe(id);
|
|
215
|
+
checks.push(id);
|
|
216
|
+
const n = pending.get(id) ?? 0;
|
|
217
|
+
if (n < 0) throw new Error("cannot tell");
|
|
218
|
+
return { count: n, progress: bytes.get(id) ?? 0 };
|
|
219
|
+
},
|
|
220
|
+
onKeep: (id, reason, n) => kept.push([id, reason, n]),
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("the hold is kept, idle time after idle time, until the work is done", async () => {
|
|
225
|
+
pending.set(P1, 2);
|
|
226
|
+
await holds.use(source, P1, async () => {});
|
|
227
|
+
|
|
228
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
229
|
+
expect(source.holds(P1)).toBe(1);
|
|
230
|
+
expect(kept).toEqual([[P1, "imports", 2]]);
|
|
231
|
+
|
|
232
|
+
pending.set(P1, 1);
|
|
233
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
234
|
+
expect(source.holds(P1)).toBe(1);
|
|
235
|
+
expect(kept).toEqual([
|
|
236
|
+
[P1, "imports", 2],
|
|
237
|
+
[P1, "imports", 1],
|
|
238
|
+
]);
|
|
239
|
+
|
|
240
|
+
pending.set(P1, 0);
|
|
241
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
242
|
+
expect(source.holds(P1)).toBe(0);
|
|
243
|
+
expect(released).toEqual([[P1, "idle"]]);
|
|
244
|
+
expect(checks).toEqual([P1, P1, P1]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("work that does not move is let go after 5 idle times in a row, saying why", async () => {
|
|
248
|
+
pending.set(P1, 2);
|
|
249
|
+
await holds.use(source, P1, async () => {});
|
|
250
|
+
// The first check has nothing to compare with; five more find the same work.
|
|
251
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS * 5);
|
|
252
|
+
expect(source.holds(P1)).toBe(1);
|
|
253
|
+
expect(kept).toHaveLength(5);
|
|
254
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
255
|
+
expect(source.holds(P1)).toBe(0);
|
|
256
|
+
expect(released).toEqual([[P1, "uploads-stalled"]]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("bytes moving keep the hold; a call starts the count again", async () => {
|
|
260
|
+
pending.set(P1, 1);
|
|
261
|
+
await holds.use(source, P1, async () => {});
|
|
262
|
+
for (let i = 1; i <= 8; i++) {
|
|
263
|
+
bytes.set(P1, i * 100);
|
|
264
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
265
|
+
}
|
|
266
|
+
expect(source.holds(P1)).toBe(1);
|
|
267
|
+
|
|
268
|
+
// Stuck now: four idle times without progress, then a call, then four more.
|
|
269
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS * 4);
|
|
270
|
+
await holds.use(source, P1, async () => {});
|
|
271
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS * 4);
|
|
272
|
+
expect(source.holds(P1)).toBe(1);
|
|
273
|
+
expect(released).toEqual([]);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("a project with no pending work is released as before", async () => {
|
|
277
|
+
await holds.use(source, P1, async () => {});
|
|
278
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
279
|
+
expect(source.holds(P1)).toBe(0);
|
|
280
|
+
expect(kept).toEqual([]);
|
|
281
|
+
expect(released).toEqual([[P1, "idle"]]);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test("a check that fails releases the hold", async () => {
|
|
285
|
+
pending.set(P1, -1);
|
|
286
|
+
await holds.use(source, P1, async () => {});
|
|
287
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
288
|
+
expect(source.holds(P1)).toBe(0);
|
|
289
|
+
expect(released).toEqual([[P1, "idle"]]);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("a call that ends during the check restarts the idle time; the check's answer is dropped", async () => {
|
|
293
|
+
const gate = deferred();
|
|
294
|
+
holds = new ProjectHolds<FakeProject>({
|
|
295
|
+
holder: HOLDER,
|
|
296
|
+
idleMs: IDLE_MS,
|
|
297
|
+
onRelease: (id, reason) => released.push([id, reason]),
|
|
298
|
+
pendingWork: async () => {
|
|
299
|
+
await gate.promise;
|
|
300
|
+
return { count: 0, progress: 0 };
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
await holds.use(source, P1, async () => {});
|
|
304
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
305
|
+
await holds.use(source, P1, async () => {});
|
|
306
|
+
gate.resolve();
|
|
307
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
308
|
+
expect(source.holds(P1)).toBe(1);
|
|
309
|
+
expect(released).toEqual([]);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("releaseAll during the check wins", async () => {
|
|
313
|
+
const gate = deferred();
|
|
314
|
+
holds = new ProjectHolds<FakeProject>({
|
|
315
|
+
holder: HOLDER,
|
|
316
|
+
idleMs: IDLE_MS,
|
|
317
|
+
onRelease: (id, reason) => released.push([id, reason]),
|
|
318
|
+
pendingWork: async () => {
|
|
319
|
+
await gate.promise;
|
|
320
|
+
return { count: 3, progress: 0 };
|
|
321
|
+
},
|
|
322
|
+
onKeep: (id, reason, n) => kept.push([id, reason, n]),
|
|
323
|
+
});
|
|
324
|
+
await holds.use(source, P1, async () => {});
|
|
325
|
+
await vi.advanceTimersByTimeAsync(IDLE_MS);
|
|
326
|
+
holds.releaseAll();
|
|
327
|
+
gate.resolve();
|
|
328
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
329
|
+
expect(source.holds(P1)).toBe(0);
|
|
330
|
+
expect(kept).toEqual([]);
|
|
331
|
+
expect(released).toEqual([[P1, "release-all"]]);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe("calls in flight", () => {
|
|
336
|
+
test("are counted per project from before the acquire until the call ends", async () => {
|
|
337
|
+
const gate = deferred();
|
|
338
|
+
const call = holds.use(source, P1, () => gate.promise);
|
|
339
|
+
expect(holds.callsInFlight(P1)).toBe(1);
|
|
340
|
+
expect(holds.callsInFlight(P2)).toBe(0);
|
|
341
|
+
const second = holds.use(source, P1, async () => {});
|
|
342
|
+
expect(holds.callsInFlight(P1)).toBe(2);
|
|
343
|
+
await second;
|
|
344
|
+
expect(holds.callsInFlight(P1)).toBe(1);
|
|
345
|
+
gate.resolve();
|
|
346
|
+
await call;
|
|
347
|
+
// The kept hold is no call.
|
|
348
|
+
expect(holds.callsInFlight(P1)).toBe(0);
|
|
349
|
+
expect(source.holds(P1)).toBe(1);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test("a call whose acquire fails is no longer counted", async () => {
|
|
353
|
+
const failing: HoldSource<FakeProject> = {
|
|
354
|
+
acquireProject: async () => {
|
|
355
|
+
throw new Error("not found");
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
await expect(holds.use(failing, P1, async () => {})).rejects.toThrow("not found");
|
|
359
|
+
expect(holds.callsInFlight(P1)).toBe(0);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
183
362
|
});
|