@armadra/agent 0.15.1 → 0.17.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/CHANGELOG.md +36 -0
- package/CHANGELOG.zh-CN.md +36 -0
- package/dist/agent/recovery-reminder.d.ts +23 -0
- package/dist/agent/recovery-reminder.js +42 -0
- package/dist/agent/reminders.d.ts +10 -4
- package/dist/agent/reminders.js +59 -19
- package/dist/agent/session-plan.js +3 -4
- package/dist/agent/session-settings.d.ts +5 -0
- package/dist/agent/session-settings.js +15 -2
- package/dist/agent/session-subagent.d.ts +5 -0
- package/dist/agent/session-subagent.js +65 -12
- package/dist/agent/session-tools.d.ts +3 -1
- package/dist/agent/session-tools.js +47 -3
- package/dist/agent/subagent-background.js +1 -0
- package/dist/agent/subagent-fork.js +3 -1
- package/dist/agent/subagent-handle.d.ts +22 -0
- package/dist/agent/subagent-handle.js +55 -0
- package/dist/agent/subagent-lifetime.d.ts +35 -0
- package/dist/agent/subagent-lifetime.js +112 -0
- package/dist/agent/subagent-registry.d.ts +23 -15
- package/dist/agent/subagent-registry.js +68 -81
- package/dist/agent/system-prompt.d.ts +8 -0
- package/dist/agent/system-prompt.js +4 -0
- package/dist/agent/types-w5.d.ts +8 -3
- package/dist/agent/types.d.ts +7 -0
- package/dist/agents/builtin.d.ts +1 -1
- package/dist/agents/builtin.js +3 -2
- package/dist/agents/report.d.ts +25 -0
- package/dist/agents/report.js +51 -0
- package/dist/agents/result.d.ts +9 -1
- package/dist/agents/result.js +16 -0
- package/dist/agents/task-control.d.ts +12 -0
- package/dist/agents/task-record.d.ts +24 -1
- package/dist/agents/task-record.js +24 -0
- package/dist/bundle/ama.cjs +3144 -1771
- package/dist/cli/bootstrap.js +14 -7
- package/dist/cli/compose-agents.d.ts +2 -1
- package/dist/cli/compose-agents.js +4 -1
- package/dist/cli/compose-events.d.ts +18 -3
- package/dist/cli/compose-events.js +17 -2
- package/dist/cli/compose-session.d.ts +1 -1
- package/dist/cli/compose-session.js +8 -16
- package/dist/cli/compose.d.ts +8 -0
- package/dist/cli/compose.js +25 -1
- package/dist/cli/deps.d.ts +12 -0
- package/dist/config/json-schema.js +4 -0
- package/dist/config/key-docs.js +3 -1
- package/dist/config/schema-w5.js +3 -0
- package/dist/config/schema.js +12 -1
- package/dist/config/settings-registry.js +4 -0
- package/dist/config/types-w5.d.ts +2 -0
- package/dist/config/types.d.ts +7 -0
- package/dist/drivers/acp/types.d.ts +4 -0
- package/dist/drivers/runner.js +5 -0
- package/dist/drivers/turn.d.ts +7 -0
- package/dist/drivers/turn.js +21 -0
- package/dist/drivers/types.d.ts +6 -0
- package/dist/hooks/dispatcher.d.ts +7 -1
- package/dist/hooks/dispatcher.js +28 -1
- package/dist/hooks/matcher.d.ts +4 -0
- package/dist/hooks/matcher.js +19 -2
- package/dist/host/api-impl.d.ts +8 -0
- package/dist/host/api-impl.js +10 -0
- package/dist/host/resources.d.ts +25 -0
- package/dist/host/resources.js +63 -0
- package/dist/host/types.d.ts +24 -2
- package/dist/host/types.js +9 -2
- package/dist/host.d.ts +1 -0
- package/dist/host.js +2 -0
- package/dist/i18n/catalog.d.ts +26 -0
- package/dist/i18n/messages/approval.d.ts +9 -0
- package/dist/i18n/messages/approval.js +9 -0
- package/dist/i18n/messages/config-keys.d.ts +8 -0
- package/dist/i18n/messages/config-keys.js +8 -0
- package/dist/i18n/messages/config.d.ts +8 -0
- package/dist/i18n/messages/drivers.d.ts +16 -0
- package/dist/i18n/messages/drivers.js +16 -0
- package/dist/i18n/messages/panels.d.ts +15 -0
- package/dist/i18n/messages/panels.js +5 -0
- package/dist/i18n/messages/settings.d.ts +6 -0
- package/dist/i18n/messages/settings.js +6 -0
- package/dist/modes/acp/acp-events.d.ts +3 -0
- package/dist/modes/acp/acp-events.js +21 -0
- package/dist/modes/interactive/agent-bar.d.ts +7 -2
- package/dist/modes/interactive/agent-bar.js +16 -6
- package/dist/modes/interactive/approval-ui.d.ts +2 -0
- package/dist/modes/interactive/approval-ui.js +4 -1
- package/dist/modes/interactive/interactive-mode.js +6 -1
- package/dist/modes/interactive/key-dispatch.d.ts +5 -0
- package/dist/modes/interactive/key-dispatch.js +28 -4
- package/dist/modes/interactive/program-status-ui.d.ts +1 -0
- package/dist/modes/interactive/program-status-ui.js +38 -5
- package/dist/modes/interactive/session-events.js +13 -1
- package/dist/modes/interactive/subagent-view.d.ts +4 -1
- package/dist/modes/interactive/subagent-view.js +26 -6
- package/dist/modes/interactive/tasks-report.d.ts +4 -1
- package/dist/modes/interactive/tasks-report.js +12 -0
- package/dist/modes/rpc/commands.d.ts +10 -1
- package/dist/modes/rpc/commands.js +19 -1
- package/dist/permissions/broker.d.ts +38 -3
- package/dist/permissions/broker.js +118 -7
- package/dist/permissions/pipeline.d.ts +11 -10
- package/dist/permissions/pipeline.js +18 -46
- package/dist/permissions/rules.d.ts +21 -4
- package/dist/permissions/rules.js +43 -9
- package/dist/permissions/session-grants.d.ts +14 -0
- package/dist/permissions/session-grants.js +47 -0
- package/dist/permissions/types.d.ts +18 -0
- package/dist/session/unclean-end.d.ts +47 -0
- package/dist/session/unclean-end.js +153 -0
- package/dist/tools/background-jobs.d.ts +50 -3
- package/dist/tools/background-jobs.js +70 -6
- package/dist/tools/bash-jobs.d.ts +26 -0
- package/dist/tools/bash-jobs.js +165 -0
- package/dist/tools/bash.d.ts +22 -5
- package/dist/tools/bash.js +76 -103
- package/dist/tools/output-accumulator.d.ts +5 -0
- package/dist/tools/output-accumulator.js +9 -0
- package/dist/tools/read-lines.d.ts +2 -0
- package/dist/tools/read-lines.js +4 -0
- package/dist/tools/read-resource.d.ts +24 -0
- package/dist/tools/read-resource.js +129 -0
- package/dist/tools/read.d.ts +7 -2
- package/dist/tools/read.js +16 -5
- package/dist/tools/registry.d.ts +5 -2
- package/dist/tools/registry.js +8 -3
- package/dist/tools/report.d.ts +24 -0
- package/dist/tools/report.js +81 -0
- package/dist/tools/resource-ref.d.ts +90 -0
- package/dist/tools/resource-ref.js +90 -0
- package/dist/tools/task-ctl.d.ts +8 -1
- package/dist/tools/task-ctl.js +31 -5
- package/dist/tools/task.d.ts +2 -0
- package/dist/tools/task.js +3 -0
- package/dist/tools/timers.d.ts +18 -0
- package/dist/tools/timers.js +26 -0
- package/dist/tools/types.d.ts +30 -0
- package/dist/tools/types.js +2 -0
- package/docs/en/guides/permissions.md +14 -0
- package/docs/en/guides/tui.md +21 -21
- package/docs/en/reference/acp.md +15 -14
- package/docs/en/reference/host-api.md +48 -20
- package/docs/en/reference/rpc.md +12 -9
- package/docs/guides/agents.md +83 -22
- package/docs/guides/hooks.md +12 -9
- package/docs/guides/permissions.md +14 -0
- package/docs/guides/tui.md +21 -21
- package/docs/reference/acp.md +15 -14
- package/docs/reference/host-api.md +69 -41
- package/docs/reference/rpc.md +15 -9
- package/docs/reference/session-format.md +29 -11
- package/package.json +3 -3
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
* allow 规则 / Hook allow 命中放行,task 同 plan 放行(子会话共用本管线),其余 deny——从不询问
|
|
22
22
|
* (危险命令、Hook ask 也 deny)。
|
|
23
23
|
*
|
|
24
|
-
* allow_session
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* allow_session 记忆(内存,不落盘,session-grants.ts):bash 记命令的前两个词(`npm test`),之后以它开头的
|
|
25
|
+
* 命令命中;带 path 的工具记文件所在目录,之后该目录下的路径命中;[#270] 已注册的资源引用记 `scheme://`;
|
|
26
|
+
* 其它工具只记工具名。记忆只作用于第 ③ 步模式产生的 ask(auto 里只作用于规则层之后)。
|
|
27
27
|
*
|
|
28
28
|
* [S2] bash 沙箱(docs/guides/permissions.md「判定顺序」):default / auto-edit 下,bash 调用若将在 OS 沙箱内
|
|
29
29
|
* 运行且沙箱拒绝网络、没请求 `sandbox: false`、命令文本不碰机密路径、嵌套不超深,第 ③ 步的 ask 变
|
|
@@ -33,11 +33,12 @@
|
|
|
33
33
|
* 包装里的命令(`sh -c '…'`、`eval`、`xargs`、`find -exec`,见 dangerous.ts)逐层核对:deny 规则命中
|
|
34
34
|
* 任一层即 deny;allow 规则与会话记忆要求外层与每一层嵌套命令都被覆盖,嵌套超深时不放行。
|
|
35
35
|
*/
|
|
36
|
-
import { dirname, sep } from "node:path";
|
|
37
36
|
import { isBackgroundJobQuery } from "../tools/background-jobs.js";
|
|
38
37
|
import { memoryEffectivePermission } from "./memory-class.js";
|
|
39
|
-
import { networkBlockReason, networkEffectivePermission
|
|
40
|
-
import { findAllowRule, findDenyRule, inputCommand, inputPath,
|
|
38
|
+
import { networkBlockReason, networkEffectivePermission } from "./network-class.js";
|
|
39
|
+
import { findAllowRule, findDenyRule, inputCommand, inputPath, inputResourceRef, } from "./rules.js";
|
|
40
|
+
import { grantCovers, sessionGrantFor } from "./session-grants.js";
|
|
41
|
+
export { sessionGrantFor } from "./session-grants.js";
|
|
41
42
|
import { collectNestedCommands, matchDangerous } from "./dangerous.js";
|
|
42
43
|
import { analyzeBashForAuto, secretReferenceReason } from "./auto-safe.js";
|
|
43
44
|
import { runsSandboxed, wantsUnsandboxed } from "../sandbox/bash.js";
|
|
@@ -116,40 +117,6 @@ function auditSummary(toolName, input) {
|
|
|
116
117
|
const text = typeof raw === "string" ? raw.replace(/\s+/g, " ").trim() : toolName;
|
|
117
118
|
return text.length > 80 ? `${text.slice(0, 79)}…` : text;
|
|
118
119
|
}
|
|
119
|
-
export function sessionGrantFor(toolName, input, cwd) {
|
|
120
|
-
if (toolName === "memory")
|
|
121
|
-
return { toolName, kind: "any", prefix: "" }; // [W6-M] 本会话允许全部写命令
|
|
122
|
-
const host = networkGrantHost(input); // web_fetch:本会话允许该主机
|
|
123
|
-
if (host !== undefined)
|
|
124
|
-
return { toolName, kind: "host", prefix: host };
|
|
125
|
-
const command = inputCommand(input);
|
|
126
|
-
if (command !== undefined) {
|
|
127
|
-
const words = normalizeCommand(command).split(" ").slice(0, 2);
|
|
128
|
-
return { toolName, kind: "command", prefix: words.join(" ") };
|
|
129
|
-
}
|
|
130
|
-
const path = inputPath(toolName, input, cwd);
|
|
131
|
-
if (path !== undefined)
|
|
132
|
-
return { toolName, kind: "dir", prefix: dirname(path) };
|
|
133
|
-
return { toolName, kind: "any", prefix: "" };
|
|
134
|
-
}
|
|
135
|
-
function grantCovers(grant, toolName, input, cwd) {
|
|
136
|
-
if (grant.toolName !== toolName)
|
|
137
|
-
return false;
|
|
138
|
-
if (grant.kind === "any")
|
|
139
|
-
return true;
|
|
140
|
-
if (grant.kind === "host")
|
|
141
|
-
return networkGrantHost(input) === grant.prefix;
|
|
142
|
-
if (grant.kind === "command") {
|
|
143
|
-
const command = inputCommand(input);
|
|
144
|
-
if (command === undefined || /\$\(|`/.test(command))
|
|
145
|
-
return false;
|
|
146
|
-
const segments = splitShellSegments(command);
|
|
147
|
-
return (segments.length > 0 &&
|
|
148
|
-
segments.every((s) => s === grant.prefix || s.startsWith(`${grant.prefix} `)));
|
|
149
|
-
}
|
|
150
|
-
const path = inputPath(toolName, input, cwd);
|
|
151
|
-
return path !== undefined && (path === grant.prefix || path.startsWith(grant.prefix + sep));
|
|
152
|
-
}
|
|
153
120
|
export class PermissionPipeline {
|
|
154
121
|
currentMode;
|
|
155
122
|
ruleList;
|
|
@@ -160,8 +127,10 @@ export class PermissionPipeline {
|
|
|
160
127
|
autoSafeCommands;
|
|
161
128
|
planBashMode;
|
|
162
129
|
bashSandbox;
|
|
130
|
+
resources;
|
|
163
131
|
constructor(options) {
|
|
164
132
|
this.bashSandbox = options.bashSandbox;
|
|
133
|
+
this.resources = options.resources;
|
|
165
134
|
this.currentMode = options.mode;
|
|
166
135
|
this.planBashMode = options.planBash ?? "readonly";
|
|
167
136
|
this.ruleList = [...options.rules];
|
|
@@ -186,7 +155,7 @@ export class PermissionPipeline {
|
|
|
186
155
|
return this.ruleList;
|
|
187
156
|
}
|
|
188
157
|
rememberForSession(toolName, input) {
|
|
189
|
-
this.grants.push(sessionGrantFor(toolName, input, this.cwd));
|
|
158
|
+
this.grants.push(sessionGrantFor(toolName, input, this.cwd, this.resources));
|
|
190
159
|
}
|
|
191
160
|
clearSessionGrants() {
|
|
192
161
|
this.grants.length = 0;
|
|
@@ -236,7 +205,7 @@ export class PermissionPipeline {
|
|
|
236
205
|
const layers = layeredInputs(toolName, input.input);
|
|
237
206
|
let deny;
|
|
238
207
|
for (const layer of layers.inputs) {
|
|
239
|
-
deny = findDenyRule(this.ruleList, toolName, layer, this.cwd);
|
|
208
|
+
deny = findDenyRule(this.ruleList, toolName, layer, this.cwd, this.resources);
|
|
240
209
|
if (deny)
|
|
241
210
|
break;
|
|
242
211
|
}
|
|
@@ -299,11 +268,11 @@ export class PermissionPipeline {
|
|
|
299
268
|
if (verdict.decision === "ask") {
|
|
300
269
|
const allow = layers.tooDeep
|
|
301
270
|
? undefined
|
|
302
|
-
: findAllowRule(this.ruleList, toolName, input.input, this.cwd);
|
|
271
|
+
: findAllowRule(this.ruleList, toolName, input.input, this.cwd, this.resources);
|
|
303
272
|
const allowAll = allow !== undefined &&
|
|
304
|
-
layers.inputs.every((l) => findAllowRule(this.ruleList, toolName, l, this.cwd));
|
|
273
|
+
layers.inputs.every((l) => findAllowRule(this.ruleList, toolName, l, this.cwd, this.resources));
|
|
305
274
|
const granted = !layers.tooDeep &&
|
|
306
|
-
layers.inputs.every((l) => this.grants.some((g) => grantCovers(g, toolName, l, this.cwd)));
|
|
275
|
+
layers.inputs.every((l) => this.grants.some((g) => grantCovers(g, toolName, l, this.cwd, this.resources)));
|
|
307
276
|
if (allowAll)
|
|
308
277
|
verdict = { decision: "allow", step: "allow-rule", rule: allow };
|
|
309
278
|
else if (input.hookDecision === "allow")
|
|
@@ -360,7 +329,7 @@ export class PermissionPipeline {
|
|
|
360
329
|
return { decision: "allow", step: "hook" };
|
|
361
330
|
if (useGrants &&
|
|
362
331
|
!layers.tooDeep &&
|
|
363
|
-
layers.inputs.every((l) => this.grants.some((g) => grantCovers(g, toolName, l, this.cwd)))) {
|
|
332
|
+
layers.inputs.every((l) => this.grants.some((g) => grantCovers(g, toolName, l, this.cwd, this.resources)))) {
|
|
364
333
|
return { decision: "allow", step: "session" };
|
|
365
334
|
}
|
|
366
335
|
return undefined;
|
|
@@ -386,6 +355,9 @@ export class PermissionPipeline {
|
|
|
386
355
|
: undefined;
|
|
387
356
|
if (typeof raw !== "string" && input.permission !== "write")
|
|
388
357
|
return {};
|
|
358
|
+
// [#270] 已注册的资源引用不是文件路径:read 类不做机密路径检查(未注册的 xx:// 仍按文件查)
|
|
359
|
+
if (input.permission === "read" && inputResourceRef(input.input, this.resources) !== undefined)
|
|
360
|
+
return {};
|
|
389
361
|
const path = inputPath(input.toolName, input.input, this.cwd);
|
|
390
362
|
if (path === undefined)
|
|
391
363
|
return {};
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* - 带 `path` 的工具(read / write / edit / ls / grep / glob 及宿主工具):对路径做 glob(`**`)。
|
|
7
7
|
* 相对模式匹配 cwd 内的相对路径;以 `**` 开头的模式也匹配绝对路径(例如 .ssh 目录规则命中 ~/.ssh 下的文件);
|
|
8
8
|
* 绝对模式(`/…`、`~/…`)匹配绝对路径。缺 path 的调用以 cwd 为路径。
|
|
9
|
+
* - [#270] `path` 是**已注册**的宿主资源引用(`scheme://path`,tools/resource-ref.ts;注册表经 `resources`
|
|
10
|
+
* 传入)时不按文件路径解析:引用先规范成 `小写scheme://path`,模式里的 scheme 也转小写,再做同一套
|
|
11
|
+
* glob(`read(demo://**)`、`read(demo://items/*)`);未注册的 scheme 或没有注册表时一律按文件路径处理;
|
|
9
12
|
* - 带 `url` 的工具(web_fetch):对 `host/path` 匹配,裸域名含子域(network-class.ts);
|
|
10
13
|
* - 其它工具带 pattern 的规则不匹配(无可比对的对象)。
|
|
11
14
|
*
|
|
@@ -15,6 +18,13 @@
|
|
|
15
18
|
* 来源约束(§7.2):项目级只能收紧——allow 被忽略、mode 只能更严,均产生 warning。
|
|
16
19
|
*/
|
|
17
20
|
import type { PermissionMode, Rule, RuleSource } from "./types.js";
|
|
21
|
+
/**
|
|
22
|
+
* [#270] 已注册资源的查找(`HostResourceLookup.get` 的子集:返回非 undefined 即已注册);
|
|
23
|
+
* 缺省 = 没有资源,`scheme://` 一律按文件路径。
|
|
24
|
+
*/
|
|
25
|
+
export type ResourceSchemes = {
|
|
26
|
+
get(scheme: string): unknown;
|
|
27
|
+
} | undefined;
|
|
18
28
|
/** 内置缺省 deny 表(用户可经 `builtinDeny` 选项移除)。 */
|
|
19
29
|
export declare const BUILTIN_DENY_RULES: readonly string[];
|
|
20
30
|
export declare function parseRule(raw: string, effect: "allow" | "deny", source: RuleSource): Rule;
|
|
@@ -26,14 +36,21 @@ export declare function normalizeCommand(command: string): string;
|
|
|
26
36
|
*/
|
|
27
37
|
export declare function splitShellSegments(command: string): string[];
|
|
28
38
|
export declare function hasCommandSubstitution(command: string): boolean;
|
|
29
|
-
/**
|
|
30
|
-
|
|
39
|
+
/**
|
|
40
|
+
* [#270] 输入里的 `path` 若是**已注册**的宿主资源引用,返回规范形式 `小写scheme://path`(deny 规则、会话
|
|
41
|
+
* 记忆与审计看到的和 read 实际读取的一致);未注册、没有注册表或不是引用形状返回 undefined。
|
|
42
|
+
*/
|
|
43
|
+
export declare function inputResourceRef(input: unknown, resources: ResourceSchemes): string | undefined;
|
|
44
|
+
/** [#270] 资源引用的会话记忆前缀:小写 `scheme://`;不是已注册的资源引用返回 undefined。 */
|
|
45
|
+
export declare function inputResourcePrefix(input: unknown, resources: ResourceSchemes): string | undefined;
|
|
46
|
+
/** 输入里的路径(绝对;[#270] 已注册的资源引用返回规范形式);没有 path 字段的工具返回 undefined。 */
|
|
47
|
+
export declare function inputPath(toolName: string, input: unknown, cwd: string, resources?: ResourceSchemes): string | undefined;
|
|
31
48
|
export declare function inputCommand(input: unknown): string | undefined;
|
|
32
49
|
export declare function pathMatches(pattern: string, absPath: string, cwd: string): boolean;
|
|
33
50
|
/** 第一条命中的 deny 规则。 */
|
|
34
|
-
export declare function findDenyRule(rules: readonly Rule[], toolName: string, input: unknown, cwd: string): Rule | undefined;
|
|
51
|
+
export declare function findDenyRule(rules: readonly Rule[], toolName: string, input: unknown, cwd: string, resources?: ResourceSchemes): Rule | undefined;
|
|
35
52
|
/** 覆盖本次调用的 allow 规则(复合命令返回第一段的覆盖者);不覆盖返回 undefined。 */
|
|
36
|
-
export declare function findAllowRule(rules: readonly Rule[], toolName: string, input: unknown, cwd: string): Rule | undefined;
|
|
53
|
+
export declare function findAllowRule(rules: readonly Rule[], toolName: string, input: unknown, cwd: string, resources?: ResourceSchemes): Rule | undefined;
|
|
37
54
|
/** a 是否比 b 更严(或相等)。 */
|
|
38
55
|
export declare function isAtLeastAsStrict(a: PermissionMode, b: PermissionMode): boolean;
|
|
39
56
|
export interface PermissionLayer {
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* - 带 `path` 的工具(read / write / edit / ls / grep / glob 及宿主工具):对路径做 glob(`**`)。
|
|
7
7
|
* 相对模式匹配 cwd 内的相对路径;以 `**` 开头的模式也匹配绝对路径(例如 .ssh 目录规则命中 ~/.ssh 下的文件);
|
|
8
8
|
* 绝对模式(`/…`、`~/…`)匹配绝对路径。缺 path 的调用以 cwd 为路径。
|
|
9
|
+
* - [#270] `path` 是**已注册**的宿主资源引用(`scheme://path`,tools/resource-ref.ts;注册表经 `resources`
|
|
10
|
+
* 传入)时不按文件路径解析:引用先规范成 `小写scheme://path`,模式里的 scheme 也转小写,再做同一套
|
|
11
|
+
* glob(`read(demo://**)`、`read(demo://items/*)`);未注册的 scheme 或没有注册表时一律按文件路径处理;
|
|
9
12
|
* - 带 `url` 的工具(web_fetch):对 `host/path` 匹配,裸域名含子域(network-class.ts);
|
|
10
13
|
* - 其它工具带 pattern 的规则不匹配(无可比对的对象)。
|
|
11
14
|
*
|
|
@@ -19,6 +22,8 @@ import { AmaError } from "../errors.js";
|
|
|
19
22
|
import { PERMISSION_MODES_STRICT_FIRST } from "./types.js";
|
|
20
23
|
import { globBody } from "../tools/glob.js";
|
|
21
24
|
import { expandHome, resolvePath, toPosix } from "../tools/paths.js";
|
|
25
|
+
import { canonicalResourceRef, parseResourceRef } from "../tools/resource-ref.js";
|
|
26
|
+
const SCHEME_PREFIX_RE = /^([A-Za-z][A-Za-z0-9+.-]{1,31}):\/\//;
|
|
22
27
|
import { memoryRuleMatches } from "./memory-class.js";
|
|
23
28
|
import { networkRuleMatches } from "./network-class.js";
|
|
24
29
|
/** 内置缺省 deny 表(用户可经 `builtinDeny` 选项移除)。 */
|
|
@@ -107,8 +112,28 @@ export function hasCommandSubstitution(command) {
|
|
|
107
112
|
function pathGlob(pattern) {
|
|
108
113
|
return new RegExp(`^${globBody(pattern, { braces: true })}$`);
|
|
109
114
|
}
|
|
110
|
-
/**
|
|
111
|
-
|
|
115
|
+
/**
|
|
116
|
+
* [#270] 输入里的 `path` 若是**已注册**的宿主资源引用,返回规范形式 `小写scheme://path`(deny 规则、会话
|
|
117
|
+
* 记忆与审计看到的和 read 实际读取的一致);未注册、没有注册表或不是引用形状返回 undefined。
|
|
118
|
+
*/
|
|
119
|
+
export function inputResourceRef(input, resources) {
|
|
120
|
+
const raw = typeof input === "object" && input !== null
|
|
121
|
+
? input["path"]
|
|
122
|
+
: undefined;
|
|
123
|
+
if (typeof raw !== "string")
|
|
124
|
+
return undefined;
|
|
125
|
+
return canonicalResourceRef(raw, resources);
|
|
126
|
+
}
|
|
127
|
+
/** [#270] 资源引用的会话记忆前缀:小写 `scheme://`;不是已注册的资源引用返回 undefined。 */
|
|
128
|
+
export function inputResourcePrefix(input, resources) {
|
|
129
|
+
const ref = inputResourceRef(input, resources);
|
|
130
|
+
return ref === undefined ? undefined : ref.slice(0, ref.indexOf("://") + 3);
|
|
131
|
+
}
|
|
132
|
+
/** 输入里的路径(绝对;[#270] 已注册的资源引用返回规范形式);没有 path 字段的工具返回 undefined。 */
|
|
133
|
+
export function inputPath(toolName, input, cwd, resources) {
|
|
134
|
+
const ref = inputResourceRef(input, resources);
|
|
135
|
+
if (ref !== undefined)
|
|
136
|
+
return ref;
|
|
112
137
|
const raw = typeof input === "object" && input !== null
|
|
113
138
|
? input["path"]
|
|
114
139
|
: undefined;
|
|
@@ -131,6 +156,14 @@ export function inputCommand(input) {
|
|
|
131
156
|
return typeof raw === "string" ? raw : undefined;
|
|
132
157
|
}
|
|
133
158
|
export function pathMatches(pattern, absPath, cwd) {
|
|
159
|
+
// [#270] 资源引用(inputPath 已规范成小写 scheme;绝对文件路径不会是这个形状):模式的 scheme 也转小写,
|
|
160
|
+
// 对引用做 glob(`**` 跨 `/`),不展开 `~`、不解析 cwd
|
|
161
|
+
const ref = parseResourceRef(absPath);
|
|
162
|
+
if (ref !== undefined) {
|
|
163
|
+
const subject = `${ref.scheme}://${ref.path}`;
|
|
164
|
+
const normalized = pattern.replace(SCHEME_PREFIX_RE, (_m, scheme) => `${scheme.toLowerCase()}://`);
|
|
165
|
+
return pathGlob(normalized).test(subject);
|
|
166
|
+
}
|
|
134
167
|
const absPosix = toPosix(absPath);
|
|
135
168
|
const expanded = expandHome(pattern);
|
|
136
169
|
if (isAbsolute(expanded))
|
|
@@ -145,7 +178,7 @@ export function pathMatches(pattern, absPath, cwd) {
|
|
|
145
178
|
return pattern.startsWith("**") && regex.test(absPosix.replace(/^\/+/, ""));
|
|
146
179
|
}
|
|
147
180
|
/** 单条规则对一个(非复合)对象是否命中。 */
|
|
148
|
-
function matchesSubject(rule, toolName, input, cwd, command) {
|
|
181
|
+
function matchesSubject(rule, toolName, input, cwd, command, resources) {
|
|
149
182
|
if (!toolNameMatches(rule.tool, toolName))
|
|
150
183
|
return false;
|
|
151
184
|
if (rule.pattern === undefined)
|
|
@@ -154,19 +187,19 @@ function matchesSubject(rule, toolName, input, cwd, command) {
|
|
|
154
187
|
return memoryRuleMatches(rule.pattern, input); // [W6-M] 逻辑路径 / 命令名
|
|
155
188
|
if (command !== undefined)
|
|
156
189
|
return wildcardToRegExp(rule.pattern).test(command);
|
|
157
|
-
const abs = inputPath(toolName, input, cwd);
|
|
190
|
+
const abs = inputPath(toolName, input, cwd, resources);
|
|
158
191
|
if (abs !== undefined)
|
|
159
192
|
return pathMatches(rule.pattern, abs, cwd);
|
|
160
193
|
return networkRuleMatches(rule.pattern, input);
|
|
161
194
|
}
|
|
162
195
|
/** 第一条命中的 deny 规则。 */
|
|
163
|
-
export function findDenyRule(rules, toolName, input, cwd) {
|
|
196
|
+
export function findDenyRule(rules, toolName, input, cwd, resources) {
|
|
164
197
|
const command = toolName === "memory" ? undefined : inputCommand(input);
|
|
165
198
|
for (const rule of rules) {
|
|
166
199
|
if (rule.effect !== "deny")
|
|
167
200
|
continue;
|
|
168
201
|
if (command === undefined || rule.pattern === undefined) {
|
|
169
|
-
if (matchesSubject(rule, toolName, input, cwd))
|
|
202
|
+
if (matchesSubject(rule, toolName, input, cwd, undefined, resources))
|
|
170
203
|
return rule;
|
|
171
204
|
continue;
|
|
172
205
|
}
|
|
@@ -177,11 +210,12 @@ export function findDenyRule(rules, toolName, input, cwd) {
|
|
|
177
210
|
return undefined;
|
|
178
211
|
}
|
|
179
212
|
/** 覆盖本次调用的 allow 规则(复合命令返回第一段的覆盖者);不覆盖返回 undefined。 */
|
|
180
|
-
export function findAllowRule(rules, toolName, input, cwd) {
|
|
213
|
+
export function findAllowRule(rules, toolName, input, cwd, resources) {
|
|
181
214
|
const allows = rules.filter((r) => r.effect === "allow");
|
|
182
215
|
const command = toolName === "memory" ? undefined : inputCommand(input);
|
|
183
|
-
if (command === undefined)
|
|
184
|
-
return allows.find((r) => matchesSubject(r, toolName, input, cwd));
|
|
216
|
+
if (command === undefined) {
|
|
217
|
+
return allows.find((r) => matchesSubject(r, toolName, input, cwd, undefined, resources));
|
|
218
|
+
}
|
|
185
219
|
const unconditional = allows.find((r) => r.pattern === undefined && toolNameMatches(r.tool, toolName));
|
|
186
220
|
if (unconditional)
|
|
187
221
|
return unconditional;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* allow_session 会话记忆的粒度(设计 §7.1;从 pipeline.ts 拆出):
|
|
3
|
+
* bash 记命令的前两个词、web_fetch 记主机、[#270] 已注册的宿主资源记 `scheme://`、带 path 的工具记目录、
|
|
4
|
+
* memory 与其它工具记工具名。`sessionGrantFor` 产生记忆,`grantCovers` 判断一次调用是否被覆盖。
|
|
5
|
+
*/
|
|
6
|
+
import { type ResourceSchemes } from "./rules.js";
|
|
7
|
+
export interface SessionGrant {
|
|
8
|
+
toolName: string;
|
|
9
|
+
/** [#270] `resource`:本会话允许该 scheme(前缀 `scheme://`)。 */
|
|
10
|
+
kind: "command" | "dir" | "host" | "resource" | "any";
|
|
11
|
+
prefix: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function sessionGrantFor(toolName: string, input: unknown, cwd: string, resources?: ResourceSchemes): SessionGrant;
|
|
14
|
+
export declare function grantCovers(grant: SessionGrant, toolName: string, input: unknown, cwd: string, resources: ResourceSchemes): boolean;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* allow_session 会话记忆的粒度(设计 §7.1;从 pipeline.ts 拆出):
|
|
3
|
+
* bash 记命令的前两个词、web_fetch 记主机、[#270] 已注册的宿主资源记 `scheme://`、带 path 的工具记目录、
|
|
4
|
+
* memory 与其它工具记工具名。`sessionGrantFor` 产生记忆,`grantCovers` 判断一次调用是否被覆盖。
|
|
5
|
+
*/
|
|
6
|
+
import { dirname, sep } from "node:path";
|
|
7
|
+
import { networkGrantHost } from "./network-class.js";
|
|
8
|
+
import { inputCommand, inputPath, inputResourcePrefix, normalizeCommand, splitShellSegments, } from "./rules.js";
|
|
9
|
+
export function sessionGrantFor(toolName, input, cwd, resources) {
|
|
10
|
+
if (toolName === "memory")
|
|
11
|
+
return { toolName, kind: "any", prefix: "" }; // [W6-M] 本会话允许全部写命令
|
|
12
|
+
const host = networkGrantHost(input); // web_fetch:本会话允许该主机
|
|
13
|
+
if (host !== undefined)
|
|
14
|
+
return { toolName, kind: "host", prefix: host };
|
|
15
|
+
const resource = inputResourcePrefix(input, resources); // [#270] 已注册资源:本会话允许该 scheme
|
|
16
|
+
if (resource !== undefined)
|
|
17
|
+
return { toolName, kind: "resource", prefix: resource };
|
|
18
|
+
const command = inputCommand(input);
|
|
19
|
+
if (command !== undefined) {
|
|
20
|
+
const words = normalizeCommand(command).split(" ").slice(0, 2);
|
|
21
|
+
return { toolName, kind: "command", prefix: words.join(" ") };
|
|
22
|
+
}
|
|
23
|
+
const path = inputPath(toolName, input, cwd, resources);
|
|
24
|
+
if (path !== undefined)
|
|
25
|
+
return { toolName, kind: "dir", prefix: dirname(path) };
|
|
26
|
+
return { toolName, kind: "any", prefix: "" };
|
|
27
|
+
}
|
|
28
|
+
export function grantCovers(grant, toolName, input, cwd, resources) {
|
|
29
|
+
if (grant.toolName !== toolName)
|
|
30
|
+
return false;
|
|
31
|
+
if (grant.kind === "any")
|
|
32
|
+
return true;
|
|
33
|
+
if (grant.kind === "host")
|
|
34
|
+
return networkGrantHost(input) === grant.prefix;
|
|
35
|
+
if (grant.kind === "resource")
|
|
36
|
+
return inputResourcePrefix(input, resources) === grant.prefix;
|
|
37
|
+
if (grant.kind === "command") {
|
|
38
|
+
const command = inputCommand(input);
|
|
39
|
+
if (command === undefined || /\$\(|`/.test(command))
|
|
40
|
+
return false;
|
|
41
|
+
const segments = splitShellSegments(command);
|
|
42
|
+
return (segments.length > 0 &&
|
|
43
|
+
segments.every((s) => s === grant.prefix || s.startsWith(`${grant.prefix} `)));
|
|
44
|
+
}
|
|
45
|
+
const path = inputPath(toolName, input, cwd, resources);
|
|
46
|
+
return path !== undefined && (path === grant.prefix || path.startsWith(grant.prefix + sep));
|
|
47
|
+
}
|
|
@@ -66,6 +66,24 @@ export interface ApprovalRequest {
|
|
|
66
66
|
* 调用带外层调用的 `parentToolCallId`。由子会话包装的 broker 填入,缺省视为主会话。
|
|
67
67
|
*/
|
|
68
68
|
context?: ApprovalRequestContext;
|
|
69
|
+
/**
|
|
70
|
+
* [#271] 切换权限模式时重新判定的入口:ama 自己的工具调用由 `gateToolCall` 填;外部 Agent 的权限请求
|
|
71
|
+
* 与首次运行确认没有(永不代答)。子会话浅拷贝请求时保留同一引用,链把结论写回 `outcome`。
|
|
72
|
+
*/
|
|
73
|
+
reevaluation?: ApprovalReevaluation;
|
|
74
|
+
}
|
|
75
|
+
/** [#271] 待审批请求按当前模式的重判。 */
|
|
76
|
+
export interface ApprovalReevaluation {
|
|
77
|
+
/** 以管线当前模式重判同一调用(Hook 决策沿用、不调分类器)。 */
|
|
78
|
+
check(): PermissionVerdict;
|
|
79
|
+
/** 被模式切换自动作答后由审批链写入(`permission_resolved.reason: "mode_changed"`)。 */
|
|
80
|
+
outcome?: ModeReevaluationOutcome;
|
|
81
|
+
}
|
|
82
|
+
export interface ModeReevaluationOutcome {
|
|
83
|
+
mode: PermissionMode;
|
|
84
|
+
decision: "allow" | "deny";
|
|
85
|
+
/** 拒绝时管线给的说明(给模型的文本里引用)。 */
|
|
86
|
+
message?: string;
|
|
69
87
|
}
|
|
70
88
|
export interface ApprovalRequestContext {
|
|
71
89
|
depth: number;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 异常结束的判定(#268):只看会话文件的活动分支,不需要新的持久化。
|
|
3
|
+
*
|
|
4
|
+
* 记 A = 分支上最后一条 assistant 消息,T = A 之后的条目。下面任一成立即「上一轮没有正常结束」:
|
|
5
|
+
* - R1 悬空调用:A 有 toolCall 块而 T 里缺对应的 toolResult(正常运行下错误 / 中断路径都由
|
|
6
|
+
* `closeDanglingCalls` 补齐,只有工具执行中进程被杀才会缺);
|
|
7
|
+
* - R2 非人为中断:A 的 stopReason 是 `aborted` 而 T 里没有 `custom_message{ama.aborted}`(人按 Esc、
|
|
8
|
+
* `interrupt: true`、RPC abort、SIGINT 都会在 abort 之后落这一条);
|
|
9
|
+
* - R3 回合没结束:本会话在写 `ama.trace`(A 之前有 step),而 A 之后没有 step(`turn_end` 时才写,
|
|
10
|
+
* toolResult 之后;结果已落、finishTurn 期间被杀就落在这里)。
|
|
11
|
+
*
|
|
12
|
+
* 明确不判:最后一条是 user(本回合还没有任何工具动作);最后一条是 toolResult 且其后有 step(既可能
|
|
13
|
+
* 是下一次请求中被杀,也可能是 terminate / 重复调用检测 / Hook 停止 / 预算到限的正常停止,文件里分不出,
|
|
14
|
+
* 而且结果都在上下文里);`error` / `length`;有 `ama.aborted` 的中断。
|
|
15
|
+
*
|
|
16
|
+
* 「只提醒一次」靠已落盘的提醒本身:注入的 `custom_message{ama.reminder}` 带 `details.recovery = true`,
|
|
17
|
+
* T 里已有它就 `reminded: true`。
|
|
18
|
+
*
|
|
19
|
+
* 本模块不 import agent/(依赖方向 agent → session),自定义类型名字面量与 agent 侧常量一致(测试守住)。
|
|
20
|
+
*/
|
|
21
|
+
import type { SessionEntry } from "./types.js";
|
|
22
|
+
/** 与 `agent/session-run.ts` 的 `ABORTED_CUSTOM_TYPE` 相同。 */
|
|
23
|
+
export declare const UNCLEAN_ABORTED_TYPE = "ama.aborted";
|
|
24
|
+
/** 与 `agent/reminders.ts` 的 `REMINDER_CUSTOM_TYPE` 相同。 */
|
|
25
|
+
export declare const UNCLEAN_REMINDER_TYPE = "ama.reminder";
|
|
26
|
+
/** 与 `trace/types.ts` 的 `TRACE_CUSTOM_TYPE` 相同。 */
|
|
27
|
+
export declare const UNCLEAN_TRACE_TYPE = "ama.trace";
|
|
28
|
+
/** 工具清单的上限:条数与单项长度。 */
|
|
29
|
+
export declare const UNCLEAN_TOOLS_MAX = 12;
|
|
30
|
+
export declare const UNCLEAN_TARGET_MAX = 80;
|
|
31
|
+
export type UncleanEndReason = "dangling_tool_calls" | "aborted_without_interrupt" | "turn_not_ended";
|
|
32
|
+
export interface UncleanEnd {
|
|
33
|
+
reason: UncleanEndReason;
|
|
34
|
+
/** 上一轮调用过的工具(`name` 或 `name target`,缺结果的带 ` (no result)`),去重、已封顶。 */
|
|
35
|
+
tools: string[];
|
|
36
|
+
/** 封顶后没列出的条数。 */
|
|
37
|
+
more: number;
|
|
38
|
+
/** T 里已有恢复提醒(第二次 resume 不再注入)。 */
|
|
39
|
+
reminded: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 工具调用的目标:bash 取命令第一个词(跳过开头的 `VAR=…` 赋值;剩下的词仍带 `=` 或以 `-` 开头时不带
|
|
43
|
+
* 目标),write / edit 取路径(去掉 `?` / `#` 之后的部分,查询串里可能有令牌),其余不带目标;从不读结果正文。
|
|
44
|
+
*/
|
|
45
|
+
export declare function toolTarget(name: string, args: Record<string, unknown>): string | undefined;
|
|
46
|
+
/** 活动分支上一轮是否异常结束;干净(或没有 assistant)返回 undefined。 */
|
|
47
|
+
export declare function detectUncleanEnd(branch: readonly SessionEntry[]): UncleanEnd | undefined;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 异常结束的判定(#268):只看会话文件的活动分支,不需要新的持久化。
|
|
3
|
+
*
|
|
4
|
+
* 记 A = 分支上最后一条 assistant 消息,T = A 之后的条目。下面任一成立即「上一轮没有正常结束」:
|
|
5
|
+
* - R1 悬空调用:A 有 toolCall 块而 T 里缺对应的 toolResult(正常运行下错误 / 中断路径都由
|
|
6
|
+
* `closeDanglingCalls` 补齐,只有工具执行中进程被杀才会缺);
|
|
7
|
+
* - R2 非人为中断:A 的 stopReason 是 `aborted` 而 T 里没有 `custom_message{ama.aborted}`(人按 Esc、
|
|
8
|
+
* `interrupt: true`、RPC abort、SIGINT 都会在 abort 之后落这一条);
|
|
9
|
+
* - R3 回合没结束:本会话在写 `ama.trace`(A 之前有 step),而 A 之后没有 step(`turn_end` 时才写,
|
|
10
|
+
* toolResult 之后;结果已落、finishTurn 期间被杀就落在这里)。
|
|
11
|
+
*
|
|
12
|
+
* 明确不判:最后一条是 user(本回合还没有任何工具动作);最后一条是 toolResult 且其后有 step(既可能
|
|
13
|
+
* 是下一次请求中被杀,也可能是 terminate / 重复调用检测 / Hook 停止 / 预算到限的正常停止,文件里分不出,
|
|
14
|
+
* 而且结果都在上下文里);`error` / `length`;有 `ama.aborted` 的中断。
|
|
15
|
+
*
|
|
16
|
+
* 「只提醒一次」靠已落盘的提醒本身:注入的 `custom_message{ama.reminder}` 带 `details.recovery = true`,
|
|
17
|
+
* T 里已有它就 `reminded: true`。
|
|
18
|
+
*
|
|
19
|
+
* 本模块不 import agent/(依赖方向 agent → session),自定义类型名字面量与 agent 侧常量一致(测试守住)。
|
|
20
|
+
*/
|
|
21
|
+
/** 与 `agent/session-run.ts` 的 `ABORTED_CUSTOM_TYPE` 相同。 */
|
|
22
|
+
export const UNCLEAN_ABORTED_TYPE = "ama.aborted";
|
|
23
|
+
/** 与 `agent/reminders.ts` 的 `REMINDER_CUSTOM_TYPE` 相同。 */
|
|
24
|
+
export const UNCLEAN_REMINDER_TYPE = "ama.reminder";
|
|
25
|
+
/** 与 `trace/types.ts` 的 `TRACE_CUSTOM_TYPE` 相同。 */
|
|
26
|
+
export const UNCLEAN_TRACE_TYPE = "ama.trace";
|
|
27
|
+
/** 工具清单的上限:条数与单项长度。 */
|
|
28
|
+
export const UNCLEAN_TOOLS_MAX = 12;
|
|
29
|
+
export const UNCLEAN_TARGET_MAX = 80;
|
|
30
|
+
function lastAssistantIndex(branch) {
|
|
31
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
32
|
+
const entry = branch[i];
|
|
33
|
+
if (entry?.type === "message" && entry.message.role === "assistant")
|
|
34
|
+
return i;
|
|
35
|
+
}
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
function isTraceStep(entry) {
|
|
39
|
+
return (entry.type === "custom" &&
|
|
40
|
+
entry.customType === UNCLEAN_TRACE_TYPE &&
|
|
41
|
+
typeof entry.data === "object" &&
|
|
42
|
+
entry.data !== null &&
|
|
43
|
+
entry.data.kind === "step");
|
|
44
|
+
}
|
|
45
|
+
function isRecoveryReminder(entry) {
|
|
46
|
+
return (entry.type === "custom_message" &&
|
|
47
|
+
entry.customType === UNCLEAN_REMINDER_TYPE &&
|
|
48
|
+
typeof entry.details === "object" &&
|
|
49
|
+
entry.details !== null &&
|
|
50
|
+
entry.details.recovery === true);
|
|
51
|
+
}
|
|
52
|
+
function resultIds(entries) {
|
|
53
|
+
const ids = new Set();
|
|
54
|
+
for (const entry of entries)
|
|
55
|
+
if (entry.type === "message" && entry.message.role === "toolResult")
|
|
56
|
+
ids.add(entry.message.toolCallId);
|
|
57
|
+
return ids;
|
|
58
|
+
}
|
|
59
|
+
function reasonOf(branch, at, assistant, after) {
|
|
60
|
+
const calls = assistant.content.filter((block) => block.type === "toolCall");
|
|
61
|
+
if (calls.length > 0) {
|
|
62
|
+
const results = resultIds(after);
|
|
63
|
+
if (calls.some((call) => !results.has(call.id)))
|
|
64
|
+
return "dangling_tool_calls";
|
|
65
|
+
}
|
|
66
|
+
if (assistant.stopReason === "aborted") {
|
|
67
|
+
const interrupted = after.some((entry) => entry.type === "custom_message" && entry.customType === UNCLEAN_ABORTED_TYPE);
|
|
68
|
+
if (!interrupted)
|
|
69
|
+
return "aborted_without_interrupt";
|
|
70
|
+
}
|
|
71
|
+
const tracing = branch.slice(0, at).some(isTraceStep);
|
|
72
|
+
if (tracing && !after.some(isTraceStep))
|
|
73
|
+
return "turn_not_ended";
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
function clip(text) {
|
|
77
|
+
const one = text.replace(/\s+/g, " ").trim();
|
|
78
|
+
return one.length > UNCLEAN_TARGET_MAX ? `${one.slice(0, UNCLEAN_TARGET_MAX - 1)}…` : one;
|
|
79
|
+
}
|
|
80
|
+
/** 命令开头的环境变量赋值(`API_KEY=… cmd`):值可能是密钥,一个字都不带出去。 */
|
|
81
|
+
const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
82
|
+
/**
|
|
83
|
+
* 工具调用的目标:bash 取命令第一个词(跳过开头的 `VAR=…` 赋值;剩下的词仍带 `=` 或以 `-` 开头时不带
|
|
84
|
+
* 目标),write / edit 取路径(去掉 `?` / `#` 之后的部分,查询串里可能有令牌),其余不带目标;从不读结果正文。
|
|
85
|
+
*/
|
|
86
|
+
export function toolTarget(name, args) {
|
|
87
|
+
if (name === "bash") {
|
|
88
|
+
const command = args["command"];
|
|
89
|
+
if (typeof command !== "string")
|
|
90
|
+
return undefined;
|
|
91
|
+
const words = command.trim().split(/\s+/);
|
|
92
|
+
const first = words.find((word) => !ENV_ASSIGNMENT.test(word));
|
|
93
|
+
if (first === undefined || first === "" || first.includes("=") || first.startsWith("-")) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
return clip(first);
|
|
97
|
+
}
|
|
98
|
+
if (name === "write" || name === "edit") {
|
|
99
|
+
const path = args["path"];
|
|
100
|
+
if (typeof path !== "string")
|
|
101
|
+
return undefined;
|
|
102
|
+
const bare = path.split(/[?#]/, 1)[0] ?? "";
|
|
103
|
+
return bare !== "" ? clip(bare) : undefined;
|
|
104
|
+
}
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
/** 上一轮 = 最后一条非 steer 的 user 消息之后;列出其中全部 assistant 的工具调用。 */
|
|
108
|
+
function lastTurnTools(branch, results) {
|
|
109
|
+
let start = 0;
|
|
110
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
111
|
+
const entry = branch[i];
|
|
112
|
+
if (entry?.type === "message" &&
|
|
113
|
+
entry.message.role === "user" &&
|
|
114
|
+
entry.message.origin !== "steer") {
|
|
115
|
+
start = i + 1;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const seen = new Set();
|
|
120
|
+
const tools = [];
|
|
121
|
+
for (const entry of branch.slice(start)) {
|
|
122
|
+
if (entry.type !== "message" || entry.message.role !== "assistant")
|
|
123
|
+
continue;
|
|
124
|
+
for (const block of entry.message.content) {
|
|
125
|
+
if (block.type !== "toolCall")
|
|
126
|
+
continue;
|
|
127
|
+
const target = toolTarget(block.name, block.arguments);
|
|
128
|
+
const head = target === undefined ? block.name : `${block.name} ${target}`;
|
|
129
|
+
const line = results.has(block.id) ? head : `${head} (no result)`;
|
|
130
|
+
if (seen.has(line))
|
|
131
|
+
continue;
|
|
132
|
+
seen.add(line);
|
|
133
|
+
tools.push(line);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const shown = tools.slice(0, UNCLEAN_TOOLS_MAX);
|
|
137
|
+
return { tools: shown, more: tools.length - shown.length };
|
|
138
|
+
}
|
|
139
|
+
/** 活动分支上一轮是否异常结束;干净(或没有 assistant)返回 undefined。 */
|
|
140
|
+
export function detectUncleanEnd(branch) {
|
|
141
|
+
const at = lastAssistantIndex(branch);
|
|
142
|
+
if (at < 0)
|
|
143
|
+
return undefined;
|
|
144
|
+
const entry = branch[at];
|
|
145
|
+
if (entry?.type !== "message" || entry.message.role !== "assistant")
|
|
146
|
+
return undefined;
|
|
147
|
+
const after = branch.slice(at + 1);
|
|
148
|
+
const reason = reasonOf(branch, at, entry.message, after);
|
|
149
|
+
if (reason === undefined)
|
|
150
|
+
return undefined;
|
|
151
|
+
const { tools, more } = lastTurnTools(branch, resultIds(branch));
|
|
152
|
+
return { reason, tools, more, reminded: after.some(isRecoveryReminder) };
|
|
153
|
+
}
|