@veda-hq/niyam 0.7.13 → 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
@@ -1746,7 +1755,7 @@ tag and the commit. Install the attached package on each machine with pnpm,
1746
1755
  once; nothing is built on install:
1747
1756
 
1748
1757
  ```bash
1749
- pnpm add -g https://github.com/veda-hq/niyam/releases/download/v0.7.13/veda-hq-niyam-0.7.13.tgz
1758
+ pnpm add -g https://github.com/veda-hq/niyam/releases/download/v0.7.14/veda-hq-niyam-0.7.14.tgz
1750
1759
  ```
1751
1760
 
1752
1761
  The package puts one command on `PATH`, `niyam`. Since P-57 a release
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.13",
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": {