Skip to content

Delegate 让 Kiro CLI 可以把耗时、独立的工作交给后台任务或专用 agent 执行。本文用中文讲清它的启用方式、任务创建流程、agent approval、安全边界、适用与不适用场景,帮助你一边保持主对话推进,一边并行处理测试、分析、构建等任务。

Kiro CLI Experimental Delegate:把耗时任务交给后台 agent

Delegate 的核心价值是“让主对话不要被长任务堵住”。当你需要跑测试、做代码分析、生成文档、监控日志,或者让某个特定 agent 处理独立任务时,可以把工作 delegate 给后台任务。Kiro 会在主会话之外启动异步执行流程,你仍然可以继续和 Kiro 讨论当前问题。

如果任务需要 agent,Kiro 会展示 agent 的配置、工具和权限,并要求你确认后再执行。这一点很重要:delegate 能提升并行效率,但也意味着后台任务可能访问文件、运行命令或读取资源,必须先看清楚再批准。

启用 delegate

使用 settings 启用:

bash
kiro-cli settings chat.enableDelegate true

也可以通过实验功能菜单启用:

bash
/experiment

然后从列表中选择 Delegate。

Delegate 是怎么工作的?

创建任务

你可以直接用自然语言要求 Kiro 创建后台任务:

text
> Can you create a background task to analyze the performance of our API endpoints?

Kiro 会依次判断:

  1. 这个任务具体要做什么。
  2. 是否需要某个 agent。
  3. 如果需要 agent,先展示审批信息。
  4. 批准后在后台启动任务。
  5. 把控制权还给主对话。

Agent approval flow

如果任务会使用 agent,Kiro 会要求显式批准,并展示:

  • Agent 名称和用途。
  • 可使用的 tools。
  • 可访问的 resources。
  • 相关配置和权限范围。

如果任务不使用 agent,也可能带有 trust-all 权限警告。这类任务可能拥有更宽的工具访问能力,更要谨慎确认。

管理任务

任务创建后会独立运行。你可以在主对话中随时:

  • 查询任务进度。
  • 读取完成结果。
  • 删除不再需要的任务。

使用示例

示例 1:后台做代码性能分析

text
> Can you create a background task to analyze the performance of our API endpoints?

I'll create a background task to analyze your API performance.

Agent: performance-analyzer
Tools: read, shell, aws
Resources: api-docs, performance-metrics

Approve this agent? (y/n): y

✓ Task created: api-performance-analysis
  Task ID: task-abc123
  Status: Running

You can continue working while the analysis runs in the background.

> Great! Now help me with the frontend code...

# 继续主对话

> Check the status of my API analysis task

Task: api-performance-analysis (task-abc123)
Status: Complete
Duration: 2m 34s

Results:
- Analyzed 15 API endpoints
- Found 3 performance bottlenecks
- Generated optimization recommendations

这类场景适合 delegate,因为分析任务耗时较长,而且和当前前端讨论相对独立。

示例 2:后台跑测试套件

text
> Run our test suite in the background while I work on the new feature

I'll start the test suite in a background task.

✓ Task created: test-suite-run
  Task ID: task-def456
  Status: Running

> How's the test suite doing?

Task: test-suite-run (task-def456)
Status: Running (45% complete)
Progress: 127/283 tests passed

当测试耗时几分钟甚至更久时,delegate 可以减少等待时间。

示例 3:后台生成 API 文档

text
> Generate API documentation in the background

✓ Task created: api-docs-generation
  Task ID: task-ghi789
  Status: Running

> Show me the results from the documentation task

Task: api-docs-generation (task-ghi789)
Status: Complete

Generated documentation for:
- 23 API endpoints
- 15 data models
- 8 authentication methods

Documentation saved to: docs/api/

文档生成、报告生成、扫描类任务通常都适合后台处理。

任务生命周期

1. Creation

用自然语言描述任务:

text
> Create a background task to [description]

Kiro 会分析任务需要什么 agent、tools 和 resources。

2. Approval

如果任务需要 agent,你会看到类似信息:

text
Agent: task-agent-name
Description: What this agent does
Tools: List of tools it can use
Resources: Files it can access

Approve this agent? (y/n):

不要机械输入 y。先确认工具和资源是否符合最小权限原则。

3. Execution

任务在后台运行,主对话继续推进。你可以继续写代码、讨论方案或处理其他问题。

