@darkrymit/crunes-cli 0.9.0 → 0.10.1

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 (58) hide show
  1. package/dist/chunks/add-2OM5KGN5.js +67 -0
  2. package/dist/chunks/{apply-YY7MIM64.js → apply-GBUQQ4XF.js} +21 -9
  3. package/dist/chunks/{benchmark-BQNMULOO.js → benchmark-QWGVX3BX.js} +21 -15
  4. package/dist/chunks/chunk-2DNYVKBF.js +109 -0
  5. package/dist/chunks/chunk-2HM5Q52B.js +131 -0
  6. package/dist/chunks/chunk-3KKRNQQU.js +275 -0
  7. package/dist/chunks/chunk-55GTJG5I.js +1154 -0
  8. package/dist/chunks/chunk-7KYCYDAS.js +325 -0
  9. package/dist/chunks/{chunk-SNC7AOT7.js → chunk-AL37NFWR.js} +45 -94
  10. package/dist/chunks/chunk-ARIFLX5E.js +9525 -0
  11. package/dist/chunks/{chunk-X7PZN73N.js → chunk-E7CZPAM2.js} +1 -1
  12. package/dist/chunks/chunk-EO3CSXWK.js +70 -0
  13. package/dist/chunks/chunk-FIAFVIAN.js +79 -0
  14. package/dist/chunks/{chunk-MRNZFUXZ.js → chunk-KQMTTEJV.js} +22 -7
  15. package/dist/chunks/chunk-KX6QEHM5.js +578 -0
  16. package/dist/chunks/{chunk-VST6HAB6.js → chunk-MTJ2KW3V.js} +2 -0
  17. package/dist/chunks/chunk-MUWDZJN2.js +71 -0
  18. package/dist/chunks/{chunk-CY6C6ADU.js → chunk-MVCBPI4J.js} +45 -478
  19. package/dist/chunks/{chunk-34F4ZURL.js → chunk-NJVPLN5D.js} +29 -12
  20. package/dist/chunks/chunk-UBCAZGLE.js +15 -0
  21. package/dist/chunks/chunk-VP7YLCQP.js +244 -0
  22. package/dist/chunks/chunk-VSMAZISQ.js +303 -0
  23. package/dist/chunks/chunk-WE4HDHFU.js +37 -0
  24. package/dist/chunks/{create-ET6DT5ZE.js → create-2CQZF53Z.js} +5 -2
  25. package/dist/chunks/{create-LM7U3CJS.js → create-36F6V4WP.js} +99 -6
  26. package/dist/chunks/{create-TLPM7HJE.js → create-DXW4YYLJ.js} +5 -5
  27. package/dist/chunks/{disable-TNTEIAKO.js → disable-FB64CSYV.js} +5 -2
  28. package/dist/chunks/doctor-PVO4JLYN.js +208 -0
  29. package/dist/chunks/{enable-GUQCB4CU.js → enable-UVXHTKCJ.js} +6 -3
  30. package/dist/chunks/{init-PJSCFOC6.js → init-GPNC7TR5.js} +5 -2
  31. package/dist/chunks/{install-4YP7FALO.js → install-3G7U5ZIO.js} +10 -5
  32. package/dist/chunks/{intro-SA7MWV3Z.js → intro-YI5HUZCF.js} +45 -19
  33. package/dist/chunks/{list-WKNPZZOV.js → list-J2UCBYAY.js} +9 -6
  34. package/dist/chunks/list-JB4447YK.js +62 -0
  35. package/dist/chunks/{list-7R6NASFI.js → list-W4JAYPAA.js} +19 -19
  36. package/dist/chunks/{manifest-7RVNKBOH.js → manifest-76MIEL2F.js} +3 -1
  37. package/dist/chunks/{registry-K5DV5NAK.js → registry-IWP7JMNI.js} +1 -1
  38. package/dist/chunks/{repl-ZB3DIJ4K.js → repl-MWFEZ65P.js} +30 -20
  39. package/dist/chunks/resolver-URPQMOIV.js +36 -0
  40. package/dist/chunks/{run-22B65ZWZ.js → run-3VKWSRLO.js} +17 -11
  41. package/dist/chunks/rune-L5EP645W.js +283 -0
  42. package/dist/chunks/runner-VPTLUQJE.js +34 -0
  43. package/dist/chunks/{shell-explain-QLITLHW7.js → shell-explain-LKBBP2BG.js} +5 -2
  44. package/dist/chunks/trust-LXXYSF6W.js +109 -0
  45. package/dist/chunks/{uninstall-4WR5IHLC.js → uninstall-J7JUYETA.js} +10 -5
  46. package/dist/chunks/{update-ACLDK6E6.js → update-ZJRLDCUT.js} +10 -5
  47. package/dist/chunks/{version-EULYWNXN.js → version-ZULO6CV3.js} +2 -2
  48. package/dist/cli.js +47 -34
  49. package/package.json +2 -2
  50. package/dist/chunks/chunk-G3U4V32O.js +0 -46
  51. package/dist/chunks/chunk-QYRF3R35.js +0 -154
  52. package/dist/chunks/chunk-YXCHIMLW.js +0 -75308
  53. package/dist/chunks/doctor-VXAQYMTR.js +0 -101
  54. package/dist/chunks/list-OMMZAQ23.js +0 -28
  55. package/dist/chunks/resolver-QIYYFIDX.js +0 -34
  56. package/dist/chunks/rune-SYK4N642.js +0 -425
  57. package/dist/chunks/{delete-5R3WBLVD.js → delete-2EXPFBFC.js} +4 -4
  58. package/dist/chunks/{delete-U4STA2QZ.js → delete-MJAECXXV.js} +4 -4
