@hasna/hooks 0.12.10 → 0.12.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.
Files changed (57) hide show
  1. package/README.md +69 -6
  2. package/bin/hooks/stop-review.js +1235 -0
  3. package/bin/hooks-mcp.js +298 -85
  4. package/bin/index.js +3309 -1086
  5. package/bin/native-readiness-entry.js +5 -1
  6. package/bin/native-safety-entry.js +27 -25
  7. package/bin/native-safety-worker.cjs +27 -25
  8. package/bin/serve.js +1362 -70
  9. package/dist/db/remote-storage.d.ts +7 -1
  10. package/dist/index.d.ts +4 -1
  11. package/dist/index.js +1665 -257
  12. package/dist/lib/codex-safety-check.d.ts +1 -1
  13. package/dist/lib/hosted-registry.d.ts +70 -0
  14. package/dist/lib/legacy-registry-artifact.d.ts +23 -0
  15. package/dist/lib/manifest.d.ts +7 -0
  16. package/dist/lib/native-safety.d.ts +1 -0
  17. package/dist/lib/native-target.d.ts +7 -0
  18. package/dist/lib/opencode-stop-plugin.d.ts +55 -0
  19. package/dist/lib/registry-artifact.d.ts +36 -0
  20. package/dist/lib/registry.d.ts +4 -0
  21. package/dist/lib/stop-review-context.d.ts +12 -0
  22. package/dist/lib/store.d.ts +4 -0
  23. package/dist/lib/sumi-stop-plugin.d.ts +129 -0
  24. package/dist/lib/sync.d.ts +10 -0
  25. package/dist/lib/transport.d.ts +1 -1
  26. package/dist/native-readiness.d.ts +1 -1
  27. package/dist/native-readiness.js +5 -1
  28. package/dist/openapi.d.ts +160 -16
  29. package/dist/opencode-stop-plugin.js +384 -0
  30. package/dist/registry-auth.d.ts +5 -0
  31. package/dist/registry-http.d.ts +4 -0
  32. package/dist/sdk/generated.d.ts +32 -12
  33. package/dist/sdk/index.d.ts +28 -5
  34. package/dist/sdk/index.js +49 -24
  35. package/dist/sdk/registry-client.d.ts +1 -1
  36. package/dist/serve.d.ts +4 -4
  37. package/dist/storage.js +33 -2
  38. package/dist/sumi-stop-plugin.js +380 -0
  39. package/hooks/hook-stop-review/README.md +109 -0
  40. package/hooks/hook-stop-review/manifest.json +10 -0
  41. package/hooks/hook-stop-review/package.json +11 -0
  42. package/hooks/hook-stop-review/src/hook.ts +59 -0
  43. package/hooks/hook-stop-review/src/index.ts +3 -0
  44. package/hooks/hook-stop-review/src/jev.ts +121 -0
  45. package/hooks/hook-stop-review/src/native.ts +246 -0
  46. package/hooks/hook-stop-review/src/policy.ts +145 -0
  47. package/hooks/hook-stop-review/src/runtime.ts +309 -0
  48. package/hooks/hook-stop-review/src/types.ts +121 -0
  49. package/hooks/hook-trash-guard/src/hook.ts +96 -0
  50. package/hooks/hook-workspace-repos-guard/src/hook.ts +116 -3
  51. package/hooks/local-executors.ts +841 -0
  52. package/package.json +22 -4
  53. package/scripts/build-bundled-hooks.ts +40 -0
  54. package/scripts/validate-bundled-hooks.ts +78 -0
  55. package/scripts/validate-opencode-stop-plugin.ts +42 -0
  56. package/scripts/validate-package.ts +9 -0
  57. package/scripts/validate-sumi-stop-plugin.ts +47 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/hooks",
3
- "version": "0.12.10",
3
+ "version": "0.12.14",
4
4
  "description": "Open source hooks library for AI coding agents - Install safety, quality, and automation hooks with a single command",
