@oked/sdk 0.6.10 → 0.6.11

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.
Files changed (2) hide show
  1. package/dist/describe.js +135 -32
  2. package/package.json +2 -2
package/dist/describe.js CHANGED
@@ -7,8 +7,9 @@
7
7
  * from the `fields` payload. `describe()` returns just the title for
8
8
  * backwards-compatible single-line consumers (audit logs, SMS).
9
9
  */
10
- import { existsSync, readFileSync, readdirSync } from "node:fs";
11
- import { basename, isAbsolute, join, resolve } from "node:path";
10
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
11
+ import { homedir } from "node:os";
12
+ import { basename, dirname, isAbsolute, join, resolve } from "node:path";
12
13
  import { blankTextCommandArgs, classify, extractShellWriteOps, splitTopLevel, stripCodeHeredocBodies, stripInlineScriptBodies } from "./classify.js";
13
14
  import { TIER_ORDER } from "./degraded.js";
14
15
  const BODY_PREVIEW_MAX = 200;
@@ -251,28 +252,43 @@ function summarizeBash(command, sizeBytes, cwd) {
251
252
  kind: "file_delete",
252
253
  };
253
254
  }
254
- // git
255
- if (/\bgit\s+push\b/.test(shellText(cmd))) {
255
+ // git. Each card names the repo: the folder the command runs in (after a
256
+ // `cd` or `git -C`), shown as owner/name from its origin remote.
257
+ const gitCmd = cmd.replace(GIT_C_RE, "git ");
258
+ const repoAt = (re) => gitRepoFor(cmd, re, cwd);
259
+ const inRepo = (re) => {
260
+ const repo = repoAt(re);
261
+ return repo ? ` in ${repo.name}` : "";
262
+ };
263
+ if (/\bgit\s+push\b/.test(shellText(gitCmd))) {
256
264
  // Parse the remote + branch ignoring flags (-u, --force, --set-upstream, …)
257
265
  // so `git push -u origin feat` renders "feat → origin", not "origin → -u".
258
- const after = cmd.match(/\bgit\s+push\b(.*)$/s)?.[1] ?? "";
266
+ // The arguments end at a quote or a separator: `node x '["git push origin
267
+ // main","git push …"]'` pushes main, not `main","git`.
268
+ const after = gitCmd.match(/\bgit\s+push\b([^\n;&|"'`)]*)/)?.[1] ?? "";
259
269
  const args = after.split(/\s+/).filter((a) => a && !a.startsWith("-"));
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" };
270
+ const repo = repoAt(/\bgit\s+(?:-C\s+\S+\s+)?push\b/);
271
+ const remote = args[0] ?? (repo?.remotes.has("origin") ? "origin" : undefined);
272
+ const branch = args[1] ?? (args[0] === undefined ? repo?.branch : undefined);
273
+ const dest = remote ? repoSlug(repo?.remotes.get(remote)) ?? remote : undefined;
274
+ // Name the local repo when the remote did not.
275
+ const annotation = repo && dest !== repo.name && !(remote && repo.remotes.has(remote)) ? `in ${repo.name}` : undefined;
276
+ const deleted = /\s(?:--delete|-d)\b/.test(after) ? args[1] : args[1]?.match(/^\+?:([^\s:]+)$/)?.[1];
277
+ if (dest && deleted) {
278
+ return { title: "Delete remote Git branch", target: `${deleted} on ${dest}`, annotation, kind: "git_branch_delete" };
264
279
  }
265
- const target = remote && branch ? `${branch} → ${remote}` : remote || "current branch";
266
- const forced = /\bgit\s+push\b[^\n]*\s(?:--force(?:-with-lease)?|-f)\b/.test(cmd);
280
+ const target = dest && branch ? `${branch} → ${dest}` : dest || "current branch";
281
+ const forced = /\bgit\s+push\b[^\n]*\s(?:--force(?:-with-lease)?|-f)\b/.test(gitCmd);
267
282
  return forced
268
- ? { title: "Force push", target, kind: "git_force_push" }
269
- : { title: "Push", target, kind: "git_push" };
283
+ ? { title: "Force push", target, annotation, kind: "git_force_push" }
284
+ : { title: "Push", target, annotation, kind: "git_push" };
270
285
  }
271
- const branchDelete = cmd.match(/\bgit\s+branch\s+(?:-D|--delete\s+--force|-d|--delete)\s+([^\s;&|]+)/);
286
+ const BRANCH_DELETE_RE = /\bgit\s+(?:-C\s+\S+\s+)?branch\s+(?:-D|--delete\s+--force|-d|--delete)\s+([^\s;&|]+)/;
287
+ const branchDelete = cmd.match(BRANCH_DELETE_RE);
272
288
  if (branchDelete) {
273
- const forced = /\bgit\s+branch\s+(?:-D|--delete\s+--force)\b/.test(cmd);
289
+ const forced = /\bgit\s+branch\s+(?:-D|--delete\s+--force)\b/.test(gitCmd);
274
290
  return {
275
- title: "Delete local Git branch",
291
+ title: `Delete local Git branch${inRepo(BRANCH_DELETE_RE)}`,
276
292
  target: branchDelete[1],
277
293
  annotation: forced ? "even if not merged" : undefined,
278
294
  kind: "git_branch_delete",
@@ -281,21 +297,29 @@ function summarizeBash(command, sizeBytes, cwd) {
281
297
  const prisma = summarizePrismaDeploy(cmd);
282
298
  if (prisma)
283
299
  return prisma;
284
- if (/\bgit\s+reset\s+--hard\b/.test(cmd))
285
- return { title: "Hard reset — discard all local changes", kind: "git_reset_hard" };
286
- if (/\bgit\s+clean\s+-f/.test(cmd))
287
- return { title: "Remove all untracked files", kind: "git_clean" };
288
- if (/\bgit\s+checkout\s+--\s+\./.test(cmd))
289
- return { title: "Discard all unstaged changes", kind: "git_checkout" };
290
- if (/\bgit\s+restore\s+--staged\s+\./.test(cmd))
291
- return { title: "Unstage all staged changes", kind: "git_restore" };
292
- if (/\bgit\s+commit\b/.test(cmd)) {
300
+ const GIT_RESET_RE = /\bgit\s+(?:-C\s+\S+\s+)?reset\s+--hard\b/;
301
+ if (GIT_RESET_RE.test(cmd))
302
+ return { title: `Hard reset${inRepo(GIT_RESET_RE)}, discard all local changes`, kind: "git_reset_hard" };
303
+ const GIT_CLEAN_RE = /\bgit\s+(?:-C\s+\S+\s+)?clean\s+-f/;
304
+ if (GIT_CLEAN_RE.test(cmd))
305
+ return { title: `Remove all untracked files${inRepo(GIT_CLEAN_RE)}`, kind: "git_clean" };
306
+ const GIT_CHECKOUT_RE = /\bgit\s+(?:-C\s+\S+\s+)?checkout\s+--\s+\./;
307
+ if (GIT_CHECKOUT_RE.test(cmd))
308
+ return { title: `Discard all unstaged changes${inRepo(GIT_CHECKOUT_RE)}`, kind: "git_checkout" };
309
+ const GIT_RESTORE_RE = /\bgit\s+(?:-C\s+\S+\s+)?restore\s+--staged\s+\./;
310
+ if (GIT_RESTORE_RE.test(cmd))
311
+ return { title: `Unstage all staged changes${inRepo(GIT_RESTORE_RE)}`, kind: "git_restore" };
312
+ const GIT_COMMIT_RE = /\bgit\s+(?:-C\s+\S+\s+)?commit\b/;
313
+ if (GIT_COMMIT_RE.test(cmd)) {
293
314
  // Pull a simple quoted -m message. Bail (show plain "Git commit") when the
294
315
  // message is a command substitution / heredoc — `-m "$(cat <<'EOF' … )"` —
295
316
  // since that has no clean inline title to extract.
296
317
  const m = cmd.match(/-m\s+["']([^"'$]+)["']/);
297
318
  // The subject line only: a multi-line message made a multi-line title.
298
- return m ? { title: `Git commit "${truncate(m[1].split("\n")[0].trim(), 60)}"`, kind: "git_commit" } : { title: "Git commit", kind: "git_commit" };
319
+ const where = inRepo(GIT_COMMIT_RE);
320
+ return m
321
+ ? { title: `Git commit "${truncate(m[1].split("\n")[0].trim(), 60)}"${where}`, kind: "git_commit" }
322
+ : { title: `Git commit${where}`, kind: "git_commit" };
299
323
  }
300
324
  const gh = summarizeGh(cmd);
301
325
  if (gh)
@@ -429,21 +453,21 @@ function summarizeCompoundBash(cmd, sizeBytes, cwd) {
429
453
  const step = expandVars(steps[lead], vars);
430
454
  // npm publish reads the package.json a preceding `cd` points at; prisma reads
431
455
  // a DATABASE_URL an earlier step exported.
456
+ // A step runs in the folder that the `cd` steps before it moved to.
457
+ const dirAt = (i) => cwdAfterCds(steps.slice(0, i), vars, cwd);
432
458
  const r = (/\bnpm\s+publish\b/.test(step) && summarizeNpmPublish(cmd, cwd)) ||
433
459
  summarizePrismaDeploy(/\bprisma\s+migrate\s+deploy\b/.test(step) ? cmd : "") ||
434
- summarizeBash(step, sizeBytes, cwd);
460
+ summarizeBash(step, sizeBytes, dirAt(lead));
435
461
  const also = [...new Set(steps
436
- .filter((_, i) => i !== lead && rank[i] >= TIER_ORDER.review)
437
- .map((s) => describeStep(expandVars(s, vars), cwd)))].filter((t) => t !== describeStep(step, cwd));
462
+ .map((s, i) => (i !== lead && rank[i] >= TIER_ORDER.review ? describeStep(expandVars(s, vars), dirAt(i)) : ""))
463
+ .filter(Boolean))].filter((t) => t !== describeStep(step, dirAt(lead)));
438
464
  // The body shows the steps that need approval, not the setup and polling
439
- // around them; the footnote says there are other steps.
465
+ // around them.
440
466
  const risky = steps.filter((_, i) => rank[i] >= TIER_ORDER.review);
441
- const quiet = steps.length - risky.length;
442
467
  return {
443
468
  ...r,
444
469
  subline: also.length ? `Also: ${also.join("; ")}` : r.subline,
445
470
  body: r.body ?? truncateBody(risky.join("\n")),
446
- footnote: r.footnote ?? (quiet ? "Other steps in this command need no approval" : undefined),
447
471
  };
448
472
  }
449
473
  function describeStep(step, cwd) {
@@ -1725,6 +1749,85 @@ function summarizePrismaDeploy(cmd) {
1725
1749
  function shellText(cmd) {
1726
1750
  return blankTextCommandArgs(stripInlineScriptBodies(cmd));
1727
1751
  }
1752
+ const GIT_C_RE = /\bgit\s+-C\s+(?:"[^"]+"|'[^']+'|[^\s;&|]+)\s+/;
1753
+ /** The folder a shell is in after these steps, following each `cd`. Unknown
1754
+ * when a `cd` goes to a relative path with no known start, or to `$VAR`. */
1755
+ function cwdAfterCds(steps, vars, cwd) {
1756
+ let dir = cwd;
1757
+ for (const s of steps) {
1758
+ const m = s.match(/^\s*cd\s+("[^"]+"|'[^']+'|[^\s;&|]+)\s*$/);
1759
+ if (m)
1760
+ dir = resolveDir(stripQuotes(expandVars(m[1], vars)), dir);
1761
+ }
1762
+ return dir;
1763
+ }
1764
+ function resolveDir(dir, base) {
1765
+ const d = dir.replace(/^~(?=\/|$)/, homedir());
1766
+ if (d.includes("$"))
1767
+ return undefined;
1768
+ return isAbsolute(d) ? d : base ? resolve(base, d) : undefined;
1769
+ }
1770
+ /** The repo the git command matched by `re` runs in: after the `cd` steps
1771
+ * before it, and its own `git -C`. */
1772
+ function gitRepoFor(cmd, re, cwd) {
1773
+ const m = re.exec(cmd);
1774
+ if (!m)
1775
+ return null;
1776
+ const before = splitTopLevel(cmd.slice(0, m.index), { pipes: false });
1777
+ let dir = cwdAfterCds(before, literalAssignments(cmd), cwd);
1778
+ const c = m[0].match(/\bgit\s+-C\s+("[^"]+"|'[^']+'|\S+)/)?.[1];
1779
+ if (c)
1780
+ dir = resolveDir(stripQuotes(expandVars(c, literalAssignments(cmd))), dir);
1781
+ return dir ? readGitRepo(dir) : null;
1782
+ }
1783
+ function readGitRepo(start) {
1784
+ try {
1785
+ for (let top = resolve(start);; top = dirname(top)) {
1786
+ const dotGit = join(top, ".git");
1787
+ if (existsSync(dotGit)) {
1788
+ // A worktree's `.git` is a file that points at its git dir, which
1789
+ // points at the main repo's git dir (`commondir`).
1790
+ let gitDir = dotGit;
1791
+ const pointer = statSync(dotGit).isFile() ? readFileSync(dotGit, "utf8").match(/^gitdir:\s*(.+)$/m) : null;
1792
+ if (pointer)
1793
+ gitDir = resolve(top, pointer[1].trim());
1794
+ const commonFile = join(gitDir, "commondir");
1795
+ const commonDir = existsSync(commonFile) ? resolve(gitDir, readFileSync(commonFile, "utf8").trim()) : gitDir;
1796
+ const remotes = new Map();
1797
+ let section;
1798
+ for (const line of readFileSync(join(commonDir, "config"), "utf8").split("\n")) {
1799
+ const head = line.match(/^\s*\[\s*remote\s+"([^"]+)"\s*\]/);
1800
+ if (head)
1801
+ section = head[1];
1802
+ else if (/^\s*\[/.test(line))
1803
+ section = undefined;
1804
+ else if (section && !remotes.has(section)) {
1805
+ const url = line.match(/^\s*url\s*=\s*(.+?)\s*$/)?.[1];
1806
+ if (url)
1807
+ remotes.set(section, url);
1808
+ }
1809
+ }
1810
+ const head = existsSync(join(gitDir, "HEAD")) ? readFileSync(join(gitDir, "HEAD"), "utf8") : "";
1811
+ const mainTop = basename(commonDir) === ".git" ? dirname(commonDir) : top;
1812
+ return {
1813
+ name: repoSlug(remotes.get("origin")) ?? basename(mainTop),
1814
+ remotes,
1815
+ branch: head.match(/^ref:\s*refs\/heads\/(.+?)\s*$/m)?.[1],
1816
+ };
1817
+ }
1818
+ if (dirname(top) === top)
1819
+ return null;
1820
+ }
1821
+ }
1822
+ catch {
1823
+ return null;
1824
+ }
1825
+ }
1826
+ /** `git@github.com:oked-ai/oked-sdk.git` or `https://github.com/oked-ai/oked-sdk` → `oked-ai/oked-sdk`. */
1827
+ function repoSlug(url) {
1828
+ const path = url?.match(/^(?:[a-z][a-z0-9+.-]*:\/\/[^/]+\/|[^@\s/]+@[^:\s]+:)(.+?)(?:\.git)?\/?$/i)?.[1];
1829
+ return path?.split("/").slice(-2).join("/") || undefined;
1830
+ }
1728
1831
  /** `NAME=value` assignments with a literal value (no `$(…)`, no backticks), in order. */
1729
1832
  function literalAssignments(cmd) {
1730
1833
  const vars = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oked/sdk",
3
- "version": "0.6.10",
3
+ "version": "0.6.11",
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.10"
48
+ "@oked/receipt-core": "0.6.11"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/node": "^22.13.10",