codersforge 0.0.1__py3-none-any.whl
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.
- codersforge/__init__.py +4 -0
- codersforge/__main__.py +8 -0
- codersforge/_core/CORE_VERSION +33 -0
- codersforge/_core/__init__.py +8 -0
- codersforge/_core/archive.py +319 -0
- codersforge/_core/backup.py +417 -0
- codersforge/_core/config.py +206 -0
- codersforge/_core/domain.py +382 -0
- codersforge/_core/errors.py +36 -0
- codersforge/_core/events.py +381 -0
- codersforge/_core/findings.py +265 -0
- codersforge/_core/gates.py +395 -0
- codersforge/_core/identity.py +259 -0
- codersforge/_core/index.py +262 -0
- codersforge/_core/items.py +513 -0
- codersforge/_core/ledger.py +194 -0
- codersforge/_core/lessons.py +142 -0
- codersforge/_core/messages.py +293 -0
- codersforge/_core/migrate.py +221 -0
- codersforge/_core/output.py +41 -0
- codersforge/_core/paths.py +62 -0
- codersforge/_core/py.typed +0 -0
- codersforge/_core/schema.py +113 -0
- codersforge/_core/ulid.py +36 -0
- codersforge/_core/upgrade.py +57 -0
- codersforge/_core/versions.py +20 -0
- codersforge/_core/views.py +67 -0
- codersforge/_core/walls.py +231 -0
- codersforge/_core/workflow.py +745 -0
- codersforge/_docs.py +33 -0
- codersforge/checks.py +94 -0
- codersforge/cli.py +865 -0
- codersforge/domain.py +270 -0
- codersforge/py.typed +0 -0
- codersforge/workflows.yaml +129 -0
- codersforge-0.0.1.dist-info/METADATA +81 -0
- codersforge-0.0.1.dist-info/RECORD +41 -0
- codersforge-0.0.1.dist-info/WHEEL +4 -0
- codersforge-0.0.1.dist-info/entry_points.txt +2 -0
- codersforge-0.0.1.dist-info/licenses/LICENSE +202 -0
- codersforge-0.0.1.dist-info/licenses/NOTICE +7 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""What a domain plugin tells the core about itself (CORE.md §3)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from collections.abc import Callable, Mapping
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from types import MappingProxyType
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from .schema import Field
|
|
13
|
+
from .versions import VERSIONED
|
|
14
|
+
from .workflow import BUILTIN_CHECKS, Check, Workflow, gate_kind_of
|
|
15
|
+
|
|
16
|
+
_NAME = re.compile(r"^[a-z][a-z0-9]*$")
|
|
17
|
+
_PREFIX = re.compile(r"^[A-Z][A-Z0-9]*$")
|
|
18
|
+
_EVENT_TYPE = re.compile(r"^[a-z][a-z0-9_]*$")
|
|
19
|
+
_ROLE = re.compile(r"^[a-z][a-z-]*$")
|
|
20
|
+
_ITEM_PREFIX = re.compile(r"^[A-Z]+$")
|
|
21
|
+
_GATE_KIND = re.compile(r"^[A-Z][A-Z0-9_]*$")
|
|
22
|
+
|
|
23
|
+
_RULING = re.compile(r"^[a-z][a-z_]*$")
|
|
24
|
+
|
|
25
|
+
#: Front-matter keys the core owns (CONTRACT §4); a domain's item schema adds others.
|
|
26
|
+
CORE_FRONT_MATTER = ("schema", "id", "title", "type", "state", "created")
|
|
27
|
+
|
|
28
|
+
#: Roles every domain has. The orchestrator is the main Claude Code session; the
|
|
29
|
+
#: operator is the human, and is never taken from an agent definition.
|
|
30
|
+
ORCHESTRATOR = "orchestrator"
|
|
31
|
+
OPERATOR = "operator"
|
|
32
|
+
CORE_ROLES: tuple[str, ...] = (ORCHESTRATOR, OPERATOR)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class ItemType:
|
|
37
|
+
"""A kind of work item: its name, ID prefix and where its primary file lives.
|
|
38
|
+
|
|
39
|
+
``path`` is relative to the state folder and contains ``{id}``. ``body`` is
|
|
40
|
+
the new file's text after the front-matter; ``{id}`` and ``{title}`` are
|
|
41
|
+
filled in.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
name: str
|
|
45
|
+
prefix: str
|
|
46
|
+
path: str = "active/{id}/spec.md"
|
|
47
|
+
initial_state: str = "INTAKE"
|
|
48
|
+
body: str = "# {id} \u2014 {title}\n"
|
|
49
|
+
|
|
50
|
+
def __post_init__(self) -> None:
|
|
51
|
+
if not _NAME.match(self.name):
|
|
52
|
+
raise ValueError(f"item type names are lowercase letters and digits: {self.name!r}")
|
|
53
|
+
if not _ITEM_PREFIX.match(self.prefix):
|
|
54
|
+
raise ValueError(f"item prefixes are uppercase letters: {self.prefix!r}")
|
|
55
|
+
parts = self.path.split("/")
|
|
56
|
+
if "{id}" not in self.path or self.path.startswith("/") or ".." in parts:
|
|
57
|
+
raise ValueError(f"item path must be relative and contain {{id}}: {self.path!r}")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass(frozen=True)
|
|
61
|
+
class GateKind:
|
|
62
|
+
"""A kind of operator decision, with the rulings it can take.
|
|
63
|
+
|
|
64
|
+
``project`` gates are not on an item. ``item_types`` limits which items the
|
|
65
|
+
gate can be opened on (empty: any). ``passing`` lists the rulings that
|
|
66
|
+
satisfy a plain ``gate:KIND`` condition. The other tuples name rulings that
|
|
67
|
+
need a ``note``, a ``review_by`` date (after today), or a ``party`` (who the
|
|
68
|
+
risk is shared with or transferred to). ``revise_targets``, when set, must
|
|
69
|
+
be given with ``revise``.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
name: str
|
|
73
|
+
rulings: tuple[str, ...]
|
|
74
|
+
project: bool = False
|
|
75
|
+
item_types: tuple[str, ...] = ()
|
|
76
|
+
passing: tuple[str, ...] = ("accept",)
|
|
77
|
+
revise_targets: tuple[str, ...] = ()
|
|
78
|
+
note_required: tuple[str, ...] = ()
|
|
79
|
+
review_required: tuple[str, ...] = ()
|
|
80
|
+
party_required: tuple[str, ...] = ()
|
|
81
|
+
|
|
82
|
+
def __post_init__(self) -> None:
|
|
83
|
+
if not _GATE_KIND.match(self.name):
|
|
84
|
+
raise ValueError(f"gate kinds are upper-case: {self.name!r}")
|
|
85
|
+
if not self.rulings or not all(_RULING.match(r) for r in self.rulings):
|
|
86
|
+
raise ValueError(f"{self.name}: rulings are lower-case words joined by _")
|
|
87
|
+
for label, names in (
|
|
88
|
+
("passing", self.passing),
|
|
89
|
+
("note_required", self.note_required),
|
|
90
|
+
("review_required", self.review_required),
|
|
91
|
+
("party_required", self.party_required),
|
|
92
|
+
):
|
|
93
|
+
unknown = sorted(set(names) - set(self.rulings))
|
|
94
|
+
if unknown:
|
|
95
|
+
raise ValueError(f"{self.name}: {label} names unknown rulings {unknown}")
|
|
96
|
+
if self.revise_targets and "revise" not in self.rulings:
|
|
97
|
+
raise ValueError(f"{self.name}: revise targets without a revise ruling")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
ANY_ROLE = "*"
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@dataclass(frozen=True)
|
|
104
|
+
class Migration:
|
|
105
|
+
"""Upgrade one record of ``kind`` from ``from_version`` to ``from_version + 1``.
|
|
106
|
+
|
|
107
|
+
``kind`` is ``events``, ``front_matter`` or ``config``. ``apply`` gets a copy
|
|
108
|
+
of the record and returns the upgraded record; the version key is set for
|
|
109
|
+
it. An event's ``id``, ``ts`` and ``prev`` are kept whatever it returns.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
kind: str
|
|
113
|
+
from_version: int
|
|
114
|
+
apply: Callable[[dict[str, Any]], dict[str, Any]]
|
|
115
|
+
summary: str
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True)
|
|
119
|
+
class Wall:
|
|
120
|
+
"""Something a role may not read or write (CONTRACT §9.1).
|
|
121
|
+
|
|
122
|
+
``applies_to`` lists restricted roles; ``*`` means every agent role (team
|
|
123
|
+
roles, the orchestrator, and agents with no role), less ``exempt``. The
|
|
124
|
+
operator is never restricted. Path patterns are relative to the repository
|
|
125
|
+
root, or start with ``~/`` for the user's home; ``{state}`` is the state
|
|
126
|
+
folder, ``{item}`` an item ID, ``*`` one path segment and ``**`` any depth.
|
|
127
|
+
A pattern with ``{item}`` applies only while that item is in one of
|
|
128
|
+
``while_states`` (if given). ``item_types`` are item types the restricted
|
|
129
|
+
roles may not write through the CLI.
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
name: str
|
|
133
|
+
applies_to: tuple[str, ...]
|
|
134
|
+
exempt: tuple[str, ...] = ()
|
|
135
|
+
read: tuple[str, ...] = ()
|
|
136
|
+
write: tuple[str, ...] = ()
|
|
137
|
+
item_types: tuple[str, ...] = ()
|
|
138
|
+
while_states: tuple[str, ...] = ()
|
|
139
|
+
reason: str = ""
|
|
140
|
+
|
|
141
|
+
def restricts(self, role: str | None) -> bool:
|
|
142
|
+
"""Whether ``role`` (``None``: an agent with no role) is behind this wall."""
|
|
143
|
+
if role == OPERATOR or (role is not None and role in self.exempt):
|
|
144
|
+
return False
|
|
145
|
+
return ANY_ROLE in self.applies_to or (role is not None and role in self.applies_to)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
@dataclass(frozen=True)
|
|
149
|
+
class MessageRule:
|
|
150
|
+
"""Who may message whom, and in which kinds (empty: every kind).
|
|
151
|
+
|
|
152
|
+
``*`` in ``sources`` or ``targets`` means any team role.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
sources: tuple[str, ...]
|
|
156
|
+
targets: tuple[str, ...]
|
|
157
|
+
kinds: tuple[str, ...] = ()
|
|
158
|
+
|
|
159
|
+
def allows(self, sender: str, to: str, kind: str) -> bool:
|
|
160
|
+
return (
|
|
161
|
+
(sender in self.sources or ANY_ROLE in self.sources)
|
|
162
|
+
and (to in self.targets or ANY_ROLE in self.targets)
|
|
163
|
+
and (not self.kinds or kind in self.kinds)
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass(frozen=True)
|
|
168
|
+
class Domain:
|
|
169
|
+
"""A domain plugin's identity and its additions to the core.
|
|
170
|
+
|
|
171
|
+
Everything a domain adds is additive: it cannot remove or loosen core rules.
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
name: str
|
|
175
|
+
title: str
|
|
176
|
+
state_dir: str
|
|
177
|
+
env_prefix: str
|
|
178
|
+
event_types: tuple[str, ...] = ()
|
|
179
|
+
roles: tuple[str, ...] = ()
|
|
180
|
+
item_types: tuple[ItemType, ...] = ()
|
|
181
|
+
item_schema: Mapping[str, Field] = field(default_factory=lambda: MappingProxyType({}))
|
|
182
|
+
workflows: Mapping[str, Workflow] = field(default_factory=lambda: MappingProxyType({}))
|
|
183
|
+
checks: Mapping[str, Check] = field(default_factory=lambda: MappingProxyType({}))
|
|
184
|
+
gate_kinds: tuple[GateKind, ...] = ()
|
|
185
|
+
desk_only: tuple[str, ...] = ()
|
|
186
|
+
desk_only_item_types: tuple[str, ...] = ()
|
|
187
|
+
severities: tuple[str, ...] = ("critical", "high", "medium", "low")
|
|
188
|
+
finding_sources: tuple[str, ...] = ()
|
|
189
|
+
no_accept_sources: tuple[str, ...] = ()
|
|
190
|
+
message_rules: tuple[MessageRule, ...] = ()
|
|
191
|
+
walls: tuple[Wall, ...] = ()
|
|
192
|
+
archivers: tuple[str, ...] = () # team roles that may archive, beside the core roles
|
|
193
|
+
migrations: tuple[Migration, ...] = ()
|
|
194
|
+
config_schema: Mapping[str, Field] = field(default_factory=lambda: MappingProxyType({}))
|
|
195
|
+
default_config: str = ""
|
|
196
|
+
|
|
197
|
+
def __post_init__(self) -> None:
|
|
198
|
+
if not _NAME.match(self.name):
|
|
199
|
+
raise ValueError(f"domain name must be lowercase letters and digits: {self.name!r}")
|
|
200
|
+
if not _PREFIX.match(self.env_prefix):
|
|
201
|
+
raise ValueError(
|
|
202
|
+
f"env_prefix must be uppercase letters and digits: {self.env_prefix!r}"
|
|
203
|
+
)
|
|
204
|
+
if not self.state_dir.startswith(".") or "/" in self.state_dir:
|
|
205
|
+
raise ValueError(f"state_dir must be a hidden directory name: {self.state_dir!r}")
|
|
206
|
+
for event_type in self.event_types:
|
|
207
|
+
if not _EVENT_TYPE.match(event_type):
|
|
208
|
+
raise ValueError(f"invalid event type name: {event_type!r}")
|
|
209
|
+
for role in self.roles:
|
|
210
|
+
if not _ROLE.match(role):
|
|
211
|
+
raise ValueError(f"role names are lowercase words joined by hyphens: {role!r}")
|
|
212
|
+
if role in CORE_ROLES:
|
|
213
|
+
raise ValueError(f"{role!r} is a core role; domains do not redeclare it")
|
|
214
|
+
if len(set(self.roles)) != len(self.roles):
|
|
215
|
+
raise ValueError("duplicate role name")
|
|
216
|
+
names = [t.name for t in self.item_types]
|
|
217
|
+
prefixes = [t.prefix for t in self.item_types]
|
|
218
|
+
if len(set(names)) != len(names) or len(set(prefixes)) != len(prefixes):
|
|
219
|
+
raise ValueError("item type names and prefixes must be unique")
|
|
220
|
+
clash = sorted(set(self.item_schema) & set(CORE_FRONT_MATTER))
|
|
221
|
+
if clash:
|
|
222
|
+
raise ValueError(f"item schema redefines core front-matter keys: {', '.join(clash)}")
|
|
223
|
+
self._check_workflows()
|
|
224
|
+
self._check_gates()
|
|
225
|
+
self._check_findings()
|
|
226
|
+
self._check_messages()
|
|
227
|
+
self._check_walls()
|
|
228
|
+
unknown = sorted(set(self.archivers) - set(self.roles))
|
|
229
|
+
if unknown:
|
|
230
|
+
raise ValueError(f"archivers: not domain roles: {', '.join(unknown)}")
|
|
231
|
+
self._check_migrations()
|
|
232
|
+
|
|
233
|
+
def _check_migrations(self) -> None:
|
|
234
|
+
"""Each kind's migrations form one unbroken chain ending at the current version."""
|
|
235
|
+
for kind in sorted({m.kind for m in self.migrations}):
|
|
236
|
+
if kind not in VERSIONED:
|
|
237
|
+
raise ValueError(f"migrations: unknown kind {kind!r}")
|
|
238
|
+
current = VERSIONED[kind][1]
|
|
239
|
+
steps = sorted(m.from_version for m in self.migrations if m.kind == kind)
|
|
240
|
+
if len(set(steps)) != len(steps):
|
|
241
|
+
raise ValueError(f"migrations: two {kind} migrations from the same version")
|
|
242
|
+
if steps != list(range(steps[0], current)) or steps[0] < 0:
|
|
243
|
+
raise ValueError(
|
|
244
|
+
f"migrations: {kind} steps {steps} must run unbroken up to {current}"
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
def migration(self, kind: str, from_version: int) -> Migration | None:
|
|
248
|
+
return next(
|
|
249
|
+
(m for m in self.migrations if m.kind == kind and m.from_version == from_version),
|
|
250
|
+
None,
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
def _check_walls(self) -> None:
|
|
254
|
+
names = [w.name for w in self.walls]
|
|
255
|
+
if len(set(names)) != len(names):
|
|
256
|
+
raise ValueError("duplicate wall name")
|
|
257
|
+
known = {*self.all_roles, ANY_ROLE}
|
|
258
|
+
types = {t.name for t in self.item_types}
|
|
259
|
+
for wall in self.walls:
|
|
260
|
+
unknown = sorted(set((*wall.applies_to, *wall.exempt)) - known)
|
|
261
|
+
if unknown or not wall.applies_to:
|
|
262
|
+
raise ValueError(f"wall {wall.name}: unknown roles {', '.join(unknown)}")
|
|
263
|
+
if not (wall.read or wall.write or wall.item_types):
|
|
264
|
+
raise ValueError(f"wall {wall.name} denies nothing")
|
|
265
|
+
bad_types = sorted(set(wall.item_types) - types)
|
|
266
|
+
if bad_types:
|
|
267
|
+
raise ValueError(f"wall {wall.name}: unknown item types {', '.join(bad_types)}")
|
|
268
|
+
for pattern in (*wall.read, *wall.write):
|
|
269
|
+
if pattern.startswith("/") or ".." in pattern.split("/"):
|
|
270
|
+
raise ValueError(f"wall {wall.name}: {pattern!r} must be relative or ~/")
|
|
271
|
+
|
|
272
|
+
def _check_messages(self) -> None:
|
|
273
|
+
known = {*self.roles, ANY_ROLE}
|
|
274
|
+
for rule in self.message_rules:
|
|
275
|
+
unknown = sorted(set((*rule.sources, *rule.targets)) - known)
|
|
276
|
+
if unknown or not rule.sources or not rule.targets:
|
|
277
|
+
raise ValueError(f"message rule names unknown roles: {', '.join(unknown)}")
|
|
278
|
+
|
|
279
|
+
def check_pairs_remove(self, entries: tuple[str, ...] | list[str]) -> None:
|
|
280
|
+
"""Every entry is ``from>to`` for a pair some rule allows."""
|
|
281
|
+
for entry in entries:
|
|
282
|
+
sender, sep, to = entry.partition(">")
|
|
283
|
+
if (
|
|
284
|
+
not sep
|
|
285
|
+
or sender == to
|
|
286
|
+
or not any(
|
|
287
|
+
(sender in r.sources or ANY_ROLE in r.sources)
|
|
288
|
+
and (to in r.targets or ANY_ROLE in r.targets)
|
|
289
|
+
for r in self.message_rules
|
|
290
|
+
)
|
|
291
|
+
or sender not in self.roles
|
|
292
|
+
or to not in self.roles
|
|
293
|
+
):
|
|
294
|
+
raise ValueError(f"pairs_remove entry {entry!r} is not an allowed from>to pair")
|
|
295
|
+
|
|
296
|
+
def _check_findings(self) -> None:
|
|
297
|
+
if not self.severities or len(set(self.severities)) != len(self.severities):
|
|
298
|
+
raise ValueError("severities must be a non-empty list of distinct names")
|
|
299
|
+
unknown = sorted(set(self.finding_sources) - set(self.roles))
|
|
300
|
+
if unknown:
|
|
301
|
+
raise ValueError(f"finding_sources: not domain roles: {', '.join(unknown)}")
|
|
302
|
+
extra = sorted(set(self.no_accept_sources) - set(self.finding_sources))
|
|
303
|
+
if extra:
|
|
304
|
+
raise ValueError(f"no_accept_sources: not finding sources: {', '.join(extra)}")
|
|
305
|
+
|
|
306
|
+
def _check_gates(self) -> None:
|
|
307
|
+
names = [k.name for k in self.gate_kinds]
|
|
308
|
+
if len(set(names)) != len(names):
|
|
309
|
+
raise ValueError("duplicate gate kind")
|
|
310
|
+
types = {t.name for t in self.item_types}
|
|
311
|
+
unknown_desk = sorted(set(self.desk_only_item_types) - types)
|
|
312
|
+
if unknown_desk:
|
|
313
|
+
raise ValueError(f"desk_only_item_types: unknown item types {', '.join(unknown_desk)}")
|
|
314
|
+
for kind in self.gate_kinds:
|
|
315
|
+
unknown = sorted(set(kind.item_types) - types)
|
|
316
|
+
if unknown:
|
|
317
|
+
raise ValueError(f"{kind.name}: unknown item types {', '.join(unknown)}")
|
|
318
|
+
self.check_desk_only(self.desk_only)
|
|
319
|
+
if self.gate_kinds:
|
|
320
|
+
checks = self.all_checks
|
|
321
|
+
for wf in self.workflows.values():
|
|
322
|
+
for cond in wf.all_conditions():
|
|
323
|
+
if checks[cond.name].is_gate and gate_kind_of(cond) not in names:
|
|
324
|
+
raise ValueError(f"{wf.type}: {cond} names no gate kind")
|
|
325
|
+
|
|
326
|
+
def gate_kind(self, name: str) -> GateKind | None:
|
|
327
|
+
"""The gate kind called ``name``, if the domain declares it."""
|
|
328
|
+
return next((k for k in self.gate_kinds if k.name == name), None)
|
|
329
|
+
|
|
330
|
+
def check_desk_only(self, entries: tuple[str, ...] | list[str]) -> None:
|
|
331
|
+
"""Every entry is ``KIND`` or ``KIND-<severity>`` for a declared kind."""
|
|
332
|
+
names = {k.name for k in self.gate_kinds}
|
|
333
|
+
for entry in entries:
|
|
334
|
+
kind = entry if entry in names else entry.rsplit("-", 1)[0]
|
|
335
|
+
if kind not in names or (kind != entry and not entry[len(kind) + 1 :]):
|
|
336
|
+
raise ValueError(f"desk_only entry {entry!r} is not a gate kind or KIND-severity")
|
|
337
|
+
|
|
338
|
+
def _check_workflows(self) -> None:
|
|
339
|
+
reused = sorted(set(self.checks) & set(BUILTIN_CHECKS))
|
|
340
|
+
if reused:
|
|
341
|
+
raise ValueError(f"checks reuse built-in names: {', '.join(reused)}")
|
|
342
|
+
if not self.workflows:
|
|
343
|
+
return
|
|
344
|
+
types = {t.name: t for t in self.item_types}
|
|
345
|
+
if set(self.workflows) != set(types):
|
|
346
|
+
raise ValueError("every item type needs exactly one workflow")
|
|
347
|
+
checks = self.all_checks
|
|
348
|
+
for name, wf in self.workflows.items():
|
|
349
|
+
if wf.initial != types[name].initial_state:
|
|
350
|
+
raise ValueError(
|
|
351
|
+
f"{name}: workflow starts at {wf.initial} but the item type at "
|
|
352
|
+
f"{types[name].initial_state}"
|
|
353
|
+
)
|
|
354
|
+
for t in wf.transitions:
|
|
355
|
+
for cond in t.requires:
|
|
356
|
+
if cond.name not in checks:
|
|
357
|
+
raise ValueError(f"{name}: unknown check {cond.name!r}")
|
|
358
|
+
try:
|
|
359
|
+
checks[cond.name].validate(cond.arg, self)
|
|
360
|
+
except ValueError as exc:
|
|
361
|
+
raise ValueError(f"{name}: {cond}: {exc}") from exc
|
|
362
|
+
|
|
363
|
+
@property
|
|
364
|
+
def all_checks(self) -> Mapping[str, Check]:
|
|
365
|
+
"""Built-in checks plus the domain's."""
|
|
366
|
+
return {**BUILTIN_CHECKS, **self.checks}
|
|
367
|
+
|
|
368
|
+
def item_type(self, name: str) -> ItemType:
|
|
369
|
+
"""The item type called ``name``; ``KeyError`` if there is none."""
|
|
370
|
+
for item_type in self.item_types:
|
|
371
|
+
if item_type.name == name:
|
|
372
|
+
return item_type
|
|
373
|
+
raise KeyError(name)
|
|
374
|
+
|
|
375
|
+
@property
|
|
376
|
+
def all_roles(self) -> tuple[str, ...]:
|
|
377
|
+
"""Core roles followed by the domain's own."""
|
|
378
|
+
return CORE_ROLES + self.roles
|
|
379
|
+
|
|
380
|
+
def env(self, suffix: str) -> str:
|
|
381
|
+
"""The name of a domain environment variable, e.g. ``CODERSFORGE_ROLE``."""
|
|
382
|
+
return f"{self.env_prefix}_{suffix}"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Exit codes and the error type every command raises (core contract)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from enum import IntEnum
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ExitCode(IntEnum):
|
|
10
|
+
"""Process exit codes. Stable across every domain."""
|
|
11
|
+
|
|
12
|
+
OK = 0
|
|
13
|
+
USAGE = 1
|
|
14
|
+
REFUSED = 2
|
|
15
|
+
NOT_FOUND = 3
|
|
16
|
+
CONFLICT = 4
|
|
17
|
+
DATA = 5
|
|
18
|
+
INTERNAL = 10
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ForgeError(Exception):
|
|
22
|
+
"""An error that ends a command with a specific exit code.
|
|
23
|
+
|
|
24
|
+
``reason`` is a short, stable, machine-readable word (for example
|
|
25
|
+
``missing_identity``); ``detail`` is a human-readable sentence.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, code: ExitCode, reason: str, detail: str) -> None:
|
|
29
|
+
super().__init__(detail)
|
|
30
|
+
self.code = code
|
|
31
|
+
self.reason = reason
|
|
32
|
+
self.detail = detail
|
|
33
|
+
|
|
34
|
+
def envelope(self) -> dict[str, dict[str, int | str]]:
|
|
35
|
+
"""The ``--json`` error document (core contract)."""
|
|
36
|
+
return {"error": {"code": int(self.code), "reason": self.reason, "detail": self.detail}}
|