mapwright-agent 0.2.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.
mapwright/routes_js.py ADDED
@@ -0,0 +1,282 @@
1
+ """Client-side route tables for JS/TS: react-router object/array configs, <Route> JSX, lazy route groups.
2
+
3
+ Pattern based, nothing is executed. Routes are emitted as method "PAGE" (a screen, not an HTTP verb) and the
4
+ handler is the page component when it can be named. Nested paths are composed: a child path that does not start
5
+ with "/" is appended to its parent's path, an absolute one replaces it.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import os
11
+ import re
12
+
13
+ _ROUTER_IMPORT = re.compile(r"""from\s+['"](?:react-router(?:-dom)?(?:/[\w-]+)?|@tanstack/react-router|@remix-run/router)['"]""")
14
+ _ROUTER_FILE = re.compile(r"(?:^|/)(?:routes?|routers?|routing|App|app)(?:[./]|$)|(?:routes?|routers?)\.\w+$", re.I)
15
+ _KEYS = re.compile(r"""(?<![\w$.])(?:path|index)\s*:|(?<![\w$.])children\s*:\s*\[""")
16
+ _ROUTE_KEYS = {"element", "Component", "lazy", "children", "index", "loader", "errorElement", "component"}
17
+ _WRAPPER = re.compile(r"^(?:Suspense|Fragment|\w*(?:Provider|Boundary|Gate|Guard|Route|Outlet))$")
18
+ _STR = r"""(?:'([^'\n]*)'|"([^"\n]*)"|`([^`\n$]*)`)"""
19
+ _PATH_ATTR = re.compile(r"""\bpath\s*=\s*(?:\{\s*)?""" + _STR)
20
+ _LAZY_CALL = re.compile(r"""\b([A-Za-z_$][\w$]*)\(\s*(?:\(\s*\)\s*=>\s*)?(?:async\s*\(\s*\)\s*=>\s*)?[^()\[\]]*?import\(\s*['"]([^'"]+)['"]""")
21
+
22
+
23
+ def wants(rel: str, text: str) -> bool:
24
+ return bool(_ROUTER_IMPORT.search(text) or (_ROUTER_FILE.search(rel) and re.search(r"\bpath\s*[:=]|\bRoute\b", text)))
25
+
26
+
27
+ def _skip_string(text: str, j: int) -> int:
28
+ q = text[j]
29
+ j += 1
30
+ while j < len(text) and text[j] != q:
31
+ j += 2 if text[j] == "\\" else 1
32
+ return j
33
+
34
+
35
+ def _match_close(text: str, i: int) -> int:
36
+ """Offset of the bracket that closes the one at text[i] (or len(text))."""
37
+ pairs = {"{": "}", "[": "]", "(": ")"}
38
+ stack, j, n = [pairs[text[i]]], i + 1, len(text)
39
+ while j < n and stack:
40
+ ch = text[j]
41
+ if ch in "'\"`":
42
+ j = _skip_string(text, j)
43
+ elif ch == "/" and text[j:j + 2] == "//":
44
+ j = text.find("\n", j)
45
+ if j < 0:
46
+ return n
47
+ elif ch == "/" and text[j:j + 2] == "/*":
48
+ j = text.find("*/", j + 2)
49
+ if j < 0:
50
+ return n
51
+ elif ch in pairs:
52
+ stack.append(pairs[ch])
53
+ elif ch == stack[-1]:
54
+ stack.pop()
55
+ j += 1
56
+ return j - 1 if not stack else n
57
+
58
+
59
+ def _entries(text: str, a: int, b: int) -> list[tuple[int, int]]:
60
+ """Top-level comma separated spans between offsets a and b."""
61
+ out, start, j = [], a, a
62
+ pairs = {"{": "}", "[": "]", "(": ")"}
63
+ while j < b:
64
+ ch = text[j]
65
+ if ch in "'\"`":
66
+ j = _skip_string(text, j)
67
+ elif ch in pairs:
68
+ j = _match_close(text, j)
69
+ elif ch == ",":
70
+ out.append((start, j))
71
+ start = j + 1
72
+ j += 1
73
+ if text[start:b].strip():
74
+ out.append((start, b))
75
+ return out
76
+
77
+
78
+ def _enclosing_open(text: str, pos: int) -> int:
79
+ depth = 0
80
+ for j in range(pos - 1, -1, -1):
81
+ ch = text[j]
82
+ if ch == "}":
83
+ depth += 1
84
+ elif ch == "{":
85
+ if depth == 0:
86
+ return j
87
+ depth -= 1
88
+ return -1
89
+
90
+
91
+ def _join(prefix: str, seg: str | None) -> str:
92
+ if seg is None:
93
+ return prefix
94
+ if seg.startswith("/"):
95
+ return seg
96
+ return (prefix.rstrip("/") + "/" + seg.lstrip("/")).rstrip("/") if seg and seg != "" else prefix
97
+
98
+
99
+ def _component(snippet: str) -> str | None:
100
+ for m in re.finditer(r"<([A-Z][\w.]*)", snippet):
101
+ if not _WRAPPER.match(m.group(1)):
102
+ return m.group(1).split(".")[-1]
103
+ return None
104
+
105
+
106
+ def _norm(p: str) -> str:
107
+ return re.sub(r"/{2,}", "/", p) if p else "/"
108
+
109
+
110
+ def _from_object(text: str, s: int, e: int, prefix: str, objs: list[tuple[int, int]], out: list, line_of) -> None:
111
+ fields: dict[str, tuple[int, int]] = {}
112
+ for a, b in _entries(text, s + 1, e):
113
+ m = re.match(r"\s*['\"]?([\w$]+)['\"]?\s*:", text[a:b])
114
+ if m:
115
+ fields[m.group(1)] = (a + m.end(), b)
116
+ seg = None
117
+ if "path" in fields:
118
+ m = re.fullmatch(r"\s*" + _STR + r"\s*", text[slice(*fields["path"])])
119
+ seg = next((g for g in m.groups() if g is not None), None) if m else None
120
+ is_index = "index" in fields and re.match(r"\s*true\b", text[slice(*fields["index"])])
121
+ route_like = (seg is not None or is_index or "children" in fields) and (set(fields) & _ROUTE_KEYS)
122
+ if not route_like:
123
+ for a, b in objs: # not a route: look for route objects inside it
124
+ if s < a and b < e and not any(s < x <= a and b <= y < e for x, y in objs if (x, y) != (a, b)):
125
+ _from_object(text, a, b, prefix, objs, out, line_of)
126
+ return
127
+ here = _join(prefix, seg) if seg is not None else prefix
128
+ handler, target = None, None
129
+ for key in ("element", "Component", "component", "lazy"):
130
+ if key not in fields:
131
+ continue
132
+ v = text[slice(*fields[key])]
133
+ if key == "element":
134
+ handler = handler or _component(v)
135
+ elif key in ("Component", "component"):
136
+ mm = re.match(r"\s*([A-Z][\w$]*)\s*$", v)
137
+ handler = handler or (mm.group(1) if mm else None)
138
+ else:
139
+ im = re.search(r"import\(\s*['\"]([^'\"]+)['\"]", v)
140
+ target = im.group(1) if im else None
141
+ tm = re.search(r"(?:default|Component)\s*:\s*\w+\.([A-Z][\w$]*)|\.then\(\s*\(?\w+\)?\s*=>\s*\w+\.([A-Z][\w$]*)", v)
142
+ handler = handler or (next((g for g in tm.groups() if g), None) if tm else None)
143
+ if seg is not None or is_index:
144
+ r = {"method": "PAGE", "path": _norm(here) if (seg is not None or here) else "/", "via": "react-router", "handler": handler,
145
+ "line": line_of(s)}
146
+ if target:
147
+ r["target"] = target
148
+ out.append(r)
149
+ if "children" in fields:
150
+ a, b = fields["children"]
151
+ i = text.find("[", a)
152
+ if 0 <= i < b:
153
+ for x, y in _entries(text, i + 1, _match_close(text, i)):
154
+ k = text.find("{", x)
155
+ if 0 <= k < y and not text[x:k].strip():
156
+ _from_object(text, k, _match_close(text, k), here, objs, out, line_of)
157
+
158
+
159
+ def _jsx_routes(text: str, out: list, line_of) -> None:
160
+ stack: list[str] = []
161
+ for m in re.finditer(r"<Route\b|</Route\s*>", text):
162
+ if m.group(0).startswith("</"):
163
+ if stack:
164
+ stack.pop()
165
+ continue
166
+ j, depth = m.end(), 0
167
+ while j < len(text):
168
+ ch = text[j]
169
+ if ch in "'\"`":
170
+ j = _skip_string(text, j)
171
+ elif ch == "{":
172
+ depth += 1
173
+ elif ch == "}":
174
+ depth -= 1
175
+ elif ch == ">" and depth == 0:
176
+ break
177
+ j += 1
178
+ tag = text[m.end():j]
179
+ selfclose = tag.rstrip().endswith("/")
180
+ parent = stack[-1] if stack else ""
181
+ pm = _PATH_ATTR.search(tag)
182
+ seg = next((g for g in pm.groups() if g is not None), None) if pm else None
183
+ here = _join(parent, seg) if seg is not None else parent
184
+ is_index = bool(re.search(r"\bindex\b(?!\s*=\s*\{?\s*false)", tag))
185
+ if seg is not None or is_index:
186
+ em = re.search(r"\belement\s*=\s*\{(.*)", tag, re.S)
187
+ cm = re.search(r"\bComponent\s*=\s*\{\s*([A-Z][\w$]*)", tag)
188
+ handler = _component(em.group(1)) if em else (cm.group(1) if cm else None)
189
+ out.append({"method": "PAGE", "path": _norm(here) if here else "/", "via": "react-router", "handler": handler,
190
+ "line": line_of(m.start())})
191
+ if not selfclose:
192
+ stack.append(here)
193
+
194
+
195
+ def _lazy_groups(text: str, out: list, line_of) -> None:
196
+ """lazyGroup(() => import('./x'), ['/a', '/b']): a helper that code-splits a list of paths."""
197
+ for m in _LAZY_CALL.finditer(text):
198
+ if m.group(1) in ("lazy", "import", "require", "Promise"):
199
+ continue
200
+ op = text.find("(", m.start())
201
+ end = _match_close(text, op)
202
+ am = re.search(r"\[\s*((?:(?:'[^'\n]*'|\"[^\"\n]*\")\s*,?\s*)+)\]", text[m.end():end])
203
+ if not am:
204
+ continue
205
+ paths = re.findall(r"""['"]([^'"\n]*)['"]""", am.group(1))
206
+ if not paths or not all(p.startswith("/") for p in paths):
207
+ continue
208
+ for p in paths:
209
+ out.append({"method": "PAGE", "path": p, "via": "lazy-group", "handler": None, "target": m.group(2),
210
+ "line": line_of(m.start())})
211
+
212
+
213
+ def find_routes(rel: str, text: str) -> list[dict]:
214
+ if not wants(rel, text):
215
+ return []
216
+ line_of = lambda pos: text.count("\n", 0, pos) + 1 # noqa: E731
217
+ out: list[dict] = []
218
+ objs = set()
219
+ for m in _KEYS.finditer(text):
220
+ s = _enclosing_open(text, m.start())
221
+ if s >= 0:
222
+ objs.add((s, _match_close(text, s)))
223
+ objs_l = sorted(objs)
224
+ roots = [(a, b) for a, b in objs_l if not any(x < a and b < y for x, y in objs_l)]
225
+ for a, b in roots:
226
+ _from_object(text, a, b, "", objs_l, out, line_of)
227
+ if "<Route" in text:
228
+ _jsx_routes(text, out, line_of)
229
+ _lazy_groups(text, out, line_of)
230
+ seen, uniq = set(), []
231
+ for r in out:
232
+ k = (r["path"], r["handler"], r["via"])
233
+ if k not in seen:
234
+ seen.add(k)
235
+ uniq.append(r)
236
+ return uniq
237
+
238
+
239
+ def drop_shadowed(routes: list[dict]) -> list[dict]:
240
+ """A lazy-group path that is also defined for real elsewhere is listed once, with the real route."""
241
+ real = {r["path"] for r in routes if r.get("via") != "lazy-group"}
242
+ return [r for r in routes if r.get("via") != "lazy-group" or r["path"] not in real]
243
+
244
+
245
+ # ---------------------------------------------------------------- React components, hooks, zod schemas
246
+ _COMPONENT_FN = re.compile(r"^[A-Z]")
247
+
248
+
249
+ def tag_react(rel: str, text: str, functions: list[dict]) -> None:
250
+ """Mark function components (capitalised, returns JSX) and hooks (use*) in .jsx/.tsx/.ts files."""
251
+ if os.path.splitext(rel)[1].lower() not in (".tsx", ".jsx", ".ts", ".js"):
252
+ return
253
+ lines = text.split("\n")
254
+ for f in functions:
255
+ if f.get("cls"):
256
+ continue
257
+ body = "\n".join(lines[f["line"] - 1:f["end"]])
258
+ if re.match(r"use[A-Z0-9]", f["name"]):
259
+ f["hints"] = [*f["hints"], "react hook"]
260
+ f["react"] = "hook"
261
+ elif _COMPONENT_FN.match(f["name"]) and re.search(r"<[A-Za-z][\w.]*[\s/>]|<>|\bjsx\(|createElement\(", body):
262
+ f["hints"] = [*f["hints"], "react component"]
263
+ f["react"] = "component"
264
+
265
+
266
+ def zod_schemas(text: str) -> list[dict]:
267
+ """`const Name = z.object({ a: z.string(), ... })` as table-like data entities."""
268
+ out = []
269
+ for m in re.finditer(r"^[ \t]*(?:export\s+)?const\s+([A-Za-z_$][\w$]*)\s*=\s*z\.object\(\s*\{", text, re.M):
270
+ s = text.find("{", m.end() - 1)
271
+ e = _match_close(text, s)
272
+ cols = []
273
+ for a, b in _entries(text, s + 1, e):
274
+ km = re.match(r"\s*['\"]?([\w$]+)['\"]?\s*:\s*(.*)", text[a:b], re.S)
275
+ if not km:
276
+ continue
277
+ tm = re.search(r"\bz\.(\w+)|\b(\w+)\b", km.group(2))
278
+ typ = (tm.group(1) or tm.group(2)) if tm else "any"
279
+ cols.append({"name": km.group(1), "type": typ, "pk": False, "fk": None})
280
+ if cols:
281
+ out.append({"name": m.group(1), "line": text.count("\n", 0, m.start()) + 1, "columns": cols[:60], "source": "zod"})
282
+ return out
mapwright/server.py ADDED
@@ -0,0 +1,388 @@
1
+ """Local daemon on 127.0.0.1: the API behind the UI, and a watcher that keeps tracked projects current.
2
+
3
+ Every /v1 route needs the local token (Bearer). Requests must carry a Host of 127.0.0.1 or
4
+ localhost on our port (DNS rebinding), cross-origin requests are refused, and POST bodies must be
5
+ JSON. File contents are served only for tracked files, never for secret-looking ones, and pass
6
+ through redaction.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import difflib
12
+ import hmac
13
+ import json
14
+ import os
15
+ import threading
16
+ import time
17
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
18
+ from pathlib import Path
19
+ from urllib.parse import parse_qs, unquote, urlparse
20
+
21
+ from . import __version__, brief, config, connect, render
22
+ from . import analyze as A
23
+ from . import concepts as C
24
+ from . import flow as F
25
+ from .redact import redact
26
+ from .track import by_key, open_project
27
+
28
+ UI = Path(__file__).parent / "ui"
29
+ STATIC = {"/": "index.html", "/index.html": "index.html", "/app.js": "app.js", "/app.css": "app.css", "/icons.svg": "icons.svg",
30
+ "/mark.svg": "mark.svg", "/vendor/mermaid.min.js": "vendor/mermaid.min.js"}
31
+ TYPES = {".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml"}
32
+ CSP = ("default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; "
33
+ "font-src 'self' data:; frame-ancestors 'self' vscode-webview: http://127.0.0.1:* http://localhost:*; base-uri 'none'; form-action 'none'")
34
+
35
+
36
+ def file_rows(now: dict, base: dict | None, cache: dict | None = None) -> list[dict]:
37
+ rows = []
38
+ for rel in sorted(now):
39
+ part = ((cache or {}).get(rel) or {}).get("part") or {}
40
+ st = None if base is None else "A" if rel not in base else "M" if base[rel] != now[rel] else None
41
+ rows.append({"path": rel, "status": st, "layer": A.layer_of(rel, part) if part else None})
42
+ if base is not None:
43
+ rows += [{"path": rel, "status": "D", "layer": None} for rel in sorted(set(base) - set(now))]
44
+ return rows
45
+
46
+
47
+ class Daemon:
48
+ def __init__(self, port: int | None = None, watch: bool = True):
49
+ self.token = config.token()
50
+ self.port = port or config.port()
51
+ self.hosts = {f"127.0.0.1:{self.port}", f"localhost:{self.port}"}
52
+ self.origins = {f"http://{h}" for h in self.hosts}
53
+ self.httpd = ThreadingHTTPServer(("127.0.0.1", self.port), self._handler())
54
+ self.httpd.daemon_threads = True
55
+ self.stopping = threading.Event()
56
+ self.watch = watch
57
+ self.errors: list[str] = []
58
+ self.started = time.time()
59
+
60
+ # ------------------------------------------------------------------ watcher
61
+ def _watch_loop(self) -> None:
62
+ while not self.stopping.is_set():
63
+ interval = max(0.5, float(config.settings()["watch_interval"]))
64
+ for key, entry in list(config.projects().items()):
65
+ if self.stopping.is_set():
66
+ break
67
+ if not os.path.isdir(entry.get("root", "")):
68
+ continue
69
+ try:
70
+ p = open_project(entry["root"])
71
+ p.refresh()
72
+ p.maybe_auto_step()
73
+ except Exception as e: # one broken project must not stop the others
74
+ self.errors = (self.errors + [f"{key}: {type(e).__name__}: {e}"])[-20:]
75
+ self.stopping.wait(interval)
76
+
77
+ def start(self) -> "Daemon":
78
+ threading.Thread(target=self.httpd.serve_forever, daemon=True, name="mw-http").start()
79
+ if self.watch:
80
+ threading.Thread(target=self._watch_loop, daemon=True, name="mw-watch").start()
81
+ return self
82
+
83
+ def serve_forever(self) -> None:
84
+ if self.watch:
85
+ threading.Thread(target=self._watch_loop, daemon=True, name="mw-watch").start()
86
+ self.httpd.serve_forever()
87
+
88
+ def shutdown(self) -> None:
89
+ self.stopping.set()
90
+ self.httpd.shutdown()
91
+ self.httpd.server_close()
92
+
93
+ # ------------------------------------------------------------------ API
94
+ def project_or_404(self, key: str):
95
+ p = by_key(key)
96
+ if p is None:
97
+ raise LookupError("unknown project")
98
+ return p.ensure()
99
+
100
+ def diagram(self, p, kind: str, q: dict) -> dict:
101
+ step = q.get("step", "live")
102
+ steps = p.steps()
103
+ if step == "live":
104
+ model, files_now = p.model, p.index
105
+ base_n = steps[-1]["n"] if steps else None
106
+ else:
107
+ data = p.load_step(int(step))
108
+ if not data:
109
+ raise LookupError("no such step")
110
+ model, files_now = data["model"], data["files"]
111
+ base_n = int(step) - 1 if int(step) > 0 else None
112
+ if q.get("base") not in (None, "", "auto"):
113
+ base_n = None if q["base"] == "none" else int(q["base"])
114
+ base = p.load_step(base_n) if base_n is not None else None
115
+ marks = A.diff_models(base["model"] if base else None, model)
116
+ focus = q.get("focus") or None
117
+ if kind == "architecture":
118
+ out = render.architecture(model, marks, base["model"] if base else None, show_tests=q.get("tests") == "1")
119
+ elif kind == "files":
120
+ out = render.files(file_rows(files_now, base["files"] if base else None, p.cache))
121
+ elif kind == "classes":
122
+ out = render.classes(model, marks, base["model"] if base else None)
123
+ elif kind == "data":
124
+ out = render.erd(model, marks, base["model"] if base else None)
125
+ elif kind == "cloud":
126
+ out = render.cloud(model.get("infra") or {"nodes": [], "edges": []})
127
+ elif kind == "processes":
128
+ out = render.processes(model, focus)
129
+ elif kind == "calls":
130
+ out = render.calls(model, marks, focus)
131
+ elif kind == "concepts":
132
+ v = p.verify()
133
+ out = render.concept_map(model, model.get("concepts") or {}, v["claims"])
134
+ else:
135
+ raise LookupError("unknown diagram")
136
+ delta = {k: dict(__import__("collections").Counter(v.values())) for k, v in marks.items() if v}
137
+ return {**out, "kind": kind, "step": step, "base": base_n, "delta": delta}
138
+
139
+ def flow(self, p, fid: str, step: str) -> dict:
140
+ model = p.model if step == "live" else (p.load_step(int(step)) or {}).get("model")
141
+ if not model or fid not in model["functions"]:
142
+ raise LookupError("unknown function")
143
+ f = model["functions"][fid]
144
+ text = p.text(f["file"], None if step == "live" else int(step))
145
+ if text is None:
146
+ raise LookupError("file not readable")
147
+ out = F.python_flow(text, f["line"]) if f["file"].endswith(".py") else F.js_outline(text, f["line"], f["end"])
148
+ if out is None:
149
+ out = F.js_outline(text, f["line"], f["end"])
150
+ out.update({"fid": fid, "function": {k: f.get(k) for k in ("qual", "file", "line", "end", "args", "complexity", "hints",
151
+ "callees", "callers", "tables", "doc", "async", "loc")}})
152
+ return out
153
+
154
+ def file(self, p, path: str, step: str) -> dict:
155
+ idx = p.index if step == "live" else p.step_files(int(step))
156
+ if path not in idx:
157
+ raise LookupError("not a tracked file")
158
+ if A._secretish(path):
159
+ return {"path": path, "secret": True, "text": None}
160
+ text = p.blob(idx[path])
161
+ if text is None:
162
+ return {"path": path, "binary": True, "text": None}
163
+ if config.settings()["redact"]:
164
+ text = redact(text)
165
+ part = (p.cache.get(path) or {}).get("part") or {}
166
+ return {"path": path, "text": text, "lang": part.get("lang") or os.path.splitext(path)[1].lstrip("."),
167
+ "symbols": [{"qual": f["qual"], "line": f["line"], "end": f["end"], "kind": "function", "complexity": f["complexity"],
168
+ "fid": f"{part['id']}:{f['qual']}"} for f in part.get("functions", [])]
169
+ + [{"qual": c["name"], "line": c["line"], "end": c.get("end"), "kind": "class"} for c in part.get("classes", [])]}
170
+
171
+ def diff(self, p, path: str, a: str, b: str) -> dict:
172
+ def idx(s):
173
+ return p.index if s == "live" else p.step_files(int(s))
174
+ ta, tb = p.blob(idx(a).get(path)), p.blob(idx(b).get(path))
175
+ if A._secretish(path):
176
+ return {"path": path, "secret": True, "diff": ""}
177
+ lines = list(difflib.unified_diff((ta or "").splitlines(), (tb or "").splitlines(), f"a/{path}", f"b/{path}", n=3, lineterm=""))
178
+ text = "\n".join(lines[:4000])
179
+ return {"path": path, "diff": redact(text) if config.settings()["redact"] else text}
180
+
181
+ def _handler(self):
182
+ d = self
183
+
184
+ class H(BaseHTTPRequestHandler):
185
+ server_version = f"mapwright/{__version__}"
186
+
187
+ def log_message(self, *a):
188
+ pass
189
+
190
+ def _send(self, code: int, body, ctype: str = "application/json") -> None:
191
+ raw = body if isinstance(body, bytes) else json.dumps(body, default=str).encode()
192
+ self.send_response(code)
193
+ self.send_header("Content-Type", ctype)
194
+ self.send_header("Content-Length", str(len(raw)))
195
+ self.send_header("Cache-Control", "no-store")
196
+ self._cors()
197
+ self.send_header("X-Content-Type-Options", "nosniff")
198
+ self.send_header("Referrer-Policy", "no-referrer")
199
+ if ctype.startswith("text/html"):
200
+ self.send_header("Content-Security-Policy", CSP)
201
+ self.end_headers()
202
+ self.wfile.write(raw)
203
+
204
+ def _allowed_origin(self):
205
+ o = self.headers.get("Origin")
206
+ return o if o and (o in d.origins or o in config.settings()["web_origins"]) else None
207
+
208
+ def _cors(self) -> None:
209
+ o = self._allowed_origin()
210
+ if o and o not in d.origins: # a hosted site on the allowlist; same-origin needs no headers
211
+ self.send_header("Access-Control-Allow-Origin", o)
212
+ self.send_header("Access-Control-Allow-Private-Network", "true")
213
+ self.send_header("Vary", "Origin")
214
+
215
+ def do_OPTIONS(self):
216
+ if self.headers.get("Host") not in d.hosts or not self._allowed_origin():
217
+ return self._send(403, {"error": "origin not allowed"})
218
+ self.send_response(204)
219
+ self._cors()
220
+ self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
221
+ self.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type")
222
+ self.send_header("Access-Control-Max-Age", "600")
223
+ self.send_header("Content-Length", "0")
224
+ self.end_headers()
225
+
226
+ def _guard(self) -> bool:
227
+ if self.headers.get("Host") not in d.hosts:
228
+ self._send(403, {"error": "bad host"})
229
+ return False
230
+ origin = self.headers.get("Origin")
231
+ if origin and not self._allowed_origin():
232
+ self._send(403, {"error": "cross-origin requests are not accepted"})
233
+ return False
234
+ auth = self.headers.get("Authorization", "")
235
+ if not (auth.startswith("Bearer ") and hmac.compare_digest(auth[7:], d.token)):
236
+ self._send(401, {"error": "unauthorized"})
237
+ return False
238
+ return True
239
+
240
+ def _body(self) -> dict:
241
+ n = int(self.headers.get("Content-Length") or 0)
242
+ if n > 1_000_000:
243
+ raise ValueError("body too large")
244
+ data = json.loads(self.rfile.read(n) or b"{}")
245
+ if not isinstance(data, dict):
246
+ raise ValueError("expected a JSON object")
247
+ return data
248
+
249
+ def do_GET(self):
250
+ u = urlparse(self.path)
251
+ if u.path in STATIC:
252
+ if self.headers.get("Host") not in d.hosts:
253
+ return self._send(403, {"error": "bad host"})
254
+ f = UI / STATIC[u.path]
255
+ try:
256
+ return self._send(200, f.read_bytes(), TYPES.get(f.suffix, "application/octet-stream"))
257
+ except OSError:
258
+ return self._send(404, {"error": "not found"})
259
+ if not u.path.startswith("/v1/"):
260
+ return self._send(404, {"error": "not found"})
261
+ if not self._guard():
262
+ return
263
+ q = {k: v[0] for k, v in parse_qs(u.query).items()}
264
+ parts = [unquote(x) for x in u.path.strip("/").split("/")]
265
+ try:
266
+ if u.path == "/v1/ping":
267
+ return self._send(200, {"ok": True, "version": __version__, "uptime": time.time() - d.started, "home": str(config.home())})
268
+ if u.path == "/v1/projects":
269
+ rows = []
270
+ for key, entry in config.projects().items():
271
+ exists = os.path.isdir(entry["root"])
272
+ row = {"key": key, "root": entry["root"], "name": os.path.basename(entry["root"]), "exists": exists}
273
+ if exists:
274
+ p = open_project(entry["root"])
275
+ steps = p.steps()
276
+ row.update(steps=len(steps), last=steps[-1]["ts"] if steps else None, version=p.version)
277
+ rows.append(row)
278
+ return self._send(200, {"projects": sorted(rows, key=lambda r: -(r.get("last") or 0)), "errors": d.errors[-5:]})
279
+ if u.path == "/v1/settings":
280
+ return self._send(200, {"settings": config.settings(), "defaults": config.DEFAULTS, "home": str(config.home()),
281
+ "port": d.port, "version": __version__})
282
+ if u.path == "/v1/catalog":
283
+ return self._send(200, {"concepts": C.catalog(), "groups": C.GROUPS, "diagrams": render.KINDS})
284
+ if u.path == "/v1/integrations":
285
+ p = by_key(q.get("key", "")) if q.get("key") else None
286
+ root = p.root if p else None
287
+ return self._send(200, {"targets": connect.status(root), "root": root})
288
+ if len(parts) >= 4 and parts[:2] == ["v1", "p"]:
289
+ p = d.project_or_404(parts[2])
290
+ what = parts[3]
291
+ if what == "version":
292
+ return self._send(200, {"version": p.version, "steps": len(p.steps()), "journal": len(p.journal())})
293
+ if what == "state":
294
+ return self._send(200, p.state())
295
+ if what == "diagram" and len(parts) == 5:
296
+ return self._send(200, d.diagram(p, parts[4], q))
297
+ if what == "flow":
298
+ return self._send(200, d.flow(p, q.get("fid", ""), q.get("step", "live")))
299
+ if what == "file":
300
+ return self._send(200, d.file(p, q.get("path", ""), q.get("step", "live")))
301
+ if what == "diff":
302
+ return self._send(200, d.diff(p, q.get("path", ""), q.get("from", "live"), q.get("to", "live")))
303
+ if what == "symbol":
304
+ hit = A.resolve(p.model, q.get("ref", ""))
305
+ node = None
306
+ if hit:
307
+ node = {k: v for k, v in hit[2].items() if k not in ("sha",)}
308
+ node["kind"], node["id"] = hit[0][:-1] if hit[0] != "classes" else "class", hit[1]
309
+ return self._send(200, {"card": brief.symbol_card(p, q.get("ref", "")), "node": node})
310
+ if what == "find":
311
+ return self._send(200, {"results": A.find_symbols(p.model, q.get("q", ""), 30)})
312
+ if what == "brief":
313
+ b = int(q.get("budget") or config.settings()["brief_budget"])
314
+ text = brief.project_brief(p, max(400, min(b, 20000)), q.get("focus") or None)
315
+ return self._send(200, {"text": text, "chars": len(text), "tokens": round(len(text) / 4)})
316
+ if what == "model":
317
+ step = q.get("step", "live")
318
+ model = p.model if step == "live" else (p.load_step(int(step)) or {}).get("model")
319
+ return self._send(200, model or {})
320
+ return self._send(404, {"error": "not found"})
321
+ except LookupError as e:
322
+ return self._send(404, {"error": str(e)})
323
+ except (ValueError, TypeError) as e:
324
+ return self._send(400, {"error": str(e)})
325
+
326
+ def do_POST(self):
327
+ u = urlparse(self.path)
328
+ if self.headers.get("Content-Type", "").split(";")[0] != "application/json":
329
+ return self._send(415, {"error": "json only"})
330
+ if not self._guard():
331
+ return
332
+ parts = [unquote(x) for x in u.path.strip("/").split("/")]
333
+ try:
334
+ body = self._body()
335
+ if u.path == "/v1/projects":
336
+ root = os.path.realpath(os.path.expanduser(str(body.get("root", ""))))
337
+ if not os.path.isdir(root) or root in (os.path.realpath(os.path.expanduser("~")), "/"):
338
+ return self._send(400, {"error": "choose a project folder (not your home directory)"})
339
+ p = open_project(root)
340
+ p.refresh()
341
+ return self._send(200, {"key": p.key, "root": p.root})
342
+ if u.path == "/v1/settings":
343
+ return self._send(200, {"settings": config.save_settings(body)})
344
+ if u.path in ("/v1/connect", "/v1/disconnect"):
345
+ p = by_key(str(body.get("key", "")))
346
+ root = p.root if p else None
347
+ if not root and body.get("scope", "project") == "project":
348
+ return self._send(400, {"error": "pick a project first"})
349
+ fn = connect.install if u.path == "/v1/connect" else connect.uninstall
350
+ files = fn(str(body.get("target", "")), str(body.get("scope", "project")), root)
351
+ return self._send(200, {"ok": True, "files": files, "targets": connect.status(root)})
352
+ if u.path == "/v1/agents-md":
353
+ p = d.project_or_404(str(body.get("key", "")))
354
+ name = body.get("file", "AGENTS.md")
355
+ if name not in ("AGENTS.md", "CLAUDE.md", ".cursorrules", "GEMINI.md"):
356
+ return self._send(400, {"error": "unsupported file"})
357
+ return self._send(200, {"path": connect.write_agents_md(p.root, name)})
358
+ if u.path == "/v1/shutdown":
359
+ self._send(200, {"ok": True})
360
+ threading.Thread(target=d.shutdown, daemon=True).start()
361
+ return
362
+ if len(parts) == 4 and parts[:2] == ["v1", "p"]:
363
+ p = d.project_or_404(parts[2])
364
+ what = parts[3]
365
+ if what == "step":
366
+ data = p.step(str(body.get("title", "")), str(body.get("intent", "")), source="user")
367
+ return self._send(200, {"receipt": brief.step_receipt(data), "n": data["n"]})
368
+ if what == "todo":
369
+ act = body.get("action")
370
+ if act == "add":
371
+ return self._send(200, p.todo_add(str(body.get("text", "")), by="user"))
372
+ if act == "done":
373
+ return self._send(200, p.todo_done(str(body.get("id", "")), str(body.get("evidence", ""))))
374
+ if act == "drop":
375
+ p.todo_drop(str(body.get("id", "")))
376
+ return self._send(200, {"ok": True})
377
+ return self._send(400, {"error": "action must be add, done or drop"})
378
+ if what == "note":
379
+ return self._send(200, p.note(str(body.get("symbol", "")), str(body.get("role", ""))))
380
+ if what == "forget":
381
+ return self._send(200, {"ok": config.unregister(p.key)})
382
+ return self._send(404, {"error": "not found"})
383
+ except LookupError as e:
384
+ return self._send(404, {"error": str(e)})
385
+ except (ValueError, TypeError, OSError) as e:
386
+ return self._send(400, {"error": str(e)})
387
+
388
+ return H