@the-vibe-company/armada 0.2.71 → 0.2.72

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/armada.js +139 -28
  2. package/package.json +1 -1
package/dist/armada.js CHANGED
@@ -10772,7 +10772,7 @@ var init_src2 = __esm(() => {
10772
10772
  });
10773
10773
 
10774
10774
  // ../plugin-api/dist/guard.js
10775
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
10775
+ import { AsyncLocalStorage as AsyncLocalStorage3 } from "node:async_hooks";
10776
10776
  async function guarded2(target, check, act) {
10777
10777
  await check();
10778
10778
  return guards2.run({ target, check }, async () => {
@@ -10794,7 +10794,7 @@ var init_guard = __esm(() => {
10794
10794
  key = Symbol.for("@armada/plugin-api/guard/v1");
10795
10795
  shared = globalThis;
10796
10796
  if (!shared[key])
10797
- shared[key] = new AsyncLocalStorage2;
10797
+ shared[key] = new AsyncLocalStorage3;
10798
10798
  guards2 = shared[key];
10799
10799
  });
10800
10800
 
@@ -46959,7 +46959,12 @@ import { basename as basename6, dirname as dirname16, join as join19, resolve as
46959
46959
  // ../core/src/merge.ts
46960
46960
  var import_picomatch3 = __toESM(require_picomatch2(), 1);
46961
46961
 
46962
+ // ../core/src/armada-api.ts
46963
+ import { AsyncLocalStorage } from "node:async_hooks";
46964
+
46962
46965
  // ../core/src/http.ts
46966
+ var TRANSIENT_CODE = /^(?:ECONNRESET|ECONNREFUSED|EPIPE|ETIMEDOUT|EAI_AGAIN|ENETUNREACH|EHOSTUNREACH|UND_ERR_(?:CONNECT_TIMEOUT|HEADERS_TIMEOUT|BODY_TIMEOUT|SOCKET)|ConnectionClosed|ConnectionRefused)$/;
46967
+
46963
46968
  class UnreadableAnswer extends Error {
46964
46969
  status;
46965
46970
  constructor(status) {
@@ -46981,10 +46986,13 @@ class HttpRequestError extends Error {
46981
46986
  attempts;
46982
46987
  transient;
46983
46988
  name = "HttpRequestError";
46989
+ code;
46984
46990
  constructor(error, timeoutMs, attempts, transient = false) {
46985
46991
  super(`${networkReason(error, timeoutMs)}${attempts > 1 ? `; failed after ${attempts} attempts (${attempts === 2 ? "one retry" : `${attempts - 1} retries`})` : ""}`);
46986
46992
  this.attempts = attempts;
46987
46993
  this.transient = transient;
46994
+ const code = codeOf(error);
46995
+ this.code = code && TRANSIENT_CODE.test(code) ? code : error instanceof Error && error.name === "TimeoutError" ? "TimeoutError" : "network";
46988
46996
  }
46989
46997
  }
46990
46998
  function networkReason(err, timeoutMs) {
@@ -47011,7 +47019,7 @@ function transient(error) {
47011
47019
  return true;
47012
47020
  const code = codeOf(error);
47013
47021
  if (code)
47014
- return /^(?:ECONNRESET|ECONNREFUSED|EPIPE|ETIMEDOUT|EAI_AGAIN|ENETUNREACH|EHOSTUNREACH|UND_ERR_(?:CONNECT_TIMEOUT|HEADERS_TIMEOUT|BODY_TIMEOUT|SOCKET)|ConnectionClosed|ConnectionRefused)$/.test(code);
47022
+ return TRANSIENT_CODE.test(code);
47015
47023
  return error instanceof TypeError && error.message === "fetch failed";
47016
47024
  }
47017
47025
  var retryStatus = (status) => [408, 429, 500, 502, 503, 504].includes(status);
@@ -47056,11 +47064,23 @@ async function wait(ms, opts, signal) {
47056
47064
  async function httpRequest(url, init, opts, read) {
47057
47065
  init.signal?.throwIfAborted();
47058
47066
  const doFetch = opts.fetch ?? fetch;
47059
- const attempts = opts.retry ? opts.retryStatus ? 3 : 2 : 1;
47067
+ const attempts = opts.retry ? opts.remainingMs ? Number.MAX_SAFE_INTEGER : opts.retryStatus ? 3 : 2 : 1;
47060
47068
  let transportRetries = 0;
47061
47069
  let waitedMs = 0;
47070
+ let lastError;
47062
47071
  for (let attempt = 1;attempt <= attempts; attempt++) {
47063
- const limit = opts.retry ? Math.min(opts.timeoutMs ?? 30000, attempt === 1 ? 5000 : 1e4) : opts.timeoutMs ?? 30000;
47072
+ init.signal?.throwIfAborted();
47073
+ const remaining = () => opts.remainingMs?.() ?? Number.POSITIVE_INFINITY;
47074
+ const checkBudget = () => {
47075
+ if (remaining() <= 0)
47076
+ throw lastError ?? new HttpRequestError(new DOMException("deadline", "TimeoutError"), 0, attempt);
47077
+ };
47078
+ checkBudget();
47079
+ await opts.beforeAttempt?.();
47080
+ init.signal?.throwIfAborted();
47081
+ checkBudget();
47082
+ const requestLimit = opts.retry ? Math.min(opts.timeoutMs ?? 30000, attempt === 1 ? 5000 : 1e4) : opts.timeoutMs ?? 30000;
47083
+ const limit = Math.max(1, Math.floor(Math.min(requestLimit, remaining())));
47064
47084
  const deadline = (opts.timeoutSignal ?? AbortSignal.timeout)(limit);
47065
47085
  const controller = new AbortController;
47066
47086
  const signal = init.signal ? AbortSignal.any([controller.signal, init.signal]) : controller.signal;
@@ -47090,15 +47110,16 @@ async function httpRequest(url, init, opts, read) {
47090
47110
  }
47091
47111
  if (opts.retry && opts.retryStatus?.(response.status)) {
47092
47112
  const after = retryAfterMs(response.headers.get("Retry-After"), opts.now ?? (() => new Date));
47093
- const remaining = 14000 - waitedMs;
47094
- if (response.status === 429 && after !== null && after > Math.min(1e4, remaining))
47113
+ const waitBudget = Math.min(opts.remainingMs ? remaining() : 14000 - waitedMs, 1e4);
47114
+ if (response.status === 429 && after !== null && after > waitBudget)
47095
47115
  throw new HttpStatusError(429, `${after / 1000} s`);
47096
47116
  if (attempt < attempts) {
47117
+ lastError = new HttpStatusError(response.status, "the confirmed request budget expires");
47097
47118
  const base = attempt === 1 ? 1000 : 3000;
47098
47119
  const jitter = base * (0.8 + (opts.random ?? Math.random)() * 0.4);
47099
47120
  pause = {
47100
47121
  status: response.status,
47101
- ms: Math.min(1e4, remaining, Math.max(after ?? 0, Math.round(jitter)))
47122
+ ms: Math.max(0, Math.min(waitBudget, Math.max(after ?? 0, Math.round(jitter))))
47102
47123
  };
47103
47124
  await response.body?.cancel().catch(() => {});
47104
47125
  return;
@@ -47107,6 +47128,7 @@ async function httpRequest(url, init, opts, read) {
47107
47128
  return read(response);
47108
47129
  })()
47109
47130
  ]);
47131
+ checkBudget();
47110
47132
  if (!pause)
47111
47133
  return answer;
47112
47134
  } catch (error) {
@@ -47118,11 +47140,18 @@ async function httpRequest(url, init, opts, read) {
47118
47140
  const transport = fetchFailed || deadline.aborted || transient(cause) || unreadable;
47119
47141
  if (!transport)
47120
47142
  throw error;
47121
- if (attempt < attempts && transportRetries < 1 && (!response || response.ok) && retryable) {
47143
+ const failure = new HttpRequestError(cause, limit, attempt, retryable);
47144
+ lastError = failure;
47145
+ if (attempt < attempts && (transportRetries < 1 || opts.remainingMs) && (!response || response.ok) && retryable) {
47122
47146
  transportRetries++;
47123
- continue;
47147
+ pause = {
47148
+ status: null,
47149
+ code: failure.code,
47150
+ ms: transportRetries === 1 ? 0 : Math.max(0, Math.min(1e4, remaining(), 3000))
47151
+ };
47152
+ } else {
47153
+ throw failure;
47124
47154
  }
47125
- throw new HttpRequestError(cause, limit, attempt, retryable);
47126
47155
  } finally {
47127
47156
  deadline.removeEventListener("abort", onDeadline);
47128
47157
  controller.abort();
@@ -47130,7 +47159,8 @@ async function httpRequest(url, init, opts, read) {
47130
47159
  response.body.cancel().catch(() => {});
47131
47160
  }
47132
47161
  if (pause) {
47133
- opts.onRetry?.(`${opts.service ?? "Service"} answered ${pause.status}; trying again in ${Math.round(pause.ms / 100) / 10} s (${attempt + 1}/${attempts})`);
47162
+ if (pause.status !== null || opts.remainingMs)
47163
+ opts.onRetry?.(`${opts.service ?? "Service"} ${pause.status === null ? `unreachable (${pause.code})` : `answered ${pause.status}`}; trying again in ${Math.round(pause.ms / 100) / 10} s (${opts.remainingMs ? "attempt " : ""}${attempt + 1}${opts.remainingMs ? "" : `/${attempts}`})`);
47134
47164
  waitedMs += pause.ms;
47135
47165
  await wait(pause.ms, opts, init.signal);
47136
47166
  }
@@ -47172,14 +47202,20 @@ class ArmadaApiError extends Error {
47172
47202
  signedOut;
47173
47203
  status;
47174
47204
  upgrade;
47175
- constructor(message, next = null, signedOut = false, status = null, upgrade = null) {
47205
+ transient;
47206
+ constructor(message, next = null, signedOut = false, status = null, upgrade = null, transient = false) {
47176
47207
  super(message);
47177
47208
  this.next = next;
47178
47209
  this.signedOut = signedOut;
47179
47210
  this.status = status;
47180
47211
  this.upgrade = upgrade;
47212
+ this.transient = transient;
47181
47213
  }
47182
47214
  }
47215
+ var fleetRequests = new AsyncLocalStorage;
47216
+ function withArmadaFleetRequest(request, act) {
47217
+ return fleetRequests.run(request, act);
47218
+ }
47183
47219
  var LOGIN_NEXT = "armada login";
47184
47220
  var TIMEOUT_MS = 30000;
47185
47221
  function apiBaseUrl(value) {
@@ -47205,7 +47241,7 @@ function armadaApi(opts) {
47205
47241
  const host = base.host;
47206
47242
  let serverCli = null;
47207
47243
  async function call(method, path, init = {}) {
47208
- const limit = init.timeoutMs ?? timeoutMs;
47244
+ const limit = Math.min(init.timeoutMs ?? timeoutMs, init.request ? 15000 : Number.POSITIVE_INFINITY);
47209
47245
  const headers = { Accept: "application/json" };
47210
47246
  if (init.body)
47211
47247
  headers["Content-Type"] = "application/json";
@@ -47218,9 +47254,11 @@ function armadaApi(opts) {
47218
47254
  return httpRequest(new URL(`api/cli/${path}`, base).toString(), {
47219
47255
  method,
47220
47256
  headers,
47257
+ signal: init.request?.signal,
47221
47258
  ...init.body ? { body: JSON.stringify(init.body) } : {}
47222
47259
  }, {
47223
47260
  ...opts,
47261
+ ...init.request,
47224
47262
  timeoutMs: limit,
47225
47263
  retry: init.retry ?? method === "GET",
47226
47264
  retryStatus: (status) => retryStatus(status) && (path !== "credentials" || status !== 429),
@@ -47251,7 +47289,7 @@ function armadaApi(opts) {
47251
47289
  if (err instanceof HttpStatusError)
47252
47290
  throw new ArmadaApiError(`Armada (${host}) ${err.message}`, "the same command after the rate limit expires", false, err.status);
47253
47291
  if (err instanceof HttpRequestError)
47254
- throw new ArmadaApiError(`Armada (${host}) unreachable: ${err.message} (${method} ${path})`, "the same command again once it answers, or check ARMADA_API_URL");
47292
+ throw new ArmadaApiError(`Armada (${host}) unreachable: ${init.request ? err.code : err.message} (${method} ${path})`, "the same command again once it answers, or check ARMADA_API_URL", false, null, null, err.transient);
47255
47293
  throw err;
47256
47294
  });
47257
47295
  }
@@ -47345,11 +47383,14 @@ function armadaApi(opts) {
47345
47383
  ] : []);
47346
47384
  },
47347
47385
  async fleet(signIn, op, body, timeoutMs) {
47386
+ const context = fleetRequests.getStore();
47387
+ const request = context?.operation === op ? context : undefined;
47348
47388
  const { status, body: answer } = await call("POST", `fleet/${op}`, {
47349
47389
  signIn,
47350
47390
  body,
47351
47391
  ...timeoutMs ? { timeoutMs } : {},
47352
- retry: [
47392
+ request,
47393
+ retry: request?.retry ?? [
47353
47394
  "job/list",
47354
47395
  "events/latest",
47355
47396
  "events/state",
@@ -47370,8 +47411,11 @@ function armadaApi(opts) {
47370
47411
  });
47371
47412
  if (status === 304)
47372
47413
  return null;
47373
- if (status !== 200)
47414
+ if (status !== 200) {
47415
+ if (request)
47416
+ throw new ArmadaApiError(`Armada refused fleet/${op} (HTTP ${status})`, status === 401 ? LOGIN_NEXT : null, status === 401, status);
47374
47417
  throw refusal(status, answer, status === 401 ? null : "Armada refused");
47418
+ }
47375
47419
  if (!("result" in answer))
47376
47420
  throw new ArmadaApiError(`Armada (${host}) answered fleet/${op} in a shape this CLI does not know`);
47377
47421
  return answer.result;
@@ -57821,6 +57865,7 @@ async function readProjectConfig(p, opts) {
57821
57865
  // ../core/src/queue-drain.ts
57822
57866
  var TTL_MS = 10 * 60000;
57823
57867
  var POLL_MS = 30000;
57868
+ var MARGIN_MS = 30000;
57824
57869
  var BACKOFF_MS2 = [60000, 5 * 60000, 15 * 60000];
57825
57870
  var PROGRESS_MAX = 300;
57826
57871
 
@@ -57829,20 +57874,76 @@ class TakenOver extends Refusal {
57829
57874
  super("the queue was taken over", "armada merge --drain after its current holder finishes");
57830
57875
  }
57831
57876
  }
57832
- var temporary = (err) => err instanceof ArmadaApiError ? !err.signedOut && !err.upgrade && (err.status === null || err.status === 429 || err.status >= 500) : err instanceof MergeStateError ? err.transient || !err.pull : err instanceof Error && ("transient" in err) && err.transient === true;
57877
+ var temporary = (err) => err instanceof ArmadaApiError ? !err.signedOut && !err.upgrade && (err.transient || err.status === 429 || err.status !== null && err.status >= 500) : err instanceof MergeStateError ? err.transient || !err.pull : err instanceof Error && ("transient" in err) && err.transient === true;
57833
57878
  async function drainMergeQueue(ctx, fleet, opts) {
57879
+ const duration = opts.monotonic ?? (() => performance.now());
57880
+ const acquiredAt = duration();
57834
57881
  const lease = { name: MERGE_QUEUE_LEASE, holder: ctx.holder, ttlMs: TTL_MS };
57835
57882
  const got = await fleet.acquireLease(lease);
57836
57883
  if (!got.acquired)
57837
57884
  throw new Refusal(`drained by ${got.held?.holder ?? "another coordinator"} until ${got.held?.expiresAt ?? "unknown"}`, "armada merge --drain once that coordinator finishes");
57838
57885
  let pulseError = null;
57886
+ let confirmedUntil = acquiredAt + TTL_MS;
57887
+ let renewing = null;
57888
+ const controller = new AbortController;
57889
+ const signal = opts.signal ? AbortSignal.any([controller.signal, opts.signal]) : controller.signal;
57839
57890
  let pulse = Promise.resolve();
57840
57891
  let stopPulse;
57892
+ const request = (operation, act, fence = false) => {
57893
+ const began = duration();
57894
+ const until = confirmedUntil;
57895
+ let notified = false;
57896
+ return withArmadaFleetRequest({
57897
+ operation,
57898
+ signal,
57899
+ retry: true,
57900
+ remainingMs: () => Math.min(until, confirmedUntil) - duration() - MARGIN_MS,
57901
+ beforeAttempt: fence ? tick : undefined,
57902
+ sleep: ctx.sleep,
57903
+ onRetry: (message) => {
57904
+ if (notified)
57905
+ return;
57906
+ notified = true;
57907
+ ctx.progress?.(`Armada unreachable, ${operation === "lease/renew" ? "renewing the lease" : operation} again: ${message}; elapsed ${Math.ceil((duration() - began) / 1000)} s; remaining ${Math.max(0, Math.floor((until - duration() - MARGIN_MS) / 1000))} s`);
57908
+ }
57909
+ }, async () => {
57910
+ const answer = await act();
57911
+ if (operation === "holds" && !Array.isArray(answer))
57912
+ throw new Refusal("Armada returned an invalid hold answer; nothing was merged", "armada merge --drain once Armada answers");
57913
+ return answer;
57914
+ }).catch((error) => {
57915
+ if (!(operation === "queue/progress" && error instanceof ArmadaApiError && error.status === 404))
57916
+ pulseError ??= error;
57917
+ throw error;
57918
+ });
57919
+ };
57841
57920
  const tick = async () => {
57921
+ signal.throwIfAborted();
57922
+ if (pulseError)
57923
+ throw pulseError;
57924
+ if (duration() >= confirmedUntil - MARGIN_MS) {
57925
+ pulseError = new Refusal("the queue lease expired; nothing was merged", "armada merge --drain once Armada answers");
57926
+ throw pulseError;
57927
+ }
57928
+ if (!renewing) {
57929
+ const started = duration();
57930
+ renewing = request("lease/renew", () => fleet.renewLease(lease)).then((held) => {
57931
+ if (held === false)
57932
+ throw new TakenOver;
57933
+ if (held !== true)
57934
+ throw new Refusal("Armada returned an invalid lease renewal; nothing was merged", "armada merge --drain once Armada answers");
57935
+ confirmedUntil = started + TTL_MS;
57936
+ }).catch((error) => {
57937
+ pulseError = error;
57938
+ throw error;
57939
+ }).finally(() => {
57940
+ renewing = null;
57941
+ });
57942
+ }
57943
+ await renewing;
57944
+ signal.throwIfAborted();
57842
57945
  if (pulseError)
57843
57946
  throw pulseError;
57844
- if (!await fleet.renewLease(lease))
57845
- throw new TakenOver;
57846
57947
  };
57847
57948
  const sleep = async (ms) => {
57848
57949
  for (let remaining = ms;remaining > 0; remaining -= POLL_MS) {
@@ -57859,8 +57960,9 @@ async function drainMergeQueue(ctx, fleet, opts) {
57859
57960
  if (mutations && !mutations.includes(key))
57860
57961
  return value.bind(target);
57861
57962
  return async (...args) => {
57963
+ await recorded;
57862
57964
  await tick();
57863
- const result = await Reflect.apply(value, target, args);
57965
+ const result = key === "holds" ? await request("holds", () => Reflect.apply(value, target, args), true) : await Reflect.apply(value, target, args);
57864
57966
  if (!mutations)
57865
57967
  await tick();
57866
57968
  return result;
@@ -57877,7 +57979,14 @@ async function drainMergeQueue(ctx, fleet, opts) {
57877
57979
  if (!entry || !detail || detail === shown)
57878
57980
  return;
57879
57981
  shown = detail;
57880
- recorded = recorded.then(() => fleet.queueProgress({ id: entry.id, holder: ctx.holder, detail })).then(() => {}, () => {});
57982
+ recorded = recorded.then(async () => {
57983
+ const held = await request("queue/progress", () => fleet.queueProgress({ id: entry.id, holder: ctx.holder, detail }), true);
57984
+ if (held !== true)
57985
+ throw new TakenOver;
57986
+ }).then(() => {}, (error) => {
57987
+ if (!(error instanceof ArmadaApiError && error.status === 404))
57988
+ pulseError = error;
57989
+ });
57881
57990
  };
57882
57991
  const run = {
57883
57992
  ...ctx,
@@ -57914,12 +58023,13 @@ async function drainMergeQueue(ctx, fleet, opts) {
57914
58023
  });
57915
58024
  for (;; ) {
57916
58025
  await tick();
57917
- const next = await fleet.queueNext({ holder: ctx.holder });
58026
+ const next = await request("queue/next", () => fleet.queueNext({ holder: ctx.holder }), true);
57918
58027
  if ("refused" in next)
57919
58028
  throw new TakenOver;
58029
+ await tick();
57920
58030
  const entry = next.entry;
57921
58031
  if (!entry) {
57922
- const pending = (await fleet.queueList()).find(queueOpen);
58032
+ const pending = (await request("queue/list", () => fleet.queueList(), true)).find(queueOpen);
57923
58033
  if (!pending) {
57924
58034
  ctx.progress?.("queue empty");
57925
58035
  return;
@@ -57980,6 +58090,7 @@ async function drainMergeQueue(ctx, fleet, opts) {
57980
58090
  } finally {
57981
58091
  stopPulse?.();
57982
58092
  current = null;
58093
+ controller.abort();
57983
58094
  await Promise.all([pulse, recorded]);
57984
58095
  await fleet.releaseLease(lease).catch(() => {});
57985
58096
  }
@@ -62469,7 +62580,7 @@ ${attachments.join(`
62469
62580
  };
62470
62581
  }
62471
62582
  // package.json
62472
- var version2 = "0.2.71";
62583
+ var version2 = "0.2.72";
62473
62584
 
62474
62585
  // src/io.ts
62475
62586
  class UsageError extends Error {
@@ -63974,7 +64085,7 @@ async function outgoingRedactor(io, config, credentials) {
63974
64085
  }
63975
64086
 
63976
64087
  // src/runtimes/adapter.ts
63977
- import { AsyncLocalStorage } from "node:async_hooks";
64088
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
63978
64089
  import { createHash as createHash6 } from "node:crypto";
63979
64090
 
63980
64091
  // src/runtimes/claude-code.ts
@@ -69134,11 +69245,11 @@ function sharesRuntimeWorkspace(ref, other) {
69134
69245
  return ref.handle === other.handle || ref.runtime === "conductor" && runtimeNameOf(other.runtime) === "conductor" && ref.handle.split("/")[0] === other.handle.split("/")[0];
69135
69246
  }
69136
69247
  var stale = (ticket) => new RuntimeError(`${ticket}'s claim changed; left the runtime untouched`, "stale", "armada inbox");
69137
- var leases = new AsyncLocalStorage;
69248
+ var leases = new AsyncLocalStorage2;
69138
69249
  function withRuntimeLease(check, act) {
69139
69250
  return leases.run(check, act);
69140
69251
  }
69141
- var guards = new AsyncLocalStorage;
69252
+ var guards = new AsyncLocalStorage2;
69142
69253
  function same(a, b) {
69143
69254
  return a.ticket === b.ticket && a.runtime === b.runtime && a.handle === b.handle && a.claimedAt === b.claimedAt && a.launchId === b.launchId && a.releasedAt === b.releasedAt && (a.claimedAt === null || (a.branch ?? null) === (b.branch ?? null));
69144
69255
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@the-vibe-company/armada",
3
- "version": "0.2.71",
3
+ "version": "0.2.72",
4
4
  "type": "module",
5
5
  "description": "Run a fleet of coding agents on one project: the armada command line.",
6
6
  "license": "MIT",