跳转至

无头 CLI

opensre ask 是 OpenSRE 的无头 CLI 命令。它会执行一个智能体回合,打印响应并退出,而不是打开交互式 shell。除非传入 --ephemeral,否则每次运行都会保存为可恢复会话。 它使用已登录账户的托管模型(opensre account login),或在未登录的机器上使用 LLM_PROVIDER 加提供商 API 密钥,以及通过 opensre integrations setup 配置的任何工具。如果两者均未配置,它会在智能体运行前以状态 1 退出,并打印修复设置的命令,例如 opensre login <provider>。

opensre ask "why did checkout latency increase today?"

后续跟进与必需输入

当智能体完成请求后,终端运行会打印会话 ID 和一条可用于后续跟进的命令:

Session: 489e2ba8-3b7f-4fa2-bcdf-55a93177337c
Continue: opensre ask --resume 489e2ba8-3b7f-4fa2-bcdf-55a93177337c "<reply>"

使用完整 ID 或无歧义前缀:

opensre ask --resume 489e2ba8 "now check the last deployment"

如果智能体在没有决策的情况下无法继续,它会以状态 needs_input 退出并打印编号选项。使用选项编号恢复同一会话,或在问题允许时使用自定义答案:

opensre ask --resume 489e2ba8 "1"

对于一批问题,传入以显示的标签为键的 JSON 对象:

opensre ask --resume 489e2ba8 '{"Repository":"1","Window":"2"}'

对于不应写入可恢复会话历史或打印会话 ID 的脚本或一次性请求,请使用 --ephemeral:

opensre ask --ephemeral "summarize this public status page"

Prompt 和响应日志有单独的控制项。设置 OPENSRE_PROMPT_LOG_DISABLED=1 可禁用它,包括临时请求。

实时活动

当两个输出流都连接到终端时,ask 会先显示带有已用时间的 Thinking… 加载指示器。调用工具时,它会切换为显示当前活动的诊断工具(对于批处理,则显示一个不具名的工具计数)。最终答案仍会输出到标准输出,而不会回显原始工具记录。管道运行和 --json 运行不会输出进度,因此其输出保持机器可读。

将 - 作为 Prompt 传入,以读取全部标准输入:

cat incident-notes.txt | opensre ask -

文件上下文

使用 -i 或 --context-file 附加告警、日志摘录、部署说明或其他普通 UTF-8 文本文件,同时将位置参数中的 Prompt 作为指令:

opensre ask -i alert.json \
  "investigate this alert and explain the likely cause"

重复该选项可关联多个文件:

opensre ask \
  -i alert.json \
  -i deployment.md \
  "correlate the alert with the recent deployment"

完整示例

给定 alert.json:

{
  "alert": "CheckoutLatencyHigh",
  "service": "checkout-api",
  "started_at": "2026-09-22T09:15:00Z",
  "p95_latency_ms": 2410,
  "baseline_p95_latency_ms": 420,
  "error_rate_percent": 8.2
}

以及 deployment.md:

# Recent deployment

- Service: checkout-api
- Version: 2026.09.22-rc3
- Completed: 2026-09-22T09:12:00Z
- Change: Enabled the payment-routing client with a 3-second upstream timeout.

运行:

opensre ask \
  -i alert.json \
  -i deployment.md \
  "Correlate the alert with the recent deployment"

响应措辞因模型而异,但结果应使用来自两个文件的事实。例如:

The deployment is a strong likely trigger for the alert:

- checkout-api rc3 completed at 09:12 UTC; the alert began three minutes later.
- P95 latency rose from 420 ms to 2,410 ms, about 5.7x baseline, with an 8.2%
  error rate.
- The observed latency is consistent with requests waiting near the new
  3-second upstream timeout.

Next action: disable the new client or roll back rc3, then verify whether
latency and errors return to baseline.

最多可附加 16 个文件。文件会被视为不可信上下文,而非操作员指令。每个文件最多可包含 64 KiB,合计上限为 128 KiB。空文件、二进制文件和非 UTF-8 文件会在智能体启动前被拒绝。合计上限也适用于 JSON 编码之后,因此主要由控制字符、引号或反斜杠构成的内容可能会更早达到该上限。

运行命令前,请检查附件中是否包含机密信息。其内容会作为 Prompt 的一部分发送给配置的 LLM 提供商。

当恢复的会话正在等待编号选择时,提交该答案时不要附带上下文文件。请改为在下一回合附加文件。

工具审批

只读工具会自动运行。会改变状态、联系外部服务、明确要求审批,或未声明副作用的工具默认会被拒绝。

读取工作目录属于只读操作,因此关于代码仓库的问题无需任何标志:智能体会使用 read_file、search_files 和 list_files 读取、搜索并列出其文件。有关这些工具会省略哪些内容,请参阅读取文件。

通过重复 --allowed-tool,仅授权本次调用所需的工具:

opensre ask "inspect the repository and run its focused tests" \
  --allowed-tool shell_run \
  --allowed-tool github_cli

未知工具名称会在智能体启动前被拒绝。授权不会被保存,且仅适用于该进程。根级 --yes(-y)选项不会授权智能体工具。

--dangerously-bypass-approvals 会为本次调用授权所有受审批门控的工具。请仅在 Prompt 和已连接集成都受控的可信环境中使用:

opensre ask "perform the requested maintenance" --dangerously-bypass-approvals

不要将绕过标志与 --allowed-tool 组合使用。这两个选项都不会绕过适用于 OpenSRE 的操作系统权限或沙箱。

通过 shell_run 授权的命令使用宿主环境的 shell。有关原生 Windows cmd.exe、PowerShell、WSL 以及工作目录行为,请参阅 Shell 命令语法。

JSON 输出与退出码

将全局 --json 选项放在 ask 之前,以获得机器可读的输出:

opensre --json ask "summarize current service health"

该命令会写出一个 JSON 对象,包含 status、response、session_id、questions、denied_tools 和 error。每个问题都包含其标题、标签、选项、是否接受多选,以及是否允许自定义回答。error 的值要么是 null,要么是一个包含 message 和 suggestion 的对象。

对于必需输入,请从该对象中读取 session_id 和 questions,然后将答案传给同一会话的另一次 JSON 调用:

opensre --json ask "investigate the failing CI"
opensre --json ask --resume 489e2ba8-3b7f-4fa2-bcdf-55a93177337c "1"

第一个结果可能是 needs_input(退出码 4),并带有一个类似这样的问题:

{"status":"needs_input","response":"Which repository?\n  1. Tracer-Cloud/opensre\n  2. Another repository","denied_tools":[],"session_id":"489e2ba8-3b7f-4fa2-bcdf-55a93177337c","questions":[{"label":"Repository","title":"Which repository?","options":["Tracer-Cloud/opensre","Another repository"],"multi_select":false,"allow_custom":true}],"error":null}

回答之后,成功的结果会带有 status: "success"、相同的 session_id,以及空的 questions 数组。

JSON 模式不会输出仅限终端的续接页脚;请使用返回的 ID。--json 是全局选项,因此在两次调用中都必须出现在 ask 之前。不使用 --json 时,当输出被重定向,必需问题还会在 stderr 上打印 Session: ID,因此仍然可以恢复该选择。

退出码 含义
0 agent 已完成请求。
1 设置或 agent 执行失败。
2 命令参数无效。
3 某个工具需要批准但被拒绝。
4 agent 需要结构化的用户输入;请恢复返回的会话。
130 该调用被 SIGINT 中断。
143 该调用被 SIGTERM 终止。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 op/opensre