| 1 | # Agent 核心精简 |
| 2 | |
| 3 | 本文档记录 Agent 核心精简改造:精简后循环的行为契约、用于前后对比的指标、 |
| 4 | 以及每个契约项的测试位置。英文版见 `AGENT_CORE_SIMPLIFICATION.md`。 |
| 5 | |
| 6 | ## 目标循环 |
| 7 | |
| 8 | ```text |
| 9 | 构造请求 |
| 10 | → provider stream |
| 11 | → clean final:结束 |
| 12 | → tool call:执行并进入下一 step |
| 13 | → request error:统一 retry |
| 14 | → 未处理错误:明确失败 |
| 15 | ``` |
| 16 | |
| 17 | ## 产品取舍 |
| 18 | |
| 19 | - 普通请求默认 executor-only;planner 改为显式启用(`planner_model`)。 |
| 20 | - 普通 Agent 默认关闭 synthetic continuation;Goal、review、guardian、 |
| 21 | typed report 等显式流程保留自己的约束。 |
| 22 | - compaction 默认单次摘要;chunked/tree-reduce 高级恢复仅限显式场景 |
| 23 | (手动 `/compact` 与明确标记的 recovery workflow)。 |
| 24 | - final readiness、工具安全、取消、预算和显式全文读取的有界暂停继续作为硬边界; |
| 25 | 普通部分读取不冻结独立工作,详见 [读取证据生命周期](READ_EVIDENCE_LIFECYCLE.zh-CN.md)。 |
| 26 | - 旧配置/旧状态保留一版读取兼容;新运行时不再执行旧 fallback。 |
| 27 | |
| 28 | ## 指标基线 |
| 29 | |
| 30 | 复用现有 usage 与 e2ebench 埋点,不新增专用 fallback telemetry。每个阶段 |
| 31 | 前后对比使用: |
| 32 | |
| 33 | - `reasonix run --metrics <path>`:单次运行 `RunMetrics`:token/成本、 |
| 34 | `usage_by_source`(executor/planner/subagent/compaction/... 的请求调用数)、 |
| 35 | `retries`、`compactions`、`steps`。 |
| 36 | - `go run ./cmd/e2ebench -task <task> -json`:每任务请求数、 |
| 37 | `usage_by_source`、trajectory 摘要(stream retry、reasoning replay、 |
| 38 | empty-final retry、TTFT、按 source 的请求数)、墙钟时间、cache 命中。 |
| 39 | |
| 40 | | 指标 | 来源 | |
| 41 | |---|---| |
| 42 | | 每个普通 turn 的模型请求数 | `usage_by_source["executor"].Calls` / trajectory `ExecutorRequests` | |
| 43 | | planner 请求数 | `usage_by_source["planner"].Calls` / trajectory `PlannerRequests` | |
| 44 | | reviewer/evaluator/guardian 请求数 | `usage_by_source["recovery_reviewer"|"goal_evaluator"]`、guardian assessment usage | |
| 45 | | synthetic continuation 次数 | clean turn 的 executor 请求数超出 1 的部分;trajectory `EmptyFinalRetries` | |
| 46 | | stream retry 次数 | trajectory `StreamRetries` / `RunMetrics.Retries` | |
| 47 | | compaction 请求数与 summary spans | `RunMetrics.Compactions`、compaction telemetry 通知(`spans=`、`reqs=`) | |
| 48 | | 首个文本事件延迟 | trajectory `TTFTMs` | |
| 49 | | 总 turn 延迟 | `RunMetrics.DurationMs` / bench `WallMs` | |
| 50 | | tool 执行成功率 | bench 任务 solved 率 / `SolvedThenBroken` | |
| 51 | | 因协议错误终止次数 | trajectory retry-exhausted 结果 | |
| 52 | |
| 53 | 至少对比三类场景:普通问答、普通代码修改、长上下文/工具密集 |
| 54 | (`context-pressure` 任务)。每阶段目标:普通请求一条 executor 请求链,无 |
| 55 | 隐式 planner/reviewer/evaluator 请求,clean final 无额外 continuation, |
| 56 | 默认 compaction 不进入多段 summary,硬安全失败率不上升。 |
| 57 | |
| 58 | ## 契约测试 |
| 59 | |
| 60 | 新增的汇总套件:`internal/agent/agent_contract_test.go`。 |
| 61 | |
| 62 | | 契约项 | 测试 | |
| 63 | |---|---| |
| 64 | | clean final 恰好一次模型请求 | `TestContractCleanFinalMakesOneModelRequest` | |
| 65 | | tool call 执行后进入下一 step | `TestContractToolCallAdvancesToNextStep` | |
| 66 | | thinking 在统一 retry 中保留(冻结请求) | `TestContractThinkingSurvivesUnifiedRetry` | |
| 67 | | retry 耗尽后不残留长期 fallback 状态 | `TestContractNoLongLivedFallbackStateAfterRetryExhaustion` | |
| 68 | | clean final 不追加 synthetic continuation | `TestContractCleanFinalAddsNoSyntheticContinuation` | |
| 69 | | reasoning-only clean stop 直接完成 | `TestRunAcceptsReasoningOnlyFinalAnswer` | |
| 70 | | 完全零内容走统一 `EMPTY_RESPONSE` retry | `TestRunRetriesZeroContentWithTheSameFrozenRequest` | |
| 71 | | retry 耗尽返回明确协议错误 | `TestRunStopsAfterExhaustedZeroContentRetriesWithoutCommittingEmptyMessages` | |
| 72 | | strict provider 缺失 reasoning 只做一次冻结请求重试 | `TestRunSilentlyRecoversMissingToolCallReasoning` 及 `loop_e2e_test.go`/`retry_e2e_test.go` 的 replay 套件(#9776 修复) | |
| 73 | | incomplete-read 门 | `incomplete_read_test.go` | |
| 74 | | final readiness | `final_readiness_test.go` | |
| 75 | | 取消 | `cancel_test.go` | |
| 76 | | task/token/cost 预算 | `run_budget_test.go` | |
| 77 | | 工具权限/畸形参数 | `argument_validation_test.go`、gate 相关测试 | |
| 78 | | 普通请求不调用 planner | `TestCoordinatorOrdinaryRequestDoesNotCallPlanner`、`TestDecidePlannerRouteExplicitOnly` | |
| 79 | | planner 无 `submit_plan` 的散文失败 | `TestCoordinatorPlanAndExecuteRequiresSubmittedPlan` | |
| 80 | | planner 失败不跑 executor | `TestCoordinatorFailsClosedWhenPlannerFails` | |
| 81 | | 不可用的 `planner_model` 是配置错误 | `TestBuildFailsWhenPlannerModelIsUnresolvable` | |
| 82 | | 普通 Agent 不追加 todo continuation | `TestStandardTodoContinuationDisabledByDefault` | |
| 83 | | 普通 compaction 保持单次摘要 | `TestPressureCompactionDoesNotCallChunkedFold` | |
| 84 | | 缺失的 guardian/recovery 模型 fail-closed | `TestBuildFailsWhenGuardianModelIsUnresolvable`、`TestBuildFailsWhenRecoveryModelIsUnresolvable` | |
| 85 | |
| 86 | ## 门禁 |
| 87 | |
| 88 | 每个阶段执行: |
| 89 | |
| 90 | ```bash |
| 91 | go test -count=1 ./internal/agent/... ./internal/control/... ./internal/config/... ./internal/boot/... |
| 92 | go test -race ./internal/agent/... ./internal/provider/... |
| 93 | go vet ./... |
| 94 | go run ./tools/repolint |
| 95 | ``` |
| 96 |