-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-context.py
More file actions
650 lines (550 loc) · 23.3 KB
/
project-context.py
File metadata and controls
650 lines (550 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#!/usr/bin/env python3
"""
project-context.py — Generate a durable project constitution artifact
Combines repo structure, detected profile conventions, hooks metadata, and
testing expectations into a single grounded project-context.md. No AI
generation, no network access, no speculative runtime changes required.
Usage:
python3 project-context.py # Write to session files/ dir
python3 project-context.py --stdout # Print to stdout only
python3 project-context.py --output PATH # Write to explicit path
python3 project-context.py --repo PATH # Use a different repo root
python3 project-context.py --profile PROFILE # Force a preset profile
python3 project-context.py --no-write # Dry-run: show target path
python3 project-context.py --list-profiles # Show available profiles
"""
import argparse
import json
import os
import subprocess
import sys
from collections import defaultdict
from pathlib import Path
# Fix Windows console encoding
if os.name == "nt":
for _s in (sys.stdout, sys.stderr):
if hasattr(_s, "reconfigure"):
_s.reconfigure(encoding="utf-8", errors="replace")
TOOLS_DIR = Path(__file__).resolve().parent
PRESETS_DIR = TOOLS_DIR / "presets"
COPILOT_HOME = Path(os.environ.get("COPILOT_HOME", str(Path.home() / ".copilot"))).expanduser()
SESSION_STATE = COPILOT_HOME / "session-state"
USER_TEMPLATES_DIR = COPILOT_HOME / "templates"
PROJECT_PRESETS_SUBDIR = Path(".copilot") / "presets"
PROJECT_OVERRIDES_SUBDIR = Path(".copilot") / "overrides"
CORE_TEMPLATE_TOKEN = "{CORE_TEMPLATE}"
_REQUIRED_PRESET_FIELDS = {"name", "description", "hooks", "workflow_phases"}
# Profile detection: ordered list of (profile_name, indicator_files/dirs)
# NOTE: build.gradle / build.gradle.kts / Podfile are intentionally absent from
# mobile indicators — they are common in JVM server-side and Swift server repos
# and produce widespread false positives. Only top-level directories that are
# unambiguously mobile-native (android/, ios/) and *.xcodeproj entries qualify.
_PROFILE_INDICATORS: list[tuple[str, list[str]]] = [
("mobile", ["android", "ios", "*.xcodeproj"]),
("fullstack", ["frontend", "backend", "client", "server", "api"]),
("typescript", ["package.json", "tsconfig.json", "yarn.lock", "pnpm-lock.yaml"]),
("python", ["requirements.txt", "setup.py", "pyproject.toml", "Pipfile", "poetry.lock"]),
]
# ─── Git helpers ─────────────────────────────────────────────────────────────
def find_git_root(start: Path | None = None) -> Path | None:
current = (start or Path.cwd()).resolve()
for candidate in [current, *current.parents]:
if (candidate / ".git").exists():
return candidate
return None
def ls_files(repo_root: Path, timeout: int = 10) -> list[str]:
try:
result = subprocess.run(
["git", "ls-files"],
capture_output=True,
text=True,
timeout=timeout,
cwd=str(repo_root),
)
if result.returncode != 0:
return []
return [line.strip() for line in result.stdout.splitlines() if line.strip()]
except Exception:
return []
def get_git_last_commit_date(repo_root: Path) -> str:
"""Return ISO 8601 date of the last commit; empty string on failure.
Using the commit date rather than wall-clock time keeps the artifact
deterministic: identical repo state → identical output.
"""
try:
r = subprocess.run(
["git", "log", "-1", "--format=%cI"],
capture_output=True,
text=True,
timeout=5,
cwd=str(repo_root),
)
if r.returncode == 0:
return r.stdout.strip()
except Exception:
pass
return ""
def get_git_remote(repo_root: Path) -> str:
"""Return the primary git remote URL, or empty string."""
try:
r = subprocess.run(
["git", "remote", "get-url", "origin"],
capture_output=True,
text=True,
timeout=5,
cwd=str(repo_root),
)
if r.returncode == 0:
return r.stdout.strip()
except Exception:
pass
return ""
def get_git_branch(repo_root: Path) -> str:
"""Return the current branch name."""
try:
r = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
capture_output=True,
text=True,
timeout=5,
cwd=str(repo_root),
)
if r.returncode == 0:
return r.stdout.strip()
except Exception:
pass
return ""
# ─── Profile detection ────────────────────────────────────────────────────────
def _glob_match_any(name: str, patterns: list[str]) -> bool:
"""Simple glob: support leading '*.' suffix patterns and exact names."""
for pat in patterns:
if pat.startswith("*"):
if name.endswith(pat[1:]):
return True
elif name == pat:
return True
return False
def detect_profile(repo_root: Path, files: list[str]) -> str:
"""Heuristically determine the best-fit profile for the repo."""
# Collect top-level names (files and dirs)
top_level: set[str] = set()
for f in files:
parts = Path(f).parts
top_level.add(parts[0])
# Also look at actual filesystem entries not just tracked files
try:
top_level.update(p.name for p in repo_root.iterdir())
except Exception:
pass
# Check indicators in priority order
for profile_name, indicators in _PROFILE_INDICATORS:
for ind in indicators:
if _glob_match_any(ind, [ind]) and ind in top_level:
return profile_name
# glob pattern check
if ind.startswith("*."):
ext = ind[1:]
if any(n.endswith(ext) for n in top_level):
return profile_name
return "default"
def _load_json_dict(path: Path) -> dict | None:
try:
data = json.loads(path.read_text(encoding="utf-8"))
except Exception:
return None
return data if isinstance(data, dict) else None
def _is_full_preset(data: dict | None) -> bool:
return isinstance(data, dict) and _REQUIRED_PRESET_FIELDS.issubset(data)
def _clone_json(value):
return json.loads(json.dumps(value))
def _apply_template_strategy(current, spec):
if isinstance(spec, dict) and "strategy" in spec:
strategy = spec.get("strategy")
value = spec.get("value")
else:
strategy = "replace"
value = spec
if strategy == "replace":
return _clone_json(value)
if strategy == "prepend":
if isinstance(current, list) and isinstance(value, list):
return _clone_json(value) + _clone_json(current)
if isinstance(current, str) and isinstance(value, str):
return value + current
return current
if strategy == "append":
if isinstance(current, list) and isinstance(value, list):
return _clone_json(current) + _clone_json(value)
if isinstance(current, str) and isinstance(value, str):
return current + value
return current
if strategy == "wrap":
if isinstance(current, str) and isinstance(value, str):
return value.replace(CORE_TEMPLATE_TOKEN, current)
return current
return current
def _apply_preset_layer(base: dict, layer: dict) -> dict:
resolved = _clone_json(base)
if _is_full_preset(layer):
resolved = _clone_json(layer)
overrides = layer.get("overrides")
if isinstance(overrides, dict):
for field, spec in overrides.items():
resolved[field] = _apply_template_strategy(resolved.get(field), spec)
return resolved
def _project_layer_paths(profile_name: str, repo_root: Path | None) -> list[Path]:
layer_paths = [USER_TEMPLATES_DIR / f"{profile_name}.json"]
if repo_root is not None:
layer_paths.extend(
[
repo_root / PROJECT_PRESETS_SUBDIR / f"{profile_name}.json",
repo_root / PROJECT_OVERRIDES_SUBDIR / f"{profile_name}.json",
]
)
return layer_paths
def load_preset(profile_name: str, repo_root: Path | None = None) -> dict:
"""Load a preset with layered project/user overrides."""
path = PRESETS_DIR / f"{profile_name}.json"
if not path.exists():
path = PRESETS_DIR / "default.json"
resolved = _load_json_dict(path)
if not resolved:
resolved = {
"name": profile_name,
"description": "",
"hooks": [],
"workflow_phases": ["CLARIFY", "BUILD", "TEST", "COMMIT"],
"workflow_notes": "",
}
for layer_path in _project_layer_paths(profile_name, repo_root):
layer = _load_json_dict(layer_path)
if isinstance(layer, dict):
resolved = _apply_preset_layer(resolved, layer)
return resolved if isinstance(resolved, dict) else {}
def list_profiles(repo_root: Path | None = None) -> list[str]:
"""Return all available profile names across core, user, and project layers."""
names: set[str] = set()
for directory in (PRESETS_DIR, USER_TEMPLATES_DIR):
if directory.exists():
names.update(p.stem for p in directory.glob("*.json"))
if repo_root is not None:
for subdir in (PROJECT_PRESETS_SUBDIR, PROJECT_OVERRIDES_SUBDIR):
directory = repo_root / subdir
if directory.exists():
names.update(p.stem for p in directory.glob("*.json"))
return sorted(names)
# ─── File structure analysis ──────────────────────────────────────────────────
def group_files(files: list[str]) -> dict[str, list[str]]:
groups: dict[str, list[str]] = defaultdict(list)
for f in sorted(files):
parts = Path(f).parts
top = parts[0] if len(parts) > 1 else "."
groups[top].append(f)
return dict(sorted(groups.items()))
def ext_summary(files: list[str], cap: int = 6) -> str:
counts: dict[str, int] = defaultdict(int)
for f in files:
ext = Path(f).suffix or "(no ext)"
counts[ext] += 1
parts = [f"{ext}×{n}" if n > 1 else ext for ext, n in sorted(counts.items())]
tail = f", +{len(parts) - cap} more" if len(parts) > cap else ""
return ", ".join(parts[:cap]) + tail
def find_test_files(files: list[str]) -> list[str]:
"""Return tracked test-related files (heuristic)."""
return [
f
for f in files
if (
"test" in Path(f).name.lower()
or "spec" in Path(f).name.lower()
or Path(f).parts[0] in ("tests", "test", "__tests__", "spec")
)
]
def find_config_files(files: list[str]) -> list[str]:
"""Return well-known config/entrypoint files."""
important = {
"package.json",
"pyproject.toml",
"setup.py",
"requirements.txt",
"Makefile",
"Dockerfile",
"docker-compose.yml",
"docker-compose.yaml",
".github",
"tsconfig.json",
"build.gradle",
"build.gradle.kts",
"Podfile",
"Cargo.toml",
"go.mod",
"pom.xml",
"AGENTS.md",
"WORKFLOW.md",
"README.md",
".copilot",
}
return [f for f in files if Path(f).name in important or Path(f).parts[0] in important]
# ─── Artifact generation ──────────────────────────────────────────────────────
def generate_context(
repo_root: Path,
files: list[str],
preset: dict,
detected_profile: str,
forced_profile: bool,
) -> str:
"""Return the full project-context.md as a string."""
commit_date = get_git_last_commit_date(repo_root)
repo_name = repo_root.name
remote = get_git_remote(repo_root)
branch = get_git_branch(repo_root)
profile_name = preset.get("name", detected_profile)
profile_desc = preset.get("description", "")
phases = preset.get("workflow_phases", [])
workflow_notes = preset.get("workflow_notes", "")
hooks = preset.get("hooks", [])
groups = group_files(files)
test_files = find_test_files(files)
config_files = find_config_files(files)
profile_source = "forced" if forced_profile else "auto-detected"
lines: list[str] = [
f"# Project Context — {repo_name}",
"",
"> Auto-generated by project-context.py — do not edit manually.",
"",
f"Last commit: {commit_date} " if commit_date else "",
f"Repository root: `{repo_root}` ",
f"Branch: `{branch}` " if branch else "",
f"Remote: {remote} " if remote else "",
f"Profile: **{profile_name}** ({profile_source}) ",
f"Total tracked files: {len(files)}",
"",
]
# Remove empty lines caused by optional fields
lines = [l for l in lines if l != ""]
# ── Identity ──────────────────────────────────────────────────────────────
lines += [
"",
"## Identity",
"",
"| Field | Value |",
"|-------|-------|",
f"| Repo name | `{repo_name}` |",
f"| Root | `{repo_root}` |",
f"| Branch | `{branch}` |" if branch else "",
f"| Remote | {remote} |" if remote else "",
f"| Profile | {profile_name} ({profile_source}) |",
f"| Files tracked | {len(files)} |",
"",
]
lines = [l for l in lines if l != ""]
# ── Profile description ───────────────────────────────────────────────────
if profile_desc:
lines += [
"",
"## Profile",
"",
profile_desc,
"",
]
# ── Workflow ──────────────────────────────────────────────────────────────
lines += [
"",
"## Workflow Phases",
"",
]
if phases:
phase_chain = " → ".join(phases)
lines.append(f"**{phase_chain}**")
lines.append("")
if workflow_notes:
lines.append(workflow_notes)
lines.append("")
lines += [
"| # | Phase | Role |",
"|---|-------|------|",
]
phase_roles = {
"CLARIFY": "Make requirements implementation-ready",
"DESIGN": "Generate visual/technical design",
"VERIFY": "Review design before coding",
"BUILD": "Implement code",
"TEST": "Functional verification — all tests must pass",
"REVIEW": "Code quality check and approval",
"QA": "Visual/manual verification with evidence",
"COMMIT": "Ship — clean git commit with trailer",
}
for i, phase in enumerate(phases):
role = phase_roles.get(phase, "")
lines.append(f"| {i} | **{phase}** | {role} |")
lines.append("")
else:
lines.append("No phases defined in preset.")
lines.append("")
# ── Quality gates / hooks ─────────────────────────────────────────────────
lines += [
"",
"## Quality Gates & Hooks",
"",
]
if hooks:
lines.append("Active hooks enforced by this profile:")
lines.append("")
hook_docs = {
"dangerous-blocker.py": "Blocks destructive command-line operations",
"secret-detector.py": "Blocks commits containing API keys or credentials",
"test-reminder.py": "Reminds to run tests before committing",
"build-reminder.py": "Reminds to verify the build before committing",
"enforce-tdd-pipeline.py": "Enforces red→green→refactor TDD cycle",
"commit-gate.py": "Blocks commits unless all quality gates pass",
"enforce-coding-standards.py": "Enforces language-specific coding conventions",
"architecture-guard.py": "Prevents cross-layer dependency violations",
"session-banner.py": "Displays project context at session start",
}
for hook in hooks:
doc = hook_docs.get(hook, "")
entry = f"- `{hook}`" + (f" — {doc}" if doc else "")
lines.append(entry)
lines.append("")
else:
lines.append("No hooks defined in this profile.")
lines.append("")
# ── Testing conventions ───────────────────────────────────────────────────
lines += [
"",
"## Testing Conventions",
"",
]
if test_files:
lines.append(f"**{len(test_files)} test file(s) found:**")
lines.append("")
for tf in sorted(test_files)[:20]: # cap at 20 for readability
lines.append(f"- `{tf}`")
if len(test_files) > 20:
lines.append(f"- … and {len(test_files) - 20} more")
lines.append("")
lines.append("> Run tests before every commit. Do NOT mark tasks complete until tests pass.")
else:
lines.append("No test files detected. Add tests before shipping.")
lines.append("")
# ── Key config files ──────────────────────────────────────────────────────
if config_files:
lines += [
"",
"## Key Configuration Files",
"",
]
for cf in sorted(set(config_files))[:30]:
lines.append(f"- `{cf}`")
lines.append("")
# ── File structure summary ────────────────────────────────────────────────
lines += [
"",
"## File Structure",
"",
f"Total: **{len(files)} tracked files** across **{len(groups)} top-level director{'y' if len(groups) == 1 else 'ies'}**",
"",
"| Directory | Files | Extensions |",
"|-----------|-------|------------|",
]
for group, gfiles in groups.items():
label = f"`{group}/`" if group != "." else "`./` (root)"
lines.append(f"| {label} | {len(gfiles)} | {ext_summary(gfiles)} |")
lines.append("")
# ── Footer ────────────────────────────────────────────────────────────────
lines += [
"",
"---",
"",
"*Auto-generated by `project-context.py` — do not edit manually.* ",
f"*Regenerate with: `python3 {TOOLS_DIR}/project-context.py`*",
]
return "\n".join(lines) + "\n"
# ─── Session output ───────────────────────────────────────────────────────────
def get_session_files_dir() -> Path | None:
if not SESSION_STATE.exists():
return None
sessions = sorted(
(d for d in SESSION_STATE.iterdir() if d.is_dir()),
key=lambda d: d.stat().st_mtime,
reverse=True,
)
if not sessions:
return None
files_dir = sessions[0] / "files"
files_dir.mkdir(parents=True, exist_ok=True)
return files_dir
def resolve_output_path(args_output: str | None) -> Path | None:
if args_output:
return Path(args_output).resolve()
files_dir = get_session_files_dir()
if files_dir:
return files_dir / "project-context.md"
return None
# ─── CLI ──────────────────────────────────────────────────────────────────────
def main() -> int:
parser = argparse.ArgumentParser(
description="Generate project-context.md — a durable project constitution artifact."
)
parser.add_argument("--stdout", action="store_true", help="Print context to stdout instead of writing a file.")
parser.add_argument("--output", metavar="PATH", help="Write to an explicit file path.")
parser.add_argument("--repo", metavar="PATH", help="Repository root (defaults to git root of cwd).")
parser.add_argument("--profile", metavar="NAME", help="Force a specific preset profile (overrides auto-detection).")
parser.add_argument("--no-write", action="store_true", help="Dry-run: show the target path without writing.")
parser.add_argument("--list-profiles", action="store_true", help="List available profiles and exit.")
args = parser.parse_args()
if args.list_profiles:
repo_root = Path(args.repo).resolve() if args.repo else find_git_root()
profiles = list_profiles(repo_root)
if not profiles:
print("No profiles found in presets/", file=sys.stderr)
return 1
print("Available profiles:")
for p in profiles:
preset = load_preset(p, repo_root)
desc = preset.get("description", "")
print(f" {p:<14} — {desc}")
return 0
repo_root = Path(args.repo).resolve() if args.repo else find_git_root()
if repo_root is None:
print("Error: not in a git repository. Use --repo to specify root.", file=sys.stderr)
return 1
files = ls_files(repo_root)
if not files:
print(f"Warning: no tracked files found in {repo_root}.", file=sys.stderr)
# Profile resolution
forced_profile = args.profile is not None
if forced_profile:
profile_name = args.profile
available = list_profiles(repo_root)
if profile_name not in available:
print(
f"Error: unknown profile '{profile_name}'. Available: {', '.join(available)}",
file=sys.stderr,
)
return 1
else:
profile_name = detect_profile(repo_root, files)
preset = load_preset(profile_name, repo_root)
content = generate_context(repo_root, files, preset, profile_name, forced_profile)
if args.stdout:
sys.stdout.write(content)
return 0
out_path = resolve_output_path(args.output)
if out_path is None:
print(
"Error: no active Copilot session found and no --output given. Use --output PATH or --stdout.",
file=sys.stderr,
)
return 1
if args.no_write:
print(f"Would write to: {out_path}")
return 0
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(content, encoding="utf-8")
print(f"✅ project-context.md → {out_path}")
print(f" Profile: {profile_name} ({'forced' if forced_profile else 'auto-detected'})")
print(f" {len(files)} tracked files")
return 0
if __name__ == "__main__":
sys.exit(main())