@oked/sdk 0.6.8 → 0.6.10

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.
@@ -28,6 +28,9 @@ export interface ShellWriteOp {
28
28
  * otherwise wreck classification). Only heredocs whose opener line invokes an
29
29
  * interpreter (`psql <<EOF`, `node - <<EOF`, `cat <<EOF | bash`) keep their
30
30
  * body, so SQL/code detection still runs. The opener line is always preserved.
31
+ * The closing delimiter line is kept too: without it, splitTopLevel cannot see
32
+ * where the heredoc ends and joins every later step (`; rm -rf …`) into the
33
+ * heredoc's step, where a delete or merge after `;` is not seen.
31
34
  */
32
35
  export declare function stripHeredocBodies(command: string): string;
33
36
  /**
@@ -35,13 +38,17 @@ export declare function stripHeredocBodies(command: string): string;
35
38
  * scans only (the same policy as `-e`/`-c` bodies below). A Python script that
36
39
  * edits a file whose text says "git push --force" or "npm publish" is not a
37
40
  * force push or a publish. SQL detection still reads the intact body.
41
+ * `dropClose` also drops the closing delimiter line, for card text only.
38
42
  */
39
- export declare function stripCodeHeredocBodies(command: string): string;
43
+ export declare function stripCodeHeredocBodies(command: string, opts?: {
44
+ dropClose?: boolean;
45
+ }): string;
40
46
  /**
41
47
  * Blank the inside of quoted arguments of text commands, for the SHELL-pattern
42
48
  * scans only, so `git commit -m "curl -d sends JSON"` or `grep "curl -X POST"`
43
- * is not read as a POST. A step that pipes on (`echo '…' | sh`) keeps its text,
44
- * because the next command may run it.
49
+ * is not read as a POST. A step that pipes into a command that runs its input
50
+ * (`echo '…' | sh`) keeps its text. A pipe into a reader (`grep "…" | head`)
51
+ * does not run the text, so the text is blanked.
45
52
  */
46
53
  export declare function blankTextCommandArgs(command: string): string;
47
54
  export declare function stripInlineScriptBodies(command: string): string;
