saucisson-gabarit 0.1.0__tar.gz
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.
- saucisson_gabarit-0.1.0/.gitignore +41 -0
- saucisson_gabarit-0.1.0/PKG-INFO +7 -0
- saucisson_gabarit-0.1.0/pyproject.toml +18 -0
- saucisson_gabarit-0.1.0/src/gabarit/__init__.py +7 -0
- saucisson_gabarit-0.1.0/src/gabarit/__main__.py +82 -0
- saucisson_gabarit-0.1.0/src/gabarit/_shared.py +279 -0
- saucisson_gabarit-0.1.0/src/gabarit/adr_sequence.py +195 -0
- saucisson_gabarit-0.1.0/src/gabarit/audit_cards.py +225 -0
- saucisson_gabarit-0.1.0/src/gabarit/day.py +1081 -0
- saucisson_gabarit-0.1.0/src/gabarit/english.py +444 -0
- saucisson_gabarit-0.1.0/src/gabarit/labels.py +152 -0
- saucisson_gabarit-0.1.0/src/gabarit/license_gate.py +208 -0
- saucisson_gabarit-0.1.0/src/gabarit/merge.py +478 -0
- saucisson_gabarit-0.1.0/src/gabarit/night.py +1645 -0
- saucisson_gabarit-0.1.0/src/gabarit/probe.py +395 -0
- saucisson_gabarit-0.1.0/src/gabarit/protection.py +209 -0
- saucisson_gabarit-0.1.0/src/gabarit/py.typed +0 -0
- saucisson_gabarit-0.1.0/src/gabarit/review.py +1049 -0
- saucisson_gabarit-0.1.0/src/gabarit/session.py +619 -0
- saucisson_gabarit-0.1.0/src/gabarit/sync_main.py +357 -0
- saucisson_gabarit-0.1.0/tests/test_adr_sequence.py +140 -0
- saucisson_gabarit-0.1.0/tests/test_audit_cards.py +44 -0
- saucisson_gabarit-0.1.0/tests/test_cli.py +24 -0
- saucisson_gabarit-0.1.0/tests/test_day.py +704 -0
- saucisson_gabarit-0.1.0/tests/test_english.py +175 -0
- saucisson_gabarit-0.1.0/tests/test_labels.py +91 -0
- saucisson_gabarit-0.1.0/tests/test_license_gate.py +179 -0
- saucisson_gabarit-0.1.0/tests/test_merge.py +439 -0
- saucisson_gabarit-0.1.0/tests/test_night.py +316 -0
- saucisson_gabarit-0.1.0/tests/test_night_escalation.py +62 -0
- saucisson_gabarit-0.1.0/tests/test_probe.py +265 -0
- saucisson_gabarit-0.1.0/tests/test_protection.py +196 -0
- saucisson_gabarit-0.1.0/tests/test_review.py +649 -0
- saucisson_gabarit-0.1.0/tests/test_session.py +407 -0
- saucisson_gabarit-0.1.0/tests/test_shared.py +38 -0
- saucisson_gabarit-0.1.0/tests/test_sync_main.py +269 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.eggs/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
wheels/
|
|
9
|
+
|
|
10
|
+
# Environments
|
|
11
|
+
.venv/
|
|
12
|
+
.env
|
|
13
|
+
|
|
14
|
+
# Tooling caches
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.mypy_cache/
|
|
17
|
+
.ruff_cache/
|
|
18
|
+
.coverage
|
|
19
|
+
coverage.xml
|
|
20
|
+
htmlcov/
|
|
21
|
+
.mutmut-cache/
|
|
22
|
+
|
|
23
|
+
# Reviewer working directory (ADR-0006) - bundles, diffs, reports, context.
|
|
24
|
+
.review/
|
|
25
|
+
|
|
26
|
+
# Unattended loop trace (ADR-0007) - verbose worklists, anti-spin decisions.
|
|
27
|
+
.night/
|
|
28
|
+
|
|
29
|
+
# Attended day loop state (ADR-0015) - trace, prompts, consumed references.
|
|
30
|
+
.day/
|
|
31
|
+
|
|
32
|
+
# uv lock is COMMITTED (ADR-0001) - never ignore it.
|
|
33
|
+
|
|
34
|
+
# Editors and OS
|
|
35
|
+
.idea/
|
|
36
|
+
.vscode/
|
|
37
|
+
.DS_Store
|
|
38
|
+
|
|
39
|
+
# Windows NTFS alternate-data-stream artifacts, never tracked
|
|
40
|
+
# (zone identifiers ride along file copies from Windows checkouts).
|
|
41
|
+
*Zone.Identifier
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "saucisson-gabarit"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "The protocol drivers of the template, one package behind one CLI (ADR-0018)."
|
|
5
|
+
license = "MIT"
|
|
6
|
+
authors = [{ name = "Alban Linard" }]
|
|
7
|
+
requires-python = ">=3.15"
|
|
8
|
+
dependencies = []
|
|
9
|
+
|
|
10
|
+
[project.scripts]
|
|
11
|
+
gabarit = "gabarit.__main__:cli"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["hatchling>=1.33.0"]
|
|
15
|
+
build-backend = "hatchling.build"
|
|
16
|
+
|
|
17
|
+
[tool.hatch.build.targets.wheel]
|
|
18
|
+
packages = ["src/gabarit"]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Driver commands of the template: one package, one CLI (ADR-0001).
|
|
2
|
+
|
|
3
|
+
Every driver lives in this package and runs as one subcommand of
|
|
4
|
+
``python -m gabarit`` - and, one-to-one, as one ``make`` target, which
|
|
5
|
+
stays THE agent surface (ADR-0001). Each module is one driver domain;
|
|
6
|
+
``_shared`` carries the plumbing shared by all of them.
|
|
7
|
+
"""
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Command-line entry point: the console command ``gabarit <command>`` (ADR-0001, ADR-0018).
|
|
2
|
+
|
|
3
|
+
Make stays THE agent surface: every subcommand below matches a make
|
|
4
|
+
target one-to-one (ADR-0001). This dispatcher only wires the
|
|
5
|
+
subcommands to their domain modules; it owns no protocol of its own.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import sys
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
from gabarit import (
|
|
13
|
+
adr_sequence,
|
|
14
|
+
audit_cards,
|
|
15
|
+
day,
|
|
16
|
+
english,
|
|
17
|
+
labels,
|
|
18
|
+
license_gate,
|
|
19
|
+
merge,
|
|
20
|
+
night,
|
|
21
|
+
probe,
|
|
22
|
+
protection,
|
|
23
|
+
review,
|
|
24
|
+
sync_main,
|
|
25
|
+
)
|
|
26
|
+
from gabarit._shared import load_environment_file
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from collections.abc import Sequence
|
|
30
|
+
from typing import Protocol
|
|
31
|
+
|
|
32
|
+
class Runner(Protocol):
|
|
33
|
+
"""Subcommand entry point: keyword-only, like every ``run()``."""
|
|
34
|
+
|
|
35
|
+
def __call__(self, *, arguments: argparse.Namespace) -> int:
|
|
36
|
+
"""Dispatch to the subcommand's ``run()``."""
|
|
37
|
+
...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
COMMAND_MODULES = (
|
|
41
|
+
adr_sequence,
|
|
42
|
+
audit_cards,
|
|
43
|
+
merge,
|
|
44
|
+
probe,
|
|
45
|
+
protection,
|
|
46
|
+
review,
|
|
47
|
+
sync_main,
|
|
48
|
+
night,
|
|
49
|
+
day,
|
|
50
|
+
english,
|
|
51
|
+
license_gate,
|
|
52
|
+
labels,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def build_argument_parser() -> argparse.ArgumentParser:
|
|
57
|
+
"""Build the CLI: one subparser per driver subcommand."""
|
|
58
|
+
argument_parser = argparse.ArgumentParser(
|
|
59
|
+
prog="gabarit",
|
|
60
|
+
description="Project drivers: one subcommand per make target (ADR-0001).",
|
|
61
|
+
)
|
|
62
|
+
subparsers = argument_parser.add_subparsers(dest="command", required=True)
|
|
63
|
+
for module in COMMAND_MODULES:
|
|
64
|
+
module.register(subparsers)
|
|
65
|
+
return argument_parser
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def main(*, command_line_arguments: Sequence[str]) -> int:
|
|
69
|
+
"""Entry point: environment loaded once, subcommand dispatched."""
|
|
70
|
+
load_environment_file()
|
|
71
|
+
arguments = build_argument_parser().parse_args(command_line_arguments)
|
|
72
|
+
runner: Runner = arguments.runner
|
|
73
|
+
return runner(arguments=arguments)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def cli() -> None:
|
|
77
|
+
"""Console script entry point (``[project.scripts] gabarit``)."""
|
|
78
|
+
sys.exit(main(command_line_arguments=sys.argv[1:]))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if __name__ == "__main__":
|
|
82
|
+
cli()
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"""Shared plumbing for every driver command (ADR-0001).
|
|
2
|
+
|
|
3
|
+
Each module of this package is one driver domain; the plumbing every
|
|
4
|
+
driver repeats - the ``.env`` loader, the repository slug, the git
|
|
5
|
+
and GitHub runners and the protocol exit codes - lives here once,
|
|
6
|
+
so a driver module carries only its own protocol.
|
|
7
|
+
|
|
8
|
+
Stdlib only, on purpose: every driver imports this module and must
|
|
9
|
+
run without any third-party package anywhere (ADR-0001). The GitHub
|
|
10
|
+
transport is the ``gh`` CLI called over subprocess: the drivers
|
|
11
|
+
inherit its authentication instead of carrying tokens and HTTP
|
|
12
|
+
clients of their own.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
19
|
+
import tomllib
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import TYPE_CHECKING
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from collections.abc import Sequence
|
|
26
|
+
|
|
27
|
+
ENVIRONMENT_FILE_NAME = ".env"
|
|
28
|
+
CLAIM_LABEL_NAME = "night-claimed"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def resolve_repository_root(*, invocation_directory: Path | None = None) -> Path:
|
|
32
|
+
"""Return the working repository root from the invocation directory.
|
|
33
|
+
|
|
34
|
+
The package can be installed (ADR-0018): its own ``__file__`` then
|
|
35
|
+
lives inside site-packages, not inside a repository, so the root is
|
|
36
|
+
the nearest enclosing git checkout of the invocation directory, or
|
|
37
|
+
the invocation directory itself when none encloses it.
|
|
38
|
+
"""
|
|
39
|
+
start = invocation_directory if invocation_directory is not None else Path.cwd()
|
|
40
|
+
resolved = start.resolve()
|
|
41
|
+
for candidate in (resolved, *resolved.parents):
|
|
42
|
+
if (candidate / ".git").exists():
|
|
43
|
+
return candidate
|
|
44
|
+
return resolved
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
REPOSITORY_ROOT = resolve_repository_root()
|
|
48
|
+
|
|
49
|
+
ROUTING_TABLE_PATH = REPOSITORY_ROOT / ".vibe" / "routing.toml"
|
|
50
|
+
|
|
51
|
+
EXIT_CODE_SUCCESS = 0
|
|
52
|
+
EXIT_CODE_CRITERIA_UNMET = 1
|
|
53
|
+
EXIT_CODE_PROTOCOL_FAILURE = 2
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def load_environment_file() -> None:
|
|
57
|
+
"""Read KEY=VALUE pairs from .env into the environment if unset.
|
|
58
|
+
|
|
59
|
+
The notify tool already loads .env through its settings; these
|
|
60
|
+
commands read it for the same settings. Existing environment
|
|
61
|
+
variables always win.
|
|
62
|
+
"""
|
|
63
|
+
environment_path = REPOSITORY_ROOT / ENVIRONMENT_FILE_NAME
|
|
64
|
+
if not environment_path.is_file():
|
|
65
|
+
return
|
|
66
|
+
for raw_line in environment_path.read_text(encoding="utf-8").splitlines():
|
|
67
|
+
line = raw_line.strip()
|
|
68
|
+
if not line or line.startswith("#") or "=" not in line:
|
|
69
|
+
continue
|
|
70
|
+
key, _, value = line.partition("=")
|
|
71
|
+
os.environ.setdefault(key.strip(), value.strip())
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def resolve_repository_slug() -> str:
|
|
75
|
+
"""Return "owner/repository" from the origin remote, or fail closed."""
|
|
76
|
+
remote_url = try_git(arguments=["remote", "get-url", "origin"]).stdout.strip()
|
|
77
|
+
marker = "github.com"
|
|
78
|
+
separator_index = remote_url.find(marker)
|
|
79
|
+
if separator_index == -1:
|
|
80
|
+
print(
|
|
81
|
+
f"Error: the origin remote is not on GitHub: {remote_url}",
|
|
82
|
+
file=sys.stderr,
|
|
83
|
+
)
|
|
84
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE)
|
|
85
|
+
slug = remote_url[separator_index + len(marker) :]
|
|
86
|
+
slug = slug.removeprefix(":").removeprefix("/").removesuffix(".git").strip("/")
|
|
87
|
+
owner, separator, repository = slug.partition("/")
|
|
88
|
+
if not owner or not separator or not repository.strip("/"):
|
|
89
|
+
print(
|
|
90
|
+
f"Error: cannot parse owner/repository from remote: {remote_url}",
|
|
91
|
+
file=sys.stderr,
|
|
92
|
+
)
|
|
93
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE)
|
|
94
|
+
return f"{owner}/{repository.strip('/')}"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def try_git(*, arguments: Sequence[str]) -> subprocess.CompletedProcess[str]:
|
|
98
|
+
"""Run one git command in the repository; the caller checks the exit."""
|
|
99
|
+
return subprocess.run(
|
|
100
|
+
["git", *arguments],
|
|
101
|
+
cwd=REPOSITORY_ROOT,
|
|
102
|
+
capture_output=True,
|
|
103
|
+
text=True,
|
|
104
|
+
check=False,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def run_git(*, arguments: Sequence[str]) -> str:
|
|
109
|
+
"""Run git in the repository, capturing stdout; fail closed."""
|
|
110
|
+
completed_process = try_git(arguments=arguments)
|
|
111
|
+
if completed_process.returncode != EXIT_CODE_SUCCESS:
|
|
112
|
+
print(
|
|
113
|
+
f"Error: git {' '.join(arguments)} failed: {completed_process.stderr.strip()}",
|
|
114
|
+
file=sys.stderr,
|
|
115
|
+
)
|
|
116
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE)
|
|
117
|
+
return completed_process.stdout
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def try_gh(*, arguments: Sequence[str]) -> subprocess.CompletedProcess[str]:
|
|
121
|
+
"""Run one gh command in the repository; the caller checks the exit.
|
|
122
|
+
|
|
123
|
+
The caller owns the failure policy: a refuse-to-work driver fails
|
|
124
|
+
closed (see ``run_gh_json``), while a warn-and-continue path (the
|
|
125
|
+
night worklist) warns and proceeds.
|
|
126
|
+
"""
|
|
127
|
+
return subprocess.run(
|
|
128
|
+
["gh", *arguments],
|
|
129
|
+
cwd=REPOSITORY_ROOT,
|
|
130
|
+
capture_output=True,
|
|
131
|
+
text=True,
|
|
132
|
+
check=False,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def run_gh_json(*, arguments: Sequence[str]) -> object:
|
|
137
|
+
"""Run one gh api query and parse its JSON; fail closed."""
|
|
138
|
+
completed_process = try_gh(arguments=arguments)
|
|
139
|
+
if completed_process.returncode != EXIT_CODE_SUCCESS:
|
|
140
|
+
print(
|
|
141
|
+
f"Error: gh {' '.join(arguments)} failed: {completed_process.stderr.strip()}",
|
|
142
|
+
file=sys.stderr,
|
|
143
|
+
)
|
|
144
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE)
|
|
145
|
+
try:
|
|
146
|
+
return json.loads(completed_process.stdout)
|
|
147
|
+
except json.JSONDecodeError as error:
|
|
148
|
+
print(
|
|
149
|
+
f"Error: gh {' '.join(arguments)} did not answer JSON: {error}",
|
|
150
|
+
file=sys.stderr,
|
|
151
|
+
)
|
|
152
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE) from error
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def fetch_pull_request(
|
|
156
|
+
*,
|
|
157
|
+
repository_slug: str,
|
|
158
|
+
pull_request_number: int,
|
|
159
|
+
) -> dict[str, object]:
|
|
160
|
+
"""Fetch the pull request through gh, or fail closed."""
|
|
161
|
+
pull_request = run_gh_json(
|
|
162
|
+
arguments=["api", f"repos/{repository_slug}/pulls/{pull_request_number}"]
|
|
163
|
+
)
|
|
164
|
+
if not isinstance(pull_request, dict):
|
|
165
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE)
|
|
166
|
+
return pull_request
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def fetch_check_runs(
|
|
170
|
+
*,
|
|
171
|
+
repository_slug: str,
|
|
172
|
+
head_sha: str,
|
|
173
|
+
) -> list[dict[str, object]]:
|
|
174
|
+
"""List the CI check runs of the pull request head, or fail closed."""
|
|
175
|
+
payload = run_gh_json(
|
|
176
|
+
arguments=["api", f"repos/{repository_slug}/commits/{head_sha}/check-runs"]
|
|
177
|
+
)
|
|
178
|
+
check_runs = payload.get("check_runs", []) if isinstance(payload, dict) else []
|
|
179
|
+
if not isinstance(check_runs, list):
|
|
180
|
+
return []
|
|
181
|
+
return [entry for entry in check_runs if isinstance(entry, dict)]
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def extract_head_sha(*, pull_request: dict[str, object]) -> str:
|
|
185
|
+
"""Return the pull request head SHA, or fail closed."""
|
|
186
|
+
head_container = pull_request.get("head")
|
|
187
|
+
head_sha = head_container.get("sha", "") if isinstance(head_container, dict) else ""
|
|
188
|
+
if not head_sha:
|
|
189
|
+
print("Error: the pull request has no head SHA.", file=sys.stderr)
|
|
190
|
+
raise SystemExit(EXIT_CODE_PROTOCOL_FAILURE)
|
|
191
|
+
return head_sha
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def extract_head_ref(*, pull_request: dict[str, object]) -> str:
|
|
195
|
+
"""Return the pull request head branch name, empty when unknown."""
|
|
196
|
+
head_container = pull_request.get("head")
|
|
197
|
+
return head_container.get("ref", "") if isinstance(head_container, dict) else ""
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
@dataclass(frozen=True)
|
|
201
|
+
class ModelProfile:
|
|
202
|
+
"""One routing profile: a model alias and a thinking level (ADR-0014)."""
|
|
203
|
+
|
|
204
|
+
name: str
|
|
205
|
+
model: str
|
|
206
|
+
thinking: str
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def load_routing_table(*, table_path: Path | None = None) -> dict[str, object] | None:
|
|
210
|
+
"""Load the routing table; None means absent - routing is an optimization, not protocol."""
|
|
211
|
+
resolved_path = table_path if table_path is not None else ROUTING_TABLE_PATH
|
|
212
|
+
if not resolved_path.is_file():
|
|
213
|
+
print(
|
|
214
|
+
f"Warning: no routing table at {resolved_path}"
|
|
215
|
+
" - sessions run on the configured default model."
|
|
216
|
+
)
|
|
217
|
+
return None
|
|
218
|
+
try:
|
|
219
|
+
raw_table: object = tomllib.loads(resolved_path.read_text(encoding="utf-8"))
|
|
220
|
+
except (OSError, tomllib.TOMLDecodeError) as table_error:
|
|
221
|
+
print(
|
|
222
|
+
f"Warning: the routing table is unreadable: {table_error}"
|
|
223
|
+
" - sessions run on the configured default model."
|
|
224
|
+
)
|
|
225
|
+
return None
|
|
226
|
+
if not isinstance(raw_table, dict):
|
|
227
|
+
print(
|
|
228
|
+
"Warning: the routing table is not an object"
|
|
229
|
+
" - sessions run on the configured default model."
|
|
230
|
+
)
|
|
231
|
+
return None
|
|
232
|
+
return raw_table
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def parse_model_profiles(*, routing_table: dict[str, object]) -> dict[str, ModelProfile]:
|
|
236
|
+
"""Narrow the [profiles] section into validated profiles; empty when absent."""
|
|
237
|
+
profiles_container: object = routing_table.get("profiles")
|
|
238
|
+
if not isinstance(profiles_container, dict):
|
|
239
|
+
print("Warning: the routing table has no [profiles] section.")
|
|
240
|
+
return {}
|
|
241
|
+
profiles: dict[str, ModelProfile] = {}
|
|
242
|
+
for profile_name, profile_entry in profiles_container.items():
|
|
243
|
+
if not isinstance(profile_entry, dict):
|
|
244
|
+
print(f"Warning: the profile {profile_name} is not an object - skipped.")
|
|
245
|
+
continue
|
|
246
|
+
model = profile_entry.get("model")
|
|
247
|
+
if not isinstance(model, str) or not model.strip():
|
|
248
|
+
print(f"Warning: the profile {profile_name} has no model - skipped.")
|
|
249
|
+
continue
|
|
250
|
+
thinking = profile_entry.get("thinking")
|
|
251
|
+
thinking_text = thinking if isinstance(thinking, str) else ""
|
|
252
|
+
profiles[profile_name] = ModelProfile(
|
|
253
|
+
name=profile_name,
|
|
254
|
+
model=model.strip(),
|
|
255
|
+
thinking=thinking_text.strip(),
|
|
256
|
+
)
|
|
257
|
+
return profiles
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def item_label_names(*, item: dict[str, object]) -> set[str]:
|
|
261
|
+
"""The item's label names, empty when the payload carries none."""
|
|
262
|
+
labels = item.get("labels", [])
|
|
263
|
+
if not isinstance(labels, list):
|
|
264
|
+
return set()
|
|
265
|
+
label_names: set[str] = set()
|
|
266
|
+
for entry in labels:
|
|
267
|
+
if isinstance(entry, dict):
|
|
268
|
+
name = entry.get("name", "")
|
|
269
|
+
if isinstance(name, str):
|
|
270
|
+
label_names.add(name)
|
|
271
|
+
return label_names
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def section_string_list(*, section: dict[str, object], key: str) -> tuple[str, ...]:
|
|
275
|
+
"""Read a list of strings from a routing section; empty when absent."""
|
|
276
|
+
values = section.get(key)
|
|
277
|
+
if not isinstance(values, list):
|
|
278
|
+
return ()
|
|
279
|
+
return tuple(value for value in values if isinstance(value, str))
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""ADR sequence gate: the decision series stays unique, gap-free and role-clean.
|
|
2
|
+
|
|
3
|
+
The constitution's decisions live in ``docs/decisions/`` as
|
|
4
|
+
``adr-NNNN-slug.md``. ``docs/rules.md`` fixes the numbering by
|
|
5
|
+
repository role: 0001-0099 belong to the template, and an instance
|
|
6
|
+
numbers its own decisions from 0100 upward. Nothing checked the series
|
|
7
|
+
mechanically, and the instance family paid for it twice: the original
|
|
8
|
+
repository collided two adr-0108s, and a rebuild drafted adr-0100 on
|
|
9
|
+
one branch for a merged-scope decision while another branch drafted
|
|
10
|
+
adr-0101 for a number the plan reserved for a different decision
|
|
11
|
+
(issue #21). adr-0100 stayed on the template itself - template
|
|
12
|
+
material parked in the instance series, colliding with every
|
|
13
|
+
instance's own 0100 - until the 2026-12-04 renumbering.
|
|
14
|
+
|
|
15
|
+
The role comes from the flat top-level ``role`` key of ``gates.yaml``
|
|
16
|
+
(a line scan of that one documented key: the drivers are stdlib-only,
|
|
17
|
+
so no YAML dependency rides along). Per ``docs/rules.md``:
|
|
18
|
+
|
|
19
|
+
- ``template``: every decision numbers 0001-0099, contiguous from
|
|
20
|
+
0001. An adr-0100 or higher on the template is a category error:
|
|
21
|
+
template material parked where every instance's own series starts.
|
|
22
|
+
- ``instance``: every committed decision numbers 0100 upward,
|
|
23
|
+
contiguous from the floor. A committed 0001-0099 file is template
|
|
24
|
+
material; an instance commits only its own decisions.
|
|
25
|
+
|
|
26
|
+
The gate fails (exit 1) on a duplicate number, on a gap inside the
|
|
27
|
+
repository's series, on a number outside the repository's role, or on
|
|
28
|
+
an ``adr-`` file whose name is not ``adr-NNNN-slug.md``; a missing or
|
|
29
|
+
ambiguous ``role`` key is a protocol failure (exit 2). A pull request
|
|
30
|
+
head carries only its own drafts, so two branches drafting the same
|
|
31
|
+
number collide at rebase time and this gate catches the residue -
|
|
32
|
+
named as a limitation, not hidden.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
import itertools
|
|
36
|
+
import re
|
|
37
|
+
import sys
|
|
38
|
+
from typing import TYPE_CHECKING, Literal
|
|
39
|
+
|
|
40
|
+
from gabarit._shared import (
|
|
41
|
+
EXIT_CODE_CRITERIA_UNMET,
|
|
42
|
+
EXIT_CODE_PROTOCOL_FAILURE,
|
|
43
|
+
EXIT_CODE_SUCCESS,
|
|
44
|
+
REPOSITORY_ROOT,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if TYPE_CHECKING:
|
|
48
|
+
import argparse
|
|
49
|
+
from collections.abc import Sequence
|
|
50
|
+
from pathlib import Path
|
|
51
|
+
|
|
52
|
+
# Runtime value, not an annotation-only alias: the role is a closed
|
|
53
|
+
# two-value set, and evaluating the annotations must not depend on
|
|
54
|
+
# PEP 649 being the interpreter's default.
|
|
55
|
+
RepositoryRole = Literal["template", "instance"]
|
|
56
|
+
|
|
57
|
+
DECISIONS_DIRECTORY_PATH = REPOSITORY_ROOT / "docs" / "decisions"
|
|
58
|
+
DECISION_FILE_NAME_RE = re.compile(r"^adr-(\d{4})-[a-z0-9-]+\.md$")
|
|
59
|
+
GATES_FILE_NAME = "gates.yaml"
|
|
60
|
+
ROLE_LINE_RE = re.compile(r"^role:[ \t]*(template|instance)[ \t]*$", re.MULTILINE)
|
|
61
|
+
ROLE_TEMPLATE: RepositoryRole = "template"
|
|
62
|
+
ROLE_INSTANCE: RepositoryRole = "instance"
|
|
63
|
+
TEMPLATE_SERIES_FLOOR = 1 # docs/rules.md: the template's series is 0001-0099
|
|
64
|
+
TEMPLATE_SERIES_MAXIMUM = 99
|
|
65
|
+
PROJECT_SERIES_FLOOR = 100 # docs/rules.md: an instance numbers from 0100 upward
|
|
66
|
+
DECISION_FILE_PREFIX = "adr-"
|
|
67
|
+
MARKDOWN_SUFFIX = ".md"
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def read_repository_role() -> RepositoryRole | None:
|
|
71
|
+
"""Read the repository role from the flat top-level gates.yaml key."""
|
|
72
|
+
gates_path = REPOSITORY_ROOT / GATES_FILE_NAME
|
|
73
|
+
try:
|
|
74
|
+
gates_text = gates_path.read_text(encoding="utf-8")
|
|
75
|
+
except OSError:
|
|
76
|
+
print(f"Error: cannot read {gates_path}.", file=sys.stderr)
|
|
77
|
+
return None
|
|
78
|
+
roles: list[RepositoryRole] = ROLE_LINE_RE.findall(gates_text)
|
|
79
|
+
if len(roles) != 1:
|
|
80
|
+
print(
|
|
81
|
+
"Error: gates.yaml must carry exactly one top-level"
|
|
82
|
+
" 'role: template' or 'role: instance' line (docs/rules.md).",
|
|
83
|
+
file=sys.stderr,
|
|
84
|
+
)
|
|
85
|
+
return None
|
|
86
|
+
return roles[0]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def parse_decision_file_name(*, file_name: str) -> int | None:
|
|
90
|
+
"""Return the ADR number of a conforming name; None means misnamed."""
|
|
91
|
+
match = DECISION_FILE_NAME_RE.match(file_name)
|
|
92
|
+
return int(match.group(1)) if match else None
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def find_series_problems(*, numbers: Sequence[int], floor: int) -> list[str]:
|
|
96
|
+
"""Check one series: duplicates and gaps, anchored at the series floor."""
|
|
97
|
+
counts: dict[int, int] = {}
|
|
98
|
+
for number in numbers:
|
|
99
|
+
counts[number] = counts.get(number, 0) + 1
|
|
100
|
+
problems: list[str] = []
|
|
101
|
+
for number, count in sorted(counts.items()):
|
|
102
|
+
if count > 1:
|
|
103
|
+
problems.append(f"duplicate ADR number {number:04d} ({count} files)")
|
|
104
|
+
ordered_numbers = sorted(counts)
|
|
105
|
+
if ordered_numbers and ordered_numbers[0] > floor:
|
|
106
|
+
problems.append(
|
|
107
|
+
f"gap in the ADR series between {floor - 1:04d} and {ordered_numbers[0]:04d}"
|
|
108
|
+
)
|
|
109
|
+
for previous_number, current_number in itertools.pairwise(ordered_numbers):
|
|
110
|
+
if current_number != previous_number + 1:
|
|
111
|
+
problems.append(
|
|
112
|
+
f"gap in the ADR series between {previous_number:04d} and {current_number:04d}"
|
|
113
|
+
)
|
|
114
|
+
return problems
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def audit_decisions(*, file_names: Sequence[str], role: RepositoryRole) -> list[str]:
|
|
118
|
+
"""Audit the decision series; every entry names one broken invariant."""
|
|
119
|
+
problems: list[str] = []
|
|
120
|
+
role_numbers: list[int] = []
|
|
121
|
+
floor = TEMPLATE_SERIES_FLOOR if role == ROLE_TEMPLATE else PROJECT_SERIES_FLOOR
|
|
122
|
+
for file_name in file_names:
|
|
123
|
+
number = parse_decision_file_name(file_name=file_name)
|
|
124
|
+
if number is None:
|
|
125
|
+
problems.append(f"misnamed decision file: {file_name}")
|
|
126
|
+
continue
|
|
127
|
+
if number < TEMPLATE_SERIES_FLOOR:
|
|
128
|
+
problems.append(
|
|
129
|
+
f"ADR number {number:04d} is outside the decision series"
|
|
130
|
+
f" (docs/rules.md): {file_name}"
|
|
131
|
+
)
|
|
132
|
+
elif role == ROLE_TEMPLATE and number > TEMPLATE_SERIES_MAXIMUM:
|
|
133
|
+
problems.append(
|
|
134
|
+
f"ADR number {number:04d} does not belong on the template: the"
|
|
135
|
+
" template carries only template decisions, 0001-0099"
|
|
136
|
+
f" (docs/rules.md): {file_name}"
|
|
137
|
+
)
|
|
138
|
+
elif role == ROLE_INSTANCE and number <= TEMPLATE_SERIES_MAXIMUM:
|
|
139
|
+
problems.append(
|
|
140
|
+
f"ADR number {number:04d} is a template decision: an instance"
|
|
141
|
+
" commits only its own decisions, numbered from 0100 upward"
|
|
142
|
+
f" (docs/rules.md): {file_name}"
|
|
143
|
+
)
|
|
144
|
+
else:
|
|
145
|
+
role_numbers.append(number)
|
|
146
|
+
problems.extend(find_series_problems(numbers=role_numbers, floor=floor))
|
|
147
|
+
return problems
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def list_decision_file_names(*, directory_path: Path) -> list[str]:
|
|
151
|
+
"""List the decision files of the directory, sorted by name."""
|
|
152
|
+
return sorted(
|
|
153
|
+
entry.name
|
|
154
|
+
for entry in directory_path.iterdir()
|
|
155
|
+
if entry.is_file()
|
|
156
|
+
and entry.name.startswith(DECISION_FILE_PREFIX)
|
|
157
|
+
and entry.name.endswith(MARKDOWN_SUFFIX)
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def register(subparsers: argparse._SubParsersAction[argparse.ArgumentParser]) -> None:
|
|
162
|
+
"""Register the adr-sequence subcommand."""
|
|
163
|
+
subparser = subparsers.add_parser(
|
|
164
|
+
"adr-sequence",
|
|
165
|
+
help="Fail on a broken ADR series (docs/rules.md).",
|
|
166
|
+
description="Fail on a broken ADR series (docs/rules.md).",
|
|
167
|
+
)
|
|
168
|
+
subparser.set_defaults(runner=run)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def run(*, arguments: argparse.Namespace) -> int:
|
|
172
|
+
"""Entry point: audit the decision series, report, fail closed."""
|
|
173
|
+
role = read_repository_role()
|
|
174
|
+
if role is None:
|
|
175
|
+
return EXIT_CODE_PROTOCOL_FAILURE
|
|
176
|
+
if not DECISIONS_DIRECTORY_PATH.is_dir():
|
|
177
|
+
print(
|
|
178
|
+
f"Error: the decisions directory is missing: {DECISIONS_DIRECTORY_PATH}.",
|
|
179
|
+
file=sys.stderr,
|
|
180
|
+
)
|
|
181
|
+
return EXIT_CODE_PROTOCOL_FAILURE
|
|
182
|
+
file_names = list_decision_file_names(directory_path=DECISIONS_DIRECTORY_PATH)
|
|
183
|
+
if not file_names:
|
|
184
|
+
print("Error: no decision file found under docs/decisions/.", file=sys.stderr)
|
|
185
|
+
return EXIT_CODE_PROTOCOL_FAILURE
|
|
186
|
+
problems = audit_decisions(file_names=file_names, role=role)
|
|
187
|
+
for problem in problems:
|
|
188
|
+
print(f"adr-sequence: {problem}")
|
|
189
|
+
if problems:
|
|
190
|
+
print(
|
|
191
|
+
f"adr-sequence: {len(problems)} problem(s): the decision series"
|
|
192
|
+
" is unique, gap-free and role-clean (docs/rules.md)."
|
|
193
|
+
)
|
|
194
|
+
return EXIT_CODE_CRITERIA_UNMET
|
|
195
|
+
return EXIT_CODE_SUCCESS
|