@michaelxwb/code-flow 0.6.5 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -3
- package/docs/USAGE.md +1529 -0
- package/docs/acceptance-contract.md +217 -0
- package/docs/ci/acceptance-evidence.md +77 -0
- package/docs/migrations/runtime-0.7.0.md +41 -0
- package/docs/migrations/runtime-0.8.0.md +154 -0
- package/docs/migrations/runtime-0.9.0.md +40 -0
- package/docs/spec-injection-hardening.md +54 -0
- package/docs/spec-injection-performance.json +73 -0
- package/docs/spec-optimization.md +12 -0
- package/package.json +18 -3
- package/src/adapters/claude/CLAUDE.md +1 -1
- package/src/adapters/claude/commands/cf-init.md +1 -3
- package/src/adapters/claude/commands/cf-spec.md +23 -3
- package/src/adapters/claude/commands/cf-stats.md +11 -1
- package/src/adapters/claude/commands/cf-sync.md +20 -6
- package/src/adapters/claude/commands/cf-task/align.md +25 -3
- package/src/adapters/claude/commands/cf-task/archive.md +36 -42
- package/src/adapters/claude/commands/cf-task/block.md +15 -2
- package/src/adapters/claude/commands/cf-task/graph.md +13 -1
- package/src/adapters/claude/commands/cf-task/note.md +13 -1
- package/src/adapters/claude/commands/cf-task/plan.md +137 -23
- package/src/adapters/claude/commands/cf-task/prd.md +13 -2
- package/src/adapters/claude/commands/cf-task/start.md +52 -39
- package/src/adapters/claude/commands/cf-task/status.md +12 -0
- package/src/adapters/claude/commands/cf-task/verify-e2e.md +68 -35
- package/src/adapters/claude/commands/cf-validate.md +30 -5
- package/src/adapters/codex/AGENTS.md +1 -1
- package/src/adapters/codex/hooks.json +14 -10
- package/src/adapters/codex/skills/cf-init/SKILL.md +4 -4
- package/src/adapters/codex/skills/cf-spec/SKILL.md +34 -6
- package/src/adapters/codex/skills/cf-stats/SKILL.md +11 -1
- package/src/adapters/codex/skills/cf-sync/SKILL.md +21 -6
- package/src/adapters/codex/skills/cf-task-align/SKILL.md +30 -8
- package/src/adapters/codex/skills/cf-task-archive/SKILL.md +36 -42
- package/src/adapters/codex/skills/cf-task-block/SKILL.md +15 -2
- package/src/adapters/codex/skills/cf-task-graph/SKILL.md +13 -1
- package/src/adapters/codex/skills/cf-task-note/SKILL.md +13 -1
- package/src/adapters/codex/skills/cf-task-plan/SKILL.md +175 -21
- package/src/adapters/codex/skills/cf-task-prd/SKILL.md +13 -2
- package/src/adapters/codex/skills/cf-task-start/SKILL.md +52 -39
- package/src/adapters/codex/skills/cf-task-status/SKILL.md +12 -0
- package/src/adapters/codex/skills/cf-task-verify-e2e/SKILL.md +69 -35
- package/src/adapters/codex/skills/cf-validate/SKILL.md +31 -5
- package/src/adapters/commands.json +115 -0
- package/src/adapters/costrict/CLAUDE.md +1 -1
- package/src/adapters/costrict/commands/cf-init.md +1 -3
- package/src/adapters/costrict/commands/cf-spec.md +23 -3
- package/src/adapters/costrict/commands/cf-stats.md +11 -1
- package/src/adapters/costrict/commands/cf-sync.md +20 -6
- package/src/adapters/costrict/commands/cf-task/align.md +25 -3
- package/src/adapters/costrict/commands/cf-task/archive.md +36 -42
- package/src/adapters/costrict/commands/cf-task/block.md +15 -2
- package/src/adapters/costrict/commands/cf-task/graph.md +13 -1
- package/src/adapters/costrict/commands/cf-task/note.md +13 -1
- package/src/adapters/costrict/commands/cf-task/plan.md +137 -23
- package/src/adapters/costrict/commands/cf-task/prd.md +13 -2
- package/src/adapters/costrict/commands/cf-task/start.md +52 -39
- package/src/adapters/costrict/commands/cf-task/status.md +12 -0
- package/src/adapters/costrict/commands/cf-task/verify-e2e.md +68 -35
- package/src/adapters/costrict/commands/cf-validate.md +30 -5
- package/src/adapters/install-plan.json +100 -0
- package/src/adapters/opencode/AGENTS.md +1 -1
- package/src/adapters/opencode/commands/cf-init.md +7 -7
- package/src/adapters/opencode/commands/cf-learn.md +6 -6
- package/src/adapters/opencode/commands/cf-spec.md +34 -2
- package/src/adapters/opencode/commands/cf-stats.md +17 -7
- package/src/adapters/opencode/commands/cf-sync.md +21 -6
- package/src/adapters/opencode/commands/cf-task/align.md +25 -8
- package/src/adapters/opencode/commands/cf-task/archive.md +36 -42
- package/src/adapters/opencode/commands/cf-task/block.md +16 -2
- package/src/adapters/opencode/commands/cf-task/graph.md +14 -1
- package/src/adapters/opencode/commands/cf-task/note.md +14 -1
- package/src/adapters/opencode/commands/cf-task/plan.md +137 -23
- package/src/adapters/opencode/commands/cf-task/prd.md +13 -2
- package/src/adapters/opencode/commands/cf-task/start.md +53 -45
- package/src/adapters/opencode/commands/cf-task/status.md +13 -0
- package/src/adapters/opencode/commands/cf-task/verify-e2e.md +68 -35
- package/src/adapters/opencode/commands/cf-validate.md +35 -9
- package/src/adapters/opencode/plugins/code-flow/index.js +128 -179
- package/src/adapters/platform-bindings.json +370 -0
- package/src/cli.js +369 -316
- package/src/core/code-flow/config.yml +4 -1
- package/src/core/code-flow/runtime-commands.json +259 -0
- package/src/core/code-flow/scripts/cf_acceptance_manifest.py +5 -289
- package/src/core/code-flow/scripts/cf_acceptance_runner.py +5 -187
- package/src/core/code-flow/scripts/cf_audit.py +10 -0
- package/src/core/code-flow/scripts/cf_codex_hook.py +10 -0
- package/src/core/code-flow/scripts/cf_design_lint.py +10 -0
- package/src/core/code-flow/scripts/cf_evidence_import.py +10 -0
- package/src/core/code-flow/scripts/cf_feedback.py +5 -51
- package/src/core/code-flow/scripts/cf_opencode_event.py +10 -0
- package/src/core/code-flow/scripts/cf_post_hook.py +6 -191
- package/src/core/code-flow/scripts/cf_pre_tool_hook.py +6 -99
- package/src/core/code-flow/scripts/cf_scenarios.py +10 -0
- package/src/core/code-flow/scripts/cf_spec_context.py +5 -1741
- package/src/core/code-flow/scripts/cf_spec_gate.py +5 -259
- package/src/core/code-flow/scripts/cf_spec_session.py +5 -114
- package/src/core/code-flow/scripts/cf_stats.py +5 -501
- package/src/core/code-flow/scripts/cf_stop_hook.py +6 -376
- package/src/core/code-flow/scripts/cf_sync.py +5 -135
- package/src/core/code-flow/scripts/cf_task_index.py +5 -143
- package/src/core/code-flow/scripts/cf_task_parallel.py +5 -389
- package/src/core/code-flow/scripts/cf_task_workflow.py +6 -201
- package/src/core/code-flow/scripts/cf_user_prompt_hook.py +6 -173
- package/src/core/code-flow/scripts/cf_validation.py +6 -77
- package/src/core/code-flow/scripts/codeflow/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_evidence.py +347 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_manifest_core.py +660 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_plan.py +202 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_registration.py +60 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_risk.py +73 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_schema.py +282 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_acceptance_selection.py +266 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_check_inputs.py +172 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_check_registration.py +326 -0
- package/src/core/code-flow/scripts/{cf_checks.py → codeflow/acceptance/cf_checks.py} +8 -11
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_plan_quality.py +197 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_scenario_quality.py +174 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_scenario_source.py +300 -0
- package/src/core/code-flow/scripts/codeflow/acceptance/cf_traceability.py +502 -0
- package/src/core/code-flow/scripts/codeflow/cli/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_acceptance_manifest.py +347 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_acceptance_runner.py +1490 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_audit.py +533 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_design_lint.py +172 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_evidence_import.py +607 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_feedback.py +53 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_scenarios.py +58 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_spec_context.py +1175 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_spec_gate.py +60 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_spec_session.py +46 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_stats.py +540 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_sync.py +144 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_task_index.py +177 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_task_parallel.py +1142 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_task_workflow.py +742 -0
- package/src/core/code-flow/scripts/codeflow/cli/cf_validation.py +31 -0
- package/src/core/code-flow/scripts/codeflow/core/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_context_cache.py +67 -0
- package/src/core/code-flow/scripts/{cf_core.py → codeflow/core/cf_core.py} +119 -297
- package/src/core/code-flow/scripts/codeflow/core/cf_exec_base.py +325 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_fs.py +57 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_git.py +92 -0
- package/src/core/code-flow/scripts/{cf_log.py → codeflow/core/cf_log.py} +13 -7
- package/src/core/code-flow/scripts/codeflow/core/cf_markdown.py +77 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_native_paths.py +26 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_patch.py +33 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_runtime_install.py +32 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_stop_guard.py +53 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_task_state.py +9 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_validators.py +408 -0
- package/src/core/code-flow/scripts/codeflow/core/cf_worktree_env.py +253 -0
- package/src/core/code-flow/scripts/codeflow/evidence/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/evidence/cf_evidence_store.py +527 -0
- package/src/core/code-flow/scripts/codeflow/hooks/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/hooks/cf_codex_hook.py +95 -0
- package/src/core/code-flow/scripts/codeflow/hooks/cf_opencode_event.py +98 -0
- package/src/core/code-flow/scripts/codeflow/hooks/cf_post_hook.py +30 -0
- package/src/core/code-flow/scripts/codeflow/hooks/cf_pre_tool_hook.py +41 -0
- package/src/core/code-flow/scripts/codeflow/hooks/cf_stop_hook.py +292 -0
- package/src/core/code-flow/scripts/codeflow/hooks/cf_user_prompt_hook.py +161 -0
- package/src/core/code-flow/scripts/codeflow/spec/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_active_task.py +412 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_context_refresh.py +301 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_edit_service.py +149 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_injection.py +111 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_rule_readiness.py +114 -0
- package/src/core/code-flow/scripts/{cf_scan.py → codeflow/spec/cf_scan.py} +93 -21
- package/src/core/code-flow/scripts/codeflow/spec/cf_spec_budget.py +55 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_spec_context_model.py +477 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_spec_impact.py +119 -0
- package/src/core/code-flow/scripts/{cf_spec_metadata.py → codeflow/spec/cf_spec_metadata.py} +5 -2
- package/src/core/code-flow/scripts/{cf_spec_migrate.py → codeflow/spec/cf_spec_migrate.py} +27 -10
- package/src/core/code-flow/scripts/codeflow/spec/cf_spec_optimize.py +193 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_spec_optimize_apply.py +187 -0
- package/src/core/code-flow/scripts/{cf_spec_resolver.py → codeflow/spec/cf_spec_resolver.py} +39 -17
- package/src/core/code-flow/scripts/{cf_spec_router.py → codeflow/spec/cf_spec_router.py} +96 -36
- package/src/core/code-flow/scripts/{cf_spec_verify.py → codeflow/spec/cf_spec_verify.py} +163 -35
- package/src/core/code-flow/scripts/codeflow/spec/cf_stage_gate.py +330 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_task_projection.py +83 -0
- package/src/core/code-flow/scripts/codeflow/spec/cf_workflow_service.py +755 -0
- package/src/core/code-flow/scripts/{cf_workflow_transaction.py → codeflow/spec/cf_workflow_transaction.py} +29 -15
- package/src/core/code-flow/scripts/codeflow/task/__init__.py +0 -0
- package/src/core/code-flow/scripts/codeflow/task/cf_requirement_verification.py +867 -0
- package/src/core/code-flow/scripts/codeflow/task/cf_task_doc.py +178 -0
- package/src/core/code-flow/scripts/codeflow/task/cf_task_merge.py +635 -0
- package/src/core/code-flow/scripts/codeflow/task/cf_task_plan.py +251 -0
- package/src/core/code-flow/scripts/codeflow/task/cf_task_runtime.py +570 -0
- package/src/core/code-flow/scripts/codeflow/task/cf_validation_service.py +73 -0
- package/src/core/code-flow/scripts/runtime-resources.json +29 -0
- package/src/core/code-flow/specs/shared/design/design-frontend.md +18 -15
- package/src/core/code-flow/specs/shared/design/design-full.md +33 -23
- package/src/core/code-flow/specs/shared/design/design-lite.md +23 -13
- package/src/core/code-flow/specs/shared/scenarios-template.md +55 -0
- package/src/core/code-flow/validation.yml +9 -3
- package/src/migrate/runtime.js +213 -0
- package/src/migrate/spec-workflow.js +16 -1
- package/src/runtime.js +219 -0
- package/src/core/code-flow/scripts/cf_acceptance_evidence.py +0 -100
- package/src/core/code-flow/scripts/cf_acceptance_schema.py +0 -83
- package/src/core/code-flow/scripts/cf_exec_base.py +0 -112
- package/src/core/code-flow/scripts/cf_task_runtime.py +0 -304
- package/src/core/code-flow/scripts/cf_task_state.py +0 -3
- package/src/core/code-flow/scripts/cf_workflow_service.py +0 -366
- /package/src/core/code-flow/scripts/{cf_session_state.py → codeflow/core/cf_session_state.py} +0 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""能力级 plan 预算评估:解析 TASK 的 Estimate/Capability,输出合并门禁。
|
|
3
|
+
|
|
4
|
+
默认 `capability` 粒度要求每个任务 1-2 天、可独立演示;任务数超过 25 或
|
|
5
|
+
平均任务时长低于 2h 时返回 `requires_merge=true`,调用方必须先合并再进入
|
|
6
|
+
start。`file` 粒度保留旧版 1-3 文件 / 15-60 分钟拆分方式,仅受任务数约束。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import re
|
|
15
|
+
import sys
|
|
16
|
+
from dataclasses import dataclass
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import IO, Optional, Sequence
|
|
19
|
+
|
|
20
|
+
from codeflow.task.cf_task_doc import TaskDocumentError, parse_task_document
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
HOURS_PER_DAY = 8.0
|
|
24
|
+
DEFAULT_MAX_TASKS = 25
|
|
25
|
+
DEFAULT_MIN_AVERAGE_HOURS = 2.0
|
|
26
|
+
TASK_SIZES = ("capability", "file")
|
|
27
|
+
|
|
28
|
+
_ESTIMATE_RE = re.compile(
|
|
29
|
+
r"(?i)(\d+(?:\.\d+)?)\s*(?:[-–]\s*(\d+(?:\.\d+)?))?\s*"
|
|
30
|
+
r"(days?|day|d|hrs?|hours?|hr|h|分钟|minutes?|mins?|m|小时|天)\b"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class PlanTask:
|
|
36
|
+
task_id: str
|
|
37
|
+
title: str
|
|
38
|
+
status: str
|
|
39
|
+
estimate_raw: str
|
|
40
|
+
hours: Optional[float]
|
|
41
|
+
capability: str
|
|
42
|
+
scenario_count: int
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def parse_estimate_hours(text: str) -> Optional[float]:
|
|
46
|
+
"""把 `1.5d` / `4h` / `90m` / `2-3d` 转成小时;范围取上限,无法解析返回 None。"""
|
|
47
|
+
match = _ESTIMATE_RE.search(text or "")
|
|
48
|
+
if match is None:
|
|
49
|
+
return None
|
|
50
|
+
value = float(match.group(2) or match.group(1))
|
|
51
|
+
unit = match.group(3).lower()
|
|
52
|
+
if unit in ("d", "day", "days", "天"):
|
|
53
|
+
return round(value * HOURS_PER_DAY, 2)
|
|
54
|
+
if unit in ("h", "hr", "hrs", "hour", "hours", "小时"):
|
|
55
|
+
return round(value, 2)
|
|
56
|
+
return round(value / 60.0, 2)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _field(section: str, name: str) -> str:
|
|
60
|
+
match = re.search(rf"(?m)^- \*\*{name}\*\*:[ \t]*(.*)$", section)
|
|
61
|
+
return match.group(1).strip() if match else ""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _section_body(section: str, heading: str) -> str:
|
|
65
|
+
match = re.search(rf"(?ms)^### {re.escape(heading)}\s*$\n(.*?)(?=^### |^## |\Z)", section)
|
|
66
|
+
return match.group(1).strip() if match else ""
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _capability(section: str) -> str:
|
|
70
|
+
explicit = _field(section, "Capability") or _field(section, "Demo")
|
|
71
|
+
if explicit:
|
|
72
|
+
return explicit
|
|
73
|
+
for line in _section_body(section, "Description").splitlines():
|
|
74
|
+
stripped = line.strip().lstrip("-* ").strip()
|
|
75
|
+
if stripped:
|
|
76
|
+
return stripped
|
|
77
|
+
return ""
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _scenario_count(section: str) -> int:
|
|
81
|
+
refs = _field(section, "Acceptance-Refs")
|
|
82
|
+
ids = set(re.findall(r"\b(?:S|E|B|RULE)-\d+\b", refs))
|
|
83
|
+
if ids:
|
|
84
|
+
return len(ids)
|
|
85
|
+
contract = _section_body(section, "Acceptance Contract")
|
|
86
|
+
rows = re.findall(r"(?m)^\|[ \t]*((?:S|E|B|RULE)-\d+)[ \t]*\|", contract)
|
|
87
|
+
return len(set(rows))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def parse_plan_tasks(task_file: str) -> tuple[PlanTask, ...]:
|
|
91
|
+
"""解析任务文件中的 TASK 段;未知状态保留,缺少 Estimate 记为 None。"""
|
|
92
|
+
try:
|
|
93
|
+
text = Path(task_file).read_text(encoding="utf-8")
|
|
94
|
+
except (OSError, UnicodeError) as exc:
|
|
95
|
+
raise ValueError(f"task_read_error: {exc}") from exc
|
|
96
|
+
try:
|
|
97
|
+
document = parse_task_document(text, source=str(task_file))
|
|
98
|
+
except TaskDocumentError as exc:
|
|
99
|
+
raise ValueError(str(exc)) from exc
|
|
100
|
+
tasks: list[PlanTask] = []
|
|
101
|
+
for task in document.tasks:
|
|
102
|
+
estimate_raw = task.fields.get("Estimate", "")
|
|
103
|
+
tasks.append(
|
|
104
|
+
PlanTask(
|
|
105
|
+
task_id=task.id,
|
|
106
|
+
title=task.title,
|
|
107
|
+
status=task.status,
|
|
108
|
+
estimate_raw=estimate_raw,
|
|
109
|
+
hours=parse_estimate_hours(estimate_raw),
|
|
110
|
+
capability=_capability(task.body),
|
|
111
|
+
scenario_count=_scenario_count(task.body),
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
return tuple(tasks)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _warning(code: str, message: str, tasks: Sequence[str] = ()) -> dict[str, object]:
|
|
118
|
+
return {"code": code, "message": message, "tasks": list(tasks)}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _budget_warnings(
|
|
122
|
+
tasks: Sequence[PlanTask],
|
|
123
|
+
total_hours: float,
|
|
124
|
+
average_hours: float,
|
|
125
|
+
task_size: str,
|
|
126
|
+
max_tasks: int,
|
|
127
|
+
minimum_average: float,
|
|
128
|
+
) -> list[dict[str, object]]:
|
|
129
|
+
warnings: list[dict[str, object]] = []
|
|
130
|
+
missing_estimate = [task.task_id for task in tasks if task.hours is None]
|
|
131
|
+
if missing_estimate:
|
|
132
|
+
warnings.append(_warning(
|
|
133
|
+
"missing_estimate",
|
|
134
|
+
"以下 TASK 缺少可解析的 Estimate(支持 1.5d / 4h / 90m):"
|
|
135
|
+
+ ", ".join(missing_estimate)
|
|
136
|
+
+ ";无法评估计划预算,请补齐或合并。",
|
|
137
|
+
missing_estimate,
|
|
138
|
+
))
|
|
139
|
+
if len(tasks) > max_tasks:
|
|
140
|
+
warnings.append(_warning(
|
|
141
|
+
"task_count_exceeds_budget",
|
|
142
|
+
f"任务数 {len(tasks)} 超过预算上限 {max_tasks};"
|
|
143
|
+
"请按 1-2 天可演示能力合并后再拆解。",
|
|
144
|
+
))
|
|
145
|
+
if total_hours > 0 and average_hours < minimum_average:
|
|
146
|
+
warnings.append(_warning(
|
|
147
|
+
"average_task_too_small",
|
|
148
|
+
f"平均任务时长 {average_hours:.1f}h 低于 {minimum_average:.1f}h;"
|
|
149
|
+
"任务粒度过碎,请合并为可独立演示的能力任务。",
|
|
150
|
+
))
|
|
151
|
+
if task_size == "capability":
|
|
152
|
+
missing_capability = [task.task_id for task in tasks if not task.capability]
|
|
153
|
+
if missing_capability:
|
|
154
|
+
warnings.append(_warning(
|
|
155
|
+
"missing_capability",
|
|
156
|
+
"以下 TASK 缺少 Capability/Demo(用户可演示能力):"
|
|
157
|
+
+ ", ".join(missing_capability)
|
|
158
|
+
+ ";请补充能力描述或合并。",
|
|
159
|
+
missing_capability,
|
|
160
|
+
))
|
|
161
|
+
return warnings
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def evaluate_plan(
|
|
165
|
+
task_file: str,
|
|
166
|
+
task_size: str = "capability",
|
|
167
|
+
max_tasks: int = DEFAULT_MAX_TASKS,
|
|
168
|
+
min_average_hours: Optional[float] = None,
|
|
169
|
+
) -> dict[str, object]:
|
|
170
|
+
"""评估任务文件的计划预算,返回可直接 JSON 序列化的结果。"""
|
|
171
|
+
if task_size not in TASK_SIZES:
|
|
172
|
+
raise ValueError(f"unknown_task_size: {task_size}")
|
|
173
|
+
if max_tasks <= 0:
|
|
174
|
+
raise ValueError("max_tasks must be positive")
|
|
175
|
+
tasks = parse_plan_tasks(task_file)
|
|
176
|
+
total_hours = round(sum(task.hours or 0.0 for task in tasks), 2)
|
|
177
|
+
average_hours = round(total_hours / len(tasks), 2)
|
|
178
|
+
minimum_average = 0.0 if task_size == "file" else (
|
|
179
|
+
DEFAULT_MIN_AVERAGE_HOURS if min_average_hours is None else min_average_hours
|
|
180
|
+
)
|
|
181
|
+
if minimum_average < 0:
|
|
182
|
+
raise ValueError("min_average_hours must be non-negative")
|
|
183
|
+
warnings = _budget_warnings(
|
|
184
|
+
tasks, total_hours, average_hours, task_size, max_tasks, minimum_average
|
|
185
|
+
)
|
|
186
|
+
requires_merge = bool(warnings)
|
|
187
|
+
return {
|
|
188
|
+
"ok": True,
|
|
189
|
+
"task_file": str(task_file),
|
|
190
|
+
"task_size": task_size,
|
|
191
|
+
"decision": "merge_required" if requires_merge else "pass",
|
|
192
|
+
"requires_merge": requires_merge,
|
|
193
|
+
"task_count": len(tasks),
|
|
194
|
+
"estimated_task_count": sum(1 for task in tasks if task.hours is not None),
|
|
195
|
+
"total_hours": total_hours,
|
|
196
|
+
"average_hours": average_hours,
|
|
197
|
+
"max_tasks": max_tasks,
|
|
198
|
+
"min_average_hours": minimum_average,
|
|
199
|
+
"warnings": warnings,
|
|
200
|
+
"tasks": [
|
|
201
|
+
{
|
|
202
|
+
"id": task.task_id,
|
|
203
|
+
"title": task.title,
|
|
204
|
+
"status": task.status,
|
|
205
|
+
"estimate": task.estimate_raw,
|
|
206
|
+
"hours": task.hours,
|
|
207
|
+
"capability": task.capability,
|
|
208
|
+
"scenario_count": task.scenario_count,
|
|
209
|
+
}
|
|
210
|
+
for task in tasks
|
|
211
|
+
],
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _write_text(stdout: IO[str], result: dict[str, object]) -> None:
|
|
216
|
+
stdout.write(
|
|
217
|
+
f"计划预算: {result['task_count']} 任务 / 总计 {result['total_hours']}h"
|
|
218
|
+
f" / 平均 {result['average_hours']}h({result['task_size']})\n"
|
|
219
|
+
)
|
|
220
|
+
for warning in result["warnings"]:
|
|
221
|
+
stdout.write(f" ! {warning['message']}\n")
|
|
222
|
+
stdout.write(f"decision: {result['decision']}\n")
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def main(argv: Optional[Sequence[str]] = None, stdout: IO[str] = sys.stdout) -> int:
|
|
226
|
+
parser = argparse.ArgumentParser(prog=os.environ.get("CF_RUNTIME_COMMAND", "cf_task_plan.py"))
|
|
227
|
+
parser.add_argument("--task-file", required=True)
|
|
228
|
+
parser.add_argument("--task-size", choices=TASK_SIZES, default="capability")
|
|
229
|
+
parser.add_argument("--max-tasks", type=int, default=DEFAULT_MAX_TASKS)
|
|
230
|
+
parser.add_argument("--min-average-hours", type=float, default=None)
|
|
231
|
+
parser.add_argument("--json", action="store_true")
|
|
232
|
+
args = parser.parse_args(argv)
|
|
233
|
+
try:
|
|
234
|
+
result = evaluate_plan(
|
|
235
|
+
args.task_file,
|
|
236
|
+
task_size=args.task_size,
|
|
237
|
+
max_tasks=args.max_tasks,
|
|
238
|
+
min_average_hours=args.min_average_hours,
|
|
239
|
+
)
|
|
240
|
+
except (OSError, ValueError) as exc:
|
|
241
|
+
stdout.write(json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False))
|
|
242
|
+
return 2
|
|
243
|
+
if args.json:
|
|
244
|
+
stdout.write(json.dumps(result, ensure_ascii=False))
|
|
245
|
+
else:
|
|
246
|
+
_write_text(stdout, result)
|
|
247
|
+
return 0 if result["decision"] == "pass" else 1
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
if __name__ == "__main__":
|
|
251
|
+
raise SystemExit(main())
|