@h-rig/core 0.0.6-alpha.17 → 0.0.6-alpha.170

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 (107) hide show
  1. package/dist/src/agent-role-registry.d.ts +4 -0
  2. package/dist/src/agent-role-registry.js +27 -0
  3. package/dist/src/authority-paths.d.ts +15 -0
  4. package/dist/src/authority-paths.js +80 -0
  5. package/dist/src/baked-secrets.d.ts +6 -0
  6. package/dist/src/baked-secrets.js +121 -0
  7. package/dist/src/build-time-config.d.ts +12 -0
  8. package/dist/src/build-time-config.js +25 -0
  9. package/dist/src/build-time-config.macro.d.ts +1 -0
  10. package/dist/src/capability-loaders.d.ts +51 -0
  11. package/dist/src/capability-loaders.js +758 -0
  12. package/dist/src/capability.d.ts +79 -0
  13. package/dist/src/capability.js +63 -0
  14. package/dist/src/checkout-root.d.ts +1 -0
  15. package/dist/src/checkout-root.js +30 -0
  16. package/dist/src/config-env.d.ts +4 -0
  17. package/dist/src/config-env.js +23 -0
  18. package/dist/src/config.d.ts +3 -0
  19. package/dist/src/config.js +44 -0
  20. package/dist/src/declarative-config.d.ts +14 -0
  21. package/dist/src/declarative-config.js +82 -0
  22. package/dist/src/default-kernel.d.ts +1 -0
  23. package/dist/src/default-kernel.js +12 -0
  24. package/dist/src/define-config.d.ts +20 -0
  25. package/dist/src/define-config.js +28 -15
  26. package/dist/src/define-plugin.d.ts +13 -0
  27. package/dist/src/define-plugin.js +4 -43
  28. package/dist/src/embedded-plugins.d.ts +59 -0
  29. package/dist/src/embedded-plugins.js +22 -0
  30. package/dist/src/exec.d.ts +13 -0
  31. package/dist/src/exec.js +101 -0
  32. package/dist/src/harness-paths.d.ts +18 -0
  33. package/dist/src/harness-paths.js +141 -0
  34. package/dist/src/hook-materializer.d.ts +72 -0
  35. package/dist/src/hook-materializer.js +281 -0
  36. package/dist/src/hook-protocol.d.ts +2 -0
  37. package/dist/src/hook-protocol.js +462 -0
  38. package/dist/src/hook-runner.d.ts +48 -0
  39. package/dist/src/hook-runner.js +756 -0
  40. package/dist/src/hook-runtime.d.ts +52 -0
  41. package/dist/src/hook-runtime.js +462 -0
  42. package/dist/src/index.d.ts +8 -0
  43. package/dist/src/index.js +210 -2499
  44. package/dist/src/json-files.d.ts +9 -0
  45. package/dist/src/json-files.js +125 -0
  46. package/dist/src/kernel-boot.d.ts +2 -0
  47. package/dist/src/kernel-boot.js +10 -0
  48. package/dist/src/kernel-entrypoint.d.ts +22 -0
  49. package/dist/src/kernel-entrypoint.js +548 -0
  50. package/dist/src/kernel-plugin-abi.d.ts +1 -0
  51. package/dist/src/kernel-plugin-abi.js +1 -0
  52. package/dist/src/kernel-resolver.d.ts +2 -0
  53. package/dist/src/kernel-resolver.js +6 -0
  54. package/dist/src/layout.d.ts +10 -0
  55. package/dist/src/layout.js +144 -0
  56. package/dist/src/load-config.d.ts +2 -0
  57. package/dist/src/load-config.js +423 -30
  58. package/dist/src/placement.d.ts +50 -0
  59. package/dist/src/placement.js +996 -0
  60. package/dist/src/plugin-host-context.d.ts +66 -0
  61. package/dist/src/plugin-host-context.js +1292 -0
  62. package/dist/src/plugin-host-registries.d.ts +31 -0
  63. package/dist/src/plugin-host-registries.js +79 -0
  64. package/dist/src/plugin-host.d.ts +77 -0
  65. package/dist/src/plugin-host.js +127 -63
  66. package/dist/src/plugin-runtime.d.ts +173 -0
  67. package/dist/src/profile-ops.d.ts +9 -0
  68. package/dist/src/profile-ops.js +252 -0
  69. package/dist/src/project-plugins.d.ts +63 -0
  70. package/dist/src/project-plugins.js +793 -0
  71. package/dist/src/remote-config.d.ts +183 -0
  72. package/dist/src/remote-config.js +574 -0
  73. package/dist/src/root-resolver.d.ts +5 -0
  74. package/dist/src/root-resolver.js +69 -0
  75. package/dist/src/run-provisioning.d.ts +58 -0
  76. package/dist/src/run-provisioning.js +128 -0
  77. package/dist/src/runtime-context.d.ts +20 -0
  78. package/dist/src/runtime-context.js +257 -0
  79. package/dist/src/runtime-events.d.ts +44 -0
  80. package/dist/src/runtime-events.js +212 -0
  81. package/dist/src/runtime-overlay.d.ts +11 -0
  82. package/dist/src/runtime-overlay.js +71 -0
  83. package/dist/src/runtime-paths.d.ts +21 -0
  84. package/dist/src/runtime-paths.js +181 -0
  85. package/dist/src/runtime-provisioning-env.d.ts +5 -0
  86. package/dist/src/runtime-provisioning-env.js +217 -0
  87. package/dist/src/runtime-runner-context.d.ts +12 -0
  88. package/dist/src/runtime-runner-context.js +1 -0
  89. package/dist/src/safe-identifiers.d.ts +44 -0
  90. package/dist/src/safe-identifiers.js +96 -0
  91. package/dist/src/scope-rules.d.ts +4 -0
  92. package/dist/src/scope-rules.js +21 -0
  93. package/dist/src/server-paths.d.ts +26 -0
  94. package/dist/src/server-paths.js +308 -0
  95. package/dist/src/setup-version.d.ts +3 -0
  96. package/dist/src/setup-version.js +14 -0
  97. package/dist/src/task-record-reader.d.ts +3 -0
  98. package/dist/src/task-record-reader.js +9 -0
  99. package/dist/src/validator-registry.d.ts +27 -0
  100. package/dist/src/validator-registry.js +64 -0
  101. package/package.json +166 -10
  102. package/dist/src/engineReadModelReducer.js +0 -1780
  103. package/dist/src/rig-init-builder.js +0 -57
  104. package/dist/src/rigSelectors.js +0 -293
  105. package/dist/src/taskGraph.js +0 -64
  106. package/dist/src/taskGraphCodes.js +0 -26
  107. package/dist/src/taskGraphLayout.js +0 -374
