@armadra/agent 0.20.0 → 0.22.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/CHANGELOG.zh-CN.md +21 -0
  3. package/dist/agent/session-subagent.js +1 -1
  4. package/dist/agent/session-tools.js +3 -1
  5. package/dist/agent/subagent-land.d.ts +4 -0
  6. package/dist/agent/subagent-land.js +23 -0
  7. package/dist/agent/subagent-registry.d.ts +1 -0
  8. package/dist/agent/subagent-registry.js +8 -7
  9. package/dist/agent/worktree-land.d.ts +22 -0
  10. package/dist/agent/worktree-land.js +91 -0
  11. package/dist/agent/worktree-setup.d.ts +12 -0
  12. package/dist/agent/worktree-setup.js +84 -0
  13. package/dist/agent/worktree.d.ts +22 -2
  14. package/dist/agent/worktree.js +104 -10
  15. package/dist/agents/catalog.d.ts +1 -0
  16. package/dist/agents/result.js +2 -0
  17. package/dist/agents/task-control.d.ts +3 -0
  18. package/dist/bundle/ama.cjs +1693 -1335
  19. package/dist/cli/compose-agents.js +4 -1
  20. package/dist/codemode/tool.js +6 -5
  21. package/dist/config/json-schema.js +1 -0
  22. package/dist/config/schema-w5.js +2 -0
  23. package/dist/config/types-w5.d.ts +5 -0
  24. package/dist/drivers/jsonrpc.d.ts +2 -2
  25. package/dist/i18n/catalog.d.ts +73 -70
  26. package/dist/i18n/messages/config-keys.d.ts +4 -0
  27. package/dist/i18n/messages/config-keys.js +4 -0
  28. package/dist/i18n/messages/config.d.ts +4 -0
  29. package/dist/i18n/messages/errors.d.ts +3 -3
  30. package/dist/i18n/messages/print.d.ts +2 -0
  31. package/dist/i18n/messages/print.js +4 -0
  32. package/dist/i18n/messages/report.d.ts +2 -2
  33. package/dist/i18n/messages/subcommands.d.ts +85 -85
  34. package/dist/modes/print/print-mode.js +3 -0
  35. package/dist/permissions/pipeline.d.ts +2 -0
  36. package/dist/permissions/pipeline.js +2 -0
  37. package/dist/tools/registry.d.ts +1 -1
  38. package/dist/tools/registry.js +4 -2
  39. package/dist/tools/task-land.d.ts +17 -0
  40. package/dist/tools/task-land.js +64 -0
  41. package/docs/guides/agents.md +17 -6
  42. package/package.json +11 -10
@@ -41,6 +41,7 @@ import { formatUsd } from "../../agent/limits.js";
41
41
  import { registryOf } from "../../agent/subagent-registry.js";
42
42
  import { promptImages, sessionModel } from "../image-input.js";
43
43
  import { msg } from "../../i18n/index.js";
44
+ import { CLASSIFIER_UNAVAILABLE_MESSAGE } from "../../permissions/pipeline.js";
44
45
  import { searchToolsHint } from "./search-hint.js";
