@oked/sdk 0.6.16 → 0.6.18

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,5 +1,11 @@
1
1
  import type { RiskTier } from "./types.js";
2
2
  export declare function classify(toolName: string, toolInput: Record<string, unknown>): RiskTier;
3
+ /**
4
+ * Tier of each step of a compound command, read the way the whole command is
5
+ * read: a step sees the temp files and temp folders that earlier steps made
6
+ * (`cat > _s.mjs <<EOF … && rm _s.mjs`). `classify` of one step alone does not.
7
+ */
8
+ export declare function classifyBashSteps(steps: string[]): RiskTier[];
3
9
  /** Split a shell command into top-level segments on the operators that
4
10
  * sequence separate commands: `|`, `||`, `&&`, `;`, and a bare newline (a
5
11
  * newline separates commands in a multi-line script exactly like `;`).
package/dist/classify.js CHANGED
@@ -146,6 +146,12 @@ const DELETE_LEAD = /^(?:(?:sudo|command|exec|builtin|time|nice|xargs|then|do|el
146
146
  // quoted DATA (echo text, a commit message) doesn't match.
147
147
  const DELETE_COMMAND_RE = /(?:^|[\n`({]|\$\()\s*(?:rm|rmdir|trash|trash-put)\b/;
148
148
  const NPX_DEPLOY_RE = /\bnpx\s+.*\s+deploy\b/;
149
+ const NPM_PUBLISH_RE = /\bnpm\s+publish\b/;
150
+ // `--help` prints help and `--dry-run` only shows what would happen. A publish
151
+ // or deploy step with one of these flags changes nothing (`railway up --help`,
152
+ // `npx wrangler deploy --dry-run`). `--dry-run=false` is a real run.
153
+ const PREVIEW_FLAG_RE = /\s--(?:help|dry-run)(?:=true)?(?=\s|$)/;
154
+ const PREVIEWABLE_COMMANDS = new Set([NPM_PUBLISH_RE, NPX_DEPLOY_RE]);
149
155
  /**
150
156
  * `prisma migrate deploy` against a DATABASE_URL on this machine (localhost,
151
157
  * 127.0.0.1, ::1) set in the same command: it changes a local test database,
@@ -195,7 +201,7 @@ const HIGH_STAKES_COMMANDS = [
195
201
  // (`curl|wget … | sh`) genuinely span a pipe and stay in this full scan.
196
202
  /\bwget\s+.*\|\s*(bash|sh|zsh)\b/,
197
203
  /\bcurl\s+.*\|\s*(bash|sh|zsh)\b/,
198
- /\bnpm\s+publish\b/,
204
+ NPM_PUBLISH_RE,
199
205
  /\bnpm\s+unpublish\b/,
200
206
  NPX_DEPLOY_RE,
201
207
  // NOTE: ssh is intentionally NOT a blanket high_stakes here. ssh is a
@@ -596,6 +602,23 @@ export function classify(toolName, toolInput) {
596
602
  // Unknown tool - default to review (require approval)
597
603
  return "review";
598
604
  }
605
+ /**
606
+ * Tier of each step of a compound command, read the way the whole command is
607
+ * read: a step sees the temp files and temp folders that earlier steps made
608
+ * (`cat > _s.mjs <<EOF … && rm _s.mjs`). `classify` of one step alone does not.
609
+ */
610
+ export function classifyBashSteps(steps) {
611
+ const vars = new Set();
612
+ for (const step of steps)
613
+ for (const stage of splitTopLevel(step))
614
+ collectMktempVars(stage, vars);
615
+ return steps.map((step) => {
616
+ const tier = classifyBashCommand(step, vars);
617
+ for (const stage of splitTopLevel(step))
618
+ noteEphemeralEffects(stripHeredocBodies(stage), vars);
619
+ return tier;
620
+ });
621
+ }
599
622
  function maxTier(a, b) {
600
623
  return TIER_ORDER[a] >= TIER_ORDER[b] ? a : b;
601
624
  }
@@ -735,6 +758,24 @@ function isEphemeralOnlyDeletion(command, ephemeralVars = new Set()) {
735
758
  (ephemeralVars.has(TEMP_CWD) && /^[\w.-][\w./-]*$/.test(t) && !t.split("/").includes(".."));
736
759
  });
737
760
  }
