Files
planet/docs/plans/phase1-compute-revision-plan.md
T
linkong e1cc27f13d
ci / backend (push) Canceled after 0s
ci / frontend (push) Canceled after 0s
ci / delivery (push) Canceled after 0s
release: bump version to 0.84.0
2026-12-04 07:07:19 +08:00

477 lines
36 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Planet 一期执行计划:一条算力数据修订如何走到地球上
## 状态
**已落地实现并验收(M0–M4 全部完成,待随新版本归档发布):**
- **M0**:已采集真实 CAICT 算力数据修订样例并固化至 `backend/tests/fixtures/compute_revision_case.json`。
- **M1**:新增不可变版本台账表 `compute_configuration_revisions`(迁移 `0007_add_compute_configuration_revisions.py`)、启动基线封存 `ensure_compute_revision_baseline`、`publish_id` 乐观锁(消除 ABA 冲突)、恢复默认新增 `action="reset"` 版本记录,以及 `as_of` / `publish_id` 系统发布时间轴回溯。
- **M2**:实现候选提取去重(`dedupe_fingerprint`)、逐国旧值→新值→差值对比与统计期冲突识别、采纳/驳回强制理由代码与说明、逐国来源 `value_sources` 保护及导入草稿冲突二次确认。
- **M3**:实现与 BGP 解耦的 `publish_id` 驱动算力修订演播(`compute-revision-notice`)及失败状态持久化、启动/定时/手动补播闭环。
- **M4**:实现地球算力卡片与参谋工作台同一 `publish_id` 绑定、历史发布版本切换、只读血缘推导抽屉(`/api/v1/data-products/compute/lineage` 公共访问自动脱敏),以及中英文技术文档与回归单测(`test_compute_revisions.py`、`test_compute_m2_candidates_and_sources.py`、`test_compute_m3_presentations.py`、`test_compute_m4_lineage_and_copilot.py`)。
以下为原始执行设计文档,保留作为历史设计上下文。
> 本文件写给执行代理(Gemini Neon)。照本文件执行,遇到与代码现状不符的地方,以代码为准并在里程碑报告中说明。
> 仓库路径:`/home/ray/dev/linkong/planet`(Windows 侧为 `\\wsl.localhost\Ubuntu\home\ray\dev\linkong\planet`)。
---
## 0. 执行前必读
### 0.1 先读这些文件(仓库规则要求)
1. `rules.md`(必载 `core` / `security` / `workflow`,涉及 Admin/Earth UI 时加载 `uiux`)
2. `AGENTS.md`
3. `CODEMAP.md`、`docs/HARNESS.md`
4. 文档改动前读 `docs/documentation-coverage-rules.md`
开工前运行:
```bash
git status --short
scripts/harness/doctor.sh
```
保留工作区里已有的用户改动,不要覆盖或回退。
### 0.2 硬性规则
- 前端只用 Bun,禁止 npm / pnpm / yarn。
- 本地前端开发用 Vite(`./planet.sh start` / `./planet.sh restart -f`),不要构建或运行静态 Nginx `planet_admin` 容器。
- 后端用结构化日志(`get_logger` + `info_event` / `warning_event`),禁止 `print()`,禁止 `except Exception: pass` 吞异常。
- 不要修改已存在的迁移文件(迁移运行器会校验文件 checksum,见 `backend/app/db/migrations.py`)。新迁移用下一个空闲编号(当前最新应为 `0006`,先 `ls backend/app/db/migration_steps/` 确认)。
- 不要 `git commit` / `git push`,除非用户明确要求。
- 不要新增外部数据源、不要占用数据源 ID(39 以后留给二期)。
- 不要编造报告数据。M0 找不到真实修订案例时,停下来报告,不要自造数字。
### 0.3 一期目标(用户能看到的结果)
领导从地球上一个刚被修订的算力数字出发,能当场查到:
1. **哪一年、哪个口径变了**,旧值 → 新值,差多少个百分点,并明确标注是“历史数据修订”,不是“今天算力跃升”;
2. **依据**:哪份报告、哪一页、报告发表日期;
3. **谁确认的**:复核人、理由、发布时间(公开页面脱敏);
4. **切回发布前**:看到当时系统里的数字和结论。
### 0.4 本期明确不做(Non-goals)
- 不接飞机、GPS 干扰、灾害等任何新数据源。
- 不建通用本体表 / `lineage_links` 关系表(关系一律读取时从现有数据推导,遵守 `backend/app/services/ai/entity_resolver.py` 第一行的“zero duplicate mirror tables”原则)。
- 不做通用的 `/ai/workflows/{id}/evaluate` 接口。
- 不做双人复核开关,不新增角色。现有角色只有 `viewer` / `admin` / `super_admin`(`backend/app/core/enums.py`)。
- 不放开同一 `(metric, year)` 多条观测并存(`ComputeConfiguration.consistent_evidence` 的唯一性约束保持不变)。
- 不改 `classify_snapshot_delta` 和 BGP 主动研判逻辑。
- 不拆除 `compute-topic.js`,不改 Copilot 打开时关闭算力卡片的现有互斥(`main.js` 约 L4659)。
- 不重构 Earth 渲染层。
### 0.5 两根时间轴(全期必须区分,命名不可混用)
| 名称 | 参数 | 含义 | 已有/新增 |
| --- | --- | --- | --- |
| 报告发表时间轴 | `time_basis="as_known_then"` + `cutoff_at` | 按报告 `published_at` / `report_year` 过滤事后报告 | 已有(`tool_registry.filter_compute_config_by_cutoff`) |
| 系统发布时间轴 | `as_of`(ISO 时间)或 `publish_id` | 取 `published_at <= as_of` 的那一版已发布台账 | **本期新增** |
两者可以叠加:先按 `as_of` 取当时的台账版本,再按 `cutoff_at` 过滤报告。UI 文案:前者叫“报告发表截至”,后者叫“系统发布版本”。
---
## 1. 现状关键事实(已核对,执行时再确认一遍)
| 位置 | 现状 | 本期要解决的问题 |
| --- | --- | --- |
| `backend/app/services/compute_configuration.py` | 配置存在 `SystemSetting(category="compute_comparison")`;`revision` 既是版本号又是乐观锁 token;恢复默认复用 `DEFAULT_REVISION`(默认文件哈希)并 `db.delete(record)` | A→B→A 后旧 token 重新生效(ABA),旧页面能静默覆盖;恢复默认后丢失历史 |
| `read_configuration()` | 唯一读入口,调用方:`compute_intelligence.py` 的 `/series` 与 `/configuration`、`tool_registry.query_compute_posture_tool` | 加 `as_of` / `publish_id` 支持只改这一处 |
| `backend/app/api/v1/compute_intelligence.py` L107–117 | 发布后调用 `process_compute_config_proactive_delta`,异常被 `except Exception: pass` 吞掉 | 演播失败无痕迹、不补播 |
| `backend/app/services/ai/proactive_pipeline.py` L290–356 | 算力演播只比较 3 个标量(`cn_total_share` 等),经 `classify_snapshot_delta` 判定;该函数与 BGP(L211)共用 | 2.0pp 阈值漏判小幅修订;`seen_fingerprints` 会把 B→A 恢复当成重复吞掉;改它会影响 BGP |
| `backend/app/schemas/compute_configuration.py` `ComputeObservation` | 一行一个 `source_id`,`values` 是多国字典 | 只修订中国时无法让美国保留原来源 |
| `frontend/src/admin/pages/compute-comparison-model.ts` L290–302 `applyApprovedCandidateToDraft` | 合并 `values` 并把整行 `source_id` 改成新报告 | 美国的值被错误地挂到新报告上 |
| `backend/app/models/situational_assessment.py` `ComputeEvidenceCandidate` | 只有 `status / proposed_payload / source_payload / reviewed_by / reviewed_at` | 没有逐国对比、理由、去重、发布关联 |
| `backend/app/api/v1/situational_awareness.py` `CandidateReviewRequest`(约 L192) | 只有 `decision` | 采纳/驳回没有理由 |
| `backend/app/services/ai/semantic_delta.py` `extract_compute_candidates_deterministic` | 正则只认 美国/中国/日本 + “份额 xx%”;每个指标出一条多国候选 | 真实报告可能需要扩展(由 M0 决定) |
| `backend/app/services/ai/workflow_runtime.py` 约 L2233 | 无 `frozen_series` 的旧记录返回 `incomplete_snapshot` | 复用该提示处理“基线之前”的回溯 |
| `stage_presentations` 表 | 已接入 `earth_data_change_events` outbox(`earth_layer_adapters.py`);前端 `stage-narrative-player.js` 有按 `(presentation_key, time_basis, revision)` 去重的队列和相机主权状态机 | 演播推送和去重复用它,不另建队列 |
---
## 2. 里程碑总览
| 里程碑 | 内容 | 完成标志 | 是否需要用户确认后继续 |
| --- | --- | --- | --- |
| **M0** | 找到真实修订案例,做成测试数据 | 有带页码出处的真实“旧值→新值”案例 fixture | **是**(找不到就停) |
| **M1** | 版本表成为唯一来源;修 ABA;恢复默认改为插入新版本;`as_of` 回溯 | A→B→A 三个时点读到 A、B、A;旧 token 返回 409 | 否,报告后继续 |
| **M2** | 候选逐国对比、必填理由、去重;`value_sources` 逐国来源;导入逻辑修复 | 只修订中国时美国仍引用原报告 | 否 |
| **M3** | 演播由 `publish_id` 驱动、失败可补播、与 BGP 解耦 | 中断后补播一次;重复事件不重播;B→A 恢复会播 | 否 |
| **M4** | 卡片/参谋带同一 `publish_id`;版本切换;血缘抽屉;文档 | 端到端演示 9 条验收全部通过 | 交付 |
每个里程碑结束输出一份简短报告(格式见第 8 节)。
---
## 3. M0:找到真实修订案例
### 3.1 任务
1. 读 `backend/app/services/compute_history_defaults.json` 的 `sources` 与 `observations`,列出每份报告覆盖的年份与指标。
2. 查找同一 `(metric, year)` 被后一份报告给出不同数值的真实案例。优先检查:
- CAICT《先进计算暨算力发展指数蓝皮书(2025年)》是否重新给出 2023 年全球份额、全球总规模或中国总规模,与 2024 年版(2023 年美 41% / 中 31%,全球 1397 EFlops,中国 435 EFlops)不同;
- CAICT 各年白皮书对前一年中国算力总规模(EFlops)的修订;
- Epoch AI 数据集对既有集群样本份额的修订。
可以使用网页搜索和抓取公开 PDF;记录 URL、页码 / 图表号、原文摘录。
3. 判断该案例属于哪个 `metric`:`global_share` / `intelligent_share` / `absolute` / `frontier_cluster_share` / `chip_inflow_share`,以及是否涉及 `period_label` 或 `global_total` 变化。
4. 用真实摘录测试 `extract_compute_candidates_deterministic` 能否提取。不能提取时,记录需要扩展的规则(例如 `absolute` 的“总规模 XXX EFlops”、全球总量),在 M2 实现,范围只限这个案例需要的写法。
### 3.2 产出
- `backend/tests/fixtures/compute_revision_case.json`:
```json
{
"case_id": "caict-2025-restates-2023-...",
"metric": "absolute",
"year": 2023,
"period_label": null,
"old": {"source_id": "caict-2024", "values": {"CN": 435}, "locator": "..."},
"new": {
"source_id": "...", "title": "...", "url": "https://...", "publisher": "...",
"report_year": 2025, "published_at": "2026-03-06",
"locator": "正文 xx 页 图 x",
"excerpt": "原文摘录(不超过 5000 字)",
"values": {"CN": 0}
},
"unchanged_codes": ["US"],
"notes": "说明为何判定为修订而非口径不同"
}
```
- M0 报告:案例出处、判断依据、抽取器是否需要扩展。
### 3.3 停止条件
找不到真实修订案例时**停止**,向用户报告并给出选项:
- (a) 使用明确标注为“演练数据”的案例(演示时全程显示“演练”标签,不能当真实数据展示);
- (b) 换用其他指标或其他机构的报告;
- (c) 调整一期演示方式。
不要自行选择 (a)。
---
## 4. M1:版本表与时间回溯
### 4.1 数据模型
新增模型 `ComputeConfigurationRevision`,放在 `backend/app/models/compute_configuration_revision.py`,并在 `backend/app/db/session.py`(或项目注册模型的位置)引入。
表 `compute_configuration_revisions`:
| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `id` | Integer PK autoincrement | 单调递增,用于排序和演播 revision |
| `publish_id` | String(64) unique not null | 每次发布 / 恢复唯一;同时是乐观锁 token |
| `parent_publish_id` | String(64) nullable | 上一版 |
| `action` | String(16) | `baseline` / `publish` / `reset` |
| `content_fingerprint` | String(64) index | 配置内容的规范 JSON SHA-256,仅用于判断内容是否变化 |
| `is_default_content` | Boolean | 内容是否等于内置默认台账 |
| `config_payload` | JSON | 完整台账快照 |
| `diff_summary` | JSON list | 相对上一版的变化(格式见 4.3) |
| `source_candidate_ids` | JSON list[int] | 本次发布包含的已采纳候选 |
| `publish_note` | Text default "" | 发布说明 |
| `published_by_id` / `published_by_name` / `published_by_role` | int / String / String,nullable | 发布人 |
| `published_at` | DateTime(tz) index not null | 发布时间 |
| `presentation_status` | String(16) | `pending` / `delivered` / `failed` / `skipped` |
| `presentation_attempts` | Integer default 0 | |
| `presentation_error` | Text default "" | |
| `presentation_delivered_at` | DateTime(tz) nullable | |
迁移 `backend/app/db/migration_steps/00NN_add_compute_configuration_revisions.py`:
- 幂等建表与索引,兼容 PostgreSQL 与 SQLite(参照 `0006_add_ai_workflow_bindings.py` 的方言分支写法);
- 同一迁移里给 `compute_evidence_candidates` 加 M2 需要的列(见 5.1),避免两次迁移;
- 迁移只建结构,不在迁移里写基线数据。
基线封存:新增 `ensure_compute_revision_baseline(db)`(放在 `compute_configuration.py`),幂等:
- 表里已有记录 → 直接返回;
- 否则读取当前 `SystemSetting` 行(没有则用默认台账),插入一条 `action="baseline"` 的记录;`publish_id` 沿用当前 `envelope["revision"]`,这样已打开的后台页面不会因为升级而报冲突;
- 在应用启动初始化处调用(用 `rg -n "ensure_builtin_workflows" backend/app` 找到现有启动初始化位置,放在一起)。
### 4.2 读写改造(`backend/app/services/compute_configuration.py`)
**唯一事实来源规则**:版本表是历史和当前版本的来源;`SystemSetting` 保留为“当前版本缓存”,和版本表在**同一事务**内写入,内容必须一致,`envelope["revision"] == 当前 publish_id`。
1. `publish_configuration(...)` 改为:
- 按现有方式加行锁,比较 `payload.revision` 与当前 `publish_id`,不一致 → 409;
- 计算新内容 `content_fingerprint`;与当前版本相同 → 不建新版本,返回当前 envelope,并在响应中带 `no_changes: true`;
- 计算 `diff_summary`;
- 插入新版本行:`publish_id = uuid4().hex`(恢复默认也生成新的 uuid,**不再复用 `DEFAULT_REVISION`**),`presentation_status` 初始为 `pending`(`diff_summary` 中没有观测值变化时为 `skipped`);
- 恢复默认:`action="reset"`,内容为默认台账,**更新** `SystemSetting`(`is_default=True`),不再 `db.delete(record)`;
- 写 `AuditLog`(保留现有字段,`details` 增加 `publish_id`、`parent_publish_id`、`diff_summary` 摘要),一次 `commit`。
2. `read_configuration(db, *, as_of: datetime | None = None, publish_id: str | None = None) -> dict`:
- 两者都没有 → 保持现有行为(读 `SystemSetting`,没有则默认);
- 有 `publish_id` → 读该版本,不存在 → 404;
- 有 `as_of` → 读 `published_at <= as_of` 的最新版本;没有(早于基线)→ 返回 `{"snapshot_unavailable": True, ...}`,调用方据此返回“历史快照不完整”提示(复用 `workflow_runtime.py` 中 `_INCOMPLETE_COMPUTE_REPLAY_NOTICE` 的文案风格);
- 返回的 envelope 增加:`publish_id`、`published_at`、`action`、`is_default`。
3. 新增 `list_configuration_revisions(db, limit=20)`,返回 `id / publish_id / action / published_at / is_default_content / 变化摘要(条数 + 首条描述)/ presentation_status`。
### 4.3 `diff_summary` 格式
新增纯函数 `diff_compute_configurations(old: ComputeConfiguration, new: ComputeConfiguration) -> list[dict]`(放在 `backend/app/services/compute_revision_diff.py`),只比较 `observations`,其他部分只记数量变化:
```json
[
{
"kind": "observation_value",
"metric": "global_share",
"year": 2024,
"period_label": null,
"code": "CN",
"old_value": 23.0,
"new_value": 24.5,
"delta": 1.5,
"change_kind": "historical_revision",
"old_source_id": "caict-2025",
"new_source_id": "report-b"
},
{"kind": "observation_row_added", "metric": "...", "year": 2025, "period_label": null},
{"kind": "observation_row_removed", "metric": "...", "year": 2019, "period_label": null},
{"kind": "period_label_changed", "metric": "...", "year": 2025, "old": "2025H1", "new": "2025"},
{"kind": "section_changed", "section": "milestones", "added": 1, "removed": 0, "modified": 0}
]
```
- `change_kind`:旧值存在且不同 → `historical_revision`;旧值不存在 → `new_observation`;值相同只换来源 → `source_reattributed`。
- 任何非零差值都要记录,**不设 2.0pp 阈值**。
- 来源按 M2 的 `value_sources` 规则取逐国来源(M1 阶段还没有 `value_sources` 时取行级 `source_id`,M2 再补)。
### 4.4 接口
- `PUT /api/v1/data-products/compute/configuration`:`ComputeConfigurationWrite` 增加 `publish_note: str = ""`(max 1000)、`source_candidate_ids: list[int] = []`(max 50)。响应增加 `publish_id`、`no_changes`、`presentation_status`。
- `POST /api/v1/data-products/compute/configuration/reset`:响应同上。
- 新增 `GET /api/v1/data-products/compute/revisions?limit=20`:权限同 `/configuration`(`configuration_admin`),返回 `list_configuration_revisions`。
- `GET /api/v1/data-products/compute/series` 与 `GET /api/v1/situational-awareness/posture/compute`:新增可选参数 `as_of`、`publish_id`,响应增加 `publish_id`、`published_at`、`as_of`、`snapshot_unavailable`。
- `query_compute_posture_tool(...)` 增加 `as_of` / `publish_id` 参数并透传给 `read_configuration`;`result["version"]` 改为 `publish_id`。
- `posture_agent.compute_posture_fingerprint` 已包含 `series_payload["version"]`,改成 `publish_id` 后,不同版本自然生成不同研判缓存;确认 `generate_or_get_compute_posture` 的 `audit_meta` 写入 `publish_id`。
### 4.5 前端(Admin)
`frontend/src/admin/pages/ComputeComparisonSettings.tsx` 与 `compute-comparison-model.ts`:
- 保存时继续传 `revision`(值即当前 `publish_id`);收到 409 时保持现有“配置已变化,请重新加载”提示;
- 新增“发布说明”输入框(可选;M2 中在混源份额行存在时变为必填);
- 新增“修订历史”区块:调用 `/revisions`,按时间列出版本(动作、时间、发布人、变化摘要、演播状态)。遵守 `rules.md` 的 `uiux` 与 Admin 控件规范,运行 `scripts/harness/frontend-rules-check.sh`。
### 4.6 M1 测试(`backend/tests/test_compute_configuration.py` 或新文件 `test_compute_revisions.py`)
1. **A→B→A 历史还原**:基线 A → 发布 B → 恢复 A,生成 3 条版本,`publish_id` 各不相同;`as_of` 分别取三个时点之后,读到 A、B、A。
2. **ABA 并发冲突**:拿基线 token `T0` 的客户端,在 B 发布、恢复 A 之后用 `T0` 提交 → 409。
3. **无变化发布**:内容与当前相同 → `no_changes: true`,不新增版本。
4. **基线幂等**:多次调用 `ensure_compute_revision_baseline` 只有一条基线;基线 `publish_id` 等于升级前的 `revision`。
5. **早于基线的 `as_of`**:返回 `snapshot_unavailable: true`。
6. **diff**:1.5pp 修订被记录为 `historical_revision`;新增年份为 `new_observation`。
7. 迁移在 SQLite 与 PostgreSQL 路径下都能重复执行(参考 `test_ha_and_migrations.py`)。
---
## 5. M2:候选复核与逐国来源
### 5.1 候选表新增列(在 M1 的迁移里一并加)
`compute_evidence_candidates`:
| 列 | 类型 | 说明 |
| --- | --- | --- |
| `comparison` | JSON default `{}` | 提取时与当前版本的逐国对比,格式见 5.2 |
| `dedupe_fingerprint` | String(64) index nullable | `sha256(source_id | metric | year | period_label | 排序后的 values)` |
| `review_reason_code` | String(40) nullable | 理由代码 |
| `review_reason` | Text default "" | 理由说明 |
| `published_publish_id` | String(64) nullable index | 发布后回填 |
同步更新 `ComputeEvidenceCandidate` 模型和 `to_dict()`。
### 5.2 提取改造(`POST /situational-awareness/candidates/extract`)
1. `CandidateExtractRequest` 增加可选 `period_label`(max 40)。
2. 按 M0 结论扩展 `extract_compute_candidates_deterministic`,只加真实案例需要的规则,并为新规则写单元测试。现有正则行为保持不变。
3. 每条候选生成后:
- 计算 `dedupe_fingerprint`;已存在相同指纹的候选(任意状态)→ 不新建,响应中返回已有候选并标 `duplicate: true`;
- 读取当前版本台账,按 `(metric, year)` 找到现有观测行,生成 `comparison`:
```json
{
"publish_id": "当前版本",
"metric": "global_share", "year": 2024,
"period_label": null, "existing_period_label": null,
"period_conflict": false,
"denominator": "global",
"codes": {
"CN": {"old_value": 23.0, "new_value": 24.5, "delta": 1.5, "change_kind": "historical_revision", "old_source_id": "caict-2025"},
"US": {"old_value": 44.0, "new_value": 44.0, "delta": 0.0, "change_kind": "unchanged", "old_source_id": "caict-2025"}
},
"has_historical_revision": true
}
```
- `denominator` 取 `compute_history.MODE_DENOMINATOR` 对应口径;
- 候选与现有行 `period_label` 不同 → `period_conflict: true`。
4. 抽取时把报告写入 `EvidenceRecord`(`id = source_id`,`excerpt`、`locator`、`content_sha256`),已存在则不覆盖。
### 5.3 复核改造(`POST /situational-awareness/candidates/{id}/review`)
`CandidateReviewRequest`:
```python
decision: Literal["approved", "rejected"]
reason_code: Literal[
"newer_authoritative_report", "corrects_prior_error", "adds_missing_observation", "methodology_update",
"unverifiable_source", "denominator_mismatch", "extraction_error", "superseded", "duplicate",
]
reason: str = Field(min_length=2, max_length=1000)
```
- 采纳只能用前 4 个代码,驳回只能用后 5 个,不匹配 → 422;
- 已发布(`published_publish_id` 非空)的候选不能再改 → 409;
- 写入 `review_reason_code`、`review_reason`、`reviewed_by`、`reviewed_at`;`AuditLog.details` 包含理由。
### 5.4 逐国来源 `value_sources`
`backend/app/schemas/compute_configuration.py` 的 `ComputeObservation` 增加:
```python
value_sources: dict[str, Identifier] | None = None
```
校验(加在 `comparable_values` 或 `ComputeConfiguration.consistent_evidence` 中):
- `value_sources` 的键必须是 `values` 的子集;
- 引用的每个来源必须存在于 `sources`,且 `year <= report_year`;
- 某国未出现在 `value_sources` 中 → 使用行级 `source_id`。
新增辅助函数 `source_for(row, code) -> str`,所有读取来源的地方都改用它。必须同步修改:
- `compute_history.compute_history`:每行输出 `evidence_by_code: {participant_code: [source_id, ...]}`(区域取成员来源去重)和 `mixed_sources: bool`;保留现有 `evidence_id`(行级)以兼容前端;
- `_latest_bilateral_pair` / `_build_bilateral_rpi`:增加 `cn_evidence_id`、`us_evidence_id`;
- `_build_denominator_attribution`:`evidence_ids` 用逐国来源;
- `tool_registry.filter_compute_config_by_cutoff`:某国来源被截断时只去掉该国的值;一行所有国家都被去掉时去掉整行;
- `diff_compute_configurations`:`old_source_id` / `new_source_id` 用逐国来源。
### 5.5 导入草稿修复(`compute-comparison-model.ts` `applyApprovedCandidateToDraft`)
规则:
1. 找不到 `(metric, year)` 行 → 按候选新建一行(含 `period_label`),行级 `source_id` 为候选来源。
2. 找到行且 `period_label` 冲突 → **不合并**,返回需要确认的状态。UI 弹确认框:“替换本年主观测”会用候选整行替换(值、`period_label`、来源),列出会被丢弃的国家代码;取消则不导入。
3. 找到行且无冲突:
- 只更新候选中出现的国家的值;
- **不修改行级 `source_id`**;候选来源与该国现来源不同时,写入 `value_sources[code] = 候选来源`;
- 合并后若所有国家来源相同 → 把行级 `source_id` 设为该来源并删除 `value_sources`。
4. 若 `metric` 属于份额类(`global_share` / `intelligent_share` / `frontier_cluster_share` / `chip_inflow_share`)且合并后一行有多个来源:在草稿上标记该行,提示“跨报告局部修订:请在发布说明中写明同分母依据”。
5. 导入时把候选 id 记入草稿的待发布候选列表,保存时作为 `source_candidate_ids` 提交。
后端发布校验(`publish_configuration`):
- 份额类混源行存在且 `publish_note` 为空 → 422,提示写明同分母依据;
- `source_candidate_ids` 中每个候选必须为 `approved` 且未发布,否则 422;发布事务内回填 `published_publish_id`。
为 `applyApprovedCandidateToDraft` 写单元测试(项目已有前端测试时放在相同位置,用 `bun test` 运行;没有则在后端用等价 Python 逻辑测试不适用,必须用 Bun 测试前端模型)。
### 5.6 Admin 界面
候选卡片显示:
- 修订类型徽章:历史修订 / 新增观测 / 统计期冲突 / 重复;
- 逐国表格:国家、旧值、新值、差值(百分点或 EFlops)、旧来源;
- 指标口径、分母、年份、报告页码;
- 采纳 / 驳回时必须选择理由代码并填写说明(表单校验,不能空提交)。
### 5.7 M2 测试
1. **只修订中国**(核心):报告 A 同时给中美份额,报告 B 只给中国新值 → 采纳 B 导入发布后,`/series` 中 `evidence_by_code` 显示 CN→B、US→A;`/lineage`(M4)中美国仍引用 A。
2. 同一报告重复提取 → 不新增候选,返回 `duplicate: true`。
3. 采纳理由缺失或代码与决定不匹配 → 422。
4. `period_label` 冲突时导入被拦截;确认替换后整行替换。
5. 份额混源行无发布说明 → 422。
6. 截断:`as_known_then` 截断报告 B 后,中国值消失、美国值保留。
7. M0 的真实案例摘录能被提取,`comparison` 中修订国家为 `historical_revision`。
---
## 6. M3:演播由版本驱动、可补播、与 BGP 解耦
### 6.1 新流程(`backend/app/services/ai/proactive_pipeline.py`)
新增 `process_compute_revision_presentation(db, revision_id) -> dict`:
1. 读取版本行;`presentation_status` 为 `delivered` 或 `skipped` → 直接返回(幂等)。
2. 从 `diff_summary` 取 `kind == "observation_value"` 且 `delta != 0` 的条目;没有 → 标记 `skipped`。
3. 用该版本的台账(`query_compute_posture_tool(..., publish_id=...)`)生成研判(`generate_or_get_compute_posture(..., persist=True)`),并编译演播契约:
- `presentation_key = "compute-revision-notice"`;
- `StagePresentation.revision = 版本行 id`(单调递增,前端按 `(key, time_basis, revision)` 去重);
- 契约中写入 `publish_id`;
- 有 `historical_revision` 时:标题“{year} 年{指标中文名}数据获修订”,正文列出“{国家} {旧值} → {新值}({差值})”,并包含固定提示“这是对历史数据的修订,不代表当期算力跃升”;
- 第一幕镜头对准第一个变化国家,`compute_mode` = 该指标对应模式,`focus_year` = 该年份,`spotlight_codes` = 变化国家;
- 恢复默认等回退也按同样规则生成(B→A 是相对上一版的真实变化,必须演播)。
4. 写入前检查:已存在 `presentation_key` 相同且契约中 `publish_id` 相同的 `StagePresentation` → 不重复写入(确认 `publish_stage_contract` 的去重方式,必要时在调用前查询)。
5. 成功 → `delivered`,记录 `presentation_delivered_at`;继续调用现有 `_evaluate_topic_watches`。
`process_compute_config_proactive_delta` 保留函数名以兼容调用方,内部改为对当前版本调用 `process_compute_revision_presentation`。算力路径**不再调用** `classify_snapshot_delta`,`_COMPUTE_STATE_KEY` 检查点不再使用。`classify_snapshot_delta` 与 BGP 代码不改。
### 6.2 失败处理与补播
- `compute_intelligence.py` 的 PUT 与 reset:删除 `except Exception: pass`。发布提交后调用演播;异常时记录 `warning_event`(`event="compute.revision.presentation_failed"`),版本行置 `failed`、`presentation_attempts += 1`、写 `presentation_error`,提交;接口仍返回 200(发布已成功),响应带 `presentation_status`。
- 新增 `retry_pending_compute_presentations(db, max_attempts=3)`:处理 `pending` / `failed` 且未超过次数的版本,按 `id` 升序。
- 调用时机:应用启动初始化一次;按 `backend/app/services/scheduler.py` 现有的定时任务写法加一个每 5 分钟的任务;`POST /situational-awareness/proactive/evaluate` 的 `domain == "compute"` 分支改为调用它。
### 6.3 前端演播行为
确认 `stage-narrative-player.js` 现有逻辑满足以下行为;只在缺失时修改:
- 地球空闲时收到新契约 → 自动播放;
- 用户正在拖拽、查看或使用参谋时 → 不抢镜头,只显示“待播新态势”提示;
- 同一 `(key, time_basis, revision)` 重复到达 → 不重复入队。
在 `domainLabels` 中为修订提示显示“算力数据修订”。
### 6.4 M3 测试
1. **中断补播**:发布时让演播函数抛异常 → 版本行 `failed`;调用 `retry_pending_compute_presentations` → `delivered`,生成恰好一条 `StagePresentation`。
2. **重复不重播**:对同一版本再次调用 → 不新增 `StagePresentation`。
3. **B→A 恢复会播**:A→B→A,第三次生成新的演播契约。
4. **1.5pp 小修订会播**。
5. **只改里程碑等非观测内容** → `skipped`,无演播。
6. BGP 主动研判现有测试全部通过,未改动。
---
## 7. M4:卡片与参谋同源、版本切换、血缘抽屉
### 7.1 血缘接口(读取时推导,不建关系表)
新增 `GET /api/v1/data-products/compute/lineage?code=CN&metric=global_share&year=2024&publish_id=...`(`publish_id` 省略为当前版本),权限 `require_earth_or_api_access("data_products:read")`。
实现:
1. 读取指定版本台账,取该国当前值和来源(`source_for`),附报告详情(标题、发布机构、URL、页码、`published_at`、`EvidenceRecord.content_sha256`)。
2. 从该版本向前遍历版本表(最多 50 条),收集 `diff_summary` 中匹配 `(metric, year, code)` 的条目,每条输出:`publish_id`、`published_at`、`action`、`old_value`、`new_value`、`delta`、`change_kind`、`old_source_id`、`new_source_id`、关联候选(通过 `source_candidate_ids`)的理由代码与理由、复核人、发布说明、发布人。
3. 输出对象级时间:观测期(`year` / `period_label`)、报告发表日、入库时间(候选 `created_at`,基线数据为空)、生效发布时间。
**脱敏**:调用者不是已登录的 `admin` / `super_admin` 时(查找项目现有的可选当前用户依赖;没有则新增一个不报错的可选版本),返回中:
- 复核人、发布人姓名替换为“已人工复核”;
- 不返回 `review_reason` 和 `publish_note` 原文,只返回理由代码对应的中文类别。
### 7.2 Earth 算力卡片(`frontend/public/earth/js/compute-topic.js`)
- 状态增加 `publishId`(取自 `/series` 响应)和 `asOf`;`/series`、`/posture/compute` 请求都带上 `as_of`(设置时)。
- 配置面板增加“系统发布版本”选择:选项来自公开的轻量接口。`/revisions` 是管理员接口,为 Earth 新增 `GET /api/v1/data-products/compute/revisions/public?limit=20`,只返回 `publish_id`、`published_at`、`action`、变化摘要(脱敏),权限 `require_earth_or_api_access("data_products:read")`。
- 查看历史版本时,卡片顶部显示提示条:“正在查看 {时间} 发布的版本(非当前)”,带“回到当前”按钮。早于基线时显示“历史快照不完整”。
- 数值旁的溯源按钮打开现有证据面板,扩展为血缘视图:当前值与来源、修订时间线、复核信息(按 7.1 脱敏)。
- `open-copilot` 和 `open-compute-workbench` 传给参谋的上下文增加 `publishId`、`asOf`(现有 `mode`、`focusYear`、`selectedCodes` 保留)。
### 7.3 参谋同源(`situational-copilot.js` 与后端工作流)
- 前端把 `publishId`、`asOf` 放入会话上下文,后端 `workflow_runtime` / `dag_executor` 调用 `query_compute_posture_tool` 时透传。
- 后端生成的 `view_spec` 增加 `publish_id`、`mode`、`focus_year`(`_compile_compute_workbench` 与 `compile_compute_view_spec`)。
- 前端渲染参谋的算力结果时,比较 `view_spec` 的 `publish_id` / `mode` / `focus_year` 与当前卡片状态:
- 一致 → 正常显示;
- 不一致 → 图表区与文字区都显示“本结果基于 {版本/口径},与当前地球视图不同”,并提供“按当前视图重新研判”按钮。**不得**出现图表与文字基于不同口径而无提示。
- 追问“这次为什么变了”时,后端把当前版本的 `diff_summary` 和关联候选理由加入工作流上下文,参谋回答必须引用同一 `publish_id` 下的证据。
### 7.4 文档
- 更新 `docs/technical/zh/platform-data-flows.md` 与 `docs/technical/en/platform-data-flows.md` 的算力部分:版本表、两根时间轴、演播补播、血缘推导、脱敏规则。
- 若 Admin / Earth 交互有用户可见变化,按 `docs/documentation-coverage-rules.md` 更新对应前端技术文档。
- 运行 `scripts/harness/docs-consistency-check.sh`。
### 7.5 M4 验收(端到端,全部通过才算一期完成)
用 M0 的真实案例走完以下 9 条,每条保留证据(测试输出或截图):
1. **修订识别**:提取真实报告摘录 → 候选显示“历史修订”、逐国旧值/新值/差值、口径、分母、报告页码。
2. **人工决策留痕**:`admin` 选理由采纳并发布;另做一次驳回,理由被记录;`viewer` 调用复核和发布接口返回 403。
3. **主动演播与礼让**:发布后空闲地球自动播放“{year} 年数据获修订”,文案包含“历史数据修订,非当期跃升”;用户操作中只显示提示;重复提交同一报告不产生新候选、不重播。
4. **卡片与参谋同源**:从卡片打开参谋,参谋结果的 `publish_id` / 口径 / 年份与卡片一致;在卡片切换口径后,参谋旧结果显示“与当前视图不同”提示,点击后按新口径重新生成。
5. **血缘一追到底**:点击地球上修订后的数字,能看到报告原文出处、页码、哈希、观测期、入库时间、复核理由类别、发布时间、修订前旧值。登录管理员能看到复核人姓名和理由原文,公开访问看不到。
6. **切回发布前**:选择发布前的版本,卡片与参谋显示旧值和当时的结论。
7. **只修订中国**:美国未变的值继续引用原报告。
8. **A→B→A**:三个时点分别显示 A、B、A,两次变更各有理由;旧页面 token 提交返回 409。
9. **中断补播**:模拟演播失败,补播后地球播放一次;同一事件再次到达时地球不动。
---
## 8. 每个里程碑的检查与报告
### 8.1 检查命令(按改动范围选择,里程碑结束时全部跑)
```bash
cd backend && uv run --frozen --group dev --project .. python -m pytest -q
scripts/harness/backend-rules-check.sh
cd frontend && bun install --frozen-lockfile && bun run build && bun run build:admin
scripts/harness/frontend-rules-check.sh
scripts/harness/docs-consistency-check.sh
scripts/harness/quick-check.sh
```
M4 结束再跑 `scripts/harness/validate.sh`。涉及 Admin / Earth 界面的改动,需要在浏览器中实测渲染(桌面、移动端、125% / 150% 缩放),并检查 Admin 一屏高度链没有被破坏。
测试失败时先判断它保护的行为:实现回归就修实现;断言绑定的是已被本计划明确改变的旧细节(例如恢复默认复用 `DEFAULT_REVISION`),才更新测试,并在报告中说明。
### 8.2 里程碑报告格式
```
## M{n} 完成报告
- 完成内容:(对应本计划的小节编号)
- 改动文件:(路径列表)
- 新增/修改测试:(测试名 + 结果)
- 检查命令结果:(通过/失败 + 失败原因)
- 与计划不一致之处:(代码现状与计划描述不同的地方,以及处理方式)
- 遗留问题与风险:
```
### 8.3 需要停下来问用户的情况
- M0 找不到真实修订案例;
- 发现计划中的约定会破坏现有功能且没有安全的保守做法;
- 需要新增数据源、新增角色、修改既有迁移文件、或改动 BGP 逻辑;
- 需要大范围重构 `compute-topic.js` 或 Earth 渲染层。