@elvis1513/auto-coding-skill 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
@@ -32,6 +36,7 @@ from scaffold_templates import scaffold_groups, templates_for
32
36
  _JENKINS_CRUMB_CACHE: dict[str, dict[str, str]] = {}
33
37
  _INVALID_PLACEHOLDERS = {"N/A", "TODO", "TBD", "CHANGEME", "CHANGE_ME", "FILL_ME", "FILL-ME", "PLACEHOLDER", "XXX"}
34
38
  _GENERATED_NOISE_PATTERNS = [
39
+ ".local/auto-coding-skill/**",
35
40
  "__pycache__/**",
36
41
  "**/__pycache__/**",
37
42
  "*.pyc",
@@ -39,6 +44,50 @@ _GENERATED_NOISE_PATTERNS = [
39
44
  ".DS_Store",
40
45
  "**/.DS_Store",
41
46
  ]
47
+ _DEFAULT_DISPOSABLE_IGNORED_PATTERNS = [
48
+ ".local/auto-coding-skill",
49
+ ".local/auto-coding-skill/**",
50
+ "node_modules",
51
+ "node_modules/**",
52
+ "**/node_modules",
53
+ "**/node_modules/**",
54
+ "target",
55
+ "target/**",
56
+ "**/target",
57
+ "**/target/**",
58
+ "dist",
59
+ "dist/**",
60
+ "**/dist",
61
+ "**/dist/**",
62
+ "build",
63
+ "build/**",
64
+ "**/build",
65
+ "**/build/**",
66
+ "coverage",
67
+ "coverage/**",
68
+ "**/coverage",
69
+ "**/coverage/**",
70
+ ".pytest_cache",
71
+ ".pytest_cache/**",
72
+ "**/.pytest_cache",
73
+ "**/.pytest_cache/**",
74
+ ".mypy_cache",
75
+ ".mypy_cache/**",
76
+ "**/.mypy_cache",
77
+ "**/.mypy_cache/**",
78
+ ".ruff_cache",
79
+ ".ruff_cache/**",
80
+ "**/.ruff_cache",
81
+ "**/.ruff_cache/**",
82
+ "__pycache__",
83
+ "__pycache__/**",
84
+ "**/__pycache__",
85
+ "**/__pycache__/**",
86
+ "*.pyc",
87
+ "**/*.pyc",
88
+ ".DS_Store",
89
+ "**/.DS_Store",
90
+ ]
42
91
 
43
92
 
44
93
  def _skill_root() -> Path:
@@ -85,6 +134,13 @@ def _append_jsonl(path: Path, payload: dict) -> None:
85
134
 
86
135
 
87
136
  def _evidence_log_path(repo: Path, cfg: dict) -> Path:
137
+ manifest = _active_task_manifest(repo)
138
+ if manifest:
139
+ docs_cfg = cfg.get("docs") or {}
140
+ task_dir = _text(docs_cfg.get("task_evidence_dir")) or "docs/tasks/evidence"
141
+ return Path(repo, task_dir, f"{manifest['task_id']}.jsonl")
142
+ if _task_isolation(cfg) == "worktree":
143
+ return Path(repo, ".local/auto-coding-skill/evidence.jsonl")
88
144
  docs_cfg = cfg.get("docs") or {}
89
145
  rel = _text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl"
90
146
  return Path(repo, rel)
@@ -1020,6 +1076,451 @@ def _text(value: object) -> str:
1020
1076
  return str(value or "").strip()
1021
1077
 
1022
1078
 
1079
+ _TASK_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
1080
+
1081
+
1082
+ def _concurrency_cfg(cfg: dict) -> dict:
1083
+ value = cfg.get("concurrency") or {}
1084
+ return value if isinstance(value, dict) else {}
1085
+
1086
+
1087
+ def _task_isolation(cfg: dict) -> str:
1088
+ isolation = _text(_concurrency_cfg(cfg).get("isolation")).lower() or "worktree"
1089
+ if isolation not in {"worktree", "legacy"}:
1090
+ raise APError("concurrency.isolation must be worktree or legacy")
1091
+ return isolation
1092
+
1093
+
1094
+ def _cleanup_policy(cfg: dict) -> dict:
1095
+ concurrency_cfg = _concurrency_cfg(cfg)
1096
+ return {
1097
+ "cleanup_merged": _bool_config(concurrency_cfg.get("cleanup_merged"), True),
1098
+ "delete_remote_branch": _bool_config(concurrency_cfg.get("delete_remote_branch"), True),
1099
+ "disposable_ignored": [
1100
+ _text(item)
1101
+ for item in _as_list(concurrency_cfg.get("disposable_ignored"))
1102
+ if _text(item)
1103
+ ],
1104
+ }
1105
+
1106
+
1107
+ def _manifest_cleanup_policy(manifest: dict, fallback_cfg: dict) -> dict:
1108
+ value = manifest.get("cleanup_policy")
1109
+ return value if isinstance(value, dict) else _cleanup_policy(fallback_cfg)
1110
+
1111
+
1112
+ def _validate_task_id(task_id: str) -> str:
1113
+ value = _text(task_id)
1114
+ if not _TASK_ID_RE.fullmatch(value) or value.endswith(".lock") or ".." in value:
1115
+ raise APError(
1116
+ "Task ID must be 1-64 characters using letters, digits, '.', '_' or '-', "
1117
+ "must start with a letter/digit, and cannot contain '..' or end with '.lock'."
1118
+ )
1119
+ return value
1120
+
1121
+
1122
+ def _resolve_git_dir(repo: Path, option: str) -> Path:
1123
+ result = run(["git", "rev-parse", option], cwd=repo, check=False)
1124
+ if result.returncode != 0 or not result.stdout.strip():
1125
+ raise APError(f"Cannot resolve {option} for Git repository: {repo}")
1126
+ value = Path(result.stdout.strip())
1127
+ if not value.is_absolute():
1128
+ value = repo / value
1129
+ return value.resolve()
1130
+
1131
+
1132
+ def _git_common_dir(repo: Path) -> Path:
1133
+ return _resolve_git_dir(repo, "--git-common-dir")
1134
+
1135
+
1136
+ def _git_dir(repo: Path) -> Path:
1137
+ return _resolve_git_dir(repo, "--git-dir")
1138
+
1139
+
1140
+ def _task_state_root(repo: Path) -> Path:
1141
+ return _git_common_dir(repo) / "auto-coding-skill"
1142
+
1143
+
1144
+ def _task_registry_path(repo: Path, task_id: str) -> Path:
1145
+ return _task_state_root(repo) / "tasks" / f"{_validate_task_id(task_id)}.json"
1146
+
1147
+
1148
+ def _worktree_manifest_path(repo: Path) -> Path:
1149
+ return _git_dir(repo) / "auto-coding-skill-task.json"
1150
+
1151
+
1152
+ def _read_json_object(path: Path) -> Optional[dict]:
1153
+ try:
1154
+ payload = json.loads(path.read_text(encoding="utf-8"))
1155
+ except (FileNotFoundError, json.JSONDecodeError, OSError):
1156
+ return None
1157
+ return payload if isinstance(payload, dict) else None
1158
+
1159
+
1160
+ def _write_json_object(path: Path, payload: dict) -> None:
1161
+ path.parent.mkdir(parents=True, exist_ok=True)
1162
+ temporary = path.with_name(f".{path.name}.{os.getpid()}.{uuid.uuid4().hex}.tmp")
1163
+ temporary.write_text(
1164
+ json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
1165
+ encoding="utf-8",
1166
+ )
1167
+ os.replace(temporary, path)
1168
+
1169
+
1170
+ def _active_task_manifest(repo: Path) -> Optional[dict]:
1171
+ try:
1172
+ return _read_json_object(_worktree_manifest_path(repo))
1173
+ except APError:
1174
+ return None
1175
+
1176
+
1177
+ def _validate_task_manifest(repo: Path, manifest: dict, expected_task_id: str = "") -> dict:
1178
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1179
+ if expected_task_id and task_id != _validate_task_id(expected_task_id):
1180
+ raise APError(
1181
+ f"Task registry identity mismatch: expected={expected_task_id}, manifest={task_id}"
1182
+ )
1183
+ task_uuid = _text(manifest.get("task_uuid"))
1184
+ if not re.fullmatch(r"[0-9a-f]{32}", task_uuid):
1185
+ raise APError(f"Invalid task manifest UUID for {task_id}; refusing Git operations.")
1186
+ task_branch = _text(manifest.get("task_branch"))
1187
+ if not task_branch.endswith(f"/{task_id}") or run(
1188
+ ["git", "check-ref-format", "--branch", task_branch],
1189
+ cwd=repo,
1190
+ check=False,
1191
+ ).returncode != 0:
1192
+ raise APError(f"Invalid task branch in manifest for {task_id}: {task_branch!r}")
1193
+ target_branch = _text(manifest.get("target_branch"))
1194
+ if run(
1195
+ ["git", "check-ref-format", "--branch", target_branch],
1196
+ cwd=repo,
1197
+ check=False,
1198
+ ).returncode != 0:
1199
+ raise APError(f"Invalid target branch in manifest for {task_id}: {target_branch!r}")
1200
+ remote = _text(manifest.get("remote"))
1201
+ if not remote or remote.startswith("-") or any(char in remote for char in "\0\r\n"):
1202
+ raise APError(f"Invalid remote in manifest for {task_id}: {remote!r}")
1203
+ base_sha = _text(manifest.get("base_sha"))
1204
+ if not re.fullmatch(r"(?:[0-9a-f]{40}|[0-9a-f]{64})", base_sha):
1205
+ raise APError(f"Invalid base commit in manifest for {task_id}: {base_sha!r}")
1206
+ worktree_raw = _text(manifest.get("worktree_path"))
1207
+ control_raw = _text(manifest.get("control_worktree_path"))
1208
+ worktree = Path(worktree_raw)
1209
+ control = Path(control_raw)
1210
+ if not worktree_raw or not control_raw or not worktree.is_absolute() or not control.is_absolute():
1211
+ raise APError(f"Task manifest paths must be absolute for {task_id}.")
1212
+ worktree = worktree.resolve()
1213
+ control = control.resolve()
1214
+ if worktree == control or control in worktree.parents or worktree in control.parents:
1215
+ raise APError(
1216
+ f"Task worktree must be outside its control checkout for {task_id}: {worktree}"
1217
+ )
1218
+ return manifest
1219
+
1220
+
1221
+ def _load_task_manifest(repo: Path, task_id: str) -> dict:
1222
+ path = _task_registry_path(repo, task_id)
1223
+ manifest = _read_json_object(path)
1224
+ if not manifest:
1225
+ raise APError(
1226
+ f"Task is not registered: {task_id}. Run `ap.py task-start {task_id}` first."
1227
+ )
1228
+ return _validate_task_manifest(repo, manifest, task_id)
1229
+
1230
+
1231
+ def _save_task_manifest(repo: Path, manifest: dict) -> None:
1232
+ _validate_task_manifest(repo, manifest)
1233
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1234
+ manifest["updated_at"] = _now_iso()
1235
+ _write_json_object(_task_registry_path(repo, task_id), manifest)
1236
+ worktree_value = _text(manifest.get("worktree_path"))
1237
+ if worktree_value:
1238
+ worktree = Path(worktree_value)
1239
+ if worktree.exists():
1240
+ try:
1241
+ _write_json_object(_worktree_manifest_path(worktree), manifest)
1242
+ except APError:
1243
+ pass
1244
+
1245
+
1246
+ def _delete_task_manifest(repo: Path, manifest: dict) -> None:
1247
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1248
+ _task_registry_path(repo, task_id).unlink(missing_ok=True)
1249
+
1250
+
1251
+ def _current_branch(repo: Path) -> str:
1252
+ result = run(["git", "branch", "--show-current"], cwd=repo, check=False)
1253
+ return result.stdout.strip() if result.returncode == 0 else ""
1254
+
1255
+
1256
+ def _require_control_checkout(repo: Path, manifest: Optional[dict] = None) -> None:
1257
+ if _active_task_manifest(repo):
1258
+ raise APError("Run this command from the primary/control checkout, not from a task worktree.")
1259
+ if _git_dir(repo) != _git_common_dir(repo):
1260
+ raise APError("Run this command from the primary/control checkout, not from a linked worktree.")
1261
+ if manifest:
1262
+ expected = _text(manifest.get("control_worktree_path"))
1263
+ if expected and repo.resolve() != Path(expected).resolve():
1264
+ raise APError(f"Task control checkout mismatch: current={repo.resolve()}, expected={expected}")
1265
+
1266
+
1267
+ def _resolve_commit(repo: Path, ref: str) -> str:
1268
+ result = run(["git", "rev-parse", "--verify", f"{ref}^{{commit}}"], cwd=repo, check=False)
1269
+ if result.returncode != 0 or not result.stdout.strip():
1270
+ raise APError(f"Cannot resolve Git commit: {ref}")
1271
+ return result.stdout.strip()
1272
+
1273
+
1274
+ def _git_z_paths(repo: Path, command: list[str]) -> list[str]:
1275
+ result = run(command, cwd=repo, check=False)
1276
+ if result.returncode != 0:
1277
+ return []
1278
+ return [item for item in result.stdout.split("\0") if item]
1279
+
1280
+
1281
+ def _checked_git_z_paths(repo: Path, command: list[str], context: str) -> list[str]:
1282
+ result = run(command, cwd=repo, check=False)
1283
+ if result.returncode != 0:
1284
+ raise APError(
1285
+ f"Cannot inspect {context}: {result.stderr.strip() or result.stdout.strip()}"
1286
+ )
1287
+ return [item for item in result.stdout.split("\0") if item]
1288
+
1289
+
1290
+ def _working_tree_paths(repo: Path) -> list[str]:
1291
+ paths: set[str] = set()
1292
+ paths.update(
1293
+ _git_z_paths(
1294
+ repo,
1295
+ ["git", "diff", "--name-only", "-z", "--diff-filter=ACDMRTUXB"],
1296
+ )
1297
+ )
1298
+ paths.update(
1299
+ _git_z_paths(
1300
+ repo,
1301
+ ["git", "diff", "--cached", "--name-only", "-z", "--diff-filter=ACDMRTUXB"],
1302
+ )
1303
+ )
1304
+ paths.update(
1305
+ _git_z_paths(repo, ["git", "ls-files", "--others", "--exclude-standard", "-z"])
1306
+ )
1307
+ return sorted(path.replace("\\", "/") for path in paths if path)
1308
+
1309
+
1310
+ def _task_runtime_paths(repo: Path, cfg: dict, manifest: Optional[dict]) -> set[str]:
1311
+ docs_cfg = cfg.get("docs") or {}
1312
+ gate_cfg = _gate_cfg(cfg)
1313
+ paths = {
1314
+ _text(gate_cfg.get("profile_log")) or ".local/auto-coding-skill/gate-profile.jsonl",
1315
+ }
1316
+ if manifest:
1317
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
1318
+ evidence_dir = _text(docs_cfg.get("task_evidence_dir")) or "docs/tasks/evidence"
1319
+ paths.add(f"{evidence_dir.rstrip('/')}/{task_id}.jsonl")
1320
+ else:
1321
+ paths.add(_text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl")
1322
+ normalized: set[str] = set()
1323
+ for path in paths:
1324
+ value = path.replace("\\", "/")
1325
+ if value.startswith("./"):
1326
+ value = value[2:]
1327
+ if value:
1328
+ normalized.add(value)
1329
+ return normalized
1330
+
1331
+
1332
+ def _task_content_fingerprint(repo: Path, cfg: dict, manifest: Optional[dict]) -> str:
1333
+ runtime_paths = _task_runtime_paths(repo, cfg, manifest)
1334
+ relevant = [
1335
+ path
1336
+ for path in _working_tree_paths(repo)
1337
+ if path not in runtime_paths and not _is_generated_noise_path(path)
1338
+ ]
1339
+ digest = hashlib.sha256()
1340
+ digest.update(f"branch:{_current_branch(repo)}\n".encode("utf-8"))
1341
+ digest.update(f"head:{_resolve_commit(repo, 'HEAD')}\n".encode("utf-8"))
1342
+ for rel in relevant:
1343
+ digest.update(rel.encode("utf-8", errors="surrogateescape"))
1344
+ path = repo / rel
1345
+ try:
1346
+ digest.update(f"mode:{path.lstat().st_mode:o}".encode("ascii"))
1347
+ except OSError:
1348
+ digest.update(b"mode:<missing>")
1349
+ if path.is_symlink():
1350
+ try:
1351
+ digest.update(os.readlink(path).encode("utf-8", errors="surrogateescape"))
1352
+ except OSError:
1353
+ digest.update(b"<unreadable-symlink>")
1354
+ elif path.is_file():
1355
+ try:
1356
+ digest.update(path.read_bytes())
1357
+ except OSError:
1358
+ digest.update(b"<unreadable>")
1359
+ else:
1360
+ digest.update(b"<missing>")
1361
+ for start in range(0, len(relevant), 100):
1362
+ batch = relevant[start : start + 100]
1363
+ if not batch:
1364
+ continue
1365
+ cached = run(
1366
+ ["git", "diff", "--cached", "--binary", "--no-ext-diff", "--", *batch],
1367
+ cwd=repo,
1368
+ check=False,
1369
+ )
1370
+ digest.update(cached.stdout.encode("utf-8", errors="surrogateescape"))
1371
+ unstaged = run(
1372
+ ["git", "diff", "--binary", "--no-ext-diff", "--", *batch],
1373
+ cwd=repo,
1374
+ check=False,
1375
+ )
1376
+ digest.update(unstaged.stdout.encode("utf-8", errors="surrogateescape"))
1377
+ index_state = run(
1378
+ ["git", "ls-files", "--stage", "-z", "--", *batch],
1379
+ cwd=repo,
1380
+ check=False,
1381
+ )
1382
+ digest.update(index_state.stdout.encode("utf-8", errors="surrogateescape"))
1383
+ return digest.hexdigest()
1384
+
1385
+
1386
+ def _stage_exact_paths(repo: Path, paths: list[str]) -> list[str]:
1387
+ expected = sorted(set(path for path in paths if path))
1388
+ for start in range(0, len(expected), 100):
1389
+ run(["git", "add", "-A", "--", *expected[start : start + 100]], cwd=repo)
1390
+ staged = sorted(
1391
+ set(
1392
+ _checked_git_z_paths(
1393
+ repo,
1394
+ [
1395
+ "git",
1396
+ "diff",
1397
+ "--cached",
1398
+ "--name-only",
1399
+ "-z",
1400
+ "--diff-filter=ACDMRTUXB",
1401
+ ],
1402
+ "staged task paths",
1403
+ )
1404
+ )
1405
+ )
1406
+ unexpected = sorted(set(staged) - set(expected))
1407
+ if unexpected:
1408
+ raise APError(
1409
+ "Refusing to commit paths outside the current task:\n- " + "\n- ".join(unexpected)
1410
+ )
1411
+ return staged
1412
+
1413
+
1414
+ def _task_commit_paths(repo: Path) -> list[str]:
1415
+ return [path for path in _working_tree_paths(repo) if not _is_generated_noise_path(path)]
1416
+
1417
+
1418
+ def _unstaged_task_paths(repo: Path) -> list[str]:
1419
+ paths = set(
1420
+ _checked_git_z_paths(
1421
+ repo,
1422
+ ["git", "diff", "--name-only", "-z", "--diff-filter=ACDMRTUXB"],
1423
+ "unstaged task paths",
1424
+ )
1425
+ )
1426
+ paths.update(
1427
+ _checked_git_z_paths(
1428
+ repo,
1429
+ ["git", "ls-files", "--others", "--exclude-standard", "-z"],
1430
+ "untracked task paths",
1431
+ )
1432
+ )
1433
+ return sorted(path for path in paths if not _is_generated_noise_path(path))
1434
+
1435
+
1436
+ def _require_task_context(repo: Path, cfg: dict, task_id: str) -> Optional[dict]:
1437
+ manifest = _active_task_manifest(repo)
1438
+ if not manifest:
1439
+ if _git_dir(repo) != _git_common_dir(repo):
1440
+ raise APError(
1441
+ "This linked worktree has no registered task manifest. Refusing legacy fallback; "
1442
+ "return to the primary checkout and create or recover the task with task-start."
1443
+ )
1444
+ if _task_isolation(cfg) == "legacy":
1445
+ return None
1446
+ raise APError(
1447
+ "This project requires an isolated task worktree. "
1448
+ f"Run `python3 docs/tools/autopipeline/ap.py task-start {task_id}` from the main checkout, "
1449
+ "then continue in the returned worktree."
1450
+ )
1451
+ expected_task = _validate_task_id(task_id)
1452
+ actual_task = _text(manifest.get("task_id"))
1453
+ if actual_task != expected_task:
1454
+ raise APError(
1455
+ f"Task/worktree mismatch: command={expected_task}, worktree={actual_task or '(missing)'}."
1456
+ )
1457
+ registered = _read_json_object(_task_registry_path(repo, expected_task))
1458
+ if not registered:
1459
+ raise APError(f"Task registry entry is missing for {expected_task}; refusing to recreate it implicitly.")
1460
+ for field in ("task_id", "task_uuid", "task_branch", "worktree_path", "base_sha"):
1461
+ if _text(registered.get(field)) != _text(manifest.get(field)):
1462
+ raise APError(f"Task manifest/registry mismatch for {expected_task}: {field}")
1463
+ manifest = registered
1464
+ expected_path = Path(_text(manifest.get("worktree_path"))).resolve()
1465
+ if repo.resolve() != expected_path:
1466
+ raise APError(f"Task manifest belongs to {expected_path}, not {repo.resolve()}.")
1467
+ branch = _current_branch(repo)
1468
+ expected_branch = _text(manifest.get("task_branch"))
1469
+ if not branch or branch != expected_branch:
1470
+ raise APError(
1471
+ f"Task branch mismatch: current={branch or '(detached)'}, expected={expected_branch}."
1472
+ )
1473
+ if _text(manifest.get("state")) in {"integrated", "finished"}:
1474
+ raise APError(f"Task {expected_task} is already {_text(manifest.get('state'))}.")
1475
+ return manifest
1476
+
1477
+
1478
+ @contextlib.contextmanager
1479
+ def _repo_lock(repo: Path, name: str, timeout_s: float = 30.0) -> Iterator[None]:
1480
+ safe_name = re.sub(r"[^A-Za-z0-9._-]+", "-", name).strip("-") or "repository"
1481
+ lock_path = _task_state_root(repo) / "locks" / f"{safe_name}.lock"
1482
+ lock_path.parent.mkdir(parents=True, exist_ok=True)
1483
+ handle = lock_path.open("a+", encoding="utf-8")
1484
+ try:
1485
+ try:
1486
+ import fcntl # type: ignore
1487
+ except ImportError as exc: # pragma: no cover - current runtime is POSIX
1488
+ raise APError("Task locking requires a POSIX fcntl runtime.") from exc
1489
+ deadline = time.time() + max(0.0, timeout_s)
1490
+ while True:
1491
+ try:
1492
+ fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
1493
+ break
1494
+ except BlockingIOError:
1495
+ if time.time() >= deadline:
1496
+ raise APError(f"Timed out waiting for repository lock: {name}")
1497
+ time.sleep(0.1)
1498
+ handle.seek(0)
1499
+ handle.truncate()
1500
+ handle.write(
1501
+ json.dumps(
1502
+ {
1503
+ "pid": os.getpid(),
1504
+ "host": socket.gethostname(),
1505
+ "acquired_at": _now_iso(),
1506
+ "name": name,
1507
+ },
1508
+ ensure_ascii=False,
1509
+ )
1510
+ + "\n"
1511
+ )
1512
+ handle.flush()
1513
+ yield
1514
+ finally:
1515
+ try:
1516
+ import fcntl # type: ignore
1517
+
1518
+ fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
1519
+ except Exception:
1520
+ pass
1521
+ handle.close()
1522
+
1523
+
1023
1524
  def _is_placeholder(value: object) -> bool:
1024
1525
  raw = _text(value)
1025
1526
  upper = raw.upper()
@@ -1226,11 +1727,12 @@ def _default_base_ref(repo: Path) -> str:
1226
1727
 
1227
1728
  def _changed_files(repo: Path, base_ref: str = "") -> list[str]:
1228
1729
  paths: list[str] = []
1229
- effective_base = base_ref or _default_base_ref(repo)
1730
+ manifest = _active_task_manifest(repo)
1731
+ effective_base = base_ref or (_text(manifest.get("base_sha")) if manifest else "") or _default_base_ref(repo)
1230
1732
  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"]))
1733
+ paths.extend(_git_lines(repo, ["git", "diff", "--name-only", "--diff-filter=ACDMRTUXB", f"{effective_base}...HEAD"]))
1734
+ paths.extend(_git_lines(repo, ["git", "diff", "--name-only", "--diff-filter=ACDMRTUXB"]))
1735
+ paths.extend(_git_lines(repo, ["git", "diff", "--cached", "--name-only", "--diff-filter=ACDMRTUXB"]))
1234
1736
  paths.extend(_git_lines(repo, ["git", "ls-files", "--others", "--exclude-standard"]))
1235
1737
  return _unique_paths(paths)
1236
1738
 
@@ -1256,10 +1758,15 @@ def _commit_changed_files(repo: Path, ref: str) -> list[str]:
1256
1758
  return _unique_paths([line.strip() for line in result.stdout.splitlines() if line.strip()])
1257
1759
 
1258
1760
 
1259
- def _cleanup_generated_noise(repo: Path) -> None:
1761
+ def _cleanup_generated_noise(repo: Path, protected_paths: Optional[set[str]] = None) -> None:
1762
+ protected = {path.replace("\\", "/") for path in (protected_paths or set())}
1260
1763
  candidates = [
1261
- rel for rel in _git_lines(repo, ["git", "ls-files", "--others", "--exclude-standard"])
1262
- if _is_generated_noise_path(rel)
1764
+ rel
1765
+ for rel in _git_z_paths(
1766
+ repo,
1767
+ ["git", "ls-files", "--others", "--exclude-standard", "-z"],
1768
+ )
1769
+ if _is_generated_noise_path(rel) and rel.replace("\\", "/") not in protected
1263
1770
  ]
1264
1771
  for rel in candidates:
1265
1772
  path = repo / rel
@@ -1373,7 +1880,7 @@ def _impact_summary(
1373
1880
  ) -> dict:
1374
1881
  paths = _changed_files(repo, base_ref=base_ref) if changed_paths is None else _unique_paths(changed_paths)
1375
1882
  docs_cfg = cfg.get("docs") or {}
1376
- ignored_runtime_paths = {
1883
+ ignored_runtime_paths = _task_runtime_paths(repo, cfg, _active_task_manifest(repo)) | {
1377
1884
  _text(docs_cfg.get("evidence_log")) or "docs/tasks/evidence.jsonl",
1378
1885
  _text(docs_cfg.get("closure_log")) or "docs/tasks/closure-log.md",
1379
1886
  _text(_gate_cfg(cfg).get("profile_log")) or ".local/auto-coding-skill/gate-profile.jsonl",
@@ -1464,9 +1971,14 @@ def _run_full_gate(repo: Path, cfg: dict) -> list[str]:
1464
1971
 
1465
1972
 
1466
1973
  def _run_git_diff_check(repo: Path, cfg: dict) -> None:
1467
- print("[diff-check] git diff --check")
1974
+ print("[diff-check] working tree + index + task commits")
1468
1975
  start = time.time()
1469
1976
  run(["git", "diff", "--check"], cwd=repo)
1977
+ run(["git", "diff", "--cached", "--check"], cwd=repo)
1978
+ manifest = _active_task_manifest(repo)
1979
+ base_sha = _text(manifest.get("base_sha")) if manifest else ""
1980
+ if base_sha:
1981
+ run(["git", "diff", "--check", f"{base_sha}...HEAD"], cwd=repo)
1470
1982
  _record_gate_profile(repo, cfg, "diff_check", "pass", time.time() - start)
1471
1983
  _record_evidence(repo, cfg, "diff_check", "pass")
1472
1984
  print("[diff-check] OK")
@@ -3329,6 +3841,7 @@ def cmd_doctor(args: argparse.Namespace) -> None:
3329
3841
  docs_cfg = (cfg.get("docs") or {})
3330
3842
  runtime_cfg = (cfg.get("runtime") or {})
3331
3843
  structure_cfg = _structure_cfg(cfg)
3844
+ concurrency_cfg = _concurrency_cfg(cfg)
3332
3845
 
3333
3846
  missing: List[str] = []
3334
3847
  validation_errors: List[str] = []
@@ -3341,6 +3854,18 @@ def cmd_doctor(args: argparse.Namespace) -> None:
3341
3854
  missing.append("workflow.mode (must be dev or verify)")
3342
3855
  if profile not in _WORKFLOW_PROFILES:
3343
3856
  missing.append("workflow.profile (must be auto, micro, standard, or high-risk)")
3857
+ isolation = _text(concurrency_cfg.get("isolation")).lower() or "worktree"
3858
+ if isolation not in {"worktree", "legacy"}:
3859
+ validation_errors.append("concurrency.isolation must be worktree or legacy")
3860
+ branch_prefix = _text(concurrency_cfg.get("branch_prefix")) or "codex/"
3861
+ if not branch_prefix.endswith("/"):
3862
+ validation_errors.append("concurrency.branch_prefix must end with '/'")
3863
+ elif run(
3864
+ ["git", "check-ref-format", "--branch", f"{branch_prefix}TASK"],
3865
+ cwd=repo,
3866
+ check=False,
3867
+ ).returncode != 0:
3868
+ validation_errors.append("concurrency.branch_prefix does not form a valid Git branch")
3344
3869
  if not str(project_cfg.get("name") or "").strip():
3345
3870
  missing.append("project.name")
3346
3871
  enforcement = _text(structure_cfg.get("enforcement")).lower()
@@ -3678,6 +4203,7 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3678
4203
  repo = Path(args.repo).resolve()
3679
4204
  ensure_git_repo(repo)
3680
4205
  cfg = _load_cfg(repo)
4206
+ context_manifest = _require_task_context(repo, cfg, args.task_id)
3681
4207
  plan = _resolve_execution_plan(
3682
4208
  cfg,
3683
4209
  repo,
@@ -3701,7 +4227,12 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3701
4227
  docs_cfg = (cfg.get("docs") or {})
3702
4228
  target_cfg = (cfg.get("target_env") or {})
3703
4229
  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")))
4230
+ manifest = context_manifest or _active_task_manifest(repo)
4231
+ closure_log = (
4232
+ _task_doc_path(repo, cfg, manifest, "closure")
4233
+ if manifest
4234
+ else Path(repo, str(docs_cfg.get("closure_log", "docs/tasks/closure-log.md")))
4235
+ )
3705
4236
  closure_log.parent.mkdir(parents=True, exist_ok=True)
3706
4237
  if not closure_log.exists():
3707
4238
  closure_log.write_text("# Closure Log\n\n", encoding="utf-8")
@@ -3739,6 +4270,8 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3739
4270
  f.write("\n")
3740
4271
  f.write("\n".join(lines))
3741
4272
  f.write("\n")
4273
+ if manifest:
4274
+ _update_active_task_status(repo, cfg, manifest, str(args.result))
3742
4275
  _record_evidence(
3743
4276
  repo,
3744
4277
  cfg,
@@ -3755,92 +4288,1794 @@ def cmd_record_closure(args: argparse.Namespace) -> None:
3755
4288
  print(f"[record-closure] OK: {closure_log}")
3756
4289
 
3757
4290
 
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
- )
4291
+ def _task_remote_and_target(cfg: dict, args: argparse.Namespace) -> tuple[str, str, str]:
4292
+ concurrency_cfg = _concurrency_cfg(cfg)
4293
+ remote = _text(getattr(args, "remote", "")) or _text(concurrency_cfg.get("remote")) or "origin"
4294
+ target_branch = (
4295
+ _text(getattr(args, "target_branch", ""))
4296
+ or _text(concurrency_cfg.get("target_branch"))
4297
+ )
4298
+ base_ref = _text(getattr(args, "base", "")) or _text(concurrency_cfg.get("base_ref"))
4299
+ if not target_branch and base_ref.startswith(f"{remote}/"):
4300
+ target_branch = base_ref[len(remote) + 1 :]
4301
+ if not target_branch:
4302
+ branch = _current_branch(Path(args.repo).resolve())
4303
+ target_branch = branch or "dev"
4304
+ if not base_ref:
4305
+ base_ref = f"{remote}/{target_branch}"
4306
+ return remote, target_branch, base_ref
4307
+
4308
+
4309
+ def _fetch_target(repo: Path, remote: str, target_branch: str) -> str:
4310
+ remote_ref = f"refs/remotes/{remote}/{target_branch}"
4311
+ run(
4312
+ [
4313
+ "git",
4314
+ "fetch",
4315
+ "--no-tags",
4316
+ remote,
4317
+ f"refs/heads/{target_branch}:{remote_ref}",
4318
+ ],
4319
+ cwd=repo,
3787
4320
  )
4321
+ return _resolve_commit(repo, remote_ref)
3788
4322
 
3789
4323
 
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(),
3798
- )
3799
- )
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)
4324
+ def _task_branch_prefix(cfg: dict) -> str:
4325
+ prefix = _text(_concurrency_cfg(cfg).get("branch_prefix")) or "codex/"
4326
+ if not prefix.endswith("/"):
4327
+ raise APError("concurrency.branch_prefix must end with '/'.")
4328
+ return prefix
3815
4329
 
3816
- msg = args.msg
3817
- structure_check_status = "skipped"
3818
4330
 
3819
- if args.require_runtime_health:
3820
- cmd_wait_health(argparse.Namespace(repo=str(repo), scope="runtime"))
4331
+ def _task_worktree_path(repo: Path, cfg: dict, task_id: str, override: str = "") -> Path:
4332
+ raw_root = override or _text(_concurrency_cfg(cfg).get("worktree_root")) or "../.worktrees"
4333
+ root = Path(raw_root).expanduser()
4334
+ if not root.is_absolute():
4335
+ root = repo / root
4336
+ root = root.resolve()
4337
+ candidate = (root / repo.name / _validate_task_id(task_id)).resolve()
4338
+ if repo.resolve() == candidate or repo.resolve() in candidate.parents:
4339
+ raise APError("Task worktrees must live outside the primary repository directory.")
4340
+ return candidate
3821
4341
 
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
- )
4342
+
4343
+ def _parse_null_config_entries(result, *, context: str) -> dict[str, list[str]]:
4344
+ if result.returncode == 1 and not result.stdout:
4345
+ return {}
4346
+ if result.returncode != 0:
4347
+ raise APError(
4348
+ f"Cannot inspect {context}: {result.stderr.strip() or result.stdout.strip()}"
3832
4349
  )
3833
- structure_check_status = "passed via light-gate" if _structure_gate_enabled(cfg) else "skipped (structure disabled)"
4350
+ entries: dict[str, list[str]] = {}
4351
+ for item in result.stdout.split("\0"):
4352
+ if not item:
4353
+ continue
4354
+ key, separator, value = item.partition("\n")
4355
+ if not separator or not key:
4356
+ raise APError(f"Malformed Git config entry while inspecting {context}: {item!r}")
4357
+ entries.setdefault(key, []).append(value)
4358
+ return entries
3834
4359
 
3835
- if args.require_jenkins:
3836
- cmd_verify_jenkins(argparse.Namespace(repo=str(repo)))
3837
4360
 
3838
- if args.require_matrix and mode != "verify":
3839
- cmd_check_matrix(argparse.Namespace(repo=str(repo)))
4361
+ def _gitmodules_urls(repo: Path) -> dict[str, str]:
4362
+ path = repo / ".gitmodules"
4363
+ if not path.exists():
4364
+ return {}
4365
+ result = run(
4366
+ [
4367
+ "git",
4368
+ "config",
4369
+ "--null",
4370
+ "--file",
4371
+ str(path),
4372
+ "--get-regexp",
4373
+ r"^submodule\..*\.url$",
4374
+ ],
4375
+ cwd=repo,
4376
+ check=False,
4377
+ )
4378
+ entries = _parse_null_config_entries(result, context=f"submodule URLs in {path}")
4379
+ return {key: values[-1] for key, values in entries.items() if values}
3840
4380
 
3841
- if mode == "dev":
3842
- dev_verification = args.verification or [
3843
- "light-gate only",
4381
+
4382
+ def _common_submodule_config(repo: Path) -> dict[str, list[str]]:
4383
+ result = run(
4384
+ [
4385
+ "git",
4386
+ "config",
4387
+ "--null",
4388
+ "--local",
4389
+ "--get-regexp",
4390
+ r"^submodule\.",
4391
+ ],
4392
+ cwd=repo,
4393
+ check=False,
4394
+ )
4395
+ return _parse_null_config_entries(result, context="shared submodule config")
4396
+
4397
+
4398
+ def _config_values(repo: Path, scope: str, key: str) -> list[str]:
4399
+ result = run(
4400
+ ["git", "config", "--null", scope, "--get-all", key],
4401
+ cwd=repo,
4402
+ check=False,
4403
+ )
4404
+ if result.returncode == 1 and not result.stdout:
4405
+ return []
4406
+ if result.returncode != 0:
4407
+ raise APError(
4408
+ f"Cannot inspect {scope} Git config {key!r}: "
4409
+ f"{result.stderr.strip() or result.stdout.strip()}"
4410
+ )
4411
+ return [value for value in result.stdout.split("\0") if value]
4412
+
4413
+
4414
+ def _replace_config_values(repo: Path, scope: str, key: str, values: list[str]) -> None:
4415
+ removed = run(
4416
+ ["git", "config", scope, "--unset-all", key],
4417
+ cwd=repo,
4418
+ check=False,
4419
+ )
4420
+ if removed.returncode not in {0, 5}:
4421
+ raise APError(
4422
+ f"Cannot clear {scope} Git config {key!r}: "
4423
+ f"{removed.stderr.strip() or removed.stdout.strip()}"
4424
+ )
4425
+ for value in values:
4426
+ run(["git", "config", scope, "--add", key, value], cwd=repo)
4427
+
4428
+
4429
+ def _enable_worktree_config(repo: Path) -> None:
4430
+ enabled = run(
4431
+ ["git", "config", "--local", "--bool", "--get", "extensions.worktreeConfig"],
4432
+ cwd=repo,
4433
+ check=False,
4434
+ )
4435
+ if enabled.returncode == 0 and enabled.stdout.strip() == "true":
4436
+ return
4437
+ run(["git", "config", "--local", "extensions.worktreeConfig", "true"], cwd=repo)
4438
+
4439
+
4440
+ def _require_worktree_config(repo: Path) -> None:
4441
+ enabled = run(
4442
+ ["git", "config", "--local", "--bool", "--get", "extensions.worktreeConfig"],
4443
+ cwd=repo,
4444
+ check=False,
4445
+ )
4446
+ if enabled.returncode != 0 or enabled.stdout.strip() != "true":
4447
+ raise APError(
4448
+ "Git extensions.worktreeConfig is not enabled. Return to the control checkout and "
4449
+ "recreate or recover the task; task commands will not write shared config."
4450
+ )
4451
+
4452
+
4453
+ def _default_submodule_remote_url(repo: Path) -> str:
4454
+ branch = _current_branch(repo)
4455
+ remote = ""
4456
+ if branch:
4457
+ configured = run(
4458
+ ["git", "config", "--get", f"branch.{branch}.remote"],
4459
+ cwd=repo,
4460
+ check=False,
4461
+ )
4462
+ if configured.returncode == 0:
4463
+ remote = configured.stdout.strip()
4464
+ if not remote:
4465
+ names = run(["git", "remote"], cwd=repo, check=False)
4466
+ if names.returncode != 0:
4467
+ raise APError(
4468
+ f"Cannot inspect Git remotes while resolving submodule URLs: {names.stderr.strip()}"
4469
+ )
4470
+ if "origin" in {line.strip() for line in names.stdout.splitlines()}:
4471
+ remote = "origin"
4472
+ if not remote or remote == ".":
4473
+ return str(repo.resolve())
4474
+ remote_url_result = run(
4475
+ ["git", "remote", "get-url", remote],
4476
+ cwd=repo,
4477
+ check=False,
4478
+ )
4479
+ if remote_url_result.returncode != 0 or not remote_url_result.stdout.strip():
4480
+ raise APError(f"Default submodule remote {remote!r} has no URL in {repo}.")
4481
+ return remote_url_result.stdout.strip()
4482
+
4483
+
4484
+ def _resolve_submodule_url(repo: Path, raw_url: str) -> str:
4485
+ if not raw_url.startswith(("./", "../")):
4486
+ return raw_url
4487
+ remote_url = _default_submodule_remote_url(repo)
4488
+ has_explicit_scheme = bool(
4489
+ re.match(r"^[A-Za-z][A-Za-z0-9+.-]*://", remote_url)
4490
+ or remote_url.startswith("file:")
4491
+ )
4492
+ scp_like = (
4493
+ None
4494
+ if has_explicit_scheme
4495
+ else re.fullmatch(r"([^/:]+(?:@[^/:]+)?):(.*)", remote_url)
4496
+ )
4497
+ if scp_like is not None:
4498
+ prefix, remote_path = scp_like.groups()
4499
+ return f"{prefix}:{posixpath.normpath(posixpath.join(remote_path, raw_url))}"
4500
+ parsed = urllib.parse.urlsplit(remote_url)
4501
+ if parsed.scheme:
4502
+ resolved_path = posixpath.normpath(posixpath.join(parsed.path, raw_url))
4503
+ if parsed.path.startswith("/") and not resolved_path.startswith("/"):
4504
+ resolved_path = f"/{resolved_path}"
4505
+ return urllib.parse.urlunsplit(
4506
+ (parsed.scheme, parsed.netloc, resolved_path, parsed.query, parsed.fragment)
4507
+ )
4508
+ remote_path = Path(remote_url).expanduser()
4509
+ if not remote_path.is_absolute():
4510
+ remote_path = (repo / remote_path).resolve()
4511
+ return str(Path(os.path.normpath(os.path.join(str(remote_path), raw_url))))
4512
+
4513
+
4514
+ def _seed_control_submodule_config(repo: Path) -> None:
4515
+ _enable_worktree_config(repo)
4516
+ for key, declared_url in _gitmodules_urls(repo).items():
4517
+ if _config_values(repo, "--worktree", key):
4518
+ continue
4519
+ effective = _config_values(repo, "--local", key)
4520
+ value = effective[-1] if effective else _resolve_submodule_url(repo, declared_url)
4521
+ _replace_config_values(repo, "--worktree", key, [value])
4522
+
4523
+
4524
+ def _sync_task_submodule_config(
4525
+ control_repo: Path,
4526
+ worktree: Path,
4527
+ manifest: dict,
4528
+ *,
4529
+ initial: bool,
4530
+ check_common: bool,
4531
+ ) -> None:
4532
+ _require_worktree_config(control_repo)
4533
+ urls = _gitmodules_urls(worktree)
4534
+ control_urls = _gitmodules_urls(control_repo)
4535
+ previous_raw = manifest.get("submodule_config_claims")
4536
+ previous_claims = (
4537
+ {str(key): str(value) for key, value in previous_raw.items()}
4538
+ if isinstance(previous_raw, dict)
4539
+ else {}
4540
+ )
4541
+ claims: dict[str, str] = {}
4542
+ for key, declared_url in urls.items():
4543
+ value = _resolve_submodule_url(worktree, declared_url)
4544
+ if initial and control_urls.get(key) == declared_url:
4545
+ control_values = _config_values(control_repo, "--worktree", key)
4546
+ if control_values:
4547
+ value = control_values[-1]
4548
+ _replace_config_values(worktree, "--worktree", key, [value])
4549
+ claims[key] = value
4550
+ active_key = f"{key[:-len('.url')]}.active"
4551
+ _replace_config_values(worktree, "--worktree", active_key, ["true"])
4552
+ claims[active_key] = "true"
4553
+
4554
+ for stale_key in sorted(set(previous_claims) - set(claims)):
4555
+ _replace_config_values(worktree, "--worktree", stale_key, [])
4556
+
4557
+ manifest["submodule_config_claims"] = claims
4558
+ if not check_common:
4559
+ return
4560
+ baseline = manifest.get("common_submodule_config")
4561
+ baseline = baseline if isinstance(baseline, dict) else {}
4562
+ candidate_values: dict[str, set[str]] = {}
4563
+ for source in (previous_claims, claims):
4564
+ for key, value in source.items():
4565
+ candidate_values.setdefault(key, set()).add(value)
4566
+ for key, candidates in candidate_values.items():
4567
+ current = _config_values(control_repo, "--local", key)
4568
+ before_raw = baseline.get(key) or []
4569
+ before = [str(value) for value in before_raw] if isinstance(before_raw, list) else []
4570
+ if current != before:
4571
+ raise APError(
4572
+ "Shared submodule config changed after task-start; no common config was modified. "
4573
+ f"Resolve ownership before continuing: key={key!r}, before={before!r}, "
4574
+ f"current={current!r}, task_values={sorted(candidates)!r}"
4575
+ )
4576
+
4577
+
4578
+ def _git_ref_exists(repo: Path, ref: str) -> bool:
4579
+ return run(["git", "show-ref", "--verify", "--quiet", ref], cwd=repo, check=False).returncode == 0
4580
+
4581
+
4582
+ def _git_status(repo: Path) -> str:
4583
+ result = run(
4584
+ ["git", "status", "--porcelain=v1", "--untracked-files=all"],
4585
+ cwd=repo,
4586
+ check=False,
4587
+ )
4588
+ if result.returncode != 0:
4589
+ raise APError(f"Cannot inspect Git status for {repo}: {result.stderr.strip()}")
4590
+ return result.stdout.strip()
4591
+
4592
+
4593
+ def _ignored_paths(repo: Path) -> list[str]:
4594
+ result = run(
4595
+ [
4596
+ "git",
4597
+ "ls-files",
4598
+ "--others",
4599
+ "--ignored",
4600
+ "--exclude-standard",
4601
+ "--directory",
4602
+ "-z",
4603
+ ],
4604
+ cwd=repo,
4605
+ check=False,
4606
+ )
4607
+ if result.returncode != 0:
4608
+ raise APError(
4609
+ f"Cannot inspect ignored files for {repo}: "
4610
+ f"{result.stderr.strip() or result.stdout.strip()}"
4611
+ )
4612
+ return sorted(
4613
+ path.rstrip("/")
4614
+ for path in result.stdout.split("\0")
4615
+ if path.rstrip("/")
4616
+ )
4617
+
4618
+
4619
+ def _pattern_may_match_descendant(directory: str, pattern_ref: str) -> bool:
4620
+ directory = directory.replace("\\", "/").lstrip("./").rstrip("/")
4621
+ pattern = _text(pattern_ref).replace("\\", "/").lstrip("./")
4622
+ if not directory or not pattern:
4623
+ return False
4624
+ if "/" not in pattern or pattern.startswith("**/"):
4625
+ return True
4626
+ wildcard = min(
4627
+ (index for token in ("*", "?", "[") if (index := pattern.find(token)) >= 0),
4628
+ default=len(pattern),
4629
+ )
4630
+ literal_prefix = pattern[:wildcard].rstrip("/")
4631
+ if not literal_prefix:
4632
+ return True
4633
+ return (
4634
+ literal_prefix == directory
4635
+ or literal_prefix.startswith(f"{directory}/")
4636
+ or directory.startswith(literal_prefix)
4637
+ )
4638
+
4639
+
4640
+ def _expanded_ignored_paths(repo: Path, directory: str) -> list[str]:
4641
+ result = run(
4642
+ [
4643
+ "git",
4644
+ "ls-files",
4645
+ "--others",
4646
+ "--ignored",
4647
+ "--exclude-standard",
4648
+ "-z",
4649
+ "--",
4650
+ directory,
4651
+ ],
4652
+ cwd=repo,
4653
+ check=False,
4654
+ )
4655
+ if result.returncode != 0:
4656
+ raise APError(
4657
+ f"Cannot inspect ignored directory {directory!r}: "
4658
+ f"{result.stderr.strip() or result.stdout.strip()}"
4659
+ )
4660
+ return sorted(path for path in result.stdout.split("\0") if path)
4661
+
4662
+
4663
+ def _unsafe_ignored_paths(repo: Path, extra_patterns: list[str]) -> list[str]:
4664
+ patterns = list(_DEFAULT_DISPOSABLE_IGNORED_PATTERNS) + [
4665
+ _text(item) for item in extra_patterns if _text(item)
4666
+ ]
4667
+ unsafe: list[str] = []
4668
+ for path in _ignored_paths(repo):
4669
+ # Git deliberately collapses ignored directories (for example `.local/`).
4670
+ # A declared disposable subtree is safe without walking it; an ambiguous
4671
+ # parent is expanded so an allowed child cannot hide an unknown sibling.
4672
+ if _path_matches(path, patterns):
4673
+ continue
4674
+ if not any(_pattern_may_match_descendant(path, pattern) for pattern in patterns):
4675
+ unsafe.append(path)
4676
+ continue
4677
+ expanded = _expanded_ignored_paths(repo, path)
4678
+ unsafe.extend(item for item in expanded if not _path_matches(item, patterns))
4679
+ if not expanded:
4680
+ unsafe.append(path)
4681
+ return sorted(set(unsafe))
4682
+
4683
+
4684
+ def _initialized_submodules(repo: Path) -> list[tuple[str, Path]]:
4685
+ result = run(
4686
+ [
4687
+ "git",
4688
+ "submodule",
4689
+ "foreach",
4690
+ "--quiet",
4691
+ "--recursive",
4692
+ 'printf "%s\\0" "$displaypath"',
4693
+ ],
4694
+ cwd=repo,
4695
+ check=False,
4696
+ )
4697
+ if result.returncode != 0:
4698
+ raise APError(
4699
+ f"Cannot inspect initialized submodules for {repo}: "
4700
+ f"{result.stderr.strip() or result.stdout.strip()}"
4701
+ )
4702
+ root = repo.resolve()
4703
+ submodules: list[tuple[str, Path]] = []
4704
+ for raw_path in result.stdout.split("\0"):
4705
+ rel = raw_path.replace("\\", "/").strip("/")
4706
+ if rel.startswith("./"):
4707
+ rel = rel[2:]
4708
+ if not rel:
4709
+ continue
4710
+ candidate = (root / rel).resolve()
4711
+ if root not in candidate.parents:
4712
+ raise APError(f"Initialized submodule resolves outside the task worktree: {rel}")
4713
+ submodules.append((rel, candidate))
4714
+ return sorted(set(submodules), key=lambda item: item[0])
4715
+
4716
+
4717
+ def _stored_submodule_gitdirs(worktree: Path) -> tuple[set[Path], list[Path]]:
4718
+ modules_root = _git_dir(worktree) / "modules"
4719
+ if not modules_root.exists():
4720
+ return set(), []
4721
+ repositories: set[Path] = set()
4722
+ unknown: list[Path] = []
4723
+
4724
+ def scan_container(container: Path) -> None:
4725
+ try:
4726
+ children = sorted(container.iterdir(), key=lambda item: item.name)
4727
+ except OSError:
4728
+ unknown.append(container)
4729
+ return
4730
+ for child in children:
4731
+ if child.is_symlink():
4732
+ unknown.append(child)
4733
+ continue
4734
+ if not child.is_dir():
4735
+ unknown.append(child)
4736
+ continue
4737
+ is_git_dir = (
4738
+ (child / "HEAD").is_file()
4739
+ and (child / "config").is_file()
4740
+ and (child / "objects").is_dir()
4741
+ )
4742
+ if is_git_dir:
4743
+ repositories.add(child.resolve())
4744
+ nested_modules = child / "modules"
4745
+ if nested_modules.exists():
4746
+ scan_container(nested_modules)
4747
+ continue
4748
+ scan_container(child)
4749
+
4750
+ scan_container(modules_root)
4751
+ return repositories, unknown
4752
+
4753
+
4754
+ def _submodule_disposable_patterns(submodule_path: str, extra_patterns: list[str]) -> list[str]:
4755
+ prefix = submodule_path.replace("\\", "/").strip("/")
4756
+ patterns = [_text(item) for item in extra_patterns if _text(item)]
4757
+ for item in list(patterns):
4758
+ normalized = item.replace("\\", "/").lstrip("./").strip("/")
4759
+ if normalized == prefix:
4760
+ patterns.append("**")
4761
+ elif normalized.startswith(f"{prefix}/"):
4762
+ patterns.append(normalized[len(prefix) + 1 :])
4763
+ return patterns
4764
+
4765
+
4766
+ def _submodule_local_only_commits(submodule: Path) -> list[str]:
4767
+ result = run(
4768
+ [
4769
+ "git",
4770
+ "rev-list",
4771
+ "--max-count=20",
4772
+ "--all",
4773
+ "--reflog",
4774
+ "--not",
4775
+ "--remotes",
4776
+ ],
4777
+ cwd=submodule,
4778
+ check=False,
4779
+ )
4780
+ if result.returncode != 0:
4781
+ raise APError(
4782
+ f"Cannot inspect local-only submodule history for {submodule}: "
4783
+ f"{result.stderr.strip() or result.stdout.strip()}"
4784
+ )
4785
+ return [line.strip() for line in result.stdout.splitlines() if line.strip()]
4786
+
4787
+
4788
+ def _submodule_unmirrored_branches(submodule: Path) -> list[str]:
4789
+ result = run(
4790
+ [
4791
+ "git",
4792
+ "for-each-ref",
4793
+ "--format=%(refname)%09%(objectname)",
4794
+ "refs/heads",
4795
+ "refs/remotes",
4796
+ ],
4797
+ cwd=submodule,
4798
+ check=False,
4799
+ )
4800
+ if result.returncode != 0:
4801
+ raise APError(
4802
+ f"Cannot inspect submodule branches for {submodule}: "
4803
+ f"{result.stderr.strip() or result.stdout.strip()}"
4804
+ )
4805
+ local: dict[str, str] = {}
4806
+ remote: dict[str, set[str]] = {}
4807
+ for line in result.stdout.splitlines():
4808
+ if "\t" not in line:
4809
+ continue
4810
+ ref, commit = line.split("\t", 1)
4811
+ if ref.startswith("refs/heads/"):
4812
+ local[ref[len("refs/heads/") :]] = commit
4813
+ continue
4814
+ if not ref.startswith("refs/remotes/"):
4815
+ continue
4816
+ remote_and_branch = ref[len("refs/remotes/") :]
4817
+ if "/" not in remote_and_branch:
4818
+ continue
4819
+ _, branch = remote_and_branch.split("/", 1)
4820
+ remote.setdefault(branch, set()).add(commit)
4821
+ return sorted(
4822
+ branch
4823
+ for branch, commit in local.items()
4824
+ if commit not in remote.get(branch, set())
4825
+ )
4826
+
4827
+
4828
+ def _submodule_other_worktrees(submodule: Path) -> list[str]:
4829
+ result = run(
4830
+ ["git", "worktree", "list", "--porcelain", "-z"],
4831
+ cwd=submodule,
4832
+ check=False,
4833
+ )
4834
+ if result.returncode != 0:
4835
+ raise APError(
4836
+ f"Cannot inspect linked submodule worktrees for {submodule}: "
4837
+ f"{result.stderr.strip() or result.stdout.strip()}"
4838
+ )
4839
+ current_paths = {submodule.resolve(), _git_dir(submodule).resolve()}
4840
+ others: list[str] = []
4841
+ for field in result.stdout.split("\0"):
4842
+ if not field.startswith("worktree "):
4843
+ continue
4844
+ raw_path = field[len("worktree ") :]
4845
+ candidate = Path(raw_path).resolve()
4846
+ if candidate not in current_paths:
4847
+ others.append(raw_path)
4848
+ return sorted(set(others))
4849
+
4850
+
4851
+ def _refresh_submodule_remotes(submodule: Path) -> None:
4852
+ remotes = run(["git", "remote"], cwd=submodule, check=False)
4853
+ if remotes.returncode != 0:
4854
+ raise APError(
4855
+ f"Cannot inspect submodule remotes for {submodule}: "
4856
+ f"{remotes.stderr.strip() or remotes.stdout.strip()}"
4857
+ )
4858
+ names = [line.strip() for line in remotes.stdout.splitlines() if line.strip()]
4859
+ if not names:
4860
+ raise APError(
4861
+ f"Initialized submodule has no remote to verify before destructive cleanup: {submodule}"
4862
+ )
4863
+ for remote in names:
4864
+ fetched = run(
4865
+ [
4866
+ "git",
4867
+ "-c",
4868
+ "credential.interactive=never",
4869
+ "fetch",
4870
+ "--prune",
4871
+ "--no-tags",
4872
+ remote,
4873
+ ],
4874
+ cwd=submodule,
4875
+ check=False,
4876
+ )
4877
+ if fetched.returncode != 0:
4878
+ raise APError(
4879
+ f"Cannot refresh submodule remote {remote!r}; cleanup was blocked so Git history remains local.\n"
4880
+ f"{fetched.stdout}\n{fetched.stderr}"
4881
+ )
4882
+
4883
+
4884
+ def _submodule_refs(submodule: Path) -> dict[str, str]:
4885
+ result = run(
4886
+ ["git", "for-each-ref", "--format=%(refname)%09%(objectname)", "refs"],
4887
+ cwd=submodule,
4888
+ check=False,
4889
+ )
4890
+ if result.returncode != 0:
4891
+ raise APError(
4892
+ f"Cannot snapshot submodule refs for {submodule}: "
4893
+ f"{result.stderr.strip() or result.stdout.strip()}"
4894
+ )
4895
+ refs: dict[str, str] = {}
4896
+ for line in result.stdout.splitlines():
4897
+ if "\t" not in line:
4898
+ continue
4899
+ ref, object_id = line.split("\t", 1)
4900
+ if ref.startswith("refs/") and object_id:
4901
+ refs[ref] = object_id
4902
+ return refs
4903
+
4904
+
4905
+ def _submodule_reflog_commits(submodule: Path) -> list[str]:
4906
+ result = run(
4907
+ ["git", "reflog", "show", "--all", "--format=%H"],
4908
+ cwd=submodule,
4909
+ check=False,
4910
+ )
4911
+ if result.returncode != 0:
4912
+ raise APError(
4913
+ f"Cannot snapshot submodule reflogs for {submodule}: "
4914
+ f"{result.stderr.strip() or result.stdout.strip()}"
4915
+ )
4916
+ commits = {line.strip() for line in result.stdout.splitlines() if line.strip()}
4917
+ commits.add(_resolve_commit(submodule, "HEAD"))
4918
+ return sorted(commits)
4919
+
4920
+
4921
+ def _archive_submodule_git_state(
4922
+ control_repo: Path,
4923
+ manifest: dict,
4924
+ submodule_path: str,
4925
+ submodule: Path,
4926
+ ) -> dict:
4927
+ task_uuid = _text(manifest.get("task_uuid")) or uuid.uuid4().hex
4928
+ object_format_result = run(
4929
+ ["git", "rev-parse", "--show-object-format"],
4930
+ cwd=submodule,
4931
+ check=False,
4932
+ )
4933
+ if object_format_result.returncode != 0:
4934
+ raise APError(
4935
+ f"Cannot inspect submodule object format for {submodule_path}: "
4936
+ f"{object_format_result.stderr.strip() or object_format_result.stdout.strip()}"
4937
+ )
4938
+ object_format = object_format_result.stdout.strip() or "sha1"
4939
+ recovery_root = _task_state_root(control_repo) / "submodule-recovery"
4940
+ repo_key = hashlib.sha256(submodule_path.encode("utf-8")).hexdigest()[:20]
4941
+ recovery_repo = recovery_root / "repos" / f"{repo_key}-{object_format}.git"
4942
+ if not recovery_repo.exists():
4943
+ recovery_repo.parent.mkdir(parents=True, exist_ok=True)
4944
+ init_command = ["git", "init", "--bare", "--quiet"]
4945
+ if object_format != "sha1":
4946
+ init_command.append(f"--object-format={object_format}")
4947
+ init_command.append(str(recovery_repo))
4948
+ run(init_command, cwd=control_repo)
4949
+ bare = run(
4950
+ ["git", f"--git-dir={recovery_repo}", "rev-parse", "--is-bare-repository"],
4951
+ cwd=control_repo,
4952
+ check=False,
4953
+ )
4954
+ recovery_format = run(
4955
+ ["git", f"--git-dir={recovery_repo}", "rev-parse", "--show-object-format"],
4956
+ cwd=control_repo,
4957
+ check=False,
4958
+ )
4959
+ if (
4960
+ bare.returncode != 0
4961
+ or bare.stdout.strip() != "true"
4962
+ or recovery_format.returncode != 0
4963
+ or recovery_format.stdout.strip() != object_format
4964
+ ):
4965
+ raise APError(f"Invalid submodule recovery repository: {recovery_repo}")
4966
+
4967
+ original_refs = _submodule_refs(submodule)
4968
+ reflog_commits = _submodule_reflog_commits(submodule)
4969
+ snapshot_id = uuid.uuid4().hex
4970
+ temporary_prefix = f"refs/auto-coding-recovery/{snapshot_id}/reflog"
4971
+ temporary_refs: dict[str, str] = {}
4972
+ try:
4973
+ for commit in reflog_commits:
4974
+ temporary_ref = f"{temporary_prefix}/{commit}"
4975
+ run(
4976
+ ["git", "update-ref", temporary_ref, commit, "0" * len(commit)],
4977
+ cwd=submodule,
4978
+ )
4979
+ temporary_refs[temporary_ref] = commit
4980
+ source_refs = _submodule_refs(submodule)
4981
+ destination_prefix = f"refs/tasks/{task_uuid}/{snapshot_id}"
4982
+ run(
4983
+ [
4984
+ "git",
4985
+ f"--git-dir={recovery_repo}",
4986
+ "fetch",
4987
+ "--no-tags",
4988
+ "--no-write-fetch-head",
4989
+ str(submodule),
4990
+ f"+refs/*:{destination_prefix}/*",
4991
+ ],
4992
+ cwd=control_repo,
4993
+ )
4994
+ archived_refs = run(
4995
+ [
4996
+ "git",
4997
+ f"--git-dir={recovery_repo}",
4998
+ "for-each-ref",
4999
+ "--format=%(refname)%09%(objectname)",
5000
+ destination_prefix,
5001
+ ],
5002
+ cwd=control_repo,
5003
+ )
5004
+ archived: dict[str, str] = {}
5005
+ for line in archived_refs.stdout.splitlines():
5006
+ if "\t" in line:
5007
+ ref, object_id = line.split("\t", 1)
5008
+ archived[ref] = object_id
5009
+ missing: list[str] = []
5010
+ for source_ref, object_id in source_refs.items():
5011
+ destination = f"{destination_prefix}/{source_ref[len('refs/'):]}"
5012
+ if archived.get(destination) != object_id:
5013
+ missing.append(source_ref)
5014
+ if missing:
5015
+ raise APError(
5016
+ f"Submodule recovery snapshot verification failed for {submodule_path}:\n- "
5017
+ + "\n- ".join(missing)
5018
+ )
5019
+ metadata_path = (
5020
+ recovery_root
5021
+ / "snapshots"
5022
+ / task_uuid
5023
+ / f"{repo_key}-{snapshot_id}.json"
5024
+ )
5025
+ _write_json_object(
5026
+ metadata_path,
5027
+ {
5028
+ "schema": 1,
5029
+ "created_at": _now_iso(),
5030
+ "task_id": _text(manifest.get("task_id")),
5031
+ "task_uuid": task_uuid,
5032
+ "submodule_path": submodule_path,
5033
+ "source_git_dir": str(_git_dir(submodule)),
5034
+ "recovery_repo": str(recovery_repo),
5035
+ "destination_prefix": destination_prefix,
5036
+ "refs": original_refs,
5037
+ "reflog_commits": reflog_commits,
5038
+ },
5039
+ )
5040
+ finally:
5041
+ for temporary_ref, commit in temporary_refs.items():
5042
+ run(
5043
+ ["git", "update-ref", "-d", temporary_ref, commit],
5044
+ cwd=submodule,
5045
+ check=False,
5046
+ )
5047
+
5048
+ print(
5049
+ f"[task-cleanup] submodule recovery snapshot: {submodule_path} -> "
5050
+ f"{recovery_repo} ({destination_prefix})"
5051
+ )
5052
+ return {
5053
+ "submodule_path": submodule_path,
5054
+ "recovery_repo": str(recovery_repo),
5055
+ "destination_prefix": destination_prefix,
5056
+ "metadata_path": str(metadata_path),
5057
+ }
5058
+
5059
+
5060
+ def _prepare_submodules_for_removal(
5061
+ control_repo: Path,
5062
+ manifest: dict,
5063
+ worktree: Path,
5064
+ extra_disposable_patterns: list[str],
5065
+ ) -> bool:
5066
+ submodules = _initialized_submodules(worktree)
5067
+ stored_gitdirs, unknown_store_paths = _stored_submodule_gitdirs(worktree)
5068
+ initialized_gitdirs = {_git_dir(submodule).resolve() for _, submodule in submodules}
5069
+ residual_gitdirs = sorted(stored_gitdirs - initialized_gitdirs)
5070
+ if residual_gitdirs or unknown_store_paths:
5071
+ details = [
5072
+ *(f"residual module Git directory: {path}" for path in residual_gitdirs),
5073
+ *(f"unrecognized module-store path: {path}" for path in unknown_store_paths[:20]),
5074
+ ]
5075
+ raise APError(
5076
+ "Task worktree has deinitialized, removed, or unrecognized submodule Git state that "
5077
+ "cannot be proven safe for forced removal. Reinitialize the submodule and rerun cleanup, "
5078
+ "or move/recover its Git data first; the task worktree and branch were retained:\n- "
5079
+ + "\n- ".join(details)
5080
+ )
5081
+ if not submodules:
5082
+ return False
5083
+ problems: list[str] = []
5084
+ for rel, submodule in submodules:
5085
+ other_worktrees = _submodule_other_worktrees(submodule)
5086
+ if other_worktrees:
5087
+ problems.append(
5088
+ f"{rel}: additional linked worktrees must be handled before cleanup:\n"
5089
+ + "\n".join(f" - {path}" for path in other_worktrees)
5090
+ )
5091
+ continue
5092
+ try:
5093
+ _refresh_submodule_remotes(submodule)
5094
+ except APError as exc:
5095
+ problems.append(f"{rel}: {exc}")
5096
+ continue
5097
+ local_only_commits = _submodule_local_only_commits(submodule)
5098
+ if local_only_commits:
5099
+ problems.append(
5100
+ f"{rel}: local refs/reflogs contain commits not reachable from remote-tracking refs:\n"
5101
+ + "\n".join(f" - {commit}" for commit in local_only_commits)
5102
+ )
5103
+ continue
5104
+ unmirrored_branches = _submodule_unmirrored_branches(submodule)
5105
+ if unmirrored_branches:
5106
+ problems.append(
5107
+ f"{rel}: local branches have no same-name remote-tracking ref at the same commit:\n"
5108
+ + "\n".join(f" - {branch}" for branch in unmirrored_branches)
5109
+ )
5110
+ continue
5111
+ dirty = _git_status(submodule)
5112
+ if dirty:
5113
+ problems.append(f"{rel}: tracked or untracked changes:\n{dirty}")
5114
+ continue
5115
+ conflicts = run(["git", "ls-files", "-u"], cwd=submodule, check=False)
5116
+ if conflicts.returncode != 0:
5117
+ raise APError(
5118
+ f"Cannot inspect submodule conflicts for {rel}: "
5119
+ f"{conflicts.stderr.strip() or conflicts.stdout.strip()}"
5120
+ )
5121
+ if conflicts.stdout.strip():
5122
+ problems.append(f"{rel}: unresolved conflicts")
5123
+ continue
5124
+ unsafe_ignored = _unsafe_ignored_paths(
5125
+ submodule,
5126
+ _submodule_disposable_patterns(rel, extra_disposable_patterns),
5127
+ )
5128
+ if unsafe_ignored:
5129
+ rendered = "\n".join(f" - {rel}/{path}" for path in unsafe_ignored)
5130
+ problems.append(f"{rel}: ignored files are not declared disposable:\n{rendered}")
5131
+ if problems:
5132
+ raise APError(
5133
+ "Task submodules contain data that cannot be discarded; refusing cleanup:\n- "
5134
+ + "\n- ".join(problems)
5135
+ + "\nPush or move local submodule history, commit/move working data, or add only "
5136
+ "disposable cache paths to concurrency.disposable_ignored; then rerun task-finish."
5137
+ )
5138
+
5139
+ for rel, submodule in submodules:
5140
+ _archive_submodule_git_state(control_repo, manifest, rel, submodule)
5141
+ return True
5142
+
5143
+
5144
+ def _task_doc_path(repo: Path, cfg: dict, manifest: dict, kind: str) -> Path:
5145
+ docs_cfg = cfg.get("docs") or {}
5146
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
5147
+ if kind == "active":
5148
+ rel = _text(docs_cfg.get("active_task_dir")) or "docs/tasks/active"
5149
+ suffix = ".md"
5150
+ elif kind == "closure":
5151
+ rel = _text(docs_cfg.get("task_closure_dir")) or "docs/tasks/closures"
5152
+ suffix = ".md"
5153
+ elif kind == "evidence":
5154
+ rel = _text(docs_cfg.get("task_evidence_dir")) or "docs/tasks/evidence"
5155
+ suffix = ".jsonl"
5156
+ else:
5157
+ raise APError(f"Unknown task document kind: {kind}")
5158
+ return repo / rel / f"{task_id}{suffix}"
5159
+
5160
+
5161
+ def _create_active_task_doc(repo: Path, cfg: dict, manifest: dict) -> None:
5162
+ path = _task_doc_path(repo, cfg, manifest, "active")
5163
+ if path.exists():
5164
+ return
5165
+ path.parent.mkdir(parents=True, exist_ok=True)
5166
+ path.write_text(
5167
+ "\n".join(
5168
+ [
5169
+ f"# Task {manifest['task_id']}",
5170
+ "",
5171
+ "- Status: Active",
5172
+ f"- Base: `{manifest['base_ref']}` (`{manifest['base_sha'][:12]}`)",
5173
+ f"- Target: `{manifest['remote']}/{manifest['target_branch']}`",
5174
+ f"- Branch: `{manifest['task_branch']}`",
5175
+ "- Scope: TODO",
5176
+ "- Acceptance: TODO",
5177
+ "",
5178
+ ]
5179
+ ),
5180
+ encoding="utf-8",
5181
+ )
5182
+
5183
+
5184
+ def _update_active_task_status(repo: Path, cfg: dict, manifest: dict, status: str) -> None:
5185
+ path = _task_doc_path(repo, cfg, manifest, "active")
5186
+ if not path.exists():
5187
+ return
5188
+ text = path.read_text(encoding="utf-8")
5189
+ updated, count = re.subn(r"(?m)^- Status: .*?$", f"- Status: {status}", text, count=1)
5190
+ if count == 0:
5191
+ updated = text.rstrip() + f"\n- Status: {status}\n"
5192
+ if updated != text:
5193
+ path.write_text(updated, encoding="utf-8")
5194
+
5195
+
5196
+ def _rollback_fresh_task_worktree(
5197
+ repo: Path,
5198
+ worktree: Path,
5199
+ task_branch: str,
5200
+ expected_tip: str,
5201
+ ) -> bool:
5202
+ run(["git", "worktree", "unlock", str(worktree)], cwd=repo, check=False)
5203
+ removed = run(
5204
+ ["git", "worktree", "remove", "--force", str(worktree)],
5205
+ cwd=repo,
5206
+ check=False,
5207
+ )
5208
+ if removed.returncode == 0:
5209
+ run(
5210
+ ["git", "update-ref", "-d", f"refs/heads/{task_branch}", expected_tip],
5211
+ cwd=repo,
5212
+ check=False,
5213
+ )
5214
+ return removed.returncode == 0
5215
+
5216
+
5217
+ def cmd_task_start(args: argparse.Namespace) -> None:
5218
+ repo = Path(args.repo).resolve()
5219
+ ensure_git_repo(repo)
5220
+ cfg = _load_cfg(repo)
5221
+ task_id = _validate_task_id(args.task_id)
5222
+ _require_control_checkout(repo)
5223
+ remote, target_branch, base_ref = _task_remote_and_target(cfg, args)
5224
+ concurrency_cfg = _concurrency_cfg(cfg)
5225
+ timeout_s = float(concurrency_cfg.get("lock_timeout_sec") or 30)
5226
+
5227
+ with (
5228
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
5229
+ _repo_lock(repo, "task-registry", timeout_s=timeout_s),
5230
+ ):
5231
+ if _read_json_object(_task_registry_path(repo, task_id)):
5232
+ raise APError(f"Task is already registered: {task_id}")
5233
+
5234
+ common_submodule_config = _common_submodule_config(repo)
5235
+ _seed_control_submodule_config(repo)
5236
+ task_uuid = uuid.uuid4().hex
5237
+
5238
+ if not getattr(args, "no_fetch", False) and base_ref.startswith(f"{remote}/"):
5239
+ _fetch_target(repo, remote, target_branch)
5240
+ base_sha = _resolve_commit(repo, base_ref)
5241
+ prefix = _task_branch_prefix(cfg)
5242
+ task_branch = f"{prefix}{task_id}"
5243
+ check_ref = run(["git", "check-ref-format", "--branch", task_branch], cwd=repo, check=False)
5244
+ if check_ref.returncode != 0:
5245
+ raise APError(f"Invalid task branch name: {task_branch}")
5246
+ if _git_ref_exists(repo, f"refs/heads/{task_branch}"):
5247
+ raise APError(f"Task branch already exists: {task_branch}. Run task-prune or choose another ID.")
5248
+ worktree = _task_worktree_path(repo, cfg, task_id, _text(getattr(args, "worktree_root", "")))
5249
+ if worktree.exists():
5250
+ raise APError(f"Task worktree path already exists: {worktree}")
5251
+ worktree.parent.mkdir(parents=True, exist_ok=True)
5252
+ run(
5253
+ [
5254
+ "git",
5255
+ "worktree",
5256
+ "add",
5257
+ "--no-track",
5258
+ "-b",
5259
+ task_branch,
5260
+ str(worktree),
5261
+ base_sha,
5262
+ ],
5263
+ cwd=repo,
5264
+ )
5265
+ run(
5266
+ ["git", "worktree", "lock", "--reason", f"auto-coding task {task_id}", str(worktree)],
5267
+ cwd=repo,
5268
+ )
5269
+ manifest = {
5270
+ "schema": 1,
5271
+ "task_id": task_id,
5272
+ "task_uuid": task_uuid,
5273
+ "owner": _text(getattr(args, "owner", "")) or os.environ.get("CODEX_THREAD_ID", "") or f"{socket.gethostname()}:{os.getpid()}",
5274
+ "base_ref": base_ref,
5275
+ "base_sha": base_sha,
5276
+ "remote": remote,
5277
+ "target_branch": target_branch,
5278
+ "task_branch": task_branch,
5279
+ "worktree_path": str(worktree.resolve()),
5280
+ "control_worktree_path": str(repo.resolve()),
5281
+ "cleanup_policy": _cleanup_policy(cfg),
5282
+ "state": "active",
5283
+ "created_at": _now_iso(),
5284
+ "initial_untracked": [],
5285
+ "claimed_paths": [],
5286
+ "remote_task_tip": "",
5287
+ "common_submodule_config": common_submodule_config,
5288
+ }
5289
+ try:
5290
+ _sync_task_submodule_config(
5291
+ repo,
5292
+ worktree,
5293
+ manifest,
5294
+ initial=True,
5295
+ check_common=True,
5296
+ )
5297
+ except APError as exc:
5298
+ rolled_back = _rollback_fresh_task_worktree(
5299
+ repo,
5300
+ worktree,
5301
+ task_branch,
5302
+ base_sha,
5303
+ )
5304
+ raise APError(
5305
+ f"Could not isolate task-local submodule config; fresh worktree rollback="
5306
+ f"{str(rolled_back).lower()}.\n{exc}"
5307
+ ) from exc
5308
+ initial_status = _git_status(worktree)
5309
+ initial_head = _resolve_commit(worktree, "HEAD")
5310
+ initial_branch = _current_branch(worktree)
5311
+ if initial_status or initial_head != base_sha or initial_branch != task_branch:
5312
+ rolled_back = _rollback_fresh_task_worktree(
5313
+ repo,
5314
+ worktree,
5315
+ task_branch,
5316
+ initial_head,
5317
+ )
5318
+ raise APError(
5319
+ "A checkout hook changed the new task worktree. The fresh worktree and branch were rolled "
5320
+ f"back={str(rolled_back).lower()} so hook-created content cannot be committed accidentally. "
5321
+ f"branch={initial_branch or '(detached)'} head={initial_head}\n{initial_status}"
5322
+ )
5323
+ _save_task_manifest(repo, manifest)
5324
+ _create_active_task_doc(worktree, cfg, manifest)
5325
+
5326
+ print(f"[task-start] task={task_id}")
5327
+ print(f"[task-start] branch={task_branch}")
5328
+ print(f"[task-start] base={base_ref}@{base_sha}")
5329
+ print(f"[task-start] worktree={worktree}")
5330
+ print(f"[task-start] next: cd {worktree}")
5331
+
5332
+
5333
+ def _task_status_payload(repo: Path, manifest: dict) -> dict:
5334
+ worktree = Path(_text(manifest.get("worktree_path")))
5335
+ task_branch = _text(manifest.get("task_branch"))
5336
+ local_ref = f"refs/heads/{task_branch}"
5337
+ tip = _resolve_commit(repo, local_ref) if _git_ref_exists(repo, local_ref) else ""
5338
+ remote = _text(manifest.get("remote")) or "origin"
5339
+ target = _text(manifest.get("target_branch"))
5340
+ remote_ref = f"refs/remotes/{remote}/{target}"
5341
+ merged = bool(
5342
+ tip
5343
+ and _git_ref_exists(repo, remote_ref)
5344
+ and run(["git", "merge-base", "--is-ancestor", tip, remote_ref], cwd=repo, check=False).returncode == 0
5345
+ )
5346
+ return {
5347
+ **manifest,
5348
+ "worktree_exists": worktree.exists(),
5349
+ "dirty": bool(_git_status(worktree)) if worktree.exists() else False,
5350
+ "local_branch_exists": bool(tip),
5351
+ "tip": tip,
5352
+ "merged_into_target": merged,
5353
+ }
5354
+
5355
+
5356
+ def cmd_task_status(args: argparse.Namespace) -> None:
5357
+ repo = Path(args.repo).resolve()
5358
+ ensure_git_repo(repo)
5359
+ task_id = _text(getattr(args, "task_id", ""))
5360
+ manifests: list[dict] = []
5361
+ if task_id:
5362
+ manifests.append(_load_task_manifest(repo, _validate_task_id(task_id)))
5363
+ else:
5364
+ registry = _task_state_root(repo) / "tasks"
5365
+ for path in sorted(registry.glob("*.json")) if registry.exists() else []:
5366
+ payload = _read_json_object(path)
5367
+ if payload:
5368
+ manifests.append(_validate_task_manifest(repo, payload))
5369
+ statuses = [_task_status_payload(repo, manifest) for manifest in manifests]
5370
+ if args.json:
5371
+ print(json.dumps({"tasks": statuses}, ensure_ascii=False, indent=2))
5372
+ return
5373
+ if not statuses:
5374
+ print("[task-status] no registered tasks")
5375
+ return
5376
+ for status in statuses:
5377
+ print(
5378
+ f"[task-status] task={status['task_id']} state={status.get('state')} "
5379
+ f"branch={status.get('task_branch')} worktree={status.get('worktree_path')} "
5380
+ f"dirty={str(status['dirty']).lower()} merged={str(status['merged_into_target']).lower()}"
5381
+ )
5382
+
5383
+
5384
+ def cmd_task_submodule_sync(args: argparse.Namespace) -> None:
5385
+ repo = Path(args.repo).resolve()
5386
+ ensure_git_repo(repo)
5387
+ cfg = _load_cfg(repo)
5388
+ task_id = _validate_task_id(args.task_id)
5389
+ manifest = _require_task_context(repo, cfg, task_id)
5390
+ if not manifest:
5391
+ raise APError("task-submodule-sync requires a registered task worktree.")
5392
+ control_repo = Path(_text(manifest.get("control_worktree_path"))).resolve()
5393
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5394
+ with (
5395
+ _repo_lock(control_repo, "integration", timeout_s=timeout_s),
5396
+ _repo_lock(control_repo, f"task-{task_id}", timeout_s=timeout_s),
5397
+ ):
5398
+ manifest = _require_task_context(repo, cfg, task_id)
5399
+ if not manifest:
5400
+ raise APError("Task manifest disappeared while synchronizing submodule config.")
5401
+ _sync_task_submodule_config(
5402
+ control_repo,
5403
+ repo,
5404
+ manifest,
5405
+ initial=False,
5406
+ check_common=True,
5407
+ )
5408
+ _save_task_manifest(repo, manifest)
5409
+ url_count = sum(
5410
+ 1 for key in (manifest.get("submodule_config_claims") or {}) if key.endswith(".url")
5411
+ )
5412
+ print(f"[task-submodule-sync] OK task={task_id} urls={url_count}")
5413
+
5414
+
5415
+ def _remote_branch_tip(repo: Path, remote: str, branch: str) -> str:
5416
+ result = run(["git", "ls-remote", "--heads", remote, f"refs/heads/{branch}"], cwd=repo, check=False)
5417
+ if result.returncode != 0:
5418
+ raise APError(
5419
+ f"Cannot inspect remote branch {remote}/{branch}: {result.stderr.strip() or result.stdout.strip()}"
5420
+ )
5421
+ if not result.stdout.strip():
5422
+ return ""
5423
+ return result.stdout.split()[0]
5424
+
5425
+
5426
+ def _branch_is_occupied(repo: Path, task_branch: str) -> bool:
5427
+ wanted = f"refs/heads/{task_branch}"
5428
+ return any(
5429
+ line.strip() == f"branch {wanted}"
5430
+ for line in run(["git", "worktree", "list", "--porcelain"], cwd=repo).stdout.splitlines()
5431
+ )
5432
+
5433
+
5434
+ def _delete_remote_task_branch(repo: Path, manifest: dict) -> bool:
5435
+ remote = _text(manifest.get("remote")) or "origin"
5436
+ branch = _text(manifest.get("task_branch"))
5437
+ actual_tip = _remote_branch_tip(repo, remote, branch)
5438
+ if not actual_tip:
5439
+ return True
5440
+ expected_tip = _text(manifest.get("remote_task_tip"))
5441
+ if not expected_tip or actual_tip != expected_tip:
5442
+ print(
5443
+ f"[task-cleanup] WARN: remote branch retained because its tip changed: "
5444
+ f"{remote}/{branch}",
5445
+ file=sys.stderr,
5446
+ )
5447
+ return False
5448
+ result = run(
5449
+ [
5450
+ "git",
5451
+ "push",
5452
+ "--no-verify",
5453
+ f"--force-with-lease=refs/heads/{branch}:{expected_tip}",
5454
+ remote,
5455
+ f":refs/heads/{branch}",
5456
+ ],
5457
+ cwd=repo,
5458
+ check=False,
5459
+ )
5460
+ if result.returncode != 0:
5461
+ print(
5462
+ f"[task-cleanup] WARN: failed to delete remote task branch {remote}/{branch}: "
5463
+ f"{result.stderr.strip()}",
5464
+ file=sys.stderr,
5465
+ )
5466
+ return False
5467
+ return True
5468
+
5469
+
5470
+ def _finish_registered_task(
5471
+ repo: Path,
5472
+ cfg: dict,
5473
+ manifest: dict,
5474
+ *,
5475
+ keep_remote: bool = False,
5476
+ ) -> dict:
5477
+ manifest = _validate_task_manifest(repo, manifest)
5478
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
5479
+ worktree = Path(_text(manifest.get("worktree_path")))
5480
+ branch = _text(manifest.get("task_branch"))
5481
+ remote = _text(manifest.get("remote")) or "origin"
5482
+ target = _text(manifest.get("target_branch"))
5483
+ cleanup_policy = _manifest_cleanup_policy(manifest, cfg)
5484
+
5485
+ if worktree.exists():
5486
+ worktree_manifest = _active_task_manifest(worktree)
5487
+ if not worktree_manifest or _text(worktree_manifest.get("task_uuid")) != _text(manifest.get("task_uuid")):
5488
+ raise APError(f"Task worktree manifest does not match the registry for {task_id}.")
5489
+ _sync_task_submodule_config(
5490
+ repo,
5491
+ worktree,
5492
+ manifest,
5493
+ initial=False,
5494
+ check_common=True,
5495
+ )
5496
+ _save_task_manifest(repo, manifest)
5497
+ unsafe_ignored = _unsafe_ignored_paths(
5498
+ worktree,
5499
+ list(cleanup_policy.get("disposable_ignored") or []),
5500
+ )
5501
+ if unsafe_ignored:
5502
+ raise APError(
5503
+ "Task worktree contains ignored files that are not declared disposable; refusing cleanup:\n- "
5504
+ + "\n- ".join(unsafe_ignored)
5505
+ )
5506
+ dirty = _git_status(worktree)
5507
+ if dirty:
5508
+ raise APError(f"Task worktree is dirty; refusing cleanup for {task_id}:\n{dirty}")
5509
+ conflicts = run(["git", "ls-files", "-u"], cwd=worktree).stdout.strip()
5510
+ if conflicts:
5511
+ raise APError(f"Task worktree has unresolved conflicts; refusing cleanup for {task_id}.")
5512
+
5513
+ target_sha = _fetch_target(repo, remote, target)
5514
+ local_ref = f"refs/heads/{branch}"
5515
+ tip = _resolve_commit(repo, local_ref) if _git_ref_exists(repo, local_ref) else _text(manifest.get("integrated_sha"))
5516
+ if not tip:
5517
+ raise APError(f"Task branch is missing and no integrated commit was recorded: {branch}")
5518
+ if run(["git", "merge-base", "--is-ancestor", tip, target_sha], cwd=repo, check=False).returncode != 0:
5519
+ raise APError(
5520
+ f"Task branch is not merged into {remote}/{target}; refusing cleanup for {task_id}."
5521
+ )
5522
+
5523
+ if worktree.exists():
5524
+ archived_submodules = _prepare_submodules_for_removal(
5525
+ repo,
5526
+ manifest,
5527
+ worktree,
5528
+ list(cleanup_policy.get("disposable_ignored") or []),
5529
+ )
5530
+ run(["git", "worktree", "unlock", str(worktree)], cwd=repo, check=False)
5531
+ remove_command = ["git", "worktree", "remove"]
5532
+ if archived_submodules:
5533
+ # Git requires --force for any worktree that has ever initialized a
5534
+ # submodule. Reaching this branch means root and recursive submodule
5535
+ # state was checked, unknown ignored data was rejected, additional
5536
+ # linked worktrees were excluded, and Git state was archived. Do not
5537
+ # run `submodule deinit` here: its config changes are shared with the
5538
+ # primary checkout.
5539
+ remove_command.append("--force")
5540
+ remove_command.append(str(worktree))
5541
+ run(remove_command, cwd=repo)
5542
+ if _branch_is_occupied(repo, branch):
5543
+ raise APError(f"Task branch is still checked out by a worktree: {branch}")
5544
+ if _git_ref_exists(repo, local_ref):
5545
+ current_tip = _resolve_commit(repo, local_ref)
5546
+ if current_tip != tip:
5547
+ raise APError(f"Task branch moved during cleanup: {branch}")
5548
+ run(["git", "update-ref", "-d", local_ref, tip], cwd=repo)
5549
+
5550
+ delete_remote = bool(cleanup_policy.get("delete_remote_branch", True))
5551
+ remote_deleted = False
5552
+ if delete_remote and not keep_remote:
5553
+ remote_deleted = _delete_remote_task_branch(repo, manifest)
5554
+ cleanup_pending = delete_remote and not keep_remote and not remote_deleted
5555
+ if cleanup_pending:
5556
+ manifest["state"] = "cleanup-pending"
5557
+ manifest["integrated_sha"] = tip
5558
+ _save_task_manifest(repo, manifest)
5559
+ else:
5560
+ _delete_task_manifest(repo, manifest)
5561
+ run(["git", "worktree", "prune"], cwd=repo, check=False)
5562
+ return {
5563
+ "task_id": task_id,
5564
+ "tip": tip,
5565
+ "target": f"{remote}/{target}",
5566
+ "remote_branch_deleted": remote_deleted,
5567
+ "cleanup_pending": cleanup_pending,
5568
+ }
5569
+
5570
+
5571
+ def cmd_task_finish(args: argparse.Namespace) -> None:
5572
+ repo = Path(args.repo).resolve()
5573
+ ensure_git_repo(repo)
5574
+ cfg = _load_cfg(repo)
5575
+ task_id = _validate_task_id(args.task_id)
5576
+ manifest = _load_task_manifest(repo, task_id)
5577
+ _require_control_checkout(repo, manifest)
5578
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5579
+ with (
5580
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
5581
+ _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s),
5582
+ ):
5583
+ manifest = _load_task_manifest(repo, task_id)
5584
+ result = _finish_registered_task(
5585
+ repo,
5586
+ cfg,
5587
+ manifest,
5588
+ keep_remote=bool(getattr(args, "keep_remote", False)),
5589
+ )
5590
+ print(
5591
+ f"[task-finish] OK task={task_id} target={result['target']} "
5592
+ f"remote_branch_deleted={str(result['remote_branch_deleted']).lower()}"
5593
+ )
5594
+
5595
+
5596
+ def _append_integration_closure(repo: Path, cfg: dict, manifest: dict, target_sha: str) -> None:
5597
+ path = _task_doc_path(repo, cfg, manifest, "closure")
5598
+ path.parent.mkdir(parents=True, exist_ok=True)
5599
+ if not path.exists():
5600
+ path.write_text(f"# Task Closure — {manifest['task_id']}\n", encoding="utf-8")
5601
+ with path.open("a", encoding="utf-8") as handle:
5602
+ handle.write("\n## Integration\n")
5603
+ handle.write(f"- Target: `{manifest['remote']}/{manifest['target_branch']}`\n")
5604
+ handle.write(f"- Base: `{target_sha}`\n")
5605
+ handle.write("- Gate: passed after rebase\n")
5606
+
5607
+
5608
+ def cmd_task_integrate(args: argparse.Namespace) -> None:
5609
+ repo = Path(args.repo).resolve()
5610
+ ensure_git_repo(repo)
5611
+ cfg = _load_cfg(repo)
5612
+ task_id = _validate_task_id(args.task_id)
5613
+ manifest = _load_task_manifest(repo, task_id)
5614
+ _require_control_checkout(repo, manifest)
5615
+ worktree = Path(_text(manifest.get("worktree_path")))
5616
+ if repo.resolve() == worktree.resolve():
5617
+ raise APError("Run task-integrate from the primary/control checkout, not from the task worktree.")
5618
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
5619
+
5620
+ with (
5621
+ _repo_lock(repo, "integration", timeout_s=timeout_s),
5622
+ _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s),
5623
+ ):
5624
+ manifest = _load_task_manifest(repo, task_id)
5625
+ worktree = Path(_text(manifest.get("worktree_path")))
5626
+ if not worktree.exists():
5627
+ raise APError(f"Task worktree is missing: {worktree}")
5628
+ worktree_manifest = _active_task_manifest(worktree)
5629
+ if not worktree_manifest or _text(worktree_manifest.get("task_uuid")) != _text(manifest.get("task_uuid")):
5630
+ raise APError(f"Task worktree manifest does not match the registry for {task_id}.")
5631
+ _sync_task_submodule_config(
5632
+ repo,
5633
+ worktree,
5634
+ manifest,
5635
+ initial=False,
5636
+ check_common=True,
5637
+ )
5638
+ _save_task_manifest(repo, manifest)
5639
+ if _text(manifest.get("state")) not in {"pushed", "integration-raced", "gate-failed"}:
5640
+ raise APError(f"Task {task_id} must be committed and pushed before integration.")
5641
+ dirty = _git_status(worktree)
5642
+ if dirty:
5643
+ raise APError(f"Task worktree is dirty; run commit-push before integration:\n{dirty}")
5644
+
5645
+ remote = _text(manifest.get("remote")) or "origin"
5646
+ target = _text(manifest.get("target_branch"))
5647
+ task_cfg = _load_cfg(worktree)
5648
+ target_sha = _fetch_target(worktree, remote, target)
5649
+ rebase = run(["git", "rebase", target_sha], cwd=worktree, check=False)
5650
+ if rebase.returncode != 0:
5651
+ manifest["state"] = "conflicted"
5652
+ _save_task_manifest(repo, manifest)
5653
+ raise APError(
5654
+ f"Task integration rebase conflicted for {task_id}; resolve it only in {worktree}.\n"
5655
+ f"{rebase.stdout}\n{rebase.stderr}"
5656
+ )
5657
+
5658
+ _sync_task_submodule_config(
5659
+ repo,
5660
+ worktree,
5661
+ manifest,
5662
+ initial=False,
5663
+ check_common=True,
5664
+ )
5665
+ _save_task_manifest(repo, manifest)
5666
+
5667
+ before_gate = _task_content_fingerprint(worktree, task_cfg, manifest)
5668
+ try:
5669
+ cmd_light_gate(
5670
+ argparse.Namespace(
5671
+ repo=str(worktree),
5672
+ scope="auto",
5673
+ profile="",
5674
+ mode="",
5675
+ base=target_sha,
5676
+ explain=False,
5677
+ )
5678
+ )
5679
+ except APError:
5680
+ manifest["state"] = "gate-failed"
5681
+ _save_task_manifest(repo, manifest)
5682
+ raise
5683
+ after_gate = _task_content_fingerprint(worktree, task_cfg, manifest)
5684
+ if after_gate != before_gate:
5685
+ manifest["state"] = "gate-mutated"
5686
+ _save_task_manifest(repo, manifest)
5687
+ raise APError(
5688
+ "Integration gate changed task-owned files. Review those changes, run commit-push again, "
5689
+ "then retry integration."
5690
+ )
5691
+ manifest = _require_task_context(worktree, task_cfg, task_id) or manifest
5692
+
5693
+ _append_integration_closure(worktree, task_cfg, manifest, target_sha)
5694
+ _update_active_task_status(worktree, task_cfg, manifest, "Integrated")
5695
+ protected = set(manifest.get("initial_untracked") or [])
5696
+ _cleanup_generated_noise(worktree, protected_paths=protected)
5697
+ integration_fingerprint = _task_content_fingerprint(worktree, task_cfg, manifest)
5698
+ manifest = _require_task_context(worktree, task_cfg, task_id) or manifest
5699
+ if _task_content_fingerprint(worktree, task_cfg, manifest) != integration_fingerprint:
5700
+ raise APError("Task-owned files changed immediately before staging integration evidence.")
5701
+ integration_paths = _task_commit_paths(worktree)
5702
+ task_tip = ""
5703
+ if integration_paths:
5704
+ staged = _stage_exact_paths(worktree, integration_paths)
5705
+ if staged:
5706
+ unstaged = _unstaged_task_paths(worktree)
5707
+ if unstaged:
5708
+ raise APError(
5709
+ "Task-owned files changed while staging integration evidence:\n- "
5710
+ + "\n- ".join(unstaged)
5711
+ )
5712
+ manifest = _require_task_context(worktree, task_cfg, task_id) or manifest
5713
+ task_tip = _commit_exact_index(
5714
+ worktree,
5715
+ f"{task_id}: record integration evidence [skip ci]",
5716
+ )
5717
+
5718
+ task_tip = task_tip or _resolve_commit(worktree, "HEAD")
5719
+ if _resolve_commit(worktree, "HEAD") != task_tip:
5720
+ raise APError("HEAD moved after the verified integration commit; refusing to push.")
5721
+ if run(
5722
+ ["git", "merge-base", "--is-ancestor", target_sha, task_tip],
5723
+ cwd=worktree,
5724
+ check=False,
5725
+ ).returncode != 0:
5726
+ raise APError(f"Integration tip {task_tip} is not a descendant of target base {target_sha}.")
5727
+ push = run(
5728
+ [
5729
+ "git",
5730
+ "push",
5731
+ f"--force-with-lease=refs/heads/{target}:{target_sha}",
5732
+ remote,
5733
+ f"{task_tip}:refs/heads/{target}",
5734
+ ],
5735
+ cwd=worktree,
5736
+ check=False,
5737
+ )
5738
+ if push.returncode != 0:
5739
+ manifest["state"] = "integration-raced"
5740
+ manifest["integration_tip"] = task_tip
5741
+ _save_task_manifest(repo, manifest)
5742
+ raise APError(
5743
+ f"Target branch moved while integrating {task_id}. Re-run task-integrate to fetch, rebase, "
5744
+ f"and gate the new target.\n{push.stdout}\n{push.stderr}"
5745
+ )
5746
+
5747
+ fresh_target = _fetch_target(worktree, remote, target)
5748
+ if run(
5749
+ ["git", "merge-base", "--is-ancestor", task_tip, fresh_target],
5750
+ cwd=worktree,
5751
+ check=False,
5752
+ ).returncode != 0:
5753
+ raise APError(
5754
+ f"Remote target verification mismatch after push: {task_tip} is not in {fresh_target}."
5755
+ )
5756
+ if _resolve_commit(worktree, "HEAD") != task_tip:
5757
+ raise APError("A push hook changed the task branch after integration; remote target is safe, local state needs review.")
5758
+ manifest["state"] = "integrated"
5759
+ manifest["integrated_sha"] = task_tip
5760
+ manifest["integration_base_sha"] = target_sha
5761
+ _save_task_manifest(repo, manifest)
5762
+
5763
+ cleanup_enabled = bool(
5764
+ _manifest_cleanup_policy(manifest, cfg).get("cleanup_merged", True)
5765
+ )
5766
+ if cleanup_enabled and not getattr(args, "keep_worktree", False):
5767
+ result = _finish_registered_task(
5768
+ repo,
5769
+ cfg,
5770
+ manifest,
5771
+ keep_remote=bool(getattr(args, "keep_remote", False)),
5772
+ )
5773
+ else:
5774
+ result = {
5775
+ "task_id": task_id,
5776
+ "target": f"{remote}/{target}",
5777
+ "remote_branch_deleted": False,
5778
+ }
5779
+
5780
+ print(
5781
+ f"[task-integrate] OK task={task_id} target={result['target']} commit={task_tip} "
5782
+ f"cleaned={str(cleanup_enabled and not getattr(args, 'keep_worktree', False)).lower()}"
5783
+ )
5784
+
5785
+
5786
+ def _worktree_branches(repo: Path) -> set[str]:
5787
+ branches: set[str] = set()
5788
+ for line in run(["git", "worktree", "list", "--porcelain"], cwd=repo).stdout.splitlines():
5789
+ if line.startswith("branch refs/heads/"):
5790
+ branches.add(line[len("branch refs/heads/") :].strip())
5791
+ return branches
5792
+
5793
+
5794
+ def cmd_task_prune(args: argparse.Namespace) -> None:
5795
+ repo = Path(args.repo).resolve()
5796
+ ensure_git_repo(repo)
5797
+ cfg = _load_cfg(repo)
5798
+ _require_control_checkout(repo)
5799
+ concurrency_cfg = _concurrency_cfg(cfg)
5800
+ remote = _text(concurrency_cfg.get("remote")) or "origin"
5801
+ target = _text(concurrency_cfg.get("target_branch")) or _current_branch(repo) or "dev"
5802
+ prefix = _task_branch_prefix(cfg)
5803
+ timeout_s = float(concurrency_cfg.get("lock_timeout_sec") or 30)
5804
+ removed: list[str] = []
5805
+ skipped: list[str] = []
5806
+
5807
+ with _repo_lock(repo, "integration", timeout_s=timeout_s):
5808
+ registry = _task_state_root(repo) / "tasks"
5809
+ registered_branches: set[str] = set()
5810
+ for path in list(sorted(registry.glob("*.json"))) if registry.exists() else []:
5811
+ manifest = _read_json_object(path)
5812
+ if not manifest:
5813
+ continue
5814
+ manifest = _validate_task_manifest(repo, manifest)
5815
+ branch = _text(manifest.get("task_branch"))
5816
+ registered_branches.add(branch)
5817
+ task_id = _validate_task_id(_text(manifest.get("task_id")))
5818
+ try:
5819
+ with _repo_lock(repo, f"task-{task_id}", timeout_s=timeout_s):
5820
+ manifest = _read_json_object(path)
5821
+ if not manifest:
5822
+ continue
5823
+ manifest = _validate_task_manifest(repo, manifest)
5824
+ branch = _text(manifest.get("task_branch"))
5825
+ if _text(manifest.get("state")) not in {"integrated", "cleanup-pending"}:
5826
+ skipped.append(f"{branch}: state={_text(manifest.get('state')) or 'unknown'}")
5827
+ continue
5828
+ result = _finish_registered_task(repo, cfg, manifest)
5829
+ except APError as exc:
5830
+ skipped.append(f"{branch}: {exc}")
5831
+ else:
5832
+ if result["cleanup_pending"]:
5833
+ skipped.append(f"{branch}: remote cleanup pending")
5834
+ else:
5835
+ removed.append(branch)
5836
+
5837
+ target_sha = _fetch_target(repo, remote, target)
5838
+ occupied = _worktree_branches(repo)
5839
+ result = run(
5840
+ [
5841
+ "git",
5842
+ "for-each-ref",
5843
+ "--format=%(refname:short)\t%(objectname)",
5844
+ f"refs/heads/{prefix}",
5845
+ ],
5846
+ cwd=repo,
5847
+ )
5848
+ for line in result.stdout.splitlines():
5849
+ if not line.strip() or "\t" not in line:
5850
+ continue
5851
+ branch, tip = line.split("\t", 1)
5852
+ if branch in registered_branches:
5853
+ skipped.append(f"{branch}: registered task")
5854
+ continue
5855
+ if branch in occupied:
5856
+ skipped.append(f"{branch}: occupied")
5857
+ continue
5858
+ if run(["git", "merge-base", "--is-ancestor", tip, target_sha], cwd=repo, check=False).returncode != 0:
5859
+ skipped.append(f"{branch}: unmerged")
5860
+ continue
5861
+ run(["git", "update-ref", "-d", f"refs/heads/{branch}", tip], cwd=repo)
5862
+ removed.append(branch)
5863
+
5864
+ for path in sorted(registry.glob("*.json")) if registry.exists() else []:
5865
+ manifest = _read_json_object(path)
5866
+ if not manifest:
5867
+ continue
5868
+ manifest = _validate_task_manifest(repo, manifest)
5869
+ if _text(manifest.get("state")) == "cleanup-pending":
5870
+ continue
5871
+ branch = _text(manifest.get("task_branch"))
5872
+ if _git_ref_exists(repo, f"refs/heads/{branch}"):
5873
+ continue
5874
+ worktree = Path(_text(manifest.get("worktree_path")))
5875
+ if worktree.exists():
5876
+ continue
5877
+ path.unlink(missing_ok=True)
5878
+ run(["git", "worktree", "prune"], cwd=repo, check=False)
5879
+
5880
+ if args.json:
5881
+ print(json.dumps({"removed": removed, "skipped": skipped}, ensure_ascii=False, indent=2))
5882
+ return
5883
+ print(f"[task-prune] removed={len(removed)} skipped={len(skipped)}")
5884
+ for branch in removed:
5885
+ print(f"[task-prune] removed: {branch}")
5886
+ for detail in skipped:
5887
+ print(f"[task-prune] kept: {detail}")
5888
+
5889
+
5890
+ def _record_commit_push_closure(
5891
+ repo: Path,
5892
+ args: argparse.Namespace,
5893
+ *,
5894
+ commit: str,
5895
+ jenkins: str,
5896
+ target_env: str,
5897
+ verification: List[str],
5898
+ result: str,
5899
+ follow_up: str,
5900
+ structure_check: str,
5901
+ ) -> None:
5902
+ cmd_record_closure(
5903
+ argparse.Namespace(
5904
+ repo=str(repo),
5905
+ task_id=args.task_id,
5906
+ title=args.title,
5907
+ commit=commit,
5908
+ jenkins=jenkins,
5909
+ target_env=target_env,
5910
+ verification=verification,
5911
+ result=result,
5912
+ follow_up=follow_up,
5913
+ profile=str(getattr(args, "effective_profile", "") or getattr(args, "profile", "") or ""),
5914
+ structure_check=structure_check,
5915
+ initial_commit=args.initial_commit,
5916
+ jenkins_failure=args.jenkins_failure,
5917
+ fix_commit=args.fix_commit,
5918
+ )
5919
+ )
5920
+
5921
+
5922
+ def _commit_exact_index(repo: Path, message: str) -> str:
5923
+ expected_parent = _resolve_commit(repo, "HEAD")
5924
+ expected_tree = run(["git", "write-tree"], cwd=repo).stdout.strip()
5925
+ run(["git", "commit", "-m", message], cwd=repo)
5926
+ commit_sha = _resolve_commit(repo, "HEAD")
5927
+ actual_tree = run(["git", "rev-parse", "HEAD^{tree}"], cwd=repo).stdout.strip()
5928
+ parents = run(["git", "show", "-s", "--format=%P", "HEAD"], cwd=repo).stdout.strip().split()
5929
+ post_commit_staged = _checked_git_z_paths(
5930
+ repo,
5931
+ [
5932
+ "git",
5933
+ "diff",
5934
+ "--cached",
5935
+ "--name-only",
5936
+ "-z",
5937
+ "--diff-filter=ACDMRTUXB",
5938
+ ],
5939
+ "post-commit staged paths",
5940
+ )
5941
+ post_commit_unstaged = _unstaged_task_paths(repo)
5942
+ if (
5943
+ parents != [expected_parent]
5944
+ or actual_tree != expected_tree
5945
+ or post_commit_staged
5946
+ or post_commit_unstaged
5947
+ ):
5948
+ raise APError(
5949
+ "Git hooks or another writer changed the commit or working tree after staging. "
5950
+ "The local commit was not pushed; inspect HEAD and rerun the gate before publishing."
5951
+ )
5952
+ return commit_sha
5953
+
5954
+
5955
+ def _push_current_task(repo: Path, manifest: Optional[dict], expected_commit: str = "") -> str:
5956
+ commit_sha = expected_commit or _resolve_commit(repo, "HEAD")
5957
+ if _resolve_commit(repo, "HEAD") != commit_sha:
5958
+ raise APError("HEAD moved after the verified commit; refusing to push.")
5959
+ if manifest:
5960
+ remote = _text(manifest.get("remote")) or "origin"
5961
+ task_branch = _text(manifest.get("task_branch"))
5962
+ remote_tip = _remote_branch_tip(repo, remote, task_branch)
5963
+ command = ["git", "push", "--set-upstream"]
5964
+ if remote_tip and run(
5965
+ ["git", "merge-base", "--is-ancestor", remote_tip, "HEAD"],
5966
+ cwd=repo,
5967
+ check=False,
5968
+ ).returncode != 0:
5969
+ expected_tip = _text(manifest.get("remote_task_tip"))
5970
+ if not expected_tip or expected_tip != remote_tip:
5971
+ raise APError(
5972
+ f"Remote task branch moved unexpectedly: {remote}/{task_branch}. "
5973
+ "No force push was attempted."
5974
+ )
5975
+ command.append(f"--force-with-lease=refs/heads/{task_branch}:{expected_tip}")
5976
+ command.extend([remote, f"{commit_sha}:refs/heads/{task_branch}"])
5977
+ run(command, cwd=repo)
5978
+ if _resolve_commit(repo, "HEAD") != commit_sha:
5979
+ raise APError("A push hook changed HEAD. The verified task commit was pushed, but local state needs review.")
5980
+ pushed_tip = _remote_branch_tip(repo, remote, task_branch)
5981
+ if pushed_tip != commit_sha:
5982
+ raise APError(
5983
+ f"Remote task branch verification failed: expected {commit_sha}, got {pushed_tip or '(missing)'}."
5984
+ )
5985
+ manifest["state"] = "pushed"
5986
+ manifest["remote_task_tip"] = commit_sha
5987
+ manifest["last_commit"] = commit_sha
5988
+ manifest["claimed_paths"] = _changed_files(repo, _text(manifest.get("base_sha")))
5989
+ _save_task_manifest(repo, manifest)
5990
+ else:
5991
+ run(["git", "push"], cwd=repo)
5992
+ return commit_sha
5993
+
5994
+
5995
+ def _cmd_commit_push_locked(
5996
+ args: argparse.Namespace,
5997
+ repo: Path,
5998
+ cfg: dict,
5999
+ manifest: Optional[dict],
6000
+ ) -> None:
6001
+ if manifest:
6002
+ control_repo = Path(_text(manifest.get("control_worktree_path"))).resolve()
6003
+ _sync_task_submodule_config(
6004
+ control_repo,
6005
+ repo,
6006
+ manifest,
6007
+ initial=False,
6008
+ check_common=False,
6009
+ )
6010
+ _save_task_manifest(repo, manifest)
6011
+ cmd_doctor(
6012
+ argparse.Namespace(
6013
+ repo=str(repo),
6014
+ profile=_text(getattr(args, "profile", "")).lower(),
6015
+ mode=_text(getattr(args, "mode", "")).lower(),
6016
+ )
6017
+ )
6018
+ base_ref = _text(manifest.get("base_sha")) if manifest else ""
6019
+ plan = _resolve_execution_plan(
6020
+ cfg,
6021
+ repo,
6022
+ requested_scope="",
6023
+ requested_profile=_text(getattr(args, "profile", "")).lower(),
6024
+ requested_mode=_text(getattr(args, "mode", "")).lower(),
6025
+ base_ref=base_ref,
6026
+ )
6027
+ mode = str(plan["effective_mode"])
6028
+ args.effective_profile = str(plan["profile"])
6029
+ if args.result:
6030
+ _validate_closure_result(mode, str(args.result))
6031
+ target_cfg = (cfg.get("target_env") or {})
6032
+ target_env_required = _verification_required(cfg, "target_env", default=True)
6033
+ jenkins_required = _verification_required(cfg, "jenkins", default=True)
6034
+
6035
+ msg = args.msg
6036
+ structure_check_status = "skipped"
6037
+
6038
+ if args.require_runtime_health:
6039
+ cmd_wait_health(argparse.Namespace(repo=str(repo), scope="runtime"))
6040
+
6041
+ before_gate = _task_content_fingerprint(repo, cfg, manifest)
6042
+ if mode in {"dev", "verify"} or args.require_light_gate:
6043
+ cmd_light_gate(
6044
+ argparse.Namespace(
6045
+ repo=str(repo),
6046
+ scope=plan["selected_scope"],
6047
+ profile=plan["profile"],
6048
+ mode=mode,
6049
+ base=base_ref,
6050
+ explain=False,
6051
+ )
6052
+ )
6053
+ structure_check_status = "passed via light-gate" if _structure_gate_enabled(cfg) else "skipped (structure disabled)"
6054
+
6055
+ after_gate = _task_content_fingerprint(repo, cfg, manifest)
6056
+ if after_gate != before_gate:
6057
+ raise APError(
6058
+ "The gate changed task-owned files or another writer modified this worktree. "
6059
+ "Review the changes and rerun commit-push; no files were staged or restored."
6060
+ )
6061
+ if manifest:
6062
+ manifest = _require_task_context(repo, cfg, args.task_id)
6063
+
6064
+ if args.require_jenkins:
6065
+ cmd_verify_jenkins(argparse.Namespace(repo=str(repo)))
6066
+
6067
+ if args.require_matrix and mode != "verify":
6068
+ cmd_check_matrix(argparse.Namespace(repo=str(repo)))
6069
+
6070
+ if _task_content_fingerprint(repo, cfg, manifest) != after_gate:
6071
+ raise APError(
6072
+ "Task-owned files changed after the gate. Review them and rerun commit-push; "
6073
+ "no files were staged or restored."
6074
+ )
6075
+
6076
+ if mode == "dev":
6077
+ dev_verification = args.verification or [
6078
+ "light-gate only",
3844
6079
  "configured CI/Jenkins not verified in dev mode",
3845
6080
  "target environment not verified in dev mode",
3846
6081
  ]
