PMFlow/backend/app/services/task_brief/shared.py

156 lines
4.3 KiB
Python
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.

"""增量任务包管理服务。"""
from __future__ import annotations
import json
import subprocess
from datetime import datetime
from pathlib import Path
from typing import Any, Optional
from uuid import uuid4
from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession
from ...models import Integration
from ...utils.timezone import now_cn_naive
INTEGRATION_TYPE = "task_briefs"
DEFAULT_TEMPLATE_ID = "tpl_default_signal_planner"
DEFAULT_TEMPLATE_NAME = "Signal Planner 默认模板"
DEFAULT_TEMPLATE_DESCRIPTION = "系统初始化模板(可按需修改)"
DEFAULT_TEMPLATE_FALLBACK = """# Task Brief
## Meta
- Generated At: {{GENERATED_AT}}
- Base Ref: {{BASE_REF}}
- Head Ref: {{HEAD_REF}}
## Changed Files
{{CHANGED_FILES}}
## Scope
{{SCOPE_SUMMARY}}
## Risks
{{RISKS}}
"""
LOW_TEMPLATE_ID = "tpl_low_model_execution"
LOW_TEMPLATE_NAME = "Low Model 执行模板(收敛)"
LOW_TEMPLATE_DESCRIPTION = "面向 low 模型:聚焦确定性执行、最小改动、可回归。"
LOW_TEMPLATE_CONTENT = """# Task Brief(Low Model 执行版)
## Meta
- Generated At: {{GENERATED_AT}}
- Base Ref: {{BASE_REF}}
- Head Ref: {{HEAD_REF}}
## Goal
- 任务目标:{{SCOPE_SUMMARY}}
- 执行原则:只做最小必要改动,不扩散需求边界。
## Changed Files
{{CHANGED_FILES}}
## Atomic Actions
1. 明确本次唯一目标与目标验收标准(1-2 条)。
2. 逐文件执行最小改动,禁止顺手重构。
3. 写前后保持接口字段兼容,避免破坏已有调用。
4. 每次改动后进行快速验证并记录结果。
## Must-Run Verification
- Backend: `python3 -m py_compile backend/app`
- Frontend: `npm run build` + `npx tsc --noEmit`
- 必须给出失败项与修复动作,禁止只报“已测试”。
## Escalation Gate (Upgrade to High Model)
- 跨模块文件超过 5 个;
- 涉及数据库 schema / 事务边界调整;
- 涉及前后端契约新增或破坏性变更;
- 无法在当前信息下保证零回归。
## Risks
{{RISKS}}
"""
HIGH_TEMPLATE_ID = "tpl_high_model_architect"
HIGH_TEMPLATE_NAME = "High Model 架构模板(跨栈)"
HIGH_TEMPLATE_DESCRIPTION = "面向 high 模型:跨栈影响评估、契约矩阵、回滚策略。"
HIGH_TEMPLATE_CONTENT = """# Task Brief(High Model 架构版)
## Meta
- Generated At: {{GENERATED_AT}}
- Base Ref: {{BASE_REF}}
- Head Ref: {{HEAD_REF}}
## Strategic Goal
- 业务目标:{{SCOPE_SUMMARY}}
- 决策边界:优先保障可回滚、契约兼容、线上稳定性。
## Changed Files
{{CHANGED_FILES}}
## Cross-Stack Impact Matrix
| 领域 | 影响描述 | 契约同步动作 | 验证方式 |
| --- | --- | --- | --- |
| Backend API | | | |
| Frontend Type | | | |
| DB/Data | | | |
| Async/Job | | | |
## Controlled Write Plan
- 所有数据库写入必须经受控接口(HTTP 或 `internal://`);
- 不允许直连 SQL 路径作为最终执行方案;
- 需给出接口化落地路径与调用顺序。
## Rollback Plan
1. 回滚触发条件(明确监控阈值)。
2. 数据层回滚策略(兼容/补偿/回放)。
3. 应用层回滚策略(开关/降级/流量切换)。
## Verification Gate
- 编译、单测、关键链路回归全部通过;
- 前后端契约文档同步完成;
- 风险项有 owner 与截止时间。
## Risks
{{RISKS}}
"""
HIGH_CONTRACT_TEMPLATE_ID = "tpl_high_model_contract_guard"
HIGH_CONTRACT_TEMPLATE_NAME = "High Model 契约审计模板"
HIGH_CONTRACT_TEMPLATE_DESCRIPTION = "面向 high 模型:前后端契约覆盖率审计与缺口修复。"
HIGH_CONTRACT_TEMPLATE_CONTENT = """# Task Brief(High Model 契约审计版)
## Meta
- Generated At: {{GENERATED_AT}}
- Base Ref: {{BASE_REF}}
- Head Ref: {{HEAD_REF}}
## Audit Scope
- 目标范围:{{SCOPE_SUMMARY}}
## Changed Files
{{CHANGED_FILES}}
## Contract Coverage Checklist
- [ ] 后端新增/变更 API 已在契约文档登记;
- [ ] 前端调用与后端路由一一对应;
- [ ] 响应包络保持 `{code, message, data}`;
- [ ] 关键类型(TS/Pydantic)字段一致;
- [ ] 历史兼容策略(向后兼容或版本化)已说明。
## Gap Register
| Gap | Severity | Owner | Fix Plan | ETA |
| --- | --- | --- | --- | --- |
| | | | | |
## Delivery Artifacts
- 契约差异摘要(新增/删除/变更字段)
- 影响接口清单与调用方映射
- 回归测试结果与剩余风险
## Risks
{{RISKS}}
"""