@mriekki/lean 0.4.4 → 0.4.6

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.
Files changed (22) hide show
  1. package/assets/components.json +3 -3
  2. package/assets/core/distribution.json +5 -0
  3. package/assets/deployment/distribution.json +2 -0
  4. package/assets/generated/ASSEMBLY.json +48 -13
  5. package/assets/generated/core/core/instructions/lean-docs.instructions.md +33 -2
  6. package/assets/generated/core/core/instructions/lean-workflow.instructions.md +42 -0
  7. package/assets/generated/core/core/process/DEPLOYMENT_ENABLEMENT.md +108 -71
  8. package/assets/generated/core/core/process/LEAN_ENGINEERING_GUIDE.md +317 -0
  9. package/assets/generated/core/core/process/PROJECT_DOCUMENTATION_FRAMEWORK.md +326 -0
  10. package/assets/generated/core/core/skills/deploy/SKILL.md +164 -121
  11. package/assets/generated/core/core/skills/deployment-readiness/SKILL.md +223 -250
  12. package/assets/generated/core/core/skills/docs/SKILL.md +116 -0
  13. package/assets/generated/core/core/skills/next-step/SKILL.md +15 -10
  14. package/assets/generated/core/core/skills/tear-down/SKILL.md +101 -61
  15. package/assets/generated/core/core/templates/ADR_TEMPLATE.md +41 -0
  16. package/assets/generated/core/tools/docs_site/docs_site.py +512 -0
  17. package/assets/generated/deployment/src/lean_poc_deployment/entrypoint.py +5 -1
  18. package/assets/generated/deployment/src/lean_poc_deployment/local_cli.py +112 -0
  19. package/assets/generated/deployment/src/lean_poc_deployment/local_runtime.py +523 -0
  20. package/assets/generated/deployment/src/lean_poc_deployment/teardown.py +49 -35
  21. package/assets/generated/deployment/src/lean_poc_deployment/teardown_cli.py +31 -12
  22. package/package.json +1 -1
