@tinoy/pi-no-subagent-fork 0.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/LICENSE +21 -0
- package/README.md +28 -0
- package/no-subagent-fork.ts +98 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tinoy Thomas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @tinoy/pi-no-subagent-fork
|
|
2
|
+
|
|
3
|
+
Rewrite a subagent spawn's `context: "fork"` to `"fresh"`.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pi install npm:@tinoy/pi-no-subagent-fork
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## What it needs at call time
|
|
10
|
+
|
|
11
|
+
Nothing: with no subagent tool the hook is never reached.
|
|
12
|
+
|
|
13
|
+
## Registers
|
|
14
|
+
|
|
15
|
+
no tool
|
|
16
|
+
|
|
17
|
+
## Caveats
|
|
18
|
+
|
|
19
|
+
No caveat rows declared: this unit has no soft dependency on another package in this repository. What it needs beyond its declared dependencies is machine capability, named below, and it refuses by name rather than failing to load.
|
|
20
|
+
|
|
21
|
+
## Dependencies
|
|
22
|
+
|
|
23
|
+
pi-supplied imports (`@earendil-works/pi-coding-agent` or "none") are peer dependencies with a `*` range and are
|
|
24
|
+
never bundled. This package has no npm dependencies.
|
|
25
|
+
|
|
26
|
+
## Licence
|
|
27
|
+
|
|
28
|
+
MIT — see the repository [LICENSE](../../LICENSE).
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* no-subagent-fork: makes `context: "fork"` impossible for subagent spawns.
|
|
3
|
+
*
|
|
4
|
+
* Two layers, because a fork can be requested two ways:
|
|
5
|
+
*
|
|
6
|
+
* 1. IMPLICIT — no `context` on the call, so the resolved policy comes from
|
|
7
|
+
* `defaultSubagentContext`. Left unset, pi-subagents falls back to fork
|
|
8
|
+
* whenever the parent session is persisted and leaf-capable. Killed at the
|
|
9
|
+
* source by the subagent settings route (`"fresh"`).
|
|
10
|
+
*
|
|
11
|
+
* 2. EXPLICIT — `context: "fork"` on the call, or inside a `workflowScript`
|
|
12
|
+
* body. Rewritten to `"fresh"` here: `event.input` is mutable, so the
|
|
13
|
+
* spawn still succeeds and no fork ever happens. A `workflowScriptPath` is
|
|
14
|
+
* handled by reading the file (never mutating it) and blocking, since the
|
|
15
|
+
* caller's source is not ours to edit.
|
|
16
|
+
*
|
|
17
|
+
* Rewrites and blocks are appended to ~/.local/share/pi-no-subagent-fork/log.jsonl
|
|
18
|
+
* for audit; nothing is ever blocked that could have been rewritten.
|
|
19
|
+
*
|
|
20
|
+
* NOT covered: `action: "resume"` replays a stored run's own context, so a run
|
|
21
|
+
* created while forking was allowed still continues its fork. New runs cannot.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
25
|
+
import { homedir } from "node:os";
|
|
26
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
27
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
28
|
+
import { isToolCallEventType } from "@earendil-works/pi-coding-agent";
|
|
29
|
+
|
|
30
|
+
const LOG_DIR = join(homedir(), ".local/share/pi-no-subagent-fork");
|
|
31
|
+
|
|
32
|
+
/** `context: "fork"` / `context:'fork'` / `context : "fork"`, quote-agnostic. */
|
|
33
|
+
const FORK_RE = /context\s*:\s*(["'])fork\1/g;
|
|
34
|
+
|
|
35
|
+
function log(kind: string, detail: string): void {
|
|
36
|
+
try {
|
|
37
|
+
mkdirSync(LOG_DIR, { recursive: true });
|
|
38
|
+
appendFileSync(
|
|
39
|
+
join(LOG_DIR, "log.jsonl"),
|
|
40
|
+
`${JSON.stringify({ ts: new Date().toISOString(), kind, detail: detail.slice(0, 400) })}\n`,
|
|
41
|
+
);
|
|
42
|
+
} catch {
|
|
43
|
+
/* never break a tool call over logging */
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Rewrite every `context: "fork"` occurrence in a workflow script body. */
|
|
48
|
+
export function deforkSource(src: string): string {
|
|
49
|
+
return src.replace(FORK_RE, 'context: "fresh"');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Does a script body or file request fork context anywhere? */
|
|
53
|
+
export function requestsFork(src: string): boolean {
|
|
54
|
+
return new RegExp(FORK_RE.source).test(src);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default function (pi: ExtensionAPI): void {
|
|
58
|
+
pi.on("tool_call", (event, ctx) => {
|
|
59
|
+
if (!isToolCallEventType("subagent", event)) return;
|
|
60
|
+
|
|
61
|
+
// Loose on purpose: the subagent tool belongs to another extension, so its
|
|
62
|
+
// input shape is not importable here.
|
|
63
|
+
const input = event.input as Record<string, unknown>;
|
|
64
|
+
const notes: string[] = [];
|
|
65
|
+
|
|
66
|
+
if (input.context === "fork") {
|
|
67
|
+
input.context = "fresh";
|
|
68
|
+
notes.push("call-level context: fork -> fresh");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (typeof input.workflowScript === "string") {
|
|
72
|
+
const src = input.workflowScript;
|
|
73
|
+
if (requestsFork(src)) {
|
|
74
|
+
input.workflowScript = deforkSource(src);
|
|
75
|
+
notes.push("workflowScript context: fork -> fresh");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (typeof input.workflowScriptPath === "string") {
|
|
80
|
+
const raw = input.workflowScriptPath;
|
|
81
|
+
const file = isAbsolute(raw) ? raw : resolve(ctx.cwd ?? process.cwd(), raw);
|
|
82
|
+
try {
|
|
83
|
+
if (existsSync(file) && requestsFork(readFileSync(file, "utf8"))) {
|
|
84
|
+
log("blocked", `workflowScriptPath ${file}`);
|
|
85
|
+
return {
|
|
86
|
+
block: true,
|
|
87
|
+
reason: `workflowScriptPath '${raw}' requests context: "fork", which is never allowed. Edit it to context: "fresh" (or pass an inline workflowScript, which this hook rewrites automatically).`,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
/* unreadable file: let the tool report its own error */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (notes.length > 0)
|
|
96
|
+
log("rewrote", `${notes.join("; ")} | ${JSON.stringify(input).slice(0, 200)}`);
|
|
97
|
+
});
|
|
98
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tinoy/pi-no-subagent-fork",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Rewrite a subagent spawn's `context: \"fork\"` to `\"fresh\"`.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/tinoy1336/pi-extensions.git",
|
|
9
|
+
"directory": "packages/no-subagent-fork"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/tinoy1336/pi-extensions/tree/main/packages/no-subagent-fork#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/tinoy1336/pi-extensions/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "no-subagent-fork.ts",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"pi-package"
|
|
19
|
+
],
|
|
20
|
+
"files": [
|
|
21
|
+
"no-subagent-fork.ts",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"pi": {
|
|
26
|
+
"extensions": [
|
|
27
|
+
"./no-subagent-fork.ts"
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
35
|
+
}
|
|
36
|
+
}
|