lean-agent-harness 1.3.0 → 1.3.1

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/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project are documented here. Format based on [Keep a
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [1.3.1] - 2026-10-31
8
+
9
+ ### Fixed
10
+
11
+ - Guard: the executable is matched without case or path (`CAT .ENV`, `/bin/cat .env`, `/bin/rm -rf` no longer slip through), and brace patterns (`*.{env,pem}`, `cat {.env,x}`) are expanded before checking, up to 256 variants: a pattern that would expand further is refused instead of hanging the hook.
12
+ - `--guard` now says `settings.json is not a JSON object` for a `settings.json` holding `null`, an array or a number, instead of a raw `TypeError`. It is still left untouched.
13
+
7
14
  ## [1.3.0] - 2026-10-30
8
15
 
9
16
  ### Added
package/GLOSSARY.md CHANGED
@@ -31,6 +31,8 @@ Single source of truth for the domain vocabulary of this project. Both human eng
31
31
  | **Pinned Download** | A `curl` install fixed to a release and verified: `LAH_VERSION` selects the tag `vX.Y.Z` (default: the branch), `LAH_SHA256` makes `install.sh` abort before deploying when the archive differs, `LAH_ARCHIVE_URL` replaces the download URL (mirrors, tests). | `install.sh` | Without `LAH_SHA256` the archive is installed unverified and the installer says so. GitHub archives are not guaranteed byte-stable: recompute a stale checksum from a trusted copy before assuming tampering. |
32
32
  | **Guard** | Opt-in enforcement of the `AGENTS.md` Hard Rules that a tool can check mechanically (secret files, destructive commands): a Claude Code `PreToolUse` hook, a Pi extension and Codex command rules. Deployed only by `--global --guard`. | `guard/`, `global-guard` rows in `manifest.tsv` | Executes code inside the agent, so it is never on by default and never project-local (Pi loads project extensions only after the project is trusted). Best effort, not a sandbox: it does not see commands built through an interpreter. |
33
33
  | **Guard Policy** | The single list of protected secret files and destructive commands, each with a decision: `ask` (human confirms; blocked when nobody can answer) or `deny`. | `guard/policy.json` | Every guard artifact is generated from it by `guard/build.js` (including the `core.js` matcher copied beside the Pi and Claude Code guards); a test fails when a committed artifact drifts. |
34
+ | **Guard Matcher** | The shared logic that decides whether a path is a secret and which shell rule a command hits, built from the Guard Policy. Names, directories and the executable (`/bin/CAT` is `cat`) compare case-insensitively (the default macOS and Windows file systems open `.ENV` as `.env`), and brace patterns (`*.{env,pem}`) are expanded. | `guard/core.js` | One source, copied by `guard/build.js` beside each guard because each deploys on its own. |
35
+ | **Settings Hook** | A manifest row of kind `settings-hook`: registers the Claude Code guard hook in `~/.claude/settings.json` by merge instead of copying a file. | `manifest.tsv`, `guard/claude/hook.json`, `guard/claude/merge-settings.js` | Idempotent. Keeps every other key, saves the previous file as `.bak` (or `.bak.N`) when it changes, and never touches a `settings.json` that is not valid JSON. |
34
36
 
35
37
  ---
36
38
 
package/README.md CHANGED
@@ -46,7 +46,7 @@ curl -fsSL https://raw.githubusercontent.com/lnardev/lean-agent-harness/main/ins
46
46
  **Pinning and verifying the `curl` install.** The one-liner runs the `main` branch unless you pin it:
47
47
 
48
48
  ```bash
49
- curl -fsSL https://raw.githubusercontent.com/lnardev/lean-agent-harness/main/install.sh | LAH_VERSION=1.3.0 LAH_SHA256=<sha256-of-the-archive> bash -s -- --global --auto
49
+ curl -fsSL https://raw.githubusercontent.com/lnardev/lean-agent-harness/main/install.sh | LAH_VERSION=1.3.1 LAH_SHA256=<sha256-of-the-archive> bash -s -- --global --auto
50
50
  ```
51
51
 
52
52
  `LAH_VERSION` downloads that release tag. `LAH_SHA256` makes the installer abort, before deploying anything, if the archive does not match. Without it the installer says that the checksum is not verified. GitHub does not publish archive checksums, so compute the value once from a copy you trust, for example `shasum -a 256` on the archive of that tag. GitHub's generated archives are not guaranteed to stay byte-identical over time, so if a previously valid checksum stops matching, recompute it from a trusted copy before assuming tampering.
@@ -38,6 +38,48 @@ function wordsOf(segment) {
38
38
  return words;
39
39
  }
40
40
 
41
+ /**
42
+ * The executable a shell word runs: `/bin/CAT` is `cat` (the default macOS and Windows file systems ignore case).
43
+ * @param {string} word
44
+ * @returns {string}
45
+ */
46
+ function executableOf(word) {
47
+ return path.basename(word).toLowerCase();
48
+ }
49
+
50
+ const BRACE_GROUP = /\{([^{}]*)\}/;
51
+ const MAX_BRACE_VARIANTS = 256;
52
+
53
+ /**
54
+ * Shell brace expansion, so `*.{env,pem}` and `{.env,x}` are checked as the names they stand for.
55
+ * Growth is exponential in the number of groups, so past MAX_BRACE_VARIANTS it gives up.
56
+ *
57
+ * @param {string} text
58
+ * @returns {string[] | null} the expanded names, or null when there are too many to check
59
+ */
60
+ function expandBraces(text) {
61
+ let variants = [text];
62
+ for (;;) {
63
+ const next = [];
64
+ let expanded = false;
65
+ for (const variant of variants) {
66
+ const group = BRACE_GROUP.exec(variant);
67
+ if (!group) {
68
+ next.push(variant);
69
+ continue;
70
+ }
71
+ expanded = true;
72
+ const [whole, alternatives] = group;
73
+ for (const alternative of alternatives.split(",")) {
74
+ next.push(variant.slice(0, group.index) + alternative + variant.slice(group.index + whole.length));
75
+ }
76
+ if (next.length > MAX_BRACE_VARIANTS) return null;
77
+ }
78
+ if (!expanded) return next;
79
+ variants = next;
80
+ }
81
+ }
82
+
41
83
  /** @param {string} command @returns {string[][]} */
42
84
  function segmentsOf(command) {
43
85
  return command.split(SEGMENT_SEPARATORS).map(wordsOf).filter((words) => words.length > 0);
@@ -65,17 +107,23 @@ function createMatcher(policy) {
65
107
  const secretDirectories = policy.secrets.directories.map((name) => name.toLowerCase());
66
108
 
67
109
  /** @param {string} filePath @returns {boolean} */
68
- function isSecretPath(filePath) {
110
+ function isSecretName(filePath) {
69
111
  const base = path.basename(filePath);
70
112
  if (allowedBasenames.some((pattern) => pattern.test(base))) return false;
71
113
  if (secretBasenames.some((pattern) => pattern.test(base))) return true;
72
114
  return filePath.split("/").some((part) => secretDirectories.includes(part.toLowerCase()));
73
115
  }
74
116
 
117
+ /** @param {string} filePath @returns {boolean} */
118
+ function isSecretPath(filePath) {
119
+ const names = expandBraces(filePath);
120
+ return names === null || names.some(isSecretName);
121
+ }
122
+
75
123
  /** @param {string} command @returns {boolean} */
76
124
  function readsSecret(command) {
77
125
  return segmentsOf(command).some(
78
- ([first, ...args]) => policy.secrets.readers.includes(first) && args.some(isSecretPath),
126
+ ([first, ...args]) => policy.secrets.readers.includes(executableOf(first)) && args.some(isSecretPath),
79
127
  );
80
128
  }
81
129
 
@@ -86,7 +134,9 @@ function createMatcher(policy) {
86
134
  function ruleHit(command) {
87
135
  for (const words of segmentsOf(command)) {
88
136
  for (const rule of policy.shell) {
89
- const matchesCommand = rule.command.every((token, index) => words[index] === token);
137
+ const matchesCommand = rule.command.every(
138
+ (token, index) => (index === 0 ? executableOf(words[0]) : words[index]) === token,
139
+ );
90
140
  if (!matchesCommand) continue;
91
141
  const rest = words.slice(rule.command.length);
92
142
  if (!rule.anyOf || rest.some((word) => rule.anyOf.includes(word))) return rule;
@@ -16,31 +16,47 @@ const path = require("node:path");
16
16
  const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
17
17
 
18
18
  /**
19
- * @param {string} hookPath JSON file with {event, entry}
20
19
  * @param {string} settingsPath
21
- * @returns {boolean} true when settings.json was written
20
+ * @returns {{raw: string | null, settings: Record<string, unknown>}} raw is null when the file does not exist yet
22
21
  */
23
- function mergeHook(hookPath, settingsPath) {
24
- const { event, entry } = JSON.parse(fs.readFileSync(hookPath, "utf8"));
25
-
22
+ function readSettings(settingsPath) {
26
23
  let raw = null;
27
24
  try {
28
25
  raw = fs.readFileSync(settingsPath, "utf8");
29
26
  } catch (err) {
30
27
  if (err.code !== "ENOENT") throw err;
31
28
  }
29
+ if (raw === null) return { raw, settings: {} };
32
30
 
33
- let settings = {};
34
- if (raw !== null) {
35
- try {
36
- settings = JSON.parse(raw);
37
- } catch (err) {
38
- throw new Error(`${settingsPath} is not valid JSON (${err.message}); fix it and run again`);
39
- }
31
+ let settings;
32
+ try {
33
+ settings = JSON.parse(raw);
34
+ } catch (err) {
35
+ throw new Error(`${settingsPath} is not valid JSON (${err.message}); fix it and run again`);
40
36
  }
37
+ if (!isObject(settings)) throw new Error(`${settingsPath} is not a JSON object; fix it and run again`);
38
+ return { raw, settings };
39
+ }
40
+
41
+ /** @param {string} settingsPath */
42
+ function keepBackup(settingsPath) {
43
+ let backup = `${settingsPath}.bak`;
44
+ for (let n = 1; fs.existsSync(backup); n++) backup = `${settingsPath}.bak.${n}`;
45
+ fs.copyFileSync(settingsPath, backup);
46
+ }
47
+
48
+ /**
49
+ * @param {string} hookPath JSON file with {event, entry}
50
+ * @param {string} settingsPath
51
+ * @returns {boolean} true when settings.json was written
52
+ */
53
+ function mergeHook(hookPath, settingsPath) {
54
+ const { event, entry } = JSON.parse(fs.readFileSync(hookPath, "utf8"));
55
+ const { raw, settings } = readSettings(settingsPath);
56
+
41
57
  const hooks = settings.hooks ?? {};
42
- const entries = hooks[event] ?? [];
43
- if (!isObject(settings) || !isObject(hooks) || !Array.isArray(entries)) {
58
+ const entries = isObject(hooks) ? hooks[event] ?? [] : null;
59
+ if (!Array.isArray(entries)) {
44
60
  throw new Error(`${settingsPath} has an unexpected "hooks" shape; fix it and run again`);
45
61
  }
46
62
 
@@ -48,11 +64,7 @@ function mergeHook(hookPath, settingsPath) {
48
64
  const registered = entries.some((existing) => Array.isArray(existing?.hooks) && existing.hooks.some((hook) => hook?.command === command));
49
65
  if (registered) return false;
50
66
 
51
- if (raw !== null) {
52
- let backup = `${settingsPath}.bak`;
53
- for (let n = 1; fs.existsSync(backup); n++) backup = `${settingsPath}.bak.${n}`;
54
- fs.copyFileSync(settingsPath, backup);
55
- }
67
+ if (raw !== null) keepBackup(settingsPath);
56
68
  fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
57
69
  fs.writeFileSync(settingsPath, `${JSON.stringify({ ...settings, hooks: { ...hooks, [event]: [...entries, entry] } }, null, 2)}\n`);
58
70
  return true;
@@ -38,6 +38,48 @@ function wordsOf(segment) {
38
38
  return words;
39
39
  }
40
40
 
41
+ /**
42
+ * The executable a shell word runs: `/bin/CAT` is `cat` (the default macOS and Windows file systems ignore case).
43
+ * @param {string} word
44
+ * @returns {string}
45
+ */
46
+ function executableOf(word) {
47
+ return path.basename(word).toLowerCase();
48
+ }
49
+
50
+ const BRACE_GROUP = /\{([^{}]*)\}/;
51
+ const MAX_BRACE_VARIANTS = 256;
52
+
53
+ /**
54
+ * Shell brace expansion, so `*.{env,pem}` and `{.env,x}` are checked as the names they stand for.
55
+ * Growth is exponential in the number of groups, so past MAX_BRACE_VARIANTS it gives up.
56
+ *
57
+ * @param {string} text
58
+ * @returns {string[] | null} the expanded names, or null when there are too many to check
59
+ */
60
+ function expandBraces(text) {
61
+ let variants = [text];
62
+ for (;;) {
63
+ const next = [];
64
+ let expanded = false;
65
+ for (const variant of variants) {
66
+ const group = BRACE_GROUP.exec(variant);
67
+ if (!group) {
68
+ next.push(variant);
69
+ continue;
70
+ }
71
+ expanded = true;
72
+ const [whole, alternatives] = group;
73
+ for (const alternative of alternatives.split(",")) {
74
+ next.push(variant.slice(0, group.index) + alternative + variant.slice(group.index + whole.length));
75
+ }
76
+ if (next.length > MAX_BRACE_VARIANTS) return null;
77
+ }
78
+ if (!expanded) return next;
79
+ variants = next;
80
+ }
81
+ }
82
+
41
83
  /** @param {string} command @returns {string[][]} */
42
84
  function segmentsOf(command) {
43
85
  return command.split(SEGMENT_SEPARATORS).map(wordsOf).filter((words) => words.length > 0);
@@ -65,17 +107,23 @@ function createMatcher(policy) {
65
107
  const secretDirectories = policy.secrets.directories.map((name) => name.toLowerCase());
66
108
 
67
109
  /** @param {string} filePath @returns {boolean} */
68
- function isSecretPath(filePath) {
110
+ function isSecretName(filePath) {
69
111
  const base = path.basename(filePath);
70
112
  if (allowedBasenames.some((pattern) => pattern.test(base))) return false;
71
113
  if (secretBasenames.some((pattern) => pattern.test(base))) return true;
72
114
  return filePath.split("/").some((part) => secretDirectories.includes(part.toLowerCase()));
73
115
  }
74
116
 
117
+ /** @param {string} filePath @returns {boolean} */
118
+ function isSecretPath(filePath) {
119
+ const names = expandBraces(filePath);
120
+ return names === null || names.some(isSecretName);
121
+ }
122
+
75
123
  /** @param {string} command @returns {boolean} */
76
124
  function readsSecret(command) {
77
125
  return segmentsOf(command).some(
78
- ([first, ...args]) => policy.secrets.readers.includes(first) && args.some(isSecretPath),
126
+ ([first, ...args]) => policy.secrets.readers.includes(executableOf(first)) && args.some(isSecretPath),
79
127
  );
80
128
  }
81
129
 
@@ -86,7 +134,9 @@ function createMatcher(policy) {
86
134
  function ruleHit(command) {
87
135
  for (const words of segmentsOf(command)) {
88
136
  for (const rule of policy.shell) {
89
- const matchesCommand = rule.command.every((token, index) => words[index] === token);
137
+ const matchesCommand = rule.command.every(
138
+ (token, index) => (index === 0 ? executableOf(words[0]) : words[index]) === token,
139
+ );
90
140
  if (!matchesCommand) continue;
91
141
  const rest = words.slice(rule.command.length);
92
142
  if (!rule.anyOf || rest.some((word) => rule.anyOf.includes(word))) return rule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lean-agent-harness",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Zero-bloat, token-efficient agent harness for Claude Code, Cursor, OpenCode, Pi, Codex, and CommandCode.",
5
5
  "author": "lnardev",
6
6
  "bin": {