@@ -0,0 +1,512 @@
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import argparse
5
+ import html
6
+ import json
7
+ import os
8
+ import re
9
+ import shutil
10
+ import subprocess
11
+ import sys
12
+ import webbrowser
13
+ from datetime import datetime, timezone
14
+ from functools import partial
15
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
16
+ from pathlib import Path, PurePosixPath, PureWindowsPath
17
+ from typing import Any
18
+
19
+ IGNORE_DIRS = {".git", ".lean", "node_modules", "bin", "obj", "dist", "build", "target", ".venv", "venv", "__pycache__"}
20
+ DEFAULT_CACHE = Path(".lean/docs-cache")
21
+ DEFAULT_SITE = Path(".lean/docs-site")
22
+ MANIFEST = Path("docs/lean-docs.json")
23
+ SITE_MARKER = ".lean-docs-site"
24
+ SITE_MARKER_CONTENT = "lean-docs-site\n"
25
+
26
+
27
+ class DocsSiteError(RuntimeError):
28
+ pass
29
+
30
+
31
+ def _read_json(path: Path) -> dict[str, Any] | None:
32
+ try:
33
+ value = json.loads(path.read_text(encoding="utf-8"))
34
+ except FileNotFoundError:
35
+ return None
36
+ except json.JSONDecodeError as exc:
37
+ raise DocsSiteError(f"Invalid JSON in {path}: {exc}") from exc
38
+ if not isinstance(value, dict):
39
+ raise DocsSiteError(f"Expected an object in {path}")
40
+ return value
41
+
42
+
43
+ def _git(root: Path, *args: str) -> str | None:
44
+ try:
45
+ result = subprocess.run(
46
+ ["git", "-C", str(root), *args],
47
+ text=True,
48
+ encoding="utf-8",
49
+ errors="replace",
50
+ capture_output=True,
51
+ timeout=5,
52
+ check=False,
53
+ )
54
+ except (OSError, subprocess.SubprocessError):
55
+ return None
56
+ value = (result.stdout or "").strip()
57
+ return value if result.returncode == 0 and value else None
58
+
59
+
60
+ def _safe_rel(root: Path, path: Path) -> str:
61
+ try:
62
+ return path.resolve().relative_to(root.resolve()).as_posix()
63
+ except ValueError as exc:
64
+ raise DocsSiteError(f"Documentation path escapes repository root: {path}") from exc
65
+
66
+
67
+ def _markdown_title(content: str, fallback: str) -> str:
68
+ for line in content.splitlines():
69
+ match = re.match(r"^#\s+(.+?)\s*$", line)
70
+ if match:
71
+ return re.sub(r"[`*_]", "", match.group(1)).strip()
72
+ return fallback
73
+
74
+
75
+ def _manifest_documents(root: Path, manifest: dict[str, Any] | None) -> tuple[list[dict[str, Any]], list[str]]:
76
+ docs: list[dict[str, Any]] = []
77
+ warnings: list[str] = []
78
+ seen: set[str] = set()
79
+
80
+ if manifest is not None:
81
+ if manifest.get("schemaVersion") != 1:
82
+ warnings.append("docs/lean-docs.json schemaVersion is not 1")
83
+ raw = manifest.get("documents")
84
+ if raw is not None and not isinstance(raw, list):
85
+ warnings.append("docs/lean-docs.json documents must be an array")
86
+ raw = []
87
+ for index, item in enumerate(raw or []):
88
+ if not isinstance(item, dict) or not isinstance(item.get("path"), str):
89
+ warnings.append(f"docs/lean-docs.json documents[{index}] is invalid")
90
+ continue
91
+ raw_path = item["path"]
92
+ windows_path = PureWindowsPath(raw_path)
93
+ relative_path = PurePosixPath(raw_path.replace("\\", "/"))
94
+ if (
95
+ relative_path.is_absolute()
96
+ or windows_path.root
97
+ or windows_path.drive
98
+ or ".." in relative_path.parts
99
+ ):
100
+ warnings.append(f"Skipped unsafe documentation path: {item['path']}")
101
+ continue
102
+ rel = relative_path.as_posix()
103
+ path = root / rel
104
+ try:
105
+ resolved_path = root / _safe_rel(root, path)
106
+ except DocsSiteError:
107
+ warnings.append(f"Skipped unsafe documentation path: {item['path']}")
108
+ continue
109
+ seen.add(rel)
110
+ if not resolved_path.is_file():
111
+ if item.get("optional") is not True:
112
+ warnings.append(f"Listed documentation is not present: {rel}")
113
+ continue
114
+ content = resolved_path.read_text(encoding="utf-8", errors="replace")
115
+ docs.append(
116
+ {
117
+ "path": rel,
118
+ "title": str(item.get("title") or _markdown_title(content, path.stem)),
119
+ "section": str(item.get("section") or "Other"),
120
+ "type": str(item.get("type") or "markdown"),
121
+ "ownership": str(item.get("ownership") or "project"),
122
+ "authority": str(item.get("authority") or "reference"),
123
+ "content": content,
124
+ }
125
+ )
126
+
127
+ candidates: list[Path] = []
128
+ for name in ("PROJECT_BRIEF.md", "README.md"):
129
+ path = root / name
130
+ if path.is_file():
131
+ candidates.append(path)
132
+ docs_root = root / "docs"
133
+ if docs_root.is_dir():
134
+ if _path_contains_link(docs_root):
135
+ warnings.append(f"Skipped unsafe documentation directory: {docs_root}")
136
+ else:
137
+ for base, dirs, names in os.walk(docs_root):
138
+ safe_dirs = []
139
+ for directory in dirs:
140
+ candidate = Path(base) / directory
141
+ if directory in IGNORE_DIRS:
142
+ continue
143
+ if _is_link(candidate):
144
+ warnings.append(f"Skipped unsafe documentation directory: {candidate}")
145
+ continue
146
+ safe_dirs.append(directory)
147
+ dirs[:] = safe_dirs
148
+ for name in names:
149
+ if name.lower().endswith(".md"):
150
+ candidates.append(Path(base) / name)
151
+
152
+ for path in sorted(candidates):
153
+ try:
154
+ rel = _safe_rel(root, path)
155
+ except DocsSiteError:
156
+ warnings.append(f"Skipped unsafe documentation path: {path}")
157
+ continue
158
+ if rel in seen:
159
+ continue
160
+ content = path.read_text(encoding="utf-8", errors="replace")
161
+ section = "Overview" if rel in {"README.md", "PROJECT_BRIEF.md", "docs/index.md"} else rel.split("/")[1].replace("-", " ").title() if rel.startswith("docs/") and "/" in rel else "Other"
162
+ docs.append(
163
+ {
164
+ "path": rel,
165
+ "title": _markdown_title(content, path.stem),
166
+ "section": section,
167
+ "type": "markdown",
168
+ "ownership": "unknown",
169
+ "authority": "reference",
170
+ "content": content,
171
+ }
172
+ )
173
+
174
+ docs.sort(key=lambda item: (item["section"].lower(), item["path"].lower()))
175
+ return docs, warnings
176
+
177
+
178
+ def _lab_status(root: Path, kind: str, current_commit: str | None) -> dict[str, Any]:
179
+ cache = root / DEFAULT_CACHE / kind
180
+ data_path = cache / "workbench.json"
181
+ index_path = cache / "index.html"
182
+ command = (
183
+ "python tools/lean-system-lab/system-workbench.py export --repo . --out .lean/docs-cache/system"
184
+ if kind == "system"
185
+ else "/db-lab"
186
+ )
187
+ result: dict[str, Any] = {
188
+ "kind": kind,
189
+ "status": "NOT GENERATED",
190
+ "detail": "No disposable local projection is available.",
191
+ "command": command,
192
+ "hasIndex": index_path.is_file(),
193
+ }
194
+ if not data_path.is_file():
195
+ return result
196
+ try:
197
+ data = json.loads(data_path.read_text(encoding="utf-8"))
198
+ except (OSError, json.JSONDecodeError):
199
+ result["status"] = "AVAILABLE / FRESHNESS UNKNOWN"
200
+ result["detail"] = "A local projection exists, but its evidence metadata could not be read."
201
+ return result
202
+
203
+ generated = data.get("generatedAt") if isinstance(data, dict) else None
204
+ if generated:
205
+ result["generatedAt"] = str(generated)
206
+
207
+ recorded_commit: str | None = None
208
+ if isinstance(data, dict):
209
+ repository = data.get("repository")
210
+ if isinstance(repository, dict) and repository.get("commit"):
211
+ recorded_commit = str(repository["commit"])
212
+ elif data.get("repositoryCommit"):
213
+ recorded_commit = str(data["repositoryCommit"])
214
+
215
+ if recorded_commit and current_commit:
216
+ result["recordedCommit"] = recorded_commit
217
+ if recorded_commit == current_commit:
218
+ result["status"] = "CURRENT"
219
+ result["detail"] = f"Projection evidence is bound to current commit {current_commit[:10]}."
220
+ else:
221
+ result["status"] = "STALE"
222
+ result["detail"] = f"Projection records {recorded_commit[:10]}; checkout is {current_commit[:10]}."
223
+ else:
224
+ result["status"] = "AVAILABLE / FRESHNESS UNKNOWN"
225
+ result["detail"] = "A projection exists, but there is not enough evidence to prove it matches the current checkout."
226
+ return result
227
+
228
+
229
+ def _prepare_system_lab(root: Path) -> str | None:
230
+ tool = root / "tools/lean-system-lab/system-workbench.py"
231
+ if not tool.is_file():
232
+ return None
233
+ out = root / DEFAULT_CACHE / "system"
234
+ out.parent.mkdir(parents=True, exist_ok=True)
235
+ try:
236
+ result = subprocess.run(
237
+ [sys.executable, str(tool), "export", "--repo", str(root), "--out", str(out)],
238
+ cwd=str(root),
239
+ text=True,
240
+ encoding="utf-8",
241
+ errors="replace",
242
+ capture_output=True,
243
+ timeout=45,
244
+ check=False,
245
+ )
246
+ except (OSError, subprocess.SubprocessError) as exc:
247
+ return f"System Lab preparation unavailable: {exc}"
248
+ if result.returncode != 0:
249
+ detail = (result.stderr or result.stdout or "").strip().splitlines()
250
+ return "System Lab preparation failed: " + (detail[-1] if detail else f"exit {result.returncode}")
251
+ return None
252
+
253
+
254
+ def _validate_lab_cache(root: Path, kind: str) -> Path:
255
+ source = root / DEFAULT_CACHE / kind
256
+ if _path_contains_link(source):
257
+ raise DocsSiteError(f"Lab cache contains a symlink or junction: {source}")
258
+ if source.exists():
259
+ try:
260
+ source.resolve().relative_to(root)
261
+ except ValueError as exc:
262
+ raise DocsSiteError(f"Lab cache escapes repository root: {source}") from exc
263
+ if source.is_dir() and _directory_contains_link(source):
264
+ raise DocsSiteError(f"Lab cache contains a symlink or junction: {source}")
265
+ return source
266
+
267
+
268
+ def _copy_lab_cache(root: Path, out: Path, kind: str) -> None:
269
+ source = _validate_lab_cache(root, kind)
270
+ target = out / "labs" / kind
271
+ try:
272
+ target.resolve().relative_to(out)
273
+ except ValueError as exc:
274
+ raise DocsSiteError(f"Lab output escapes generated site directory: {target}") from exc
275
+ if _path_contains_link(target):
276
+ raise DocsSiteError(f"Lab output contains a symlink or junction: {target}")
277
+ if target.exists():
278
+ if target.is_symlink() or not target.is_dir():
279
+ raise DocsSiteError(f"Unexpected lab output path: {target}")
280
+ shutil.rmtree(target)
281
+ if source.is_dir():
282
+ shutil.copytree(source, target)
283
+
284
+
285
+ def _is_link(path: Path) -> bool:
286
+ if path.is_symlink():
287
+ return True
288
+ is_junction = getattr(path, "is_junction", None)
289
+ return bool(is_junction and is_junction())
290
+
291
+
292
+ def _path_contains_link(path: Path) -> bool:
293
+ absolute = Path(os.path.abspath(path))
294
+ current = Path(absolute.anchor)
295
+ for part in absolute.parts[1:]:
296
+ current /= part
297
+ if _is_link(current):
298
+ return True
299
+ return False
300
+
301
+
302
+ def _paths_overlap(left: Path, right: Path) -> bool:
303
+ left = Path(os.path.abspath(left))
304
+ right = Path(os.path.abspath(right))
305
+ return left == right or left in right.parents or right in left.parents
306
+
307
+
308
+ def _directory_contains_link(root: Path) -> bool:
309
+ directories = [root]
310
+ while directories:
311
+ current = directories.pop()
312
+ with os.scandir(current) as entries:
313
+ for entry in entries:
314
+ path = Path(entry.path)
315
+ if _is_link(path):
316
+ return True
317
+ if entry.is_dir(follow_symlinks=False):
318
+ directories.append(path)
319
+ return False
320
+
321
+
322
+ def _prepare_output(root: Path, out: Path) -> None:
323
+ if _path_contains_link(out):
324
+ raise DocsSiteError(f"Refusing to use an output path containing a symlink or junction: {out}")
325
+ if out == root or out in root.parents:
326
+ raise DocsSiteError(f"Refusing to use the repository root or one of its parents as output: {out}")
327
+ for kind in ("system", "database"):
328
+ cache = root / DEFAULT_CACHE / kind
329
+ if _paths_overlap(out, cache):
330
+ raise DocsSiteError(f"Output path overlaps a lab cache: {cache}")
331
+
332
+ if out.exists():
333
+ if not out.is_dir():
334
+ raise DocsSiteError(f"Output path is not a directory: {out}")
335
+ marker = out / SITE_MARKER
336
+ if any(out.iterdir()):
337
+ if marker.is_symlink() or not marker.is_file():
338
+ raise DocsSiteError(f"Refusing to overwrite an unowned output directory: {out}")
339
+ try:
340
+ marker_content = marker.read_text(encoding="utf-8")
341
+ except OSError as exc:
342
+ raise DocsSiteError(f"Unable to verify output ownership: {marker}") from exc
343
+ if marker_content != SITE_MARKER_CONTENT:
344
+ raise DocsSiteError(f"Refusing to overwrite an unowned output directory: {out}")
345
+
346
+ if _directory_contains_link(out):
347
+ raise DocsSiteError(f"Refusing to write through a symlink in output directory: {out}")
348
+
349
+ for name in (SITE_MARKER, "index.html", "site.json"):
350
+ path = out / name
351
+ if path.is_file():
352
+ try:
353
+ link_count = path.stat().st_nlink
354
+ except OSError as exc:
355
+ raise DocsSiteError(f"Unable to verify output file links: {path}") from exc
356
+ if link_count > 1:
357
+ raise DocsSiteError(f"Refusing to overwrite hard-linked output file: {path}")
358
+
359
+ out.mkdir(parents=True, exist_ok=True)
360
+ (out / SITE_MARKER).write_text(SITE_MARKER_CONTENT, encoding="utf-8")
361
+
362
+
363
+ def _site_payload(root: Path) -> dict[str, Any]:
364
+ manifest = _read_json(root / MANIFEST)
365
+ docs, warnings = _manifest_documents(root, manifest)
366
+ current_commit = _git(root, "rev-parse", "HEAD")
367
+ branch = _git(root, "branch", "--show-current")
368
+ site = manifest.get("site") if isinstance(manifest, dict) and isinstance(manifest.get("site"), dict) else {}
369
+ title = str(site.get("title") or root.name)
370
+ entry = str(site.get("entry") or ("PROJECT_BRIEF.md" if (root / "PROJECT_BRIEF.md").is_file() else "README.md"))
371
+ return {
372
+ "title": title,
373
+ "entry": entry,
374
+ "generatedAt": datetime.now(timezone.utc).isoformat(),
375
+ "repository": {"name": root.name, "commit": current_commit, "branch": branch},
376
+ "documents": docs,
377
+ "warnings": warnings,
378
+ "labs": {
379
+ "system": _lab_status(root, "system", current_commit),
380
+ "database": _lab_status(root, "database", current_commit),
381
+ },
382
+ }
383
+
384
+
385
+ def _html_document(payload: dict[str, Any]) -> str:
386
+ data = json.dumps(payload, ensure_ascii=False).replace("</", "<\\/")
387
+ title = html.escape(str(payload["title"]))
388
+ return f'''<!doctype html>
389
+ <html lang="en">
390
+ <head>
391
+ <meta charset="utf-8">
392
+ <meta name="viewport" content="width=device-width,initial-scale=1">
393
+ <title>{title} — Project Docs</title>
394
+ <style>
395
+ :root{{color-scheme:light dark;font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;--line:#8885;--muted:#777;--panel:color-mix(in srgb,Canvas 96%,CanvasText 4%);--accent:LinkText}}
396
+ *{{box-sizing:border-box}} body{{margin:0;background:Canvas;color:CanvasText}} a{{color:LinkText}}
397
+ header{{position:sticky;top:0;z-index:5;display:flex;gap:12px;align-items:center;padding:10px 14px;border-bottom:1px solid var(--line);background:Canvas}}
398
+ header h1{{font-size:17px;margin:0;white-space:nowrap}} #search{{flex:1;max-width:620px;padding:7px 9px;font:inherit}}
399
+ .repo{{font-size:12px;color:var(--muted);margin-left:auto}}
400
+ .layout{{display:grid;grid-template-columns:300px minmax(0,1fr) 300px;min-height:calc(100vh - 52px)}}
401
+ nav{{border-right:1px solid var(--line);padding:12px;overflow:auto}} nav h3{{font-size:12px;text-transform:uppercase;letter-spacing:.08em;margin:16px 4px 6px;color:var(--muted)}}
402
+ nav button{{width:100%;text-align:left;border:0;background:none;color:inherit;padding:7px;border-radius:6px;cursor:pointer}} nav button:hover,nav button.active{{background:var(--panel)}} nav small{{display:block;color:var(--muted);overflow:hidden;text-overflow:ellipsis}}
403
+ main{{padding:28px clamp(18px,4vw,64px);min-width:0}} article{{max-width:980px;margin:auto;line-height:1.6}} article h1{{font-size:2rem}} article h2{{margin-top:2.2rem;border-bottom:1px solid var(--line);padding-bottom:.25rem}} article pre{{overflow:auto;padding:12px;border:1px solid var(--line);border-radius:8px;background:var(--panel)}} article code{{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}} article :not(pre)>code{{padding:.1em .3em;border-radius:4px;background:var(--panel)}} article table{{border-collapse:collapse;width:100%;display:block;overflow:auto}} article th,article td{{border:1px solid var(--line);padding:7px 9px;text-align:left;vertical-align:top}} article blockquote{{border-left:4px solid var(--line);margin-left:0;padding-left:14px;color:var(--muted)}}
404
+ aside{{border-left:1px solid var(--line);padding:12px;overflow:auto}} .card{{border:1px solid var(--line);border-radius:8px;padding:10px;margin-bottom:10px;background:var(--panel)}} .card h3{{margin:0 0 6px;font-size:14px}} .status{{font-weight:700;font-size:12px}} .muted{{color:var(--muted)}} .badge{{display:inline-block;border:1px solid var(--line);border-radius:999px;padding:2px 6px;font-size:11px;margin-right:4px}} .warning{{border-left:4px solid #b8860b;padding:8px 10px;background:var(--panel);margin:10px 0}}
405
+ button.copy{{font:inherit;font-size:12px;padding:4px 7px}} .mermaid-lite{{overflow:auto;border:1px solid var(--line);border-radius:8px;padding:8px;background:var(--panel)}} .mermaid-lite svg{{min-width:680px;width:100%;height:auto}} .mermaid-fallback{{white-space:pre-wrap;font-family:ui-monospace,monospace;font-size:12px}}
406
+ @media(max-width:1100px){{.layout{{grid-template-columns:250px 1fr}}aside{{display:none}}}} @media(max-width:760px){{.layout{{display:block}}nav{{border-right:0;border-bottom:1px solid var(--line);max-height:280px}}header{{flex-wrap:wrap}}.repo{{width:100%;margin-left:0}}}}
407
+ </style>
408
+ </head>
409
+ <body>
410
+ <header><h1 id="siteTitle"></h1><input id="search" type="search" placeholder="Search project docs"><div class="repo" id="repo"></div></header>
411
+ <div class="layout"><nav id="nav"></nav><main><article id="doc"></article></main><aside id="side"></aside></div>
412
+ <script>
413
+ const SITE={data};
414
+ const state={{path:SITE.entry,query:''}};
415
+ const byPath=new Map(SITE.documents.map(d=>[d.path,d]));
416
+ function esc(v){{return String(v??'').replace(/[&<>"']/g,c=>({{'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}})[c])}}
417
+ function safeHref(v){{let s=String(v||'').trim();if(!s)return '#';if(/^https?:\/\//i.test(s)||/^mailto:/i.test(s)||/^#/.test(s)||/^[.\w\/-]+(?:#[\w-]+)?$/.test(s))return s;return '#'}}
418
+ function inline(v){{let s=esc(v);s=s.replace(/`([^`]+)`/g,'<code>$1</code>');s=s.replace(/\*\*([^*]+)\*\*/g,'<strong>$1</strong>');s=s.replace(/\*([^*]+)\*/g,'<em>$1</em>');s=s.replace(/\[([^\]]+)\]\(([^)]+)\)/g,(_,t,u)=>`<a href="${{esc(safeHref(u))}}" target="${{/^https?:/.test(u)?'_blank':'_self'}}">${{t}}</a>`);return s}}
419
+ function md(text){{let lines=String(text||'').replace(/\r/g,'').split('\n'),out=[],i=0;while(i<lines.length){{let l=lines[i];if(/^```/.test(l)){{let lang=l.slice(3).trim(),buf=[];i++;while(i<lines.length&&!/^```/.test(lines[i]))buf.push(lines[i++]);i++;let src=buf.join('\n');if(lang==='mermaid')out.push(`<div class="mermaid-lite" data-mermaid="${{esc(src)}}"></div>`);else out.push(`<pre><code>${{esc(src)}}</code></pre>`);continue}}if(/^\|.*\|\s*$/.test(l)&&i+1<lines.length&&/^\|?\s*:?-+/.test(lines[i+1])){{let rows=[];rows.push(l);i+=2;while(i<lines.length&&/^\|.*\|\s*$/.test(lines[i]))rows.push(lines[i++]);let cells=x=>x.trim().replace(/^\||\|$/g,'').split('|').map(c=>c.trim());let h=cells(rows[0]);out.push('<table><thead><tr>'+h.map(c=>`<th>${{inline(c)}}</th>`).join('')+'</tr></thead><tbody>'+rows.slice(1).map(r=>'<tr>'+cells(r).map(c=>`<td>${{inline(c)}}</td>`).join('')+'</tr>').join('')+'</tbody></table>');continue}}let m=l.match(/^(#{{1,6}})\s+(.+)$/);if(m){{let n=m[1].length;out.push(`<h${{n}}>${{inline(m[2])}}</h${{n}}>`);i++;continue}}if(/^---+\s*$/.test(l)){{out.push('<hr>');i++;continue}}if(/^>\s?/.test(l)){{let b=[];while(i<lines.length&&/^>\s?/.test(lines[i]))b.push(lines[i++].replace(/^>\s?/,''));out.push(`<blockquote>${{md(b.join('\n'))}}</blockquote>`);continue}}if(/^[-*+]\s+/.test(l)){{let a=[];while(i<lines.length&&/^[-*+]\s+/.test(lines[i]))a.push(lines[i++].replace(/^[-*+]\s+/,''));out.push('<ul>'+a.map(x=>`<li>${{inline(x)}}</li>`).join('')+'</ul>');continue}}if(/^\d+\.\s+/.test(l)){{let a=[];while(i<lines.length&&/^\d+\.\s+/.test(lines[i]))a.push(lines[i++].replace(/^\d+\.\s+/,''));out.push('<ol>'+a.map(x=>`<li>${{inline(x)}}</li>`).join('')+'</ol>');continue}}if(!l.trim()){{i++;continue}}let p=[l];i++;while(i<lines.length&&lines[i].trim()&&!/^(#|```|>|[-*+]\s+|\d+\.\s+)/.test(lines[i])&&!/^\|.*\|\s*$/.test(lines[i]))p.push(lines[i++]);out.push(`<p>${{inline(p.join(' '))}}</p>`);}}return out.join('\n')}}
420
+ function mermaidLite(el){{let src=el.dataset.mermaid||'',first=src.split('\n').find(x=>x.trim())||'';let mm=first.match(/^\s*flowchart\s+(LR|TD)/i);if(!mm){{el.innerHTML=`<div class="mermaid-fallback">${{esc(src)}}</div>`;return}}let dir=mm[1].toUpperCase(),nodes=new Map(),edges=[];for(let raw of src.split('\n').slice(1)){{let l=raw.trim();if(!l||l.startsWith('%%'))continue;let re=/([A-Za-z0-9_-]+)(?:\[([^\]]+)\])?\s*--+>?\s*([A-Za-z0-9_-]+)(?:\[([^\]]+)\])?/;let x=l.match(re);if(x){{nodes.set(x[1],(x[2]||nodes.get(x[1])||x[1]).replace(/<br\s*\/?>/gi,' / '));nodes.set(x[3],(x[4]||nodes.get(x[3])||x[3]).replace(/<br\s*\/?>/gi,' / '));edges.push([x[1],x[3]]);continue}}let n=l.match(/^([A-Za-z0-9_-]+)\[([^\]]+)\]/);if(n)nodes.set(n[1],n[2].replace(/<br\s*\/?>/gi,' / '))}}let ids=[...nodes.keys()];if(!ids.length){{el.innerHTML=`<div class="mermaid-fallback">${{esc(src)}}</div>`;return}}let w=dir==='LR'?Math.max(720,ids.length*190):900,h=dir==='LR'?220:Math.max(260,Math.ceil(ids.length/4)*130),pos=new Map();ids.forEach((id,k)=>{{if(dir==='LR')pos.set(id,{{x:30+k*180,y:70}});else pos.set(id,{{x:30+(k%4)*215,y:35+Math.floor(k/4)*120}})}});let svg=[`<svg viewBox="0 0 ${{w}} ${{h}}" role="img"><defs><marker id="arr" markerWidth="8" markerHeight="8" refX="7" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 z" fill="currentColor"/></marker></defs>`];for(let [a,b] of edges){{let p=pos.get(a),q=pos.get(b);if(!p||!q)continue;svg.push(`<line x1="${{p.x+140}}" y1="${{p.y+35}}" x2="${{q.x}}" y2="${{q.y+35}}" stroke="currentColor" stroke-opacity=".55" marker-end="url(#arr)"/>`)}}for(let id of ids){{let p=pos.get(id),label=nodes.get(id);svg.push(`<rect x="${{p.x}}" y="${{p.y}}" width="140" height="70" rx="9" fill="Canvas" stroke="currentColor" stroke-opacity=".55"/><foreignObject x="${{p.x+8}}" y="${{p.y+8}}" width="124" height="54"><div xmlns="http://www.w3.org/1999/xhtml" style="font:12px system-ui;text-align:center;display:flex;align-items:center;justify-content:center;height:54px">${{esc(label)}}</div></foreignObject>`)}}svg.push('</svg>');el.innerHTML=svg.join('')}}
421
+ function nav(){{let q=state.query.toLowerCase(),groups=new Map();for(let d of SITE.documents){{if(q&&!`${{d.title}} ${{d.path}} ${{d.content}}`.toLowerCase().includes(q))continue;let a=groups.get(d.section)||[];a.push(d);groups.set(d.section,a)}}let n=document.querySelector('#nav');n.innerHTML='';for(let [g,ds] of groups){{let h=document.createElement('h3');h.textContent=g;n.appendChild(h);for(let d of ds){{let b=document.createElement('button');b.className=d.path===state.path?'active':'';b.innerHTML=`${{esc(d.title)}}<small>${{esc(d.path)}}</small>`;b.onclick=()=>{{state.path=d.path;render()}};n.appendChild(b)}}}}}}
422
+ function labCard(label,l){{let link=l.hasIndex?`<p><a href="labs/${{l.kind}}/index.html" target="_blank">Open existing projection</a></p>`:'';return `<div class="card"><h3>${{esc(label)}}</h3><div class="status">${{esc(l.status)}}</div><p class="muted">${{esc(l.detail)}}</p>${{link}}<p><code>${{esc(l.command)}}</code></p><button class="copy" data-copy="${{esc(l.command)}}">Copy command</button></div>`}}
423
+ function side(d){{let s=`<div class="card"><h3>Document</h3><span class="badge">${{esc(d.ownership)}}</span><span class="badge">${{esc(d.authority)}}</span><p class="muted">${{esc(d.path)}}</p></div>`;s+=labCard('System Lab',SITE.labs.system)+labCard('Database Lab',SITE.labs.database);if(SITE.warnings.length)s+=`<div class="card"><h3>Documentation checks</h3>${{SITE.warnings.map(x=>`<div class="warning">${{esc(x)}}</div>`).join('')}}</div>`;s+=`<div class="card"><h3>Learning</h3><p><code>/system-lab</code><br><code>/db-lab</code><br><code>/code-walkthrough</code><br><code>/learning-review</code><br><code>/coach</code></p></div>`;document.querySelector('#side').innerHTML=s;document.querySelectorAll('[data-copy]').forEach(b=>b.onclick=()=>navigator.clipboard?.writeText(b.dataset.copy))}}
424
+ function render(){{let d=byPath.get(state.path)||SITE.documents[0];if(!d){{document.querySelector('#doc').innerHTML='<h1>No Markdown documents found</h1>';return}}state.path=d.path;document.querySelector('#doc').innerHTML=md(d.content);document.querySelectorAll('.mermaid-lite').forEach(mermaidLite);nav();side(d);history.replaceState(null,'','#'+encodeURIComponent(d.path))}}
425
+ document.querySelector('#siteTitle').textContent=SITE.title;document.querySelector('#repo').textContent=`${{SITE.repository.branch||''}} ${{SITE.repository.commit?SITE.repository.commit.slice(0,10):'no git'}}`;document.querySelector('#search').oninput=e=>{{state.query=e.target.value;nav()}};let h=decodeURIComponent(location.hash.slice(1));if(byPath.has(h))state.path=h;render();
426
+ </script>
427
+ </body></html>'''
428
+
429
+
430
+ def build(root: Path, out: Path, *, prepare_system_lab: bool = False) -> tuple[Path, dict[str, Any]]:
431
+ root = root.resolve()
432
+ out = Path(os.path.abspath(out))
433
+ if not root.is_dir():
434
+ raise DocsSiteError(f"Repository root does not exist: {root}")
435
+ for kind in ("system", "database"):
436
+ _validate_lab_cache(root, kind)
437
+ _prepare_output(root, out)
438
+ prep_warning = _prepare_system_lab(root) if prepare_system_lab else None
439
+ payload = _site_payload(root)
440
+ if prep_warning:
441
+ payload["warnings"].append(prep_warning)
442
+ _copy_lab_cache(root, out, "system")
443
+ _copy_lab_cache(root, out, "database")
444
+ index = out / "index.html"
445
+ index.write_text(_html_document(payload), encoding="utf-8")
446
+ (out / "site.json").write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
447
+ return index, payload
448
+
449
+
450
+ def serve(root: Path, out: Path, port: int, *, open_browser: bool, prepare_system_lab: bool) -> int:
451
+ index, payload = build(root, out, prepare_system_lab=prepare_system_lab)
452
+ handler = partial(SimpleHTTPRequestHandler, directory=str(index.parent))
453
+ server = ThreadingHTTPServer(("127.0.0.1", port), handler)
454
+ actual_port = int(server.server_address[1])
455
+ url = f"http://127.0.0.1:{actual_port}/"
456
+ print(f"Lean Project Docs: {url}")
457
+ print(f"Documents: {len(payload['documents'])}")
458
+ print(f"System Lab: {payload['labs']['system']['status']}")
459
+ print(f"Database Lab: {payload['labs']['database']['status']}")
460
+ if open_browser:
461
+ webbrowser.open(url)
462
+ try:
463
+ server.serve_forever()
464
+ except KeyboardInterrupt:
465
+ pass
466
+ finally:
467
+ server.server_close()
468
+ return 0
469
+
470
+
471
+ def parse_args() -> argparse.Namespace:
472
+ parser = argparse.ArgumentParser(description="Build or serve the dependency-free Lean project documentation site.")
473
+ sub = parser.add_subparsers(dest="command", required=True)
474
+
475
+ build_parser = sub.add_parser("build")
476
+ build_parser.add_argument("--root", type=Path, default=Path("."))
477
+ build_parser.add_argument("--out", type=Path)
478
+ build_parser.add_argument("--prepare-system-lab", action="store_true")
479
+
480
+ serve_parser = sub.add_parser("serve")
481
+ serve_parser.add_argument("--root", type=Path, default=Path("."))
482
+ serve_parser.add_argument("--out", type=Path)
483
+ serve_parser.add_argument("--port", type=int, default=8765)
484
+ serve_parser.add_argument("--open", action="store_true", dest="open_browser")
485
+ serve_parser.add_argument("--no-system-lab", action="store_true")
486
+ return parser.parse_args()
487
+
488
+
489
+ def main() -> int:
490
+ args = parse_args()
491
+ root = args.root.resolve()
492
+ out = args.out if args.out else root / DEFAULT_SITE
493
+ try:
494
+ if args.command == "build":
495
+ index, payload = build(root, out, prepare_system_lab=args.prepare_system_lab)
496
+ print(f"Lean Project Docs built: {index}")
497
+ print(f"Documents: {len(payload['documents'])}")
498
+ return 0
499
+ return serve(
500
+ root,
501
+ out,
502
+ args.port,
503
+ open_browser=args.open_browser,
504
+ prepare_system_lab=not args.no_system_lab,
505
+ )
506
+ except DocsSiteError as exc:
507
+ print(f"ERROR: {exc}", file=sys.stderr)
508
+ return 2
509
+
510
+
511
+ if __name__ == "__main__":
512
+ raise SystemExit(main())
@@ -6,6 +6,7 @@ import sys
6
6
 