5
5
  "type": "module",
6
6
  "bin": {
@@ -28,12 +28,20 @@
28
28
  "./native-readiness": {
29
29
  "types": "./dist/native-readiness.d.ts",
30
30
  "import": "./dist/native-readiness.js"
31
+ },
32
+ "./sumi-stop-plugin": {
33
+ "types": "./dist/lib/sumi-stop-plugin.d.ts",
34
+ "import": "./dist/sumi-stop-plugin.js"
35
+ },
36
+ "./opencode-stop-plugin": {
37
+ "types": "./dist/lib/opencode-stop-plugin.d.ts",
38
+ "import": "./dist/opencode-stop-plugin.js"
31
39
  }
32
40
  },
33
41
  "main": "./dist/index.js",
34
42
  "types": "./dist/index.d.ts",
35
43
  "scripts": {
36
- "build": "rm -rf dist bin && bun build ./src/cli/index.tsx --outdir ./bin --target bun --external pg --external ink --external react --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/serve.ts --outdir ./bin --target bun --external pg && bun build ./src/mcp/hooks-mcp.ts --outdir ./bin --target bun --external pg --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/sdk/index.ts --outdir ./dist/sdk --target bun && bun build ./src/index.ts ./src/storage.ts --outdir ./dist --target bun --external pg && bun run build:native-safety && bun run build:native-safety-worker && bun run build:types",
44
+ "build": "rm -rf dist bin && bun build ./src/cli/index.tsx --outdir ./bin --target bun --external pg --external ink --external react --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/serve.ts --outdir ./bin --target bun --external pg && bun build ./src/mcp/hooks-mcp.ts --outdir ./bin --target bun --external pg --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/sdk/index.ts --outdir ./dist/sdk --target bun && bun build ./src/index.ts ./src/storage.ts --outdir ./dist --target bun --external pg && bun run build:native-safety && bun run build:native-safety-worker && bun run build:bundled-hooks && bun run build:sumi-stop-plugin && bun run build:opencode-stop-plugin && bun run build:types",
37
45
  "build:types": "tsc -p tsconfig.build.json",
38
46
  "generate:sdk": "bun run scripts/generate-sdk.ts",
39
47
  "verify:generated": "bun run scripts/verify-generated-artifacts.ts",
@@ -47,7 +55,10 @@
47
55
  "prepack": "bun run build && bun run scan:artifact",
48
56
  "build:native-safety": "bun build ./hooks/native-safety-entry.ts --outdir ./bin --target bun --minify --define import.meta.main=false && bun build ./src/native-safety.ts --outdir ./dist --target bun && bun run build:native-readiness",
49
57
  "build:native-readiness": "bun build ./hooks/native-readiness-entry.ts --outdir ./bin --target node --format=cjs && bun build ./src/native-readiness.ts --outdir ./dist --target bun",
50
- "build:native-safety-worker": "bun scripts/build-native-worker.ts"
58
+ "build:native-safety-worker": "bun scripts/build-native-worker.ts",
59
+ "build:bundled-hooks": "bun scripts/build-bundled-hooks.ts",
60
+ "build:sumi-stop-plugin": "bun build ./src/lib/sumi-stop-plugin.ts --outdir ./dist --target node",
61
+ "build:opencode-stop-plugin": "bun build ./src/lib/opencode-stop-plugin.ts --outdir ./dist --target node"
51
62
  },
52
63
  "keywords": [
53
64
  "claude-code",
@@ -73,18 +84,25 @@
73
84
  "@hasna/skills": "0.10.55",
74
85
  "@hasna/events": "^0.1.16",
75
86
  "@hasna/secrets": "0.4.2",
76
- "@modelcontextprotocol/sdk": "^1.26.0",
87
+ "@modelcontextprotocol/sdk": "1.31.0",
77
88
  "chalk": "^5.3.0",
78
89
  "commander": "^12.1.0",
79
90
  "conf": "^13.0.1",
91
+ "fast-uri": "3.1.7",
80
92
  "ink": "^5.0.1",
81
93
  "ink-select-input": "^6.0.0",
82
94
  "ink-spinner": "^5.0.0",
83
95
  "ink-text-input": "^6.0.0",
84
96
  "pg": "^8.16.3",
97
+ "proxy-addr": "2.0.8",
85
98
  "react": "^18.2.0",
86
99
  "zod": "3.25.76"
87
100
  },
