@h-rig/cli 0.0.6-alpha.21 → 0.0.6-alpha.23

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.
@@ -1,8 +1,8 @@
1
1
  // @bun
2
2
  // packages/cli/src/commands/task.ts
3
- import { readFileSync as readFileSync4 } from "fs";
3
+ import { readFileSync as readFileSync3 } from "fs";
4
4
  import { spawnSync } from "child_process";
5
- import { resolve as resolve4 } from "path";
5
+ import { resolve as resolve3 } from "path";
6
6
  import { cancel as cancel2, confirm, isCancel as isCancel2 } from "@clack/prompts";
7
7
 
8
8
  // packages/cli/src/runner.ts
@@ -13,9 +13,6 @@ import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
13
13
  import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
14
14
  import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
15
15
  import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
16
- function formatCommand(parts) {
17
- return parts.map((part) => /[^a-zA-Z0-9_./:-]/.test(part) ? JSON.stringify(part) : part).join(" ");
18
- }
19
16
  function takeFlag(args, flag) {
20
17
  const rest = [];
21
18
  let value = false;
@@ -362,6 +359,66 @@ async function steerRunViaServer(context, runId, message) {
362
359
  });
363
360
  return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
364
361
  }
362
+ async function getRunPiSessionViaServer(context, runId) {
363
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi`);
364
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
365
+ }
366
+ async function getRunPiMessagesViaServer(context, runId) {
367
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/messages`);
368
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { messages: [] };
369
+ }
370
+ async function getRunPiStatusViaServer(context, runId) {
371
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/status`);
372
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
373
+ }
374
+ async function getRunPiCommandsViaServer(context, runId) {
375
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands`);
376
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { commands: [] };
377
+ }
378
+ async function sendRunPiPromptViaServer(context, runId, text, streamingBehavior) {
379
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/prompt`, {
380
+ method: "POST",
381
+ headers: { "content-type": "application/json" },
382
+ body: JSON.stringify({ text, streamingBehavior })
383
+ });
384
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
385
+ }
386
+ async function sendRunPiShellViaServer(context, runId, text) {
387
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/shell`, {
388
+ method: "POST",
389
+ headers: { "content-type": "application/json" },
390
+ body: JSON.stringify({ text })
391
+ });
392
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
393
+ }
394
+ async function runRunPiCommandViaServer(context, runId, text) {
395
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands/run`, {
396
+ method: "POST",
397
+ headers: { "content-type": "application/json" },
398
+ body: JSON.stringify({ text })
399
+ });
400
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { type: "done" };
401
+ }
402
+ async function respondRunPiExtensionUiViaServer(context, runId, requestId, valueOrCancel) {
403
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/extension-ui/respond`, {
404
+ method: "POST",
405
+ headers: { "content-type": "application/json" },
406
+ body: JSON.stringify({ requestId, ...valueOrCancel })
407
+ });
408
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
409
+ }
410
+ async function abortRunPiViaServer(context, runId) {
411
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/abort`, { method: "POST" });
412
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { aborted: true };
413
+ }
414
+ async function buildRunPiEventsWebSocketUrl(context, runId) {
415
+ const server = await ensureServerForCli(context.projectRoot);
416
+ const url = new URL(`${server.baseUrl.replace(/\/+$/, "")}/api/runs/${encodeURIComponent(runId)}/pi/events`);
417
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
418
+ if (server.authToken)
419
+ url.searchParams.set("token", server.authToken);
420
+ return url.toString();
421
+ }
365
422
  async function submitTaskRunViaServer(context, input) {
366
423
  const isTaskRun = Boolean(input.taskId);
367
424
  const endpoint = isTaskRun ? "/api/runs/task" : "/api/runs/adhoc";
@@ -394,85 +451,6 @@ async function submitTaskRunViaServer(context, input) {
394
451
  return { runId };
395
452
  }
396
453
 
397
- // packages/cli/src/commands/_pi-install.ts
398
- import { existsSync as existsSync3, readFileSync as readFileSync3, rmSync } from "fs";
399
- import { homedir as homedir2 } from "os";
400
- import { resolve as resolve3 } from "path";
401
- var PI_RIG_PACKAGE_NAME = "@h-rig/pi-rig";
402
- var LEGACY_PI_RIG_PACKAGE_NAME = "@rig/pi-rig";
403
- async function defaultCommandRunner(command, options = {}) {
404
- const proc = Bun.spawn(command, { cwd: options.cwd, stdout: "pipe", stderr: "pipe" });
405
- const [stdout, stderr, exitCode] = await Promise.all([
406
- new Response(proc.stdout).text(),
407
- new Response(proc.stderr).text(),
408
- proc.exited
409
- ]);
410
- return { exitCode, stdout, stderr };
411
- }
412
- function resolvePiRigExtensionPath(homeDir) {
413
- return resolve3(homeDir, ".pi", "agent", "extensions", "pi-rig");
414
- }
415
- function resolvePiRigPackageSource(projectRoot, exists = existsSync3) {
416
- const localPackage = resolve3(projectRoot, "packages", "pi-rig");
417
- if (exists(resolve3(localPackage, "package.json")))
418
- return localPackage;
419
- return `npm:${PI_RIG_PACKAGE_NAME}`;
420
- }
421
- function resolvePiHomeDir(inputHomeDir) {
422
- return inputHomeDir ?? process.env.RIG_PI_HOME_DIR?.trim() ?? homedir2();
423
- }
424
- function piListContainsPiRig(output) {
425
- return output.split(/\r?\n/).some((line) => {
426
- const normalized = line.trim();
427
- return normalized.includes(PI_RIG_PACKAGE_NAME) || normalized.includes(LEGACY_PI_RIG_PACKAGE_NAME) || /(?:^|[\\/])packages[\\/]pi-rig(?:$|\s)/.test(normalized);
428
- });
429
- }
430
- async function safeRun(runner, command, options) {
431
- try {
432
- return await runner(command, options);
433
- } catch (error) {
434
- return { exitCode: 1, stdout: "", stderr: error instanceof Error ? error.message : String(error) };
435
- }
436
- }
437
- async function checkPiRigInstall(input = {}) {
438
- const home = resolvePiHomeDir(input.homeDir);
439
- const extensionPath = resolvePiRigExtensionPath(home);
440
- if (process.env.RIG_TEST_FAKE_PI_INSTALL === "1") {
441
- return {
442
- extensionPath,
443
- pi: { ok: true, label: "pi", detail: "fake-pi" },
444
- piRig: { ok: true, label: "pi-rig global extension", detail: extensionPath }
445
- };
446
- }
447
- const exists = input.exists ?? existsSync3;
448
- const runner = input.commandRunner ?? defaultCommandRunner;
449
- const piResult = await safeRun(runner, ["pi", "--version"]);
450
- const piListResult = piResult.exitCode === 0 ? await safeRun(runner, ["pi", "list"]) : { exitCode: 1, stdout: "", stderr: "" };
451
- const listedPiRig = piListResult.exitCode === 0 && piListContainsPiRig(`${piListResult.stdout}
452
- ${piListResult.stderr}`);
453
- const legacyBridge = exists(resolve3(extensionPath, "index.ts"));
454
- const hasPiRig = listedPiRig;
455
- return {
456
- extensionPath,
457
- pi: {
458
- ok: piResult.exitCode === 0,
459
- label: "pi",
460
- detail: (piResult.stdout || piResult.stderr).trim() || undefined,
461
- hint: piResult.exitCode === 0 ? undefined : "Install Pi or run `rig init --yes` to install/update the Pi runtime."
462
- },
463
- piRig: {
464
- ok: hasPiRig,
465
- label: "pi-rig global extension",
466
- detail: hasPiRig ? piListResult.stdout.trim() || PI_RIG_PACKAGE_NAME : legacyBridge ? `${extensionPath} (legacy bridge; reinstall required)` : undefined,
467
- hint: hasPiRig ? undefined : "Run `rig init --yes` to install/enable the global pi-rig package with `pi install`."
468
- }
469
- };
470
- }
471
- async function buildPiSetupChecks(input = {}) {
472
- const status = await checkPiRigInstall(input);
473
- return [status.pi, status.piRig];
474
- }
475
-
476
454
  // packages/cli/src/commands/_preflight.ts
477
455
  function preflightCheck(id, label, status, detail, remediation) {
478
456
  return {
@@ -630,14 +608,7 @@ async function runFastTaskRunPreflight(context, options = {}) {
630
608
  }
631
609
  }
632
610
  if ((options.runtimeAdapter ?? "pi") === "pi") {
633
- const piChecks = await (options.piChecks ?? (() => buildPiSetupChecks()))().catch((error) => [{
634
- ok: false,
635
- label: "pi/pi-rig checks",
636
- hint: message(error)
637
- }]);
638
- for (const pi of piChecks) {
639
- checks.push(preflightCheck(pi.label === "pi" ? "pi" : "pi-rig", pi.label, pi.ok ? "pass" : "fail", pi.detail, pi.hint ?? (pi.ok ? undefined : "Run `rig init --yes` to install/update Pi and enable pi-rig.")));
640
- }
611
+ checks.push(preflightCheck("runtime", "worker Pi SDK session daemon", "pass", selectedServer?.connectionKind === "remote" ? "remote worker-owned runtime" : "bundled server-owned runtime"));
641
612
  } else {
642
613
  checks.push(preflightCheck("runtime", "runtime adapter", "pass", options.runtimeAdapter));
643
614
  }
@@ -974,8 +945,477 @@ async function selectTaskWithTextPicker(tasks, io = {}) {
974
945
  return Number.isFinite(index) ? tasks[index] ?? null : null;
975
946
  }
976
947
 
977
- // packages/cli/src/commands/_operator-view.ts
948
+ // packages/cli/src/commands/_pi-frontend.ts
949
+ import { mkdtempSync, rmSync } from "fs";
950
+ import { tmpdir } from "os";
951
+ import { join } from "path";
952
+ import { main as runPiMain } from "@earendil-works/pi-coding-agent";
953
+
954
+ // packages/cli/src/commands/_pi-worker-bridge-extension.ts
978
955
  var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
956
+ var MAX_TRANSCRIPT_LINES = 120;
957
+ function recordOf(value) {
958
+ return value && typeof value === "object" && !Array.isArray(value) ? value : null;
959
+ }
960
+ function asText(value) {
961
+ if (typeof value === "string")
962
+ return value;
963
+ if (value === null || value === undefined)
964
+ return "";
965
+ if (typeof value === "number" || typeof value === "boolean")
966
+ return String(value);
967
+ try {
968
+ return JSON.stringify(value);
969
+ } catch {
970
+ return String(value);
971
+ }
972
+ }
973
+ function textFromContent(content) {
974
+ if (typeof content === "string")
975
+ return content;
976
+ if (!Array.isArray(content))
977
+ return asText(content);
978
+ return content.flatMap((part) => {
979
+ const item = recordOf(part);
980
+ if (!item)
981
+ return [];
982
+ if (typeof item.text === "string")
983
+ return [item.text];
984
+ if (typeof item.content === "string")
985
+ return [item.content];
986
+ if (item.type === "toolCall")
987
+ return [`\u23FA ${String(item.name ?? "tool")} ${asText(item.arguments ?? "")}`.trim()];
988
+ if (item.type === "toolResult")
989
+ return [`\u21B3 ${asText(item.content ?? item.result ?? "")}`.trim()];
990
+ return [];
991
+ }).join(`
992
+ `);
993
+ }
994
+ function appendTranscript(state, label, text) {
995
+ const trimmed = text.trimEnd();
996
+ if (!trimmed)
997
+ return;
998
+ const lines = trimmed.split(/\r?\n/);
999
+ state.transcript.push(`${label}: ${lines[0] ?? ""}`);
1000
+ for (const line of lines.slice(1))
1001
+ state.transcript.push(` ${line}`);
1002
+ if (state.transcript.length > MAX_TRANSCRIPT_LINES) {
1003
+ state.transcript.splice(0, state.transcript.length - MAX_TRANSCRIPT_LINES);
1004
+ }
1005
+ }
1006
+ function parseExtensionUiRequest(value) {
1007
+ const request = recordOf(value) ?? {};
1008
+ const requestId = String(request.requestId ?? request.id ?? `ui-${Date.now()}`);
1009
+ const method = String(request.method ?? request.type ?? "input");
1010
+ const prompt = asText(request.prompt ?? request.message ?? request.title ?? method);
1011
+ const rawOptions = Array.isArray(request.options) ? request.options : Array.isArray(request.items) ? request.items : [];
1012
+ const options = rawOptions.map((option) => {
1013
+ const record = recordOf(option);
1014
+ return record ? asText(record.label ?? record.value ?? record.name ?? option) : asText(option);
1015
+ }).filter(Boolean);
1016
+ return { requestId, method, prompt, options };
1017
+ }
1018
+ function renderBridgeWidget(state) {
1019
+ const statusParts = [
1020
+ state.wsConnected ? "live WS" : "WS pending",
1021
+ state.status,
1022
+ state.model,
1023
+ state.cwd
1024
+ ].filter(Boolean);
1025
+ const lines = [`Worker Pi daemon bridge \xB7 ${statusParts.join(" \xB7 ")}`];
1026
+ if (state.activity)
1027
+ lines.push(state.activity);
1028
+ if (state.commands.length > 0) {
1029
+ lines.push(`Worker commands: ${state.commands.slice(0, 10).join(", ")}${state.commands.length > 10 ? ", \u2026" : ""}`);
1030
+ }
1031
+ lines.push("");
1032
+ if (state.transcript.length > 0) {
1033
+ lines.push(...state.transcript.slice(-MAX_TRANSCRIPT_LINES));
1034
+ } else {
1035
+ lines.push("Waiting for worker Pi daemon transcript\u2026");
1036
+ }
1037
+ if (state.pendingUi) {
1038
+ lines.push("");
1039
+ lines.push(`Extension UI request \xB7 ${state.pendingUi.method}`);
1040
+ lines.push(state.pendingUi.prompt);
1041
+ state.pendingUi.options.forEach((option, index) => lines.push(`${index + 1}. ${option}`));
1042
+ lines.push("Reply in the Pi editor. /cancel cancels this request.");
1043
+ }
1044
+ return lines;
1045
+ }
1046
+ function updatePiUi(ctx, state) {
1047
+ ctx.ui.setTitle("Pi \xB7 Rig worker daemon");
1048
+ ctx.ui.setStatus("rig-worker-pi", state.wsConnected ? "worker Pi WS live" : state.status);
1049
+ ctx.ui.setWorkingVisible(false);
1050
+ ctx.ui.setWidget("rig-worker-pi-transcript", renderBridgeWidget(state), { placement: "aboveEditor" });
1051
+ }
1052
+ function applyStatus(state, payload) {
1053
+ const status = recordOf(payload.status) ?? payload;
1054
+ state.streaming = status.isStreaming === true || status.isCompacting === true || status.isBashRunning === true;
1055
+ state.cwd = typeof status.cwd === "string" ? status.cwd : state.cwd;
1056
+ state.model = typeof status.model === "string" ? status.model : state.model;
1057
+ const pending = typeof status.pendingMessageCount === "number" ? status.pendingMessageCount : 0;
1058
+ state.status = `${state.streaming ? "streaming" : "idle"}${pending ? ` \xB7 ${pending} queued` : ""}`;
1059
+ }
1060
+ function applyMessage(state, message2) {
1061
+ const record = recordOf(message2);
1062
+ if (!record)
1063
+ return;
1064
+ const role = String(record.role ?? "system");
1065
+ const label = role === "assistant" ? "Pi" : role === "user" ? "You" : role === "tool" || role === "toolResult" ? "Tool" : "System";
1066
+ appendTranscript(state, label, textFromContent(record.content ?? record.message ?? record.text ?? ""));
1067
+ }
1068
+ function applyPiEvent(state, eventValue) {
1069
+ const event = recordOf(eventValue);
1070
+ if (!event)
1071
+ return;
1072
+ const type = String(event.type ?? "event");
1073
+ if (type === "agent_start") {
1074
+ state.streaming = true;
1075
+ state.status = "streaming";
1076
+ return;
1077
+ }
1078
+ if (type === "agent_end") {
1079
+ state.streaming = false;
1080
+ state.status = "idle";
1081
+ appendTranscript(state, "System", "Agent turn complete.");
1082
+ return;
1083
+ }
1084
+ if (type === "message_start" || type === "message_end" || type === "turn_end") {
1085
+ applyMessage(state, event.message);
1086
+ return;
1087
+ }
1088
+ if (type === "message_update") {
1089
+ const assistantEvent = recordOf(event.assistantMessageEvent);
1090
+ const delta = typeof assistantEvent?.delta === "string" ? assistantEvent.delta : typeof assistantEvent?.text === "string" ? assistantEvent.text : "";
1091
+ if (delta)
1092
+ appendTranscript(state, assistantEvent?.type === "thinking_delta" ? "Thinking" : "Pi", delta);
1093
+ return;
1094
+ }
1095
+ if (type === "tool_execution_start") {
1096
+ appendTranscript(state, "Tool", `${String(event.toolName ?? "tool")} ${asText(event.args ?? "")}`.trim());
1097
+ return;
1098
+ }
1099
+ if (type === "tool_execution_update") {
1100
+ appendTranscript(state, "Tool", asText(event.partialResult ?? ""));
1101
+ return;
1102
+ }
1103
+ if (type === "tool_execution_end") {
1104
+ appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.result ?? `${String(event.toolName ?? "tool")} complete`));
1105
+ return;
1106
+ }
1107
+ if (type === "queue_update") {
1108
+ const steering = Array.isArray(event.steering) ? event.steering.length : 0;
1109
+ const followUp = Array.isArray(event.followUp) ? event.followUp.length : 0;
1110
+ state.status = `queued \xB7 steer ${steering} \xB7 follow-up ${followUp}`;
1111
+ }
1112
+ }
1113
+ function applyUiEvent(state, value) {
1114
+ const event = recordOf(value);
1115
+ if (!event)
1116
+ return;
1117
+ const type = String(event.type ?? "ui");
1118
+ if (type === "shell.start")
1119
+ appendTranscript(state, "Tool", `$ ${asText(event.command)}`);
1120
+ else if (type === "shell.chunk")
1121
+ appendTranscript(state, "Tool", asText(event.chunk));
1122
+ else if (type === "shell.end")
1123
+ appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.output ?? `exit ${String(event.exitCode ?? "")}`));
1124
+ else
1125
+ appendTranscript(state, "System", `${type}: ${asText(event)}`);
1126
+ }
1127
+ function applyEnvelope(state, envelopeValue) {
1128
+ const envelope = recordOf(envelopeValue);
1129
+ if (!envelope)
1130
+ return;
1131
+ const type = String(envelope.type ?? "");
1132
+ if (type === "ready") {
1133
+ const metadata = recordOf(envelope.metadata);
1134
+ state.cwd = typeof metadata?.cwd === "string" ? metadata.cwd : state.cwd;
1135
+ state.status = "worker Pi daemon ready";
1136
+ appendTranscript(state, "System", "Connected to worker Pi daemon.");
1137
+ } else if (type === "status.update") {
1138
+ applyStatus(state, envelope);
1139
+ } else if (type === "activity.update") {
1140
+ const activity = recordOf(envelope.activity);
1141
+ state.activity = [activity?.label, activity?.detail].map(asText).filter(Boolean).join(" \u2014 ");
1142
+ } else if (type === "extension_ui_request") {
1143
+ state.pendingUi = parseExtensionUiRequest(envelope.request);
1144
+ appendTranscript(state, "System", `Extension UI request: ${state.pendingUi.prompt}`);
1145
+ } else if (type === "pi.ui_event") {
1146
+ applyUiEvent(state, envelope.event);
1147
+ } else if (type === "pi.event") {
1148
+ applyPiEvent(state, envelope.event);
1149
+ } else if (type === "error") {
1150
+ appendTranscript(state, "Error", asText(envelope.message ?? envelope.detail ?? "unknown error"));
1151
+ }
1152
+ }
1153
+ async function waitForWorkerReady(options, ctx, state) {
1154
+ while (true) {
1155
+ const session = await getRunPiSessionViaServer(options.context, options.runId).catch((error) => ({
1156
+ ready: false,
1157
+ status: error instanceof Error ? error.message : String(error),
1158
+ retryAfterMs: 1000
1159
+ }));
1160
+ if (session.ready === false) {
1161
+ const status = String(session.status ?? "starting");
1162
+ state.status = `waiting for worker Pi daemon \xB7 ${status}`;
1163
+ updatePiUi(ctx, state);
1164
+ if (TERMINAL_RUN_STATUSES.has(status.toLowerCase())) {
1165
+ appendTranscript(state, "Error", `Run ended before worker Pi daemon became ready: ${status}`);
1166
+ return false;
1167
+ }
1168
+ await Bun.sleep(typeof session.retryAfterMs === "number" ? session.retryAfterMs : 750);
1169
+ continue;
1170
+ }
1171
+ const sessionRecord = recordOf(session) ?? {};
1172
+ applyEnvelope(state, { type: "ready", metadata: sessionRecord.metadata ?? sessionRecord });
1173
+ updatePiUi(ctx, state);
1174
+ return true;
1175
+ }
1176
+ }
1177
+ function parseWsPayload(message2) {
1178
+ if (typeof message2.data === "string")
1179
+ return JSON.parse(message2.data);
1180
+ return JSON.parse(Buffer.from(message2.data).toString("utf8"));
1181
+ }
1182
+ async function connectWorkerStream(options, ctx, state) {
1183
+ const ready = await waitForWorkerReady(options, ctx, state);
1184
+ if (!ready)
1185
+ return;
1186
+ let catchupDone = false;
1187
+ const buffered = [];
1188
+ const wsUrl = await buildRunPiEventsWebSocketUrl(options.context, options.runId);
1189
+ const socket = new WebSocket(wsUrl);
1190
+ const closePromise = new Promise((resolve3) => {
1191
+ socket.onopen = () => {
1192
+ state.wsConnected = true;
1193
+ state.status = "live worker Pi WebSocket connected";
1194
+ updatePiUi(ctx, state);
1195
+ };
1196
+ socket.onmessage = (message2) => {
1197
+ try {
1198
+ const payload = parseWsPayload(message2);
1199
+ if (!catchupDone)
1200
+ buffered.push(payload);
1201
+ else {
1202
+ applyEnvelope(state, payload);
1203
+ updatePiUi(ctx, state);
1204
+ }
1205
+ } catch (error) {
1206
+ appendTranscript(state, "Error", `Unparseable worker Pi event: ${error instanceof Error ? error.message : String(error)}`);
1207
+ updatePiUi(ctx, state);
1208
+ }
1209
+ };
1210
+ socket.onerror = () => socket.close();
1211
+ socket.onclose = () => {
1212
+ state.wsConnected = false;
1213
+ state.status = "worker Pi WebSocket disconnected";
1214
+ updatePiUi(ctx, state);
1215
+ resolve3();
1216
+ };
1217
+ });
1218
+ try {
1219
+ const [messagesPayload, statusPayload, commandsPayload] = await Promise.all([
1220
+ getRunPiMessagesViaServer(options.context, options.runId),
1221
+ getRunPiStatusViaServer(options.context, options.runId),
1222
+ getRunPiCommandsViaServer(options.context, options.runId)
1223
+ ]);
1224
+ const messages = Array.isArray(messagesPayload.messages) ? messagesPayload.messages : [];
1225
+ for (const message2 of messages)
1226
+ applyMessage(state, message2);
1227
+ applyStatus(state, statusPayload);
1228
+ const commands = Array.isArray(commandsPayload.commands) ? commandsPayload.commands : [];
1229
+ state.commands = commands.flatMap((command) => {
1230
+ const record = recordOf(command);
1231
+ return typeof record?.name === "string" ? [`/${record.name}`] : [];
1232
+ });
1233
+ catchupDone = true;
1234
+ for (const payload of buffered.splice(0))
1235
+ applyEnvelope(state, payload);
1236
+ updatePiUi(ctx, state);
1237
+ } catch (error) {
1238
+ appendTranscript(state, "Error", `Worker Pi catch-up failed: ${error instanceof Error ? error.message : String(error)}`);
1239
+ catchupDone = true;
1240
+ updatePiUi(ctx, state);
1241
+ }
1242
+ await closePromise;
1243
+ }
1244
+ async function answerPendingUi(options, state, line) {
1245
+ const pending = state.pendingUi;
1246
+ if (!pending)
1247
+ return false;
1248
+ if (line === "/cancel") {
1249
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { cancelled: true });
1250
+ } else if (pending.method === "confirm") {
1251
+ const confirmed = /^(y|yes|true|1)$/i.test(line);
1252
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: confirmed, confirmed });
1253
+ } else if (pending.options.length > 0 && /^\d+$/.test(line)) {
1254
+ const selected = pending.options[Math.max(0, Number(line) - 1)] ?? line;
1255
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: selected });
1256
+ } else {
1257
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: line });
1258
+ }
1259
+ appendTranscript(state, "System", `Responded to extension UI request ${pending.requestId}.`);
1260
+ state.pendingUi = null;
1261
+ return true;
1262
+ }
1263
+ async function routeInput(options, ctx, state, line) {
1264
+ const text = line.trim();
1265
+ if (!text)
1266
+ return;
1267
+ if (await answerPendingUi(options, state, text)) {
1268
+ updatePiUi(ctx, state);
1269
+ return;
1270
+ }
1271
+ if (text === "/detach" || text === "/quit" || text === "/q") {
1272
+ appendTranscript(state, "System", "Detached locally; worker Pi daemon continues.");
1273
+ updatePiUi(ctx, state);
1274
+ ctx.shutdown();
1275
+ return;
1276
+ }
1277
+ if (text === "/stop") {
1278
+ await abortRunPiViaServer(options.context, options.runId);
1279
+ appendTranscript(state, "System", "Stop requested for worker Pi daemon.");
1280
+ updatePiUi(ctx, state);
1281
+ ctx.shutdown();
1282
+ return;
1283
+ }
1284
+ if (text.startsWith("!")) {
1285
+ appendTranscript(state, "You", text);
1286
+ await sendRunPiShellViaServer(options.context, options.runId, text);
1287
+ } else if (text.startsWith("/")) {
1288
+ appendTranscript(state, "You", text);
1289
+ const result = await runRunPiCommandViaServer(options.context, options.runId, text);
1290
+ const message2 = typeof result.message === "string" ? result.message : "worker command accepted";
1291
+ appendTranscript(state, "System", message2);
1292
+ } else {
1293
+ appendTranscript(state, "You", text);
1294
+ await sendRunPiPromptViaServer(options.context, options.runId, text, state.streaming ? "steer" : undefined);
1295
+ }
1296
+ updatePiUi(ctx, state);
1297
+ }
1298
+ function createRigWorkerPiBridgeExtension(options) {
1299
+ return (pi) => {
1300
+ const state = {
1301
+ transcript: [],
1302
+ status: "starting worker Pi daemon bridge",
1303
+ activity: "",
1304
+ cwd: "",
1305
+ model: "",
1306
+ commands: [],
1307
+ streaming: false,
1308
+ pendingUi: null,
1309
+ wsConnected: false
1310
+ };
1311
+ if (options.initialMessageSent)
1312
+ appendTranscript(state, "System", "Initial message sent to worker Pi daemon.");
1313
+ pi.on("session_start", async (_event, ctx) => {
1314
+ updatePiUi(ctx, state);
1315
+ ctx.ui.notify("Rig worker Pi bridge extension loaded", "info");
1316
+ ctx.ui.onTerminalInput((data) => {
1317
+ if (data.includes("\x04")) {
1318
+ ctx.shutdown();
1319
+ return { consume: true };
1320
+ }
1321
+ if (!data.includes("\r") && !data.includes(`
1322
+ `))
1323
+ return;
1324
+ const inlineText = data.replace(/[\r\n]+/g, "").trim();
1325
+ const editorText = ctx.ui.getEditorText().trim();
1326
+ const text = [editorText, inlineText].filter(Boolean).join(" ").trim();
1327
+ if (!text)
1328
+ return;
1329
+ ctx.ui.setEditorText("");
1330
+ routeInput(options, ctx, state, text).catch((error) => {
1331
+ appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
1332
+ updatePiUi(ctx, state);
1333
+ });
1334
+ return { consume: true };
1335
+ });
1336
+ connectWorkerStream(options, ctx, state).catch((error) => {
1337
+ appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
1338
+ updatePiUi(ctx, state);
1339
+ });
1340
+ });
1341
+ pi.on("session_shutdown", () => {});
1342
+ };
1343
+ }
1344
+
1345
+ // packages/cli/src/commands/_pi-frontend.ts
1346
+ function setTemporaryEnv(updates) {
1347
+ const previous = new Map;
1348
+ for (const [key, value] of Object.entries(updates)) {
1349
+ previous.set(key, process.env[key]);
1350
+ process.env[key] = value;
1351
+ }
1352
+ return () => {
1353
+ for (const [key, value] of previous) {
1354
+ if (value === undefined)
1355
+ delete process.env[key];
1356
+ else
1357
+ process.env[key] = value;
1358
+ }
1359
+ };
1360
+ }
1361
+ async function attachRunBundledPiFrontend(context, input) {
1362
+ const tempRoot = mkdtempSync(join(tmpdir(), "rig-pi-frontend-"));
1363
+ const cwd = join(tempRoot, "workspace");
1364
+ const agentDir = join(tempRoot, "agent");
1365
+ const sessionDir = join(tempRoot, "sessions");
1366
+ const previousCwd = process.cwd();
1367
+ const restoreEnv = setTemporaryEnv({
1368
+ PI_CODING_AGENT_DIR: agentDir,
1369
+ PI_CODING_AGENT_SESSION_DIR: sessionDir,
1370
+ PI_OFFLINE: "1",
1371
+ PI_SKIP_VERSION_CHECK: "1"
1372
+ });
1373
+ let detached = false;
1374
+ try {
1375
+ await Bun.$`mkdir -p ${cwd} ${agentDir} ${sessionDir}`.quiet();
1376
+ process.chdir(cwd);
1377
+ await runPiMain([
1378
+ "--offline",
1379
+ "--no-session",
1380
+ "--no-tools",
1381
+ "--no-builtin-tools",
1382
+ "--no-skills",
1383
+ "--no-prompt-templates",
1384
+ "--no-themes",
1385
+ "--no-context-files",
1386
+ "--no-approve"
1387
+ ], {
1388
+ extensionFactories: [
1389
+ createRigWorkerPiBridgeExtension({
1390
+ context,
1391
+ runId: input.runId,
1392
+ initialMessageSent: input.steered === true
1393
+ })
1394
+ ]
1395
+ });
1396
+ detached = true;
1397
+ } finally {
1398
+ process.chdir(previousCwd);
1399
+ restoreEnv();
1400
+ rmSync(tempRoot, { recursive: true, force: true });
1401
+ }
1402
+ let run = { runId: input.runId, status: "unknown" };
1403
+ try {
1404
+ run = await getRunDetailsViaServer(context, input.runId);
1405
+ } catch {}
1406
+ return {
1407
+ run,
1408
+ logs: [],
1409
+ timeline: [],
1410
+ timelineCursor: null,
1411
+ steered: input.steered === true,
1412
+ detached,
1413
+ rendered: "actual bundled Pi frontend hosted Rig worker Pi bridge extension"
1414
+ };
1415
+ }
1416
+
1417
+ // packages/cli/src/commands/_operator-view.ts
1418
+ var TERMINAL_RUN_STATUSES2 = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
979
1419
  function runStatusFromPayload(payload) {
980
1420
  const run = payload.run && typeof payload.run === "object" && !Array.isArray(payload.run) ? payload.run : payload;
981
1421
  return String(run.status ?? "unknown").toLowerCase();
@@ -1017,9 +1457,15 @@ async function readOperatorSnapshot(context, runId, options = {}) {
1017
1457
  async function attachRunOperatorView(context, input) {
1018
1458
  let steered = false;
1019
1459
  if (input.message?.trim()) {
1020
- await steerRunViaServer(context, input.runId, input.message.trim());
1460
+ await sendRunPiPromptViaServer(context, input.runId, input.message.trim(), "steer").catch(() => steerRunViaServer(context, input.runId, input.message.trim()));
1021
1461
  steered = true;
1022
1462
  }
1463
+ if (input.follow && !input.once && input.interactive !== false && context.outputMode === "text" && Boolean(process.stdin.isTTY && process.stdout.isTTY)) {
1464
+ return attachRunBundledPiFrontend(context, {
1465
+ runId: input.runId,
1466
+ steered
1467
+ });
1468
+ }
1023
1469
  const surface = createOperatorSurface({ interactive: input.interactive !== false });
1024
1470
  let snapshot = await readOperatorSnapshot(context, input.runId);
1025
1471
  if (context.outputMode === "text") {
@@ -1027,7 +1473,7 @@ async function attachRunOperatorView(context, input) {
1027
1473
  surface.renderTimeline(snapshot.timeline);
1028
1474
  surface.renderLogs(snapshot.logs);
1029
1475
  if (steered)
1030
- surface.info("Steering message queued.");
1476
+ surface.info("Message submitted to worker Pi.");
1031
1477
  }
1032
1478
  let detached = false;
1033
1479
  let commandInput = null;
@@ -1046,7 +1492,7 @@ async function attachRunOperatorView(context, input) {
1046
1492
  }
1047
1493
  const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 2000));
1048
1494
  let timelineCursor = snapshot.timelineCursor;
1049
- while (!detached && !TERMINAL_RUN_STATUSES.has(runStatusFromPayload(snapshot.run))) {
1495
+ while (!detached && !TERMINAL_RUN_STATUSES2.has(runStatusFromPayload(snapshot.run))) {
1050
1496
  await Bun.sleep(pollMs);
1051
1497
  snapshot = await readOperatorSnapshot(context, input.runId, { timelineCursor });
1052
1498
  timelineCursor = snapshot.timelineCursor;
@@ -1137,92 +1583,6 @@ function formatSubmittedRun(input) {
1137
1583
  `);
