@minhtri2710/pi-durable-shared 2.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 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: every server of the user-level `<agent dir>/mcp.json` that is not `enabled: false`, connected separately from Pi's own. An invalid or failing 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`.
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 with npm link
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 link ../pi-durable-shared # likewise pi-teamwork, pi-orchestrator, pi-schedule
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 link` makes `node_modules/@minhtri2710/pi-durable-shared` a symlink to this directory, so a consumer runs the built `dist`: run `npm run build` (or `npx tsc -p tsconfig.build.json --watch`) after each change here. A consumer's `npm install` removes the link; run `npm link ../pi-durable-shared` again.
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": "^2.0.0"`. The package ships a prebuilt `dist`, so installing it runs no scripts. `prepublishOnly` builds and tests before each publish.
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.js CHANGED
@@ -80,19 +80,27 @@ async function saveToTempFile(data, extension) {
80
80
  await writeFile(file, data, { flag: "wx", mode: 0o600 });
81
81
  return file;
82
82
  }
83
- /** The servers workers use: every server the user-level `<agent dir>/mcp.json` defines that is not `enabled: false`. An invalid one is reported in `errors`. */
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`. */
84
84
  function selectedServers(errors) {
85
85
  const defined = readJson(join(getAgentDir(), "mcp.json"))?.mcpServers ?? {};
86
86
  const chosen = [];
87
87
  const namespaces = new Map();
88
88
  for (const name of Object.keys(defined)) {
89
- const checked = validateMcpServerConfig(name, defined[name], { checkMetadataDocumentCallback });
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 });
90
100
  if (checked.kind === "invalid") {
91
101
  errors.push(checked.error);
92
102
  continue;
93
103
  }
94
- if (checked.config.enabled === false)
95
- continue;
96
104
  const ns = mcpNamespace(name);
97
105
  const existing = namespaces.get(ns);
98
106
  if (existing && existing !== name) {
@@ -0,0 +1,2 @@
1
+ import type { TSchema } from "typebox";
2
+ export declare function parseJsonStringArguments(schema: TSchema): (args: unknown) => any;
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@minhtri2710/pi-durable-shared",
3
- "version": "2.0.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",
@@ -36,21 +36,23 @@
36
36
  "prepublishOnly": "npm run build && npm test"
37
37
  },
38
38
  "dependencies": {
39
- "@earendil-works/chord": "1.8.0",
40
39
  "@earendil-works/pi-codemode": "1.8.0",
41
- "@earendil-works/pi-durable": "1.8.0",
42
40
  "@earendil-works/pi-mcp": "1.8.0",
43
41
  "proper-lockfile": "4.1.2"
44
42
  },
45
43
  "peerDependencies": {
44
+ "@earendil-works/chord": "1.8.0",
46
45
  "@earendil-works/pi-ai": "*",
47
46
  "@earendil-works/pi-coding-agent": "*",
47
+ "@earendil-works/pi-durable": "1.8.0",
48
48
  "@earendil-works/pi-tui": "*",
49
49
  "typebox": "*"
50
50
  },
51
51
  "devDependencies": {
52
+ "@earendil-works/chord": "1.8.0",
52
53
  "@earendil-works/pi-ai": "1.8.0",
53
54
  "@earendil-works/pi-coding-agent": "1.8.0",
55
+ "@earendil-works/pi-durable": "1.8.0",
54
56
  "@earendil-works/pi-tui": "1.8.0",
55
57
  "@types/node": "^22.20.7",
56
58
  "@types/proper-lockfile": "4.1.4",