agent-transport-system 0.4.1 → 0.4.3

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/ats.js CHANGED
@@ -27,7 +27,7 @@ import wrapAnsi from "wrap-ansi";
27
27
  import { Box, Container, Editor, Key, ProcessTerminal, TUI, Text, getEditorKeybindings, matchesKey } from "@mariozechner/pi-tui";
28
28
 
29
29
  //#region package.json
30
- var version = "0.4.1";
30
+ var version = "0.4.3";
31
31
  var package_default = {
32
32
  $schema: "https://www.schemastore.org/package.json",
33
33
  name: "agent-transport-system",
@@ -54812,9 +54812,9 @@ function stripAnsi(value) {
54812
54812
 
54813
54813
  //#endregion
54814
54814
  //#region src/system/selected-local-runtime-reporting.ts
54815
- async function submitSelectedLocalRuntimeReports(input) {
54815
+ function resolveSelectedLocalRuntimeReadiness(input) {
54816
54816
  const uniqueAgentIds = [...new Set(input.localAgentIds.map(normalizeId))].filter(Boolean).sort();
54817
- const results = [];
54817
+ const decisions = [];
54818
54818
  for (const agentId of uniqueAgentIds) {
54819
54819
  const capabilityRef = resolveCapabilityRefFromControllerBinding({
54820
54820
  controllerKind: "builtin",
@@ -54825,18 +54825,26 @@ async function submitSelectedLocalRuntimeReports(input) {
54825
54825
  candidate: input.readiness.agents.candidates.find((currentCandidate) => currentCandidate.agentId === agentId) ?? null,
54826
54826
  readiness: input.readiness
54827
54827
  });
54828
- results.push({
54828
+ decisions.push({
54829
54829
  agentId,
54830
54830
  capabilityRef,
54831
54831
  localParticipationState: readinessDecision.localParticipationState,
54832
- reasonCodes: readinessDecision.reasonCodes,
54833
- runtimeReportSubmission: await submitPrepareRuntimeCapabilityReport({
54834
- capabilityRef,
54835
- localParticipationState: readinessDecision.localParticipationState,
54836
- reasonCodes: readinessDecision.reasonCodes
54837
- })
54832
+ reasonCodes: readinessDecision.reasonCodes
54838
54833
  });
54839
54834
  }
54835
+ return decisions;
54836
+ }
54837
+ async function submitSelectedLocalRuntimeReports(input) {
54838
+ const decisions = resolveSelectedLocalRuntimeReadiness(input);
54839
+ const results = [];
54840
+ for (const decision of decisions) results.push({
54841
+ ...decision,
54842
+ runtimeReportSubmission: await submitPrepareRuntimeCapabilityReport({
54843
+ capabilityRef: decision.capabilityRef,
54844
+ localParticipationState: decision.localParticipationState,
54845
+ reasonCodes: decision.reasonCodes
54846
+ })
54847
+ });
54840
54848
  return results;
54841
54849
  }
54842
54850
  function assertSelectedLocalRuntimeReportsAccepted(results) {
@@ -54845,6 +54853,11 @@ function assertSelectedLocalRuntimeReportsAccepted(results) {
54845
54853
  const reasonCode = failed.reasonCodes[0] ?? failed.runtimeReportSubmission.reasonCodes[0] ?? failed.runtimeReportSubmission.reason ?? "runtime.reporting.unavailable";
54846
54854
  throw new Error(formatSelectedRuntimeReportFailure(failed.agentId, reasonCode));
54847
54855
  }
54856
+ function assertSelectedLocalRuntimeReadinessReady(decisions) {
54857
+ const failed = decisions.find((decision) => decision.localParticipationState !== "ready");
54858
+ if (!failed) return;
54859
+ throw new Error(formatSelectedRuntimeReportFailure(failed.agentId, failed.reasonCodes[0] ?? "runtime.reporting.unavailable"));
54860
+ }
54848
54861
  function formatSelectedRuntimeReportFailure(agentId, reasonCode) {
54849
54862
  if (reasonCode === "service.not_running") return `ATS could not connect selected agents to ATS for ${agentId} because ATS Service is not running. Run \`${formatAtsCliCommand("ats service status")}\` to inspect it, then \`${formatAtsCliCommand("ats service run")}\` to start it and retry this Web setup command.`;
54850
54863
  if (reasonCode === "service.not_installed") return `ATS could not connect selected agents to ATS for ${agentId} because ATS Service is not installed. Run \`${formatAtsCliCommand("ats service install")}\`, then \`${formatAtsCliCommand("ats service run")}\`, and retry this Web setup command.`;
@@ -54975,6 +54988,8 @@ async function syncLocalSetupCurrentDeviceTargetFromServiceContract(input) {
54975
54988
  const PREPARE_TOKEN_HEADER = "x-ats-prepare-token";
54976
54989
  const PREPARE_SESSION_MAX_RECONNECT_ATTEMPTS = 3;
54977
54990
  const PREPARE_SESSION_RECONNECT_DELAY_MS = 1e3;
54991
+ const ROUTE_CATALOG_SYNC_MAX_ATTEMPTS = 8;
54992
+ const ROUTE_CATALOG_SYNC_RETRY_DELAY_MS = 750;
54978
54993
  const TERMINAL_PREPARE_STEP_ORDER = [
54979
54994
  "prepare_selected_agents",
54980
54995
  "refresh_service",
@@ -55225,14 +55240,15 @@ function buildTerminalStep(input) {
55225
55240
  run: async () => {
55226
55241
  return await connectSelectedLocalAgentsToAts({
55227
55242
  gatewayUrl: input.input.gatewayUrl,
55228
- localAgentIds: input.localAgentIds
55243
+ localAgentIds: input.localAgentIds,
55244
+ syncRouteCatalog: true
55229
55245
  });
55230
55246
  }
55231
55247
  };
55232
55248
  case "verify_setup": return {
55233
55249
  message: "Checking that selected local agents are ready.",
55234
55250
  run: async () => {
55235
- return await connectSelectedLocalAgentsToAts({
55251
+ return await verifySelectedLocalAgentsConnectedToAts({
55236
55252
  gatewayUrl: input.input.gatewayUrl,
55237
55253
  localAgentIds: input.localAgentIds
55238
55254
  });
@@ -55298,11 +55314,71 @@ async function connectSelectedLocalAgentsToAts(input) {
55298
55314
  readiness,
55299
55315
  localAgentIds: input.localAgentIds
55300
55316
  }));
55317
+ if (input.syncRouteCatalog) await waitForConfirmedRouteCatalogSync({ reason: "prepare_selected_agents_connected" });
55318
+ return {
55319
+ ...runtimeIdentity,
55320
+ localReadinessSnapshot: buildPrepareLocalReadinessSnapshot(readiness)
55321
+ };
55322
+ }
55323
+ async function verifySelectedLocalAgentsConnectedToAts(input) {
55324
+ const runtimeIdentity = await readTerminalRuntimeIdentityFromServiceContract();
55325
+ const readiness = await collectStartLocalReadiness({ auth: {
55326
+ status: "valid",
55327
+ email: null,
55328
+ authBaseUrl: null,
55329
+ gatewayUrl: input.gatewayUrl
55330
+ } });
55331
+ const selectedReadiness = resolveSelectedLocalRuntimeReadiness({
55332
+ readiness,
55333
+ localAgentIds: input.localAgentIds
55334
+ });
55335
+ assertSelectedLocalRuntimeReadinessReady(selectedReadiness);
55336
+ await assertSelectedRuntimeReadinessIsReadableByAts({
55337
+ gatewayUrl: input.gatewayUrl,
55338
+ runtimeIdentity,
55339
+ selectedReadiness
55340
+ });
55301
55341
  return {
55302
55342
  ...runtimeIdentity,
55303
55343
  localReadinessSnapshot: buildPrepareLocalReadinessSnapshot(readiness)
55304
55344
  };
55305
55345
  }
55346
+ async function readTerminalRuntimeIdentityFromServiceContract() {
55347
+ const serviceContract = await readDaemonServiceContract();
55348
+ const deviceId = serviceContract?.deviceId;
55349
+ const runtimeLane = serviceContract?.lane;
55350
+ if (!(deviceId && runtimeLane)) throw new Error("ATS could not verify selected agents because ATS Service did not provide this computer's device identity yet. Keep ATS Service running, then run the same setup command again.");
55351
+ return {
55352
+ deviceId,
55353
+ runtimeLane
55354
+ };
55355
+ }
55356
+ async function waitForConfirmedRouteCatalogSync(input) {
55357
+ let latestDelivery = null;
55358
+ for (let attempt = 1; attempt <= ROUTE_CATALOG_SYNC_MAX_ATTEMPTS; attempt++) {
55359
+ latestDelivery = await notifyDaemonRouteCatalogChanged({ reason: input.reason });
55360
+ if (latestDelivery.delivery === "control_plane") return latestDelivery;
55361
+ if (attempt < ROUTE_CATALOG_SYNC_MAX_ATTEMPTS) await sleep$1(ROUTE_CATALOG_SYNC_RETRY_DELAY_MS);
55362
+ }
55363
+ throw new Error(formatRouteCatalogSyncFailure(latestDelivery));
55364
+ }
55365
+ async function assertSelectedRuntimeReadinessIsReadableByAts(input) {
55366
+ const capabilityRefs = input.selectedReadiness.map((decision) => decision.capabilityRef);
55367
+ if (capabilityRefs.length === 0) return;
55368
+ const response = await createPrepareReadinessApi(createCliApiClientFoundation(input.gatewayUrl)).getReadiness({
55369
+ capabilityRefs,
55370
+ deviceId: input.runtimeIdentity.deviceId,
55371
+ lane: input.runtimeIdentity.runtimeLane
55372
+ });
55373
+ const runnableCapabilityRefs = new Set(response.runnableCapabilities.map((capability) => capability.capabilityRef));
55374
+ const missingCapabilityRef = capabilityRefs.find((capabilityRef) => !runnableCapabilityRefs.has(capabilityRef));
55375
+ if (response.status === "ready" && response.runtimeId && !missingCapabilityRef) return;
55376
+ const reasonCode = response.summary.reasonCodes[0] ?? response.reason ?? (missingCapabilityRef ? "prepare_readiness.capability_not_ready" : "prepare_readiness.not_ready");
55377
+ throw new Error(`ATS could not verify that selected agents are connected to ATS yet. Keep ATS Service running, then run the same setup command again. Support code: ${reasonCode}.`);
55378
+ }
55379
+ function formatRouteCatalogSyncFailure(latestDelivery) {
55380
+ return `ATS Service started, but ATS could not confirm that selected agents are available for Wake yet. Keep ATS Service running, then run the same setup command again. Support code: ${latestDelivery?.delivery === "signal" ? `route_catalog_sync.${latestDelivery.fallbackReason}` : "route_catalog_sync.unavailable"}.`;
55381
+ }
55306
55382
  function buildPrepareLocalReadinessSnapshot(readiness) {
55307
55383
  return {
55308
55384
  observedAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -55491,9 +55567,13 @@ function startHumanWaitingIndicator(input, message) {
55491
55567
  }
55492
55568
  let tick = 0;
55493
55569
  const render = () => {
55494
- const dots = ".".repeat(tick % 3 + 1);
55570
+ const line = formatHumanWaitingIndicatorLine({
55571
+ message: normalizedMessage,
55572
+ terminalColumns: process$1.stdout.columns,
55573
+ tick
55574
+ });
55495
55575
  tick += 1;
55496
- process$1.stdout.write(`\r${normalizedMessage}${dots} `);
55576
+ process$1.stdout.write(`\r\x1b[2K${line}`);
55497
55577
  };
55498
55578
  render();
55499
55579
  const timer = setInterval(render, 500);
@@ -55502,6 +55582,23 @@ function startHumanWaitingIndicator(input, message) {
55502
55582
  process$1.stdout.write("\r\x1B[2K");
55503
55583
  } };
55504
55584
  }
55585
+ function formatHumanWaitingIndicatorLine(input) {
55586
+ const label = shortHumanWaitingIndicatorLabel(input.message);
55587
+ const dots = ".".repeat(input.tick % 3 + 1);
55588
+ const maxLength = typeof input.terminalColumns === "number" && input.terminalColumns > 12 ? input.terminalColumns - 1 : 79;
55589
+ return `${truncateTerminalLine(label, Math.max(8, maxLength - dots.length))}${dots}`;
55590
+ }
55591
+ function shortHumanWaitingIndicatorLabel(message) {
55592
+ const normalizedMessage = message.toLowerCase();
55593
+ if (normalizedMessage.includes("choice in ats web")) return "Waiting for ATS Web";
55594
+ if (normalizedMessage.includes("connecting")) return "Connecting to ATS Web";
55595
+ return message;
55596
+ }
55597
+ function truncateTerminalLine(value, maxLength) {
55598
+ if (value.length <= maxLength) return value;
55599
+ if (maxLength <= 1) return value.slice(0, maxLength);
55600
+ return `${value.slice(0, maxLength - 1)}.`;
55601
+ }
55505
55602
  function toStatusCodeSlug(title) {
55506
55603
  const slug = title.toLowerCase().replaceAll(/[^a-z0-9]+/g, "_").replaceAll(/^_+|_+$/g, "");
55507
55604
  return slug.length > 0 ? slug : "status";