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
codersforge/__init__.py
ADDED
codersforge/__main__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"core": "pluginforge",
|
|
3
|
+
"version": "0.14.0",
|
|
4
|
+
"source_commit": "bce8b649c8b0b3edbbb461d31710ccc993ed419e",
|
|
5
|
+
"files": {
|
|
6
|
+
"__init__.py": "42f3b1db4261e0f86c10e3732f03c3e517ebf1a939c17b8af047a424a890e3d6",
|
|
7
|
+
"archive.py": "3855889a0a362694f5a7b311e49b6378eee244d37dbf485523dfb9ef817c1b09",
|
|
8
|
+
"backup.py": "75b1a5e9c13516fe7f2c244271785eb4d27582d5f317439ef4294ade1fcbe4f0",
|
|
9
|
+
"config.py": "e433d11c3fc42e5751030ef87cbd729b5369b547b02d5dba7e1c8fbccec38d6b",
|
|
10
|
+
"domain.py": "0f2b8d115533f66c62537152a6b4a52498dd5b19180a1e16c1d968a6d3263a7d",
|
|
11
|
+
"errors.py": "756ef768fbbbc160915e7cb56989d78016c240d0d495e6c29ec62126a85b1b6e",
|
|
12
|
+
"events.py": "689b1956c49282a8ad5e24e57eb39e8f335a5c8da211a24b6f769ce036a9b51d",
|
|
13
|
+
"findings.py": "2abf2030ae34c0e0b7813a693f88ab75002623d3586b2642692edbd15e91dccc",
|
|
14
|
+
"gates.py": "dd26b2f2a453d0f3809407012fcfa435c9cace289b23fd2be545408f8fce17a1",
|
|
15
|
+
"identity.py": "cf638dfd5414323fd91fd6257378f0ddc4616a01e1bb5e9a464b9106a00b7085",
|
|
16
|
+
"index.py": "3addedcd376e25af5f2baad916535df17318dfd9e2f9d6cd33411680b96005f2",
|
|
17
|
+
"items.py": "5007134ca8c54c24733037c19b5c476537496aaeaff3085bb14b0644f44707b7",
|
|
18
|
+
"ledger.py": "d14e8e9ff38bcd00ce2a352dca492a9911e03d2ed30f634e44e4a91e8b1a6bca",
|
|
19
|
+
"lessons.py": "40507b6800465df1e305ce24c334fa4669e190e85369b2d7e954a2993f5f80d2",
|
|
20
|
+
"messages.py": "c79fbcfa7d0233d26cf7a49a12a3ec4e39cc4cbb47bab13371c080fe71fac511",
|
|
21
|
+
"migrate.py": "38412c5ff67e5cb083afae110d9b1ee079308762f6d4d422f34eff546e492fe2",
|
|
22
|
+
"output.py": "59322b0ba6ac6a0033b3b3b4bce76b070e4cdbe2a72e38408e3bf0be864482d9",
|
|
23
|
+
"paths.py": "6be3d38850ca159483ad46fc6859a64070a8e2d0bfadac11c34a86193847d6dc",
|
|
24
|
+
"py.typed": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
25
|
+
"schema.py": "39edbd153f2657c5a19f707e8dc99c8e13e9cb1acfe154d4cc0b99eac747393e",
|
|
26
|
+
"ulid.py": "451f84887657e805697ff3c8c8046fdfa7dcf5367f600468f7109c1ce1dfa8f6",
|
|
27
|
+
"upgrade.py": "fdfb4a111872b755ef9d4f3eae68c6a164296e806408f1d2183c3b3d1262360c",
|
|
28
|
+
"versions.py": "9e81b83fb0b9e54fceba461769f396f626b6bfb869ba3b18207a0c29f16ffafc",
|
|
29
|
+
"views.py": "76c6c564b2db66efc760239bf4b8180619d203b94ee9d306e4a5b3f47ec41928",
|
|
30
|
+
"walls.py": "efd23dece5d7faf7a10dbb1ec0fb15ab14e47202de3a7a1175fc0960b7879cfa",
|
|
31
|
+
"workflow.py": "c4747bfdf2c36bc1796acbc0678f278ee1db6c6db3132131e00448febf3a76a8"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Pluginforge: the shared core of the -forge Claude Code plugins.
|
|
3
|
+
|
|
4
|
+
This package is vendored into each domain plugin as ``<package>._core``.
|
|
5
|
+
It uses relative imports only, so the copy works unchanged.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.14.0"
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Archive: roll a closed item's folder up into one file and remove the folder.
|
|
3
|
+
|
|
4
|
+
An item is archivable when its workflow has a transition into the archived
|
|
5
|
+
state made ``via: archive`` from the state it is in (usually ``DONE`` and
|
|
6
|
+
``ABANDONED``). The rollup, ``archive/<ID>.md``, holds the item's front-matter,
|
|
7
|
+
its record from the log (history, gates, findings, lessons) and every file from
|
|
8
|
+
its folder, each in its own fenced block.
|
|
9
|
+
|
|
10
|
+
The rollup cannot be rebuilt from the log, so the order differs from other
|
|
11
|
+
writes. Inside one store transaction:
|
|
12
|
+
|
|
13
|
+
1. write ``archive/<ID>.md`` (atomically);
|
|
14
|
+
2. append the ``state`` event, carrying the new path and the file's SHA-256;
|
|
15
|
+
3. remove the item's folder.
|
|
16
|
+
|
|
17
|
+
A crash after 1 leaves the item unarchived and the next run overwrites the
|
|
18
|
+
rollup. A crash after 2 leaves the folder behind; the next run checks the
|
|
19
|
+
rollup against its recorded hash and finishes the removal.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import hashlib
|
|
25
|
+
import os
|
|
26
|
+
import re
|
|
27
|
+
import shutil
|
|
28
|
+
from collections.abc import Mapping, Sequence
|
|
29
|
+
from dataclasses import dataclass
|
|
30
|
+
from datetime import UTC, date, datetime
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
from typing import Any
|
|
33
|
+
|
|
34
|
+
from .domain import OPERATOR, ORCHESTRATOR, Domain
|
|
35
|
+
from .errors import ExitCode, ForgeError
|
|
36
|
+
from .events import EventStore
|
|
37
|
+
from .identity import Identity
|
|
38
|
+
from .items import (
|
|
39
|
+
Item,
|
|
40
|
+
Items,
|
|
41
|
+
_write_atomic,
|
|
42
|
+
fold,
|
|
43
|
+
render_front_matter,
|
|
44
|
+
)
|
|
45
|
+
from .paths import Layout
|
|
46
|
+
from .versions import FRONT_MATTER_SCHEMA_VERSION
|
|
47
|
+
from .views import ItemView, describe_item
|
|
48
|
+
from .walls import enforce_item_write
|
|
49
|
+
from .workflow import CheckContext, decide
|
|
50
|
+
|
|
51
|
+
VIA = "archive"
|
|
52
|
+
ARCHIVE_DIR = "archive"
|
|
53
|
+
_DASH = " \u2014 "
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class Archived:
|
|
58
|
+
"""What ``archive`` did."""
|
|
59
|
+
|
|
60
|
+
item: Item
|
|
61
|
+
previous_state: str
|
|
62
|
+
removed: str # the folder or file taken away, relative to the state folder
|
|
63
|
+
files: tuple[str, ...] # files rolled up, relative to the removed folder
|
|
64
|
+
finished: bool = False # True: an earlier interrupted run was completed
|
|
65
|
+
|
|
66
|
+
def as_dict(self) -> dict[str, Any]:
|
|
67
|
+
return {
|
|
68
|
+
**self.item.as_dict(),
|
|
69
|
+
"previous_state": self.previous_state,
|
|
70
|
+
"removed": self.removed,
|
|
71
|
+
"files": list(self.files),
|
|
72
|
+
"finished": self.finished,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def archive_path(item_id: str) -> str:
|
|
77
|
+
"""``archive/FEAT-0001.md``, relative to the state folder."""
|
|
78
|
+
return f"{ARCHIVE_DIR}/{item_id}.md"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def sha256_file(path: Path) -> str:
|
|
82
|
+
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _cell(text: object) -> str:
|
|
86
|
+
return str(text).replace("|", "\\|").replace("\n", " ")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _fence(text: str) -> str:
|
|
90
|
+
"""A backtick fence longer than any run of backticks in ``text``."""
|
|
91
|
+
longest = max((len(m) for m in re.findall(r"`+", text)), default=0)
|
|
92
|
+
return "`" * max(3, longest + 1)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Archiver:
|
|
96
|
+
"""Archive items as one pinned identity."""
|
|
97
|
+
|
|
98
|
+
def __init__(
|
|
99
|
+
self,
|
|
100
|
+
layout: Layout,
|
|
101
|
+
domain: Domain,
|
|
102
|
+
identity: Identity,
|
|
103
|
+
config: Mapping[str, Any] | None = None,
|
|
104
|
+
) -> None:
|
|
105
|
+
self.layout = layout
|
|
106
|
+
self.domain = domain
|
|
107
|
+
self.identity = identity
|
|
108
|
+
self.items = Items(layout, domain, identity, config)
|
|
109
|
+
self.store = EventStore(layout, domain)
|
|
110
|
+
|
|
111
|
+
def may_archive(self, role: str) -> bool:
|
|
112
|
+
return role in (ORCHESTRATOR, OPERATOR, *self.domain.archivers)
|
|
113
|
+
|
|
114
|
+
def archive(self, item_id: str, today: date | None = None) -> Archived:
|
|
115
|
+
"""Roll up ``item_id`` and remove its folder; REFUSED unless it is closed."""
|
|
116
|
+
who = self.identity
|
|
117
|
+
if not self.may_archive(who.role):
|
|
118
|
+
allowed = ", ".join((ORCHESTRATOR, OPERATOR, *self.domain.archivers))
|
|
119
|
+
raise ForgeError(
|
|
120
|
+
ExitCode.REFUSED, "role_not_allowed", f"only {allowed} may archive items"
|
|
121
|
+
)
|
|
122
|
+
with self.store.transaction() as tx:
|
|
123
|
+
events = list(tx.events())
|
|
124
|
+
item = self.items.get_from(events, item_id)
|
|
125
|
+
enforce_item_write(tx, self.domain, who, item.type, item_id)
|
|
126
|
+
if item.archived:
|
|
127
|
+
return self._finish(events, item)
|
|
128
|
+
workflow = self.items.workflow(item.type)
|
|
129
|
+
edges = [e for e in workflow.via_edges(VIA) if e.source == item.state]
|
|
130
|
+
if not edges:
|
|
131
|
+
sources = sorted({e.source for e in workflow.via_edges(VIA)})
|
|
132
|
+
raise ForgeError(
|
|
133
|
+
ExitCode.REFUSED,
|
|
134
|
+
"not_archivable",
|
|
135
|
+
f"{item.label()} is {item.state}; a {item.type} is archived from "
|
|
136
|
+
f"{' or '.join(sources) or 'no state'}",
|
|
137
|
+
)
|
|
138
|
+
target = edges[0].target
|
|
139
|
+
ctx = CheckContext(
|
|
140
|
+
item=item,
|
|
141
|
+
events=events,
|
|
142
|
+
config=self.items.config,
|
|
143
|
+
today=today or datetime.now(UTC).date(),
|
|
144
|
+
domain=self.domain,
|
|
145
|
+
)
|
|
146
|
+
decision = decide(
|
|
147
|
+
workflow, ctx, item.state, target, checks=self.domain.all_checks, via=VIA
|
|
148
|
+
)
|
|
149
|
+
if not decision.allowed:
|
|
150
|
+
raise ForgeError(
|
|
151
|
+
ExitCode.REFUSED, decision.reason, f"{item.label()}: {decision.detail}"
|
|
152
|
+
)
|
|
153
|
+
removed, files = self._contents(item)
|
|
154
|
+
view = describe_item(events, self.domain, item_id)
|
|
155
|
+
rollup = self.layout.state / archive_path(item_id)
|
|
156
|
+
_write_atomic(rollup, self._render(view, target, removed, files, ctx.today))
|
|
157
|
+
data: dict[str, Any] = {
|
|
158
|
+
"from": item.state,
|
|
159
|
+
"to": target,
|
|
160
|
+
"path": archive_path(item_id),
|
|
161
|
+
"was": item.path,
|
|
162
|
+
"sha256": sha256_file(rollup),
|
|
163
|
+
}
|
|
164
|
+
if decision.gate:
|
|
165
|
+
data["gate"] = decision.gate
|
|
166
|
+
tx.append(
|
|
167
|
+
"state",
|
|
168
|
+
actor=who.role,
|
|
169
|
+
session=who.session,
|
|
170
|
+
data=data,
|
|
171
|
+
item=item_id,
|
|
172
|
+
title=item.title,
|
|
173
|
+
)
|
|
174
|
+
self._remove(removed)
|
|
175
|
+
archived = fold(self.store.events(), self.domain)[item_id]
|
|
176
|
+
return Archived(archived, item.state, removed, tuple(files))
|
|
177
|
+
|
|
178
|
+
# ------------------------------------------------------------ internals
|
|
179
|
+
|
|
180
|
+
def _finish(self, events: Sequence[Mapping[str, Any]], item: Item) -> Archived:
|
|
181
|
+
"""An archived item whose folder survived an interrupted run."""
|
|
182
|
+
record = next(
|
|
183
|
+
e
|
|
184
|
+
for e in reversed(events)
|
|
185
|
+
if e["type"] == "state" and e.get("item") == item.id and "sha256" in e["data"]
|
|
186
|
+
)
|
|
187
|
+
was = str(record["data"]["was"])
|
|
188
|
+
removed = self._folder_of(item.id, was)
|
|
189
|
+
if not (self.layout.state / removed).exists():
|
|
190
|
+
raise ForgeError(
|
|
191
|
+
ExitCode.USAGE, "already_archived", f"{item.label()} is archived at {item.path}"
|
|
192
|
+
)
|
|
193
|
+
rollup = self.layout.state / item.path
|
|
194
|
+
if not rollup.is_file() or sha256_file(rollup) != item.archive_sha256:
|
|
195
|
+
raise ForgeError(
|
|
196
|
+
ExitCode.DATA,
|
|
197
|
+
"archive_altered",
|
|
198
|
+
f"{rollup} is missing or does not match the log; "
|
|
199
|
+
f"{removed} was kept so nothing is lost",
|
|
200
|
+
)
|
|
201
|
+
self._remove(removed)
|
|
202
|
+
return Archived(item, str(record["data"]["from"]), removed, (), finished=True)
|
|
203
|
+
|
|
204
|
+
def _folder_of(self, item_id: str, path: str) -> str:
|
|
205
|
+
"""The item's own folder when its file sits in one named for it, else the file."""
|
|
206
|
+
parent = Path(path).parent
|
|
207
|
+
return parent.as_posix() if parent.name == item_id else path
|
|
208
|
+
|
|
209
|
+
def _contents(self, item: Item) -> tuple[str, list[str]]:
|
|
210
|
+
removed = self._folder_of(item.id, item.path)
|
|
211
|
+
base = self.layout.state / removed
|
|
212
|
+
if base.is_symlink() or not base.exists():
|
|
213
|
+
raise ForgeError(
|
|
214
|
+
ExitCode.DATA,
|
|
215
|
+
"item_file_missing",
|
|
216
|
+
f"{base} is missing or a link; run reindex before archiving",
|
|
217
|
+
)
|
|
218
|
+
if base.is_file():
|
|
219
|
+
self._check_text(base, base.name)
|
|
220
|
+
return removed, [base.name]
|
|
221
|
+
files: list[str] = []
|
|
222
|
+
for top, dirs, names in os.walk(base, followlinks=False):
|
|
223
|
+
dirs.sort()
|
|
224
|
+
for name in [*dirs, *names]:
|
|
225
|
+
if Path(top, name).is_symlink():
|
|
226
|
+
rel = Path(top, name).relative_to(base).as_posix()
|
|
227
|
+
raise ForgeError(
|
|
228
|
+
ExitCode.DATA,
|
|
229
|
+
"archive_link",
|
|
230
|
+
f"{removed}/{rel} is a link; links are not archived. Remove it first",
|
|
231
|
+
)
|
|
232
|
+
for name in sorted(names):
|
|
233
|
+
rel = Path(top, name).relative_to(base).as_posix()
|
|
234
|
+
self._check_text(Path(top, name), f"{removed}/{rel}")
|
|
235
|
+
files.append(rel)
|
|
236
|
+
primary = Path(item.path).relative_to(removed).as_posix()
|
|
237
|
+
files.sort(key=lambda f: (f != primary, f))
|
|
238
|
+
return removed, files
|
|
239
|
+
|
|
240
|
+
@staticmethod
|
|
241
|
+
def _check_text(path: Path, shown: str) -> None:
|
|
242
|
+
try:
|
|
243
|
+
path.read_text(encoding="utf-8")
|
|
244
|
+
except UnicodeDecodeError:
|
|
245
|
+
raise ForgeError(
|
|
246
|
+
ExitCode.DATA,
|
|
247
|
+
"archive_binary",
|
|
248
|
+
f"{shown} is not UTF-8 text; only text is archived. Move it out first",
|
|
249
|
+
) from None
|
|
250
|
+
|
|
251
|
+
def _remove(self, removed: str) -> None:
|
|
252
|
+
target = self.layout.state / removed
|
|
253
|
+
if target.is_dir():
|
|
254
|
+
shutil.rmtree(target)
|
|
255
|
+
else:
|
|
256
|
+
target.unlink(missing_ok=True)
|
|
257
|
+
|
|
258
|
+
def _render(
|
|
259
|
+
self,
|
|
260
|
+
view: ItemView,
|
|
261
|
+
target: str,
|
|
262
|
+
removed: str,
|
|
263
|
+
files: Sequence[str],
|
|
264
|
+
today: date,
|
|
265
|
+
) -> str:
|
|
266
|
+
item = view.item
|
|
267
|
+
front: dict[str, Any] = {
|
|
268
|
+
"schema": FRONT_MATTER_SCHEMA_VERSION,
|
|
269
|
+
"id": item.id,
|
|
270
|
+
"title": item.title,
|
|
271
|
+
"type": item.type,
|
|
272
|
+
"state": target,
|
|
273
|
+
**{k: v.default for k, v in self.domain.item_schema.items()},
|
|
274
|
+
**dict(item.fields),
|
|
275
|
+
"created": item.created,
|
|
276
|
+
}
|
|
277
|
+
lines = [
|
|
278
|
+
"",
|
|
279
|
+
f"# {item.label()}",
|
|
280
|
+
"",
|
|
281
|
+
f"Archived {today.isoformat()} from `{removed}` by `{self.domain.name} archive`. "
|
|
282
|
+
"Do not edit: the log records this file's hash.",
|
|
283
|
+
"",
|
|
284
|
+
"## History",
|
|
285
|
+
"",
|
|
286
|
+
"| When | By | From | To | Gate | Note |",
|
|
287
|
+
"|---|---|---|---|---|---|",
|
|
288
|
+
]
|
|
289
|
+
lines += [
|
|
290
|
+
f"| {h['ts'][:16].replace('T', ' ')} | {h['actor']} | {h['from']} | {h['to']} "
|
|
291
|
+
f"| {h.get('gate', '')} | {_cell(h.get('note', ''))} |"
|
|
292
|
+
for h in view.history
|
|
293
|
+
]
|
|
294
|
+
lines += [
|
|
295
|
+
f"| {today.isoformat()} | {self.identity.role} | {item.state} | {target} | | |",
|
|
296
|
+
]
|
|
297
|
+
sections: list[tuple[str, list[str]]] = [
|
|
298
|
+
("Gates", [f"- {g.id}{_DASH}{g.summary}{_DASH}{g.status()}" for g in view.gates]),
|
|
299
|
+
(
|
|
300
|
+
"Findings",
|
|
301
|
+
[
|
|
302
|
+
f"- {f.id}{_DASH}{f.severity}{_DASH}{f.summary}{_DASH}{f.status()}"
|
|
303
|
+
for f in view.findings
|
|
304
|
+
],
|
|
305
|
+
),
|
|
306
|
+
("Lessons", [f"- {x.label()}" for x in view.lessons]),
|
|
307
|
+
]
|
|
308
|
+
for heading, rows in sections:
|
|
309
|
+
lines += ["", f"## {heading} ({len(rows)})", ""]
|
|
310
|
+
lines += rows or ["None."]
|
|
311
|
+
lines += ["", "## Files", ""]
|
|
312
|
+
base = self.layout.state / removed
|
|
313
|
+
for rel in files:
|
|
314
|
+
path = base if base.is_file() else base / rel
|
|
315
|
+
text = path.read_text(encoding="utf-8")
|
|
316
|
+
fence = _fence(text)
|
|
317
|
+
ending = "" if text.endswith("\n") or not text else "\n"
|
|
318
|
+
lines += [f"### {rel}", "", f"{fence}text", text + ending + fence, ""]
|
|
319
|
+
return render_front_matter(front, self.domain) + "\n".join(lines).rstrip("\n") + "\n"
|