@elvis1513/auto-coding-skill 4.2.2 → 4.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -4
- package/cli/assets/managed-install.json +1 -1
- package/cli/assets/skill/SKILL.md +4 -5
- package/cli/assets/skill/data/policies/workflow-migrations-v1.json +2 -2
- package/cli/assets/skill/data/templates/ENGINEERING.md +3 -2
- package/cli/assets/skill/data/templates/bridges/AGENTS.md +5 -5
- package/cli/assets/skill/scripts/ap.py +575 -21
- package/cli/tests/ap_profiles_test.py +408 -0
- package/cli/tests/index.test.js +63 -18
- package/package.json +1 -1
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
from __future__ import annotations
|
|
6
6
|
|
|
7
7
|
import argparse
|
|
8
|
+
import ast
|
|
8
9
|
import base64
|
|
9
10
|
import contextlib
|
|
10
11
|
import hashlib
|
|
@@ -84,7 +85,7 @@ _FINAL_GATE_CACHE_ALGORITHM = "final-gate-v1"
|
|
|
84
85
|
_WORKFLOW_MIGRATION_POLICY = Path("data/policies/workflow-migrations-v1.json")
|
|
85
86
|
_FALLBACK_WORKFLOW_MIGRATION_POLICY = {
|
|
86
87
|
"schema_version": 1,
|
|
87
|
-
"managed_versions": {"agents": "4.2.
|
|
88
|
+
"managed_versions": {"agents": "4.2.3", "engineering": "4.2.3"},
|
|
88
89
|
"known_official_engineering_body_sha256": [
|
|
89
90
|
"d1306cc626e8baf8c83c953b760fd771066de2bf125168eca3a7b7d6ff2b87a2",
|
|
90
91
|
"305931c6edef770033a4f1970b00e5fb1c1728351856a173dbfa497daf563021",
|
|
@@ -989,7 +990,11 @@ def _converged_engineering_document(repo: Path, template_path: Path) -> str:
|
|
|
989
990
|
value = current_structure.get(key)
|
|
990
991
|
if isinstance(value, int) and not isinstance(value, bool):
|
|
991
992
|
converged["structure"][key] = value
|
|
992
|
-
for key in (
|
|
993
|
+
for key in (
|
|
994
|
+
"require_reuse_search",
|
|
995
|
+
"block_new_responsibility_in_large_file",
|
|
996
|
+
"block_warnings",
|
|
997
|
+
):
|
|
993
998
|
value = current_structure.get(key)
|
|
994
999
|
if isinstance(value, bool):
|
|
995
1000
|
converged["structure"][key] = value
|
|
@@ -1143,8 +1148,8 @@ def _migrate_fast_development_defaults(cfg: dict, repo: Path) -> list[str]:
|
|
|
1143
1148
|
if _text(workflow_cfg.get("completion")).lower() != "push":
|
|
1144
1149
|
workflow_cfg["completion"] = "push"
|
|
1145
1150
|
changed.append("workflow.completion")
|
|
1146
|
-
if _text(workflow_cfg.get("skill_version")) != "4.2.
|
|
1147
|
-
workflow_cfg["skill_version"] = "4.2.
|
|
1151
|
+
if _text(workflow_cfg.get("skill_version")) != "4.2.3":
|
|
1152
|
+
workflow_cfg["skill_version"] = "4.2.3"
|
|
1148
1153
|
changed.append("workflow.skill_version")
|
|
1149
1154
|
|
|
1150
1155
|
commands_cfg = cfg.setdefault("commands", {})
|
|
@@ -1457,16 +1462,16 @@ def cmd_upgrade(args: argparse.Namespace) -> None:
|
|
|
1457
1462
|
if current_agents:
|
|
1458
1463
|
archive_dir = repo / "docs" / "archive" / "workflow"
|
|
1459
1464
|
archive_header = (
|
|
1460
|
-
"# Archived AGENTS.md before auto-coding-skill 4.2.
|
|
1465
|
+
"# Archived AGENTS.md before auto-coding-skill 4.2.3\n\n"
|
|
1461
1466
|
"This file is historical and non-authoritative. The root AGENTS.md is fully managed.\n"
|
|
1462
1467
|
"Move any still-current project facts into docs/ENGINEERING.md or docs/project/,\n"
|
|
1463
1468
|
"without copying workflow rules back into the root AGENTS.md.\n\n---\n\n"
|
|
1464
1469
|
)
|
|
1465
1470
|
archive_content = archive_header + current_agents
|
|
1466
|
-
archive = archive_dir / "AGENTS.pre-4.2.
|
|
1471
|
+
archive = archive_dir / "AGENTS.pre-4.2.3.md"
|
|
1467
1472
|
if archive.exists() and archive.read_text(encoding="utf-8") != archive_content:
|
|
1468
1473
|
digest = hashlib.sha256(current_agents.encode("utf-8")).hexdigest()[:12]
|
|
1469
|
-
archive = archive_dir / f"AGENTS.pre-4.2.
|
|
1474
|
+
archive = archive_dir / f"AGENTS.pre-4.2.3-{digest}.md"
|
|
1470
1475
|
if not archive.exists():
|
|
1471
1476
|
add_action("policy", archive, "archive", "historical and non-authoritative")
|
|
1472
1477
|
if write:
|
|
@@ -4138,16 +4143,27 @@ _TEXT_SOURCE_FILENAMES = {
|
|
|
4138
4143
|
"tsconfig.json",
|
|
4139
4144
|
}
|
|
4140
4145
|
_FUNCTION_START_RE = re.compile(
|
|
4141
|
-
r"^\s*(?:export\s+)?(?:async\s+)?(?:def|function
|
|
4146
|
+
r"^\s*(?:export\s+)?(?:async\s+)?(?:def|function)\s+[A-Za-z_][\w]*\b"
|
|
4147
|
+
r"|^\s*func\s+(?:\([^)]*\)\s*)?[A-Za-z_][\w]*\b"
|
|
4142
4148
|
r"|^\s*(?:public|private|protected|static|final|async|\s)+\s*[\w<>\[\], ?]+\s+[A-Za-z_]\w*\s*\([^;]*\)\s*\{"
|
|
4143
|
-
r"|^\s*(?:export\s+)?(?:const|let|var)\s+[A-Za-z_]\w*\s*=\s*(?:async\s
|
|
4149
|
+
r"|^\s*(?:export\s+)?(?:const|let|var)\s+[A-Za-z_]\w*\s*=\s*(?:async\s+)?"
|
|
4150
|
+
r"(?:\([^)]*\)|[A-Za-z_]\w*)\s*(?::\s*[^=]+)?=>"
|
|
4151
|
+
)
|
|
4152
|
+
_ARROW_DECLARATION_RE = re.compile(
|
|
4153
|
+
r"^\s*(?:export\s+)?(?:const|let|var)\s+[A-Za-z_]\w*\s*=\s*(?:async\s+)?"
|
|
4144
4154
|
)
|
|
4155
|
+
_ARROW_FUNCTION_BODY_RE = re.compile(r"=>\s*(?::\s*[^=]+)?\{")
|
|
4145
4156
|
_IMPORT_PATTERNS = [
|
|
4146
4157
|
re.compile(r"^\s*import\s+(?:.+?\s+from\s+)?[\"']([^\"']+)[\"']"),
|
|
4147
4158
|
re.compile(r"\brequire\(\s*[\"']([^\"']+)[\"']\s*\)"),
|
|
4148
4159
|
re.compile(r"\bimport\(\s*[\"']([^\"']+)[\"']\s*\)"),
|
|
4149
4160
|
re.compile(r"^\s*from\s+([A-Za-z_][\w.]*)\s+import\b"),
|
|
4150
|
-
re.compile(
|
|
4161
|
+
re.compile(
|
|
4162
|
+
r"^\s*import\s+(?:static\s+)?"
|
|
4163
|
+
r"([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*(?:\.\*)?)"
|
|
4164
|
+
r"(?:\s+as\s+[A-Za-z_]\w*)?\s*;?\s*"
|
|
4165
|
+
r"(?://.*|/\*.*)?$"
|
|
4166
|
+
),
|
|
4151
4167
|
re.compile(r"^\s*import\s+[\"']([^\"']+)[\"']\s*$"),
|
|
4152
4168
|
re.compile(r"^\s*[\"']([^\"']+/[^\"']*)[\"']\s*$"),
|
|
4153
4169
|
]
|
|
@@ -4349,19 +4365,531 @@ def _added_lines_by_path(repo: Path, base_ref: str = "") -> dict[str, int]:
|
|
|
4349
4365
|
return added
|
|
4350
4366
|
|
|
4351
4367
|
|
|
4352
|
-
def
|
|
4368
|
+
def _ensure_structure_deadline(deadline: Optional[float]) -> None:
|
|
4369
|
+
if deadline is not None and time.monotonic() >= deadline:
|
|
4370
|
+
raise APError("Final changed-scope gate timed out during structure check.")
|
|
4371
|
+
|
|
4372
|
+
|
|
4373
|
+
def _looks_like_regex_start(line: str, index: int) -> bool:
|
|
4374
|
+
prefix = line[:index].rstrip()
|
|
4375
|
+
if not prefix:
|
|
4376
|
+
return True
|
|
4377
|
+
if prefix.endswith(("=", "(", "[", "{", ",", ":", ";", "!", "?", "=>")):
|
|
4378
|
+
return True
|
|
4379
|
+
return bool(re.search(r"\b(?:return|case|throw|yield)\s*$", prefix))
|
|
4380
|
+
|
|
4381
|
+
|
|
4382
|
+
def _python_function_end(
|
|
4383
|
+
lines: list[str],
|
|
4384
|
+
start: int,
|
|
4385
|
+
threshold: int,
|
|
4386
|
+
deadline: Optional[float],
|
|
4387
|
+
) -> tuple[int, bool]:
|
|
4388
|
+
"""Best-effort range for temporarily invalid Python during an edit."""
|
|
4389
|
+
indent = len(lines[start]) - len(lines[start].lstrip())
|
|
4390
|
+
limit = min(len(lines), start + threshold + 1)
|
|
4391
|
+
body_start: Optional[int] = None
|
|
4392
|
+
for index in range(start, limit):
|
|
4393
|
+
if (index - start) % 32 == 0:
|
|
4394
|
+
_ensure_structure_deadline(deadline)
|
|
4395
|
+
header = re.search(r"\)\s*(?:->\s*.*?)?\s*:", lines[index])
|
|
4396
|
+
if not header:
|
|
4397
|
+
continue
|
|
4398
|
+
tail = lines[index][header.end() :].strip()
|
|
4399
|
+
if tail and not tail.startswith("#"):
|
|
4400
|
+
return index + 1, False
|
|
4401
|
+
body_start = index + 1
|
|
4402
|
+
break
|
|
4403
|
+
if body_start is None:
|
|
4404
|
+
return limit, limit < len(lines)
|
|
4405
|
+
|
|
4406
|
+
end = body_start
|
|
4407
|
+
for index in range(body_start, limit):
|
|
4408
|
+
if (index - body_start) % 32 == 0:
|
|
4409
|
+
_ensure_structure_deadline(deadline)
|
|
4410
|
+
stripped = lines[index].strip()
|
|
4411
|
+
if not stripped or stripped.startswith("#"):
|
|
4412
|
+
continue
|
|
4413
|
+
current_indent = len(lines[index]) - len(lines[index].lstrip())
|
|
4414
|
+
if current_indent <= indent:
|
|
4415
|
+
return end, False
|
|
4416
|
+
end = index + 1
|
|
4417
|
+
return limit if limit < len(lines) else end, limit < len(lines)
|
|
4418
|
+
|
|
4419
|
+
|
|
4420
|
+
def _python_function_ranges(
|
|
4421
|
+
lines: list[str],
|
|
4422
|
+
threshold: int,
|
|
4423
|
+
deadline: Optional[float],
|
|
4424
|
+
) -> list[tuple[int, int, bool]]:
|
|
4425
|
+
_ensure_structure_deadline(deadline)
|
|
4426
|
+
try:
|
|
4427
|
+
tree = ast.parse("\n".join(lines))
|
|
4428
|
+
except (SyntaxError, ValueError):
|
|
4429
|
+
ranges = []
|
|
4430
|
+
for start, line in enumerate(lines):
|
|
4431
|
+
if not re.match(r"^\s*(?:async\s+)?def\s+[A-Za-z_]\w*\b", line):
|
|
4432
|
+
continue
|
|
4433
|
+
end, truncated = _python_function_end(lines, start, threshold, deadline)
|
|
4434
|
+
ranges.append((start, end, truncated))
|
|
4435
|
+
return ranges
|
|
4436
|
+
|
|
4437
|
+
_ensure_structure_deadline(deadline)
|
|
4438
|
+
ranges = []
|
|
4439
|
+
for node in ast.walk(tree):
|
|
4440
|
+
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
|
4441
|
+
continue
|
|
4442
|
+
start = max(0, int(node.lineno) - 1)
|
|
4443
|
+
end = int(getattr(node, "end_lineno", 0) or node.lineno)
|
|
4444
|
+
ranges.append((start, end, False))
|
|
4445
|
+
return sorted(ranges)
|
|
4446
|
+
|
|
4447
|
+
|
|
4448
|
+
def _find_arrow_line(
|
|
4449
|
+
lines: list[str],
|
|
4450
|
+
start: int,
|
|
4451
|
+
deadline: Optional[float],
|
|
4452
|
+
) -> Optional[int]:
|
|
4453
|
+
"""Find a declaration's top-level arrow, ignoring arrows inside parameters."""
|
|
4454
|
+
state = "code"
|
|
4455
|
+
escaped = False
|
|
4456
|
+
regex_char_class = False
|
|
4457
|
+
paren_depth = 0
|
|
4458
|
+
bracket_depth = 0
|
|
4459
|
+
brace_depth = 0
|
|
4460
|
+
indent = len(lines[start]) - len(lines[start].lstrip())
|
|
4461
|
+
limit = min(len(lines), start + 200)
|
|
4462
|
+
equals = lines[start].find("=")
|
|
4463
|
+
for line_index in range(start, limit):
|
|
4464
|
+
if (line_index - start) % 32 == 0:
|
|
4465
|
+
_ensure_structure_deadline(deadline)
|
|
4466
|
+
line = lines[line_index]
|
|
4467
|
+
if line_index > start:
|
|
4468
|
+
stripped = line.strip()
|
|
4469
|
+
current_indent = len(line) - len(line.lstrip())
|
|
4470
|
+
if stripped and current_indent <= indent and not (paren_depth or bracket_depth or brace_depth):
|
|
4471
|
+
return None
|
|
4472
|
+
char_index = equals + 1 if line_index == start and equals >= 0 else 0
|
|
4473
|
+
while char_index < len(line):
|
|
4474
|
+
char = line[char_index]
|
|
4475
|
+
next_char = line[char_index + 1] if char_index + 1 < len(line) else ""
|
|
4476
|
+
if state == "block_comment":
|
|
4477
|
+
if char == "*" and next_char == "/":
|
|
4478
|
+
state = "code"
|
|
4479
|
+
char_index += 2
|
|
4480
|
+
continue
|
|
4481
|
+
char_index += 1
|
|
4482
|
+
continue
|
|
4483
|
+
if state == "regex":
|
|
4484
|
+
if escaped:
|
|
4485
|
+
escaped = False
|
|
4486
|
+
elif char == "\\":
|
|
4487
|
+
escaped = True
|
|
4488
|
+
elif char == "[":
|
|
4489
|
+
regex_char_class = True
|
|
4490
|
+
elif char == "]":
|
|
4491
|
+
regex_char_class = False
|
|
4492
|
+
elif char == "/" and not regex_char_class:
|
|
4493
|
+
state = "code"
|
|
4494
|
+
char_index += 1
|
|
4495
|
+
continue
|
|
4496
|
+
if state in {"single_quote", "double_quote", "backtick"}:
|
|
4497
|
+
quote = {"single_quote": "'", "double_quote": '"', "backtick": "`"}[state]
|
|
4498
|
+
if escaped:
|
|
4499
|
+
escaped = False
|
|
4500
|
+
elif char == "\\":
|
|
4501
|
+
escaped = True
|
|
4502
|
+
elif char == quote:
|
|
4503
|
+
state = "code"
|
|
4504
|
+
char_index += 1
|
|
4505
|
+
continue
|
|
4506
|
+
if char == "/" and next_char == "/":
|
|
4507
|
+
break
|
|
4508
|
+
if char == "/" and next_char == "*":
|
|
4509
|
+
state = "block_comment"
|
|
4510
|
+
char_index += 2
|
|
4511
|
+
continue
|
|
4512
|
+
if char == "/" and _looks_like_regex_start(line, char_index):
|
|
4513
|
+
state = "regex"
|
|
4514
|
+
regex_char_class = False
|
|
4515
|
+
escaped = False
|
|
4516
|
+
char_index += 1
|
|
4517
|
+
continue
|
|
4518
|
+
if char in {"'", '"', "`"}:
|
|
4519
|
+
state = {"'": "single_quote", '"': "double_quote", "`": "backtick"}[char]
|
|
4520
|
+
escaped = False
|
|
4521
|
+
elif char == "(":
|
|
4522
|
+
paren_depth += 1
|
|
4523
|
+
elif char == ")":
|
|
4524
|
+
paren_depth = max(0, paren_depth - 1)
|
|
4525
|
+
elif char == "[":
|
|
4526
|
+
bracket_depth += 1
|
|
4527
|
+
elif char == "]":
|
|
4528
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
4529
|
+
elif char == "{":
|
|
4530
|
+
brace_depth += 1
|
|
4531
|
+
elif char == "}":
|
|
4532
|
+
brace_depth = max(0, brace_depth - 1)
|
|
4533
|
+
elif char == "=" and next_char == ">" and not (paren_depth or bracket_depth or brace_depth):
|
|
4534
|
+
return line_index
|
|
4535
|
+
elif char == ";" and not (paren_depth or bracket_depth or brace_depth):
|
|
4536
|
+
return None
|
|
4537
|
+
char_index += 1
|
|
4538
|
+
if line_index > start:
|
|
4539
|
+
stripped = line.strip()
|
|
4540
|
+
current_indent = len(line) - len(line.lstrip())
|
|
4541
|
+
if stripped and current_indent <= indent and not (paren_depth or bracket_depth or brace_depth):
|
|
4542
|
+
return None
|
|
4543
|
+
return None
|
|
4544
|
+
|
|
4545
|
+
|
|
4546
|
+
def _arrow_body_kind(lines: list[str], arrow_line: int) -> str:
|
|
4547
|
+
arrow_column = lines[arrow_line].find("=>")
|
|
4548
|
+
for line_index in range(arrow_line, min(len(lines), arrow_line + 20)):
|
|
4549
|
+
text = lines[line_index][arrow_column + 2 :] if line_index == arrow_line else lines[line_index]
|
|
4550
|
+
stripped = text.strip()
|
|
4551
|
+
if not stripped or stripped.startswith(("//", "/*", "*")):
|
|
4552
|
+
continue
|
|
4553
|
+
return "block" if stripped.startswith("{") else "expression"
|
|
4554
|
+
return "expression"
|
|
4555
|
+
|
|
4556
|
+
|
|
4557
|
+
def _looks_like_return_type_brace(line: str, index: int) -> bool:
|
|
4558
|
+
prefix = line[:index].rstrip()
|
|
4559
|
+
if re.search(r"\b(?:struct|interface)\s*$", prefix):
|
|
4560
|
+
return True
|
|
4561
|
+
close_paren = prefix.rfind(")")
|
|
4562
|
+
return close_paren >= 0 and ":" in prefix[close_paren + 1 :]
|
|
4563
|
+
|
|
4564
|
+
|
|
4565
|
+
def _brace_function_end(
|
|
4566
|
+
lines: list[str],
|
|
4567
|
+
start: int,
|
|
4568
|
+
threshold: int,
|
|
4569
|
+
deadline: Optional[float],
|
|
4570
|
+
arrow_line: Optional[int] = None,
|
|
4571
|
+
) -> Optional[tuple[int, bool]]:
|
|
4572
|
+
depth = 0
|
|
4573
|
+
opened = False
|
|
4574
|
+
state = "code"
|
|
4575
|
+
escaped = False
|
|
4576
|
+
regex_char_class = False
|
|
4577
|
+
paren_depth = 0
|
|
4578
|
+
bracket_depth = 0
|
|
4579
|
+
type_brace_depth = 0
|
|
4580
|
+
arrow_seen = arrow_line is None
|
|
4581
|
+
limit = min(len(lines), start + threshold + 1)
|
|
4582
|
+
|
|
4583
|
+
for line_index in range(start, limit):
|
|
4584
|
+
if (line_index - start) % 32 == 0:
|
|
4585
|
+
_ensure_structure_deadline(deadline)
|
|
4586
|
+
line = lines[line_index]
|
|
4587
|
+
char_index = 0
|
|
4588
|
+
while char_index < len(line):
|
|
4589
|
+
char = line[char_index]
|
|
4590
|
+
next_char = line[char_index + 1] if char_index + 1 < len(line) else ""
|
|
4591
|
+
|
|
4592
|
+
if state == "block_comment":
|
|
4593
|
+
if char == "*" and next_char == "/":
|
|
4594
|
+
state = "code"
|
|
4595
|
+
char_index += 2
|
|
4596
|
+
continue
|
|
4597
|
+
char_index += 1
|
|
4598
|
+
continue
|
|
4599
|
+
if state == "regex":
|
|
4600
|
+
if escaped:
|
|
4601
|
+
escaped = False
|
|
4602
|
+
elif char == "\\":
|
|
4603
|
+
escaped = True
|
|
4604
|
+
elif char == "[":
|
|
4605
|
+
regex_char_class = True
|
|
4606
|
+
elif char == "]":
|
|
4607
|
+
regex_char_class = False
|
|
4608
|
+
elif char == "/" and not regex_char_class:
|
|
4609
|
+
state = "code"
|
|
4610
|
+
char_index += 1
|
|
4611
|
+
continue
|
|
4612
|
+
if state in {"single_quote", "double_quote"}:
|
|
4613
|
+
if escaped:
|
|
4614
|
+
escaped = False
|
|
4615
|
+
elif char == "\\":
|
|
4616
|
+
escaped = True
|
|
4617
|
+
elif (state == "single_quote" and char == "'") or (
|
|
4618
|
+
state == "double_quote" and char == '"'
|
|
4619
|
+
):
|
|
4620
|
+
state = "code"
|
|
4621
|
+
char_index += 1
|
|
4622
|
+
continue
|
|
4623
|
+
if state == "backtick":
|
|
4624
|
+
if escaped:
|
|
4625
|
+
escaped = False
|
|
4626
|
+
elif char == "\\":
|
|
4627
|
+
escaped = True
|
|
4628
|
+
elif char == "`":
|
|
4629
|
+
state = "code"
|
|
4630
|
+
char_index += 1
|
|
4631
|
+
continue
|
|
4632
|
+
|
|
4633
|
+
if char == "/" and next_char == "/":
|
|
4634
|
+
break
|
|
4635
|
+
if char == "/" and next_char == "*":
|
|
4636
|
+
state = "block_comment"
|
|
4637
|
+
char_index += 2
|
|
4638
|
+
continue
|
|
4639
|
+
if char == "/" and _looks_like_regex_start(line, char_index):
|
|
4640
|
+
state = "regex"
|
|
4641
|
+
regex_char_class = False
|
|
4642
|
+
escaped = False
|
|
4643
|
+
char_index += 1
|
|
4644
|
+
continue
|
|
4645
|
+
if char == "=" and next_char == ">":
|
|
4646
|
+
if arrow_line is not None and line_index == arrow_line:
|
|
4647
|
+
arrow_seen = True
|
|
4648
|
+
char_index += 2
|
|
4649
|
+
continue
|
|
4650
|
+
if char == "'":
|
|
4651
|
+
state = "single_quote"
|
|
4652
|
+
escaped = False
|
|
4653
|
+
elif char == '"':
|
|
4654
|
+
state = "double_quote"
|
|
4655
|
+
escaped = False
|
|
4656
|
+
elif char == "`":
|
|
4657
|
+
state = "backtick"
|
|
4658
|
+
escaped = False
|
|
4659
|
+
elif not opened and char == "(":
|
|
4660
|
+
paren_depth += 1
|
|
4661
|
+
elif not opened and char == ")":
|
|
4662
|
+
paren_depth = max(0, paren_depth - 1)
|
|
4663
|
+
elif not opened and char == "[":
|
|
4664
|
+
bracket_depth += 1
|
|
4665
|
+
elif not opened and char == "]":
|
|
4666
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
4667
|
+
elif char == "{":
|
|
4668
|
+
if not opened:
|
|
4669
|
+
if not arrow_seen:
|
|
4670
|
+
char_index += 1
|
|
4671
|
+
continue
|
|
4672
|
+
if arrow_line is None and (paren_depth > 0 or bracket_depth > 0):
|
|
4673
|
+
char_index += 1
|
|
4674
|
+
continue
|
|
4675
|
+
if type_brace_depth > 0:
|
|
4676
|
+
type_brace_depth += 1
|
|
4677
|
+
char_index += 1
|
|
4678
|
+
continue
|
|
4679
|
+
if arrow_line is None and _looks_like_return_type_brace(line, char_index):
|
|
4680
|
+
type_brace_depth = 1
|
|
4681
|
+
char_index += 1
|
|
4682
|
+
continue
|
|
4683
|
+
opened = True
|
|
4684
|
+
depth += 1
|
|
4685
|
+
elif char == "}" and not opened and type_brace_depth > 0:
|
|
4686
|
+
type_brace_depth -= 1
|
|
4687
|
+
elif char == "}" and opened:
|
|
4688
|
+
depth -= 1
|
|
4689
|
+
if depth == 0:
|
|
4690
|
+
return line_index + 1, False
|
|
4691
|
+
char_index += 1
|
|
4692
|
+
if limit < len(lines):
|
|
4693
|
+
return limit, True
|
|
4694
|
+
return None
|
|
4695
|
+
|
|
4696
|
+
|
|
4697
|
+
_EXPRESSION_CONTINUATION_SUFFIXES = (
|
|
4698
|
+
"?", ":", ".", ",", "+", "-", "*", "/", "%", "&&", "||", "??", "=>",
|
|
4699
|
+
)
|
|
4700
|
+
_EXPRESSION_CONTINUATION_PREFIXES = (
|
|
4701
|
+
".", "?.", "+", "-", "*", "/", "%", "&&", "||", "??", "?", ":", ",",
|
|
4702
|
+
)
|
|
4703
|
+
_JSX_TAG_RE = re.compile(r"</?>|</?[A-Za-z][A-Za-z0-9_.:-]*(?:\s[^<>]*?)?/?>")
|
|
4704
|
+
|
|
4705
|
+
|
|
4706
|
+
def _expression_arrow_end(
|
|
4707
|
+
lines: list[str],
|
|
4708
|
+
start: int,
|
|
4709
|
+
arrow_line: int,
|
|
4710
|
+
threshold: int,
|
|
4711
|
+
deadline: Optional[float],
|
|
4712
|
+
suffix: str,
|
|
4713
|
+
) -> tuple[int, bool]:
|
|
4714
|
+
indent = len(lines[start]) - len(lines[start].lstrip())
|
|
4715
|
+
limit = min(len(lines), start + threshold + 1)
|
|
4716
|
+
state = "code"
|
|
4717
|
+
escaped = False
|
|
4718
|
+
regex_char_class = False
|
|
4719
|
+
paren_depth = bracket_depth = brace_depth = 0
|
|
4720
|
+
jsx_mode = False
|
|
4721
|
+
jsx_depth = 0
|
|
4722
|
+
saw_jsx_tag = False
|
|
4723
|
+
arrow_column = lines[arrow_line].find("=>")
|
|
4724
|
+
|
|
4725
|
+
for line_index in range(arrow_line, limit):
|
|
4726
|
+
if (line_index - arrow_line) % 32 == 0:
|
|
4727
|
+
_ensure_structure_deadline(deadline)
|
|
4728
|
+
line = lines[line_index]
|
|
4729
|
+
char_index = arrow_column + 2 if line_index == arrow_line else 0
|
|
4730
|
+
visible: list[str] = []
|
|
4731
|
+
top_level_semicolon = False
|
|
4732
|
+
while char_index < len(line):
|
|
4733
|
+
char = line[char_index]
|
|
4734
|
+
next_char = line[char_index + 1] if char_index + 1 < len(line) else ""
|
|
4735
|
+
if state == "block_comment":
|
|
4736
|
+
if char == "*" and next_char == "/":
|
|
4737
|
+
state = "code"
|
|
4738
|
+
char_index += 2
|
|
4739
|
+
continue
|
|
4740
|
+
char_index += 1
|
|
4741
|
+
continue
|
|
4742
|
+
if state == "regex":
|
|
4743
|
+
if escaped:
|
|
4744
|
+
escaped = False
|
|
4745
|
+
elif char == "\\":
|
|
4746
|
+
escaped = True
|
|
4747
|
+
elif char == "[":
|
|
4748
|
+
regex_char_class = True
|
|
4749
|
+
elif char == "]":
|
|
4750
|
+
regex_char_class = False
|
|
4751
|
+
elif char == "/" and not regex_char_class:
|
|
4752
|
+
state = "code"
|
|
4753
|
+
char_index += 1
|
|
4754
|
+
continue
|
|
4755
|
+
if state in {"single_quote", "double_quote", "backtick"}:
|
|
4756
|
+
quote = {"single_quote": "'", "double_quote": '"', "backtick": "`"}[state]
|
|
4757
|
+
if escaped:
|
|
4758
|
+
escaped = False
|
|
4759
|
+
elif char == "\\":
|
|
4760
|
+
escaped = True
|
|
4761
|
+
elif char == quote:
|
|
4762
|
+
state = "code"
|
|
4763
|
+
char_index += 1
|
|
4764
|
+
continue
|
|
4765
|
+
if char == "/" and next_char == "/":
|
|
4766
|
+
break
|
|
4767
|
+
if char == "/" and next_char == "*":
|
|
4768
|
+
state = "block_comment"
|
|
4769
|
+
char_index += 2
|
|
4770
|
+
continue
|
|
4771
|
+
if char == "/" and _looks_like_regex_start(line, char_index):
|
|
4772
|
+
state = "regex"
|
|
4773
|
+
regex_char_class = False
|
|
4774
|
+
escaped = False
|
|
4775
|
+
visible.append(" ")
|
|
4776
|
+
char_index += 1
|
|
4777
|
+
continue
|
|
4778
|
+
if char in {"'", '"', "`"}:
|
|
4779
|
+
state = {"'": "single_quote", '"': "double_quote", "`": "backtick"}[char]
|
|
4780
|
+
escaped = False
|
|
4781
|
+
visible.append(" ")
|
|
4782
|
+
else:
|
|
4783
|
+
visible.append(char)
|
|
4784
|
+
if char == "(":
|
|
4785
|
+
paren_depth += 1
|
|
4786
|
+
elif char == ")":
|
|
4787
|
+
paren_depth = max(0, paren_depth - 1)
|
|
4788
|
+
elif char == "[":
|
|
4789
|
+
bracket_depth += 1
|
|
4790
|
+
elif char == "]":
|
|
4791
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
4792
|
+
elif char == "{":
|
|
4793
|
+
brace_depth += 1
|
|
4794
|
+
elif char == "}":
|
|
4795
|
+
brace_depth = max(0, brace_depth - 1)
|
|
4796
|
+
elif char == ";" and not (paren_depth or bracket_depth or brace_depth):
|
|
4797
|
+
top_level_semicolon = True
|
|
4798
|
+
char_index += 1
|
|
4799
|
+
|
|
4800
|
+
code = "".join(visible).strip()
|
|
4801
|
+
if suffix in {".jsx", ".tsx"} and not jsx_mode and code.startswith("<") and not code.startswith(("<=", "<<")):
|
|
4802
|
+
jsx_mode = True
|
|
4803
|
+
if jsx_mode:
|
|
4804
|
+
for match in _JSX_TAG_RE.finditer(code):
|
|
4805
|
+
token = match.group(0)
|
|
4806
|
+
saw_jsx_tag = True
|
|
4807
|
+
if token.startswith("</"):
|
|
4808
|
+
jsx_depth = max(0, jsx_depth - 1)
|
|
4809
|
+
elif token not in {"</>"} and not token.endswith("/>"):
|
|
4810
|
+
jsx_depth += 1
|
|
4811
|
+
|
|
4812
|
+
if paren_depth or bracket_depth or brace_depth or jsx_depth > 0:
|
|
4813
|
+
continue
|
|
4814
|
+
if top_level_semicolon:
|
|
4815
|
+
return line_index + 1, False
|
|
4816
|
+
if jsx_mode and saw_jsx_tag:
|
|
4817
|
+
return line_index + 1, False
|
|
4818
|
+
|
|
4819
|
+
next_index = line_index + 1
|
|
4820
|
+
while next_index < len(lines) and not lines[next_index].strip():
|
|
4821
|
+
next_index += 1
|
|
4822
|
+
if next_index >= len(lines):
|
|
4823
|
+
return line_index + 1, False
|
|
4824
|
+
next_text = lines[next_index].strip()
|
|
4825
|
+
next_indent = len(lines[next_index]) - len(lines[next_index].lstrip())
|
|
4826
|
+
if code.endswith(_EXPRESSION_CONTINUATION_SUFFIXES):
|
|
4827
|
+
continue
|
|
4828
|
+
if next_indent > indent:
|
|
4829
|
+
continue
|
|
4830
|
+
if next_text.startswith(_EXPRESSION_CONTINUATION_PREFIXES):
|
|
4831
|
+
continue
|
|
4832
|
+
if jsx_mode and next_text.startswith(("</", ">")):
|
|
4833
|
+
continue
|
|
4834
|
+
return line_index + 1, False
|
|
4835
|
+
|
|
4836
|
+
return limit, limit < len(lines)
|
|
4837
|
+
|
|
4838
|
+
|
|
4839
|
+
def _function_size_warnings(
|
|
4840
|
+
path: str,
|
|
4841
|
+
lines: list[str],
|
|
4842
|
+
threshold: int,
|
|
4843
|
+
deadline: Optional[float] = None,
|
|
4844
|
+
) -> list[str]:
|
|
4353
4845
|
if threshold <= 0:
|
|
4354
4846
|
return []
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4847
|
+
suffix = Path(path).suffix.lower()
|
|
4848
|
+
if suffix == ".py":
|
|
4849
|
+
ranges = _python_function_ranges(lines, threshold, deadline)
|
|
4850
|
+
else:
|
|
4851
|
+
candidates: dict[int, Optional[int]] = {}
|
|
4852
|
+
for index, line in enumerate(lines):
|
|
4853
|
+
if index % 256 == 0:
|
|
4854
|
+
_ensure_structure_deadline(deadline)
|
|
4855
|
+
if _ARROW_DECLARATION_RE.search(line):
|
|
4856
|
+
arrow_line = _find_arrow_line(lines, index, deadline)
|
|
4857
|
+
if arrow_line is not None:
|
|
4858
|
+
candidates[index] = arrow_line
|
|
4859
|
+
continue
|
|
4860
|
+
if _ARROW_FUNCTION_BODY_RE.search(line):
|
|
4861
|
+
candidates[index] = index
|
|
4862
|
+
elif Path(path).name == "Jenkinsfile":
|
|
4863
|
+
if re.match(r"^\s*def\s+[A-Za-z_]\w*\s*\(", line):
|
|
4864
|
+
candidates[index] = None
|
|
4865
|
+
elif _FUNCTION_START_RE.search(line):
|
|
4866
|
+
candidates[index] = None
|
|
4867
|
+
|
|
4868
|
+
ranges = []
|
|
4869
|
+
for start, arrow_line in sorted(candidates.items()):
|
|
4870
|
+
_ensure_structure_deadline(deadline)
|
|
4871
|
+
if arrow_line is not None and _arrow_body_kind(lines, arrow_line) == "expression":
|
|
4872
|
+
end, truncated = _expression_arrow_end(
|
|
4873
|
+
lines, start, arrow_line, threshold, deadline, suffix
|
|
4874
|
+
)
|
|
4875
|
+
ranges.append((start, end, truncated))
|
|
4876
|
+
continue
|
|
4877
|
+
result = _brace_function_end(
|
|
4878
|
+
lines, start, threshold, deadline, arrow_line=arrow_line
|
|
4879
|
+
)
|
|
4880
|
+
if result is not None:
|
|
4881
|
+
end, truncated = result
|
|
4882
|
+
ranges.append((start, end, truncated))
|
|
4883
|
+
|
|
4359
4884
|
warnings: list[str] = []
|
|
4360
|
-
for
|
|
4361
|
-
end = starts[pos + 1] if pos + 1 < len(starts) else len(lines)
|
|
4885
|
+
for start, end, truncated in ranges:
|
|
4362
4886
|
size = end - start
|
|
4363
|
-
if size
|
|
4364
|
-
|
|
4887
|
+
if size <= threshold:
|
|
4888
|
+
continue
|
|
4889
|
+
qualifier = "at least " if truncated else ""
|
|
4890
|
+
warnings.append(
|
|
4891
|
+
f"{path}:{start + 1} function-like block has {qualifier}{size} lines (warn>{threshold})"
|
|
4892
|
+
)
|
|
4365
4893
|
return warnings
|
|
4366
4894
|
|
|
4367
4895
|
|
|
@@ -5013,6 +5541,7 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
5013
5541
|
cfg = _load_cfg(repo)
|
|
5014
5542
|
requested_scope = str(getattr(args, "scope", "") or "").strip().lower()
|
|
5015
5543
|
base_ref = str(getattr(args, "base", "") or "")
|
|
5544
|
+
deadline = getattr(args, "deadline", None)
|
|
5016
5545
|
selected_scope = _select_structure_scope(cfg, repo, requested_scope, base_ref)
|
|
5017
5546
|
|
|
5018
5547
|
structure_cfg = _structure_cfg(cfg)
|
|
@@ -5025,6 +5554,9 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
5025
5554
|
warn_function_lines = _int_config(structure_cfg.get("max_function_lines_warn"), 120)
|
|
5026
5555
|
max_added_to_large = _int_config(structure_cfg.get("max_added_lines_to_large_file"), 80)
|
|
5027
5556
|
block_large_growth = _bool_config(structure_cfg.get("block_new_responsibility_in_large_file"), True)
|
|
5557
|
+
block_warnings = enforcement == "blocking" and _bool_config(
|
|
5558
|
+
structure_cfg.get("block_warnings"), False
|
|
5559
|
+
)
|
|
5028
5560
|
layer_cfg = _layer_rules_config(structure_cfg)
|
|
5029
5561
|
block_layer_violations = _bool_config(layer_cfg.get("block"), True)
|
|
5030
5562
|
|
|
@@ -5036,12 +5568,14 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
5036
5568
|
inspected = 0
|
|
5037
5569
|
|
|
5038
5570
|
for path in paths:
|
|
5571
|
+
_ensure_structure_deadline(deadline)
|
|
5039
5572
|
lines = _read_text_lines(repo, path)
|
|
5040
5573
|
if lines is None:
|
|
5041
5574
|
continue
|
|
5042
5575
|
inspected += 1
|
|
5043
5576
|
line_count = len(lines)
|
|
5044
5577
|
accepted_debt = _is_structure_accepted_debt(path, structure_cfg)
|
|
5578
|
+
block_path_size_warnings = block_warnings and not accepted_debt
|
|
5045
5579
|
if block_file_lines > 0 and line_count > block_file_lines:
|
|
5046
5580
|
message = f"{path} has {line_count} lines (block>{block_file_lines}); split responsibilities before adding more work"
|
|
5047
5581
|
if accepted_debt:
|
|
@@ -5049,7 +5583,11 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
5049
5583
|
else:
|
|
5050
5584
|
blocking.append(message)
|
|
5051
5585
|
elif warn_file_lines > 0 and line_count > warn_file_lines:
|
|
5052
|
-
|
|
5586
|
+
message = (
|
|
5587
|
+
f"{path} has {line_count} lines (warn>{warn_file_lines}); "
|
|
5588
|
+
"prefer extraction before extending it"
|
|
5589
|
+
)
|
|
5590
|
+
(blocking if block_path_size_warnings else warnings).append(message)
|
|
5053
5591
|
|
|
5054
5592
|
added_lines = added_by_path.get(path, 0)
|
|
5055
5593
|
if (
|
|
@@ -5065,8 +5603,15 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
5065
5603
|
"extract a module/helper or document why this is intentionally co-located"
|
|
5066
5604
|
)
|
|
5067
5605
|
|
|
5068
|
-
|
|
5606
|
+
function_warnings = _function_size_warnings(
|
|
5607
|
+
path,
|
|
5608
|
+
lines,
|
|
5609
|
+
warn_function_lines,
|
|
5610
|
+
deadline=deadline,
|
|
5611
|
+
)
|
|
5612
|
+
(blocking if block_path_size_warnings else warnings).extend(function_warnings)
|
|
5069
5613
|
boundary_issues = _structure_boundary_issues(path, lines, structure_cfg)
|
|
5614
|
+
_ensure_structure_deadline(deadline)
|
|
5070
5615
|
if block_layer_violations:
|
|
5071
5616
|
blocking.extend(boundary_issues)
|
|
5072
5617
|
else:
|
|
@@ -5091,6 +5636,7 @@ def cmd_structure_check(args: argparse.Namespace) -> None:
|
|
|
5091
5636
|
result = {
|
|
5092
5637
|
"scope": selected_scope,
|
|
5093
5638
|
"enforcement": enforcement,
|
|
5639
|
+
"block_warnings": block_warnings,
|
|
5094
5640
|
"inspected_files": inspected,
|
|
5095
5641
|
"blocking": blocking,
|
|
5096
5642
|
"warnings": warnings,
|
|
@@ -5136,6 +5682,7 @@ def _run_structure_check_for_gate(
|
|
|
5136
5682
|
scope=selected_scope,
|
|
5137
5683
|
base=base_ref,
|
|
5138
5684
|
json=False,
|
|
5685
|
+
deadline=deadline,
|
|
5139
5686
|
)
|
|
5140
5687
|
)
|
|
5141
5688
|
return ["structure_check"]
|
|
@@ -7293,6 +7840,13 @@ def cmd_doctor(args: argparse.Namespace) -> None:
|
|
|
7293
7840
|
enforcement = _text(structure_cfg.get("enforcement")).lower()
|
|
7294
7841
|
if enforcement and enforcement not in {"advisory", "blocking"}:
|
|
7295
7842
|
validation_errors.append("structure.enforcement must be advisory or blocking")
|
|
7843
|
+
raw_block_warnings = structure_cfg.get("block_warnings")
|
|
7844
|
+
if "block_warnings" in structure_cfg and not isinstance(raw_block_warnings, bool):
|
|
7845
|
+
validation_errors.append("structure.block_warnings must be a boolean")
|
|
7846
|
+
elif raw_block_warnings is True and enforcement != "blocking":
|
|
7847
|
+
advisories.append(
|
|
7848
|
+
"structure.block_warnings has no effect unless structure.enforcement=blocking"
|
|
7849
|
+
)
|
|
7296
7850
|
gate_cfg = _gate_cfg(cfg)
|
|
7297
7851
|
if "full_on" in gate_cfg:
|
|
7298
7852
|
validation_errors.append("gate.full_on is legacy automatic full escalation; run ap.py upgrade")
|