@gonrocca/nodd 0.2.2 → 0.3.0

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.
@@ -1,6 +1,7 @@
1
1
  import { test } from "node:test";
2
2
  import assert from "node:assert/strict";
3
- import { readFileSync } from "node:fs";
3
+ import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
4
5
  import { dirname, join } from "node:path";
5
6
  import { fileURLToPath } from "node:url";
6
7
  import register, { createKernel } from "./nodd-kernel.ts";
@@ -20,6 +21,55 @@ function fakePi() {
20
21
  };
21
22
  }
22
23
 
24
+ test("the config is read from the given home, so the suite never reads the user's", () => {
25
+ // Without an injectable home, `register()` reads the real `~/.pi/nodd.json`
26
+ // and the suite's verdict depends on the machine running it: measured on one
27
+ // commit, a local config with the gates off turned 434 passes into 408.
28
+ const home = mkdtempSync(join(tmpdir(), "nodd-home-"));
29
+ mkdirSync(join(home, ".pi"), { recursive: true });
30
+ writeFileSync(join(home, ".pi", "nodd.json"), JSON.stringify({ gates: { classify: { enabled: false } } }));
31
+
32
+ const kernel = register(fakePi() as never, mkdtempSync(join(tmpdir(), "nodd-cwd-")), home);
33
+ assert.deepEqual(kernel.policy().config, { classify: { enabled: false } }, "the given home is what was read");
34
+
35
+ const other = register(fakePi() as never, mkdtempSync(join(tmpdir(), "nodd-cwd-")), mkdtempSync(join(tmpdir(), "nodd-empty-")));
36
+ assert.deepEqual(other.policy().config, {}, "an empty home means nobody chose, whatever the real one says");
37
+ });
38
+
39
+ test("reloadPolicy picks up a config written after the session started", () => {
40
+ // `/nodd-gates off` writes the file; without a re-read the gate keeps blocking
41
+ // until pi restarts, while disk already says it is off. A user who turns off
42
+ // the kill switch and watches it keep blocking concludes it does not work.
43
+ const home = mkdtempSync(join(tmpdir(), "nodd-home-"));
44
+ mkdirSync(join(home, ".pi"), { recursive: true });
45
+ const kernel = register(fakePi() as never, mkdtempSync(join(tmpdir(), "nodd-cwd-")), home);
46
+ assert.deepEqual(kernel.policy().config, {}, "nothing configured yet");
47
+
48
+ writeFileSync(join(home, ".pi", "nodd.json"), JSON.stringify({ gates: { track: { enabled: false } } }));
49
+ kernel.reloadPolicy();
50
+ assert.deepEqual(kernel.policy().config, { track: { enabled: false } }, "the new config is in effect");
51
+ });
52
+
53
+ test("/nodd-gates disable takes effect without restarting pi", () => {
54
+ // The kill switch is user-owned: it must obey at once. Reading the config
55
+ // only at startup left the gate blocking while disk already said it was off,
56
+ // and the refusal kept offering /nodd-allow as though nobody had decided —
57
+ // a user who turns it off and watches it keep blocking concludes it is broken.
58
+ const home = mkdtempSync(join(tmpdir(), "nodd-home-"));
59
+ mkdirSync(join(home, ".pi"), { recursive: true });
60
+ const pi = fakePi();
61
+ register(pi as never, mkdtempSync(join(tmpdir(), "nodd-cwd-")), home);
62
+
63
+ const write = { toolName: "write", toolCallId: "c1", input: { file_path: "a.ts", content: "x" } };
64
+ assert.equal(pi.emit("tool_call", write)?.block, true, "undeclared writes are blocked while the gate is on");
65
+
66
+ // What `/nodd-gates disable classify` writes, mid-session.
67
+ writeFileSync(join(home, ".pi", "nodd.json"), JSON.stringify({ gates: { classify: { enabled: false } } }));
68
+
69
+ const after = pi.emit("tool_call", { ...write, toolCallId: "c2" });
70
+ assert.equal(after, undefined, "the very next call sees the gate off, with no restart");
71
+ });
72
+
23
73
  test("a read-only session of read/grep/ls produces zero blocks", () => {
24
74
  const pi = fakePi();
25
75
  register(pi as never);
@@ -142,17 +142,23 @@ export type Kernel = {
142
142
  /** Run the ordered registry against one call. `null` means let it run. */
143
143
  checkCall(request: GateRequest): { block: true; reason: string } | null;
144
144
  setPolicy(policy: Policy): void;
145
+ /** Re-read the configured flags, keeping runtime flags and hatches. */
146
+ reloadPolicy(): Policy;
145
147
  policy(): Policy;
146
148
  };
147
149
 
148
150
  export function createKernel(
149
151
  now: () => string = () => new Date().toISOString(),
150
152
  cwd: string = process.cwd(),
153
+ loadPolicy: () => Policy = emptyPolicy,
151
154
  ): Kernel {
152
155
  const state = emptyState();
153
156
  // Mutable because `/nodd-allow` grants a hatch mid-session and a refusal
154
157
  // spends it. The policy is session state, not a constant.
155
158
  let policy: Policy = emptyPolicy();
159
+ // Set by `setPolicy`: the caller owns the whole policy from then on, and
160
+ // `reloadPolicy` stops re-reading the file over their choice.
161
+ let policyIsPinned = false;
156
162
 
157
163
  const readDoc = (slug: string): FeatureDoc | null => {
158
164
  const path = featureDocPath(cwd, slug);
@@ -349,7 +355,20 @@ export function createKernel(
349
355
  return null;
350
356
  },
351
357
 
358
+ reloadPolicy() {
359
+ // Re-read rather than cache: `/nodd-gates off` writes the file, and a
360
+ // policy only read at startup leaves the gate blocking while disk already
361
+ // says it is off — a kill switch the user watches fail.
362
+ //
363
+ // An explicit `setPolicy` wins: it is the caller stating the whole policy,
364
+ // and re-reading over it would silently undo what they just set.
365
+ if (policyIsPinned) return policy;
366
+ policy = { ...loadPolicy(), flags: policy.flags, hatches: policy.hatches };
367
+ return policy;
368
+ },
369
+
352
370
  setPolicy(next) {
371
+ policyIsPinned = true;
353
372
  policy = next;
354
373
  },
355
374
 
@@ -518,23 +537,32 @@ function promotionSignals(committed: Committed, request: GateRequest) {
518
537
  };
519
538
  }
520
539
 
521
- /** Gate flags as configured. An unreadable config means nobody chose. */
522
- function readPolicy(): Policy {
540
+ /**
541
+ * Gate flags as configured. An unreadable config means nobody chose.
542
+ *
543
+ * `home` is a parameter, not `homedir()` inside: the tests call `register()`
544
+ * directly, and reading the real `~/.pi/nodd.json` made the suite's verdict
545
+ * depend on the machine running it.
546
+ */
547
+ function readPolicy(home?: string): Policy {
523
548
  try {
524
- const { config } = parseConfig(readFileSync(noddConfigPath(), "utf8"));
549
+ const { config } = parseConfig(readFileSync(noddConfigPath(home), "utf8"));
525
550
  return { ...emptyPolicy(), config: config.gates };
526
551
  } catch {
527
552
  return emptyPolicy();
528
553
  }
529
554
  }
530
555
 
531
- export default function register(pi?: PiApi, cwd: string = process.cwd()): Kernel {
532
- const kernel = createKernel(undefined, cwd);
533
- if (!pi || typeof pi.on !== "function") return kernel;
556
+ export default function register(pi?: PiApi, cwd: string = process.cwd(), home?: string): Kernel {
557
+ const kernel = createKernel(undefined, cwd, () => readPolicy(home));
558
+ if (!pi || typeof pi.on !== "function") {
559
+ kernel.reloadPolicy();
560
+ return kernel;
561
+ }
534
562
 
535
563
  // Flags the user set persist into the session's policy. `/nodd-allow` adds
536
564
  // one-shot hatches on top of this at runtime.
537
- kernel.setPolicy(readPolicy());
565
+ kernel.reloadPolicy();
538
566
 
539
567
  pi.registerTool?.({
540
568
  name: "nodd_declare",
@@ -552,6 +580,10 @@ export default function register(pi?: PiApi, cwd: string = process.cwd()): Kerne
552
580
  pi.on("tool_call", ((event: ToolCallEvent) => {
553
581
  let decision: { block: true; reason: string } | null = null;
554
582
  try {
583
+ // Re-read the flags before deciding. `/nodd-gates disable` writes the
584
+ // config from another extension, and a policy read only at startup would
585
+ // keep blocking while disk already said the gate was off.
586
+ kernel.reloadPolicy();
555
587
  decision = kernel.checkCall({ toolName: event?.toolName ?? "", input: normalizeInput(event?.input) });
556
588
  } catch {
557
589
  // A gate that throws must not break the session. Failing open here is
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gonrocca/nodd",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Non-negotiable Organic Driven Development — the ODD protocol as runtime mechanism for pi: blocking gates, observed evidence, and promotion to /forge.",
5
5
  "type": "module",
6
6
  "keywords": [