@@ -3,10 +3,7 @@ import {
3
3
  readRawConfig,
4
4
  setPluginEnabled,
5
5
  writeRawConfig
6
- } from "./chunk-X7PZN73N.js";
7
- import {
8
- coercePlugins
9
- } from "./chunk-QYRF3R35.js";
6
+ } from "./chunk-E7CZPAM2.js";
10
7
  import {
11
8
  output
12
9
  } from "./chunk-MIKWIFYR.js";
@@ -14,19 +11,29 @@ import {
14
11
  R,
15
12
  le
16
13
  } from "./chunk-CGZL5SSH.js";
14
+ import {
15
+ isNativeRune
16
+ } from "./chunk-MUWDZJN2.js";
17
+ import {
18
+ coercePlugins
19
+ } from "./chunk-3KKRNQQU.js";
17
20
  import {
18
21
  loadRegistry,
19
22
  registerPlugin,
20
23
  removePlugin
21
- } from "./chunk-VST6HAB6.js";
24
+ } from "./chunk-MTJ2KW3V.js";
25
+ import {
26
+ loadPluginJson
27
+ } from "./chunk-FIAFVIAN.js";
28
+ import {
29
+ blockOf,
30
+ scopedPermissions
31
+ } from "./chunk-WE4HDHFU.js";
22
32
  import {
23
33
  ensureStoreDirs,
24
34
  getPluginCacheDir,
25
35
  getPnpmStorePath
26
36
  } from "./chunk-2Q3KLBE6.js";
27
- import {
28
- loadPluginJson
29
- } from "./chunk-G3U4V32O.js";
30
37
  import {
31
38
  init_require_shim
32
39
  } from "./chunk-TPK5AGRA.js";
@@ -141,14 +148,24 @@ function formatConsentScreen(pluginName, pluginJson) {
141
148
  const lines = [`${pluginName} requests the following permissions:
142
149
  `];
143
150
  for (const [runeKey, rune] of Object.entries(pluginJson.runes)) {
144
- const allow = collectAllow(rune.permissions);
151
+ const allow = collectAllow(scopedPermissions(rune));
145
152
  if (allow.length === 0) continue;
146
153
  lines.push(` ${runeKey}`);
147
154
  for (const perm of allow) {
148
- lines.push(` ${perm}`);
155
+ if (perm === "*" || perm === "*:*") {
156
+ lines.push(` * (UNRESTRICTED: allows all filesystem, shell, network, and environment operations)`);
157
+ } else {
158
+ lines.push(` ${perm}`);
159
+ }
149
160
  }
150
161
  lines.push("");
151
162
  }
163
+ const natives = Object.entries(pluginJson.runes).filter(([, rune]) => isNativeRune(rune));
164
+ if (natives.length) {
165
+ lines.push("Native runes \u2014 these run outside the sandbox, with your full access:\n");
166
+ for (const [runeKey, rune] of natives) lines.push(` ${runeKey} runs ${blockOf(rune, "run").command}`);
167
+ lines.push("", `They will not run until you review and trust them: crunes plugin trust ${pluginName}`, "");
168
+ }
152
169
  return lines.join("\n");
153
170
  }