1138
1584
  }
1139
1585
 
1140
- // packages/cli/src/commands/_pi-session.ts
1141
- import { spawn } from "child_process";
1142
- function buildPiRigSessionEnv(input) {
1143
- return {
1144
- RIG_PROJECT_ROOT: input.projectRoot,
1145
- PROJECT_RIG_ROOT: input.projectRoot,
1146
- RIG_RUN_ID: input.runId,
1147
- RIG_SERVER_RUN_ID: input.runId,
1148
- RIG_RUNTIME_ADAPTER: "pi",
1149
- RIG_SERVER_URL: input.serverUrl,
1150
- RIG_SERVER_BASE_URL: input.serverUrl,
1151
- RIG_STEERING_POLL_MS: process.env.RIG_STEERING_POLL_MS?.trim() || "1000",
1152
- RIG_PI_OPERATOR_SESSION: "1",
1153
- ...input.taskId ? { RIG_TASK_ID: input.taskId } : {},
1154
- ...input.authToken ? { RIG_AUTH_TOKEN: input.authToken } : {}
1155
- };
1156
- }
1157
- function shellBinary(name) {
1158
- const explicit = process.env.RIG_PI_BINARY?.trim();
1159
- if (explicit)
1160
- return explicit;
1161
- return Bun.which(name) || name;
1162
- }
1163
- function buildPiRigSessionCommand(input) {
1164
- const configuredExtension = input.extensionSource ?? process.env.RIG_PI_RIG_EXTENSION_SOURCE?.trim();
1165
- const extensionSource = configuredExtension && configuredExtension.length > 0 ? configuredExtension : resolvePiRigPackageSource(input.projectRoot);
1166
- const initialCommand = `/rig attach ${input.runId}`;
1167
- return [
1168
- shellBinary("pi"),
1169
- "--no-extensions",
1170
- "--extension",
1171
- extensionSource,
1172
- initialCommand
1173
- ];
1174
- }
1175
- async function launchPiRigSession(context, input) {
1176
- if (context.outputMode !== "text" || !process.stdin.isTTY || !process.stdout.isTTY) {
1177
- return { launched: false, exitCode: null, command: [] };
1178
- }
1179
- if (process.env.RIG_DISABLE_PI_LAUNCH === "1") {
1180
- return { launched: false, exitCode: null, command: [] };
1181
- }
1182
- const server = await ensureServerForCli(context.projectRoot);
1183
- const command = buildPiRigSessionCommand({ ...input, projectRoot: context.projectRoot });
1184
- const env = {
1185
- ...process.env,
1186
- ...buildPiRigSessionEnv({
1187
- projectRoot: context.projectRoot,
1188
- runId: input.runId,
1189
- taskId: input.taskId,
1190
- serverUrl: server.baseUrl,
1191
- authToken: server.authToken
1192
- })
1193
- };
1194
- process.stdout.write(`Launching Pi for Rig run ${input.runId}\u2026
1195
- `);
1196
- process.stdout.write(`Pi command: ${formatCommand(command)}
1197
- `);
1198
- const launchedAt = Date.now();
1199
- const child = spawn(command[0], command.slice(1), {
1200
- cwd: context.projectRoot,
1201
- env,
1202
- stdio: "inherit"
1203
- });
1204
- const launchError = await new Promise((resolve4) => {
1205
- child.once("error", (error) => {
1206
- resolve4({ error: error.message });
1207
- });
1208
- child.once("close", (code) => resolve4({ code }));
1209
- });
1210
- if ("error" in launchError) {
1211
- process.stderr.write(`Failed to launch Pi; falling back to Rig attach view: ${launchError.error}
1212
- `);
1213
- return { launched: false, exitCode: null, command, error: launchError.error };
1214
- }
1215
- const exitCode = launchError.code;
1216
- const elapsedMs = Date.now() - launchedAt;
1217
- if (typeof exitCode === "number" && exitCode !== 0 && elapsedMs < 5000) {
1218
- const error = `Pi exited during startup with code ${exitCode}.`;
1219
- process.stderr.write(`${error} Falling back to Rig attach view.
1220
- `);
1221
- return { launched: false, exitCode, command, error };
1222
- }
1223
- return { launched: true, exitCode, command };
1224
- }
1225
-
1226
1586
  // packages/cli/src/commands/task.ts
