agent-transport-system 0.3.0 → 0.3.1

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
@@ -24,7 +24,7 @@ import wrapAnsi from "wrap-ansi";
24
24
  import { Box, Container, Editor, Key, ProcessTerminal, TUI, Text, getEditorKeybindings, matchesKey } from "@mariozechner/pi-tui";
25
25
 
26
26
  //#region package.json
27
- var version$1 = "0.3.0";
27
+ var version$1 = "0.3.1";
28
28
  var package_default = {
29
29
  name: "agent-transport-system",
30
30
  version: version$1,
@@ -13127,6 +13127,13 @@ function daemonServiceStableLauncherPath(pathInput = {}) {
13127
13127
  function daemonTokenPath(pathInput = {}) {
13128
13128
  return join(systemDir(pathInput), "daemon-token.json");
13129
13129
  }
13130
+ async function restoreDaemonInstallStateSnapshot(snapshot, pathInput = {}) {
13131
+ if (!snapshot) {
13132
+ await rm(daemonStatePath(pathInput), { force: true });
13133
+ return;
13134
+ }
13135
+ await writeDaemonInstallState(snapshot, pathInput);
13136
+ }
13130
13137
  function resolveDaemonControlPlaneEndpoint(pathInput = {}) {
13131
13138
  const runtimeRoot = daemonRuntimeRootPath(pathInput);
13132
13139
  const suffix = createHash("sha256").update(runtimeRoot).digest("hex").slice(0, 24);
@@ -13306,9 +13313,9 @@ function parseDaemonInstallState(raw) {
13306
13313
  function isDaemonInstallSource(value) {
13307
13314
  return value === "cli.auto_check" || value === "daemon.command.install";
13308
13315
  }
13309
- async function writeDaemonInstallState(state) {
13310
- const path = daemonStatePath();
13311
- await mkdir(systemDir(), { recursive: true });
13316
+ async function writeDaemonInstallState(state, pathInput = {}) {
13317
+ const path = daemonStatePath(pathInput);
13318
+ await mkdir(systemDir(pathInput), { recursive: true });
13312
13319
  const tempPath = `${path}.tmp-${process.pid}-${Date.now()}`;
13313
13320
  let shouldCleanupTemp = true;
13314
13321
  try {
@@ -17432,6 +17439,17 @@ async function writeDaemonServiceLauncher() {
17432
17439
  mode: 448
17433
17440
  });
17434
17441
  }
17442
+ async function restoreDaemonActiveBundleStateSnapshot(snapshot, pathInput = {}) {
17443
+ if (!snapshot) {
17444
+ await rm(daemonServiceActiveBundlePath(pathInput), { force: true });
17445
+ return;
17446
+ }
17447
+ await mkdir(daemonRuntimeStateRootPath(pathInput), { recursive: true });
17448
+ await writeFile(daemonServiceActiveBundlePath(pathInput), `${JSON.stringify(snapshot, null, 2)}\n`, {
17449
+ encoding: "utf8",
17450
+ mode: 384
17451
+ });
17452
+ }
17435
17453
  async function cleanupInactiveDaemonServiceBundles(input = {}) {
17436
17454
  const [active, serviceContract, runtimeLease] = await Promise.all([
17437
17455
  readDaemonActiveBundleState(),
@@ -29611,6 +29629,7 @@ async function runDaemonServiceFullAlignmentLocked(input) {
29611
29629
  });
29612
29630
  const inventoryBefore = input.state.inventoryBefore;
29613
29631
  if (!inventoryBefore) throw new Error("ATS could not resolve local service inventory for alignment.");
29632
+ const desiredState = resolveAlignmentDesiredState(inventoryBefore);
29614
29633
  const shouldRestoreBackground = resolveShouldRestoreBackground(inventoryBefore);
29615
29634
  const commandOptions = await resolveAlignmentCommandOptions({
29616
29635
  inventory: inventoryBefore,
@@ -29621,9 +29640,16 @@ async function runDaemonServiceFullAlignmentLocked(input) {
29621
29640
  });
29622
29641
  const serviceManager = await resolveAlignmentServiceManager(inventoryBefore) ?? null;
29623
29642
  if (!serviceManager) throw new Error("ATS could not resolve a system service manager for background service alignment.");
29643
+ const restorePoint = {
29644
+ desiredState,
29645
+ previousActiveBundle: inventoryBefore.activeBundle ?? null,
29646
+ previousContract: inventoryBefore.serviceContract ?? null,
29647
+ previousInstallState: inventoryBefore.installStatus.installed ? inventoryBefore.installStatus.state : null
29648
+ };
29624
29649
  const alignmentContext = {
29625
29650
  commandOptions,
29626
29651
  currentContract: inventoryBefore.serviceContract ?? null,
29652
+ desiredState,
29627
29653
  serviceManager,
29628
29654
  shouldRestoreBackground
29629
29655
  };
@@ -29646,8 +29672,7 @@ async function runDaemonServiceFullAlignmentLocked(input) {
29646
29672
  } catch (error) {
29647
29673
  try {
29648
29674
  await restoreDaemonServiceAfterFailedAlignment({
29649
- commandOptions: alignmentContext.commandOptions,
29650
- currentContract: alignmentContext.currentContract,
29675
+ restorePoint,
29651
29676
  serviceManager: alignmentContext.serviceManager,
29652
29677
  shouldRestoreBackground: alignmentContext.shouldRestoreBackground
29653
29678
  });
@@ -29689,7 +29714,7 @@ async function executeDaemonServiceFullAlignment(input) {
29689
29714
  commandOptions: input.alignmentContext.commandOptions,
29690
29715
  currentContract: input.alignmentContext.currentContract,
29691
29716
  daemonVersion: input.expectedVersion,
29692
- desiredState: input.alignmentContext.shouldRestoreBackground ? "background" : "stopped",
29717
+ desiredState: input.alignmentContext.desiredState,
29693
29718
  preparedBundle,
29694
29719
  serviceController: input.alignmentContext.serviceManager.controller
29695
29720
  });
@@ -29722,6 +29747,7 @@ async function executeDaemonServiceFullAlignment(input) {
29722
29747
  ...input.alignmentContext.shouldRestoreBackground ? { intendedMode: "background" } : {}
29723
29748
  });
29724
29749
  const verificationError = verifyAlignedInventory({
29750
+ desiredState: input.alignmentContext.desiredState,
29725
29751
  inventory: inventoryAfter,
29726
29752
  shouldRestoreBackground: input.alignmentContext.shouldRestoreBackground,
29727
29753
  target: input.target
@@ -29795,9 +29821,15 @@ async function resolveAlignmentCommandOptions(input) {
29795
29821
  ...typeof heartbeatMs === "number" ? { heartbeatMs } : {}
29796
29822
  };
29797
29823
  }
29824
+ function resolveAlignmentDesiredState(inventory) {
29825
+ if (inventory.serviceContract?.desiredState === "background") return "background";
29826
+ if (inventory.serviceContract?.desiredState === "stopped") return "stopped";
29827
+ if (inventory.serviceManagerStatus?.running) return "background";
29828
+ if (inventory.runtimeStatus.mode !== "background") return "stopped";
29829
+ return inventory.runtimeStatus.status === "running" || inventory.runtimeStatus.status === "stale" || inventory.runtimeStatus.managedBySystemService || inventory.runtimeStatus.autoStart ? "background" : "stopped";
29830
+ }
29798
29831
  function resolveShouldRestoreBackground(inventory) {
29799
- if (inventory.serviceContract?.desiredState === "background") return true;
29800
- if (inventory.serviceContract?.desiredState === "stopped") return false;
29832
+ if (resolveAlignmentDesiredState(inventory) !== "background") return false;
29801
29833
  if (inventory.serviceManagerStatus?.running) return true;
29802
29834
  if (inventory.runtimeStatus.mode !== "background") return false;
29803
29835
  return inventory.runtimeStatus.status === "running" || inventory.runtimeStatus.status === "stale" || inventory.runtimeStatus.managedBySystemService || inventory.runtimeStatus.autoStart;
@@ -29912,8 +29944,7 @@ function verifyAlignedInventoryContractState(input) {
29912
29944
  if (!contract) return "Alignment verification failed because the service contract is missing.";
29913
29945
  if (contract.daemonVersion !== input.target.daemonVersion) return `Alignment verification failed because service contract version is ${contract.daemonVersion} instead of ${input.target.daemonVersion}.`;
29914
29946
  if (input.inventory.activeBundle && contract.bundleId !== input.inventory.activeBundle.bundleId) return `Alignment verification failed because service contract bundle is ${contract.bundleId} instead of ${input.inventory.activeBundle.bundleId}.`;
29915
- const expectedDesiredState = input.shouldRestoreBackground ? "background" : "stopped";
29916
- if (contract.desiredState !== expectedDesiredState) return `Alignment verification failed because service contract desiredState is ${contract.desiredState} instead of ${expectedDesiredState}.`;
29947
+ if (contract.desiredState !== input.desiredState) return `Alignment verification failed because service contract desiredState is ${contract.desiredState} instead of ${input.desiredState}.`;
29917
29948
  return null;
29918
29949
  }
29919
29950
  function verifyAlignedInventoryBundleState(input) {
@@ -29946,16 +29977,28 @@ async function ensureAlignmentServiceContract(input) {
29946
29977
  ...input.currentContract?.lane ? { lane: input.currentContract.lane } : {}
29947
29978
  });
29948
29979
  }
29949
- async function restoreDaemonServiceAfterFailedAlignment(input) {
29950
- const activeBundle = await readDaemonActiveBundleState().catch(() => null);
29951
- if (activeBundle) await ensureAlignmentServiceContract({
29952
- commandOptions: input.commandOptions,
29953
- currentContract: input.currentContract,
29954
- daemonVersion: activeBundle.daemonVersion ?? input.currentContract?.daemonVersion ?? "unknown",
29955
- desiredState: input.shouldRestoreBackground ? "background" : "stopped",
29956
- preparedBundle: activeBundle,
29957
- serviceController: input.serviceManager.controller
29980
+ async function restoreAlignmentServiceContractSnapshot(snapshot) {
29981
+ if (!snapshot) {
29982
+ await clearDaemonServiceContract();
29983
+ return;
29984
+ }
29985
+ await upsertDaemonServiceContract({
29986
+ daemonVersion: snapshot.daemonVersion,
29987
+ bundleId: snapshot.bundleId,
29988
+ desiredState: snapshot.desiredState,
29989
+ serviceController: snapshot.serviceController,
29990
+ autoStart: snapshot.autoStart,
29991
+ gatewayUrl: snapshot.gatewayUrl,
29992
+ profileId: snapshot.profileId,
29993
+ deviceId: snapshot.deviceId,
29994
+ heartbeatMs: snapshot.heartbeatMs,
29995
+ lane: snapshot.lane
29958
29996
  });
29997
+ }
29998
+ async function restoreDaemonServiceAfterFailedAlignment(input) {
29999
+ await restoreDaemonInstallStateSnapshot(input.restorePoint.previousInstallState);
30000
+ await restoreDaemonActiveBundleStateSnapshot(input.restorePoint.previousActiveBundle);
30001
+ await restoreAlignmentServiceContractSnapshot(input.restorePoint.previousContract);
29959
30002
  await installSystemServiceRegistrationForAlignment({ serviceManager: input.serviceManager });
29960
30003
  if (input.shouldRestoreBackground) {
29961
30004
  await startDaemonSystemServiceWithAutoRepair({
@@ -37417,9 +37460,11 @@ const loadLedgerState = async (input) => {
37417
37460
  };
37418
37461
  const handleInvalidLedgerState = async (input) => {
37419
37462
  if (input.invalidStateStrategy !== "backup_and_reset") throw new Error(input.errorCode);
37420
- const emptyState = createEmptyLedgerState();
37463
+ const resetAtMs = Date.now();
37464
+ const emptyState = createEmptyLedgerState(resetAtMs);
37421
37465
  await persistLedgerStateAtomic({
37422
37466
  io: input.io,
37467
+ nowMs: resetAtMs,
37423
37468
  paths: input.paths,
37424
37469
  state: emptyState
37425
37470
  });
@@ -37428,7 +37473,7 @@ const handleInvalidLedgerState = async (input) => {
37428
37473
  const buildScopedStateTempFilePath = (input) => `${input.stateTempFilePath}.${Date.now()}.${Math.random().toString(36).slice(2, 10)}`;
37429
37474
  const persistLedgerStateAtomic = async (input) => {
37430
37475
  const normalizedState = compactLedgerState({
37431
- nowMs: Date.now(),
37476
+ nowMs: input.nowMs ?? Date.now(),
37432
37477
  state: cloneLedgerState$1(input.state)
37433
37478
  });
37434
37479
  await input.io.mkdir(input.paths.rootDirPath, { recursive: true });