canon-cli 0.1.0__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.
- canon/__init__.py +3 -0
- canon/__main__.py +6 -0
- canon/activate.py +143 -0
- canon/audit.py +418 -0
- canon/brief.py +235 -0
- canon/cli.py +1143 -0
- canon/gitx.py +263 -0
- canon/ledger.py +418 -0
- canon/mcp.py +337 -0
- canon/prompt.py +45 -0
- canon/scope.py +589 -0
- canon/select.py +173 -0
- canon/verify.py +399 -0
- canon_cli-0.1.0.dist-info/METADATA +360 -0
- canon_cli-0.1.0.dist-info/RECORD +19 -0
- canon_cli-0.1.0.dist-info/WHEEL +5 -0
- canon_cli-0.1.0.dist-info/entry_points.txt +2 -0
- canon_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- canon_cli-0.1.0.dist-info/top_level.txt +1 -0
canon/brief.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""The brief: the only text an agent is asked to read.
|
|
2
|
+
|
|
3
|
+
A brief is a pure function of (snapshot, footprint, budget). Same inputs, same
|
|
4
|
+
bytes. That is what makes it cacheable, diffable and testable, and it is what
|
|
5
|
+
projection means: the markdown is downstream of the store, never the reverse.
|
|
6
|
+
|
|
7
|
+
Shape, borrowed from what works (OptMem's `wake`): fixed frame, hard rules
|
|
8
|
+
first, explicit budget, explicit omissions, and a closing instruction. Every
|
|
9
|
+
line either changes what the reader does or is deleted.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
|
|
16
|
+
from .gitx import Footprint
|
|
17
|
+
from .select import Jurisdiction, all_rules, estimate_tokens, select, token_budget
|
|
18
|
+
from .verify import has_predicate
|
|
19
|
+
|
|
20
|
+
__all__ = ["render", "render_json"]
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class Brief:
|
|
24
|
+
text: str
|
|
25
|
+
snapshot: str
|
|
26
|
+
kept: list[Jurisdiction]
|
|
27
|
+
omitted: list[Jurisdiction]
|
|
28
|
+
tokens: int
|
|
29
|
+
|
|
30
|
+
def to_dict(self) -> dict:
|
|
31
|
+
return {
|
|
32
|
+
"snapshot": self.snapshot,
|
|
33
|
+
"tokens": self.tokens,
|
|
34
|
+
"rules": [j.id for j in self.kept],
|
|
35
|
+
"omitted": [j.id for j in self.omitted],
|
|
36
|
+
"text": self.text,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _check_label(check) -> str:
|
|
41
|
+
if isinstance(check, str):
|
|
42
|
+
return check
|
|
43
|
+
if not isinstance(check, dict):
|
|
44
|
+
return "none — declared, not proven"
|
|
45
|
+
kind = str(check.get("kind", "shell"))
|
|
46
|
+
run = str(check.get("run", ""))
|
|
47
|
+
expect = str(check.get("expect", ""))
|
|
48
|
+
suffix = f" (expect {expect})" if expect else ""
|
|
49
|
+
if kind == "grep":
|
|
50
|
+
return f"grep {run!r} in changed files{suffix}"
|
|
51
|
+
if kind == "path":
|
|
52
|
+
return f"path {run}{suffix or ' (expect exists)'}"
|
|
53
|
+
if kind == "judge":
|
|
54
|
+
return f"judge: {run}"
|
|
55
|
+
if kind == "manual":
|
|
56
|
+
return "human judgement"
|
|
57
|
+
if kind == "none":
|
|
58
|
+
return "none — declared, not proven"
|
|
59
|
+
return run + suffix
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _rule_lines(j: Jurisdiction) -> list[str]:
|
|
63
|
+
rec = j.rec
|
|
64
|
+
out = [f"{j.id:<8}{rec.text}"]
|
|
65
|
+
pad = " " * 13
|
|
66
|
+
if rec.why:
|
|
67
|
+
out.append(f"{pad}why {rec.why}")
|
|
68
|
+
if not j.scope.covers_all():
|
|
69
|
+
out.append(f"{pad}when {j.scope.describe()}")
|
|
70
|
+
out.append(f"{pad}check {_check_label(rec.get('check'))}")
|
|
71
|
+
return out
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def render(
|
|
75
|
+
kept: list[Jurisdiction],
|
|
76
|
+
omitted: list[Jurisdiction],
|
|
77
|
+
fp: Footprint,
|
|
78
|
+
snapshot: str,
|
|
79
|
+
*,
|
|
80
|
+
task: str = "",
|
|
81
|
+
orientation: bool = False,
|
|
82
|
+
map_entries: list[tuple[str, list[str]]] | None = None,
|
|
83
|
+
) -> Brief:
|
|
84
|
+
rules = [j for j in kept if j.rec.kind == "rule"]
|
|
85
|
+
facts = [j for j in kept if j.rec.kind == "fact"]
|
|
86
|
+
|
|
87
|
+
head: list[str] = []
|
|
88
|
+
if orientation:
|
|
89
|
+
head.append(f"snapshot {snapshot} · orientation · no change observed yet")
|
|
90
|
+
head.append("read this, then run `canon brief` again once you have edited")
|
|
91
|
+
else:
|
|
92
|
+
counted = f"{len(rules)} rule(s)"
|
|
93
|
+
if facts:
|
|
94
|
+
counted += f", {len(facts)} fact(s)"
|
|
95
|
+
head.append(f"snapshot {snapshot} · {counted} · {fp.summary()}")
|
|
96
|
+
if task:
|
|
97
|
+
head.append(f"task {task}")
|
|
98
|
+
if fp.changes:
|
|
99
|
+
shown = ", ".join(c.path for c in fp.changes[:6])
|
|
100
|
+
more = "" if len(fp.changes) <= 6 else f" (+{len(fp.changes) - 6})"
|
|
101
|
+
head.append(f"change {shown}{more}")
|
|
102
|
+
|
|
103
|
+
sections = [
|
|
104
|
+
(
|
|
105
|
+
"HARD — the change is rejected if these fail",
|
|
106
|
+
[j for j in rules if j.sev == "hard" and has_predicate(j.rec)],
|
|
107
|
+
),
|
|
108
|
+
(
|
|
109
|
+
"HARD, UNPROVEN — binding in intent; nothing will catch a violation",
|
|
110
|
+
[j for j in rules if j.sev == "hard" and not has_predicate(j.rec)],
|
|
111
|
+
),
|
|
112
|
+
("SOFT — expected, judged on intent", [j for j in rules if j.sev == "soft"]),
|
|
113
|
+
("TIPS — only if relevant", [j for j in rules if j.sev == "advice"]),
|
|
114
|
+
]
|
|
115
|
+
body: list[str] = []
|
|
116
|
+
for title, group in sections:
|
|
117
|
+
if not group:
|
|
118
|
+
continue
|
|
119
|
+
body.append("")
|
|
120
|
+
body.append(f"## {title}")
|
|
121
|
+
for j in group:
|
|
122
|
+
body.extend(_rule_lines(j))
|
|
123
|
+
|
|
124
|
+
if facts:
|
|
125
|
+
body.append("")
|
|
126
|
+
body.append("## FACTS — true of this repository now")
|
|
127
|
+
for j in facts:
|
|
128
|
+
tag = "" if j.rec.get("reviewed", True) else " (unreviewed)"
|
|
129
|
+
body.append(f"{j.id:<8}{j.rec.text}{tag}")
|
|
130
|
+
|
|
131
|
+
tail: list[str] = []
|
|
132
|
+
if orientation and map_entries:
|
|
133
|
+
tail.append("")
|
|
134
|
+
tail.append("## WHERE THE REST LIVE")
|
|
135
|
+
tail.append(" scoped rules are loaded by path; touch one and re-run the brief")
|
|
136
|
+
for scope, ids in map_entries[:14]:
|
|
137
|
+
label = scope if len(scope) <= 40 else scope[:39] + "…"
|
|
138
|
+
tail.append(f" {label:<42}{', '.join(ids)}")
|
|
139
|
+
if len(map_entries) > 14:
|
|
140
|
+
tail.append(f" … and {len(map_entries) - 14} more (canon brief --all)")
|
|
141
|
+
if omitted:
|
|
142
|
+
ids = ", ".join(j.id for j in omitted)
|
|
143
|
+
tail.append("")
|
|
144
|
+
tail.append(f"## NOT SHOWN — {len(omitted)} over budget: {ids}")
|
|
145
|
+
tail.append(" `canon brief --all` for everything; `canon explain <id>` for one.")
|
|
146
|
+
if not kept and not map_entries:
|
|
147
|
+
tail.append("")
|
|
148
|
+
tail.append("No rule governs this change. Proceed, and run `canon check` before commit.")
|
|
149
|
+
else:
|
|
150
|
+
tail.append("")
|
|
151
|
+
tail.append("Verify with `canon check` before you commit. If a HARD rule fails,")
|
|
152
|
+
tail.append("fix the change — do not edit the rule and do not use --no-verify.")
|
|
153
|
+
|
|
154
|
+
text = "\n".join(["# canon brief"] + head + body + tail).rstrip() + "\n"
|
|
155
|
+
return Brief(
|
|
156
|
+
text=text,
|
|
157
|
+
snapshot=snapshot,
|
|
158
|
+
kept=kept,
|
|
159
|
+
omitted=omitted,
|
|
160
|
+
tokens=estimate_tokens(text),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def compile_for(
|
|
165
|
+
ledger,
|
|
166
|
+
fp: Footprint,
|
|
167
|
+
*,
|
|
168
|
+
max_tokens: int | None = 1200,
|
|
169
|
+
include_all: bool = False,
|
|
170
|
+
) -> tuple[list[Jurisdiction], list[Jurisdiction], list[tuple[str, list[str]]], bool]:
|
|
171
|
+
"""Everything the brief needs, from a ledger and a footprint.
|
|
172
|
+
|
|
173
|
+
Shared by the CLI and the MCP server so the two can never drift.
|
|
174
|
+
"""
|
|
175
|
+
orientation = not fp.changes
|
|
176
|
+
map_entries: list[tuple[str, list[str]]] = []
|
|
177
|
+
if orientation:
|
|
178
|
+
everything = all_rules(ledger)
|
|
179
|
+
|
|
180
|
+
def governs_editing(j: Jurisdiction) -> bool:
|
|
181
|
+
return bool({"write", "delete"} & set(j.scope.ops))
|
|
182
|
+
|
|
183
|
+
keep = [
|
|
184
|
+
j
|
|
185
|
+
for j in everything
|
|
186
|
+
if governs_editing(j) and (j.sev == "hard" or j.scope.covers_all())
|
|
187
|
+
]
|
|
188
|
+
kept, omitted = token_budget(keep, None if include_all else max_tokens)
|
|
189
|
+
shown = {j.id for j in kept}
|
|
190
|
+
buckets: dict[str, list[str]] = {}
|
|
191
|
+
for j in everything:
|
|
192
|
+
if j.id in shown or j.sev == "hard" or j.scope.covers_all():
|
|
193
|
+
continue
|
|
194
|
+
buckets.setdefault(j.scope.describe(), []).append(j.id)
|
|
195
|
+
map_entries = sorted(buckets.items(), key=lambda kv: (-len(kv[1]), kv[0]))
|
|
196
|
+
else:
|
|
197
|
+
kept, omitted = select(
|
|
198
|
+
ledger, fp, max_tokens=None if include_all else max_tokens,
|
|
199
|
+
include_all=include_all,
|
|
200
|
+
)
|
|
201
|
+
return kept, omitted, map_entries, orientation
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def render_json(
|
|
205
|
+
kept: list[Jurisdiction],
|
|
206
|
+
omitted: list[Jurisdiction],
|
|
207
|
+
fp: Footprint,
|
|
208
|
+
snapshot: str,
|
|
209
|
+
*,
|
|
210
|
+
task: str = "",
|
|
211
|
+
) -> dict:
|
|
212
|
+
return {
|
|
213
|
+
"snapshot": snapshot,
|
|
214
|
+
"task": task,
|
|
215
|
+
"footprint": {
|
|
216
|
+
"digest": fp.digest(),
|
|
217
|
+
"ops": sorted(fp.ops),
|
|
218
|
+
"changes": [{"path": c.path, "op": c.op} for c in fp.changes],
|
|
219
|
+
},
|
|
220
|
+
"rules": [
|
|
221
|
+
{
|
|
222
|
+
"id": j.id,
|
|
223
|
+
"sev": j.sev,
|
|
224
|
+
"kind": j.rec.kind,
|
|
225
|
+
"text": j.rec.text,
|
|
226
|
+
"why": j.rec.why,
|
|
227
|
+
"scope": j.scope.describe(),
|
|
228
|
+
"matched": j.matched,
|
|
229
|
+
"check": j.rec.get("check"),
|
|
230
|
+
"rev": j.rec.rev,
|
|
231
|
+
}
|
|
232
|
+
for j in kept
|
|
233
|
+
],
|
|
234
|
+
"omitted": [{"id": j.id, "sev": j.sev} for j in omitted],
|
|
235
|
+
}
|