7
7
  from .app_cli import main as app_main
8
8
  from .cli import main as core_main
9
+ from .local_cli import main as local_main
9
10
  from .registry_cli import main as registry_main
10
11
  from .sqlite_cli import main as sqlite_main
11
12
  from .teardown_cli import main as teardown_main
@@ -15,6 +16,8 @@ def main(argv: list[str] | None = None) -> int:
15
16
  args = list(sys.argv[1:] if argv is None else argv)
16
17
  if args and args[0] == "app":
17
18
  return app_main(args[1:])
19
+ if args and args[0] == "local":
20
+ return local_main(args[1:])
18
21
  if args and args[0] == "registry":
19
22
  return registry_main(args[1:])
20
23
  if args and args[0] == "sqlite":
@@ -22,7 +25,8 @@ def main(argv: list[str] | None = None) -> int:
22
25
  if args and args[0] == "tear-down":
23
26
  return teardown_main(args[1:])
24
27
  if not args or args == ["--help"] or args == ["-h"]:
25
- print("Command group: app Generic immutable application runtime inventory/deployment")
28
+ print("Command group: app Remote immutable application runtime inventory/deployment")
29
+ print("Command group: local Loopback-only workstation application runtime")
26
30
  print("Command group: registry Read-only exact-image registry access verification")
