@veda-hq/niyam 0.7.12 → 0.7.14

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/README.md CHANGED
@@ -1207,7 +1207,16 @@ merges did not bring, as before. In an agent session, a push is refused when the
1207
1207
  missing, or, with `work-rules` on, the session has no token. When the repository already has its own
1208
1208
  pre-push hook, or git runs hooks from a folder outside the repository (a
1209
1209
  global `core.hooksPath`), `onboard` leaves it alone and lists the line to add
1210
- under "By hand:": `niyam pre-push "$@"`. The client workflow re-runs the checks when a
1210
+ under "By hand:": `niyam pre-push "$@"`. A hook that has the line, or one that
1211
+ hands its push to a body file through one `dvara run <file> "$@"` line whose
1212
+ body has it (#540), needs nothing. The line, and the variable's assignment
1213
+ if it names one, sit outside any `if`, `case`, loop or `{ }` block. The file
1214
+ is a literal path or one plain variable assigned once in the hook, before
1215
+ the hand-off, inside the repository and readable; any
1216
+ other hand-off still gets the By hand step, and a comment saying the line is
1217
+ elsewhere counts for nothing. A cloud session does not count such a body as
1218
+ checked, since a body may skip niyam there (athya-rules' does), and says only
1219
+ the pull request's checks may hold the work rules. The client workflow re-runs the checks when a
1211
1220
  pull request's description is edited. After upgrading niyam, run
1212
1221
  `onboard` again: it upgrades the hook entries in `.claude/settings.json` and
1213
1222
  the workflow. An older release's workflow, one job that let every run write
@@ -1408,7 +1417,12 @@ run in the client repo, warns under "This repo" about each hook in `.claude/sett
1408
1417
  calls `gh` or git's remote commands outside `as-agent`. A script that
1409
1418
  imports a Python module tracked in the repo whose code runs `as-agent` (a
1410
1419
  helper wrapping each `gh` call) counts as routed through it; the hook's
1411
- command is still judged. Outside a cloud session, a script is read only up
1420
+ command is still judged. So does a script that builds its gh in the
1421
+ positional parameters and runs it as `"$@"`: `set -- gh`, then, where
1422
+ niyam's launcher is, `set -- "$cli" as-agent --session "$sid" -- gh`. Its
1423
+ plain `set -- gh` is the cloud's, whose proxy authenticates it, so it is not
1424
+ counted; every other `gh` in the script still is, and `set -- gh` with no
1425
+ `as-agent` form beside it counts. Outside a cloud session, a script is read only up
1412
1426
  to a line, at column 0, that exits there,
1413
1427
  `[ "${CLAUDE_CODE_REMOTE:-}" = true ] || exit 0` or
1414
1428
  `[ "$CLAUDE_CODE_REMOTE" != true ] && exit`, when only blank lines, `set`,
@@ -1741,7 +1755,7 @@ tag and the commit. Install the attached package on each machine with pnpm,
1741
1755
  once; nothing is built on install:
1742
1756
 
1743
1757
  ```bash
1744
- pnpm add -g https://github.com/veda-hq/niyam/releases/download/v0.7.12/veda-hq-niyam-0.7.12.tgz
1758
+ pnpm add -g https://github.com/veda-hq/niyam/releases/download/v0.7.14/veda-hq-niyam-0.7.14.tgz
1745
1759
  ```
1746
1760
 
1747
1761
  The package puts one command on `PATH`, `niyam`. Since P-57 a release
package/dist/as-agent.js CHANGED
@@ -129,8 +129,12 @@ const OWN_COMMANDS = new Set([HOOK_COMMAND, GUARD_COMMAND, ...OLDER_HOOK_COMMAND
129
129
  * reads text, so a script that runs another script is missed. Comments and
130
130
  * docstrings are left out, so prose that names gh or git push is not a call.
131
131
  * A script that loads a repo helper running `as-agent` is taken as routed
132
- * through it (`routedByHelper`). Outside a cloud session, a script is read
133
- * only up to a top-level line that exits there (`CLOUD_ONLY`).
132
+ * through it (`routedByHelper`). A script that sets its positional
133
+ * parameters to `as-agent` running gh and runs `"$@"` is taken as routed,
134
+ * its plain `set -- gh` being the cloud's (`withoutRoutedSetGh`); read as
135
+ * text, so which `"$@"` runs which is not followed. Outside a cloud session,
136
+ * a script is read only up to a top-level line that exits there
137
+ * (`CLOUD_ONLY`), before either form is looked for.
134
138
  */
135
139
  export function hooksHittingLock(sys) {
136
140
  const top = sys.exec('git', ['rev-parse', '--show-toplevel'], { cwd: sys.cwd });
@@ -147,7 +151,7 @@ export function hooksHittingLock(sys) {
147
151
  const judgedScript = script && !routedByHelper(sys, root, script.text) ? script : undefined;
148
152
  const [line, code] = [withoutComments(command), withoutComments(judgedScript?.text ?? '')];
149
153
  const judged = cloud ? `${line}\n${code}` : `${beforeCloudOnly(line)}\n${beforeCloudOnly(code)}`;
150
- if (reachesGitHub(withoutAsAgent(judged)))
154
+ if (reachesGitHub(withoutAsAgent(withoutRoutedSetGh(judged))))
151
155
  found.push(`${file} ${event} ${kind} ${judgedScript ? judgedScript.path : JSON.stringify(command)}`);
152
156
  }
153
157
  return found;
@@ -308,6 +312,27 @@ function routedByHelper(sys, root, script) {
308
312
  }
309
313
  });
310
314
  }
315
+ /** A command that only puts gh in the positional parameters, for `"$@"` to run: `set -- gh …`, with no command substitution in it. */
316
+ const SET_GH = /^\s*(?:(?:then|else|do)\s+)?set\s+--\s+gh(?:\s(?:(?!\$\(|`).)*)?$/;
317
+ /** One that puts `as-agent` running gh there instead: `set -- "$cli" as-agent --session "$sid" -- gh …`. */
318
+ const SET_AS_AGENT_GH = /^\s*(?:(?:then|else|do)\s+)?set\s+--\s+.*\bas-agent\s.*\s--\s+gh(?:\s|$)/;
319
+ /**
320
+ * The text without its `set -- gh` commands, when it also has a
321
+ * `set -- … as-agent … -- gh`: a script that builds its gh in the
322
+ * positional parameters and runs it as `"$@"` has gh plain only where it
323
+ * finds no niyam to run `as-agent` with, which is the cloud, whose proxy
324
+ * authenticates it; a Mac with no launcher is `doctor`'s own ✗ already
325
+ * (`launcherFinding`). Each other command is kept, so a plain `gh` call in
326
+ * the same script is still judged, and so is a `set -- gh` that runs a
327
+ * command substitution. Without the `as-agent` form, `set -- gh` is kept:
328
+ * its `"$@"` runs gh plain.
329
+ */
330
+ function withoutRoutedSetGh(text) {
331
+ const commands = text.split(/[;&|\n]+/);
332
+ if (!commands.some((command) => SET_AS_AGENT_GH.test(command)))
333
+ return text;
334
+ return commands.filter((command) => !SET_GH.test(command)).join('\n');
335
+ }
311
336
  /** The text with each command that runs `as-agent` taken out, the rest of its line kept. */
