feat(ai): complete v3.2 risk watch agent

This commit is contained in:
2026-07-08 19:57:38 +08:00
parent deebc5404a
commit e949d0f5d3
10 changed files with 445 additions and 30 deletions

View File

@@ -132,22 +132,21 @@
**触发**
- `on_track` 不触发。
- `at_risk``likely_delayed``blocked` 自动触发。
- `attention` 在风险分明显上升、趋势连续上升、关键 Bug 增加、失败用例增加、阻塞增加、静默风险增加、置信度下降或预测发版日延后时触发
- `attention` 当前服务端只在临近发版且仍有未完成工作时触发;前端历史趋势策略保留为兼容展示,不作为 V3.2 服务端后台触发要求
**输出**`summary``why[]``forecast``recommendedReleaseWindow``suggestedActions[]``ownerHints[]`
**权限**:读规则结果和压缩证据;写 `xiaobao-risk-insights` 缓存。不修改 Version、Requirement、DevTask、TestCase、Bug、Member。
**权限**:读规则结果和压缩证据;写 `xiaobao_risk_insights` 关系表缓存(历史兼容名 `xiaobao-risk-insights`。不修改 Version、Requirement、DevTask、TestCase、Bug、Member。
**失败回退**AI 不可用时保留规则预警前端显示“规则预警已生成AI 解读会在触发条件满足时自动补充”。AI 失败不影响快照保存和规则风险展示。
### Agent 3Schedule Suggest Agent排期建议— 待规划
仅占位,正式规划见 roadmap.md V3.2
仅占位。本次 V3.2 不执行排期建议;后续如要做成员负载与历史耗时驱动的排期建议,需要先补独立规划
## 多 Agent 协作(V3.2 规划
## 多 Agent 协作(暂不执行
当前 V3.1 只有 Prototype Decompose Agent 单独运行
未来 Risk Watch / Schedule Suggest 引入后,编排策略另行设计。
当前 Prototype Decompose Agent 和 Risk Watch Agent 相互独立运行:原型拆解由用户在产品方案 Tab 触发Risk Watch 由小宝 summary 后台刷新后按 policy 入队。当前不做多 Agent 编排;未来 Schedule Suggest 引入后再另行设计
## 实现位置
@@ -285,3 +284,37 @@ AI 草案在 DevTask / TestCase 列表中的视觉区分:
- 标题旁紫色徽章「AI 草案」(`bg-purple-100 text-purple-600`
- 用户在详情抽屉里编辑保存任意字段后,徽章和左边线自动消失
- 无需求ID分组不额外显示“原型发现”等标记只在分组标题中展示 `无需求ID · {requirementName}`
## Business Analysis Agent规划
Business Analysis Agent 是只读业务数据分析 Agent不复用 Prototype Decompose Agent 的草案写入契约,也不复用 Risk Watch Agent 的单版本风险解释契约。
入口:
- `/wenfan-xiaobao`:业务数据分析对话。
- 产品、项目、版本详情页:带当前上下文的智能分析入口。
核心链路:
```text
Question + Context
-> Analysis Planner
-> Semantic Layer
-> Permission Scope Resolver
-> Analysis Strategy
-> Analysis Plan Processor
-> Metric Engine
-> Metric Result
-> ChartSpec / Insight / Report / Follow-ups
```
硬约束:
- AI 不写 SQL不直接查数据库。
- AI Planning 只生成 `AnalysisPlan` 建议,必须经系统校验和规范化。
- Agent 仅查询当前用户已有权限的数据,不允许自然语言绕过权限。
- 输出 ChartSpec 是平台统一契约,不是 ECharts option前端第一版用 ECharts Renderer。
- Metric Catalog 记录 metric version公式或业务口径变化必须升版本。
- 输出必须包含 Insight Card、图表、固定结构报告、可点击 Evidence 和只读 Follow-up。
视觉规范见 `docs/superpowers/specs/2026-07-08-business-analysis-agent-design.md` 的 AI Analysis Design System。

View File

@@ -213,7 +213,7 @@ V2.5 后端 mutation API 已接入服务端 RBAC
详细规范见 `agent-spec.md`。要点:
- AI Agent 不是一个独立服务,而是嵌在前端的"特定调用入口"。当前 V3.1 Prototype Decompose Agent。
- AI Agent 不是一个独立服务,而是嵌在前端/后台任务中的特定调用入口。当前 V3.1 Prototype Decompose Agent 与 V3.2 Risk Watch Agent 已落地
- Agent 写入数据时必须带 `aiDraft: true` 标记,列表中视觉区分(紫色边)。用户编辑后自动清除标记。
- DevTask / TestCase 加入 `references[]` 字段,记录任务/用例的来源(需求 / 原型批注。Agent 和人工创建均强制至少 1 条引用。
- 原型中有明确功能但没有匹配到关联需求时AI 可以生成无需求ID分组草案写入任务/用例的 `requirementName`,不创建 Requirement不加入关联需求列表。
@@ -281,7 +281,7 @@ The rule surface stays in pure frontend engines:
Managers with `xiaobao.warning:manage` can see all unfinished versions. Non-managers with `xiaobao.warning:view` can only see unfinished versions where the current user is in `version.members`.
AI explains rule results only. It writes interpretation cache to `xiaobao-risk-insights` and never mutates Version, Requirement, DevTask, TestCase, Bug, or Member data. Risk snapshots are saved to `xiaobao-risk-snapshots` when the page is opened.
AI explains rule results only. It writes interpretation cache to `xiaobao-risk-insights` / `xiaobao_risk_insights` and never mutates Version, Requirement, DevTask, TestCase, Bug, or Member data. Risk snapshots are saved to `xiaobao-risk-snapshots` when the page is opened.
V2.6 moves the current risk summary refresh to the server:
@@ -290,7 +290,7 @@ V2.6 moves the current risk summary refresh to the server:
- Domain writes that produce work activity already mark the affected version dirty; V2.6 also enqueues a deduped refresh job. Plain update/delete paths for version plans, dev tasks, test cases, and bugs explicitly mark the version dirty as well.
- `XiaobaoAiService` evaluates the refreshed summary and enqueues `xiaobao.ai.interpret` when policy allows. The worker reloads the latest summary, skips stale signatures, calls the existing `AiService.interpretRisk()` prompt path, and writes only `xiaobao_risk_insights`.
- AI interpretation cache uses the summary `riskSignature`, exact cache reuse, a six-hour cooldown, and risk-level escalation bypass. Until the server has full daily trend snapshots, `attention` summaries trigger server-side AI only when release is within one day and unfinished work remains.
- Frontend `/xiaobao-warning` still consumes V2.2 summary reads first and only falls back to AppData calculation when summaries are empty or unavailable.
- Frontend `/xiaobao-warning` consumes V2.2 summary reads first. The V2.2 response includes the latest generated relation-table AI insight when available, and the frontend prefers that insight over legacy AppData insight cache. It only falls back to AppData risk calculation when summaries are empty or unavailable.
Per-user warning read state is saved to `xiaobao-warning-views`. The read marker stores `userId + versionId + risk signature`, so the sidebar can turn the Xiaobao badge blue when any visible risk has a completed unread update, then return to the red risk-count badge after the user opens every updated warning. AI interpretation that is still generating only shows the "updating" notice and must not produce the blue update badge yet.
## V2.2 Partitioned Domain Data Layer (2026-07-03)
@@ -370,3 +370,41 @@ V2.7 uses stable server adapters for collaboration and governance modules:
When the later JWT/NextAuth server verification replaces the current header auth adapter, global permission sourcing should be swapped behind these auth/RBAC adapters; feature modules should keep depending on the adapter boundary.
Management overview reads only relation tables and summaries. It intentionally avoids AppData so it reflects the target backend boundary rather than the compatibility document store.
## V3 Business Analysis Agent Layer (planned)
Business Analysis Agent is a read-only AI analysis layer for business data conversations and context-page analysis. It does not mutate Product, Project, Version, Requirement, VersionPlan, DevTask, TestCase, Bug, Member, WorkActivity, TaskWorklog, Overtime, or Xiaobao data.
The analysis pipeline is:
```
User question + context
-> Analysis Planner
-> Semantic Layer
-> Permission Scope Resolver
-> Analysis Strategy
1. Template Strategy
2. Rule Composition Strategy (Deterministic)
3. AI Planning Strategy
-> Analysis Plan Processor (validate + normalize)
-> Metric Engine
-> Metric Result
-> ChartSpec Builder
-> Insight Engine
-> Report Builder
-> Follow-up Builder
-> UI Composition
```
Key boundaries:
- AI may propose an `AnalysisPlan`, but the system validates and normalizes it before execution.
- AI never writes SQL, never directly queries the database, and never widens user permissions.
- The Semantic Layer maps business language such as "忙", "压力", "风险", "延期", "效率", and "质量" to Metric Catalog definitions.
- Metric Catalog owns metric formula, owner, supported dimensions, supported analysis types, default time policy, and metric `version`. Formula changes require a metric version bump.
- Metric Engine returns renderer-agnostic `MetricResult`. Chart, insight, report, evidence, follow-ups, export, and future dashboard cards consume this result in parallel.
- Chart output uses a platform Unified ChartSpec. The first renderer is ECharts, but raw ECharts options are not the backend or Agent contract.
- Evidence is clickable structured proof with label, value, source domain, and optional drilldown filters.
- Reports have a fixed structure: Summary, Key Findings, Evidence, Suggestions, Data Scope.
The first product surfaces are `/wenfan-xiaobao` business-data conversation and product/project/version detail analysis entries. Visual design follows the AI Analysis Design System: Apple Vision style, large whitespace, large radius, light shadow, translucent material, restrained color, number-first cards, smooth line-area charts, rounded horizontal bars, and no BI big-screen styling.

View File

@@ -68,11 +68,15 @@ V2.4 已将高增长和核心业务领域从“AppData 主写 + 关系表同步
- V2.7.4 已新增管理驾驶舱和治理设置,聚合关系表指标并维护治理字典。
- V2.7.5 已新增协作治理 RBAC/audit adapter避免新增模块绕开服务端权限和审计边界。
- V2.8 新增运维交付物集中在 `scripts/``.github/workflows/deploy-production.yml``deploy/monitoring/``docs/runbooks/``docs/deployment.md``docs/production-readiness.md`
- V3.1 Prototype Decompose Agent 已落地:后端 `/api/v1/ai/decompose`、前端产品方案 Tab 拆解入口、对账报告、去重、采纳草案、AI 估时、推荐负责人和无需求ID分组均已接入。
- V3.2 Risk Watch Agent 已落地:服务端小宝 summary 刷新后按 policy 排入 `xiaobao.ai.interpret`AI 解读只写 `xiaobao_risk_insights`V2.2 小宝快读返回最新 generated insight前端优先展示关系表 AI 解读。
- AppData 退场、RBAC/审计、性能、小宝后台化、协作治理和生产发布都通过 production readiness 证据项追踪,避免把运维稳定版误当成一次性口头验收。
### 已完成(按时间倒序)
**2026-07-08**
- V3.2 completed Risk Watch Agent read closure: `/api/v1/v2.2/xiaobao-warning` now attaches the latest generated relation-table AI insight, and the frontend merges relation-backed insights ahead of legacy AppData insight cache.
- V3.1 status corrected to completed: prototype decomposition already has backend AI calls, frontend decomposition buttons, report modal, dedupe, adoption, AI estimates, assignee recommendations, and no-requirement grouping.
- V2.8 added PostgreSQL backup, fresh DB restore with explicit overwrite confirmation, and `server_data` volume backup automation.
- V2.8 added release smoke suite and wired GitHub Actions deployment verification to runtime version, frontend root, products, V2.2 read path, and AI config checks.
- V2.8 added optional monitoring profile with Prometheus, Grafana, Loki, Promtail, postgres-exporter, node-exporter, cAdvisor, and blackbox-exporter.
@@ -239,11 +243,11 @@ V2.5 完成后的保留边界:`GET /api/v1/data/:key` 仍可读历史 JSONX
详细 Agent 规范见 `agent-spec.md`。本节只列规划,不重复 Agent 实现细节。
### V3.1 — Prototype Decompose Agent首个 Agent
### V3.1 — Prototype Decompose Agent已完成
**目标**:从产品方案的原型 + 关联需求,拆解出开发任务草案 + 测试用例草案原型中明确可拆但没有匹配到关联需求的内容按无需求ID分组进入任务/用例,不补需求池。
**已完成的数据底座**2026-06
**已完成**
- DevTask / TestCase 加 references[] + aiDraft + aiDraftAt
- 创建表单加「原型批注」字段
- 列表中 AI 草案视觉区分(紫色边 + 徽章)
@@ -251,35 +255,71 @@ V2.5 完成后的保留边界:`GET /api/v1/data/:key` 仍可读历史 JSONX
- agent-spec.md / glossary.md 文档落地
- 约定:原型链接 = 产品方案 (VersionPlan type=product) 已完成计划的 resultUrl不在 Version 上独立存储
- V2.4 已完成 DevTask/TestCase/Bug 的 `versionId` 版本主归属和领域主写AI 写入可直接走版本级领域 API。
**待实现**
1. 后端 `AiGateway` + `PrototypeDecomposeService`NestJS module
2. 前端「AI 拆解任务和用例」按钮(产品方案 Tab
3. 对账报告组件(弹窗呈现:完美对应 / 需求未见原型 / 无需求ID分组 / 含糊)
4. 用户确认后批量创建 DevTask + TestCase 草案
5. AiLog 表调用记录、token 计量、用时)
- 后端 `AiGateway` / `AiService.decompose` / `POST /api/v1/ai/decompose` 已落地。
- 前端产品方案 Tab 已有「AI 拆解开发任务 / AI 拆解测试用例」入口。
- 对账报告弹窗、重复草案过滤、用户确认后批量创建 DevTask/TestCase 草案已落地。
- AI 估时、推荐负责人、无需求ID分组和任务类型自动入库策略已落地。
- `AiLog` 表已建;调用级日志目前通过返回 `meta`、计划日志和运维慢请求/队列监控追踪,后续若要完整审计每次 AI 调用再单独接写入。
**MVP 范围限制**
- 不自动分配 assignee留给用户在草案上手填
- 不做"上一版基准 diff"(按 decisions.md #17 决议)
- 单 Agent 单 Round不做多 Agent 编排
### V3.2 — 小宝预警 / Risk Watch Agent
### V3.2 — 小宝预警 / Risk Watch Agent(已完成)
小宝预警以版本发版风险为核心,先通过规则引擎计算风险分、趋势、静默风险、预计可发日期和置信度,再由 Risk Watch Agent 自动解释高风险版本。第一版已落地页面触发模式:打开 `/xiaobao-warning` 时保存当天快照,并在满足触发条件时自动生成 AI 解读。
小宝预警以版本发版风险为核心,先通过规则引擎计算风险分、趋势、静默风险、预计可发日期和置信度,再由 Risk Watch Agent 自动解释高风险版本。
当前不做后台定时 Agent。后续如果需要主动通知再在已有 `xiaobao-risk-snapshots``xiaobao-risk-insights` 基础上增加定时巡检与消息推送。
当前完成边界:
- 领域写入标记版本风险 summary dirty并排入 `xiaobao.summary.refresh` 后台任务。
- `XiaobaoRiskService.refreshSummary()` 从关系表重算确定性风险结果,并交给 `XiaobaoAiService.evaluateSummary()` 按 policy 判断是否入队。
- `xiaobao.ai.interpret` worker 重新读取当前 summary跳过过期 signature调用 `AiService.interpretRisk()`,只写 `xiaobao_risk_insights` 缓存。
- 触发策略:`on_track` 不触发;`at_risk``likely_delayed``blocked` 自动触发;`attention` 当前服务端只在临近发版且仍有未完成工作时触发。
- 缓存策略exact signature 复用、6 小时 cooldown、风险等级升级可绕过 cooldown。
- V2.2 小宝快读返回最新 generated insight前端优先展示关系表 AI 解读,再兼容旧 AppData insight cache。
**Schedule Suggest Agent**:基于成员负载和历史耗时,建议下一阶段任务分配,仍作为后续候选
当前不继续做后台定时巡检或额外主动推送;已有 `risk_alert` 通知保持现状。若后续需要“主动每日巡检 + 消息推送”,另起阶段,不塞进 V3.2
**多 Agent 协作设计**:等 Risk Watch 与 Schedule Suggest 都稳定后再设计编排策略
**Schedule Suggest Agent**:基于成员负载和历史耗时,建议下一阶段任务分配,仍作为后续候选,本次不执行
### V3.3 — 其他场景候选
**多 Agent 协作设计**:本次不执行。等未来确实引入排期 Agent 后再设计编排策略。
### V3.3 — 其他场景候选(暂不执行)
1. 需求智能分类(自动归档到产品 / 项目)
2. 健康度智能解读(数据指标 → 自然语言报告)
3. 需求转任务(需求采纳后一键生成 DevTask 草稿)
### V3.4 — Business Analysis Agent下一阶段设计已确认
**目标**:把 `/wenfan-xiaobao` 从内置帮助问答升级为业务数据分析对话,并在产品、项目、版本详情页提供带上下文的智能分析入口。用户可以围绕产品、项目、版本、需求、部门和成员进行单维或多维提问,系统返回 Insight Card、图表、分析报告、可点击证据和连续追问。
**设计边界**
- 只读 Agent不修改业务实体、不创建草案、不触发状态流转。
- 使用 Semantic Layer 和 Metric Catalog 承接自然语言到业务指标的映射。
- Metric Catalog 必须记录 metric version便于未来算法口径变化后复现历史分析。
- Analysis Strategy 分为 Template Strategy、Rule Composition StrategyDeterministic和 AI Planning Strategy。
- AI Planning 只生成 `AnalysisPlan` 建议Analysis Plan Processor 校验并规范化后Metric Engine 才能执行。
- Metric Engine 输出 `MetricResult`ChartSpec Builder、Insight Engine、Report Builder、Follow-up Builder 并行消费。
- ChartSpec 为平台统一契约,前端第一版用 ECharts Renderer不把 ECharts option 暴露为后端契约。
- AI Analysis Design System 采用 Apple Vision 风格:大留白、大圆角、轻阴影、半透明材质、数字优先、折线面积渐变、横向圆角柱状、少颜色、无大屏炫光。
**MVP 模板候选**
1. 版本风险排行
2. 项目/版本完成趋势
3. 逾期事项分布
4. 产品需求状态分布
5. 需求完成趋势
6. 需求来源/类型占比
7. 部门负载排行
8. 成员待办排行
9. 成员投入排行
10. Bug 严重度分布
11. 测试通过率趋势
12. 加班原因占比 / 加班排行
完整设计见 `docs/superpowers/specs/2026-07-08-business-analysis-agent-design.md`
### 落地约束
- **不直接动数据**:所有 Agent 写入必须带 `aiDraft: true`,用户编辑后才转正

View File

@@ -339,9 +339,9 @@ Implementation convention:
- `xiaobao.warning:manage`:查看所有未结束版本的预警。
- `xiaobao.warning:view`:仅查看当前用户在 `version.members` 中的未结束版本。
V2.6 后小宝当前 summary 由服务端后台刷新:领域写入标记 `xiaobao_risk_summaries.dirty=true` 并排入 `xiaobao.summary.refresh`worker 从版本下的计划、开发任务、测试用例、Bug、日报和工作活动重新计算风险。页面仍可用前端 `buildXiaobaoWorkItems` 做兼容聚合和快照保存,但默认优先读取 V2.2 summary。
V2.6/V3.2 后小宝当前 summary 由服务端后台刷新:领域写入标记 `xiaobao_risk_summaries.dirty=true` 并排入 `xiaobao.summary.refresh`worker 从版本下的计划、开发任务、测试用例、Bug、日报和工作活动重新计算风险。页面仍可用前端 `buildXiaobaoWorkItems` 做兼容聚合和快照保存,但默认优先读取 V2.2 summary。
AI 解读不由人工按钮触发。服务端 summary 刷新后按 policy 排入 `xiaobao.ai.interpret``at_risk``likely_delayed``blocked` 自动触发;`attention` 当前服务端只在临近发版且仍有未完成工作时触发,页面完整趋势策略仍保留作为兼容。缓存命中时复用解读;同版本最近 6 小时内已有解读时进入 cooldown不重复请求风险等级升级时可绕过缓存保存时间使用服务端写入时间不信任模型返回的 `generatedAt` 作为缓存新鲜度。
AI 解读不由人工按钮触发。服务端 summary 刷新后按 policy 排入 `xiaobao.ai.interpret``at_risk``likely_delayed``blocked` 自动触发;`attention` 当前服务端只在临近发版且仍有未完成工作时触发,页面完整趋势策略仍保留作为兼容。缓存命中时复用解读;同版本最近 6 小时内已有解读时进入 cooldown不重复请求风险等级升级时可绕过缓存保存时间使用服务端写入时间不信任模型返回的 `generatedAt` 作为缓存新鲜度。V2.2 小宝快读会带出当前版本最新 generated 关系表解读,前端优先展示这份解读,再兼容旧 `xiaobao-risk-insights` AppData 缓存。
静默风险包括长期无更新、无日报、无活动、进行中事项无人处理等信号。日报和工作活动是风险解释的重要证据,必须进入 AI 解读输入。
@@ -360,6 +360,43 @@ AI 解读不由人工按钮触发。服务端 summary 刷新后按 policy 排入
管理驾驶舱 `/admin/management` 只查关系表和小宝 summary不读取 AppData并通过 RBAC adapter 校验 `management:view`。治理设置 `/admin/governance` 集中维护任务类型与需求字典;使用中的字典不可硬删,字典变更必须写 audit并通过 RBAC adapter 校验 `governance:manage`
## Business Analysis Agent 工作流(规划)
Business Analysis Agent 用于 AI 助手业务数据对话,以及产品/项目/版本详情页的上下文分析入口。它只读关系表和 summary不写业务数据。
标准流程:
1. 用户在 `/wenfan-xiaobao` 或详情页输入问题。
2. 前端提交问题和上下文:`surface``productId``projectId``versionId`
3. 后端 Analysis Planner 识别用户意图。
4. Semantic Layer 将自然语言映射成系统语义、指标、维度、分析类型和时间意图。
5. Permission Scope Resolver 根据当前用户权限和页面上下文收窄数据范围。
6. Analysis Strategy 生成分析计划:
- Template Strategy 优先命中固定高频模板。
- Rule Composition Strategy 用确定性规则组合已知指标、维度和时间口径。
- AI Planning Strategy 只生成计划建议,不直接执行。
7. Analysis Plan Processor 校验权限、指标、维度、聚合、时间范围、Top N 和筛选条件,并规范化为统一 `AnalysisPlan`
8. Metric Engine 执行关系表查询和聚合,返回 `MetricResult`
9. ChartSpec Builder、Insight Engine、Report Builder、Follow-up Builder 并行消费 `MetricResult`
10. 前端展示 Insight Card、ECharts 图表、固定结构报告、可点击 Evidence 和只读 Follow-up。
时间口径:
- 用户明确说本月、季度、年份、最近 N 天时,按用户指定。
- 当前状态或风险问题不默认套近 30 天,使用实时状态、未完成事项、小宝 summary、阻塞、Bug、测试完成率和发版日期。
- 趋势、吞吐、效率、投入和变化类问题,用户未指定时间时默认近 30 天。
- 对比类问题未指定基准时,用近 30 天对比前 30 天。
- 生命周期类问题按对象生命周期统计。
- 每次回答必须显示统计范围、数据截止时间、权限范围和时间口径类型。
无数据时返回 No Data 结果,不生成虚假解释。低语义置信时先说明系统默认解释,必要时给用户可选追问。
视觉工作流:
- 结果先显示一句 Insight Card再显示图表和报告。
- 图表使用平台 Unified ChartSpec通过前端 ECharts Renderer 渲染。
- 图表遵循 AI Analysis Design SystemApple Vision 风格、大留白、大圆角、轻阴影、半透明材质、数字优先、折线面积渐变、横向圆角柱状、少坐标、少颜色、无大屏炫光。
## 日期选择与计划时间
- 调研、产品方案、UI 设计、开发任务、测试用例、Bug 创建时使用统一工作日日期时间选择器。