@@ -0,0 +1,758 @@
1
+ // @bun
2
+ // packages/core/src/project-plugins.ts
3
+ import { existsSync as existsSync3, statSync as statSync2 } from "fs";
4
+ import { resolve as resolve2 } from "path";
5
+
6
+ // packages/core/src/plugin-host.ts
7
+ function indexById(contributions, kind) {
8
+ const map = new Map;
9
+ const registrant = new Map;
10
+ for (const { item, pluginName } of contributions) {
11
+ if (map.has(item.id)) {
12
+ throw new Error(`duplicate ${kind} id "${item.id}": registered by plugins "${registrant.get(item.id)}" and "${pluginName}"`);
13
+ }
14
+ map.set(item.id, item);
15
+ registrant.set(item.id, pluginName);
16
+ }
17
+ return map;
18
+ }
19
+ function assertUniquePluginNames(plugins) {
20
+ const seen = new Set;
21
+ for (const plugin of plugins) {
22
+ if (seen.has(plugin.name)) {
23
+ throw new Error(`duplicate plugin name "${plugin.name}"`);
24
+ }
25
+ seen.add(plugin.name);
26
+ }
27
+ }
28
+ function createPluginHost(plugins) {
29
+ assertUniquePluginNames(plugins);
30
+ const validators = [];
31
+ const hooks = [];
32
+ const skills = [];
33
+ const repoSources = [];
34
+ const agentRoles = [];
35
+ const taskFieldExtensions = [];
36
+ const taskSources = [];
37
+ const cliCommands = [];
38
+ const capabilities = [];
39
+ const panels = [];
40
+ const blockerClassifiers = [];
41
+ const sessionExtensions = [];
42
+ const seedEntrypoints = [];
43
+ const stages = [];
44
+ const stageMutations = [];
45
+ const stageExecutors = {};
46
+ for (const plugin of plugins) {
47
+ const c = plugin.contributes;
48
+ if (!c)
49
+ continue;
50
+ const pluginName = plugin.name;
51
+ if (c.validators)
52
+ validators.push(...c.validators.map((item) => ({ item, pluginName })));
53
+ if (c.hooks)
54
+ hooks.push(...c.hooks.map((item) => ({ item, pluginName })));
55
+ if (c.skills)
56
+ skills.push(...c.skills.map((item) => ({ item, pluginName })));
57
+ if (c.repoSources)
58
+ repoSources.push(...c.repoSources.map((item) => ({ item, pluginName })));
59
+ if (c.agentRoles)
60
+ agentRoles.push(...c.agentRoles.map((item) => ({ item, pluginName })));
61
+ if (c.taskFieldSchemas)
62
+ taskFieldExtensions.push(...c.taskFieldSchemas.map((item) => ({ item, pluginName })));
63
+ if (c.taskSources)
64
+ taskSources.push(...c.taskSources.map((item) => ({ item, pluginName })));
65
+ if (c.cliCommands)
66
+ cliCommands.push(...c.cliCommands.map((item) => ({ item, pluginName })));
67
+ if (c.capabilities)
68
+ capabilities.push(...c.capabilities.map((item) => ({ item, pluginName })));
69
+ if (c.panels)
70
+ panels.push(...c.panels.map((item) => ({ item, pluginName })));
71
+ if (c.blockerClassifiers)
72
+ blockerClassifiers.push(...c.blockerClassifiers.map((item) => ({ item, pluginName })));
73
+ if (c.sessionExtensions)
74
+ sessionExtensions.push(...c.sessionExtensions.map((item) => ({ item, pluginName })));
75
+ if (c.seedEntrypoints)
76
+ seedEntrypoints.push(...c.seedEntrypoints.map((item) => ({ item, pluginName })));
77
+ if (c.stageMutations)
78
+ stageMutations.push(...c.stageMutations.map((item) => ({ item, pluginName })));
79
+ if (c.stages) {
80
+ for (const stage of c.stages) {
81
+ stages.push({ item: stage, pluginName });
82
+ if (stage.run)
83
+ stageExecutors[stage.id] = stage.run;
84
+ }
85
+ }
86
+ }
87
+ const validatorMap = indexById(validators, "validator");
88
+ const hookMap = indexById(hooks, "hook");
89
+ const skillMap = indexById(skills, "skill");
90
+ const repoSourceMap = indexById(repoSources, "repoSource");
91
+ const agentRoleMap = indexById(agentRoles, "agentRole");
92
+ const taskFieldExtMap = indexById(taskFieldExtensions, "taskFieldExtension");
93
+ const taskSourceMap = indexById(taskSources, "taskSource");
94
+ const cliCommandMap = indexById(cliCommands, "cliCommand");
95
+ const capabilityMap = indexById(capabilities, "capability");
96
+ const panelMap = indexById(panels, "panel");
97
+ const blockerClassifierMap = indexById(blockerClassifiers, "blockerClassifier");
98
+ indexById(sessionExtensions, "sessionExtension");
99
+ indexById(seedEntrypoints, "seedEntrypoint");
100
+ const taskSourceFactoryByKind = new Map;
101
+ const taskSourceKindRegistrant = new Map;
102
+ for (const { item, pluginName } of taskSources) {
103
+ if (taskSourceFactoryByKind.has(item.kind)) {
104
+ throw new Error(`duplicate task source kind "${item.kind}": registered by plugins "${taskSourceKindRegistrant.get(item.kind)}" and "${pluginName}"`);
105
+ }
106
+ taskSourceFactoryByKind.set(item.kind, item);
107
+ taskSourceKindRegistrant.set(item.kind, pluginName);
108
+ }
109
+ const seedEntrypointByBasename = new Map;
110
+ const seedEntrypointByWorkerArg = new Map;
111
+ let insidePtySeedEntrypoint;
112
+ const registerSeedEntrypointSelector = (map, selectorKind, selector, contribution) => {
113
+ const existing = map.get(selector);
114
+ if (existing) {
115
+ throw new Error(`duplicate seed entrypoint ${selectorKind} "${selector}": registered by entrypoint "${existing.item.id}" from plugin "${existing.pluginName}" and entrypoint "${contribution.item.id}" from plugin "${contribution.pluginName}"`);
116
+ }
117
+ map.set(selector, contribution);
118
+ };
119
+ for (const contribution of seedEntrypoints) {
120
+ const entrypoint = contribution.item;
121
+ let selectorCount = 0;
122
+ if (entrypoint.basename) {
123
+ selectorCount += 1;
124
+ registerSeedEntrypointSelector(seedEntrypointByBasename, "basename", entrypoint.basename, contribution);
125
+ }
126
+ if (entrypoint.workerArg) {
127
+ selectorCount += 1;
128
+ registerSeedEntrypointSelector(seedEntrypointByWorkerArg, "workerArg", entrypoint.workerArg, contribution);
129
+ }
130
+ if (entrypoint.insidePty) {
131
+ selectorCount += 1;
132
+ if (insidePtySeedEntrypoint) {
133
+ throw new Error(`duplicate seed entrypoint inside-pty selector: registered by entrypoint "${insidePtySeedEntrypoint.item.id}" from plugin "${insidePtySeedEntrypoint.pluginName}" and entrypoint "${entrypoint.id}" from plugin "${contribution.pluginName}"`);
134
+ }
135
+ insidePtySeedEntrypoint = contribution;
136
+ }
137
+ if (selectorCount === 0) {
138
+ throw new Error(`seed entrypoint "${entrypoint.id}" from plugin "${contribution.pluginName}" must declare at least one selector (basename, workerArg, or insidePty)`);
139
+ }
140
+ }
141
+ const allValidators = validators.map((c) => c.item);
142
+ const allHooks = hooks.map((c) => c.item);
143
+ const allSkills = skills.map((c) => c.item);
144
+ const allRepoSources = repoSources.map((c) => c.item);
145
+ const allAgentRoles = agentRoles.map((c) => c.item);
146
+ const allTaskFieldExtensions = taskFieldExtensions.map((c) => c.item);
147
+ const allTaskSources = taskSources.map((c) => c.item);
148
+ const allCliCommands = cliCommands.map((c) => c.item);
149
+ const allStageMutations = stageMutations.map((c) => c.item);
150
+ const allStages = stages.map((c) => c.item);
151
+ const allCapabilities = capabilities.map((c) => c.item);
152
+ const allPanels = panels.map((c) => c.item);
153
+ const allBlockerClassifiers = blockerClassifiers.map((c) => c.item);
154
+ const allSessionExtensions = sessionExtensions.map((c) => c.item);
155
+ const allSeedEntrypoints = seedEntrypoints.map((c) => c.item);
156
+ const executableCliCommandByName = new Map;
157
+ const registerExecutableCliCommandSelector = (selector, contribution) => {
158
+ const existing = executableCliCommandByName.get(selector);
159
+ if (existing && existing.item.id !== contribution.item.id) {
160
+ throw new Error(`duplicate executable CLI selector "${selector}" registered by command "${existing.item.id}" from plugin "${existing.pluginName}" and command "${contribution.item.id}" from plugin "${contribution.pluginName}"`);
161
+ }
162
+ executableCliCommandByName.set(selector, contribution);
163
+ };
164
+ for (const contribution of cliCommands) {
165
+ const command = contribution.item;
166
+ const family = command.family ?? command.id;
167
+ registerExecutableCliCommandSelector(command.id, contribution);
168
+ registerExecutableCliCommandSelector(family, contribution);
169
+ for (const alias of command.aliases ?? []) {
170
+ registerExecutableCliCommandSelector(alias, contribution);
171
+ }
172
+ }
173
+ let defaultRootCliCommand;
174
+ for (const contribution of cliCommands) {
175
+ if (!contribution.item.rootDefault)
176
+ continue;
177
+ if (defaultRootCliCommand) {
178
+ throw new Error(`duplicate default root CLI command registered by command "${defaultRootCliCommand.item.id}" from plugin "${defaultRootCliCommand.pluginName}" and command "${contribution.item.id}" from plugin "${contribution.pluginName}"`);
179
+ }
180
+ defaultRootCliCommand = contribution;
181
+ }
182
+ return {
183
+ getValidator: (id) => validatorMap.get(id),
184
+ getHook: (id) => hookMap.get(id),
185
+ getSkill: (id) => skillMap.get(id),
186
+ getRepoSource: (id) => repoSourceMap.get(id),
187
+ getAgentRole: (id) => agentRoleMap.get(id),
188
+ getTaskFieldExtension: (id) => taskFieldExtMap.get(id),
189
+ getTaskSource: (id) => taskSourceMap.get(id),
190
+ getCliCommand: (id) => cliCommandMap.get(id),
191
+ getCapability: (id) => capabilityMap.get(id),
192
+ getPanel: (id) => panelMap.get(id),
193
+ getBlockerClassifier: (id) => blockerClassifierMap.get(id),
194
+ listValidators: () => allValidators,
195
+ listHooks: () => allHooks,
196
+ listSkills: () => allSkills,
197
+ listRepoSources: () => allRepoSources,
198
+ listAgentRoles: () => allAgentRoles,
199
+ listTaskFieldExtensions: () => allTaskFieldExtensions,
200
+ listTaskSources: () => allTaskSources,
201
+ listCliCommands: () => allCliCommands,
202
+ listCapabilities: () => allCapabilities,
203
+ listPanels: () => allPanels,
204
+ listBlockerClassifiers: () => allBlockerClassifiers,
205
+ listStages: () => allStages,
206
+ listStageMutations: () => allStageMutations,
207
+ listStageExecutors: () => stageExecutors,
208
+ listExecutableValidators: () => allValidators.filter((v) => typeof v.run === "function"),
209
+ listExecutableTaskSources: () => allTaskSources,
210
+ listExecutableCapabilities: () => allCapabilities.filter((c) => typeof c.run === "function"),
211
+ listExecutablePanels: () => allPanels.filter((p) => typeof p.produce === "function"),
212
+ listExecutableBlockerClassifiers: () => allBlockerClassifiers,
213
+ listExecutableCliCommands: () => allCliCommands,
214
+ listSessionExtensions: () => allSessionExtensions,
215
+ listSeedEntrypoints: () => allSeedEntrypoints,
216
+ resolveSeedEntrypointByBasename: (value) => seedEntrypointByBasename.get(value)?.item,
217
+ resolveSeedEntrypointByWorkerArg: (value) => seedEntrypointByWorkerArg.get(value)?.item,
218
+ resolveInsidePtySeedEntrypoint: () => insidePtySeedEntrypoint?.item,
219
+ resolveExecutableCliCommand: (requested) => executableCliCommandByName.get(requested)?.item,
220
+ resolveDefaultRootCliCommand: () => defaultRootCliCommand?.item,
221
+ resolveTaskSourceFactoryByKind: (kind) => taskSourceFactoryByKind.get(kind)
222
+ };
223
+ }
224
+
225
+ // packages/core/src/load-config.ts
226
+ import { existsSync as existsSync2, mkdirSync, mkdtempSync, readFileSync as readFileSync2, readdirSync, rmSync, statSync } from "fs";
227
+ import { isBuiltin } from "module";
228
+ import { dirname, isAbsolute, join as join2, relative, resolve } from "path";
229
+ import { pathToFileURL } from "url";
230
+ import { Schema as Schema3 } from "effect";
231
+ import { RigConfig as RigConfig3 } from "@rig/contracts";
232
+
233
+ // packages/core/src/define-config.ts
234
+ import { Schema } from "effect";
235
+ import { RigConfig } from "@rig/contracts";
236
+ function normalizeWorkspaceConfig(raw) {
237
+ const workspace = raw && typeof raw === "object" && !Array.isArray(raw) ? { ...raw } : {};
238
+ if (workspace.mainRepo === undefined)
239
+ workspace.mainRepo = ".";
240
+ if (workspace.checkout === undefined && workspace.isolation !== undefined)
241
+ workspace.checkout = workspace.isolation;
242
+ if (workspace.isolation === undefined && workspace.checkout !== undefined)
243
+ workspace.isolation = workspace.checkout;
244
+ return workspace;
245
+ }
246
+ function applyConfigDefaults(raw) {
247
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
248
+ return raw;
249
+ const record = raw;
250
+ return {
251
+ ...record,
252
+ plugins: Array.isArray(record.plugins) ? record.plugins.flat() : [],
253
+ workspace: normalizeWorkspaceConfig(record.workspace)
254
+ };
255
+ }
256
+
257
+ // packages/core/src/declarative-config.ts
258
+ import { existsSync, readFileSync } from "fs";
259
+ import { join } from "path";
260
+ import { parse as parseToml } from "smol-toml";
261
+ import { Schema as Schema2 } from "effect";
262
+ import { RigConfig as RigConfig2 } from "@rig/contracts";
263
+
264
+ // packages/core/src/embedded-plugins.ts
265
+ var registered = null;
266
+ function getStandardPluginsResolver() {
267
+ return registered;
268
+ }
269
+ var bakedProjectConfig = null;
270
+ function getEmbeddedProjectConfig() {
271
+ return bakedProjectConfig;
272
+ }
273
+
274
+ // packages/core/src/declarative-config.ts
275
+ var DECLARATIVE_CONFIG_NAMES = ["rigfig.toml", "rigfig.json"];
276
+ function findDeclarativeConfigPath(cwd) {
277
+ const dir = join(cwd, ".rig");
278
+ for (const name of DECLARATIVE_CONFIG_NAMES) {
279
+ const candidate = join(dir, name);
280
+ if (existsSync(candidate))
281
+ return candidate;
282
+ }
283
+ return null;
284
+ }
285
+ function parseDeclarativeFile(path) {
286
+ const raw = readFileSync(path, "utf8");
287
+ const parsed = path.endsWith(".json") ? JSON.parse(raw) : parseToml(raw);
288
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
289
+ throw new Error(`Declarative config ${path} must parse to an object.`);
290
+ }
291
+ return parsed;
292
+ }
293
+ function loadDeclarativeConfig(path) {
294
+ const data = parseDeclarativeFile(path);
295
+ const standardSection = data.standard && typeof data.standard === "object" && !Array.isArray(data.standard) ? data.standard : {};
296
+ const useStandard = standardSection.enabled !== false;
297
+ let plugins = [];
298
+ if (useStandard) {
299
+ const resolver = getStandardPluginsResolver();
300
+ if (!resolver) {
301
+ throw new Error(`Declarative config ${path} needs the embedded standard plugins, but none were registered. ` + `This is a seed/boot wiring error (the binary entrypoint must import the standard-plugin registration).`);
302
+ }
303
+ plugins = resolver(data);
304
+ }
305
+ const { standard: _standardDirective, ...configFields } = data;
306
+ const withDefaults = applyConfigDefaults({ ...configFields, plugins });
307
+ const explicitPlugins = Array.isArray(withDefaults.plugins) ? [...withDefaults.plugins] : [];
308
+ const decoded = Schema2.decodeUnknownSync(RigConfig2)({
309
+ ...withDefaults,
310
+ plugins: explicitPlugins
311
+ });
312
+ return { ...decoded, plugins: explicitPlugins };
313
+ }
314
+
315
+ // packages/core/src/load-config.ts
316
+ var TS_NAMES = [".rig/rig.config.ts", ".rig/rig.config.mts"];
317
+ var JSON_NAMES = [".rig/rig.config.json"];
318
+ function isModuleResolutionError(error) {
319
+ const message = error instanceof Error ? error.message : String(error);
320
+ return message.includes("Cannot find module") || message.includes("Could not resolve");
321
+ }
322
+ function runningFromCompiledBinary() {
323
+ return import.meta.url.includes("$bunfs");
324
+ }
325
+ function packageNameAndSubpath(specifier) {
326
+ if (specifier.startsWith(".") || specifier.startsWith("/") || /^[a-zA-Z]+:/.test(specifier))
327
+ return null;
328
+ const parts = specifier.split("/");
329
+ const packageName = specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
330
+ if (!packageName)
331
+ return null;
332
+ const rest = parts.slice(specifier.startsWith("@") ? 2 : 1).join("/");
333
+ return { packageName, subpath: rest ? `./${rest}` : "." };
334
+ }
335
+ function exportTargetFromEntry(entry) {
336
+ if (typeof entry === "string")
337
+ return entry;
338
+ if (entry && typeof entry === "object" && !Array.isArray(entry)) {
339
+ const conditions = entry;
340
+ for (const key of ["bun", "import", "default", "require"]) {
341
+ if (typeof conditions[key] === "string")
342
+ return conditions[key];
343
+ }
344
+ }
345
+ return null;
346
+ }
347
+ function patternExportTarget(record, subpath) {
348
+ for (const [pattern, entry] of Object.entries(record)) {
349
+ if (!pattern.includes("*"))
350
+ continue;
351
+ const [prefix = "", suffix = ""] = pattern.split("*");
352
+ if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix))
353
+ continue;
354
+ const replacement = subpath.slice(prefix.length, subpath.length - suffix.length);
355
+ const target = exportTargetFromEntry(entry);
356
+ if (target)
357
+ return target.replace("*", replacement);
358
+ }
359
+ return null;
360
+ }
361
+ function exportTargetFromPackageJson(pkg, subpath) {
362
+ const exportsField = pkg.exports;
363
+ const target = (() => {
364
+ if (typeof exportsField === "string" && subpath === ".")
365
+ return exportsField;
366
+ if (!exportsField || typeof exportsField !== "object" || Array.isArray(exportsField))
367
+ return null;
368
+ const record = exportsField;
369
+ return exportTargetFromEntry(record[subpath] ?? (subpath === "." ? record["."] : undefined)) ?? patternExportTarget(record, subpath);
370
+ })();
371
+ if (target)
372
+ return target;
373
+ return subpath === "." && typeof pkg.module === "string" ? pkg.module : subpath === "." && typeof pkg.main === "string" ? pkg.main : null;
374
+ }
375
+ function resolvePackageDirFromBunStore(packageName, nodeModulesDir) {
376
+ const storeDir = join2(nodeModulesDir, ".bun");
377
+ if (!existsSync2(storeDir))
378
+ return null;
379
+ const encoded = packageName.replace("/", "+");
380
+ try {
381
+ const candidates = readdirSync(storeDir).filter((entry) => entry.startsWith(`${encoded}@`)).map((entry) => {
382
+ const candidateDir = join2(storeDir, entry, "node_modules", packageName);
383
+ const packageJsonPath = join2(candidateDir, "package.json");
384
+ if (!existsSync2(packageJsonPath))
385
+ return null;
386
+ try {
387
+ const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
388
+ return {
389
+ dir: candidateDir,
390
+ sortKey: pkg.version?.trim() || entry
391
+ };
392
+ } catch {
393
+ return {
394
+ dir: candidateDir,
395
+ sortKey: entry
396
+ };
397
+ }
398
+ }).filter((candidate) => candidate !== null).sort((a, b) => b.sortKey.localeCompare(a.sortKey, undefined, { numeric: true, sensitivity: "base" }));
399
+ return candidates[0]?.dir ?? null;
400
+ } catch {
401
+ return null;
402
+ }
403
+ }
404
+ function resolveDirectoryModulePath(directoryPath) {
405
+ const packageJsonPath = join2(directoryPath, "package.json");
406
+ if (existsSync2(packageJsonPath)) {
407
+ try {
408
+ const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
409
+ const target = exportTargetFromPackageJson(pkg, ".");
410
+ if (target) {
411
+ const resolved = resolveModulePath(join2(directoryPath, target));
412
+ if (resolved)
413
+ return resolved;
414
+ }
415
+ } catch {}
416
+ }
417
+ for (const candidate of ["index.js", "index.mjs", "index.cjs", "index.ts", "index.json"]) {
418
+ const resolved = resolveModulePath(join2(directoryPath, candidate));
419
+ if (resolved)
420
+ return resolved;
421
+ }
422
+ return null;
423
+ }
424
+ function resolveModulePath(candidatePath) {
425
+ if (!existsSync2(candidatePath)) {
426
+ for (const extension of [".ts", ".mts", ".tsx", ".js", ".mjs", ".cjs", ".json"]) {
427
+ const withExtension = `${candidatePath}${extension}`;
428
+ if (existsSync2(withExtension))
429
+ return resolveModulePath(withExtension);
430
+ }
431
+ return null;
432
+ }
433
+ try {
434
+ const stat = statSync(candidatePath);
435
+ if (stat.isFile())
436
+ return candidatePath;
437
+ if (stat.isDirectory())
438
+ return resolveDirectoryModulePath(candidatePath);
439
+ } catch {
440
+ return null;
441
+ }
442
+ return null;
443
+ }
444
+ function resolvePackageExportFromDir(packageDir, subpath) {
445
+ const packageJsonPath = join2(packageDir, "package.json");
446
+ if (existsSync2(packageJsonPath)) {
447
+ try {
448
+ const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
449
+ const target = exportTargetFromPackageJson(pkg, subpath);
450
+ if (target) {
451
+ const resolved = resolveModulePath(join2(packageDir, target));
452
+ if (resolved)
453
+ return resolved;
454
+ }
455
+ } catch {
456
+ return null;
457
+ }
458
+ }
459
+ if (subpath !== ".") {
460
+ const legacySubpath = subpath.replace(/^\.\//, "");
461
+ for (const candidate of [
462
+ join2(packageDir, legacySubpath),
463
+ join2(packageDir, `${legacySubpath}.js`),
464
+ join2(packageDir, `${legacySubpath}.mjs`),
465
+ join2(packageDir, `${legacySubpath}.cjs`),
466
+ join2(packageDir, `${legacySubpath}.ts`),
467
+ join2(packageDir, `${legacySubpath}.json`)
468
+ ]) {
469
+ const resolved = resolveModulePath(candidate);
470
+ if (resolved)
471
+ return resolved;
472
+ }
473
+ }
474
+ return subpath === "." ? resolveDirectoryModulePath(packageDir) : null;
475
+ }
476
+ var runtimeBundleQueue = Promise.resolve();
477
+ function enqueueRuntimeBundle(operation) {
478
+ const next = runtimeBundleQueue.then(operation, operation);
479
+ runtimeBundleQueue = next.then(() => {
480
+ return;
481
+ }, () => {
482
+ return;
483
+ });
484
+ return next;
485
+ }
486
+ function isWithinDir(candidatePath, rootPath) {
487
+ const rel = relative(resolve(rootPath), resolve(candidatePath));
488
+ return rel === "" || !rel.startsWith("..") && !isAbsolute(rel);
489
+ }
490
+ function resolvedFilePath(path, rootPath) {
491
+ if (!path || !isAbsolute(path))
492
+ return null;
493
+ const resolved = resolveModulePath(path);
494
+ if (!resolved)
495
+ return null;
496
+ return rootPath && !isWithinDir(resolved, rootPath) ? null : resolved;
497
+ }
498
+ function resolveProjectPackageImport(specifier, configDir) {
499
+ const parsed = packageNameAndSubpath(specifier);
500
+ if (!parsed)
501
+ return null;
502
+ const nodeModulesDir = join2(configDir, "node_modules");
503
+ const directPackageDir = join2(nodeModulesDir, parsed.packageName);
504
+ const packageDir = existsSync2(join2(directPackageDir, "package.json")) ? directPackageDir : resolvePackageDirFromBunStore(parsed.packageName, nodeModulesDir);
505
+ return packageDir ? resolvePackageExportFromDir(packageDir, parsed.subpath) : null;
506
+ }
507
+ function canImportRigWorkspacePackagesDirectly(source, configDir) {
508
+ const packageNames = new Set;
509
+ for (const match of source.matchAll(/(?:from\s*|import\s*\(\s*|import\s*)["'](@rig\/[^"']+)["']/g)) {
510
+ const parsed = packageNameAndSubpath(match[1] ?? "");
511
+ if (parsed?.packageName)
512
+ packageNames.add(parsed.packageName);
513
+ }
514
+ if (packageNames.size === 0)
515
+ return false;
516
+ for (const packageName of packageNames) {
517
+ const shortName = packageName.replace(/^@rig\//, "");
518
+ const manifestPath = join2(configDir, "packages", shortName, "package.json");
519
+ if (!existsSync2(manifestPath))
520
+ return false;
521
+ try {
522
+ const parsed = JSON.parse(readFileSync2(manifestPath, "utf8"));
523
+ if (parsed.name !== packageName)
524
+ return false;
525
+ } catch {
526
+ return false;
527
+ }
528
+ }
529
+ return true;
530
+ }
531
+ async function importConfigViaRuntimeBundleUnserialized(configPath) {
532
+ const bun = globalThis.Bun;
533
+ if (!bun?.build) {
534
+ throw new Error(`Failed to import ${configPath}: bare imports could not be resolved and no Bun.build runtime bundler is available.`);
535
+ }
536
+ const RUNTIME_ONLY_EXTERNAL_PACKAGES = new Set(["effect", "mupdf", "fastembed", "onnxruntime-node", "markit-ai"]);
537
+ const configDir = dirname(configPath);
538
+ const UNRESOLVED_NAMESPACE = "rig-config-unresolved";
539
+ const unresolvedLocalPlugin = {
540
+ name: "rig-config-unresolved-local",
541
+ setup(build) {
542
+ build.onLoad({ filter: /\.(?:html|txt)$/ }, (args) => ({
543
+ loader: "js",
544
+ contents: `export default ${JSON.stringify(readFileSync2(args.path, "utf8"))};
545
+ `
546
+ }));
547
+ build.onResolve({ filter: /.*/ }, (args) => {
548
+ const directFilePath = resolvedFilePath(args.path, configDir);
549
+ if (directFilePath)
550
+ return { path: directFilePath };
551
+ const packageImport = packageNameAndSubpath(args.path);
552
+ if (packageImport && (packageImport.packageName.startsWith("@rig/") || RUNTIME_ONLY_EXTERNAL_PACKAGES.has(packageImport.packageName))) {
553
+ return { path: args.path, external: true };
554
+ }
555
+ const projectPackagePath = resolveProjectPackageImport(args.path, configDir);
556
+ if (projectPackagePath)
557
+ return { path: projectPackagePath };
558
+ if (/^(?:node|bun):/.test(args.path) || isBuiltin(args.path))
559
+ return;
560
+ if (packageImport)
561
+ return { path: args.path, external: true };
562
+ const parentCandidates = args.path.startsWith(".") ? [args.importer && isAbsolute(args.importer) ? dirname(args.importer) : null, configDir].filter((value) => Boolean(value)) : [args.importer && isAbsolute(args.importer) ? dirname(args.importer) : configDir];
563
+ for (const parent2 of parentCandidates) {
564
+ const filePath = resolvedFilePath(resolve(parent2, args.path), configDir);
565
+ if (filePath)
566
+ return { path: filePath };
567
+ }
568
+ const parent = parentCandidates[0] ?? configDir;
569
+ try {
570
+ const resolved = bun.resolveSync?.(args.path, parent) ?? resolve(parent, args.path);
571
+ const filePath = resolvedFilePath(resolved, configDir);
572
+ if (filePath)
573
+ return { path: filePath };
574
+ } catch {}
575
+ return { path: args.path, namespace: UNRESOLVED_NAMESPACE };
576
+ });
577
+ build.onLoad({ filter: /.*/, namespace: UNRESOLVED_NAMESPACE }, (args) => ({
578
+ loader: "js",
579
+ contents: `module.exports = {};
580
+ throw new Error(${JSON.stringify(`Failed to bundle ${configPath}: Could not resolve local import "${args.path}". Maybe you need to fix a relative import in rig.config.ts or install project deps.`)});
581
+ `
582
+ }));
583
+ }
584
+ };
585
+ const result = await bun.build({
586
+ entrypoints: [configPath],
587
+ target: "bun",
588
+ external: ["effect", "mupdf", "fastembed", "onnxruntime-node", "markit-ai"],
589
+ format: "esm",
590
+ throw: false,
591
+ packages: "bundle",
592
+ plugins: [unresolvedLocalPlugin]
593
+ });
594
+ if (!result.success || !result.outputs[0]) {
595
+ const detail = result.logs.map((log) => String(log)).join(`
596
+ `);
597
+ throw new Error(`Failed to bundle ${configPath}: ${detail || "unknown bundler error"}`);
598
+ }
599
+ const bundleParentDir = join2(configDir, ".rig", "tmp");
600
+ mkdirSync(bundleParentDir, { recursive: true });
601
+ const dir = mkdtempSync(join2(bundleParentDir, "rig-config-bundle-"));
602
+ try {
603
+ const bundledPath = join2(dir, "rig.config.bundled.js");
604
+ await bun.write(bundledPath, await result.outputs[0].text());
605
+ return await import(pathToFileURL(bundledPath).href);
606
+ } finally {
607
+ try {
608
+ rmSync(dir, { recursive: true, force: true });
609
+ } catch {}
610
+ }
611
+ }
612
+ async function loadConfig(cwd) {
613
+ const baked = getEmbeddedProjectConfig();
614
+ if (baked && resolve(baked.projectRoot) === resolve(cwd)) {
615
+ return decodeExplicitPluginConfig(baked.raw);
616
+ }
617
+ for (const name of TS_NAMES) {
618
+ const p = join2(cwd, name);
619
+ if (existsSync2(p)) {
620
+ const mod = await enqueueRuntimeBundle(async () => {
621
+ if (runningFromCompiledBinary()) {
622
+ return importConfigViaRuntimeBundleUnserialized(p);
623
+ }
624
+ const source = readFileSync2(p, "utf8");
625
+ const importsRigHostPackages = /(?:import\s+[^;]*?from\s*|import\s*\()\s*["']@rig\//.test(source);
626
+ if (importsRigHostPackages && !canImportRigWorkspacePackagesDirectly(source, cwd)) {
627
+ return importConfigViaRuntimeBundleUnserialized(p);
628
+ }
629
+ try {
630
+ return await import(pathToFileURL(p).href);
631
+ } catch (error) {
632
+ if (!isModuleResolutionError(error))
633
+ throw error;
634
+ return importConfigViaRuntimeBundleUnserialized(p);
635
+ }
636
+ });
637
+ const raw = mod.default ?? mod.config;
638
+ return decodeExplicitPluginConfig(raw);
639
+ }
640
+ }
641
+ const declarativePath = findDeclarativeConfigPath(cwd);
642
+ if (declarativePath) {
643
+ return loadDeclarativeConfig(declarativePath);
644
+ }
645
+ for (const name of JSON_NAMES) {
646
+ const p = join2(cwd, name);
647
+ if (existsSync2(p)) {
648
+ const raw = JSON.parse(readFileSync2(p, "utf-8"));
649
+ return decodeExplicitPluginConfig(raw);
650
+ }
651
+ }
652
+ throw new Error(`no .rig/rig.config.{ts,mts,json} or .rig/rigfig.{toml,json} found in ${cwd}`);
653
+ }
654
+ function decodeExplicitPluginConfig(raw) {
655
+ const withDefaults = applyConfigDefaults(raw);
656
+ const explicitPlugins = Array.isArray(withDefaults.plugins) ? [...withDefaults.plugins] : [];
657
+ const decoded = Schema3.decodeUnknownSync(RigConfig3)({
658
+ ...withDefaults,
659
+ plugins: explicitPlugins
660
+ });
661
+ return { ...decoded, plugins: explicitPlugins };
662
+ }
663
+
664
+ // packages/core/src/project-plugins.ts
665
+ var TS_CONFIG_FILENAMES = ["rig.config.ts", "rig.config.mts"];
666
+ var JSON_CONFIG_FILENAMES = ["rig.config.json"];
667
+ function findProjectConfigPath(projectRoot) {
668
+ for (const name of TS_CONFIG_FILENAMES) {
669
+ const path = resolve2(projectRoot, ".rig", name);
670
+ if (existsSync3(path))
671
+ return path;
672
+ }
673
+ const declarativePath = findDeclarativeConfigPath(projectRoot);
674
+ if (declarativePath)
675
+ return declarativePath;
676
+ for (const name of JSON_CONFIG_FILENAMES) {
677
+ const path = resolve2(projectRoot, ".rig", name);
678
+ if (existsSync3(path))
679
+ return path;
680
+ }
681
+ return null;
682
+ }
683
+ var pluginHostCache = new Map;
684
+ function configFileMtimeMs(normalizedRoot) {
685
+ const path = findProjectConfigPath(normalizedRoot);
686
+ if (!path)
687
+ return 0;
688
+ try {
689
+ return statSync2(path).mtimeMs;
690
+ } catch {
691
+ return 0;
692
+ }
693
+ }
694
+ function resolvePluginHost(projectRoot, options = {}) {
695
+ const normalizedRoot = resolve2(projectRoot);
696
+ const build = (load) => load(normalizedRoot).then((config) => ({
697
+ host: createPluginHost(config.plugins),
698
+ config
699
+ }));
700
+ if (options.load)
701
+ return build(options.load);
702
+ const mtimeMs = configFileMtimeMs(normalizedRoot);
703
+ const cached = pluginHostCache.get(normalizedRoot);
704
+ if (cached && cached.mtimeMs === mtimeMs)
705
+ return cached.promise;
706
+ const promise = build(loadConfig);
707
+ promise.catch(() => {
708
+ if (pluginHostCache.get(normalizedRoot)?.promise === promise) {
709
+ pluginHostCache.delete(normalizedRoot);
710
+ }
711
+ });
712
+ pluginHostCache.set(normalizedRoot, { mtimeMs, promise });
713
+ return promise;
714
+ }
715
+
716
+ // packages/core/src/capability-loaders.ts
717
+ async function buildProjectPluginHost(projectRoot) {
718
+ try {
719
+ return (await resolvePluginHost(projectRoot)).host;
720
+ } catch {
721
+ return null;
722
+ }
723
+ }
724
+ async function loadCapabilityForRoot(projectRoot, capability) {
725
+ const host = await buildProjectPluginHost(projectRoot);
726
+ return host ? capability.resolve(host) : null;
727
+ }
728
+ async function requireCapabilityForRoot(projectRoot, capability, message) {
729
+ const resolved = await loadCapabilityForRoot(projectRoot, capability);
730
+ if (!resolved)
731
+ throw new Error(message);
732
+ return resolved;
733
+ }
734
+ var INSTALLED = new Map;
735
+ function installCapability(capability, impl) {
736
+ INSTALLED.set(String(capability.id), impl);
737
+ }
738
+ function clearInstalledCapability(capability) {
739
+ INSTALLED.delete(String(capability.id));
740
+ }
741
+ function getInstalledCapability(capability) {
742
+ return INSTALLED.get(String(capability.id)) ?? null;
743
+ }
744
+ function requireInstalledCapability(capability, message) {
745
+ const impl = getInstalledCapability(capability);
746
+ if (!impl)
747
+ throw new Error(message);
748
+ return impl;
749
+ }
750
+ export {
751
+ requireInstalledCapability,
752
+ requireCapabilityForRoot,
753
+ loadCapabilityForRoot,
754
+ installCapability,
755
+ getInstalledCapability,
756
+ clearInstalledCapability,
757
+ buildProjectPluginHost
758
+ };