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,69 @@
|
|
|
1
|
+
import type { ExtensionContext, SessionStartEvent } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { reportUnreachableEntries, reportedUntrackedWorktreeProfiles, warnCompatLayer, warnLegacyBashColumns, warnedLegacyBashColumns } from "../notices.ts";
|
|
3
|
+
import { bindingKey } from "../role-config.ts";
|
|
4
|
+
import { bashBlocker, detectPnpmStore, helperPath, reportedHiddenGrants } from "../sandbox/index.ts";
|
|
5
|
+
import type { ActivateOptions, Wiring } from "./context.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* session_start 的正文(逐字搬自 index.ts 的处理器):接上本会话的 UI 提示通道、清空
|
|
9
|
+
* 「只说一次」的告警护栏,最后向父代理汇报生效范围。角色 md 的基线在
|
|
10
|
+
* before_agent_start 的接线里解析,这里不再读任何配置文件。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* session_start 的正文(逐字搬自 index.ts 的处理器):策略只来自激活时定格的 bindings,
|
|
16
|
+
* 这里接上 UI 提示通道、清空告警护栏并汇报生效范围;options 与 event 都用不到,
|
|
17
|
+
* 保持处理器的调用形状。
|
|
18
|
+
*/
|
|
19
|
+
export async function wireSessionStart(w: Wiring, _options: ActivateOptions, _event: SessionStartEvent, ctx: ExtensionContext): Promise<void> {
|
|
20
|
+
w.currentCtx = ctx;
|
|
21
|
+
// 提示状态按会话重置:一个进程可能服务多个会话,模块级的 set 留到下一个会话就会把
|
|
22
|
+
// 本该发出的告警吞掉(那是「lost, not duplicated」的一种,且完全没有痕迹)。
|
|
23
|
+
reportedHiddenGrants.clear();
|
|
24
|
+
reportedUntrackedWorktreeProfiles.clear();
|
|
25
|
+
warnedLegacyBashColumns.clear();
|
|
26
|
+
// UI 提示通道在这里接上会话的 ctx:`judgeToolCall` 只在工具调用时绑定,session_start
|
|
27
|
+
// 里的告警要发得出去,就不能等第一次工具调用。
|
|
28
|
+
w.uiWarning = (message) => {
|
|
29
|
+
const ui = ctx.ui as { notify?: (m: string, t?: string) => void } | undefined;
|
|
30
|
+
ui?.notify?.(message, "warning");
|
|
31
|
+
};
|
|
32
|
+
const cwd = ctx.cwd ?? process.cwd();
|
|
33
|
+
warnCompatLayer(cwd, w.uiWarning);
|
|
34
|
+
warnLegacyBashColumns(w.call.config, `extensionBindings ${bindingKey()}`, w.uiWarning);
|
|
35
|
+
if (!w.policy.active) return;
|
|
36
|
+
const budget = w.policy.maxReads > 0 ? `, read budget ${w.policy.maxReads}` : "";
|
|
37
|
+
const bash = w.policy.bash
|
|
38
|
+
/**
|
|
39
|
+
* 报「配置里到底有什么」,不是「哪一列有值」:Phase 2 之后合法配置只用 `allow`,
|
|
40
|
+
* 而这一行曾经只数 legacy 的 read/write,于是只用 allow 的角色在启动摘要里显示成
|
|
41
|
+
* `bash 0 read / 0 write` —— 白名单满的,摘要却说空。legacy 列只在真的配了才提。
|
|
42
|
+
*/
|
|
43
|
+
? (() => {
|
|
44
|
+
const parts: string[] = [];
|
|
45
|
+
if (w.policy.bash.allow.length > 0) parts.push(`${w.policy.bash.allow.length} allowed`);
|
|
46
|
+
if (w.policy.bash.read.length > 0) parts.push(`${w.policy.bash.read.length} read`);
|
|
47
|
+
if (w.policy.bash.write.length > 0) parts.push(`${w.policy.bash.write.length} write`);
|
|
48
|
+
return `, bash ${parts.join(" / ") || "no entries"}`;
|
|
49
|
+
})()
|
|
50
|
+
: ", bash off";
|
|
51
|
+
const probeLine = w.probe ? ", probe on" : "";
|
|
52
|
+
const storeLine = w.policy.bash ? `, pnpm store ${detectPnpmStore(cwd).path ?? "detection failed"}` : "";
|
|
53
|
+
const writeGrants = [...w.policy.writeGlobs, ...w.policy.absoluteWrite];
|
|
54
|
+
let mode = "read-write";
|
|
55
|
+
if (w.policy.readOnly) mode = writeGrants.length > 0 ? "read-only, except the granted writes" : "read-only";
|
|
56
|
+
const sameWrite = w.policy.writeGlobs.join("\n") === w.policy.globs.join("\n");
|
|
57
|
+
const writeNote = sameWrite ? "" : `, write scope ${w.policy.writeGlobs.length} entries`;
|
|
58
|
+
const msg = w.policy.globs.length
|
|
59
|
+
? `scope active (${w.policy.source}, ${w.policy.globs.length} entries${writeNote}, ${mode}${budget}${bash}${probeLine}${storeLine})`
|
|
60
|
+
: `scope is empty: every file access is refused${budget}${bash}${probeLine}${storeLine}`;
|
|
61
|
+
ctx.ui.notify(`pi-subagents-extend: ${msg}`, "info");
|
|
62
|
+
// 只在该会话真的会跑 bash 时才喊:父会话没 bindings 时 bash 不受管,不必打扰。
|
|
63
|
+
if (w.policy.bash) {
|
|
64
|
+
const blocker = bashBlocker(helperPath());
|
|
65
|
+
if (blocker) ctx.ui.notify(`pi-subagents-extend: ${blocker}`, "warning");
|
|
66
|
+
// 授权写了却永远命中不了:告诉父代理一次(走 UI,不进模型上下文)。
|
|
67
|
+
reportUnreachableEntries(w.policy.bash, cwd, w.uiWarning);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { statSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 从锚点解析目标文件。
|
|
8
|
+
*
|
|
9
|
+
* 锚点编辑工具(replace / replace_match / insert / copy / move)在入参里可以不带路径:
|
|
10
|
+
* 目标由「锚点归属」决定,而归属记录在宿主扩展自己的会话 sidecar 里
|
|
11
|
+
* (`<config>/sessions/<sha256(sessionFile) 前 24 位>.registry.jsonl`,事件为
|
|
12
|
+
* `allocate {path, rows}` / `free` / `clear`)。
|
|
13
|
+
*
|
|
14
|
+
* 我们读它,是为了给这些工具判「目标在不在写范围里」。不读就只能一律拒绝,那样子代理在
|
|
15
|
+
* 这类宿主上一个字也改不了。代价是依赖一份第三方私有格式,所以证据链是单向的:
|
|
16
|
+
* 读不到、读不懂、查不到 —— 一律当作**拿不到目标**,然后拒绝。绝不会因为读不到而放行。
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* 一次锚点解析的三种结局。
|
|
20
|
+
*
|
|
21
|
+
* 把「锚点不认识」和「锚点分属两个文件」分开,是因为两者的出路完全不同:前者要模型重新读文件
|
|
22
|
+
* (锚点会过期)或者改用显式 path;后者是入参本身自相矛盾,重读一百次也没用,只能拆成两次调用。
|
|
23
|
+
* 混成一句「不在注册表里」,模型就会去重读,然后拿回同样的拒绝。
|
|
24
|
+
*/
|
|
25
|
+
export type AnchorResolution =
|
|
26
|
+
| { path: string }
|
|
27
|
+
| { miss: "unknown" }
|
|
28
|
+
| { miss: "disagree"; files: string[] };
|
|
29
|
+
export function anchorTargetPath(
|
|
30
|
+
sessionFile: string | undefined,
|
|
31
|
+
anchors: string[],
|
|
32
|
+
): AnchorResolution {
|
|
33
|
+
if (!sessionFile || anchors.length === 0) return { miss: "unknown" };
|
|
34
|
+
const dir = join(
|
|
35
|
+
process.env.PI_HASHLINE_DIR ??
|
|
36
|
+
join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "pi-hashline-edit-pro"),
|
|
37
|
+
"sessions",
|
|
38
|
+
);
|
|
39
|
+
const key = createHash("sha256").update(sessionFile).digest("hex").slice(0, 24);
|
|
40
|
+
const sidecar = join(dir, `${key}.registry.jsonl`);
|
|
41
|
+
let raw: string;
|
|
42
|
+
try {
|
|
43
|
+
// 一次 stat 取全(mtimeNs + size + ino):分两次取的话文件可能在两次之间被改写,
|
|
44
|
+
// 拼出来的 stamp 谁都不对应。用纳秒而不是毫秒 —— 宿主重写(压缩)sidecar 时字节数
|
|
45
|
+
// 可能刚好不变,毫秒粒度也可能落在同一刻,那样就会拿旧解析结果判新文件。
|
|
46
|
+
const info = statSync(sidecar, { bigint: true });
|
|
47
|
+
const stamp = `${info.mtimeNs}:${info.size}:${info.ino}`;
|
|
48
|
+
const cached = anchorCache.get(sidecar);
|
|
49
|
+
if (cached?.stamp === stamp) raw = cached.raw;
|
|
50
|
+
else {
|
|
51
|
+
raw = readFileSync(sidecar, "utf8");
|
|
52
|
+
anchorCache.set(sidecar, { stamp, raw });
|
|
53
|
+
}
|
|
54
|
+
} catch {
|
|
55
|
+
return { miss: "unknown" };
|
|
56
|
+
}
|
|
57
|
+
const wanted = new Set(anchors);
|
|
58
|
+
const owned = new Map<string, string>();
|
|
59
|
+
for (const line of raw.split("\n")) {
|
|
60
|
+
if (!line) continue;
|
|
61
|
+
let event: { kind?: string; path?: string; rows?: unknown; anchors?: unknown };
|
|
62
|
+
try {
|
|
63
|
+
event = JSON.parse(line) as typeof event;
|
|
64
|
+
} catch {
|
|
65
|
+
// 半行(正在追加)或坏行:跳过。宁可判不出来而拒绝,也不猜。
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (event.kind === "clear") owned.clear();
|
|
69
|
+
else if (event.kind === "free") {
|
|
70
|
+
if (Array.isArray(event.anchors)) {
|
|
71
|
+
for (const anchor of event.anchors) if (typeof anchor === "string") owned.delete(anchor);
|
|
72
|
+
} else if (typeof event.path === "string") {
|
|
73
|
+
for (const [anchor, path] of owned) if (path === event.path) owned.delete(anchor);
|
|
74
|
+
}
|
|
75
|
+
} else if (event.kind === "allocate" && typeof event.path === "string" && Array.isArray(event.rows)) {
|
|
76
|
+
for (const row of event.rows) {
|
|
77
|
+
// 只留我们查的那几个锚点:一次 allocate 可能带整份文件的行(实测一份 1998 条),
|
|
78
|
+
// 全部收进 map 没有意义。
|
|
79
|
+
if (Array.isArray(row) && typeof row[0] === "string" && wanted.has(row[0])) {
|
|
80
|
+
owned.set(row[0], event.path);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// 所有锚点必须指向同一个文件,拿不到唯一目标就拒绝。
|
|
86
|
+
//
|
|
87
|
+
// 宿主自己也有一条同义规则:两个锚点归属不同文件时它抛
|
|
88
|
+
// `[E_BAD_SHAPE] The anchors are owned by different files … edit one file per call`
|
|
89
|
+
// (edit-common.ts 的 resolveEditTarget,paths.size > 1)。这里跟着拒,是为了让本层的
|
|
90
|
+
// 判断不依赖宿主先把它拦下来 —— 我们判的是「将要被写的文件在不在写范围里」,目标都不唯一
|
|
91
|
+
// 就没有答案,不能挑一个算数。
|
|
92
|
+
//
|
|
93
|
+
// 关于两种拼写(`remove_from` / `replace_from` 互为别名):宿主在 `prepareArguments` 里
|
|
94
|
+
// 就按自己的规范名归一化、把另一个删掉,而 pi 的 `tool_call` 钩子拿到的是归一化并校验
|
|
95
|
+
// 之后的入参(agent-loop: prepareToolCallArguments → validateToolArguments →
|
|
96
|
+
// beforeToolCall({args})),所以同时带两种拼写、各指一处的入参到不了这里。这条规则防的
|
|
97
|
+
// 是「锚点确实分属两个文件」的入参,不是别名。
|
|
98
|
+
const resolvedFiles = new Set<string>();
|
|
99
|
+
for (const anchor of anchors) {
|
|
100
|
+
const found = owned.get(anchor);
|
|
101
|
+
if (found) resolvedFiles.add(found);
|
|
102
|
+
}
|
|
103
|
+
if (resolvedFiles.size === 0) return { miss: "unknown" };
|
|
104
|
+
if (resolvedFiles.size > 1) return { miss: "disagree", files: [...resolvedFiles].sort() };
|
|
105
|
+
const [onlyFile] = resolvedFiles;
|
|
106
|
+
return { path: onlyFile };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** sidecar 解析缓存:一次工具调用读一次文件太长,按 mtime+size 失效。 */
|
|
110
|
+
const anchorCache = new Map<string, { stamp: string; raw: string }>();
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 可能承载锚点的入参名。两种拼写都在里面(`replace` 用 remove_*,`replace_match` 用
|
|
114
|
+
* replace_*,宿主互为别名),所以列全;至于它们是不是都指向同一份文件,由
|
|
115
|
+
* `anchorTargetPath` 自己核对 —— 不能指望工具去校验,它会先删掉别名。
|
|
116
|
+
*/
|
|
117
|
+
export const ANCHOR_ARGS = [
|
|
118
|
+
"remove_from",
|
|
119
|
+
"remove_to",
|
|
120
|
+
"replace_from",
|
|
121
|
+
"replace_to",
|
|
122
|
+
"source_from",
|
|
123
|
+
"source_to",
|
|
124
|
+
"anchor",
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 双文件工具(copy / move)把两份文件写在同一组入参里:`source_from` / `source_to` 属于
|
|
129
|
+
* 源文件,`anchor` 属于目标文件。两组锚点归属不同文件,必须分开解析 —— 合成一个列表去查,
|
|
130
|
+
* 只会返回先命中的那一个,等于只判了一侧。
|
|
131
|
+
*
|
|
132
|
+
* 目标侧只有 `anchor`:宿主的 copy/move 契约只认 path / source_from / source_to / anchor /
|
|
133
|
+
* direction,传 `remove_from` 会被它以未知字段拒掉(payload-contract.ts 的 TRANSFER_KEYS)。
|
|
134
|
+
* 早先这里把 `remove_from` / `remove_to` 也算进目标侧,今天造不成问题(宿主先拒),但排序是
|
|
135
|
+
* 隐患:多锚点取值取第一个能命中的,那样返回的是源文件,真正的目标反而没判。
|
|
136
|
+
*/
|
|
137
|
+
export const TRANSFER_SOURCE_ARGS = ["source_from", "source_to"];
|
|
138
|
+
export const TRANSFER_DEST_ARGS = ["anchor"];
|
|
File without changes
|
|
File without changes
|