154
171
  async function promptConsent(pluginName, pluginJson, { yes = false } = {}) {
@@ -162,7 +179,7 @@ async function promptConsent(pluginName, pluginJson, { yes = false } = {}) {
162
179
  function diffPermissions(oldConsented, newPluginJson) {
163
180
  const added = {};
164
181
  for (const [runeKey, rune] of Object.entries(newPluginJson.runes)) {
165
- const newPerms = collectAllow(rune.permissions);
182
+ const newPerms = collectAllow(scopedPermissions(rune));
166
183
  const oldPerms = oldConsented[runeKey] ?? [];
167
184
  const newOnes = newPerms.filter((perm) => !oldPerms.includes(perm));
168
185
  if (newOnes.length > 0) added[runeKey] = newOnes;
@@ -191,7 +208,7 @@ async function promptReConsent(pluginName, diff, { yes = false } = {}) {
191
208
  var execFileAsync2 = promisify2(execFile2);
192
209
  function collectAllowFromRune(rune) {
193
210
  const all = [];
194
- for (const lifecycle of Object.values(rune.permissions ?? {})) {
211
+ for (const lifecycle of Object.values(scopedPermissions(rune) ?? {})) {
195
212
  for (const perm of lifecycle?.allow ?? []) {
196
213
  if (!all.includes(perm)) all.push(perm);
197
214
  }
@@ -0,0 +1,15 @@
1
+ import {
2
+ init_require_shim
3
+ } from "./chunk-TPK5AGRA.js";
4
+
5
+ // src/shared/command.js
6
+ init_require_shim();
7
+ import path from "node:path";
8
+ var ISOLATE_MODULE = /\.rune\.m?js$/i;
9
+ function isPathCommand(command) {
10
+ return command.includes("/") || command.includes("\\") || command.startsWith(".") || path.isAbsolute(command) || ISOLATE_MODULE.test(command);
11
+ }
12
+
13
+ export {
14
+ isPathCommand
15
+ };
@@ -0,0 +1,244 @@
1
+ import {
2
+ parseFlags
3
+ } from "./chunk-55GTJG5I.js";
4
+ import {
5
+ init_require_shim
6
+ } from "./chunk-TPK5AGRA.js";
7
+
8
+ // src/rune/native/schema.js
9
+ init_require_shim();
10
+ import { readFileSync } from "node:fs";
11
+ var SCHEMA_KEYS = ["positionals", "options", "examples", "commands"];
12
+ var NODE_KEYS = {
13
+ root: SCHEMA_KEYS,
14
+ command: ["name", "description", "to", ...SCHEMA_KEYS],
15
+ positional: ["spec", "description", "type", "choices", "to"],
16
+ option: ["flags", "description", "type", "default", "required", "choices", "to"],
17
+ example: ["usage", "description"]
18
+ };
19
+ var POSITIONAL_SPEC = /^[<[]([a-zA-Z0-9_-]+)(\.\.\.)?[>\]]$/;
20
+ var VALUE_TYPES = ["string", "number", "integer"];
21
+ function isInlineSchema(block) {
22
+ return block != null && typeof block === "object" && !Array.isArray(block) && Object.keys(block).some((k) => SCHEMA_KEYS.includes(k));
23
+ }
24
+ function fail(where, message) {
25
+ throw new Error(`${where}: ${message}`);
26
+ }
27
+ function checkKeys(where, node, kind) {
28
+ if (node == null || typeof node !== "object" || Array.isArray(node)) fail(where, "must be an object");
29
+ const unknown = Object.keys(node).filter((k) => !NODE_KEYS[kind].includes(k));
30
+ if (unknown.length) fail(where, `unknown field${unknown.length > 1 ? "s" : ""} ${unknown.map((k) => `"${k}"`).join(", ")} \u2014 allowed: ${NODE_KEYS[kind].join(", ")}`);
31
+ }
32
+ function checkChoices(where, choices) {
33
+ if (choices !== void 0 && (!Array.isArray(choices) || choices.length === 0 || choices.some((c) => typeof c !== "string" && typeof c !== "number"))) {
34
+ fail(where, '"choices" must be a non-empty array of strings or numbers');
35
+ }
36
+ }
37
+ function checkTo(where, to) {
38
+ if (to !== void 0 && typeof to !== "string" && !(Array.isArray(to) && to.every((t) => typeof t === "string"))) {
39
+ fail(where, '"to" must be a string or an array of strings \u2014 the program arguments this one becomes, with {value} for its value');
40
+ }
41
+ }
42
+ function list(where, node, field) {
43
+ const value = node[field];
44
+ if (value === void 0) return [];
45
+ if (!Array.isArray(value)) fail(where, `"${field}" must be an array`);
46
+ return value;
47
+ }
48
+ function normaliseJsonSchema(schema, where = "args") {
49
+ return normaliseNode(schema, where, "root");
50
+ }
51
+ function normaliseNode(node, where, kind) {
52
+ checkKeys(where, node, kind);
53
+ const out = {};
54
+ if (kind === "command") {
55
+ if (typeof node.name !== "string" || !node.name) fail(where, '"name" must be a non-empty string');
56
+ out.name = node.name;
57
+ out.description = node.description ?? "";
58
+ checkTo(where, node.to);
59
+ if (node.to !== void 0) out.to = node.to;
60
+ }
61
+ out.positionals = list(where, node, "positionals").map((p, i) => {
62
+ const at = `${where}.positionals[${i}]`;
63
+ checkKeys(at, p, "positional");
64
+ if (typeof p.spec !== "string" || !POSITIONAL_SPEC.test(p.spec)) fail(at, `"spec" must look like <name>, [name] or <names...>`);
65
+ if (p.type !== void 0 && !VALUE_TYPES.includes(p.type)) fail(at, `"type" must be one of ${VALUE_TYPES.join(", ")}`);
66
+ checkChoices(at, p.choices);
67
+ checkTo(at, p.to);
68
+ return { spec: p.spec, description: p.description ?? "", ...p.type && { type: p.type }, ...p.choices && { choices: p.choices }, ...p.to !== void 0 && { to: p.to } };
69
+ });
70
+ out.options = list(where, node, "options").map((o, i) => {
71
+ const at = `${where}.options[${i}]`;
72
+ checkKeys(at, o, "option");
73
+ if (typeof o.flags !== "string") fail(at, '"flags" must be a string such as "--min <percent>"');
74
+ let flag;
75
+ try {
76
+ flag = parseFlags(o.flags);
77
+ } catch (err) {
78
+ fail(at, err.message);
79
+ }
80
+ if (o.type !== void 0) {
81
+ const takesValue = flag.type !== "boolean";
82
+ if (takesValue ? !VALUE_TYPES.includes(o.type) : o.type !== "boolean") {
83
+ fail(at, `"type" must be ${takesValue ? `one of ${VALUE_TYPES.join(", ")} for an option that takes a value` : '"boolean" for a flag that takes no value'}`);
84
+ }
85
+ }
86
+ if (o.required !== void 0 && typeof o.required !== "boolean") fail(at, '"required" must be true or false');
87
+ checkChoices(at, o.choices);
88
+ checkTo(at, o.to);
89
+ return {
90
+ flags: o.flags,
91
+ description: o.description ?? "",
92
+ ...o.type && o.type !== "boolean" && { type: o.type },
93
+ ...o.default !== void 0 && { def: o.default },
94
+ ...o.required && { required: true },
95
+ ...o.choices && { choices: o.choices },
96
+ ...o.to !== void 0 && { to: o.to }
97
+ };
98
+ });
99
+ out.examples = list(where, node, "examples").map((e, i) => {
100
+ const at = `${where}.examples[${i}]`;
101
+ checkKeys(at, e, "example");
102
+ if (typeof e.usage !== "string") fail(at, '"usage" must be a string');
103
+ return { usage: e.usage, description: e.description ?? "" };
104
+ });
105
+ out.commands = list(where, node, "commands").map((c, i) => normaliseNode(c, `${where}.commands[${i}]`, "command"));
106
+ return out;
107
+ }
108
+ function loadJsonSchema(file, shownAs = file) {
109
+ let parsed;
110
+ try {
111
+ parsed = JSON.parse(readFileSync(file, "utf8"));
112
+ } catch (err) {
113
+ throw new Error(`${shownAs}: ${err.code === "ENOENT" ? "file not found" : `invalid JSON \u2014 ${err.message}`}`);
114
+ }
115
+ return normaliseJsonSchema(parsed, shownAs);
116
+ }
117
+ function nodeFor(schema, commands) {
118
+ let node = schema;
119
+ const options = [...schema.options ?? []];
120
+ for (const name of commands) {
121
+ node = (node.commands ?? []).find((c) => c.name === name) ?? node;
122
+ options.push(...node.options ?? []);
123
+ }
124
+ return { node, options };
125
+ }
126
+ var shown = (value) => JSON.stringify(value);
127
+ function typeProblem(type, value) {
128
+ if (type === "number" && !Number.isFinite(value)) return "must be a number";
129
+ if (type === "integer" && !Number.isInteger(value)) return "must be an integer";
130
+ return null;
131
+ }
132
+ function validateParsed(schema, parsed, key) {
133
+ const { node, options } = nodeFor(schema, parsed.$commands ?? []);
134
+ const usage = `Run \`crunes run ${key}${parsed.$command ? ` ${parsed.$command}` : ""} --help\` for usage.`;
135
+ const problems = [];
136
+ for (const p of node.positionals ?? []) {
137
+ const [, name, variadic] = p.spec.match(POSITIONAL_SPEC);
138
+ const required = p.spec.startsWith("<");
139
+ const value = parsed[name];
140
+ const values = variadic ? value ?? [] : value === void 0 ? [] : [value];
141
+ if (required && values.length === 0) problems.push(`missing required ${p.spec}`);
142
+ for (const v of values) {
143
+ const problem = typeProblem(p.type, v);
144
+ if (problem) problems.push(`${p.spec} ${problem} \u2014 got ${shown(String(v))}`);
145
+ }
146
+ if (p.choices) {
147
+ for (const v of values) {
148
+ if (!p.choices.map(String).includes(String(v))) problems.push(`${p.spec} must be one of ${p.choices.map(shown).join(", ")} \u2014 got ${shown(String(v))}`);
149
+ }
150
+ }
151
+ }
152
+ for (const o of options) {
153
+ const { key: optKey } = parseFlags(o.flags);
154
+ const value = parsed[optKey];
155
+ if (o.required && value === void 0) problems.push(`missing required option ${o.flags}`);
156
+ if (value !== void 0) {
157
+ for (const v of Array.isArray(value) ? value : [value]) {
158
+ const problem = typeProblem(o.type, v);
159
+ if (problem) problems.push(`${o.flags} ${problem}${Number.isNaN(v) ? "" : ` \u2014 got ${shown(String(v))}`}`);
160
+ }
161
+ }
162
+ if (o.choices && value !== void 0) {
163
+ const values = Array.isArray(value) ? value : [value];
164
+ for (const v of values) {
165
+ if (!o.choices.map(String).includes(String(v))) problems.push(`${o.flags} must be one of ${o.choices.map(shown).join(", ")} \u2014 got ${shown(String(v))}`);
166
+ }
167
+ }
168
+ }
169
+ if (problems.length) throw new Error(`Invalid arguments for "${key}":
170
+ ${problems.join("\n ")}
171
+ ${usage}`);
172
+ }
173
+ function expand(to, value) {
174
+ return (Array.isArray(to) ? to : [to]).map((t) => value === void 0 ? t : t.replaceAll("{value}", String(value)));
175
+ }
176
+ function flagToken(option, key) {
177
+ return /--/.test(option.flags) ? `--${key}` : `-${key}`;
178
+ }
179
+ function mapToArgv(schema, parsed) {
180
+ const tokens = [];
181
+ let node = schema;
182
+ for (const name of parsed.$commands ?? []) {
183
+ node = (node.commands ?? []).find((c) => c.name === name) ?? node;
184
+ tokens.push(...expand(node.to ?? name));
185
+ }
186
+ for (const o of nodeFor(schema, parsed.$commands ?? []).options) {
187
+ const { key, type } = parseFlags(o.flags);
188
+ const value = parsed[key];
189
+ if (value === void 0 || value === false) continue;
190
+ if (type === "boolean" && !o.type) {
191
+ if (value === true) tokens.push(...o.to !== void 0 ? expand(o.to) : [flagToken(o, key)]);
192
+ continue;
193
+ }
194
+ for (const v of Array.isArray(value) ? value : [value]) {
195
+ tokens.push(...o.to !== void 0 ? expand(o.to, v) : [flagToken(o, key), String(v)]);
196
+ }
197
+ }
198
+ for (const p of node.positionals ?? []) {
199
+ const [, name] = p.spec.match(POSITIONAL_SPEC);
200
+ const value = parsed[name];
201
+ if (value === void 0) continue;
202
+ for (const v of Array.isArray(value) ? value : [value]) tokens.push(...p.to !== void 0 ? expand(p.to, v) : [String(v)]);
203
+ }
204
+ return tokens;
205
+ }
206
+ function undeclaredArgs(schema, parsed, raw) {
207
+ const { options } = nodeFor(schema, parsed.$commands ?? []);
208
+ const long = /* @__PURE__ */ new Set(["help"]);
209
+ const short = /* @__PURE__ */ new Map([["h", false]]);
210
+ for (const o of options) {
211
+ const { key, alias, type } = parseFlags(o.flags);
212
+ if (/--/.test(o.flags)) long.add(key);
213
+ else short.set(key, type !== "boolean");
214
+ if (alias) short.set(alias, type !== "boolean");
215
+ if (type === "boolean") long.add(`no-${key}`);
216
+ }
217
+ const problems = [];
218
+ for (const token of raw) {
219
+ if (token === "--") break;
220
+ const longMatch = token.match(/^--([^=]+)/);
221
+ if (longMatch) {
222
+ if (!long.has(longMatch[1])) problems.push(`unknown option --${longMatch[1]}`);
223
+ } else if (/^-[^-\d]/.test(token)) {
224
+ for (const c of token.slice(1).split("=")[0]) {
225
+ if (!short.has(c)) {
226
+ problems.push(`unknown option -${c}`);
227
+ break;
228
+ }
229
+ if (short.get(c)) break;
230
+ }
231
+ }
232
+ }
233
+ if (parsed.$rest?.length) problems.push(`unexpected argument${parsed.$rest.length > 1 ? "s" : ""} ${parsed.$rest.map((v) => JSON.stringify(String(v))).join(" ")}`);
234
+ return problems;
235
+ }
236
+
237
+ export {
238
+ isInlineSchema,
239
+ normaliseJsonSchema,
240
+ loadJsonSchema,
241
+ validateParsed,
242
+ mapToArgv,
243
+ undeclaredArgs
244
+ };
@@ -0,0 +1,303 @@
1
+ import {
2
+ runRunBlock
3
+ } from "./chunk-KX6QEHM5.js";
4
+ import {
5
+ isNativeRune,
6
+ trustDigest,
7
+ trustState,
8
+ untrustedError
9
+ } from "./chunk-MUWDZJN2.js";
10
+ import {
11
+ enabledPluginKeys,
12
+ getRune
13
+ } from "./chunk-3KKRNQQU.js";
14
+ import {
15
+ loadRegistry,
16
+ resolvePluginKeyScoped
17
+ } from "./chunk-MTJ2KW3V.js";
18
+ import {
19
+ loadPluginJson
20
+ } from "./chunk-FIAFVIAN.js";
21
+ import {
22
+ blockModule,
23
+ scopedPermissions
24
+ } from "./chunk-WE4HDHFU.js";
25
+ import {
26
+ computeEffectivePermissions
27
+ } from "./chunk-MVCBPI4J.js";
28
+ import {
29
+ getLocalBase,
30
+ getStorePath
31
+ } from "./chunk-2Q3KLBE6.js";
32
+ import {
33
+ init_require_shim
34
+ } from "./chunk-TPK5AGRA.js";
35
+
36
+ // src/rune/resolver.js
37
+ init_require_shim();
38
+ import { join } from "node:path";
39
+
40
+ // src/core/errors.js
41
+ init_require_shim();
42
+ var CircularRuneError = class extends Error {
43
+ constructor(chain) {
44
+ super(`Circular rune call: ${chain.join(" \u2192 ")}`);
45
+ this.name = "CircularRuneError";
46
+ }
47
+ };
48
+
49
+ // src/rune/resolver.js
50
+ function describeConfigLayers(configDir) {
51
+ return [
52
+ `project config (${configDir})`,
53
+ "enabled plugins",
54
+ process.env.CRUNES_NO_GLOBAL === "1" ? "global config (skipped: CRUNES_NO_GLOBAL=1)" : `global config (${join(getStorePath(), "config.json")})`
55
+ ].join(", ");
56
+ }
57
+ async function resolvePluginRune(config, key) {
58
+ const colonIdx = key.indexOf(":");
59
+ if (colonIdx === -1) return null;
60
+ const enabledPlugins = enabledPluginKeys(config);
61
+ if (enabledPlugins.length === 0) return null;
62
+ const pluginPart = key.slice(0, colonIdx);
63
+ const runeKey = key.slice(colonIdx + 1);
64
+ let registry;
65
+ try {
66
+ registry = await loadRegistry();
67
+ } catch {
68
+ return null;
69
+ }
70
+ const pluginKey = resolvePluginKeyScoped(pluginPart, registry, enabledPlugins);
71
+ if (!pluginKey) return null;
72
+ if (!enabledPlugins.includes(pluginKey)) return null;
73
+ const entry = registry.plugins?.[pluginKey];
74
+ if (!entry) return null;
75
+ return { pluginKey, runeKey, pluginDir: entry.path, pluginCacheDir: entry.cacheDir ?? entry.path };
76
+ }
77
+ async function resolveRuneFromPlugins(config, runeKey) {
78
+ const enabledPlugins = enabledPluginKeys(config);
79
+ if (enabledPlugins.length === 0) return null;
80
+ let registry;
81
+ try {
82
+ registry = await loadRegistry();
83
+ } catch {
84
+ return null;
85
+ }
86
+ const matches = [];
87
+ for (const pluginKey of enabledPlugins) {
88
+ const entry = registry.plugins?.[pluginKey];
89
+ if (!entry) continue;
90
+ let pluginJson;
91
+ try {
92
+ pluginJson = await loadPluginJson(entry.path);
93
+ } catch {
94
+ continue;
95
+ }
96
+ if ((pluginJson.runes ?? {})[runeKey]) {
97
+ matches.push({ pluginKey, runeKey, pluginDir: entry.path, pluginCacheDir: entry.cacheDir ?? entry.path, pluginJson });
98
+ }
99
+ }
100
+ if (matches.length > 1) {
101
+ const names = matches.map((m) => m.pluginKey).join(", ");
102
+ const options = matches.map((m) => `${m.pluginKey}:${runeKey}`).join(" or ");
103
+ throw new Error(`"${runeKey}" matches runes in multiple plugins: ${names}. Use ${options} to specify one.`);
104
+ }
105
+ return matches[0] ?? null;
106
+ }
107
+ async function runPluginMatch({ match, pluginJson, projectPerms, projectVars, key, args, dir, config, opts, runeCallback, instanceId }) {
108
+ const { pluginKey, runeKey, pluginDir, pluginCacheDir } = match;
109
+ const rune = pluginJson.runes?.[runeKey];
110
+ if (rune && isNativeRune(rune)) {
111
+ const registry = await loadRegistry();
112
+ const state = trustState(registry.plugins?.[pluginKey], runeKey, trustDigest(pluginDir, rune));
113
+ if (state !== "trusted") throw untrustedError(pluginKey, runeKey, state);
114
+ process.stderr.write(`[crunes] ${pluginKey}:${runeKey} is a plugin script: it runs natively, outside the sandbox, with your full access.
115
+ `);
116
+ const entry = { run: rune.run, args: rune.args, vars: { ...rune.vars ?? {}, ...projectVars } };
117
+ const result2 = await runRunBlock({
118
+ dir,
119
+ key,
120
+ args,
121
+ entry,
122
+ opts,
123
+ configDir: pluginDir,
124
+ runeCallback,
125
+ instanceId,
126
+ extraEnv: { CRUNES_PLUGIN_ROOT: pluginDir }
127
+ });
128
+ return result2?.directExitCode !== void 0 ? result2 : normaliseResult(result2);
129
+ }
130
+ const { executePluginRune } = await import("./runner-VPTLUQJE.js");
131
+ const result = await executePluginRune({
132
+ pluginDir,
133
+ pluginCacheDir,
134
+ runeKey,
135
+ pluginJson,
136
+ projectPerms,
137
+ projectVars,
138
+ args,
139
+ projectDir: dir,
140
+ opts: config,
141
+ runeCallback,
142
+ sections: opts.sections ?? null,
143
+ lifecycle: "run",
144
+ onEvent: opts.onEvent ?? null,
145
+ instanceId
146
+ });
147
+ return normaliseResult(result);
148
+ }
149
+ async function runRune(dir, config, key, args, opts = {}, _callStack = []) {
150
+ const configDir = opts.configDir ?? dir;
151
+ const instanceId = opts.instanceId ?? "1";
152
+ let localOnly = false;
153
+ if (key.startsWith("project:")) {
154
+ key = key.slice(8);
155
+ localOnly = true;
156
+ }
157
+ if (_callStack.includes(key)) {
158
+ throw new CircularRuneError([..._callStack, key]);
159
+ }
160
+ const nextStack = [..._callStack, key];
161
+ let childCounter = 0;
162
+ const runeCallback = (childKey, childArgs) => {
163
+ childCounter++;
164
+ const childInstanceId = `${instanceId}.${childCounter}`;
165
+ return runRune(dir, config, childKey, childArgs, {
166
+ configDir,
167
+ onEvent: opts.onEvent,
168
+ instanceId: childInstanceId
169
+ }, nextStack);
170
+ };
171
+ const pluginMatch = localOnly ? null : await resolvePluginRune(config, key);
172
+ if (pluginMatch) {
173
+ const { pluginKey, runeKey, pluginDir } = pluginMatch;
174
+ const pluginJson = await loadPluginJson(pluginDir);
175
+ const projectPerms = scopedPermissions(config.runes?.[`${pluginKey}:${runeKey}`]);
176
+ const projectVars = config.runes?.[`${pluginKey}:${runeKey}`]?.vars ?? {};
177
+ return runPluginMatch({ match: pluginMatch, pluginJson, projectPerms, projectVars, key, args, dir, config, opts, runeCallback, instanceId });
178
+ }
179
+ const entry = getRune(config, key);
180
+ if (!entry && !localOnly) {
181
+ const autoMatch = await resolveRuneFromPlugins(config, key);
182
+ if (autoMatch) {
183
+ const { pluginKey, runeKey, pluginJson } = autoMatch;
184
+ const projectPerms = scopedPermissions(config.runes?.[`${pluginKey}:${runeKey}`]);
185
+ const projectVars = config.runes?.[`${pluginKey}:${runeKey}`]?.vars ?? {};
186
+ return runPluginMatch({ match: autoMatch, pluginJson, projectPerms, projectVars, key, args, dir, config, opts, runeCallback, instanceId });
187
+ }
188
+ return null;
189
+ }
190
+ if (!entry) return null;
191
+ if (entry.plugin) {
192
+ const aliasMatch = await resolvePluginRune(config, entry.plugin);
193
+ if (!aliasMatch) throw new Error(`Plugin alias "${key}" \u2192 "${entry.plugin}" is not enabled or installed.`);
194
+ const { pluginKey, runeKey, pluginDir } = aliasMatch;
195
+ const pluginJson = await loadPluginJson(pluginDir);
196
+ const projectPerms = scopedPermissions(entry) ?? scopedPermissions(config.runes?.[`${pluginKey}:${runeKey}`]);
197
+ const projectVars = entry.vars ?? config.runes?.[`${pluginKey}:${runeKey}`]?.vars ?? {};
198
+ return runPluginMatch({ match: aliasMatch, pluginJson, projectPerms, projectVars, key, args, dir, config, opts, runeCallback, instanceId });
199
+ }
200
+ const result = await runRunBlock({ dir, key, args, entry, opts, configDir, runeCallback, instanceId });
201
+ return result?.directExitCode !== void 0 ? result : normaliseResult(result);
202
+ }
203
+ function normaliseResult(result) {
204
+ if (result == null) return [];
205
+ const items = Array.isArray(result) ? result : [result];
206
+ let strings = 0;
207
+ return items.map((item) => {
208
+ if (typeof item !== "string") return item;
209
+ strings++;
210
+ const name = strings === 1 ? "output" : `output-${strings}`;
211
+ return { name, attrs: {}, data: { type: "markdown", content: item } };
212
+ });
213
+ }
214
+ async function resolveRuneEntry(projectDir, config, key, configDir = projectDir) {
215
+ const pluginMatch = await resolvePluginRune(config, key);
216
+ if (pluginMatch) {
217
+ const { pluginKey, runeKey, pluginDir, pluginCacheDir } = pluginMatch;
218
+ const pluginJson = await loadPluginJson(pluginDir);
219
+ const projectPerms = scopedPermissions(config.runes?.[`${pluginKey}:${runeKey}`]);
220
+ const effective = computeEffectivePermissions(
221
+ scopedPermissions(pluginJson.runes[runeKey]) ?? {},
222
+ projectPerms ?? {},
223
+ "repl"
224
+ );
225
+ const vars = { ...pluginJson.runes[runeKey]?.vars ?? {}, ...config.runes?.[`${pluginKey}:${runeKey}`]?.vars ?? {} };
226
+ return {
227
+ async createReplSession(args, opts = {}) {
228
+ const { getPluginRunePath, runRuneInRepl } = await import("./runner-VPTLUQJE.js");
229
+ const runeFile = getPluginRunePath(pluginDir, runeKey, pluginJson, "repl");
230
+ const nodeModulesDir = join(pluginCacheDir, "node_modules");
231
+ return runRuneInRepl(runeFile, effective, args, projectDir, {
232
+ nodeModulesDir,
233
+ pluginDeps: pluginJson.dependencies ?? {},
234
+ pluginDir,
235
+ pluginId: `${pluginJson.name}@${pluginJson.version}`,
236
+ vars,
237
+ runeKey,
238
+ onEvent: opts.onEvent ?? null,
239
+ instanceId: opts.instanceId ?? "1"
240
+ });
241
+ }
242
+ };
243
+ }
244
+ const entry = getRune(config, key);
245
+ if (entry && !entry.plugin && entry.repl == null) {
246
+ throw new Error(`Rune "${key}" has no repl block \u2014 a rune has a REPL only when its entry declares one.`);
247
+ }
248
+ if (entry && !entry.plugin) {
249
+ const runeFile = blockModule(entry, "repl", configDir);
250
+ const effective = computeEffectivePermissions(scopedPermissions(entry) ?? {}, void 0, "repl");
251
+ const vars = entry.vars ?? {};
252
+ return {
253
+ async createReplSession(args, opts = {}) {
254
+ const { runRuneInRepl } = await import("./runner-VPTLUQJE.js");
255
+ return runRuneInRepl(runeFile, effective, args, projectDir, {
256
+ vars,
257
+ runeKey: key,
258
+ onEvent: opts.onEvent ?? null,
259
+ instanceId: opts.instanceId ?? "1",
260
+ nodeModulesDir: join(getLocalBase(configDir), "node_modules"),
261
+ pluginDeps: null
262
+ });
263
+ }
264
+ };
265
+ }
266
+ const autoMatch = await resolveRuneFromPlugins(config, key);
267
+ if (autoMatch) {
268
+ const { pluginKey, runeKey, pluginDir, pluginCacheDir, pluginJson } = autoMatch;
269
+ const projectPerms = scopedPermissions(config.runes?.[`${pluginKey}:${runeKey}`]);
270
+ const effective = computeEffectivePermissions(
271
+ scopedPermissions(pluginJson.runes[runeKey]) ?? {},
272
+ projectPerms ?? {},
273
+ "repl"
274
+ );
275
+ const vars = { ...pluginJson.runes[runeKey]?.vars ?? {}, ...config.runes?.[`${pluginKey}:${runeKey}`]?.vars ?? {} };
276
+ return {
277
+ async createReplSession(args, opts = {}) {
278
+ const { getPluginRunePath, runRuneInRepl } = await import("./runner-VPTLUQJE.js");
279
+ const runeFile = getPluginRunePath(pluginDir, runeKey, pluginJson, "repl");
280
+ const nodeModulesDir = join(pluginCacheDir, "node_modules");
281
+ return runRuneInRepl(runeFile, effective, args, projectDir, {
282
+ nodeModulesDir,
283
+ pluginDeps: pluginJson.dependencies ?? {},
284
+ pluginDir,
285
+ pluginId: `${pluginJson.name}@${pluginJson.version}`,
286
+ vars,
287
+ runeKey,
288
+ onEvent: opts.onEvent ?? null,
289
+ instanceId: opts.instanceId ?? "1"
290
+ });
291
+ }
292
+ };
293
+ }
294
+ throw new Error(`Unknown key: "${key}". Looked in: ${describeConfigLayers(configDir)}.`);
295
+ }
296
+
297
+ export {
298
+ describeConfigLayers,
299
+ resolvePluginRune,
300
+ resolveRuneFromPlugins,
301
+ runRune,
302
+ resolveRuneEntry
303
+ };
@@ -0,0 +1,37 @@
1
+ import {
2
+ init_require_shim
3
+ } from "./chunk-TPK5AGRA.js";
4
+
5
+ // src/core/rune-entry.js
6
+ init_require_shim();
7
+ import { join, isAbsolute } from "node:path";
8
+ var ISOLATE_MODULE = /\.rune\.m?js$/i;
9
+ function blockOf(entry, name) {
10
+ const block = entry?.[name];
11
+ if (block == null) return null;
12
+ return typeof block === "string" ? { command: block } : block;
13
+ }
14
+ function scopedPermissions(entry) {
15
+ const out = {};
16
+ for (const mode of ["run", "repl"]) {
17
+ const permissions = blockOf(entry, mode)?.permissions;
18
+ if (permissions) out[mode] = permissions;
19
+ }
20
+ return Object.keys(out).length ? out : void 0;
21
+ }
22
+ function blockModule(entry, mode, base) {
23
+ const command = blockOf(entry, mode)?.command;
24
+ if (!command) return null;
25
+ return isAbsolute(command) ? command : join(base, command);
26
+ }
27
+ function isolateName(file) {
28
+ if (ISOLATE_MODULE.test(file)) return file;
29
+ return file.replace(/\.(m?js)$/i, ".rune.$1");
30
+ }
31
+
32
+ export {
33
+ blockOf,
34
+ scopedPermissions,
35
+ blockModule,
36
+ isolateName
37
+ };