@elvis1513/auto-coding-skill 2.2.1 → 3.0.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.
@@ -6,19 +6,23 @@ from __future__ import annotations
6
6
 
7
7
  import argparse
8
8
  import base64
9
+ import contextlib
9
10
  import hashlib
10
11
  import datetime as _dt
11
12
  import fnmatch
12
13
  import json
13
14
  import os
15
+ import posixpath
14
16
  import re
17
+ import socket
15
18
  import sys
16
19
  import time
17
20
  import urllib.parse
18
21
  import urllib.error
19
22
  import urllib.request
23
+ import uuid
20
24
  from pathlib import Path
21
- from typing import Optional, List
25
+ from typing import Iterator, Optional, List
22
26
 
23
27
  try:
24
28
  import yaml # type: ignore
@@ -30,8 +34,9 @@ from scaffold_templates import scaffold_groups, templates_for
30
34
 
31
35
 
32
36
  _JENKINS_CRUMB_CACHE: dict[str, dict[str, str]] = {}
33
- _INVALID_PLACEHOLDERS = {"N/A", "TODO", "TBD", "CHANGEME", "CHANGE_ME", "FILL_ME", "FILL-ME", "PLACEHOLDER", "XXX"}
37
+ _INVALID_PLACEHOLDERS = {"N/A", "TODO", "TBD", "CHANGEME", "CHANGE_ME", "FILL_ME", "FILL-ME", "PLACEHOLDER", "XXX", "NULL", "~"}
34
38
  _GENERATED_NOISE_PATTERNS = [
39
+ ".local/auto-coding-skill/**",
35
40
  "__pycache__/**",
36
41
  "**/__pycache__/**",
37
42
  "*.pyc",
@@ -39,6 +44,52 @@ _GENERATED_NOISE_PATTERNS = [
39
44
  ".DS_Store",
40
45
  "**/.DS_Store",
41
46
  ]
47
+ _AGENT_CONTRACT_VERSION = 1
48
+ _AGENT_CONTRACT_SCHEMA = "data/contracts/orchestration-v1.schema.json"
49
+ _DEFAULT_DISPOSABLE_IGNORED_PATTERNS = [
50
+ ".local/auto-coding-skill",
51
+ ".local/auto-coding-skill/**",
52
+ "node_modules",
53
+ "node_modules/**",
54
+ "**/node_modules",
55
+ "**/node_modules/**",
56
+ "target",
57
+ "target/**",
58
+ "**/target",
59
+ "**/target/**",
60
+ "dist",
61
+ "dist/**",
62
+ "**/dist",
63
+ "**/dist/**",
64
+ "build",
65
+ "build/**",
66
+ "**/build",
67
+ "**/build/**",
68
+ "coverage",
69
+ "coverage/**",
70
+ "**/coverage",
71
+ "**/coverage/**",
72
+ ".pytest_cache",
73
+ ".pytest_cache/**",
74
+ "**/.pytest_cache",
75
+ "**/.pytest_cache/**",
76
+ ".mypy_cache",
77
+ ".mypy_cache/**",
78
+ "**/.mypy_cache",
79
+ "**/.mypy_cache/**",
80
+ ".ruff_cache",
81
+ ".ruff_cache/**",
82
+ "**/.ruff_cache",
83
+ "**/.ruff_cache/**",
84
+ "__pycache__",
85
+ "__pycache__/**",
86
+ "**/__pycache__",
87
+ "**/__pycache__/**",
88
+ "*.pyc",
89
+ "**/*.pyc",
90
+ ".DS_Store",
91
+ "**/.DS_Store",
92
+ ]
42
93
 
43
94
 
44
95
  def _skill_root() -> Path:
@@ -85,9 +136,12 @@ def _append_jsonl(path: Path, payload: dict) -> None:
85
136
 
86
137
 
87
138
  def _evidence_log_path(repo: Path, cfg: dict) -> Path:
88
- docs_cfg = cfg.get("docs") or {}
89
- rel = _text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl"
90
- return Path(repo, rel)
139
+ manifest = _active_task_manifest(repo)
140
+ if manifest:
141
+ docs_cfg = cfg.get("docs") or {}
142
+ task_dir = _text(docs_cfg.get("task_evidence_dir")) or "docs/tasks/evidence"
143
+ return Path(repo, task_dir, f"{manifest['task_id']}.jsonl")
144
+ return Path(repo, ".local/auto-coding-skill/evidence.jsonl")
91
145
 
92
146
 
93
147
  def _gate_profile_path(repo: Path, cfg: dict) -> Path:
@@ -507,21 +561,9 @@ def _inferred_gate_commands(repo: Path) -> dict[str, str]:
507
561
  scripts = payload.get("scripts") or {}
508
562
  if not isinstance(scripts, dict):
509
563
  return {}
510
- inferred: dict[str, str] = {}
511
- if isinstance(scripts.get("test"), str) and scripts["test"].strip():
512
- inferred["gate_changed"] = (
513
- "npm run test:changed"
514
- if isinstance(scripts.get("test:changed"), str) and scripts["test:changed"].strip()
515
- else "npm test"
516
- )
517
- inferred["gate_standard"] = (
518
- "npm run test:standard"
519
- if isinstance(scripts.get("test:standard"), str) and scripts["test:standard"].strip()
520
- else "npm test"
521
- )
522
- if isinstance(scripts.get("test:full"), str) and scripts["test:full"].strip():
523
- inferred["gate_full"] = "npm run test:full"
524
- return inferred
564
+ if isinstance(scripts.get("test:changed"), str) and scripts["test:changed"].strip():
565
+ return {"gate_changed": "npm run test:changed"}
566
+ return {}
525
567
 
526
568
 
527
569
  def _initialize_engineering_defaults(path: Path, repo: Path) -> None:
@@ -534,15 +576,67 @@ def _initialize_engineering_defaults(path: Path, repo: Path) -> None:
534
576
  1,
535
577
  )
536
578
  for key, gate_command in _inferred_gate_commands(repo).items():
537
- updated = updated.replace(
538
- f' {key}: ""',
539
- f" {key}: {json.dumps(gate_command)}",
540
- 1,
541
- )
579
+ for default_value in ['""', '"git diff --check"']:
580
+ candidate = updated.replace(
581
+ f" {key}: {default_value}",
582
+ f" {key}: {json.dumps(gate_command)}",
583
+ 1,
584
+ )
585
+ if candidate != updated:
586
+ updated = candidate
587
+ break
542
588
  if updated != text:
543
589
  path.write_text(updated, encoding="utf-8")
544
590
 
545
591
 
592
+ def _migrate_fast_development_defaults(cfg: dict, repo: Path) -> list[str]:
593
+ changed: list[str] = []
594
+
595
+ workflow_cfg = cfg.setdefault("workflow", {})
596
+ if not isinstance(workflow_cfg, dict):
597
+ workflow_cfg = {}
598
+ cfg["workflow"] = workflow_cfg
599
+ if _text(workflow_cfg.get("mode")).lower() != "dev":
600
+ workflow_cfg["mode"] = "dev"
601
+ changed.append("workflow.mode")
602
+ if _text(workflow_cfg.get("completion")).lower() != "push":
603
+ workflow_cfg["completion"] = "push"
604
+ changed.append("workflow.completion")
605
+
606
+ commands_cfg = cfg.setdefault("commands", {})
607
+ if not isinstance(commands_cfg, dict):
608
+ commands_cfg = {}
609
+ cfg["commands"] = commands_cfg
610
+ current_changed_gate = _text(commands_cfg.get("gate_changed"))
611
+ if current_changed_gate in {"", "npm test"}:
612
+ replacement = _inferred_gate_commands(repo).get("gate_changed", "git diff --check")
613
+ if current_changed_gate != replacement:
614
+ commands_cfg["gate_changed"] = replacement
615
+ changed.append("commands.gate_changed")
616
+
617
+ gate_cfg = cfg.setdefault("gate", {})
618
+ if not isinstance(gate_cfg, dict):
619
+ gate_cfg = {}
620
+ cfg["gate"] = gate_cfg
621
+ for key in ["default_scope", "fallback_scope", "no_change_scope"]:
622
+ if _text(gate_cfg.get(key)).lower() != "changed":
623
+ gate_cfg[key] = "changed"
624
+ changed.append(f"gate.{key}")
625
+ if _bool_config(gate_cfg.get("full_on_unknown"), False):
626
+ gate_cfg["full_on_unknown"] = False
627
+ changed.append("gate.full_on_unknown")
628
+
629
+ concurrency_cfg = cfg.setdefault("concurrency", {})
630
+ if not isinstance(concurrency_cfg, dict):
631
+ concurrency_cfg = {}
632
+ cfg["concurrency"] = concurrency_cfg
633
+ if _text(concurrency_cfg.get("isolation")).lower() != "worktree":
634
+ concurrency_cfg["isolation"] = "worktree"
635
+ changed.append("concurrency.isolation")
636
+
637
+ return changed
638
+
639
+
546
640
  def cmd_scaffold(args: argparse.Namespace) -> None:
547
641
  repo = Path(args.repo).resolve()
548
642
  group = str(args.group or "").strip().lower()
@@ -615,8 +709,8 @@ def cmd_install(args: argparse.Namespace) -> None:
615
709
  layout = "full" if args.full else "minimal"
616
710
  print(f"[install] OK: {layout} scaffold installed into {repo}")
617
711
  print(
618
- "[install] Next: edit docs/ENGINEERING.md frontmatter, set verification.*_required, "
619
- "fill only enabled environment fields, and commit that file into Git."
712
+ "[install] Next: fill every access.* URL, username, and password in "
713
+ "docs/ENGINEERING.md, run doctor, and commit that file into Git."
620
714
  )
621
715
 
622
716
 
@@ -709,8 +803,14 @@ def cmd_upgrade(args: argparse.Namespace) -> None:
709
803
  template_cfg, _ = _read_frontmatter_markdown(template_engineering)
710
804
  merged_cfg = json.loads(json.dumps(current_cfg))
711
805
  added_keys = _deep_merge_missing(merged_cfg, template_cfg)
712
- if added_keys:
713
- add_action("config", engineering, "merge", ", ".join(added_keys))
806
+ migrated_keys = _migrate_fast_development_defaults(merged_cfg, repo)
807
+ if added_keys or migrated_keys:
808
+ detail_parts = []
809
+ if added_keys:
810
+ detail_parts.append("add " + ", ".join(added_keys))
811
+ if migrated_keys:
812
+ detail_parts.append("migrate " + ", ".join(migrated_keys))
813
+ add_action("config", engineering, "merge", "; ".join(detail_parts))
714
814
  if write:
715
815
  _write_frontmatter_markdown(engineering, merged_cfg, body)
716
816
  else:
@@ -796,9 +896,9 @@ def cmd_gen_summary(args: argparse.Namespace) -> None:
796
896
 
797
897
  ---
798
898
 
799
- ## 1. 目标与验收结论
899
+ ## 1. 目标与开发闭环
800
900
  - 目标:TODO
801
- - 验收结论:PASS / FAIL — TODO
901
+ - 开发结论:DEV-CLOSED / BLOCKED — TODO
802
902
 
803
903
  ## 2. 变更概览
804
904
  ### Git change snapshot
@@ -815,14 +915,13 @@ def cmd_gen_summary(args: argparse.Namespace) -> None:
815
915
  - 变更记录位置:`{api_change_log}`
816
916
 
817
917
  ## 4. 质量证据
818
- - 本地轻量校验:light_gate or quick_test/test/build / api docs / CI/Jenkins / diff-check — TODO
819
- - 结构检查:structure-check — TODO
918
+ - 本地快速门禁:changed gate + diff-check — TODO
919
+ - 项目 Git hooks:commit/push 时执行
820
920
  - 结构化证据:docs/tasks/evidence.jsonl — TODO
821
921
  - 门禁画像:.local/auto-coding-skill/gate-profile.jsonl — TODO
822
- - CI/Jenkins Build:TODO
823
- - 目标环境验证:TODO
824
- - 闭环记录:TODO
825
- - 回归矩阵(如有):`{regression_matrix}`
922
+ - 目标分支推送:TODO
923
+ - Jenkins / 构建 / 部署 / 实际验收:项目负责人推送后处理
924
+ - 可选诊断(如明确要求):`{regression_matrix}`
826
925
 
827
926
  ## 5. 风险与回滚
828
927
  - 风险:TODO
@@ -1011,8 +1110,8 @@ def _workflow_mode(cfg: dict, args: Optional[argparse.Namespace] = None) -> str:
1011
1110
  explicit = str(getattr(args, "mode", "") or "").strip().lower() if args else ""
1012
1111
  configured = str(((cfg.get("workflow") or {}).get("mode")) or "dev").strip().lower()
1013
1112
  mode = explicit or configured or "dev"
1014
- if mode not in {"dev", "verify"}:
1015
- raise APError("workflow.mode must be 'dev' or 'verify'.")
1113
+ if mode != "dev":
1114
+ raise APError("workflow.mode must be 'dev'; external verification is owner-managed after push.")
1016
1115
  return mode
1017
1116
 
1018
1117
 
@@ -1020,6 +1119,650 @@ def _text(value: object) -> str:
1020
1119
  return str(value or "").strip()
1021
1120
 
1022
1121
 
