ragent-cli 1.6.2 → 1.7.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 (2) hide show
  1. package/dist/index.js +81 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var require_package = __commonJS({
31
31
  "package.json"(exports2, module2) {
32
32
  module2.exports = {
33
33
  name: "ragent-cli",
34
- version: "1.6.2",
34
+ version: "1.7.0",
35
35
  description: "CLI agent for rAgent Live \u2014 browser-first terminal control plane for AI coding agents",
36
36
  main: "dist/index.js",
37
37
  bin: {
@@ -682,6 +682,43 @@ function decodeJwtExp(token) {
682
682
  return null;
683
683
  }
684
684
  }
685
+ async function startSessionWithMachineSecret(params) {
686
+ console.log("[rAgent] Attempting session recovery with machine credential...");
687
+ const response = await fetch(`${params.portal}/api/agent/session/start`, {
688
+ method: "POST",
689
+ headers: { "Content-Type": "application/json" },
690
+ body: JSON.stringify({
691
+ hostId: params.hostId,
692
+ machineSecret: params.machineSecret
693
+ })
694
+ });
695
+ if (response.status === 401 || response.status === 403) {
696
+ throw new AuthError(
697
+ "Machine credential rejected \u2014 agent may be revoked. Re-connect with: ragent connect --token <token>"
698
+ );
699
+ }
700
+ if (!response.ok) {
701
+ const data2 = await response.json().catch(() => ({}));
702
+ throw new Error(
703
+ `Session start failed: ${data2.error || response.status}`
704
+ );
705
+ }
706
+ const data = await response.json();
707
+ if (!data.agentToken) {
708
+ throw new Error("Session start response missing agentToken");
709
+ }
710
+ const patch = {
711
+ agentToken: data.agentToken,
712
+ tokenExpiresAt: data.expiresAt || ""
713
+ };
714
+ if (data.refreshToken) {
715
+ patch.refreshToken = data.refreshToken;
716
+ patch.refreshExpiresAt = data.refreshExpiresAt || "";
717
+ }
718
+ saveConfigPatch(patch);
719
+ console.log("[rAgent] Session recovered via machine credential.");
720
+ return data.agentToken;
721
+ }
685
722
  async function refreshTokenIfNeeded(params) {
686
723
  const config = loadConfig();
687
724
  const refreshToken = config.refreshToken;
@@ -707,10 +744,23 @@ async function refreshTokenIfNeeded(params) {
707
744
  body: JSON.stringify(body)
708
745
  });
709
746
  if (!response.ok) {
710
- const data2 = await response.json().catch(() => ({}));
747
+ const errorData = await response.json().catch(() => ({}));
711
748
  console.warn(
712
- `[rAgent] Token refresh failed: ${data2.error || response.status}`
749
+ `[rAgent] Token refresh failed: ${errorData.error || response.status}`
713
750
  );
751
+ if (config.machineSecret && config.hostId) {
752
+ try {
753
+ return await startSessionWithMachineSecret({
754
+ portal: params.portal,
755
+ hostId: config.hostId,
756
+ machineSecret: config.machineSecret
757
+ });
758
+ } catch (mcError) {
759
+ if (mcError instanceof AuthError) throw mcError;
760
+ const mcMessage = mcError instanceof Error ? mcError.message : String(mcError);
761
+ console.warn(`[rAgent] Machine credential recovery failed: ${mcMessage}`);
762
+ }
763
+ }
714
764
  return params.agentToken;
715
765
  }
716
766
  const data = await response.json();
@@ -723,10 +773,14 @@ async function refreshTokenIfNeeded(params) {
723
773
  patch.refreshToken = data.refreshToken;
724
774
  patch.refreshExpiresAt = data.refreshExpiresAt || "";
725
775
  }
776
+ if (data.machineSecret) {
777
+ patch.machineSecret = data.machineSecret;
778
+ }
726
779
  saveConfigPatch(patch);
727
780
  console.log("[rAgent] Token refreshed successfully.");
728
781
  return data.agentToken;
729
782
  } catch (error) {
783
+ if (error instanceof AuthError) throw error;
730
784
  const message = error instanceof Error ? error.message : String(error);
731
785
  console.warn(`[rAgent] Token refresh error: ${message}`);
732
786
  return params.agentToken;
@@ -753,6 +807,9 @@ async function claimHost(params) {
753
807
  if (!data.agentToken) {
754
808
  throw new Error("Missing connector token in claim response");
755
809
  }
810
+ if (data.machineSecret) {
811
+ saveConfigPatch({ machineSecret: data.machineSecret });
812
+ }
756
813
  return data;
757
814
  }
758
815
  async function negotiateAgent(params) {
@@ -3667,6 +3724,27 @@ async function runAgent(rawOptions) {
3667
3724
  });
3668
3725
  } catch (error) {
3669
3726
  if (error instanceof AuthError) {
3727
+ const cfg = loadConfig();
3728
+ if (cfg.machineSecret && cfg.hostId) {
3729
+ try {
3730
+ options.agentToken = await startSessionWithMachineSecret({
3731
+ portal: options.portal,
3732
+ hostId: cfg.hostId,
3733
+ machineSecret: cfg.machineSecret
3734
+ });
3735
+ inventory.updateOptions(options);
3736
+ dispatcher.updateOptions(options);
3737
+ console.log("[rAgent] Recovered from auth failure via machine credential. Reconnecting...");
3738
+ continue;
3739
+ } catch (mcError) {
3740
+ if (mcError instanceof AuthError) {
3741
+ console.error(`[rAgent] ${mcError.message}`);
3742
+ } else {
3743
+ const mcMsg = mcError instanceof Error ? mcError.message : String(mcError);
3744
+ console.error(`[rAgent] Machine credential recovery failed: ${mcMsg}`);
3745
+ }
3746
+ }
3747
+ }
3670
3748
  console.error(`[rAgent] ${error.message}`);
3671
3749
  console.error(
3672
3750
  "[rAgent] Connector token is invalid or revoked. Stopping. Re-connect with: ragent connect --token <token>"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ragent-cli",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "description": "CLI agent for rAgent Live — browser-first terminal control plane for AI coding agents",
5
5
  "main": "dist/index.js",
6
6
  "bin": {