candor-ts 0.8.9 → 0.8.12
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.
- package/AGENTS.md +10 -7
- package/lsp.mjs +85 -5
- package/mcp.mjs +22 -0
- package/package.json +1 -1
- package/query-core.mjs +127 -0
- package/query.mjs +34 -0
package/AGENTS.md
CHANGED
|
@@ -97,6 +97,8 @@ Q map $P 1 # {module: {effects, functions}}
|
|
|
97
97
|
Q containment $P [baseline-prefix] # §6.1 boundary-effect dispersion; with a baseline = AS-EFF-010 ratchet (exit 1 on a leak)
|
|
98
98
|
Q blindspots $P # the Unknown SOURCES (fns with unknownWhy), ranked by Unknown blast radius
|
|
99
99
|
Q whatif $P <fn> <Effect> [policy] # pre-edit gate verdict (exit 1 if it would violate)
|
|
100
|
+
Q fix $P <fn> <Effect> <policy> # the boundary FIX: where the effect belongs + the hoist refactor
|
|
101
|
+
Q fix-gate $P <policy> # a fix for EVERY crossing — the loop's block-message remedy
|
|
100
102
|
Q diff $P <baseline-prefix> 1 # per-function effect delta (exit 1 on a gained effect)
|
|
101
103
|
Q gains $P <baseline-prefix> # supply-chain alarm: {gained, byFunction} — effects a surface grew
|
|
102
104
|
Q reachable $P 1 # what the app DOES at runtime: effects over the entry points
|
|
@@ -105,13 +107,14 @@ Q parsepolicy <policy-file> # the canonical §6.2 parse (what the gate w
|
|
|
105
107
|
|
|
106
108
|
And as an MCP server, so an agent pulls these as tools instead of shelling out:
|
|
107
109
|
`CANDOR_REPORT=$P npx -y candor-ts-mcp` (tools `candor_impact`/`candor_reachable`/`candor_where`/…,
|
|
108
|
-
plus `candor_gate`/`candor_whatif` — a given-but-unreadable `policy` is a loud tool
|
|
109
|
-
clean verdict). `npx -y candor-ts-watch <dir>` keeps the report fresh as you edit (and
|
|
110
|
-
edit-delta); `candor-lsp` serves the same report as CodeLens/hover/diagnostics in any LSP
|
|
111
|
-
plus
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
plus `candor_gate`/`candor_whatif`/`candor_fix` — a given-but-unreadable `policy` is a loud tool
|
|
111
|
+
error, never a clean verdict). `npx -y candor-ts-watch <dir>` keeps the report fresh as you edit (and
|
|
112
|
+
reports the edit-delta); `candor-lsp` serves the same report as CodeLens/hover/diagnostics in any LSP
|
|
113
|
+
editor, plus two code actions (plain LSP — helix/neovim/VS Code/JetBrains-via-LSP4IJ all get them
|
|
114
|
+
without client code): the pre-edit whatif (`candor: what if <fn> performed <E>?` → the `candor.whatif`
|
|
115
|
+
command) and, when the cursor sits in a function that actually violates the policy, the boundary FIX
|
|
116
|
+
(`candor fix: hoist <E> out of <fn>` → the `candor.fix` command: where the effect belongs + the hoist
|
|
117
|
+
refactor, as a showMessage and a transient diagnostic, cleared on the file's next open/save).
|
|
115
118
|
CAVEAT — the MCP/LSP gate verdicts are computed FROM THE REPORT: the engine's own `--policy` /
|
|
116
119
|
`--gate-json` run additionally fails an allow rule whose literal surface is incomplete (a masked
|
|
117
120
|
endpoint — internal state, not a report field), so treat a report-side green as advisory and the
|
package/lsp.mjs
CHANGED
|
@@ -219,6 +219,7 @@ function showMessage(type, message) { send({ jsonrpc: "2.0", method: "window/sho
|
|
|
219
219
|
// BOUNDARY effect (Q.CONTAINED — ambient effects gate nothing) the fn does not already carry. The action
|
|
220
220
|
// carries a plain `command` (no client-side resolve, no edit) so it works in any LSP client verbatim.
|
|
221
221
|
const WHATIF_COMMAND = "candor.whatif";
|
|
222
|
+
const FIX_COMMAND = "candor.fix";
|
|
222
223
|
function codeActions(docPath, uri, range) {
|
|
223
224
|
const at = enclosingEntry(docPath, range?.start?.line ?? 0);
|
|
224
225
|
if (!at) return []; // a fn the report doesn't know → no actions, never an error
|
|
@@ -235,6 +236,30 @@ function codeActions(docPath, uri, range) {
|
|
|
235
236
|
},
|
|
236
237
|
});
|
|
237
238
|
}
|
|
239
|
+
// The REMEDIAL companion (integrations/FIX-SPEC.md): for each BOUNDARY effect the fn ALREADY performs that
|
|
240
|
+
// the active policy FORBIDS here, offer the FIX — where the effect belongs + the hoist. Only real crossings
|
|
241
|
+
// are offered (Q.fix returns `crossing:false` otherwise), so this is empty unless the cursor sits in a
|
|
242
|
+
// function that actually violates the boundary. Same policy source as the diagnostics + the whatif action.
|
|
243
|
+
const policyText = activePolicy();
|
|
244
|
+
if (policyText !== null && hasReport(reportPrefix)) {
|
|
245
|
+
const pol = parsePolicy(policyText);
|
|
246
|
+
const cg = Q.loadCallgraph(reportPrefix);
|
|
247
|
+
const fns = Q.loadReport(reportPrefix);
|
|
248
|
+
for (const eff of Q.CONTAINED) {
|
|
249
|
+
if (!have.has(eff)) continue;
|
|
250
|
+
const r = Q.fix(cg, fns, at.entry.fn, eff, pol, scopeMatches);
|
|
251
|
+
if (r && r.crossing) {
|
|
252
|
+
out.push({
|
|
253
|
+
title: `candor fix: hoist ${eff} out of ${at.entry.fn}`,
|
|
254
|
+
command: {
|
|
255
|
+
title: `candor fix: hoist ${eff} out of ${at.entry.fn}`,
|
|
256
|
+
command: FIX_COMMAND,
|
|
257
|
+
arguments: [{ fn: at.entry.fn, effect: eff, uri, line: at.line }],
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
238
263
|
return out;
|
|
239
264
|
}
|
|
240
265
|
|
|
@@ -290,6 +315,59 @@ function runWhatif(a) {
|
|
|
290
315
|
return r; // the raw whatif result rides back as the executeCommand result (a thick client can render it)
|
|
291
316
|
}
|
|
292
317
|
|
|
318
|
+
// The candor.fix command: the SAME query-core `fix` the CLI (`query.mjs fix`) and MCP (`candor_fix`) run —
|
|
319
|
+
// the boundary remedy (where the effect belongs + the hoist refactor), against the live policy (same source
|
|
320
|
+
// as the diagnostics). Re-read per call (the freshness contract). Malformed args → logMessage + null.
|
|
321
|
+
function runFix(a) {
|
|
322
|
+
if (!a || typeof a !== "object" || typeof a.fn !== "string" || typeof a.effect !== "string") {
|
|
323
|
+
logMessage(`candor-lsp: ${FIX_COMMAND} called with malformed arguments (expected [{ fn, effect, uri?, line? }]) — ignored`);
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
if (!hasReport(reportPrefix)) {
|
|
327
|
+
showMessage(2, "candor: no report found — scan first (candor-ts <dir> --out .candor/report)");
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
const policyText = activePolicy();
|
|
331
|
+
if (policyText === null) {
|
|
332
|
+
showMessage(2, "candor: no policy discovered — a fix is defined relative to a boundary; set CANDOR_POLICY or check one into .candor/config");
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
const r = Q.fix(Q.loadCallgraph(reportPrefix), Q.loadReport(reportPrefix), a.fn, a.effect,
|
|
336
|
+
parsePolicy(policyText), scopeMatches);
|
|
337
|
+
if (r === null) {
|
|
338
|
+
showMessage(2, `candor: no function matching \`${a.fn}\` in the call graph — the report may be stale`);
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
if (!r.crossing) {
|
|
342
|
+
showMessage(3, `candor: \`${a.fn}\` — ${a.effect} isn't forbidden here; no boundary fix needed`);
|
|
343
|
+
return r;
|
|
344
|
+
}
|
|
345
|
+
const verdict = r.cleanHoist
|
|
346
|
+
? `candor fix: hoist ${a.effect} to ${r.hoistTo.join(", ")} — the ${r.deniedSpan.length} ${r.layer || "(root)"} function(s) then stay pure (or relax the boundary: ${r.policyAlternative})`
|
|
347
|
+
: `candor fix: no clean hoist for ${a.effect} — introduce a port, or relax the boundary: ${r.policyAlternative}`;
|
|
348
|
+
showMessage(2, verdict);
|
|
349
|
+
if (typeof a.uri === "string" && Number.isInteger(a.line)) { // the plan, pinned at the fn's line
|
|
350
|
+
const lines = [`candor fix — hoist ${a.effect} out of the ${r.layer || "(root)"} boundary`];
|
|
351
|
+
lines.push(`site: ${r.site.join(", ") || "(cross-module or Unknown source)"}`);
|
|
352
|
+
if (r.cleanHoist) {
|
|
353
|
+
lines.push(`hoist ${a.effect} to: ${r.hoistTo.join(", ")}`);
|
|
354
|
+
lines.push(`then pure (thread the value): ${r.deniedSpan.join(", ")}`);
|
|
355
|
+
if (r.hoistHigher?.length) {
|
|
356
|
+
lines.push(`or hoist higher (up to ${r.hoistHigher.slice(0, 4).join(", ")}): keeps the frontier pure too, threads through more signatures`);
|
|
357
|
+
}
|
|
358
|
+
} else {
|
|
359
|
+
lines.push("no clean hoist — introduce a port (inject the effect from an allowed layer), or relax the boundary");
|
|
360
|
+
}
|
|
361
|
+
lines.push(`policy alternative: ${r.policyAlternative}`);
|
|
362
|
+
transient.set(a.uri, [{
|
|
363
|
+
range: { start: { line: a.line, character: 0 }, end: { line: a.line, character: 200 } },
|
|
364
|
+
severity: 3, source: "candor", code: "fix", message: lines.join("\n"),
|
|
365
|
+
}]);
|
|
366
|
+
publishDiagnostics(a.uri);
|
|
367
|
+
}
|
|
368
|
+
return r; // the raw remedy rides back as the executeCommand result (a thick client can render it)
|
|
369
|
+
}
|
|
370
|
+
|
|
293
371
|
// ---- the LSP method surface ---------------------------------------------------------------------------
|
|
294
372
|
function handle(msg) {
|
|
295
373
|
const { id, method, params } = msg;
|
|
@@ -307,7 +385,7 @@ function handle(msg) {
|
|
|
307
385
|
codeLensProvider: { resolveProvider: false },
|
|
308
386
|
hoverProvider: true,
|
|
309
387
|
codeActionProvider: { resolveProvider: false }, // actions carry their command inline
|
|
310
|
-
executeCommandProvider: { commands: [WHATIF_COMMAND] },
|
|
388
|
+
executeCommandProvider: { commands: [WHATIF_COMMAND, FIX_COMMAND] },
|
|
311
389
|
},
|
|
312
390
|
serverInfo: { name: "candor-lsp", version: VERSION },
|
|
313
391
|
});
|
|
@@ -334,12 +412,14 @@ function handle(msg) {
|
|
|
334
412
|
catch { return result(id, []); } // unknown fn / non-file URI / unreadable report → no actions, never an error
|
|
335
413
|
}
|
|
336
414
|
if (method === "workspace/executeCommand") {
|
|
337
|
-
|
|
338
|
-
|
|
415
|
+
const handlers = { [WHATIF_COMMAND]: runWhatif, [FIX_COMMAND]: runFix };
|
|
416
|
+
const run = handlers[params?.command];
|
|
417
|
+
if (!run) {
|
|
418
|
+
logMessage(`candor-lsp: unknown command \`${params?.command}\` — this server provides ${WHATIF_COMMAND} and ${FIX_COMMAND}`);
|
|
339
419
|
return result(id, null);
|
|
340
420
|
}
|
|
341
|
-
try { return result(id,
|
|
342
|
-
catch (e) { logMessage(`candor-lsp: ${
|
|
421
|
+
try { return result(id, run(params?.arguments?.[0])); }
|
|
422
|
+
catch (e) { logMessage(`candor-lsp: ${params?.command} failed: ${e.message}`); return result(id, null); }
|
|
343
423
|
}
|
|
344
424
|
if (method === "shutdown") return result(id, null);
|
|
345
425
|
if (method === "exit") process.exit(0);
|
package/mcp.mjs
CHANGED
|
@@ -178,6 +178,28 @@ const TOOLS = {
|
|
|
178
178
|
return r;
|
|
179
179
|
},
|
|
180
180
|
},
|
|
181
|
+
candor_fix: {
|
|
182
|
+
description: "THE BOUNDARY FIX: when `fn` performs `effect` in a layer the policy forbids (a violation candor_whatif/candor_gate reports), compute the architectural REMEDY — not just 'the domain can't do Net', but WHERE the effect belongs and the refactor to put it there: the direct call site to hoist, the forbidden-layer functions that become pure and thread the value as a parameter, and the nearest allowed-layer caller to perform the effect ({ crossing, site, deniedSpan, hoistTo, policyAlternative }). The remedial inverse of candor_whatif. Call this INSTEAD OF guessing a fix (adding `allow` to the domain, moving the I/O one call up, threading a handle the wrong way). Advisory: it names the structure, you write the code; the gate re-scan verifies. Uses `policy` if given, else the repo's checked-in .candor/config policy (spec §3.4).",
|
|
183
|
+
schema: { type: "object", properties: { fn: { type: "string" }, effect: { type: "string" }, policy: { type: "string", description: "path to a §6.2 policy file (optional; defaults to the repo's .candor/config `policy`)" }, ...reportArg }, required: ["fn", "effect"] },
|
|
184
|
+
run: (a, p) => {
|
|
185
|
+
// The fix is defined relative to a boundary — a policy is required. Given → confined fail-closed read;
|
|
186
|
+
// else the repo's checked-in policy (same resolution as candor_gate), so it works zero-config.
|
|
187
|
+
let text;
|
|
188
|
+
if (a.policy) text = confinedPolicyRead(a.policy, p);
|
|
189
|
+
else {
|
|
190
|
+
const cfg = configPolicy(p);
|
|
191
|
+
if (!cfg) throw new Error("no policy: pass `policy`, or check one into the repo's .candor/config (spec §3.4) — the fix is defined relative to the boundary it crosses");
|
|
192
|
+
text = confinedPolicyRead(cfg.policyPath, p, cfg.repoRoot);
|
|
193
|
+
}
|
|
194
|
+
const cg = Q.loadCallgraph(p);
|
|
195
|
+
// The sidecar is the only graph a candor-ts report carries — fail loud (tool error) when it's absent,
|
|
196
|
+
// never a degenerate empty-graph remedy. (/code-review.)
|
|
197
|
+
if (!cg || Object.keys(cg).length === 0) throw new Error(`no call-graph sidecar for the report — fix needs it (re-scan with --out)`);
|
|
198
|
+
const r = Q.fix(cg, Q.loadReport(p), a.fn, a.effect, parsePolicy(text), scopeMatches);
|
|
199
|
+
if (r === null) throw new Error(`no function matching \`${clip(a.fn)}\` in the call graph`);
|
|
200
|
+
return r;
|
|
201
|
+
},
|
|
202
|
+
},
|
|
181
203
|
candor_gate: {
|
|
182
204
|
description: "The policy verdict over this report: { ok, violations:[{rule, fn, effects, detail}] } — 'would this repo pass its architecture gate?'. Uses `policy` if given, else the repo's checked-in .candor/config policy (spec §3.4). Computed from the report — the engine's own --gate-json run is the authoritative CI form: it additionally fails an allow rule whose literal surface is INCOMPLETE (a masked/invisible endpoint), which is not a report field, so a green here can still be red in CI.",
|
|
183
205
|
schema: { type: "object", properties: { policy: { type: "string", description: "path to a §6.2 policy file (optional; defaults to the repo's .candor/config `policy`)" }, ...reportArg }, required: [] },
|
package/package.json
CHANGED
package/query-core.mjs
CHANGED
|
@@ -486,3 +486,130 @@ export function whatif(cg, target, eff, policyParsed, scopeMatches) {
|
|
|
486
486
|
}
|
|
487
487
|
return { of: targets, effect: eff, affected: [...affected].sort(), violations, ok: violations.length === 0 };
|
|
488
488
|
}
|
|
489
|
+
|
|
490
|
+
// deniedLayer: the deny/`pure` scope (the "layer") forbidding `eff` at `fn`, or null if allowed there.
|
|
491
|
+
// Mirrors the gate's AS-EFF-006 predicate (candor-java/candor-query): a `deny` fires when it names the
|
|
492
|
+
// effect; a `pure` rule (empty effects) forbids every real effect but not Unknown.
|
|
493
|
+
function deniedLayer(fn, eff, policyParsed, scopeMatches) {
|
|
494
|
+
for (const r of policyParsed.deny) {
|
|
495
|
+
const denies = r.effects.length === 0 ? eff !== "Unknown" : r.effects.includes(eff);
|
|
496
|
+
if (denies && (!r.scope || scopeMatches(fn, r.scope))) return r.scope ?? "";
|
|
497
|
+
}
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// The site-anchored cut (integrations/FIX-SPEC.md), shared by fix + fixGate — the byte-for-byte port of
|
|
502
|
+
// candor-query / candor-java's computeRemedy. Forward-BFS to the direct site(s), then climb UP through the
|
|
503
|
+
// denied layer so the pure span is the same whichever inheriting function triggered it (root-independent);
|
|
504
|
+
// the allowed-layer callers where the climb stops are the hoist frontier.
|
|
505
|
+
function computeRemedy(start, eff, layer, cg, rev, byName, policyParsed, scopeMatches) {
|
|
506
|
+
const sites = new Set();
|
|
507
|
+
const fseen = new Set([start]);
|
|
508
|
+
const fq = [start];
|
|
509
|
+
while (fq.length) {
|
|
510
|
+
const cur = fq.shift();
|
|
511
|
+
const fe = byName.get(cur);
|
|
512
|
+
if (fe && (fe.direct ?? []).includes(eff)) sites.add(cur);
|
|
513
|
+
for (const c of cg[cur] ?? []) {
|
|
514
|
+
const ce = byName.get(c);
|
|
515
|
+
if (ce && (ce.inferred ?? []).includes(eff) && !fseen.has(c)) { fseen.add(c); fq.push(c); }
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const anchors = sites.size ? [...sites] : [start];
|
|
519
|
+
const deniedSpan = new Set();
|
|
520
|
+
const hoistTo = new Set();
|
|
521
|
+
const up = [];
|
|
522
|
+
for (const a of anchors) {
|
|
523
|
+
if (deniedLayer(a, eff, policyParsed, scopeMatches) !== null) deniedSpan.add(a);
|
|
524
|
+
up.push(a);
|
|
525
|
+
}
|
|
526
|
+
while (up.length) {
|
|
527
|
+
const cur = up.shift();
|
|
528
|
+
for (const caller of rev.get(cur) ?? []) {
|
|
529
|
+
const ce = byName.get(caller);
|
|
530
|
+
// skip a caller that doesn't route the effect — INCLUDING one absent from the report (a pure
|
|
531
|
+
// callgraph-only node never carries the effect). Matches candor-swift. (/code-review — was `ce && !…`.)
|
|
532
|
+
if (!ce || !(ce.inferred ?? []).includes(eff)) continue;
|
|
533
|
+
if (deniedLayer(caller, eff, policyParsed, scopeMatches) !== null) {
|
|
534
|
+
if (!deniedSpan.has(caller)) { deniedSpan.add(caller); up.push(caller); }
|
|
535
|
+
} else {
|
|
536
|
+
hoistTo.add(caller);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
// higher hoist options: allowed-layer transitive callers of the minimal frontier that also route the
|
|
541
|
+
// effect — hoisting higher keeps the frontier pure too, at the cost of threading through more signatures
|
|
542
|
+
// (FIX-SPEC: the trade-off, disclosed not hidden).
|
|
543
|
+
const hoistHigher = new Set();
|
|
544
|
+
const hseen = new Set(hoistTo);
|
|
545
|
+
const hq = [...hoistTo];
|
|
546
|
+
while (hq.length) {
|
|
547
|
+
const cur = hq.shift();
|
|
548
|
+
for (const caller of rev.get(cur) ?? []) {
|
|
549
|
+
const ce = byName.get(caller);
|
|
550
|
+
if (ce && (ce.inferred ?? []).includes(eff) && deniedLayer(caller, eff, policyParsed, scopeMatches) === null && !hseen.has(caller)) {
|
|
551
|
+
hseen.add(caller);
|
|
552
|
+
hoistHigher.add(caller);
|
|
553
|
+
hq.push(caller);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
fn: start, effect: eff, layer,
|
|
559
|
+
cleanHoist: hoistTo.size > 0,
|
|
560
|
+
site: [...sites].sort(),
|
|
561
|
+
deniedSpan: [...deniedSpan].sort(),
|
|
562
|
+
hoistTo: [...hoistTo].sort(),
|
|
563
|
+
hoistHigher: [...hoistHigher].sort(),
|
|
564
|
+
policyAlternative: layer ? `allow ${eff} ${layer}` : `allow ${eff}`,
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// fix: the boundary remedy for ONE function (the remedial inverse of whatif). Returns null if the function
|
|
569
|
+
// isn't in the graph; `{ crossing:false, reason }` if it performs the effect but no policy forbids it there
|
|
570
|
+
// (or it doesn't perform it) — a no-op the caller reports plainly; else the full remedy (`crossing:true`).
|
|
571
|
+
// NOTE: `cg` (the callgraph sidecar) is REQUIRED — unlike candor-query/java/swift, a candor-ts report does not
|
|
572
|
+
// embed inline `calls`, so the sidecar is the only graph; the CLI/MCP callers fail loud when it's absent
|
|
573
|
+
// rather than compute a degenerate empty-graph remedy. (/code-review.)
|
|
574
|
+
export function fix(cg, fns, target, eff, policyParsed, scopeMatches) {
|
|
575
|
+
// Resolve against REPORT function names only (not callgraph nodes, which include pure fns absent from the
|
|
576
|
+
// report) — so `fix <pure-fn>` is a uniform "no such fn" across engines, not a TS-only crossing:false.
|
|
577
|
+
// (/code-review — candor-query/java/swift all match report fns only.)
|
|
578
|
+
const m = matches(fns.map((e) => e.fn), target);
|
|
579
|
+
if (m.length === 0) return null;
|
|
580
|
+
const byName = indexFns(fns);
|
|
581
|
+
// prefer a match that actually performs the effect, so a bare leaf resolves to the violating function
|
|
582
|
+
const start = m.find((n) => (byName.get(n)?.inferred ?? []).includes(eff)) ?? m[0];
|
|
583
|
+
const se = byName.get(start);
|
|
584
|
+
if (!se || !(se.inferred ?? []).includes(eff))
|
|
585
|
+
return { fn: start, effect: eff, crossing: false, reason: "does-not-perform" };
|
|
586
|
+
const layer = deniedLayer(start, eff, policyParsed, scopeMatches);
|
|
587
|
+
if (layer === null)
|
|
588
|
+
return { fn: start, effect: eff, crossing: false, reason: "not-forbidden" };
|
|
589
|
+
const rev = reverseGraph(cg);
|
|
590
|
+
return { crossing: true, ...computeRemedy(start, eff, layer, cg, rev, byName, policyParsed, scopeMatches) };
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// fixGate: a remedy for EVERY deny/`pure` (AS-EFF-006) crossing in the report, collapsing the inheritors of
|
|
594
|
+
// one root cause to a single plan (keyed by effect|layer|site|hoist). Returns { ok, remedies } — the shape
|
|
595
|
+
// the edit-time loop folds into its block message.
|
|
596
|
+
export function fixGate(cg, fns, policyParsed, scopeMatches) {
|
|
597
|
+
const byName = indexFns(fns);
|
|
598
|
+
const rev = reverseGraph(cg);
|
|
599
|
+
const plans = new Map();
|
|
600
|
+
// Iterate functions in sorted-name order so the first-writer-wins `fn` representative of a collapsed
|
|
601
|
+
// remedy is deterministic across engines (candor-query/java/swift all iterate a sorted key set).
|
|
602
|
+
for (const e of [...fns].sort((a, b) => (a.fn < b.fn ? -1 : a.fn > b.fn ? 1 : 0))) {
|
|
603
|
+
for (const eff of [...(e.inferred ?? [])].sort()) {
|
|
604
|
+
const layer = deniedLayer(e.fn, eff, policyParsed, scopeMatches);
|
|
605
|
+
if (layer !== null) {
|
|
606
|
+
const p = computeRemedy(e.fn, eff, layer, cg, rev, byName, policyParsed, scopeMatches);
|
|
607
|
+
const key = `${p.effect}|${p.layer}|${p.site}|${p.hoistTo}`;
|
|
608
|
+
if (!plans.has(key)) plans.set(key, p);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
// Emit remedies in dedup-key order (candor-query BTreeMap / java TreeMap / swift sorted-keys all do).
|
|
613
|
+
const remedies = [...plans.keys()].sort().map((k) => plans.get(k));
|
|
614
|
+
return { ok: remedies.length === 0, remedies };
|
|
615
|
+
}
|
package/query.mjs
CHANGED
|
@@ -31,6 +31,7 @@ import { impact as coreImpact, path as corePath, gains as coreGains,
|
|
|
31
31
|
callers as coreCallers, callersFrontier, loadHierarchy,
|
|
32
32
|
containment as coreContainment, diff as coreDiff,
|
|
33
33
|
where as coreWhere, map as coreMap, whatif as coreWhatif,
|
|
34
|
+
fix as coreFix, fixGate as coreFixGate,
|
|
34
35
|
loadReport, loadCallgraph, reportVersion } from "./query-core.mjs";
|
|
35
36
|
const emit = (v) => console.log(JSON.stringify(v, null, 1));
|
|
36
37
|
|
|
@@ -57,6 +58,8 @@ const SUBCOMMANDS = [
|
|
|
57
58
|
["gains", "<cur-prefix> <base-prefix>", "the supply-chain alarm: what the surface gained between two reports"],
|
|
58
59
|
["path", "<prefix> <fn> <Effect>", "a call path from a function to where an effect enters"],
|
|
59
60
|
["whatif", "<prefix> <fn> <Effect> [policy-file] [0|1]", "the impact of giving a function an effect, vs a policy (exit 1 on a violation)"],
|
|
61
|
+
["fix", "<prefix> <fn> <Effect> <policy-file>", "the boundary fix: where the effect belongs + the hoist refactor"],
|
|
62
|
+
["fix-gate", "<prefix> <policy-file>", "a fix for EVERY boundary crossing — the loop's block-message remedy"],
|
|
60
63
|
["agents", "", "print the agent contract for this build (AGENTS.md)"],
|
|
61
64
|
];
|
|
62
65
|
|
|
@@ -250,6 +253,37 @@ switch (cmd) {
|
|
|
250
253
|
process.exit(r.violations.length ? 1 : 0);
|
|
251
254
|
break; // unreachable (process.exit), but eslint can't prove it — defends against fallthrough
|
|
252
255
|
}
|
|
256
|
+
case "fix": {
|
|
257
|
+
// THE BOUNDARY FIX (integrations/FIX-SPEC.md): where a forbidden effect belongs + the hoist refactor.
|
|
258
|
+
// The remedial inverse of whatif. A policy is REQUIRED and must be readable (the fix is defined relative
|
|
259
|
+
// to the boundary the edit crossed) — a typo'd path fails LOUD, never a silently-empty "no crossing".
|
|
260
|
+
const [prefix, target, eff, policyFile] = args;
|
|
261
|
+
if (!target || !eff) { console.error("usage: candor-ts-query fix <prefix> <fn> <Effect> <policy-file>"); process.exit(2); }
|
|
262
|
+
if (!policyFile) { console.error("candor: fix requires a policy file — the fix is the refactor that restores the boundary the edit crossed"); process.exit(2); }
|
|
263
|
+
let ptext;
|
|
264
|
+
try { ptext = fs.readFileSync(policyFile, "utf8"); }
|
|
265
|
+
catch { console.error(`candor: policy ${policyFile} could not be read — no fix computed`); process.exit(2); }
|
|
266
|
+
const cg = loadCallgraph(prefix);
|
|
267
|
+
// The sidecar is the ONLY graph a candor-ts report carries (it embeds no inline `calls`). Fail LOUD when
|
|
268
|
+
// it's absent — never compute a degenerate empty-graph remedy that reads as a false "no clean hoist".
|
|
269
|
+
if (!cg || Object.keys(cg).length === 0) { console.error(`candor: no call-graph sidecar for '${prefix}' — fix needs it (re-run: candor-ts <src> --out ${prefix})`); process.exit(2); }
|
|
270
|
+
const r = coreFix(cg, loadReport(prefix), target, eff, parsePolicy(ptext), scopeMatches);
|
|
271
|
+
if (r === null) { console.error(`candor: no function matching \`${target}\` in the call graph`); process.exit(2); }
|
|
272
|
+
emit(r);
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case "fix-gate": {
|
|
276
|
+
// A remedy for EVERY deny/pure crossing — the shape the edit-time loop folds into its block message.
|
|
277
|
+
const [prefix, policyFile] = args;
|
|
278
|
+
if (!policyFile) { console.error("candor: fix-gate requires a policy file"); process.exit(2); }
|
|
279
|
+
let ptext;
|
|
280
|
+
try { ptext = fs.readFileSync(policyFile, "utf8"); }
|
|
281
|
+
catch { console.error(`candor: policy ${policyFile} could not be read — no fix computed`); process.exit(2); }
|
|
282
|
+
const cg = loadCallgraph(prefix);
|
|
283
|
+
if (!cg || Object.keys(cg).length === 0) { console.error(`candor: no call-graph sidecar for '${prefix}' — fix-gate needs it (re-run: candor-ts <src> --out ${prefix})`); process.exit(2); }
|
|
284
|
+
emit(coreFixGate(cg, loadReport(prefix), parsePolicy(ptext), scopeMatches));
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
253
287
|
default:
|
|
254
288
|
// no command (cmd === undefined) or an unknown one: the FULL usage, not the stale 6-item list.
|
|
255
289
|
if (cmd !== undefined) console.error(`candor-ts-query: unknown command '${cmd}'`);
|