@mozhi0012/pi-terminal-worker 0.3.0 → 0.4.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/package.json CHANGED
@@ -1,75 +1,78 @@
1
- {
2
- "name": "@mozhi0012/pi-terminal-worker",
3
- "version": "0.3.0",
4
- "description": "Pi coding agent extension for supervised worker Pi in independent Windows Terminal window with bi-directional IPC",
5
- "type": "module",
6
- "main": "./dist/extension.js",
7
- "types": "./dist/extension.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/extension.d.ts",
11
- "import": "./dist/extension.js"
12
- }
13
- },
14
- "files": [
15
- "dist",
16
- "runtime",
17
- "src",
18
- "README.md",
19
- "LICENSE"
20
- ],
21
- "keywords": [
22
- "pi-package",
23
- "pi-extension",
24
- "coding-agent",
25
- "windows-terminal",
26
- "subagent",
27
- "worker"
28
- ],
29
- "pi": {
30
- "extensions": [
31
- "./src/extension.ts"
32
- ]
33
- },
34
- "scripts": {
35
- "build": "tsc",
36
- "typecheck": "tsc --noEmit",
37
- "test": "node --test test/**/*.test.ts",
38
- "prepack": "npm run build",
39
- "prepublishOnly": "npm run build"
40
- },
41
- "repository": {
42
- "type": "git",
43
- "url": "git+https://github.com/mozhi012/pi-terminal-worker.git"
44
- },
45
- "author": "mozhi012 <mozhi0012@gmail.com>",
46
- "license": "MIT",
47
- "publishConfig": {
48
- "access": "public"
49
- },
50
- "bugs": {
51
- "url": "https://github.com/mozhi012/pi-terminal-worker/issues"
52
- },
53
- "homepage": "https://github.com/mozhi012/pi-terminal-worker#readme",
54
- "peerDependencies": {
55
- "@earendil-works/pi-coding-agent": ">=0.87.0",
56
- "typebox": ">=1.0.0"
57
- },
58
- "peerDependenciesMeta": {
59
- "@earendil-works/pi-coding-agent": {
60
- "optional": true
61
- },
62
- "typebox": {
63
- "optional": true
64
- }
65
- },
66
- "devDependencies": {
67
- "@earendil-works/pi-coding-agent": "^1.2.0",
68
- "@types/node": "^22.13.0",
69
- "typebox": "^1.3.27",
70
- "typescript": "^5.7.3"
71
- },
72
- "dependencies": {
73
- "yaml": "^2.9.1"
74
- }
75
- }
1
+ {
2
+ "name": "@mozhi0012/pi-terminal-worker",
3
+ "version": "0.4.0",
4
+ "description": "Pi coding agent extension for supervised workers on Windows and Linux via herdr, tmux and Windows terminals with bi-directional IPC",
5
+ "type": "module",
6
+ "main": "./dist/extension.js",
7
+ "types": "./dist/extension.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/extension.d.ts",
11
+ "import": "./dist/extension.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "runtime",
17
+ "src",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "keywords": [
22
+ "pi-package",
23
+ "pi-extension",
24
+ "coding-agent",
25
+ "windows-terminal",
26
+ "linux",
27
+ "herdr",
28
+ "tmux",
29
+ "subagent",
30
+ "worker"
31
+ ],
32
+ "pi": {
33
+ "extensions": [
34
+ "./src/extension.ts"
35
+ ]
36
+ },
37
+ "scripts": {
38
+ "build": "tsc",
39
+ "typecheck": "tsc --noEmit",
40
+ "test": "node --test test/**/*.test.ts",
41
+ "prepack": "npm run build",
42
+ "prepublishOnly": "npm run build"
43
+ },
44
+ "repository": {
45
+ "type": "git",
46
+ "url": "git+https://github.com/mozhi012/pi-terminal-worker.git"
47
+ },
48
+ "author": "mozhi012 <mozhi0012@gmail.com>",
49
+ "license": "MIT",
50
+ "publishConfig": {
51
+ "access": "public"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/mozhi012/pi-terminal-worker/issues"
55
+ },
56
+ "homepage": "https://github.com/mozhi012/pi-terminal-worker#readme",
57
+ "peerDependencies": {
58
+ "@earendil-works/pi-coding-agent": ">=0.87.0",
59
+ "typebox": ">=1.0.0"
60
+ },
61
+ "peerDependenciesMeta": {
62
+ "@earendil-works/pi-coding-agent": {
63
+ "optional": true
64
+ },
65
+ "typebox": {
66
+ "optional": true
67
+ }
68
+ },
69
+ "devDependencies": {
70
+ "@earendil-works/pi-coding-agent": "^1.2.0",
71
+ "@types/node": "^22.13.0",
72
+ "typebox": "^1.3.27",
73
+ "typescript": "^5.7.3"
74
+ },
75
+ "dependencies": {
76
+ "yaml": "^2.9.1"
77
+ }
78
+ }
@@ -1,67 +1,68 @@
1
- /** 与 supervisor 入口分离的纯校验和可注入父进程监测,便于无终端验证。 */
2
- export function validateDescriptor(descriptor) {
3
- if (!descriptor || typeof descriptor !== "object" || Array.isArray(descriptor) || descriptor.version !== 1) {
4
- throw new Error("描述符格式不符合规范");
5
- }
6
- for (const field of ["controllerId", "workerId", "pipePath", "bootstrapToken"]) {
7
- if (typeof descriptor[field] !== "string") throw new Error(`描述符字段非法: ${field}`);
8
- }
9
- if (descriptor.role !== undefined && descriptor.role !== "worker" && descriptor.role !== "lead") {
10
- throw new Error("描述符 role 必须是 worker 或 lead");
11
- }
12
- if (descriptor.ownerPid !== undefined && (!Number.isSafeInteger(descriptor.ownerPid) || descriptor.ownerPid <= 0)) {
13
- throw new Error("描述符 ownerPid 必须是正整数");
14
- }
15
- return descriptor;
16
- }
17
-
18
- /**
19
- * 组装 Worker 进程环境。工具策略必须先清除再按本次 launch 设置:
20
- * 受限的 lead 启动子实例时,子实例不能继承 lead 自己的策略。
21
- */
22
- export function buildChildEnv(baseEnv, { role, controllerId, workerId, workerPipePath, workerToken, toolPolicy }) {
23
- const env = {
24
- ...baseEnv,
25
- PI_TERMINAL_WORKER_ROLE: role ?? "worker",
26
- PI_TERMINAL_WORKER_CONTROLLER_ID: controllerId,
27
- PI_TERMINAL_WORKER_WORKER_ID: workerId,
28
- PI_TERMINAL_WORKER_PIPE_PATH: workerPipePath,
29
- PI_TERMINAL_WORKER_TOKEN: workerToken,
30
- };
31
- delete env.PI_TERMINAL_WORKER_TOOL_POLICY;
32
- if (toolPolicy) env.PI_TERMINAL_WORKER_TOOL_POLICY = toolPolicy;
33
- return env;
34
- }
35
-
36
- export function isProcessAlive(pid) {
37
- try {
38
- process.kill(pid, 0);
39
- return true;
40
- } catch (error) {
41
- // 权限不足并不意味着进程已死;只在明确不存在时执行级联终止。
42
- return error?.code !== "ESRCH";
43
- }
44
- }
45
-
46
- /** 仅带 ownerPid 的 L2 启用;stop 幂等,owner 消失后最多通知一次。 */
47
- export function monitorOwner(ownerPid, onOwnerLost, {
48
- isAlive = isProcessAlive,
49
- schedule = setInterval,
50
- cancel = clearInterval,
51
- intervalMs = 4000,
52
- } = {}) {
53
- if (ownerPid === undefined) return () => {};
54
- let stopped = false;
55
- const timer = schedule(() => {
56
- if (stopped || isAlive(ownerPid)) return;
57
- stop();
58
- onOwnerLost();
59
- }, intervalMs);
60
- timer.unref?.();
61
- function stop() {
62
- if (stopped) return;
63
- stopped = true;
64
- cancel(timer);
65
- }
66
- return stop;
67
- }
1
+ /** 与 supervisor 入口分离的纯校验和可注入父进程监测,便于无终端验证。 */
2
+ export function validateDescriptor(descriptor) {
3
+ if (!descriptor || typeof descriptor !== "object" || Array.isArray(descriptor) || descriptor.version !== 1) {
4
+ throw new Error("描述符格式不符合规范");
5
+ }
6
+ for (const field of ["controllerId", "workerId", "pipePath", "bootstrapToken"]) {
7
+ if (typeof descriptor[field] !== "string") throw new Error(`描述符字段非法: ${field}`);
8
+ }
9
+ if (descriptor.role !== undefined && descriptor.role !== "worker" && descriptor.role !== "lead") {
10
+ throw new Error("描述符 role 必须是 worker 或 lead");
11
+ }
12
+ if (descriptor.ownerPid !== undefined && (!Number.isSafeInteger(descriptor.ownerPid) || descriptor.ownerPid <= 0)) {
13
+ throw new Error("描述符 ownerPid 必须是正整数");
14
+ }
15
+ return descriptor;
16
+ }
17
+
18
+ /**
19
+ * 组装 Worker 进程环境。工具策略必须先清除再按本次 launch 设置:
20
+ * 受限的 lead 启动子实例时,子实例不能继承 lead 自己的策略。
21
+ */
22
+ export function buildChildEnv(baseEnv, { role, controllerId, workerId, workerPipePath, workerToken, toolPolicy }) {
23
+ const env = {
24
+ ...baseEnv,
25
+ PI_TERMINAL_WORKER_ROLE: role ?? "worker",
26
+ PI_TERMINAL_WORKER_CONTROLLER_ID: controllerId,
27
+ PI_TERMINAL_WORKER_WORKER_ID: workerId,
28
+ PI_TERMINAL_WORKER_PIPE_PATH: workerPipePath,
29
+ PI_TERMINAL_WORKER_TOKEN: workerToken,
30
+ };
31
+ delete env.PI_TERMINAL_WORKER_TOOL_POLICY;
32
+ for (const key of Object.keys(env)) if (key.startsWith("PITW_BOOTSTRAP_")) delete env[key];
33
+ if (toolPolicy) env.PI_TERMINAL_WORKER_TOOL_POLICY = toolPolicy;
34
+ return env;
35
+ }
36
+
37
+ export function isProcessAlive(pid) {
38
+ try {
39
+ process.kill(pid, 0);
40
+ return true;
41
+ } catch (error) {
42
+ // 权限不足并不意味着进程已死;只在明确不存在时执行级联终止。
43
+ return error?.code !== "ESRCH";
44
+ }
45
+ }
46
+
47
+ /** 仅带 ownerPid 的 L2 启用;stop 幂等,owner 消失后最多通知一次。 */
48
+ export function monitorOwner(ownerPid, onOwnerLost, {
49
+ isAlive = isProcessAlive,
50
+ schedule = setInterval,
51
+ cancel = clearInterval,
52
+ intervalMs = 4000,
53
+ } = {}) {
54
+ if (ownerPid === undefined) return () => {};
55
+ let stopped = false;
56
+ const timer = schedule(() => {
57
+ if (stopped || isAlive(ownerPid)) return;
58
+ stop();
59
+ onOwnerLost();
60
+ }, intervalMs);
61
+ timer.unref?.();
62
+ function stop() {
63
+ if (stopped) return;
64
+ stopped = true;
65
+ cancel(timer);
66
+ }
67
+ return stop;
68
+ }