@moxt-ai/cli 0.8.0-beta.3 → 0.8.0-beta.4

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/dist/index.cjs CHANGED
@@ -4855,7 +4855,7 @@ function toRelayEvent(event) {
4855
4855
  function normalizeOrigin(relayOrigin) {
4856
4856
  return relayOrigin.replace(/\/+$/, "");
4857
4857
  }
4858
- var requestTimeoutMilliseconds, RelayHttpError, PairingRelayClient, MachineRelayClient;
4858
+ var requestTimeoutMilliseconds, RelayHttpError, MachineConnectionRevokedError, PairingRelayClient, MachineRelayClient;
4859
4859
  var init_relay_client = __esm({
4860
4860
  "../machine-connector/dist/runtime/relay-client.js"() {
4861
4861
  "use strict";
@@ -4872,6 +4872,14 @@ var init_relay_client = __esm({
4872
4872
  this.responseBody = responseBody;
4873
4873
  }
4874
4874
  };
4875
+ MachineConnectionRevokedError = class extends Error {
4876
+ connectionId;
4877
+ constructor(connectionId) {
4878
+ super(`Machine Relay rejected the refresh credential for connection ${connectionId}`);
4879
+ this.name = "MachineConnectionRevokedError";
4880
+ this.connectionId = connectionId;
4881
+ }
4882
+ };
4875
4883
  PairingRelayClient = class {
4876
4884
  relayOrigin;
4877
4885
  constructor(relayOrigin) {
@@ -4887,10 +4895,16 @@ var init_relay_client = __esm({
4887
4895
  MachineRelayClient = class {
4888
4896
  connection;
4889
4897
  store;
4898
+ hooks;
4890
4899
  refreshState = {};
4891
- constructor(connection, store) {
4900
+ revoked = false;
4901
+ constructor(connection, store, hooks = {}) {
4892
4902
  this.connection = connection;
4893
4903
  this.store = store;
4904
+ this.hooks = hooks;
4905
+ }
4906
+ isRevoked() {
4907
+ return this.revoked;
4894
4908
  }
4895
4909
  async heartbeat(request2) {
4896
4910
  await this.authenticatedPost("heartbeat", request2);
@@ -4924,6 +4938,9 @@ var init_relay_client = __esm({
4924
4938
  await this.authenticatedPost("controls/acknowledge", request2);
4925
4939
  }
4926
4940
  async authenticatedPost(path3, body) {
4941
+ if (this.revoked) {
4942
+ throw new MachineConnectionRevokedError(this.connection.connectionId);
4943
+ }
4927
4944
  const url = `${normalizeOrigin(this.connection.relayOrigin)}/machine-relay/v1/${path3}`;
4928
4945
  try {
4929
4946
  return await postJson(url, body, this.connection.accessToken);
@@ -4944,7 +4961,18 @@ var init_relay_client = __esm({
4944
4961
  await this.refreshState.promise;
4945
4962
  }
4946
4963
  async refreshAccessToken() {
4947
- const response = await postJson(`${normalizeOrigin(this.connection.relayOrigin)}/machine-relay/v1/pairing/refresh`, { refreshToken: this.connection.refreshToken });
4964
+ let response;
4965
+ try {
4966
+ response = await postJson(`${normalizeOrigin(this.connection.relayOrigin)}/machine-relay/v1/pairing/refresh`, { refreshToken: this.connection.refreshToken });
4967
+ } catch (error) {
4968
+ if (!(error instanceof RelayHttpError) || error.status !== 401) {
4969
+ throw error;
4970
+ }
4971
+ const revokedError = new MachineConnectionRevokedError(this.connection.connectionId);
4972
+ this.revoked = true;
4973
+ await this.hooks.onConnectionRevoked?.(revokedError);
4974
+ throw revokedError;
4975
+ }
4948
4976
  this.connection = {
4949
4977
  ...this.connection,
4950
4978
  accessToken: response.accessToken,
@@ -5179,7 +5207,7 @@ var init_store = __esm({
5179
5207
 
5180
5208
  // ../machine-connector/dist/runtime/daemon.js
5181
5209
  function isPermanentRelayFailure(error) {
5182
- return error instanceof RelayHttpError && (error.status === 400 || error.status === 401 || error.status === 403 || error.status === 404 || error.status === 409 || error.status === 410);
5210
+ return error instanceof MachineConnectionRevokedError || error instanceof RelayHttpError && (error.status === 400 || error.status === 401 || error.status === 403 || error.status === 404 || error.status === 409 || error.status === 410);
5183
5211
  }
5184
5212
  async function withDeadline(operation, deadline) {
5185
5213
  if (deadline === void 0) {
@@ -5247,6 +5275,7 @@ var init_daemon = __esm({
5247
5275
  connectorInstanceId = (0, import_node_crypto4.randomUUID)();
5248
5276
  executionTasks = /* @__PURE__ */ new Set();
5249
5277
  logger;
5278
+ relayClients = /* @__PURE__ */ new Map();
5250
5279
  startedAt = (/* @__PURE__ */ new Date()).toISOString();
5251
5280
  connections = [];
5252
5281
  ipcServer;
@@ -5320,15 +5349,43 @@ var init_daemon = __esm({
5320
5349
  };
5321
5350
  }
5322
5351
  async runConnection(connection) {
5323
- const relay = new MachineRelayClient(connection, this.store);
5352
+ const relay = this.relayForConnection(connection);
5324
5353
  await Promise.all([
5325
5354
  this.runHeartbeatLoop(connection, relay),
5326
5355
  this.runClaimLoop(connection, relay),
5327
5356
  this.runControlLoop(connection, relay)
5328
5357
  ]);
5329
5358
  }
5359
+ relayForConnection(connection) {
5360
+ const existing = this.relayClients.get(connection.connectionId);
5361
+ if (existing) {
5362
+ return existing;
5363
+ }
5364
+ const relay = new MachineRelayClient(connection, this.store, {
5365
+ onConnectionRevoked: async (error) => {
5366
+ await this.handleConnectionRevoked(connection, error);
5367
+ }
5368
+ });
5369
+ this.relayClients.set(connection.connectionId, relay);
5370
+ return relay;
5371
+ }
5372
+ async handleConnectionRevoked(connection, error) {
5373
+ this.connections = this.connections.filter((candidate) => candidate.connectionId !== connection.connectionId);
5374
+ await this.logger.info(`Relay revoked Local Machine connection ${connection.connectionId}; stopping local activity`, {
5375
+ connectionId: connection.connectionId,
5376
+ reason: error.message
5377
+ });
5378
+ try {
5379
+ await this.store.removeConnection(connection.connectionId);
5380
+ } catch (removalError) {
5381
+ await this.logger.error(`Failed to remove revoked Local Machine connection ${connection.connectionId} from local configuration`, removalError);
5382
+ }
5383
+ await Promise.all([...this.activeJobs.values()].filter((activeJob) => activeJob.connection.connectionId === connection.connectionId).map(async (activeJob) => {
5384
+ await activeJob.cancellation.request(ExecutionCancellationReason.LeaseLost);
5385
+ }));
5386
+ }
5330
5387
  async runHeartbeatLoop(connection, relay) {
5331
- while (!this.stopping) {
5388
+ while (!this.stopping && !relay.isRevoked()) {
5332
5389
  try {
5333
5390
  await relay.heartbeat({
5334
5391
  connectorInstanceId: this.connectorInstanceId,
@@ -5336,13 +5393,16 @@ var init_daemon = __esm({
5336
5393
  protocolVersion: machineProtocolVersion
5337
5394
  });
5338
5395
  } catch (error) {
5396
+ if (relay.isRevoked()) {
5397
+ return;
5398
+ }
5339
5399
  await this.logger.error(`Heartbeat failed for connection ${connection.connectionId}`, error);
5340
5400
  }
5341
5401
  await this.waitUntilStop(connection.heartbeatIntervalSeconds * 1e3);
5342
5402
  }
5343
5403
  }
5344
5404
  async runClaimLoop(connection, relay) {
5345
- while (!this.stopping) {
5405
+ while (!this.stopping && !relay.isRevoked()) {
5346
5406
  try {
5347
5407
  const response = await relay.claim({
5348
5408
  connectorInstanceId: this.connectorInstanceId,
@@ -5358,6 +5418,9 @@ var init_daemon = __esm({
5358
5418
  this.startClaimExecution(activeJob);
5359
5419
  }
5360
5420
  } catch (error) {
5421
+ if (relay.isRevoked()) {
5422
+ return;
5423
+ }
5361
5424
  await this.logger.error(`Job claim failed for connection ${connection.connectionId}`, error);
5362
5425
  await this.waitUntilStop(this.networkRetryMilliseconds);
5363
5426
  }
@@ -5365,7 +5428,7 @@ var init_daemon = __esm({
5365
5428
  }
5366
5429
  async runControlLoop(connection, relay) {
5367
5430
  let cursor = 0;
5368
- while (!this.stopping) {
5431
+ while (!this.stopping && !relay.isRevoked()) {
5369
5432
  try {
5370
5433
  const response = await relay.pollControls({
5371
5434
  after: cursor,
@@ -5380,6 +5443,9 @@ var init_daemon = __esm({
5380
5443
  cursor = response.nextCursor;
5381
5444
  }
5382
5445
  } catch (error) {
5446
+ if (relay.isRevoked()) {
5447
+ return;
5448
+ }
5383
5449
  await this.logger.error(`Control poll failed for connection ${connection.connectionId}`, error);
5384
5450
  await this.waitUntilStop(this.networkRetryMilliseconds);
5385
5451
  }
@@ -5573,7 +5639,7 @@ var init_daemon = __esm({
5573
5639
  async runEventUploadLoop(activeJob) {
5574
5640
  const durableJob = this.requireDurableJob(activeJob);
5575
5641
  let retryMilliseconds = this.networkRetryMilliseconds;
5576
- while (!this.stopping) {
5642
+ while (!this.stopping && !activeJob.relay.isRevoked()) {
5577
5643
  const outcome = await this.tryFlushEvents(activeJob);
5578
5644
  if (durableJob.snapshot().phase !== PersistedMachineJobPhase.Running) {
5579
5645
  return;
@@ -5590,7 +5656,7 @@ var init_daemon = __esm({
5590
5656
  }
5591
5657
  async runLeaseLoop(activeJob) {
5592
5658
  let retryMilliseconds = this.networkRetryMilliseconds;
5593
- while (!this.stopping) {
5659
+ while (!this.stopping && !activeJob.relay.isRevoked()) {
5594
5660
  const durableJob = this.requireDurableJob(activeJob);
5595
5661
  if (durableJob.snapshot().phase !== PersistedMachineJobPhase.Running) {
5596
5662
  return;
@@ -5637,6 +5703,9 @@ var init_daemon = __esm({
5637
5703
  }
5638
5704
  }
5639
5705
  async tryUploadTerminal(activeJob, budgetMilliseconds) {
5706
+ if (activeJob.relay.isRevoked()) {
5707
+ return TerminalUploadOutcome.PermanentFailure;
5708
+ }
5640
5709
  if (this.stopping) {
5641
5710
  return TerminalUploadOutcome.RetryableFailure;
5642
5711
  }
@@ -5670,6 +5739,10 @@ var init_daemon = __esm({
5670
5739
  async retryTerminalUpload(activeJob) {
5671
5740
  let retryMilliseconds = this.networkRetryMilliseconds;
5672
5741
  while (!this.stopping) {
5742
+ if (activeJob.relay.isRevoked()) {
5743
+ await this.requireDurableJob(activeJob).markUploaded();
5744
+ return;
5745
+ }
5673
5746
  const jitteredDelay = retryMilliseconds * (0.75 + this.random() * 0.5);
5674
5747
  await this.waitUntilStop(jitteredDelay);
5675
5748
  const outcome = await this.tryUploadTerminal(activeJob);
@@ -5740,7 +5813,7 @@ var init_daemon = __esm({
5740
5813
  }
5741
5814
  async confirmRunning(activeJob) {
5742
5815
  let retryMilliseconds = this.networkRetryMilliseconds;
5743
- while (!this.stopping && activeJob.cancellation.currentReason() === void 0) {
5816
+ while (!this.stopping && !activeJob.relay.isRevoked() && activeJob.cancellation.currentReason() === void 0) {
5744
5817
  try {
5745
5818
  await this.reportRunning(activeJob);
5746
5819
  return true;
@@ -5817,7 +5890,7 @@ var init_daemon = __esm({
5817
5890
  if (!connection) {
5818
5891
  continue;
5819
5892
  }
5820
- const relay = new MachineRelayClient(connection, this.store);
5893
+ const relay = this.relayForConnection(connection);
5821
5894
  const durableJob = DurableMachineJob.fromPersisted(this.store, job);
5822
5895
  if (job.phase !== PersistedMachineJobPhase.TerminalPendingUpload) {
5823
5896
  await durableJob.markTerminal({
@@ -7839,6 +7912,7 @@ __export(dist_exports, {
7839
7912
  LocalIpcServer: () => LocalIpcServer,
7840
7913
  MachineClaudePermissionMode: () => MachineClaudePermissionMode,
7841
7914
  MachineCodexPermissionMode: () => MachineCodexPermissionMode,
7915
+ MachineConnectionRevokedError: () => MachineConnectionRevokedError,
7842
7916
  MachineConnectionStatus: () => MachineConnectionStatus,
7843
7917
  MachineConnectionTargetKind: () => MachineConnectionTargetKind,
7844
7918
  MachineConnectorDaemon: () => MachineConnectorDaemon,
@@ -8091,7 +8165,7 @@ function getApiKeyOrUndefined() {
8091
8165
 
8092
8166
  // src/utils/http.ts
8093
8167
  function getUserAgent() {
8094
- return `moxt-cli/${"0.8.0-beta.3"} (${(0, import_os.platform)()}/${(0, import_os.arch)()}) node/${process.versions.node}`;
8168
+ return `moxt-cli/${"0.8.0-beta.4"} (${(0, import_os.platform)()}/${(0, import_os.arch)()}) node/${process.versions.node}`;
8095
8169
  }
8096
8170
  async function fetchApi(method, path3, body) {
8097
8171
  const apiKey = getApiKey();
@@ -10556,7 +10630,7 @@ var FLUSH_TIMEOUT_MS = 3e3;
10556
10630
  var MAX_BATCH_SIZE = 100;
10557
10631
  function commonLabels() {
10558
10632
  return {
10559
- cli_version: "0.8.0-beta.3",
10633
+ cli_version: "0.8.0-beta.4",
10560
10634
  node_version: process.versions.node,
10561
10635
  os: (0, import_node_os7.platform)(),
10562
10636
  arch: (0, import_node_os7.arch)(),
@@ -10602,7 +10676,7 @@ async function flush() {
10602
10676
  }
10603
10677
  const batch = buffer.splice(0, MAX_BATCH_SIZE);
10604
10678
  const payload = {
10605
- release_id: "0.8.0-beta.3",
10679
+ release_id: "0.8.0-beta.4",
10606
10680
  client_type: "cli",
10607
10681
  metric_data_list: batch.map((m) => ({
10608
10682
  profile: "cli",
@@ -10918,8 +10992,8 @@ function commandPath2(command) {
10918
10992
  }
10919
10993
 
10920
10994
  // src/index.ts
10921
- var program2 = createProgram("0.8.0-beta.3");
10922
- installUpdateHook(program2, "0.8.0-beta.3");
10995
+ var program2 = createProgram("0.8.0-beta.4");
10996
+ installUpdateHook(program2, "0.8.0-beta.4");
10923
10997
  instrumentProgram(program2);
10924
10998
  installExitHandler();
10925
10999
  program2.parseAsync(process.argv).then(async () => {