@minhtri2710/pi-durable-shared 1.0.0 → 2.1.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 +12 -5
- package/dist/mcp-tools.d.ts +2 -4
- package/dist/mcp-tools.js +16 -17
- package/dist/tool-arguments.d.ts +2 -0
- package/dist/tool-arguments.js +27 -0
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -8,23 +8,30 @@ Install with `npm install @minhtri2710/pi-durable-shared`. Import each as `@minh
|
|
|
8
8
|
|
|
9
9
|
- `process-state`: `processState(key, init)`, the one way to keep state on `globalThis` so it outlives a `/reload` and is shared by every copy of this package in the process. The key is used as given (`pi-guards` reads `pi-worker-mcp`).
|
|
10
10
|
- `durable-runtime`, `session-store`, `session-lock`: the per-session durable harness, its SQLite store and its lock (a stale reclaim claim ages by `ctime`).
|
|
11
|
-
- `mcp-tools`, `mcp-oauth`: MCP servers for workers
|
|
11
|
+
- `mcp-tools`, `mcp-oauth`: MCP servers for workers: every server of the user-level `<agent dir>/mcp.json` except those with `enabled: false` (off everywhere) or `workers: false` (off for workers only), connected separately from Pi's own. A switched-off entry is skipped before validation, so it never yields an error. An invalid or failing active server is reported in `errors`, not thrown. `$NAME`, `${NAME}` and `$$` expand in `env` and `headers`. A script sees the limited content, not the server's raw blocks or `_meta`.
|
|
12
12
|
- `worker-codemode`: the code mode sandbox and tool. Scripts run at most `max(4, availableParallelism)` at a time (`PI_CODEMODE_MAX_WORKERS` overrides); one over the limit waits, and leaves the queue when its signal aborts. `configureWorkerCodemode({ inlineBudget })` sets the budget the sandbox is created with.
|
|
13
13
|
- `search-tools`: `grep` and `find` for workers. `execWithTimeout` takes milliseconds and passes seconds to `exec`.
|
|
14
14
|
- `retention`: removes `pi-mcp-*` temp files that are regular, owned by the current user, mode 0600 and older than 7 days; runs once per process.
|
|
15
15
|
- `project-rules`: the AGENTS.md / CLAUDE.md text for a worker's instructions.
|
|
16
|
+
- `tool-arguments`: `parseJsonStringArguments(schema)`, for a tool's `prepareArguments`: it parses arguments that arrive as a JSON string into the object the schema describes.
|
|
16
17
|
- `testing`: `loadTwice(specifier)`, which loads a module twice as a reload does.
|
|
17
18
|
|
|
18
19
|
`pi-lint-state` (a `bin`) fails when a file that defines tools or tasks has module-level `let`, `new Map/Set` or a raw `??=` slot instead of `processState`. The consumers run it as `npm run lint:state`, first in `npm test`. It needs ripgrep.
|
|
19
20
|
|
|
20
|
-
## Develop
|
|
21
|
+
## Develop against a local build
|
|
22
|
+
|
|
23
|
+
`@earendil-works/pi-durable` and `@earendil-works/chord` are peer dependencies, along with pi-ai, pi-coding-agent, pi-tui and typebox. The host supplies them, so the host and this package run one copy of each. Per-copy state, such as the signal tokens made by `defineSignal`, only works within a single copy.
|
|
24
|
+
|
|
25
|
+
Do not use `npm link`. It symlinks the consumer to this directory, so Node resolves `@earendil-works/pi-durable` from here as a second copy. A token from the host's `defineSignal` is then unknown to the harness opened here, and `harness.signal` throws `Signal ... was not built by defineSignal()`.
|
|
26
|
+
|
|
27
|
+
Install a real copy of the local build instead:
|
|
21
28
|
|
|
22
29
|
```sh
|
|
23
30
|
cd pi-durable-shared && npm install && npm run build
|
|
24
|
-
cd ../pi-swarm && npm
|
|
31
|
+
cd ../pi-swarm && npm install --no-save --install-links ../pi-durable-shared # likewise pi-teamwork, pi-orchestrator, pi-schedule
|
|
25
32
|
```
|
|
26
33
|
|
|
27
|
-
`npm
|
|
34
|
+
Run `npm run build` here and repeat the install in the consumer after each change. `--no-save` leaves the consumer's `package.json` alone, and a plain `npm install` in the consumer replaces the copy with the published version.
|
|
28
35
|
|
|
29
36
|
```sh
|
|
30
37
|
npm test # node --test src/*.test.ts
|
|
@@ -33,7 +40,7 @@ npm run typecheck # tsc -p .
|
|
|
33
40
|
|
|
34
41
|
## Publish to npm
|
|
35
42
|
|
|
36
|
-
Consumers depend on `"@minhtri2710/pi-durable-shared": "^1.0
|
|
43
|
+
Consumers depend on `"@minhtri2710/pi-durable-shared": "^2.1.0"`. The package ships a prebuilt `dist`, so installing it runs no scripts. `prepublishOnly` builds and tests before each publish.
|
|
37
44
|
|
|
38
45
|
```sh
|
|
39
46
|
npm login # once, in your terminal
|
package/dist/mcp-tools.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { type Extension, type ToolRegistration } from "@earendil-works/pi-durable";
|
|
2
2
|
import { McpClient } from "@earendil-works/pi-mcp";
|
|
3
|
-
/** `<agent dir>/pi-worker-mcp.json`: `{ "servers": ["name", ...] }`, names from the user's `mcp.json`. Workers get no MCP tool unless listed. */
|
|
4
|
-
export declare const WORKER_MCP_FILE = "pi-worker-mcp.json";
|
|
5
3
|
/** `extensions`: one per connected server, each a code mode namespace. `tools`: all their tools. */
|
|
6
4
|
type Link = {
|
|
7
5
|
client: McpClient;
|
|
@@ -28,12 +26,12 @@ type AuthResolver = (provider: string) => Promise<string | undefined>;
|
|
|
28
26
|
export declare function setWorkerMcpAuthResolver(resolver: AuthResolver | undefined): void;
|
|
29
27
|
export declare const expand: (text: string) => string;
|
|
30
28
|
/**
|
|
31
|
-
* The MCP tools workers may call, connected once per process; `errors` names servers that were
|
|
29
|
+
* The MCP tools workers may call, connected once per process; `errors` names servers of `mcp.json` that were unusable.
|
|
32
30
|
* A set with errors is opened again once `RETRY_MS` have passed, so a server that was down, or a fixed configuration, is
|
|
33
31
|
* picked up without a restart.
|
|
34
32
|
*/
|
|
35
33
|
export declare function workerMcp(): Promise<Connected>;
|
|
36
|
-
/** Marks the cached MCP set stale so the next `workerMcp()` call reconciles
|
|
34
|
+
/** Marks the cached MCP set stale so the next `workerMcp()` call reconciles the user-level `mcp.json`. */
|
|
37
35
|
export declare function invalidateWorkerMcp(): void;
|
|
38
36
|
/** Closes the connections; the next `workerMcp()` connects again. */
|
|
39
37
|
export declare function closeWorkerMcp(): Promise<void>;
|
package/dist/mcp-tools.js
CHANGED
|
@@ -10,8 +10,6 @@ import { McpOAuthAuthorizationRequiredError } from "@earendil-works/pi-mcp/oauth
|
|
|
10
10
|
import { processState } from "./process-state.js";
|
|
11
11
|
import { sweepTempFilesOnce } from "./retention.js";
|
|
12
12
|
import { checkMetadataDocumentCallback, usesOAuth, workerAuthProvider } from "./mcp-oauth.js";
|
|
13
|
-
/** `<agent dir>/pi-worker-mcp.json`: `{ "servers": ["name", ...] }`, names from the user's `mcp.json`. Workers get no MCP tool unless listed. */
|
|
14
|
-
export const WORKER_MCP_FILE = "pi-worker-mcp.json";
|
|
15
13
|
const CONNECT_MS = 20_000;
|
|
16
14
|
/** A set with an unusable server is opened again by a call this long after it, keeping the servers that connected. */
|
|
17
15
|
const RETRY_MS = 30_000;
|
|
@@ -82,26 +80,27 @@ async function saveToTempFile(data, extension) {
|
|
|
82
80
|
await writeFile(file, data, { flag: "wx", mode: 0o600 });
|
|
83
81
|
return file;
|
|
84
82
|
}
|
|
85
|
-
/** The servers workers
|
|
83
|
+
/** The servers workers use: every server the user-level `<agent dir>/mcp.json` defines, except those with `enabled: false` (off everywhere) or `workers: false` (off for workers only). Those are skipped before validation, so a switched-off entry never yields an error. An invalid active one is reported in `errors`. */
|
|
86
84
|
function selectedServers(errors) {
|
|
87
|
-
const listed = readJson(join(getAgentDir(), WORKER_MCP_FILE))?.servers;
|
|
88
|
-
if (listed === undefined)
|
|
89
|
-
return [];
|
|
90
|
-
if (!Array.isArray(listed) || listed.some((name) => typeof name !== "string")) {
|
|
91
|
-
errors.push(`${WORKER_MCP_FILE}: servers must be a list of server names`);
|
|
92
|
-
return [];
|
|
93
|
-
}
|
|
94
85
|
const defined = readJson(join(getAgentDir(), "mcp.json"))?.mcpServers ?? {};
|
|
95
86
|
const chosen = [];
|
|
96
87
|
const namespaces = new Map();
|
|
97
|
-
for (const name of
|
|
98
|
-
const
|
|
88
|
+
for (const name of Object.keys(defined)) {
|
|
89
|
+
const entry = defined[name];
|
|
90
|
+
if (entry && typeof entry === "object") {
|
|
91
|
+
const { enabled, workers } = entry;
|
|
92
|
+
if (workers !== undefined && typeof workers !== "boolean") {
|
|
93
|
+
errors.push(`${name}: workers must be true or false`);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (enabled === false || workers === false)
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const checked = validateMcpServerConfig(name, entry, { checkMetadataDocumentCallback });
|
|
99
100
|
if (checked.kind === "invalid") {
|
|
100
|
-
errors.push(
|
|
101
|
+
errors.push(checked.error);
|
|
101
102
|
continue;
|
|
102
103
|
}
|
|
103
|
-
if (checked.config.enabled === false)
|
|
104
|
-
continue;
|
|
105
104
|
const ns = mcpNamespace(name);
|
|
106
105
|
const existing = namespaces.get(ns);
|
|
107
106
|
if (existing && existing !== name) {
|
|
@@ -300,7 +299,7 @@ async function open(previous) {
|
|
|
300
299
|
return { served, links: served.map((item) => item.link), errors, extensions, tools: extensions.flatMap((extension) => [...(extension.tools ?? [])]), at: Date.now() };
|
|
301
300
|
}
|
|
302
301
|
/**
|
|
303
|
-
* The MCP tools workers may call, connected once per process; `errors` names servers that were
|
|
302
|
+
* The MCP tools workers may call, connected once per process; `errors` names servers of `mcp.json` that were unusable.
|
|
304
303
|
* A set with errors is opened again once `RETRY_MS` have passed, so a server that was down, or a fixed configuration, is
|
|
305
304
|
* picked up without a restart.
|
|
306
305
|
*/
|
|
@@ -318,7 +317,7 @@ export function workerMcp() {
|
|
|
318
317
|
shared.connecting = undefined; throw error; });
|
|
319
318
|
return connecting;
|
|
320
319
|
}
|
|
321
|
-
/** Marks the cached MCP set stale so the next `workerMcp()` call reconciles
|
|
320
|
+
/** Marks the cached MCP set stale so the next `workerMcp()` call reconciles the user-level `mcp.json`. */
|
|
322
321
|
export function invalidateWorkerMcp() {
|
|
323
322
|
if (shared.settled)
|
|
324
323
|
shared.stale = true;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
// Models sometimes send nested array/object parameters as JSON strings; parse those so schema validation sees the real type.
|
|
3
|
+
export function parseJsonStringArguments(schema) {
|
|
4
|
+
const properties = schema.properties ?? {};
|
|
5
|
+
const structured = Object.entries(properties).flatMap(([key, property]) => property.type === "array" || property.type === "object" ? [[key, property.type]] : []);
|
|
6
|
+
return (args) => {
|
|
7
|
+
if (!isPlainObject(args))
|
|
8
|
+
return args;
|
|
9
|
+
let result = args;
|
|
10
|
+
for (const [key, type] of structured) {
|
|
11
|
+
const value = args[key];
|
|
12
|
+
if (typeof value !== "string")
|
|
13
|
+
continue;
|
|
14
|
+
let parsed;
|
|
15
|
+
try {
|
|
16
|
+
parsed = JSON.parse(value);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (type === "array" ? Array.isArray(parsed) : isPlainObject(parsed)) {
|
|
22
|
+
result = { ...result, [key]: parsed };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@minhtri2710/pi-durable-shared",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Durable runtime, MCP, search and code mode helpers shared by the pi-orchestrator, pi-swarm, pi-teamwork and pi-schedule extensions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"keywords": [
|
|
7
|
+
"keywords": [
|
|
8
|
+
"pi",
|
|
9
|
+
"pi-extension",
|
|
10
|
+
"durable",
|
|
11
|
+
"mcp",
|
|
12
|
+
"codemode"
|
|
13
|
+
],
|
|
8
14
|
"repository": {
|
|
9
15
|
"type": "git",
|
|
10
16
|
"url": "git+https://github.com/minhtri2710/pi-durable-shared.git"
|
|
@@ -30,21 +36,23 @@
|
|
|
30
36
|
"prepublishOnly": "npm run build && npm test"
|
|
31
37
|
},
|
|
32
38
|
"dependencies": {
|
|
33
|
-
"@earendil-works/chord": "1.8.0",
|
|
34
39
|
"@earendil-works/pi-codemode": "1.8.0",
|
|
35
|
-
"@earendil-works/pi-durable": "1.8.0",
|
|
36
40
|
"@earendil-works/pi-mcp": "1.8.0",
|
|
37
41
|
"proper-lockfile": "4.1.2"
|
|
38
42
|
},
|
|
39
43
|
"peerDependencies": {
|
|
44
|
+
"@earendil-works/chord": "1.8.0",
|
|
40
45
|
"@earendil-works/pi-ai": "*",
|
|
41
46
|
"@earendil-works/pi-coding-agent": "*",
|
|
47
|
+
"@earendil-works/pi-durable": "1.8.0",
|
|
42
48
|
"@earendil-works/pi-tui": "*",
|
|
43
49
|
"typebox": "*"
|
|
44
50
|
},
|
|
45
51
|
"devDependencies": {
|
|
52
|
+
"@earendil-works/chord": "1.8.0",
|
|
46
53
|
"@earendil-works/pi-ai": "1.8.0",
|
|
47
54
|
"@earendil-works/pi-coding-agent": "1.8.0",
|
|
55
|
+
"@earendil-works/pi-durable": "1.8.0",
|
|
48
56
|
"@earendil-works/pi-tui": "1.8.0",
|
|
49
57
|
"@types/node": "^22.20.7",
|
|
50
58
|
"@types/proper-lockfile": "4.1.4",
|