Real-time codebase architecture analysis and visualization for ProvenMap Portal. Analyzes codebases across multiple languages, detects tech stacks, identifies…

The pmap-code plugin — codebase architecture analysis for ProvenMap Portal. Discovers your project's components, classifies them against your org's archetype catalogue, maps relationships, and syncs the result as a layered architecture board you can review and share.
ProvenMap Code runs in Claude Code. It also grounds a board in a repo's documents with /ground — the ADRs and design docs beside the code, or a repo that holds only documents and grounds a board an architect authored.
Add the ProvenMap marketplace once, then install the plugin:
/plugin marketplace add provenmap/pmap-claude
/plugin install pmap-code@provenmap
Restart Claude Code so the commands load. Scope the install with --scope user (default), --scope project, or --scope local. Claude Cowork installs from the same marketplace.
pmap-codeis the install id — everywhere else it's ProvenMap Code.
Two ways to connect this repo to a board — both end with the credential pair in .provenmap/credentials.json (owner-read-only) and the settings in .provenmap/config.json:
Browser login (fastest): run /login. It signs you in through the ProvenMap portal, lets you pick a workspace and a board that already has a Code Plugin binding, and writes both files for you — no tokens to copy. Re-running /login while connected just confirms the connection; /login switch (or /configure's "change board" option) binds the project to a different board, and /logout disconnects it (clears the local credentials).
Manual: get a Binding Token and API Secret from the ProvenMap Portal (Sources → Add Source → Board Builder — saving generates the secret), then create two files under .provenmap/ at your repo root. credentials.json holds the pair (/login writes it with mode 0600; do the same by hand):
{
"bindingToken": "your-base64url-binding-token",
"apiSecret": "pmap_cp_live_your_api_secret"
}
config.json holds the settings — a credential field left here is ignored:
{
"boardSlug": "my-project-overview",
"branch": "main",
"baseUrl": "https://platform.provenmap.com/api"
}
| Field | File | Required | Notes |
|---|---|---|---|
bindingToken | credentials.json | yes | Base64url workspaceId::bindingId from the portal. Sent as X-CodePlugin-Token. |
apiSecret | credentials.json | yes | Starts with pmap_cp_live_ (pmap_cp_test_ from a non-production platform; secrets issued earlier start ck_cp_live_ and keep working). Sent as X-CodePlugin-Secret. |
boardSlug | config.json | yes | Target board. /configure can discover and write it for you. |
branch | config.json | yes | Must match the binding, or /sync returns 400 Branch Mismatch. |
baseUrl | config.json | no | Override for self-hosted (default https://platform.provenmap.com/api). |
excludePaths | config.json | no | Paths skipped during analysis (default node_modules, dist, .git, coverage). |
includeTests | config.json | no | Include test files in analysis (default false). |
includeSourceReferences | config.json | no | Attach file-path references to synced nodes/edges (default true). |
analysis.minorFiles | config.json | no | How small helper files land on drill-down boards (default all): a file of at most analysis.minorMaxLines lines that other files import and that exports no class folds into the node that uses it instead of becoming its own node. one-host folds only files with a single importer; off disables the fold. |
analysis.minorMaxLines | config.json | no | Line cap for that fold (default 100). Files above it are always their own candidates. |
Run /configure to validate both files, test the connection, and add .provenmap/ to your .gitignore. Credentials live only in credentials.json — never logged, and sent only to platform.provenmap.com.
Testing against a non-production server: set
PMAP_BASE_URL(or pass--base-urlto the CLI scripts) to point every command — including/login's device handshake — at a staging or local API. The browser login and app URLs then follow from that server's configuration, so nothing is pinned to production. A successful/loginwrites the URL it ran against into.provenmap/config.json, so the repo stays on that server without the env var; when both are set,PMAP_BASE_URLwins.
/start — the guided front door: the surfaces card, the ranked next step for this repo's real state, and a menu to run it. Every command closes with an Outcome — what it did, what it left, the next move and why — written from a script brief of this repo's real state./login (browser) or /configure (manual) — connect to the portal (one-time per repo)/analyze — full architecture analysis (incremental — only changed files re-analyzed)/sync — push the analysis to your board/ground — link the repo's ADRs, RFCs and design docs to the board's nodes (or, in a documents-only repo, mirror the architect-authored board and ground it)/insights — run server-defined analyses (security, performance, etc.) against the board/status — see what's analyzed, synced and grounded/analyze-archetypes is optional — see Archetypes.
Automatically detects projects in 7+ languages via their manifest files:
| Language | Manifest | Common frameworks |
|---|---|---|
| JS/TypeScript | package.json | Next.js, NestJS, Express, React, Angular, Vue |
| Python | requirements.txt, pyproject.toml, Pipfile | Django, FastAPI, Flask, Celery |
| Java | pom.xml, build.gradle | Spring Boot, Quarkus, Micronaut |
| Go | go.mod | Gin, Echo, Fiber, gRPC |
| C# / .NET | *.csproj, *.sln | ASP.NET Core, Blazor, EF |
| Ruby | Gemfile | Rails, Sinatra, Sidekiq |
| Rust | Cargo.toml | Actix, Axum, Rocket |
Polyglot projects produce a unified board with cross-language relationships (HTTP, gRPC, message queues).
| Command | Description | ||
|---|---|---|---|
/start | Start here — the surfaces card, the ranked next step, and a menu to run it | ||
/login | Browser sign-in: pick a workspace + bound board, writes config automatically | ||
/configure | Set up portal credentials manually, or switch to a different board | ||
/analyze [path] | Analyze codebase architecture (incremental; re-analyzes only changed files) | ||
/analyze --clean | Full re-analysis from scratch, ignoring existing board data | ||
/analyze --drill <board>/<node> | Drill into a node to produce a child layer board | ||
/analyze --all | Re-analyze every layer board in the manifest | ||
/analyze --scheduled | Unattended upkeep for a host schedule: refresh the boards whose files changed, then sync them (the recurring-runs reference in the provenmap-integration skill has the recipe) | ||
/sync [--board <slug>] | Push analysis to portal (smart diff: only changed elements) | ||
/sync --all | Push every board in the manifest | ||
/ground [--board <slug>] | Ground the board in this repo's documents: mirror the authored board (or read the analysed, pushed one), propose and push node↔document evidence links, report drift | ||
/insights | List available insight skills and run one against the current board | ||
/insights <skill-slug> | Run a specific insight skill directly | ||
/insights --all | Run every available insight skill | ||
/skills [--status] | Compile the platform's skill bundle (specs + guidelines) into the repo — never overwrites local edits | ||
/build [--plan] | Build the app from the platform's spec — compiled skills, work items, board design, aspect contracts (write-capable; --plan = plan only) | ||
/work-items | Pull architect-authored work items for the board and pick one to implement | ||
/work-items <workItemId> | Claim, implement, verify, and resolve a specific work item (write-capable — edits project files) | ||
/discover [count] [--auto] [--lens …] [--board <slug>] [focus] | Discover the insights and context boards worth showing — ranked by the graph, picked by you or chosen for you, authored in parallel, pushed to ProvenMap | ||
| `/aspects [--aspect <kind> \ | --db \ | --api]` | Extract a code aspect (database schema, API surface, frontend pages, event catalog) onto the bound board |
/monitor · /monitor setup | Correlate monitoring signals (errors, logs, cloud costs) with the board and push findings as a draft insight; setup configures sources + a recurring run | ||
/status | Show the lifecycle dial, config state, analysis summary, and per-board sync status | ||
/help | List commands grouped by lifecycle stage, with the plugin version | ||
/update | Update this plugin to the latest published version for your host | ||
/analyze-archetypes | Advanced — customize the archetype vocabulary: scan for gaps, submit proposals for admin review | ||
/analyze-archetypes --dry-run | Validate scan locally + ask server to dry-run, don't persist or POST | ||
/analyze-archetypes --skip-submit | Write the proposals file for manual review; don't POST | ||
/analyze-archetypes --replace | When submitting, send mode='replace' to overwrite pending payload |
/ground keeps a board honest against the documents that back it. It has two shapes:
/analyze built. After /sync has pushed the board, /ground reads the local board, inventories the repo's documents (ADRs, RFCs, READMEs, design docs) and proposes an evidence link wherever a document substantiates a node — anchor and quoted excerpt included. The analysed board is never overwritten./ground mirrors it locally (so /insights and /discover can read it), inventories the corpus and links the documents to its nodes. There is no /analyze phase — grounding is the whole lifecycle, and /start routes there.Either way the push replaces the binding's evidence set on the server, and a later run reports drift — a linked document changed or vanished — so a citation never goes silently stale. A run whose pull finds nothing drifted, missing or unlinked closes without proposing anything, which makes a scheduled /ground cheap (the recurring-runs reference in the provenmap-integration skill has the surfaces).
Supported document formats: Markdown/MDX (.md, .mdx, .markdown), reStructuredText (.rst), AsciiDoc (.adoc, .asciidoc), plain text (.txt), HTML wiki exports (.html), PDF (.pdf, extractable text). The walk covers the whole repo, skipping dot-directories, node_modules and any configured excludePaths.
For non-trivial codebases, /analyze produces a hierarchy of boards so you can navigate from a 10–30 node overview down to component-level detail without overwhelming any single view:
| Layer | Name | Scope | Target node count |
|---|---|---|---|
| L0 | System Context | This system's deployables + the outside systems they talk to | 10–30 |
| L1 | Domain | Domain or workspace drill-down | 10–40 per board |
| L2 | Component | Service or module drill-down | 5–20 per board |
| L3+ | Detail | Deep internals, as deep as the code demands — to L4 by default (analysis.plan.maxDepth) | 5–15 per board |
Board hierarchy and per-board sync state live in .provenmap/boards/:
manifest.json — every board's slug, layer, parent, and analysis state<board-slug>.json — analysis output (nodes + edges) for that boardstores/<board-slug>.store.json — sync state, content hashes, last pushComponents are classified using archetypes defined on your ProvenMap server, not a fixed list shipped with the plugin. /analyze fetches the current catalogue via /code-plugin/archetypes and the architecture-analyzer agent assigns each discovered node a valid archetype name.
Where your codebase has a pattern the catalogue has no good name for, /analyze types it with the closest available archetype, records the gap in the board's metadata.archetypeGaps, and names it once at the end of the run. Nothing blocks — the board is complete either way.
Acting on that is optional. /analyze-archetypes scans for the same gaps and submits proposals (new archetypes, or improvements to existing ones) for admin review; once approved, /analyze --clean retypes the affected components.
Set this in .provenmap/config.json to restore the old two-phase workflow, where /analyze stops and prompts whenever the archetype lock is missing, stale, or was skipped:
{ "analysis": { "archetypeGate": "strict" } }
Remove the key to go back to the optional default.
Open the ProvenMap UI to see the current archetype catalogue — the plugin fetches it automatically during /analyze.
Analysis output at .provenmap/boards/<board-slug>.json:
{
"metadata": {
"analyzedAt": "2026-05-13T10:30:00Z",
"analyzedAtCommit": "abc1234",
"projectName": "my-project",
"languages": ["python", "typescript"],
"techStacks": ["fastapi", "react"],
"layer": 0
},
"nodes": [
{
"slug": "user-service",
"name": "User Service",
"type": "<server-archetype-name>",
"description": "User management service",
"path": "src/services/user_service.py",
"parentSlug": "backend-domain",
"metadata": { "language": "python", "framework": "fastapi" }
}
],
"edges": [
{
"sourceSlug": "user-service",
"targetSlug": "user-repository",
"type": "<server-edge-archetype-name>",
"metadata": { "importPath": "from .repository import UserRepository" }
}
]
}
The type field holds the server archetype name assigned by the analyzer. Edge type values come from the edge-archetype list returned by the same endpoint.
| Agent | Role |
|---|---|
| architecture-analyzer | Multi-language structure analysis, archetype classification, domain grouping, hierarchy building |
| relationship-detector | Imports, DB operations, HTTP/gRPC calls, message queue patterns across languages |
/analyze orchestrates both agents. They can also be invoked directly from Claude Code's subagent picker to debug a specific analysis step.
git diff for change detection)BUSL-1.1
hooks/pmap-guard.js 302 lines1// ProvenMap guard: a Claude Code mod (hooks.json "modules"), shipped as authored.
2//
3// It enforces two rules the shipped content only states in prose, at the tool call:
4// 1. Secrets never enter the chat. No Read, Grep or shell command touches the
5// ProvenMap credential files or the PMAP_* secret variables, and any ProvenMap
6// secret in what a tool returns (a credential pasted into config.json, an MCP
7// entry in a host config) reaches Claude masked to its prefix.
8// 2. Script-owned state is written only by the scripts. No Edit, Write or shell
9// write touches the element and evidence stores, the board manifest or the
10// tree plan, whose hashes and records the scripts own.
11// 3. The workspace (root) board is drawn only from a plan the architect approved.
12// A create_nodes / create_edges on root goes through only after a
13// `pmap-architect.js --render-plan` succeeded and the architect answered its
14// go-ahead (the AskUserQuestion offering "Draw it", its preview showing the
15// rendered sketch): "Draw it" approves everything that plan shows; "Let me
16// pick" approves what it confirmed plus the suggestions they tick in the pick
17// rounds that follow. A new render, failed or not, revokes the approval.
18//
19// Claude Code only: Codex and Cursor have no mods, so there the prose is the rule.
20// A user-scope plugin's mod runs in every session, ProvenMap repo or not, so each
21// rule needs a `.provenmap/` path or a PMAP_ name before it fires. Every deny tells
22// Claude what to run instead. A rule that throws lets the call through (fails open),
23// like the settings-hook scripts; the output is masked either way (see `recover`).
24
25const SECRET_FILE =
26 /(^|\/)\.provenmap\/(credentials\.json|login-state\.json|login-state(\/|$)|architect-mcp\.json)/;
27const SECRET_ENV = /\bPMAP_(API_SECRET|BINDING_TOKEN)\b/;
28// Every ProvenMap secret names its family up front: pmap_<kind>_<env>_<body> (cp, mcp,
29// sess, share, app, …), or the older ck_[<kind>_]live_<body>. This is the platform's
30// own redaction pattern (its credential registry): loose enough for a truncated
31// secret, while a bare prefix (`pmap_cp_live_…`) or a short fixture stays readable.
32const SECRET_VALUE = /\b((?:pmap|ck)_(?:[a-z]+_)?(?:live|test)_)[0-9A-Za-z_-]{16,}/g;
33const SCRIPT_OWNED =
34 /(^|\/)\.provenmap\/(boards\/manifest\.json|boards\/stores\/[^/]+\.(store|evidence)\.json|tree-plan\.json|plan-run\.json)$/;
35
36// A Grep or shell read rooted at these directories (or a glob in them) reads the
37// credential files inside them.
38const SECRET_DIR = /(^|\/)\.provenmap(\/login-state)?\/?(\*[^/]*)?$/;
39
40// Programs that can name a credential path without printing the file.
41const SAFE_PROGRAM = /^(ls|stat|test|\[|chmod|find|mkdir|cd|touch|echo|printf|true|false)$/;
42const SECRET_NAME = /credentials\.json|login-state|architect-mcp\.json/;
43
44function normalise(p) {
45 return typeof p === "string" ? p.replace(/\\/g, "/") : "";
46}
47
48function unquote(word) {
49 return normalise(word.replace(/^["']|["']$/g, ""));
50}
51
52function shellSegments(command) {
53 return command.split(/&&|\|\||[;|\n]/);
54}
55
56function secretDeny(target) {
57 const architect = /architect-mcp\.json/.test(target);
58 return [
59 `ProvenMap guard: ${target} holds a ProvenMap credential, and credentials never enter the chat.`,
60 architect
61 ? "Run /pmap-workspace:status to check the token, or /pmap-workspace:login to replace it."
62 : "Run /pmap-code:configure to check the credentials (its script reports each field's shape and the masked secret), or /pmap-code:login to replace them. The user edits the file by hand, never through the chat.",
63 ].join(" ");
64}
65
66function scriptOwnedDeny(target) {
67 const store = /\.(store|evidence)\.json$/.test(target);
68 return [
69 `ProvenMap guard: ${target} is written only by the ProvenMap scripts, and a hand edit breaks the record they verify.`,
70 store
71 ? "Run /pmap-code:sync to update the element store, or /pmap-code:ground for the evidence store."
72 : "Change the board files instead and let the scripts rewrite it; run /pmap-code:analyze to rebuild the plan (--clean starts over).",
73 ].join(" ");
74}
75
76function mentionsSecret(text) {
77 return (
78 (/\.provenmap/.test(normalise(text)) && SECRET_NAME.test(text)) ||
79 text.split(/[\s;&|<>()`]+/).some((w) => SECRET_DIR.test(unquote(w)))
80 );
81}
82
83// A shell command that could print a credential: a substitution around one, or a
84// segment naming one whose program reads files (or reads it through `<`). Once the
85// command names .provenmap, a bare file name counts too (`cd .provenmap && cat …`).
86function shellReadsSecret(command) {
87 if (!mentionsSecret(command)) return false;
88 if (/`|\$\(/.test(command)) return true;
89 return shellSegments(command).some((segment) => {
90 if (!SECRET_NAME.test(segment) && !mentionsSecret(segment)) return false;
91 const [program, ...args] = segment.trim().split(/\s+/);
92 if (program === "git") return args[0] !== "check-ignore";
93 return !SAFE_PROGRAM.test(program) || segment.includes("<");
94 });
95}
96
97// A shell write into a script-owned file: a redirect target, or an argument of a
98// program that changes the files it names (cp only writes its last one).
99function shellWriteTarget(command) {
100 for (const m of command.matchAll(/>>?\s*["']?([^\s;&|"'<>]+)/g)) {
101 if (SCRIPT_OWNED.test(normalise(m[1]))) return normalise(m[1]);
102 }
103 for (const segment of shellSegments(command)) {
104 const [program, ...args] = segment.trim().split(/\s+/).map(unquote);
105 const inPlace = /^(sed|perl)$/.test(program) && args.some((a) => /^-[a-zA-Z]*i/.test(a));
106 const targets =
107 program === "cp" ? args.slice(-1) : ["rm", "mv", "tee", "truncate"].includes(program) || inPlace ? args : [];
108 const hit = targets.find((w) => SCRIPT_OWNED.test(w));
109 if (hit) return hit;
110 }
111 return null;
112}
113
114// The go-ahead's answers — the labels landscape-plan.ts renders (a test pins them).
115const DRAW_ALL = "Draw it";
116const PICK = "Let me pick";
117const ROOT_DRAW = /^mcp__.*provenmap.*__(create_nodes|create_edges)$/i;
118
119function parseJson(text) {
120 if (typeof text !== "string") return null;
121 try {
122 return JSON.parse(text);
123 } catch {
124 const m = text.match(/\{[\s\S]*\}/);
125 try {
126 return m ? JSON.parse(m[0]) : null;
127 } catch {
128 return null;
129 }
130 }
131}
132
133const edgeKey = (e) => `${e?.sourceSlug ?? ""}>${e?.targetSlug ?? ""}`;
134
135/**
136 * Rule 3's memory for one session: the last plan --render-plan rendered and what
137 * the architect's answers approved — the confirmed elements, the suggestions kept
138 * (all on "Draw it", the ticked ones on "Let me pick"), and the connections whose
139 * suggested ends were all kept.
140 * `observe` reads each finished call; `check` is the deny text for a root draw,
141 * or null. Exported for the plugin repo's tests.
142 */
143export function planGate() {
144 let rendered = null;
145 let approved = null;
146 const approve = (keptLabels) => {
147 const kept = rendered.suggestions.filter((s) => keptLabels.has(s.label));
148 const dropped = new Set(rendered.suggestions.filter((s) => s.node && !keptLabels.has(s.label)).map((s) => s.node));
149 const clear = (pair) => pair.split(">").every((slug) => !dropped.has(slug));
150 return {
151 nodes: new Set([...rendered.nodes, ...kept.filter((s) => s.node).map((s) => s.node)]),
152 edges: new Set([
153 ...rendered.edges,
154 ...rendered.conditional.filter(clear),
155 ...kept.filter((s) => s.edge && clear(s.edge)).map((s) => s.edge),
156 ]),
157 };
158 };
159 // After "Let me pick": each pick question's ticked labels, by question (a re-ask replaces its answer).
160 let picks = null;
161 return {
162 observe(tool, input, outcome) {
163 if ((tool === "Bash" || tool === "PowerShell") && /--render-plan\b/.test(String(input?.command ?? ""))) {
164 rendered = null;
165 approved = null;
166 picks = null;
167 const out = outcome && !outcome.deny ? parseJson(outcome.text) : null;
168 if (out?.success && Array.isArray(out.goAhead?.questions) && out.approval)
169 rendered = {
170 preview: out.goAhead.questions[0]?.options?.[0]?.preview ?? "",
171 nodes: out.approval.nodes ?? [],
172 edges: out.approval.edges ?? [],
173 conditional: out.approval.conditional ?? [],
174 suggestions: out.approval.suggestions ?? [],
175 };
176 return;
177 }
178 if (tool !== "AskUserQuestion" || !rendered || !outcome || outcome.deny) return;
179 const questions = Array.isArray(input?.questions) ? input.questions : [];
180 const answers = outcome.result?.answers ?? {};
181 const labels = new Set(rendered.suggestions.map((s) => s.label));
182 // The go-ahead is the asked question offering "Draw it" (only the go-ahead does),
183 // matched by its options so a reworded question still counts.
184 const go = questions.find((q) => (q?.options ?? []).some((o) => o?.label === DRAW_ALL));
185 if (go && answers[go.question] !== undefined) {
186 // The dialog must have shown the plan: "Draw it" carries the render's sketch as its preview.
187 const shown = go.options.find((o) => o?.label === DRAW_ALL)?.preview ?? "";
188 const answer = shown.trim() === rendered.preview.trim() ? answers[go.question] : null;
189 picks = answer === PICK && labels.size > 0 ? new Map() : null;
190 approved = answer === DRAW_ALL ? approve(labels) : picks ? approve(new Set()) : null;
191 return;
192 }
193 // A pick round: the questions whose options are this render's suggestions.
194 if (!picks) return;
195 for (const q of questions.filter((q) => (q?.options ?? []).some((o) => labels.has(o?.label))))
196 if (answers[q.question] !== undefined)
197 picks.set(q.question, String(answers[q.question]).split(", ").filter((l) => labels.has(l)));
198 approved = approve(new Set([...picks.values()].flat()));
199 },
200 check(tool, input) {
201 if (!ROOT_DRAW.test(tool) || input?.workBoardSlug !== "root") return null;
202 if (!approved)
203 return "ProvenMap guard: the workspace board is drawn only from a plan the architect approved. Run pmap-architect.js --render-plan, print its display, ask its goAhead question with AskUserQuestion as given (and on \"Let me pick\" its pick rounds), then --plan-approve with the answers and draw the wire file it names.";
204 const nodes = /create_nodes$/i.test(tool);
205 const items = nodes
206 ? (Array.isArray(input.nodes) ? input.nodes : []).map((n) => n?.slug ?? "")
207 : (Array.isArray(input.edges) ? input.edges : []).map(edgeKey);
208 const outside = items.filter((k) => !(nodes ? approved.nodes : approved.edges).has(k));
209 return outside.length === 0
210 ? null
211 : `ProvenMap guard: ${outside.map((k) => `\`${k}\``).join(", ")} ${outside.length === 1 ? "is" : "are"} not in what the architect approved. Draw the wire file --plan-approve wrote, or add ${outside.length === 1 ? "it" : "them"} to the plan, re-render and ask again.`;
212 },
213 };
214}
215
216/**
217 * The deny text for one tool call, or null to let it through. Exported for the
218 * plugin repo's tests; the engine only calls `register`.
219 */
220export function guard(tool, input) {
221 if (!input || typeof input !== "object") return null;
222 if (tool === "Read") {
223 const p = normalise(input.file_path);
224 return SECRET_FILE.test(p) ? secretDeny(p) : null;
225 }
226 if (tool === "Grep") {
227 const p = normalise(input.path);
228 return SECRET_FILE.test(p) || SECRET_DIR.test(p) ? secretDeny(p) : null;
229 }
230 if (tool === "Edit" || tool === "Write" || tool === "NotebookEdit") {
231 const p = normalise(input.file_path ?? input.notebook_path);
232 if (SECRET_FILE.test(p)) return secretDeny(p);
233 return SCRIPT_OWNED.test(p) ? scriptOwnedDeny(p) : null;
234 }
235 if (tool === "Bash" || tool === "PowerShell") {
236 const command = typeof input.command === "string" ? input.command : "";
237 if (SECRET_ENV.test(command)) return secretDeny(command.match(SECRET_ENV)[0]);
238 if (shellReadsSecret(command)) return secretDeny((command.match(SECRET_NAME) ?? [".provenmap/"])[0]);
239 const written = shellWriteTarget(command);
240 return written ? scriptOwnedDeny(written) : null;
241 }
242 return null;
243}
244
245function redactText(text) {
246 return text.replace(SECRET_VALUE, "$1****");
247}
248
249function redactDeep(value) {
250 if (typeof value === "string") return redactText(value);
251 if (Array.isArray(value)) return value.map(redactDeep);
252 if (value && typeof value === "object") {
253 return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, redactDeep(v)]));
254 }
255 return value;
256}
257
258/**
259 * What a tool call resolved to, with every ProvenMap secret masked, or the same
260 * object when it holds none (so core keeps its own rendering). A rewritten result
261 * drops `ref` and `text`, which name the unmasked output, and core re-renders it.
262 */
263export function redact(outcome) {
264 if (!outcome || outcome.deny !== undefined) return outcome;
265 const seen = typeof outcome.text === "string" ? outcome.text : JSON.stringify(outcome.result) ?? "";
266 if (!seen.match(SECRET_VALUE)) return outcome;
267 if (outcome.isError) return { deny: redactText(seen) };
268 return {
269 result: redactDeep(outcome.result),
270 ...(outcome.context ? { context: outcome.context.map(redactText) } : {}),
271 };
272}
273
274/**
275 * When the hook fails, the call goes through (a broken rule must not block every
276 * tool in every session), but its output is still masked, and output that cannot
277 * be masked is masked as text or withheld. `next` here replays a call already made.
278 */
279export async function recover($, e, next) {
280 const outcome = await next(e);
281 try {
282 return redact(outcome);
283 } catch {
284 return typeof outcome?.text === "string"
285 ? { deny: redactText(outcome.text) }
286 : { deny: "ProvenMap guard could not check this output for ProvenMap secrets, so it was withheld." };
287 }
288}
289
290/** @type {import('claude-code').Register} */
291export const register = (on) => {
292 // One gate per load: a plugin reload starts it over, so an approval never outlives the code that gave it.
293 const gate = planGate();
294 on("tool.call", async ($, e, next) => {
295 const deny = guard(e.tool, e) ?? gate.check(e.tool, e);
296 if (deny) return { deny };
297 const outcome = await next(e);
298 gate.observe(e.tool, e, outcome);
299 return redact(outcome);
300 }).catch(recover);
301};
302