4. Monitoring

随时查询状态:

text
> Check on [task name]
> What's the status of my background task?
> Show me task progress

5. Completion

任务完成后读取结果:

text
> Show me the results from [task name]
> What did the background task find?

6. Cleanup

任务不再需要后可以删除:

text
> Delete the [task name] task
> Clean up completed tasks

涉及敏感输出的任务尤其应该及时清理。

什么时候适合用 delegate?

适合:

  • 长时间运行的操作:测试套件、构建、代码分析。
  • 独立任务:不需要你中途持续输入。
  • 并行工作:多个任务之间依赖关系弱。
  • 后台监控:日志观察、文件变化监测、系统指标检查。

不适合:

  • 需要频繁人工决策的交互式任务。
  • 几秒钟就能完成的小命令。
  • 强依赖主对话最新结果的任务。
  • 直接执行更简单的单步命令。

判断标准很简单:如果你需要“等它跑完才能继续”,并且它不需要你中途参与,就可以考虑 delegate。

安全注意事项

仔细审查 agent approval

批准前至少看三点:

  • Tools:它能做什么?是否能运行 shell、读写文件或调用外部服务?
  • Resources:它能访问哪些文件、目录或文档?
  • Permissions:权限是否超过当前任务所需?

小心 trust-all warning

没有专用 agent 的任务可能以更宽权限运行:

  • 可能访问所有 tools。
  • 可能读取更广的文件范围。
  • 可能执行你没有细看过的操作。

如果任务涉及密钥、客户数据、生产配置,建议使用权限更窄的 custom agent。

最小权限原则

  1. 为高频后台任务创建专用 agent。
  2. 只给 agent 必要工具。
  3. 只允许访问必要资源。
  4. 定期查看任务状态和输出。
  5. 包含敏感数据的任务完成后及时删除。

限制

并发限制

  • 同时运行的任务数量有限。
  • 多个任务会共享系统资源。
  • 任务太多可能拖慢主会话或本机性能。

隔离限制

  • 后台任务独立运行。
  • 它不会自动和主对话直接同步。
  • 你需要显式查询结果并把结论带回主线。

持久化限制

  • 任务通常是会话级的。
  • 会话重启后可能无法继续。
  • 长期有价值的结果应保存到文件或文档中。

常见排障

任务没有启动怎么办?

先确认 delegate 已启用:

bash
kiro-cli settings chat.enableDelegate

然后查看聊天输出中的错误信息。建议先用一个简单任务测试 delegate 是否可用,再尝试复杂任务。

查不到任务状态怎么办?

确认你使用的是正确的 task name 或 task ID。如果任务已经完成并被清理,可能无法继续查询。你也可以让 Kiro 列出当前活跃任务。

Agent approval 出问题怎么办?

检查 agent 是否存在、配置是否正确、tools 和 resources 是否可用。如果不确定权限范围,先不要批准,改用更具体的 agent 或更小的任务描述。

进阶用法:为后台任务创建 custom agent

对于经常执行的后台任务,可以创建专用 agent,例如 test runner:

json
{
  "name": "test-runner",
  "description": "Runs test suites in background",
  "tools": ["shell", "read"],
  "allowedTools": ["shell"],
  "toolsSettings": {
    "shell": {
      "allowedCommands": ["npm test", "pytest", "cargo test"]
    }
  }
}

这样做的好处是权限更清晰,也更容易复用。

常见任务模式

Analysis tasks

text
> Analyze code quality in the background
> Run security scan as a background task
> Generate performance report in background

Build tasks

text
> Build the project in the background
> Compile and run tests as a background task
> Generate production bundle in background

Monitoring tasks

text
> Monitor log files in the background
> Watch for file changes as a background task
> Track system metrics in background

相关功能

常见问题

Delegate 和普通命令执行有什么区别?

普通命令通常会占用当前对话流程,delegate 则把任务放到后台运行。你可以继续主对话,并在需要时查询进度或结果。

所有任务都应该 delegate 吗?

不是。几秒钟能完成、需要你频繁输入、或者强依赖当前对话上下文的任务,直接在主对话中做更简单。

为什么 agent 任务需要审批?

因为 agent 可能拥有工具权限和资源访问能力。审批流程能让你在执行前确认它能做什么、能看什么,避免后台任务越权操作。