27
31
  print("Command group: sqlite SQLite persistence import/backup/migration/rollback checks")
28
32
  print("Command group: tear-down Scoped destructive POC environment/local cleanup")
@@ -0,0 +1,112 @@
1
+ """CLI for loopback-only workstation application deployment."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import json
7
+ import sys
8
+ from typing import Any
9
+
10
+ from .errors import DeploymentKitError
11
+ from .local_runtime import (
12
+ apply_local_deploy,
13
+ local_deploy_write_phrase,
14
+ local_inventory,
15
+ normalize_local_spec,
16
+ plan_local_deploy,
17
+ )
18
+
19
+
20
+ def _print(value: Any, *, as_json: bool) -> None:
21
+ if as_json:
22
+ print(json.dumps(value, indent=2, sort_keys=True))
23
+ elif isinstance(value, dict):
24
+ for key, item in value.items():
25
+ print(f"{key}: {json.dumps(item, sort_keys=True) if isinstance(item, (dict, list)) else item}")
26
+ else:
27
+ print(value)
28
+
29
+
30
+ def _parse_env(items: list[str]) -> list[tuple[str, str]]:
31
+ result: list[tuple[str, str]] = []
32
+ for item in items:
33
+ if "=" not in item:
34
+ raise DeploymentKitError("--env entries must use NAME=value form.")
35
+ key, value = item.split("=", 1)
36
+ result.append((key, value))
37
+ return result
38
+
39
+
40
+ def _confirm(args: argparse.Namespace, phrase: str, action: str) -> None:
41
+ if args.yes:
42
+ return
43
+ entered = input(f"Type {phrase} to {action}: ").strip()
44
+ if entered != phrase:
45
+ raise DeploymentKitError(f"{action.capitalize()} was not authorized.")
46
+
47
+
48
+ def build_parser() -> argparse.ArgumentParser:
49
+ parser = argparse.ArgumentParser(prog="lean-poc-deploy local")
50
+ sub = parser.add_subparsers(dest="command", required=True)
51
+
52
+ inventory = sub.add_parser(
53
+ "inventory",
54
+ help="Read Lean-owned workstation application runtime evidence without writes",
55
+ )
56
+ inventory.add_argument("--json", action="store_true")
57
+
58
+ deploy = sub.add_parser(
59
+ "deploy",
60
+ help="Deploy one immutable application container bound only to loopback",
61
+ )
62
+ deploy.add_argument("--owner-repo", required=True)
63
+ deploy.add_argument("--environment", required=True)
64
+ deploy.add_argument("--app-slug", required=True)
65
+ deploy.add_argument("--container-name", required=True)
66
+ deploy.add_argument("--image", required=True)
67
+ deploy.add_argument("--runtime-port", type=int, required=True)
68
+ deploy.add_argument("--host-port", type=int, required=True)
69
+ deploy.add_argument("--health-path", required=True)
70
+ deploy.add_argument("--run-as", required=True)
71
+ deploy.add_argument("--persistence-mode", choices=("none", "directory"), required=True)
72
+ deploy.add_argument("--data-container-path")
73
+ deploy.add_argument("--env", action="append", default=[])
74
+ deploy.add_argument("--json", action="store_true")
75
+ deploy.add_argument("--yes", action="store_true")
76
+ return parser
77
+
78
+
79
+ def main(argv: list[str] | None = None) -> int:
80
+ args = build_parser().parse_args(argv)
81
+ try:
82
+ if args.command == "inventory":
83
+ _print(local_inventory(), as_json=args.json)
84
+ return 0
85
+
86
+ spec = normalize_local_spec(
87
+ owner_repo=args.owner_repo,
88
+ environment=args.environment,
89
+ app_slug=args.app_slug,
90
+ container_name=args.container_name,
91
+ image=args.image,
92
+ runtime_port=args.runtime_port,
93
+ host_port=args.host_port,
94
+ health_path=args.health_path,
95
+ run_as=args.run_as,
96
+ persistence_mode=args.persistence_mode,
97
+ data_container_path=args.data_container_path,
98
+ environment_variables=_parse_env(args.env),
99
+ )
100
+ plan = plan_local_deploy(spec)
101
+ _print({key: value for key, value in plan.items() if key != "spec"}, as_json=False)
102
+ phrase = local_deploy_write_phrase(plan)
103
+ _confirm(
104
+ args,
105
+ phrase,
106
+ f"deploy {plan['appSlug']} ({plan['environment']}) on this workstation",
107
+ )
108
+ _print(apply_local_deploy(plan), as_json=args.json)
109
+ return 0
110
+ except DeploymentKitError as error:
111
+ print(f"ERROR: {error}", file=sys.stderr)
112
+ return 2