Files
pikasTech-agentrun/docs/reference/spec-v01-cli.md
T
2026-06-02 00:07:01 +08:00

12 KiB
Raw Blame History

v0.1 AgentRun CLI 规格

AgentRun CLI 是 v0.1 的受控人工操作和验收入口。它必须遵循 UniDesk cli-spec 原则:默认 JSON 输出、禁止空输出伪成功、禁止长阻塞、日志可见、配置显式校验,并优先通过 agentrun-mgr RESTful API 完成跨服务操作。

在系统中的职责划分

  • 创建 run、查询 run、提交 command、查询 command、分页读取 events。
  • 创建和查询 Queue task,查看 Queue stats/read/commander,并从 Queue task 获取 Session 引用。
  • 查询 Session 输出、trace 和会话控制;Queue 命令不得代理输出或 trace。
  • 手动启动 runner,本地 process 或 Kubernetes Job 均必须快速返回 job/process identity 和 logPath。
  • 查询 backend capability 和 manager health。
  • 为综合联调提供 CLI 交互面;不得替代 RESTful API 合同测试。
  • 不直连 Postgres,不读取 provider Secret 值,不把 debug/mock 路径伪装成正式验收。

CLI 入口

v0.1 固定规划入口:

scripts/agentrun-cli.ts
scripts/src/*.ts

CLI 官方 TypeScript 入口固定为 scripts/agentrun-cli.ts。在 G14 非交互 SSH route、CI task 和人工验收命令中,推荐使用仓库内 launcher:

./scripts/agentrun ...

该 launcher 只负责定位 bun(优先 BUN_BIN、其次 PATH、$HOME/.bun/bin/bun 和 G14 默认 /root/.bun/bin/bun),然后转入同一个 scripts/agentrun-cli.ts。它不实现第二套路由、不读取额外配置、不绕过 TypeScript CLI。bun scripts/agentrun-cli.ts ... 仍可在 PATH 已包含 Bun 的交互 shell 中使用;nodetsx 或其他 wrapper 只能作为本地开发辅助,不能成为 v0.1 综合联调的权威入口。

入口文件只负责参数解析和路由,具体实现拆到 scripts/src/。CLI 命令默认输出 JSON;任何命令无 stdout 都是失败。单次 CLI 调用必须在 60 秒内返回,长时间模型 turn 通过 status/events 后续命令观察。

初始命令族

./scripts/agentrun runs create --json-file <run.json>
./scripts/agentrun runs show <runId>
./scripts/agentrun runs events <runId> --after-seq <n> --limit <n>
./scripts/agentrun runs result <runId> [--command-id <commandId>]
./scripts/agentrun runs cancel <runId> [--reason <text>]
./scripts/agentrun commands create <runId> --type turn --json-file <payload.json>
./scripts/agentrun commands show <commandId> --run-id <runId>
./scripts/agentrun commands result <commandId> --run-id <runId>
./scripts/agentrun commands cancel <commandId> [--reason <text>]
./scripts/agentrun runner start --run-id <runId> --backend <backendProfile>
./scripts/agentrun runner job --run-id <runId> --command-id <commandId> [--idempotency-key <key>]
./scripts/agentrun runner job --dry-run --run-id <runId> --command-id <commandId> --image <image>
./scripts/agentrun runner jobs --run-id <runId> [--command-id <commandId>]
./scripts/agentrun runner job-status [runnerJobId] --run-id <runId>
./scripts/agentrun secrets codex render --dry-run [--profile codex|deepseek] [--codex-home <dir>]
./scripts/agentrun backends list
./scripts/agentrun server start [--port <port>] [--host <host>] [--foreground]
./scripts/agentrun server status [--port <port>]
./scripts/agentrun server logs [--port <port>] [--tail-bytes <bytes>] [--log-file <path>]
./scripts/agentrun server stop [--port <port>]
./scripts/agentrun queue submit --json-file <task.json>
./scripts/agentrun queue list [--queue <queue>] [--state <state>] [--cursor <cursor>] [--limit <limit>]
./scripts/agentrun queue show <taskId>
./scripts/agentrun queue stats [--queue <queue>]
./scripts/agentrun queue commander [--queue <queue>]
./scripts/agentrun queue read <taskId>
./scripts/agentrun queue cancel <taskId> [--reason <text>]
./scripts/agentrun queue dispatch <taskId> [--json-file <dispatch.json>]
./scripts/agentrun queue refresh <taskId>
./scripts/agentrun sessions show <sessionId|sessionPath>
./scripts/agentrun sessions output <sessionId|sessionPath> [--cursor <cursor>] [--limit <limit>]
./scripts/agentrun sessions trace <sessionId|sessionPath> [--cursor <cursor>] [--limit <limit>]
./scripts/agentrun sessions control <sessionId|sessionPath> --json-file <control.json>

具体参数可以在实现时按代码结构微调,但行为必须保持:

  • 创建类命令返回 runIdcommandId、status 和下一步 poll command。
  • runner start 返回 attemptId、job/process identity、logPath 和后续 status/events 命令。
  • runner jobs / runner job-status 返回 manager 持久化的 runner Job 最小状态摘要,包括 attemptId、runnerId、namespace、jobName、phase、terminalStatus、logPath、retention 和 redacted Kubernetes identity;业务方不需要直连 Kubernetes 才能定位当前 attempt。
  • 查询类命令返回当前 state、terminal_status、failureKind、event cursor 或 logPath。
  • events 默认分页且有界,必须支持 afterSeqlimit
  • server start 默认以本地后台进程启动 manager,立刻返回 pid、pidFile、logPath、baseUrl 和后续 status/stop 命令;只有显式 --foreground 才允许占用当前终端。启动前必须检查 pidFile 与端口占用,避免同一端口上堆叠临时 manager。
  • server status 必须同时返回本地 pid/port/logPath 状态和 /health/readiness 结果;即使 readiness 失败,也要输出结构化 JSON 和 failure details。
  • server logs 必须返回有界日志尾部、bytes、truncated 和 logPath;找不到日志文件时也必须返回非空 JSON。
  • server stop 必须按 pidFile 与端口进程清理本地 manager,并返回 before/after 状态;不得要求人工用 ps/kill/ss 组合命令清理常见临时服务。
  • secrets codex render --dry-run 返回 Codex stdio profile Secret 创建计划、输入文件 bytes/hash、SecretRef、manifest 摘要和 apply 命令形状;--profile codex 默认 Secret name 为 agentrun-v01-provider-codex--profile deepseek 默认 Secret name 为 agentrun-v01-provider-deepseek;它不得输出 Secret value 或执行 Kubernetes 写操作。
  • backends list 必须显示 codexdeepseek profile 的 backendKind、protocol、transport、command、requiredSecretKeys 和状态;不得因为 deepseek 尚未配置 Secret 就隐藏 capability。
  • queue dispatch 是 Q2 的受控手动调度入口,只对单个 task 显式创建 attempt 和 Core run/command/runner job;不得伪装成自动 scheduler。
  • queue refresh 只根据 Queue task 中保存的 Core run/command 引用回写 Queue attempt 状态,不读取 Core trace 反推 commander 或统计。
  • queue show 必须返回 task/attempt summary、state、read cursor、stats 相关字段和 sessionPath;不得返回或代理完整 output/trace。
  • sessions outputsessions trace 是输出和 trace 的唯一 CLI 查询入口;不得新增 queue outputqueue trace 兼容命令。

配置与 Secret 边界

  • CLI 配置必须显式校验;部署关键值不得静默 fallback。
  • CLI 调用 manager REST API;不得直接连 Postgres 或读 Kubernetes Secret value。
  • CLI 可以显示 SecretRef 名称、key、credential source、tool credential scope 和 readiness,但不得显示 Secret value、Codex auth.json、Codex config.toml、DSN password、token、SSH private key 或 URL credential。
  • CLI 不得把 GitHub token、provider key 或长期 SSH key 通过 transientEnv 传入 runner;涉及 agent shell/tool 授权时,必须先按 spec-v01-runtime-assembly.mdtoolCredentials 装配路径实现,再提供 CLI 参数。
  • Debug 子命令可以用于开发,但综合联调和测试规格不得用 debug 子命令作为通过证据。

测试规格

T1 CLI run 生命周期

阅读 AGENTS.md、本文和 spec-v01-validation.md,然后用 ./scripts/agentrun 创建 run、提交 turn command、启动 runner、轮询 command 和 events 直到 terminal_status。确认每个命令输出非空 JSON,60 秒内返回,并给出下一步可执行命令或 logPath。若直接执行 bun scripts/agentrun-cli.ts,必须先证明当前 shell 的 PATH 能找到 BunG14 route 默认以 ./scripts/agentrun 为准。

T2 CLI 错误可见性

阅读本文,然后用非法 backendProfile、缺失 SecretRef 和无效 runId 分别调用正式 CLI。确认每次失败都有 JSON 输出、failureKind、message、trace correlation 和 logPath 或诊断入口;不得空输出或只输出布尔值。

T3 CLI 日志可见性

阅读本文,然后启动一个会失败的 runner,并通过 CLI 返回的 logPath 或 pod identity 读取实际日志。确认日志包含足够定位失败原因的信息、异常 trace 或错误分类,同时没有 Secret value。

T4 CLI 与 RESTful 一致性

阅读本文和 spec-v01-agentrun-mgr.md,然后对同一个 run 分别使用 CLI 和 RESTful API 查询 run、command、events 和 terminal_status。确认 CLI 不维护独立状态,两种交互面观察结果一致。

T5 Backend profile CLI 切换

阅读本文、spec-v01-backend-codex.mdspec-v01-validation.md,然后用正式 CLI 分别创建 backendProfile=codexbackendProfile=deepseek 的 run,按 codex -> deepseek -> codex 顺序执行真实 runner。确认 CLI 输出非空 JSONbackend_status 显示正确 profile/backendKind/protocol,缺失 deepseek SecretRef 时返回 secret-unavailable,不会 fallback 到 codex

T6 Queue 与 Session CLI 分层

阅读本文和 spec-v01-queue.md,然后用正式 CLI 创建一个 Queue task,轮询 queue list/show/stats/commander/read,再使用 queue show 返回的 sessionPath 调用 sessions output/trace。确认 Queue 命令只返回 summary/stats/read/sessionPath,不代理 output/traceSession 命令能查询输出和 trace。最终交互验收必须使用真实 runtime,不使用 mock,不写自动交互脚本。

规格的实现情况

规格项 状态 说明
AgentRun CLI 规格 已定义 本文为 v0.1 CLI 权威。
scripts/agentrun-cli.ts 已实现 已提供 run/command/event/backend/server 基础命令和 JSON envelopescripts/agentrun 是同一入口的 Bun 定位 launcher。
CLI 调 manager REST 已实现 CLI 通过 ManagerClient 调 manager REST;自测试可用内存 manager,综合联调必须指向真实 agentrun-v01 manager。
runner start/job 已实现 runner start 可执行 host process runnerrunner job --dry-run 可渲染 Kubernetes Job JSONrunner job 正式路径通过 manager REST 创建 Kubernetes Job,支持 --idempotency-key 并快速返回 job identity、SecretRef、retention 和轮询命令。
runner jobs/job-status 已实现 CLI 通过 manager REST 查询 runner Job 持久记录和最小状态摘要,不直连 Kubernetes、不读取 Secret 值。
result/cancel CLI 已实现 runs resultcommands resultruns cancelcommands cancel 均调用 manager REST,不维护独立状态。
Queue CLI 已实现/Q1 已提供 queue submit/list/show/stats/commander/read/cancel,通过 manager REST 访问 Queue task 和 stats,不直连 Postgres。
Queue dispatch/refresh CLI 已实现/Q2 queue dispatch 受控创建 Core run/command/runner jobqueue refresh 从 Core run/command 终态回写 Queue task/latestAttempt。
本地 server 生命周期 CLI 已实现/Q2 hardening server start 默认后台短返回,server status/stop 提供 pid、port、logPath 和 readiness 可见性;--foreground 保留给容器/显式调试。
Session CLI 待实现 规格见 spec-v01-queue.md;输出和 trace 进入 Session 命令,Queue 命令不得代理 output/trace。
CLI 测试规格 已定义/已验证主闭环 综合联调见 spec-v01-validation.md;每次发布仍按手动交互验收复跑。
deepseek profile CLI 已实现/已通过主闭环 secrets codex render --profile deepseekbackends listrunner start --backendrunner job 和 JSON 错误可见性已实现;真实 CLI/RESTful 联调已通过 codex -> deepseek -> codex 切换主闭环。