761
+ // Build output folders in the project. The next build makes them again, so
762
+ // deleting them is a reversible local change.
763
+ const BUILD_OUTPUT_DIRS = new Set(["build", "dist", ".build", "DerivedData", ".next"]);
764
+ // rm/trash whose every target is a relative path inside a build output folder
765
+ // (`rm -rf build/OKed.xcarchive dist`). An absolute path, `..`, a variable or a
766
+ // glob in the first segment can point anywhere, so it stays a real deletion.
767
+ function isBuildOutputOnlyDeletion(command) {
768
+ const m = command.match(/^(?:rm|trash|trash-put)\b\s+(.+)$/s);
769
+ if (!m)
770
+ return false;
771
+ const targets = splitArgs(m[1]).filter((a) => !a.startsWith("-") && !/^(?:\d|&)?>>?\S+$/.test(a));
772
+ if (targets.length === 0)
773
+ return false;
774
+ return targets.every((a) => {
775
+ const parts = unquote(a).replace(/^(?:\.\/)+/, "").split("/");
776
+ return BUILD_OUTPUT_DIRS.has(parts[0]) && !parts.includes("..");
777
+ });
778
+ }
738
779
  /**
739
780
  * Classifies a single stage that begins with one or more `NAME=value`
740
781
  * assignments (a pure assignment like `TARGET=abc`, an env prefix like
@@ -1082,6 +1123,9 @@ function classifyBashCommand(command, ephemeralVars = new Set()) {
1082
1123
  if (pattern === NPX_DEPLOY_RE && (isLocalPrismaDeploy(stripped) ||
1083
1124
  (ephemeralVars.has(LOCAL_DB) && /\bprisma\s+migrate\s+deploy\b/.test(stripped))))
1084
1125
  continue;
1126
+ if (PREVIEWABLE_COMMANDS.has(pattern) &&
1127
+ !splitTopLevel(stripped).some((s) => pattern.test(s) && !PREVIEW_FLAG_RE.test(s)))
1128
+ continue;
1085
1129
  if (pattern.test(stripped))
1086
1130
  return "high_stakes";
1087
1131
  }
@@ -1145,6 +1189,8 @@ function classifyStageShellEffects(stage, ephemeralVars = new Set()) {
1145
1189
  const gitTier = classifyGitGlobalOptsStage(stage, ephemeralVars);
1146
1190
  if (gitTier !== null)
1147
1191
  return gitTier;
1192
+ if (isBuildOutputOnlyDeletion(stage.replace(DELETE_LEAD, "")))
1193
+ return "warning";
1148
1194
  // rmdir removes only EMPTY folders (it fails on a folder with files), so no
1149
1195
  // data is lost and `mkdir` undoes it: a reversible local change.
1150
1196
  const deleteStage = stage.replace(DELETE_LEAD, "");
@@ -1169,6 +1215,8 @@ function classifyStageShellEffects(stage, ephemeralVars = new Set()) {
1169
1215
  // without Face ID.
1170
1216
  if (isRestartableKill(bareStage))
1171
1217
  return "review";
1218
+ if (/^railway\s/.test(bareStage) && PREVIEW_FLAG_RE.test(bareStage))
1219
+ return "safe";
1172
1220
  for (const pattern of HIGH_STAKES_STAGE) {
1173
1221
  if (pattern.test(bareStage))
1174
1222
  return "high_stakes";
package/dist/describe.js CHANGED
@@ -10,7 +10,7 @@
10
10
  import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
11
11
  import { homedir } from "node:os";
12
12
  import { basename, dirname, isAbsolute, join, resolve } from "node:path";
13
- import { blankTextCommandArgs, classify, extractShellWriteOps, splitTopLevel, stripCodeHeredocBodies, stripInlineScriptBodies } from "./classify.js";
13
+ import { blankTextCommandArgs, classify, classifyBashSteps, extractShellWriteOps, splitTopLevel, stripCodeHeredocBodies, stripInlineScriptBodies } from "./classify.js";
14
14
  import { TIER_ORDER } from "./degraded.js";
15
15
  const BODY_PREVIEW_MAX = 200;
16
16
  const COMMAND_INLINE_MAX = 50;
@@ -131,7 +131,10 @@ function summarizeMutatingCurl(command) {
131
131
  // word char before the `-`, so `\b-X` never matches the usual ` -X DELETE`.
132
132
  const methodM = cmd.match(/(?:^|\s)-X\s*(DELETE|POST|PUT|PATCH)\b|(?:^|\s)--request[=\s]+(DELETE|POST|PUT|PATCH)\b/i);
133
133
  const method = (methodM?.[1] || methodM?.[2] || "POST").toUpperCase();
134
- const url = cmd.match(/https?:\/\/[^\s'"]+/)?.[0] || "";
134
+ // In `$(curl … https://host/path)` the `)` closes the substitution, not the URL.
135
+ let url = cmd.match(/https?:\/\/[^\s'"]+/)?.[0] || "";
136
+ while (url.endsWith(")") && url.split("(").length < url.split(")").length)
137
+ url = url.slice(0, -1);
135
138
  const host = url ? extractHost(url) : "URL";
136
139
  const kind = method === "DELETE" ? "http_delete" : method === "PUT" ? "http_put" : "http_post";
137
140
  // The endpoint path and the payload say what the request does; the raw
@@ -459,8 +462,9 @@ function summarizeCompoundBash(cmd, sizeBytes, cwd) {
459
462
  const steps = splitTopLevel(cmd, { pipes: false });
460
463
  if (steps.length < 2)
461
464
  return null;
462
- const tiers = steps.map((s) => classify("Bash", { command: s }));
463
- const rank = tiers.map((t) => TIER_ORDER[t]);
465
+ // Each step sees what earlier steps made (a temp file a later `rm` deletes),
466
+ // as the whole command does. Alone, that `rm` would outrank the real step.
467
+ const rank = classifyBashSteps(steps).map((t) => TIER_ORDER[t]);
464
468
  const top = Math.max(...rank);
465
469
  if (top < TIER_ORDER.review)
466
470
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oked/sdk",
3
- "version": "0.6.16",
3
+ "version": "0.6.18",
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.16"
48
+ "@oked/receipt-core": "0.6.18"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/node": "^22.13.10",