diff --git a/docs/agent-platform-upgrade-plan.md b/docs/agent-platform-upgrade-plan.md index 5ed1be1..82f7442 100644 --- a/docs/agent-platform-upgrade-plan.md +++ b/docs/agent-platform-upgrade-plan.md @@ -33,7 +33,7 @@ P0 运行时内核 ──► P1 工具网关 ──► P2 会话持久化 ── **四个设计红线**(贯穿全部阶段,后文不再重复): -1. **前端零改动**:`/api/v1/query_ai_agent_config_list`、`/api/v1/create_session`、`/api/v1/chat` 三个端点的**请求与响应结构必须保持不变**(`frontend/src/api/agent.ts:25,39,54` 直接依赖)。 +1. **前端零改动**:`/api/v1/query_ai_agent_config_list`、`/api/v1/create_session`、`/api/v1/chat` 三个端点的**请求与响应结构必须保持不变**(`agentApi` in `frontend/src/api/agent.ts` 直接依赖)。 2. **配置向后兼容**:`configs/agent/*.yaml` 只做**增量字段**,旧字段语义不变;行为变更(如 parallel 真并发)必须通过开关切换,默认保持旧行为一个版本。 3. **六边形架构不破**:新增能力一律「先在 `internal/domain/` 定端口,再到 `internal/infrastructure/` 落适配器,最后在 `bootstrap.Build()` 显式装配」。禁止在 handler 或 adapter 里直接 `new` 基础设施。 4. **每阶段必须可独立上线 + 可回滚**:用 4 个配置开关控制(`runtime.enabled` / `memory.enabled` / `tools.governance.enabled` / `workflow.execution-mode`),任何阶段出问题能退回上一阶段的行为。 @@ -93,26 +93,26 @@ P0 运行时内核 ──► P1 工具网关 ──► P2 会话持久化 ── | ID | 现象 | 证据(file:line) | 影响 | 归属 | |---|---|---|---|---| -| D-01 | 每轮对话只构造 `system + user` 两条消息,无历史 | `internal/infrastructure/adk/adapter.go:379-386` | 多轮对话第二十轮丢失首轮信息 | P4 | -| D-02 | `Runner.Run/Stream` 用 `context.Background()` | `adk/adapter.go:415,449,469` | HTTP 取消/超时传不到 Agent;客户端断开仍烧 token | P0 | -| D-03 | 任一工具失败即整轮失败 | `adk/adapter.go:274-294` | 一个下游 5xx 让整个 run 失败 | P1 | -| D-04 | 所有工具参数硬编码为单个 `query: string` | `ai/openai_client.go:278-299` | 模型无法正确填参;无法做参数校验 | P1 | -| D-05 | `ListTools` 只取 `name`,丢弃 description/inputSchema | `ai/mcp_sse_client.go:348-369` | 与 D-04 叠加,工具描述与 schema 双双缺失 | P1 | -| D-06 | `callRPC` 在 `postURL==""` 时直接 return,未删 `pending[id]` | `ai/mcp_sse_client.go:402-411` | 内存泄漏 | P1 | -| D-07 | `MCPToolRouter` 无锁,多 goroutine 并发注册/调用 | `ai/mcp_sse_client.go:487-505` | data race | P1 | -| D-08 | `maxToolCallIterations = 4` 常量 | `adk/adapter.go:15` | 无法按 Agent 配置;复杂任务早退 | P0 | -| D-09 | `max-iterations` 配置被解析但从未使用 | `adk/adapter.go:330-348` | 配置项形同虚设 | P5 | -| D-10 | parallel 与 loop 都退化为串行 fan-out | `adk/adapter.go:330-348` | 并行分支耗时 = 串行之和 | P5 | -| D-11 | `CreateSession` 先 `Get` 再 `Set`,无锁无唯一约束 | `chat/service.go:37-53` | 并发创建出多个会话 | P2 | -| D-12 | `resolveRunnerSession` 直接用请求里的 `sessionID` | `chat/service.go:97-116` | 任意用户可读写他人会话(越权) | P2 | -| D-13 | `SessionStore` 只存 ID,`ChatMessage` 从不落库 | `ports/ports.go:104-107` | 重启即失忆;无法回溯 | P2/P3 | -| D-14 | `Runner.CreateSession` 用全局原子计数拼 ID | `adk/adapter.go:391-398` | 跨 Agent 可能撞 ID;无租户维度 | P2 | +| D-01 | 每轮对话只构造 `system + user` 两条消息,无历史 | `initialMessages` in `internal/infrastructure/adk/adapter.go` | 多轮对话第二十轮丢失首轮信息 | P4 | +| D-02 | `Runner.Run/Stream` 用 `context.Background()` | `(*Runner).Run`、`(*Runner).Stream`、`(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go` | HTTP 取消/超时传不到 Agent;客户端断开仍烧 token | P0 | +| D-03 | 任一工具失败即整轮失败 | `(*Agent).executeToolCalls` in `internal/infrastructure/adk/adapter.go` | 一个下游 5xx 让整个 run 失败 | P1 | +| D-04 | 所有工具参数硬编码为单个 `query: string` | `encodeTools` in `internal/infrastructure/ai/openai_client.go` | 模型无法正确填参;无法做参数校验 | P1 | +| D-05 | `ListTools` 只取 `name`,丢弃 description/inputSchema | `(*MCPSSEClient).ListTools` in `internal/infrastructure/ai/mcp_sse_client.go` | 与 D-04 叠加,工具描述与 schema 双双缺失 | P1 | +| D-06 | `callRPC` 在 `postURL==""` 时直接 return,未删 `pending[id]` | `(*MCPSSEClient).callRPC` in `internal/infrastructure/ai/mcp_sse_client.go` | 内存泄漏 | P1 | +| D-07 | `MCPToolRouter` 无锁,多 goroutine 并发注册/调用 | `MCPToolRouter` in `internal/infrastructure/ai/mcp_sse_client.go` | data race | P1 | +| D-08 | `maxToolCallIterations = 4` 常量 | `maxToolCallIterations` in `internal/infrastructure/adk/adapter.go` | 无法按 Agent 配置;复杂任务早退 | P0 | +| D-09 | `max-iterations` 配置被解析但从未使用 | `(*Agent).runFanOut` in `internal/infrastructure/adk/adapter.go` | 配置项形同虚设 | P5 | +| D-10 | parallel 与 loop 都退化为串行 fan-out | `(*Agent).runFanOut` in `internal/infrastructure/adk/adapter.go` | 并行分支耗时 = 串行之和 | P5 | +| D-11 | `CreateSession` 先 `Get` 再 `Set`,无锁无唯一约束 | `(*Service).CreateSession` in `internal/domain/agent/service/chat/service.go` | 并发创建出多个会话 | P2 | +| D-12 | `resolveRunnerSession` 直接用请求里的 `sessionID` | `(*Service).resolveRunnerSession` in `internal/domain/agent/service/chat/service.go` | 任意用户可读写他人会话(越权) | P2 | +| D-13 | `SessionStore` 只存 ID,`ChatMessage` 从不落库 | `SessionStore` in `internal/domain/agent/ports/ports.go` | 重启即失忆;无法回溯 | P2/P3 | +| D-14 | `Runner.CreateSession` 用全局原子计数拼 ID | `(*Runner).CreateSession` in `internal/infrastructure/adk/adapter.go` | 跨 Agent 可能撞 ID;无租户维度 | P2 | | D-15 | Armory 遍历 map 装配 | `armory/service.go` | 启动顺序不确定,问题难复现 | P0 | | D-16 | 工具无超时,仅靠 HTTP 客户端整体超时 | `ai/mcp_sse_client.go` `RequestTimeout` 默认 300s | 单工具挂起拖死整个 run | P1 | -| D-17 | 无 run_id / trace_id,日志无法关联一次执行 | `adk/adapter.go:460-470` | 线上问题无法定位 | P7 | -| D-18 | `writeError(c, nil)` | `internal/trigger/http/agent_handler.go:102-109` | nil 解引用 panic | P8 | -| D-19 | CORS 反射任意 Origin 且允许凭证 | `bootstrap/bootstrap.go:120-138` | 跨站请求伪造面 | P8 | -| D-20 | 无优雅停机,`engine.Run()` 直接 `Router.Run(addr)` | `bootstrap/bootstrap.go:93-102` | 发布丢请求、MCP 连接不复位 | P8 | +| D-17 | 无 run_id / trace_id,日志无法关联一次执行 | `(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go` | 线上问题无法定位 | P7 | +| D-18 | `writeError(c, nil)` | `writeError` in `internal/trigger/http/agent_handler.go` | nil 解引用 panic | P8 | +| D-19 | CORS 反射任意 Origin 且允许凭证 | `corsMiddleware` in `internal/app/bootstrap/bootstrap.go` | 跨站请求伪造面 | P8 | +| D-20 | 无优雅停机,`engine.Run()` 直接 `Router.Run(addr)` | `(*Engine).Run` in `internal/app/bootstrap/bootstrap.go` | 发布丢请求、MCP 连接不复位 | P8 | > 完整审计见 `docs/项目完善报告.md`(P0-1..P3-23,带 file:line),本表只保留与升级路线强相关的 20 条。 @@ -265,12 +265,12 @@ cmd/migrate/ # ★新增:迁移执行 | 证据 | 问题 | |---|---| -| `adk/adapter.go:415,449,469` | 三处 `context.Background()`,请求取消传不进去(D-02) | -| `adk/adapter.go:15` | `maxToolCallIterations = 4` 硬编码(D-08) | +| `(*Runner).Run`、`(*Runner).Stream`、`(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go` | 三处 `context.Background()`,请求取消传不进去(D-02) | +| `maxToolCallIterations` in `internal/infrastructure/adk/adapter.go` | `maxToolCallIterations = 4` 硬编码(D-08) | | `adk/adapter.go` 全文 572 行 | 定义/执行/流程/插件混在一个文件,无法单测 | -| `adk/adapter.go:460-470` | 插件输出走 `fmt.Printf`,无 run 上下文(D-17) | +| `(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go` | 插件输出走 `fmt.Printf`,无 run 上下文(D-17) | | `armory/service.go` | map 遍历装配,顺序随机(D-15) | -| `ports.go:104-107` | `SessionStore` 无 ctx,存储层无法超时/取消 | +| `SessionStore` in `internal/domain/agent/ports/ports.go` | `SessionStore` 无 ctx,存储层无法超时/取消 | ### 设计 @@ -394,7 +394,7 @@ const ( 现在的分派逻辑: ```go -// adk/adapter.go:129 现状 +// 现状:(*Agent).runWithVars in internal/infrastructure/adk/adapter.go func (a *Agent) runWithVars(ctx context.Context, vars map[string]string) (string, error) { switch a.kind { case KindLLM: return a.runLLM(ctx, vars) @@ -657,12 +657,12 @@ runtime: | 证据 | 问题 | |---|---| -| `openai_client.go:278-299` | 所有工具只暴露一个 `query` 字符串参数,模型无法填结构化参数,也没有可校验的 schema(D-04) | -| `mcp_sse_client.go:348-369` | `tools/list` 只取 name,丢弃 `description`/`inputSchema`(D-05) | -| `adk/adapter.go:274-294` | 任一工具失败 `return nil, err`,整轮终止(D-03) | +| `encodeTools` in `internal/infrastructure/ai/openai_client.go` | 所有工具只暴露一个 `query` 字符串参数,模型无法填结构化参数,也没有可校验的 schema(D-04) | +| `(*MCPSSEClient).ListTools` in `internal/infrastructure/ai/mcp_sse_client.go` | `tools/list` 只取 name,丢弃 `description`/`inputSchema`(D-05) | +| `(*Agent).executeToolCalls` in `internal/infrastructure/adk/adapter.go` | 任一工具失败 `return nil, err`,整轮终止(D-03) | | `tools.go` `normalizeTimeout` | 默认 300s,无单工具超时控制(D-16) | -| `mcp_sse_client.go:402-411` | `pending` map 泄漏(D-06) | -| `mcp_sse_client.go:487-505` | Router 无锁(D-07) | +| `(*MCPSSEClient).callRPC` in `internal/infrastructure/ai/mcp_sse_client.go` | `pending` map 泄漏(D-06) | +| `MCPToolRouter` in `internal/infrastructure/ai/mcp_sse_client.go` | Router 无锁(D-07) | | `ai/tools.go` 的 `SkillTool` | Skill 注册成工具但没有 `Invoke`,调用即空转;本阶段改为只注入 instruction(见 4.2.8) | | 全局 | 无重试、无熔断、无幂等、无结果大小限制 | @@ -832,7 +832,7 @@ func (c *MCPSSEClient) ListTools(ctx context.Context) ([]ServerTool, error) 同时修复 `encodeTools`: ```go -// ai/openai_client.go:278 现状:所有工具共用 {query: string} +// 现状:encodeTools in internal/infrastructure/ai/openai_client.go 让所有工具共用 {query: string} // 改为: func encodeTools(specs []tool.Spec) []map[string]any { out := make([]map[string]any, 0, len(specs)) @@ -994,11 +994,11 @@ tools: | 证据 | 问题 | |---|---| -| `ports.go:109-167` | `InMemorySessionStore` 只有 `Get/Set`,重启即丢(D-13) | -| `chat/service.go:37-53` | `CreateSession` 先 Get 再 Set,无锁无唯一约束(D-11) | -| `chat/service.go:97-116` | `resolveRunnerSession` 直接用请求里的 `sessionID`,不校验归属(D-12) | -| `adk/adapter.go:391-398` | `appName:userID:counter` 拼 ID,无租户、可能撞号(D-14) | -| `bootstrap.go:49-50` | 写死 `NewInMemorySessionStore()`,MySQL/Redis 适配器未接线 | +| `InMemoryAgentRegistry`/`InMemorySessionStore` in `internal/domain/agent/ports/ports.go` | `InMemorySessionStore` 只有 `Get/Set`,重启即丢(D-13) | +| `(*Service).CreateSession` in `internal/domain/agent/service/chat/service.go` | `CreateSession` 先 Get 再 Set,无锁无唯一约束(D-11) | +| `(*Service).resolveRunnerSession` in `internal/domain/agent/service/chat/service.go` | `resolveRunnerSession` 直接用请求里的 `sessionID`,不校验归属(D-12) | +| `(*Runner).CreateSession` in `internal/infrastructure/adk/adapter.go` | `appName:userID:counter` 拼 ID,无租户、可能撞号(D-14) | +| `Build` in `internal/app/bootstrap/bootstrap.go` | 写死 `NewInMemorySessionStore()`,MySQL/Redis 适配器未接线 | | 全局 | 无消息表、无历史查询、无 TTL、无归档 | ### 设计 @@ -1678,7 +1678,7 @@ knowledge: ### 当前问题 ```go -// adk/adapter.go:379-386 现状 +// 现状:initialMessages in internal/infrastructure/adk/adapter.go func initialMessages(instruction, userText string) []ports.ChatMessage { return []ports.ChatMessage{ {Role: ports.ChatRoleSystem, Content: instruction}, @@ -1920,7 +1920,7 @@ context: ### 当前问题 ```go -// adk/adapter.go:330-348 现状:parallel 与 loop 都走同一个串行 fan-out +// 现状:(*Agent).runFanOut in internal/infrastructure/adk/adapter.go,parallel 与 loop 走同一个串行 fan-out func (a *Agent) runFanOut(ctx context.Context, vars map[string]string) (string, error) { var parts []string for _, sub := range a.subAgents { // ← 串行 @@ -1934,7 +1934,7 @@ func (a *Agent) runFanOut(ctx context.Context, vars map[string]string) (string, | 问题 | 证据 | 影响 | |---|---|---| -| parallel 串行执行 | `adk/adapter.go:330-348` | 3 分支各 10s,实际 30s | +| parallel 串行执行 | `(*Agent).runFanOut` in `internal/infrastructure/adk/adapter.go` | 3 分支各 10s,实际 30s | | `max-iterations` 从未使用 | 同上 + `model/config.go` | loop 配置形同虚设 | | 无节点级重试 | 同上 | 一个分支抖动毁掉整条链 | | 无补偿/回滚 | 全局无 | 第 3 步失败,前 2 步的副作用留在系统里 | @@ -2576,7 +2576,7 @@ ai-api: | 问题 | 证据 | |---|---| -| 日志是 `fmt.Printf`,无 run_id/step_id | `adk/adapter.go:460-470`(`myTestPlugin`) | +| 日志是 `fmt.Printf`,无 run_id/step_id | `(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go`(`myTestPlugin`) | | 无 trace,跨 LLM/工具调用无法串联 | 全局 | | 无断言,模型答非所问无人发现 | 全局 | | 无评测,改动靠"感觉" | 零测试 | @@ -2877,9 +2877,9 @@ CREATE TABLE `token_ledger` ( | 问题 | 证据 | |---|---| | 无鉴权:任何人可调 `/chat` | `trigger/http/agent_handler.go` | -| CORS 反射任意 Origin + `Allow-Credentials: true` | `bootstrap/bootstrap.go:120-138`(D-19) | -| 无优雅停机:`kill` 即丢请求、MCP 连接不复位 | `bootstrap/bootstrap.go:93-102`(D-20) | -| `writeError(c, nil)` 会 panic | `agent_handler.go:102-109`(D-18) | +| CORS 反射任意 Origin + `Allow-Credentials: true` | `corsMiddleware` in `internal/app/bootstrap/bootstrap.go`(D-19) | +| 无优雅停机:`kill` 即丢请求、MCP 连接不复位 | `(*Engine).Run` in `internal/app/bootstrap/bootstrap.go`(D-20) | +| `writeError(c, nil)` 会 panic | `writeError` in `internal/trigger/http/agent_handler.go`(D-18) | | `.env.example` 含疑似真实密钥与推广链接 | `.env.example` | | 配置校验弱:`application.yaml` 不做 `${VAR}` 展开 | `app/config/application.go` | | 无健康检查、无指标端点 | 全局 | @@ -3133,7 +3133,7 @@ func (e *Error) HTTPStatus() int | 现状 | 改造后 | |---|---| -| `writeError(c, err)`(`agent_handler.go:102-109`,`err==nil` 会 panic) | `response.Error(c, err)`,内部 `nil` 保护 + `AppError` 类型断言 + 未知错误归一 | +| `writeError` in `internal/trigger/http/agent_handler.go` 未判 `err==nil`,会 panic | `response.Error(c, err)`,内部 `nil` 保护 + `AppError` 类型断言 + 未知错误归一 | | handler 里散落 `response.Fail(c, "0002", ...)` | 统一 `apperr.Invalid(...)` / `apperr.Forbidden(...)` 构造器 | | 引擎返回裸 `fmt.Errorf("tool %q: %w")` | `Failure` 结构(P1)→ 映射到 sub_code | diff --git a/docs/完善报告-评审与收编清单.md b/docs/完善报告-评审与收编清单.md index 92f646f..2e5d76c 100644 --- a/docs/完善报告-评审与收编清单.md +++ b/docs/完善报告-评审与收编清单.md @@ -32,20 +32,20 @@ | 编号 | 报告声明 | 核对结果 | 证据 | |---|---|---|---| -| P0-1 | CORS 反射 Origin + 允许凭证 | ✅ 属实 | `bootstrap.go:120-138`,`Access-Control-Allow-Origin: origin` + `Allow-Credentials: true` | -| P0-4 | `pending` 在提前返回/超时路径泄漏 | ✅ 属实 | `mcp_sse_client.go:402-411`:`c.pending[id] = ch` 之后才判 `postURL == ""` 并直接 return | -| P0-5 | `ensureStarted` 并发首次调用失败 | ✅ 属实 | `mcp_sse_client.go:66-78`:锁内置 `c.started = true`,锁外才 `openSSE`;并发者看到 true 直接返回,`postURL` 仍空 | -| P0-7 | `writeError(c, nil)` panic | ✅ 属实 | `agent_handler.go:102-109`:`errors.As(nil, ...)` 为 false 后走 `err.Error()` | -| P0-8 | 无优雅停机 | ✅ 属实 | `bootstrap.go:93-102` 直接 `e.Router.Run(addr)`;`main.go` 无 signal | -| P1-4 | ctx 未透传 | ✅ 属实 | `adapter.go:415/449/460` 三处 `context.Background()` | -| P1-13 | `adkRunnerPlugin` 是空壳 | ✅ 属实 | `adapter.go:557-570`:只判 `OnUserMessageCallback() != nil`,从未调用它 | +| P0-1 | CORS 反射 Origin + 允许凭证 | ✅ 属实 | `corsMiddleware` in `internal/app/bootstrap/bootstrap.go`,`Access-Control-Allow-Origin: origin` + `Allow-Credentials: true` | +| P0-4 | `pending` 在提前返回/超时路径泄漏 | ✅ 属实 | `(*MCPSSEClient).callRPC` in `internal/infrastructure/ai/mcp_sse_client.go`:`c.pending[id] = ch` 之后才判 `postURL == ""` 并直接 return | +| P0-5 | `ensureStarted` 并发首次调用失败 | ✅ 属实 | `(*MCPSSEClient).ensureStarted` in `internal/infrastructure/ai/mcp_sse_client.go`:锁内置 `c.started = true`,锁外才 `openSSE`;并发者看到 true 直接返回,`postURL` 仍空 | +| P0-7 | `writeError(c, nil)` panic | ✅ 属实 | `writeError` in `internal/trigger/http/agent_handler.go`:`errors.As(nil, ...)` 为 false 后走 `err.Error()` | +| P0-8 | 无优雅停机 | ✅ 属实 | `(*Engine).Run` in `internal/app/bootstrap/bootstrap.go` 直接 `e.Router.Run(addr)`;`main.go` 无 signal | +| P1-4 | ctx 未透传 | ✅ 属实 | `(*Runner).Run`、`(*Runner).Stream`、`(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go` 三处 `context.Background()` | +| P1-13 | `adkRunnerPlugin` 是空壳 | ✅ 属实 | `(*adkRunnerPlugin).OnUserMessage`/`BeforeAgent` in `internal/infrastructure/adk/adapter.go`:只判 `OnUserMessageCallback() != nil`,从未调用它 | | P1-14 | MCP 发现失败静默降级 | ✅ 属实 | `RegisterAndExpand` 里 `listFailures` 只写不读,失败时把配置名当工具返回 | | P2-19 | 三处库名/端口不一致 | ✅ 属实(且比报告更糟) | compose `ai-agent-scaffold`、yaml `ai-agent-scaffold-go`、`.env` `ai_agent_scaffold_go`;`.env` 还指向 3306/6379 | -| P2-23 | 流式未发 `Accept: text/event-stream` | ✅ 属实 | `openai_client.go:205` 恒为 `application/json` | +| P2-23 | 流式未发 `Accept: text/event-stream` | ✅ 属实 | `(*OpenAIClient).do` in `internal/infrastructure/ai/openai_client.go` 恒为 `application/json` | | P3-9 | `gofmt -l` 全量误报(CRLF) | ✅ 属实 | 无 `.gitattributes`;`bootstrap.go` 前 200 字节含 CRLF | -| P3-16 | 空列表响应缺 `data` 字段 | ✅ 属实 | `response.go:5-9` 的 `Data T \`json:"data,omitempty"\`` | -| P3-20 | `validateTable` 必填校验顺序随机 | ✅ 属实 | `loader.go:95-107` 遍历 `map[string]string` 报错 | -| P3-21 | 只有 `/healthz`,无 `/readyz` | ✅ 属实 | 全仓库仅 `bootstrap.go:114` 一处 | +| P3-16 | 空列表响应缺 `data` 字段 | ✅ 属实 | `Envelope` in `internal/api/response/response.go` 的 `Data T \`json:"data,omitempty"\`` | +| P3-20 | `validateTable` 必填校验顺序随机 | ✅ 属实 | `validateTable` in `internal/app/config/loader.go` 遍历 `map[string]string` 报错 | +| P3-21 | 只有 `/healthz`,无 `/readyz` | ✅ 属实 | 全仓库仅 `newRouter` in `internal/app/bootstrap/bootstrap.go` 一处 | | P1-11 | compose redis 引用未声明网络 | ✅ 属实 | `deployments/docker-compose.yml`,`voice-input-net` 未在顶层声明 | **抽查结论**:没有发现误报,也没有发现"看起来严重其实无害"的虚高条目。报告的判断力是可信的。