@h-rig/runtime 0.0.6-alpha.0

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 (176) hide show
  1. package/README.md +27 -0
  2. package/dist/bin/rig-agent-dispatch.js +9615 -0
  3. package/dist/bin/rig-agent.js +9512 -0
  4. package/dist/bin/rig-browser-tool.js +269 -0
  5. package/dist/src/agent-mode.js +48 -0
  6. package/dist/src/baked-secrets.js +121 -0
  7. package/dist/src/binary-build-worker.js +312 -0
  8. package/dist/src/binary-run.js +540 -0
  9. package/dist/src/boundaries.js +1 -0
  10. package/dist/src/build-time-config.js +25 -0
  11. package/dist/src/control-plane/agent-roles.js +27 -0
  12. package/dist/src/control-plane/agent-wrapper.js +9621 -0
  13. package/dist/src/control-plane/authority-files.js +582 -0
  14. package/dist/src/control-plane/browser-contract.js +135 -0
  15. package/dist/src/control-plane/controlled-bash.js +1111 -0
  16. package/dist/src/control-plane/errors.js +13 -0
  17. package/dist/src/control-plane/harness-main.js +10828 -0
  18. package/dist/src/control-plane/hook-materializer.js +75 -0
  19. package/dist/src/control-plane/hooks/audit-trail.js +353 -0
  20. package/dist/src/control-plane/hooks/completion-verification.js +7552 -0
  21. package/dist/src/control-plane/hooks/import-guard.js +890 -0
  22. package/dist/src/control-plane/hooks/inject-context.js +4189 -0
  23. package/dist/src/control-plane/hooks/post-edit-lint.js +43 -0
  24. package/dist/src/control-plane/hooks/safety-guard.js +910 -0
  25. package/dist/src/control-plane/hooks/scope-guard.js +907 -0
  26. package/dist/src/control-plane/hooks/shared.js +44 -0
  27. package/dist/src/control-plane/hooks/submodule-branch.js +7797 -0
  28. package/dist/src/control-plane/hooks/task-runtime-start.js +7799 -0
  29. package/dist/src/control-plane/hooks/test-integrity-guard.js +891 -0
  30. package/dist/src/control-plane/materialize-task-config.js +453 -0
  31. package/dist/src/control-plane/memory-sync/cli.js +2019 -0
  32. package/dist/src/control-plane/memory-sync/db.js +753 -0
  33. package/dist/src/control-plane/memory-sync/embed.js +281 -0
  34. package/dist/src/control-plane/memory-sync/index.js +2049 -0
  35. package/dist/src/control-plane/memory-sync/query.js +294 -0
  36. package/dist/src/control-plane/memory-sync/read.js +784 -0
  37. package/dist/src/control-plane/memory-sync/types.js +6 -0
  38. package/dist/src/control-plane/memory-sync/write.js +1547 -0
  39. package/dist/src/control-plane/native/git-native.js +490 -0
  40. package/dist/src/control-plane/native/git-ops.js +2860 -0
  41. package/dist/src/control-plane/native/harness-cli.js +9721 -0
  42. package/dist/src/control-plane/native/pr-automation.js +373 -0
  43. package/dist/src/control-plane/native/profile-ops.js +481 -0
  44. package/dist/src/control-plane/native/repo-ops.js +2342 -0
  45. package/dist/src/control-plane/native/root-resolver.js +66 -0
  46. package/dist/src/control-plane/native/run-ops.js +3281 -0
  47. package/dist/src/control-plane/native/runtime-native-sidecar.js +299 -0
  48. package/dist/src/control-plane/native/runtime-native.js +392 -0
  49. package/dist/src/control-plane/native/scope-rules.js +17 -0
  50. package/dist/src/control-plane/native/task-ops.js +6320 -0
  51. package/dist/src/control-plane/native/task-state.js +1512 -0
  52. package/dist/src/control-plane/native/utils.js +535 -0
  53. package/dist/src/control-plane/native/validator-binaries.js +889 -0
  54. package/dist/src/control-plane/native/validator.js +2197 -0
  55. package/dist/src/control-plane/native/verifier.js +3249 -0
  56. package/dist/src/control-plane/native/workspace-ops.js +1635 -0
  57. package/dist/src/control-plane/plugin-host-context.js +334 -0
  58. package/dist/src/control-plane/project-main-pre-run-sync.js +630 -0
  59. package/dist/src/control-plane/provider/claude-stream-records.js +158 -0
  60. package/dist/src/control-plane/provider/codex-app-server.js +885 -0
  61. package/dist/src/control-plane/provider/codex-exec-records.js +203 -0
  62. package/dist/src/control-plane/provider/rig-task-run-skill.js +39 -0
  63. package/dist/src/control-plane/provider/runtime-instructions.js +96 -0
  64. package/dist/src/control-plane/remote.js +854 -0
  65. package/dist/src/control-plane/repos/index.js +473 -0
  66. package/dist/src/control-plane/repos/layout.js +124 -0
  67. package/dist/src/control-plane/repos/mirror/bootstrap.js +268 -0
  68. package/dist/src/control-plane/repos/mirror/refresh.js +398 -0
  69. package/dist/src/control-plane/repos/mirror/state.js +167 -0
  70. package/dist/src/control-plane/repos/registry.js +77 -0
  71. package/dist/src/control-plane/repos/types.js +1 -0
  72. package/dist/src/control-plane/runtime/agent-mode.js +48 -0
  73. package/dist/src/control-plane/runtime/baked-secrets.js +120 -0
  74. package/dist/src/control-plane/runtime/claude-tool-router-binary.js +343 -0
  75. package/dist/src/control-plane/runtime/claude-tool-router.js +520 -0
  76. package/dist/src/control-plane/runtime/context.js +216 -0
  77. package/dist/src/control-plane/runtime/events.js +218 -0
  78. package/dist/src/control-plane/runtime/guard-types.js +6 -0
  79. package/dist/src/control-plane/runtime/guard.js +880 -0
  80. package/dist/src/control-plane/runtime/image/fingerprint-sidecar.js +1194 -0
  81. package/dist/src/control-plane/runtime/image/index.js +2255 -0
  82. package/dist/src/control-plane/runtime/image-fingerprint-sidecar.js +1191 -0
  83. package/dist/src/control-plane/runtime/image.js +2255 -0
  84. package/dist/src/control-plane/runtime/index.js +8511 -0
  85. package/dist/src/control-plane/runtime/isolation/discovery.js +599 -0
  86. package/dist/src/control-plane/runtime/isolation/home.js +1217 -0
  87. package/dist/src/control-plane/runtime/isolation/index.js +8193 -0
  88. package/dist/src/control-plane/runtime/isolation/runner.js +2651 -0
  89. package/dist/src/control-plane/runtime/isolation/shared.js +501 -0
  90. package/dist/src/control-plane/runtime/isolation/toolchain.js +1892 -0
  91. package/dist/src/control-plane/runtime/isolation/types.js +1 -0
  92. package/dist/src/control-plane/runtime/isolation/worktree.js +509 -0
  93. package/dist/src/control-plane/runtime/isolation.js +8193 -0
  94. package/dist/src/control-plane/runtime/overlay.js +67 -0
  95. package/dist/src/control-plane/runtime/plugin-mode.js +41 -0
  96. package/dist/src/control-plane/runtime/plugins.js +1131 -0
  97. package/dist/src/control-plane/runtime/provisioning-env.js +220 -0
  98. package/dist/src/control-plane/runtime/queue.js +8358 -0
  99. package/dist/src/control-plane/runtime/rig-shell.js +205 -0
  100. package/dist/src/control-plane/runtime/rig-tools.js +182 -0
  101. package/dist/src/control-plane/runtime/runner-context.js +1 -0
  102. package/dist/src/control-plane/runtime/runtime-paths.js +184 -0
  103. package/dist/src/control-plane/runtime/sandbox/backend-bwrap.js +311 -0
  104. package/dist/src/control-plane/runtime/sandbox/backend-none.js +21 -0
  105. package/dist/src/control-plane/runtime/sandbox/backend-seatbelt.js +268 -0
  106. package/dist/src/control-plane/runtime/sandbox/backend.js +1718 -0
  107. package/dist/src/control-plane/runtime/sandbox/orchestrator.js +1745 -0
  108. package/dist/src/control-plane/runtime/sandbox/utils.js +137 -0
  109. package/dist/src/control-plane/runtime/sandbox-backend-bwrap.js +311 -0
  110. package/dist/src/control-plane/runtime/sandbox-backend-none.js +21 -0
  111. package/dist/src/control-plane/runtime/sandbox-backend-seatbelt.js +268 -0
  112. package/dist/src/control-plane/runtime/sandbox-backend.js +1718 -0
  113. package/dist/src/control-plane/runtime/sandbox-orchestrator.js +1745 -0
  114. package/dist/src/control-plane/runtime/sandbox-utils.js +137 -0
  115. package/dist/src/control-plane/runtime/snapshot/index.js +454 -0
  116. package/dist/src/control-plane/runtime/snapshot/sidecar.js +502 -0
  117. package/dist/src/control-plane/runtime/snapshot/task-run.js +1578 -0
  118. package/dist/src/control-plane/runtime/snapshot-sidecar.js +498 -0
  119. package/dist/src/control-plane/runtime/snapshot.js +454 -0
  120. package/dist/src/control-plane/runtime/task-run-snapshot.js +1578 -0
  121. package/dist/src/control-plane/runtime/tool-gateway.js +422 -0
  122. package/dist/src/control-plane/runtime/tooling/browser-tools.js +32 -0
  123. package/dist/src/control-plane/runtime/tooling/claude-router-binary.js +343 -0
  124. package/dist/src/control-plane/runtime/tooling/claude-router.js +524 -0
  125. package/dist/src/control-plane/runtime/tooling/file-tools.js +182 -0
  126. package/dist/src/control-plane/runtime/tooling/gateway.js +422 -0
  127. package/dist/src/control-plane/runtime/tooling/index.js +1290 -0
  128. package/dist/src/control-plane/runtime/tooling/shell.js +205 -0
  129. package/dist/src/control-plane/runtime/types.js +1 -0
  130. package/dist/src/control-plane/setup-version.js +14 -0
  131. package/dist/src/control-plane/state-sync/index.js +1509 -0
  132. package/dist/src/control-plane/state-sync/read.js +856 -0
  133. package/dist/src/control-plane/state-sync/reconcile.js +260 -0
  134. package/dist/src/control-plane/state-sync/repo.js +302 -0
  135. package/dist/src/control-plane/state-sync/types.js +111 -0
  136. package/dist/src/control-plane/state-sync/write.js +1469 -0
  137. package/dist/src/control-plane/task-fields.js +38 -0
  138. package/dist/src/control-plane/task-source-bootstrap.js +46 -0
  139. package/dist/src/control-plane/task-source.js +30 -0
  140. package/dist/src/control-plane/tasks/legacy-task-config-source.js +130 -0
  141. package/dist/src/control-plane/tasks/plugin-task-source.js +103 -0
  142. package/dist/src/control-plane/tasks/source-aware-task-config-source.js +611 -0
  143. package/dist/src/control-plane/tasks/source-lifecycle.js +1093 -0
  144. package/dist/src/control-plane/tasks/task-record-reader.js +9 -0
  145. package/dist/src/control-plane/validators/boundary/public-apis.js +107 -0
  146. package/dist/src/control-plane/validators/integration/_shared.js +51 -0
  147. package/dist/src/control-plane/validators/integration/adm-audit-http.js +85 -0
  148. package/dist/src/control-plane/validators/integration/adm-auth-http.js +78 -0
  149. package/dist/src/control-plane/validators/integration/adm-issuer-http.js +80 -0
  150. package/dist/src/control-plane/validators/integration/adm-migration.js +78 -0
  151. package/dist/src/control-plane/validators/integration/adm-scaffold.js +78 -0
  152. package/dist/src/control-plane/validators/runtime-registration.js +64 -0
  153. package/dist/src/control-plane/validators/shared.js +683 -0
  154. package/dist/src/events.js +218 -0
  155. package/dist/src/execution.js +35 -0
  156. package/dist/src/index.js +1633 -0
  157. package/dist/src/layout.js +145 -0
  158. package/dist/src/local-server.js +202 -0
  159. package/dist/src/plugins.js +329 -0
  160. package/dist/src/remote-http.js +83 -0
  161. package/dist/src/runtime-context.js +216 -0
  162. package/dist/src/types.js +1 -0
  163. package/native/darwin-arm64/bin/rig-git +0 -0
  164. package/native/darwin-arm64/bin/rig-shell +0 -0
  165. package/native/darwin-arm64/bin/rig-tools +0 -0
  166. package/native/darwin-arm64/lib/runtime-native-darwin-arm64.dylib +0 -0
  167. package/native/darwin-arm64/lib/runtime-native.dylib +0 -0
  168. package/native/darwin-arm64/manifest.json +1 -0
  169. package/native/linux-x64/bin/rig-git +0 -0
  170. package/native/linux-x64/bin/rig-shell +0 -0
  171. package/native/linux-x64/bin/rig-tools +0 -0
  172. package/native/linux-x64/lib/runtime-native-linux-x64.so +0 -0
  173. package/native/linux-x64/lib/runtime-native.so +0 -0
  174. package/native/linux-x64/manifest.json +1 -0
  175. package/package.json +74 -0
  176. package/skills/rig-task-run.md +71 -0