1122
+ _TASK_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
1123
+
1124
+
1125
+ def _concurrency_cfg(cfg: dict) -> dict:
1126
+ value = cfg.get("concurrency") or {}
1127
+ return value if isinstance(value, dict) else {}
1128
+
1129
+
1130
+ def _task_isolation(cfg: dict) -> str:
1131
+ isolation = _text(_concurrency_cfg(cfg).get("isolation")).lower() or "worktree"
1132
+ if isolation != "worktree":
1133
+ raise APError(
1134
+ "concurrency.isolation must be worktree; shared-checkout/legacy writes are no longer supported"
1135
+ )
1136
+ return isolation
1137
+
1138
+
1139
+ def _cleanup_policy(cfg: dict) -> dict:
1140
+ concurrency_cfg = _concurrency_cfg(cfg)
1141
+ return {
1142
+ "cleanup_merged": _bool_config(concurrency_cfg.get("cleanup_merged"), True),
1143
+ "delete_remote_branch": _bool_config(concurrency_cfg.get("delete_remote_branch"), True),
1144
+ "disposable_ignored": [
1145
+ _text(item)
1146
+ for item in _as_list(concurrency_cfg.get("disposable_ignored"))
1147
+ if _text(item)
1148
+ ],
1149
+ }
1150
+
1151
+
1152
+ def _manifest_cleanup_policy(manifest: dict, fallback_cfg: dict) -> dict:
1153
+ value = manifest.get("cleanup_policy")
1154
+ return value if isinstance(value, dict) else _cleanup_policy(fallback_cfg)
1155
+
1156
+
1157
+ def _validate_task_id(task_id: str) -> str:
1158
+ value = _text(task_id)
1159
+ if not _TASK_ID_RE.fullmatch(value) or value.endswith(".lock") or ".." in value:
1160
+ raise APError(
1161
+ "Task ID must be 1-64 characters using letters, digits, '.', '_' or '-', "
1162
+ "must start with a letter/digit, and cannot contain '..' or end with '.lock'."
1163
+ )
1164
+ return value
1165
+
1166
+
1167
+ def _resolve_git_dir(repo: Path, option: str) -> Path:
1168
+ result = run(["git", "rev-parse", option], cwd=repo, check=False)
1169
+ if result.returncode != 0 or not result.stdout.strip():
1170
+ raise APError(f"Cannot resolve {option} for Git repository: {repo}")
1171
+ value = Path(result.stdout.strip())
1172
+ if not value.is_absolute():
1173
+ value = repo / value
1174
+ return value.resolve()
1175
+
1176
+
1177
+ def _git_common_dir(repo: Path) -> Path:
1178
+ return _resolve_git_dir(repo, "--git-common-dir")
1179
+
1180
+
1181
+ def _git_dir(repo: Path) -> Path:
1182
+ return _resolve_git_dir(repo, "--git-dir")
1183
+
1184
+
1185
+ def _task_state_root(repo: Path) -> Path:
1186
+ return _git_common_dir(repo) / "auto-coding-skill"
1187
+
1188
+
1189
+ def _task_registry_path(repo: Path, task_id: str) -> Path:
1190
+ return _task_state_root(repo) / "tasks" / f"{_validate_task_id(task_id)}.json"
1191
+
1192
+
1193
+ def _worktree_manifest_path(repo: Path) -> Path:
1194
+ return _git_dir(repo) / "auto-coding-skill-task.json"
1195
+
1196
+
1197
+ def _read_json_object(path: Path) -> Optional[dict]:
1198
+ try:
1199
+ payload = json.loads(path.read_text(encoding="utf-8"))
1200
+ except (FileNotFoundError, json.JSONDecodeError, OSError):
1201
+ return None
1202
+ return payload if isinstance(payload, dict) else None
1203
+
1204
+
1205
+ def _write_json_object(path: Path, payload: dict) -> None:
1206
+ path.parent.mkdir(parents=True, exist_ok=True)
1207
+ temporary = path.with_name(f".{path.name}.{os.getpid()}.{uuid.uuid4().hex}.tmp")
1208
+ temporary.write_text(
1209
+ json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
1210
+ encoding="utf-8",
1211
+ )
1212
+ os.replace(temporary, path)
1213
+
1214
+
1215
+ def _active_task_manifest(repo: Path) -> Optional[dict]:
1216
+ try:
1217
+ return _read_json_object(_worktree_manifest_path(repo))
1218
+ except APError:
1219
+ return None
1220
+
1221
+
1222
+ def _validate_task_manifest(repo: Path, manifest: dict, expected_task_id: str = "") -> dict:
1223
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1224
+ if expected_task_id and task_id != _validate_task_id(expected_task_id):
1225
+ raise APError(
1226
+ f"Task registry identity mismatch: expected={expected_task_id}, manifest={task_id}"
1227
+ )
1228
+ try:
1229
+ schema = int(manifest.get("schema") or 1)
1230
+ except (TypeError, ValueError) as exc:
1231
+ raise APError(f"Task {task_id} has an invalid manifest schema.") from exc
1232
+ if schema < 2:
1233
+ raise APError(
1234
+ f"Task {task_id} is a legacy schema-1 task. This runtime cannot safely infer or claim "
1235
+ "owned_paths for an in-flight task. Finish or clean it with the previously installed "
1236
+ "runtime before running autocoding sync, or restore the 3.0.0 runtime."
1237
+ )
1238
+ task_uuid = _text(manifest.get("task_uuid"))
1239
+ if not re.fullmatch(r"[0-9a-f]{32}", task_uuid):
1240
+ raise APError(f"Invalid task manifest UUID for {task_id}; refusing Git operations.")
1241
+ task_branch = _text(manifest.get("task_branch"))
1242
+ if not task_branch.endswith(f"/{task_id}") or run(
1243
+ ["git", "check-ref-format", "--branch", task_branch],
1244
+ cwd=repo,
1245
+ check=False,
1246
+ ).returncode != 0:
1247
+ raise APError(f"Invalid task branch in manifest for {task_id}: {task_branch!r}")
1248
+ target_branch = _text(manifest.get("target_branch"))
1249
+ if run(
1250
+ ["git", "check-ref-format", "--branch", target_branch],
1251
+ cwd=repo,
1252
+ check=False,
1253
+ ).returncode != 0:
1254
+ raise APError(f"Invalid target branch in manifest for {task_id}: {target_branch!r}")
1255
+ remote = _text(manifest.get("remote"))
1256
+ if not remote or remote.startswith("-") or any(char in remote for char in "\0\r\n"):
1257
+ raise APError(f"Invalid remote in manifest for {task_id}: {remote!r}")
1258
+ base_sha = _text(manifest.get("base_sha"))
1259
+ if not re.fullmatch(r"(?:[0-9a-f]{40}|[0-9a-f]{64})", base_sha):
1260
+ raise APError(f"Invalid base commit in manifest for {task_id}: {base_sha!r}")
1261
+ worktree_raw = _text(manifest.get("worktree_path"))
1262
+ control_raw = _text(manifest.get("control_worktree_path"))
1263
+ worktree = Path(worktree_raw)
1264
+ control = Path(control_raw)
1265
+ if not worktree_raw or not control_raw or not worktree.is_absolute() or not control.is_absolute():
1266
+ raise APError(f"Task manifest paths must be absolute for {task_id}.")
1267
+ worktree = worktree.resolve()
1268
+ control = control.resolve()
1269
+ if worktree == control or control in worktree.parents or worktree in control.parents:
1270
+ raise APError(
1271
+ f"Task worktree must be outside its control checkout for {task_id}: {worktree}"
1272
+ )
1273
+ owned_paths = manifest.get("owned_paths")
1274
+ if not isinstance(owned_paths, list) or not owned_paths or not all(
1275
+ isinstance(item, str) and item for item in owned_paths
1276
+ ):
1277
+ raise APError(f"Task {task_id} has no valid owned_paths; refusing lifecycle operations.")
1278
+ depends_on = manifest.get("depends_on")
1279
+ prerequisite_shas = manifest.get("prerequisite_shas")
1280
+ if not isinstance(depends_on, list) or not isinstance(prerequisite_shas, dict):
1281
+ raise APError(f"Task {task_id} has an invalid dependency contract.")
1282
+ if set(depends_on) != set(prerequisite_shas):
1283
+ raise APError(f"Task {task_id} dependency IDs and prerequisite SHAs do not match.")
1284
+ for dependency, sha in prerequisite_shas.items():
1285
+ _validate_task_id(str(dependency))
1286
+ if not re.fullmatch(r"(?:[0-9a-f]{40}|[0-9a-f]{64})", _text(sha)):
1287
+ raise APError(f"Task {task_id} has an invalid prerequisite SHA for {dependency}.")
1288
+ lease = manifest.get("writer_lease")
1289
+ if not isinstance(lease, dict) or not _text(lease.get("holder")):
1290
+ raise APError(f"Task {task_id} has an invalid writer lease.")
1291
+ if int(lease.get("generation") or 0) < 1 or _text(lease.get("state")) != "active":
1292
+ raise APError(f"Task {task_id} writer lease is not active.")
1293
+ review = manifest.get("review")
1294
+ if not isinstance(review, dict) or _text(review.get("verdict")) not in {
1295
+ "pending",
1296
+ "approved",
1297
+ "changes-requested",
1298
+ }:
1299
+ raise APError(f"Task {task_id} has an invalid review contract.")
1300
+ return manifest
1301
+
1302
+
1303
+ def _load_task_manifest(repo: Path, task_id: str) -> dict:
1304
+ path = _task_registry_path(repo, task_id)
1305
+ manifest = _read_json_object(path)
1306
+ if not manifest:
1307
+ raise APError(
1308
+ f"Task is not registered: {task_id}. Run `ap.py task-start {task_id}` first."
1309
+ )
1310
+ return _validate_task_manifest(repo, manifest, task_id)
1311
+
1312
+
1313
+ def _save_task_manifest(repo: Path, manifest: dict) -> None:
1314
+ _validate_task_manifest(repo, manifest)
1315
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1316
+ manifest["updated_at"] = _now_iso()
1317
+ _write_json_object(_task_registry_path(repo, task_id), manifest)
1318
+ worktree_value = _text(manifest.get("worktree_path"))
1319
+ if worktree_value:
1320
+ worktree = Path(worktree_value)
1321
+ if worktree.exists():
1322
+ try:
1323
+ _write_json_object(_worktree_manifest_path(worktree), manifest)
1324
+ except APError:
1325
+ pass
1326
+
1327
+
1328
+ def _delete_task_manifest(repo: Path, manifest: dict) -> None:
1329
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1330
+ _task_registry_path(repo, task_id).unlink(missing_ok=True)
1331
+
1332
+
1333
+ def _current_branch(repo: Path) -> str:
1334
+ result = run(["git", "branch", "--show-current"], cwd=repo, check=False)
1335
+ return result.stdout.strip() if result.returncode == 0 else ""
1336
+
1337
+
1338
+ def _require_control_checkout(repo: Path, manifest: Optional[dict] = None) -> None:
1339
+ if _active_task_manifest(repo):
1340
+ raise APError("Run this command from the primary/control checkout, not from a task worktree.")
1341
+ if _git_dir(repo) != _git_common_dir(repo):
1342
+ raise APError("Run this command from the primary/control checkout, not from a linked worktree.")
1343
+ if manifest:
1344
+ expected = _text(manifest.get("control_worktree_path"))
1345
+ if expected and repo.resolve() != Path(expected).resolve():
1346
+ raise APError(f"Task control checkout mismatch: current={repo.resolve()}, expected={expected}")
1347
+
1348
+
1349
+ def _resolve_commit(repo: Path, ref: str) -> str:
1350
+ result = run(["git", "rev-parse", "--verify", f"{ref}^{{commit}}"], cwd=repo, check=False)
1351
+ if result.returncode != 0 or not result.stdout.strip():
1352
+ raise APError(f"Cannot resolve Git commit: {ref}")
1353
+ return result.stdout.strip()
1354
+
1355
+
1356
+ def _git_z_paths(repo: Path, command: list[str]) -> list[str]:
1357
+ result = run(command, cwd=repo, check=False)
1358
+ if result.returncode != 0:
1359
+ return []
1360
+ return [item for item in result.stdout.split("\0") if item]
1361
+
1362
+
1363
+ def _checked_git_z_paths(repo: Path, command: list[str], context: str) -> list[str]:
1364
+ result = run(command, cwd=repo, check=False)
1365
+ if result.returncode != 0:
1366
+ raise APError(
1367
+ f"Cannot inspect {context}: {result.stderr.strip() or result.stdout.strip()}"
1368
+ )
1369
+ return [item for item in result.stdout.split("\0") if item]
1370
+
1371
+
1372
+ def _working_tree_paths(repo: Path) -> list[str]:
1373
+ paths: set[str] = set()
1374
+ paths.update(
1375
+ _git_z_paths(
1376
+ repo,
1377
+ ["git", "diff", "--name-only", "-z", "--diff-filter=ACDMRTUXB"],
1378
+ )
1379
+ )
1380
+ paths.update(
1381
+ _git_z_paths(
1382
+ repo,
1383
+ ["git", "diff", "--cached", "--name-only", "-z", "--diff-filter=ACDMRTUXB"],
1384
+ )
1385
+ )
1386
+ paths.update(
1387
+ _git_z_paths(repo, ["git", "ls-files", "--others", "--exclude-standard", "-z"])
1388
+ )
1389
+ return sorted(path.replace("\\", "/") for path in paths if path)
1390
+
1391
+
1392
+ def _task_runtime_paths(repo: Path, cfg: dict, manifest: Optional[dict]) -> set[str]:
1393
+ docs_cfg = cfg.get("docs") or {}
1394
+ gate_cfg = _gate_cfg(cfg)
1395
+ paths = {
1396
+ _text(gate_cfg.get("profile_log")) or ".local/auto-coding-skill/gate-profile.jsonl",
1397
+ }
1398
+ if manifest:
1399
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1400
+ evidence_dir = _text(docs_cfg.get("task_evidence_dir")) or "docs/tasks/evidence"
1401
+ paths.add(f"{evidence_dir.rstrip('/')}/{task_id}.jsonl")
1402
+ else:
1403
+ paths.add(_text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl")
1404
+ normalized: set[str] = set()
1405
+ for path in paths:
1406
+ value = path.replace("\\", "/")
1407
+ if value.startswith("./"):
1408
+ value = value[2:]
1409
+ if value:
1410
+ normalized.add(value)
1411
+ return normalized
1412
+
1413
+
1414
+ def _task_content_fingerprint(repo: Path, cfg: dict, manifest: Optional[dict]) -> str:
1415
+ runtime_paths = _task_runtime_paths(repo, cfg, manifest)
1416
+ relevant = [
1417
+ path
1418
+ for path in _working_tree_paths(repo)
1419
+ if path not in runtime_paths and not _is_generated_noise_path(path)
1420
+ ]
1421
+ digest = hashlib.sha256()
1422
+ digest.update(f"branch:{_current_branch(repo)}\n".encode("utf-8"))
1423
+ digest.update(f"head:{_resolve_commit(repo, 'HEAD')}\n".encode("utf-8"))
1424
+ for rel in relevant:
1425
+ digest.update(rel.encode("utf-8", errors="surrogateescape"))
1426
+ path = repo / rel
1427
+ try:
1428
+ digest.update(f"mode:{path.lstat().st_mode:o}".encode("ascii"))
1429
+ except OSError:
1430
+ digest.update(b"mode:<missing>")
1431
+ if path.is_symlink():
1432
+ try:
1433
+ digest.update(os.readlink(path).encode("utf-8", errors="surrogateescape"))
1434
+ except OSError:
1435
+ digest.update(b"<unreadable-symlink>")
1436
+ elif path.is_file():
1437
+ try:
1438
+ digest.update(path.read_bytes())
1439
+ except OSError:
1440
+ digest.update(b"<unreadable>")
1441
+ else:
1442
+ digest.update(b"<missing>")
1443
+ for start in range(0, len(relevant), 100):
1444
+ batch = relevant[start : start + 100]
1445
+ if not batch:
1446
+ continue
1447
+ cached = run(
1448
+ ["git", "diff", "--cached", "--binary", "--no-ext-diff", "--", *batch],
1449
+ cwd=repo,
1450
+ check=False,
1451
+ )
1452
+ digest.update(cached.stdout.encode("utf-8", errors="surrogateescape"))
1453
+ unstaged = run(
1454
+ ["git", "diff", "--binary", "--no-ext-diff", "--", *batch],
1455
+ cwd=repo,
1456
+ check=False,
1457
+ )
1458
+ digest.update(unstaged.stdout.encode("utf-8", errors="surrogateescape"))
1459
+ index_state = run(
1460
+ ["git", "ls-files", "--stage", "-z", "--", *batch],
1461
+ cwd=repo,
1462
+ check=False,
1463
+ )
1464
+ digest.update(index_state.stdout.encode("utf-8", errors="surrogateescape"))
1465
+ return digest.hexdigest()
1466
+
1467
+
1468
+ def _stage_exact_paths(repo: Path, paths: list[str]) -> list[str]:
1469
+ expected = sorted(set(path for path in paths if path))
1470
+ for start in range(0, len(expected), 100):
1471
+ run(["git", "add", "-A", "--", *expected[start : start + 100]], cwd=repo)
1472
+ staged = sorted(
1473
+ set(
1474
+ _checked_git_z_paths(
1475
+ repo,
1476
+ [
1477
+ "git",
1478
+ "diff",
1479
+ "--cached",
1480
+ "--name-only",
1481
+ "-z",
1482
+ "--diff-filter=ACDMRTUXB",
1483
+ ],
1484
+ "staged task paths",
1485
+ )
1486
+ )
1487
+ )
1488
+ unexpected = sorted(set(staged) - set(expected))
1489
+ if unexpected:
1490
+ raise APError(
1491
+ "Refusing to commit paths outside the current task:\n- " + "\n- ".join(unexpected)
1492
+ )
1493
+ return staged
1494
+
1495
+
1496
+ def _task_commit_paths(repo: Path) -> list[str]:
1497
+ return [path for path in _working_tree_paths(repo) if not _is_generated_noise_path(path)]
1498
+
1499
+
1500
+ def _normalize_owned_path(value: object) -> str:
1501
+ raw = _text(value).replace("\\", "/")
1502
+ while raw.startswith("./"):
1503
+ raw = raw[2:]
1504
+ normalized = posixpath.normpath(raw)
1505
+ if (
1506
+ not raw
1507
+ or raw.startswith("/")
1508
+ or normalized == ".."
1509
+ or normalized.startswith("../")
1510
+ or normalized == ".git"
1511
+ or normalized.startswith(".git/")
1512
+ ):
1513
+ raise APError(f"Owned paths must be safe repository-relative paths: {value!r}")
1514
+ return normalized
1515
+
1516
+
1517
+ def _path_is_owned(path: str, owned_paths: list[str]) -> bool:
1518
+ normalized = _normalize_owned_path(path)
1519
+ return any(
1520
+ owner == "." or normalized == owner or normalized.startswith(owner.rstrip("/") + "/")
1521
+ for owner in owned_paths
1522
+ )
1523
+
1524
+
1525
+ def _task_managed_paths(cfg: dict, manifest: dict) -> list[str]:
1526
+ docs_cfg = cfg.get("docs") or {}
1527
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1528
+ return sorted(
1529
+ {
1530
+ f"{(_text(docs_cfg.get('active_task_dir')) or 'docs/tasks/active').rstrip('/')}/{task_id}.md",
1531
+ f"{(_text(docs_cfg.get('task_closure_dir')) or 'docs/tasks/closures').rstrip('/')}/{task_id}.md",
1532
+ f"{(_text(docs_cfg.get('task_evidence_dir')) or 'docs/tasks/evidence').rstrip('/')}/{task_id}.jsonl",
1533
+ }
1534
+ )
1535
+
1536
+
1537
+ def _task_changed_paths_from_base(repo: Path, base_sha: str) -> list[str]:
1538
+ paths = set(
1539
+ _checked_git_z_paths(
1540
+ repo,
1541
+ ["git", "diff", "--name-only", "-z", "--diff-filter=ACDMRTUXB", base_sha, "--"],
1542
+ "task diff paths",
1543
+ )
1544
+ )
1545
+ paths.update(
1546
+ _checked_git_z_paths(
1547
+ repo,
1548
+ ["git", "ls-files", "--others", "--exclude-standard", "-z"],
1549
+ "task untracked paths",
1550
+ )
1551
+ )
1552
+ return sorted(path.replace("\\", "/") for path in paths if not _is_generated_noise_path(path))
1553
+
1554
+
1555
+ def _task_unowned_paths(repo: Path, cfg: dict, manifest: dict) -> list[str]:
1556
+ owned = [_normalize_owned_path(item) for item in manifest.get("owned_paths") or []]
1557
+ managed = set(_task_managed_paths(cfg, manifest))
1558
+ return [
1559
+ path
1560
+ for path in _task_changed_paths_from_base(repo, _text(manifest.get("base_sha")))
1561
+ if path not in managed and not _path_is_owned(path, owned)
1562
+ ]
1563
+
1564
+
1565
+ def _task_review_fingerprint(repo: Path, manifest: dict, cfg: Optional[dict] = None) -> str:
1566
+ base_sha = _text(manifest.get("base_sha"))
1567
+ owned = [_normalize_owned_path(item) for item in manifest.get("owned_paths") or []]
1568
+ managed = set(_task_managed_paths(cfg or _load_cfg(repo), manifest))
1569
+ paths = [
1570
+ path
1571
+ for path in _task_changed_paths_from_base(repo, base_sha)
1572
+ if path not in managed and _path_is_owned(path, owned)
1573
+ ]
1574
+ digest = hashlib.sha256()
1575
+ digest.update(f"contract:{_AGENT_CONTRACT_VERSION}\nbase:{base_sha}\n".encode("utf-8"))
1576
+ for rel in paths:
1577
+ digest.update(f"path:{rel}\0".encode("utf-8", errors="surrogateescape"))
1578
+ path = repo / rel
1579
+ try:
1580
+ mode = path.lstat().st_mode
1581
+ except OSError:
1582
+ digest.update(b"missing\0")
1583
+ continue
1584
+ digest.update(f"mode:{mode:o}\0".encode("ascii"))
1585
+ if path.is_symlink():
1586
+ digest.update(os.readlink(path).encode("utf-8", errors="surrogateescape"))
1587
+ elif path.is_file():
1588
+ digest.update(path.read_bytes())
1589
+ else:
1590
+ digest.update(b"non-file")
1591
+ digest.update(b"\0")
1592
+ return digest.hexdigest()
1593
+
1594
+
1595
+ def _invalidate_task_review(manifest: dict, reason: str) -> None:
1596
+ manifest["review"] = {
1597
+ "verdict": "pending",
1598
+ "diff_base": _text(manifest.get("base_sha")),
1599
+ "diff_head": "",
1600
+ "diff_fingerprint": "",
1601
+ "reviewer": "",
1602
+ "reviewed_at": "",
1603
+ "reason": reason,
1604
+ }
1605
+
1606
+
1607
+ def _actor_id(args: argparse.Namespace, field: str = "writer") -> str:
1608
+ explicit = _text(getattr(args, field, ""))
1609
+ runtime_actor = _text(os.environ.get("CODEX_THREAD_ID"))
1610
+ if explicit and runtime_actor and explicit != runtime_actor:
1611
+ raise APError(
1612
+ f"Explicit --{field.replace('_', '-')} does not match CODEX_THREAD_ID; "
1613
+ "actor identity cannot be overridden."
1614
+ )
1615
+ actor = runtime_actor or explicit
1616
+ if not actor:
1617
+ raise APError(f"A stable --{field.replace('_', '-')} or CODEX_THREAD_ID is required.")
1618
+ if any(char in actor for char in "\0\r\n"):
1619
+ raise APError("Actor identity contains invalid characters.")
1620
+ return actor
1621
+
1622
+
1623
+ def _require_current_writer(manifest: dict, args: argparse.Namespace) -> str:
1624
+ actor = _actor_id(args, "writer")
1625
+ lease = manifest.get("writer_lease") or {}
1626
+ if _text(lease.get("state")) != "active" or actor != _text(lease.get("holder")):
1627
+ raise APError(
1628
+ f"Writer lease mismatch: current={actor}, holder={_text(lease.get('holder')) or '(missing)'}."
1629
+ )
1630
+ return actor
1631
+
1632
+
1633
+ def _require_dependencies(repo: Path, manifest: dict, ancestor_ref: str) -> None:
1634
+ for dependency in manifest.get("depends_on") or []:
1635
+ sha = _text((manifest.get("prerequisite_shas") or {}).get(dependency))
1636
+ if run(
1637
+ ["git", "merge-base", "--is-ancestor", sha, ancestor_ref],
1638
+ cwd=repo,
1639
+ check=False,
1640
+ ).returncode != 0:
1641
+ raise APError(
1642
+ f"Prerequisite {dependency} at {sha} is not integrated into {ancestor_ref}."
1643
+ )
1644
+
1645
+
1646
+ def _require_approved_review(repo: Path, cfg: dict, manifest: dict) -> str:
1647
+ unowned = _task_unowned_paths(repo, cfg, manifest)
1648
+ if unowned:
1649
+ raise APError("Changes outside task owned_paths:\n- " + "\n- ".join(unowned))
1650
+ fingerprint = _task_review_fingerprint(repo, manifest)
1651
+ review = manifest.get("review") or {}
1652
+ if _text(review.get("verdict")) != "approved":
1653
+ raise APError("Task review must be approved before commit-push or integration.")
1654
+ if _text(review.get("diff_fingerprint")) != fingerprint:
1655
+ raise APError("Approved review fingerprint is stale for the current owned diff.")
1656
+ if _text(review.get("diff_base")) != _text(manifest.get("base_sha")):
1657
+ raise APError("Approved review base is stale for the current task base.")
1658
+ return fingerprint
1659
+
1660
+
1661
+ def _unstaged_task_paths(repo: Path) -> list[str]:
1662
+ paths = set(
1663
+ _checked_git_z_paths(
1664
+ repo,
1665
+ ["git", "diff", "--name-only", "-z", "--diff-filter=ACDMRTUXB"],
1666
+ "unstaged task paths",
1667
+ )
1668
+ )
1669
+ paths.update(
1670
+ _checked_git_z_paths(
1671
+ repo,
1672
+ ["git", "ls-files", "--others", "--exclude-standard", "-z"],
1673
+ "untracked task paths",
1674
+ )
1675
+ )
1676
+ return sorted(path for path in paths if not _is_generated_noise_path(path))
1677
+
1678
+
1679
+ def _require_task_context(repo: Path, cfg: dict, task_id: str) -> dict:
1680
+ _task_isolation(cfg)
1681
+ manifest = _active_task_manifest(repo)
1682
+ if not manifest:
1683
+ if _git_dir(repo) != _git_common_dir(repo):
1684
+ raise APError(
1685
+ "This linked worktree has no registered task manifest. "
1686
+ "return to the primary checkout and create or recover the task with task-start."
1687
+ )
1688
+ raise APError(
1689
+ "This project requires an isolated task worktree. "
1690
+ f"Run `python3 docs/tools/autopipeline/ap.py task-start {task_id}` from the main checkout, "
1691
+ "then continue in the returned worktree."
1692
+ )
1693
+ expected_task = _validate_task_id(task_id)
1694
+ actual_task = _text(manifest.get("task_id"))
1695
+ if actual_task != expected_task:
1696
+ raise APError(
1697
+ f"Task/worktree mismatch: command={expected_task}, worktree={actual_task or '(missing)'}."
1698
+ )
1699
+ registered = _read_json_object(_task_registry_path(repo, expected_task))
1700
+ if not registered:
1701
+ raise APError(f"Task registry entry is missing for {expected_task}; refusing to recreate it implicitly.")
1702
+ for field in ("task_id", "task_uuid", "task_branch", "worktree_path", "base_sha"):
1703
+ if _text(registered.get(field)) != _text(manifest.get(field)):
1704
+ raise APError(f"Task manifest/registry mismatch for {expected_task}: {field}")
1705
+ manifest = registered
1706
+ expected_path = Path(_text(manifest.get("worktree_path"))).resolve()
1707
+ if repo.resolve() != expected_path:
1708
+ raise APError(f"Task manifest belongs to {expected_path}, not {repo.resolve()}.")
1709
+ branch = _current_branch(repo)
1710
+ expected_branch = _text(manifest.get("task_branch"))
1711
+ if not branch or branch != expected_branch:
1712
+ raise APError(
1713
+ f"Task branch mismatch: current={branch or '(detached)'}, expected={expected_branch}."
1714
+ )
1715
+ if _text(manifest.get("state")) in {"integrated", "finished"}:
1716
+ raise APError(f"Task {expected_task} is already {_text(manifest.get('state'))}.")
1717
+ return manifest
1718
+
1719
+
1720
+ @contextlib.contextmanager
1721
+ def _repo_lock(repo: Path, name: str, timeout_s: float = 30.0) -> Iterator[None]:
1722
+ safe_name = re.sub(r"[^A-Za-z0-9._-]+", "-", name).strip("-") or "repository"
1723
+ lock_path = _task_state_root(repo) / "locks" / f"{safe_name}.lock"
1724
+ lock_path.parent.mkdir(parents=True, exist_ok=True)
1725
+ handle = lock_path.open("a+", encoding="utf-8")
1726
+ try:
1727
+ try:
1728
+ import fcntl # type: ignore
1729
+ except ImportError as exc: # pragma: no cover - current runtime is POSIX
1730
+ raise APError("Task locking requires a POSIX fcntl runtime.") from exc
1731
+ deadline = time.time() + max(0.0, timeout_s)
1732
+ while True:
1733
+ try:
1734
+ fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
1735
+ break
1736
+ except BlockingIOError:
1737
+ if time.time() >= deadline:
1738
+ raise APError(f"Timed out waiting for repository lock: {name}")
1739
+ time.sleep(0.1)
1740
+ handle.seek(0)
1741
+ handle.truncate()
1742
+ handle.write(
1743
+ json.dumps(
1744
+ {
1745
+ "pid": os.getpid(),
1746
+ "host": socket.gethostname(),
1747
+ "acquired_at": _now_iso(),
1748
+ "name": name,
1749
+ },
1750
+ ensure_ascii=False,
1751
+ )
1752
+ + "\n"
1753
+ )
1754
+ handle.flush()
1755
+ yield
1756
+ finally:
1757
+ try:
1758
+ import fcntl # type: ignore
1759
+
1760
+ fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
1761
+ except Exception:
1762
+ pass
1763
+ handle.close()
1764
+
1765
+
1023
1766
  def _is_placeholder(value: object) -> bool:
1024
1767
  raw = _text(value)
1025
1768
  upper = raw.upper()
@@ -1058,6 +1801,48 @@ def _require_explicit_field(missing: List[str], field: str, value: object) -> No
1058
1801
  missing.append(f"{field} (must be explicitly filled, not blank/TODO)")
1059
1802
 
1060
1803
 
1804
+ _REQUIRED_ACCESS_FIELDS = [
1805
+ "access.project.frontend.url",
1806
+ "access.project.frontend.username",
1807
+ "access.project.frontend.password",
1808
+ "access.project.backend.url",
1809
+ "access.project.backend.username",
1810
+ "access.project.backend.password",
1811
+ "access.jenkins.frontend.url",
1812
+ "access.jenkins.frontend.username",
1813
+ "access.jenkins.frontend.password",
1814
+ "access.jenkins.backend.url",
1815
+ "access.jenkins.backend.username",
1816
+ "access.jenkins.backend.password",
1817
+ "access.gitlab.url",
1818
+ "access.gitlab.username",
1819
+ "access.gitlab.password",
1820
+ "access.nexus.frontend.url",
1821
+ "access.nexus.frontend.username",
1822
+ "access.nexus.frontend.password",
1823
+ ]
1824
+
1825
+
1826
+ def _nested_config_value(cfg: dict, field: str) -> object:
1827
+ value: object = cfg
1828
+ for part in field.split("."):
1829
+ if not isinstance(value, dict):
1830
+ return None
1831
+ value = value.get(part)
1832
+ return value
1833
+
1834
+
1835
+ def _access_config_issues(cfg: dict) -> list[str]:
1836
+ issues: list[str] = []
1837
+ for field in _REQUIRED_ACCESS_FIELDS:
1838
+ value = _nested_config_value(cfg, field)
1839
+ if not isinstance(value, str) or not _is_explicit_fill(value):
1840
+ issues.append(f"{field} (must be an explicitly filled string, not blank/TODO)")
1841
+ for field in [name for name in _REQUIRED_ACCESS_FIELDS if name.endswith(".url")]:
1842
+ _validate_url_field(issues, field, _nested_config_value(cfg, field))
1843
+ return issues
1844
+
1845
+
1061
1846
  def _has_secret_reference(section_cfg: dict, field: str) -> bool:
1062
1847
  return _is_explicit_fill(section_cfg.get(field)) or _is_explicit_fill(section_cfg.get(f"{field}_env"))
1063
1848
 
@@ -1095,7 +1880,7 @@ def _resolve_secret(section_name: str, section_cfg: dict, field: str) -> str:
1095
1880
 
1096
1881
  _GATE_SCOPES = {"auto", "changed", "standard", "full"}
1097
1882
  _WORKFLOW_PROFILES = {"auto", "micro", "standard", "high-risk"}
1098
- _PROFILE_GATE_SCOPE = {"micro": "changed", "standard": "standard", "high-risk": "full"}
1883
+ _PROFILE_GATE_SCOPE = {"micro": "changed", "standard": "changed", "high-risk": "changed"}
1099
1884
  _PROFILE_RANK = {"micro": 0, "standard": 1, "high-risk": 2}
1100
1885
  _SCOPE_RANK = {"changed": 0, "standard": 1, "full": 2}
1101
1886
  _DOC_PATH_PATTERNS = ["*.md", "docs/**"]
@@ -1226,11 +2011,12 @@ def _default_base_ref(repo: Path) -> str:
1226
2011
 
1227
2012
  def _changed_files(repo: Path, base_ref: str = "") -> list[str]:
1228
2013
  paths: list[str] = []
1229
- effective_base = base_ref or _default_base_ref(repo)
2014
+ manifest = _active_task_manifest(repo)
2015
+ effective_base = base_ref or (_text(manifest.get("base_sha")) if manifest else "") or _default_base_ref(repo)
1230
2016
  if effective_base:
1231
- paths.extend(_git_lines(repo, ["git", "diff", "--name-only", "--diff-filter=ACMRTUXB", f"{effective_base}...HEAD"]))
1232
- paths.extend(_git_lines(repo, ["git", "diff", "--name-only", "--diff-filter=ACMRTUXB"]))
1233
- paths.extend(_git_lines(repo, ["git", "diff", "--cached", "--name-only", "--diff-filter=ACMRTUXB"]))
2017
+ paths.extend(_git_lines(repo, ["git", "diff", "--name-only", "--diff-filter=ACDMRTUXB", f"{effective_base}...HEAD"]))
2018
+ paths.extend(_git_lines(repo, ["git", "diff", "--name-only", "--diff-filter=ACDMRTUXB"]))
2019
+ paths.extend(_git_lines(repo, ["git", "diff", "--cached", "--name-only", "--diff-filter=ACDMRTUXB"]))
1234
2020
  paths.extend(_git_lines(repo, ["git", "ls-files", "--others", "--exclude-standard"]))
1235
2021
  return _unique_paths(paths)
1236
2022
 
@@ -1256,10 +2042,15 @@ def _commit_changed_files(repo: Path, ref: str) -> list[str]:
1256
2042
  return _unique_paths([line.strip() for line in result.stdout.splitlines() if line.strip()])
1257
2043
 
1258
2044
 
1259
- def _cleanup_generated_noise(repo: Path) -> None:
2045
+ def _cleanup_generated_noise(repo: Path, protected_paths: Optional[set[str]] = None) -> None:
2046
+ protected = {path.replace("\\", "/") for path in (protected_paths or set())}
1260
2047
  candidates = [
1261
- rel for rel in _git_lines(repo, ["git", "ls-files", "--others", "--exclude-standard"])
1262
- if _is_generated_noise_path(rel)
2048
+ rel
2049
+ for rel in _git_z_paths(
2050
+ repo,
2051
+ ["git", "ls-files", "--others", "--exclude-standard", "-z"],
2052
+ )
2053
+ if _is_generated_noise_path(rel) and rel.replace("\\", "/") not in protected
1263
2054
  ]
1264
2055
  for rel in candidates:
1265
2056
  path = repo / rel
@@ -1324,44 +2115,16 @@ def _has_changed_gate(cfg: dict, gate_cfg: dict, paths: list[str]) -> bool:
1324
2115
 
1325
2116
  def _select_gate_scope(cfg: dict, requested_scope: str, paths: list[str]) -> tuple[str, list[str], list[dict]]:
1326
2117
  gate_cfg = _gate_cfg(cfg)
1327
- scope = (requested_scope or _text(gate_cfg.get("default_scope")) or "standard").lower()
2118
+ scope = (requested_scope or _text(gate_cfg.get("default_scope")) or "changed").lower()
1328
2119
  if scope not in _GATE_SCOPES:
1329
2120
  raise APError("gate scope must be one of: " + ", ".join(sorted(_GATE_SCOPES)))
1330
2121
 
1331
2122
  matching_rules = _matching_gate_rules(paths, gate_cfg)
1332
- reasons: list[str] = []
1333
-
1334
- rule_scopes = {_text(rule.get("scope")).lower() for rule in matching_rules if _text(rule.get("scope"))}
1335
- if "full" in rule_scopes:
1336
- return "full", ["matched gate rule with scope=full"], matching_rules
1337
- if any(_path_matches(path, _gate_full_patterns(gate_cfg)) for path in paths):
1338
- return "full", ["changed files match full-gate patterns"], matching_rules
1339
-
1340
- if scope != "auto":
1341
- return scope, [f"requested scope: {scope}"], matching_rules
1342
-
1343
- if not paths:
1344
- fallback = _text(gate_cfg.get("no_change_scope")) or "standard"
1345
- if fallback not in {"changed", "standard", "full"}:
1346
- fallback = "standard"
1347
- return fallback, ["no changed files detected"], matching_rules
1348
-
1349
- if "standard" in rule_scopes:
1350
- return "standard", ["matched gate rule with scope=standard"], matching_rules
1351
-
1352
- if _docs_only(paths):
1353
- return "changed", ["docs-only change"], matching_rules
1354
-
1355
- if _has_changed_gate(cfg, gate_cfg, paths):
1356
- return "changed", ["matched changed-gate command or rule"], matching_rules
1357
-
1358
- fallback = _text(gate_cfg.get("fallback_scope")) or "standard"
1359
- if fallback not in {"changed", "standard", "full"}:
1360
- fallback = "standard"
1361
- if fallback == "changed" and _text(gate_cfg.get("full_on_unknown")).lower() in {"1", "true", "yes", "on"}:
1362
- fallback = "full"
1363
- reasons.append("no changed-gate command/rule matched")
1364
- return fallback, reasons, matching_rules
2123
+ requested = "auto" if scope == "auto" else scope
2124
+ reason = "local development gate is fixed to changed/quick scope"
2125
+ if requested not in {"auto", "changed"}:
2126
+ reason += f"; ignored requested scope={requested}"
2127
+ return "changed", [reason], matching_rules
1365
2128
 
1366
2129
 
1367
2130
  def _impact_summary(
@@ -1373,7 +2136,7 @@ def _impact_summary(
1373
2136
  ) -> dict:
1374
2137
  paths = _changed_files(repo, base_ref=base_ref) if changed_paths is None else _unique_paths(changed_paths)
1375
2138
  docs_cfg = cfg.get("docs") or {}
1376
- ignored_runtime_paths = {
2139
+ ignored_runtime_paths = _task_runtime_paths(repo, cfg, _active_task_manifest(repo)) | {
1377
2140
  _text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl",
1378
2141
  _text(docs_cfg.get("closure_log")) or "docs/tasks/closure-log.md",
1379
2142
  _text(_gate_cfg(cfg).get("profile_log")) or ".local/auto-coding-skill/gate-profile.jsonl",
@@ -1415,12 +2178,13 @@ def _print_impact(summary: dict, as_json: bool = False) -> None:
1415
2178
  print("[impact] changed: (none)")
1416
2179
 
1417
2180
 
1418
- def _run_changed_gate(repo: Path, cfg: dict, paths: list[str], matching_rules: list[dict]) -> list[str]:
1419
- executed: list[str] = []
1420
- for rule in matching_rules:
1421
- executed.extend(_run_configured_command_list(repo, cfg, _as_list(rule.get("commands"))))
1422
- if executed:
1423
- return executed
2181
+ def _run_changed_gate(repo: Path, cfg: dict, paths: list[str]) -> list[str]:
2182
+ # The generic scaffold uses this command as its whole fast gate. The
2183
+ # built-in diff check below covers working tree, index, and task commits, so
2184
+ # do not execute the narrower configured spelling first as a duplicate.
2185
+ if _configured_command(cfg, "gate_changed").strip() == "git diff --check":
2186
+ print("[gate] gate_changed is handled by the built-in diff check")
2187
+ return []
1424
2188
 
1425
2189
  fallback_commands = ["gate_changed"]
1426
2190
  if _docs_only(paths):
@@ -1433,13 +2197,13 @@ def _run_changed_gate(repo: Path, cfg: dict, paths: list[str], matching_rules: l
1433
2197
  print("[gate] docs-only change with no changed-gate command configured; running built-in post checks only.")
1434
2198
  return ["docs_only_builtin"]
1435
2199
 
1436
- fallback = _run_first_configured_command(repo, cfg, ["quick_test", "test", "build"])
2200
+ fallback = _run_first_configured_command(repo, cfg, ["quick_test"])
1437
2201
  if fallback:
1438
2202
  return [fallback]
1439
2203
 
1440
2204
  raise APError(
1441
- "Changed gate has no configured command. Add commands.gate_changed or matching gate.rules commands, "
1442
- "or run light-gate --scope standard/full."
2205
+ "Changed gate has no fast command. Add commands.gate_changed or commands.quick_test. "
2206
+ "gate.rules commands are never executed by the automatic development flow."
1443
2207
  )
1444
2208
 
1445
2209
 
@@ -1457,16 +2221,21 @@ def _run_full_gate(repo: Path, cfg: dict) -> list[str]:
1457
2221
  command = _run_first_configured_command(repo, cfg, ["gate_full", "full_gate"])
1458
2222
  if not command:
1459
2223
  raise APError(
1460
- "Full gate is under-configured. High-risk and verify work requires "
1461
- "commands.gate_full or commands.full_gate; light/standard fallbacks do not count."
2224
+ "Optional full diagnostic is under-configured. Add commands.gate_full or "
2225
+ "commands.full_gate when you explicitly want to run it."
1462
2226
  )
1463
2227
  return [command]
1464
2228
 
1465
2229
 
1466
2230
  def _run_git_diff_check(repo: Path, cfg: dict) -> None:
1467
- print("[diff-check] git diff --check")
2231
+ print("[diff-check] working tree + index + task commits")
1468
2232
  start = time.time()
1469
2233
  run(["git", "diff", "--check"], cwd=repo)
2234
+ run(["git", "diff", "--cached", "--check"], cwd=repo)
2235
+ manifest = _active_task_manifest(repo)
2236
+ base_sha = _text(manifest.get("base_sha")) if manifest else ""
2237
+ if base_sha:
2238
+ run(["git", "diff", "--check", f"{base_sha}...HEAD"], cwd=repo)
1470
2239
  _record_gate_profile(repo, cfg, "diff_check", "pass", time.time() - start)
1471
2240
  _record_evidence(repo, cfg, "diff_check", "pass")
1472
2241
  print("[diff-check] OK")
@@ -2853,13 +3622,17 @@ def _classify_paths(paths: list[str]) -> dict:
2853
3622
  categories.add("docs")
2854
3623
  if any(token in lower for token in ["domain", "service", "usecase", "repository", "infrastructure", "adapter", "shared", "utils"]):
2855
3624
  categories.add("structure")
3625
+ return _classification_for_categories(categories, len(paths))
3626
+
3627
+
3628
+ def _classification_for_categories(categories: set[str], file_count: int) -> dict:
2856
3629
  return {
2857
3630
  "categories": sorted(categories),
2858
3631
  "needs_dd": bool(
2859
3632
  categories
2860
3633
  & {"api", "db", "auth", "payment", "file_transfer", "gateway", "prod_config", "release_or_tooling"}
2861
3634
  )
2862
- or len(paths) > 12,
3635
+ or file_count > 12,
2863
3636
  "needs_adr": bool(categories & {"structure", "release_or_tooling"}) and not categories <= {"docs"},
2864
3637
  "needs_browser": "ui" in categories,
2865
3638
  "needs_jenkins": bool(
@@ -2871,6 +3644,29 @@ def _classify_paths(paths: list[str]) -> dict:
2871
3644
  }
2872
3645
 
2873
3646
 
3647
+ def _classify_intent(intent: str) -> list[str]:
3648
+ value = _text(intent).lower()
3649
+ keyword_categories = {
3650
+ "db": ["database", "migration", "schema", "sql", "数据库", "数据迁移", "表结构"],
3651
+ "api": ["api", "controller", "endpoint", "接口", "控制器"],
3652
+ "auth": ["auth", "permission", "security", "登录", "认证", "鉴权", "权限", "安全"],
3653
+ "payment": ["payment", "billing", "checkout", "支付", "账单", "结算"],
3654
+ "file_transfer": ["upload", "download", "上传", "下载", "附件"],
3655
+ "gateway": ["gateway", "ingress", "nginx", "网关", "反向代理"],
3656
+ "prod_config": ["production config", "prod config", "生产配置"],
3657
+ "release_or_tooling": ["release", "deploy", "jenkins", "nexus", "发布", "部署", "构建"],
3658
+ "ui": ["frontend", "page", "component", "ui", "前端", "页面", "组件", "界面"],
3659
+ "test": ["test", "spec", "测试"],
3660
+ "docs": ["documentation", "docs", "文档"],
3661
+ "structure": ["refactor", "architecture", "重构", "架构", "分层"],
3662
+ }
3663
+ return sorted(
3664
+ category
3665
+ for category, keywords in keyword_categories.items()
3666
+ if any(keyword in value for keyword in keywords)
3667
+ )
3668
+
3669
+
2874
3670
  def _normalize_workflow_profile(value: object, default: str = "auto") -> str:
2875
3671
  profile = _text(value).lower() or default
2876
3672
  if profile not in _WORKFLOW_PROFILES:
@@ -2883,17 +3679,197 @@ def _configured_workflow_profile(cfg: dict) -> str:
2883
3679
 
2884
3680
 
2885
3681
  def _recommended_agents(profile: str, categories: set[str], classification: dict) -> list[str]:
3682
+ roles: list[str] = []
3683
+ for stage in _agent_execution_plan(profile, categories, classification)["stages"]:
3684
+ for role in stage["roles"]:
3685
+ if role != "main" and role not in roles:
3686
+ roles.append(role)
3687
+ return roles
3688
+
3689
+
3690
+ def _agent_contract_shape() -> tuple[dict, list[str]]:
3691
+ return (
3692
+ {
3693
+ "common": [
3694
+ "contract_version",
3695
+ "node_id",
3696
+ "task_id",
3697
+ "role",
3698
+ "base_sha",
3699
+ "scope",
3700
+ "depends_on",
3701
+ "acceptance",
3702
+ ],
3703
+ "writer": ["task_branch", "worktree_path", "owned_paths"],
3704
+ "reviewer": [
3705
+ "task_id",
3706
+ "diff_base",
3707
+ "diff_head",
3708
+ "diff_fingerprint",
3709
+ "owning_fixer",
3710
+ ],
3711
+ },
3712
+ [
3713
+ "contract_version",
3714
+ "node_id",
3715
+ "role",
3716
+ "task_id",
3717
+ "base_sha",
3718
+ "status",
3719
+ "summary",
3720
+ "depends_on",
3721
+ "owned_paths",
3722
+ "changed_paths",
3723
+ "diff_fingerprint",
3724
+ "evidence",
3725
+ "findings",
3726
+ "verdict",
3727
+ "risks",
3728
+ "next_owner",
3729
+ ],
3730
+ )
3731
+
3732
+
3733
+ def _validate_agent_plan(plan: dict) -> dict:
3734
+ required = {
3735
+ "contract_version",
3736
+ "contract_schema",
3737
+ "strategy",
3738
+ "policies",
3739
+ "assignment_contract",
3740
+ "result_contract",
3741
+ "stages",
3742
+ "constraints",
3743
+ }
3744
+ missing = sorted(required - set(plan))
3745
+ if missing or plan.get("contract_version") != _AGENT_CONTRACT_VERSION:
3746
+ raise APError("Invalid agent plan contract: " + (", ".join(missing) or "version"))
3747
+ if plan.get("contract_schema") != _AGENT_CONTRACT_SCHEMA:
3748
+ raise APError("Invalid agent plan schema path.")
3749
+ if not all(
3750
+ isinstance(stage, dict)
3751
+ and {"id", "mode", "roles", "depends_on"} <= set(stage)
3752
+ for stage in plan.get("stages") or []
3753
+ ):
3754
+ raise APError("Invalid agent plan stage contract.")
3755
+ return plan
3756
+
3757
+
3758
+ def _agent_execution_plan(profile: str, categories: set[str], classification: dict) -> dict:
3759
+ assignment_contract, result_contract = _agent_contract_shape()
2886
3760
  if profile == "micro":
2887
- return []
2888
- roles = ["explorer"]
3761
+ return _validate_agent_plan({
3762
+ "contract_version": _AGENT_CONTRACT_VERSION,
3763
+ "contract_schema": _AGENT_CONTRACT_SCHEMA,
3764
+ "strategy": "main-only",
3765
+ "policies": {
3766
+ "one_writer_per_worktree": True,
3767
+ "path_ownership": "explicit-non-overlapping",
3768
+ "dependency_policy": "integrate-before-dependent-start",
3769
+ "review_feedback_owner": "main",
3770
+ "review_binding": "diff-fingerprint",
3771
+ "lifecycle_owner": "main",
3772
+ "gate_owner": "main",
3773
+ },
3774
+ "assignment_contract": assignment_contract,
3775
+ "result_contract": result_contract,
3776
+ "stages": [
3777
+ {
3778
+ "id": "delivery",
3779
+ "mode": "serial",
3780
+ "roles": ["main"],
3781
+ "depends_on": [],
3782
+ }
3783
+ ],
3784
+ "constraints": [
3785
+ "Do not create subagents when the task has no independent work worth delegating.",
3786
+ "The main agent owns the fast gate, Git integration, push, and cleanup.",
3787
+ ],
3788
+ })
3789
+
3790
+ discovery_roles = ["explorer"]
2889
3791
  if categories & {"api", "release_or_tooling"}:
2890
- roles.append("docs_researcher")
2891
- roles.append("fixer")
3792
+ discovery_roles.append("docs_researcher")
2892
3793
  if classification.get("needs_browser"):
2893
- roles.append("browser_debugger")
2894
- if profile == "high-risk":
2895
- roles.append("reviewer")
2896
- return roles
3794
+ discovery_roles.append("browser_debugger")
3795
+
3796
+ return _validate_agent_plan({
3797
+ "contract_version": _AGENT_CONTRACT_VERSION,
3798
+ "contract_schema": _AGENT_CONTRACT_SCHEMA,
3799
+ "strategy": "orchestrated-subagents",
3800
+ "policies": {
3801
+ "one_writer_per_worktree": True,
3802
+ "path_ownership": "explicit-non-overlapping",
3803
+ "dependency_policy": "integrate-before-dependent-start",
3804
+ "review_feedback_owner": "owning-fixer",
3805
+ "review_binding": "diff-fingerprint",
3806
+ "lifecycle_owner": "main",
3807
+ "gate_owner": "main",
3808
+ },
3809
+ "assignment_contract": assignment_contract,
3810
+ "result_contract": result_contract,
3811
+ "stages": [
3812
+ {
3813
+ "id": "decomposition",
3814
+ "mode": "serial",
3815
+ "roles": ["main"],
3816
+ "depends_on": [],
3817
+ },
3818
+ {
3819
+ "id": "discovery",
3820
+ "mode": "parallel",
3821
+ "roles": discovery_roles,
3822
+ "depends_on": ["decomposition"],
3823
+ },
3824
+ {
3825
+ "id": "design",
3826
+ "mode": "serial",
3827
+ "roles": ["main"],
3828
+ "depends_on": ["discovery"],
3829
+ },
3830
+ {
3831
+ "id": "delivery",
3832
+ "mode": "dependency-waves",
3833
+ "roles": ["fixer", "reviewer", "main"],
3834
+ "depends_on": ["design"],
3835
+ "scale": "one fixer per independent development unit in the current dependency layer",
3836
+ "wave_phases": [
3837
+ {
3838
+ "id": "implementation",
3839
+ "mode": "parallel-isolated",
3840
+ "roles": ["fixer"],
3841
+ },
3842
+ {
3843
+ "id": "review",
3844
+ "mode": "parallel-read-only",
3845
+ "roles": ["reviewer"],
3846
+ "feedback_to": "owning fixer",
3847
+ },
3848
+ {
3849
+ "id": "gate-integrate",
3850
+ "mode": "serial",
3851
+ "roles": ["main"],
3852
+ },
3853
+ ],
3854
+ "next_wave": "start only after every prerequisite in the next layer is integrated",
3855
+ },
3856
+ {
3857
+ "id": "closure",
3858
+ "mode": "serial",
3859
+ "roles": ["main"],
3860
+ "depends_on": ["delivery"],
3861
+ },
3862
+ ],
3863
+ "constraints": [
3864
+ "Delegate only independent bounded work and declare dependencies before dispatch.",
3865
+ "Every fixer owns exactly one registered task branch/worktree and an explicit path scope.",
3866
+ "Never run two writers in one worktree or let the main agent edit a fixer-owned worktree concurrently.",
3867
+ "Start a dependent write task only after its prerequisite has been integrated into the target branch.",
3868
+ "Subagents do not commit, push, integrate, or clean task branches.",
3869
+ "Reviewers inspect a stable diff; changes-requested returns to the owning fixer and any edit requires re-review.",
3870
+ "The main agent routes review feedback, runs the single fast gate for each write task, integrates in dependency order, pushes, and cleans branches.",
3871
+ ],
3872
+ })
2897
3873
 
2898
3874
 
2899
3875
  def _resolve_execution_plan(
@@ -2905,6 +3881,8 @@ def _resolve_execution_plan(
2905
3881
  requested_mode: str = "",
2906
3882
  base_ref: str = "",
2907
3883
  changed_paths: Optional[list[str]] = None,
3884
+ planned_paths: Optional[list[str]] = None,
3885
+ intent: str = "",
2908
3886
  ) -> dict:
2909
3887
  impact = _impact_summary(
2910
3888
  cfg,
@@ -2914,21 +3892,29 @@ def _resolve_execution_plan(
2914
3892
  changed_paths=changed_paths,
2915
3893
  )
2916
3894
  paths = list(impact.get("changed_files") or [])
2917
- classification = _classify_paths(paths)
3895
+ normalized_planned_paths = sorted(
3896
+ {_normalize_owned_path(item) for item in (planned_paths or []) if _text(item)}
3897
+ )
3898
+ classification_inputs = [*paths, *normalized_planned_paths]
3899
+ classification = _classify_paths(classification_inputs)
3900
+ intent_categories = _classify_intent(intent)
3901
+ merged_categories = set(classification["categories"]) | set(intent_categories)
3902
+ classification = _classification_for_categories(merged_categories, len(classification_inputs))
2918
3903
  categories = set(classification["categories"])
2919
- matching_rules = _matching_gate_rules(paths, _gate_cfg(cfg))
3904
+ matching_rules = _matching_gate_rules(classification_inputs, _gate_cfg(cfg))
2920
3905
  configured_profile = _configured_workflow_profile(cfg)
2921
3906
  requested_profile_value = _normalize_workflow_profile(requested_profile) if requested_profile else ""
2922
3907
  configured_mode = _workflow_mode(cfg)
2923
3908
  requested_mode_value = _text(requested_mode).lower()
2924
- if requested_mode_value and requested_mode_value not in {"dev", "verify"}:
2925
- raise APError("workflow.mode must be 'dev' or 'verify'.")
3909
+ if requested_mode_value and requested_mode_value != "dev":
3910
+ raise APError("workflow.mode must be 'dev'; use explicit diagnostic commands when requested.")
2926
3911
 
2927
3912
  detected_profile = "standard"
2928
3913
  profile_reasons: list[str] = []
2929
- docs_only_paths = _docs_only(paths)
2930
- docs_or_tests_only = bool(paths) and (
2931
- (docs_only_paths and str(impact["selected_scope"]) != "full") or _tests_only(paths)
3914
+ docs_only_paths = _docs_only(classification_inputs)
3915
+ docs_or_tests_only = bool(classification_inputs) and (
3916
+ (docs_only_paths and str(impact["selected_scope"]) != "full")
3917
+ or _tests_only(classification_inputs)
2932
3918
  )
2933
3919
  if docs_or_tests_only and str(impact["selected_scope"]) != "full":
2934
3920
  detected_profile = "micro"
@@ -2960,14 +3946,10 @@ def _resolve_execution_plan(
2960
3946
  high_signals: list[str] = []
2961
3947
  if categories & high_categories and not docs_or_tests_only:
2962
3948
  high_signals.append("high-risk category: " + ", ".join(sorted(categories & high_categories)))
2963
- if str(impact["selected_scope"]) == "full":
2964
- high_signals.append("impact requires full gate")
2965
3949
  if "high-risk" in rule_profiles:
2966
3950
  high_signals.append("matched gate rule with profile=high-risk")
2967
- if len(paths) > 12 and not docs_or_tests_only:
3951
+ if len(classification_inputs) > 12 and not docs_or_tests_only:
2968
3952
  high_signals.append("change spans more than 12 files")
2969
- if requested_scope == "full":
2970
- high_signals.append("full scope explicitly requested")
2971
3953
 
2972
3954
  if high_signals:
2973
3955
  detected_profile = "high-risk"
@@ -2989,34 +3971,27 @@ def _resolve_execution_plan(
2989
3971
  if detected_profile == "high-risk" and effective_profile != "high-risk":
2990
3972
  effective_profile = "high-risk"
2991
3973
  profile_reasons.append("high-risk signals cannot be downgraded")
2992
- effective_mode = requested_mode_value or configured_mode
2993
- if configured_mode == "verify":
2994
- effective_mode = "verify"
2995
- if effective_mode == "verify" and effective_profile != "high-risk":
2996
- effective_profile = "high-risk"
2997
- profile_reasons.append("verify mode requires high-risk execution")
3974
+ effective_mode = "dev"
2998
3975
  if not profile_reasons:
2999
3976
  profile_reasons.append(f"configured profile={configured_profile}")
3000
3977
 
3001
- selected_scope = _PROFILE_GATE_SCOPE[effective_profile]
3002
- explicit_scope = _text(requested_scope).lower()
3003
- if explicit_scope in _SCOPE_RANK and _SCOPE_RANK[explicit_scope] > _SCOPE_RANK[selected_scope]:
3004
- selected_scope = explicit_scope
3005
- if str(impact["selected_scope"]) == "full":
3006
- selected_scope = "full"
3007
- effective_mode = "verify" if effective_profile == "high-risk" else effective_mode
3978
+ selected_scope = "changed"
3008
3979
  execution_reasons = list(impact.get("reasons") or [])
3009
- if selected_scope != str(impact["selected_scope"]):
3010
- execution_reasons.append(f"profile={effective_profile} requires scope={selected_scope}")
3980
+ execution_reasons.append("Jenkins/build/deploy and owner acceptance happen after coding completion")
3011
3981
 
3012
- jenkins_required = _verification_required(cfg, "jenkins", default=True)
3013
- target_env_required = _verification_required(cfg, "target_env", default=True)
3014
- needs_jenkins = bool(classification["needs_jenkins"] and jenkins_required)
3015
- needs_target = bool(classification["needs_target"] and target_env_required)
3982
+ needs_jenkins = False
3983
+ needs_target = False
3016
3984
  needs_dd = bool(classification["needs_dd"] or effective_profile == "high-risk")
3017
3985
 
3986
+ agent_plan = _agent_execution_plan(effective_profile, categories, classification)
3018
3987
  return {
3019
3988
  **impact,
3989
+ "contract_version": _AGENT_CONTRACT_VERSION,
3990
+ "contract_schema": _AGENT_CONTRACT_SCHEMA,
3991
+ "planned_files": normalized_planned_paths,
3992
+ "classification_files": classification_inputs,
3993
+ "intent_provided": bool(_text(intent)),
3994
+ "intent_categories": intent_categories,
3020
3995
  "reasons": execution_reasons,
3021
3996
  "configured_profile": configured_profile,
3022
3997
  "requested_profile": requested_profile_value or None,
@@ -3033,12 +4008,25 @@ def _resolve_execution_plan(
3033
4008
  "needs_jenkins": needs_jenkins,
3034
4009
  "needs_target": needs_target,
3035
4010
  "recommended_agents": _recommended_agents(effective_profile, categories, classification),
4011
+ "agent_plan": agent_plan,
3036
4012
  }
3037
4013
 
3038
4014
 
3039
4015
  def cmd_classify(args: argparse.Namespace) -> None:
3040
4016
  repo = Path(args.repo).resolve()
3041
4017
  cfg = _load_cfg(repo)
4018
+ intent_parts = [_text(getattr(args, "intent", ""))]
4019
+ intent_file = _text(getattr(args, "intent_file", ""))
4020
+ if intent_file:
4021
+ path = Path(intent_file)
4022
+ if not path.is_absolute():
4023
+ path = repo / path
4024
+ try:
4025
+ if path.stat().st_size > 65536:
4026
+ raise APError("--intent-file is limited to 64 KiB.")
4027
+ intent_parts.append(path.read_text(encoding="utf-8"))
4028
+ except OSError as exc:
4029
+ raise APError(f"Cannot read --intent-file: {path}: {exc}") from exc
3042
4030
  plan = _resolve_execution_plan(
3043
4031
  cfg,
3044
4032
  repo,
@@ -3046,17 +4034,13 @@ def cmd_classify(args: argparse.Namespace) -> None:
3046
4034
  requested_profile=_text(getattr(args, "profile", "")).lower(),
3047
4035
  requested_mode=_text(getattr(args, "mode", "")).lower(),
3048
4036
  base_ref=_text(getattr(args, "base", "")),
4037
+ planned_paths=list(getattr(args, "planned_path", []) or []),
4038
+ intent="\n".join(part for part in intent_parts if part),
3049
4039
  )
3050
4040
  risk = {"micro": "P3", "standard": "P2", "high-risk": "P1"}[plan["profile"]]
3051
- commands = [
3052
- "python3 docs/tools/autopipeline/ap.py impact --scope auto",
3053
- "python3 docs/tools/autopipeline/ap.py structure-check --scope auto",
3054
- f"python3 docs/tools/autopipeline/ap.py light-gate --profile {plan['profile']} --explain",
3055
- ]
4041
+ commands: list[str] = []
3056
4042
  if plan["needs_dd"]:
3057
4043
  commands.append("python3 docs/tools/autopipeline/ap.py scaffold design --write")
3058
- if plan["needs_jenkins"]:
3059
- commands.append("python3 docs/tools/autopipeline/ap.py verify-jenkins")
3060
4044
  result = {
3061
4045
  **plan,
3062
4046
  "risk": risk,
@@ -3071,6 +4055,13 @@ def cmd_classify(args: argparse.Namespace) -> None:
3071
4055
  print(f"[classify] selected_scope={result['selected_scope']}")
3072
4056
  print("[classify] categories=" + (", ".join(result["categories"]) or "(none)"))
3073
4057
  print("[classify] agents=" + (", ".join(result["recommended_agents"]) or "(main agent only)"))
4058
+ print(f"[classify] agent_strategy={result['agent_plan']['strategy']}")
4059
+ for stage in result["agent_plan"]["stages"]:
4060
+ print(
4061
+ "[classify] agent_stage="
4062
+ f"{stage['id']} mode={stage['mode']} roles={','.join(stage['roles'])} "
4063
+ f"depends_on={','.join(stage['depends_on']) or '-'}"
4064
+ )
3074
4065
  for key in ["needs_dd", "needs_adr", "needs_browser", "needs_jenkins", "needs_target"]:
3075
4066
  print(f"[classify] {key}={result[key]}")
3076
4067
  for command in commands:
@@ -3104,18 +4095,17 @@ def cmd_run(args: argparse.Namespace) -> None:
3104
4095
  def cmd_light_gate(args: argparse.Namespace) -> None:
3105
4096
  repo = Path(args.repo).resolve()
3106
4097
  light_gate_start = time.time()
3107
- cmd_doctor(
3108
- argparse.Namespace(
3109
- repo=str(repo),
3110
- profile=_text(getattr(args, "profile", "")).lower(),
3111
- mode=_text(getattr(args, "mode", "")).lower(),
3112
- )
3113
- )
3114
4098
  cfg = _load_cfg(repo)
4099
+ requested_scope = _text(getattr(args, "scope", "")).lower()
4100
+ if requested_scope not in {"", "auto", "changed"}:
4101
+ raise APError(
4102
+ "light-gate only runs the changed-scope fast gate. "
4103
+ "Use 'ap.py run gate_standard' or 'ap.py run gate_full' for an explicit diagnostic."
4104
+ )
3115
4105
  plan = _resolve_execution_plan(
3116
4106
  cfg,
3117
4107
  repo,
3118
- requested_scope=_text(getattr(args, "scope", "")).lower(),
4108
+ requested_scope=requested_scope,
3119
4109
  requested_profile=_text(getattr(args, "profile", "")).lower(),
3120
4110
  requested_mode=_text(getattr(args, "mode", "")).lower(),
3121
4111
  base_ref=_text(getattr(args, "base", "")),
@@ -3125,27 +4115,12 @@ def cmd_light_gate(args: argparse.Namespace) -> None:
3125
4115
  for reason in plan["profile_reasons"]:
3126
4116
  print(f"[impact] profile_reason: {reason}")
3127
4117
 
3128
- selected_scope = str(plan["selected_scope"])
4118
+ selected_scope = "changed"
3129
4119
  paths = list(plan.get("changed_files") or [])
3130
- matching_rules = _matching_gate_rules(paths, _gate_cfg(cfg))
3131
- executed: list[str] = []
3132
- executed.extend(_run_structure_check_for_gate(repo, cfg, selected_scope, str(getattr(args, "base", "") or "")))
3133
-
3134
- if selected_scope == "changed":
3135
- executed.extend(_run_changed_gate(repo, cfg, paths, matching_rules))
3136
- elif selected_scope == "full":
3137
- executed.extend(_run_full_gate(repo, cfg))
3138
- else:
3139
- executed.extend(_run_standard_gate(repo, cfg))
4120
+ executed = _run_changed_gate(repo, cfg, paths)
3140
4121
 
3141
4122
  _run_git_diff_check(repo, cfg)
3142
- cmd_verify_api_docs(argparse.Namespace(repo=str(repo)))
3143
- executed.extend(["diff_check", "verify_api_docs"])
3144
- if _verification_required(cfg, "jenkins", default=True):
3145
- cmd_verify_jenkins(argparse.Namespace(repo=str(repo)))
3146
- executed.append("verify_jenkins")
3147
- else:
3148
- executed.append("verify_jenkins_skipped")
4123
+ executed.append("diff_check")
3149
4124
  duration_s = time.time() - light_gate_start
3150
4125
  _record_gate_profile(repo, cfg, "light_gate", "pass", duration_s, scope=selected_scope, detail=", ".join(executed))
3151
4126
  _record_evidence(
@@ -3323,26 +4298,40 @@ def cmd_doctor(args: argparse.Namespace) -> None:
3323
4298
  cfg = _load_cfg(repo)
3324
4299
  workflow_cfg = (cfg.get("workflow") or {})
3325
4300
  project_cfg = (cfg.get("project") or {})
3326
- commands = (cfg.get("commands") or {})
3327
- target_cfg = (cfg.get("target_env") or {})
3328
- jenkins_cfg = (cfg.get("jenkins") or {})
3329
4301
  docs_cfg = (cfg.get("docs") or {})
3330
4302
  runtime_cfg = (cfg.get("runtime") or {})
3331
4303
  structure_cfg = _structure_cfg(cfg)
4304
+ concurrency_cfg = _concurrency_cfg(cfg)
3332
4305
 
3333
4306
  missing: List[str] = []
3334
4307
  validation_errors: List[str] = []
3335
4308
 
3336
4309
  mode = str(workflow_cfg.get("mode") or "dev").strip().lower()
3337
4310
  profile = str(workflow_cfg.get("profile") or "auto").strip().lower()
3338
- target_env_required = _verification_required(cfg, "target_env", default=True)
3339
- jenkins_required = _verification_required(cfg, "jenkins", default=True)
3340
- if mode not in {"dev", "verify"}:
3341
- missing.append("workflow.mode (must be dev or verify)")
4311
+ completion = str(workflow_cfg.get("completion") or "").strip().lower()
4312
+ if mode != "dev":
4313
+ missing.append("workflow.mode (must be dev; external verification is owner-managed)")
3342
4314
  if profile not in _WORKFLOW_PROFILES:
3343
4315
  missing.append("workflow.profile (must be auto, micro, standard, or high-risk)")
4316
+ if completion != "push":
4317
+ missing.append("workflow.completion (must be push)")
4318
+ isolation = _text(concurrency_cfg.get("isolation")).lower() or "worktree"
4319
+ if isolation != "worktree":
4320
+ validation_errors.append(
4321
+ "concurrency.isolation must be worktree; shared-checkout/legacy writes are no longer supported"
4322
+ )
4323
+ branch_prefix = _text(concurrency_cfg.get("branch_prefix")) or "codex/"
4324
+ if not branch_prefix.endswith("/"):
4325
+ validation_errors.append("concurrency.branch_prefix must end with '/'")
4326
+ elif run(
4327
+ ["git", "check-ref-format", "--branch", f"{branch_prefix}TASK"],
4328
+ cwd=repo,
4329
+ check=False,
4330
+ ).returncode != 0:
4331
+ validation_errors.append("concurrency.branch_prefix does not form a valid Git branch")
3344
4332
  if not str(project_cfg.get("name") or "").strip():
3345
4333
  missing.append("project.name")
4334
+ missing.extend(_access_config_issues(cfg))
3346
4335
  enforcement = _text(structure_cfg.get("enforcement")).lower()
3347
4336
  if enforcement and enforcement not in {"advisory", "blocking"}:
3348
4337
  validation_errors.append("structure.enforcement must be advisory or blocking")
@@ -3370,72 +4359,8 @@ def cmd_doctor(args: argparse.Namespace) -> None:
3370
4359
  f"gate.rules[{index}].scope must be changed, standard, or full"
3371
4360
  )
3372
4361
 
3373
- if mode in {"dev", "verify"} and profile in _WORKFLOW_PROFILES and rules_valid:
3374
- try:
3375
- plan = _resolve_execution_plan(
3376
- cfg,
3377
- repo,
3378
- requested_profile=_text(getattr(args, "profile", "")).lower(),
3379
- requested_mode=_text(getattr(args, "mode", "")).lower(),
3380
- )
3381
- except APError as exc:
3382
- validation_errors.append(str(exc))
3383
- else:
3384
- effective_profile = str(plan["profile"])
3385
- changed_files = list(plan.get("changed_files") or [])
3386
- matching_rules = _matching_gate_rules(changed_files, gate_cfg)
3387
- rule_changed_gate = any(
3388
- any(_configured_command(cfg, _command_name(ref)) for ref in _as_list(rule.get("commands")))
3389
- for rule in matching_rules
3390
- )
3391
- changed_gate = bool(
3392
- _configured_command(cfg, "gate_changed")
3393
- or _configured_command(cfg, "quick_test")
3394
- or _configured_command(cfg, "test")
3395
- or _configured_command(cfg, "build")
3396
- or rule_changed_gate
3397
- or _docs_only(changed_files)
3398
- )
3399
- standard_gate = bool(
3400
- _configured_command(cfg, "gate_standard")
3401
- or _configured_command(cfg, "light_gate")
3402
- or _configured_command(cfg, "quick_test")
3403
- or _configured_command(cfg, "test")
3404
- or _configured_command(cfg, "build")
3405
- )
3406
- full_gate = bool(
3407
- _configured_command(cfg, "gate_full")
3408
- or _configured_command(cfg, "full_gate")
3409
- )
3410
- if effective_profile == "micro" and not changed_gate:
3411
- missing.append(
3412
- "micro gate command: commands.gate_changed, quick_test, test, build, "
3413
- "or a matching gate rule command"
3414
- )
3415
- elif effective_profile == "standard" and not standard_gate:
3416
- missing.append(
3417
- "standard gate command: commands.gate_standard, light_gate, quick_test, test, or build"
3418
- )
3419
- elif effective_profile == "high-risk" and not full_gate:
3420
- missing.append(
3421
- "full gate command: commands.gate_full or commands.full_gate "
3422
- "(required for high-risk/verify execution)"
3423
- )
3424
- if target_env_required:
3425
- _require_explicit_field(missing, "target_env.health_base_url", target_cfg.get("health_base_url"))
3426
- _require_explicit_field(missing, "target_env.health_path", target_cfg.get("health_path"))
3427
-
3428
- if jenkins_required:
3429
- _require_explicit_field(missing, "jenkins.base_url", jenkins_cfg.get("base_url"))
3430
- _require_explicit_field(missing, "jenkins.ui_username", jenkins_cfg.get("ui_username"))
3431
- _require_secret_reference(missing, "jenkins", jenkins_cfg, "ui_password")
3432
- _require_explicit_field(missing, "jenkins.job_url", jenkins_cfg.get("job_url"))
3433
- _require_explicit_field(missing, "jenkins.trigger_branch", jenkins_cfg.get("trigger_branch"))
3434
- _require_explicit_field(missing, "jenkins.image_repository", jenkins_cfg.get("image_repository"))
3435
- _require_explicit_field(missing, "jenkins.image_tag_strategy", jenkins_cfg.get("image_tag_strategy"))
3436
- _require_explicit_field(missing, "jenkins.deploy_env", jenkins_cfg.get("deploy_env"))
3437
- _require_explicit_field(missing, "jenkins.api_user", jenkins_cfg.get("api_user"))
3438
- _require_secret_reference(missing, "jenkins", jenkins_cfg, "api_password")
4362
+ if not (_configured_command(cfg, "gate_changed") or _configured_command(cfg, "quick_test")):
4363
+ missing.append("fast gate command: commands.gate_changed or commands.quick_test")
3439
4364
 
3440
4365
  repo_docs = {
3441
4366
  "docs.taskbook": Path(repo, str(docs_cfg.get("taskbook", "docs/tasks/taskbook.md"))),
@@ -3455,33 +4380,10 @@ def cmd_doctor(args: argparse.Namespace) -> None:
3455
4380
  if not path.exists():
3456
4381
  validation_errors.append(f"{key} missing on disk: {path}")
3457
4382
 
3458
- _validate_url_field(validation_errors, "target_env.frontend_base_url", target_cfg.get("frontend_base_url"))
3459
- _validate_url_field(validation_errors, "target_env.backend_base_url", target_cfg.get("backend_base_url"))
3460
- _validate_url_field(validation_errors, "target_env.health_base_url", target_cfg.get("health_base_url"))
3461
- _validate_path_field(validation_errors, "target_env.health_path", target_cfg.get("health_path"))
3462
- _validate_url_field(validation_errors, "jenkins.base_url", jenkins_cfg.get("base_url"))
3463
- _validate_url_field(validation_errors, "jenkins.job_url", jenkins_cfg.get("job_url"))
3464
-
3465
4383
  runtime_enabled = any(str(runtime_cfg.get(key) or "").strip() for key in ["docker_compose_file", "docker_service", "health_base_url", "health_path"])
3466
4384
  if runtime_enabled and not str(runtime_cfg.get("docker_compose_file") or "").strip():
3467
4385
  validation_errors.append("runtime config is partially enabled but runtime.docker_compose_file is missing")
3468
4386
 
3469
- ledger_result = _docs_ledger_check_result(repo, cfg)
3470
- ledger_status = "skipped" if not ledger_result.get("enabled", True) else ("fail" if ledger_result.get("blocking") else "pass")
3471
- _record_evidence(repo, cfg, "docs_ledger_check", ledger_status, ledger_result)
3472
- for issue in ledger_result.get("blocking") or []:
3473
- validation_errors.append(f"docs-ledger: {issue}")
3474
- for warning in ledger_result.get("warnings") or []:
3475
- print(f"[doctor] WARN docs-ledger: {warning}")
3476
-
3477
- try:
3478
- timeout_s = int(jenkins_cfg.get("deploy_timeout_sec") or 0)
3479
- if (jenkins_required or _is_explicit_fill(jenkins_cfg.get("deploy_timeout_sec"))) and timeout_s <= 0:
3480
- validation_errors.append("jenkins.deploy_timeout_sec must be a positive integer")
3481
- except Exception:
3482
- if jenkins_required or _is_explicit_fill(jenkins_cfg.get("deploy_timeout_sec")):
3483
- validation_errors.append("jenkins.deploy_timeout_sec must be a positive integer")
3484
-
3485
4387
  missing.extend(validation_errors)
3486
4388
 
3487
4389
  if missing:
@@ -3493,7 +4395,7 @@ def cmd_doctor(args: argparse.Namespace) -> None:
3493
4395
  cfg,
3494
4396
  "doctor",
3495
4397
  "pass",
3496
- {"mode": mode, "target_env_required": target_env_required, "jenkins_required": jenkins_required},
4398
+ {"mode": mode, "completion": completion, "access_fields": len(_REQUIRED_ACCESS_FIELDS)},
3497
4399
  )
3498
4400
  print("[doctor] OK")
3499
4401
 
@@ -3657,9 +4559,7 @@ def _validate_closure_result(effective_mode: str, result: str) -> None:
3657
4559
  mode = _text(effective_mode).lower()
3658
4560
  normalized_result = _text(result).upper()
3659
4561
  if mode == "dev" and normalized_result == "PASS":
3660
- raise APError("PASS requires verify mode; use DEV-CLOSED, PARTIAL, or FAIL for dev execution.")
3661
- if mode == "verify" and normalized_result == "DEV-CLOSED":
3662
- raise APError("DEV-CLOSED is only valid for dev mode; use PASS, PARTIAL, or FAIL for verify execution.")
4562
+ raise APError("Automatic coding closure uses DEV-CLOSED; owner acceptance is handled after push.")
3663
4563
 
3664
4564
 
3665
4565
  def _validate_closure_evidence(result: str, verification_items: list[str]) -> None:
@@ -3678,6 +4578,7 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3678
4578
  repo = Path(args.repo).resolve()
3679
4579
  ensure_git_repo(repo)
3680
4580
  cfg = _load_cfg(repo)
4581
+ context_manifest = _require_task_context(repo, cfg, args.task_id)
3681
4582
  plan = _resolve_execution_plan(
3682
4583
  cfg,
3683
4584
  repo,
@@ -3701,7 +4602,12 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3701
4602
  docs_cfg = (cfg.get("docs") or {})
3702
4603
  target_cfg = (cfg.get("target_env") or {})
3703
4604
  taskbook = Path(repo, str(docs_cfg.get("taskbook", "docs/tasks/taskbook.md")))
3704
- closure_log = Path(repo, str(docs_cfg.get("closure_log", "docs/tasks/closure-log.md")))
4605
+ manifest = context_manifest or _active_task_manifest(repo)
4606
+ closure_log = (
4607
+ _task_doc_path(repo, cfg, manifest, "closure")
4608
+ if manifest
4609
+ else Path(repo, str(docs_cfg.get("closure_log", "docs/tasks/closure-log.md")))
4610
+ )
3705
4611
  closure_log.parent.mkdir(parents=True, exist_ok=True)
3706
4612
  if not closure_log.exists():
3707
4613
  closure_log.write_text("# Closure Log\n\n", encoding="utf-8")
@@ -3710,20 +4616,20 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3710
4616
  title = str(args.title or "").strip() or _infer_title(taskbook, task_id)
3711
4617
  timestamp = _dt.datetime.now().strftime("%Y-%m-%d %H:%M")
3712
4618
  commit_value = _resolve_git_short_sha(repo, args.commit)
3713
- target_env = str(args.target_env or target_cfg.get("name") or "").strip() or "(not set)"
3714
- verification_text = "; ".join(verification_items) if verification_items else "TODO"
4619
+ target_env = str(args.target_env or target_cfg.get("name") or "").strip() or "owner-managed acceptance"
4620
+ verification_text = "; ".join(verification_items) if verification_items else "fast gate evidence not recorded"
3715
4621
  follow_up = str(args.follow_up or "").strip() or "none"
3716
- jenkins_build = str(args.jenkins or "").strip() or "TODO"
3717
- structure_check = str(getattr(args, "structure_check", "") or "").strip() or "TODO"
4622
+ jenkins_build = str(args.jenkins or "").strip() or "owner-managed after push"
4623
+ structure_check = str(getattr(args, "structure_check", "") or "").strip() or "not part of the fast local gate"
3718
4624
  lines = [
3719
4625
  f"## {task_id} — {title} — {timestamp}",
3720
4626
  f"- Task: {task_id}",
3721
4627
  f"- Commit: {commit_value}",
3722
- f"- CI/Jenkins Build: {jenkins_build}",
3723
- f"- Target Env: {target_env}",
3724
- f"- Verification: {verification_text}",
4628
+ f"- Fast Local Gate: {verification_text}",
4629
+ f"- External Build/Deploy: {jenkins_build}",
4630
+ f"- Owner Acceptance: {target_env}",
3725
4631
  f"- Effective Profile: {profile}",
3726
- f"- Structure Check: {structure_check}",
4632
+ f"- Optional Structure Diagnostic: {structure_check}",
3727
4633
  f"- Result: {args.result}",
3728
4634
  f"- Follow-up: {follow_up}",
3729
4635
  ]
@@ -3739,6 +4645,8 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3739
4645
  f.write("\n")
3740
4646
  f.write("\n".join(lines))
3741
4647
  f.write("\n")
4648
+ if manifest:
4649
+ _update_active_task_status(repo, cfg, manifest, str(args.result))
3742
4650
  _record_evidence(
3743
4651
  repo,
3744
4652
  cfg,
@@ -3755,283 +4663,2088 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3755
4663
  print(f"[record-closure] OK: {closure_log}")
3756
4664
 
3757
4665
 
3758
- def _record_commit_push_closure(
3759
- repo: Path,
3760
- args: argparse.Namespace,
3761
- *,
3762
- commit: str,
3763
- jenkins: str,
3764
- target_env: str,
3765
- verification: List[str],
3766
- result: str,
3767
- follow_up: str,
3768
- structure_check: str,
3769
- ) -> None:
3770
- cmd_record_closure(
3771
- argparse.Namespace(
3772
- repo=str(repo),
3773
- task_id=args.task_id,
3774
- title=args.title,
3775
- commit=commit,
3776
- jenkins=jenkins,
3777
- target_env=target_env,
3778
- verification=verification,
3779
- result=result,
3780
- follow_up=follow_up,
3781
- profile=str(getattr(args, "effective_profile", "") or getattr(args, "profile", "") or ""),
3782
- structure_check=structure_check,
3783
- initial_commit=args.initial_commit,
3784
- jenkins_failure=args.jenkins_failure,
3785
- fix_commit=args.fix_commit,
3786
- )
4666
+ def _task_remote_and_target(cfg: dict, args: argparse.Namespace) -> tuple[str, str, str]:
4667
+ concurrency_cfg = _concurrency_cfg(cfg)
4668
+ remote = _text(getattr(args, "remote", "")) or _text(concurrency_cfg.get("remote")) or "origin"
4669
+ target_branch = (
4670
+ _text(getattr(args, "target_branch", ""))
4671
+ or _text(concurrency_cfg.get("target_branch"))
4672
+ )
4673
+ base_ref = _text(getattr(args, "base", "")) or _text(concurrency_cfg.get("base_ref"))
4674
+ if not target_branch and base_ref.startswith(f"{remote}/"):
4675
+ target_branch = base_ref[len(remote) + 1 :]
4676
+ if not target_branch:
4677
+ branch = _current_branch(Path(args.repo).resolve())
4678
+ target_branch = branch or "dev"
4679
+ if not base_ref:
4680
+ base_ref = f"{remote}/{target_branch}"
4681
+ return remote, target_branch, base_ref
4682
+
4683
+
4684
+ def _fetch_target(repo: Path, remote: str, target_branch: str) -> str:
4685
+ remote_ref = f"refs/remotes/{remote}/{target_branch}"
4686
+ run(
4687
+ [
4688
+ "git",
4689
+ "fetch",
4690
+ "--no-tags",
4691
+ remote,
4692
+ f"refs/heads/{target_branch}:{remote_ref}",
4693
+ ],
4694
+ cwd=repo,
3787
4695
  )
4696
+ return _resolve_commit(repo, remote_ref)
3788
4697
 
3789
4698
 
3790
- def cmd_commit_push(args: argparse.Namespace) -> None:
3791
- repo = Path(args.repo).resolve()
3792
- ensure_git_repo(repo)
3793
- cmd_doctor(
3794
- argparse.Namespace(
3795
- repo=str(repo),
3796
- profile=_text(getattr(args, "profile", "")).lower(),
3797
- mode=_text(getattr(args, "mode", "")).lower(),
4699
+ def _task_branch_prefix(cfg: dict) -> str:
4700
+ prefix = _text(_concurrency_cfg(cfg).get("branch_prefix")) or "codex/"
4701
+ if not prefix.endswith("/"):
4702
+ raise APError("concurrency.branch_prefix must end with '/'.")
4703
+ return prefix
4704
+
4705
+
4706
+ def _task_worktree_path(repo: Path, cfg: dict, task_id: str, override: str = "") -> Path:
4707
+ raw_root = override or _text(_concurrency_cfg(cfg).get("worktree_root")) or "../.worktrees"
4708
+ root = Path(raw_root).expanduser()
4709
+ if not root.is_absolute():
4710
+ root = repo / root
4711
+ root = root.resolve()
4712
+ candidate = (root / repo.name / _validate_task_id(task_id)).resolve()
4713
+ if repo.resolve() == candidate or repo.resolve() in candidate.parents:
4714
+ raise APError("Task worktrees must live outside the primary repository directory.")
4715
+ return candidate
4716
+
4717
+
4718
+ def _parse_null_config_entries(result, *, context: str) -> dict[str, list[str]]:
4719
+ if result.returncode == 1 and not result.stdout:
4720
+ return {}
4721
+ if result.returncode != 0:
4722
+ raise APError(
4723
+ f"Cannot inspect {context}: {result.stderr.strip() or result.stdout.strip()}"
3798
4724
  )
4725
+ entries: dict[str, list[str]] = {}
4726
+ for item in result.stdout.split("\0"):
4727
+ if not item:
4728
+ continue
4729
+ key, separator, value = item.partition("\n")
4730
+ if not separator or not key:
4731
+ raise APError(f"Malformed Git config entry while inspecting {context}: {item!r}")
4732
+ entries.setdefault(key, []).append(value)
4733
+ return entries
4734
+
4735
+
4736
+ def _gitmodules_urls(repo: Path) -> dict[str, str]:
4737
+ path = repo / ".gitmodules"
4738
+ if not path.exists():
4739
+ return {}
4740
+ result = run(
4741
+ [
4742
+ "git",
4743
+ "config",
4744
+ "--null",
4745
+ "--file",
4746
+ str(path),
4747
+ "--get-regexp",
4748
+ r"^submodule\..*\.url$",
4749
+ ],
4750
+ cwd=repo,
4751
+ check=False,
3799
4752
  )
3800
- cfg = _load_cfg(repo)
3801
- plan = _resolve_execution_plan(
3802
- cfg,
3803
- repo,
3804
- requested_scope="",
3805
- requested_profile=_text(getattr(args, "profile", "")).lower(),
3806
- requested_mode=_text(getattr(args, "mode", "")).lower(),
3807
- )
3808
- mode = str(plan["effective_mode"])
3809
- args.effective_profile = str(plan["profile"])
3810
- if args.result:
3811
- _validate_closure_result(mode, str(args.result))
3812
- target_cfg = (cfg.get("target_env") or {})
3813
- target_env_required = _verification_required(cfg, "target_env", default=True)
3814
- jenkins_required = _verification_required(cfg, "jenkins", default=True)
4753
+ entries = _parse_null_config_entries(result, context=f"submodule URLs in {path}")
4754
+ return {key: values[-1] for key, values in entries.items() if values}
3815
4755
 
3816
- msg = args.msg
3817
- structure_check_status = "skipped"
3818
-
3819
- if args.require_runtime_health:
3820
- cmd_wait_health(argparse.Namespace(repo=str(repo), scope="runtime"))
3821
-
3822
- if mode in {"dev", "verify"} or args.require_light_gate:
3823
- cmd_light_gate(
3824
- argparse.Namespace(
3825
- repo=str(repo),
3826
- scope=plan["selected_scope"],
3827
- profile=plan["profile"],
3828
- mode=mode,
3829
- base="",
3830
- explain=False,
3831
- )
3832
- )
3833
- structure_check_status = "passed via light-gate" if _structure_gate_enabled(cfg) else "skipped (structure disabled)"
3834
4756
 
3835
- if args.require_jenkins:
3836
- cmd_verify_jenkins(argparse.Namespace(repo=str(repo)))
4757
+ def _common_submodule_config(repo: Path) -> dict[str, list[str]]:
4758
+ result = run(
4759
+ [
4760
+ "git",
4761
+ "config",
4762
+ "--null",
4763
+ "--local",
4764
+ "--get-regexp",
4765
+ r"^submodule\.",
4766
+ ],
4767
+ cwd=repo,
4768
+ check=False,
4769
+ )
4770
+ return _parse_null_config_entries(result, context="shared submodule config")
3837
4771
 
3838
- if args.require_matrix and mode != "verify":
3839
- cmd_check_matrix(argparse.Namespace(repo=str(repo)))
3840
4772
 
3841
- if mode == "dev":
3842
- dev_verification = args.verification or [
3843
- "light-gate only",
3844
- "configured CI/Jenkins not verified in dev mode",
3845
- "target environment not verified in dev mode",
3846
- ]
3847
- _record_commit_push_closure(
3848
- repo,
3849
- args,
3850
- commit="generated by this commit-push run",
3851
- jenkins=args.jenkins_build or "not verified in dev mode",
3852
- target_env=args.target_env or "not verified in dev mode",
3853
- verification=dev_verification,
3854
- result=args.result or "DEV-CLOSED",
3855
- follow_up=args.follow_up or "Run verify mode when configured CI/Jenkins and target-environment evidence is required.",
3856
- structure_check=args.structure_check or structure_check_status,
4773
+ def _config_values(repo: Path, scope: str, key: str) -> list[str]:
4774
+ result = run(
4775
+ ["git", "config", "--null", scope, "--get-all", key],
4776
+ cwd=repo,
4777
+ check=False,
4778
+ )
4779
+ if result.returncode == 1 and not result.stdout:
4780
+ return []
4781
+ if result.returncode != 0:
4782
+ raise APError(
4783
+ f"Cannot inspect {scope} Git config {key!r}: "
4784
+ f"{result.stderr.strip() or result.stdout.strip()}"
3857
4785
  )
4786
+ return [value for value in result.stdout.split("\0") if value]
3858
4787
 
3859
- _cleanup_generated_noise(repo)
3860
- run(["git", "add", "-A"], cwd=repo)
3861
- diff = run(["git", "diff", "--cached", "--name-only"], cwd=repo).stdout.strip()
3862
- if not diff:
3863
- raise APError("Nothing to commit.")
3864
4788
 
3865
- run(["git", "commit", "-m", msg], cwd=repo)
3866
- run(["git", "push"], cwd=repo)
3867
-
3868
- if mode == "verify":
3869
- if jenkins_required:
3870
- jenkins_build = cmd_verify_jenkins_build(
3871
- argparse.Namespace(
3872
- repo=str(repo),
3873
- git_ref="HEAD",
3874
- job_name=args.job_name,
3875
- job_url=args.job_url,
3876
- multibranch_root_job=args.multibranch_root_job,
3877
- branch_name=args.branch_name,
3878
- build_number=args.build_number,
3879
- max_builds=args.max_builds,
3880
- timeout_sec=args.timeout_sec,
3881
- poll_sec=args.poll_sec,
3882
- allow_no_deploy=args.allow_no_deploy,
3883
- )
3884
- )
3885
- else:
3886
- jenkins_build = "skipped by verification.jenkins_required=false"
3887
-
3888
- if target_env_required:
3889
- target_summary = cmd_verify_target(
3890
- argparse.Namespace(
3891
- repo=str(repo),
3892
- backend_path=args.backend_path,
3893
- frontend_path=args.frontend_path,
3894
- backend_basic_auth=args.backend_basic_auth,
3895
- frontend_basic_auth=args.frontend_basic_auth,
3896
- )
3897
- )
3898
- else:
3899
- target_summary = "skipped by verification.target_env_required=false"
3900
- if args.require_matrix:
3901
- cmd_check_matrix(argparse.Namespace(repo=str(repo)))
3902
- verification = args.verification
3903
- if not verification:
3904
- verification = []
3905
- if jenkins_required:
3906
- verification.append(f"CI/Jenkins build verified: {jenkins_build or 'verified by git-ref HEAD'}")
3907
- else:
3908
- verification.append("CI/Jenkins verification skipped by verification.jenkins_required=false")
3909
- if target_env_required:
3910
- verification.append(f"Target verification: {target_summary}")
3911
- else:
3912
- verification.append("Target verification skipped by verification.target_env_required=false")
3913
- _record_commit_push_closure(
3914
- repo,
3915
- args,
3916
- commit="HEAD",
3917
- jenkins=args.jenkins_build or jenkins_build or "verified by git-ref HEAD",
3918
- target_env=args.target_env
3919
- or (str(target_cfg.get("name") or "").strip() if target_env_required else "skipped by verification.target_env_required=false"),
3920
- verification=verification,
3921
- result=args.result or "PASS",
3922
- follow_up=args.follow_up or "none",
3923
- structure_check=args.structure_check or structure_check_status,
3924
- )
3925
- _cleanup_generated_noise(repo)
3926
- run(["git", "add", "-A"], cwd=repo)
3927
- closure_diff = run(["git", "diff", "--cached", "--name-only"], cwd=repo).stdout.strip()
3928
- if closure_diff:
3929
- run(["git", "commit", "-m", f"{args.task_id}: record verification closure [skip ci]"], cwd=repo)
3930
- run(["git", "push"], cwd=repo)
3931
- elif args.record_closure and mode != "dev":
3932
- _record_commit_push_closure(
3933
- repo,
3934
- args,
3935
- commit="HEAD",
3936
- jenkins=args.jenkins_build or "TODO",
3937
- target_env=args.target_env or str(target_cfg.get("name") or "").strip(),
3938
- verification=args.verification or [],
3939
- result=args.result or "PARTIAL",
3940
- follow_up=args.follow_up or "none",
3941
- structure_check=args.structure_check or structure_check_status,
4789
+ def _replace_config_values(repo: Path, scope: str, key: str, values: list[str]) -> None:
4790
+ removed = run(
4791
+ ["git", "config", scope, "--unset-all", key],
4792
+ cwd=repo,
4793
+ check=False,
4794
+ )
4795
+ if removed.returncode not in {0, 5}:
4796
+ raise APError(
4797
+ f"Cannot clear {scope} Git config {key!r}: "
4798
+ f"{removed.stderr.strip() or removed.stdout.strip()}"
3942
4799
  )
3943
- print(f"[commit-push] OK - profile={plan['profile']} mode={mode} scope={plan['selected_scope']}")
4800
+ for value in values:
4801
+ run(["git", "config", scope, "--add", key, value], cwd=repo)
3944
4802
 
3945
4803
 
3946
- def main(argv: Optional[List[str]] = None) -> int:
3947
- p = argparse.ArgumentParser(prog="autopipeline")
3948
- p.add_argument("--repo", default=".")
3949
- sp = p.add_subparsers(dest="cmd", required=True)
4804
+ def _enable_worktree_config(repo: Path) -> None:
4805
+ enabled = run(
4806
+ ["git", "config", "--local", "--bool", "--get", "extensions.worktreeConfig"],
4807
+ cwd=repo,
4808
+ check=False,
4809
+ )
4810
+ if enabled.returncode == 0 and enabled.stdout.strip() == "true":
4811
+ return
4812
+ run(["git", "config", "--local", "extensions.worktreeConfig", "true"], cwd=repo)
3950
4813
 
3951
- s = sp.add_parser("install")
3952
- s.add_argument("--bridges", action="store_true")
3953
- s.add_argument("--force", action="store_true")
3954
- s.add_argument("--full", action="store_true", help="Also materialize all optional document templates")
3955
- s.set_defaults(func=cmd_install)
3956
4814
 
3957
- s = sp.add_parser("scaffold")
3958
- s.add_argument("group", choices=scaffold_groups() + ["all"])
3959
- s.add_argument("--write", action="store_true")
3960
- s.add_argument("--force", action="store_true")
3961
- s.add_argument("--json", action="store_true")
3962
- s.set_defaults(func=cmd_scaffold)
4815
+ def _require_worktree_config(repo: Path) -> None:
4816
+ enabled = run(
4817
+ ["git", "config", "--local", "--bool", "--get", "extensions.worktreeConfig"],
4818
+ cwd=repo,
4819
+ check=False,
4820
+ )
4821
+ if enabled.returncode != 0 or enabled.stdout.strip() != "true":
4822
+ raise APError(
4823
+ "Git extensions.worktreeConfig is not enabled. Return to the control checkout and "
4824
+ "recreate or recover the task; task commands will not write shared config."
4825
+ )
3963
4826
 
3964
- s = sp.add_parser("upgrade")
3965
- s.add_argument("--dry-run", action="store_true")
3966
- s.add_argument("--write", action="store_true")
3967
- s.add_argument("--json", action="store_true")
3968
- s.set_defaults(func=cmd_upgrade)
3969
4827
 
3970
- s = sp.add_parser("baseline")
3971
- baseline_sp = s.add_subparsers(dest="baseline_cmd", required=True)
3972
- b = baseline_sp.add_parser("init")
3973
- b.add_argument("--write", action="store_true")
3974
- b.add_argument("--force", action="store_true")
3975
- b.add_argument("--update-config", action="store_true")
3976
- b.add_argument("--json", action="store_true")
3977
- b.set_defaults(func=cmd_baseline_init)
4828
+ def _default_submodule_remote_url(repo: Path) -> str:
4829
+ branch = _current_branch(repo)
4830
+ remote = ""
4831
+ if branch:
4832
+ configured = run(
4833
+ ["git", "config", "--get", f"branch.{branch}.remote"],
4834
+ cwd=repo,
4835
+ check=False,
4836
+ )
4837
+ if configured.returncode == 0:
4838
+ remote = configured.stdout.strip()
4839
+ if not remote:
4840
+ names = run(["git", "remote"], cwd=repo, check=False)
4841
+ if names.returncode != 0:
4842
+ raise APError(
4843
+ f"Cannot inspect Git remotes while resolving submodule URLs: {names.stderr.strip()}"
4844
+ )
4845
+ if "origin" in {line.strip() for line in names.stdout.splitlines()}:
4846
+ remote = "origin"
4847
+ if not remote or remote == ".":
4848
+ return str(repo.resolve())
4849
+ remote_url_result = run(
4850
+ ["git", "remote", "get-url", remote],
4851
+ cwd=repo,
4852
+ check=False,
4853
+ )
4854
+ if remote_url_result.returncode != 0 or not remote_url_result.stdout.strip():
4855
+ raise APError(f"Default submodule remote {remote!r} has no URL in {repo}.")
4856
+ return remote_url_result.stdout.strip()
4857
+
4858
+
4859
+ def _resolve_submodule_url(repo: Path, raw_url: str) -> str:
4860
+ if not raw_url.startswith(("./", "../")):
4861
+ return raw_url
4862
+ remote_url = _default_submodule_remote_url(repo)
4863
+ has_explicit_scheme = bool(
4864
+ re.match(r"^[A-Za-z][A-Za-z0-9+.-]*://", remote_url)
4865
+ or remote_url.startswith("file:")
4866
+ )
4867
+ scp_like = (
4868
+ None
4869
+ if has_explicit_scheme
4870
+ else re.fullmatch(r"([^/:]+(?:@[^/:]+)?):(.*)", remote_url)
4871
+ )
4872
+ if scp_like is not None:
4873
+ prefix, remote_path = scp_like.groups()
4874
+ return f"{prefix}:{posixpath.normpath(posixpath.join(remote_path, raw_url))}"
4875
+ parsed = urllib.parse.urlsplit(remote_url)
4876
+ if parsed.scheme:
4877
+ resolved_path = posixpath.normpath(posixpath.join(parsed.path, raw_url))
4878
+ if parsed.path.startswith("/") and not resolved_path.startswith("/"):
4879
+ resolved_path = f"/{resolved_path}"
4880
+ return urllib.parse.urlunsplit(
4881
+ (parsed.scheme, parsed.netloc, resolved_path, parsed.query, parsed.fragment)
4882
+ )
4883
+ remote_path = Path(remote_url).expanduser()
4884
+ if not remote_path.is_absolute():
4885
+ remote_path = (repo / remote_path).resolve()
4886
+ return str(Path(os.path.normpath(os.path.join(str(remote_path), raw_url))))
3978
4887
 
3979
- s = sp.add_parser("gen-summary")
3980
- s.add_argument("task_id")
3981
- s.add_argument("--title")
3982
- s.add_argument("--force", action="store_true")
3983
- s.set_defaults(func=cmd_gen_summary)
3984
4888
 
3985
- s = sp.add_parser("check-matrix")
3986
- s.set_defaults(func=cmd_check_matrix)
4889
+ def _seed_control_submodule_config(repo: Path) -> None:
4890
+ _enable_worktree_config(repo)
4891
+ for key, declared_url in _gitmodules_urls(repo).items():
4892
+ if _config_values(repo, "--worktree", key):
4893
+ continue
4894
+ effective = _config_values(repo, "--local", key)
4895
+ value = effective[-1] if effective else _resolve_submodule_url(repo, declared_url)
4896
+ _replace_config_values(repo, "--worktree", key, [value])
3987
4897
 
3988
- s = sp.add_parser("run")
3989
- s.add_argument("name")
3990
- s.set_defaults(func=cmd_run)
3991
4898
 
3992
- s = sp.add_parser("impact")
3993
- s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
3994
- s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES), default="")
3995
- s.add_argument("--mode", choices=["dev", "verify"], default="")
3996
- s.add_argument("--base")
3997
- s.add_argument("--json", action="store_true")
3998
- s.set_defaults(func=cmd_impact)
4899
+ def _sync_task_submodule_config(
4900
+ control_repo: Path,
4901
+ worktree: Path,
4902
+ manifest: dict,
4903
+ *,
4904
+ initial: bool,
4905
+ check_common: bool,
4906
+ ) -> None:
4907
+ _require_worktree_config(control_repo)
4908
+ urls = _gitmodules_urls(worktree)
4909
+ control_urls = _gitmodules_urls(control_repo)
4910
+ previous_raw = manifest.get("submodule_config_claims")
4911
+ previous_claims = (
4912
+ {str(key): str(value) for key, value in previous_raw.items()}
4913
+ if isinstance(previous_raw, dict)
4914
+ else {}
4915
+ )
4916
+ claims: dict[str, str] = {}
4917
+ for key, declared_url in urls.items():
4918
+ value = _resolve_submodule_url(worktree, declared_url)
4919
+ if initial and control_urls.get(key) == declared_url:
4920
+ control_values = _config_values(control_repo, "--worktree", key)
4921
+ if control_values:
4922
+ value = control_values[-1]
4923
+ _replace_config_values(worktree, "--worktree", key, [value])
4924
+ claims[key] = value
4925
+ active_key = f"{key[:-len('.url')]}.active"
4926
+ _replace_config_values(worktree, "--worktree", active_key, ["true"])
4927
+ claims[active_key] = "true"
4928
+
4929
+ for stale_key in sorted(set(previous_claims) - set(claims)):
4930
+ _replace_config_values(worktree, "--worktree", stale_key, [])
4931
+
4932
+ manifest["submodule_config_claims"] = claims
4933
+ if not check_common:
4934
+ return
4935
+ baseline = manifest.get("common_submodule_config")
4936
+ baseline = baseline if isinstance(baseline, dict) else {}
4937
+ candidate_values: dict[str, set[str]] = {}
4938
+ for source in (previous_claims, claims):
4939
+ for key, value in source.items():
4940
+ candidate_values.setdefault(key, set()).add(value)
4941
+ for key, candidates in candidate_values.items():
4942
+ current = _config_values(control_repo, "--local", key)
4943
+ before_raw = baseline.get(key) or []
4944
+ before = [str(value) for value in before_raw] if isinstance(before_raw, list) else []
4945
+ if current != before:
4946
+ raise APError(
4947
+ "Shared submodule config changed after task-start; no common config was modified. "
4948
+ f"Resolve ownership before continuing: key={key!r}, before={before!r}, "
4949
+ f"current={current!r}, task_values={sorted(candidates)!r}"
4950
+ )
3999
4951
 
4000
- s = sp.add_parser("classify")
4001
- s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
4002
- s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES), default="")
4003
- s.add_argument("--mode", choices=["dev", "verify"], default="")
4004
- s.add_argument("--base")
4005
- s.add_argument("--json", action="store_true")
4006
- s.set_defaults(func=cmd_classify)
4007
4952
 
4008
- s = sp.add_parser("structure-check")
4009
- s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
4010
- s.add_argument("--base")
4011
- s.add_argument("--json", action="store_true")
4012
- s.set_defaults(func=cmd_structure_check)
4953
+ def _git_ref_exists(repo: Path, ref: str) -> bool:
4954
+ return run(["git", "show-ref", "--verify", "--quiet", ref], cwd=repo, check=False).returncode == 0
4013
4955
 
4014
- s = sp.add_parser("gate-profile")
4015
- s.add_argument("--limit", type=int, default=20)
4016
- s.add_argument("--json", action="store_true")
4017
- s.set_defaults(func=cmd_gate_profile)
4018
4956
 
4019
- s = sp.add_parser("docs-ledger-check")
4020
- s.add_argument("--json", action="store_true")
4021
- s.set_defaults(func=cmd_docs_ledger_check)
4957
+ def _git_status(repo: Path) -> str:
4958
+ result = run(
4959
+ ["git", "status", "--porcelain=v1", "--untracked-files=all"],
4960
+ cwd=repo,
4961
+ check=False,
4962
+ )
4963
+ if result.returncode != 0:
4964
+ raise APError(f"Cannot inspect Git status for {repo}: {result.stderr.strip()}")
4965
+ return result.stdout.strip()
4022
4966
 
4023
- s = sp.add_parser("docs-ledger-archive")
4024
- mode = s.add_mutually_exclusive_group(required=True)
4025
- mode.add_argument("--plan", action="store_true")
4026
- mode.add_argument("--write", action="store_true")
4027
- s.add_argument("--period", help="Archive period in YYYY-MM; defaults to current month")
4028
- s.add_argument("--json", action="store_true")
4029
- s.set_defaults(func=cmd_docs_ledger_archive)
4030
4967
 
4031
- s = sp.add_parser("light-gate")
4032
- s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
4033
- s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES), default="")
4034
- s.add_argument("--mode", choices=["dev", "verify"], default="")
4968
+ def _ignored_paths(repo: Path) -> list[str]:
4969
+ result = run(
4970
+ [
4971
+ "git",
4972
+ "ls-files",
4973
+ "--others",
4974
+ "--ignored",
4975
+ "--exclude-standard",
4976
+ "--directory",
4977
+ "-z",
4978
+ ],
4979
+ cwd=repo,
4980
+ check=False,
4981
+ )
4982
+ if result.returncode != 0:
4983
+ raise APError(
4984
+ f"Cannot inspect ignored files for {repo}: "
4985
+ f"{result.stderr.strip() or result.stdout.strip()}"
4986
+ )
4987
+ return sorted(
4988
+ path.rstrip("/")
4989
+ for path in result.stdout.split("\0")
4990
+ if path.rstrip("/")
4991
+ )
4992
+
4993
+
4994
+ def _pattern_may_match_descendant(directory: str, pattern_ref: str) -> bool:
4995
+ directory = directory.replace("\\", "/").lstrip("./").rstrip("/")
4996
+ pattern = _text(pattern_ref).replace("\\", "/").lstrip("./")
4997
+ if not directory or not pattern:
4998
+ return False
4999
+ if "/" not in pattern or pattern.startswith("**/"):
5000
+ return True
5001
+ wildcard = min(
5002
+ (index for token in ("*", "?", "[") if (index := pattern.find(token)) >= 0),
5003
+ default=len(pattern),
5004
+ )
5005
+ literal_prefix = pattern[:wildcard].rstrip("/")
5006
+ if not literal_prefix:
5007
+ return True
5008
+ return (
5009
+ literal_prefix == directory
5010
+ or literal_prefix.startswith(f"{directory}/")
5011
+ or directory.startswith(literal_prefix)
5012
+ )
5013
+
5014
+
5015
+ def _expanded_ignored_paths(repo: Path, directory: str) -> list[str]:
5016
+ result = run(
5017
+ [
5018
+ "git",
5019
+ "ls-files",
5020
+ "--others",
5021
+ "--ignored",
5022
+ "--exclude-standard",
5023
+ "-z",
5024
+ "--",
5025
+ directory,
5026
+ ],
5027
+ cwd=repo,
5028
+ check=False,
5029
+ )
5030
+ if result.returncode != 0:
5031
+ raise APError(
5032
+ f"Cannot inspect ignored directory {directory!r}: "
5033
+ f"{result.stderr.strip() or result.stdout.strip()}"
5034
+ )
5035
+ return sorted(path for path in result.stdout.split("\0") if path)
5036
+
5037
+
5038
+ def _unsafe_ignored_paths(repo: Path, extra_patterns: list[str]) -> list[str]:
5039
+ patterns = list(_DEFAULT_DISPOSABLE_IGNORED_PATTERNS) + [
5040
+ _text(item) for item in extra_patterns if _text(item)
5041
+ ]
5042
+ unsafe: list[str] = []
5043
+ for path in _ignored_paths(repo):
5044
+ # Git deliberately collapses ignored directories (for example `.local/`).
5045
+ # A declared disposable subtree is safe without walking it; an ambiguous
5046
+ # parent is expanded so an allowed child cannot hide an unknown sibling.
5047
+ if _path_matches(path, patterns):
5048
+ continue
5049
+ if not any(_pattern_may_match_descendant(path, pattern) for pattern in patterns):
5050
+ unsafe.append(path)
5051
+ continue
5052
+ expanded = _expanded_ignored_paths(repo, path)
5053
+ unsafe.extend(item for item in expanded if not _path_matches(item, patterns))
5054
+ if not expanded:
5055
+ unsafe.append(path)
5056
+ return sorted(set(unsafe))
5057
+
5058
+
5059
+ def _initialized_submodules(repo: Path) -> list[tuple[str, Path]]:
5060
+ result = run(
5061
+ [
5062
+ "git",
5063
+ "submodule",
5064
+ "foreach",
5065
+ "--quiet",
5066
+ "--recursive",
5067
+ 'printf "%s\\0" "$displaypath"',
5068
+ ],
5069
+ cwd=repo,
5070
+ check=False,
5071
+ )
5072
+ if result.returncode != 0:
5073
+ raise APError(
5074
+ f"Cannot inspect initialized submodules for {repo}: "
5075
+ f"{result.stderr.strip() or result.stdout.strip()}"
5076
+ )
5077
+ root = repo.resolve()
5078
+ submodules: list[tuple[str, Path]] = []
5079
+ for raw_path in result.stdout.split("\0"):
5080
+ rel = raw_path.replace("\\", "/").strip("/")
5081
+ if rel.startswith("./"):
5082
+ rel = rel[2:]
5083
+ if not rel:
5084
+ continue
5085
+ candidate = (root / rel).resolve()
5086
+ if root not in candidate.parents:
5087
+ raise APError(f"Initialized submodule resolves outside the task worktree: {rel}")
5088
+ submodules.append((rel, candidate))
5089
+ return sorted(set(submodules), key=lambda item: item[0])
5090
+
5091
+
5092
+ def _stored_submodule_gitdirs(worktree: Path) -> tuple[set[Path], list[Path]]:
5093
+ modules_root = _git_dir(worktree) / "modules"
5094
+ if not modules_root.exists():
5095
+ return set(), []
5096
+ repositories: set[Path] = set()
5097
+ unknown: list[Path] = []
5098
+
5099
+ def scan_container(container: Path) -> None:
5100
+ try:
5101
+ children = sorted(container.iterdir(), key=lambda item: item.name)
5102
+ except OSError:
5103
+ unknown.append(container)
5104
+ return
5105
+ for child in children:
5106
+ if child.is_symlink():
5107
+ unknown.append(child)
5108
+ continue
5109
+ if not child.is_dir():
5110
+ unknown.append(child)
5111
+ continue
5112
+ is_git_dir = (
5113
+ (child / "HEAD").is_file()
5114
+ and (child / "config").is_file()
5115
+ and (child / "objects").is_dir()
5116
+ )
5117
+ if is_git_dir:
5118
+ repositories.add(child.resolve())
5119
+ nested_modules = child / "modules"
5120
+ if nested_modules.exists():
5121
+ scan_container(nested_modules)
5122
+ continue
5123
+ scan_container(child)
5124
+
5125
+ scan_container(modules_root)
5126
+ return repositories, unknown
5127
+
5128
+
5129
+ def _submodule_disposable_patterns(submodule_path: str, extra_patterns: list[str]) -> list[str]:
5130
+ prefix = submodule_path.replace("\\", "/").strip("/")
5131
+ patterns = [_text(item) for item in extra_patterns if _text(item)]
5132
+ for item in list(patterns):
5133
+ normalized = item.replace("\\", "/").lstrip("./").strip("/")
5134
+ if normalized == prefix:
5135
+ patterns.append("**")
5136
+ elif normalized.startswith(f"{prefix}/"):
5137
+ patterns.append(normalized[len(prefix) + 1 :])
5138
+ return patterns
5139
+
5140
+
5141
+ def _submodule_local_only_commits(submodule: Path) -> list[str]:
5142
+ result = run(
5143
+ [
5144
+ "git",
5145
+ "rev-list",
5146
+ "--max-count=20",
5147
+ "--all",
5148
+ "--reflog",
5149
+ "--not",
5150
+ "--remotes",
5151
+ ],
5152
+ cwd=submodule,
5153
+ check=False,
5154
+ )
5155
+ if result.returncode != 0:
5156
+ raise APError(
5157
+ f"Cannot inspect local-only submodule history for {submodule}: "
5158
+ f"{result.stderr.strip() or result.stdout.strip()}"
5159
+ )
5160
+ return [line.strip() for line in result.stdout.splitlines() if line.strip()]
5161
+
5162
+
5163
+ def _submodule_unmirrored_branches(submodule: Path) -> list[str]:
5164
+ result = run(
5165
+ [
5166
+ "git",
5167
+ "for-each-ref",
5168
+ "--format=%(refname)%09%(objectname)",
5169
+ "refs/heads",
5170
+ "refs/remotes",
5171
+ ],
5172
+ cwd=submodule,
5173
+ check=False,
5174
+ )
5175
+ if result.returncode != 0:
5176
+ raise APError(
5177
+ f"Cannot inspect submodule branches for {submodule}: "
5178
+ f"{result.stderr.strip() or result.stdout.strip()}"
5179
+ )
5180
+ local: dict[str, str] = {}
5181
+ remote: dict[str, set[str]] = {}
5182
+ for line in result.stdout.splitlines():
5183
+ if "\t" not in line:
5184
+ continue
5185
+ ref, commit = line.split("\t", 1)
5186
+ if ref.startswith("refs/heads/"):
5187
+ local[ref[len("refs/heads/") :]] = commit
5188
+ continue
5189
+ if not ref.startswith("refs/remotes/"):
5190
+ continue
5191
+ remote_and_branch = ref[len("refs/remotes/") :]
5192
+ if "/" not in remote_and_branch:
5193
+ continue
5194
+ _, branch = remote_and_branch.split("/", 1)
5195
+ remote.setdefault(branch, set()).add(commit)
5196
+ return sorted(
5197
+ branch
5198
+ for branch, commit in local.items()
5199
+ if commit not in remote.get(branch, set())
5200
+ )
5201
+
5202
+
5203
+ def _submodule_other_worktrees(submodule: Path) -> list[str]:
5204
+ result = run(
5205
+ ["git", "worktree", "list", "--porcelain", "-z"],
5206
+ cwd=submodule,
5207
+ check=False,
5208
+ )
5209
+ if result.returncode != 0:
5210
+ raise APError(
5211
+ f"Cannot inspect linked submodule worktrees for {submodule}: "
5212
+ f"{result.stderr.strip() or result.stdout.strip()}"
5213
+ )
5214
+ current_paths = {submodule.resolve(), _git_dir(submodule).resolve()}
5215
+ others: list[str] = []
5216
+ for field in result.stdout.split("\0"):
5217
+ if not field.startswith("worktree "):
5218
+ continue
5219
+ raw_path = field[len("worktree ") :]
5220
+ candidate = Path(raw_path).resolve()
5221
+ if candidate not in current_paths:
5222
+ others.append(raw_path)
5223
+ return sorted(set(others))
5224
+
5225
+
5226
+ def _refresh_submodule_remotes(submodule: Path) -> None:
5227
+ remotes = run(["git", "remote"], cwd=submodule, check=False)
5228
+ if remotes.returncode != 0:
5229
+ raise APError(
5230
+ f"Cannot inspect submodule remotes for {submodule}: "
5231
+ f"{remotes.stderr.strip() or remotes.stdout.strip()}"
5232
+ )
5233
+ names = [line.strip() for line in remotes.stdout.splitlines() if line.strip()]
5234
+ if not names:
5235
+ raise APError(
5236
+ f"Initialized submodule has no remote to verify before destructive cleanup: {submodule}"
5237
+ )
5238
+ for remote in names:
5239
+ fetched = run(
5240
+ [
5241
+ "git",
5242
+ "-c",
5243
+ "credential.interactive=never",
5244
+ "fetch",
5245
+ "--prune",
5246
+ "--no-tags",
5247
+ remote,
5248
+ ],
5249
+ cwd=submodule,
5250
+ check=False,
5251
+ )
5252
+ if fetched.returncode != 0:
5253
+ raise APError(
5254
+ f"Cannot refresh submodule remote {remote!r}; cleanup was blocked so Git history remains local.\n"
5255
+ f"{fetched.stdout}\n{fetched.stderr}"
5256
+ )
5257
+
5258
+
5259
+ def _submodule_refs(submodule: Path) -> dict[str, str]:
5260
+ result = run(
5261
+ ["git", "for-each-ref", "--format=%(refname)%09%(objectname)", "refs"],
5262
+ cwd=submodule,
5263
+ check=False,
5264
+ )
5265
+ if result.returncode != 0:
5266
+ raise APError(
5267
+ f"Cannot snapshot submodule refs for {submodule}: "
5268
+ f"{result.stderr.strip() or result.stdout.strip()}"
5269
+ )
5270
+ refs: dict[str, str] = {}
5271
+ for line in result.stdout.splitlines():
5272
+ if "\t" not in line:
5273
+ continue
5274
+ ref, object_id = line.split("\t", 1)
5275
+ if ref.startswith("refs/") and object_id:
5276
+ refs[ref] = object_id
5277
+ return refs
5278
+
5279
+
5280
+ def _submodule_reflog_commits(submodule: Path) -> list[str]:
5281
+ result = run(
5282
+ ["git", "reflog", "show", "--all", "--format=%H"],
5283
+ cwd=submodule,
5284
+ check=False,
5285
+ )
5286
+ if result.returncode != 0:
5287
+ raise APError(
5288
+ f"Cannot snapshot submodule reflogs for {submodule}: "
5289
+ f"{result.stderr.strip() or result.stdout.strip()}"
5290
+ )
5291
+ commits = {line.strip() for line in result.stdout.splitlines() if line.strip()}
5292
+ commits.add(_resolve_commit(submodule, "HEAD"))
5293
+ return sorted(commits)
5294
+
5295
+
5296
+ def _archive_submodule_git_state(
5297
+ control_repo: Path,
5298
+ manifest: dict,
5299
+ submodule_path: str,
5300
+ submodule: Path,
5301
+ ) -> dict:
5302
+ task_uuid = _text(manifest.get("task_uuid")) or uuid.uuid4().hex
5303
+ object_format_result = run(
5304
+ ["git", "rev-parse", "--show-object-format"],
5305
+ cwd=submodule,
5306
+ check=False,
5307
+ )
5308
+ if object_format_result.returncode != 0:
5309
+ raise APError(
5310
+ f"Cannot inspect submodule object format for {submodule_path}: "
5311
+ f"{object_format_result.stderr.strip() or object_format_result.stdout.strip()}"
5312
+ )
5313
+ object_format = object_format_result.stdout.strip() or "sha1"
5314
+ recovery_root = _task_state_root(control_repo) / "submodule-recovery"
5315
+ repo_key = hashlib.sha256(submodule_path.encode("utf-8")).hexdigest()[:20]
5316
+ recovery_repo = recovery_root / "repos" / f"{repo_key}-{object_format}.git"
5317
+ if not recovery_repo.exists():
5318
+ recovery_repo.parent.mkdir(parents=True, exist_ok=True)
5319
+ init_command = ["git", "init", "--bare", "--quiet"]
5320
+ if object_format != "sha1":
5321
+ init_command.append(f"--object-format={object_format}")
5322
+ init_command.append(str(recovery_repo))
5323
+ run(init_command, cwd=control_repo)
5324
+ bare = run(
5325
+ ["git", f"--git-dir={recovery_repo}", "rev-parse", "--is-bare-repository"],
5326
+ cwd=control_repo,
5327
+ check=False,
5328
+ )
5329
+ recovery_format = run(
5330
+ ["git", f"--git-dir={recovery_repo}", "rev-parse", "--show-object-format"],
5331
+ cwd=control_repo,
5332
+ check=False,
5333
+ )
5334
+ if (
5335
+ bare.returncode != 0
5336
+ or bare.stdout.strip() != "true"
5337
+ or recovery_format.returncode != 0
5338
+ or recovery_format.stdout.strip() != object_format
5339
+ ):
5340
+ raise APError(f"Invalid submodule recovery repository: {recovery_repo}")
5341
+
5342
+ original_refs = _submodule_refs(submodule)
5343
+ reflog_commits = _submodule_reflog_commits(submodule)
5344
+ snapshot_id = uuid.uuid4().hex
5345
+ temporary_prefix = f"refs/auto-coding-recovery/{snapshot_id}/reflog"
5346
+ temporary_refs: dict[str, str] = {}
5347
+ try:
5348
+ for commit in reflog_commits:
5349
+ temporary_ref = f"{temporary_prefix}/{commit}"
5350
+ run(
5351
+ ["git", "update-ref", temporary_ref, commit, "0" * len(commit)],
5352
+ cwd=submodule,
5353
+ )
5354
+ temporary_refs[temporary_ref] = commit
5355
+ source_refs = _submodule_refs(submodule)
5356
+ destination_prefix = f"refs/tasks/{task_uuid}/{snapshot_id}"
5357
+ run(
5358
+ [
5359
+ "git",
5360
+ f"--git-dir={recovery_repo}",
5361
+ "fetch",
5362
+ "--no-tags",
5363
+ "--no-write-fetch-head",
5364
+ str(submodule),
5365
+ f"+refs/*:{destination_prefix}/*",
5366
+ ],
5367
+ cwd=control_repo,
5368
+ )
5369
+ archived_refs = run(
5370
+ [
5371
+ "git",
5372
+ f"--git-dir={recovery_repo}",
5373
+ "for-each-ref",
5374
+ "--format=%(refname)%09%(objectname)",
5375
+ destination_prefix,
5376
+ ],
5377
+ cwd=control_repo,
5378
+ )
5379
+ archived: dict[str, str] = {}
5380
+ for line in archived_refs.stdout.splitlines():
5381
+ if "\t" in line:
5382
+ ref, object_id = line.split("\t", 1)
5383
+ archived[ref] = object_id
5384
+ missing: list[str] = []
5385
+ for source_ref, object_id in source_refs.items():
5386
+ destination = f"{destination_prefix}/{source_ref[len('refs/'):]}"
5387
+ if archived.get(destination) != object_id:
5388
+ missing.append(source_ref)
5389
+ if missing:
5390
+ raise APError(
5391
+ f"Submodule recovery snapshot verification failed for {submodule_path}:\n- "
5392
+ + "\n- ".join(missing)
5393
+ )
5394
+ metadata_path = (
5395
+ recovery_root
5396
+ / "snapshots"
5397
+ / task_uuid
5398
+ / f"{repo_key}-{snapshot_id}.json"
5399
+ )
5400
+ _write_json_object(
5401
+ metadata_path,
5402
+ {
5403
+ "schema": 1,
5404
+ "created_at": _now_iso(),
5405
+ "task_id": _text(manifest.get("task_id")),
5406
+ "task_uuid": task_uuid,
5407
+ "submodule_path": submodule_path,
5408
+ "source_git_dir": str(_git_dir(submodule)),
5409
+ "recovery_repo": str(recovery_repo),
5410
+ "destination_prefix": destination_prefix,
5411
+ "refs": original_refs,
5412
+ "reflog_commits": reflog_commits,
5413
+ },
5414
+ )
5415
+ finally:
5416
+ for temporary_ref, commit in temporary_refs.items():
5417
+ run(
5418
+ ["git", "update-ref", "-d", temporary_ref, commit],
5419
+ cwd=submodule,
5420
+ check=False,
5421
+ )
5422
+
5423
+ print(
5424
+ f"[task-cleanup] submodule recovery snapshot: {submodule_path} -> "
5425
+ f"{recovery_repo} ({destination_prefix})"
5426
+ )
5427
+ return {
5428
+ "submodule_path": submodule_path,
5429
+ "recovery_repo": str(recovery_repo),
5430
+ "destination_prefix": destination_prefix,
5431
+ "metadata_path": str(metadata_path),
5432
+ }
5433
+
5434
+
5435
+ def _prepare_submodules_for_removal(
5436
+ control_repo: Path,
5437
+ manifest: dict,
5438
+ worktree: Path,
5439
+ extra_disposable_patterns: list[str],
5440
+ ) -> bool:
5441
+ submodules = _initialized_submodules(worktree)
5442
+ stored_gitdirs, unknown_store_paths = _stored_submodule_gitdirs(worktree)
5443
+ initialized_gitdirs = {_git_dir(submodule).resolve() for _, submodule in submodules}
5444
+ residual_gitdirs = sorted(stored_gitdirs - initialized_gitdirs)
5445
+ if residual_gitdirs or unknown_store_paths:
5446
+ details = [
5447
+ *(f"residual module Git directory: {path}" for path in residual_gitdirs),
5448
+ *(f"unrecognized module-store path: {path}" for path in unknown_store_paths[:20]),
5449
+ ]
5450
+ raise APError(
5451
+ "Task worktree has deinitialized, removed, or unrecognized submodule Git state that "
5452
+ "cannot be proven safe for forced removal. Reinitialize the submodule and rerun cleanup, "
5453
+ "or move/recover its Git data first; the task worktree and branch were retained:\n- "
5454
+ + "\n- ".join(details)
5455
+ )
5456
+ if not submodules:
5457
+ return False
5458
+ problems: list[str] = []
5459
+ for rel, submodule in submodules:
5460
+ other_worktrees = _submodule_other_worktrees(submodule)
5461
+ if other_worktrees:
5462
+ problems.append(
5463
+ f"{rel}: additional linked worktrees must be handled before cleanup:\n"
5464
+ + "\n".join(f" - {path}" for path in other_worktrees)
5465
+ )
5466
+ continue
5467
+ try:
5468
+ _refresh_submodule_remotes(submodule)
5469
+ except APError as exc:
5470
+ problems.append(f"{rel}: {exc}")
5471
+ continue
5472
+ local_only_commits = _submodule_local_only_commits(submodule)
5473
+ if local_only_commits:
5474
+ problems.append(
5475
+ f"{rel}: local refs/reflogs contain commits not reachable from remote-tracking refs:\n"
5476
+ + "\n".join(f" - {commit}" for commit in local_only_commits)
5477
+ )
5478
+ continue
5479
+ unmirrored_branches = _submodule_unmirrored_branches(submodule)
5480
+ if unmirrored_branches:
5481
+ problems.append(
5482
+ f"{rel}: local branches have no same-name remote-tracking ref at the same commit:\n"
5483
+ + "\n".join(f" - {branch}" for branch in unmirrored_branches)
5484
+ )
5485
+ continue
5486
+ dirty = _git_status(submodule)
5487
+ if dirty:
5488
+ problems.append(f"{rel}: tracked or untracked changes:\n{dirty}")
5489
+ continue
5490
+ conflicts = run(["git", "ls-files", "-u"], cwd=submodule, check=False)
5491
+ if conflicts.returncode != 0:
5492
+ raise APError(
5493
+ f"Cannot inspect submodule conflicts for {rel}: "
5494
+ f"{conflicts.stderr.strip() or conflicts.stdout.strip()}"
5495
+ )
5496
+ if conflicts.stdout.strip():
5497
+ problems.append(f"{rel}: unresolved conflicts")
5498
+ continue
5499
+ unsafe_ignored = _unsafe_ignored_paths(
5500
+ submodule,
5501
+ _submodule_disposable_patterns(rel, extra_disposable_patterns),
5502
+ )
5503
+ if unsafe_ignored:
5504
+ rendered = "\n".join(f" - {rel}/{path}" for path in unsafe_ignored)
5505
+ problems.append(f"{rel}: ignored files are not declared disposable:\n{rendered}")
5506
+ if problems:
5507
+ raise APError(
5508
+ "Task submodules contain data that cannot be discarded; refusing cleanup:\n- "
5509
+ + "\n- ".join(problems)
5510
+ + "\nPush or move local submodule history, commit/move working data, or add only "
5511
+ "disposable cache paths to concurrency.disposable_ignored; then rerun task-finish."
5512
+ )
5513
+
5514
+ for rel, submodule in submodules:
5515
+ _archive_submodule_git_state(control_repo, manifest, rel, submodule)
5516
+ return True
5517
+
5518
+
5519
+ def _task_doc_path(repo: Path, cfg: dict, manifest: dict, kind: str) -> Path:
5520
+ docs_cfg = cfg.get("docs") or {}
5521
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
5522
+ if kind == "active":
5523
+ rel = _text(docs_cfg.get("active_task_dir")) or "docs/tasks/active"
5524
+ suffix = ".md"
5525
+ elif kind == "closure":
5526
+ rel = _text(docs_cfg.get("task_closure_dir")) or "docs/tasks/closures"
5527
+ suffix = ".md"
5528
+ elif kind == "evidence":
5529
+ rel = _text(docs_cfg.get("task_evidence_dir")) or "docs/tasks/evidence"
5530
+ suffix = ".jsonl"
5531
+ else:
5532
+ raise APError(f"Unknown task document kind: {kind}")
5533
+ return repo / rel / f"{task_id}{suffix}"
5534
+
5535
+
5536
+ def _create_active_task_doc(repo: Path, cfg: dict, manifest: dict) -> None:
5537
+ path = _task_doc_path(repo, cfg, manifest, "active")
5538
+ if path.exists():
5539
+ return
5540
+ path.parent.mkdir(parents=True, exist_ok=True)
5541
+ path.write_text(
5542
+ "\n".join(
5543
+ [
5544
+ f"# Task {manifest['task_id']}",
5545
+ "",
5546
+ "- Status: Active",
5547
+ f"- Base: `{manifest['base_ref']}` (`{manifest['base_sha'][:12]}`)",
5548
+ f"- Target: `{manifest['remote']}/{manifest['target_branch']}`",
5549
+ f"- Branch: `{manifest['task_branch']}`",
5550
+ f"- Worktree: `{manifest['worktree_path']}`",
5551
+ f"- Orchestrator: {manifest.get('owner') or 'TODO'}",
5552
+ f"- Owning fixer: {(manifest.get('writer_lease') or {}).get('holder') or 'TODO'}",
5553
+ "- Owned paths: " + ", ".join(f"`{item}`" for item in manifest.get("owned_paths") or []),
5554
+ "- Depends on integrated tasks: "
5555
+ + (
5556
+ ", ".join(
5557
+ f"`{item}`@`{(manifest.get('prerequisite_shas') or {}).get(item, '')[:12]}`"
5558
+ for item in manifest.get("depends_on") or []
5559
+ )
5560
+ or "none"
5561
+ ),
5562
+ "- Reviewer / stable diff: TODO",
5563
+ "- Review verdict: pending",
5564
+ "- Scope: TODO",
5565
+ "- Development acceptance: TODO",
5566
+ "",
5567
+ ]
5568
+ ),
5569
+ encoding="utf-8",
5570
+ )
5571
+
5572
+
5573
+ def _update_active_task_status(repo: Path, cfg: dict, manifest: dict, status: str) -> None:
5574
+ path = _task_doc_path(repo, cfg, manifest, "active")
5575
+ if not path.exists():
5576
+ return
5577
+ text = path.read_text(encoding="utf-8")
5578
+ updated, count = re.subn(r"(?m)^- Status: .*?$", f"- Status: {status}", text, count=1)
5579
+ if count == 0:
5580
+ updated = text.rstrip() + f"\n- Status: {status}\n"
5581
+ if updated != text:
5582
+ path.write_text(updated, encoding="utf-8")
5583
+
5584
+
5585
+ def _rollback_fresh_task_worktree(
5586
+ repo: Path,
5587
+ worktree: Path,
5588
+ task_branch: str,
5589
+ expected_tip: str,
5590
+ ) -> bool:
5591
+ run(["git", "worktree", "unlock", str(worktree)], cwd=repo, check=False)
5592
+ removed = run(
5593
+ ["git", "worktree", "remove", "--force", str(worktree)],
5594
+ cwd=repo,
5595
+ check=False,
5596
+ )
5597
+ if removed.returncode == 0:
5598
+ run(
5599
+ ["git", "update-ref", "-d", f"refs/heads/{task_branch}", expected_tip],
5600
+ cwd=repo,
5601
+ check=False,
5602
+ )
5603
+ return removed.returncode == 0
5604
+
5605
+
5606
+ def cmd_task_start(args: argparse.Namespace) -> None:
5607
+ repo = Path(args.repo).resolve()
5608
+ ensure_git_repo(repo)
5609
+ cfg = _load_cfg(repo)
5610
+ _task_isolation(cfg)
5611
+ access_issues = _access_config_issues(cfg)
5612
+ if access_issues:
5613
+ raise APError(
5614
+ "Project initialization is incomplete; fill docs/ENGINEERING.md before starting work:\n- "
5615
+ + "\n- ".join(access_issues)
5616
+ )
5617
+ task_id = _validate_task_id(args.task_id)
5618
+ _require_control_checkout(repo)
5619
+ remote, target_branch, base_ref = _task_remote_and_target(cfg, args)
5620
+ concurrency_cfg = _concurrency_cfg(cfg)
5621
+ timeout_s = float(concurrency_cfg.get("lock_timeout_sec") or 30)
5622
+
5623
+ with (
5624
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
5625
+ _repo_lock(repo, "task-registry", timeout_s=timeout_s),
5626
+ ):
5627
+ if _read_json_object(_task_registry_path(repo, task_id)):
5628
+ raise APError(f"Task is already registered: {task_id}")
5629
+
5630
+ common_submodule_config = _common_submodule_config(repo)
5631
+ _seed_control_submodule_config(repo)
5632
+ task_uuid = uuid.uuid4().hex
5633
+
5634
+ if not getattr(args, "no_fetch", False) and base_ref.startswith(f"{remote}/"):
5635
+ _fetch_target(repo, remote, target_branch)
5636
+ base_sha = _resolve_commit(repo, base_ref)
5637
+ owned_paths = sorted({_normalize_owned_path(item) for item in (args.owned_path or [])})
5638
+ if not owned_paths:
5639
+ raise APError("task-start requires at least one --owned-path.")
5640
+ depends_on: list[str] = []
5641
+ prerequisite_shas: dict[str, str] = {}
5642
+ for raw_dependency in args.depends_on or []:
5643
+ dependency, separator, sha = _text(raw_dependency).partition("=")
5644
+ dependency = _validate_task_id(dependency)
5645
+ if not separator or not re.fullmatch(r"(?:[0-9a-f]{40}|[0-9a-f]{64})", sha):
5646
+ raise APError("--depends-on must use TASK_ID=full_commit_SHA.")
5647
+ if dependency in prerequisite_shas and prerequisite_shas[dependency] != sha:
5648
+ raise APError(f"Conflicting prerequisite SHAs for {dependency}.")
5649
+ depends_on.append(dependency)
5650
+ prerequisite_shas[dependency] = sha
5651
+ depends_on = sorted(set(depends_on))
5652
+ dependency_contract = {
5653
+ "depends_on": depends_on,
5654
+ "prerequisite_shas": prerequisite_shas,
5655
+ }
5656
+ _require_dependencies(repo, dependency_contract, base_sha)
5657
+ owner = _text(getattr(args, "owner", "")) or _text(os.environ.get("CODEX_THREAD_ID"))
5658
+ if not owner:
5659
+ raise APError("task-start requires --owner or CODEX_THREAD_ID.")
5660
+ writer = _text(getattr(args, "writer", "")) or owner
5661
+ prefix = _task_branch_prefix(cfg)
5662
+ task_branch = f"{prefix}{task_id}"
5663
+ check_ref = run(["git", "check-ref-format", "--branch", task_branch], cwd=repo, check=False)
5664
+ if check_ref.returncode != 0:
5665
+ raise APError(f"Invalid task branch name: {task_branch}")
5666
+ if _git_ref_exists(repo, f"refs/heads/{task_branch}"):
5667
+ raise APError(f"Task branch already exists: {task_branch}. Run task-prune or choose another ID.")
5668
+ worktree = _task_worktree_path(repo, cfg, task_id, _text(getattr(args, "worktree_root", "")))
5669
+ if worktree.exists():
5670
+ raise APError(f"Task worktree path already exists: {worktree}")
5671
+ worktree.parent.mkdir(parents=True, exist_ok=True)
5672
+ run(
5673
+ [
5674
+ "git",
5675
+ "worktree",
5676
+ "add",
5677
+ "--no-track",
5678
+ "-b",
5679
+ task_branch,
5680
+ str(worktree),
5681
+ base_sha,
5682
+ ],
5683
+ cwd=repo,
5684
+ )
5685
+ run(
5686
+ ["git", "worktree", "lock", "--reason", f"auto-coding task {task_id}", str(worktree)],
5687
+ cwd=repo,
5688
+ )
5689
+ manifest = {
5690
+ "schema": 2,
5691
+ "task_id": task_id,
5692
+ "task_uuid": task_uuid,
5693
+ "owner": owner,
5694
+ "base_ref": base_ref,
5695
+ "base_sha": base_sha,
5696
+ "remote": remote,
5697
+ "target_branch": target_branch,
5698
+ "task_branch": task_branch,
5699
+ "worktree_path": str(worktree.resolve()),
5700
+ "control_worktree_path": str(repo.resolve()),
5701
+ "cleanup_policy": _cleanup_policy(cfg),
5702
+ "state": "active",
5703
+ "created_at": _now_iso(),
5704
+ "initial_untracked": [],
5705
+ "owned_paths": owned_paths,
5706
+ "depends_on": depends_on,
5707
+ "prerequisite_shas": prerequisite_shas,
5708
+ "writer_lease": {
5709
+ "holder": writer,
5710
+ "generation": 1,
5711
+ "state": "active",
5712
+ "acquired_at": _now_iso(),
5713
+ },
5714
+ "review": {
5715
+ "verdict": "pending",
5716
+ "diff_base": base_sha,
5717
+ "diff_head": "",
5718
+ "diff_fingerprint": "",
5719
+ "reviewer": "",
5720
+ "reviewed_at": "",
5721
+ "reason": "task started",
5722
+ },
5723
+ "claimed_paths": [],
5724
+ "remote_task_tip": "",
5725
+ "common_submodule_config": common_submodule_config,
5726
+ }
5727
+ try:
5728
+ _sync_task_submodule_config(
5729
+ repo,
5730
+ worktree,
5731
+ manifest,
5732
+ initial=True,
5733
+ check_common=True,
5734
+ )
5735
+ except APError as exc:
5736
+ rolled_back = _rollback_fresh_task_worktree(
5737
+ repo,
5738
+ worktree,
5739
+ task_branch,
5740
+ base_sha,
5741
+ )
5742
+ raise APError(
5743
+ f"Could not isolate task-local submodule config; fresh worktree rollback="
5744
+ f"{str(rolled_back).lower()}.\n{exc}"
5745
+ ) from exc
5746
+ initial_status = _git_status(worktree)
5747
+ initial_head = _resolve_commit(worktree, "HEAD")
5748
+ initial_branch = _current_branch(worktree)
5749
+ if initial_status or initial_head != base_sha or initial_branch != task_branch:
5750
+ rolled_back = _rollback_fresh_task_worktree(
5751
+ repo,
5752
+ worktree,
5753
+ task_branch,
5754
+ initial_head,
5755
+ )
5756
+ raise APError(
5757
+ "A checkout hook changed the new task worktree. The fresh worktree and branch were rolled "
5758
+ f"back={str(rolled_back).lower()} so hook-created content cannot be committed accidentally. "
5759
+ f"branch={initial_branch or '(detached)'} head={initial_head}\n{initial_status}"
5760
+ )
5761
+ _save_task_manifest(repo, manifest)
5762
+ _create_active_task_doc(worktree, cfg, manifest)
5763
+
5764
+ print(f"[task-start] task={task_id}")
5765
+ print(f"[task-start] branch={task_branch}")
5766
+ print(f"[task-start] base={base_ref}@{base_sha}")
5767
+ print(f"[task-start] worktree={worktree}")
5768
+ print(f"[task-start] next: cd {worktree}")
5769
+
5770
+
5771
+ def _task_status_payload(repo: Path, manifest: dict) -> dict:
5772
+ worktree = Path(_text(manifest.get("worktree_path")))
5773
+ task_branch = _text(manifest.get("task_branch"))
5774
+ local_ref = f"refs/heads/{task_branch}"
5775
+ tip = _resolve_commit(repo, local_ref) if _git_ref_exists(repo, local_ref) else ""
5776
+ remote = _text(manifest.get("remote")) or "origin"
5777
+ target = _text(manifest.get("target_branch"))
5778
+ remote_ref = f"refs/remotes/{remote}/{target}"
5779
+ merged = bool(
5780
+ tip
5781
+ and _git_ref_exists(repo, remote_ref)
5782
+ and run(["git", "merge-base", "--is-ancestor", tip, remote_ref], cwd=repo, check=False).returncode == 0
5783
+ )
5784
+ return {
5785
+ **manifest,
5786
+ "worktree_exists": worktree.exists(),
5787
+ "dirty": bool(_git_status(worktree)) if worktree.exists() else False,
5788
+ "local_branch_exists": bool(tip),
5789
+ "tip": tip,
5790
+ "merged_into_target": merged,
5791
+ "current_diff_fingerprint": _task_review_fingerprint(worktree, manifest)
5792
+ if worktree.exists()
5793
+ else "",
5794
+ }
5795
+
5796
+
5797
+ def cmd_task_status(args: argparse.Namespace) -> None:
5798
+ repo = Path(args.repo).resolve()
5799
+ ensure_git_repo(repo)
5800
+ task_id = _text(getattr(args, "task_id", ""))
5801
+ manifests: list[dict] = []
5802
+ if task_id:
5803
+ manifests.append(_load_task_manifest(repo, _validate_task_id(task_id)))
5804
+ else:
5805
+ registry = _task_state_root(repo) / "tasks"
5806
+ for path in sorted(registry.glob("*.json")) if registry.exists() else []:
5807
+ payload = _read_json_object(path)
5808
+ if payload:
5809
+ manifests.append(_validate_task_manifest(repo, payload))
5810
+ statuses = [_task_status_payload(repo, manifest) for manifest in manifests]
5811
+ if args.json:
5812
+ print(json.dumps({"tasks": statuses}, ensure_ascii=False, indent=2))
5813
+ return
5814
+ if not statuses:
5815
+ print("[task-status] no registered tasks")
5816
+ return
5817
+ for status in statuses:
5818
+ print(
5819
+ f"[task-status] task={status['task_id']} state={status.get('state')} "
5820
+ f"branch={status.get('task_branch')} worktree={status.get('worktree_path')} "
5821
+ f"dirty={str(status['dirty']).lower()} merged={str(status['merged_into_target']).lower()}"
5822
+ )
5823
+
5824
+
5825
+ def _task_lifecycle_context(repo: Path, cfg: dict, task_id: str) -> tuple[Path, Path, dict]:
5826
+ active = _active_task_manifest(repo)
5827
+ if active:
5828
+ manifest = _require_task_context(repo, cfg, task_id)
5829
+ return Path(_text(manifest.get("control_worktree_path"))).resolve(), repo, manifest
5830
+ manifest = _load_task_manifest(repo, task_id)
5831
+ _require_control_checkout(repo, manifest)
5832
+ return repo, Path(_text(manifest.get("worktree_path"))).resolve(), manifest
5833
+
5834
+
5835
+ def cmd_task_review(args: argparse.Namespace) -> None:
5836
+ repo = Path(args.repo).resolve()
5837
+ ensure_git_repo(repo)
5838
+ cfg = _load_cfg(repo)
5839
+ task_id = _validate_task_id(args.task_id)
5840
+ control_repo, worktree, _ = _task_lifecycle_context(repo, cfg, task_id)
5841
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5842
+ with _repo_lock(control_repo, f"task-{task_id}", timeout_s=timeout_s):
5843
+ manifest = _load_task_manifest(control_repo, task_id)
5844
+ lifecycle_actor = _text(os.environ.get("CODEX_THREAD_ID"))
5845
+ if not lifecycle_actor or lifecycle_actor != _text(manifest.get("owner")):
5846
+ raise APError("Only the task lifecycle owner may record a review verdict.")
5847
+ if _text(manifest.get("state")) not in {"active", "pushed", "integration-raced"}:
5848
+ raise APError(f"Task {task_id} is not reviewable in state={manifest.get('state')}.")
5849
+ if not worktree.exists():
5850
+ raise APError(f"Task worktree is missing: {worktree}")
5851
+ worktree_cfg = _load_cfg(worktree)
5852
+ unowned = _task_unowned_paths(worktree, worktree_cfg, manifest)
5853
+ if unowned:
5854
+ raise APError("Changes outside task owned_paths:\n- " + "\n- ".join(unowned))
5855
+ fingerprint = _task_review_fingerprint(worktree, manifest)
5856
+ supplied = _text(args.diff_fingerprint)
5857
+ if supplied != fingerprint:
5858
+ raise APError(
5859
+ f"Review fingerprint mismatch: supplied={supplied or '(missing)'}, current={fingerprint}."
5860
+ )
5861
+ reviewer = _text(getattr(args, "reviewer", "")) or _text(os.environ.get("CODEX_THREAD_ID"))
5862
+ if not reviewer:
5863
+ raise APError("task-review requires --reviewer or CODEX_THREAD_ID.")
5864
+ manifest["review"] = {
5865
+ "verdict": args.verdict,
5866
+ "diff_base": _text(manifest.get("base_sha")),
5867
+ "diff_head": _resolve_commit(worktree, "HEAD"),
5868
+ "diff_fingerprint": fingerprint,
5869
+ "reviewer": reviewer,
5870
+ "reviewed_at": _now_iso(),
5871
+ "reason": "",
5872
+ }
5873
+ _save_task_manifest(control_repo, manifest)
5874
+ print(f"[task-review] task={task_id} verdict={args.verdict} fingerprint={fingerprint}")
5875
+
5876
+
5877
+ def cmd_task_handoff(args: argparse.Namespace) -> None:
5878
+ repo = Path(args.repo).resolve()
5879
+ ensure_git_repo(repo)
5880
+ cfg = _load_cfg(repo)
5881
+ task_id = _validate_task_id(args.task_id)
5882
+ control_repo, _, _ = _task_lifecycle_context(repo, cfg, task_id)
5883
+ actor = _text(os.environ.get("CODEX_THREAD_ID"))
5884
+ if not actor:
5885
+ raise APError("task-handoff requires CODEX_THREAD_ID for the current writer.")
5886
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5887
+ with _repo_lock(control_repo, f"task-{task_id}", timeout_s=timeout_s):
5888
+ manifest = _load_task_manifest(control_repo, task_id)
5889
+ if _text(manifest.get("state")) in {"integrated", "cleanup-pending"}:
5890
+ raise APError(f"Task {task_id} no longer accepts writer handoff.")
5891
+ lease = manifest.get("writer_lease") or {}
5892
+ generation = int(lease.get("generation") or 0)
5893
+ if _text(lease.get("holder")) != args.from_writer:
5894
+ raise APError("--from must match the current writer lease holder.")
5895
+ if actor not in {args.from_writer, _text(manifest.get("owner"))}:
5896
+ raise APError("Only the current writer or task lifecycle owner may hand off the task.")
5897
+ if args.generation is not None and args.generation != generation:
5898
+ raise APError(f"Writer lease generation changed: expected={args.generation}, current={generation}.")
5899
+ lease.update(
5900
+ {
5901
+ "holder": args.to_writer,
5902
+ "generation": generation + 1,
5903
+ "state": "active",
5904
+ "acquired_at": _now_iso(),
5905
+ }
5906
+ )
5907
+ manifest["writer_lease"] = lease
5908
+ _save_task_manifest(control_repo, manifest)
5909
+ print(f"[task-handoff] task={task_id} writer={args.to_writer} generation={generation + 1}")
5910
+
5911
+
5912
+ def cmd_task_resume(args: argparse.Namespace) -> None:
5913
+ repo = Path(args.repo).resolve()
5914
+ ensure_git_repo(repo)
5915
+ cfg = _load_cfg(repo)
5916
+ task_id = _validate_task_id(args.task_id)
5917
+ manifest = _load_task_manifest(repo, task_id)
5918
+ _require_control_checkout(repo, manifest)
5919
+ _require_current_writer(manifest, args)
5920
+ worktree = Path(_text(manifest.get("worktree_path"))).resolve()
5921
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5922
+ with (
5923
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
5924
+ _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s),
5925
+ ):
5926
+ manifest = _load_task_manifest(repo, task_id)
5927
+ _require_current_writer(manifest, args)
5928
+ if _text(manifest.get("state")) != "conflicted":
5929
+ raise APError(f"Task {task_id} is not awaiting conflict resume.")
5930
+ for marker in ("rebase-merge", "rebase-apply"):
5931
+ path = Path(run(["git", "rev-parse", "--git-path", marker], cwd=worktree).stdout.strip())
5932
+ if not path.is_absolute():
5933
+ path = worktree / path
5934
+ if path.exists():
5935
+ raise APError("Rebase is still in progress; resolve conflicts and run git rebase --continue first.")
5936
+ if _git_status(worktree):
5937
+ raise APError("Task worktree must be clean before task-resume.")
5938
+ target_sha = _text(manifest.get("rebase_target_sha"))
5939
+ if not target_sha or run(
5940
+ ["git", "merge-base", "--is-ancestor", target_sha, "HEAD"],
5941
+ cwd=worktree,
5942
+ check=False,
5943
+ ).returncode != 0:
5944
+ raise APError("Resolved task HEAD does not contain the recorded rebase target.")
5945
+ manifest["base_sha"] = target_sha
5946
+ manifest["base_ref"] = target_sha
5947
+ unowned = _task_unowned_paths(worktree, _load_cfg(worktree), manifest)
5948
+ if unowned:
5949
+ raise APError("Resolved rebase changed paths outside owned_paths:\n- " + "\n- ".join(unowned))
5950
+ _invalidate_task_review(manifest, "rebase conflict resolved; review again")
5951
+ manifest["state"] = "active"
5952
+ _save_task_manifest(repo, manifest)
5953
+ _push_current_task(worktree, manifest)
5954
+ print(f"[task-resume] task={task_id} state=pushed review=pending")
5955
+
5956
+
5957
+ def cmd_task_submodule_sync(args: argparse.Namespace) -> None:
5958
+ repo = Path(args.repo).resolve()
5959
+ ensure_git_repo(repo)
5960
+ cfg = _load_cfg(repo)
5961
+ task_id = _validate_task_id(args.task_id)
5962
+ manifest = _require_task_context(repo, cfg, task_id)
5963
+ if not manifest:
5964
+ raise APError("task-submodule-sync requires a registered task worktree.")
5965
+ control_repo = Path(_text(manifest.get("control_worktree_path"))).resolve()
5966
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5967
+ with (
5968
+ _repo_lock(control_repo, "integration", timeout_s=timeout_s),
5969
+ _repo_lock(control_repo, f"task-{task_id}", timeout_s=timeout_s),
5970
+ ):
5971
+ manifest = _require_task_context(repo, cfg, task_id)
5972
+ if not manifest:
5973
+ raise APError("Task manifest disappeared while synchronizing submodule config.")
5974
+ _sync_task_submodule_config(
5975
+ control_repo,
5976
+ repo,
5977
+ manifest,
5978
+ initial=False,
5979
+ check_common=True,
5980
+ )
5981
+ _save_task_manifest(repo, manifest)
5982
+ url_count = sum(
5983
+ 1 for key in (manifest.get("submodule_config_claims") or {}) if key.endswith(".url")
5984
+ )
5985
+ print(f"[task-submodule-sync] OK task={task_id} urls={url_count}")
5986
+
5987
+
5988
+ def _remote_branch_tip(repo: Path, remote: str, branch: str) -> str:
5989
+ result = run(["git", "ls-remote", "--heads", remote, f"refs/heads/{branch}"], cwd=repo, check=False)
5990
+ if result.returncode != 0:
5991
+ raise APError(
5992
+ f"Cannot inspect remote branch {remote}/{branch}: {result.stderr.strip() or result.stdout.strip()}"
5993
+ )
5994
+ if not result.stdout.strip():
5995
+ return ""
5996
+ return result.stdout.split()[0]
5997
+
5998
+
5999
+ def _branch_is_occupied(repo: Path, task_branch: str) -> bool:
6000
+ wanted = f"refs/heads/{task_branch}"
6001
+ return any(
6002
+ line.strip() == f"branch {wanted}"
6003
+ for line in run(["git", "worktree", "list", "--porcelain"], cwd=repo).stdout.splitlines()
6004
+ )
6005
+
6006
+
6007
+ def _delete_remote_task_branch(repo: Path, manifest: dict) -> bool:
6008
+ remote = _text(manifest.get("remote")) or "origin"
6009
+ branch = _text(manifest.get("task_branch"))
6010
+ actual_tip = _remote_branch_tip(repo, remote, branch)
6011
+ if not actual_tip:
6012
+ return True
6013
+ expected_tip = _text(manifest.get("remote_task_tip"))
6014
+ if not expected_tip or actual_tip != expected_tip:
6015
+ print(
6016
+ f"[task-cleanup] WARN: remote branch retained because its tip changed: "
6017
+ f"{remote}/{branch}",
6018
+ file=sys.stderr,
6019
+ )
6020
+ return False
6021
+ result = run(
6022
+ [
6023
+ "git",
6024
+ "push",
6025
+ "--no-verify",
6026
+ f"--force-with-lease=refs/heads/{branch}:{expected_tip}",
6027
+ remote,
6028
+ f":refs/heads/{branch}",
6029
+ ],
6030
+ cwd=repo,
6031
+ check=False,
6032
+ )
6033
+ if result.returncode != 0:
6034
+ print(
6035
+ f"[task-cleanup] WARN: failed to delete remote task branch {remote}/{branch}: "
6036
+ f"{result.stderr.strip()}",
6037
+ file=sys.stderr,
6038
+ )
6039
+ return False
6040
+ return True
6041
+
6042
+
6043
+ def _finish_registered_task(
6044
+ repo: Path,
6045
+ cfg: dict,
6046
+ manifest: dict,
6047
+ *,
6048
+ keep_remote: bool = False,
6049
+ ) -> dict:
6050
+ manifest = _validate_task_manifest(repo, manifest)
6051
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
6052
+ worktree = Path(_text(manifest.get("worktree_path")))
6053
+ branch = _text(manifest.get("task_branch"))
6054
+ remote = _text(manifest.get("remote")) or "origin"
6055
+ target = _text(manifest.get("target_branch"))
6056
+ cleanup_policy = _manifest_cleanup_policy(manifest, cfg)
6057
+
6058
+ if worktree.exists():
6059
+ worktree_manifest = _active_task_manifest(worktree)
6060
+ if not worktree_manifest or _text(worktree_manifest.get("task_uuid")) != _text(manifest.get("task_uuid")):
6061
+ raise APError(f"Task worktree manifest does not match the registry for {task_id}.")
6062
+ _sync_task_submodule_config(
6063
+ repo,
6064
+ worktree,
6065
+ manifest,
6066
+ initial=False,
6067
+ check_common=True,
6068
+ )
6069
+ _save_task_manifest(repo, manifest)
6070
+ unsafe_ignored = _unsafe_ignored_paths(
6071
+ worktree,
6072
+ list(cleanup_policy.get("disposable_ignored") or []),
6073
+ )
6074
+ if unsafe_ignored:
6075
+ raise APError(
6076
+ "Task worktree contains ignored files that are not declared disposable; refusing cleanup:\n- "
6077
+ + "\n- ".join(unsafe_ignored)
6078
+ )
6079
+ dirty = _git_status(worktree)
6080
+ if dirty:
6081
+ raise APError(f"Task worktree is dirty; refusing cleanup for {task_id}:\n{dirty}")
6082
+ conflicts = run(["git", "ls-files", "-u"], cwd=worktree).stdout.strip()
6083
+ if conflicts:
6084
+ raise APError(f"Task worktree has unresolved conflicts; refusing cleanup for {task_id}.")
6085
+
6086
+ target_sha = _fetch_target(repo, remote, target)
6087
+ local_ref = f"refs/heads/{branch}"
6088
+ tip = _resolve_commit(repo, local_ref) if _git_ref_exists(repo, local_ref) else _text(manifest.get("integrated_sha"))
6089
+ if not tip:
6090
+ raise APError(f"Task branch is missing and no integrated commit was recorded: {branch}")
6091
+ if run(["git", "merge-base", "--is-ancestor", tip, target_sha], cwd=repo, check=False).returncode != 0:
6092
+ raise APError(
6093
+ f"Task branch is not merged into {remote}/{target}; refusing cleanup for {task_id}."
6094
+ )
6095
+
6096
+ if worktree.exists():
6097
+ archived_submodules = _prepare_submodules_for_removal(
6098
+ repo,
6099
+ manifest,
6100
+ worktree,
6101
+ list(cleanup_policy.get("disposable_ignored") or []),
6102
+ )
6103
+ run(["git", "worktree", "unlock", str(worktree)], cwd=repo, check=False)
6104
+ remove_command = ["git", "worktree", "remove"]
6105
+ if archived_submodules:
6106
+ # Git requires --force for any worktree that has ever initialized a
6107
+ # submodule. Reaching this branch means root and recursive submodule
6108
+ # state was checked, unknown ignored data was rejected, additional
6109
+ # linked worktrees were excluded, and Git state was archived. Do not
6110
+ # run `submodule deinit` here: its config changes are shared with the
6111
+ # primary checkout.
6112
+ remove_command.append("--force")
6113
+ remove_command.append(str(worktree))
6114
+ run(remove_command, cwd=repo)
6115
+ if _branch_is_occupied(repo, branch):
6116
+ raise APError(f"Task branch is still checked out by a worktree: {branch}")
6117
+ if _git_ref_exists(repo, local_ref):
6118
+ current_tip = _resolve_commit(repo, local_ref)
6119
+ if current_tip != tip:
6120
+ raise APError(f"Task branch moved during cleanup: {branch}")
6121
+ run(["git", "update-ref", "-d", local_ref, tip], cwd=repo)
6122
+
6123
+ delete_remote = bool(cleanup_policy.get("delete_remote_branch", True))
6124
+ remote_deleted = False
6125
+ if delete_remote and not keep_remote:
6126
+ remote_deleted = _delete_remote_task_branch(repo, manifest)
6127
+ cleanup_pending = delete_remote and not keep_remote and not remote_deleted
6128
+ if cleanup_pending:
6129
+ manifest["state"] = "cleanup-pending"
6130
+ manifest["integrated_sha"] = tip
6131
+ _save_task_manifest(repo, manifest)
6132
+ else:
6133
+ _delete_task_manifest(repo, manifest)
6134
+ run(["git", "worktree", "prune"], cwd=repo, check=False)
6135
+ return {
6136
+ "task_id": task_id,
6137
+ "tip": tip,
6138
+ "target": f"{remote}/{target}",
6139
+ "remote_branch_deleted": remote_deleted,
6140
+ "cleanup_pending": cleanup_pending,
6141
+ }
6142
+
6143
+
6144
+ def cmd_task_finish(args: argparse.Namespace) -> None:
6145
+ repo = Path(args.repo).resolve()
6146
+ ensure_git_repo(repo)
6147
+ cfg = _load_cfg(repo)
6148
+ task_id = _validate_task_id(args.task_id)
6149
+ manifest = _load_task_manifest(repo, task_id)
6150
+ _require_control_checkout(repo, manifest)
6151
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
6152
+ with (
6153
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
6154
+ _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s),
6155
+ ):
6156
+ manifest = _load_task_manifest(repo, task_id)
6157
+ result = _finish_registered_task(
6158
+ repo,
6159
+ cfg,
6160
+ manifest,
6161
+ keep_remote=bool(getattr(args, "keep_remote", False)),
6162
+ )
6163
+ print(
6164
+ f"[task-finish] OK task={task_id} target={result['target']} "
6165
+ f"remote_branch_deleted={str(result['remote_branch_deleted']).lower()}"
6166
+ )
6167
+
6168
+
6169
+ def cmd_task_integrate(args: argparse.Namespace) -> None:
6170
+ repo = Path(args.repo).resolve()
6171
+ ensure_git_repo(repo)
6172
+ cfg = _load_cfg(repo)
6173
+ task_id = _validate_task_id(args.task_id)
6174
+ manifest = _load_task_manifest(repo, task_id)
6175
+ _require_control_checkout(repo, manifest)
6176
+ _require_current_writer(manifest, args)
6177
+ worktree = Path(_text(manifest.get("worktree_path")))
6178
+ if repo.resolve() == worktree.resolve():
6179
+ raise APError("Run task-integrate from the primary/control checkout, not from the task worktree.")
6180
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
6181
+
6182
+ with (
6183
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
6184
+ _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s),
6185
+ ):
6186
+ manifest = _load_task_manifest(repo, task_id)
6187
+ _require_current_writer(manifest, args)
6188
+ worktree = Path(_text(manifest.get("worktree_path")))
6189
+ if not worktree.exists():
6190
+ raise APError(f"Task worktree is missing: {worktree}")
6191
+ worktree_manifest = _active_task_manifest(worktree)
6192
+ if not worktree_manifest or _text(worktree_manifest.get("task_uuid")) != _text(manifest.get("task_uuid")):
6193
+ raise APError(f"Task worktree manifest does not match the registry for {task_id}.")
6194
+ _sync_task_submodule_config(
6195
+ repo,
6196
+ worktree,
6197
+ manifest,
6198
+ initial=False,
6199
+ check_common=True,
6200
+ )
6201
+ _save_task_manifest(repo, manifest)
6202
+ if _text(manifest.get("state")) not in {"pushed", "integration-raced"}:
6203
+ raise APError(f"Task {task_id} must be committed and pushed before integration.")
6204
+ dirty = _git_status(worktree)
6205
+ if dirty:
6206
+ raise APError(f"Task worktree is dirty; run commit-push before integration:\n{dirty}")
6207
+
6208
+ remote = _text(manifest.get("remote")) or "origin"
6209
+ target = _text(manifest.get("target_branch"))
6210
+ task_cfg = _load_cfg(worktree)
6211
+ target_sha = _fetch_target(worktree, remote, target)
6212
+ _require_dependencies(worktree, manifest, target_sha)
6213
+ _require_approved_review(worktree, task_cfg, manifest)
6214
+ if target_sha == _text(manifest.get("base_sha")):
6215
+ rebase = None
6216
+ else:
6217
+ rebase = run(["git", "rebase", target_sha], cwd=worktree, check=False)
6218
+ if rebase is not None and rebase.returncode != 0:
6219
+ manifest["state"] = "conflicted"
6220
+ manifest["rebase_target_sha"] = target_sha
6221
+ _invalidate_task_review(manifest, "integration rebase conflicted")
6222
+ _save_task_manifest(repo, manifest)
6223
+ raise APError(
6224
+ f"Task integration rebase conflicted for {task_id}; resolve it only in {worktree}.\n"
6225
+ f"{rebase.stdout}\n{rebase.stderr}"
6226
+ )
6227
+ if rebase is not None:
6228
+ manifest["base_sha"] = target_sha
6229
+ manifest["base_ref"] = target_sha
6230
+ manifest["rebase_target_sha"] = ""
6231
+ _invalidate_task_review(manifest, "target changed and task was rebased; review again")
6232
+ manifest["state"] = "active"
6233
+ _save_task_manifest(repo, manifest)
6234
+ _push_current_task(worktree, manifest)
6235
+ raise APError(
6236
+ f"Task {task_id} was rebased onto {target_sha}; the task backup branch was updated. "
6237
+ "Run task-review again before final integration."
6238
+ )
6239
+
6240
+ _sync_task_submodule_config(
6241
+ repo,
6242
+ worktree,
6243
+ manifest,
6244
+ initial=False,
6245
+ check_common=True,
6246
+ )
6247
+ _save_task_manifest(repo, manifest)
6248
+
6249
+ manifest = _require_task_context(worktree, task_cfg, task_id) or manifest
6250
+ _require_current_writer(manifest, args)
6251
+ _require_approved_review(worktree, task_cfg, manifest)
6252
+ task_tip = _resolve_commit(worktree, "HEAD")
6253
+ if _resolve_commit(worktree, "HEAD") != task_tip:
6254
+ raise APError("HEAD moved before the target push; refusing to push.")
6255
+ if run(
6256
+ ["git", "merge-base", "--is-ancestor", target_sha, task_tip],
6257
+ cwd=worktree,
6258
+ check=False,
6259
+ ).returncode != 0:
6260
+ raise APError(f"Integration tip {task_tip} is not a descendant of target base {target_sha}.")
6261
+ push = run(
6262
+ [
6263
+ "git",
6264
+ "push",
6265
+ f"--force-with-lease=refs/heads/{target}:{target_sha}",
6266
+ remote,
6267
+ f"{task_tip}:refs/heads/{target}",
6268
+ ],
6269
+ cwd=worktree,
6270
+ check=False,
6271
+ )
6272
+ if push.returncode != 0:
6273
+ manifest["state"] = "integration-raced"
6274
+ manifest["integration_tip"] = task_tip
6275
+ _save_task_manifest(repo, manifest)
6276
+ raise APError(
6277
+ f"Target branch moved while integrating {task_id}. Re-run task-integrate to fetch and "
6278
+ f"rebase the new target.\n{push.stdout}\n{push.stderr}"
6279
+ )
6280
+
6281
+ fresh_target = _fetch_target(worktree, remote, target)
6282
+ if run(
6283
+ ["git", "merge-base", "--is-ancestor", task_tip, fresh_target],
6284
+ cwd=worktree,
6285
+ check=False,
6286
+ ).returncode != 0:
6287
+ raise APError(
6288
+ f"Remote target verification mismatch after push: {task_tip} is not in {fresh_target}."
6289
+ )
6290
+ if _resolve_commit(worktree, "HEAD") != task_tip:
6291
+ raise APError("A push hook changed the task branch after integration; remote target is safe, local state needs review.")
6292
+ manifest["state"] = "integrated"
6293
+ manifest["integrated_sha"] = task_tip
6294
+ manifest["integration_base_sha"] = target_sha
6295
+ _save_task_manifest(repo, manifest)
6296
+
6297
+ cleanup_enabled = bool(
6298
+ _manifest_cleanup_policy(manifest, cfg).get("cleanup_merged", True)
6299
+ )
6300
+ if cleanup_enabled and not getattr(args, "keep_worktree", False):
6301
+ result = _finish_registered_task(
6302
+ repo,
6303
+ cfg,
6304
+ manifest,
6305
+ keep_remote=bool(getattr(args, "keep_remote", False)),
6306
+ )
6307
+ else:
6308
+ result = {
6309
+ "task_id": task_id,
6310
+ "target": f"{remote}/{target}",
6311
+ "remote_branch_deleted": False,
6312
+ }
6313
+
6314
+ print(
6315
+ f"[task-integrate] OK task={task_id} target={result['target']} commit={task_tip} "
6316
+ f"cleaned={str(cleanup_enabled and not getattr(args, 'keep_worktree', False)).lower()}"
6317
+ )
6318
+
6319
+
6320
+ def _worktree_branches(repo: Path) -> set[str]:
6321
+ branches: set[str] = set()
6322
+ for line in run(["git", "worktree", "list", "--porcelain"], cwd=repo).stdout.splitlines():
6323
+ if line.startswith("branch refs/heads/"):
6324
+ branches.add(line[len("branch refs/heads/") :].strip())
6325
+ return branches
6326
+
6327
+
6328
+ def cmd_task_prune(args: argparse.Namespace) -> None:
6329
+ repo = Path(args.repo).resolve()
6330
+ ensure_git_repo(repo)
6331
+ cfg = _load_cfg(repo)
6332
+ _require_control_checkout(repo)
6333
+ concurrency_cfg = _concurrency_cfg(cfg)
6334
+ remote = _text(concurrency_cfg.get("remote")) or "origin"
6335
+ target = _text(concurrency_cfg.get("target_branch")) or _current_branch(repo) or "dev"
6336
+ prefix = _task_branch_prefix(cfg)
6337
+ timeout_s = float(concurrency_cfg.get("lock_timeout_sec") or 30)
6338
+ removed: list[str] = []
6339
+ skipped: list[str] = []
6340
+
6341
+ with _repo_lock(repo, "integration", timeout_s=timeout_s):
6342
+ registry = _task_state_root(repo) / "tasks"
6343
+ registered_branches: set[str] = set()
6344
+ for path in list(sorted(registry.glob("*.json"))) if registry.exists() else []:
6345
+ manifest = _read_json_object(path)
6346
+ if not manifest:
6347
+ continue
6348
+ manifest = _validate_task_manifest(repo, manifest)
6349
+ branch = _text(manifest.get("task_branch"))
6350
+ registered_branches.add(branch)
6351
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
6352
+ try:
6353
+ with _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s):
6354
+ manifest = _read_json_object(path)
6355
+ if not manifest:
6356
+ continue
6357
+ manifest = _validate_task_manifest(repo, manifest)
6358
+ branch = _text(manifest.get("task_branch"))
6359
+ if _text(manifest.get("state")) not in {"integrated", "cleanup-pending"}:
6360
+ skipped.append(f"{branch}: state={_text(manifest.get('state')) or 'unknown'}")
6361
+ continue
6362
+ result = _finish_registered_task(repo, cfg, manifest)
6363
+ except APError as exc:
6364
+ skipped.append(f"{branch}: {exc}")
6365
+ else:
6366
+ if result["cleanup_pending"]:
6367
+ skipped.append(f"{branch}: remote cleanup pending")
6368
+ else:
6369
+ removed.append(branch)
6370
+
6371
+ target_sha = _fetch_target(repo, remote, target)
6372
+ occupied = _worktree_branches(repo)
6373
+ result = run(
6374
+ [
6375
+ "git",
6376
+ "for-each-ref",
6377
+ "--format=%(refname:short)\t%(objectname)",
6378
+ f"refs/heads/{prefix}",
6379
+ ],
6380
+ cwd=repo,
6381
+ )
6382
+ for line in result.stdout.splitlines():
6383
+ if not line.strip() or "\t" not in line:
6384
+ continue
6385
+ branch, tip = line.split("\t", 1)
6386
+ if branch in registered_branches:
6387
+ skipped.append(f"{branch}: registered task")
6388
+ continue
6389
+ if branch in occupied:
6390
+ skipped.append(f"{branch}: occupied")
6391
+ continue
6392
+ if run(["git", "merge-base", "--is-ancestor", tip, target_sha], cwd=repo, check=False).returncode != 0:
6393
+ skipped.append(f"{branch}: unmerged")
6394
+ continue
6395
+ run(["git", "update-ref", "-d", f"refs/heads/{branch}", tip], cwd=repo)
6396
+ removed.append(branch)
6397
+
6398
+ for path in sorted(registry.glob("*.json")) if registry.exists() else []:
6399
+ manifest = _read_json_object(path)
6400
+ if not manifest:
6401
+ continue
6402
+ manifest = _validate_task_manifest(repo, manifest)
6403
+ if _text(manifest.get("state")) == "cleanup-pending":
6404
+ continue
6405
+ branch = _text(manifest.get("task_branch"))
6406
+ if _git_ref_exists(repo, f"refs/heads/{branch}"):
6407
+ continue
6408
+ worktree = Path(_text(manifest.get("worktree_path")))
6409
+ if worktree.exists():
6410
+ continue
6411
+ path.unlink(missing_ok=True)
6412
+ run(["git", "worktree", "prune"], cwd=repo, check=False)
6413
+
6414
+ if args.json:
6415
+ print(json.dumps({"removed": removed, "skipped": skipped}, ensure_ascii=False, indent=2))
6416
+ return
6417
+ print(f"[task-prune] removed={len(removed)} skipped={len(skipped)}")
6418
+ for branch in removed:
6419
+ print(f"[task-prune] removed: {branch}")
6420
+ for detail in skipped:
6421
+ print(f"[task-prune] kept: {detail}")
6422
+
6423
+
6424
+ def _record_commit_push_closure(
6425
+ repo: Path,
6426
+ args: argparse.Namespace,
6427
+ *,
6428
+ commit: str,
6429
+ jenkins: str,
6430
+ target_env: str,
6431
+ verification: List[str],
6432
+ result: str,
6433
+ follow_up: str,
6434
+ structure_check: str,
6435
+ ) -> None:
6436
+ cmd_record_closure(
6437
+ argparse.Namespace(
6438
+ repo=str(repo),
6439
+ task_id=args.task_id,
6440
+ title=args.title,
6441
+ commit=commit,
6442
+ jenkins=jenkins,
6443
+ target_env=target_env,
6444
+ verification=verification,
6445
+ result=result,
6446
+ follow_up=follow_up,
6447
+ profile=str(getattr(args, "effective_profile", "") or getattr(args, "profile", "") or ""),
6448
+ structure_check=structure_check,
6449
+ initial_commit=args.initial_commit,
6450
+ jenkins_failure=args.jenkins_failure,
6451
+ fix_commit=args.fix_commit,
6452
+ )
6453
+ )
6454
+
6455
+
6456
+ def _commit_exact_index(repo: Path, message: str) -> str:
6457
+ expected_parent = _resolve_commit(repo, "HEAD")
6458
+ expected_tree = run(["git", "write-tree"], cwd=repo).stdout.strip()
6459
+ run(["git", "commit", "-m", message], cwd=repo)
6460
+ commit_sha = _resolve_commit(repo, "HEAD")
6461
+ actual_tree = run(["git", "rev-parse", "HEAD^{tree}"], cwd=repo).stdout.strip()
6462
+ parents = run(["git", "show", "-s", "--format=%P", "HEAD"], cwd=repo).stdout.strip().split()
6463
+ post_commit_staged = _checked_git_z_paths(
6464
+ repo,
6465
+ [
6466
+ "git",
6467
+ "diff",
6468
+ "--cached",
6469
+ "--name-only",
6470
+ "-z",
6471
+ "--diff-filter=ACDMRTUXB",
6472
+ ],
6473
+ "post-commit staged paths",
6474
+ )
6475
+ post_commit_unstaged = _unstaged_task_paths(repo)
6476
+ if (
6477
+ parents != [expected_parent]
6478
+ or actual_tree != expected_tree
6479
+ or post_commit_staged
6480
+ or post_commit_unstaged
6481
+ ):
6482
+ raise APError(
6483
+ "Git hooks or another writer changed the commit or working tree after staging. "
6484
+ "The local commit was not pushed; inspect HEAD and rerun the gate before publishing."
6485
+ )
6486
+ return commit_sha
6487
+
6488
+
6489
+ def _push_current_task(repo: Path, manifest: dict, expected_commit: str = "") -> str:
6490
+ commit_sha = expected_commit or _resolve_commit(repo, "HEAD")
6491
+ if _resolve_commit(repo, "HEAD") != commit_sha:
6492
+ raise APError("HEAD moved after the verified commit; refusing to push.")
6493
+ remote = _text(manifest.get("remote")) or "origin"
6494
+ task_branch = _text(manifest.get("task_branch"))
6495
+ remote_tip = _remote_branch_tip(repo, remote, task_branch)
6496
+ command = ["git", "push", "--set-upstream"]
6497
+ # This is an internal backup branch, not the final project push. The
6498
+ # configured pre-push hook runs once later for the target-branch push.
6499
+ command.append("--no-verify")
6500
+ if remote_tip and run(
6501
+ ["git", "merge-base", "--is-ancestor", remote_tip, "HEAD"],
6502
+ cwd=repo,
6503
+ check=False,
6504
+ ).returncode != 0:
6505
+ expected_tip = _text(manifest.get("remote_task_tip"))
6506
+ if not expected_tip or expected_tip != remote_tip:
6507
+ raise APError(
6508
+ f"Remote task branch moved unexpectedly: {remote}/{task_branch}. "
6509
+ "No force push was attempted."
6510
+ )
6511
+ command.append(f"--force-with-lease=refs/heads/{task_branch}:{expected_tip}")
6512
+ command.extend([remote, f"{commit_sha}:refs/heads/{task_branch}"])
6513
+ run(command, cwd=repo)
6514
+ if _resolve_commit(repo, "HEAD") != commit_sha:
6515
+ raise APError("A push hook changed HEAD. The verified task commit was pushed, but local state needs review.")
6516
+ pushed_tip = _remote_branch_tip(repo, remote, task_branch)
6517
+ if pushed_tip != commit_sha:
6518
+ raise APError(
6519
+ f"Remote task branch verification failed: expected {commit_sha}, got {pushed_tip or '(missing)'}."
6520
+ )
6521
+ manifest["state"] = "pushed"
6522
+ manifest["remote_task_tip"] = commit_sha
6523
+ manifest["last_commit"] = commit_sha
6524
+ manifest["claimed_paths"] = _changed_files(repo, _text(manifest.get("base_sha")))
6525
+ review = manifest.get("review") or {}
6526
+ if _text(review.get("verdict")) == "approved":
6527
+ review["diff_head"] = commit_sha
6528
+ review["diff_fingerprint"] = _task_review_fingerprint(repo, manifest)
6529
+ manifest["review"] = review
6530
+ _save_task_manifest(repo, manifest)
6531
+ return commit_sha
6532
+
6533
+
6534
+ def _cmd_commit_push_locked(
6535
+ args: argparse.Namespace,
6536
+ repo: Path,
6537
+ cfg: dict,
6538
+ manifest: dict,
6539
+ ) -> None:
6540
+ control_repo = Path(_text(manifest.get("control_worktree_path"))).resolve()
6541
+ _sync_task_submodule_config(
6542
+ control_repo,
6543
+ repo,
6544
+ manifest,
6545
+ initial=False,
6546
+ check_common=False,
6547
+ )
6548
+ _save_task_manifest(repo, manifest)
6549
+ _require_current_writer(manifest, args)
6550
+ _require_dependencies(repo, manifest, _text(manifest.get("base_sha")))
6551
+ _require_approved_review(repo, cfg, manifest)
6552
+ cmd_doctor(
6553
+ argparse.Namespace(
6554
+ repo=str(repo),
6555
+ profile=_text(getattr(args, "profile", "")).lower(),
6556
+ mode=_text(getattr(args, "mode", "")).lower(),
6557
+ )
6558
+ )
6559
+ base_ref = _text(manifest.get("base_sha"))
6560
+ plan = _resolve_execution_plan(
6561
+ cfg,
6562
+ repo,
6563
+ requested_scope="",
6564
+ requested_profile=_text(getattr(args, "profile", "")).lower(),
6565
+ requested_mode=_text(getattr(args, "mode", "")).lower(),
6566
+ base_ref=base_ref,
6567
+ )
6568
+ mode = str(plan["effective_mode"])
6569
+ args.effective_profile = str(plan["profile"])
6570
+ if args.result:
6571
+ _validate_closure_result(mode, str(args.result))
6572
+
6573
+ msg = args.msg
6574
+ structure_check_status = "not part of the fast local gate"
6575
+
6576
+ before_gate = _task_content_fingerprint(repo, cfg, manifest)
6577
+ cmd_light_gate(
6578
+ argparse.Namespace(
6579
+ repo=str(repo),
6580
+ scope="changed",
6581
+ profile=plan["profile"],
6582
+ mode="dev",
6583
+ base=base_ref,
6584
+ explain=False,
6585
+ )
6586
+ )
6587
+
6588
+ after_gate = _task_content_fingerprint(repo, cfg, manifest)
6589
+ if after_gate != before_gate:
6590
+ raise APError(
6591
+ "The gate changed task-owned files or another writer modified this worktree. "
6592
+ "Review the changes and rerun commit-push; no files were staged or restored."
6593
+ )
6594
+ manifest = _require_task_context(repo, cfg, args.task_id)
6595
+ _require_current_writer(manifest, args)
6596
+ _require_approved_review(repo, cfg, manifest)
6597
+
6598
+ if _task_content_fingerprint(repo, cfg, manifest) != after_gate:
6599
+ raise APError(
6600
+ "Task-owned files changed after the gate. Review them and rerun commit-push; "
6601
+ "no files were staged or restored."
6602
+ )
6603
+
6604
+ dev_verification = args.verification or [
6605
+ "fast changed-scope gate passed",
6606
+ "project commit/push hooks remain enabled",
6607
+ ]
6608
+ _record_commit_push_closure(
6609
+ repo,
6610
+ args,
6611
+ commit="generated by this commit-push run",
6612
+ jenkins="owner-managed after push",
6613
+ target_env="owner-managed acceptance",
6614
+ verification=dev_verification,
6615
+ result=args.result or "DEV-CLOSED",
6616
+ follow_up=args.follow_up or "Complete the push stage with task-integrate; do not wait for Jenkins.",
6617
+ structure_check=args.structure_check or structure_check_status,
6618
+ )
6619
+
6620
+ protected = set(manifest.get("initial_untracked") or [])
6621
+ _cleanup_generated_noise(repo, protected_paths=protected)
6622
+ pre_stage_fingerprint = _task_content_fingerprint(repo, cfg, manifest)
6623
+ manifest = _require_task_context(repo, cfg, args.task_id)
6624
+ _require_current_writer(manifest, args)
6625
+ _require_approved_review(repo, cfg, manifest)
6626
+ if _task_content_fingerprint(repo, cfg, manifest) != pre_stage_fingerprint:
6627
+ raise APError("Task-owned files changed immediately before staging; refusing to commit.")
6628
+ task_paths = _task_commit_paths(repo)
6629
+ staged = _stage_exact_paths(repo, task_paths)
6630
+ if not staged:
6631
+ raise APError("Nothing to commit.")
6632
+ unstaged = _unstaged_task_paths(repo)
6633
+ if unstaged:
6634
+ raise APError(
6635
+ "Task-owned files changed while staging; refusing to commit:\n- " + "\n- ".join(unstaged)
6636
+ )
6637
+ manifest = _require_task_context(repo, cfg, args.task_id)
6638
+
6639
+ committed_sha = _commit_exact_index(repo, msg)
6640
+ _push_current_task(repo, manifest, committed_sha)
6641
+ print(f"[commit-push] OK - profile={plan['profile']} mode={mode} scope={plan['selected_scope']}")
6642
+
6643
+
6644
+ def cmd_commit_push(args: argparse.Namespace) -> None:
6645
+ repo = Path(args.repo).resolve()
6646
+ ensure_git_repo(repo)
6647
+ cfg = _load_cfg(repo)
6648
+ manifest = _require_task_context(repo, cfg, args.task_id)
6649
+ _require_current_writer(manifest, args)
6650
+ lock_name = f"task-{_validate_task_id(args.task_id)}"
6651
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
6652
+ with _repo_lock(repo, lock_name, timeout_s=timeout_s):
6653
+ _cmd_commit_push_locked(args, repo, cfg, manifest)
6654
+
6655
+
6656
+ def main(argv: Optional[List[str]] = None) -> int:
6657
+ p = argparse.ArgumentParser(prog="autopipeline")
6658
+ p.add_argument("--repo", default=".")
6659
+ sp = p.add_subparsers(dest="cmd", required=True)
6660
+
6661
+ s = sp.add_parser("install")
6662
+ s.add_argument("--bridges", action="store_true")
6663
+ s.add_argument("--force", action="store_true")
6664
+ s.add_argument("--full", action="store_true", help="Also materialize all optional document templates")
6665
+ s.set_defaults(func=cmd_install)
6666
+
6667
+ s = sp.add_parser("scaffold")
6668
+ s.add_argument("group", choices=scaffold_groups() + ["all"])
6669
+ s.add_argument("--write", action="store_true")
6670
+ s.add_argument("--force", action="store_true")
6671
+ s.add_argument("--json", action="store_true")
6672
+ s.set_defaults(func=cmd_scaffold)
6673
+
6674
+ s = sp.add_parser("upgrade")
6675
+ s.add_argument("--dry-run", action="store_true")
6676
+ s.add_argument("--write", action="store_true")
6677
+ s.add_argument("--json", action="store_true")
6678
+ s.set_defaults(func=cmd_upgrade)
6679
+
6680
+ s = sp.add_parser("baseline")
6681
+ baseline_sp = s.add_subparsers(dest="baseline_cmd", required=True)
6682
+ b = baseline_sp.add_parser("init")
6683
+ b.add_argument("--write", action="store_true")
6684
+ b.add_argument("--force", action="store_true")
6685
+ b.add_argument("--update-config", action="store_true")
6686
+ b.add_argument("--json", action="store_true")
6687
+ b.set_defaults(func=cmd_baseline_init)
6688
+
6689
+ s = sp.add_parser("gen-summary")
6690
+ s.add_argument("task_id")
6691
+ s.add_argument("--title")
6692
+ s.add_argument("--force", action="store_true")
6693
+ s.set_defaults(func=cmd_gen_summary)
6694
+
6695
+ s = sp.add_parser("check-matrix")
6696
+ s.set_defaults(func=cmd_check_matrix)
6697
+
6698
+ s = sp.add_parser("run")
6699
+ s.add_argument("name")
6700
+ s.set_defaults(func=cmd_run)
6701
+
6702
+ s = sp.add_parser("impact")
6703
+ s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
6704
+ s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES), default="")
6705
+ s.add_argument("--mode", choices=["dev"], default="")
6706
+ s.add_argument("--base")
6707
+ s.add_argument("--json", action="store_true")
6708
+ s.set_defaults(func=cmd_impact)
6709
+
6710
+ s = sp.add_parser("classify")
6711
+ s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
6712
+ s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES), default="")
6713
+ s.add_argument("--mode", choices=["dev"], default="")
6714
+ s.add_argument("--base")
6715
+ s.add_argument("--planned-path", action="append")
6716
+ s.add_argument("--intent")
6717
+ s.add_argument("--intent-file")
6718
+ s.add_argument("--json", action="store_true")
6719
+ s.set_defaults(func=cmd_classify)
6720
+
6721
+ s = sp.add_parser("structure-check")
6722
+ s.add_argument("--scope", choices=sorted(_GATE_SCOPES), default="")
6723
+ s.add_argument("--base")
6724
+ s.add_argument("--json", action="store_true")
6725
+ s.set_defaults(func=cmd_structure_check)
6726
+
6727
+ s = sp.add_parser("gate-profile")
6728
+ s.add_argument("--limit", type=int, default=20)
6729
+ s.add_argument("--json", action="store_true")
6730
+ s.set_defaults(func=cmd_gate_profile)
6731
+
6732
+ s = sp.add_parser("docs-ledger-check")
6733
+ s.add_argument("--json", action="store_true")
6734
+ s.set_defaults(func=cmd_docs_ledger_check)
6735
+
6736
+ s = sp.add_parser("docs-ledger-archive")
6737
+ mode = s.add_mutually_exclusive_group(required=True)
6738
+ mode.add_argument("--plan", action="store_true")
6739
+ mode.add_argument("--write", action="store_true")
6740
+ s.add_argument("--period", help="Archive period in YYYY-MM; defaults to current month")
6741
+ s.add_argument("--json", action="store_true")
6742
+ s.set_defaults(func=cmd_docs_ledger_archive)
6743
+
6744
+ s = sp.add_parser("light-gate")
6745
+ s.add_argument("--scope", choices=["auto", "changed"], default="")
6746
+ s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES), default="")
6747
+ s.add_argument("--mode", choices=["dev"], default="")
4035
6748
  s.add_argument("--base")
4036
6749
  s.add_argument("--explain", action="store_true")
4037
6750
  s.set_defaults(func=cmd_light_gate)
@@ -4091,32 +6804,69 @@ def main(argv: Optional[List[str]] = None) -> int:
4091
6804
  s.add_argument("--fix-commit")
4092
6805
  s.set_defaults(func=cmd_record_closure)
4093
6806
 
6807
+ s = sp.add_parser("task-start")
6808
+ s.add_argument("task_id")
6809
+ s.add_argument("--base")
6810
+ s.add_argument("--target-branch")
6811
+ s.add_argument("--remote")
6812
+ s.add_argument("--worktree-root")
6813
+ s.add_argument("--owner")
6814
+ s.add_argument("--writer")
6815
+ s.add_argument("--owned-path", action="append")
6816
+ s.add_argument("--depends-on", action="append", metavar="TASK_ID=SHA")
6817
+ s.add_argument("--no-fetch", action="store_true")
6818
+ s.set_defaults(func=cmd_task_start)
6819
+
6820
+ s = sp.add_parser("task-status")
6821
+ s.add_argument("task_id", nargs="?")
6822
+ s.add_argument("--json", action="store_true")
6823
+ s.set_defaults(func=cmd_task_status)
6824
+
6825
+ s = sp.add_parser("task-submodule-sync")
6826
+ s.add_argument("task_id")
6827
+ s.set_defaults(func=cmd_task_submodule_sync)
6828
+
6829
+ s = sp.add_parser("task-review")
6830
+ s.add_argument("task_id")
6831
+ s.add_argument("--verdict", choices=["approved", "changes-requested"], required=True)
6832
+ s.add_argument("--diff-fingerprint", required=True)
6833
+ s.add_argument("--reviewer")
6834
+ s.set_defaults(func=cmd_task_review)
6835
+
6836
+ s = sp.add_parser("task-handoff")
6837
+ s.add_argument("task_id")
6838
+ s.add_argument("--from", dest="from_writer", required=True)
6839
+ s.add_argument("--to", dest="to_writer", required=True)
6840
+ s.add_argument("--generation", type=int)
6841
+ s.set_defaults(func=cmd_task_handoff)
6842
+
6843
+ s = sp.add_parser("task-resume")
6844
+ s.add_argument("task_id")
6845
+ s.add_argument("--writer")
6846
+ s.set_defaults(func=cmd_task_resume)
6847
+
6848
+ s = sp.add_parser("task-integrate")
6849
+ s.add_argument("task_id")
6850
+ s.add_argument("--keep-worktree", action="store_true")
6851
+ s.add_argument("--keep-remote", action="store_true")
6852
+ s.add_argument("--writer")
6853
+ s.set_defaults(func=cmd_task_integrate)
6854
+
6855
+ s = sp.add_parser("task-finish")
6856
+ s.add_argument("task_id")
6857
+ s.add_argument("--keep-remote", action="store_true")
6858
+ s.set_defaults(func=cmd_task_finish)
6859
+
6860
+ s = sp.add_parser("task-prune")
6861
+ s.add_argument("--json", action="store_true")
6862
+ s.set_defaults(func=cmd_task_prune)
6863
+
4094
6864
  s = sp.add_parser("commit-push")
4095
6865
  s.add_argument("task_id")
4096
6866
  s.add_argument("--title")
4097
6867
  s.add_argument("--msg", required=True)
4098
- s.add_argument("--mode", choices=["dev", "verify"])
6868
+ s.add_argument("--mode", choices=["dev"])
4099
6869
  s.add_argument("--profile", choices=sorted(_WORKFLOW_PROFILES))
4100
- s.add_argument("--require-light-gate", action="store_true")
4101
- s.add_argument("--require-runtime-health", action="store_true")
4102
- s.add_argument("--require-jenkins", action="store_true")
4103
- s.add_argument("--require-matrix", action="store_true")
4104
- s.add_argument("--record-closure", action="store_true")
4105
- s.add_argument("--job-name")
4106
- s.add_argument("--job-url")
4107
- s.add_argument("--multibranch-root-job")
4108
- s.add_argument("--branch-name")
4109
- s.add_argument("--build-number", type=int)
4110
- s.add_argument("--max-builds", type=int, default=20)
4111
- s.add_argument("--timeout-sec", type=int, default=300)
4112
- s.add_argument("--poll-sec", type=int, default=5)
4113
- s.add_argument("--allow-no-deploy", action="store_true")
4114
- s.add_argument("--backend-path", action="append")
4115
- s.add_argument("--frontend-path", action="append")
4116
- s.add_argument("--backend-basic-auth", action="store_true")
4117
- s.add_argument("--frontend-basic-auth", action="store_true")
4118
- s.add_argument("--ci-build", "--jenkins-build", dest="jenkins_build", metavar="CI_BUILD")
4119
- s.add_argument("--target-env")
4120
6870
  s.add_argument("--verification", action="append")
4121
6871
  s.add_argument("--structure-check")
4122
6872
  s.add_argument("--result", choices=["DEV-CLOSED", "PASS", "FAIL", "PARTIAL"])
@@ -4124,6 +6874,7 @@ def main(argv: Optional[List[str]] = None) -> int:
4124
6874
  s.add_argument("--initial-commit")
4125
6875
  s.add_argument("--ci-failure", "--jenkins-failure", dest="jenkins_failure", metavar="CI_FAILURE")
4126
6876
  s.add_argument("--fix-commit")
6877
+ s.add_argument("--writer")
4127
6878
  s.set_defaults(func=cmd_commit_push)
4128
6879
 
4129
6880
  try: