diff --git a/.agents/flow/issues/m10-candidate-launch-override.en.md b/.agents/flow/issues/m10-candidate-launch-override.en.md new file mode 100644 index 00000000..4b39ff37 --- /dev/null +++ b/.agents/flow/issues/m10-candidate-launch-override.en.md @@ -0,0 +1,186 @@ +# [m10] Template machine launch: candidate-commit injection and service tokens + +English | [中文](m10-candidate-launch-override.md) + +```yaml flow +kind: issue +milestone: m10 +priority: P0 +status: ready +scope: + - .agents/flow/issues/m10-candidate-launch-override.md + - .agents/flow/issues/m10-candidate-launch-override.en.md + - .agents/flow/issues/m10-candidate-launch-override.i18n.yaml + - plans/2026-11-25-m10-candidate-launch-override/** + - backend/app/api/eval_templates.py + - backend/app/services/eval_templates.py + - backend/app/api/results.py + - backend/app/api/dependencies.py + - backend/app/models/evaluation.py + - backend/app/models/identity.py + - backend/app/services/service_tokens.py + - backend/app/api/service_tokens.py + - backend/app/main.py + - backend/migrations/** + - tests/backend/test_eval_templates.py + - tests/backend/test_results.py + - tests/backend/test_service_tokens.py + - docs/design/eval-platform-architecture.md + - docs/design/eval-platform-architecture.en.md + - docs/design/eval-platform-architecture.i18n.yaml +adr: [] +github: + number: 429 + url: https://github.com/Shadow-Azure/ai-eval-platform/issues/429 +``` + +## Background + +The three stable execution APIs from M10 speak console-session semantics: launch accepts only an administrator Cookie session and its body carries just an idempotency key plus an optional name, and status/results sit behind a session too; harness-evolution's self-evolution loop needs to trigger evaluations as a machine — after each round publishes a candidate commit, it pins a template evaluation to that commit, polls the status, and fetches the measurements. The requester confirmed the scope in a discussion on 2026-11-25, which converged into this issue; the full analysis and discussion record is in "Deliverables" below. + +## Analysis process + +- The override pipeline already exists: the run-level `POST /api/v1/evaluations` has long supported `asset_source_id / requested_ref / asset_paths / asset_types` and freezes an immutable asset snapshot (`backend/app/api/evaluations.py`, `backend/app/services/agent_assets.py`); what is missing is only the parameter entry on template launches. +- Asset snapshots are frozen by commit (`resolved_commit`, tree digest, bundle), so moving a branch after launch cannot change a running evaluation; `resolve_commit` accepts branch/tag/SHA and a SHA resolves to itself, which directly supports "commit as the authority plus an assertion". +- The template launch chain (`launch_template`) currently resolves assets only from the template's own source and ref; adding override parameters reuses the existing `_resolve_template_assets → build_preparation → consume_preparation` path, so no new execution path is needed. +- The platform has no machine identity: launch requires an administrator session and status/results sit behind a session; harness-evolution cannot hold a browser session, so a service token is the only identity layer that must be added. +- The platform run state machine has 11 states (including partial/paused/cancelled) while the HE poller knows only 5; per D1 the adaptation happens in HE and the platform contract does not change for it. +- Platform results carry only measurements (metric items plus `is_final`) with no verdict; per D4 the verdict stays in HE, and the candidate-evaluation template keeps self-evolution reporting disabled (D5), so duplicate reporting is a non-issue. + +## Discussion conclusions with the requester + +The requester confirmed the following five decisions one by one on 2026-11-25; the V1 gap analysis converged into the V2 design based on them, and this issue is its landing vehicle. + +| Decision | Conclusion | Impact on the design | +|---|---|---| +| D1 contract direction | HE adapts to the platform; the platform contract does not bend toward a generic `/evals` shape | New logic follows the platform's existing style; HE writes the adapter | +| D2 injection parameters | The trigger API supports optional parameters: repository + commit + asset configuration | launch gains a controlled override block; the commit is the authoritative identity with a resolution assertion | +| D3 cross-run activation | The platform does not own promotion; after evaluation HE opens a PR and a human decides the merge | No promotion/merge design in the platform; the template ref target is an operations convention | +| D4 measurement and verdict | The platform returns measurements only; the verdict happens in HE | No verdict/policy fields on the platform; the results API keeps its measurement semantics | +| D5 bad case reporting | The self-evolution evaluation template keeps reporting disabled | Duplicate reporting/dedup is a non-issue; an override launch with a reporting-enabled template only warns | + +## Requirements + +1. HE adapts to the platform mechanism; the platform contract evolves in its own style and does not become a generic `/evals` shape for HE. +2. The trigger API must support optional parameters: the asset configuration of a chosen repository and commit id can be injected into one evaluation; without the parameters the one-click template semantics stay unchanged. +3. The commit is the authoritative identity: injection must resolve by commit, freeze the snapshot, and assert the resolved result; ref is display/audit only. +4. The platform returns measurements only and does not make accept/reject verdicts; the verdict policy is implemented by HE. +5. The evaluation template used for candidate evaluations keeps self-evolution bad case reporting disabled; the platform does not design dedup for it. +6. Cross-run activation is out of the platform's scope: after evaluation HE opens a PR and a human decides whether to merge. + +## Solution overview + +The platform-side changes concentrate in three files plus one new table: optional fields on the request model, override resolution plus assertion in launch, asset identity echo on status/results, and the service-token table; asset resolution, snapshot freezing, and the prepare/create chain are all reused, with no new execution path. + +Interaction flow: HE publishes a candidate → calls launch with `asset_override` → the platform validates, freezes the snapshot, and echoes `resolved_commit` → HE compares and then polls status → on the terminal state it fetches results and converts them into verdict inputs (objective/coverage/guardrails) → HE decides and advances the iteration; promotion is a PR from HE with a human merge (D3, outside the platform). + +- `LaunchTemplateRequest` gains optional `asset_override` (repo_url/ref/commit/asset_paths/asset_types) and `lineage`; each field is replaced as a whole and an absent field inherits the template. +- `launch_template` supports override resolution: a missing repo_url uses the template asset source, otherwise the registered source is found by exact repo_url lookup; a commit resolves by itself with an assertion that `resolved_commit` matches; the run stores `asset_override_json / evolution_lineage_json`. +- status/results echo an `asset` block (resolved_commit, asset_snapshot_id, requested_ref); results also echo lineage when provided. +- A new `service_token` table plus `POST/GET/DELETE /api/v1/service-tokens`; launch accepts "administrator session or service token", status/results accept "any session or an `eval:read` token", with template allowlists and concurrency/budget caps. + +HE-side adaptation (gateway adapter, state mapping, result mapping, baseline and verdict) progresses in the harness-evolution repository and is not part of this issue. + +## Interface changes + +### Before + +```text +POST /api/v1/eval-templates/{template_id}/launch + auth: console session (admin role only); no machine identity + headers: Idempotency-Key (optional) + body: { "idempotency_key": string | null, "name": string | null } + 202 -> { "run_id", "template_id", "status", "status_url", "result_url", "created" } + +GET /api/v1/eval-runs/{run_id}/status + -> { "run_id", "template_id", "template_version", "status", "terminal", "total_cases", + "queued_count", "running_count", "succeeded_count", "failed_count", "finished_count", + "progress": { "total", "finished", "percent" }, "resolved_count", "scored_count", + "resolve_rate", "started_at", "finished_at", "duration_seconds", + "status_url", "result_url" } + +GET /api/v1/eval-runs/{run_id}/results + -> { "run_id", "template_id", "status", "is_final", "status_url", "result_url", + "items": [ { "metric_key", "value", "is_final", ... } ], "token_summary" } +``` + +### After + +```text +POST /api/v1/eval-templates/{template_id}/launch + auth: admin session OR service token (Bearer hev_..., scope eval:launch, + optional template allowlist and budget caps) + headers: Idempotency-Key (unchanged; replay returns the same run) + body: { "idempotency_key": string | null, "name": string | null, + "asset_override": { # all optional; absent = inherit template + "repo_url": string, # default: template asset source + "ref": string, # display/audit only + "commit": string, # 40-hex; authoritative identity + "asset_paths": [string], # whole-field replacement + "asset_types": { string: string } }, # whole-field replacement + "lineage": { # optional; stored on the run only + "evolutionRunId": string, "evolutionRoundId": string, + "roundSeq": int, "parentCommit": string } } + 202 -> previous fields + "asset": { "repo_url", "requested_ref", "requested_commit", + "resolved_commit", "asset_snapshot_id", "bundle_digest" } + + "warnings": [ ... ] # only when override is present and + # the template enables self-evolution + # reporting + 422 -> candidate_commit_mismatch | asset_source_unresolved | asset_ref_unresolved | + asset_selection_empty | asset_path_not_found (no run is created) + +GET /api/v1/eval-runs/{run_id}/status + -> previous fields + "asset": { "resolved_commit", "asset_snapshot_id", "requested_ref" } + +GET /api/v1/eval-runs/{run_id}/results + -> previous fields + "asset" (same shape) + "lineage" (echoed when provided) +``` + +### Input and return changes + +| API | Input change | Return change | +|---|---|---| +| launch | Adds optional `asset_override` and `lineage`; authentication extends from "administrator session only" to "administrator session or service token" | Adds the `asset` block and possible `warnings`; other fields and idempotency semantics stay unchanged | +| status | No input change (machines read with an `eval:read` token) | Adds the `asset` block | +| results | No input change | Adds the `asset` block and `lineage` (echoed when provided) | +| service-tokens (new) | `POST /api/v1/service-tokens` creates (the plaintext is returned once, only in the creation response), `GET` lists (masked), `DELETE` revokes | Only the token hash is stored; no response, log, or snapshot ever echoes the plaintext | + +### New error codes + +| Error code (HTTP) | Trigger condition | +|---|---| +| `candidate_commit_mismatch` (422) | The resolved commit differs from the requested commit; never evaluate silently | +| `asset_source_unresolved` (422) | `asset_override.repo_url` cannot be resolved to a registered asset source by exact repo_url lookup (credentials undeterminable) | +| `asset_ref_unresolved` (422) | commit/ref cannot be resolved in the asset source repository (unreachable repository, invalid credentials, or a missing object) | +| `asset_selection_empty` (422, existing code reused) | The asset selection after override or inheritance is empty | +| `asset_path_not_found` (422, existing code reused) | An asset path does not exist at that commit | + +## Deliverables + +- [OpenAPI change draft](../../../plans/2026-11-25-m10-candidate-launch-override/openapi-change.md): schema-level diff (request models, response extensions, error codes, service-token endpoints). +- [Docs §5.14 revision text](../../../plans/2026-11-25-m10-candidate-launch-override/docs-5-14-revision.md): replacement paragraphs in Chinese and English, to be applied through the platform documentation flow during implementation. + +## Acceptance + +- Launch one real candidate commit with an override: the 202 response's `asset.resolved_commit` equals the requested commit verbatim, and status/results echo it consistently. +- Golden injection proof: a candidate modifies one SKILL.md, and the snapshot manifest's digest for that file equals the digest of the corresponding blob in that commit. +- Commit authority: when both ref and commit are given, resolution follows the commit and ref is echoed verbatim; an unresolvable commit alone answers 422 `asset_ref_unresolved` and creates no run. +- Cross-repository injection: an unregistered `repo_url` answers 422 `asset_source_unresolved`; after registration the same request succeeds. +- Empty selection or a missing path after override: 422 `asset_selection_empty / asset_path_not_found`, and no run is created. +- Console one-click launches without `asset_override` behave unchanged: no `asset` block in the response, and permission, idempotency, and validation semantics stay the same. +- Service token: no token, wrong scope, or a template outside the allowlist → 403; audit records `created_by=svc:`; an idempotent replay returns the same run; `npm test`, `npm run verify:flow`, `npm run lint:docs`, `npm run verify:docs`, and PR CI pass. + +## Non-goals + +- No change to evaluation execution, scheduling, scoring, or the trace chain; the override only affects asset source resolution. +- No accept/reject verdict or policy fields (D4); the verdict is implemented by HE. +- No promotion, merge, or rollback (D3); pointing the template ref at a merged branch or a release tag is an operations convention. +- No bad case dedup or reporting-mode design (D5); reporting semantics are decided entirely by the template configuration. +- HE-side adaptation (gateway, mappings, verdict) is not implemented in this repository. + +## References + +- Design: [docs/design/eval-platform-architecture.en.md](../../../docs/design/eval-platform-architecture.en.md) §5.14 (evaluation templates and one-click launch). +- Related: issue #378 (evaluation templates), #382 (template-level self-evolution reporting), #410 (template provenance display). +- Platform state machine and results read model: `backend/app/models/evaluation.py`, `backend/app/api/results.py`. +- Evidence of the existing override pipeline: `backend/app/api/evaluations.py` (CreateEvaluationRequest), `backend/app/services/agent_assets.py` (resolve_assets). diff --git a/.agents/flow/issues/m10-candidate-launch-override.i18n.yaml b/.agents/flow/issues/m10-candidate-launch-override.i18n.yaml new file mode 100644 index 00000000..dbf6ec5c --- /dev/null +++ b/.agents/flow/issues/m10-candidate-launch-override.i18n.yaml @@ -0,0 +1,5 @@ +# 双语配对一致性记录(docs/i18n/README.md):两侧在最后一次确认一致状态下的 +# git blob hash。两种语言同等权威;编辑任一侧后带上另一侧,然后重录: +# pnpm run record:i18n -- .agents/flow/issues/m10-candidate-launch-override.md +m10-candidate-launch-override.md: 9764f3009c23d705bbecae7081ae01b748b7a7d5 +m10-candidate-launch-override.en.md: 4b39ff374064102e9802e4a7d4be099c7a450975 diff --git a/.agents/flow/issues/m10-candidate-launch-override.md b/.agents/flow/issues/m10-candidate-launch-override.md new file mode 100644 index 00000000..9764f300 --- /dev/null +++ b/.agents/flow/issues/m10-candidate-launch-override.md @@ -0,0 +1,186 @@ +# [m10] 评测模板机器触发:可选参数注入候选 commit 与服务令牌 + +[English](m10-candidate-launch-override.en.md) | 中文 + +```yaml flow +kind: issue +milestone: m10 +priority: P0 +status: ready +scope: + - .agents/flow/issues/m10-candidate-launch-override.md + - .agents/flow/issues/m10-candidate-launch-override.en.md + - .agents/flow/issues/m10-candidate-launch-override.i18n.yaml + - plans/2026-11-25-m10-candidate-launch-override/** + - backend/app/api/eval_templates.py + - backend/app/services/eval_templates.py + - backend/app/api/results.py + - backend/app/api/dependencies.py + - backend/app/models/evaluation.py + - backend/app/models/identity.py + - backend/app/services/service_tokens.py + - backend/app/api/service_tokens.py + - backend/app/main.py + - backend/migrations/** + - tests/backend/test_eval_templates.py + - tests/backend/test_results.py + - tests/backend/test_service_tokens.py + - docs/design/eval-platform-architecture.md + - docs/design/eval-platform-architecture.en.md + - docs/design/eval-platform-architecture.i18n.yaml +adr: [] +github: + number: 429 + url: https://github.com/Shadow-Azure/ai-eval-platform/issues/429 +``` + +## 背景 + +M10 的三个稳定执行接口是控制台会话语义:launch 仅接受管理员 Cookie 会话、body 只有幂等键与可选名称,状态与结果也在会话之后;而 harness-evolution 的自进化迭代需要以机器身份触发评测——每轮发布候选 commit 后,把一次模板评测钉在该 commit 上启动、轮询状态并回取测量。2026-11-25 与需求方讨论后收敛为本 issue,完整分析过程与讨论记录见文末「交付物」。 + +## 分析过程 + +- 覆盖管道已存在:run 级 `POST /api/v1/evaluations` 早已支持 `asset_source_id / requested_ref / asset_paths / asset_types` 并冻结不可变资产快照(`backend/app/api/evaluations.py`、`backend/app/services/agent_assets.py`),缺的只是模板启动上的参数入口。 +- 资产快照按 commit 冻结(`resolved_commit`、tree digest、bundle),启动后分支移动不影响运行;`resolve_commit` 接受 branch/tag/SHA 且 SHA 解析返回自身,天然支持「commit 权威 + 断言」。 +- 模板启动链路(`launch_template`)目前只按模板自己的 source+ref 解析资产;加入覆盖参数即可复用既有 `_resolve_template_assets → build_preparation → consume_preparation`,不需要新执行路径。 +- 平台没有机器身份:launch 需要管理员会话、状态与结果在会话之后;HE 无法持浏览器会话,服务令牌是唯一必须新增的身份层。 +- 平台状态机有 11 态(含 partial/paused/cancelled),HE 轮询器只认 5 态;按 D1 由 HE 适配,平台契约不为此改变。 +- 平台结果只有测量(指标项 + `is_final`),没有 verdict;按 D4 裁决在 HE,候选评测模板不开启自进化上报(D5),重复上报问题不成立。 + +## 与需求方的讨论结论 + +以下五条决策由需求方在 2026-11-25 逐条确认;V1 差距分析据此收敛为 V2 方案,本 issue 即其落地载体。 + +| 决策 | 结论 | 对设计的影响 | +|---|---|---| +| D1 契约方向 | HE 适配平台机制,平台契约不向通用 `/evals` 形状靠拢 | 新逻辑按平台现有风格设计,HE 写适配器 | +| D2 注入参数 | 触发接口支持可选参数:代码仓 + commit + 资产配置 | launch 增加受控覆盖块,commit 为权威身份并断言解析一致 | +| D3 跨运行生效 | 平台不承担晋升;评测完由 HE 提 PR,人工裁决合入 | 平台无晋升/合并设计,模板 Ref 指向由运维约定 | +| D4 测量与裁决 | 平台只给测量,裁决在 HE | 平台不加 verdict/策略字段,结果接口保持测量语义 | +| D5 bad case 上报 | 自进化评测模板不开启上报 | 重复上报/去重不成立,覆盖启动与上报配置不一致时仅提示 | + +## 需求 + +1. HE 负责适配平台机制;平台契约按自身风格演进,不为 HE 改成通用 `/evals` 形状。 +2. 评测触发接口必须支持可选参数:能把某个代码仓、某个 commit id 的资产配置注入本次评测;不传参数时保持模板一键启动语义不变。 +3. commit 是权威身份:注入必须按 commit 解析、冻结快照并断言解析结果一致;ref 只作展示审计。 +4. 平台只返回测量,不做 accept/reject 裁决;裁决策略由 HE 实现。 +5. 候选评测使用的评测模板不开启自进化 bad case 上报;平台不为此设计去重。 +6. 跨运行生效不在平台范围内:评测完成后由 HE 提交 PR,由人判断是否合入。 + +## 方案概述 + +平台侧改动集中在三个文件加一张新表:请求模型加可选字段、launch 支持覆盖解析与断言、status/results 回显资产身份,以及服务令牌表;资产解析、快照冻结与 prepare/create 链路全部复用既有实现,不新增执行路径。 + +交互流程:HE 发布候选 → 携带 `asset_override` 调 launch → 平台校验并冻结快照、回显 `resolved_commit` → HE 比对后轮询 status → 终态取 results 并换算为裁决输入(目标分/覆盖率/护栏)→ HE 裁决并推进迭代;晋升由 HE 提 PR 人工合入(D3,不在平台范围)。 + +- `LaunchTemplateRequest` 增加可选 `asset_override`(repo_url/ref/commit/asset_paths/asset_types)与 `lineage`,字段整体替换、缺省继承模板。 +- `launch_template` 支持覆盖解析:repo_url 缺省用模板资产源,否则按 repo_url 匹配已登记资产源;有 commit 按其解析并断言 `resolved_commit` 一致;run 写入 `asset_override_json / evolution_lineage_json`。 +- status/results 回显 `asset` 块(resolved_commit、asset_snapshot_id、requested_ref),results 另回显 lineage(提供时)。 +- 新增 `service_token` 表与 `POST/GET/DELETE /api/v1/service-tokens`;launch 接受「管理员会话或服务令牌」,status/results 接受「任意会话或 `eval:read` 令牌」,支持模板白名单与并发/预算上限。 + +HE 侧适配(网关适配器、状态映射、结果映射、基线与裁决)在 harness-evolution 仓库另行推进,不属于本 issue。 + +## 接口变更 + +### 变更前 + +```text +POST /api/v1/eval-templates/{template_id}/launch + auth: console session (admin role only); no machine identity + headers: Idempotency-Key (optional) + body: { "idempotency_key": string | null, "name": string | null } + 202 -> { "run_id", "template_id", "status", "status_url", "result_url", "created" } + +GET /api/v1/eval-runs/{run_id}/status + -> { "run_id", "template_id", "template_version", "status", "terminal", "total_cases", + "queued_count", "running_count", "succeeded_count", "failed_count", "finished_count", + "progress": { "total", "finished", "percent" }, "resolved_count", "scored_count", + "resolve_rate", "started_at", "finished_at", "duration_seconds", + "status_url", "result_url" } + +GET /api/v1/eval-runs/{run_id}/results + -> { "run_id", "template_id", "status", "is_final", "status_url", "result_url", + "items": [ { "metric_key", "value", "is_final", ... } ], "token_summary" } +``` + +### 变更后 + +```text +POST /api/v1/eval-templates/{template_id}/launch + auth: admin session OR service token (Bearer hev_..., scope eval:launch, + optional template allowlist and budget caps) + headers: Idempotency-Key (unchanged; replay returns the same run) + body: { "idempotency_key": string | null, "name": string | null, + "asset_override": { # all optional; absent = inherit template + "repo_url": string, # default: template asset source + "ref": string, # display/audit only + "commit": string, # 40-hex; authoritative identity + "asset_paths": [string], # whole-field replacement + "asset_types": { string: string } }, # whole-field replacement + "lineage": { # optional; stored on the run only + "evolutionRunId": string, "evolutionRoundId": string, + "roundSeq": int, "parentCommit": string } } + 202 -> previous fields + "asset": { "repo_url", "requested_ref", "requested_commit", + "resolved_commit", "asset_snapshot_id", "bundle_digest" } + + "warnings": [ ... ] # only when override is present and + # the template enables self-evolution + # reporting + 422 -> candidate_commit_mismatch | asset_source_unresolved | asset_ref_unresolved | + asset_selection_empty | asset_path_not_found (no run is created) + +GET /api/v1/eval-runs/{run_id}/status + -> previous fields + "asset": { "resolved_commit", "asset_snapshot_id", "requested_ref" } + +GET /api/v1/eval-runs/{run_id}/results + -> previous fields + "asset" (same shape) + "lineage" (echoed when provided) +``` + +### 输入与返回变化 + +| 接口 | 输入变化 | 返回变化 | +|---|---|---| +| launch | 新增可选 `asset_override` 与 `lineage`;鉴权从「仅管理员会话」扩展为「管理员会话或服务令牌」 | 新增 `asset` 块与可能的 `warnings`;其余字段与幂等语义不变 | +| status | 无输入变化(机器可凭 `eval:read` 令牌读取) | 新增 `asset` 块 | +| results | 无输入变化 | 新增 `asset` 块与 `lineage`(提供时回显) | +| service-tokens(新增) | `POST /api/v1/service-tokens` 创建(明文仅在创建响应返回一次)、`GET` 列表(掩码)、`DELETE` 撤销 | 令牌只存哈希,任何响应、日志与快照不回显明文 | + +### 新增错误码 + +| 错误码(HTTP) | 触发条件 | +|---|---| +| `candidate_commit_mismatch`(422) | 解析到的 commit 与请求 commit 不一致;绝不静默评测 | +| `asset_source_unresolved`(422) | `asset_override.repo_url` 无法按 repo_url 匹配到已登记资产源(凭据不可确定) | +| `asset_ref_unresolved`(422) | commit/ref 在资产源仓库不可解析(仓库不可达、凭据失效或对象不存在) | +| `asset_selection_empty`(422,既有码复用) | 覆盖或继承后的资产选择为空 | +| `asset_path_not_found`(422,既有码复用) | 资产路径在该 commit 不存在 | + +## 交付物 + +- [OpenAPI 变更稿](../../../plans/2026-11-25-m10-candidate-launch-override/openapi-change.md):schema 级 diff(请求模型、响应扩展、错误码、服务令牌端点)。 +- [文档 §5.14 修订文案](../../../plans/2026-11-25-m10-candidate-launch-override/docs-5-14-revision.md):中英对照的替换段落,实施时按平台文档流程落库。 + +## 验收 + +- 带覆盖 launch 一个真实候选 commit:202 响应的 `asset.resolved_commit` 逐字等于请求 commit,status/results 回显一致。 +- golden 注入证明:候选改一个 SKILL.md,快照 manifest 中该文件 digest 与该 commit 中对应 blob 的 digest 一致。 +- commit 权威:同时给 ref 与 commit 时按 commit 解析、ref 原样回显;仅给不可解析的 commit 时 422 `asset_ref_unresolved` 且不创建 run。 +- 跨仓注入:`repo_url` 未登记时 422 `asset_source_unresolved`;登记后同一请求成功。 +- 覆盖后选择为空或路径不存在:422 `asset_selection_empty / asset_path_not_found`,不创建 run。 +- 不带 `asset_override` 的控制台一键启动行为不变:响应无 `asset` 块,权限、幂等与校验语义不变。 +- 服务令牌:无令牌/错 scope/模板不在白名单 → 403;审计记录 `created_by=svc:`;幂等重放返回同一 run;`npm test`、`npm run verify:flow`、`npm run lint:docs`、`npm run verify:docs` 与 PR CI 通过。 + +## 非目标 + +- 不改评测执行、调度、评分与轨迹链路;覆盖只影响资产来源解析。 +- 不做 accept/reject 裁决与任何策略字段(D4);裁决由 HE 实现。 +- 不做晋升、合并与回滚(D3);模板 Ref 指向合并后分支或发布 tag 由运维约定。 +- 不做 bad case 去重或上报模式设计(D5);上报语义完全由模板配置决定。 +- HE 侧适配(网关、映射与裁决)不在本仓库实现。 + +## 参考 + +- 设计:[docs/design/eval-platform-architecture.md](../../../docs/design/eval-platform-architecture.md) §5.14(评测模板与一键启动)。 +- 相关:issue #378(评测模板)、#382(模板级自进化上报)、#410(模板来源展示)。 +- 平台状态机与结果读模型:`backend/app/models/evaluation.py`、`backend/app/api/results.py`。 +- 既有覆盖管道证据:`backend/app/api/evaluations.py`(CreateEvaluationRequest)、`backend/app/services/agent_assets.py`(resolve_assets)。 diff --git a/plans/2026-11-25-m10-candidate-launch-override/docs-5-14-revision.md b/plans/2026-11-25-m10-candidate-launch-override/docs-5-14-revision.md new file mode 100644 index 00000000..9364b218 --- /dev/null +++ b/plans/2026-11-25-m10-candidate-launch-override/docs-5-14-revision.md @@ -0,0 +1,118 @@ +# 文档 §5.14 修订文案 — M11 评测模板机器触发 + +- 目标文件:`docs/design/eval-platform-architecture.md`(中文 base)与 `docs/design/eval-platform-architecture.en.md`(英文侧)。 +- 本文件只给「替换文案」,不在本 PR 中直接改常驻文档;实施 issue 落地时按下列 before/after 逐句替换,改完任一侧后最小修补另一侧并运行: + +```sh +npm run record:i18n -- docs/design/eval-platform-architecture.md +npm run lint:docs +npm run verify:docs +``` + +- 注意:该文档预算当前为 13500 词;实施时若超出,按门禁提示压缩或在本 manifest 登记 `//` 说明后提预算。 +- 结构镜像约束:两侧段落与表格行列保持镜像;新增句子的中英文各自成段(一行到底)。 + +## 修订 1:覆盖参数(替换 §5.14 第二段中的一句) + +### 中文侧 + +Before(现网原文,段内最后一句): + +> 模板启动不接受配置覆盖,保证“一键”语义与结果可复现;需要不同组合时复制模板再编辑,或走现有的自定义发起流程。 + +After(替换为): + +> 控制台一键启动不接受配置覆盖,保证“一键”语义与结果可复现,需要不同组合时复制模板再编辑或走自定义发起流程;机器触发(服务令牌)可在启动请求携带可选 `asset_override`(repo_url、ref、commit、asset_paths、asset_types),把一次评测钉在指定代码仓的指定候选 commit 上,字段整体替换、缺省继承模板,commit 为权威身份且平台必须断言解析出的 `resolved_commit` 与请求 commit 一致,不一致即 422 拒绝、绝不静默评测,`ref` 仅作展示审计。 + +### 英文侧 + +Before(现网原文,段内最后一句): + +> A template launch accepts no configuration overrides, preserving the one-click semantics and reproducibility; use a copied and edited template, or the existing custom launch flow, for a different combination. + +After(替换为): + +> Console one-click launches accept no configuration overrides, preserving the one-click semantics and reproducibility, so a different combination uses a copied and edited template or the existing custom launch flow; machine triggers (service tokens) may carry an optional `asset_override` (repo_url, ref, commit, asset_paths, asset_types) to pin one evaluation to a chosen repository and candidate commit, fields are replaced as a whole with absent fields inheriting the template, the commit is the authoritative identity, and the platform must assert that the resolved commit equals the requested commit, rejecting with 422 on a mismatch instead of ever evaluating silently, while ref is display and audit only. + +## 修订 2:机器身份(替换 §5.14「三个稳定接口」段的鉴权句) + +### 中文侧 + +Before(现网原文): + +> 三个接口都复用控制台会话鉴权:启动需要管理员权限,状态与结果对已登录用户只读;首期不提供独立的机器令牌。 + +After(替换为): + +> 三个接口复用「控制台会话或服务令牌」鉴权:启动需要管理员权限或持 `eval:launch` 的服务令牌,状态与结果对已登录用户或持 `eval:read` 的服务令牌只读;服务令牌由管理员在 `/api/v1/service-tokens` 创建、列表与撤销,支持 scope、模板白名单与并发/预算上限,明文仅在创建响应返回一次,库内只存哈希,机器启动的 `created_by` 落 `svc:<名称>` 并进入既有审计与幂等约束。 + +### 英文侧 + +Before(现网原文): + +> All three reuse console session authentication: launching requires administrator permission, while status and results are read-only for signed-in users; the first release does not provide a separate machine token. + +After(替换为): + +> All three reuse "console session or service token" authentication: launching requires administrator permission or a service token with `eval:launch`, while status and results are read-only for signed-in users or a service token with `eval:read`; administrators create, list, and revoke service tokens at `/api/v1/service-tokens`, with scopes, a template allowlist, and concurrency/budget caps, the plaintext is returned exactly once in the creation response and only its hash is stored, and machine launches record `created_by` as `svc:` in the existing audit and idempotency constraints. + +## 修订 3:启动语义段补回显与错误码(§5.14「启动接口先校验…」段句尾追加) + +### 中文侧 + +Before(现网原文,§5.14 表 12 后一段): + +> 启动接口先校验模板存在且启用、引用对象仍可用,再走既有的 prepare/create 流程;返回 404 表示模板或评测不存在,409 表示模板停用或幂等键冲突,422 表示引用对象失效或字段不可用,403 表示当前账号没有启动权限。 + +After(段尾追加一句): + +> 启动接口先校验模板存在且启用、引用对象仍可用,再走既有的 prepare/create 流程;返回 404 表示模板或评测不存在,409 表示模板停用或幂等键冲突,422 表示引用对象失效或字段不可用,403 表示当前账号没有启动权限。带覆盖的启动在 202 响应回显 `asset`(resolved_commit、asset_snapshot_id、bundle_digest),status 与 results 同样回显该身份供调用方比对,新增 422 错误码 `candidate_commit_mismatch`、`asset_source_unresolved`、`asset_ref_unresolved`(`asset_selection_empty` 与 `asset_path_not_found` 复用既有码),任一命中即不创建评测。 + +### 英文侧 + +Before(现网原文): + +> The launch interface first validates that the template exists and is enabled and that its references are usable, then walks the existing prepare/create flow; 404 means the template or evaluation does not exist, 409 means the template is disabled or an idempotency-key conflict, 422 means a reference is invalid or a field is unusable, and 403 means the current account lacks launch permission. + +After(段尾追加一句): + +> The launch interface first validates that the template exists and is enabled and that its references are usable, then walks the existing prepare/create flow; 404 means the template or evaluation does not exist, 409 means the template is disabled or an idempotency-key conflict, 422 means a reference is invalid or a field is unusable, and 403 means the current account lacks launch permission. An override launch echoes `asset` (resolved_commit, asset_snapshot_id, bundle_digest) in the 202 response, status and results echo the same identity for caller-side comparison, and new 422 codes `candidate_commit_mismatch`, `asset_source_unresolved`, and `asset_ref_unresolved` join the reused `asset_selection_empty` and `asset_path_not_found`, none of which create an evaluation. + +## 修订 4:上报与覆盖并存的提示(§5.14 末段句尾追加,可选) + +### 中文侧 + +Before(现网原文,§5.14 最后一段): + +> 启动在创建运行前解密保存的令牌,把它与其他上报字段一并传入既有 prepare/create 链路,从模板启动的评测在终态按 M9 #266/#341 outbox 上报 bad case;启用但配置不完整或密文无法解密时,启动在创建运行前返回 422。 + +After(段尾追加一句): + +> 启动在创建运行前解密保存的令牌,把它与其他上报字段一并传入既有 prepare/create 链路,从模板启动的评测在终态按 M9 #266/#341 outbox 上报 bad case;启用但配置不完整或密文无法解密时,启动在创建运行前返回 422。带 `asset_override` 的机器触发沿用模板的上报语义,不自增开关也不做去重;自进化候选评测应使用未启用上报的模板,模板启用上报时带覆盖启动在响应 `warnings` 中提示一次。 + +### 英文侧 + +Before(现网原文,对位段): + +> The launch decrypts the stored token before creating the run and passes it with the other reporting fields into the existing prepare/create chain, so template-launched evaluations report bad cases through the M9 #266/#341 outbox at a terminal state; when enabled but incompletely configured or undecryptable, the launch answers 422 before creating a run. + +After(段尾追加一句): + +> The launch decrypts the stored token before creating the run and passes it with the other reporting fields into the existing prepare/create chain, so template-launched evaluations report bad cases through the M9 #266/#341 outbox at a terminal state; when enabled but incompletely configured or undecryptable, the launch answers 422 before creating a run. Machine triggers with `asset_override` inherit the template's reporting semantics with no extra switch and no dedup; self-evolution candidate evaluations should use a template with reporting disabled, and an override launch against a reporting-enabled template surfaces one warning in the response `warnings`. + +## 修订 5:数据模型表(§6 表 2,可选但建议) + +### 中文侧 + +表 2 中 `eval_run / eval_case_run` 行的「关键字段」列,句尾追加:`、资产覆盖与血缘(asset_override、evolution lineage)`;如新增服务令牌行,则在表尾追加一行:`| service_token | 机器触发身份 | 哈希令牌、scope、模板白名单、并发/预算上限、最近使用与撤销状态 |`。 + +### 英文侧 + +镜像修改:追加 `; asset override and lineage (asset_override, evolution lineage)`;表尾新增行同义翻译:`| service_token | Machine-trigger identity | hashed token, scopes, template allowlist, concurrency/budget caps, last-used and revocation state |`。 + +## 实施清单(落到常驻文档时的顺序) + +1. 按「修订 1/2/3/4/5」逐句替换中英两侧;保持段落一行到底与表格镜像。 +2. `npm run record:i18n -- docs/design/eval-platform-architecture.md` 重录配对。 +3. `npm run lint:docs` → `npm run verify:docs`;预算超限时按门禁提示压缩或登记提预算说明。 +4. 同步检查 §5.14 接口示例块(若实施时补 curl 示例,两侧围栏逐字一致)。 diff --git a/plans/2026-11-25-m10-candidate-launch-override/openapi-change.md b/plans/2026-11-25-m10-candidate-launch-override/openapi-change.md new file mode 100644 index 00000000..2eaa8bd8 --- /dev/null +++ b/plans/2026-11-25-m10-candidate-launch-override/openapi-change.md @@ -0,0 +1,277 @@ +# OpenAPI 变更稿 — M11 评测模板机器触发(候选 commit 注入 + 服务令牌) + +- 基线:main @ `a038388`(M10 评测模板三稳定接口)。 +- 性质:**纯增量(additive)**。既有字段、既有错误码、既有幂等语义全部不变;不带 `asset_override` 的请求(控制台一键启动)行为与响应形状完全不变。 +- 平台 OpenAPI 由 FastAPI 运行时生成,本文给出实现用的 schema 级 diff;实施后以 `docs/design/eval-platform-architecture.md` §5.14 修订文案(同目录 `docs-5-14-revision.md`)同步常驻文档。 + +## 0. 变更总览 + +| 位置 | 变更 | 类型 | +|---|---|---| +| `components.securitySchemes` | 新增 `serviceToken`(Bearer,前缀 `hev_`) | 新增 | +| `POST /api/v1/eval-templates/{template_id}/launch` | requestBody 新增可选 `asset_override`、`lineage`;202 响应新增 `asset`、`warnings`;新增 422 错误码 | 扩展 | +| `GET /api/v1/eval-runs/{run_id}/status` | 响应新增 `asset` | 扩展 | +| `GET /api/v1/eval-runs/{run_id}/results` | 响应新增 `asset`、`lineage` | 扩展 | +| `POST/GET/DELETE /api/v1/service-tokens` | 服务令牌管理端点(管理员) | 新增 | +| 错误码 | 新增 `candidate_commit_mismatch`、`asset_source_unresolved`、`asset_ref_unresolved`;复用既有 `asset_selection_empty`、`asset_path_not_found` | 新增/复用 | + +## 1. securitySchemes + +```yaml +components: + securitySchemes: + serviceToken: + type: http + scheme: bearer + bearerFormat: hev_ + description: > + Machine identity for harness-evolution (or other services). Scopes: + eval:launch (template launch), eval:read (status/results reads). + Optional per-token template allowlist and concurrency/budget caps. + Stored hashed; the plaintext is returned exactly once at creation. +``` + +launch 的 security 组合由实现改为「`sessionCookie` 或 `serviceToken`(需 `eval:launch`)」;status/results 为「`sessionCookie` 或 `serviceToken`(需 `eval:read`)」。FastAPI 侧对应新的 `require_session_or_service` 依赖,OpenAPI 中以 `security: [{ sessionCookie: [] }, { serviceToken: [] }]` 表达。 + +## 2. `POST /api/v1/eval-templates/{template_id}/launch` + +### 2.1 requestBody(扩展前 → 扩展后) + +扩展前(现网行为,保留): + +```yaml +requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/LaunchTemplateRequest" +# LaunchTemplateRequest(现网): +# idempotency_key: string | null (max 128) +# name: string | null (max 200) +``` + +扩展后: + +```yaml +components: + schemas: + LaunchTemplateRequest: + type: object + properties: + idempotency_key: + type: [string, "null"] + maxLength: 128 + name: + type: [string, "null"] + maxLength: 200 + asset_override: + $ref: "#/components/schemas/AssetOverrideRequest" + lineage: + $ref: "#/components/schemas/LaunchLineageRequest" + additionalProperties: false + + AssetOverrideRequest: + type: object + description: > + Optional. Whole-field replacement semantics: a field that is absent + inherits the template; a field that is present replaces the template + value entirely (no merging). The console one-click launch never sends + this object. + properties: + repo_url: + type: string + description: > + Default: the template's asset source. When present, the platform + resolves the source by exact repo_url match among registered + AgentAssetSource rows of the same tenant; credentials and scan + paths follow the matched source. + ref: + type: string + maxLength: 200 + description: Display/audit only. Never authoritative. + commit: + type: string + pattern: "^[0-9a-f]{40}$" + description: > + Authoritative identity. The platform resolves this commit in the + source repository and MUST assert + resolved_commit == commit; a mismatch fails the launch. + asset_paths: + type: array + items: { type: string, maxLength: 500 } + asset_types: + type: object + additionalProperties: { type: string } + + LaunchLineageRequest: + type: object + description: > + Optional provenance from the caller (harness-evolution). Stored on the + eval run and echoed in results; it never participates in evaluation + semantics. + properties: + evolutionRunId: { type: string, maxLength: 64 } + evolutionRoundId: { type: string, maxLength: 64 } + roundSeq: { type: integer, minimum: 1 } + parentCommit: { type: string, maxLength: 40 } +``` + +### 2.2 202 响应(扩展前 → 扩展后) + +扩展后新增两个字段: + +```yaml +components: + schemas: + LaunchAcceptedResponse: + type: object + required: [run_id, template_id, status, status_url, result_url, created, asset] + properties: + run_id: { type: string } + template_id: { type: string } + template_version: { type: integer } + status: { type: string } # preparing | queued | running(沿用现网) + status_url: { type: string } + result_url: { type: string } + created: { type: boolean } + asset: + $ref: "#/components/schemas/RunAssetIdentity" + warnings: + type: array + items: { type: string } + description: > + Present only when asset_override was provided while the template + enables self-evolution reporting; informational, never blocking. + + RunAssetIdentity: + type: object + required: [resolved_commit, asset_snapshot_id] + properties: + repo_url: { type: string } + requested_ref: { type: [string, "null"] } + requested_commit: { type: [string, "null"] } + resolved_commit: + type: string + description: > + The commit the evaluation is pinned to. When requested_commit was + provided, resolved_commit == requested_commit is enforced at launch. + asset_snapshot_id: { type: string } + bundle_digest: { type: [string, "null"] } +``` + +控制台一键启动(不带 `asset_override`)的 202 响应**不含** `asset` 与 `warnings`,与现网逐字节一致;`asset` 在 schema 中非 required 于「无覆盖」场景,实现可用「仅带覆盖时输出」表达(或在 OpenAPI 中声明 `asset` 可空),以实现 PR 为准。 + +### 2.3 新增 422 错误码(共享错误信封不变) + +错误信封沿用现网 `{"error": {"code", "message", "request_id", ...}}`: + +```yaml +components: + schemas: + LaunchErrorCodes: + type: string + enum: + - template_not_found # 404(既有) + - template_disabled # 409(既有) + - idempotency_conflict # 409(既有) + - template_reference_invalid # 422(既有) + - candidate_commit_mismatch # 422(新增) + - asset_source_unresolved # 422(新增) + - asset_ref_unresolved # 422(新增) + - asset_selection_empty # 422(既有码,launch 场景复用) + - asset_path_not_found # 422(既有码,launch 场景复用) + - image_not_ready # 409(既有) + - forbidden # 403(既有;服务令牌 scope/白名单不足) +``` + +新增三个码的触发条件: + +| code | HTTP | 触发条件 | +|---|---|---| +| `candidate_commit_mismatch` | 422 | 解析到的 `resolved_commit` 与请求 `commit` 不一致(ref 漂移、对象被替换)。绝不静默评测。 | +| `asset_source_unresolved` | 422 | `asset_override.repo_url` 在同租户已登记资产源中无精确 `repo_url` 匹配,凭据不可确定。 | +| `asset_ref_unresolved` | 422 | commit/ref 在资产源仓库不可解析(仓库不可达、凭据失效、对象不存在)。 | + +带 `details`:`{ "field": "asset_override", "requested": ..., "resolved": ... }`,便于 HE 定位。 + +## 3. `GET /api/v1/eval-runs/{run_id}/status`(响应扩展) + +```yaml +EvalRunStatusResponse: + # 既有字段全部保留(status/terminal/progress/counts/resolve_rate/...) + properties: + asset: + $ref: "#/components/schemas/RunAssetIdentityEcho" + # RunAssetIdentityEcho = { resolved_commit, asset_snapshot_id, requested_ref } + # - 仅模板启动且解析过资产的 run 输出;合成评测为 null / 省略 +``` + +## 4. `GET /api/v1/eval-runs/{run_id}/results`(响应扩展) + +```yaml +EvalRunResultsResponse: + # 既有字段全部保留(status/is_final/items/token_summary/...) + properties: + asset: + $ref: "#/components/schemas/RunAssetIdentityEcho" + lineage: + type: object + description: Echoed only when lineage was provided at launch. + properties: + evolutionRunId: { type: string } + evolutionRoundId: { type: string } + roundSeq: { type: integer } + parentCommit: { type: [string, "null"] } +``` + +## 5. 服务令牌端点(新增) + +```yaml +paths: + /api/v1/service-tokens: + post: + summary: Create a service token (admin session only) + security: [{ sessionCookie: [] }] + requestBody: + content: + application/json: + schema: + type: object + required: [name, scopes] + properties: + name: { type: string, maxLength: 100 } + scopes: + type: array + items: { type: string, enum: [eval:launch, eval:read] } + template_allowlist: + type: array + items: { type: string } # template_id; empty = all templates + max_concurrent_runs: { type: [integer, "null"], minimum: 1 } + daily_budget_cny: { type: [number, "null"], minimum: 0 } + expires_at: { type: [string, "null"], format: date-time } + responses: + "201": + description: > + The plaintext token (hev_...) is returned exactly once and never + stored; only its SHA-256 hash persists. + /api/v1/service-tokens/{token_id}: + delete: + summary: Revoke a service token (admin session only) + responses: { "204": { description: Revoked } } + /api/v1/service-tokens: + get: + summary: List service tokens (admin session only; masked) + responses: + "200": + description: id, name, scopes, allowlist size, last_used_at, revoked_at; no secret material. +``` + +对应存储:`service_token(id, tenant_id, name, token_hash, scopes_json, template_allowlist_json, max_concurrent_runs, daily_budget_cny, expires_at, revoked_at, last_used_at, created_by)`,Alembic 迁移一份。认证依赖:`require_session_or_service`(launch=eval:launch;status/results=eval:read);`created_by` 落 `svc:`,服务令牌发起的 launch 同样写入既有审计与幂等约束(`(tenant_id, created_by, idempotency_key)`)。 + +## 6. 兼容性说明 + +- 全部为增量:不删字段、不改既有字段语义、不改既有错误码含义;旧客户端(控制台)零感知。 +- `asset` 块只对「模板启动且解析过资产」的 run 输出;合成评测与旧 run 不输出(字段可空或省略,实现 PR 定稿)。 +- 幂等键唯一约束仍是 `(tenant_id, created_by, idempotency_key)`;服务令牌主体 `svc:` 与真人会话天然隔离。 +- 平台不给 verdict/策略字段(D4);HE 依据 `results` 的指标项与 `status` 计数自行换算裁决输入。