pi-subagents-extend 0.0.0-stage → 0.1.2
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 +133 -2
- package/bin/pi-subagents-extend-helper-linux-arm64 +0 -0
- package/bin/pi-subagents-extend-helper-linux-x64 +0 -0
- package/package.json +63 -4
- package/skills/pi-subagents-extend/SKILL.md +312 -0
- package/skills/pi-subagents-extend-context/SKILL.md +103 -0
- package/skills/pi-subagents-extend-probes/SKILL.md +151 -0
- package/src/bash/env.ts +108 -0
- package/src/bash/index.ts +27 -0
- package/src/bash/network.ts +97 -0
- package/src/bash/programs.ts +367 -0
- package/src/bash/rules.ts +247 -0
- package/src/bash/tokens.ts +169 -0
- package/src/bash/unreachable.ts +429 -0
- package/src/budgets.ts +89 -0
- package/src/child-context.ts +24 -0
- package/src/context-scope.ts +93 -0
- package/src/deliverable.ts +216 -0
- package/src/dependencies.ts +320 -0
- package/src/handoff-hint.ts +69 -0
- package/src/index.ts +181 -0
- package/src/main-context.ts +30 -0
- package/src/main-skills.ts +77 -0
- package/src/notices.ts +299 -0
- package/src/paths/index.ts +165 -0
- package/src/policy/glob.ts +146 -0
- package/src/policy/index.ts +36 -0
- package/src/policy/payload.ts +312 -0
- package/src/probe.ts +495 -0
- package/src/role-config.ts +434 -0
- package/src/sandbox/grants.ts +339 -0
- package/src/sandbox/helper.ts +137 -0
- package/src/sandbox/index.ts +12 -0
- package/src/sandbox/spec.ts +285 -0
- package/src/sandbox-note.ts +194 -0
- package/src/wiring/agent-start.ts +371 -0
- package/src/wiring/context.ts +216 -0
- package/src/wiring/misc.ts +55 -0
- package/src/wiring/session-start.ts +69 -0
- package/src/wiring/tool-call/anchor.ts +138 -0
- package/src/wiring/tool-call/bash-path.ts +0 -0
- package/src/wiring/tool-call/dispatch-preflight.ts +0 -0
- package/src/wiring/tool-call/judge.ts +585 -0
- package/src/wiring/tool-call/original-args.ts +64 -0
- package/src/wiring/tool-call/paths.ts +235 -0
- package/src/wiring/tool-result.ts +68 -0
- package/src/worktree-default.ts +27 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bash 命令的切词与路径候选抽取。
|
|
3
|
+
*
|
|
4
|
+
* 从 bash/index.ts 拆出来的纯函数层:只做文本处理,不做授权判断。
|
|
5
|
+
* bash/index.ts 仍是唯一入口,这里的东西由它转出口。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const RE_PREFIX = "re:";
|
|
9
|
+
|
|
10
|
+
export function tokens(text: string): string[] {
|
|
11
|
+
return text.trim().split(/\s+/);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 把命令切成参数,按 bash 的引号语义。
|
|
16
|
+
*
|
|
17
|
+
* 与 index.ts 的 `hasUnquotedShellControl` 是同一套扫描规则,但**保留引号字符本身**:
|
|
18
|
+
* 下游把「含引号」当作「这段是代码或字面量,不是路径」的直接信号,剥掉反而要多猜一次。
|
|
19
|
+
* 空白只在引号之外切词,所以 `-e 'require("x")'` 是两个 token,不是一个。
|
|
20
|
+
*/
|
|
21
|
+
export function shellTokens(command: string): string[] {
|
|
22
|
+
const out: string[] = [];
|
|
23
|
+
let current = "";
|
|
24
|
+
let quote: "'" | '"' | undefined;
|
|
25
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
26
|
+
const ch = command[i] as string;
|
|
27
|
+
if (quote === "'") {
|
|
28
|
+
current += ch;
|
|
29
|
+
if (ch === "'") quote = undefined;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (quote === '"') {
|
|
33
|
+
current += ch;
|
|
34
|
+
// 双引号内反斜杠仍会转义,跳过后一个字符(与 hasUnquotedShellControl 一致)
|
|
35
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
36
|
+
current += command[i + 1] as string;
|
|
37
|
+
i += 1;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (ch === '"') quote = undefined;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (ch === "'" || ch === '"') {
|
|
44
|
+
quote = ch;
|
|
45
|
+
current += ch;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (/\s/.test(ch)) {
|
|
49
|
+
if (current) {
|
|
50
|
+
out.push(current);
|
|
51
|
+
current = "";
|
|
52
|
+
}
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
current += ch;
|
|
56
|
+
}
|
|
57
|
+
// 引号没闭合时前半句照常返回:那种命令会被元字符检查先拒掉,这里不必再判一次。
|
|
58
|
+
if (current) out.push(current);
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 「像路径」的字符集:只由这些字符构成(斜杠另判,见 `pathCandidates`)。
|
|
64
|
+
*
|
|
65
|
+
* 斜杠是「路径」与「包名 / 裸词」在文本上唯一稳定的分界 —— `foo`、`--flag`、`HEAD` 都不含斜杠。
|
|
66
|
+
* 反过来只认以 `/` 或 `./` 开头的写法会漏掉一个真实形态:cwd 下有 `escape-link -> /home/other`
|
|
67
|
+
* 时,`rg foo escape-link/x` 照样读到树外,而它看起来完全不像「绝对路径」。
|
|
68
|
+
*
|
|
69
|
+
* 放宽这一条不会新增误拒:相对候选按 cwd 解析后落在 cwd 之下,而 cwd 本来就在允许集内。
|
|
70
|
+
*/
|
|
71
|
+
const PATH_TOKEN = /^[-A-Za-z0-9._/@+%~]+$/;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 开头是这两种字符的不是路径:`-` 是 flag(`--file=x/y`),`@` 是 npm 包名(`@scope/pkg`)。
|
|
75
|
+
* 只挡开头,`src/@types/x.ts` 这类中间出现的照样算。
|
|
76
|
+
*/
|
|
77
|
+
const PATH_TOKEN_SKIP_HEADS = ["-", "@"];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 出现这些字符的 token 一律不当路径:动态拼接(`$DIR`、反引号)、代码片段(括号、引号)、
|
|
81
|
+
* URL(冒号)、glob(`*` `?` `{}`)、以及 shell 语法残留。
|
|
82
|
+
*
|
|
83
|
+
* 其中大部分字符本来就进不了 `PATH_TOKEN` 的字符类,这条表因此是「判据以后放宽了也别把它们
|
|
84
|
+
* 当路径」的显式记录;先跳过再判正则,也让意图只有一处。
|
|
85
|
+
*/
|
|
86
|
+
const PATH_TOKEN_SKIP = ["$", "`", "*", "?", "(", ")", "{", "}", ",", "'", '"', ";", "|", "<", ">", ":"];
|
|
87
|
+
|
|
88
|
+
/** 最多看这么多条:一条命令里的路径参数不会更多,扫描也不该无上限。 */
|
|
89
|
+
const MAX_PATH_CANDIDATES = 32;
|
|
90
|
+
|
|
91
|
+
/** 超长 token 直接跳过:那多半不是路径,白做一次 realpath。 */
|
|
92
|
+
const MAX_PATH_TOKEN_LENGTH = 200;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 从一条命令里抽出「看起来像路径」的参数候选;纯抽取,不做授权判断。宁可漏放,不许误拒。
|
|
96
|
+
*
|
|
97
|
+
* 第一个 token 是程序位,它本身是路径形态时**不算**参数候选:可执行文件由 bash 通过 PATH 解析,
|
|
98
|
+
* 根本不进参数表;只有把程序写成绝对路径(`/nix/store/…/bin/foo args`)时它才出现在命令开头,
|
|
99
|
+
* 而那是「正在执行的工具」,不是在工具链目录里翻东西。路径形态的四种写法(绝对路径、`~/`、`./`、
|
|
100
|
+
* `../`)都带斜杠,一条斜杠判据就覆盖了全部。
|
|
101
|
+
*/
|
|
102
|
+
export function pathCandidates(command: string): string[] {
|
|
103
|
+
const out: string[] = [];
|
|
104
|
+
const words = shellTokens(command);
|
|
105
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
106
|
+
const word = words[i] as string;
|
|
107
|
+
// 粘值写法(`--file=/a/b`)要判的是 `=` 后面的路径,不是整段旗标:
|
|
108
|
+
// 整段以 `-` 开头会被 SKIP_HEADS 丢掉,路径就永远进不了判定。
|
|
109
|
+
// 不认旗标名,只按第一个 `=` 切;斜杠、字符集、跳过表这些判据自己能分辨值是不是路径。
|
|
110
|
+
const token = word.startsWith("-") && word.includes("=") ? word.slice(word.indexOf("=") + 1) : word;
|
|
111
|
+
// 程序位是路径形态时跳过:它是要执行的工具,不是这条命令的参数。
|
|
112
|
+
if (i === 0 && token.includes("/")) continue;
|
|
113
|
+
if (out.length >= MAX_PATH_CANDIDATES) break;
|
|
114
|
+
if (token.length > MAX_PATH_TOKEN_LENGTH) continue;
|
|
115
|
+
if (PATH_TOKEN_SKIP.some((ch) => token.includes(ch))) continue;
|
|
116
|
+
if (PATH_TOKEN_SKIP_HEADS.some((ch) => token.startsWith(ch))) continue;
|
|
117
|
+
// 没有斜杠的一律不是路径:`foo`、`--flag`、`HEAD` 都是裸词。
|
|
118
|
+
if (!token.includes("/")) continue;
|
|
119
|
+
if (!PATH_TOKEN.test(token)) continue;
|
|
120
|
+
out.push(token);
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 按 shell 控制符切段;引号内与反斜杠转义的字符不当分隔符。
|
|
127
|
+
*/
|
|
128
|
+
export function commandSegments(command: string): string[] {
|
|
129
|
+
const out: string[] = [];
|
|
130
|
+
let current = "";
|
|
131
|
+
let quote: "'" | '"' | undefined;
|
|
132
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
133
|
+
const ch = command[i] as string;
|
|
134
|
+
if (quote === "'") {
|
|
135
|
+
current += ch;
|
|
136
|
+
if (ch === "'") quote = undefined;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (quote === '"') {
|
|
140
|
+
current += ch;
|
|
141
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
142
|
+
current += command[i + 1] as string;
|
|
143
|
+
i += 1;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (ch === '"') quote = undefined;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (ch === "'" || ch === '"') {
|
|
150
|
+
quote = ch;
|
|
151
|
+
current += ch;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
155
|
+
current += ch + (command[i + 1] as string);
|
|
156
|
+
i += 1;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (ch === ";" || ch === "|" || ch === "\n" || ch === "&") {
|
|
160
|
+
out.push(current);
|
|
161
|
+
current = "";
|
|
162
|
+
if (ch === "&" && command[i + 1] === "&") i += 1;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
current += ch;
|
|
166
|
+
}
|
|
167
|
+
out.push(current);
|
|
168
|
+
return out.map((segment) => segment.trim()).filter((segment) => segment !== "");
|
|
169
|
+
}
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「授权写了却永远命中不了」的判定。
|
|
3
|
+
*
|
|
4
|
+
* 这里说的「命中不了」不是语法不匹配(那种情况命令会被拒,拒绝文案里会说清),而是这条命令
|
|
5
|
+
* 在会话的工作目录下**根本跑不起来**:仓库根没有 package.json,`pnpm run x` 只会报
|
|
6
|
+
* "No package.json found"。子代理没有 `cd`、也没有 `&&`,于是这条授权永远用不上,
|
|
7
|
+
* 它只能自己找一个等价写法 —— 那正是要避免的坑。
|
|
8
|
+
*
|
|
9
|
+
* 只判能确定的事实,不猜:
|
|
10
|
+
* - `re:` 正则条目无法静态判断,跳过;
|
|
11
|
+
* - 程序名是工具链里到处都有的(`git`、`rg`、`node`)时,工作目录语义太杂,一律不判:
|
|
12
|
+
* 误报比漏报糟;
|
|
13
|
+
* - 判据与理由文本必须一致:manifest 按工具自己的规矩**向上**找(cwd 的祖先也算,
|
|
14
|
+
* 两条链都查:字面路径与真实路径),所以 `why` 写的是 "in <cwd> or its ancestors"。
|
|
15
|
+
*
|
|
16
|
+
* 从 bash/index.ts 拆出来的诊断层:只判文件系统上能确定的事实。
|
|
17
|
+
* bash/index.ts 仍是唯一入口,这里的东西由它转出口。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
21
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
22
|
+
import { PACKAGE_DIR_FLAGS, PACKAGE_MANAGERS } from "./programs.ts";
|
|
23
|
+
import { RE_PREFIX, tokens } from "./tokens.ts";
|
|
24
|
+
import { firstToken, type UnreachableEntry } from "./rules.ts";
|
|
25
|
+
|
|
26
|
+
/** 条目里指定的包目录(`--dir <p>` / `--prefix <p>` / `-C <p>` / `--cwd <p>`)。 */
|
|
27
|
+
interface PackageDir {
|
|
28
|
+
/** 旗标原文。 */
|
|
29
|
+
flag: string;
|
|
30
|
+
/** 条目里写的路径。 */
|
|
31
|
+
path: string;
|
|
32
|
+
/** 旗标在条目里的位置:`<p>` 与旗标分写时,值在下一个位置。 */
|
|
33
|
+
index: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 找出条目里那个「换包目录」的旗标;判不了时返回 undefined(不猜)。 */
|
|
37
|
+
function packageDirFlag(entry: string): PackageDir | undefined {
|
|
38
|
+
const words = tokens(entry);
|
|
39
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
40
|
+
const word = words[i] as string;
|
|
41
|
+
for (const flag of PACKAGE_DIR_FLAGS) {
|
|
42
|
+
if (word === flag) {
|
|
43
|
+
const value = words[i + 1] as string | undefined;
|
|
44
|
+
// 旗标后面没有值:判不了,不猜。
|
|
45
|
+
if (value === undefined) return undefined;
|
|
46
|
+
return { flag, path: value, index: i };
|
|
47
|
+
}
|
|
48
|
+
if (word.startsWith(`${flag}=`)) {
|
|
49
|
+
const value = word.slice(flag.length + 1);
|
|
50
|
+
if (!value) return undefined;
|
|
51
|
+
return { flag, path: value, index: i };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 找包目录时最多往下走几层。 */
|
|
59
|
+
const MAX_PACKAGE_SEARCH_DEPTH = 4;
|
|
60
|
+
|
|
61
|
+
/** 向上找 manifest 时最多走几层:病态的深路径不至于把判定拖成扫盘。 */
|
|
62
|
+
const MAX_MANIFEST_SEARCH_DEPTH = 8;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 从 cwd 向上找 manifest(`package.json` / `Cargo.toml`)。
|
|
66
|
+
*
|
|
67
|
+
* npm/pnpm/yarn/bun 与 cargo 都是**向上**找 manifest:cwd 是仓库里的子目录时,
|
|
68
|
+
* 祖先目录里有 manifest 就说明这条命令跑得起来。只看 cwd 会把它误判成不可达,
|
|
69
|
+
* 而误报会让父代理去改一条本来没坏的配置。
|
|
70
|
+
*
|
|
71
|
+
* 起点是**物理**路径(见 `searchRoots`):工具看到的是 `process.cwd()`,软链已经被解开。
|
|
72
|
+
* 依据是「工具自己的规矩」,不是「把可能的路径都试一遍」——后者只会多报可达,
|
|
73
|
+
* 把一条死授权藏起来,而那正是这个判据要防的事。
|
|
74
|
+
*
|
|
75
|
+
*/
|
|
76
|
+
function manifestAbove(cwd: string, manifest: string): string | undefined {
|
|
77
|
+
for (const root of searchRoots(cwd)) {
|
|
78
|
+
const found = manifestUp(root, manifest);
|
|
79
|
+
if (found !== undefined) return found;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 上行的起点:**物理**路径优先。
|
|
86
|
+
*
|
|
87
|
+
* 工具看到的是 `process.cwd()`,它已经把软链解开(内核在 `chdir` 时解析),沙箱 helper
|
|
88
|
+
* 也是 `chdir(spec.cwd)` 起的进程。所以字面链上命中的 manifest 工具**永远看不到**:
|
|
89
|
+
* 把两条链取并集,只会把一条真的跑不起来的授权判成可达、从此不再报告 —— 而这正是
|
|
90
|
+
* 这个判据要防的事。`realpathSync` 失败(cwd 已不存在或没权限)时才退回字面路径:
|
|
91
|
+
* 判定不该因为读不到目录而抛。
|
|
92
|
+
*/
|
|
93
|
+
function searchRoots(cwd: string): string[] {
|
|
94
|
+
try {
|
|
95
|
+
return [realpathSync(cwd)];
|
|
96
|
+
} catch {
|
|
97
|
+
return [resolve(cwd)];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 一条链上的上行查找。 */
|
|
102
|
+
function manifestUp(start: string, manifest: string): string | undefined {
|
|
103
|
+
let dir = start;
|
|
104
|
+
for (let depth = 0; depth <= MAX_MANIFEST_SEARCH_DEPTH; depth += 1) {
|
|
105
|
+
if (existsSync(join(dir, manifest))) return dir;
|
|
106
|
+
const parent = dirname(dir);
|
|
107
|
+
if (parent === dir) return undefined;
|
|
108
|
+
dir = parent;
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 这些子命令本来就不需要 manifest:版本号、帮助、store/config 之类。
|
|
115
|
+
* 它们在没有任何 package.json 的目录里照样跑得起来,判成不可达就是误报。
|
|
116
|
+
*/
|
|
117
|
+
const MANIFEST_EXEMPT_SUBCOMMANDS = new Set([
|
|
118
|
+
"--version",
|
|
119
|
+
"--help",
|
|
120
|
+
"-v",
|
|
121
|
+
"-V",
|
|
122
|
+
"store",
|
|
123
|
+
"config",
|
|
124
|
+
"setup",
|
|
125
|
+
"env",
|
|
126
|
+
"cache",
|
|
127
|
+
"dlx",
|
|
128
|
+
]);
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 这些旗标让「从 cwd 找 manifest」这件事判不了:工作区范围(`-r`、`--filter`)
|
|
132
|
+
* 或别的目录(cargo 的 `-C`)。带着它们猜 cwd 必然猜错。
|
|
133
|
+
*/
|
|
134
|
+
const MANIFEST_EXEMPT_FLAGS = new Set([
|
|
135
|
+
"-w",
|
|
136
|
+
"--workspace-root",
|
|
137
|
+
"--filter",
|
|
138
|
+
"-r",
|
|
139
|
+
"--recursive",
|
|
140
|
+
"--manifest-path",
|
|
141
|
+
"-C",
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
/** 条目里有没有这些旗标(`--flag=value` 形态也算)。 */
|
|
145
|
+
function hasExemptFlag(entry: string): boolean {
|
|
146
|
+
return tokens(entry).some((word) => {
|
|
147
|
+
if (MANIFEST_EXEMPT_FLAGS.has(word)) return true;
|
|
148
|
+
for (const flag of MANIFEST_EXEMPT_FLAGS) {
|
|
149
|
+
if (flag.startsWith("--") && word.startsWith(`${flag}=`)) return true;
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 条目的子命令位置上有没有不需要 manifest 的词(`pnpm store path`、`npm --help`…)。 */
|
|
156
|
+
function hasExemptSubcommand(entry: string): boolean {
|
|
157
|
+
return tokens(entry).slice(1).some((word) => MANIFEST_EXEMPT_SUBCOMMANDS.has(word));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* bun 自带的子命令:`test` 用它自己的 runner,`x` 是它的 dlx —— 两者都不看 `package.json`。
|
|
162
|
+
*
|
|
163
|
+
* 判据是「`run` 出现在子命令之前 ⇒ 那是包脚本」,所以 `bun run test` 与
|
|
164
|
+
* `bun --cwd <dir> run test` 不豁免,而 `bun test`、`bun --bun test`、`bun --cwd <dir> test`
|
|
165
|
+
* 都豁免。这样不必知道每个旗标吃几个值,也就不会把旗标的**值**当成子命令。
|
|
166
|
+
*/
|
|
167
|
+
function bunBuiltin(entry: string): string | undefined {
|
|
168
|
+
if (firstToken(entry) !== "bun") return undefined;
|
|
169
|
+
const rest = tokens(entry).slice(1);
|
|
170
|
+
const runAt = rest.indexOf("run");
|
|
171
|
+
const testAt = rest.indexOf("test");
|
|
172
|
+
const xAt = rest.indexOf("x");
|
|
173
|
+
if (runAt !== -1 && testAt !== -1 && runAt < testAt) return undefined;
|
|
174
|
+
if (testAt !== -1) return "test";
|
|
175
|
+
if (xAt !== -1) return "x";
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 这条**命令**自己需不需要 manifest —— 与「条目」的判据分开。
|
|
181
|
+
*
|
|
182
|
+
* 宽条目(`pnpm`)放行的 `pnpm --version` 并不需要 manifest,它失败的原因是网络、二进制
|
|
183
|
+
* 缺失或别的什么;拿「这条条目没有 manifest 可用」去解释它,是把原因安到了不成立的前提上。
|
|
184
|
+
*/
|
|
185
|
+
export function commandNeedsNoManifest(command: string): boolean {
|
|
186
|
+
const entry = command.trim();
|
|
187
|
+
if (!entry) return false;
|
|
188
|
+
if (hasExemptSubcommand(entry) || hasExemptFlag(entry)) return true;
|
|
189
|
+
// 自己指了包目录(`pnpm --dir packages/a run build`):cwd 有没有 manifest 与之无关。
|
|
190
|
+
if (packageDirFlag(entry) !== undefined) return true;
|
|
191
|
+
return bunBuiltin(entry) !== undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 构建产物目录:路径里有一段是这些目录时,文件不存在只说明还没构建,
|
|
196
|
+
* 不说明这条授权命中不了(`packages/helper/target/release/…` 要等一次 cargo build)。
|
|
197
|
+
*/
|
|
198
|
+
const BUILD_ARTIFACT_DIRS = new Set([
|
|
199
|
+
"target",
|
|
200
|
+
"dist",
|
|
201
|
+
"build",
|
|
202
|
+
"out",
|
|
203
|
+
"node_modules",
|
|
204
|
+
".git",
|
|
205
|
+
".venv",
|
|
206
|
+
"__pycache__",
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
/** 路径里有没有一段是构建产物目录。 */
|
|
210
|
+
function isBuildArtifactPath(path: string): boolean {
|
|
211
|
+
return path.split(/[/\\]/).some((part) => BUILD_ARTIFACT_DIRS.has(part));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* cwd 下面**所有**带 package.json 的目录(相对 cwd 的路径)。
|
|
216
|
+
*
|
|
217
|
+
* 收集全部而不是第一个:仓库里常有多个包,「这条授权原本指哪个包」只有父代理知道,
|
|
218
|
+
* 随便挑一个再给一条 `--dir` 建议,会把命令指到别的包上。
|
|
219
|
+
*/
|
|
220
|
+
function packageCandidatesUnder(cwd: string): string[] {
|
|
221
|
+
const out: string[] = [];
|
|
222
|
+
let frontier = [""];
|
|
223
|
+
for (let depth = 0; depth < MAX_PACKAGE_SEARCH_DEPTH; depth += 1) {
|
|
224
|
+
const next: string[] = [];
|
|
225
|
+
for (const rel of frontier) {
|
|
226
|
+
const abs = rel === "" ? cwd : join(cwd, rel);
|
|
227
|
+
let names: string[];
|
|
228
|
+
try {
|
|
229
|
+
names = readdirSync(abs, { withFileTypes: true })
|
|
230
|
+
.filter(
|
|
231
|
+
(entry) =>
|
|
232
|
+
(entry.isDirectory() || entry.isSymbolicLink()) &&
|
|
233
|
+
!entry.name.startsWith(".") &&
|
|
234
|
+
entry.name !== "node_modules",
|
|
235
|
+
)
|
|
236
|
+
.map((entry) => entry.name)
|
|
237
|
+
.sort();
|
|
238
|
+
} catch {
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
for (const name of names) {
|
|
242
|
+
const childRel = rel === "" ? name : join(rel, name);
|
|
243
|
+
if (existsSync(join(cwd, childRel, "package.json"))) out.push(childRel);
|
|
244
|
+
next.push(childRel);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
frontier = next;
|
|
248
|
+
}
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 条目里的脚本名:`run <script>` 与裸 `test` 两种形态。
|
|
254
|
+
*
|
|
255
|
+
* 只认这两种:别的裸词(`pnpm install`)是子命令还是脚本名猜不准,
|
|
256
|
+
* 猜错就会把一条本来跑得起来的命令判成不可达。
|
|
257
|
+
*/
|
|
258
|
+
function scriptName(entry: string): string | undefined {
|
|
259
|
+
const words = tokens(entry);
|
|
260
|
+
if (words.length < 2) return undefined;
|
|
261
|
+
if (words[1] === "run") return words[2];
|
|
262
|
+
// `bun test` 跑的是 bun 自带的测试运行器,不看 `scripts.test`,连 package.json 都不需要;
|
|
263
|
+
// 把它当成「包里的 test 脚本」会让文案说一句假话("no candidate has a `test` script")。
|
|
264
|
+
// 只有 `bun run test` 才是脚本。
|
|
265
|
+
if (words[1] === "test") return words[0] === "bun" ? undefined : "test";
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** 候选包的 package.json 里真的有这个脚本吗。 */
|
|
270
|
+
function hasScript(cwd: string, pkgRel: string, script: string): boolean {
|
|
271
|
+
try {
|
|
272
|
+
const raw = readFileSync(join(cwd, pkgRel, "package.json"), "utf8");
|
|
273
|
+
const parsed = JSON.parse(raw) as { scripts?: Record<string, unknown> };
|
|
274
|
+
return parsed.scripts !== undefined && Object.prototype.hasOwnProperty.call(parsed.scripts, script);
|
|
275
|
+
} catch {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** `packageFixFor` 的答案:有改法就给改法,没有就说清为什么没有。 */
|
|
281
|
+
interface PackageFixOutcome {
|
|
282
|
+
/** 可靠改法;给不出来时没有这个字段。 */
|
|
283
|
+
fix?: string;
|
|
284
|
+
/** 恰好一个候选、但那个包里没有这个脚本:交给文案去说明。 */
|
|
285
|
+
scriptMissing?: string;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* 改法只在**恰好一个候选**、且那个包里真的有这个脚本时才给。
|
|
290
|
+
*
|
|
291
|
+
* 多个候选时给不出来:这条授权原本指哪个包只有父代理知道,随便挑一个会把命令
|
|
292
|
+
* 指到别的包上。脚本不在包里时那条建议自己也跑不通(pnpm 报 "Command not found"),
|
|
293
|
+
* 那是脚本不存在,不是授权不可达 —— 所以既不给改法,也要把「原因不是包选错了」
|
|
294
|
+
* 带回去(`scriptMissing`),否则文案只会让人在候选里挑一个,而挑哪一个都不行。
|
|
295
|
+
*/
|
|
296
|
+
function packageFixFor(
|
|
297
|
+
cwd: string,
|
|
298
|
+
head: string,
|
|
299
|
+
managerFlag: string,
|
|
300
|
+
entry: string,
|
|
301
|
+
candidates: string[],
|
|
302
|
+
script: string | undefined,
|
|
303
|
+
dir?: PackageDir,
|
|
304
|
+
): PackageFixOutcome {
|
|
305
|
+
if (candidates.length !== 1) return {};
|
|
306
|
+
const pkg = candidates[0] as string;
|
|
307
|
+
if (script !== undefined && !hasScript(cwd, pkg, script)) return { scriptMissing: script };
|
|
308
|
+
return { fix: packageFix(head, managerFlag, pkg, entry, dir) };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** 造一条包管理器条目的不可达记录:候选列表,加上可靠时才有的改法。 */
|
|
312
|
+
function packagingItem(
|
|
313
|
+
cwd: string,
|
|
314
|
+
entry: string,
|
|
315
|
+
head: string,
|
|
316
|
+
managerFlag: string,
|
|
317
|
+
why: string,
|
|
318
|
+
candidates: string[],
|
|
319
|
+
dir?: PackageDir,
|
|
320
|
+
): UnreachableEntry {
|
|
321
|
+
const item: UnreachableEntry = { entry, why, lookedIn: searchRoots(cwd)[0] };
|
|
322
|
+
if (candidates.length > 0) item.candidates = candidates;
|
|
323
|
+
const { fix, scriptMissing } = packageFixFor(cwd, head, managerFlag, entry, candidates, scriptName(entry), dir);
|
|
324
|
+
if (fix) item.fix = fix;
|
|
325
|
+
if (scriptMissing) item.scriptMissing = scriptMissing;
|
|
326
|
+
return item;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* 包管理器的改法:`pnpm --dir <pkg> run <script>` 这类形态。
|
|
331
|
+
*
|
|
332
|
+
* 原条目里的换目录旗标被换掉:那个路径正是失效的原因。
|
|
333
|
+
*/
|
|
334
|
+
function packageFix(head: string, managerFlag: string, pkgDir: string, entry: string, dir?: PackageDir): string {
|
|
335
|
+
const words = tokens(entry);
|
|
336
|
+
const rest: string[] = [];
|
|
337
|
+
for (let i = 1; i < words.length; i += 1) {
|
|
338
|
+
const word = words[i] as string;
|
|
339
|
+
if (dir && i === dir.index) {
|
|
340
|
+
// `--dir <p>`:旗标与值一起跳过;`--dir=<p>` 只跳过旗标本身。
|
|
341
|
+
if (word === dir.flag) i += 1;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
rest.push(word);
|
|
345
|
+
}
|
|
346
|
+
return [head, managerFlag, pkgDir, ...rest].join(" ");
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 一条包管理器条目在 cwd 下跑不跑得起来;跑不起来时给出记录。 */
|
|
350
|
+
function packageManagerItem(
|
|
351
|
+
cwd: string,
|
|
352
|
+
entry: string,
|
|
353
|
+
head: string,
|
|
354
|
+
managerFlag: string,
|
|
355
|
+
candidates: string[],
|
|
356
|
+
): UnreachableEntry | undefined {
|
|
357
|
+
// bun 自带的子命令先判:`bun --cwd <dir> test` 里的 `--cwd` 会被当成包目录旗标,
|
|
358
|
+
// 于是它先掉进下面那个分支、被按「那个目录里没有 package.json」判成不可达 ——
|
|
359
|
+
// 而 `bun test` 用的是它自己的 runner,跟 package.json 无关。
|
|
360
|
+
if (bunBuiltin(entry) !== undefined) return undefined;
|
|
361
|
+
const dir = packageDirFlag(entry);
|
|
362
|
+
if (dir) {
|
|
363
|
+
// 条目自己指了包目录:那个目录里没有 package.json 才算命中不了。
|
|
364
|
+
const target = isAbsolute(dir.path) ? dir.path : resolve(cwd, dir.path);
|
|
365
|
+
if (existsSync(join(target, "package.json"))) return undefined;
|
|
366
|
+
return packagingItem(cwd, entry, head, managerFlag, `no package.json in ${dir.path}`, candidates, dir);
|
|
367
|
+
}
|
|
368
|
+
// 不需要 manifest 的子命令(`pnpm store path`)与判不了的旗标(`pnpm -r test`):不判。
|
|
369
|
+
if (hasExemptSubcommand(entry) || hasExemptFlag(entry)) return undefined;
|
|
370
|
+
// manifest 向上找:cwd 是子目录时,祖先有 package.json 就说明跑得起来。
|
|
371
|
+
if (manifestAbove(cwd, "package.json")) return undefined;
|
|
372
|
+
return packagingItem(cwd, entry, head, managerFlag, `no package.json in ${cwd} or its ancestors`, candidates);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** 一条 cargo 条目在 cwd 下跑不跑得起来。 */
|
|
376
|
+
function cargoItem(cwd: string, entry: string): UnreachableEntry | undefined {
|
|
377
|
+
// `cargo --manifest-path <p>` / `cargo -C <dir>` 指了别的包:判不了,不判。
|
|
378
|
+
if (hasExemptFlag(entry) || hasExemptSubcommand(entry)) return undefined;
|
|
379
|
+
if (manifestAbove(cwd, "Cargo.toml")) return undefined;
|
|
380
|
+
return { entry, why: `no Cargo.toml in ${cwd} or its ancestors` };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* 扫出永远命中不了的条目。
|
|
385
|
+
*
|
|
386
|
+
* 判据只有「这条命令在 cwd 下跑不起来」这一类事实:语法不匹配的条目不在这里(那条命令
|
|
387
|
+
* 本来就会被拒,拒绝文案里会说)。判不出来的(正则、工具链里到处都有的程序名)一律放过。
|
|
388
|
+
*
|
|
389
|
+
* 「跑不起来」按工具自己的规矩算:manifest 向上找(cwd 与它的祖先),找不到才算。
|
|
390
|
+
*/
|
|
391
|
+
export function unreachableEntries(entries: string[], cwd: string): UnreachableEntry[] {
|
|
392
|
+
const out: UnreachableEntry[] = [];
|
|
393
|
+
// 候选目录只找一次:多个包管理器条目指向同一批候选目录,找一次就够。
|
|
394
|
+
let candidates: string[] | undefined;
|
|
395
|
+
const findCandidates = (): string[] => {
|
|
396
|
+
if (candidates === undefined) candidates = packageCandidatesUnder(cwd);
|
|
397
|
+
return candidates;
|
|
398
|
+
};
|
|
399
|
+
for (const raw of entries) {
|
|
400
|
+
const entry = raw.trim();
|
|
401
|
+
if (!entry) continue;
|
|
402
|
+
// 正则条目无法静态判断:跳过,不猜。
|
|
403
|
+
if (entry.startsWith(RE_PREFIX)) continue;
|
|
404
|
+
const head = firstToken(entry);
|
|
405
|
+
if (!head) continue;
|
|
406
|
+
const managerFlag = PACKAGE_MANAGERS[head];
|
|
407
|
+
if (managerFlag !== undefined) {
|
|
408
|
+
const item = packageManagerItem(cwd, entry, head, managerFlag, findCandidates());
|
|
409
|
+
if (item) out.push(item);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
if (head === "cargo") {
|
|
413
|
+
const item = cargoItem(cwd, entry);
|
|
414
|
+
if (item) out.push(item);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (head.startsWith("/")) {
|
|
418
|
+
// 程序位写成绝对路径:这个可以静态核对。
|
|
419
|
+
// 构建产物目录里的文件不存在只说明还没构建,不说明这条授权命中不了。
|
|
420
|
+
if (isBuildArtifactPath(head)) continue;
|
|
421
|
+
if (!existsSync(head)) {
|
|
422
|
+
out.push({ entry, why: `${head} does not exist` });
|
|
423
|
+
}
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
// 其它程序名一律不判:`git`、`rg`、`node` 的工作目录语义太杂,误报比漏报糟。
|
|
427
|
+
}
|
|
428
|
+
return out;
|
|
429
|
+
}
|
package/src/budgets.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BUDGETS 层:拒绝预算 —— 连续拒绝对计数器与它的缺省值。计数器自己合成两类拒绝的出路
|
|
3
|
+
* 文案(授权不够去求援,写范围不够改回报告);index.ts 只做接线。
|
|
4
|
+
* 模块拆分第五步:DenyCounter 从 deny-count.ts 原样搬入,缺省预算从 policy.ts 原样搬入,
|
|
5
|
+
* 行为不变;policy.ts 反过来从本模块导入缺省值,依赖方向无环。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 撞权限判据:连续被范围规则拒绝 maxDenies 次,多半是父代理的授权写错了,不是子代理
|
|
10
|
+
* 走错了目录。计满后第一次给 warn 级拒绝(要求向父代理报告缺什么),再撞一次就停。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface DenyCountResult {
|
|
14
|
+
/** 是否要交给调用方拒绝这次调用。 */
|
|
15
|
+
yes: boolean;
|
|
16
|
+
/** 拒绝文案;undefined 表示这次拒绝不用附加探针提示。 */
|
|
17
|
+
note?: string;
|
|
18
|
+
/** 停止本轮。 */
|
|
19
|
+
stop: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class DenyCounter {
|
|
23
|
+
#limit: number;
|
|
24
|
+
#count = 0;
|
|
25
|
+
#warned = false;
|
|
26
|
+
/**
|
|
27
|
+
* 两类拒绝的出路不同:授权不够要去求援,写范围不够要改成回报告。文案必须分开,
|
|
28
|
+
* 否则子代理会把「这里写不了」当成「授权配错了」,继续换个路径试。
|
|
29
|
+
*/
|
|
30
|
+
#kind: "grant" | "write";
|
|
31
|
+
|
|
32
|
+
constructor(limit: number, kind: "grant" | "write" = "grant") {
|
|
33
|
+
this.#limit = limit;
|
|
34
|
+
this.#kind = kind;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get count(): number {
|
|
38
|
+
return this.#count;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 一次放行的调用:连续计数归零(warned 一起归零,否则清零后第一次警告会被吃掉)。
|
|
43
|
+
*
|
|
44
|
+
* 文档、skill 和拒绝文案承诺的都是「连续 N 次」;不在这里清零,早期一次误判会一直带到最后,
|
|
45
|
+
* 硬停一次就丢掉整轮任务 —— 正是这条判据本来要避免的事。
|
|
46
|
+
*/
|
|
47
|
+
reset(): void {
|
|
48
|
+
this.#count = 0;
|
|
49
|
+
this.#warned = false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 一次被范围规则拒绝。 */
|
|
53
|
+
record(): DenyCountResult {
|
|
54
|
+
if (this.#limit <= 0) return { yes: false, stop: false };
|
|
55
|
+
this.#count += 1;
|
|
56
|
+
if (this.#count < this.#limit) return { yes: false, stop: false };
|
|
57
|
+
if (!this.#warned) {
|
|
58
|
+
this.#warned = true;
|
|
59
|
+
return {
|
|
60
|
+
yes: true,
|
|
61
|
+
stop: false,
|
|
62
|
+
note: this.#kind === "write"
|
|
63
|
+
? `Write refusal ${this.#count} in a row — the write scope does not cover this path, ` +
|
|
64
|
+
"and it will not change. Deliver the finding as a report; one more refusal ends the run."
|
|
65
|
+
: `Refusal ${this.#count} in a row — usually the grant does not fit the task. ` +
|
|
66
|
+
"Use contact_supervisor to say what you need; one more ends the run.",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
yes: true,
|
|
71
|
+
stop: true,
|
|
72
|
+
// 硬停之后模型没有下一轮,这段文字的实际读者是父代理:写成诊断,而不是叮嘱。
|
|
73
|
+
note: this.#kind === "write"
|
|
74
|
+
? `Write refusal ${this.#count} (criterion: write budget): the run is over. For the parent session — ` +
|
|
75
|
+
"the subagent kept writing outside writeScope; a report is the intended deliverable, " +
|
|
76
|
+
"or re-dispatch with a wider writeScope."
|
|
77
|
+
: `Refusal ${this.#count} (criterion: denial budget): the run is over. For the parent session — ` +
|
|
78
|
+
"the subagent can no longer act; re-dispatch with a wider grant if the need was not escalated.",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
status(): string {
|
|
83
|
+
return this.#limit > 0 ? `${this.#count}/${this.#limit}` : "off";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 撞权限的缺省预算;两处都要用,所以提出来。 */
|
|
88
|
+
export const DEFAULT_MAX_DENIES = 5;
|
|
89
|
+
export const DEFAULT_MAX_WRITE_DENIES = 2;
|