101
+ "overrides": {
102
+ "@modelcontextprotocol/sdk": "1.31.0",
103
+ "fast-uri": "3.1.7",
104
+ "proxy-addr": "2.0.8"
105
+ },
88
106
  "engines": {
89
107
  "bun": ">=1.0.0"
90
108
  },
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env bun
2
+ /** Build the execution closure of builtins which import package-owned sources. */
3
+ import { mkdirSync, writeFileSync } from "node:fs";
4
+ import { builtinModules } from "node:module";
5
+ import { dirname, join, resolve } from "node:path";
6
+ import { HOOKS } from "../src/lib/registry";
7
+ import { assertContained, parseManifest, resolveScript } from "../src/lib/manifest";
8
+
9
+ export async function buildBundledHooks(packageDir = resolve(import.meta.dir, "..")): Promise<string[]> {
10
+ const built: Array<{ path: string; source: string }> = [];
11
+ const builtins = new Set(builtinModules.map((name) => name.replace(/^node:/, "")));
12
+ for (const meta of HOOKS) {
13
+ if (!meta.bundledEntry) continue;
14
+ if (!/^bin\/hooks\/[a-z0-9-]+\.js$/.test(meta.bundledEntry)) throw new Error(`Invalid bundled entry for ${meta.name}`);
15
+ const hookDir = join(packageDir, "hooks", `hook-${meta.name}`);
16
+ const manifest = parseManifest(await Bun.file(join(hookDir, "manifest.json")).text());
17
+ if (manifest.script_kind !== "file") throw new Error(`Bundled hook ${meta.name} requires an explicit file entry`);
18
+ const entry = resolveScript(manifest, hookDir);
19
+ const result = await Bun.build({ entrypoints: [join(hookDir, entry.path)], target: "bun", format: "esm", throw: false });
20
+ if (!result.success || result.outputs.length !== 1) throw new Error(`Bundled hook ${meta.name} failed: ${result.logs.map(String).join("\n")}`);
21
+ const source = await result.outputs[0]!.text();
22
+ // Installed execution must not depend on package sources or dependencies
23
+ // omitted from the tarball. Node builtins are the only external imports.
24
+ const imports = new Bun.Transpiler({ loader: "js" }).scanImports(source.replace(/^#![^\n]*\n/, ""));
25
+ if (imports.some(({ path }) => !builtins.has(path.replace(/^node:/, "")))) {
26
+ throw new Error(`Bundled hook ${meta.name} has an external non-builtin import`);
27
+ }
28
+ built.push({ path: assertContained(join(packageDir, meta.bundledEntry), packageDir), source });
29
+ }
30
+ for (const output of built) {
31
+ mkdirSync(dirname(output.path), { recursive: true });
32
+ writeFileSync(output.path, output.source, { mode: 0o644 });
33
+ }
34
+ return built.map(({ path }) => path);
35
+ }
36
+
37
+ if (import.meta.main) {
38
+ const paths = await buildBundledHooks();
39
+ console.log(`Built ${paths.length} self-contained builtin hook entries`);
40
+ }
@@ -0,0 +1,78 @@
1
+ /** Exercise build-owned builtin entries in the actual extracted npm package. */
2
+ import { spawnSync } from "node:child_process";
3
+ import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join, resolve } from "node:path";
6
+ import { validatePackedSumiStopPlugin } from "./validate-sumi-stop-plugin";
7
+ import { validatePackedOpenCodeStopPlugin } from "./validate-opencode-stop-plugin";
8
+
9
+ export function validatePackedBundledHooks(packageDir: string, consumerDir: string): void {
10
+ if (existsSync(join(packageDir, "src"))) throw new Error("Packed builtin proof must not have the unshipped app source tree");
11
+ const probe = join(consumerDir, "bundled-hook-probe.ts");
12
+ // Import the packed SDK, so an accidental SDK-only raw-source fallback is
13
+ // covered as well as the CLI's shared resolver. The hook itself runs with no
14
+ // credentials, configuration or model request, in a synthetic home.
15
+ writeFileSync(probe, `
16
+ import { strict as assert } from "node:assert";
17
+ import { spawnSync } from "node:child_process";
18
+ import { existsSync, renameSync } from "node:fs";
19
+ import { join } from "node:path";
20
+ import { HOOKS, resolveScriptPath, runHook } from ${JSON.stringify(join(packageDir, "dist/index.js"))};
21
+ const entries = HOOKS.filter((meta) => meta.bundledEntry);
22
+ assert(entries.length > 0, "expected a build-owned builtin execution entry");
23
+ for (const meta of entries) {
24
+ const expected = join(${JSON.stringify(packageDir)}, meta.bundledEntry);
25
+ assert.equal(resolveScriptPath(meta.name), expected);
26
+ assert(existsSync(expected), "declared bundle must be packed");
27
+ // Stop review is deliberately disabled: this proves execution closure, not
28
+ // provider assessment or native continuation, and performs no model call.
29
+ if (meta.name === "stop-review") {
30
+ const result = spawnSync(process.execPath, [expected, "--harness", "claude"], {
31
+ cwd: ${JSON.stringify(consumerDir)}, encoding: "utf8", input: "{}", timeout: 5000,
32
+ env: { PATH: process.env.PATH, HOME: process.env.HOME, HOOKS_STOP_REVIEW_DISABLED: "1" }
33
+ });
34
+ assert.equal(result.status, 0, result.stderr);
35
+ assert.deepEqual(JSON.parse(result.stdout), {});
36
+ assert.equal(JSON.parse(result.stderr).hook, "stop-review");
37
+ const sdk = await runHook(meta.name, {}, { target: "claude" });
38
+ assert.equal(sdk.exitCode, 0, sdk.stderr);
39
+ assert.deepEqual(sdk.output, {});
40
+ }
41
+ // A broken published bundle must fail closed even while its historical raw
42
+ // hook source is still packed. Only this synthetic extracted copy is moved.
43
+ const missing = expected + ".fixture-missing";
44
+ renameSync(expected, missing);
45
+ try { assert.equal(resolveScriptPath(meta.name), undefined); }
46
+ finally { renameSync(missing, expected); }
47
+ }
48
+ console.log("packed builtin resolver, execution closure, SDK dispatch and missing-bundle refusal passed");
49
+ `);
50
+ const result = spawnSync(process.execPath, [probe], {
51
+ cwd: consumerDir, encoding: "utf8", timeout: 20_000,
52
+ env: { PATH: process.env.PATH, HOME: join(consumerDir, "synthetic-home"), HASNA_HOOKS_LOCAL: "1", HOOKS_STOP_REVIEW_DISABLED: "1" },
53
+ });
54
+ if (result.status !== 0) throw new Error(`Packed builtin execution failed: ${result.stderr}`);
55
+ validatePackedSumiStopPlugin(packageDir, consumerDir);
56
+ validatePackedOpenCodeStopPlugin(packageDir, consumerDir);
57
+ }
58
+
59
+ if (import.meta.main) {
60
+ const root = resolve(import.meta.dir, "..");
61
+ const workspace = mkdtempSync(join(tmpdir(), "hooks-bundled-pack-"));
62
+ try {
63
+ const pack = spawnSync("npm", ["pack", "--json", "--ignore-scripts", "--pack-destination", workspace], { cwd: root, encoding: "utf8" });
64
+ if (pack.status !== 0) throw new Error(`npm pack failed: ${pack.stderr}`);
65
+ const [{ filename }] = JSON.parse(pack.stdout);
66
+ const packageDir = join(workspace, "package");
67
+ mkdirSync(packageDir);
68
+ const extract = spawnSync("tar", ["-xzf", join(workspace, filename), "-C", packageDir, "--strip-components=1"], { encoding: "utf8" });
69
+ if (extract.status !== 0) throw new Error(`Package extraction failed: ${extract.stderr}`);
70
+ // The root SDK exports the storage adapter. Link just its declared pg
71
+ // dependency; the hook execution bundle has no dependency tree of its own.
72
+ mkdirSync(join(packageDir, "node_modules"));
73
+ symlinkSync(join(root, "node_modules/pg"), join(packageDir, "node_modules/pg"));
74
+ if (!readFileSync(join(packageDir, "package.json"), "utf8").includes('"@hasna/hooks"')) throw new Error("Unexpected packed package");
75
+ validatePackedBundledHooks(packageDir, workspace);
76
+ console.log("Packed builtin validation passed");
77
+ } finally { rmSync(workspace, { recursive: true, force: true }); }
78
+ }
@@ -0,0 +1,42 @@
1
+ /** Prove the native OpenCode plugin's exported npm surface works without app sources. */
2
+ import { spawnSync } from "node:child_process";
3
+ import { existsSync, readFileSync, writeFileSync } from "node:fs";
4
+ import { builtinModules } from "node:module";
5
+ import { join } from "node:path";
6
+
7
+ export function validatePackedOpenCodeStopPlugin(packageDir: string, consumerDir: string): void {
8
+ const manifest = JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8"));
9
+ const entry = manifest.exports?.["./opencode-stop-plugin"];
10
+ if (!entry || typeof entry.import !== "string" || typeof entry.types !== "string") throw new Error("Native OpenCode plugin export is missing");
11
+ if (!existsSync(join(packageDir, entry.types))) throw new Error("Native OpenCode plugin declarations are not packed");
12
+ if (existsSync(join(packageDir, "src"))) throw new Error("Native plugin proof must not have app sources");
13
+ // OpenCode loads this file directly by URL: it must be self-contained.
14
+ const code = readFileSync(join(packageDir, entry.import), "utf8");
15
+ const builtins = new Set(builtinModules.map((name) => name.replace(/^node:/, "")));
16
+ if (new Bun.Transpiler({ loader: "js" }).scanImports(code.replace(/^#![^\n]*\n/, ""))
17
+ .some(({ path }) => !builtins.has(path.replace(/^node:/, "")))) throw new Error("Native OpenCode plugin depends on a non-builtin module");
18
+
19
+ const probe = join(packageDir, ".opencode-stop-plugin-probe.mjs");
20
+ writeFileSync(probe, `
21
+ import { strict as assert } from "node:assert";
22
+ import plugin, { createOpenCodeStopReviewPlugin, isOpenCodeStopReviewCapability } from "@hasna/hooks/opencode-stop-plugin";
23
+ const options = { schema: "hasna.hooks.opencode-stop-plugin.v1", hooksCommand: process.execPath, hooksCommandSha256: "0".repeat(64), runtimeCommand: process.execPath, runtimeCommandSha256: "0".repeat(64) };
24
+ const capability = Object.freeze({ schema: "hooks.stop-review.v1", admission: "parent_final_cas", harness: "opencode", hook: "experimental.session.stop", maxContinuations: 1 });
25
+ assert.equal(plugin.id, "hasna-hooks-stop-review");
26
+ assert.equal(typeof plugin.server, "function");
27
+ // A stock OpenCode passes no capability: registration must fail closed.
28
+ for (const input of [undefined, {}, { experimental_stop_review: { ...capability } }, { experimental_stop_review: Object.freeze({ ...capability, harness: "sumi" }) }]) {
29
+ await assert.rejects(plugin.server(input, options), /native_capability_unavailable/);
30
+ }
31
+ await assert.rejects(plugin.server({ experimental_stop_review: capability }, { ...options, extra: true }), /invalid_options/);
32
+ assert.equal(isOpenCodeStopReviewCapability(capability), true);
33
+ const hooks = await createOpenCodeStopReviewPlugin(options).server({ experimental_stop_review: capability });
34
+ assert.deepEqual(Object.keys(hooks), ["experimental.session.stop"]);
35
+ console.log("packed native OpenCode plugin export, dependency closure and capability-gated registration passed");
36
+ `);
37
+ const result = spawnSync("node", [probe], {
38
+ cwd: consumerDir, encoding: "utf8", timeout: 10_000,
39
+ env: { PATH: process.env.PATH, HOME: join(consumerDir, "synthetic-plugin-home") },
40
+ });
41
+ if (result.status !== 0) throw new Error(`Packed native OpenCode plugin failed: ${result.stderr}`);
42
+ }
@@ -2,6 +2,7 @@ import { spawn, spawnSync } from "node:child_process";
2
2
  import { existsSync } from "node:fs";
3
3
  import { copyFile, mkdtemp, mkdir, readFile, realpath, rm, symlink, writeFile } from "node:fs/promises";
4
4
  import { dirname, join } from "node:path";
5
+ import { validatePackedBundledHooks } from "./validate-bundled-hooks";
5
6
 
6
7
  type PackageJson = {
7
8
  name: string;
@@ -90,6 +91,8 @@ async function validateConsumerImports(pkg: PackageJson): Promise<void> {
90
91
  [
91
92
  `import { HOOKS, type HookInput } from "${pkg.name}";`,
92
93
  `import { getStorageStatus, type StorageStatus } from "${pkg.name}/storage";`,
94
+ `import { createSumiStopReviewPlugin } from "${pkg.name}/sumi-stop-plugin";`,
95
+ `import openCodeStopPlugin, { createOpenCodeStopReviewPlugin, type OpenCodeStopReviewCapability } from "${pkg.name}/opencode-stop-plugin";`,
93
96
  "",
94
97
  "const input: HookInput = { cwd: process.cwd() };",
95
98
  "const hookCount: number = HOOKS.length;",
@@ -97,6 +100,11 @@ async function validateConsumerImports(pkg: PackageJson): Promise<void> {
97
100
  "void input;",
98
101
  "void hookCount;",
99
102
  "void status;",
103
+ "void createSumiStopReviewPlugin;",
104
+ "void openCodeStopPlugin;",
105
+ "void createOpenCodeStopReviewPlugin;",
106
+ "const capability: OpenCodeStopReviewCapability | undefined = undefined;",
107
+ "void capability;",
100
108
  "",
101
109
  ].join("\n"),
102
110
  );
@@ -397,6 +405,7 @@ async function validateExtractedSmoke(pkg: PackageJson): Promise<void> {
397
405
  // pg link is best-effort; the import below will fail loudly if needed.
398
406
  }
399
407
  const sdkSmoke = join(smokeDir, "sdk-smoke.ts");
408
+ validatePackedBundledHooks(packageDir, smokeDir);
400
409
  await writeFile(sdkSmoke, `import { HOOKS, getStorageStatus } from "@hasna/hooks";\nimport { getStorageStatus as ss } from "@hasna/hooks/storage";\nimport { HooksClient, createHooksClient } from "@hasna/hooks/sdk";\nlet sdk = "threw";\ntry { createHooksClient({ env: { HASNA_STATION: "no-such-station" }, credentials: { keychain: { enabled: false } } }); sdk = "returned"; } catch (e) { sdk = /REMOTE_API_/.test(String(e)) ? "fail-closed" : "threw"; }\nconsole.log(JSON.stringify({ count: HOOKS.length, backend: getStorageStatus().backend, ss: ss().backend, sdk, hasClient: typeof HooksClient === "function" }));\n`);
401
410
  const sdk = spawnSync("bun", ["run", sdkSmoke], { cwd: smokeDir, env, encoding: "utf8", timeout: 20000 });
402
411
  if (sdk.status !== 0) throw new Error(`packed SDK import failed: ${sdk.stderr}`);
@@ -0,0 +1,47 @@
1
+ /** Prove the native plugin's exported npm surface works without app sources. */
2
+ import { spawnSync } from "node:child_process";
3
+ import { existsSync, readFileSync, writeFileSync } from "node:fs";
4
+ import { builtinModules } from "node:module";
5
+ import { join } from "node:path";
6
+
7
+ export function validatePackedSumiStopPlugin(packageDir: string, consumerDir: string): void {
8
+ const manifest = JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8"));
9
+ const entry = manifest.exports?.["./sumi-stop-plugin"];
10
+ if (!entry || typeof entry.import !== "string" || typeof entry.types !== "string") throw new Error("Native Sumi plugin export is missing");
11
+ if (!existsSync(join(packageDir, entry.types))) throw new Error("Native Sumi plugin declarations are not packed");
12
+ if (existsSync(join(packageDir, "src"))) throw new Error("Native plugin proof must not have app sources");
13
+ const code = readFileSync(join(packageDir, entry.import), "utf8");
14
+ const builtins = new Set(builtinModules.map((name) => name.replace(/^node:/, "")));
15
+ if (new Bun.Transpiler({ loader: "js" }).scanImports(code.replace(/^#![^\n]*\n/, ""))
16
+ .some(({ path }) => !builtins.has(path.replace(/^node:/, "")))) throw new Error("Native Sumi plugin depends on a non-builtin module");
17
+
18
+ // A file inside the extracted package resolves its public self-reference
19
+ // through package.json exports, exactly as an installed native consumer does.
20
+ const probe = join(packageDir, ".sumi-stop-plugin-probe.mjs");
21
+ writeFileSync(probe, `
22
+ import { strict as assert } from "node:assert";
23
+ import { createSumiStopReviewPlugin } from "@hasna/hooks/sumi-stop-plugin";
24
+ const current = { schema: "hooks.stop-review.v1", admission: "parent_final_cas", harness: "sumi" };
25
+ for (const [capability, expected] of [[undefined, 0], [{ ...current, admission: "unsupported" }, 0], [current, 1]]) {
26
+ const registrations = [];
27
+ const plugin = createSumiStopReviewPlugin({ hooksCommand: process.execPath, hooksCommandSha256: "0".repeat(64), runtimeCommand: process.execPath, runtimeCommandSha256: "0".repeat(64) });
28
+ assert.equal(plugin.id, "local.hasna-hooks-stop-review");
29
+ try {
30
+ await plugin.setup({ session: { stopReview: capability, hook: (...args) => { registrations.push(args); return () => {}; } } });
31
+ } catch (error) {
32
+ if (expected !== 0) throw error;
33
+ }
34
+ assert.equal(registrations.length, expected, "only the parent-CAS-capable Sumi interface may register the hook");
35
+ if (expected) {
36
+ assert.equal(registrations[0][0], "stop");
37
+ assert.equal(typeof registrations[0][1], "function");
38
+ }
39
+ }
40
+ console.log("packed native Sumi plugin export, dependency closure and capability-gated setup passed");
41
+ `);
42
+ const result = spawnSync("node", [probe], {
43
+ cwd: consumerDir, encoding: "utf8", timeout: 10_000,
44
+ env: { PATH: process.env.PATH, HOME: join(consumerDir, "synthetic-plugin-home") },
45
+ });
46
+ if (result.status !== 0) throw new Error(`Packed native Sumi plugin failed: ${result.stderr}`);
47
+ }