@elvis1513/auto-coding-skill 2.0.5 → 2.1.0
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 +63 -0
- package/cli/assets/agents/explorer.toml +8 -6
- package/cli/assets/agents/fixer.toml +8 -7
- package/cli/assets/agents/reviewer.toml +7 -6
- package/cli/assets/skill/SKILL.md +36 -8
- package/cli/assets/skill/data/templates/ENGINEERING.md +67 -0
- package/cli/assets/skill/data/templates/bridges/CLAUDE.md +3 -0
- package/cli/assets/skill/data/templates/bridges/CODEX.md +3 -0
- package/cli/assets/skill/data/templates/docs/architecture/structure-standard.md +16 -0
- package/cli/assets/skill/data/templates/docs/reviews/_TEMPLATE-REVIEW.md +2 -0
- package/cli/assets/skill/data/templates/docs/tasks/closure-log.md +1 -0
- package/cli/assets/skill/data/templates/docs/tasks/summaries/_TEMPLATE-TASK-SUMMARY.md +2 -0
- package/cli/assets/skill/data/templates/docs/tasks/taskbook.md +1 -0
- package/cli/assets/skill/scripts/ap.py +870 -8
- package/cli/src/index.js +194 -7
- package/package.json +1 -1
|
@@ -6,11 +6,13 @@ from __future__ import annotations
|
|
|
6
6
|
|
|
7
7
|
import argparse
|
|
8
8
|
import base64
|
|
9
|
+
import hashlib
|
|
9
10
|
import datetime as _dt
|
|
10
11
|
import fnmatch
|
|
11
12
|
import json
|
|
12
13
|
import os
|
|
13
14
|
import re
|
|
15
|
+
import sys
|
|
14
16
|
import time
|
|
15
17
|
import urllib.parse
|
|
16
18
|
import urllib.error
|
|
@@ -18,6 +20,11 @@ import urllib.request
|
|
|
18
20
|
from pathlib import Path
|
|
19
21
|
from typing import Optional, List
|
|
20
22
|
|
|
23
|
+
try:
|
|
24
|
+
import yaml # type: ignore
|
|
25
|
+
except Exception:
|
|
26
|
+
yaml = None
|
|
27
|
+
|
|
21
28
|
from core import APError, ensure_git_repo, copy_tree, run, load_yaml, find_config, run_shell, http_get_status
|
|
22
29
|
|
|
23
30
|
|
|
@@ -50,14 +57,92 @@ def _join_url(base: str, path: str) -> str:
|
|
|
50
57
|
return base + path
|
|
51
58
|
|
|
52
59
|
|
|
60
|
+
def _now_iso() -> str:
|
|
61
|
+
return _dt.datetime.now(_dt.timezone.utc).isoformat(timespec="seconds")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _repo_rel(repo: Path, path: Path) -> str:
|
|
65
|
+
try:
|
|
66
|
+
return str(path.resolve().relative_to(repo.resolve())).replace("\\", "/")
|
|
67
|
+
except Exception:
|
|
68
|
+
return str(path).replace("\\", "/")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _append_jsonl(path: Path, payload: dict) -> None:
|
|
72
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
with path.open("a", encoding="utf-8") as handle:
|
|
74
|
+
handle.write(json.dumps(payload, ensure_ascii=False, sort_keys=True))
|
|
75
|
+
handle.write("\n")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _evidence_log_path(repo: Path, cfg: dict) -> Path:
|
|
79
|
+
docs_cfg = cfg.get("docs") or {}
|
|
80
|
+
rel = _text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl"
|
|
81
|
+
return Path(repo, rel)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _gate_profile_path(repo: Path, cfg: dict) -> Path:
|
|
85
|
+
gate_cfg = _gate_cfg(cfg)
|
|
86
|
+
rel = _text(gate_cfg.get("profile_log")) or ".local/auto-coding-skill/gate-profile.jsonl"
|
|
87
|
+
return Path(repo, rel)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _record_evidence(repo: Path, cfg: dict, event: str, status: str, payload: Optional[dict] = None) -> None:
|
|
91
|
+
record = {
|
|
92
|
+
"timestamp": _now_iso(),
|
|
93
|
+
"event": event,
|
|
94
|
+
"status": status,
|
|
95
|
+
"repo": str(repo),
|
|
96
|
+
}
|
|
97
|
+
if payload:
|
|
98
|
+
record.update(payload)
|
|
99
|
+
try:
|
|
100
|
+
_append_jsonl(_evidence_log_path(repo, cfg), record)
|
|
101
|
+
except Exception as exc:
|
|
102
|
+
print(f"[evidence] WARN: failed to write evidence log: {exc}", file=sys.stderr)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _record_gate_profile(
|
|
106
|
+
repo: Path,
|
|
107
|
+
cfg: dict,
|
|
108
|
+
name: str,
|
|
109
|
+
status: str,
|
|
110
|
+
duration_s: float,
|
|
111
|
+
scope: str = "",
|
|
112
|
+
detail: str = "",
|
|
113
|
+
) -> None:
|
|
114
|
+
record = {
|
|
115
|
+
"timestamp": _now_iso(),
|
|
116
|
+
"name": name,
|
|
117
|
+
"status": status,
|
|
118
|
+
"duration_s": round(duration_s, 3),
|
|
119
|
+
"scope": scope,
|
|
120
|
+
"detail": detail,
|
|
121
|
+
}
|
|
122
|
+
try:
|
|
123
|
+
_append_jsonl(_gate_profile_path(repo, cfg), record)
|
|
124
|
+
except Exception as exc:
|
|
125
|
+
print(f"[gate-profile] WARN: failed to write profile log: {exc}", file=sys.stderr)
|
|
126
|
+
|
|
127
|
+
|
|
53
128
|
def _run_configured_command(repo: Path, cfg: dict, name: str) -> bool:
|
|
54
129
|
commands = (cfg.get("commands") or {})
|
|
55
130
|
command = str(commands.get(name) or "").strip()
|
|
56
131
|
if not command:
|
|
57
132
|
return False
|
|
58
133
|
print(f"[run] {name}: {command}")
|
|
59
|
-
|
|
60
|
-
|
|
134
|
+
start = time.time()
|
|
135
|
+
try:
|
|
136
|
+
run_shell(command, cwd=repo)
|
|
137
|
+
except APError as exc:
|
|
138
|
+
duration_s = time.time() - start
|
|
139
|
+
_record_gate_profile(repo, cfg, name, "fail", duration_s, detail=str(exc))
|
|
140
|
+
_record_evidence(repo, cfg, "command", "fail", {"name": name, "duration_s": round(duration_s, 3)})
|
|
141
|
+
raise
|
|
142
|
+
duration_s = time.time() - start
|
|
143
|
+
_record_gate_profile(repo, cfg, name, "pass", duration_s)
|
|
144
|
+
_record_evidence(repo, cfg, "command", "pass", {"name": name, "duration_s": round(duration_s, 3)})
|
|
145
|
+
print(f"[run] OK: {name} ({duration_s:.1f}s)")
|
|
61
146
|
return True
|
|
62
147
|
|
|
63
148
|
|
|
@@ -401,6 +486,121 @@ def cmd_install(args: argparse.Namespace) -> None:
|
|
|
401
486
|
print("[install] Next: edit docs/ENGINEERING.md frontmatter, fill all platform credentials, and commit that file into Git.")
|
|
402
487
|
|
|
403
488
|
|
|
489
|
+
def cmd_upgrade(args: argparse.Namespace) -> None:
|
|
490
|
+
repo = Path(args.repo).resolve()
|
|
491
|
+
ensure_git_repo(repo)
|
|
492
|
+
write = bool(args.write) and not bool(args.dry_run)
|
|
493
|
+
source_root = _find_skill_asset_root(repo)
|
|
494
|
+
templates = source_root / "data" / "templates"
|
|
495
|
+
actions: list[dict] = []
|
|
496
|
+
|
|
497
|
+
def add_action(kind: str, path: Path, action: str, detail: str = "") -> None:
|
|
498
|
+
actions.append({
|
|
499
|
+
"kind": kind,
|
|
500
|
+
"path": _repo_rel(repo, path),
|
|
501
|
+
"action": action,
|
|
502
|
+
"detail": detail,
|
|
503
|
+
})
|
|
504
|
+
|
|
505
|
+
tool_files = [
|
|
506
|
+
(source_root / "scripts" / "ap.py", repo / "docs" / "tools" / "autopipeline" / "ap.py"),
|
|
507
|
+
(source_root / "scripts" / "core.py", repo / "docs" / "tools" / "autopipeline" / "core.py"),
|
|
508
|
+
(source_root / "scripts" / "http_checks.py", repo / "docs" / "tools" / "autopipeline" / "http_checks.py"),
|
|
509
|
+
]
|
|
510
|
+
for src, dst in tool_files:
|
|
511
|
+
if not src.exists():
|
|
512
|
+
continue
|
|
513
|
+
if not dst.exists():
|
|
514
|
+
add_action("tool", dst, "create")
|
|
515
|
+
if write:
|
|
516
|
+
copy_tree(src, dst)
|
|
517
|
+
elif _files_differ(src, dst):
|
|
518
|
+
add_action("tool", dst, "update")
|
|
519
|
+
if write:
|
|
520
|
+
copy_tree(src, dst)
|
|
521
|
+
else:
|
|
522
|
+
add_action("tool", dst, "ok")
|
|
523
|
+
|
|
524
|
+
project_skill = repo / ".agents" / "skills" / "auto-coding-skill"
|
|
525
|
+
if project_skill.exists():
|
|
526
|
+
for src in _iter_files(source_root):
|
|
527
|
+
if ".git" in src.parts:
|
|
528
|
+
continue
|
|
529
|
+
rel = src.relative_to(source_root)
|
|
530
|
+
dst = project_skill / rel
|
|
531
|
+
if not dst.exists():
|
|
532
|
+
add_action("skill", dst, "create")
|
|
533
|
+
if write:
|
|
534
|
+
copy_tree(src, dst)
|
|
535
|
+
elif _files_differ(src, dst):
|
|
536
|
+
add_action("skill", dst, "update")
|
|
537
|
+
if write:
|
|
538
|
+
copy_tree(src, dst)
|
|
539
|
+
for dst in _iter_files(project_skill):
|
|
540
|
+
rel = dst.relative_to(project_skill)
|
|
541
|
+
if not (source_root / rel).exists():
|
|
542
|
+
add_action("skill", dst, "extra", "present only in project copy")
|
|
543
|
+
else:
|
|
544
|
+
add_action("skill", project_skill, "missing", "run autocoding init --ai codex --mode project --force")
|
|
545
|
+
|
|
546
|
+
docs_template = templates / "docs"
|
|
547
|
+
for src in _iter_files(docs_template):
|
|
548
|
+
rel = src.relative_to(docs_template)
|
|
549
|
+
dst = repo / "docs" / rel
|
|
550
|
+
if not dst.exists():
|
|
551
|
+
add_action("doc", dst, "create")
|
|
552
|
+
if write:
|
|
553
|
+
copy_tree(src, dst)
|
|
554
|
+
elif _files_differ(src, dst) and str(rel).startswith(("architecture/", "reviews/optimization-backlog.md", "reviews/project-health-baseline.md")):
|
|
555
|
+
add_action("doc", dst, "stale", "kept unchanged; review manually")
|
|
556
|
+
else:
|
|
557
|
+
add_action("doc", dst, "ok")
|
|
558
|
+
|
|
559
|
+
engineering = repo / "docs" / "ENGINEERING.md"
|
|
560
|
+
template_engineering = templates / "ENGINEERING.md"
|
|
561
|
+
if engineering.exists() and template_engineering.exists():
|
|
562
|
+
current_cfg, body = _read_frontmatter_markdown(engineering)
|
|
563
|
+
template_cfg, _ = _read_frontmatter_markdown(template_engineering)
|
|
564
|
+
merged_cfg = json.loads(json.dumps(current_cfg))
|
|
565
|
+
added_keys = _deep_merge_missing(merged_cfg, template_cfg)
|
|
566
|
+
if added_keys:
|
|
567
|
+
add_action("config", engineering, "merge", ", ".join(added_keys))
|
|
568
|
+
if write:
|
|
569
|
+
_write_frontmatter_markdown(engineering, merged_cfg, body)
|
|
570
|
+
else:
|
|
571
|
+
add_action("config", engineering, "ok")
|
|
572
|
+
elif template_engineering.exists():
|
|
573
|
+
add_action("config", engineering, "create")
|
|
574
|
+
if write:
|
|
575
|
+
copy_tree(template_engineering, engineering)
|
|
576
|
+
|
|
577
|
+
result = {
|
|
578
|
+
"source_root": str(source_root),
|
|
579
|
+
"mode": "write" if write else "dry-run",
|
|
580
|
+
"actions": actions,
|
|
581
|
+
}
|
|
582
|
+
if args.json:
|
|
583
|
+
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
584
|
+
else:
|
|
585
|
+
print(f"[upgrade] source={source_root}")
|
|
586
|
+
print(f"[upgrade] mode={'write' if write else 'dry-run'}")
|
|
587
|
+
for item in actions:
|
|
588
|
+
if item["action"] == "ok":
|
|
589
|
+
continue
|
|
590
|
+
detail = f" - {item['detail']}" if item.get("detail") else ""
|
|
591
|
+
print(f"[upgrade] {item['action']} {item['kind']}: {item['path']}{detail}")
|
|
592
|
+
if not any(item["action"] != "ok" for item in actions):
|
|
593
|
+
print("[upgrade] OK: no changes needed")
|
|
594
|
+
elif not write:
|
|
595
|
+
print("[upgrade] dry-run only; re-run with --write to apply safe updates")
|
|
596
|
+
try:
|
|
597
|
+
cfg = _load_cfg(repo)
|
|
598
|
+
except Exception:
|
|
599
|
+
cfg = {}
|
|
600
|
+
if cfg and write:
|
|
601
|
+
_record_evidence(repo, cfg, "upgrade", "pass", {"mode": result["mode"], "action_count": len(actions)})
|
|
602
|
+
|
|
603
|
+
|
|
404
604
|
def _infer_title(taskbook: Path, task_id: str) -> str:
|
|
405
605
|
if not taskbook.exists():
|
|
406
606
|
return "<Title>"
|
|
@@ -470,6 +670,8 @@ def cmd_gen_summary(args: argparse.Namespace) -> None:
|
|
|
470
670
|
## 4. 质量证据
|
|
471
671
|
- 本地轻量校验:light_gate or quick_test/test/build / api docs / jenkins / diff-check — TODO
|
|
472
672
|
- 结构检查:structure-check — TODO
|
|
673
|
+
- 结构化证据:docs/tasks/evidence.jsonl — TODO
|
|
674
|
+
- 门禁画像:.local/auto-coding-skill/gate-profile.jsonl — TODO
|
|
473
675
|
- Jenkins Build:TODO
|
|
474
676
|
- 目标环境验证:TODO
|
|
475
677
|
- 闭环记录:TODO
|
|
@@ -555,6 +757,83 @@ def _load_cfg(repo: Path) -> dict:
|
|
|
555
757
|
return load_yaml(cfg_path)
|
|
556
758
|
|
|
557
759
|
|
|
760
|
+
def _candidate_skill_roots(repo: Path) -> list[Path]:
|
|
761
|
+
return [
|
|
762
|
+
_skill_root(),
|
|
763
|
+
repo / ".agents" / "skills" / "auto-coding-skill",
|
|
764
|
+
Path.home() / ".agents" / "skills" / "auto-coding-skill",
|
|
765
|
+
repo / ".claude" / "skills" / "auto-coding-skill",
|
|
766
|
+
Path.home() / ".claude" / "skills" / "auto-coding-skill",
|
|
767
|
+
]
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
def _find_skill_asset_root(repo: Path) -> Path:
|
|
771
|
+
for root in _candidate_skill_roots(repo):
|
|
772
|
+
if (root / "data" / "templates").exists() and (root / "scripts" / "ap.py").exists():
|
|
773
|
+
return root
|
|
774
|
+
raise APError(
|
|
775
|
+
"Cannot find auto-coding-skill asset root. Run `autocoding init --ai codex --mode global --force` "
|
|
776
|
+
"or execute this command from an installed skill copy."
|
|
777
|
+
)
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
def _file_sha256(path: Path) -> str:
|
|
781
|
+
if not path.exists() or not path.is_file():
|
|
782
|
+
return ""
|
|
783
|
+
h = hashlib.sha256()
|
|
784
|
+
with path.open("rb") as handle:
|
|
785
|
+
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
|
|
786
|
+
h.update(chunk)
|
|
787
|
+
return h.hexdigest()
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
def _files_differ(a: Path, b: Path) -> bool:
|
|
791
|
+
return _file_sha256(a) != _file_sha256(b)
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
def _read_frontmatter_markdown(path: Path) -> tuple[dict, str]:
|
|
795
|
+
if not path.exists():
|
|
796
|
+
return {}, ""
|
|
797
|
+
text = path.read_text(encoding="utf-8")
|
|
798
|
+
m = re.match(r"^---\s*\n(.*?)\n---\s*(\n|$)(.*)$", text, flags=re.DOTALL)
|
|
799
|
+
if not m:
|
|
800
|
+
raise APError(f"Markdown frontmatter not found: {path}")
|
|
801
|
+
if yaml is None:
|
|
802
|
+
raise APError("PyYAML not installed. Install dependencies with: pip install pyyaml requests")
|
|
803
|
+
data = yaml.safe_load(m.group(1)) or {}
|
|
804
|
+
return data, m.group(3)
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
def _write_frontmatter_markdown(path: Path, data: dict, body: str) -> None:
|
|
808
|
+
if yaml is None:
|
|
809
|
+
raise APError("PyYAML not installed. Install dependencies with: pip install pyyaml requests")
|
|
810
|
+
dumped = yaml.safe_dump(data, allow_unicode=True, sort_keys=False).strip()
|
|
811
|
+
path.write_text(f"---\n{dumped}\n---\n{body}", encoding="utf-8")
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
def _deep_merge_missing(target: dict, template: dict, prefix: str = "") -> list[str]:
|
|
815
|
+
added: list[str] = []
|
|
816
|
+
for key, value in template.items():
|
|
817
|
+
field = f"{prefix}.{key}" if prefix else str(key)
|
|
818
|
+
if key not in target or target.get(key) is None:
|
|
819
|
+
target[key] = value
|
|
820
|
+
added.append(field)
|
|
821
|
+
continue
|
|
822
|
+
if isinstance(target.get(key), dict) and isinstance(value, dict):
|
|
823
|
+
added.extend(_deep_merge_missing(target[key], value, field))
|
|
824
|
+
return added
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
def _iter_files(root: Path) -> list[Path]:
|
|
828
|
+
if not root.exists():
|
|
829
|
+
return []
|
|
830
|
+
out: list[Path] = []
|
|
831
|
+
for path in root.rglob("*"):
|
|
832
|
+
if path.is_file() and "__pycache__" not in path.parts and path.name != ".DS_Store" and not path.name.endswith(".pyc"):
|
|
833
|
+
out.append(path)
|
|
834
|
+
return sorted(out)
|
|
835
|
+
|
|
836
|
+
|
|
558
837
|
def _workflow_mode(cfg: dict, args: Optional[argparse.Namespace] = None) -> str:
|
|
559
838
|
explicit = str(getattr(args, "mode", "") or "").strip().lower() if args else ""
|
|
560
839
|
configured = str(((cfg.get("workflow") or {}).get("mode")) or "dev").strip().lower()
|
|
@@ -933,7 +1212,10 @@ def _run_full_gate(repo: Path, cfg: dict) -> list[str]:
|
|
|
933
1212
|
|
|
934
1213
|
def _run_git_diff_check(repo: Path, cfg: dict) -> None:
|
|
935
1214
|
print("[diff-check] git diff --check")
|
|
1215
|
+
start = time.time()
|
|
936
1216
|
run(["git", "diff", "--check"], cwd=repo)
|
|
1217
|
+
_record_gate_profile(repo, cfg, "diff_check", "pass", time.time() - start)
|
|
1218
|
+
_record_evidence(repo, cfg, "diff_check", "pass")
|
|
937
1219
|
print("[diff-check] OK")
|
|
938
1220
|
|
|
939
1221
|
|
|
@@ -1027,6 +1309,70 @@ _FUNCTION_START_RE = re.compile(
|
|
|
1027
1309
|
r"|^\s*(?:public|private|protected|static|final|async|\s)+\s*[\w<>\[\], ?]+\s+[A-Za-z_]\w*\s*\([^;]*\)\s*\{"
|
|
1028
1310
|
r"|^\s*(?:export\s+)?(?:const|let|var)\s+[A-Za-z_]\w*\s*=\s*(?:async\s*)?\([^)]*\)\s*=>"
|
|
1029
1311
|
)
|
|
1312
|
+
_IMPORT_PATTERNS = [
|
|
1313
|
+
re.compile(r"^\s*import\s+(?:.+?\s+from\s+)?[\"']([^\"']+)[\"']"),
|
|
1314
|
+
re.compile(r"\brequire\(\s*[\"']([^\"']+)[\"']\s*\)"),
|
|
1315
|
+
re.compile(r"\bimport\(\s*[\"']([^\"']+)[\"']\s*\)"),
|
|
1316
|
+
re.compile(r"^\s*from\s+([A-Za-z_][\w.]*)\s+import\b"),
|
|
1317
|
+
re.compile(r"^\s*import\s+([A-Za-z_][\w.]*)(?:\s+as\s+\w+)?\s*$"),
|
|
1318
|
+
re.compile(r"^\s*import\s+[\"']([^\"']+)[\"']\s*$"),
|
|
1319
|
+
re.compile(r"^\s*[\"']([^\"']+/[^\"']*)[\"']\s*$"),
|
|
1320
|
+
]
|
|
1321
|
+
_DEFAULT_LAYER_RULES = {
|
|
1322
|
+
"enabled": True,
|
|
1323
|
+
"block": True,
|
|
1324
|
+
"rules": [
|
|
1325
|
+
{
|
|
1326
|
+
"name": "domain",
|
|
1327
|
+
"paths": ["**/domain/**", "**/domains/**", "**/model/**", "**/models/**"],
|
|
1328
|
+
"forbidden_imports": [
|
|
1329
|
+
"**/infrastructure/**",
|
|
1330
|
+
"**/infra/**",
|
|
1331
|
+
"**/adapter/**",
|
|
1332
|
+
"**/repository/**",
|
|
1333
|
+
"**/repositories/**",
|
|
1334
|
+
"**/client/**",
|
|
1335
|
+
"**/clients/**",
|
|
1336
|
+
"**/controller/**",
|
|
1337
|
+
"**/handler/**",
|
|
1338
|
+
"**/page/**",
|
|
1339
|
+
"**/pages/**",
|
|
1340
|
+
"**/component/**",
|
|
1341
|
+
"**/components/**",
|
|
1342
|
+
"**/view/**",
|
|
1343
|
+
"**/views/**",
|
|
1344
|
+
],
|
|
1345
|
+
},
|
|
1346
|
+
{
|
|
1347
|
+
"name": "application",
|
|
1348
|
+
"paths": ["**/application/**", "**/service/**", "**/services/**", "**/usecase/**", "**/usecases/**"],
|
|
1349
|
+
"forbidden_imports": [
|
|
1350
|
+
"**/controller/**",
|
|
1351
|
+
"**/handler/**",
|
|
1352
|
+
"**/page/**",
|
|
1353
|
+
"**/pages/**",
|
|
1354
|
+
"**/component/**",
|
|
1355
|
+
"**/components/**",
|
|
1356
|
+
"**/view/**",
|
|
1357
|
+
"**/views/**",
|
|
1358
|
+
],
|
|
1359
|
+
},
|
|
1360
|
+
{
|
|
1361
|
+
"name": "shared",
|
|
1362
|
+
"paths": ["**/shared/**", "**/common/**", "**/utils/**", "**/lib/**"],
|
|
1363
|
+
"forbidden_imports": [
|
|
1364
|
+
"**/domain/**",
|
|
1365
|
+
"**/application/**",
|
|
1366
|
+
"**/service/**",
|
|
1367
|
+
"**/services/**",
|
|
1368
|
+
"**/infrastructure/**",
|
|
1369
|
+
"**/controller/**",
|
|
1370
|
+
"**/page/**",
|
|
1371
|
+
"**/pages/**",
|
|
1372
|
+
],
|
|
1373
|
+
},
|
|
1374
|
+
],
|
|
1375
|
+
}
|
|
1030
1376
|
|
|
1031
1377
|
|
|
1032
1378
|
def _structure_cfg(cfg: dict) -> dict:
|
|
@@ -1163,6 +1509,85 @@ def _function_size_warnings(path: str, lines: list[str], threshold: int) -> list
|
|
|
1163
1509
|
return warnings
|
|
1164
1510
|
|
|
1165
1511
|
|
|
1512
|
+
def _layer_rules_config(structure_cfg: dict) -> dict:
|
|
1513
|
+
raw = structure_cfg.get("layer_rules")
|
|
1514
|
+
if not isinstance(raw, dict):
|
|
1515
|
+
return dict(_DEFAULT_LAYER_RULES)
|
|
1516
|
+
merged = dict(_DEFAULT_LAYER_RULES)
|
|
1517
|
+
merged.update(raw)
|
|
1518
|
+
if "rules" not in raw:
|
|
1519
|
+
merged["rules"] = _DEFAULT_LAYER_RULES["rules"]
|
|
1520
|
+
return merged
|
|
1521
|
+
|
|
1522
|
+
|
|
1523
|
+
def _extract_import_targets(path: str, lines: list[str]) -> list[tuple[int, str]]:
|
|
1524
|
+
suffix = Path(path).suffix.lower()
|
|
1525
|
+
if suffix not in {".py", ".js", ".jsx", ".ts", ".tsx", ".vue", ".go", ".java", ".kt", ".kts", ".rs", ".swift"}:
|
|
1526
|
+
return []
|
|
1527
|
+
targets: list[tuple[int, str]] = []
|
|
1528
|
+
for index, line in enumerate(lines):
|
|
1529
|
+
stripped = line.strip()
|
|
1530
|
+
if not stripped or stripped.startswith(("#", "//", "*")):
|
|
1531
|
+
continue
|
|
1532
|
+
for pattern in _IMPORT_PATTERNS:
|
|
1533
|
+
match = pattern.search(line)
|
|
1534
|
+
if match:
|
|
1535
|
+
targets.append((index + 1, match.group(1).strip()))
|
|
1536
|
+
break
|
|
1537
|
+
return targets
|
|
1538
|
+
|
|
1539
|
+
|
|
1540
|
+
def _resolve_relative_import(source_path: str, target: str) -> str:
|
|
1541
|
+
normalized = target.replace("\\", "/")
|
|
1542
|
+
if not normalized.startswith("."):
|
|
1543
|
+
return normalized
|
|
1544
|
+
source_dir = Path(source_path).parent
|
|
1545
|
+
parts = []
|
|
1546
|
+
for part in (source_dir / normalized).parts:
|
|
1547
|
+
if part in {"", "."}:
|
|
1548
|
+
continue
|
|
1549
|
+
if part == "..":
|
|
1550
|
+
if parts:
|
|
1551
|
+
parts.pop()
|
|
1552
|
+
continue
|
|
1553
|
+
parts.append(part)
|
|
1554
|
+
return "/".join(parts)
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
def _import_matches_pattern(target: str, pattern: str) -> bool:
|
|
1558
|
+
normalized = target.replace("\\", "/").lstrip("./")
|
|
1559
|
+
dotted_as_path = normalized.replace(".", "/")
|
|
1560
|
+
pat = pattern.replace("\\", "/").lstrip("./")
|
|
1561
|
+
if _path_matches(normalized, [pat]) or _path_matches(dotted_as_path, [pat]):
|
|
1562
|
+
return True
|
|
1563
|
+
marker = pat.replace("**/", "").replace("/**", "").strip("/")
|
|
1564
|
+
if marker and (f"/{marker}/" in f"/{normalized}/" or f"/{marker}/" in f"/{dotted_as_path}/"):
|
|
1565
|
+
return True
|
|
1566
|
+
return False
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
def _structure_boundary_issues(path: str, lines: list[str], structure_cfg: dict) -> list[str]:
|
|
1570
|
+
layer_cfg = _layer_rules_config(structure_cfg)
|
|
1571
|
+
if not _bool_config(layer_cfg.get("enabled"), True):
|
|
1572
|
+
return []
|
|
1573
|
+
rules = [rule for rule in _as_list(layer_cfg.get("rules")) if isinstance(rule, dict)]
|
|
1574
|
+
if not rules:
|
|
1575
|
+
return []
|
|
1576
|
+
matching_rules = [rule for rule in rules if _path_matches(path, _as_list(rule.get("paths")))]
|
|
1577
|
+
if not matching_rules:
|
|
1578
|
+
return []
|
|
1579
|
+
imports = _extract_import_targets(path, lines)
|
|
1580
|
+
issues: list[str] = []
|
|
1581
|
+
for rule in matching_rules:
|
|
1582
|
+
name = _text(rule.get("name")) or "layer"
|
|
1583
|
+
forbidden = _as_list(rule.get("forbidden_imports"))
|
|
1584
|
+
for line_no, target in imports:
|
|
1585
|
+
resolved = _resolve_relative_import(path, target)
|
|
1586
|
+
if any(_import_matches_pattern(target, pattern) or _import_matches_pattern(resolved, pattern) for pattern in forbidden):
|
|
1587
|
+
issues.append(f"{path}:{line_no} layer '{name}' imports forbidden target '{target}'")
|
|
1588
|
+
return issues
|
|
1589
|
+
|
|
1590
|
+
|
|
1166
1591
|
def _configured_structure_docs(cfg: dict) -> dict[str, str]:
|
|
1167
1592
|
docs_cfg = cfg.get("docs") or {}
|
|
1168
1593
|
return {
|
|
@@ -1173,12 +1598,12 @@ def _configured_structure_docs(cfg: dict) -> dict[str, str]:
|
|
|
1173
1598
|
|
|
1174
1599
|
|
|
1175
1600
|
def _structure_gate_enabled(cfg: dict) -> bool:
|
|
1176
|
-
if _configured_command(cfg, "structure_check"):
|
|
1177
|
-
return True
|
|
1178
1601
|
structure_cfg = cfg.get("structure")
|
|
1179
|
-
if
|
|
1602
|
+
if isinstance(structure_cfg, dict) and not _bool_config(structure_cfg.get("enabled"), default=True):
|
|
1180
1603
|
return False
|
|
1181
|
-
|
|
1604
|
+
if _configured_command(cfg, "structure_check"):
|
|
1605
|
+
return True
|
|
1606
|
+
return isinstance(structure_cfg, dict)
|
|
1182
1607
|
|
|
1183
1608
|
|
|
1184
1609
|
def _print_limited(label: str, items: list[str], max_items: int = 60) -> None:
|
|
@@ -1208,6 +1633,8 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
1208
1633
|
warn_function_lines = _int_config(structure_cfg.get("max_function_lines_warn"), 120)
|
|
1209
1634
|
max_added_to_large = _int_config(structure_cfg.get("max_added_lines_to_large_file"), 80)
|
|
1210
1635
|
block_large_growth = _bool_config(structure_cfg.get("block_new_responsibility_in_large_file"), True)
|
|
1636
|
+
layer_cfg = _layer_rules_config(structure_cfg)
|
|
1637
|
+
block_layer_violations = _bool_config(layer_cfg.get("block"), True)
|
|
1211
1638
|
|
|
1212
1639
|
paths = _structure_paths_for_scope(repo, selected_scope, base_ref, structure_cfg)
|
|
1213
1640
|
added_by_path = _added_lines_by_path(repo, base_ref=base_ref) if selected_scope != "full" else {}
|
|
@@ -1247,6 +1674,11 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
1247
1674
|
)
|
|
1248
1675
|
|
|
1249
1676
|
warnings.extend(_function_size_warnings(path, lines, warn_function_lines))
|
|
1677
|
+
boundary_issues = _structure_boundary_issues(path, lines, structure_cfg)
|
|
1678
|
+
if block_layer_violations:
|
|
1679
|
+
blocking.extend(boundary_issues)
|
|
1680
|
+
else:
|
|
1681
|
+
warnings.extend(boundary_issues)
|
|
1250
1682
|
|
|
1251
1683
|
missing_docs: list[str] = []
|
|
1252
1684
|
for key, rel_path in _configured_structure_docs(cfg).items():
|
|
@@ -1271,9 +1703,12 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
1271
1703
|
_print_limited("[structure-check] warnings", warnings)
|
|
1272
1704
|
|
|
1273
1705
|
if blocking:
|
|
1706
|
+
_record_evidence(repo, cfg, "structure_check", "fail", result)
|
|
1274
1707
|
raise APError(f"structure-check failed with {len(blocking)} blocking issue(s)")
|
|
1275
1708
|
|
|
1276
|
-
|
|
1709
|
+
_record_evidence(repo, cfg, "structure_check", "pass", result)
|
|
1710
|
+
if not getattr(args, "json", False):
|
|
1711
|
+
print(f"[structure-check] OK scope={selected_scope} inspected={inspected} warnings={len(warnings)}")
|
|
1277
1712
|
|
|
1278
1713
|
|
|
1279
1714
|
def _run_structure_check_for_gate(repo: Path, cfg: dict, selected_scope: str, base_ref: str) -> list[str]:
|
|
@@ -1293,6 +1728,370 @@ def _run_structure_check_for_gate(repo: Path, cfg: dict, selected_scope: str, ba
|
|
|
1293
1728
|
return ["structure_check"]
|
|
1294
1729
|
|
|
1295
1730
|
|
|
1731
|
+
def _current_git_ref(repo: Path) -> str:
|
|
1732
|
+
value = run(["git", "rev-parse", "--short=12", "HEAD"], cwd=repo, check=False).stdout.strip()
|
|
1733
|
+
return value or "uncommitted"
|
|
1734
|
+
|
|
1735
|
+
|
|
1736
|
+
def _directory_hotspots(paths: list[str]) -> list[tuple[str, int]]:
|
|
1737
|
+
counts: dict[str, int] = {}
|
|
1738
|
+
for path in paths:
|
|
1739
|
+
parts = path.split("/")
|
|
1740
|
+
if len(parts) <= 1:
|
|
1741
|
+
key = "."
|
|
1742
|
+
elif parts[0] in {"frontend", "backend", "packages", "apps", "services"} and len(parts) >= 3:
|
|
1743
|
+
key = "/".join(parts[:3])
|
|
1744
|
+
else:
|
|
1745
|
+
key = "/".join(parts[:2])
|
|
1746
|
+
counts[key] = counts.get(key, 0) + 1
|
|
1747
|
+
return sorted(counts.items(), key=lambda item: (-item[1], item[0]))[:20]
|
|
1748
|
+
|
|
1749
|
+
|
|
1750
|
+
def _scan_structure_inventory(repo: Path, cfg: dict) -> dict:
|
|
1751
|
+
structure_cfg = _structure_cfg(cfg)
|
|
1752
|
+
warn_file_lines = _int_config(structure_cfg.get("max_file_lines_warn"), 800)
|
|
1753
|
+
block_file_lines = _int_config(structure_cfg.get("max_file_lines_block"), 1500)
|
|
1754
|
+
paths = [path for path in _tracked_files(repo) if _is_structure_candidate(path, structure_cfg)]
|
|
1755
|
+
large_files: list[dict] = []
|
|
1756
|
+
for path in paths:
|
|
1757
|
+
lines = _read_text_lines(repo, path)
|
|
1758
|
+
if lines is None:
|
|
1759
|
+
continue
|
|
1760
|
+
line_count = len(lines)
|
|
1761
|
+
if line_count > warn_file_lines:
|
|
1762
|
+
large_files.append({
|
|
1763
|
+
"path": path,
|
|
1764
|
+
"lines": line_count,
|
|
1765
|
+
"priority": "P1" if block_file_lines > 0 and line_count > block_file_lines else "P2",
|
|
1766
|
+
})
|
|
1767
|
+
large_files.sort(key=lambda item: (-int(item["lines"]), str(item["path"])))
|
|
1768
|
+
return {
|
|
1769
|
+
"files_inspected": len(paths),
|
|
1770
|
+
"large_files": large_files,
|
|
1771
|
+
"hotspots": _directory_hotspots(paths),
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
|
|
1775
|
+
def _render_health_baseline(repo: Path, cfg: dict, inventory: dict) -> str:
|
|
1776
|
+
date = _dt.date.today().isoformat()
|
|
1777
|
+
commit = _current_git_ref(repo)
|
|
1778
|
+
large_rows = "\n".join(
|
|
1779
|
+
f"| `{item['path']}` | {item['lines']} lines | Accepted for baseline; no large new additions without extraction | {date} |"
|
|
1780
|
+
for item in inventory["large_files"][:30]
|
|
1781
|
+
) or "| (none) | | | |"
|
|
1782
|
+
debt_rows = "\n".join(
|
|
1783
|
+
f"| DEBT-{idx:03d} | {item['priority']} | `{item['path']}` | Large file baseline: {item['lines']} lines | Existing state at baseline | New large additions or touched feature work |"
|
|
1784
|
+
for idx, item in enumerate(inventory["large_files"][:30], start=1)
|
|
1785
|
+
) or "| (none) | | | | | |"
|
|
1786
|
+
return f"""# Project Health Baseline
|
|
1787
|
+
|
|
1788
|
+
> Generated by `ap.py baseline init`. Update this file when accepted structure debt changes.
|
|
1789
|
+
|
|
1790
|
+
- Baseline date: {date}
|
|
1791
|
+
- Baseline commit: `{commit}`
|
|
1792
|
+
- Owner: TODO
|
|
1793
|
+
- Review scope: repo
|
|
1794
|
+
- Standard: `docs/architecture/structure-standard.md`
|
|
1795
|
+
- Files inspected: {inventory['files_inspected']}
|
|
1796
|
+
|
|
1797
|
+
## 1. Current Accepted Structure
|
|
1798
|
+
|
|
1799
|
+
| Area | Current state | Accepted because | Review date |
|
|
1800
|
+
| --- | --- | --- | --- |
|
|
1801
|
+
{large_rows}
|
|
1802
|
+
|
|
1803
|
+
## 2. Closed Optimization Scope
|
|
1804
|
+
|
|
1805
|
+
| ID | Closed date | Scope | Acceptance evidence |
|
|
1806
|
+
| --- | --- | --- | --- |
|
|
1807
|
+
| (none) | | | |
|
|
1808
|
+
|
|
1809
|
+
## 3. Accepted Debt
|
|
1810
|
+
|
|
1811
|
+
| ID | Priority | Scope | Debt | Why accepted | Revisit trigger |
|
|
1812
|
+
| --- | --- | --- | --- | --- | --- |
|
|
1813
|
+
{debt_rows}
|
|
1814
|
+
|
|
1815
|
+
## 4. Priority Rules
|
|
1816
|
+
|
|
1817
|
+
- P0: blocks build, release, deploy, core user flow, data integrity, security, or compliance.
|
|
1818
|
+
- P1: clear architectural violation, contract drift, missing test around high-risk change, or recently introduced maintainability risk.
|
|
1819
|
+
- P2: planned debt such as large files, hot directories, module extraction, stronger tests, or tool consolidation.
|
|
1820
|
+
- P3: optional naming, style, polish, or further abstraction.
|
|
1821
|
+
|
|
1822
|
+
## 5. Completion Standard
|
|
1823
|
+
|
|
1824
|
+
An optimization task is complete when all are true:
|
|
1825
|
+
|
|
1826
|
+
- The scoped P0/P1/P2 items listed for this task are closed.
|
|
1827
|
+
- Local gate passed, including structure check when enabled.
|
|
1828
|
+
- No new unclassified P0/P1 was introduced.
|
|
1829
|
+
- Remaining P2/P3 items are either in `docs/reviews/optimization-backlog.md` or accepted debt above.
|
|
1830
|
+
- Review output says explicitly whether the project meets this baseline.
|
|
1831
|
+
|
|
1832
|
+
## 6. New Review Instructions
|
|
1833
|
+
|
|
1834
|
+
- Read this baseline first.
|
|
1835
|
+
- Read `docs/reviews/optimization-backlog.md` second.
|
|
1836
|
+
- Report only new or worsened issues, unrecorded P0/P1, priority upgrades, or baseline drift.
|
|
1837
|
+
"""
|
|
1838
|
+
|
|
1839
|
+
|
|
1840
|
+
def _render_optimization_backlog(inventory: dict) -> str:
|
|
1841
|
+
date = _dt.date.today().isoformat()
|
|
1842
|
+
rows = "\n".join(
|
|
1843
|
+
f"| OPT-{idx:03d} | {item['priority']} | accepted-debt | `{item['path']}` | Split or reduce {item['lines']}-line file | Existing large file baseline | No new responsibilities; extract during touched feature work | {date} |"
|
|
1844
|
+
for idx, item in enumerate(inventory["large_files"][:30], start=1)
|
|
1845
|
+
) or "| (none) | | | | | | | |"
|
|
1846
|
+
hotspot_rows = "\n".join(
|
|
1847
|
+
f"| HOT-{idx:03d} | P3 | open | `{path}` | Review module cohesion ({count} tracked source files) | Hotspot directory | Split only when feature work naturally touches this area | {date} |"
|
|
1848
|
+
for idx, (path, count) in enumerate(inventory["hotspots"][:10], start=1)
|
|
1849
|
+
)
|
|
1850
|
+
if hotspot_rows:
|
|
1851
|
+
rows = rows + "\n" + hotspot_rows
|
|
1852
|
+
return f"""# Optimization Backlog
|
|
1853
|
+
|
|
1854
|
+
> Generated by `ap.py baseline init`. Accepted debt is not a current-task failure unless it worsens or is touched by new feature work.
|
|
1855
|
+
|
|
1856
|
+
## Status Values
|
|
1857
|
+
|
|
1858
|
+
- `open`: confirmed and waiting for planning.
|
|
1859
|
+
- `accepted-debt`: accepted current debt with a revisit trigger.
|
|
1860
|
+
- `in-progress`: currently being handled.
|
|
1861
|
+
- `closed`: completed with evidence.
|
|
1862
|
+
- `superseded`: replaced by another item.
|
|
1863
|
+
|
|
1864
|
+
## Backlog
|
|
1865
|
+
|
|
1866
|
+
| ID | Priority | Status | Scope | Item | Reason | Acceptance | Last reviewed |
|
|
1867
|
+
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
1868
|
+
{rows}
|
|
1869
|
+
|
|
1870
|
+
## Review Rules
|
|
1871
|
+
|
|
1872
|
+
- P0/P1 cannot remain in backlog without immediate owner and plan.
|
|
1873
|
+
- P2 needs scope, reason, and acceptance.
|
|
1874
|
+
- P3 does not block completion.
|
|
1875
|
+
- New reviews update existing rows instead of duplicating equivalent findings.
|
|
1876
|
+
"""
|
|
1877
|
+
|
|
1878
|
+
|
|
1879
|
+
def _update_accepted_debt_paths(repo: Path, cfg: dict, paths: list[str]) -> list[str]:
|
|
1880
|
+
if not paths:
|
|
1881
|
+
return []
|
|
1882
|
+
engineering = find_config(repo)
|
|
1883
|
+
current_cfg, body = _read_frontmatter_markdown(engineering)
|
|
1884
|
+
structure_cfg = current_cfg.setdefault("structure", {})
|
|
1885
|
+
existing = [str(item) for item in _as_list(structure_cfg.get("accepted_debt_paths")) if str(item).strip()]
|
|
1886
|
+
added = [path for path in paths if path not in existing]
|
|
1887
|
+
if added:
|
|
1888
|
+
structure_cfg["accepted_debt_paths"] = existing + added
|
|
1889
|
+
_write_frontmatter_markdown(engineering, current_cfg, body)
|
|
1890
|
+
return added
|
|
1891
|
+
|
|
1892
|
+
|
|
1893
|
+
def cmd_baseline_init(args: argparse.Namespace) -> None:
|
|
1894
|
+
repo = Path(args.repo).resolve()
|
|
1895
|
+
ensure_git_repo(repo)
|
|
1896
|
+
cfg = _load_cfg(repo)
|
|
1897
|
+
docs_cfg = cfg.get("docs") or {}
|
|
1898
|
+
inventory = _scan_structure_inventory(repo, cfg)
|
|
1899
|
+
health_path = Path(repo, _text(docs_cfg.get("health_baseline")) or "docs/reviews/project-health-baseline.md")
|
|
1900
|
+
backlog_path = Path(repo, _text(docs_cfg.get("optimization_backlog")) or "docs/reviews/optimization-backlog.md")
|
|
1901
|
+
write = bool(args.write)
|
|
1902
|
+
force = bool(args.force)
|
|
1903
|
+
update_config = bool(args.update_config)
|
|
1904
|
+
|
|
1905
|
+
actions: list[dict] = []
|
|
1906
|
+
for path, content, label in [
|
|
1907
|
+
(health_path, _render_health_baseline(repo, cfg, inventory), "health_baseline"),
|
|
1908
|
+
(backlog_path, _render_optimization_backlog(inventory), "optimization_backlog"),
|
|
1909
|
+
]:
|
|
1910
|
+
if path.exists() and not force:
|
|
1911
|
+
actions.append({"path": _repo_rel(repo, path), "action": "exists", "kind": label})
|
|
1912
|
+
continue
|
|
1913
|
+
actions.append({"path": _repo_rel(repo, path), "action": "write" if write else "would-write", "kind": label})
|
|
1914
|
+
if write:
|
|
1915
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
1916
|
+
path.write_text(content, encoding="utf-8")
|
|
1917
|
+
|
|
1918
|
+
accepted_added: list[str] = []
|
|
1919
|
+
accepted_paths = [str(item["path"]) for item in inventory["large_files"] if item["priority"] in {"P1", "P2"}]
|
|
1920
|
+
if write and update_config:
|
|
1921
|
+
accepted_added = _update_accepted_debt_paths(repo, cfg, accepted_paths)
|
|
1922
|
+
if accepted_added:
|
|
1923
|
+
actions.append({
|
|
1924
|
+
"path": "docs/ENGINEERING.md",
|
|
1925
|
+
"action": "merge",
|
|
1926
|
+
"kind": "accepted_debt_paths",
|
|
1927
|
+
"detail": ", ".join(accepted_added),
|
|
1928
|
+
})
|
|
1929
|
+
|
|
1930
|
+
result = {
|
|
1931
|
+
"files_inspected": inventory["files_inspected"],
|
|
1932
|
+
"large_files": inventory["large_files"],
|
|
1933
|
+
"hotspots": inventory["hotspots"],
|
|
1934
|
+
"actions": actions,
|
|
1935
|
+
"updated_accepted_debt_paths": accepted_added,
|
|
1936
|
+
}
|
|
1937
|
+
if args.json:
|
|
1938
|
+
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
1939
|
+
else:
|
|
1940
|
+
print(f"[baseline] files_inspected={inventory['files_inspected']}")
|
|
1941
|
+
print(f"[baseline] large_files={len(inventory['large_files'])}")
|
|
1942
|
+
for item in actions:
|
|
1943
|
+
detail = f" - {item.get('detail')}" if item.get("detail") else ""
|
|
1944
|
+
print(f"[baseline] {item['action']} {item['kind']}: {item['path']}{detail}")
|
|
1945
|
+
if not write:
|
|
1946
|
+
print("[baseline] dry-run only; re-run with --write to create baseline files")
|
|
1947
|
+
|
|
1948
|
+
if write:
|
|
1949
|
+
_record_evidence(
|
|
1950
|
+
repo,
|
|
1951
|
+
cfg,
|
|
1952
|
+
"baseline_init",
|
|
1953
|
+
"pass",
|
|
1954
|
+
{"write": write, "large_file_count": len(inventory["large_files"]), "files_inspected": inventory["files_inspected"]},
|
|
1955
|
+
)
|
|
1956
|
+
|
|
1957
|
+
|
|
1958
|
+
def _read_jsonl(path: Path) -> list[dict]:
|
|
1959
|
+
if not path.exists():
|
|
1960
|
+
return []
|
|
1961
|
+
records: list[dict] = []
|
|
1962
|
+
for line in path.read_text(encoding="utf-8").splitlines():
|
|
1963
|
+
if not line.strip():
|
|
1964
|
+
continue
|
|
1965
|
+
try:
|
|
1966
|
+
records.append(json.loads(line))
|
|
1967
|
+
except json.JSONDecodeError:
|
|
1968
|
+
continue
|
|
1969
|
+
return records
|
|
1970
|
+
|
|
1971
|
+
|
|
1972
|
+
def cmd_gate_profile(args: argparse.Namespace) -> None:
|
|
1973
|
+
repo = Path(args.repo).resolve()
|
|
1974
|
+
cfg = _load_cfg(repo)
|
|
1975
|
+
records = _read_jsonl(_gate_profile_path(repo, cfg))
|
|
1976
|
+
limit = int(args.limit or 20)
|
|
1977
|
+
by_name: dict[str, dict] = {}
|
|
1978
|
+
for record in records:
|
|
1979
|
+
name = _text(record.get("name")) or "(unknown)"
|
|
1980
|
+
bucket = by_name.setdefault(name, {"name": name, "runs": 0, "failures": 0, "total_duration_s": 0.0, "max_duration_s": 0.0})
|
|
1981
|
+
duration = float(record.get("duration_s") or 0)
|
|
1982
|
+
bucket["runs"] += 1
|
|
1983
|
+
bucket["total_duration_s"] += duration
|
|
1984
|
+
bucket["max_duration_s"] = max(float(bucket["max_duration_s"]), duration)
|
|
1985
|
+
if _text(record.get("status")) != "pass":
|
|
1986
|
+
bucket["failures"] += 1
|
|
1987
|
+
summary = []
|
|
1988
|
+
for bucket in by_name.values():
|
|
1989
|
+
runs = int(bucket["runs"])
|
|
1990
|
+
total = float(bucket["total_duration_s"])
|
|
1991
|
+
summary.append({
|
|
1992
|
+
"name": bucket["name"],
|
|
1993
|
+
"runs": runs,
|
|
1994
|
+
"failures": int(bucket["failures"]),
|
|
1995
|
+
"failure_rate": round(int(bucket["failures"]) / runs, 3) if runs else 0,
|
|
1996
|
+
"avg_duration_s": round(total / runs, 3) if runs else 0,
|
|
1997
|
+
"max_duration_s": round(float(bucket["max_duration_s"]), 3),
|
|
1998
|
+
})
|
|
1999
|
+
summary.sort(key=lambda item: (-float(item["avg_duration_s"]), str(item["name"])))
|
|
2000
|
+
result = {"profile_log": str(_gate_profile_path(repo, cfg)), "commands": summary[:limit], "record_count": len(records)}
|
|
2001
|
+
if args.json:
|
|
2002
|
+
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
2003
|
+
else:
|
|
2004
|
+
print(f"[gate-profile] log={result['profile_log']}")
|
|
2005
|
+
print(f"[gate-profile] records={len(records)}")
|
|
2006
|
+
if not summary:
|
|
2007
|
+
print("[gate-profile] no profile records yet; run light-gate or configured commands first")
|
|
2008
|
+
for item in summary[:limit]:
|
|
2009
|
+
print(
|
|
2010
|
+
f"[gate-profile] {item['name']}: runs={item['runs']} "
|
|
2011
|
+
f"avg={item['avg_duration_s']}s max={item['max_duration_s']}s failures={item['failures']}"
|
|
2012
|
+
)
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
def _classify_paths(paths: list[str]) -> dict:
|
|
2016
|
+
categories: set[str] = set()
|
|
2017
|
+
for path in paths:
|
|
2018
|
+
lower = path.lower()
|
|
2019
|
+
if _path_matches(path, _DEFAULT_FULL_PATH_PATTERNS):
|
|
2020
|
+
categories.add("release_or_tooling")
|
|
2021
|
+
if any(token in lower for token in ["migration", "schema", "database", "/db/", "/sql/"]) or lower.endswith(".sql"):
|
|
2022
|
+
categories.add("db")
|
|
2023
|
+
if any(token in lower for token in ["api", "controller", "handler", "route", "server"]):
|
|
2024
|
+
categories.add("api")
|
|
2025
|
+
if any(token in lower for token in ["auth", "permission", "role", "tenant", "security"]):
|
|
2026
|
+
categories.add("auth")
|
|
2027
|
+
if any(token in lower for token in ["page", "component", "view", "frontend", "miniapp", ".tsx", ".jsx", ".vue", ".scss", ".css"]):
|
|
2028
|
+
categories.add("ui")
|
|
2029
|
+
if any(token in lower for token in ["test", "spec", "__tests__"]):
|
|
2030
|
+
categories.add("test")
|
|
2031
|
+
if lower.startswith("docs/") or lower.endswith(".md"):
|
|
2032
|
+
categories.add("docs")
|
|
2033
|
+
if any(token in lower for token in ["domain", "service", "usecase", "repository", "infrastructure", "adapter", "shared", "utils"]):
|
|
2034
|
+
categories.add("structure")
|
|
2035
|
+
return {
|
|
2036
|
+
"categories": sorted(categories),
|
|
2037
|
+
"needs_dd": bool(categories & {"api", "db", "auth", "release_or_tooling"}) or len(paths) > 12,
|
|
2038
|
+
"needs_adr": bool(categories & {"structure", "release_or_tooling"}) and not categories <= {"docs"},
|
|
2039
|
+
"needs_browser": "ui" in categories,
|
|
2040
|
+
"needs_jenkins": bool(categories & {"release_or_tooling", "db", "auth"}),
|
|
2041
|
+
"needs_target": bool(categories & {"api", "db", "auth", "ui"}),
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
|
|
2045
|
+
def cmd_classify(args: argparse.Namespace) -> None:
|
|
2046
|
+
repo = Path(args.repo).resolve()
|
|
2047
|
+
cfg = _load_cfg(repo)
|
|
2048
|
+
scope = str(args.scope or "").strip().lower()
|
|
2049
|
+
impact = _impact_summary(cfg, repo, requested_scope=scope, base_ref=str(args.base or ""))
|
|
2050
|
+
paths = list(impact.get("changed_files") or [])
|
|
2051
|
+
path_classification = _classify_paths(paths)
|
|
2052
|
+
selected_scope = str(impact["selected_scope"])
|
|
2053
|
+
risk = "P3"
|
|
2054
|
+
if path_classification["needs_jenkins"] or selected_scope == "full":
|
|
2055
|
+
risk = "P1"
|
|
2056
|
+
elif path_classification["needs_target"] or path_classification["needs_dd"]:
|
|
2057
|
+
risk = "P2"
|
|
2058
|
+
elif not paths:
|
|
2059
|
+
risk = "P3"
|
|
2060
|
+
commands = [
|
|
2061
|
+
"python3 docs/tools/autopipeline/ap.py impact --scope auto",
|
|
2062
|
+
"python3 docs/tools/autopipeline/ap.py structure-check --scope auto",
|
|
2063
|
+
f"python3 docs/tools/autopipeline/ap.py light-gate --scope {selected_scope} --explain",
|
|
2064
|
+
]
|
|
2065
|
+
if path_classification["needs_jenkins"]:
|
|
2066
|
+
commands.append("python3 docs/tools/autopipeline/ap.py verify-jenkins")
|
|
2067
|
+
result = {
|
|
2068
|
+
"requested_scope": impact["requested_scope"],
|
|
2069
|
+
"selected_scope": selected_scope,
|
|
2070
|
+
"risk": risk,
|
|
2071
|
+
"changed_files": paths,
|
|
2072
|
+
"categories": path_classification["categories"],
|
|
2073
|
+
"needs_dd": path_classification["needs_dd"],
|
|
2074
|
+
"needs_adr": path_classification["needs_adr"],
|
|
2075
|
+
"needs_browser": path_classification["needs_browser"],
|
|
2076
|
+
"needs_jenkins": path_classification["needs_jenkins"],
|
|
2077
|
+
"needs_target": path_classification["needs_target"],
|
|
2078
|
+
"recommended_commands": commands,
|
|
2079
|
+
"reasons": impact.get("reasons") or [],
|
|
2080
|
+
"matched_rules": impact.get("matched_rules") or [],
|
|
2081
|
+
}
|
|
2082
|
+
if args.json:
|
|
2083
|
+
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
2084
|
+
else:
|
|
2085
|
+
print(f"[classify] risk={risk}")
|
|
2086
|
+
print(f"[classify] selected_scope={selected_scope}")
|
|
2087
|
+
print("[classify] categories=" + (", ".join(result["categories"]) or "(none)"))
|
|
2088
|
+
for key in ["needs_dd", "needs_adr", "needs_browser", "needs_jenkins", "needs_target"]:
|
|
2089
|
+
print(f"[classify] {key}={result[key]}")
|
|
2090
|
+
for command in commands:
|
|
2091
|
+
print(f"[classify] command: {command}")
|
|
2092
|
+
_record_evidence(repo, cfg, "classify", "pass", result)
|
|
2093
|
+
|
|
2094
|
+
|
|
1296
2095
|
def cmd_run(args: argparse.Namespace) -> None:
|
|
1297
2096
|
"""
|
|
1298
2097
|
Run any configured gate command by name.
|
|
@@ -1318,6 +2117,7 @@ def cmd_run(args: argparse.Namespace) -> None:
|
|
|
1318
2117
|
|
|
1319
2118
|
def cmd_light_gate(args: argparse.Namespace) -> None:
|
|
1320
2119
|
repo = Path(args.repo).resolve()
|
|
2120
|
+
light_gate_start = time.time()
|
|
1321
2121
|
cmd_doctor(argparse.Namespace(repo=str(repo)))
|
|
1322
2122
|
cfg = _load_cfg(repo)
|
|
1323
2123
|
requested_scope = str(getattr(args, "scope", "") or "").strip().lower()
|
|
@@ -1342,6 +2142,15 @@ def cmd_light_gate(args: argparse.Namespace) -> None:
|
|
|
1342
2142
|
cmd_verify_api_docs(argparse.Namespace(repo=str(repo)))
|
|
1343
2143
|
cmd_verify_jenkins(argparse.Namespace(repo=str(repo)))
|
|
1344
2144
|
executed.extend(["diff_check", "verify_api_docs", "verify_jenkins"])
|
|
2145
|
+
duration_s = time.time() - light_gate_start
|
|
2146
|
+
_record_gate_profile(repo, cfg, "light_gate", "pass", duration_s, scope=selected_scope, detail=", ".join(executed))
|
|
2147
|
+
_record_evidence(
|
|
2148
|
+
repo,
|
|
2149
|
+
cfg,
|
|
2150
|
+
"light_gate",
|
|
2151
|
+
"pass",
|
|
2152
|
+
{"scope": selected_scope, "executed": executed, "duration_s": round(duration_s, 3), "changed_files": paths},
|
|
2153
|
+
)
|
|
1345
2154
|
print(f"[light-gate] OK scope={selected_scope}: " + ", ".join(executed))
|
|
1346
2155
|
|
|
1347
2156
|
|
|
@@ -1351,6 +2160,7 @@ def cmd_impact(args: argparse.Namespace) -> None:
|
|
|
1351
2160
|
scope = str(args.scope or "").strip().lower()
|
|
1352
2161
|
summary = _impact_summary(cfg, repo, requested_scope=scope, base_ref=str(args.base or ""))
|
|
1353
2162
|
_print_impact(summary, as_json=bool(args.json))
|
|
2163
|
+
_record_evidence(repo, cfg, "impact", "pass", summary)
|
|
1354
2164
|
|
|
1355
2165
|
|
|
1356
2166
|
def cmd_runtime_up(args: argparse.Namespace) -> None:
|
|
@@ -1456,6 +2266,7 @@ def cmd_verify_target(args: argparse.Namespace) -> None:
|
|
|
1456
2266
|
checks.append(f"frontend:{url}->{status}")
|
|
1457
2267
|
|
|
1458
2268
|
summary = ", ".join(checks) if checks else "health-only"
|
|
2269
|
+
_record_evidence(repo, cfg, "verify_target", "pass", {"summary": summary, "checks": checks})
|
|
1459
2270
|
print(f"[verify-target] OK: {summary}")
|
|
1460
2271
|
return summary
|
|
1461
2272
|
|
|
@@ -1483,6 +2294,7 @@ def cmd_verify_jenkins(args: argparse.Namespace) -> None:
|
|
|
1483
2294
|
missing = [name for name, value in required if not str(value or "").strip()]
|
|
1484
2295
|
if missing:
|
|
1485
2296
|
raise APError("Missing Jenkins config: " + ", ".join(missing))
|
|
2297
|
+
_record_evidence(repo, cfg, "verify_jenkins", "pass", {"jenkinsfile": str(jenkinsfile)})
|
|
1486
2298
|
print(f"[verify-jenkins] OK: {jenkinsfile}")
|
|
1487
2299
|
|
|
1488
2300
|
|
|
@@ -1573,8 +2385,10 @@ def cmd_doctor(args: argparse.Namespace) -> None:
|
|
|
1573
2385
|
missing.extend(validation_errors)
|
|
1574
2386
|
|
|
1575
2387
|
if missing:
|
|
2388
|
+
_record_evidence(repo, cfg, "doctor", "fail", {"issues": missing})
|
|
1576
2389
|
raise APError("Doctor found blocking config issues:\n- " + "\n- ".join(missing))
|
|
1577
2390
|
|
|
2391
|
+
_record_evidence(repo, cfg, "doctor", "pass", {"mode": mode})
|
|
1578
2392
|
print("[doctor] OK")
|
|
1579
2393
|
|
|
1580
2394
|
|
|
@@ -1641,6 +2455,13 @@ def cmd_verify_jenkins_build(args: argparse.Namespace) -> None:
|
|
|
1641
2455
|
args.allow_no_deploy,
|
|
1642
2456
|
)
|
|
1643
2457
|
build_url = str(payload.get("url") or "").strip()
|
|
2458
|
+
_record_evidence(
|
|
2459
|
+
repo,
|
|
2460
|
+
cfg,
|
|
2461
|
+
"verify_jenkins_build",
|
|
2462
|
+
"pass",
|
|
2463
|
+
{"job": job_label, "build": payload.get("number"), "result": result, "url": build_url},
|
|
2464
|
+
)
|
|
1644
2465
|
print(
|
|
1645
2466
|
"[verify-jenkins-build] OK: "
|
|
1646
2467
|
f"job={job_label} "
|
|
@@ -1681,6 +2502,13 @@ def cmd_verify_jenkins_build(args: argparse.Namespace) -> None:
|
|
|
1681
2502
|
args.allow_no_deploy,
|
|
1682
2503
|
)
|
|
1683
2504
|
build_url = str(matched.get("url") or "").strip()
|
|
2505
|
+
_record_evidence(
|
|
2506
|
+
repo,
|
|
2507
|
+
cfg,
|
|
2508
|
+
"verify_jenkins_build",
|
|
2509
|
+
"pass",
|
|
2510
|
+
{"commit": git_short_sha, "job": job_label, "build": matched.get("number"), "result": result, "url": build_url},
|
|
2511
|
+
)
|
|
1684
2512
|
print(
|
|
1685
2513
|
"[verify-jenkins-build] OK: "
|
|
1686
2514
|
f"commit={git_short_sha} "
|
|
@@ -1703,6 +2531,7 @@ def cmd_verify_api_docs(args: argparse.Namespace) -> None:
|
|
|
1703
2531
|
missing = [p for p in [api_doc, change_log] if not p.exists()]
|
|
1704
2532
|
if missing:
|
|
1705
2533
|
raise APError("Missing API docs: " + ", ".join([str(p) for p in missing]))
|
|
2534
|
+
_record_evidence(repo, cfg, "verify_api_docs", "pass", {"api_doc": str(api_doc), "api_change_log": str(change_log)})
|
|
1706
2535
|
print(f"[verify-api-docs] OK: {api_doc} + {change_log}")
|
|
1707
2536
|
|
|
1708
2537
|
|
|
@@ -1752,6 +2581,13 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
|
|
|
1752
2581
|
f.write("\n")
|
|
1753
2582
|
f.write("\n".join(lines))
|
|
1754
2583
|
f.write("\n")
|
|
2584
|
+
_record_evidence(
|
|
2585
|
+
repo,
|
|
2586
|
+
cfg,
|
|
2587
|
+
"record_closure",
|
|
2588
|
+
"pass",
|
|
2589
|
+
{"task_id": task_id, "result": args.result, "commit": commit_value, "structure_check": structure_check},
|
|
2590
|
+
)
|
|
1755
2591
|
print(f"[record-closure] OK: {closure_log}")
|
|
1756
2592
|
|
|
1757
2593
|
|
|
@@ -1907,6 +2743,21 @@ def main(argv: Optional[List[str]] = None) -> int:
|
|
|
1907
2743
|
s.add_argument("--bridges", action="store_true")
|
|
1908
2744
|
s.set_defaults(func=cmd_install)
|
|
1909
2745
|
|
|
2746
|
+
s = sp.add_parser("upgrade")
|
|
2747
|
+
s.add_argument("--dry-run", action="store_true")
|
|
2748
|
+
s.add_argument("--write", action="store_true")
|
|
2749
|
+
s.add_argument("--json", action="store_true")
|
|
2750
|
+
s.set_defaults(func=cmd_upgrade)
|
|
2751
|
+
|
|
2752
|
+
s = sp.add_parser("baseline")
|
|
2753
|
+
baseline_sp = s.add_subparsers(dest="baseline_cmd", required=True)
|
|
2754
|
+
b = baseline_sp.add_parser("init")
|
|
2755
|
+
b.add_argument("--write", action="store_true")
|
|
2756
|
+
b.add_argument("--force", action="store_true")
|
|
2757
|
+
b.add_argument("--update-config", action="store_true")
|
|
2758
|
+
b.add_argument("--json", action="store_true")
|
|
2759
|
+
b.set_defaults(func=cmd_baseline_init)
|
|
2760
|
+
|
|
1910
2761
|
s = sp.add_parser("gen-summary")
|
|
1911
2762
|
s.add_argument("task_id")
|
|
1912
2763
|
s.add_argument("--title")
|
|
@@ -1926,12 +2777,23 @@ def main(argv: Optional[List[str]] = None) -> int:
|
|
|
1926
2777
|
s.add_argument("--json", action="store_true")
|
|
1927
2778
|
s.set_defaults(func=cmd_impact)
|
|
1928
2779
|
|
|
2780
|
+
s = sp.add_parser("classify")
|
|
2781
|
+
s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
|
|
2782
|
+
s.add_argument("--base")
|
|
2783
|
+
s.add_argument("--json", action="store_true")
|
|
2784
|
+
s.set_defaults(func=cmd_classify)
|
|
2785
|
+
|
|
1929
2786
|
s = sp.add_parser("structure-check")
|
|
1930
2787
|
s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
|
|
1931
2788
|
s.add_argument("--base")
|
|
1932
2789
|
s.add_argument("--json", action="store_true")
|
|
1933
2790
|
s.set_defaults(func=cmd_structure_check)
|
|
1934
2791
|
|
|
2792
|
+
s = sp.add_parser("gate-profile")
|
|
2793
|
+
s.add_argument("--limit", type=int, default=20)
|
|
2794
|
+
s.add_argument("--json", action="store_true")
|
|
2795
|
+
s.set_defaults(func=cmd_gate_profile)
|
|
2796
|
+
|
|
1935
2797
|
s = sp.add_parser("light-gate")
|
|
1936
2798
|
s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
|
|
1937
2799
|
s.add_argument("--base")
|
|
@@ -2031,7 +2893,7 @@ def main(argv: Optional[List[str]] = None) -> int:
|
|
|
2031
2893
|
args.func(args)
|
|
2032
2894
|
return 0
|
|
2033
2895
|
except APError as e:
|
|
2034
|
-
print(f"[ERROR] {e}")
|
|
2896
|
+
print(f"[ERROR] {e}", file=sys.stderr)
|
|
2035
2897
|
return 2
|
|
2036
2898
|
|
|
2037
2899
|
|