contextplate 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.
- contextplate/__init__.py +7 -0
- contextplate/__main__.py +5 -0
- contextplate/adapters/__init__.py +50 -0
- contextplate/adapters/aider.py +28 -0
- contextplate/adapters/base.py +85 -0
- contextplate/adapters/claude.py +181 -0
- contextplate/adapters/codex.py +119 -0
- contextplate/adapters/copilot.py +57 -0
- contextplate/adapters/core.py +197 -0
- contextplate/adapters/cursor.py +55 -0
- contextplate/adapters/gemini.py +31 -0
- contextplate/adopt.py +596 -0
- contextplate/cli.py +308 -0
- contextplate/config.py +306 -0
- contextplate/doctor.py +147 -0
- contextplate/frontmatter.py +226 -0
- contextplate/globs.py +84 -0
- contextplate/model.py +185 -0
- contextplate/presets.py +165 -0
- contextplate/scaffold.py +162 -0
- contextplate/templates/check.sh +44 -0
- contextplate/templates/hooks/format.py +74 -0
- contextplate/templates/hooks/guard.py +121 -0
- contextplate/templates/hooks/session_context.py +79 -0
- contextplate/templates/scaffold/AGENTS.md +29 -0
- contextplate/templates/scaffold/agents/agents/researcher.md +16 -0
- contextplate/templates/scaffold/agents/agents/reviewer.md +20 -0
- contextplate/templates/scaffold/agents/agents/test-runner.md +17 -0
- contextplate/templates/scaffold/agents/rules/ai-assets.md +12 -0
- contextplate/templates/scaffold/agents/rules/docs.md +11 -0
- contextplate/templates/scaffold/agents/skills/adr/SKILL.md +12 -0
- contextplate/templates/scaffold/agents/skills/adr/template.md +21 -0
- contextplate/templates/scaffold/agents/skills/handoff/SKILL.md +17 -0
- contextplate/templates/scaffold/agents/skills/plan-task/SKILL.md +15 -0
- contextplate/templates/scaffold/agents/skills/plan-task/template.md +28 -0
- contextplate/templates/scaffold/ai_app/data/README.md +5 -0
- contextplate/templates/scaffold/ai_app/evals/README.md +7 -0
- contextplate/templates/scaffold/ai_app/prompts/README.md +17 -0
- contextplate/templates/scaffold/docs/agent-context.md +42 -0
- contextplate/templates/scaffold/docs/architecture.md +27 -0
- contextplate/templates/scaffold/docs/decisions/0001-record-architecture-decisions.md +22 -0
- contextplate/templates/scaffold/docs/glossary.md +7 -0
- contextplate/templates/scaffold/work/handoff.md +22 -0
- contextplate/writer.py +396 -0
- contextplate-0.1.0.dist-info/METADATA +201 -0
- contextplate-0.1.0.dist-info/RECORD +49 -0
- contextplate-0.1.0.dist-info/WHEEL +4 -0
- contextplate-0.1.0.dist-info/entry_points.txt +3 -0
- contextplate-0.1.0.dist-info/licenses/LICENSE +21 -0
contextplate/writer.py
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"""Non-destructive file writer with ownership tracking.
|
|
2
|
+
|
|
3
|
+
Three strategies:
|
|
4
|
+
- owned: contextplate writes the whole file. A file that changed since contextplate
|
|
5
|
+
last wrote it (or that contextplate never wrote) is never overwritten without --force.
|
|
6
|
+
- block: contextplate owns a marked region inside a file the user owns
|
|
7
|
+
(AGENTS.md, CLAUDE.md, .gitignore, TOML/YAML configs).
|
|
8
|
+
- json: contextplate merges its keys into a JSON file the user owns and removes
|
|
9
|
+
only the entries it added last time.
|
|
10
|
+
|
|
11
|
+
Ownership lives in .agents/contextplate.lock.json, which should be committed.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import copy
|
|
17
|
+
import hashlib
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import re
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
from . import LOCK_PATH, __version__
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class Output:
|
|
29
|
+
path: str
|
|
30
|
+
strategy: str = "owned" # owned | block | json
|
|
31
|
+
content: str = ""
|
|
32
|
+
data: dict | None = None
|
|
33
|
+
block_id: str = ""
|
|
34
|
+
position: str = "bottom" # block placement when inserting: top | bottom
|
|
35
|
+
executable: bool = False
|
|
36
|
+
owner: str = "core"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class Action:
|
|
41
|
+
kind: str # create | update | unchanged | conflict | delete | keep
|
|
42
|
+
path: str
|
|
43
|
+
detail: str = ""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class Result:
|
|
48
|
+
actions: list[Action] = field(default_factory=list)
|
|
49
|
+
|
|
50
|
+
def count(self, kind: str) -> int:
|
|
51
|
+
return sum(1 for a in self.actions if a.kind == kind)
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def changed(self) -> bool:
|
|
55
|
+
return any(a.kind in ("create", "update", "delete", "conflict") for a in self.actions)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def sha(text: str) -> str:
|
|
59
|
+
return hashlib.sha256(text.encode("utf-8")).hexdigest()[:16]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def same_modulo_whitespace(a: str, b: str) -> bool:
|
|
63
|
+
"""True when two texts differ only in whitespace (e.g. a formatter re-wrapped them)."""
|
|
64
|
+
return re.sub(r"\s+", "", a) == re.sub(r"\s+", "", b)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def read_text(p: Path) -> str:
|
|
68
|
+
return p.read_text(encoding="utf-8")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def write_text(p: Path, text: str, executable: bool = False) -> None:
|
|
72
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
with open(p, "w", encoding="utf-8", newline="\n") as fh:
|
|
74
|
+
fh.write(text)
|
|
75
|
+
if executable and os.name != "nt":
|
|
76
|
+
p.chmod(p.stat().st_mode | 0o111)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# ---------------------------------------------------------------- lock
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def load_lock(root: Path) -> dict:
|
|
83
|
+
p = root / LOCK_PATH
|
|
84
|
+
if not p.exists():
|
|
85
|
+
return {"version": 1, "files": {}}
|
|
86
|
+
try:
|
|
87
|
+
lock = json.loads(read_text(p))
|
|
88
|
+
except ValueError:
|
|
89
|
+
return {"version": 1, "files": {}}
|
|
90
|
+
lock.setdefault("files", {})
|
|
91
|
+
return lock
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def save_lock(root: Path, lock: dict) -> None:
|
|
95
|
+
lock["version"] = 1
|
|
96
|
+
lock["generator"] = f"ctxplate {__version__}"
|
|
97
|
+
lock["files"] = dict(sorted(lock["files"].items()))
|
|
98
|
+
write_text(root / LOCK_PATH, json.dumps(lock, indent=2, ensure_ascii=False) + "\n")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# ---------------------------------------------------------------- blocks
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _markers(path: str, block_id: str) -> tuple[str, str]:
|
|
105
|
+
note = "generated by ctxplate; edit .agents/ and run `ctxplate sync`"
|
|
106
|
+
if path.endswith((".md", ".mdc", ".html")):
|
|
107
|
+
return f"<!-- ctxplate:begin {block_id} ({note}) -->", f"<!-- ctxplate:end {block_id} -->"
|
|
108
|
+
return f"# ctxplate:begin {block_id} ({note})", f"# ctxplate:end {block_id}"
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _block_re(path: str, block_id: str) -> re.Pattern:
|
|
112
|
+
bid = re.escape(block_id)
|
|
113
|
+
if path.endswith((".md", ".mdc", ".html")):
|
|
114
|
+
return re.compile(
|
|
115
|
+
rf"<!-- ctxplate:begin {bid}\b[^\n]*-->\n.*?<!-- ctxplate:end {bid} -->\n?", re.S
|
|
116
|
+
)
|
|
117
|
+
return re.compile(rf"# ctxplate:begin {bid}\b[^\n]*\n.*?# ctxplate:end {bid}\n?", re.S)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def has_block(text: str, path: str, block_id: str) -> bool:
|
|
121
|
+
return bool(_block_re(path, block_id).search(text))
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def apply_block(text: str, path: str, block_id: str, content: str, position: str) -> str:
|
|
125
|
+
begin, end = _markers(path, block_id)
|
|
126
|
+
if path.endswith((".md", ".mdc", ".html")):
|
|
127
|
+
# Blank lines around the content match what markdown formatters (prettier) emit.
|
|
128
|
+
block = f"{begin}\n\n{content.strip()}\n\n{end}\n"
|
|
129
|
+
else:
|
|
130
|
+
block = f"{begin}\n{content.rstrip()}\n{end}\n"
|
|
131
|
+
rx = _block_re(path, block_id)
|
|
132
|
+
if rx.search(text):
|
|
133
|
+
return rx.sub(lambda _m: block, text, count=1)
|
|
134
|
+
if not text.strip():
|
|
135
|
+
return block
|
|
136
|
+
if position == "top":
|
|
137
|
+
return block + "\n" + text.lstrip("\n")
|
|
138
|
+
return text.rstrip("\n") + "\n\n" + block
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def remove_block(text: str, path: str, block_id: str) -> str:
|
|
142
|
+
out = _block_re(path, block_id).sub("", text, count=1)
|
|
143
|
+
return re.sub(r"\n{3,}", "\n\n", out)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def strip_blocks(text: str, path: str) -> str:
|
|
147
|
+
"""Text outside any contextplate block (used to check what the user wrote)."""
|
|
148
|
+
if path.endswith((".md", ".mdc", ".html")):
|
|
149
|
+
rx = re.compile(r"<!-- ctxplate:begin (\S+)[^\n]*-->\n.*?<!-- ctxplate:end \1 -->\n?", re.S)
|
|
150
|
+
else:
|
|
151
|
+
rx = re.compile(r"# ctxplate:begin (\S+)[^\n]*\n.*?# ctxplate:end \1\n?", re.S)
|
|
152
|
+
return rx.sub("", text)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
# ---------------------------------------------------------------- json merge
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def json_subtract(a, prev):
|
|
159
|
+
if isinstance(a, dict) and isinstance(prev, dict):
|
|
160
|
+
out = dict(a)
|
|
161
|
+
for k, pv in prev.items():
|
|
162
|
+
if k not in out:
|
|
163
|
+
continue
|
|
164
|
+
av = out[k]
|
|
165
|
+
if isinstance(av, dict) and isinstance(pv, dict):
|
|
166
|
+
sub = json_subtract(av, pv)
|
|
167
|
+
if sub:
|
|
168
|
+
out[k] = sub
|
|
169
|
+
else:
|
|
170
|
+
del out[k]
|
|
171
|
+
elif isinstance(av, list) and isinstance(pv, list):
|
|
172
|
+
kept = [x for x in av if x not in pv]
|
|
173
|
+
if kept:
|
|
174
|
+
out[k] = kept
|
|
175
|
+
else:
|
|
176
|
+
del out[k]
|
|
177
|
+
elif av == pv:
|
|
178
|
+
del out[k]
|
|
179
|
+
return out
|
|
180
|
+
return a
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def json_add(a, new):
|
|
184
|
+
if isinstance(a, dict) and isinstance(new, dict):
|
|
185
|
+
out = dict(a)
|
|
186
|
+
for k, nv in new.items():
|
|
187
|
+
if k in out:
|
|
188
|
+
out[k] = json_add(out[k], nv)
|
|
189
|
+
else:
|
|
190
|
+
out[k] = copy.deepcopy(nv)
|
|
191
|
+
return out
|
|
192
|
+
if isinstance(a, list) and isinstance(new, list):
|
|
193
|
+
return a + [x for x in new if x not in a]
|
|
194
|
+
return copy.deepcopy(new)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def json_merge_fragments(frags: list[dict]) -> dict:
|
|
198
|
+
out: dict = {}
|
|
199
|
+
for f in frags:
|
|
200
|
+
out = json_add(out, f)
|
|
201
|
+
return out
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def dump_json(data: dict) -> str:
|
|
205
|
+
return json.dumps(data, indent=2, ensure_ascii=False) + "\n"
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
# ---------------------------------------------------------------- planning
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _consolidate(outputs: list[Output]) -> dict[str, list[Output]]:
|
|
212
|
+
by_path: dict[str, list[Output]] = {}
|
|
213
|
+
for o in outputs:
|
|
214
|
+
by_path.setdefault(o.path, []).append(o)
|
|
215
|
+
for path, outs in by_path.items():
|
|
216
|
+
kinds = {o.strategy for o in outs}
|
|
217
|
+
if len(kinds) > 1:
|
|
218
|
+
raise ValueError(f"{path}: mixed strategies {kinds}")
|
|
219
|
+
if "owned" in kinds and len({o.content for o in outs}) > 1:
|
|
220
|
+
owners = ", ".join(o.owner for o in outs)
|
|
221
|
+
raise ValueError(f"{path}: produced differently by {owners}")
|
|
222
|
+
return by_path
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def sync(
|
|
226
|
+
root: Path, outputs: list[Output], *, dry_run: bool = False, force: bool = False
|
|
227
|
+
) -> Result:
|
|
228
|
+
lock = load_lock(root)
|
|
229
|
+
files: dict = lock["files"]
|
|
230
|
+
new_files: dict = {}
|
|
231
|
+
result = Result()
|
|
232
|
+
by_path = _consolidate(outputs)
|
|
233
|
+
|
|
234
|
+
for path, outs in sorted(by_path.items()):
|
|
235
|
+
target = root / path
|
|
236
|
+
prev = files.get(path)
|
|
237
|
+
strategy = outs[0].strategy
|
|
238
|
+
|
|
239
|
+
if strategy == "owned":
|
|
240
|
+
o = outs[0]
|
|
241
|
+
new_sha = sha(o.content)
|
|
242
|
+
if not target.exists():
|
|
243
|
+
result.actions.append(Action("create", path))
|
|
244
|
+
if not dry_run:
|
|
245
|
+
write_text(target, o.content, o.executable)
|
|
246
|
+
new_files[path] = {"strategy": "owned", "sha": new_sha}
|
|
247
|
+
continue
|
|
248
|
+
cur = read_text(target)
|
|
249
|
+
cur_sha = sha(cur)
|
|
250
|
+
if cur_sha == new_sha:
|
|
251
|
+
result.actions.append(Action("unchanged", path))
|
|
252
|
+
new_files[path] = {"strategy": "owned", "sha": new_sha}
|
|
253
|
+
if (
|
|
254
|
+
o.executable
|
|
255
|
+
and not dry_run
|
|
256
|
+
and os.name != "nt"
|
|
257
|
+
and not os.access(target, os.X_OK)
|
|
258
|
+
):
|
|
259
|
+
target.chmod(target.stat().st_mode | 0o111)
|
|
260
|
+
elif same_modulo_whitespace(cur, o.content) and not force:
|
|
261
|
+
# A formatter touched it; content is equivalent. Adopt the file as-is.
|
|
262
|
+
result.actions.append(Action("unchanged", path, "reformatted"))
|
|
263
|
+
new_files[path] = {"strategy": "owned", "sha": cur_sha}
|
|
264
|
+
elif (prev and prev.get("sha") == cur_sha) or force:
|
|
265
|
+
result.actions.append(
|
|
266
|
+
Action(
|
|
267
|
+
"update",
|
|
268
|
+
path,
|
|
269
|
+
"forced" if force and not (prev and prev.get("sha") == cur_sha) else "",
|
|
270
|
+
)
|
|
271
|
+
)
|
|
272
|
+
if not dry_run:
|
|
273
|
+
write_text(target, o.content, o.executable)
|
|
274
|
+
new_files[path] = {"strategy": "owned", "sha": new_sha}
|
|
275
|
+
else:
|
|
276
|
+
why = "you edited it" if prev else "file exists and ctxplate did not create it"
|
|
277
|
+
result.actions.append(Action("conflict", path, f"{why}; use --force to overwrite"))
|
|
278
|
+
if prev:
|
|
279
|
+
new_files[path] = prev
|
|
280
|
+
|
|
281
|
+
elif strategy == "block":
|
|
282
|
+
existed = target.exists()
|
|
283
|
+
text = read_text(target) if existed else ""
|
|
284
|
+
new_text = text
|
|
285
|
+
ids = []
|
|
286
|
+
for o in outs:
|
|
287
|
+
new_text = apply_block(new_text, path, o.block_id, o.content, o.position)
|
|
288
|
+
ids.append(o.block_id)
|
|
289
|
+
# drop blocks we used to manage in this file but no longer produce
|
|
290
|
+
for old_id in (prev or {}).get("blocks", []):
|
|
291
|
+
if old_id not in ids:
|
|
292
|
+
new_text = remove_block(new_text, path, old_id)
|
|
293
|
+
created = (prev or {}).get("created", not existed)
|
|
294
|
+
if new_text == text:
|
|
295
|
+
result.actions.append(Action("unchanged", path))
|
|
296
|
+
else:
|
|
297
|
+
result.actions.append(
|
|
298
|
+
Action("update" if existed else "create", path, "managed block")
|
|
299
|
+
)
|
|
300
|
+
if not dry_run:
|
|
301
|
+
write_text(target, new_text)
|
|
302
|
+
new_files[path] = {"strategy": "block", "blocks": sorted(ids), "created": created}
|
|
303
|
+
|
|
304
|
+
elif strategy == "json":
|
|
305
|
+
existed = target.exists()
|
|
306
|
+
managed = json_merge_fragments([o.data or {} for o in outs])
|
|
307
|
+
if not existed and not managed:
|
|
308
|
+
continue
|
|
309
|
+
try:
|
|
310
|
+
cur = json.loads(read_text(target)) if existed else {}
|
|
311
|
+
except ValueError:
|
|
312
|
+
result.actions.append(
|
|
313
|
+
Action("conflict", path, "existing file is not valid JSON; fix it and re-run")
|
|
314
|
+
)
|
|
315
|
+
if prev:
|
|
316
|
+
new_files[path] = prev
|
|
317
|
+
continue
|
|
318
|
+
merged = json_add(json_subtract(cur, (prev or {}).get("managed", {})), managed)
|
|
319
|
+
created = (prev or {}).get("created", not existed)
|
|
320
|
+
if existed and merged == cur:
|
|
321
|
+
result.actions.append(Action("unchanged", path))
|
|
322
|
+
else:
|
|
323
|
+
result.actions.append(
|
|
324
|
+
Action("update" if existed else "create", path, "merged keys")
|
|
325
|
+
)
|
|
326
|
+
if not dry_run:
|
|
327
|
+
write_text(target, dump_json(merged))
|
|
328
|
+
new_files[path] = {"strategy": "json", "managed": managed, "created": created}
|
|
329
|
+
|
|
330
|
+
# orphans: things we generated before but no longer produce
|
|
331
|
+
for path, prev in sorted(files.items()):
|
|
332
|
+
if path in by_path:
|
|
333
|
+
continue
|
|
334
|
+
target = root / path
|
|
335
|
+
if not target.exists():
|
|
336
|
+
continue
|
|
337
|
+
strategy = prev.get("strategy")
|
|
338
|
+
if strategy == "owned":
|
|
339
|
+
if sha(read_text(target)) == prev.get("sha"):
|
|
340
|
+
result.actions.append(Action("delete", path, "no longer generated"))
|
|
341
|
+
if not dry_run:
|
|
342
|
+
target.unlink()
|
|
343
|
+
_prune_dirs(root, target.parent)
|
|
344
|
+
else:
|
|
345
|
+
result.actions.append(
|
|
346
|
+
Action(
|
|
347
|
+
"keep",
|
|
348
|
+
path,
|
|
349
|
+
"no longer generated, but you edited it; delete it yourself if unneeded",
|
|
350
|
+
)
|
|
351
|
+
)
|
|
352
|
+
elif strategy == "block":
|
|
353
|
+
text = read_text(target)
|
|
354
|
+
new_text = text
|
|
355
|
+
for bid in prev.get("blocks", []):
|
|
356
|
+
new_text = remove_block(new_text, path, bid)
|
|
357
|
+
if prev.get("created") and not new_text.strip():
|
|
358
|
+
result.actions.append(Action("delete", path, "no longer generated"))
|
|
359
|
+
if not dry_run:
|
|
360
|
+
target.unlink()
|
|
361
|
+
_prune_dirs(root, target.parent)
|
|
362
|
+
elif new_text != text:
|
|
363
|
+
result.actions.append(Action("update", path, "removed managed block"))
|
|
364
|
+
if not dry_run:
|
|
365
|
+
write_text(target, new_text.lstrip("\n"))
|
|
366
|
+
elif strategy == "json":
|
|
367
|
+
try:
|
|
368
|
+
cur = json.loads(read_text(target))
|
|
369
|
+
except ValueError:
|
|
370
|
+
continue
|
|
371
|
+
remaining = json_subtract(cur, prev.get("managed", {}))
|
|
372
|
+
if prev.get("created") and not remaining:
|
|
373
|
+
result.actions.append(Action("delete", path, "no longer generated"))
|
|
374
|
+
if not dry_run:
|
|
375
|
+
target.unlink()
|
|
376
|
+
_prune_dirs(root, target.parent)
|
|
377
|
+
elif remaining != cur:
|
|
378
|
+
result.actions.append(Action("update", path, "removed managed keys"))
|
|
379
|
+
if not dry_run:
|
|
380
|
+
write_text(target, dump_json(remaining))
|
|
381
|
+
|
|
382
|
+
if not dry_run:
|
|
383
|
+
lock["files"] = new_files
|
|
384
|
+
save_lock(root, lock)
|
|
385
|
+
return result
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def _prune_dirs(root: Path, d: Path) -> None:
|
|
389
|
+
root = root.resolve()
|
|
390
|
+
d = d.resolve()
|
|
391
|
+
while d != root and root in d.parents:
|
|
392
|
+
try:
|
|
393
|
+
d.rmdir()
|
|
394
|
+
except OSError:
|
|
395
|
+
return
|
|
396
|
+
d = d.parent
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: contextplate
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Boilerplate for agent context. Declare it once. Render it for Claude Code, Codex, Copilot, Cursor, Gemini CLI, and any AGENTS.md tool.
|
|
5
|
+
Project-URL: Homepage, https://github.com/deepskpathak/contextplate
|
|
6
|
+
Project-URL: Issues, https://github.com/deepskpathak/contextplate/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/deepskpathak/contextplate/blob/master/CHANGELOG.md
|
|
8
|
+
Project-URL: Documentation, https://github.com/deepskpathak/contextplate/tree/master/docs
|
|
9
|
+
Author: ContextPlate contributors
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: AGENTS.md,agent-skills,agents,claude-code,codex,context-engineering,copilot,cursor,gemini-cli,scaffolding
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff==0.17.6; extra == 'dev'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# ContextPlate
|
|
29
|
+
|
|
30
|
+
[](https://github.com/deepskpathak/contextplate/actions/workflows/ci.yml)
|
|
31
|
+
[](https://pypi.org/project/contextplate/)
|
|
32
|
+
[](https://pypi.org/project/contextplate/)
|
|
33
|
+
[](LICENSE)
|
|
34
|
+
|
|
35
|
+
**Boilerplate for agent context.** Declare it once; render it for every harness.
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pipx install contextplate # installs the `ctxplate` command
|
|
39
|
+
ctxplate init my-app
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
ContextPlate keeps one harness-neutral source of truth (`AGENTS.md` plus `.agents/`) and generates the native files each tool expects: Claude Code, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, and Aider. Any other tool that reads `AGENTS.md` works with no extra setup.
|
|
43
|
+
|
|
44
|
+
It scaffolds new projects, organizes existing ones, and never overwrites a file you edited.
|
|
45
|
+
|
|
46
|
+
```text
|
|
47
|
+
you edit ctxplate sync each tool reads
|
|
48
|
+
┌───────────────────────────┐ ┌──────────────────────────────┐
|
|
49
|
+
│ AGENTS.md │ │ CLAUDE.md, .claude/... │
|
|
50
|
+
│ .agents/contextplate.toml │ ─────────────────────────────────► │ .cursor/rules/*.mdc, ... │
|
|
51
|
+
│ .agents/rules/*.md │ │ .github/instructions/, ... │
|
|
52
|
+
│ .agents/skills/*/SKILL.md │ │ .codex/agents/*.toml, ... │
|
|
53
|
+
│ .agents/agents/*.md │ │ .gemini/settings.json, ... │
|
|
54
|
+
└───────────────────────────┘ └──────────────────────────────┘
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Why
|
|
58
|
+
|
|
59
|
+
- **Every tool wants different files.** Scoped rules use `paths` in Claude Code, `globs` in Cursor `.mdc` files, and `applyTo` in Copilot. Subagents are Markdown in one tool and TOML in another. Keeping them in sync by hand drifts within a week.
|
|
60
|
+
- **Context is a budget.** Everything in an always-loaded file costs tokens on every turn. ContextPlate keeps the always-on layer small and moves the rest into rules, skills, and subagents that load only when relevant. `ctxplate doctor` measures it.
|
|
61
|
+
- **Instructions ask; config enforces.** Protected paths become real permission rules and a pre-edit guard hook, not a sentence the model may ignore.
|
|
62
|
+
|
|
63
|
+
## Quick start
|
|
64
|
+
|
|
65
|
+
Requires Python 3.11+. No runtime dependencies.
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pipx install contextplate # or: uvx contextplate ... (command: ctxplate)
|
|
69
|
+
# before the first PyPI release: pipx install git+https://github.com/deepskpathak/contextplate
|
|
70
|
+
|
|
71
|
+
# New project
|
|
72
|
+
ctxplate init my-app --preset python # harnesses default to the agent CLIs on your PATH
|
|
73
|
+
|
|
74
|
+
# Existing repository (dry run first, then apply)
|
|
75
|
+
cd my-repo
|
|
76
|
+
ctxplate adopt # shows exactly what would change
|
|
77
|
+
ctxplate adopt --apply # imports existing instructions, rules, skills, subagents, MCP servers
|
|
78
|
+
|
|
79
|
+
# Day to day
|
|
80
|
+
ctxplate sync # after editing anything in .agents/
|
|
81
|
+
ctxplate doctor # context budget, placeholders, portability, drift
|
|
82
|
+
ctxplate enable cursor # add a harness later
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## What you get
|
|
86
|
+
|
|
87
|
+
```text
|
|
88
|
+
my-app/
|
|
89
|
+
├── AGENTS.md # the always-on layer (you own it; two small blocks are managed)
|
|
90
|
+
├── CLAUDE.md # @AGENTS.md plus Claude-specific notes (managed block)
|
|
91
|
+
├── .agents/
|
|
92
|
+
│ ├── contextplate.toml # harnesses, modules, commands, protected paths, MCP servers
|
|
93
|
+
│ ├── contextplate.lock.json # what ContextPlate generated (commit it)
|
|
94
|
+
│ ├── rules/ # scoped rules: `paths:` frontmatter, loaded on demand
|
|
95
|
+
│ ├── skills/ # Agent Skills (SKILL.md), read natively by Codex, Copilot, Cursor, Gemini
|
|
96
|
+
│ ├── agents/ # roles (subagents) in one neutral format
|
|
97
|
+
│ └── hooks/ # guard, session snapshot, formatter (stdlib Python)
|
|
98
|
+
├── docs/ # architecture, glossary, decisions/ (module: docs)
|
|
99
|
+
├── work/ # handoff.md, plans/, scratch/ (module: work)
|
|
100
|
+
├── scripts/check.sh # lint + types + tests, terse output (module: check_script)
|
|
101
|
+
└── generated per tool: .claude/ .cursor/ .github/{instructions,agents}/ .codex/ .gemini/ .aider.conf.yml
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Optional `ai_app` module adds `prompts/`, `evals/`, and `data/` with matching rules and read protection.
|
|
105
|
+
|
|
106
|
+
## Supported harnesses
|
|
107
|
+
|
|
108
|
+
| | Instructions | Scoped rules | Skills | Subagents | MCP | Hooks |
|
|
109
|
+
|---|---|---|---|---|---|---|
|
|
110
|
+
| **Claude Code** | `CLAUDE.md` imports `AGENTS.md` | `.claude/rules/` | `.claude/skills/` (copied) | `.claude/agents/` | `.mcp.json` | guard, snapshot, format |
|
|
111
|
+
| **Codex** | `AGENTS.md` (native) | index in `AGENTS.md` | `.agents/skills/` (native) | `.codex/agents/*.toml` | `.codex/config.toml` block | |
|
|
112
|
+
| **Copilot** | `AGENTS.md` (native) | `.github/instructions/` | `.agents/skills/` (native) | `.github/agents/` | `.mcp.json` | |
|
|
113
|
+
| **Cursor** | `AGENTS.md` (native) | `.cursor/rules/*.mdc` | `.agents/skills/` (native) | `.cursor/agents/` | `.cursor/mcp.json` | |
|
|
114
|
+
| **Gemini CLI** | `.gemini/settings.json` points at `AGENTS.md` | index in `AGENTS.md` | `.agents/skills/` | `.gemini/agents/` | `.gemini/settings.json` | |
|
|
115
|
+
| **Aider** | `.aider.conf.yml` reads `AGENTS.md` | index in `AGENTS.md` | | | | |
|
|
116
|
+
| **Anything else** | `AGENTS.md` | index in `AGENTS.md` | `.agents/skills/` | read the role file | | |
|
|
117
|
+
|
|
118
|
+
Formats were checked against each vendor's documentation; see [docs/harnesses.md](docs/harnesses.md) for sources and caveats.
|
|
119
|
+
|
|
120
|
+
## How it stays safe
|
|
121
|
+
|
|
122
|
+
- **Your edits win.** ContextPlate records a hash for every file it writes. A generated file you changed is reported as a conflict and left alone unless you pass `--force`. Whitespace-only changes (formatters) are not conflicts.
|
|
123
|
+
- **Managed blocks, not takeovers.** In files you own (`AGENTS.md`, `CLAUDE.md`, `.gitignore`, `.codex/config.toml`), ContextPlate edits only its marked block. In JSON configs (`.claude/settings.json`, `.mcp.json`, `.gemini/settings.json`) it merges its keys and later removes only what it added.
|
|
124
|
+
- **Adopt is a dry run by default.** With `--apply`, originals that would be duplicated are moved to `.agents/imported/*.bak`, never deleted.
|
|
125
|
+
- **Hooks are small and local.** Standard-library Python, no network, no telemetry. Read them in `.agents/hooks/`.
|
|
126
|
+
|
|
127
|
+
## Concepts
|
|
128
|
+
|
|
129
|
+
- **Four context tiers:** always on (`AGENTS.md`), on demand (rules, skills, docs), session state (`work/handoff.md` plus a git snapshot injected at start), isolated (subagents return summaries). See [docs/concepts.md](docs/concepts.md).
|
|
130
|
+
- **Neutral formats:** rules use `paths:`; skills follow the Agent Skills spec with tool hints under `metadata`; roles use neutral `tools` (`read, search, web, shell, edit`) and model tiers (`fast, strong, inherit`). Tool-specific extras use prefixed keys such as `claude.memory: project`.
|
|
131
|
+
- **Lightly opinionated:** presets (`generic, python, node, go, rust, jvm`) and modules (`docs, work, ai_app, hooks, check_script`) are defaults, not requirements. Everything is a line in `.agents/contextplate.toml`.
|
|
132
|
+
|
|
133
|
+
## Commands
|
|
134
|
+
|
|
135
|
+
| Command | What it does |
|
|
136
|
+
|---|---|
|
|
137
|
+
| `ctxplate init [dir]` | Scaffold a new project. `--preset`, `--harness a,b\|all\|none`, `--with` / `--without` modules |
|
|
138
|
+
| `ctxplate adopt [dir]` | Organize an existing repo. Dry run unless `--apply`; `--minimal` imports only |
|
|
139
|
+
| `ctxplate sync` | Regenerate harness files. `--check` for CI, `--dry-run`, `--force` |
|
|
140
|
+
| `ctxplate doctor` | Budget, placeholders, skill portability, drift. `--strict` fails on warnings |
|
|
141
|
+
| `ctxplate enable/disable <harness>` | Edit the harness list and sync |
|
|
142
|
+
| `ctxplate list` | Show harnesses, presets, and modules |
|
|
143
|
+
|
|
144
|
+
### In CI and pre-commit
|
|
145
|
+
|
|
146
|
+
Fail builds when generated files drift: run `ctxplate sync --check` in CI, or add the hook to `.pre-commit-config.yaml`:
|
|
147
|
+
|
|
148
|
+
```yaml
|
|
149
|
+
- repo: https://github.com/deepskpathak/contextplate
|
|
150
|
+
rev: v0.1.0
|
|
151
|
+
hooks:
|
|
152
|
+
- id: ctxplate-sync-check
|
|
153
|
+
- id: ctxplate-doctor # optional
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Configuration
|
|
157
|
+
|
|
158
|
+
See [docs/configuration.md](docs/configuration.md) for every key and the canonical file formats. A taste:
|
|
159
|
+
|
|
160
|
+
```toml
|
|
161
|
+
harnesses = ["claude", "codex", "copilot", "cursor", "gemini"]
|
|
162
|
+
|
|
163
|
+
[project]
|
|
164
|
+
name = "my-app"
|
|
165
|
+
preset = "python"
|
|
166
|
+
|
|
167
|
+
[commands]
|
|
168
|
+
test = "pytest -q"
|
|
169
|
+
lint = "ruff check ."
|
|
170
|
+
|
|
171
|
+
[protect]
|
|
172
|
+
no_read = [".env", ".env.*", "!.env.example"]
|
|
173
|
+
no_edit = [".env", ".env.*", "!.env.example", ".git/**", "uv.lock"]
|
|
174
|
+
|
|
175
|
+
[mcp.servers.docs]
|
|
176
|
+
url = "https://mcp.context7.com/mcp"
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## FAQ
|
|
180
|
+
|
|
181
|
+
**Should I commit the generated files?** Yes. Teammates and cloud agents read them directly, and nobody needs ContextPlate installed to benefit. Run `ctxplate sync --check` in CI to catch drift.
|
|
182
|
+
|
|
183
|
+
**Can I edit a generated file?** You can, and ContextPlate will not overwrite it. The better path is to edit the source in `.agents/` so every tool gets the change.
|
|
184
|
+
|
|
185
|
+
**Why do Copilot and Cursor show a skill twice?** Both tools read `.claude/skills/` as well as `.agents/skills/`. Claude Code only reads `.claude/skills/`, so the copy is needed when Claude is enabled.
|
|
186
|
+
|
|
187
|
+
**Windows?** Supported. Set `[hooks] python = "py"` (or `"python"`) and run `scripts/check.sh` from Git Bash or WSL.
|
|
188
|
+
|
|
189
|
+
**A tool changed its format.** Open an issue with a link to the vendor's docs. Adapters are small, single-file modules; see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
190
|
+
|
|
191
|
+
**Related projects.** Other tools also sync rules across agents (for example ruler and rulesync). ContextPlate's focus is the whole context system: scaffolding, adopting existing repos, skills, subagents, MCP, enforcement, and a measured context budget.
|
|
192
|
+
|
|
193
|
+
## Contributing
|
|
194
|
+
|
|
195
|
+
Issues and pull requests are welcome. Start with [CONTRIBUTING.md](CONTRIBUTING.md). Security reports: [SECURITY.md](SECURITY.md).
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
[MIT](LICENSE).
|
|
200
|
+
|
|
201
|
+
The files ContextPlate generates or copies into your project (AGENTS.md blocks, rules, skills, roles, hooks, configs, and starter docs) are yours. Use them under any license you like, with no attribution or license notice required.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
contextplate/__init__.py,sha256=1Z1VD4mlhveFwTreWVpqw36PSWTiaSIVFNt5zzyhQjc,213
|
|
2
|
+
contextplate/__main__.py,sha256=E6Gls0DNz8GQK2K-kOUIx8cYhgANW_CH54VKrfCfs14,52
|
|
3
|
+
contextplate/adopt.py,sha256=tT3R0Y3VfrajJtrlEmSAVePE2oyCCqQjjU5oWlu6ZAo,21976
|
|
4
|
+
contextplate/cli.py,sha256=_OekUnqdxu6i3wiwSP_-RWoIIlysU65rIzH5begNvUU,11262
|
|
5
|
+
contextplate/config.py,sha256=hxQZ65lRG1m0p9GjOsaleK8iejP6bdRWY4CWY66TMyY,10294
|
|
6
|
+
contextplate/doctor.py,sha256=EH3bgMzxIE7DAjJ1IJ8TGh7G-DfqDZlPq1jYpulAy-0,5204
|
|
7
|
+
contextplate/frontmatter.py,sha256=tgKPrXuBst3I47OnpW8f3I00wvrE4SnBl2UITjVnXhc,6845
|
|
8
|
+
contextplate/globs.py,sha256=s4W8Cu6nroMNErMwE6dwAz3XzzCXWeD7TIkyK5QplgQ,2601
|
|
9
|
+
contextplate/model.py,sha256=o3sVY_HEkN1aSd-FM0YKh2qF1K3js-DXoK3XDgVSEbY,5434
|
|
10
|
+
contextplate/presets.py,sha256=512dSVGm3dIOGeywas7myHxFK6erOpAPwWWm0CxBwsY,5703
|
|
11
|
+
contextplate/scaffold.py,sha256=e4v4zBy0b30P-6sL5SMq2WxCJenMA9TgUuz71SI72Zg,6200
|
|
12
|
+
contextplate/writer.py,sha256=JcYn-L5glQx_kxMGM99XGdacjZravKkl2j-ECuRfF6o,14360
|
|
13
|
+
contextplate/adapters/__init__.py,sha256=7VqC6_jzE2xoP64s5UWA0l8or6qboJ73yI3aS7mPdiQ,1493
|
|
14
|
+
contextplate/adapters/aider.py,sha256=fIc70HLP4ISfK9tbYCISp6T9VCZiDsrsBEERj54yRzM,750
|
|
15
|
+
contextplate/adapters/base.py,sha256=AoVdhTlWBsezS10qNlvVdhU2xSPk1HTahI5ndjgXS8U,2726
|
|
16
|
+
contextplate/adapters/claude.py,sha256=uzTyuNF_c9YJ32j3iLvaJrumNOfI10wlj_ltj0pEJAc,6023
|
|
17
|
+
contextplate/adapters/codex.py,sha256=5dEnUccU4dK2SPNF09mKDTwnzT-leylmtQQDDFvJSvQ,3884
|
|
18
|
+
contextplate/adapters/copilot.py,sha256=_ciN5uObGOo31NwlFRqmIUV1RqAI6GSpn1rA8hfZ9ps,1785
|
|
19
|
+
contextplate/adapters/core.py,sha256=6t_UystNZRSTtcGX_PBNSeqHaY8wXPMDpbsSNMeREfY,6760
|
|
20
|
+
contextplate/adapters/cursor.py,sha256=IZ1o5kQamfpg_qP6zBXmQcmWQhQRt1zsmF5lCgf97Tg,1686
|
|
21
|
+
contextplate/adapters/gemini.py,sha256=2P1805SqqU96oXCX4qdgokeQ6r2gnFKgXVaz-jlQdjk,1071
|
|
22
|
+
contextplate/templates/check.sh,sha256=3Hrm4AR19r6vyHM1qjUtG2PtAHovF8ioKBlADzHfWoQ,1058
|
|
23
|
+
contextplate/templates/hooks/format.py,sha256=ztEuw4qyUfaYjg1DK5Z1qHo9z8cgKOEeK50lF54lLeY,2449
|
|
24
|
+
contextplate/templates/hooks/guard.py,sha256=LfHg8RyNRb66VCRNUjLnnHADmhQLfKJVCVphudt8sOo,3604
|
|
25
|
+
contextplate/templates/hooks/session_context.py,sha256=E6HMZLJunP0dDw4hTE3Eej08Z6Z52hd9L9v3BGUnuXs,2639
|
|
26
|
+
contextplate/templates/scaffold/AGENTS.md,sha256=KblnRgDzaAvudcgMqnwp69D7M3SxmM4Hjuctkj8NTsI,828
|
|
27
|
+
contextplate/templates/scaffold/agents/agents/researcher.md,sha256=DAlUuIeI_w6xhJBj4edfLsdPY8zbt-OZ8AWIhcl-IsE,617
|
|
28
|
+
contextplate/templates/scaffold/agents/agents/reviewer.md,sha256=E2xSbwCoFMwmbhOVmZYA28CXadUTE9JnX7-UqjMecww,982
|
|
29
|
+
contextplate/templates/scaffold/agents/agents/test-runner.md,sha256=HUdpisK51e5GJCsleFUWtex3ekJgmlHVyVYjUpRXpas,716
|
|
30
|
+
contextplate/templates/scaffold/agents/rules/ai-assets.md,sha256=q-lyRPVOz6sNDWkVY60rElZNaSM42M6hmwqXIfO53hM,553
|
|
31
|
+
contextplate/templates/scaffold/agents/rules/docs.md,sha256=OZqilqKHYxMVYvr1VBl-olRG4aNEBzXbMOfxWmk69PE,449
|
|
32
|
+
contextplate/templates/scaffold/agents/skills/adr/SKILL.md,sha256=BMNrMnjiugZwhU500Cw9Fyx6E_hchH1MEtiaO0OE_CE,501
|
|
33
|
+
contextplate/templates/scaffold/agents/skills/adr/template.md,sha256=GmMa6jcxumguAwYimqKHxVo3mfis0hnQEnNxzC8qmWs,354
|
|
34
|
+
contextplate/templates/scaffold/agents/skills/handoff/SKILL.md,sha256=IhNq6ArD0iIWtPRd_zWN4K8yp7YAB-YmvIuL8y8AuKQ,921
|
|
35
|
+
contextplate/templates/scaffold/agents/skills/plan-task/SKILL.md,sha256=lSzBRD1wwcu8FMbp5WvshXoxxRgTEegro5jfGSrWMDs,705
|
|
36
|
+
contextplate/templates/scaffold/agents/skills/plan-task/template.md,sha256=KTQpfoN-5QrfZxq3jAcYU_tuuTsYbGAYzekO21xfH9A,478
|
|
37
|
+
contextplate/templates/scaffold/ai_app/data/README.md,sha256=A0iYUi1JQxXf5SHaRI_Zu3lWGO6s8_eyE3xCirPR--4,323
|
|
38
|
+
contextplate/templates/scaffold/ai_app/evals/README.md,sha256=zweI4_D51kad-vQJY8OMqlAgS9cFR_BFh3Y5Y-va-Po,327
|
|
39
|
+
contextplate/templates/scaffold/ai_app/prompts/README.md,sha256=L1NcQ9yeTXGnwEaD3TYK6lR_Tuw_khOZG5LBQTKuCNs,312
|
|
40
|
+
contextplate/templates/scaffold/docs/agent-context.md,sha256=BHpOpUDAUWYu9NF9y9YtRTt5aKZ0bADcWt8yvJRenCM,3345
|
|
41
|
+
contextplate/templates/scaffold/docs/architecture.md,sha256=zy0RbGJoSmuvRt7ut1ziHGMiq5cBcViOuJ-xRBBysyc,831
|
|
42
|
+
contextplate/templates/scaffold/docs/glossary.md,sha256=_wQ8ugolmWKIVliwwKDgkZeR3dEnuVSZrl40mK-XixA,403
|
|
43
|
+
contextplate/templates/scaffold/docs/decisions/0001-record-architecture-decisions.md,sha256=zesgvS8nmnXTTryOSxPruwhhH90ienKr-gKzijYhB1g,659
|
|
44
|
+
contextplate/templates/scaffold/work/handoff.md,sha256=p1ICotZ4IXRolc7hgw6Wre5qetykwc9925FFVK4q1-s,547
|
|
45
|
+
contextplate-0.1.0.dist-info/METADATA,sha256=i1HJovVZT8kjnV5Ly6cVkB7-s2BK5-IH74KFTHyrH0s,11907
|
|
46
|
+
contextplate-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
47
|
+
contextplate-0.1.0.dist-info/entry_points.txt,sha256=4eSKc4-LanBPKcVCa9WeGQ-hfLC_e_2y9uG7fGyyuLo,88
|
|
48
|
+
contextplate-0.1.0.dist-info/licenses/LICENSE,sha256=JSl2gjcv_LbUQD7LpK_lv1jbA2NpANDee9pXtK3ph0I,1082
|
|
49
|
+
contextplate-0.1.0.dist-info/RECORD,,
|