@@ -0,0 +1,520 @@
1
+ // @bun
2
+ // packages/runtime/src/control-plane/runtime/tooling/claude-router.ts
3
+ import { existsSync, mkdirSync, statSync, writeFileSync } from "fs";
4
+ import { resolve } from "path";
5
+ import { createInterface } from "readline";
6
+ var CLAUDE_ROUTER_SERVER_NAME = "rig_runtime_tools";
7
+ var CLAUDE_DISABLED_FILE_TOOLS = [
8
+ "Read",
9
+ "Edit",
10
+ "Write",
11
+ "NotebookEdit",
12
+ "Glob",
13
+ "Grep"
14
+ ];
15
+ var ROUTER_VERSION = "0.1.0";
16
+ var CLAUDE_ROUTER_TOOL_DEFINITIONS = [
17
+ {
18
+ name: "read",
19
+ description: "Read a UTF-8 file from the current Rig task worktree, the outer control-plane repo ($PROJECT_RIG_ROOT), or the host monorepo checkout ($MONOREPO_MAIN_ROOT).",
20
+ inputSchema: {
21
+ type: "object",
22
+ properties: {
23
+ path: { type: "string", description: "Absolute or workspace-relative file path." },
24
+ offset: { type: "integer", minimum: 0 },
25
+ limit: { type: "integer", minimum: 1, maximum: 2097152 }
26
+ },
27
+ required: ["path"],
28
+ additionalProperties: false
29
+ }
30
+ },
31
+ {
32
+ name: "write",
33
+ description: "Write UTF-8 content to a file inside the current Rig task workspace only.",
34
+ inputSchema: {
35
+ type: "object",
36
+ properties: {
37
+ path: { type: "string", description: "Absolute or workspace-relative file path." },
38
+ content: { type: "string" },
39
+ create_parents: { type: "boolean" }
40
+ },
41
+ required: ["path", "content"],
42
+ additionalProperties: false
43
+ }
44
+ },
45
+ {
46
+ name: "edit",
47
+ description: "Replace one or more exact string occurrences in a UTF-8 file inside the current Rig task workspace only.",
48
+ inputSchema: {
49
+ type: "object",
50
+ properties: {
51
+ path: { type: "string", description: "Absolute or workspace-relative file path." },
52
+ old_string: { type: "string" },
53
+ new_string: { type: "string" },
54
+ replace_all: { type: "boolean" }
55
+ },
56
+ required: ["path", "old_string", "new_string"],
57
+ additionalProperties: false
58
+ }
59
+ },
60
+ {
61
+ name: "glob",
62
+ description: "Search files using a glob pattern across the current task worktree, $PROJECT_RIG_ROOT, or $MONOREPO_MAIN_ROOT.",
63
+ inputSchema: {
64
+ type: "object",
65
+ properties: {
66
+ pattern: { type: "string" },
67
+ path: { type: "string", description: "Optional workspace-relative search root." },
68
+ include_hidden: { type: "boolean" },
69
+ max_results: { type: "integer", minimum: 1, maximum: 5000 }
70
+ },
71
+ required: ["pattern"],
72
+ additionalProperties: false
73
+ }
74
+ },
75
+ {
76
+ name: "grep",
77
+ description: "Search file contents across the current task worktree, $PROJECT_RIG_ROOT, or $MONOREPO_MAIN_ROOT.",
78
+ inputSchema: {
79
+ type: "object",
80
+ properties: {
81
+ pattern: { type: "string" },
82
+ path: { type: "string", description: "Optional workspace-relative search root." },
83
+ literal: { type: "boolean" },
84
+ include_hidden: { type: "boolean" },
85
+ max_results: { type: "integer", minimum: 1, maximum: 500 },
86
+ glob: { type: "string", description: "Optional file glob filter passed through to ripgrep." }
87
+ },
88
+ required: ["pattern"],
89
+ additionalProperties: false
90
+ }
91
+ }
92
+ ];
93
+ function claudeRuntimeToolCliArgs(configPath) {
94
+ return [
95
+ "--disallowedTools",
96
+ CLAUDE_DISABLED_FILE_TOOLS.join(","),
97
+ "--mcp-config",
98
+ configPath
99
+ ];
100
+ }
101
+ function buildRuntimeToolRouterServerConfig(options) {
102
+ return {
103
+ type: "stdio",
104
+ command: resolve(options.binDir, "rig-tool-router"),
105
+ args: [],
106
+ env: {
107
+ RIG_RUNTIME_CONTEXT_FILE: options.contextFile,
108
+ RIG_RUNTIME_BIN_DIR: options.binDir
109
+ }
110
+ };
111
+ }
112
+ function escapeTomlString(value) {
113
+ return value.replaceAll("\\", "\\\\").replaceAll('"', "\\\"");
114
+ }
115
+ function tomlString(value) {
116
+ return `"${escapeTomlString(value)}"`;
117
+ }
118
+ function tomlStringArray(values) {
119
+ return `[${values.map((value) => tomlString(value)).join(", ")}]`;
120
+ }
121
+ function tomlInlineTable(entries) {
122
+ return `{ ${Object.entries(entries).map(([key, value]) => `${key} = ${tomlString(value)}`).join(", ")} }`;
123
+ }
124
+ function codexRuntimeToolCliArgs(options) {
125
+ const serverConfig = buildRuntimeToolRouterServerConfig(options);
126
+ return [
127
+ "-a",
128
+ "never",
129
+ "-c",
130
+ `mcp_servers.${CLAUDE_ROUTER_SERVER_NAME}.command=${tomlString(serverConfig.command)}`,
131
+ "-c",
132
+ `mcp_servers.${CLAUDE_ROUTER_SERVER_NAME}.args=${tomlStringArray(serverConfig.args)}`,
133
+ "-c",
134
+ `mcp_servers.${CLAUDE_ROUTER_SERVER_NAME}.env=${tomlInlineTable(serverConfig.env)}`
135
+ ];
136
+ }
137
+ function writeClaudeRuntimeToolRouterConfig(options) {
138
+ const configPath = resolve(options.stateDir, "claude-runtime-tools.mcp.json");
139
+ mkdirSync(options.stateDir, { recursive: true });
140
+ const payload = {
141
+ mcpServers: {
142
+ [CLAUDE_ROUTER_SERVER_NAME]: buildRuntimeToolRouterServerConfig(options)
143
+ }
144
+ };
145
+ writeFileSync(configPath, `${JSON.stringify(payload, null, 2)}
146
+ `, "utf-8");
147
+ return configPath;
148
+ }
149
+ async function runClaudeToolRouterServer() {
150
+ const lineReader = createInterface({
151
+ input: process.stdin,
152
+ crlfDelay: Infinity
153
+ });
154
+ const inFlightToolCalls = new Set;
155
+ const trackToolCall = (call) => {
156
+ inFlightToolCalls.add(call);
157
+ call.finally(() => inFlightToolCalls.delete(call)).catch((err) => {
158
+ console.warn("[claude-router] in-flight tool call failure:", err);
159
+ });
160
+ };
161
+ for await (const line of lineReader) {
162
+ const trimmed = line.trim();
163
+ if (!trimmed) {
164
+ continue;
165
+ }
166
+ let request;
167
+ try {
168
+ request = JSON.parse(trimmed);
169
+ } catch {
170
+ continue;
171
+ }
172
+ if (!request.method) {
173
+ continue;
174
+ }
175
+ if (request.method === "notifications/initialized") {
176
+ continue;
177
+ }
178
+ if (request.method === "initialize") {
179
+ writeMessage({
180
+ jsonrpc: "2.0",
181
+ id: request.id ?? null,
182
+ result: {
183
+ protocolVersion: "2024-11-05",
184
+ capabilities: {
185
+ tools: {}
186
+ },
187
+ serverInfo: {
188
+ name: CLAUDE_ROUTER_SERVER_NAME,
189
+ version: ROUTER_VERSION
190
+ }
191
+ }
192
+ });
193
+ continue;
194
+ }
195
+ if (request.method === "ping") {
196
+ writeMessage({
197
+ jsonrpc: "2.0",
198
+ id: request.id ?? null,
199
+ result: {}
200
+ });
201
+ continue;
202
+ }
203
+ if (request.method === "tools/list") {
204
+ writeMessage({
205
+ jsonrpc: "2.0",
206
+ id: request.id ?? null,
207
+ result: {
208
+ tools: CLAUDE_ROUTER_TOOL_DEFINITIONS
209
+ }
210
+ });
211
+ continue;
212
+ }
213
+ if (request.method === "tools/call") {
214
+ const toolName = typeof request.params?.name === "string" ? request.params.name : "";
215
+ const args = isRecord(request.params?.arguments) ? request.params.arguments : {};
216
+ const requestId = request.id ?? null;
217
+ const toolCall = (async () => {
218
+ try {
219
+ const result = await invokeRuntimeTool(toolName, args);
220
+ writeMessage({
221
+ jsonrpc: "2.0",
222
+ id: requestId,
223
+ result
224
+ });
225
+ } catch (error) {
226
+ writeMessage({
227
+ jsonrpc: "2.0",
228
+ id: requestId,
229
+ error: {
230
+ code: -32603,
231
+ message: error instanceof Error ? error.message : String(error)
232
+ }
233
+ });
234
+ }
235
+ })();
236
+ trackToolCall(toolCall);
237
+ continue;
238
+ }
239
+ writeMessage({
240
+ jsonrpc: "2.0",
241
+ id: request.id ?? null,
242
+ error: {
243
+ code: -32601,
244
+ message: `Unsupported method: ${request.method}`
245
+ }
246
+ });
247
+ }
248
+ if (inFlightToolCalls.size > 0) {
249
+ await Promise.allSettled(Array.from(inFlightToolCalls));
250
+ }
251
+ }
252
+ async function invokeRuntimeTool(toolName, args, options = {}) {
253
+ if (toolName === "shell") {
254
+ return invokeRuntimeShellTool(args, options);
255
+ }
256
+ const invocationEnv = options.env ?? process.env;
257
+ const binaryPath = resolveRuntimeToolBinary(toolName, invocationEnv);
258
+ if (!binaryPath) {
259
+ return {
260
+ content: [
261
+ {
262
+ type: "text",
263
+ text: `Unknown Rig runtime tool: ${toolName}`
264
+ }
265
+ ],
266
+ isError: true
267
+ };
268
+ }
269
+ const proc = Bun.spawn([binaryPath], {
270
+ stdin: "pipe",
271
+ stdout: "pipe",
272
+ stderr: "pipe",
273
+ env: invocationEnv
274
+ });
275
+ const requestBody = `${JSON.stringify(args)}
276
+ `;
277
+ if (proc.stdin) {
278
+ await proc.stdin.write(requestBody);
279
+ await proc.stdin.end();
280
+ }
281
+ const [exitCode, stdout, stderr] = await Promise.all([
282
+ proc.exited,
283
+ readBunProcessPipe(proc.stdout),
284
+ readBunProcessPipe(proc.stderr)
285
+ ]);
286
+ if (exitCode !== 0) {
287
+ const text = [stdout.trim(), stderr.trim()].filter(Boolean).join(`
288
+ `) || `Tool ${toolName} exited with code ${exitCode}`;
289
+ return {
290
+ content: [{ type: "text", text }],
291
+ isError: true
292
+ };
293
+ }
294
+ let payload;
295
+ try {
296
+ payload = JSON.parse(stdout);
297
+ } catch {
298
+ return {
299
+ content: [{ type: "text", text: stdout.trim() || `(empty output from ${toolName})` }],
300
+ isError: false
301
+ };
302
+ }
303
+ if (payload.ok === false) {
304
+ return {
305
+ content: [{ type: "text", text: String(payload.message ?? `Tool ${toolName} failed`) }],
306
+ structuredContent: payload,
307
+ isError: true
308
+ };
309
+ }
310
+ return {
311
+ content: [{ type: "text", text: renderToolText(toolName, payload) }],
312
+ structuredContent: payload,
313
+ isError: false
314
+ };
315
+ }
316
+ async function readBunProcessPipe(pipe) {
317
+ if (typeof pipe === "number" || !pipe) {
318
+ return "";
319
+ }
320
+ return new Response(pipe).text();
321
+ }
322
+ function resolveRuntimeToolBinary(toolName, env) {
323
+ const binDir = env.RIG_RUNTIME_BIN_DIR?.trim() || "";
324
+ if (!binDir) {
325
+ return "";
326
+ }
327
+ const mapping = {
328
+ read: "rig-read",
329
+ write: "rig-write",
330
+ edit: "rig-edit",
331
+ glob: "rig-glob",
332
+ grep: "rig-grep"
333
+ };
334
+ const executable = mapping[toolName];
335
+ return executable ? resolve(binDir, executable) : "";
336
+ }
337
+ function renderToolText(toolName, payload) {
338
+ if (toolName === "shell") {
339
+ const command = typeof payload.command === "string" ? payload.command : "(unknown command)";
340
+ const stdout = typeof payload.stdout === "string" ? payload.stdout.trim() : "";
341
+ const stderr = typeof payload.stderr === "string" ? payload.stderr.trim() : "";
342
+ const exitCode = typeof payload.exit_code === "number" ? payload.exit_code : null;
343
+ const output = [stdout, stderr].filter(Boolean).join(`
344
+ `);
345
+ const summary = exitCode === null ? command : `${command}
346
+
347
+ exit_code=${String(exitCode)}`;
348
+ return output ? `${summary}
349
+
350
+ ${output}` : summary;
351
+ }
352
+ if (toolName === "read") {
353
+ const path = typeof payload.path === "string" ? payload.path : "(unknown path)";
354
+ const content = typeof payload.content === "string" ? payload.content : "";
355
+ const truncated = payload.truncated === true ? `
356
+
357
+ [truncated]` : "";
358
+ return `${path}
359
+
360
+ ${content}${truncated}`;
361
+ }
362
+ if (toolName === "glob") {
363
+ const matches = Array.isArray(payload.matches) ? payload.matches.filter((value) => typeof value === "string") : [];
364
+ return matches.length > 0 ? matches.join(`
365
+ `) : "(no matches)";
366
+ }
367
+ if (toolName === "grep") {
368
+ const matches = Array.isArray(payload.matches) ? payload.matches.filter(isRecord) : [];
369
+ if (matches.length === 0) {
370
+ return "(no matches)";
371
+ }
372
+ return matches.map((match) => `${String(match.path ?? "(unknown)")}:${String(match.line_number ?? "?")}: ${String(match.line ?? "")}`).join(`
373
+ `);
374
+ }
375
+ if (toolName === "write") {
376
+ return `Wrote ${String(payload.bytes_written ?? 0)} bytes to ${String(payload.path ?? "(unknown path)")}.`;
377
+ }
378
+ if (toolName === "edit") {
379
+ return `Edited ${String(payload.path ?? "(unknown path)")}; replacements: ${String(payload.replacements ?? 0)}.`;
380
+ }
381
+ return JSON.stringify(payload, null, 2);
382
+ }
383
+ async function invokeRuntimeShellTool(args, options = {}) {
384
+ const command = typeof args.command === "string" ? args.command.trim() : "";
385
+ if (!command) {
386
+ return {
387
+ content: [{ type: "text", text: "Missing required shell `command` string." }],
388
+ isError: true
389
+ };
390
+ }
391
+ const invocationEnv = options.env ?? process.env;
392
+ const shellName = args.shell === "sh" ? "sh" : args.shell === "zsh" ? "zsh" : "bash";
393
+ const shellBinary = resolveRuntimeShellBinary(shellName, invocationEnv);
394
+ if (!shellBinary) {
395
+ return {
396
+ content: [{ type: "text", text: `Missing Rig runtime shell binary for ${shellName}.` }],
397
+ isError: true
398
+ };
399
+ }
400
+ const workspaceRoot = resolve(invocationEnv.RIG_TASK_WORKSPACE?.trim() || process.cwd());
401
+ const requestedWorkdir = typeof args.workdir === "string" ? args.workdir.trim() : "";
402
+ const workdir = requestedWorkdir ? resolveWithinWorkspace(workspaceRoot, requestedWorkdir) : workspaceRoot;
403
+ if (!existsSync(workdir)) {
404
+ return {
405
+ content: [{
406
+ type: "text",
407
+ text: `Shell workdir does not exist inside the task workspace: ${requestedWorkdir || "."}`
408
+ }],
409
+ isError: true
410
+ };
411
+ }
412
+ try {
413
+ if (!statSync(workdir).isDirectory()) {
414
+ return {
415
+ content: [{
416
+ type: "text",
417
+ text: `Shell workdir is not a directory inside the task workspace: ${requestedWorkdir || "."}`
418
+ }],
419
+ isError: true
420
+ };
421
+ }
422
+ } catch (error) {
423
+ return {
424
+ content: [{
425
+ type: "text",
426
+ text: error instanceof Error ? error.message : String(error)
427
+ }],
428
+ isError: true
429
+ };
430
+ }
431
+ let proc = null;
432
+ try {
433
+ proc = Bun.spawn([shellBinary, shellName === "bash" ? "-lc" : "-c", command], {
434
+ cwd: workdir,
435
+ env: invocationEnv,
436
+ stdout: "pipe",
437
+ stderr: "pipe"
438
+ });
439
+ } catch (error) {
440
+ return {
441
+ content: [{
442
+ type: "text",
443
+ text: error instanceof Error ? error.message : String(error)
444
+ }],
445
+ isError: true
446
+ };
447
+ }
448
+ const [exitCode, stdout, stderr] = await Promise.all([
449
+ proc.exited,
450
+ readBunProcessPipe(proc.stdout),
451
+ readBunProcessPipe(proc.stderr)
452
+ ]);
453
+ const payload = {
454
+ ok: exitCode === 0,
455
+ shell: shellName,
456
+ cwd: workdir,
457
+ command,
458
+ stdout,
459
+ stderr,
460
+ exit_code: exitCode
461
+ };
462
+ return {
463
+ content: [{ type: "text", text: renderToolText("shell", payload) }],
464
+ structuredContent: payload,
465
+ isError: exitCode !== 0
466
+ };
467
+ }
468
+ function resolveRuntimeShellBinary(shellName, env) {
469
+ const binDir = env.RIG_RUNTIME_BIN_DIR?.trim() || "";
470
+ return binDir ? resolve(binDir, shellName) : "";
471
+ }
472
+ function resolveWithinWorkspace(workspaceRoot, target) {
473
+ const resolvedTarget = resolve(workspaceRoot, normalizeWorkspaceRelativeTarget(target));
474
+ const normalizedWorkspace = workspaceRoot.endsWith("/") ? workspaceRoot : `${workspaceRoot}/`;
475
+ const normalizedTarget = resolvedTarget.endsWith("/") ? resolvedTarget : `${resolvedTarget}/`;
476
+ if (resolvedTarget === workspaceRoot || normalizedTarget.startsWith(normalizedWorkspace)) {
477
+ return resolvedTarget;
478
+ }
479
+ throw new Error(`Shell workdir must stay inside the task workspace: ${target}`);
480
+ }
481
+ function normalizeWorkspaceRelativeTarget(target) {
482
+ const trimmed = target.trim();
483
+ if (!trimmed) {
484
+ return ".";
485
+ }
486
+ if (trimmed === "$MONOREPO_ROOT" || trimmed === "$RIG_TASK_WORKSPACE") {
487
+ return ".";
488
+ }
489
+ if (trimmed.startsWith("$MONOREPO_ROOT/")) {
490
+ return trimmed.slice("$MONOREPO_ROOT/".length);
491
+ }
492
+ if (trimmed.startsWith("$RIG_TASK_WORKSPACE/")) {
493
+ return trimmed.slice("$RIG_TASK_WORKSPACE/".length);
494
+ }
495
+ if (trimmed === "repos/spliter-monorepo") {
496
+ return ".";
497
+ }
498
+ if (trimmed.startsWith("repos/spliter-monorepo/")) {
499
+ return trimmed.slice("repos/spliter-monorepo/".length);
500
+ }
501
+ return trimmed;
502
+ }
503
+ function isRecord(value) {
504
+ return typeof value === "object" && value !== null && !Array.isArray(value);
505
+ }
506
+ function writeMessage(payload) {
507
+ process.stdout.write(`${JSON.stringify(payload)}
508
+ `);
509
+ }
510
+ if (false) {}
511
+ export {
512
+ writeClaudeRuntimeToolRouterConfig,
513
+ runClaudeToolRouterServer,
514
+ invokeRuntimeTool,
515
+ codexRuntimeToolCliArgs,
516
+ claudeRuntimeToolCliArgs,
517
+ CLAUDE_ROUTER_TOOL_DEFINITIONS,
518
+ CLAUDE_ROUTER_SERVER_NAME,
519
+ CLAUDE_DISABLED_FILE_TOOLS
520
+ };