45
46
  export function joinPrompt(argument, piped) {
46
47
  const parts = [argument ?? "", piped.replace(/\s+$/, "")].filter((p) => p.trim() !== "");
@@ -335,6 +336,8 @@ export function describeDenied(denied) {
335
336
  for (const item of denied)
336
337
  counts.set(item.toolName, (counts.get(item.toolName) ?? 0) + 1);
337
338
  const tools = [...counts].map(([name, n]) => `${name} ×${n}`);
339
+ if (denied.some((item) => item.reason === CLASSIFIER_UNAVAILABLE_MESSAGE))
340
+ return msg().print.print.deniedClassifier(denied.length, tools);
338
341
  return msg().print.print.denied(denied.length, tools, denied[0]?.reason ?? "");
339
342
  }
340
343
  /** json 结果的 `context`:上下文用量、窗口与占用(0–100);各项未知时省略,全都未知时不带 `context`。 */
@@ -59,6 +59,8 @@ export declare function planDecision(input: Pick<PermissionCheckInput, "toolName
59
59
  message?: string;
60
60
  };
61
61
  export declare const UNATTENDED_MESSAGE = "This tool call requires approval, but no one is available to approve it (unattended mode).";
62
+ /** [#313] auto 模式的分类器请求失败、又没有人审批时的拒绝原因(固定英文,不带上游错误原文)。 */
63
+ export declare const CLASSIFIER_UNAVAILABLE_MESSAGE = "This tool call requires approval: the auto-mode classifier is unavailable and no one is available to approve it (unattended mode).";
62
64
  export declare const ALLOWLIST_MESSAGE = "Not in the allowlist: permission mode \"allowlist\" only runs read-only tools and calls matched by an allow rule, and never asks.";
63
65
  export interface PermissionPipelineOptions {
64
66
  mode: PermissionMode;
@@ -106,6 +106,8 @@ export function planDecision(input, planBash, cwd, projectRoot = cwd) {
106
106
  return { decision: "deny", message: PLAN_MODE_MESSAGE };
107
107
  }
108
108
  export const UNATTENDED_MESSAGE = "This tool call requires approval, but no one is available to approve it (unattended mode).";
109
+ /** [#313] auto 模式的分类器请求失败、又没有人审批时的拒绝原因(固定英文,不带上游错误原文)。 */
110
+ export const CLASSIFIER_UNAVAILABLE_MESSAGE = "This tool call requires approval: the auto-mode classifier is unavailable and no one is available to approve it (unattended mode).";
109
111
  export const ALLOWLIST_MESSAGE = 'Not in the allowlist: permission mode "allowlist" only runs read-only tools and calls matched by an allow rule, and never asks.';
110
112
  /** 审计摘要:bash 取命令,带 path 的取路径,其它取工具名;单行、截断。 */
111
113
  function auditSummary(toolName, input) {
@@ -52,7 +52,7 @@ export interface BuiltinToolOptions {
52
52
  task?: TaskToolOptions;
53
53
  webFetch?: WebFetchOptions;
54
54
  }
55
- /** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / report / web_fetch)。 */
55
+ /** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / task_land / report / web_fetch)。 */
56
56
  export declare function builtinTools(options?: BuiltinToolOptions): ToolDefinition[];
57
57
  /** 新建注册表并登记全部内置工具;`disabled` 来自 config.tools.disabled。 */
58
58
  export declare function createToolRegistry(options?: BuiltinToolOptions & {
@@ -23,6 +23,7 @@ import { createLsTool } from "./ls.js";
23
23
  import { createTodoTool } from "./todo.js";
24
24
  import { createTaskTool } from "./task.js";
25
25
  import { TASK_CTL_TOOL, createTaskCtlTool } from "./task-ctl.js";
26
+ import { TASK_LAND_TOOL, createTaskLandTool } from "./task-land.js";
26
27
  import { REPORT_TOOL, createReportTool } from "./report.js";
27
28
  import { createWebFetchTool } from "./web/fetch.js";
28
29
  /**
@@ -30,7 +31,7 @@ import { createWebFetchTool } from "./web/fetch.js";
30
31
  * 的表里也要有它(运行时拒绝),子会话的工具表才与父逐字节一致。
31
32
  */
32
33
  export const TOOL_COMPANIONS = Object.freeze({
33
- task: [TASK_CTL_TOOL, REPORT_TOOL],
34
+ task: [TASK_CTL_TOOL, TASK_LAND_TOOL, REPORT_TOOL],
34
35
  });
35
36
  /** 按 `TOOL_COMPANIONS` 配对:有主工具就带上(已注册的)伴随工具,没有就去掉。 */
36
37
  export function pairCompanions(names, available) {
@@ -120,7 +121,7 @@ export class ToolRegistry {
120
121
  this.activeNames = new Set(pairCompanions(names, (n) => this.entries.has(n) && !this.disabled.has(n)));
121
122
  }
122
123
  }
123
- /** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / report / web_fetch)。 */
124
+ /** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / task_land / report / web_fetch)。 */
124
125
  export function builtinTools(options = {}) {
125
126
  const tools = [
126
127
  createReadTool(options.read),
@@ -133,6 +134,7 @@ export function builtinTools(options = {}) {
133
134
  createTodoTool(),
134
135
  createTaskTool(options.task),
135
136
  createTaskCtlTool(),
137
+ createTaskLandTool(),
136
138
  createReportTool(),
137
139
  createWebFetchTool(options.webFetch),
138
140
  ];
@@ -0,0 +1,17 @@
1
+ /**
2
+ * [#313] `task_land`:把 worktree 子任务的提交落到当前分支。
3
+ *
4
+ * 会改父仓库,所以是独立的 `execute` 工具、走完整权限管线,不放进权限为 `read` 的 `task_ctl`。与 `task`
5
+ * 同进退;子会话里运行时拒绝;`coordinator` 预设(协调者不写文件)下同样拒绝,工具表不变;不进 codemode
6
+ * 的可调用清单(不增加 codemode 描述的长度,也不让脚本批量落地)。
7
+ */
8
+ import type { LandResult } from "../agent/worktree-land.js";
9
+ import type { ToolDefinition, ToolResult } from "./types.js";
10
+ export declare const TASK_LAND_TOOL = "task_land";
11
+ export interface TaskLandInput {
12
+ taskId: string;
13
+ }
14
+ /** 组装根在 `coordinator` 预设下调用:描述与 schema 不变,执行时拒绝。 */
15
+ export declare function refuseTaskLand(tool: ToolDefinition | undefined): void;
16
+ export declare function landText(taskId: string, result: LandResult): ToolResult;
17
+ export declare function createTaskLandTool(): ToolDefinition<TaskLandInput>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * [#313] `task_land`:把 worktree 子任务的提交落到当前分支。
3
+ *
4
+ * 会改父仓库,所以是独立的 `execute` 工具、走完整权限管线,不放进权限为 `read` 的 `task_ctl`。与 `task`
5
+ * 同进退;子会话里运行时拒绝;`coordinator` 预设(协调者不写文件)下同样拒绝,工具表不变;不进 codemode
6
+ * 的可调用清单(不增加 codemode 描述的长度,也不让脚本批量落地)。
7
+ */
8
+ import { taskControl } from "../agents/task-control.js";
9
+ import { nestedTaskRejection } from "./task.js";
10
+ export const TASK_LAND_TOOL = "task_land";
11
+ const refused = new WeakSet();
12
+ /** 组装根在 `coordinator` 预设下调用:描述与 schema 不变,执行时拒绝。 */
13
+ export function refuseTaskLand(tool) {
14
+ if (tool !== undefined)
15
+ refused.add(tool);
16
+ }
17
+ function fail(message) {
18
+ return { content: message, isError: true };
19
+ }
20
+ export function landText(taskId, result) {
21
+ switch (result.status) {
22
+ case "landed": {
23
+ const head = `Landed task ${taskId}: ${result.commits.length} commit(s) on the current branch (${result.commits.map((c) => c.slice(0, 9)).join(", ")}).`;
24
+ return {
25
+ content: result.cleanupError === undefined
26
+ ? `${head} Worktree removed.`
27
+ : `${head} Worktree cleanup failed: ${result.cleanupError}`,
28
+ };
29
+ }
30
+ case "nothing":
31
+ return { content: `Task ${taskId} has no changes to land. Worktree removed.` };
32
+ case "blocked": {
33
+ const files = result.files === undefined ? "" : `\nConflicts: ${result.files.join(", ")}`;
34
+ return fail(`Task ${taskId} not landed; its worktree is kept. ${result.reason}${files}`);
35
+ }
36
+ }
37
+ }
38
+ export function createTaskLandTool() {
39
+ const tool = {
40
+ name: TASK_LAND_TOOL,
41
+ label: "TaskLand",
42
+ description: "Land a finished worktree task's commits on the current branch; on conflict nothing changes.",
43
+ parameters: {
44
+ type: "object",
45
+ properties: { taskId: { type: "string" } },
46
+ required: ["taskId"],
47
+ additionalProperties: false,
48
+ },
49
+ promptSnippet: "task_land: land a worktree task on the current branch",
50
+ permission: "execute",
51
+ executionMode: "sequential",
52
+ async execute(input, ctx) {
53
+ if (ctx.depth >= 1)
54
+ return fail(nestedTaskRejection(ctx, TASK_LAND_TOOL, "task_land is not available inside a sub-agent."));
55
+ if (refused.has(tool))
56
+ return fail("task_land is not available to a coordinator.");
57
+ const control = taskControl(ctx.sessionId);
58
+ if (control?.land === undefined || control.get(input.taskId) === undefined)
59
+ return fail(`Unknown task ${input.taskId}.`);
60
+ return landText(input.taskId, await control.land(input.taskId));
61
+ },
62
+ };
63
+ return tool;
64
+ }
@@ -1,6 +1,6 @@
1
1
  # Agent:子 Agent 与外部 Agent
2
2
 
3
- 模型只认识两个工具:`task`(委派)与 `task_ctl`(管理后台任务)。`task(agent=…)` 的 `agent` 既可以是 ama 自己的子 Agent
3
+ 模型只认识三个工具:`task`(委派)、`task_ctl`(管理后台任务)与 `task_land`(把 worktree 任务落到当前分支)。`task(agent=…)` 的 `agent` 既可以是 ama 自己的子 Agent
4
4
  类型,也可以是外部 CLI Agent(外部部分见下文「外部 Agent」节)。设计依据:[wave5-plan.md](../history/wave5-plan.md) §7、D13、D22–D24。
5
5
 
6
6
  ## 子 Agent
@@ -200,11 +200,21 @@ Relay this question to the user verbatim; do not answer it yourself.
200
200
  ### worktree 隔离
201
201
 
202
202
  `isolation: "worktree"` 时,ama 在父 cwd 所在仓库里执行
203
- `git worktree add -b ama/task-<名字> <仓库>/.ama/worktrees/<名字>`(名字 = 会话 id 前 8 位 + `taskId`),子会话的 cwd 是
204
- worktree 里与父 cwd 对应的目录。结束时没有改动(工作区干净且没有新提交)就删掉 worktree 与分支;有改动则保留,结果末尾给出
205
- 分支名、路径与 `git diff --stat`(含未跟踪文件),由你决定是否合并。`.ama/worktrees/` 下自动写一个 `.gitignore`(`*`),
206
- 父仓库的 `git status` 与 grep / glob 都看不到这些目录。不在 git 仓库里时直接报错,不会退回共享目录。
207
- 多个隔离任务同时开始或结束时,同一仓库的 worktree 增删与删分支按顺序执行(它们都会改 `.git/worktrees`);另一个 ama 进程同时操作时遇到的瞬时错误会短暂重试。worktree 里的编辑不记进父会话的检查点。注意:worktree 不共享依赖(`node_modules` 等),不保证能直接构建或运行测试。
203
+ `git worktree add -b ama/task-<名字> <仓库>/.ama/worktrees/<名字> <起点>`(名字 = 会话 id 前 8 位 + `taskId`),子会话的 cwd 是
204
+ worktree 里与父 cwd 对应的目录。`.ama/worktrees/` 下自动写一个 `.gitignore`(`*`),父仓库的 `git status` 与 grep / glob 都看不到这些目录。
205
+ 不在 git 仓库里时直接报错,不会退回共享目录。多个隔离任务同时开始或结束时,同一仓库的 worktree 增删与删分支按顺序执行;另一个 ama 进程同时操作时遇到的瞬时错误会短暂重试。worktree 里的编辑不记进父会话的检查点。
206
+
207
+ **起点包含未提交改动。** 父工作区有已暂存、未暂存或未跟踪的改动(不含已忽略文件)时,ama 经临时索引把它们写成一个合成提交,任务分支从这里开始,父的索引与工作区不变。有无改动、diff 摘要和落地都以这个起点为基准,继承来的改动不算任务产出。快照失败时回落到 `HEAD`,结果里会写明。注意:合成提交在任务分支上,含未跟踪文件,不要直接推送 `ama/task-*` 分支。
208
+
209
+ **结束。** 没有改动(工作区干净且没有新提交)就删掉 worktree 与分支;有改动则保留,结果末尾给出分支名、路径与 `git diff --stat`(含未跟踪文件)。
210
+
211
+ **落地:`task_land { taskId }`。** 把任务遗留改动提交到任务分支,再把任务自己的提交 cherry-pick 到父仓库当前分支,成功后删除 worktree 与分支;合成提交不会被落地。它是 `execute` 权限的独立工具,走完整权限管线;子会话、`coordinator` 预设和 codemode 脚本里都不可用。
212
+
213
+ - 冲突、会覆盖父的未提交改动、任务分支含 merge 提交时不落地:父的 HEAD、索引和工作区恢复原样,worktree 保留,结果列出冲突文件。
214
+ - 父仓库正在 merge、rebase、cherry-pick 等操作时直接拒绝,不会中止你自己的操作。
215
+ - 会话 resume 后不再持有 worktree 记录,`task_land` 会拒绝,请用 git 处理保留的分支。
216
+
217
+ **初始化:`subagents.worktreeSetup`。** worktree 不共享依赖(`node_modules` 等)。这个配置给一条命令,在每个新建 worktree 的根目录运行一次,环境变量 `AMA_WORKTREE_PARENT` 指向父仓库,例如 `ln -s "$AMA_WORKTREE_PARENT/node_modules" node_modules`。命令失败、超时(10 分钟)或被中止时删除该 worktree 并让任务失败。只认用户级配置,项目级配置不能借此执行命令。
208
218
 
209
219
  ### fork 模式
210
220
 
@@ -263,6 +273,7 @@ RPC `get_tasks` / `get_agents` 返回任务快照与可用类型(来源、定
263
273
  | `subagents.retainSessions` | 保留在内存里的已结束子会话数(LRU),缺省 4,0 = 结束即释放;只认用户级 |
264
274
  | `subagents.defaultModel` | 子 Agent 缺省模型,不设继承父会话 |
265
275
  | `subagents.forkMaxContextRatio` | fork 回落比例(见「fork 模式」),0.05–0.95,缺省 0.5;只认用户级 |
276
+ | `subagents.worktreeSetup` | 新建任务 worktree 后运行一次的命令(见「worktree 隔离」);只认用户级 |
266
277
  | `subagents.background` | `auto`(缺省)\| `always` \| `never`,见「前台与后台」;用户 / 项目 / 宿主级都认 |
267
278
  | `subagents.autoBackgroundAfterMs` | 前台任务运行超过该毫秒数自动转后台,缺省 0(关闭);用户 / 项目级都认 |
268
279
  | `subagents.backgroundMaxMs` | 后台任务的寿命上限(毫秒),缺省 3 600 000(1 小时),0 不限;只认用户级(见「长命令与后台寿命」) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armadra/agent",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "A coding and coordination agent that runs standalone or embedded in Armadra",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -82,15 +82,15 @@
82
82
  "provenance": true
83
83
  },
84
84
  "scripts": {
85
- "build": "pnpm build:lib && pnpm build:bundle",
85
+ "build": "pnpm run \"/^build:/\"",
86
86
  "build:lib": "tsc -p tsconfig.build.json",
87
87
  "build:bundle": "node scripts/build-bundle.mjs",
88
88
  "typecheck": "tsc --noEmit",
89
89
  "test": "vitest run",
90
90
  "test:watch": "vitest",
91
91
  "test:e2e": "AMA_E2E=1 vitest run test/e2e",
92
- "fmt": "prettier --write .",
93
- "fmt:check": "prettier --check .",
92
+ "fmt": "prettier --write --cache --cache-location node_modules/.cache/prettier/.prettier-cache .",
93
+ "fmt:check": "prettier --check --cache --cache-location node_modules/.cache/prettier/.prettier-cache .",
94
94
  "check:deps": "node scripts/check-no-deps.mjs",
95
95
  "check:i18n": "node scripts/check-i18n.mjs",
96
96
  "check:docs": "node scripts/check-doc-links.mjs",
@@ -99,15 +99,16 @@
99
99
  "prepare": "node scripts/hooks/install.mjs",
100
100
  "bench:presets": "node scripts/bench-presets.mjs",
101
101
  "bench:cache": "node scripts/cache-experiment.mjs",
102
- "ci": "pnpm typecheck && pnpm fmt:check && pnpm check:deps && pnpm check:i18n && pnpm check:docs && pnpm check:portability && pnpm release:check && pnpm test && pnpm build && node dist/bundle/ama.cjs --version"
102
+ "ci": "node scripts/ci-run.mjs"
103
103
  },
104
104
  "dependencies": {},
105
105
  "devDependencies": {
106
- "@types/node": "^22.20.4",
106
+ "@types/node": "^22.20.5",
107
+ "@typescript/typescript6": "6.0.2",
107
108
  "esbuild": "^0.28.2",
108
- "prettier": "^3.9.10",
109
- "typescript": "~5.9.3",
110
- "vitest": "^4.1.12"
109
+ "prettier": "^3.10.1",
110
+ "typescript": "^7.0.2",
111
+ "vitest": "^5.1.1"
111
112
  },
112
- "packageManager": "pnpm@12.18.0"
113
+ "packageManager": "pnpm@12.21.0"
113
114
  }