@@ -3856,16 +6091,31 @@ def cmd_commit_push(args: argparse.Namespace) -> None:
3856
6091
  structure_check=args.structure_check or structure_check_status,
3857
6092
  )
3858
6093
 
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:
6094
+ protected = set(manifest.get("initial_untracked") or []) if manifest else set()
6095
+ if manifest:
6096
+ _cleanup_generated_noise(repo, protected_paths=protected)
6097
+ pre_stage_fingerprint = _task_content_fingerprint(repo, cfg, manifest)
6098
+ if manifest:
6099
+ manifest = _require_task_context(repo, cfg, args.task_id)
6100
+ if _task_content_fingerprint(repo, cfg, manifest) != pre_stage_fingerprint:
6101
+ raise APError("Task-owned files changed immediately before staging; refusing to commit.")
6102
+ task_paths = _task_commit_paths(repo)
6103
+ staged = _stage_exact_paths(repo, task_paths)
6104
+ if not staged:
3863
6105
  raise APError("Nothing to commit.")
6106
+ unstaged = _unstaged_task_paths(repo)
6107
+ if unstaged:
6108
+ raise APError(
6109
+ "Task-owned files changed while staging; refusing to commit:\n- " + "\n- ".join(unstaged)
6110
+ )
6111
+ if manifest:
6112
+ manifest = _require_task_context(repo, cfg, args.task_id)
3864
6113
 