312
337
  function withoutAsAgent(text) {
313
338
  return text.split(/[;&|\n]+/).filter((command) => !/\bas-agent\b/.test(command)).join('\n');
package/dist/onboard.js CHANGED
@@ -127,11 +127,103 @@ export const OLDER_PRE_PUSH_HOOKS = [
127
127
  '',
128
128
  ].join('\n'),
129
129
  ];
130
+ /** A line that runs the CLI's pre-push, in a hook or the body it hands off to. */
131
+ const RUNS_PRE_PUSH = /^\s*(?:exec\s+)?(?:"\$cli"|niyam)\s+pre-push\b/m;
132
+ /** A hook's hand-off to a body under dvara (`exec node_modules/.bin/dvara run "$B" "$@"`), the body as written. */
133
+ const DVARA_HANDOFF = /^\s*(?:exec\s+)?(?:\S*\/)?dvara\s+run\s+(\S+)\s+"\$@"\s*$/;
134
+ /** A plain assignment of a literal, `B=path` or `B="path"`. */
135
+ const ASSIGNMENT = /^\s*([A-Za-z_]\w*)=(\S+)\s*$/;
136
+ /** A word with its one pair of surrounding quotes taken off. */
137
+ function unquoted(word) {
138
+ const match = /^(["'])(.*)\1$/.exec(word);
139
+ return match ? match[2] : word;
140
+ }
141
+ /** A path inside `root` with its `.` and `..` read, lexically (pnpm's links stay as written), or undefined when it leaves `root`. */
142
+ function insideRoot(root, path) {
143
+ const parts = [];
144
+ for (const part of (path.startsWith('/') ? path : `${root}/${path}`).split('/')) {
145
+ if (part === '' || part === '.')
146
+ continue;
147
+ if (part === '..')
148
+ parts.pop();
149
+ else
150
+ parts.push(part);
151
+ }
152
+ const full = `/${parts.join('/')}`;
153
+ return full.startsWith(`${root}/`) ? full : undefined;
154
+ }
155
+ /**
156
+ * The body a hook hands its push to through `dvara run <file> "$@"`, when
157
+ * that body runs the CLI's pre-push (#540): one uncommented hand-off, its
158
+ * file a literal or one plain variable assigned once in the hook, inside the
159
+ * repository and there. dvara runs the file under bash with git's arguments
160
+ * and stdin, and skips nothing. Anything else is undefined, so the hook stays
161
+ * the client's own: a false "already wired" would leave an agent's push
162
+ * unchecked.
163
+ */
164
+ /**
165
+ * How deep in the shell's blocks each line starts: `if`/`fi`, `case`/`esac`,
166
+ * `for`, `while` and `until` to `done`, and `{` to `}` (not `${`), counted as
167
+ * text. A line inside one may not run, so only depth 0 is taken as run.
168
+ */
169
+ function blockDepths(lines) {
170
+ let depth = 0;
171
+ return lines.map((line) => {
172
+ const at = depth;
173
+ const count = (pattern, text) => text.match(pattern)?.length ?? 0;
174
+ const braces = line.replace(/\$\{[^}]*\}/g, '');
175
+ depth +=
176
+ count(/(?:^|[;&|(]\s*)(?:if|case|for|while|until)\b/g, line) -
177
+ count(/(?:^|[;&|]\s*)(?:fi|esac|done)\b/g, line) +
178
+ count(/\{/g, braces) -
179
+ count(/\}/g, braces);
180
+ return at;
181
+ });
182
+ }
183
+ function handedOffBody(sys, root, text) {
184
+ const lines = text.split('\n').filter((line) => !/^\s*#/.test(line));
185
+ const depths = blockDepths(lines);
186
+ const handoffs = lines.flatMap((line, at) => {
187
+ const word = DVARA_HANDOFF.exec(line)?.[1];
188
+ return word === undefined ? [] : [{ word, at }];
189
+ });
190
+ const handoff = handoffs.length === 1 ? handoffs[0] : undefined;
191
+ if (handoff === undefined || depths[handoff.at] !== 0)
192
+ return undefined;
193
+ let target = unquoted(handoff.word);
194
+ const variable = /^\$(?:([A-Za-z_]\w*)|\{([A-Za-z_]\w*)\})$/.exec(target);
195
+ if (variable) {
196
+ // Every line that sets the variable, in any form (`export B=`, `B+=`, `x && B=`), counts: one alone, plain, before the hand-off.
197
+ const name = variable[1] ?? variable[2];
198
+ const sets = new RegExp(`(?:^|[^\\w$])${name}\\+?=`);
199
+ const setting = lines.flatMap((line, at) => (sets.test(line) ? [{ line, at }] : []));
200
+ const only = setting.length === 1 ? setting[0] : undefined;
201
+ const match = only && only.at < handoff.at && depths[only.at] === 0 ? ASSIGNMENT.exec(only.line) : null;
202
+ if (!match || match[1] !== name)
203
+ return undefined;
204
+ target = unquoted(match[2]);
205
+ }
206
+ if (/[$`*?\\]/.test(target))
207
+ return undefined;
208
+ const body = insideRoot(root, target);
209
+ if (body === undefined || !sys.exists(body))
210
+ return undefined;
211
+ let bodyText;
212
+ try {
213
+ bodyText = sys.read(body);
214
+ }
215
+ catch {
216
+ // fail-open: a folder or a file it cannot read is a hand-off it cannot check, so the hook stays the client's own.
217
+ return undefined;
218
+ }
219
+ return RUNS_PRE_PUSH.test(bodyText) ? body.slice(root.length + 1) : undefined;
220
+ }
130
221
  /**
131
222
  * The pre-push git hook git runs in the repository at `root`, and how it
132
223
  * stands: outside the repository (a shared hooks folder, the owner's to
133
224
  * change), missing, one an older release wrote, this release's or one that
134
- * runs the CLI, or the client's own.
225
+ * runs the CLI, one that hands off to a body that runs it (`body`, #540), or
226
+ * the client's own.
135
227
  */
136
228
  export function prePushHook(sys, root) {
137
229
  const path = sys.exec('git', ['rev-parse', '--git-path', 'hooks/pre-push'], { cwd: root }).stdout.trim();
@@ -144,7 +236,10 @@ export function prePushHook(sys, root) {
144
236
  const text = sys.read(file);
145
237
  if (OLDER_PRE_PUSH_HOOKS.includes(text))
146
238
  return { path, file, state: 'older' };
147
- return { path, file, state: /^\s*(?:exec\s+)?(?:"\$cli"|niyam)\s+pre-push\b/m.test(text) ? 'ours' : 'own' };
239
+ if (RUNS_PRE_PUSH.test(text))
240
+ return { path, file, state: 'ours' };
241
+ const body = handedOffBody(sys, root, text);
242
+ return body === undefined ? { path, file, state: 'own' } : { path, file, state: 'body', body };
148
243
  }
149
244
  /** How a hook finds the CLI: the launcher `machine` writes, else niyam on PATH. */
150
245
  const FIND_CLI = `cli="${HOOK_LAUNCHER}"; [ -x "$cli" ] || cli=${CLI}; `;
@@ -1609,7 +1704,8 @@ export async function onboard(sys, io, args = []) {
1609
1704
  },
1610
1705
  });
1611
1706
  }
1612
- // Every adopter: the identity guard holds where the Claude Code hooks did not run.
1707
+ // Every adopter: the identity guard holds where the Claude Code hooks did not run. A hook that runs the CLI, or hands
1708
+ // off to a body that does (`body`, #540), needs nothing.
1613
1709
  const hook = prePushHook(sys, root);
1614
1710
  const hookLine = `${CLI} pre-push "$@"`;
1615
1711
  if (hook.state === 'missing' || hook.state === 'older') {
@@ -172,7 +172,10 @@ async function cloudStart(sys) {
172
172
  ? ` Its pre-push hook ${hook.path} does not run niyam, so only the pull request's checks hold the work rules.`
173
173
  : hook.state === 'outside'
174
174
  ? ` Its pre-push hook ${hook.path} is outside the repository and was not checked, so only the pull request's checks may hold the work rules.`
175
- : '';
175
+ : hook.state === 'body'
176
+ // A body may skip niyam in a cloud session (athya-rules' does), so its hand-off is not counted as checked here (#540).
177
+ ? ` Its pre-push hook ${hook.path} hands off to ${hook.body}, which was not checked for a cloud session, so only the pull request's checks may hold the work rules.`
178
+ : '';
176
179
  const signed = cloudCommits(sys, root, envFile, asked, pinnedCloudKeys(settings, asked.login));
177
180
  return { line: `niyam: this cloud session works as ${asked.login} on GitHub.${unchecked}${signed}`, agent: asked.login };
178
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veda-hq/niyam",
3
- "version": "0.7.12",
3
+ "version": "0.7.14",
4
4
  "description": "Work rules for repos that track work in GitHub Issues: setup, agent identity, push and PR checks.",
5
5
  "license": "MIT",
6
6
  "repository": {