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/infra.py ADDED
@@ -0,0 +1,253 @@
1
+ """Infrastructure and cloud: what the project runs on and talks to, read from the files that describe it.
2
+
3
+ Sources: Dockerfile, docker-compose, Terraform, Kubernetes manifests, serverless.yml, Procfile,
4
+ GitHub Actions, Vercel/Netlify/Fly/Render configs, `.env.example` keys, and the SDKs the code
5
+ imports. Every node records the file it came from.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import os
12
+ import re
13
+
14
+ KINDS = ("app", "container", "function", "db", "cache", "queue", "storage", "search", "external", "ci", "cdn", "cloud")
15
+
16
+ # SDK / driver import -> (node id, label, kind)
17
+ SDKS = {
18
+ "boto3": ("aws", "AWS", "cloud"), "botocore": ("aws", "AWS", "cloud"), "@aws-sdk": ("aws", "AWS", "cloud"), "aws-sdk": ("aws", "AWS", "cloud"),
19
+ "google.cloud": ("gcp", "Google Cloud", "cloud"), "@google-cloud": ("gcp", "Google Cloud", "cloud"), "firebase_admin": ("firebase", "Firebase", "cloud"),
20
+ "firebase": ("firebase", "Firebase", "cloud"), "azure": ("azure", "Azure", "cloud"), "@azure": ("azure", "Azure", "cloud"),
21
+ "psycopg": ("postgres", "PostgreSQL", "db"), "psycopg2": ("postgres", "PostgreSQL", "db"), "asyncpg": ("postgres", "PostgreSQL", "db"), "pg": ("postgres", "PostgreSQL", "db"),
22
+ "pymysql": ("mysql", "MySQL", "db"), "mysql": ("mysql", "MySQL", "db"), "mysql2": ("mysql", "MySQL", "db"),
23
+ "sqlite3": ("sqlite", "SQLite", "db"), "better-sqlite3": ("sqlite", "SQLite", "db"),
24
+ "pymongo": ("mongodb", "MongoDB", "db"), "motor": ("mongodb", "MongoDB", "db"), "mongoose": ("mongodb", "MongoDB", "db"), "mongodb": ("mongodb", "MongoDB", "db"),
25
+ "redis": ("redis", "Redis", "cache"), "ioredis": ("redis", "Redis", "cache"), "aioredis": ("redis", "Redis", "cache"),
26
+ "celery": ("broker", "Task broker", "queue"), "pika": ("rabbitmq", "RabbitMQ", "queue"), "aio_pika": ("rabbitmq", "RabbitMQ", "queue"), "amqplib": ("rabbitmq", "RabbitMQ", "queue"),
27
+ "kafka": ("kafka", "Kafka", "queue"), "aiokafka": ("kafka", "Kafka", "queue"), "confluent_kafka": ("kafka", "Kafka", "queue"), "kafkajs": ("kafka", "Kafka", "queue"),
28
+ "bullmq": ("redis", "Redis", "cache"), "elasticsearch": ("elasticsearch", "Elasticsearch", "search"), "@elastic/elasticsearch": ("elasticsearch", "Elasticsearch", "search"),
29
+ "meilisearch": ("meilisearch", "Meilisearch", "search"), "stripe": ("stripe", "Stripe", "external"), "@stripe": ("stripe", "Stripe", "external"),
30
+ "sendgrid": ("sendgrid", "SendGrid", "external"), "twilio": ("twilio", "Twilio", "external"), "resend": ("resend", "Resend", "external"),
31
+ "anthropic": ("anthropic", "Anthropic API", "external"), "@anthropic-ai": ("anthropic", "Anthropic API", "external"),
32
+ "openai": ("openai", "OpenAI API", "external"), "slack_sdk": ("slack", "Slack", "external"), "@slack": ("slack", "Slack", "external"),
33
+ "supabase": ("supabase", "Supabase", "cloud"), "@supabase": ("supabase", "Supabase", "cloud"), "@prisma/client": ("prisma-db", "Database (Prisma)", "db"),
34
+ }
35
+ # env key -> (node id, label, kind); the value of a URL can refine the label
36
+ ENV = [
37
+ (r"^(DATABASE_URL|DB_URL|POSTGRES\w*|PG\w+)$", ("db", "Database", "db")), (r"^MYSQL\w*$", ("mysql", "MySQL", "db")),
38
+ (r"^(MONGO\w*)$", ("mongodb", "MongoDB", "db")), (r"^REDIS\w*$", ("redis", "Redis", "cache")),
39
+ (r"^(AMQP\w*|RABBIT\w*|BROKER_URL|CELERY_BROKER\w*)$", ("broker", "Message broker", "queue")), (r"^KAFKA\w*$", ("kafka", "Kafka", "queue")),
40
+ (r"^(AWS_\w+|S3_\w+)$", ("aws", "AWS", "cloud")), (r"^(GCP_\w+|GOOGLE_APPLICATION_CREDENTIALS)$", ("gcp", "Google Cloud", "cloud")),
41
+ (r"^AZURE_\w+$", ("azure", "Azure", "cloud")), (r"^STRIPE_\w+$", ("stripe", "Stripe", "external")),
42
+ (r"^(SMTP_\w+|SENDGRID\w*|MAILGUN\w*|POSTMARK\w*|RESEND\w*)$", ("email", "Email provider", "external")),
43
+ (r"^ANTHROPIC_\w+$", ("anthropic", "Anthropic API", "external")), (r"^OPENAI_\w+$", ("openai", "OpenAI API", "external")),
44
+ (r"^SENTRY_\w+$", ("sentry", "Sentry", "external")), (r"^(ELASTIC\w*|OPENSEARCH\w*)$", ("elasticsearch", "Elasticsearch", "search")),
45
+ ]
46
+ _IMAGE_KIND = [(r"postgres|postgis|timescale", "db", "PostgreSQL"), (r"mysql|mariadb", "db", "MySQL"), (r"mongo", "db", "MongoDB"),
47
+ (r"redis|valkey|keydb", "cache", "Redis"), (r"memcached", "cache", "Memcached"), (r"rabbitmq", "queue", "RabbitMQ"),
48
+ (r"kafka|redpanda", "queue", "Kafka"), (r"elasticsearch|opensearch", "search", "Search"), (r"meilisearch|typesense", "search", "Search"),
49
+ (r"minio|localstack", "storage", "Object storage"), (r"nginx|traefik|caddy|envoy|haproxy", "cdn", "Proxy"),
50
+ (r"clickhouse", "db", "ClickHouse"), (r"cassandra|scylla", "db", "Cassandra")]
51
+ _TF_KIND = [(r"_(db_instance|rds_cluster|sql_database_instance|sql_server|postgresql|mysql|dynamodb_table|cosmosdb|firestore|bigtable|spanner)", "db"),
52
+ (r"_(elasticache|memorystore|redis)", "cache"), (r"_(sqs|sns|pubsub|kinesis|eventbridge|servicebus|eventhub|msk|mq_broker)", "queue"),
53
+ (r"_(s3_bucket|storage_bucket|storage_account|storage_container)$", "storage"), (r"_(lambda_function|cloudfunctions|cloud_run|function_app|app_runner)", "function"),
54
+ (r"_(ecs_service|ecs_task|eks|container_cluster|kubernetes|instance|compute_instance|virtual_machine|app_service|beanstalk)", "container"),
55
+ (r"_(cloudfront|cdn|lb|alb|elb|load_balancer|api_gateway|apigateway)", "cdn"), (r"_(opensearch|elasticsearch)", "search")]
56
+
57
+
58
+ def mini_yaml(text: str):
59
+ """A small YAML subset (mappings, lists, scalars, inline [a, b]) for compose/k8s/workflow files when
60
+ PyYAML is not installed. Anchors, multi-line strings and flow mappings are not supported."""
61
+ try:
62
+ import yaml # type: ignore
63
+ docs = [d for d in yaml.safe_load_all(text) if d is not None]
64
+ return docs[0] if len(docs) == 1 else docs
65
+ except ImportError:
66
+ pass
67
+ except Exception:
68
+ return None
69
+
70
+ def scalar(v: str):
71
+ v = v.strip()
72
+ if not v:
73
+ return None
74
+ if v[0] in "\"'" and v[-1] == v[0]:
75
+ return v[1:-1]
76
+ if v.startswith("[") and v.endswith("]"):
77
+ return [scalar(x) for x in v[1:-1].split(",") if x.strip()]
78
+ return v
79
+ docs = []
80
+ for chunk in re.split(r"(?m)^---\s*$", text):
81
+ lines = [(len(ln) - len(ln.lstrip(" ")), ln.strip()) for ln in chunk.splitlines()
82
+ if ln.strip() and not ln.strip().startswith("#")]
83
+ pos = 0
84
+
85
+ def block(indent: int):
86
+ nonlocal pos
87
+ if pos >= len(lines):
88
+ return None
89
+ if lines[pos][1].startswith("- "):
90
+ out = []
91
+ while pos < len(lines) and lines[pos][0] == indent and lines[pos][1].startswith("- "):
92
+ item = lines[pos][1][2:]
93
+ if re.match(r"^[\w.\-\"']+:\s", item + " ") and not item.startswith(("http", "\"", "'")):
94
+ lines[pos] = (indent + 2, item)
95
+ out.append(block(indent + 2))
96
+ else:
97
+ pos += 1
98
+ out.append(scalar(item))
99
+ return out
100
+ out = {}
101
+ while pos < len(lines) and lines[pos][0] == indent and not lines[pos][1].startswith("- "):
102
+ m = re.match(r"^([^:]+?):(?:\s+(.*))?$", lines[pos][1])
103
+ if not m:
104
+ pos += 1
105
+ continue
106
+ key, val = m.group(1).strip().strip("\"'"), m.group(2)
107
+ pos += 1
108
+ if val and val.strip() not in ("|", ">", "|-", ">-"):
109
+ out[key] = scalar(val)
110
+ elif pos < len(lines) and (lines[pos][0] > indent or (lines[pos][0] == indent and lines[pos][1].startswith("- "))):
111
+ out[key] = block(lines[pos][0])
112
+ else:
113
+ out[key] = None
114
+ return out
115
+ d = block(lines[0][0]) if lines else None
116
+ if d is not None:
117
+ docs.append(d)
118
+ return docs[0] if len(docs) == 1 else docs
119
+
120
+
121
+ def detect(root: str, index: dict, read, modules: dict) -> dict:
122
+ """{nodes: [...], edges: [...]} for the infrastructure diagram. `read(rel)` returns file text."""
123
+ nodes: dict[str, dict] = {}
124
+ edges: set[tuple[str, str, str]] = set()
125
+
126
+ def node(nid: str, label: str, kind: str, src: str, detail: str = "", group: str = "") -> str:
127
+ nid = re.sub(r"[^\w-]", "_", nid)[:60]
128
+ n = nodes.setdefault(nid, {"id": nid, "label": label, "kind": kind, "sources": [], "detail": detail, "group": group})
129
+ if src not in n["sources"]:
130
+ n["sources"].append(src)
131
+ if detail and not n["detail"]:
132
+ n["detail"] = detail
133
+ if group and not n["group"]:
134
+ n["group"] = group
135
+ return nid
136
+
137
+ app = None
138
+ if modules:
139
+ app = node("app", os.path.basename(root.rstrip("/")) or "app", "app", "source code",
140
+ f"{len(modules)} modules", "application")
141
+ files = sorted(index)
142
+ for rel in files:
143
+ base = os.path.basename(rel)
144
+ low = rel.lower()
145
+ text = None
146
+ if base in ("Dockerfile", "Containerfile") or base.endswith(".Dockerfile") or base.startswith("Dockerfile."):
147
+ text = read(rel) or ""
148
+ img = re.findall(r"(?im)^\s*FROM\s+(\S+)", text)
149
+ ports = re.findall(r"(?im)^\s*EXPOSE\s+([\d\s/a-z]+)", text)
150
+ cid = node("container_" + (os.path.dirname(rel) or "app"), f"container ({os.path.dirname(rel) or 'root'})", "container", rel,
151
+ ("from " + img[-1] if img else "") + (f" · port {' '.join(ports).strip()}" if ports else ""), "containers")
152
+ if app:
153
+ edges.add((cid, app, "runs"))
154
+ elif re.search(r"(^|/)(docker-)?compose[\w.-]*\.ya?ml$", low):
155
+ doc = mini_yaml(read(rel) or "")
156
+ services = (doc or {}).get("services") if isinstance(doc, dict) else None
157
+ if isinstance(services, dict):
158
+ for name, svc in services.items():
159
+ svc = svc if isinstance(svc, dict) else {}
160
+ image = str(svc.get("image") or "")
161
+ kind, label = "container", name
162
+ for pat, k, lab in _IMAGE_KIND:
163
+ if re.search(pat, image.lower()) or re.search(pat, name.lower()):
164
+ kind, label = k, f"{name} ({lab})"
165
+ break
166
+ ports = svc.get("ports") or []
167
+ detail = (image or ("build " + str(svc.get("build")) if svc.get("build") else "")) + \
168
+ (f" · ports {', '.join(map(str, ports))}" if isinstance(ports, list) and ports else "")
169
+ sid = node("svc_" + name, label, kind, rel, detail, "docker compose")
170
+ deps = svc.get("depends_on") or []
171
+ for d in (deps if isinstance(deps, list) else list(deps) if isinstance(deps, dict) else []):
172
+ edges.add((sid, "svc_" + re.sub(r"[^\w-]", "_", str(d)), "depends on"))
173
+ if svc.get("build") and app:
174
+ edges.add((sid, app, "builds"))
175
+ elif low.endswith(".tf"):
176
+ text = read(rel) or ""
177
+ for prov in re.findall(r'(?m)^\s*provider\s+"([\w-]+)"', text):
178
+ node("tf_provider_" + prov, {"aws": "AWS", "google": "Google Cloud", "azurerm": "Azure"}.get(prov, prov), "cloud", rel, "terraform provider", "terraform")
179
+ for rtype, rname in re.findall(r'(?m)^\s*resource\s+"([\w-]+)"\s+"([\w-]+)"', text):
180
+ kind = next((k for pat, k in _TF_KIND if re.search(pat, rtype)), None)
181
+ if not kind:
182
+ continue
183
+ rid = node(f"tf_{rtype}_{rname}", f"{rname} ({rtype.split('_', 1)[-1].replace('_', ' ')})", kind, rel, rtype, "terraform")
184
+ prov = rtype.split("_")[0]
185
+ pid = {"aws": "tf_provider_aws", "google": "tf_provider_google", "azurerm": "tf_provider_azurerm"}.get(prov)
186
+ if pid:
187
+ edges.add((rid, pid, "on"))
188
+ for a, b in re.findall(r'resource\s+"[\w-]+"\s+"([\w-]+)"[^{]*\{[^}]*?\b[a-z]+_[\w]+\.([\w-]+)\.', text):
189
+ pass # cross-resource references are noisy; providers are enough for the overview
190
+ elif low.endswith((".yaml", ".yml")) and re.search(r"(?m)^kind:\s*(Deployment|StatefulSet|Service|Ingress|CronJob|DaemonSet)\b", read(rel) or ""):
191
+ text = read(rel) or ""
192
+ for kind_, name in re.findall(r"(?ms)^kind:\s*(\w+).*?^\s+name:\s*([\w.-]+)", text):
193
+ k = {"Ingress": "cdn", "CronJob": "function", "StatefulSet": "db"}.get(kind_, "container")
194
+ node(f"k8s_{kind_}_{name}", f"{name} ({kind_})", k, rel, "kubernetes", "kubernetes")
195
+ elif re.search(r"(^|/)serverless\.ya?ml$", low):
196
+ doc = mini_yaml(read(rel) or "") or {}
197
+ for fn in (doc.get("functions") or {}) if isinstance(doc, dict) else {}:
198
+ fid = node("fn_" + fn, f"{fn} (function)", "function", rel, "serverless", "serverless")
199
+ if app:
200
+ edges.add((fid, app, "runs"))
201
+ elif base == "Procfile":
202
+ for proc, cmd in re.findall(r"(?m)^([\w-]+):\s*(.+)$", read(rel) or ""):
203
+ node("proc_" + proc, f"{proc} process", "container", rel, cmd.strip()[:60], "processes")
204
+ elif low.startswith(".github/workflows/") and low.endswith((".yml", ".yaml")):
205
+ text = read(rel) or ""
206
+ m = re.search(r"(?m)^name:\s*(.+)$", text)
207
+ node("ci_" + base.rsplit(".", 1)[0], f"CI: {(m.group(1).strip() if m else base)[:40]}", "ci", rel, "GitHub Actions", "delivery")
208
+ elif base in ("vercel.json", "netlify.toml", "fly.toml", "render.yaml", "app.yaml", "railway.json", "wrangler.toml"):
209
+ label = {"vercel.json": "Vercel", "netlify.toml": "Netlify", "fly.toml": "Fly.io", "render.yaml": "Render",
210
+ "app.yaml": "App Engine", "railway.json": "Railway", "wrangler.toml": "Cloudflare Workers"}[base]
211
+ hid = node("host_" + label.lower().replace(" ", "_").replace(".", ""), label, "cloud", rel, "hosting", "hosting")
212
+ if app:
213
+ edges.add((app, hid, "deployed to"))
214
+ elif re.match(r"(^|.*/)\.env\.(example|sample|template)$", low) or base in (".env.example", ".env.sample", ".env.template"):
215
+ for key, val in re.findall(r"(?m)^\s*([A-Z][A-Z0-9_]+)\s*=\s*(.*)$", read(rel) or ""):
216
+ for pat, (nid, label, kind) in ENV:
217
+ if re.match(pat, key):
218
+ v = val.lower()
219
+ if nid == "db":
220
+ nid, label = next(((i, lab) for s_, i, lab in (("postgres", "postgres", "PostgreSQL"), ("mysql", "mysql", "MySQL"),
221
+ ("sqlite", "sqlite", "SQLite"), ("mongodb", "mongodb", "MongoDB"))
222
+ if s_ in v), (nid, label))
223
+ n_ = node(nid, label, kind, rel, f"env {key}", "services")
224
+ if app:
225
+ edges.add((app, n_, "uses"))
226
+ break
227
+ for mid, mod in modules.items():
228
+ for e in mod.get("ext", []):
229
+ for prefix, (nid, label, kind) in SDKS.items():
230
+ if e == prefix or e.startswith(prefix + ".") or e.startswith(prefix + "/"):
231
+ n_ = node(nid, label, kind, mod["file"], f"imported by {mod['file']}", "services")
232
+ if app:
233
+ edges.add((app, n_, "uses"))
234
+ break
235
+ # merge the same backing service found two ways (e.g. compose `db` with image postgres + psycopg import)
236
+ by_label = {}
237
+ for nid, n in list(nodes.items()):
238
+ if n["kind"] in ("db", "cache", "queue", "search") and nid.startswith("svc_"):
239
+ by_label[n["label"].split("(")[-1].rstrip(")").lower()] = nid
240
+ for nid, n in list(nodes.items()):
241
+ if not nid.startswith(("svc_", "tf_", "k8s_")) and n["kind"] in ("db", "cache", "queue", "search"):
242
+ twin = by_label.get(n["label"].lower())
243
+ if twin:
244
+ nodes[twin]["sources"] += [s for s in n["sources"] if s not in nodes[twin]["sources"]]
245
+ del nodes[nid]
246
+ edges = {(twin if a == nid else a, twin if b == nid else b, lab) for a, b, lab in edges}
247
+ edges = {e for e in edges if e[0] in nodes and e[1] in nodes and e[0] != e[1]}
248
+ return {"nodes": sorted(nodes.values(), key=lambda n: (KINDS.index(n["kind"]) if n["kind"] in KINDS else 99, n["id"])),
249
+ "edges": [{"from": a, "to": b, "label": lab} for a, b, lab in sorted(edges)]}
250
+
251
+
252
+ def summary(infra: dict) -> str:
253
+ return json.dumps({n["id"]: n["kind"] for n in infra.get("nodes", [])})
mapwright/mcp.py ADDED
@@ -0,0 +1,153 @@
1
+ """MCP server (stdio, JSON-RPC 2.0): how any MCP-capable coding agent talks to Mapwright.
2
+
3
+ Tool descriptions and results are deliberately short: every tool definition is sent to the model
4
+ on every turn, so a verbose server costs tokens on each request. Results never include source
5
+ code; they point at file:line so the agent opens only what it needs.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import os
12
+ import sys
13
+ import traceback
14
+
15
+ from . import __version__, brief
16
+ from . import analyze as A
17
+ from .track import open_project
18
+
19
+ VERSIONS = ("2025-06-18", "2025-03-26", "2024-11-05")
20
+ INSTRUCTIONS = ("Mapwright keeps a live map of this project from its code. Start with `map` instead of exploring files. "
21
+ "After each meaningful change call `step` with a short title (what changed is computed, not taken from you). "
22
+ "Track plan items with `todo`. `claim` a mechanism only where the code shows it; claims are verified.")
23
+
24
+
25
+ def _s(props: dict, req: list[str] | None = None) -> dict:
26
+ return {"type": "object", "properties": props, "required": req or [], "additionalProperties": False}
27
+
28
+
29
+ STR = {"type": "string"}
30
+ TOOLS = [
31
+ {"name": "map", "description": "Project map: layers, modules and key symbols, routes, tables, infra, mechanisms, todos. No code.",
32
+ "inputSchema": _s({"focus": {**STR, "description": "path prefix to expand"}, "budget": {"type": "integer", "description": "max chars"}})},
33
+ {"name": "find", "description": "Locate symbols by name. Returns id, kind, file:line.", "inputSchema": _s({"query": STR}, ["query"])},
34
+ {"name": "symbol", "description": "One symbol: signature, callers, callees, tables, routes, history. No code.",
35
+ "inputSchema": _s({"name": STR}, ["name"])},
36
+ {"name": "step", "description": "Record a step after a change. Title only; files and design changes are computed.",
37
+ "inputSchema": _s({"title": STR, "intent": {**STR, "description": "optional: why"}}, ["title"])},
38
+ {"name": "todo", "description": "Plan items. action: add | done | drop | list. done needs evidence (symbol or file you changed).",
39
+ "inputSchema": _s({"action": {"type": "string", "enum": ["add", "done", "drop", "list"]}, "text": STR, "id": STR, "evidence": STR}, ["action"])},
40
+ {"name": "claim", "description": "Tag a symbol with a mechanism id from `catalog` (e.g. caching, retry-backoff). Verified against the code.",
41
+ "inputSchema": _s({"concept": STR, "symbol": STR, "note": STR}, ["concept", "symbol"])},
42
+ {"name": "note", "description": "One-line role of a module or symbol, shown on the map.", "inputSchema": _s({"symbol": STR, "role": STR}, ["symbol", "role"])},
43
+ {"name": "link", "description": "A relation static analysis cannot see (queue, event, HTTP between services).",
44
+ "inputSchema": _s({"from": STR, "to": STR, "label": STR}, ["from", "to"])},
45
+ {"name": "catalog", "description": "Concept ids usable in `claim`.", "inputSchema": _s({})},
46
+ ]
47
+
48
+
49
+ def call(project, name: str, args: dict) -> str:
50
+ p = project
51
+ p.refresh()
52
+ if name == "map":
53
+ b = args.get("budget")
54
+ return brief.project_brief(p, int(b) if isinstance(b, int) and 400 <= b <= 20000 else _budget(), args.get("focus"))
55
+ if name == "find":
56
+ rows = A.find_symbols(p.model, str(args.get("query", "")), 15)
57
+ return "\n".join(f"{r['id']} ({r['kind']}) {r['file']}:{r['line'] or ''}" for r in rows) or "no match"
58
+ if name == "symbol":
59
+ return brief.symbol_card(p, str(args.get("name", "")))
60
+ if name == "step":
61
+ return brief.step_receipt(p.step(str(args.get("title", "")), str(args.get("intent", "") or "")))
62
+ if name == "todo":
63
+ act = args.get("action")
64
+ if act == "add":
65
+ ev = p.todo_add(str(args.get("text", "")))
66
+ return f"added {ev['tid']}"
67
+ if act == "done":
68
+ t = p.todo_done(str(args.get("id", "")), str(args.get("evidence", "") or ""))
69
+ return f"{t['id']} done · {'verified' if t.get('verified') else 'unverified'}: {t.get('reason', '')}"
70
+ if act == "drop":
71
+ p.todo_drop(str(args.get("id", "")))
72
+ return "dropped"
73
+ todos = p.verify()["todos"]
74
+ return "\n".join(f"{t['id']} [{t['status']}{'' if t['status'] != 'done' else ', verified' if t.get('verified') else ', unverified'}] {t['text']}"
75
+ for t in todos) or "no todos"
76
+ if name == "claim":
77
+ c = p.claim(str(args.get("concept", "")), str(args.get("symbol", "")), str(args.get("note", "") or ""))
78
+ return f"{c['concept']} → {c['symbol']}: {c['status']} ({c.get('reason', '')})"
79
+ if name == "note":
80
+ n = p.note(str(args.get("symbol", "")), str(args.get("role", "")))
81
+ return f"noted ({n['status']})"
82
+ if name == "link":
83
+ ln = p.link(str(args.get("from", "")), str(args.get("to", "")), str(args.get("label", "") or ""))
84
+ return f"linked ({ln['status']})"
85
+ if name == "catalog":
86
+ return brief.catalog_text()
87
+ raise ValueError(f"unknown tool {name}")
88
+
89
+
90
+ def _budget() -> int:
91
+ from . import config
92
+ return config.settings()["brief_budget"]
93
+
94
+
95
+ def handle(project, msg: dict) -> dict | None:
96
+ mid, method = msg.get("id"), msg.get("method")
97
+ if method is None:
98
+ return None # a response to something we never send
99
+ if mid is None:
100
+ return None # notification (notifications/initialized, cancelled …)
101
+ try:
102
+ if method == "initialize":
103
+ want = (msg.get("params") or {}).get("protocolVersion")
104
+ result = {"protocolVersion": want if want in VERSIONS else VERSIONS[0],
105
+ "capabilities": {"tools": {"listChanged": False}},
106
+ "serverInfo": {"name": "mapwright", "version": __version__}, "instructions": INSTRUCTIONS}
107
+ elif method == "ping":
108
+ result = {}
109
+ elif method == "tools/list":
110
+ result = {"tools": TOOLS}
111
+ elif method == "tools/call":
112
+ params = msg.get("params") or {}
113
+ name, args = params.get("name"), params.get("arguments") or {}
114
+ if name not in {t["name"] for t in TOOLS}:
115
+ return {"jsonrpc": "2.0", "id": mid, "error": {"code": -32602, "message": f"unknown tool {name}"}}
116
+ try:
117
+ text, err = call(project, name, args if isinstance(args, dict) else {}), False
118
+ except (ValueError, KeyError) as e:
119
+ text, err = f"error: {e}", True
120
+ result = {"content": [{"type": "text", "text": text}], "isError": err}
121
+ elif method in ("resources/list", "prompts/list"):
122
+ result = {method.split("/")[0]: []}
123
+ else:
124
+ return {"jsonrpc": "2.0", "id": mid, "error": {"code": -32601, "message": f"method not found: {method}"}}
125
+ except Exception as e: # never kill the agent's session over one bad call
126
+ traceback.print_exc(file=sys.stderr)
127
+ return {"jsonrpc": "2.0", "id": mid, "error": {"code": -32603, "message": f"internal error: {type(e).__name__}: {e}"}}
128
+ return {"jsonrpc": "2.0", "id": mid, "result": result}
129
+
130
+
131
+ def serve(root: str | None = None, stdin=None, stdout=None) -> int:
132
+ stdin = stdin or sys.stdin
133
+ stdout = stdout or sys.stdout
134
+ root = root or os.environ.get("MAPWRIGHT_ROOT") or os.getcwd()
135
+ from . import config
136
+ config.set_start(root)
137
+ project = open_project(root)
138
+ for line in stdin:
139
+ line = line.strip()
140
+ if not line:
141
+ continue
142
+ try:
143
+ msg = json.loads(line)
144
+ except ValueError:
145
+ out = {"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "parse error"}}
146
+ else:
147
+ batch = msg if isinstance(msg, list) else [msg]
148
+ outs = [r for r in (handle(project, m) for m in batch if isinstance(m, dict)) if r is not None]
149
+ out = outs if isinstance(msg, list) else (outs[0] if outs else None)
150
+ if out is not None:
151
+ stdout.write(json.dumps(out, separators=(",", ":")) + "\n")
152
+ stdout.flush()
153
+ return 0
mapwright/noise.py ADDED
@@ -0,0 +1,80 @@
1
+ """Noise classifier: which changed paths are lockfiles, build output, generated shards or tool config.
2
+
3
+ Noisy changes are still tracked in a step, but they are collapsed into one line and never count
4
+ towards `files changed`, and a turn that only touched noisy files does not warrant an auto step.
5
+ Pure functions, stdlib only.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import posixpath
11
+ import re
12
+ from typing import Callable, Iterable
13
+
14
+ LOCKFILES = {
15
+ "package-lock.json", "npm-shrinkwrap.json", "pnpm-lock.yaml", "yarn.lock", "bun.lockb", "bun.lock", "poetry.lock",
16
+ "uv.lock", "pipfile.lock", "cargo.lock", "gemfile.lock", "composer.lock", "go.sum", "pdm.lock", "mix.lock",
17
+ "podfile.lock", "flake.lock", "packages.lock.json", "gradle.lockfile",
18
+ }
19
+ NOISE_DIRS = {
20
+ "node_modules", "dist", "build", "out", ".next", ".nuxt", ".svelte-kit", ".turbo", ".cache", ".parcel-cache",
21
+ "coverage", "__pycache__", ".pytest_cache", ".mypy_cache", ".ruff_cache", "target", ".gradle", ".venv", "venv",
22
+ ".mapwright", ".mapwright-venv", ".idea", ".firebase", "shards", "__generated__", "generated",
23
+ }
24
+ _FILE_PATTERNS = re.compile(
25
+ r"(\.min\.(js|css)$|\.map$|\.bundle\.js$|\.chunk\.js$|\.pyc$|\.log$|\.tsbuildinfo$|\.generated\.\w+$|"
26
+ r"_pb2(_grpc)?\.py$|\.pb\.go$|\.snap$|(^|[-_.])shard[-_.]?\d+|[-_.]part[-_.]?\d{3,}\.\w+$|^\.ds_store$)",
27
+ re.I)
28
+ LARGE_DATA_LINES = 2000 # a .json/.jsonl/.csv change bigger than this (added+removed lines) is data, not design
29
+
30
+
31
+ def classify(path: str, lines_changed: int | None = None, is_ignored: Callable[[str], bool] | None = None) -> str | None:
32
+ """Why `path` is noise ('lockfile', 'generated', 'tool config', 'data', 'ignored'), or None."""
33
+ p = path.replace("\\", "/")
34
+ while p.startswith("./"):
35
+ p = p[2:]
36
+ base = posixpath.basename(p).lower()
37
+ if base in LOCKFILES:
38
+ return "lockfile"
39
+ parts = [s.lower() for s in p.split("/")[:-1]]
40
+ if base == ".mcp.json" or ".claude" in parts:
41
+ return "tool config"
42
+ if any(s in NOISE_DIRS for s in parts) or _FILE_PATTERNS.search(base):
43
+ return "generated"
44
+ if lines_changed is not None and lines_changed >= LARGE_DATA_LINES and base.endswith((".json", ".jsonl", ".csv", ".ndjson")):
45
+ return "data"
46
+ fn = is_ignored if is_ignored is not None else default_is_ignored()
47
+ if fn is not None:
48
+ try:
49
+ if fn(p):
50
+ return "ignored"
51
+ except Exception:
52
+ pass
53
+ return None
54
+
55
+
56
+ def default_is_ignored() -> Callable[[str], bool] | None:
57
+ """`mapwright.ignore.is_ignored(path)` when that module exists (gitignore-aware scanning plugs in here)."""
58
+ try:
59
+ from . import ignore as _ig
60
+ except ImportError:
61
+ return None
62
+ fn = getattr(_ig, "is_ignored", None)
63
+ return fn if callable(fn) else None
64
+
65
+
66
+ def split(changes: Iterable[dict], is_ignored: Callable[[str], bool] | None = None) -> tuple[list[dict], list[dict]]:
67
+ """(meaningful, noisy) changes. Each noisy change is a copy with a `noise` reason key."""
68
+ real, noisy = [], []
69
+ for c in changes:
70
+ n = (c.get("added") or 0) + (c.get("removed") or 0) if ("added" in c or "removed" in c) else None
71
+ why = classify(c["path"], n, is_ignored)
72
+ if why:
73
+ noisy.append({**c, "noise": why})
74
+ else:
75
+ real.append(c)
76
+ return real, noisy
77
+
78
+
79
+ def collapse_line(noisy: list[dict]) -> str:
80
+ return f"generated/lockfile ({len(noisy)} file{'s' if len(noisy) != 1 else ''})"
mapwright/redact.py ADDED
@@ -0,0 +1,39 @@
1
+ """Redaction applied before anything is stored or displayed."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ _PATTERNS = [
8
+ (re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----.*?(?:-----END [A-Z ]*PRIVATE KEY-----|\Z)", re.S), "⟨private-key⟩"),
9
+ (re.compile(r"fwk_live_[A-Za-z0-9]{6,}"), "⟨canary⟩"),
10
+ (re.compile(r"sk-ant-[A-Za-z0-9_\-]{10,}"), "⟨anthropic-key⟩"),
11
+ (re.compile(r"\b[sr]k_(?:live|test)_[A-Za-z0-9]{16,}"), "⟨stripe-key⟩"),
12
+ (re.compile(r"sk-[A-Za-z0-9_\-]{20,}"), "⟨api-key⟩"),
13
+ (re.compile(r"gh[pousr]_[A-Za-z0-9]{20,}"), "⟨github-token⟩"),
14
+ (re.compile(r"github_pat_[A-Za-z0-9_]{20,}"), "⟨github-token⟩"),
15
+ (re.compile(r"glpat-[A-Za-z0-9_\-]{16,}"), "⟨gitlab-token⟩"),
16
+ (re.compile(r"A[KS]IA[0-9A-Z]{16}"), "⟨aws-key⟩"),
17
+ (re.compile(r"AIza[0-9A-Za-z_\-]{30,}"), "⟨google-key⟩"),
18
+ (re.compile(r"xox[abprs]-[A-Za-z0-9\-]{10,}"), "⟨slack-token⟩"),
19
+ (re.compile(r"\beyJ[A-Za-z0-9_\-]{8,}\.eyJ[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}"), "⟨jwt⟩"),
20
+ (re.compile(r"(?i)\b(bearer|basic)\s+[A-Za-z0-9._\-+/=]{16,}"), r"\1 ⟨token⟩"),
21
+ (re.compile(r"(https?://)[^/\s:@]+:[^/\s@]+@"), r"\1⟨user:pass⟩@"),
22
+ # NAME=value anywhere on a line (after `export`, or as a prefix to a command)
23
+ (re.compile(r"(?i)(?<![A-Za-z0-9_])([A-Z0-9_]*(?:KEY|SECRET|TOKEN|PASSWORD|PASSWD|PASS|CREDENTIALS?)[A-Z0-9_]*\s*=\s*)(?!⟨)(\"[^\"]*\"|'[^']*'|[^\s;&|]+)"), r"\1⟨redacted⟩"),
24
+ # "password": "value" and password: value
25
+ (re.compile(r"(?i)([\"']?[a-z0-9_\-]*(?:password|passwd|secret|api[_\-]?key|token)[\"']?\s*:\s*)(?!⟨)(\"[^\"]*\"|'[^']*'|[^\s,;}]+)"), r"\1⟨redacted⟩"),
26
+ # --password value, --token=value
27
+ (re.compile(r"(?i)(--?(?:password|passwd|token|secret|api-key|apikey)[= ]\s*)(?!⟨)(\"[^\"]*\"|'[^']*'|\S+)"), r"\1⟨redacted⟩"),
28
+ ]
29
+
30
+
31
+ def redact(text: str, canaries: dict[str, str] | None = None) -> str:
32
+ if not text:
33
+ return text
34
+ for value, cid in (canaries or {}).items():
35
+ if value:
36
+ text = text.replace(value, f"⟨canary:{cid}⟩")
37
+ for pat, rep in _PATTERNS:
38
+ text = pat.sub(rep, text)
39
+ return text