@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.
@@ -10,9 +10,6 @@ import { PluginManager } from "@rig/runtime/control-plane/runtime/plugins";
10
10
  import { loadRuntimeContextFromEnv } from "@rig/runtime/control-plane/runtime/context";
11
11
  import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
12
12
  import { CliError as CliError2 } from "@rig/runtime/control-plane/errors";
13
- function formatCommand(parts) {
14
- return parts.map((part) => /[^a-zA-Z0-9_./:-]/.test(part) ? JSON.stringify(part) : part).join(" ");
15
- }
16
13
  function takeFlag(args, flag) {
17
14
  const rest = [];
18
15
  let value = false;
@@ -308,6 +305,66 @@ async function steerRunViaServer(context, runId, message) {
308
305
  });
309
306
  return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { ok: true };
310
307
  }
308
+ async function getRunPiSessionViaServer(context, runId) {
309
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi`);
310
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
311
+ }
312
+ async function getRunPiMessagesViaServer(context, runId) {
313
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/messages`);
314
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { messages: [] };
315
+ }
316
+ async function getRunPiStatusViaServer(context, runId) {
317
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/status`);
318
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
319
+ }
320
+ async function getRunPiCommandsViaServer(context, runId) {
321
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands`);
322
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { commands: [] };
323
+ }
324
+ async function sendRunPiPromptViaServer(context, runId, text, streamingBehavior) {
325
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/prompt`, {
326
+ method: "POST",
327
+ headers: { "content-type": "application/json" },
328
+ body: JSON.stringify({ text, streamingBehavior })
329
+ });
330
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
331
+ }
332
+ async function sendRunPiShellViaServer(context, runId, text) {
333
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/shell`, {
334
+ method: "POST",
335
+ headers: { "content-type": "application/json" },
336
+ body: JSON.stringify({ text })
337
+ });
338
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
339
+ }
340
+ async function runRunPiCommandViaServer(context, runId, text) {
341
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/commands/run`, {
342
+ method: "POST",
343
+ headers: { "content-type": "application/json" },
344
+ body: JSON.stringify({ text })
345
+ });
346
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { type: "done" };
347
+ }
348
+ async function respondRunPiExtensionUiViaServer(context, runId, requestId, valueOrCancel) {
349
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/extension-ui/respond`, {
350
+ method: "POST",
351
+ headers: { "content-type": "application/json" },
352
+ body: JSON.stringify({ requestId, ...valueOrCancel })
353
+ });
354
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { accepted: true };
355
+ }
356
+ async function abortRunPiViaServer(context, runId) {
357
+ const payload = await requestServerJson(context, `/api/runs/${encodeURIComponent(runId)}/pi/abort`, { method: "POST" });
358
+ return payload && typeof payload === "object" && !Array.isArray(payload) ? payload : { aborted: true };
359
+ }
360
+ async function buildRunPiEventsWebSocketUrl(context, runId) {
361
+ const server = await ensureServerForCli(context.projectRoot);
362
+ const url = new URL(`${server.baseUrl.replace(/\/+$/, "")}/api/runs/${encodeURIComponent(runId)}/pi/events`);
363
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
364
+ if (server.authToken)
365
+ url.searchParams.set("token", server.authToken);
366
+ return url.toString();
367
+ }
311
368
 
312
369
  // packages/cli/src/commands/_operator-surface.ts
313
370
  import { createInterface } from "readline";
@@ -485,8 +542,477 @@ function createOperatorSurface(options = {}) {
485
542
  };
486
543
  }
487
544
 
488
- // packages/cli/src/commands/_operator-view.ts
545
+ // packages/cli/src/commands/_pi-frontend.ts
546
+ import { mkdtempSync, rmSync } from "fs";
547
+ import { tmpdir } from "os";
548
+ import { join } from "path";
549
+ import { main as runPiMain } from "@earendil-works/pi-coding-agent";
550
+
551
+ // packages/cli/src/commands/_pi-worker-bridge-extension.ts
489
552
  var TERMINAL_RUN_STATUSES = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