package/dist/classify.js CHANGED
@@ -235,6 +235,10 @@ const HIGH_STAKES_STAGE = [
235
235
  /^git\s+branch\s+(?:-D|--delete)\b/,
236
236
  /^git\s+tag\s+(?:-d|--delete)\b/,
237
237
  /^git\s+stash\s+(?:drop|clear)\b/,
238
+ // Remote branch delete: it leaves the machine and removes the branch for
239
+ // everyone (`git push origin --delete feat`, `git push origin :feat`).
240
+ /^git\s+push\b.*\s(?:--delete|-d)\b/,
241
+ /^git\s+push\b.*\s\+?:[^\s:]+(?:\s|$)/,
238
242
  // Process termination.
239
243
  /^kill\b/,
240
244
  /^pkill\b/,
@@ -672,7 +676,8 @@ function isEphemeralOnlyDeletion(command, ephemeralVars = new Set()) {
672
676
  const m = command.match(/^(?:sudo\s+)?(?:rm|rmdir|trash|trash-put)\b\s+(.+)$/s);
673
677
  if (!m)
674
678
  return false;
675
- const targets = splitArgs(m[1]).filter((a) => !a.startsWith("-"));
679
+ // A redirect (`2>/dev/null`) is not a target.
680
+ const targets = splitArgs(m[1]).filter((a) => !a.startsWith("-") && !/^(?:\d|&)?>>?\S+$/.test(a));
676
681
  if (targets.length === 0)
677
682
  return false;
678
683
  return targets.every((a) => {
@@ -1016,7 +1021,13 @@ function classifyStageShellEffects(stage, ephemeralVars = new Set()) {
1016
1021
  // NOT a deletion — that's the false positive DELETE_COMMAND_RE avoids.
1017
1022
  if (isEphemeralOnlyDeletion(stage, ephemeralVars))
1018
1023
  return "warning";
1019
- if (DELETE_COMMAND_RE.test(stage.replace(DELETE_LEAD, "")))
1024
+ // rmdir removes only EMPTY folders (it fails on a folder with files), so no
1025
+ // data is lost and `mkdir` undoes it: a reversible local change.
1026
+ const deleteStage = stage.replace(DELETE_LEAD, "");
1027
+ const rmdirArgs = deleteStage.match(/^(?:sudo\s+)?rmdir\b(.*)$/s)?.[1];
1028
+ if (rmdirArgs !== undefined && !DELETE_COMMAND_RE.test(rmdirArgs))
1029
+ return "warning";
1030
+ if (DELETE_COMMAND_RE.test(deleteStage))
1020
1031
  return "high_stakes";
1021
1032
  // Destructive/irreversible single commands, matched at stage start with any
1022
1033
  // leading `sudo ` stripped (so `sudo mkfs …` is still caught, but the word
@@ -1147,8 +1158,13 @@ function classifySqlSeverity(sql) {
1147
1158
  // scannable. Detected as a command word at the start of the opener line or
1148
1159
  // after a pipe / `&&` / `;` / `$(` / backtick.
1149
1160
  const HEREDOC_INTERPRETER_RE = /(?:^|\||&&|;|\$\(|`)\s*(?:\w+=\S+\s+)*(?:sudo\s+)?(?:psql|mysql|mariadb|sqlite3?|node|python\d?|ruby|perl|deno|bun|bash|sh|zsh|ksh|fish)\b/;
1161
+ // Only the step that holds the heredoc reads its body. An interpreter in an
1162
+ // earlier step on the same line (`V=$(node -p '…'); cat > f <<'EOF'`) does not.
1163
+ // splitTopLevel keeps the opener and the rest of the line in the last step, so
1164
+ // a pipe after the opener (`cat <<EOF | bash`) still counts.
1150
1165
  function openerFeedsInterpreter(line) {
1151
- return HEREDOC_INTERPRETER_RE.test(line);
1166
+ const steps = splitTopLevel(line, { pipes: false });
1167
+ return HEREDOC_INTERPRETER_RE.test(steps[steps.length - 1] ?? line);
1152
1168
  }
1153
1169
  /**
1154
1170
  * Removes heredoc *bodies* unless they're fed to an interpreter/DB/shell that
@@ -1159,6 +1175,9 @@ function openerFeedsInterpreter(line) {
1159
1175
  * otherwise wreck classification). Only heredocs whose opener line invokes an
1160
1176
  * interpreter (`psql <<EOF`, `node - <<EOF`, `cat <<EOF | bash`) keep their
1161
1177
  * body, so SQL/code detection still runs. The opener line is always preserved.
1178
+ * The closing delimiter line is kept too: without it, splitTopLevel cannot see
1179
+ * where the heredoc ends and joins every later step (`; rm -rf …`) into the
1180
+ * heredoc's step, where a delete or merge after `;` is not seen.
1162
1181
  */
1163
1182
  export function stripHeredocBodies(command) {
1164
1183
  const lines = command.split("\n");
@@ -1177,7 +1196,7 @@ export function stripHeredocBodies(command) {
1177
1196
  while (i < lines.length && lines[i].trim() !== delim)
1178
1197
  i++; // drop body
1179
1198
  if (i < lines.length)
1180
- i++; // drop the closing delimiter line
1199
+ out.push(lines[i++]); // keep the closing delimiter line
1181
1200
  continue;
1182
1201
  }
1183
1202
  i++;
@@ -1192,8 +1211,9 @@ const CODE_HEREDOC_RE = /(?:^|\||&&|;|\$\(|`)\s*(?:\w+=\S+\s+)*(?:sudo\s+)?(?:ps
1192
1211
  * scans only (the same policy as `-e`/`-c` bodies below). A Python script that
1193
1212
  * edits a file whose text says "git push --force" or "npm publish" is not a
1194
1213
  * force push or a publish. SQL detection still reads the intact body.
1214
+ * `dropClose` also drops the closing delimiter line, for card text only.
1195
1215
  */
1196
- export function stripCodeHeredocBodies(command) {
1216
+ export function stripCodeHeredocBodies(command, opts = {}) {
1197
1217
  const lines = command.split("\n");
1198
1218
  const out = [];
1199
1219
  let i = 0;
@@ -1206,8 +1226,11 @@ export function stripCodeHeredocBodies(command) {
1206
1226
  i++;
1207
1227
  while (i < lines.length && lines[i].trim() !== delim)
1208
1228
  i++;
1209
- if (i < lines.length)
1229
+ if (i < lines.length) {
1230
+ if (!opts.dropClose)
1231
+ out.push(lines[i]); // keep the closing line (see stripHeredocBodies)
1210
1232
  i++;
1233
+ }
1211
1234
  continue;
1212
1235
  }
1213
1236
  i++;
@@ -1218,16 +1241,31 @@ export function stripCodeHeredocBodies(command) {
1218
1241
  // search pattern, a PR body, printed text. `gh api` is left out: its fields are
1219
1242
  // a request payload (a GraphQL mutation, for example).
1220
1243
  const TEXT_ARG_COMMAND_RE = /^(?:\w+=(?:"[^"]*"|'[^']*'|\S+)\s+)*(?:git|grep|egrep|fgrep|rg|echo|printf|gh(?!\s+api\b))\b/;
1244
+ // A script file run by node/tsx/bun (`node scripts/x.mjs …`), not inline code
1245
+ // (`node -e`). Its quoted long-option values (`--reason "…"`) are text. Its
1246
+ // quoted positional arguments keep their text, because the script may run them.
1247
+ const SCRIPT_FILE_STEP_RE = /^(?:\\\s+)?(?:\w+=(?:"[^"]*"|'[^']*'|\S+)\s+)*(?:node|tsx|bun)\s+(?!-)\S+\.[cm]?[jt]s(?:\s|$)/;
1248
+ const LONG_OPTION_QUOTED_VALUE_RE = /(--[\w-]+(?:=|\s+))('[^']*'|"(?:[^"\\]|\\.)*")/g;
1249
+ // Commands that run the text piped into them as code.
1250
+ const RUNS_PIPED_TEXT_RE = /^(?:sudo\s+)?(?:(?:ba|z|k|da)?sh|xargs|eval|source|\.|node|deno|bun|python\d?(?:\.\d+)?|ruby|perl|psql|mysql|mariadb|sqlite3?)\b/;
1221
1251
  /**
1222
1252
  * Blank the inside of quoted arguments of text commands, for the SHELL-pattern
1223
1253
  * scans only, so `git commit -m "curl -d sends JSON"` or `grep "curl -X POST"`
1224
- * is not read as a POST. A step that pipes on (`echo '…' | sh`) keeps its text,
1225
- * because the next command may run it.
1254
+ * is not read as a POST. A step that pipes into a command that runs its input
1255
+ * (`echo '…' | sh`) keeps its text. A pipe into a reader (`grep "…" | head`)
1256
+ * does not run the text, so the text is blanked.
1226
1257
  */
1227
1258
  export function blankTextCommandArgs(command) {
1228
1259
  return splitTopLevel(command, { pipes: false })
1229
1260
  .map((step) => {
1230
- if (!TEXT_ARG_COMMAND_RE.test(step) || splitTopLevel(step).length > 1)
1261
+ if (SCRIPT_FILE_STEP_RE.test(step)) {
1262
+ if (splitTopLevel(step).slice(1).some((p) => RUNS_PIPED_TEXT_RE.test(p)))
1263
+ return step;
1264
+ return step.replace(LONG_OPTION_QUOTED_VALUE_RE, (_m, opt, q) => opt + q[0] + " ".repeat(Math.max(0, q.length - 2)) + q[0]);
1265
+ }
1266
+ if (!TEXT_ARG_COMMAND_RE.test(step))
1267
+ return step;
1268
+ if (splitTopLevel(step).slice(1).some((p) => RUNS_PIPED_TEXT_RE.test(p)))
1231
1269
  return step;
1232
1270
  // A quoted redirect target (`>> "$HOME/.ssh/authorized_keys"`) is a path, not text.
1233
1271
  return step.replace(/'[^']*'|"(?:[^"\\]|\\.)*"/g, (q, at) => /[<>]\s*$/.test(step.slice(0, at)) ? q : q[0] + " ".repeat(Math.max(0, q.length - 2)) + q[0]);
package/dist/describe.js CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
  import { existsSync, readFileSync, readdirSync } from "node:fs";
11
11
  import { basename, isAbsolute, join, resolve } from "node:path";
12
- import { blankTextCommandArgs, classify, extractShellWriteOps, splitTopLevel, stripCodeHeredocBodies } from "./classify.js";
12
+ import { blankTextCommandArgs, classify, extractShellWriteOps, splitTopLevel, stripCodeHeredocBodies, stripInlineScriptBodies } from "./classify.js";
13
13
  import { TIER_ORDER } from "./degraded.js";
14
14
  const BODY_PREVIEW_MAX = 200;
15
15
  const COMMAND_INLINE_MAX = 50;
@@ -189,7 +189,7 @@ function summarizeBash(command, sizeBytes, cwd) {
189
189
  const script = summarizeCodeHeredoc(cmd);
190
190
  if (script)
191
191
  return script;
192
- cmd = stripCodeHeredocBodies(cmd);
192
+ cmd = stripCodeHeredocBodies(cmd, { dropClose: true });
193
193
  // Text piped into a shell runs as a script that no step shows by name.
194
194
  const toShell = cmd.match(/^(.*?)\|\s*(?:sudo\s+)?((?:ba|z|k|da)?sh)\b(?!\s+-n\b)/s);
195
195
  if (toShell && !/\b(?:curl|wget)\b/.test(toShell[1]) && splitTopLevel(cmd, { pipes: false }).length === 1) {
@@ -215,15 +215,24 @@ function summarizeBash(command, sizeBytes, cwd) {
215
215
  // `(.+)$`: without the `m` flag `$` only reaches end-of-string, so a `rm` on
216
216
  // an interior line of a multi-line script (e.g. `rm "$WT/node_modules"` before
217
217
  // a trailing `cd … && git status`) wouldn't match and the card fell back to
218
- // the generic "Run command".
219
- const rmMatch = cmd.match(/\b(?:rm|trash|trash-put|rmdir)\s+(?:(-rf?|-fr|--recursive|-r)\s+)?([^\n]+)/);
218
+ // the generic "Run command". Match the shell text, so `rmdir` in a commit
219
+ // message or a `--reason "…"` value is not a delete.
220
+ const rmMatch = shellText(cmd).match(/\b(rm|trash|trash-put|rmdir)\s+(?:(-rf?|-fr|--recursive|-r)\s+)?([^\n]+)/);
221
+ if (rmMatch && rmMatch[1] === "rmdir") {
222
+ // rmdir removes empty folders only.
223
+ const vars = literalAssignments(cmd);
224
+ const targets = parseRmTargets(rmMatch[3]).map((t) => shortenPath(stripQuotes(expandVars(t, vars))));
225
+ return targets.length <= 1
226
+ ? { title: "Delete empty folder", target: targets[0], kind: "file_delete" }
227
+ : { title: `Delete ${targets.length} empty folders`, target: targets.join("\n"), kind: "file_delete" };
228
+ }
220
229
  if (rmMatch) {
221
230
  // Recursive only with an r/R flag: `rm -f x` deletes one file.
222
- const flags = (rmMatch[1] ? `${rmMatch[1]} ` : "") + rmMatch[2];
231
+ const flags = (rmMatch[2] ? `${rmMatch[2]} ` : "") + rmMatch[3];
223
232
  const recursive = /(?:^|\s)(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)(?=\s|$)/.test(flags.split(/[;&|\n]/)[0]);
224
233
  // `D=/some/dir; rm -f $D/x` shows the real path, not `$D/x`.
225
234
  const vars = literalAssignments(cmd);
226
- const targets = parseRmTargets(rmMatch[2]).map((t) => expandVars(t, vars));
235
+ const targets = parseRmTargets(rmMatch[3]).map((t) => expandVars(t, vars));
227
236
  if (targets.length <= 1) {
228
237
  const target = targets[0] || "files";
229
238
  const sqlExt = !recursive && /\.sql$/i.test(target);
@@ -243,12 +252,16 @@ function summarizeBash(command, sizeBytes, cwd) {
243
252
  };
244
253
  }
245
254
  // git
246
- if (/\bgit\s+push\b/.test(cmd)) {
255
+ if (/\bgit\s+push\b/.test(shellText(cmd))) {
247
256
  // Parse the remote + branch ignoring flags (-u, --force, --set-upstream, …)
248
257
  // so `git push -u origin feat` renders "feat → origin", not "origin → -u".
249
258
  const after = cmd.match(/\bgit\s+push\b(.*)$/s)?.[1] ?? "";
250
259
  const args = after.split(/\s+/).filter((a) => a && !a.startsWith("-"));
251
260
  const [remote, branch] = args;
261
+ const deleted = /\s(?:--delete|-d)\b/.test(after) ? branch : branch?.match(/^\+?:([^\s:]+)$/)?.[1];
262
+ if (remote && deleted) {
263
+ return { title: "Delete remote Git branch", target: `${deleted} on ${remote}`, kind: "git_branch_delete" };
264
+ }
252
265
  const target = remote && branch ? `${branch} → ${remote}` : remote || "current branch";
253
266
  const forced = /\bgit\s+push\b[^\n]*\s(?:--force(?:-with-lease)?|-f)\b/.test(cmd);
254
267
  return forced
@@ -371,9 +384,9 @@ function summarizeBash(command, sizeBytes, cwd) {
371
384
  return { title: "Install npm dependencies", kind: "npm_install" };
372
385
  if (/\bnpm\s+test\b/.test(cmd))
373
386
  return { title: "Run npm tests", kind: "npm_test" };
374
- if (/\bnpm\s+unpublish\b/.test(cmd))
387
+ if (/\bnpm\s+unpublish\b/.test(shellText(cmd)))
375
388
  return { title: "Unpublish package from npm", kind: "npm_unpublish" };
376
- if (/\bnpx\s+.*\s+deploy\b/.test(cmd))
389
+ if (/\bnpx\s+.*\s+deploy\b/.test(shellText(cmd)))
377
390
  return { title: "Deploy via npx", body: truncateBody(cmd), kind: "npx_deploy" };
378
391
  // sudo / chmod (kill/pkill/killall are handled in command position above,
379
392
  // ahead of the compound catch-all).
@@ -502,9 +515,11 @@ function summarizeGh(cmd) {
502
515
  * Name/version come from the package.json the publish will read: the cwd,
503
516
  * adjusted by a preceding `cd`, a `--workspace`, or a folder argument.
504
517
  */
518
+ const NPM_PUBLISH_STEP_RE = /(?:^|[\n;&|(]|\$\()\s*(?:\w+=\S+\s+)*(?:sudo\s+)?npm\s+publish\b([^\n;&|)]*)/;
505
519
  function summarizeNpmPublish(cmd, cwd) {
506
- const m = cmd.match(/(?:^|[\n;&|(]|\$\()\s*(?:\w+=\S+\s+)*(?:sudo\s+)?npm\s+publish\b([^\n;&|)]*)/);
507
- if (!m)
520
+ const m = cmd.match(NPM_PUBLISH_STEP_RE);
521
+ // `grep "verify\|npm publish" x | head` searches for the words, it does not publish.
522
+ if (!m || !NPM_PUBLISH_STEP_RE.test(shellText(cmd)))
508
523
  return null;
509
524
  const tokens = m[1].trim().split(/\s+/).filter(Boolean).map(stripQuotes);
510
525
  const flags = {};
@@ -524,7 +539,9 @@ function summarizeNpmPublish(cmd, cwd) {
524
539
  else
525
540
  flags[name] = value;
526
541
  }
527
- else if (positional === undefined) {
542
+ else if (positional === undefined && /^[\w.~/@-][\w.~/@+-]*$/.test(t)) {
543
+ // A folder or tarball. Quote and bracket leftovers from quoted data
544
+ // (`…publish\"]'`) are not a path.
528
545
  positional = t;
529
546
  }
530
547
  }
@@ -534,7 +551,8 @@ function summarizeNpmPublish(cmd, cwd) {
534
551
  const cds = [...before.matchAll(/(?:^|[\n;&|(])\s*cd\s+("[^"]+"|'[^']+'|\S+)/g)];
535
552
  if (cds.length) {
536
553
  const dir = stripQuotes(cds[cds.length - 1][1]);
537
- baseDir = isAbsolute(dir) ? dir : baseDir ? resolve(baseDir, dir) : undefined;
554
+ // `cd $SP/pkg`: the variable is not known here, so the folder is not known.
555
+ baseDir = /[$`]/.test(dir) ? undefined : isAbsolute(dir) ? dir : baseDir ? resolve(baseDir, dir) : undefined;
538
556
  }
539
557
  const allWorkspaces = flags["--workspaces"] === true || flags["-ws"] === true;
540
558
  let pkgDirs = [];
@@ -1689,7 +1707,7 @@ function summarizeCodeHeredoc(cmd) {
1689
1707
  }
1690
1708
  /** `prisma migrate deploy`: which database, from a DATABASE_URL set in the command. */
1691
1709
  function summarizePrismaDeploy(cmd) {
1692
- if (!/\bprisma\s+migrate\s+deploy\b/.test(cmd))
1710
+ if (!/\bprisma\s+migrate\s+deploy\b/.test(shellText(cmd)))
1693
1711
  return null;
1694
1712
  const url = cmd.match(/DATABASE_URL=["']?([a-z]+:\/\/[^\s"';&|]+)/)?.[1];
1695
1713
  let db;
@@ -1702,6 +1720,11 @@ function summarizePrismaDeploy(cmd) {
1702
1720
  }
1703
1721
  return { title: "Apply Prisma database migrations", target: db, annotation: db ? undefined : "to the database in DATABASE_URL", kind: "sql_migrate" };
1704
1722
  }
1723
+ /** The command as the shell runs it: quoted text arguments and inline script
1724
+ * bodies (`node -e '…'`) blanked, so words in them do not name the card. */
1725
+ function shellText(cmd) {
1726
+ return blankTextCommandArgs(stripInlineScriptBodies(cmd));
1727
+ }
1705
1728
  /** `NAME=value` assignments with a literal value (no `$(…)`, no backticks), in order. */
1706
1729
  function literalAssignments(cmd) {
1707
1730
  const vars = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oked/sdk",
3
- "version": "0.6.8",
3
+ "version": "0.6.10",
4
4
  "description": "OKed SDK - human approval layer for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -45,7 +45,7 @@
45
45
  "access": "public"
46
46
  },
47
47
  "dependencies": {
48
- "@oked/receipt-core": "0.6.8"
48
+ "@oked/receipt-core": "0.6.10"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/node": "^22.13.10",