Files
watchman/.task/2026-11-17/m01-foundation/implementation-plan.md
T
unknownandCursor 8fb16f4a4a feat(infra): 落地 M01 基础框架与本机开发环境
交付 FastAPI/Celery/Vue 薄壳、FORCE RLS、Compose 回环与 review 修复证据,关闭 M01 DONE_LOCAL。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 15:00:39 +08:00

498 lines
16 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.
# M01 基础框架 Implementation Plan
## Requirement Source
`.task/2026-11-17/m01-foundation/requirement.md`
## Scope Summary
按已冻结的 M01 需求,顺序建立 uv 与 `web/` 工作区、环境分层、API 信封、带强制 RLS 的两张平台表、Celery 探测进程、管理台薄壳,以及只绑定本机的开发 Compose。不进入业务模块。
## Involved Modules Or Areas
Watchman M01。一个 Python 服务包,API / worker / beat 三个进程。前端是 `web/apps/console` 薄壳。
## Loaded Rules
- Marionettist:本任务 `gatePolicy.selected=autonomous`,`finalApprovalRequired=true`。分析到编码仍须用户确认。`risk_score >= 4` 和 `high-risk` 切片完成后必须停。
- `standards://engineering/code-layout?version=1.0.1`
- `standards://api/api-envelope?version=1.1.0`
- `standards://api/error-handling?version=1.1.1`
- `standards://frontend/frontend-stack?version=2.1.0`
- `standards://frontend/element-plus?version=1.0.2`
- `standards://security/red-line?version=1.1.0`
- `standards://security/env-secrets?version=1.1.1`
- `standards://engineering/code-comments?version=1.1.1`
- `standards://workflow/git-commit?version=1.1.0`
- Watchman D01、D04、D12–D15。
## Loaded Docs
- `docs/tasks/INDEX.md` M01
- `docs/development-plan.md` 技术栈;目录以 D12 为准
- `docs/decisions.md`
- syops 只用于对照,不复制 Flask、Redis worker 或字符串错误码
## Global Forbidden Scope
- `D:\PROJECT\syops` 内的任何写入。
- 业务包:数据源、查询、规则、报警、通知、场景、AI。
- Casdoor 真接入、权限点编码、vben5、Redis。
- 把 API、worker 拆成独立服务或独立数据库。
- 监听 `0.0.0.0`,或把开发端口写进任何非开发配置。
- 真实密钥、生产连接串、修改已合入迁移。
- 为列表分页做协议选择。
## Execution Strategy
- Complexity: complex
- Default Execution: sequential
- Parallel Execution: not-needed
- Fallback Execution: sequential
- Merge Owner: 主 Agent
- Conflict Resolution Rule: 工作区根、锁文件、env 样例和迁移目录由当前切片独占。后一切片不得改前一切片已经冻结的信封字段和 RLS 策略语义。
选择顺序执行,因为根工作区、环境样例和数据库会话会被多个切片碰到。
## Slice Dependency Graph
| Slice | Depends On | Can Run In Parallel With | Fallback Order | Shared Files | Merge Owner | Conflict Risk |
| --- | --- | --- | --- | --- | --- | --- |
| S1 工作区 | 无 | 无 | 1 | 根 `pyproject.toml`、`web/`、`.gitignore` | 主 Agent | high |
| S2 环境分层 | S1 | 无 | 2 | env 样例、`.gitignore` | 主 Agent | high |
| S3 信封 | S1 | 无 | 3 | `services/watchman` | 主 Agent | medium |
| S4 RLS | S2、S3 | 无 | 4 | 迁移、数据库会话 | 主 Agent | high |
| S5 队列 | S2、S4 | 无 | 5 | 同一 Python 包、Compose | 主 Agent | medium |
| S6 薄壳 | S3 | 无 | 6 | `web/` | 主 Agent | medium |
| S7 手册与总验 | S5、S6 | 无 | 7 | `deploy/`、`docs/runbook.md` | 主 Agent | medium |
S6 依赖 S3 的信封,不依赖 S4 的表。仍按上表顺序做,避免和 S5 同时改 Compose。
## Implementation Slices
### Slice 1: 工作区与工具链
#### Goal
建立可安装的 uv 工作区和 pnpm `web/` 工作区,锁定 Python 3.12 候选上的最小依赖。不写业务行为。
#### Allowed Modification Scope
- 根 `pyproject.toml`、`uv.lock`
- `services/watchman/pyproject.toml`
- `services/watchman/src/watchman/` 的空包和进程入口文件
- `web/package.json`、`web/pnpm-workspace.yaml`、`web/pnpm-lock.yaml`
- `web/apps/console` 的空 Vite 应用
- `web/packages/api`、`web/packages/ui-components`、`web/design` 的空包
- `.gitignore` 中与虚拟环境和前端依赖相关的条目
#### Forbidden Scope
- 业务模块、迁移、Compose、页面视觉、密钥文件。
#### Execution
- Mode: sequential
- Depends On: 无
- Can Run With: 无
- Must Not Run With: S2–S7
- Fallback Order: 1
- Shared Files: 根工作区
- Merge Owner: 主 Agent
- Conflict Risk: high
- Gate Class: standard
- Risk Score: 3
- Gate Reasons: new-dependencies
- Validation Level: slice
- Recommended Agent Strategy: 编码模型实现;审查用另一个模型系列
#### Steps
1. 核对 FastAPI、Pydantic v2、SQLAlchemy 2、Alembic、Celery、pytest 与 Python 3.12 的当前兼容版本,把结果写进切片记录。AGPL 依赖不引入。
2. 创建 uv 工作区。只建一个成员:`services/watchman`。入口预留 API、worker、beat,不拆服务。
3. 创建 pnpm 工作区:`web/apps/console`、`web/packages/api`、`web/packages/ui-components`、`web/design`。
4. 生成锁文件。
#### Validation
- `uv lock` 成功。
- `uv run --package watchman python -c "import watchman"` 成功。
- `pnpm install` 在 `web/` 成功,`pnpm -r list` 能看到 console 和两个包。
#### Done Criteria
- 锁文件入库所需的依赖已经固定。
- 还没有健康检查、表和页面行为。
#### Rollback Notes
删除新建的工作区文件和锁文件即可。没有数据库可回滚。
### Slice 2: 环境分层
#### Goal
按公司环境规范提供可提交的无密样例,并让进程从分层 env 读取配置。
#### Allowed Modification Scope
- `.env.shared`
- `.env.example`
- `.gitignore` 的 env 例外
- `services/watchman` 的配置读取模块
- 配置测试
#### Forbidden Scope
- `.env.local`、真实 `.env.dev`、真实密码。
- 监听地址改到非回环。
#### Execution
- Mode: sequential
- Depends On: S1
- Can Run With: 无
- Must Not Run With: S3–S7
- Fallback Order: 2
- Shared Files: `.gitignore`
- Merge Owner: 主 Agent
- Conflict Risk: high
- Gate Class: standard
- Risk Score: 3
- Gate Reasons: secrets-config
- Validation Level: slice
- Recommended Agent Strategy: 编码模型实现
#### Steps
1. `.env.shared` 只放三环境相同且非机密的值,包括开发绑定地址说明。
2. `.env.example` 列出 `.env.dev` 与 `.env.local` 要填的键,值用占位符。
3. 调整忽略规则:提交 `.env.shared` 和 `.env.example`;忽略 `.env.local`、`.env.dev`、`.env.test`、`.env.prod`。
4. 加载顺序实现为:系统环境、`.env.local`、`.env.dev`、`.env.shared`。
5. 开发端口写死为仅开发:API `127.0.0.1:8000`,控制台 `127.0.0.1:5173`,Postgres `127.0.0.1:54329`,RabbitMQ `127.0.0.1:56729`,管理端口 `127.0.0.1:15679`。
#### Validation
- 测试证明 `.env.local` 覆盖 `.env.dev`,`.env.dev` 覆盖 `.env.shared`。
- 仓库检索确认样例不含真实口令、token 或内网账号。
#### Done Criteria
- 缺配置时进程失败并给出键名,不打印其他配置值。
- 开发端口只出现在开发样例和开发 Compose 计划中。
#### Rollback Notes
恢复 `.gitignore` 并删除新的 env 样例和配置模块。
### Slice 3: API 信封与错误
#### Goal
提供健康检查和全局失败信封。路由不手写错误 JSON。
#### Allowed Modification Scope
- `services/watchman/src/watchman/api/`
- `services/watchman/src/watchman/services/` 中的健康服务
- 错误类型、响应构造、请求号中间件
- 对应 pytest
#### Forbidden Scope
- 数据库表、RLS、前端页面、队列任务。
#### Execution
- Mode: sequential
- Depends On: S1
- Can Run With: 无
- Must Not Run With: S4–S7
- Fallback Order: 3
- Shared Files: `services/watchman`
- Merge Owner: 主 Agent
- Conflict Risk: medium
- Gate Class: standard
- Risk Score: 3
- Gate Reasons: public-api-contract
- Validation Level: slice
- Recommended Agent Strategy: 编码模型实现
#### Steps
1. 成功体使用 `success`、`data`、`request_id`。失败体使用 `success=false`、`error`、`request_id`。
2. 错误码只用五段位整数。内部错误对外为 50000。
3. 尊重传入的 `X-Request-Id`;没有则生成。响应头回写同一个值。
4. `GET /health` 返回进程存活,不访问数据库,不返回租户、主机或连接串。
5. 增加一个只用于测试的参数错误路径,确认 40000 由全局处理生成。
#### Validation
- `uv run pytest services/watchman/tests/test_envelope.py`
- 断言成功与 40000、50000 的形状,并断言响应不含 traceback。
#### Done Criteria
- 健康检查和两类失败响应有测试。
- 没有信封例外出口。
#### Rollback Notes
移除 API 包中本切片新增的模块和测试。
### Slice 4: 平台表、租户会话与 RLS
#### Goal
在 PostgreSQL 建立审计事件和运行记录,应用角色被强制 RLS 限制在服务端租户内。
#### Allowed Modification Scope
- `services/watchman/migrations/`
- 数据库会话和租户上下文
- 审计、运行记录的最小写入服务
- RLS 与幂等测试
- 开发 Compose 中仅数据库服务的部分可以在本切片先写出,完整应用服务留到 S7
#### Forbidden Scope
- 报警、查询、场景等业务表。
- 从请求体、查询串或请求头读取租户。
- 用超级用户作为应用连接。
- 修改已经写好的迁移文件;纠正用新迁移。
#### Execution
- Mode: sequential
- Depends On: S2、S3
- Can Run With: 无
- Must Not Run With: S5–S7
- Fallback Order: 4
- Shared Files: 迁移目录、数据库会话
- Merge Owner: 主 Agent
- Conflict Risk: high
- Gate Class: high-risk
- Risk Score: 5
- Gate Reasons: schema, security, rls
- Validation Level: slice
- Recommended Agent Strategy: 独立实现与不同系列审查。本切片结束必须停,即使用户选择了 autonomous。
#### Steps
1. Compose 只启动 PostgreSQL,发布在 `127.0.0.1:54329`。创建迁移角色和应用角色。应用角色不是超级用户,也不是表所有者。
2. 迁移创建审计事件和运行记录。两表都有服务端写入的租户列和幂等键唯一约束。
3. 启用并强制 RLS。策略比较租户列和 `current_setting('app.tenant_id', true)`。
4. 每个应用事务在本地设置 `app.tenant_id`,值只来自服务端配置。
5. 写接口要求 `x-idempotency-key`。重复键返回第一次的结果,不插入第二行。
6. 测试覆盖:未设置租户不可读不可写;其他租户不可见;正确租户可读写;重复键不新增。
#### Validation
- 启动开发 Postgres 后执行迁移。
- `uv run pytest services/watchman/tests/test_rls.py services/watchman/tests/test_idempotency.py`
- 用应用角色手工查询确认没有租户设置时结果为空。
#### Done Criteria
- 空库迁移成功。
- RLS 正反用例通过。
- 健康检查仍然不访问这两张表。
#### Rollback Notes
停掉开发库卷并删除本切片迁移文件。若迁移已被共享环境应用,只能再写一条逆向迁移,不改原文件。
### Slice 5: 队列进程
#### Goal
API 之外的 worker 能连接 RabbitMQ,并注册一个不写业务事实的探测任务。
#### Allowed Modification Scope
- 同一 `services/watchman` 包内的 Celery 入口
- Compose 中的 RabbitMQ 服务,只绑定 `127.0.0.1:56729` 和 `127.0.0.1:15679`
- worker 启动测试
#### Forbidden Scope
- Redis result backend。
- 业务周期任务、报警计数、真实外发。
- 第二个调度框架。
#### Execution
- Mode: sequential
- Depends On: S2、S4
- Can Run With: 无
- Must Not Run With: S6、S7
- Fallback Order: 5
- Shared Files: Python 包、Compose
- Merge Owner: 主 Agent
- Conflict Risk: medium
- Gate Class: standard
- Risk Score: 3
- Gate Reasons: new-infrastructure
- Validation Level: slice
- Recommended Agent Strategy: 编码模型实现
#### Steps
1. Celery broker 指向开发 RabbitMQ。不配置 Redis。
2. 注册一个探测任务,返回静态状态,不写审计表,不增加任何业务次数。
3. beat 进程可以启动,但 M01 不注册业务日程。
#### Validation
- RabbitMQ 容器健康。
- worker 进程启动后能列出探测任务。
- 执行一次探测任务,确认审计表行数不变。
#### Done Criteria
- 三个进程入口都存在,仍然是同一个包。
- 探测任务没有业务副作用。
#### Rollback Notes
停止 RabbitMQ 容器并移除 Celery 入口。不删除 S4 的表。
### Slice 6: 管理台薄壳
#### Goal
本机控制台显示健康检查的加载、成功和失败。四语言目录齐全。不引入后台框架。
#### Allowed Modification Scope
- `web/apps/console`
- `web/packages/api` 的唯一 JSON 客户端
- `web/design/tokens.css` 的最小令牌
- `web/packages/ui-components` 保持空包或只放说明,不放 ProTable
#### Forbidden Scope
- vben5、业务菜单、列表页、第二套组件库。
- 页面内直接 `fetch`。
- 全局 `app.use(ElementPlus)`。
#### Execution
- Mode: sequential
- Depends On: S3
- Can Run With: 无
- Must Not Run With: S7
- Fallback Order: 6
- Shared Files: `web/`
- Merge Owner: 主 Agent
- Conflict Risk: medium
- Gate Class: standard
- Risk Score: 2
- Gate Reasons: none
- Validation Level: slice
- Recommended Agent Strategy: 编码模型实现,并用浏览器核对健康三态
#### Steps
1. 唯一客户端解析信封,失败抛带 `code`、`requestId` 的错误对象。
2. App 根注入一次 Element Plus 语言。页面按需引入用到的组件。
3. 建立 `locales/zh-CN`、`zh-TW`、`en`、`th`。健康文案四份都写上。
4. 页面请求 `/health`,分别呈现加载、成功和失败。
5. 开发服务器只绑 `127.0.0.1:5173`。
#### Validation
- `pnpm --filter console exec vue-tsc --noEmit`
- 浏览器打开本机控制台:API 可用时看到成功;API 停掉时看到失败;请求未完成时看到加载。
- 抽查一种非默认语言,健康文案不是硬编码中文。
#### Done Criteria
- 三态都实际看过,不只看截图。
- 四语言目录和 `errors.json` 骨架存在。
#### Rollback Notes
恢复 `web/apps/console` 到 S1 的空应用,保留工作区结构。
### Slice 7: 开发手册与总验
#### Goal
把 API、worker、beat、前端和两个基础设施服务收成一条开发手册,并跑本模块测试。
#### Allowed Modification Scope
- `deploy/compose.dev.yml`
- `docs/runbook.md`
- 根 README 里指向手册的短节
- `docs/tasks/M01.md` 的证据栏
- `docs/progress.md`
#### Forbidden Scope
- 生产 Compose、域名 443 配置、业务服务。
- 把 `0.0.0.0` 写进 Compose。
#### Execution
- Mode: sequential
- Depends On: S5、S6
- Can Run With: 无
- Must Not Run With: 无
- Fallback Order: 7
- Shared Files: `deploy/`、文档
- Merge Owner: 主 Agent
- Conflict Risk: medium
- Gate Class: standard
- Risk Score: 3
- Gate Reasons: dev-runtime
- Validation Level: final
- Recommended Agent Strategy: 主 Agent 汇总证据;测试由独立执行完成
#### Steps
1. Compose 包含 Postgres、RabbitMQ、API、worker、beat。端口只发布到 127.0.0.1,并在文件头标明仅开发。
2. 手册写明启动、迁移、测试和停止命令。
3. 跑后端测试、前端类型检查,并按 S6 再看一次健康三态。
#### Validation
- `docker compose -f deploy/compose.dev.yml config`
- 手册中的测试命令实际执行并记录退出码。
- 容器未启动时记录未运行,不记通过。
#### Done Criteria
- 手册命令和实际命令一致。
- M01 仍不声称业务模块或真实登录已经完成。
#### Rollback Notes
停止 Compose 并删除开发卷。文档可以回退到本切片之前。
## Parallel Slice Groups
没有并行组。
## Final Validation
- 信封测试、RLS 测试、幂等测试、worker 探测测试通过。
- `vue-tsc` 通过。
- 浏览器确认健康三态。
- `docker compose config` 通过,发布地址都是 127.0.0.1。
## Documentation Update Requirement
- 已在分析阶段记录 D12–D15。
- S1 记录实际锁定的版本,替换 O09 里本模块已经核实的部分。
- S7 更新 `docs/runbook.md`、`docs/progress.md` 和 `docs/tasks/M01.md`。
- 不把实现文件清单写进架构文档。
## Risks
- S4 是高风险切片。autonomous 不能越过它。
- 版本号要在 S1 查官方资料后锁定。计划里故意不预写。
- 分页协议和 vben5 仍延后。
- O08 的模型授权仍未确认。编码开始前要记下实际子 Agent 模型;不能把未执行的审查写成通过。