@xevy/cli 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -149,6 +149,11 @@ xevy dev cb49c02e-66d8-4475-95a8-6f3930643ec8
149
149
  xevy dev VI22-07 --base https://xevy.example.com
150
150
  ```
151
151
 
152
+ The command stays attached across normal Pi exits and transient terminal
153
+ disconnects. It shows startup/restart status while the remote terminal is being
154
+ prepared; press `Ctrl+]` to detach intentionally and leave the Dev Session
155
+ running.
156
+
152
157
  The CLI resolves an exact task ID or task code, attaches to its existing Dev
153
158
  Session, or starts one from a linked repository. It then renders Pi's native TUI
154
159
  from the isolated remote workspace: normal composer, thinking, tools, history,
@@ -13,6 +13,10 @@ const HEARTBEAT_INTERVAL_MS = 60_000;
13
13
  const SESSION_POLL_INTERVAL_MS = 2_000;
14
14
  const SESSION_START_TIMEOUT_MS = 10 * 60_000;
15
15
  const EXECUTION_RECONNECT_LIMIT = 8;
16
+ const TERMINAL_RECONNECT_LIMIT = 8;
17
+ const TERMINAL_RECONNECT_MAX_DELAY_MS = 5_000;
18
+ const TERMINAL_STABLE_RUNTIME_MS = 10_000;
19
+ const TERMINAL_SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
16
20
 
17
21
  export function parseCredentialsText(source) {
18
22
  const values = {};
@@ -538,6 +542,64 @@ async function waitForReady({ api, sessionPath, initialSession, writeLine, sleep
538
542
  return session;
539
543
  }
540
544
 
545
+ /** A deliberately restrained terminal status indicator. It animates only on
546
+ * the real interactive stdout; redirected output, CI, and tests receive stable
547
+ * newline-delimited status messages. */
548
+ export function createTerminalStatus({ output, writeLine, animate }) {
549
+ const shouldAnimate = animate ?? Boolean(
550
+ output === process.stdout
551
+ && output.isTTY
552
+ && !process.env.CI
553
+ && process.env.TERM !== "dumb",
554
+ );
555
+ let label = "";
556
+ let previousStaticLabel = "";
557
+ let frame = 0;
558
+ let timer = null;
559
+ let painted = false;
560
+
561
+ const render = () => {
562
+ if (!label) return;
563
+ painted = true;
564
+ output.write(`\r\x1b[2K${TERMINAL_SPINNER_FRAMES[frame % TERMINAL_SPINNER_FRAMES.length]} ${label}`);
565
+ frame += 1;
566
+ };
567
+ const clear = () => {
568
+ if (timer) clearInterval(timer);
569
+ timer = null;
570
+ if (painted) output.write("\r\x1b[2K");
571
+ painted = false;
572
+ };
573
+
574
+ return {
575
+ update(nextLabel) {
576
+ label = String(nextLabel || "");
577
+ if (!label) return;
578
+ if (!shouldAnimate) {
579
+ if (label !== previousStaticLabel) writeLine(label);
580
+ previousStaticLabel = label;
581
+ return;
582
+ }
583
+ if (!timer) {
584
+ render();
585
+ timer = setInterval(render, 80);
586
+ timer.unref?.();
587
+ }
588
+ },
589
+ succeed(finalLabel) {
590
+ clear();
591
+ label = "";
592
+ previousStaticLabel = "";
593
+ if (finalLabel) writeLine(`✓ ${finalLabel}`);
594
+ },
595
+ stop() {
596
+ clear();
597
+ label = "";
598
+ previousStaticLabel = "";
599
+ },
600
+ };
601
+ }
602
+
541
603
  function sessionSummary(task, session, writeLine) {
542
604
  const url = canonicalSessionUrl(session.hostname);
543
605
  writeLine("\nDev Session");
@@ -549,13 +611,22 @@ function sessionSummary(task, session, writeLine) {
549
611
  writeLine("\nNative Pi terminal · press Ctrl+] to detach and leave it running\n");
550
612
  }
551
613
 
552
- async function attachNativeTerminal({ api, base, sessionPath, input, output, signal, WebSocketImpl = WebSocket }) {
614
+ async function attachNativeTerminal({
615
+ api,
616
+ base,
617
+ sessionPath,
618
+ input,
619
+ output,
620
+ signal,
621
+ onReady,
622
+ WebSocketImpl = WebSocket,
623
+ }) {
553
624
  const ticket = await api.request(`${sessionPath}/terminal-ticket`, { method: "POST", body: {}, signal });
554
625
  if (!ticket?.ticket || !ticket?.terminalPath) throw new Error("Xevy did not return a terminal ticket.");
555
626
  const url = new URL(ticket.terminalPath, `${base}/`);
556
627
  url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
557
628
 
558
- await new Promise((resolve, reject) => {
629
+ return new Promise((resolve, reject) => {
559
630
  const socket = new WebSocketImpl(url, {
560
631
  headers: { "X-Xevy-Terminal-Ticket": ticket.ticket },
561
632
  maxPayload: 2 * 1024 * 1024,
@@ -563,12 +634,16 @@ async function attachNativeTerminal({ api, base, sessionPath, input, output, sig
563
634
  let ready = false;
564
635
  let settled = false;
565
636
  let rawEnabled = false;
637
+ let detachedByUser = false;
638
+ const connectedAt = Date.now();
639
+ const pendingOutput = [];
566
640
  const onAbort = () => {
641
+ detachedByUser = true;
567
642
  if (socket.readyState === WebSocketImpl.OPEN) socket.close(1000, "client_detach");
568
643
  else socket.terminate?.();
569
- finish(new Error("Detached."));
644
+ finish(null, { reason: "detached", code: 1000, durationMs: Date.now() - connectedAt });
570
645
  };
571
- const finish = (error) => {
646
+ const finish = (error, result) => {
572
647
  if (settled) return;
573
648
  settled = true;
574
649
  input.off("data", onInput);
@@ -576,7 +651,7 @@ async function attachNativeTerminal({ api, base, sessionPath, input, output, sig
576
651
  signal?.removeEventListener("abort", onAbort);
577
652
  if (rawEnabled && typeof input.setRawMode === "function") input.setRawMode(false);
578
653
  if (error) reject(error);
579
- else resolve();
654
+ else resolve(result);
580
655
  };
581
656
  const sendResize = () => {
582
657
  if (socket.readyState !== WebSocketImpl.OPEN) return;
@@ -591,12 +666,13 @@ async function attachNativeTerminal({ api, base, sessionPath, input, output, sig
591
666
  const value = Buffer.from(chunk);
592
667
  const detachAt = value.indexOf(0x1d);
593
668
  if (detachAt >= 0) {
669
+ detachedByUser = true;
594
670
  if (detachAt > 0 && socket.readyState === WebSocketImpl.OPEN) {
595
671
  socket.send(value.subarray(0, detachAt), { binary: true });
596
672
  }
597
673
  output.write("\r\nDetached. Pi and the Dev Session are still running.\r\n");
598
674
  socket.close(1000, "client_detach");
599
- finish();
675
+ finish(null, { reason: "detached", code: 1000, durationMs: Date.now() - connectedAt });
600
676
  return;
601
677
  }
602
678
  if (socket.readyState === WebSocketImpl.OPEN) socket.send(value, { binary: true });
@@ -610,13 +686,17 @@ async function attachNativeTerminal({ api, base, sessionPath, input, output, sig
610
686
 
611
687
  socket.on("message", (data, isBinary) => {
612
688
  if (isBinary) {
613
- output.write(Buffer.from(data));
689
+ const chunk = Buffer.from(data);
690
+ if (ready) output.write(chunk);
691
+ else pendingOutput.push(chunk);
614
692
  return;
615
693
  }
616
694
  let control;
617
695
  try { control = JSON.parse(data.toString()); } catch { return; }
618
696
  if (control.type === "ready" && !ready) {
619
697
  ready = true;
698
+ onReady?.();
699
+ for (const chunk of pendingOutput.splice(0)) output.write(chunk);
620
700
  if (typeof input.setRawMode === "function") {
621
701
  input.setRawMode(true);
622
702
  rawEnabled = true;
@@ -635,12 +715,85 @@ async function attachNativeTerminal({ api, base, sessionPath, input, output, sig
635
715
  socket.once("error", (error) => finish(new Error(`Dev Session terminal connection failed: ${error.message}`)));
636
716
  socket.once("close", (code) => {
637
717
  if (!ready && !settled) finish(new Error(`Dev Session terminal closed before it was ready (code ${code}).`));
638
- else if (ready && code !== 1000 && !settled) finish(new Error("Dev Session terminal connection was lost. Rerun `xevy dev` to reattach."));
639
- else finish();
718
+ else if (!settled) finish(null, {
719
+ reason: detachedByUser ? "detached" : code === 1000 ? "terminal-exit" : "transport-lost",
720
+ code,
721
+ durationMs: Date.now() - connectedAt,
722
+ });
640
723
  });
641
724
  });
642
725
  }
643
726
 
727
+ async function attachNativeTerminalPersistently({
728
+ api,
729
+ base,
730
+ sessionPath,
731
+ input,
732
+ output,
733
+ signal,
734
+ sleep,
735
+ writeLine,
736
+ WebSocketImpl,
737
+ animateStatus,
738
+ }) {
739
+ const status = createTerminalStatus({ output, writeLine, animate: animateStatus });
740
+ let reconnecting = false;
741
+ let consecutiveFailures = 0;
742
+ try {
743
+ while (!signal?.aborted) {
744
+ status.update(reconnecting ? "Restarting remote Pi terminal…" : "Starting remote Pi terminal…");
745
+ try {
746
+ const result = await attachNativeTerminal({
747
+ api,
748
+ base,
749
+ sessionPath,
750
+ input,
751
+ output,
752
+ signal,
753
+ onReady: () => status.succeed(reconnecting ? "Pi terminal restarted" : "Pi terminal ready"),
754
+ WebSocketImpl,
755
+ });
756
+ if (result?.reason === "detached") return;
757
+ reconnecting = true;
758
+ consecutiveFailures = result?.durationMs >= TERMINAL_STABLE_RUNTIME_MS
759
+ ? 0
760
+ : consecutiveFailures + 1;
761
+ status.update(result?.reason === "transport-lost"
762
+ ? "Terminal connection lost · reconnecting…"
763
+ : "Pi exited · restarting terminal…");
764
+ } catch (error) {
765
+ if (signal?.aborted) throw error;
766
+ if (error instanceof DevSessionApiError && ![409, 502, 503, 504].includes(error.status)) throw error;
767
+ reconnecting = true;
768
+ consecutiveFailures += 1;
769
+ status.update("Terminal is not ready yet · retrying…");
770
+ }
771
+
772
+ if (consecutiveFailures >= TERMINAL_RECONNECT_LIMIT) {
773
+ throw new Error("The remote Pi terminal repeatedly exited before stabilizing. The Dev Session remains active; check its logs and rerun `xevy dev` after correcting the startup error.");
774
+ }
775
+ await sleep(
776
+ Math.min(500 * 2 ** Math.max(0, consecutiveFailures - 1), TERMINAL_RECONNECT_MAX_DELAY_MS),
777
+ signal,
778
+ );
779
+ let current = null;
780
+ try {
781
+ current = await api.request(`${sessionPath}/`, { signal });
782
+ } catch (error) {
783
+ if (signal?.aborted) throw error;
784
+ if (error instanceof DevSessionApiError && [401, 403, 404].includes(error.status)) throw error;
785
+ status.update("Session status unavailable · retrying terminal…");
786
+ }
787
+ if (current && !current.session) throw new Error("The Dev Session disappeared while reconnecting the terminal.");
788
+ if (current?.session && SESSION_TERMINAL_STATUSES.has(current.session.status)) {
789
+ throw new Error(current.session.error || `Dev Session ${current.session.status}.`);
790
+ }
791
+ }
792
+ } finally {
793
+ status.stop();
794
+ }
795
+ }
796
+
644
797
  function commandHelp(writeLine) {
645
798
  writeLine("Commands:");
646
799
  writeLine(" /status Refresh session status");
@@ -843,14 +996,17 @@ export async function runDevSession(options) {
843
996
  const nativeTerminal = options.nativeTerminal
844
997
  ?? (input === process.stdin && output === process.stdout);
845
998
  if (interactive && nativeTerminal) {
846
- await attachNativeTerminal({
999
+ await attachNativeTerminalPersistently({
847
1000
  api,
848
1001
  base,
849
1002
  sessionPath,
850
1003
  input,
851
1004
  output,
852
1005
  signal: detachController.signal,
1006
+ sleep,
1007
+ writeLine,
853
1008
  WebSocketImpl: options.webSocketImpl || WebSocket,
1009
+ animateStatus: options.animateStatus,
854
1010
  });
855
1011
  detached = true;
856
1012
  return { task, session, detached: true };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xevy/cli",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "Xevy CLI — connect to remote Dev Sessions, configure credentials, pi, coding-agent skills, and MCP, and keep integrations current.",
5
5
  "type": "module",
6
6
  "keywords": [