1227
1587
  import { buildPluginHostContext } from "@rig/runtime/control-plane/plugin-host-context";
1228
1588
  import { loadConfig } from "@rig/core/load-config";
@@ -1484,7 +1844,7 @@ async function executeTask(context, args, options) {
1484
1844
  const fileFlag = takeOption(rest.slice(1), "--file");
1485
1845
  let content;
1486
1846
  if (fileFlag.value) {
1487
- content = readFileSync4(resolve4(context.projectRoot, fileFlag.value), "utf-8");
1847
+ content = readFileSync3(resolve3(context.projectRoot, fileFlag.value), "utf-8");
1488
1848
  } else {
1489
1849
  content = await readStdin();
1490
1850
  }
@@ -1614,20 +1974,7 @@ async function executeTask(context, args, options) {
1614
1974
  let attachDetails = null;
1615
1975
  if (!detachResult.value && context.outputMode === "text") {
1616
1976
  console.log(formatSubmittedRun({ runId: submitted.runId, task: selectedTask ? summarizeTask(selectedTask) : null }));
1617
- if (runtimeAdapter === "pi") {
1618
- const piSession = await launchPiRigSession(context, {
1619
- runId: submitted.runId,
1620
- taskId: selectedTaskId,
1621
- title: titleResult.value ?? readTaskString(selectedTask ?? {}, "title"),
1622
- runtimeAdapter
1623
- });
1624
- attachDetails = { mode: "pi", ...piSession };
1625
- if (!piSession.launched) {
1626
- attachDetails = await attachRunOperatorView(context, { runId: submitted.runId, follow: true });
1627
- }
1628
- } else {
1629
- attachDetails = await attachRunOperatorView(context, { runId: submitted.runId, follow: true });
1630
- }
1977
+ attachDetails = await attachRunOperatorView(context, { runId: submitted.runId, follow: true });
1631
1978
  } else if (context.outputMode === "text") {
1632
1979
  console.log(formatSubmittedRun({ runId: submitted.runId, task: selectedTask ? summarizeTask(selectedTask) : null }));
1633
1980
  }