@h-rig/core 0.0.6-alpha.18 → 0.0.6-alpha.181
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.
- package/dist/src/agent-role-registry.d.ts +4 -0
- package/dist/src/agent-role-registry.js +27 -0
- package/dist/src/authority-paths.d.ts +15 -0
- package/dist/src/authority-paths.js +80 -0
- package/dist/src/baked-secrets.d.ts +3 -0
- package/dist/src/baked-secrets.js +63 -0
- package/dist/src/build-time-config.d.ts +12 -0
- package/dist/src/build-time-config.js +25 -0
- package/dist/src/build-time-config.macro.d.ts +1 -0
- package/dist/src/capability-loaders.d.ts +51 -0
- package/dist/src/capability-loaders.js +870 -0
- package/dist/src/capability.d.ts +79 -0
- package/dist/src/capability.js +63 -0
- package/dist/src/checkout-root.d.ts +1 -0
- package/dist/src/checkout-root.js +30 -0
- package/dist/src/config-env.d.ts +4 -0
- package/dist/src/config-env.js +23 -0
- package/dist/src/config.d.ts +3 -0
- package/dist/src/config.js +44 -0
- package/dist/src/declarative-config.d.ts +14 -0
- package/dist/src/declarative-config.js +85 -0
- package/dist/src/default-kernel.d.ts +1 -0
- package/dist/src/default-kernel.js +12 -0
- package/dist/src/define-config.d.ts +20 -0
- package/dist/src/define-config.js +28 -15
- package/dist/src/define-plugin.d.ts +13 -0
- package/dist/src/define-plugin.js +4 -43
- package/dist/src/embedded-plugins.d.ts +59 -0
- package/dist/src/embedded-plugins.js +22 -0
- package/dist/src/exec.d.ts +13 -0
- package/dist/src/exec.js +101 -0
- package/dist/src/harness-paths.d.ts +9 -0
- package/dist/src/harness-paths.js +126 -0
- package/dist/src/hook-materializer.d.ts +21 -0
- package/dist/src/hook-materializer.js +152 -0
- package/dist/src/hook-protocol.d.ts +2 -0
- package/dist/src/hook-protocol.js +432 -0
- package/dist/src/hook-runner.d.ts +48 -0
- package/dist/src/hook-runner.js +868 -0
- package/dist/src/hook-runtime.d.ts +52 -0
- package/dist/src/hook-runtime.js +432 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.js +210 -2499
- package/dist/src/json-files.d.ts +9 -0
- package/dist/src/json-files.js +124 -0
- package/dist/src/kernel-boot.d.ts +2 -0
- package/dist/src/kernel-boot.js +10 -0
- package/dist/src/kernel-entrypoint.d.ts +22 -0
- package/dist/src/kernel-entrypoint.js +660 -0
- package/dist/src/kernel-plugin-abi.d.ts +1 -0
- package/dist/src/kernel-plugin-abi.js +1 -0
- package/dist/src/kernel-resolver.d.ts +2 -0
- package/dist/src/kernel-resolver.js +6 -0
- package/dist/src/layout.d.ts +10 -0
- package/dist/src/layout.js +138 -0
- package/dist/src/load-config.d.ts +2 -0
- package/dist/src/load-config.js +535 -30
- package/dist/src/placement.d.ts +58 -0
- package/dist/src/placement.js +53 -0
- package/dist/src/plugin-host-context.d.ts +65 -0
- package/dist/src/plugin-host-context.js +1171 -0
- package/dist/src/plugin-host-registries.d.ts +31 -0
- package/dist/src/plugin-host-registries.js +79 -0
- package/dist/src/plugin-host.d.ts +77 -0
- package/dist/src/plugin-host.js +127 -63
- package/dist/src/plugin-runtime.d.ts +173 -0
- package/dist/src/project-plugins.d.ts +63 -0
- package/dist/src/project-plugins.js +905 -0
- package/dist/src/remote-config.d.ts +125 -0
- package/dist/src/remote-config.js +85 -0
- package/dist/src/root-resolver.d.ts +5 -0
- package/dist/src/root-resolver.js +68 -0
- package/dist/src/run-provisioning.d.ts +37 -0
- package/dist/src/run-provisioning.js +35 -0
- package/dist/src/runtime-context.d.ts +20 -0
- package/dist/src/runtime-context.js +257 -0
- package/dist/src/runtime-events.d.ts +44 -0
- package/dist/src/runtime-events.js +208 -0
- package/dist/src/runtime-overlay.d.ts +11 -0
- package/dist/src/runtime-overlay.js +69 -0
- package/dist/src/runtime-paths.d.ts +21 -0
- package/dist/src/runtime-paths.js +181 -0
- package/dist/src/runtime-provisioning-env.d.ts +5 -0
- package/dist/src/runtime-provisioning-env.js +217 -0
- package/dist/src/runtime-runner-context.d.ts +12 -0
- package/dist/src/runtime-runner-context.js +1 -0
- package/dist/src/safe-identifiers.d.ts +44 -0
- package/dist/src/safe-identifiers.js +96 -0
- package/dist/src/scope-rules.d.ts +4 -0
- package/dist/src/scope-rules.js +21 -0
- package/dist/src/server-paths.d.ts +22 -0
- package/dist/src/server-paths.js +219 -0
- package/dist/src/setup-version.d.ts +3 -0
- package/dist/src/setup-version.js +14 -0
- package/dist/src/task-record-reader.d.ts +3 -0
- package/dist/src/task-record-reader.js +9 -0
- package/dist/src/validator-registry.d.ts +27 -0
- package/dist/src/validator-registry.js +64 -0
- package/package.json +162 -10
- package/dist/src/engineReadModelReducer.js +0 -1780
- package/dist/src/rig-init-builder.js +0 -57
- package/dist/src/rigSelectors.js +0 -293
- package/dist/src/taskGraph.js +0 -64
- package/dist/src/taskGraphCodes.js +0 -26
- package/dist/src/taskGraphLayout.js +0 -374
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/core/src/kernel-entrypoint.ts
|
|
3
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
4
|
+
import { resolve as resolve2 } from "path";
|
|
5
|
+
|
|
6
|
+
// packages/core/src/kernel-boot.ts
|
|
7
|
+
import {
|
|
8
|
+
bootDefaultKernelIntoProcess,
|
|
9
|
+
getProcessKernel
|
|
10
|
+
} from "@rig/kernel-seed/boot-default";
|
|
11
|
+
|
|
12
|
+
// packages/core/src/default-kernel.ts
|
|
13
|
+
import {
|
|
14
|
+
createDefaultKernel,
|
|
15
|
+
createDefaultKernelPlugin,
|
|
16
|
+
createPlacementTransportPlugin
|
|
17
|
+
} from "@rig/kernel-seed/default-kernel";
|
|
18
|
+
|
|
19
|
+
// packages/core/src/config-env.ts
|
|
20
|
+
function applyConfigEnv(config, env = process.env) {
|
|
21
|
+
const fill = (key, value) => {
|
|
22
|
+
if (value && !env[key])
|
|
23
|
+
env[key] = value;
|
|
24
|
+
};
|
|
25
|
+
const server = config.runtime?.server;
|
|
26
|
+
if (server) {
|
|
27
|
+
const host = server.host?.trim();
|
|
28
|
+
fill("RIG_COLLAB_RELAY", server.relayUrl ?? (host ? `wss://${host}` : undefined));
|
|
29
|
+
fill("RIG_REGISTRY_URL", server.registryUrl ?? (host ? `https://${host}/registry` : undefined));
|
|
30
|
+
fill("RIG_SSH_TARGET", server.sshTarget);
|
|
31
|
+
fill("RIG_REMOTE_CHECKOUT", server.checkout);
|
|
32
|
+
}
|
|
33
|
+
for (const [key, value] of Object.entries(config.env ?? {}))
|
|
34
|
+
fill(key, value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// packages/core/src/load-config.ts
|
|
38
|
+
import { existsSync as existsSync2, mkdirSync, mkdtempSync, readFileSync as readFileSync2, readdirSync, rmSync, statSync } from "fs";
|
|
39
|
+
import { isBuiltin } from "module";
|
|
40
|
+
import { basename, dirname, isAbsolute, join as join2, relative, resolve } from "path";
|
|
41
|
+
import { pathToFileURL } from "url";
|
|
42
|
+
import { Schema as Schema3 } from "effect";
|
|
43
|
+
import { RigConfig as RigConfig3 } from "@rig/contracts";
|
|
44
|
+
|
|
45
|
+
// packages/core/src/define-config.ts
|
|
46
|
+
import { Schema } from "effect";
|
|
47
|
+
import { RigConfig } from "@rig/contracts";
|
|
48
|
+
function normalizeWorkspaceConfig(raw) {
|
|
49
|
+
const workspace = raw && typeof raw === "object" && !Array.isArray(raw) ? { ...raw } : {};
|
|
50
|
+
if (workspace.mainRepo === undefined)
|
|
51
|
+
workspace.mainRepo = ".";
|
|
52
|
+
if (workspace.checkout === undefined && workspace.isolation !== undefined)
|
|
53
|
+
workspace.checkout = workspace.isolation;
|
|
54
|
+
if (workspace.isolation === undefined && workspace.checkout !== undefined)
|
|
55
|
+
workspace.isolation = workspace.checkout;
|
|
56
|
+
return workspace;
|
|
57
|
+
}
|
|
58
|
+
function applyConfigDefaults(raw) {
|
|
59
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
60
|
+
return raw;
|
|
61
|
+
const record = raw;
|
|
62
|
+
return {
|
|
63
|
+
...record,
|
|
64
|
+
plugins: Array.isArray(record.plugins) ? record.plugins.flat() : [],
|
|
65
|
+
workspace: normalizeWorkspaceConfig(record.workspace)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// packages/core/src/declarative-config.ts
|
|
70
|
+
import { existsSync, readFileSync } from "fs";
|
|
71
|
+
import { join } from "path";
|
|
72
|
+
import { parse as parseToml } from "smol-toml";
|
|
73
|
+
import { Schema as Schema2 } from "effect";
|
|
74
|
+
import { RigConfig as RigConfig2 } from "@rig/contracts";
|
|
75
|
+
|
|
76
|
+
// packages/core/src/embedded-plugins.ts
|
|
77
|
+
var registered = null;
|
|
78
|
+
function getStandardPluginsResolver() {
|
|
79
|
+
return registered;
|
|
80
|
+
}
|
|
81
|
+
var bakedProjectConfig = null;
|
|
82
|
+
function getEmbeddedProjectConfig() {
|
|
83
|
+
return bakedProjectConfig;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// packages/core/src/declarative-config.ts
|
|
87
|
+
var DECLARATIVE_CONFIG_NAMES = ["rigfig.toml", "rigfig.json"];
|
|
88
|
+
function findDeclarativeConfigPath(cwd) {
|
|
89
|
+
const dir = join(cwd, ".rig");
|
|
90
|
+
for (const name of DECLARATIVE_CONFIG_NAMES) {
|
|
91
|
+
const candidate = join(dir, name);
|
|
92
|
+
if (existsSync(candidate))
|
|
93
|
+
return candidate;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
function parseDeclarativeFile(path) {
|
|
98
|
+
const raw = readFileSync(path, "utf8");
|
|
99
|
+
const parsed = path.endsWith(".json") ? JSON.parse(raw) : parseToml(raw);
|
|
100
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
101
|
+
throw new Error(`Declarative config ${path} must parse to an object.`);
|
|
102
|
+
}
|
|
103
|
+
return parsed;
|
|
104
|
+
}
|
|
105
|
+
function loadDeclarativeConfig(path) {
|
|
106
|
+
const data = parseDeclarativeFile(path);
|
|
107
|
+
const standardSection = data.standard && typeof data.standard === "object" && !Array.isArray(data.standard) ? data.standard : null;
|
|
108
|
+
if (standardSection?.enabled !== true && standardSection?.enabled !== false) {
|
|
109
|
+
throw new Error(`Declarative config ${path} must explicitly set [standard] enabled = true or false.`);
|
|
110
|
+
}
|
|
111
|
+
const useStandard = standardSection.enabled === true;
|
|
112
|
+
let plugins = [];
|
|
113
|
+
if (useStandard) {
|
|
114
|
+
const resolver = getStandardPluginsResolver();
|
|
115
|
+
if (!resolver) {
|
|
116
|
+
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).`);
|
|
117
|
+
}
|
|
118
|
+
plugins = resolver(data);
|
|
119
|
+
}
|
|
120
|
+
const { standard: _standardDirective, ...configFields } = data;
|
|
121
|
+
const withDefaults = applyConfigDefaults({ ...configFields, plugins });
|
|
122
|
+
const explicitPlugins = Array.isArray(withDefaults.plugins) ? [...withDefaults.plugins] : [];
|
|
123
|
+
const decoded = Schema2.decodeUnknownSync(RigConfig2)({
|
|
124
|
+
...withDefaults,
|
|
125
|
+
plugins: explicitPlugins
|
|
126
|
+
});
|
|
127
|
+
return { ...decoded, plugins: explicitPlugins };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// packages/core/src/load-config.ts
|
|
131
|
+
var TS_NAMES = [".rig/rig.config.ts", ".rig/rig.config.mts"];
|
|
132
|
+
var JSON_NAMES = [".rig/rig.config.json"];
|
|
133
|
+
function isModuleResolutionError(error) {
|
|
134
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
135
|
+
return message.includes("Cannot find module") || message.includes("Could not resolve");
|
|
136
|
+
}
|
|
137
|
+
function runningFromCompiledBinary() {
|
|
138
|
+
return import.meta.url.includes("$bunfs");
|
|
139
|
+
}
|
|
140
|
+
function packageNameAndSubpath(specifier) {
|
|
141
|
+
if (specifier.startsWith(".") || specifier.startsWith("/") || /^[a-zA-Z]+:/.test(specifier))
|
|
142
|
+
return null;
|
|
143
|
+
const parts = specifier.split("/");
|
|
144
|
+
const packageName = specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
145
|
+
if (!packageName)
|
|
146
|
+
return null;
|
|
147
|
+
const rest = parts.slice(specifier.startsWith("@") ? 2 : 1).join("/");
|
|
148
|
+
return { packageName, subpath: rest ? `./${rest}` : "." };
|
|
149
|
+
}
|
|
150
|
+
function exportTargetFromEntry(entry) {
|
|
151
|
+
if (typeof entry === "string")
|
|
152
|
+
return entry;
|
|
153
|
+
if (Array.isArray(entry)) {
|
|
154
|
+
for (const candidate of entry) {
|
|
155
|
+
const target = exportTargetFromEntry(candidate);
|
|
156
|
+
if (target)
|
|
157
|
+
return target;
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (entry && typeof entry === "object") {
|
|
162
|
+
const conditions = entry;
|
|
163
|
+
for (const key of ["bun", "node", "import", "default", "require"]) {
|
|
164
|
+
const target = exportTargetFromEntry(conditions[key]);
|
|
165
|
+
if (target)
|
|
166
|
+
return target;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
function patternExportTarget(record, subpath) {
|
|
172
|
+
const entries = Object.entries(record).filter(([pattern]) => pattern.includes("*")).sort(([a], [b]) => b.replace("*", "").length - a.replace("*", "").length);
|
|
173
|
+
for (const [pattern, entry] of entries) {
|
|
174
|
+
const [prefix = "", suffix = ""] = pattern.split("*");
|
|
175
|
+
if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix))
|
|
176
|
+
continue;
|
|
177
|
+
const replacement = subpath.slice(prefix.length, subpath.length - suffix.length);
|
|
178
|
+
const target = exportTargetFromEntry(entry);
|
|
179
|
+
if (target)
|
|
180
|
+
return target.replace("*", replacement);
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
function exportTargetFromPackageJson(pkg, subpath) {
|
|
185
|
+
const exportsField = pkg.exports;
|
|
186
|
+
const target = (() => {
|
|
187
|
+
if (typeof exportsField === "string" && subpath === ".")
|
|
188
|
+
return exportsField;
|
|
189
|
+
if (!exportsField || typeof exportsField !== "object" || Array.isArray(exportsField))
|
|
190
|
+
return null;
|
|
191
|
+
const record = exportsField;
|
|
192
|
+
return exportTargetFromEntry(record[subpath] ?? (subpath === "." ? record["."] : undefined)) ?? patternExportTarget(record, subpath);
|
|
193
|
+
})();
|
|
194
|
+
if (target)
|
|
195
|
+
return target;
|
|
196
|
+
return subpath === "." && typeof pkg.module === "string" ? pkg.module : subpath === "." && typeof pkg.main === "string" ? pkg.main : null;
|
|
197
|
+
}
|
|
198
|
+
function patternImportTarget(record, specifier) {
|
|
199
|
+
for (const [pattern, entry] of Object.entries(record)) {
|
|
200
|
+
if (!pattern.includes("*"))
|
|
201
|
+
continue;
|
|
202
|
+
const [prefix = "", suffix = ""] = pattern.split("*");
|
|
203
|
+
if (!specifier.startsWith(prefix) || !specifier.endsWith(suffix))
|
|
204
|
+
continue;
|
|
205
|
+
const replacement = specifier.slice(prefix.length, specifier.length - suffix.length);
|
|
206
|
+
const target = exportTargetFromEntry(entry);
|
|
207
|
+
if (target)
|
|
208
|
+
return target.replace("*", replacement);
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
function resolvePackagePrivateImport(specifier, importer, projectRoot) {
|
|
213
|
+
if (!specifier.startsWith("#") || !importer || !isAbsolute(importer))
|
|
214
|
+
return null;
|
|
215
|
+
let dir = dirname(importer);
|
|
216
|
+
const stop = resolve(projectRoot);
|
|
217
|
+
while (isWithinDir(dir, stop)) {
|
|
218
|
+
const packageJsonPath = join2(dir, "package.json");
|
|
219
|
+
if (existsSync2(packageJsonPath)) {
|
|
220
|
+
try {
|
|
221
|
+
const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
|
|
222
|
+
const imports = pkg.imports;
|
|
223
|
+
if (imports && typeof imports === "object" && !Array.isArray(imports)) {
|
|
224
|
+
const record = imports;
|
|
225
|
+
const target = exportTargetFromEntry(record[specifier]) ?? patternImportTarget(record, specifier);
|
|
226
|
+
if (target)
|
|
227
|
+
return resolveModulePath(join2(dir, target));
|
|
228
|
+
}
|
|
229
|
+
} catch {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
const parent = dirname(dir);
|
|
235
|
+
if (parent === dir)
|
|
236
|
+
return null;
|
|
237
|
+
dir = parent;
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
function resolvePackageDirFromBunStore(packageName, nodeModulesDir) {
|
|
242
|
+
const storeDir = join2(nodeModulesDir, ".bun");
|
|
243
|
+
if (!existsSync2(storeDir))
|
|
244
|
+
return null;
|
|
245
|
+
const encoded = packageName.replace("/", "+");
|
|
246
|
+
try {
|
|
247
|
+
const candidates = readdirSync(storeDir).filter((entry) => entry.startsWith(`${encoded}@`)).map((entry) => {
|
|
248
|
+
const candidateDir = join2(storeDir, entry, "node_modules", packageName);
|
|
249
|
+
const packageJsonPath = join2(candidateDir, "package.json");
|
|
250
|
+
if (!existsSync2(packageJsonPath))
|
|
251
|
+
return null;
|
|
252
|
+
try {
|
|
253
|
+
const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
|
|
254
|
+
return {
|
|
255
|
+
dir: candidateDir,
|
|
256
|
+
sortKey: pkg.version?.trim() || entry
|
|
257
|
+
};
|
|
258
|
+
} catch {
|
|
259
|
+
return {
|
|
260
|
+
dir: candidateDir,
|
|
261
|
+
sortKey: entry
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
}).filter((candidate) => candidate !== null).sort((a, b) => b.sortKey.localeCompare(a.sortKey, undefined, { numeric: true, sensitivity: "base" }));
|
|
265
|
+
return candidates[0]?.dir ?? null;
|
|
266
|
+
} catch {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function resolveDirectoryModulePath(directoryPath) {
|
|
271
|
+
const packageJsonPath = join2(directoryPath, "package.json");
|
|
272
|
+
if (existsSync2(packageJsonPath)) {
|
|
273
|
+
try {
|
|
274
|
+
const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
|
|
275
|
+
const target = exportTargetFromPackageJson(pkg, ".");
|
|
276
|
+
if (target) {
|
|
277
|
+
const resolved = resolveModulePath(join2(directoryPath, target));
|
|
278
|
+
if (resolved)
|
|
279
|
+
return resolved;
|
|
280
|
+
}
|
|
281
|
+
} catch {}
|
|
282
|
+
}
|
|
283
|
+
for (const candidate of ["index.js", "index.mjs", "index.cjs", "index.ts", "index.json"]) {
|
|
284
|
+
const resolved = resolveModulePath(join2(directoryPath, candidate));
|
|
285
|
+
if (resolved)
|
|
286
|
+
return resolved;
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
function resolveModulePath(candidatePath) {
|
|
291
|
+
if (!existsSync2(candidatePath)) {
|
|
292
|
+
for (const extension of [".ts", ".mts", ".tsx", ".js", ".mjs", ".cjs", ".json"]) {
|
|
293
|
+
const withExtension = `${candidatePath}${extension}`;
|
|
294
|
+
if (existsSync2(withExtension))
|
|
295
|
+
return resolveModulePath(withExtension);
|
|
296
|
+
}
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
const stat = statSync(candidatePath);
|
|
301
|
+
if (stat.isFile())
|
|
302
|
+
return candidatePath;
|
|
303
|
+
if (stat.isDirectory())
|
|
304
|
+
return resolveDirectoryModulePath(candidatePath);
|
|
305
|
+
} catch {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
function resolvePackageExportFromDir(packageDir, subpath) {
|
|
311
|
+
const packageJsonPath = join2(packageDir, "package.json");
|
|
312
|
+
if (existsSync2(packageJsonPath)) {
|
|
313
|
+
try {
|
|
314
|
+
const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf8"));
|
|
315
|
+
const targets = [
|
|
316
|
+
exportTargetFromPackageJson(pkg, subpath),
|
|
317
|
+
...subpath === "." ? [typeof pkg.module === "string" ? pkg.module : null, typeof pkg.main === "string" ? pkg.main : null] : []
|
|
318
|
+
];
|
|
319
|
+
const seen = new Set;
|
|
320
|
+
for (const target of targets) {
|
|
321
|
+
if (!target || seen.has(target))
|
|
322
|
+
continue;
|
|
323
|
+
seen.add(target);
|
|
324
|
+
const resolved = resolveModulePath(join2(packageDir, target));
|
|
325
|
+
if (resolved)
|
|
326
|
+
return resolved;
|
|
327
|
+
}
|
|
328
|
+
} catch {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (subpath !== ".") {
|
|
333
|
+
const legacySubpath = subpath.replace(/^\.\//, "");
|
|
334
|
+
for (const candidate of [
|
|
335
|
+
join2(packageDir, legacySubpath),
|
|
336
|
+
join2(packageDir, `${legacySubpath}.js`),
|
|
337
|
+
join2(packageDir, `${legacySubpath}.mjs`),
|
|
338
|
+
join2(packageDir, `${legacySubpath}.cjs`),
|
|
339
|
+
join2(packageDir, `${legacySubpath}.ts`),
|
|
340
|
+
join2(packageDir, `${legacySubpath}.json`)
|
|
341
|
+
]) {
|
|
342
|
+
const resolved = resolveModulePath(candidate);
|
|
343
|
+
if (resolved)
|
|
344
|
+
return resolved;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return subpath === "." ? resolveDirectoryModulePath(packageDir) : null;
|
|
348
|
+
}
|
|
349
|
+
var runtimeBundleQueue = Promise.resolve();
|
|
350
|
+
function enqueueRuntimeBundle(operation) {
|
|
351
|
+
const next = runtimeBundleQueue.then(operation, operation);
|
|
352
|
+
runtimeBundleQueue = next.then(() => {
|
|
353
|
+
return;
|
|
354
|
+
}, () => {
|
|
355
|
+
return;
|
|
356
|
+
});
|
|
357
|
+
return next;
|
|
358
|
+
}
|
|
359
|
+
function isWithinDir(candidatePath, rootPath) {
|
|
360
|
+
const rel = relative(resolve(rootPath), resolve(candidatePath));
|
|
361
|
+
return rel === "" || !rel.startsWith("..") && !isAbsolute(rel);
|
|
362
|
+
}
|
|
363
|
+
function resolvedFilePath(path, rootPath) {
|
|
364
|
+
if (!path || !isAbsolute(path))
|
|
365
|
+
return null;
|
|
366
|
+
const resolved = resolveModulePath(path);
|
|
367
|
+
if (!resolved)
|
|
368
|
+
return null;
|
|
369
|
+
return rootPath && !isWithinDir(resolved, rootPath) ? null : resolved;
|
|
370
|
+
}
|
|
371
|
+
function resolveProjectPackageImport(specifier, configDir) {
|
|
372
|
+
const parsed = packageNameAndSubpath(specifier);
|
|
373
|
+
if (!parsed)
|
|
374
|
+
return null;
|
|
375
|
+
const nodeModulesDir = join2(configDir, "node_modules");
|
|
376
|
+
const directPackageDir = join2(nodeModulesDir, parsed.packageName);
|
|
377
|
+
const packageDir = existsSync2(join2(directPackageDir, "package.json")) ? directPackageDir : resolvePackageDirFromBunStore(parsed.packageName, nodeModulesDir);
|
|
378
|
+
return packageDir ? resolvePackageExportFromDir(packageDir, parsed.subpath) : null;
|
|
379
|
+
}
|
|
380
|
+
function canImportRigWorkspacePackagesDirectly(source, configDir) {
|
|
381
|
+
const packageNames = new Set;
|
|
382
|
+
for (const match of source.matchAll(/(?:from\s*|import\s*\(\s*|import\s*)["'](@rig\/[^"']+)["']/g)) {
|
|
383
|
+
const parsed = packageNameAndSubpath(match[1] ?? "");
|
|
384
|
+
if (parsed?.packageName)
|
|
385
|
+
packageNames.add(parsed.packageName);
|
|
386
|
+
}
|
|
387
|
+
if (packageNames.size === 0)
|
|
388
|
+
return false;
|
|
389
|
+
for (const packageName of packageNames) {
|
|
390
|
+
const shortName = packageName.replace(/^@rig\//, "");
|
|
391
|
+
const manifestPath = join2(configDir, "packages", shortName, "package.json");
|
|
392
|
+
if (!existsSync2(manifestPath))
|
|
393
|
+
return false;
|
|
394
|
+
try {
|
|
395
|
+
const parsed = JSON.parse(readFileSync2(manifestPath, "utf8"));
|
|
396
|
+
if (parsed.name !== packageName)
|
|
397
|
+
return false;
|
|
398
|
+
} catch {
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
async function importConfigViaRuntimeBundleUnserialized(configPath) {
|
|
405
|
+
const bun = globalThis.Bun;
|
|
406
|
+
if (!bun?.build) {
|
|
407
|
+
throw new Error(`Failed to import ${configPath}: bare imports could not be resolved and no Bun.build runtime bundler is available.`);
|
|
408
|
+
}
|
|
409
|
+
const RUNTIME_ONLY_EXTERNAL_PACKAGES = new Set(["effect", "mupdf", "fastembed", "onnxruntime-node", "markit-ai"]);
|
|
410
|
+
const configDir = dirname(configPath);
|
|
411
|
+
const configProjectRoot = basename(configDir) === ".rig" ? dirname(configDir) : configDir;
|
|
412
|
+
const UNRESOLVED_NAMESPACE = "rig-config-unresolved";
|
|
413
|
+
const ABSOLUTE_EXTERNAL_NAMESPACE = "rig-config-absolute-external";
|
|
414
|
+
const unresolvedLocalPlugin = {
|
|
415
|
+
name: "rig-config-unresolved-local",
|
|
416
|
+
setup(build) {
|
|
417
|
+
build.onLoad({ filter: /[\\/]@oh-my-pi[\\/]pi-coding-agent[\\/]src[\\/]export[\\/]html[\\/](?:template\.css|template\.html|template\.js|tool-views\.generated\.js)$/ }, (args) => ({
|
|
418
|
+
loader: "js",
|
|
419
|
+
contents: `export default ${JSON.stringify(readFileSync2(args.path, "utf8"))};
|
|
420
|
+
`
|
|
421
|
+
}));
|
|
422
|
+
build.onLoad({ filter: /\.(?:html|txt)$/ }, (args) => ({
|
|
423
|
+
loader: "js",
|
|
424
|
+
contents: `export default ${JSON.stringify(readFileSync2(args.path, "utf8"))};
|
|
425
|
+
`
|
|
426
|
+
}));
|
|
427
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
428
|
+
const directFilePath = resolvedFilePath(args.path, configDir);
|
|
429
|
+
if (directFilePath)
|
|
430
|
+
return { path: directFilePath };
|
|
431
|
+
if (args.path.startsWith("#")) {
|
|
432
|
+
const packagePrivatePath = resolvePackagePrivateImport(args.path, args.importer, configProjectRoot);
|
|
433
|
+
if (packagePrivatePath)
|
|
434
|
+
return { path: packagePrivatePath };
|
|
435
|
+
try {
|
|
436
|
+
const parent2 = args.importer && isAbsolute(args.importer) ? dirname(args.importer) : configProjectRoot;
|
|
437
|
+
const resolved = bun.resolveSync?.(args.path, parent2);
|
|
438
|
+
const filePath = resolvedFilePath(resolved, configProjectRoot);
|
|
439
|
+
if (filePath)
|
|
440
|
+
return { path: filePath };
|
|
441
|
+
} catch {}
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const packageImport = packageNameAndSubpath(args.path);
|
|
445
|
+
if (packageImport?.packageName === "uhyphen" && packageImport.subpath === ".") {
|
|
446
|
+
const uhyphenPath = resolveProjectPackageImport("uhyphen", configProjectRoot);
|
|
447
|
+
if (uhyphenPath)
|
|
448
|
+
return { path: uhyphenPath };
|
|
449
|
+
}
|
|
450
|
+
if (packageImport && packageImport.packageName !== "uhyphen") {
|
|
451
|
+
try {
|
|
452
|
+
const parent2 = args.importer && isAbsolute(args.importer) ? dirname(args.importer) : configProjectRoot;
|
|
453
|
+
const resolved = bun.resolveSync?.(args.path, parent2);
|
|
454
|
+
const filePath = resolvedFilePath(resolved, configProjectRoot);
|
|
455
|
+
if (filePath)
|
|
456
|
+
return { path: filePath };
|
|
457
|
+
} catch {}
|
|
458
|
+
}
|
|
459
|
+
const projectPackagePath = packageImport ? resolveProjectPackageImport(args.path, configProjectRoot) : null;
|
|
460
|
+
if (projectPackagePath)
|
|
461
|
+
return { path: projectPackagePath };
|
|
462
|
+
if (packageImport && (packageImport.packageName.startsWith("@rig/") || RUNTIME_ONLY_EXTERNAL_PACKAGES.has(packageImport.packageName))) {
|
|
463
|
+
return { path: args.path, external: true };
|
|
464
|
+
}
|
|
465
|
+
if (/^(?:node|bun):/.test(args.path) || isBuiltin(args.path))
|
|
466
|
+
return;
|
|
467
|
+
if (packageImport)
|
|
468
|
+
return { path: args.path, external: true };
|
|
469
|
+
const importerDir = args.importer && isAbsolute(args.importer) ? dirname(args.importer) : null;
|
|
470
|
+
if (args.path.startsWith(".") && importerDir) {
|
|
471
|
+
const fromImporter = resolveModulePath(resolve(importerDir, args.path));
|
|
472
|
+
if (fromImporter)
|
|
473
|
+
return { path: fromImporter };
|
|
474
|
+
if (/[\\/]node_modules[\\/]/.test(args.importer ?? ""))
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const parentCandidates = args.path.startsWith(".") ? [importerDir, configDir].filter((value) => Boolean(value)) : [importerDir ?? configDir];
|
|
478
|
+
for (const parent2 of parentCandidates) {
|
|
479
|
+
const filePath = resolvedFilePath(resolve(parent2, args.path), configProjectRoot);
|
|
480
|
+
if (filePath)
|
|
481
|
+
return { path: filePath };
|
|
482
|
+
}
|
|
483
|
+
const parent = parentCandidates[0] ?? configDir;
|
|
484
|
+
try {
|
|
485
|
+
const resolved = bun.resolveSync?.(args.path, parent) ?? resolve(parent, args.path);
|
|
486
|
+
const filePath = resolvedFilePath(resolved, configProjectRoot);
|
|
487
|
+
if (filePath)
|
|
488
|
+
return { path: filePath };
|
|
489
|
+
} catch {}
|
|
490
|
+
return { path: args.path, namespace: UNRESOLVED_NAMESPACE };
|
|
491
|
+
});
|
|
492
|
+
build.onLoad({ filter: /.*/, namespace: ABSOLUTE_EXTERNAL_NAMESPACE }, (args) => {
|
|
493
|
+
const href = pathToFileURL(args.path).href;
|
|
494
|
+
return {
|
|
495
|
+
loader: "js",
|
|
496
|
+
contents: `export * from ${JSON.stringify(href)};
|
|
497
|
+
const mod = await import(${JSON.stringify(href)});
|
|
498
|
+
export default (mod && "default" in mod) ? mod.default : mod;
|
|
499
|
+
`
|
|
500
|
+
};
|
|
501
|
+
});
|
|
502
|
+
build.onLoad({ filter: /.*/, namespace: UNRESOLVED_NAMESPACE }, (args) => ({
|
|
503
|
+
loader: "js",
|
|
504
|
+
contents: `module.exports = {};
|
|
505
|
+
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.`)});
|
|
506
|
+
`
|
|
507
|
+
}));
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
const result = await bun.build({
|
|
511
|
+
entrypoints: [configPath],
|
|
512
|
+
target: "bun",
|
|
513
|
+
format: "esm",
|
|
514
|
+
throw: false,
|
|
515
|
+
packages: "bundle",
|
|
516
|
+
plugins: [unresolvedLocalPlugin]
|
|
517
|
+
});
|
|
518
|
+
if (!result.success || !result.outputs[0]) {
|
|
519
|
+
const detail = result.logs.map((log) => String(log)).join(`
|
|
520
|
+
`);
|
|
521
|
+
throw new Error(`Failed to bundle ${configPath}: ${detail || "unknown bundler error"}`);
|
|
522
|
+
}
|
|
523
|
+
const bundleParentDir = join2(configProjectRoot, ".rig", "tmp");
|
|
524
|
+
mkdirSync(bundleParentDir, { recursive: true });
|
|
525
|
+
const dir = mkdtempSync(join2(bundleParentDir, "rig-config-bundle-"));
|
|
526
|
+
try {
|
|
527
|
+
const bundledPath = join2(dir, "rig.config.bundled.js");
|
|
528
|
+
await bun.write(bundledPath, await result.outputs[0].text());
|
|
529
|
+
return await import(pathToFileURL(bundledPath).href);
|
|
530
|
+
} finally {
|
|
531
|
+
try {
|
|
532
|
+
rmSync(dir, { recursive: true, force: true });
|
|
533
|
+
} catch {}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
async function loadConfig(cwd) {
|
|
537
|
+
const baked = getEmbeddedProjectConfig();
|
|
538
|
+
if (baked && resolve(baked.projectRoot) === resolve(cwd)) {
|
|
539
|
+
return decodeExplicitPluginConfig(baked.raw);
|
|
540
|
+
}
|
|
541
|
+
for (const name of TS_NAMES) {
|
|
542
|
+
const p = join2(cwd, name);
|
|
543
|
+
if (existsSync2(p)) {
|
|
544
|
+
const mod = await enqueueRuntimeBundle(async () => {
|
|
545
|
+
if (runningFromCompiledBinary()) {
|
|
546
|
+
return importConfigViaRuntimeBundleUnserialized(p);
|
|
547
|
+
}
|
|
548
|
+
const source = readFileSync2(p, "utf8");
|
|
549
|
+
const importsRigHostPackages = /(?:import\s+[^;]*?from\s*|import\s*\()\s*["']@rig\//.test(source);
|
|
550
|
+
if (importsRigHostPackages && !canImportRigWorkspacePackagesDirectly(source, cwd)) {
|
|
551
|
+
return importConfigViaRuntimeBundleUnserialized(p);
|
|
552
|
+
}
|
|
553
|
+
try {
|
|
554
|
+
return await import(pathToFileURL(p).href);
|
|
555
|
+
} catch (error) {
|
|
556
|
+
if (!isModuleResolutionError(error))
|
|
557
|
+
throw error;
|
|
558
|
+
return importConfigViaRuntimeBundleUnserialized(p);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
const raw = mod.default ?? mod.config;
|
|
562
|
+
return decodeExplicitPluginConfig(raw);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const declarativePath = findDeclarativeConfigPath(cwd);
|
|
566
|
+
if (declarativePath) {
|
|
567
|
+
return loadDeclarativeConfig(declarativePath);
|
|
568
|
+
}
|
|
569
|
+
for (const name of JSON_NAMES) {
|
|
570
|
+
const p = join2(cwd, name);
|
|
571
|
+
if (existsSync2(p)) {
|
|
572
|
+
const raw = JSON.parse(readFileSync2(p, "utf-8"));
|
|
573
|
+
return decodeExplicitPluginConfig(raw);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
throw new Error(`no .rig/rig.config.{ts,mts,json} or .rig/rigfig.{toml,json} found in ${cwd}`);
|
|
577
|
+
}
|
|
578
|
+
function decodeExplicitPluginConfig(raw) {
|
|
579
|
+
const withDefaults = applyConfigDefaults(raw);
|
|
580
|
+
const explicitPlugins = Array.isArray(withDefaults.plugins) ? [...withDefaults.plugins] : [];
|
|
581
|
+
const decoded = Schema3.decodeUnknownSync(RigConfig3)({
|
|
582
|
+
...withDefaults,
|
|
583
|
+
plugins: explicitPlugins
|
|
584
|
+
});
|
|
585
|
+
return { ...decoded, plugins: explicitPlugins };
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// packages/core/src/kernel-entrypoint.ts
|
|
589
|
+
function unquoteDotenvValue(value) {
|
|
590
|
+
const trimmed = value.trim();
|
|
591
|
+
if (trimmed.length >= 2 && (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
592
|
+
return trimmed.slice(1, -1);
|
|
593
|
+
}
|
|
594
|
+
return trimmed;
|
|
595
|
+
}
|
|
596
|
+
function hydrateDotenv(path, env) {
|
|
597
|
+
if (!existsSync3(path))
|
|
598
|
+
return false;
|
|
599
|
+
const source = readFileSync3(path, "utf8");
|
|
600
|
+
for (const rawLine of source.split(/\r?\n/)) {
|
|
601
|
+
const line = rawLine.trim();
|
|
602
|
+
if (!line || line.startsWith("#"))
|
|
603
|
+
continue;
|
|
604
|
+
const assignment = line.startsWith("export ") ? line.slice("export ".length).trim() : line;
|
|
605
|
+
const equals = assignment.indexOf("=");
|
|
606
|
+
if (equals <= 0)
|
|
607
|
+
continue;
|
|
608
|
+
const key = assignment.slice(0, equals).trim();
|
|
609
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key) || env[key])
|
|
610
|
+
continue;
|
|
611
|
+
env[key] = unquoteDotenvValue(assignment.slice(equals + 1));
|
|
612
|
+
}
|
|
613
|
+
return true;
|
|
614
|
+
}
|
|
615
|
+
function createPlacementKernelTransportPlugin(transport) {
|
|
616
|
+
return createPlacementTransportPlugin(transport);
|
|
617
|
+
}
|
|
618
|
+
async function hydrateProjectProcessEnv(projectRoot, options = {}) {
|
|
619
|
+
const normalizedRoot = resolve2(projectRoot);
|
|
620
|
+
const env = options.env ?? process.env;
|
|
621
|
+
const errors = [];
|
|
622
|
+
let dotenvLoaded = false;
|
|
623
|
+
let configLoaded = false;
|
|
624
|
+
try {
|
|
625
|
+
dotenvLoaded = hydrateDotenv(resolve2(normalizedRoot, options.dotenvPath ?? ".env"), env);
|
|
626
|
+
} catch (error) {
|
|
627
|
+
errors.push(`dotenv: ${error instanceof Error ? error.message : String(error)}`);
|
|
628
|
+
}
|
|
629
|
+
try {
|
|
630
|
+
applyConfigEnv(await loadConfig(normalizedRoot), env);
|
|
631
|
+
configLoaded = true;
|
|
632
|
+
} catch (error) {
|
|
633
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
634
|
+
if (!message.includes("no rig.config")) {
|
|
635
|
+
errors.push(`config: ${message}`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return { projectRoot: normalizedRoot, dotenvLoaded, configLoaded, errors };
|
|
639
|
+
}
|
|
640
|
+
async function adopt(root, options = {}) {
|
|
641
|
+
if (options.hydrateEnv === true && root !== undefined) {
|
|
642
|
+
await hydrateProjectProcessEnv(root, options);
|
|
643
|
+
}
|
|
644
|
+
const bootInput = {
|
|
645
|
+
...options.entrypoint !== undefined ? { entrypoint: options.entrypoint } : {},
|
|
646
|
+
...options.journal !== undefined ? { journal: options.journal } : {}
|
|
647
|
+
};
|
|
648
|
+
if (options.transport === undefined) {
|
|
649
|
+
return await bootDefaultKernelIntoProcess(bootInput);
|
|
650
|
+
}
|
|
651
|
+
return await bootDefaultKernelIntoProcess({
|
|
652
|
+
...bootInput,
|
|
653
|
+
extraPlugins: [createPlacementKernelTransportPlugin(options.transport)]
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
export {
|
|
657
|
+
hydrateProjectProcessEnv,
|
|
658
|
+
createPlacementKernelTransportPlugin,
|
|
659
|
+
adopt
|
|
660
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { CapabilityProviderPlugin } from "@rig/kernel-seed/plugin-abi";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RIG_DEFINITION_DIRNAME, RIG_STATE_DIRNAME, type RigLayout, type RuntimeWorkspaceLayout } from "@rig/contracts";
|
|
2
|
+
import { resolveCheckoutRoot } from "./checkout-root";
|
|
3
|
+
declare const resolveMonorepoRoot: typeof resolveCheckoutRoot;
|
|
4
|
+
export { resolveMonorepoRoot };
|
|
5
|
+
export type { RigLayout, RuntimeWorkspaceLayout };
|
|
6
|
+
export { RIG_DEFINITION_DIRNAME, RIG_STATE_DIRNAME };
|
|
7
|
+
export declare function resolveNearestRigProjectRoot(startDir: string): string;
|
|
8
|
+
export declare function resolveRigDataRoot(projectRoot: string): string;
|
|
9
|
+
export declare function resolveRuntimeWorkspaceLayout(workspaceDir: string): RuntimeWorkspaceLayout;
|
|
10
|
+
export declare function resolveRigLayout(projectRoot: string): RigLayout;
|