@h-rig/core 0.0.6-alpha.18 → 0.0.6-alpha.180
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,432 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/core/src/hook-runtime.ts
|
|
3
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, realpathSync, writeSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import { RIG_DEFINITION_DIRNAME, RIG_STATE_DIRNAME } from "@rig/contracts";
|
|
6
|
+
function bunRuntime() {
|
|
7
|
+
const runtime = globalThis;
|
|
8
|
+
return runtime.Bun;
|
|
9
|
+
}
|
|
10
|
+
function normalizeBuildConfig(value) {
|
|
11
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string"));
|
|
15
|
+
}
|
|
16
|
+
function readBuildConfigForCoreHooks() {
|
|
17
|
+
if (typeof __RIG_BUILD_CONFIG__ !== "undefined") {
|
|
18
|
+
return normalizeBuildConfig(__RIG_BUILD_CONFIG__);
|
|
19
|
+
}
|
|
20
|
+
const raw = process.env.RIG_BUILD_CONFIG_JSON?.trim();
|
|
21
|
+
if (!raw) {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
return normalizeBuildConfig(JSON.parse(raw));
|
|
26
|
+
} catch {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
var BUILD_CONFIG = readBuildConfigForCoreHooks();
|
|
31
|
+
var BAKED_PROJECT_ROOT = BUILD_CONFIG.AGENT_PROJECT_ROOT ?? "";
|
|
32
|
+
var BAKED_TASK_ID = BUILD_CONFIG.AGENT_TASK_ID ?? "";
|
|
33
|
+
var BAKED_STATE_DIR = BUILD_CONFIG.AGENT_STATE_DIR ?? "";
|
|
34
|
+
var BAKED_BUN_PATH = BUILD_CONFIG.AGENT_BUN_PATH ?? "";
|
|
35
|
+
var BAKED_TASK_CONFIG = BUILD_CONFIG.AGENT_TASK_CONFIG ?? "";
|
|
36
|
+
var BAKED_POLICY_CONTENT = BUILD_CONFIG.AGENT_POLICY_CONTENT ?? "";
|
|
37
|
+
var BAKED_TASK_SCOPES = BUILD_CONFIG.AGENT_TASK_SCOPES ?? "";
|
|
38
|
+
var RUNTIME_CONTEXT_ENV = "RIG_RUNTIME_CONTEXT_FILE";
|
|
39
|
+
function isPlainObject(value) {
|
|
40
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
function isStringArray(value) {
|
|
43
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
44
|
+
}
|
|
45
|
+
function loadHookContextFromEnv() {
|
|
46
|
+
const contextFile = process.env[RUNTIME_CONTEXT_ENV]?.trim();
|
|
47
|
+
let filePath = contextFile || "";
|
|
48
|
+
if (!filePath) {
|
|
49
|
+
let current = resolve(process.cwd());
|
|
50
|
+
while (true) {
|
|
51
|
+
const candidate = resolve(current, "runtime-context.json");
|
|
52
|
+
if (existsSync(candidate)) {
|
|
53
|
+
filePath = candidate;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
const parent = resolve(current, "..");
|
|
57
|
+
if (parent === current) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
current = parent;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!filePath || !existsSync(filePath)) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const raw = JSON.parse(readFileSync(filePath, "utf-8"));
|
|
68
|
+
if (!isPlainObject(raw)) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const taskId = typeof raw.taskId === "string" ? raw.taskId : "";
|
|
72
|
+
const role = typeof raw.role === "string" ? raw.role : "";
|
|
73
|
+
const scopes = isStringArray(raw.scopes) ? raw.scopes : [];
|
|
74
|
+
const validation = isStringArray(raw.validation) ? raw.validation : [];
|
|
75
|
+
const hostProjectRoot = typeof raw.hostProjectRoot === "string" ? raw.hostProjectRoot : undefined;
|
|
76
|
+
const monorepoMainRoot = typeof raw.monorepoMainRoot === "string" ? raw.monorepoMainRoot : undefined;
|
|
77
|
+
const stateDir = typeof raw.stateDir === "string" ? raw.stateDir : "";
|
|
78
|
+
const policyFile = typeof raw.policyFile === "string" ? raw.policyFile : "";
|
|
79
|
+
if (!taskId || !stateDir) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
taskId,
|
|
84
|
+
role,
|
|
85
|
+
scopes,
|
|
86
|
+
validation,
|
|
87
|
+
hostProjectRoot,
|
|
88
|
+
monorepoMainRoot,
|
|
89
|
+
stateDir,
|
|
90
|
+
policyFile
|
|
91
|
+
};
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
var cachedContext;
|
|
97
|
+
function getContext() {
|
|
98
|
+
if (cachedContext !== undefined) {
|
|
99
|
+
return cachedContext;
|
|
100
|
+
}
|
|
101
|
+
cachedContext = loadHookContextFromEnv();
|
|
102
|
+
return cachedContext;
|
|
103
|
+
}
|
|
104
|
+
function resolveProjectRoot() {
|
|
105
|
+
const ctx = getContext();
|
|
106
|
+
if (ctx?.hostProjectRoot)
|
|
107
|
+
return ctx.hostProjectRoot;
|
|
108
|
+
if (BAKED_PROJECT_ROOT) {
|
|
109
|
+
return BAKED_PROJECT_ROOT;
|
|
110
|
+
}
|
|
111
|
+
if (process.env.PROJECT_RIG_ROOT) {
|
|
112
|
+
return process.env.PROJECT_RIG_ROOT;
|
|
113
|
+
}
|
|
114
|
+
const candidates = [process.cwd(), resolve(import.meta.dirname, "../..")];
|
|
115
|
+
for (const candidate of candidates) {
|
|
116
|
+
if (existsSync(resolve(candidate, RIG_DEFINITION_DIRNAME)) || existsSync(resolve(candidate, RIG_STATE_DIRNAME))) {
|
|
117
|
+
return candidate;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return resolve(import.meta.dirname, "../..");
|
|
121
|
+
}
|
|
122
|
+
function resolveTaskIdForHook(_projectRoot) {
|
|
123
|
+
const ctx = getContext();
|
|
124
|
+
if (ctx)
|
|
125
|
+
return ctx.taskId;
|
|
126
|
+
if (BAKED_TASK_ID) {
|
|
127
|
+
return BAKED_TASK_ID;
|
|
128
|
+
}
|
|
129
|
+
const fromEnv = (process.env.RIG_TASK_ID || "").trim();
|
|
130
|
+
if (fromEnv) {
|
|
131
|
+
return fromEnv;
|
|
132
|
+
}
|
|
133
|
+
const runtimeId = (process.env.RIG_TASK_RUNTIME_ID || "").trim();
|
|
134
|
+
if (runtimeId.startsWith("task-") && runtimeId.length > "task-".length) {
|
|
135
|
+
return runtimeId.slice("task-".length);
|
|
136
|
+
}
|
|
137
|
+
return "";
|
|
138
|
+
}
|
|
139
|
+
function isTaskConfigEntry(value) {
|
|
140
|
+
if (!isPlainObject(value)) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
if ("role" in value && value.role !== undefined && typeof value.role !== "string") {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
if ("scope" in value && value.scope !== undefined && !isStringArray(value.scope)) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
if ("validation" in value && value.validation !== undefined && !isStringArray(value.validation)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
async function resolveTaskConfig(_projectRoot, _taskId) {
|
|
155
|
+
const ctx = getContext();
|
|
156
|
+
if (ctx) {
|
|
157
|
+
return {
|
|
158
|
+
scope: ctx.scopes,
|
|
159
|
+
validation: ctx.validation,
|
|
160
|
+
role: ctx.role
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (BAKED_TASK_CONFIG) {
|
|
164
|
+
try {
|
|
165
|
+
const parsed = JSON.parse(BAKED_TASK_CONFIG);
|
|
166
|
+
if (isTaskConfigEntry(parsed) && Object.keys(parsed).length > 0) {
|
|
167
|
+
return parsed;
|
|
168
|
+
}
|
|
169
|
+
} catch {}
|
|
170
|
+
}
|
|
171
|
+
return {};
|
|
172
|
+
}
|
|
173
|
+
async function resolveTaskScopes(projectRoot, taskId) {
|
|
174
|
+
const ctx = getContext();
|
|
175
|
+
if (ctx && ctx.scopes.length > 0)
|
|
176
|
+
return ctx.scopes;
|
|
177
|
+
if (BAKED_TASK_SCOPES) {
|
|
178
|
+
try {
|
|
179
|
+
const parsed = JSON.parse(BAKED_TASK_SCOPES);
|
|
180
|
+
if (Array.isArray(parsed) && parsed.length > 0 && parsed.every((item) => typeof item === "string")) {
|
|
181
|
+
return parsed;
|
|
182
|
+
}
|
|
183
|
+
} catch {}
|
|
184
|
+
}
|
|
185
|
+
return (await resolveTaskConfig(projectRoot, taskId)).scope || [];
|
|
186
|
+
}
|
|
187
|
+
function resolvePolicyContent(projectRoot) {
|
|
188
|
+
const ctx = getContext();
|
|
189
|
+
if (ctx?.policyFile && existsSync(ctx.policyFile)) {
|
|
190
|
+
return readFileSync(ctx.policyFile, "utf-8");
|
|
191
|
+
}
|
|
192
|
+
if (BAKED_POLICY_CONTENT)
|
|
193
|
+
return BAKED_POLICY_CONTENT;
|
|
194
|
+
const policyPath = resolve(projectRoot, "rig/policy/policy.json");
|
|
195
|
+
if (existsSync(policyPath))
|
|
196
|
+
return readFileSync(policyPath, "utf-8");
|
|
197
|
+
return "{}";
|
|
198
|
+
}
|
|
199
|
+
async function readHookInput() {
|
|
200
|
+
let text = "";
|
|
201
|
+
const inputFile = process.env.RIG_HOOK_INPUT_FILE?.trim();
|
|
202
|
+
if (inputFile) {
|
|
203
|
+
text = readFileSync(inputFile, "utf-8");
|
|
204
|
+
} else {
|
|
205
|
+
try {
|
|
206
|
+
text = readFileSync("/dev/stdin", "utf-8");
|
|
207
|
+
} catch {
|
|
208
|
+
text = readFileSync(0, "utf-8");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (!text.trim()) {
|
|
212
|
+
return { input: {}, valid: true, hadPayload: false };
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
return {
|
|
216
|
+
input: JSON.parse(text),
|
|
217
|
+
valid: true,
|
|
218
|
+
hadPayload: true
|
|
219
|
+
};
|
|
220
|
+
} catch {
|
|
221
|
+
return { input: {}, valid: false, hadPayload: true };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function extractToolFilePaths(toolName, input) {
|
|
225
|
+
const paths = [];
|
|
226
|
+
const add = (value) => {
|
|
227
|
+
if (typeof value === "string" && value.trim()) {
|
|
228
|
+
paths.push(value.trim());
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
if (toolName === "Read" || toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit") {
|
|
232
|
+
add(input.file_path);
|
|
233
|
+
add(input.path);
|
|
234
|
+
} else if (toolName === "Glob") {
|
|
235
|
+
add(input.path);
|
|
236
|
+
} else if (toolName === "Grep") {
|
|
237
|
+
add(input.path);
|
|
238
|
+
} else {
|
|
239
|
+
add(input.file_path);
|
|
240
|
+
add(input.path);
|
|
241
|
+
}
|
|
242
|
+
return paths;
|
|
243
|
+
}
|
|
244
|
+
function isTestFilePath(path) {
|
|
245
|
+
return /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(path) || /\/(__tests__|tests|test)\//.test(path);
|
|
246
|
+
}
|
|
247
|
+
function buildPluginHookContext(parsed, opts) {
|
|
248
|
+
const toolName = parsed.input.tool_name;
|
|
249
|
+
const toolInput = parsed.input.tool_input ?? {};
|
|
250
|
+
return {
|
|
251
|
+
event: opts.event,
|
|
252
|
+
toolName,
|
|
253
|
+
toolInput,
|
|
254
|
+
filePaths: toolName ? extractToolFilePaths(toolName, toolInput) : [],
|
|
255
|
+
projectRoot: opts.projectRoot,
|
|
256
|
+
taskId: opts.taskId
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function hookResultToProtocol(result) {
|
|
260
|
+
if (result.decision === "block") {
|
|
261
|
+
const lines = [`BLOCKED: ${result.reason ?? "blocked by plugin hook"}`];
|
|
262
|
+
if (result.systemMessage) {
|
|
263
|
+
lines.push(result.systemMessage);
|
|
264
|
+
}
|
|
265
|
+
return { exitCode: 1, stdout: `${lines.join(`
|
|
266
|
+
`)}
|
|
267
|
+
` };
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
exitCode: 0,
|
|
271
|
+
stdout: result.systemMessage ? `${result.systemMessage}
|
|
272
|
+
` : ""
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function escapeRegExp(value) {
|
|
276
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
277
|
+
}
|
|
278
|
+
function block(hookName, message, projectRoot) {
|
|
279
|
+
const runtimeWorkspace = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
280
|
+
const stateDir = BAKED_STATE_DIR || (runtimeWorkspace ? resolve(runtimeWorkspace, ".rig", "state") : resolve(projectRoot, ".rig", "state"));
|
|
281
|
+
mkdirSync(stateDir, { recursive: true });
|
|
282
|
+
const tripLog = resolve(stateDir, "hook_trips.log");
|
|
283
|
+
let count = 0;
|
|
284
|
+
if (existsSync(tripLog)) {
|
|
285
|
+
const content = readFileSync(tripLog, "utf-8");
|
|
286
|
+
count = (content.match(new RegExp(`^${escapeRegExp(hookName)}\\s`, "gm")) || []).length;
|
|
287
|
+
}
|
|
288
|
+
appendFileSync(tripLog, `${hookName} ${new Date().toISOString()}
|
|
289
|
+
`, "utf-8");
|
|
290
|
+
count += 1;
|
|
291
|
+
const lines = [
|
|
292
|
+
`BLOCKED: ${message}`,
|
|
293
|
+
"",
|
|
294
|
+
"Re-read CLAUDE.md safety rules before your next action."
|
|
295
|
+
];
|
|
296
|
+
if (count >= 3) {
|
|
297
|
+
lines.push("");
|
|
298
|
+
lines.push(`REPEATED VIOLATION (${count}x ${hookName}).`);
|
|
299
|
+
lines.push("STOP. Read the FULL CLAUDE.md from top to bottom.");
|
|
300
|
+
lines.push("If stuck, record the failed approach in the task-state failure log and request help.");
|
|
301
|
+
}
|
|
302
|
+
writeSync(1, `${lines.join(`
|
|
303
|
+
`)}
|
|
304
|
+
`);
|
|
305
|
+
process.exit(1);
|
|
306
|
+
}
|
|
307
|
+
function normalizeExecutablePath(candidate) {
|
|
308
|
+
if (!candidate) {
|
|
309
|
+
return "";
|
|
310
|
+
}
|
|
311
|
+
const normalized = resolve(candidate);
|
|
312
|
+
if (!existsSync(normalized)) {
|
|
313
|
+
return "";
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
return realpathSync(normalized);
|
|
317
|
+
} catch {
|
|
318
|
+
return normalized;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function looksLikeRuntimeGateway(candidate) {
|
|
322
|
+
const normalized = resolve(candidate).replace(/\\/g, "/");
|
|
323
|
+
return normalized.includes("/.rig/bin/") || normalized.endsWith("/rig-shell") || normalized.endsWith("/rig-agent");
|
|
324
|
+
}
|
|
325
|
+
function resolveBunBinaryPath() {
|
|
326
|
+
const explicit = normalizeExecutablePath(process.env.RIG_BUN_PATH?.trim());
|
|
327
|
+
if (explicit) {
|
|
328
|
+
return explicit;
|
|
329
|
+
}
|
|
330
|
+
const bunWhich = bunRuntime()?.which?.("bun");
|
|
331
|
+
const pathBun = normalizeExecutablePath(bunWhich?.trim());
|
|
332
|
+
if (pathBun && !looksLikeRuntimeGateway(pathBun)) {
|
|
333
|
+
return pathBun;
|
|
334
|
+
}
|
|
335
|
+
const home = process.env.HOME?.trim();
|
|
336
|
+
const fallbackCandidates = [
|
|
337
|
+
home ? resolve(home, ".bun/bin/bun") : "",
|
|
338
|
+
"/opt/homebrew/bin/bun",
|
|
339
|
+
"/usr/local/bin/bun",
|
|
340
|
+
"/usr/bin/bun"
|
|
341
|
+
];
|
|
342
|
+
for (const candidate of fallbackCandidates) {
|
|
343
|
+
const normalized = normalizeExecutablePath(candidate);
|
|
344
|
+
if (normalized) {
|
|
345
|
+
return normalized;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const execPath = normalizeExecutablePath(process.execPath?.trim());
|
|
349
|
+
if (execPath && !looksLikeRuntimeGateway(execPath)) {
|
|
350
|
+
return execPath;
|
|
351
|
+
}
|
|
352
|
+
throw new Error("bun not found in PATH");
|
|
353
|
+
}
|
|
354
|
+
function resolveBunCli() {
|
|
355
|
+
return resolveBunCliInvocation().command;
|
|
356
|
+
}
|
|
357
|
+
function resolveBunCliInvocation() {
|
|
358
|
+
if (BAKED_BUN_PATH) {
|
|
359
|
+
return {
|
|
360
|
+
command: BAKED_BUN_PATH,
|
|
361
|
+
env: {}
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
if (process.env.RIG_BUN_PATH?.trim()) {
|
|
365
|
+
return {
|
|
366
|
+
command: process.env.RIG_BUN_PATH.trim(),
|
|
367
|
+
env: {}
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
const systemBun = resolveBunBinaryPath();
|
|
372
|
+
return {
|
|
373
|
+
command: systemBun,
|
|
374
|
+
env: {}
|
|
375
|
+
};
|
|
376
|
+
} catch {}
|
|
377
|
+
if (process.execPath?.trim()) {
|
|
378
|
+
return {
|
|
379
|
+
command: process.execPath,
|
|
380
|
+
env: { BUN_BE_BUN: "1" }
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
return { command: "bun", env: {} };
|
|
384
|
+
}
|
|
385
|
+
async function runTypedHook(fn, opts) {
|
|
386
|
+
const parsed = await readHookInput();
|
|
387
|
+
const projectRoot = opts.projectRoot ?? resolveProjectRoot();
|
|
388
|
+
const taskId = opts.taskId ?? resolveTaskIdForHook(projectRoot);
|
|
389
|
+
const ctx = buildPluginHookContext(parsed, {
|
|
390
|
+
event: opts.event,
|
|
391
|
+
projectRoot,
|
|
392
|
+
taskId
|
|
393
|
+
});
|
|
394
|
+
let result;
|
|
395
|
+
try {
|
|
396
|
+
result = await fn(ctx);
|
|
397
|
+
} catch (err) {
|
|
398
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
399
|
+
writeSync(2, `[rig hook] typed hook threw: ${message}
|
|
400
|
+
`);
|
|
401
|
+
process.exit(0);
|
|
402
|
+
}
|
|
403
|
+
const { exitCode, stdout } = hookResultToProtocol(result);
|
|
404
|
+
if (stdout) {
|
|
405
|
+
writeSync(1, stdout);
|
|
406
|
+
}
|
|
407
|
+
process.exit(exitCode);
|
|
408
|
+
}
|
|
409
|
+
export {
|
|
410
|
+
runTypedHook,
|
|
411
|
+
resolveTaskScopes,
|
|
412
|
+
resolveTaskIdForHook,
|
|
413
|
+
resolveTaskConfig,
|
|
414
|
+
resolveProjectRoot,
|
|
415
|
+
resolvePolicyContent,
|
|
416
|
+
resolveBunCliInvocation,
|
|
417
|
+
resolveBunCli,
|
|
418
|
+
readHookInput,
|
|
419
|
+
isTestFilePath,
|
|
420
|
+
hookResultToProtocol,
|
|
421
|
+
extractToolFilePaths,
|
|
422
|
+
escapeRegExp,
|
|
423
|
+
buildPluginHookContext,
|
|
424
|
+
block,
|
|
425
|
+
BAKED_TASK_SCOPES,
|
|
426
|
+
BAKED_TASK_ID,
|
|
427
|
+
BAKED_TASK_CONFIG,
|
|
428
|
+
BAKED_STATE_DIR,
|
|
429
|
+
BAKED_PROJECT_ROOT,
|
|
430
|
+
BAKED_POLICY_CONTENT,
|
|
431
|
+
BAKED_BUN_PATH
|
|
432
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* hook-runner.ts — entrypoint for typed plugin hooks.
|
|
3
|
+
*
|
|
4
|
+
* The provider-owned session-hook materializer writes a shim command into
|
|
5
|
+
* the active session host for every plugin hook that ships a typed
|
|
6
|
+
* implementation instead of a raw command string:
|
|
7
|
+
*
|
|
8
|
+
* <bun> <this file> --plugin <name> --hook <id> [--event <event>] [--project-root <dir>]
|
|
9
|
+
*
|
|
10
|
+
* When Claude Code fires the hook, this process:
|
|
11
|
+
* 1. loads the project's rig config via @rig/core's loadConfig
|
|
12
|
+
* (which preserves each explicit plugin object),
|
|
13
|
+
* 2. finds the typed implementation `hooks[<id>]` on the named
|
|
14
|
+
* plugin,
|
|
15
|
+
* 3. hands it to the local typed-hook protocol bridge, which parses the hook
|
|
16
|
+
* stdin payload into a HookContext, calls the function, and serializes the
|
|
17
|
+
* HookResult to the Claude Code stdout/exit-code protocol.
|
|
18
|
+
*
|
|
19
|
+
* Failure policy: configuration problems (missing config, missing plugin,
|
|
20
|
+
* missing implementation) warn on stderr and exit 0. A misconfigured hook
|
|
21
|
+
* must never block every tool call; blocking is reserved for an explicit
|
|
22
|
+
* `{ decision: "block" }` from a resolved implementation.
|
|
23
|
+
*/
|
|
24
|
+
import type { HookImplementation, HookRegistration } from "@rig/contracts";
|
|
25
|
+
import type { RigPlugin } from "./plugin-runtime";
|
|
26
|
+
export interface HookRunnerArgs {
|
|
27
|
+
plugin?: string;
|
|
28
|
+
hook?: string;
|
|
29
|
+
event?: string;
|
|
30
|
+
projectRoot?: string;
|
|
31
|
+
}
|
|
32
|
+
/** Parse the shim's argv (everything after the script path). */
|
|
33
|
+
export declare function parseHookRunnerArgs(argv: readonly string[]): HookRunnerArgs;
|
|
34
|
+
export interface ResolvedTypedHook {
|
|
35
|
+
implementation: HookImplementation;
|
|
36
|
+
registration: HookRegistration;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Find the typed implementation + metadata registration for a hook id on a
|
|
40
|
+
* named plugin. Returns null (with a reason) instead of throwing so the
|
|
41
|
+
* caller can apply the fail-open policy uniformly.
|
|
42
|
+
*/
|
|
43
|
+
export declare function findTypedHook(plugins: readonly RigPlugin[], pluginName: string, hookId: string): {
|
|
44
|
+
hook: ResolvedTypedHook | null;
|
|
45
|
+
reason?: string;
|
|
46
|
+
};
|
|
47
|
+
/** Run the typed hook described by argv. Exits the process; never returns. */
|
|
48
|
+
export declare function main(argv: readonly string[]): Promise<never>;
|