feat: dual execution model (SSH CLI + HTTP pull)

- ExecutionMode enum: SshCli (orchestrator dispatches) | HttpPull (agent pulls)
- SSH CLI executor: spawn remote agents via ssh + CLI template
- Local subprocess as SSH special case (localhost)
- HostConfig with capability matching and load-based selection
- Dispatch loop: scan created tasks → select host → execute → update
- CliAdapterConfig: CLI templates for Codex and Claude Code
- Structured prompt construction (Issue → goal/constraints/validation)
- Output parsers: Codex JSON, Claude Code JSON, raw fallback
- TaskStatus::ReviewPending + review_count loop limit
- Forgejo webhook: pull_request (opened→review_pending, merged→completed)
- Forgejo webhook: push events (task/* branch → last_activity_at)
- HTTP API: dequeue only returns http_pull tasks
- HTTP API: status update only for http_pull mode
- Token auth config for http_pull agents
- Adapter module rewritten: AgentAdapter trait removed → config-driven CLI templates
- New fields: execution_mode, assigned_host, branch_name, pr_title, last_activity_at, review_count
- 30/30 tests pass
This commit is contained in:
Zer4tul 2026-05-12 14:07:56 +08:00
parent 1bc7580ecc
commit e39a16498c
34 changed files with 2541 additions and 1555 deletions

View file

@ -0,0 +1,36 @@
## MODIFIED Requirements
### Requirement: Unified adapter interface
系统 SHALL 定义统一的 Agent 客户端协议(非 trait描述远程 Agent 如何通过 HTTP API 与 Orchestrator 交互。Agent 可以运行在任何机器上,只要能访问 Orchestrator 的 HTTP 端点。
协议 SHALL 包含:
- `POST /api/v1/agents/register` — 注册到 Registry
- `POST /api/v1/agents/heartbeat` — 发送心跳
- `POST /api/v1/tasks/dequeue` — 主动拉取任务(替代被动的 execute
- `POST /api/v1/tasks/{task_id}/status` — 更新任务状态
- `GET /api/v1/tasks/{task_id}` — 查询任务详情
- `POST /api/v1/receipts` — 提交 receipt
- `POST /api/v1/agents/deregister` — 注销
#### Scenario: Remote Agent on different machine
- **WHEN** Agent `worker-03` 运行在 host-worker-02与 Orchestrator 不同机器)
- **THEN** Agent SHALL 通过 HTTP 调用 Orchestrator API 完成注册、领取任务、提交 receipt
- **AND** 无需 SSH、无需共享文件系统、无需同一 OpenClaw 实例
#### Scenario: OpenClaw-managed Agent
- **WHEN** Agent 由 OpenClaw 管理(如 Jeeves 调度 Codex
- **THEN** OpenClaw Agent SHALL 作为 Orchestrator 的客户端,通过 HTTP API 调用
- **AND** 任务的实际执行由 OpenClaw 内部的 ACP 机制完成
### Requirement: Adapter configuration
每个 Agent 实例 SHALL 通过配置文件指定 Orchestrator 连接信息、自身身份、工作参数。
#### Scenario: Remote Agent configuration
- **WHEN** Agent 在远程机器上配置
- **THEN** 配置 SHALL 包含:`{orchestrator_url: "http://arm0:9090", agent_id: "worker-03", token: "xxx", capabilities: ["code:rust"], work_dir: "/path/to/repo"}`
## REMOVED Requirements
### Requirement: Adapter health check
**Reason**: Orchestrator 不再主动连接 Agent。健康检查通过心跳机制实现——Agent 主动发心跳Orchestrator 检测超时。
**Migration**: 已有心跳机制(`POST /api/v1/agents/heartbeat` + TimeoutChecker覆盖此需求。

View file

@ -0,0 +1,14 @@
## MODIFIED Requirements
### Requirement: Agent self-registration
每台机器上的 Agent 启动时 SHALL 向 Orchestrator Registry 注册自身信息。注册成功后 SHALL 返回 registry token后续 API 调用需携带此 token。
#### Scenario: New agent starts and registers
- **WHEN** 一个远程 Agent 在 host-worker-02 上启动
- **THEN** 它 SHALL 调用 `POST /api/v1/agents/register`
- **AND** Orchestrator 记录该 Agent 信息并返回 `{agent_id, registry_token}`
- **AND** 后续所有 Agent API 调用 SHALL 在 header 中携带 `Authorization: Bearer {registry_token}`
#### Scenario: Duplicate registration with same agent_id
- **WHEN** 已注册的 Agent 重启后再次注册(相同 agent_id
- **THEN** 系统 SHALL 更新该 Agent 的信息并返回新的 registry token

View file

@ -0,0 +1,44 @@
## ADDED Requirements
### Requirement: Git branch as task execution unit
每个任务 SHALL 关联一个 Git 分支。Agent 在该分支上工作,通过 PR 提交结果。分支命名约定:`task/{task_id}`(例如 `task/org%2Frepo%2342`)。
#### Scenario: Agent creates branch for task
- **WHEN** Agent 领取任务 org/repo#42
- **THEN** Agent SHALL 在目标仓库创建分支 `task/org%2Frepo%2342`(基于 master/main
#### Scenario: Agent pushes commits to task branch
- **WHEN** Agent 执行过程中产生代码变更
- **THEN** Agent SHALL 推送 commit 到对应的 task 分支
### Requirement: PR webhook as completion notification
Agent 完成任务后 SHALL 在 Forgejo 创建 Pull Request。Forgejo 的 PR webhook 触发 Orchestrator 状态更新,替代不可靠的直接通知。
#### Scenario: Agent creates PR → Orchestrator receives webhook
- **WHEN** Agent 为任务 org/repo#42 创建 PR
- **AND** Forgejo 触发 `pull_request.opened` webhook
- **THEN** Orchestrator SHALL 收到 webhook识别 PR 标题或分支名中的 task_id
- **AND** 将任务状态更新为 `review_pending`(等待 receipt 验证)
#### Scenario: PR merged → receipt auto-validated
- **WHEN** PR 被 merge
- **AND** Forgejo 触发 `pull_request.merged` webhook
- **THEN** Orchestrator SHALL 自动将任务状态转为 `completed`,生成 receipt
- **AND** 在对应 Issue 添加评论:`✅ Task completed — PR #15 merged`
### Requirement: Task branch and PR naming convention
Orchestrator SHALL 在任务详情中返回预期的分支名和 PR 标题格式,供 Agent 使用。
#### Scenario: Task detail includes branch info
- **WHEN** Agent 查询 `GET /api/v1/tasks/org/repo#42`
- **THEN** 返回 JSON SHALL 包含 `branch_name``pr_title` 字段
- **AND** `branch_name` 格式为 `task/{url_encoded_task_id}`
- **AND** `pr_title` 格式为 `feat: {issue_title} (#{issue_number})`
### Requirement: Push events as progress tracking
Forgejo push webhook SHALL 作为 Agent 工作进度的间接信号。
#### Scenario: Agent pushes to task branch
- **WHEN** Forgejo 触发 `push` webhook目标分支匹配 `task/*` 模式
- **THEN** Orchestrator SHALL 记录该 push 事件作为任务进度信号
- **AND** 更新任务的 `last_activity_at` 时间戳

View file

@ -0,0 +1,67 @@
## ADDED Requirements
### Requirement: Agent task dequeue (pull model)
Agent SHALL 通过 `POST /api/v1/tasks/dequeue` 主动拉取任务。Orchestrator 根据 Agent 声明的 capabilities 匹配最优任务,原子性地分配给该 Agent。
#### Scenario: Agent dequeues a matching task
- **WHEN** Agent `worker-03` 发送 `POST /api/v1/tasks/dequeue`body 包含 `{agent_id: "worker-03", capabilities: ["code:rust", "review"]}`
- **THEN** Orchestrator SHALL 在单个事务中找到 status=created 且匹配 capabilities 的最高优先级任务
- **AND** 将该任务状态转为 `assigned`assigned_agent_id 设为 `worker-03`
- **AND** 返回任务详情 JSON
#### Scenario: No matching task available
- **WHEN** Agent 发送 dequeue 但无匹配任务
- **THEN** Orchestrator SHALL 返回 204 No Content
### Requirement: Agent task status update
Agent 执行过程中 SHALL 通过 `POST /api/v1/tasks/{task_id}/status` 更新任务状态。
#### Scenario: Agent starts execution
- **WHEN** Agent 开始执行任务,发送 `POST /api/v1/tasks/org/repo#42/status` body `{status: "running"}`
- **THEN** Orchestrator SHALL 将任务状态更新为 `running`,记录 started_at
#### Scenario: Agent reports progress
- **WHEN** Agent 发送状态更新但任务已不在 assigned 给该 Agent
- **THEN** Orchestrator SHALL 返回 403 Forbidden
### Requirement: Single task detail query
Orchestrator SHALL 提供 `GET /api/v1/tasks/{task_id}` 返回单个任务详情。
#### Scenario: Query existing task
- **WHEN** 发送 `GET /api/v1/tasks/org/repo#42`
- **THEN** 返回任务完整信息 JSON包含所有字段和事件历史
#### Scenario: Query non-existent task
- **WHEN** 发送 `GET /api/v1/tasks/nonexistent`
- **THEN** 返回 404 Not Found
### Requirement: Agent authentication
Agent 调用任务相关 APIdequeue、status update、receipt时 SHALL 携带注册时获得的 token。Orchestrator SHALL 验证 token 有效性。
#### Scenario: Valid token
- **WHEN** Agent 携带有效 token 调用 dequeue
- **THEN** 请求正常处理
#### Scenario: Invalid or missing token
- **WHEN** Agent 不携带 token 或 token 无效
- **THEN** 返回 401 Unauthorized
### Requirement: Non-PR task completion endpoint
对于不产生 PR 的任务research、review 等Agent SHALL 通过 `POST /api/v1/tasks/{task_id}/complete` 显式提交完成,并附带 receipt。
#### Scenario: Agent completes a non-PR task
- **WHEN** Agent 发送 `POST /api/v1/tasks/org/repo#42/complete`,附带 receipt
- **THEN** Orchestrator SHALL 验证 receipt`POST /api/v1/receipts` 相同验证逻辑)
- **AND** 任务状态转为 `completed`
#### Scenario: Non-owner attempts to complete
- **WHEN** 非 assigned Agent 尝试 complete
- **THEN** 返回 403 Forbidden
### Requirement: Review loop limit
任务在 `running``review_pending` 之间循环 SHALL 有最大次数限制,防止死循环。
#### Scenario: Review loop exceeds limit
- **WHEN** 任务的 review 循环次数超过 `max_retries`
- **THEN** Orchestrator SHALL 将任务标记为 `failed`
- **AND** 在对应 Issue 添加评论说明超限原因