553
+ var MAX_TRANSCRIPT_LINES = 120;
554
+ function recordOf(value) {
555
+ return value && typeof value === "object" && !Array.isArray(value) ? value : null;
556
+ }
557
+ function asText(value) {
558
+ if (typeof value === "string")
559
+ return value;
560
+ if (value === null || value === undefined)
561
+ return "";
562
+ if (typeof value === "number" || typeof value === "boolean")
563
+ return String(value);
564
+ try {
565
+ return JSON.stringify(value);
566
+ } catch {
567
+ return String(value);
568
+ }
569
+ }
570
+ function textFromContent(content) {
571
+ if (typeof content === "string")
572
+ return content;
573
+ if (!Array.isArray(content))
574
+ return asText(content);
575
+ return content.flatMap((part) => {
576
+ const item = recordOf(part);
577
+ if (!item)
578
+ return [];
579
+ if (typeof item.text === "string")
580
+ return [item.text];
581
+ if (typeof item.content === "string")
582
+ return [item.content];
583
+ if (item.type === "toolCall")
584
+ return [`\u23FA ${String(item.name ?? "tool")} ${asText(item.arguments ?? "")}`.trim()];
585
+ if (item.type === "toolResult")
586
+ return [`\u21B3 ${asText(item.content ?? item.result ?? "")}`.trim()];
587
+ return [];
588
+ }).join(`
589
+ `);
590
+ }
591
+ function appendTranscript(state, label, text) {
592
+ const trimmed = text.trimEnd();
593
+ if (!trimmed)
594
+ return;
595
+ const lines = trimmed.split(/\r?\n/);
596
+ state.transcript.push(`${label}: ${lines[0] ?? ""}`);
597
+ for (const line of lines.slice(1))
598
+ state.transcript.push(` ${line}`);
599
+ if (state.transcript.length > MAX_TRANSCRIPT_LINES) {
600
+ state.transcript.splice(0, state.transcript.length - MAX_TRANSCRIPT_LINES);
601
+ }
602
+ }
603
+ function parseExtensionUiRequest(value) {
604
+ const request = recordOf(value) ?? {};
605
+ const requestId = String(request.requestId ?? request.id ?? `ui-${Date.now()}`);
606
+ const method = String(request.method ?? request.type ?? "input");
607
+ const prompt = asText(request.prompt ?? request.message ?? request.title ?? method);
608
+ const rawOptions = Array.isArray(request.options) ? request.options : Array.isArray(request.items) ? request.items : [];
609
+ const options = rawOptions.map((option) => {
610
+ const record = recordOf(option);
611
+ return record ? asText(record.label ?? record.value ?? record.name ?? option) : asText(option);
612
+ }).filter(Boolean);
613
+ return { requestId, method, prompt, options };
614
+ }
615
+ function renderBridgeWidget(state) {
616
+ const statusParts = [
617
+ state.wsConnected ? "live WS" : "WS pending",
618
+ state.status,
619
+ state.model,
620
+ state.cwd
621
+ ].filter(Boolean);
622
+ const lines = [`Worker Pi daemon bridge \xB7 ${statusParts.join(" \xB7 ")}`];
623
+ if (state.activity)
624
+ lines.push(state.activity);
625
+ if (state.commands.length > 0) {
626
+ lines.push(`Worker commands: ${state.commands.slice(0, 10).join(", ")}${state.commands.length > 10 ? ", \u2026" : ""}`);
627
+ }
628
+ lines.push("");
629
+ if (state.transcript.length > 0) {
630
+ lines.push(...state.transcript.slice(-MAX_TRANSCRIPT_LINES));
631
+ } else {
632
+ lines.push("Waiting for worker Pi daemon transcript\u2026");
633
+ }
634
+ if (state.pendingUi) {
635
+ lines.push("");
636
+ lines.push(`Extension UI request \xB7 ${state.pendingUi.method}`);
637
+ lines.push(state.pendingUi.prompt);
638
+ state.pendingUi.options.forEach((option, index) => lines.push(`${index + 1}. ${option}`));
639
+ lines.push("Reply in the Pi editor. /cancel cancels this request.");
640
+ }
641
+ return lines;
642
+ }
643
+ function updatePiUi(ctx, state) {
644
+ ctx.ui.setTitle("Pi \xB7 Rig worker daemon");
645
+ ctx.ui.setStatus("rig-worker-pi", state.wsConnected ? "worker Pi WS live" : state.status);
646
+ ctx.ui.setWorkingVisible(false);
647
+ ctx.ui.setWidget("rig-worker-pi-transcript", renderBridgeWidget(state), { placement: "aboveEditor" });
648
+ }
649
+ function applyStatus(state, payload) {
650
+ const status = recordOf(payload.status) ?? payload;
651
+ state.streaming = status.isStreaming === true || status.isCompacting === true || status.isBashRunning === true;
652
+ state.cwd = typeof status.cwd === "string" ? status.cwd : state.cwd;
653
+ state.model = typeof status.model === "string" ? status.model : state.model;
654
+ const pending = typeof status.pendingMessageCount === "number" ? status.pendingMessageCount : 0;
655
+ state.status = `${state.streaming ? "streaming" : "idle"}${pending ? ` \xB7 ${pending} queued` : ""}`;
656
+ }
657
+ function applyMessage(state, message) {
658
+ const record = recordOf(message);
659
+ if (!record)
660
+ return;
661
+ const role = String(record.role ?? "system");
662
+ const label = role === "assistant" ? "Pi" : role === "user" ? "You" : role === "tool" || role === "toolResult" ? "Tool" : "System";
663
+ appendTranscript(state, label, textFromContent(record.content ?? record.message ?? record.text ?? ""));
664
+ }
665
+ function applyPiEvent(state, eventValue) {
666
+ const event = recordOf(eventValue);
667
+ if (!event)
668
+ return;
669
+ const type = String(event.type ?? "event");
670
+ if (type === "agent_start") {
671
+ state.streaming = true;
672
+ state.status = "streaming";
673
+ return;
674
+ }
675
+ if (type === "agent_end") {
676
+ state.streaming = false;
677
+ state.status = "idle";
678
+ appendTranscript(state, "System", "Agent turn complete.");
679
+ return;
680
+ }
681
+ if (type === "message_start" || type === "message_end" || type === "turn_end") {
682
+ applyMessage(state, event.message);
683
+ return;
684
+ }
685
+ if (type === "message_update") {
686
+ const assistantEvent = recordOf(event.assistantMessageEvent);
687
+ const delta = typeof assistantEvent?.delta === "string" ? assistantEvent.delta : typeof assistantEvent?.text === "string" ? assistantEvent.text : "";
688
+ if (delta)
689
+ appendTranscript(state, assistantEvent?.type === "thinking_delta" ? "Thinking" : "Pi", delta);
690
+ return;
691
+ }
692
+ if (type === "tool_execution_start") {
693
+ appendTranscript(state, "Tool", `${String(event.toolName ?? "tool")} ${asText(event.args ?? "")}`.trim());
694
+ return;
695
+ }
696
+ if (type === "tool_execution_update") {
697
+ appendTranscript(state, "Tool", asText(event.partialResult ?? ""));
698
+ return;
699
+ }
700
+ if (type === "tool_execution_end") {
701
+ appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.result ?? `${String(event.toolName ?? "tool")} complete`));
702
+ return;
703
+ }
704
+ if (type === "queue_update") {
705
+ const steering = Array.isArray(event.steering) ? event.steering.length : 0;
706
+ const followUp = Array.isArray(event.followUp) ? event.followUp.length : 0;
707
+ state.status = `queued \xB7 steer ${steering} \xB7 follow-up ${followUp}`;
708
+ }
709
+ }
710
+ function applyUiEvent(state, value) {
711
+ const event = recordOf(value);
712
+ if (!event)
713
+ return;
714
+ const type = String(event.type ?? "ui");
715
+ if (type === "shell.start")
716
+ appendTranscript(state, "Tool", `$ ${asText(event.command)}`);
717
+ else if (type === "shell.chunk")
718
+ appendTranscript(state, "Tool", asText(event.chunk));
719
+ else if (type === "shell.end")
720
+ appendTranscript(state, event.isError === true ? "Error" : "Tool", asText(event.output ?? `exit ${String(event.exitCode ?? "")}`));
721
+ else
722
+ appendTranscript(state, "System", `${type}: ${asText(event)}`);
723
+ }
724
+ function applyEnvelope(state, envelopeValue) {
725
+ const envelope = recordOf(envelopeValue);
726
+ if (!envelope)
727
+ return;
728
+ const type = String(envelope.type ?? "");
729
+ if (type === "ready") {
730
+ const metadata = recordOf(envelope.metadata);
731
+ state.cwd = typeof metadata?.cwd === "string" ? metadata.cwd : state.cwd;
732
+ state.status = "worker Pi daemon ready";
733
+ appendTranscript(state, "System", "Connected to worker Pi daemon.");
734
+ } else if (type === "status.update") {
735
+ applyStatus(state, envelope);
736
+ } else if (type === "activity.update") {
737
+ const activity = recordOf(envelope.activity);
738
+ state.activity = [activity?.label, activity?.detail].map(asText).filter(Boolean).join(" \u2014 ");
739
+ } else if (type === "extension_ui_request") {
740
+ state.pendingUi = parseExtensionUiRequest(envelope.request);
741
+ appendTranscript(state, "System", `Extension UI request: ${state.pendingUi.prompt}`);
742
+ } else if (type === "pi.ui_event") {
743
+ applyUiEvent(state, envelope.event);
744
+ } else if (type === "pi.event") {
745
+ applyPiEvent(state, envelope.event);
746
+ } else if (type === "error") {
747
+ appendTranscript(state, "Error", asText(envelope.message ?? envelope.detail ?? "unknown error"));
748
+ }
749
+ }
750
+ async function waitForWorkerReady(options, ctx, state) {
751
+ while (true) {
752
+ const session = await getRunPiSessionViaServer(options.context, options.runId).catch((error) => ({
753
+ ready: false,
754
+ status: error instanceof Error ? error.message : String(error),
755
+ retryAfterMs: 1000
756
+ }));
757
+ if (session.ready === false) {
758
+ const status = String(session.status ?? "starting");
759
+ state.status = `waiting for worker Pi daemon \xB7 ${status}`;
760
+ updatePiUi(ctx, state);
761
+ if (TERMINAL_RUN_STATUSES.has(status.toLowerCase())) {
762
+ appendTranscript(state, "Error", `Run ended before worker Pi daemon became ready: ${status}`);
763
+ return false;
764
+ }
765
+ await Bun.sleep(typeof session.retryAfterMs === "number" ? session.retryAfterMs : 750);
766
+ continue;
767
+ }
768
+ const sessionRecord = recordOf(session) ?? {};
769
+ applyEnvelope(state, { type: "ready", metadata: sessionRecord.metadata ?? sessionRecord });
770
+ updatePiUi(ctx, state);
771
+ return true;
772
+ }
773
+ }
774
+ function parseWsPayload(message) {
775
+ if (typeof message.data === "string")
776
+ return JSON.parse(message.data);
777
+ return JSON.parse(Buffer.from(message.data).toString("utf8"));
778
+ }
779
+ async function connectWorkerStream(options, ctx, state) {
780
+ const ready = await waitForWorkerReady(options, ctx, state);
781
+ if (!ready)
782
+ return;
783
+ let catchupDone = false;
784
+ const buffered = [];
785
+ const wsUrl = await buildRunPiEventsWebSocketUrl(options.context, options.runId);
786
+ const socket = new WebSocket(wsUrl);
787
+ const closePromise = new Promise((resolve3) => {
788
+ socket.onopen = () => {
789
+ state.wsConnected = true;
790
+ state.status = "live worker Pi WebSocket connected";
791
+ updatePiUi(ctx, state);
792
+ };
793
+ socket.onmessage = (message) => {
794
+ try {
795
+ const payload = parseWsPayload(message);
796
+ if (!catchupDone)
797
+ buffered.push(payload);
798
+ else {
799
+ applyEnvelope(state, payload);
800
+ updatePiUi(ctx, state);
801
+ }
802
+ } catch (error) {
803
+ appendTranscript(state, "Error", `Unparseable worker Pi event: ${error instanceof Error ? error.message : String(error)}`);
804
+ updatePiUi(ctx, state);
805
+ }
806
+ };
807
+ socket.onerror = () => socket.close();
808
+ socket.onclose = () => {
809
+ state.wsConnected = false;
810
+ state.status = "worker Pi WebSocket disconnected";
811
+ updatePiUi(ctx, state);
812
+ resolve3();
813
+ };
814
+ });
815
+ try {
816
+ const [messagesPayload, statusPayload, commandsPayload] = await Promise.all([
817
+ getRunPiMessagesViaServer(options.context, options.runId),
818
+ getRunPiStatusViaServer(options.context, options.runId),
819
+ getRunPiCommandsViaServer(options.context, options.runId)
820
+ ]);
821
+ const messages = Array.isArray(messagesPayload.messages) ? messagesPayload.messages : [];
822
+ for (const message of messages)
823
+ applyMessage(state, message);
824
+ applyStatus(state, statusPayload);
825
+ const commands = Array.isArray(commandsPayload.commands) ? commandsPayload.commands : [];
826
+ state.commands = commands.flatMap((command) => {
827
+ const record = recordOf(command);
828
+ return typeof record?.name === "string" ? [`/${record.name}`] : [];
829
+ });
830
+ catchupDone = true;
831
+ for (const payload of buffered.splice(0))
832
+ applyEnvelope(state, payload);
833
+ updatePiUi(ctx, state);
834
+ } catch (error) {
835
+ appendTranscript(state, "Error", `Worker Pi catch-up failed: ${error instanceof Error ? error.message : String(error)}`);
836
+ catchupDone = true;
837
+ updatePiUi(ctx, state);
838
+ }
839
+ await closePromise;
840
+ }
841
+ async function answerPendingUi(options, state, line) {
842
+ const pending = state.pendingUi;
843
+ if (!pending)
844
+ return false;
845
+ if (line === "/cancel") {
846
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { cancelled: true });
847
+ } else if (pending.method === "confirm") {
848
+ const confirmed = /^(y|yes|true|1)$/i.test(line);
849
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: confirmed, confirmed });
850
+ } else if (pending.options.length > 0 && /^\d+$/.test(line)) {
851
+ const selected = pending.options[Math.max(0, Number(line) - 1)] ?? line;
852
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: selected });
853
+ } else {
854
+ await respondRunPiExtensionUiViaServer(options.context, options.runId, pending.requestId, { value: line });
855
+ }
856
+ appendTranscript(state, "System", `Responded to extension UI request ${pending.requestId}.`);
857
+ state.pendingUi = null;
858
+ return true;
859
+ }
860
+ async function routeInput(options, ctx, state, line) {
861
+ const text = line.trim();
862
+ if (!text)
863
+ return;
864
+ if (await answerPendingUi(options, state, text)) {
865
+ updatePiUi(ctx, state);
866
+ return;
867
+ }
868
+ if (text === "/detach" || text === "/quit" || text === "/q") {
869
+ appendTranscript(state, "System", "Detached locally; worker Pi daemon continues.");
870
+ updatePiUi(ctx, state);
871
+ ctx.shutdown();
872
+ return;
873
+ }
874
+ if (text === "/stop") {
875
+ await abortRunPiViaServer(options.context, options.runId);
876
+ appendTranscript(state, "System", "Stop requested for worker Pi daemon.");
877
+ updatePiUi(ctx, state);
878
+ ctx.shutdown();
879
+ return;
880
+ }
881
+ if (text.startsWith("!")) {
882
+ appendTranscript(state, "You", text);
883
+ await sendRunPiShellViaServer(options.context, options.runId, text);
884
+ } else if (text.startsWith("/")) {
885
+ appendTranscript(state, "You", text);
886
+ const result = await runRunPiCommandViaServer(options.context, options.runId, text);
887
+ const message = typeof result.message === "string" ? result.message : "worker command accepted";
888
+ appendTranscript(state, "System", message);
889
+ } else {
890
+ appendTranscript(state, "You", text);
891
+ await sendRunPiPromptViaServer(options.context, options.runId, text, state.streaming ? "steer" : undefined);
892
+ }
893
+ updatePiUi(ctx, state);
894
+ }
895
+ function createRigWorkerPiBridgeExtension(options) {
896
+ return (pi) => {
897
+ const state = {
898
+ transcript: [],
899
+ status: "starting worker Pi daemon bridge",
900
+ activity: "",
901
+ cwd: "",
902
+ model: "",
903
+ commands: [],
904
+ streaming: false,
905
+ pendingUi: null,
906
+ wsConnected: false
907
+ };
908
+ if (options.initialMessageSent)
909
+ appendTranscript(state, "System", "Initial message sent to worker Pi daemon.");
910
+ pi.on("session_start", async (_event, ctx) => {
911
+ updatePiUi(ctx, state);
912
+ ctx.ui.notify("Rig worker Pi bridge extension loaded", "info");
913
+ ctx.ui.onTerminalInput((data) => {
914
+ if (data.includes("\x04")) {
915
+ ctx.shutdown();
916
+ return { consume: true };
917
+ }
918
+ if (!data.includes("\r") && !data.includes(`
919
+ `))
920
+ return;
921
+ const inlineText = data.replace(/[\r\n]+/g, "").trim();
922
+ const editorText = ctx.ui.getEditorText().trim();
923
+ const text = [editorText, inlineText].filter(Boolean).join(" ").trim();
924
+ if (!text)
925
+ return;
926
+ ctx.ui.setEditorText("");
927
+ routeInput(options, ctx, state, text).catch((error) => {
928
+ appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
929
+ updatePiUi(ctx, state);
930
+ });
931
+ return { consume: true };
932
+ });
933
+ connectWorkerStream(options, ctx, state).catch((error) => {
934
+ appendTranscript(state, "Error", error instanceof Error ? error.message : String(error));
935
+ updatePiUi(ctx, state);
936
+ });
937
+ });
938
+ pi.on("session_shutdown", () => {});
939
+ };
940
+ }
941
+
942
+ // packages/cli/src/commands/_pi-frontend.ts
943
+ function setTemporaryEnv(updates) {
944
+ const previous = new Map;
945
+ for (const [key, value] of Object.entries(updates)) {
946
+ previous.set(key, process.env[key]);
947
+ process.env[key] = value;
948
+ }
949
+ return () => {
950
+ for (const [key, value] of previous) {
951
+ if (value === undefined)
952
+ delete process.env[key];
953
+ else
954
+ process.env[key] = value;
955
+ }
956
+ };
957
+ }
958
+ async function attachRunBundledPiFrontend(context, input) {
959
+ const tempRoot = mkdtempSync(join(tmpdir(), "rig-pi-frontend-"));
960
+ const cwd = join(tempRoot, "workspace");
961
+ const agentDir = join(tempRoot, "agent");
962
+ const sessionDir = join(tempRoot, "sessions");
963
+ const previousCwd = process.cwd();
964
+ const restoreEnv = setTemporaryEnv({
965
+ PI_CODING_AGENT_DIR: agentDir,
966
+ PI_CODING_AGENT_SESSION_DIR: sessionDir,
967
+ PI_OFFLINE: "1",
968
+ PI_SKIP_VERSION_CHECK: "1"
969
+ });
970
+ let detached = false;
971
+ try {
972
+ await Bun.$`mkdir -p ${cwd} ${agentDir} ${sessionDir}`.quiet();
973
+ process.chdir(cwd);
974
+ await runPiMain([
975
+ "--offline",
976
+ "--no-session",
977
+ "--no-tools",
978
+ "--no-builtin-tools",
979
+ "--no-skills",
980
+ "--no-prompt-templates",
981
+ "--no-themes",
982
+ "--no-context-files",
983
+ "--no-approve"
984
+ ], {
985
+ extensionFactories: [
986
+ createRigWorkerPiBridgeExtension({
987
+ context,
988
+ runId: input.runId,
989
+ initialMessageSent: input.steered === true
990
+ })
991
+ ]
992
+ });
993
+ detached = true;
994
+ } finally {
995
+ process.chdir(previousCwd);
996
+ restoreEnv();
997
+ rmSync(tempRoot, { recursive: true, force: true });
998
+ }
999
+ let run = { runId: input.runId, status: "unknown" };
1000
+ try {
1001
+ run = await getRunDetailsViaServer(context, input.runId);
1002
+ } catch {}
1003
+ return {
1004
+ run,
1005
+ logs: [],
1006
+ timeline: [],
1007
+ timelineCursor: null,
1008
+ steered: input.steered === true,
1009
+ detached,
1010
+ rendered: "actual bundled Pi frontend hosted Rig worker Pi bridge extension"
1011
+ };
1012
+ }
1013
+
1014
+ // packages/cli/src/commands/_operator-view.ts
1015
+ var TERMINAL_RUN_STATUSES2 = new Set(["completed", "failed", "stopped", "cancelled", "canceled", "closed", "merged", "needs_attention", "needs-attention"]);
490
1016
  function runStatusFromPayload(payload) {
491
1017
  const run = payload.run && typeof payload.run === "object" && !Array.isArray(payload.run) ? payload.run : payload;
492
1018
  return String(run.status ?? "unknown").toLowerCase();
@@ -528,9 +1054,15 @@ async function readOperatorSnapshot(context, runId, options = {}) {
528
1054
  async function attachRunOperatorView(context, input) {
529
1055
  let steered = false;
530
1056
  if (input.message?.trim()) {
531
- await steerRunViaServer(context, input.runId, input.message.trim());
1057
+ await sendRunPiPromptViaServer(context, input.runId, input.message.trim(), "steer").catch(() => steerRunViaServer(context, input.runId, input.message.trim()));
532
1058
  steered = true;
533
1059
  }
1060
+ if (input.follow && !input.once && input.interactive !== false && context.outputMode === "text" && Boolean(process.stdin.isTTY && process.stdout.isTTY)) {
1061
+ return attachRunBundledPiFrontend(context, {
1062
+ runId: input.runId,
1063
+ steered
1064
+ });
1065
+ }
534
1066
  const surface = createOperatorSurface({ interactive: input.interactive !== false });
535
1067
  let snapshot = await readOperatorSnapshot(context, input.runId);
536
1068
  if (context.outputMode === "text") {
@@ -538,7 +1070,7 @@ async function attachRunOperatorView(context, input) {
538
1070
  surface.renderTimeline(snapshot.timeline);
539
1071
  surface.renderLogs(snapshot.logs);
540
1072
  if (steered)
541
- surface.info("Steering message queued.");
1073
+ surface.info("Message submitted to worker Pi.");
542
1074
  }
543
1075
  let detached = false;
544
1076
  let commandInput = null;
@@ -557,7 +1089,7 @@ async function attachRunOperatorView(context, input) {
557
1089
  }
558
1090
  const pollMs = Math.max(250, Math.trunc(input.pollMs ?? 2000));
559
1091
  let timelineCursor = snapshot.timelineCursor;
560
- while (!detached && !TERMINAL_RUN_STATUSES.has(runStatusFromPayload(snapshot.run))) {
1092
+ while (!detached && !TERMINAL_RUN_STATUSES2.has(runStatusFromPayload(snapshot.run))) {
561
1093
  await Bun.sleep(pollMs);
562
1094
  snapshot = await readOperatorSnapshot(context, input.runId, { timelineCursor });
563
1095
  timelineCursor = snapshot.timelineCursor;
@@ -622,105 +1154,6 @@ function formatRunList(runs, options = {}) {
622
1154
  `);
623
1155
  }
624
1156
 
625
- // packages/cli/src/commands/_pi-session.ts
626
- import { spawn } from "child_process";
627
-
628
- // packages/cli/src/commands/_pi-install.ts
629
- import { existsSync as existsSync3, readFileSync as readFileSync3, rmSync } from "fs";
630
- import { resolve as resolve3 } from "path";
631
- var PI_RIG_PACKAGE_NAME = "@h-rig/pi-rig";
632
- function resolvePiRigPackageSource(projectRoot, exists = existsSync3) {
633
- const localPackage = resolve3(projectRoot, "packages", "pi-rig");
634
- if (exists(resolve3(localPackage, "package.json")))
635
- return localPackage;
636
- return `npm:${PI_RIG_PACKAGE_NAME}`;
637
- }
638
-
639
- // packages/cli/src/commands/_pi-session.ts
640
- function buildPiRigSessionEnv(input) {
641
- return {
642
- RIG_PROJECT_ROOT: input.projectRoot,
643
- PROJECT_RIG_ROOT: input.projectRoot,
644
- RIG_RUN_ID: input.runId,
645
- RIG_SERVER_RUN_ID: input.runId,
646
- RIG_RUNTIME_ADAPTER: "pi",
647
- RIG_SERVER_URL: input.serverUrl,
648
- RIG_SERVER_BASE_URL: input.serverUrl,
649
- RIG_STEERING_POLL_MS: process.env.RIG_STEERING_POLL_MS?.trim() || "1000",
650
- RIG_PI_OPERATOR_SESSION: "1",
651
- ...input.taskId ? { RIG_TASK_ID: input.taskId } : {},
652
- ...input.authToken ? { RIG_AUTH_TOKEN: input.authToken } : {}
653
- };
654
- }
655
- function shellBinary(name) {
656
- const explicit = process.env.RIG_PI_BINARY?.trim();
657
- if (explicit)
658
- return explicit;
659
- return Bun.which(name) || name;
660
- }
661
- function buildPiRigSessionCommand(input) {
662
- const configuredExtension = input.extensionSource ?? process.env.RIG_PI_RIG_EXTENSION_SOURCE?.trim();
663
- const extensionSource = configuredExtension && configuredExtension.length > 0 ? configuredExtension : resolvePiRigPackageSource(input.projectRoot);
664
- const initialCommand = `/rig attach ${input.runId}`;
665
- return [
666
- shellBinary("pi"),
667
- "--no-extensions",
668
- "--extension",
669
- extensionSource,
670
- initialCommand
671
- ];
672
- }
673
- async function launchPiRigSession(context, input) {
674
- if (context.outputMode !== "text" || !process.stdin.isTTY || !process.stdout.isTTY) {
675
- return { launched: false, exitCode: null, command: [] };
676
- }
677
- if (process.env.RIG_DISABLE_PI_LAUNCH === "1") {
678
- return { launched: false, exitCode: null, command: [] };
679
- }
680
- const server = await ensureServerForCli(context.projectRoot);
681
- const command = buildPiRigSessionCommand({ ...input, projectRoot: context.projectRoot });
682
- const env = {
683
- ...process.env,
684
- ...buildPiRigSessionEnv({
685
- projectRoot: context.projectRoot,
686
- runId: input.runId,
687
- taskId: input.taskId,
688
- serverUrl: server.baseUrl,
689
- authToken: server.authToken
690
- })
691
- };
692
- process.stdout.write(`Launching Pi for Rig run ${input.runId}\u2026
693
- `);
694
- process.stdout.write(`Pi command: ${formatCommand(command)}
695
- `);
696
- const launchedAt = Date.now();
697
- const child = spawn(command[0], command.slice(1), {
698
- cwd: context.projectRoot,
699
- env,
700
- stdio: "inherit"
701
- });
702
- const launchError = await new Promise((resolve4) => {
703
- child.once("error", (error) => {
704
- resolve4({ error: error.message });
705
- });
706
- child.once("close", (code) => resolve4({ code }));
707
- });
708
- if ("error" in launchError) {
709
- process.stderr.write(`Failed to launch Pi; falling back to Rig attach view: ${launchError.error}
710
- `);
711
- return { launched: false, exitCode: null, command, error: launchError.error };
712
- }
713
- const exitCode = launchError.code;
714
- const elapsedMs = Date.now() - launchedAt;
715
- if (typeof exitCode === "number" && exitCode !== 0 && elapsedMs < 5000) {
716
- const error = `Pi exited during startup with code ${exitCode}.`;
717
- process.stderr.write(`${error} Falling back to Rig attach view.
718
- `);
719
- return { launched: false, exitCode, command, error };
720
- }
721
- return { launched: true, exitCode, command };
722
- }
723
-
724
1157
  // packages/cli/src/commands/run.ts
725
1158
  function normalizeRemoteRunDetails(payload) {
726
1159
  const run = payload.run;
@@ -945,20 +1378,13 @@ async function executeRun(context, args) {
945
1378
  throw new CliError2("run attach requires a run id.", 2);
946
1379
  }
947
1380
  let steered = false;
948
- const shouldTryPiAttach = context.outputMode === "text" && follow.value && !once.value && Boolean(process.stdin.isTTY && process.stdout.isTTY) && process.env.RIG_DISABLE_PI_LAUNCH !== "1";
949
- if (shouldTryPiAttach && messageOption.value?.trim()) {
1381
+ if (messageOption.value?.trim()) {
950
1382
  await steerRunViaServer(context, runId, messageOption.value.trim());
951
1383
  steered = true;
952
1384
  }
953
- if (shouldTryPiAttach) {
954
- const piSession = await launchPiRigSession(context, { runId });
955
- if (piSession.launched) {
956
- return { ok: true, group: "run", command, details: { runId, steered, mode: "pi", ...piSession } };
957
- }
958
- }
959
1385
  const attached = await attachRunOperatorView(context, {
960
1386
  runId,
961
- message: shouldTryPiAttach ? null : messageOption.value ?? null,
1387
+ message: null,
962
1388
  once: once.value,
963
1389
  follow: follow.value,
964
1390
  pollMs: parsePositiveInt(pollMs.value, "--poll-ms", 2000)