3865
- run(["git", "commit", "-m", msg], cwd=repo)
3866
- run(["git", "push"], cwd=repo)
6114
+ committed_sha = _commit_exact_index(repo, msg)
6115
+ _push_current_task(repo, manifest, committed_sha)
3867
6116
 
3868
6117
  if mode == "verify":
6118
+ verification_fingerprint = _task_content_fingerprint(repo, cfg, manifest)
3869
6119
  if jenkins_required:
3870
6120
  jenkins_build = cmd_verify_jenkins_build(
3871
6121
  argparse.Namespace(
@@ -3910,6 +6160,13 @@ def cmd_commit_push(args: argparse.Namespace) -> None:
3910
6160
  verification.append(f"Target verification: {target_summary}")
3911
6161
  else:
3912
6162
  verification.append("Target verification skipped by verification.target_env_required=false")
6163
+ if _task_content_fingerprint(repo, cfg, manifest) != verification_fingerprint:
6164
+ raise APError(
6165
+ "Verification changed task-owned files. Review them and rerun commit-push before "
6166
+ "recording the verification closure."
6167
+ )
6168
+ if manifest:
6169
+ manifest = _require_task_context(repo, cfg, args.task_id)
3913
6170
  _record_commit_push_closure(
3914
6171
  repo,
3915
6172
  args,
@@ -3922,12 +6179,24 @@ def cmd_commit_push(args: argparse.Namespace) -> None:
3922
6179
  follow_up=args.follow_up or "none",
3923
6180
  structure_check=args.structure_check or structure_check_status,
3924
6181
  )
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)
6182
+ if manifest:
6183
+ _cleanup_generated_noise(repo, protected_paths=protected)
6184
+ closure_fingerprint = _task_content_fingerprint(repo, cfg, manifest)
6185
+ if manifest:
6186
+ manifest = _require_task_context(repo, cfg, args.task_id)
6187
+ if _task_content_fingerprint(repo, cfg, manifest) != closure_fingerprint:
6188
+ raise APError("Task-owned files changed immediately before staging verification closure.")
6189
+ closure_paths = _task_commit_paths(repo)
6190
+ closure_staged = _stage_exact_paths(repo, closure_paths)
6191
+ if closure_staged:
6192
+ unstaged = _unstaged_task_paths(repo)
6193
+ if unstaged:
6194
+ raise APError(
6195
+ "Task-owned files changed while staging verification closure:\n- "
6196
+ + "\n- ".join(unstaged)
6197
+ )
6198
+ closure_sha = _commit_exact_index(repo, f"{args.task_id}: record verification closure [skip ci]")
6199
+ _push_current_task(repo, manifest, closure_sha)
3931
6200
  elif args.record_closure and mode != "dev":
3932
6201
  _record_commit_push_closure(
3933
6202
  repo,
@@ -3943,6 +6212,17 @@ def cmd_commit_push(args: argparse.Namespace) -> None:
3943
6212
  print(f"[commit-push] OK - profile={plan['profile']} mode={mode} scope={plan['selected_scope']}")
3944
6213
 
3945
6214
 
6215
+ def cmd_commit_push(args: argparse.Namespace) -> None:
6216
+ repo = Path(args.repo).resolve()
6217
+ ensure_git_repo(repo)
6218
+ cfg = _load_cfg(repo)
6219
+ manifest = _require_task_context(repo, cfg, args.task_id)
6220
+ lock_name = f"task-{_validate_task_id(args.task_id)}" if manifest else "legacy-commit"
6221
+ timeout_s = float(_concurrency_cfg(cfg).get("lock_timeout_sec") or 30)
6222
+ with _repo_lock(repo, lock_name, timeout_s=timeout_s):
6223
+ _cmd_commit_push_locked(args, repo, cfg, manifest)
6224
+
6225
+
3946
6226
  def main(argv: Optional[List[str]] = None) -> int:
3947
6227
  p = argparse.ArgumentParser(prog="autopipeline")
3948
6228
  p.add_argument("--repo", default=".")
@@ -4091,6 +6371,40 @@ def main(argv: Optional[List[str]] = None) -> int:
4091
6371
  s.add_argument("--fix-commit")
4092
6372
  s.set_defaults(func=cmd_record_closure)
4093
6373
 
6374
+ s = sp.add_parser("task-start")
6375
+ s.add_argument("task_id")
6376
+ s.add_argument("--base")
6377
+ s.add_argument("--target-branch")
6378
+ s.add_argument("--remote")
6379
+ s.add_argument("--worktree-root")
6380
+ s.add_argument("--owner")
6381
+ s.add_argument("--no-fetch", action="store_true")
6382
+ s.set_defaults(func=cmd_task_start)
6383
+
6384
+ s = sp.add_parser("task-status")
6385
+ s.add_argument("task_id", nargs="?")
6386
+ s.add_argument("--json", action="store_true")
6387
+ s.set_defaults(func=cmd_task_status)
6388
+
6389
+ s = sp.add_parser("task-submodule-sync")
6390
+ s.add_argument("task_id")
6391
+ s.set_defaults(func=cmd_task_submodule_sync)
6392
+
6393
+ s = sp.add_parser("task-integrate")
6394
+ s.add_argument("task_id")
6395
+ s.add_argument("--keep-worktree", action="store_true")
6396
+ s.add_argument("--keep-remote", action="store_true")
6397
+ s.set_defaults(func=cmd_task_integrate)
6398
+
6399
+ s = sp.add_parser("task-finish")
6400
+ s.add_argument("task_id")
6401
+ s.add_argument("--keep-remote", action="store_true")
6402
+ s.set_defaults(func=cmd_task_finish)
6403
+
6404
+ s = sp.add_parser("task-prune")
6405
+ s.add_argument("--json", action="store_true")
6406
+ s.set_defaults(func=cmd_task_prune)
6407
+
4094
6408
  s = sp.add_parser("commit-push")
4095
6409
  s.add_argument("task_id")
4096
6410
  s.add_argument("--title")