agent-transport-system 0.7.18 → 0.7.20

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.7.18";
30
+ var version = "0.7.20";
31
31
  var package_default = {
32
32
  $schema: "https://www.schemastore.org/package.json",
33
33
  name: "agent-transport-system",
@@ -26205,6 +26205,28 @@ async function recoverLifecycleFailure(input) {
26205
26205
  const originalErrorMessage = toErrorMessage$19(input.error);
26206
26206
  const originalFailureStage = resolveLifecycleFailureStage(input.error, input.state.failureStage);
26207
26207
  const originalReasonCodes = resolveLifecycleFailureReasonCodes(input.error);
26208
+ const restartedCurrentVersion = await recoverBackgroundLifecycleVerificationFailure({
26209
+ commandOptions: input.commandOptions,
26210
+ error: input.error,
26211
+ expectedVersion: input.expectedVersion,
26212
+ failureStage: originalFailureStage,
26213
+ operation: input.operation,
26214
+ restorePoint: input.restorePoint,
26215
+ serviceManager: input.serviceManager,
26216
+ target: input.target
26217
+ }).catch(() => null);
26218
+ if (restartedCurrentVersion) return {
26219
+ status: "aligned",
26220
+ targetVersion: input.expectedVersion,
26221
+ inventoryBefore: input.inventoryBefore,
26222
+ inventoryAfter: restartedCurrentVersion,
26223
+ reasonCodes: [],
26224
+ completedPhases: input.state.completedPhases,
26225
+ desiredState: input.restorePoint.desiredState,
26226
+ shouldStartAfterLifecycle: input.restorePoint.shouldStartAfterLifecycle,
26227
+ backupDataPath: null,
26228
+ errorMessage: null
26229
+ };
26208
26230
  if (input.restorePoint.trusted) {
26209
26231
  const restoredPrevious = await restoreVerifiedPreviousState({
26210
26232
  restorePoint: input.restorePoint,
@@ -26334,6 +26356,47 @@ async function transitionLifecycleToStoppedSafeCurrentVersion(input) {
26334
26356
  target: buildDaemonServiceTargetSpec({ daemonVersion: input.expectedVersion })
26335
26357
  });
26336
26358
  }
26359
+ async function recoverBackgroundLifecycleVerificationFailure(input) {
26360
+ if (!shouldRecoverBackgroundLifecycleVerificationFailure({
26361
+ failureInventory: readLifecycleFailureInventory(input.error),
26362
+ failureStage: input.failureStage,
26363
+ restorePoint: input.restorePoint
26364
+ })) return null;
26365
+ await input.serviceManager.stop();
26366
+ await input.serviceManager.waitUntilStopped();
26367
+ await ensureNoOwnedProcessesRemain();
26368
+ await startLifecycleServiceWithRetry({
26369
+ serviceCommandSpec: buildDaemonSystemServiceCommandSpec({
26370
+ controller: input.serviceManager.controller,
26371
+ environmentTarget: input.commandOptions.environmentTarget
26372
+ }),
26373
+ serviceManager: input.serviceManager
26374
+ });
26375
+ await input.serviceManager.waitUntilRunning();
26376
+ return await waitForVerifiedLifecycleInventory({
26377
+ desiredState: "background",
26378
+ expectedVersion: input.expectedVersion,
26379
+ operation: input.operation,
26380
+ shouldStartAfterLifecycle: true,
26381
+ target: input.target
26382
+ });
26383
+ }
26384
+ function shouldRecoverBackgroundLifecycleVerificationFailure(input) {
26385
+ if (input.failureStage !== "verify_final_inventory" || input.restorePoint.desiredState !== "background" || input.restorePoint.shouldStartAfterLifecycle !== true || !input.failureInventory) return false;
26386
+ const anomalyCodes = input.failureInventory.anomalies.map((anomaly) => anomaly.code);
26387
+ if (anomalyCodes.length === 0) return false;
26388
+ const recoverableCodes = new Set(["manager_running_without_runtime", "runtime_stale"]);
26389
+ return anomalyCodes.every((code) => recoverableCodes.has(code));
26390
+ }
26391
+ function readLifecycleFailureInventory(error) {
26392
+ if (!(error instanceof DaemonServiceLifecycleStageError)) return null;
26393
+ return isDaemonServiceInventory(error.cause) ? error.cause : null;
26394
+ }
26395
+ function isDaemonServiceInventory(value) {
26396
+ if (!(value && typeof value === "object")) return false;
26397
+ const candidate = value;
26398
+ return Array.isArray(candidate.anomalies) && !!candidate.runtimeStatus;
26399
+ }
26337
26400
  function createLifecycleRestorePoint(input) {
26338
26401
  return {
26339
26402
  desiredState: input.desiredState,
@@ -26618,6 +26681,7 @@ async function waitForVerifiedLifecycleInventory(input) {
26618
26681
  shouldStartAfterLifecycle: input.shouldStartAfterLifecycle
26619
26682
  })) throw new DaemonServiceLifecycleStageError({
26620
26683
  message: verificationError,
26684
+ reasonCodes: readDaemonServiceInventoryAnomalyCodes(inventory),
26621
26685
  stage: "verify_final_inventory",
26622
26686
  cause: inventory
26623
26687
  });
@@ -26627,10 +26691,14 @@ async function waitForVerifiedLifecycleInventory(input) {
26627
26691
  }
26628
26692
  throw new DaemonServiceLifecycleStageError({
26629
26693
  message: lastError ?? "ATS could not verify the final local service inventory state.",
26694
+ reasonCodes: lastInventory ? readDaemonServiceInventoryAnomalyCodes(lastInventory) : [],
26630
26695
  stage: "verify_final_inventory",
26631
26696
  cause: lastInventory
26632
26697
  });
26633
26698
  }
26699
+ function readDaemonServiceInventoryAnomalyCodes(inventory) {
26700
+ return inventory.anomalies.map((anomaly) => anomaly.code);
26701
+ }
26634
26702
  async function startLifecycleServiceWithRetry(input) {
26635
26703
  try {
26636
26704
  await input.serviceManager.start();
@@ -39584,6 +39652,7 @@ function normalizeOptionalText$12(value) {
39584
39652
  //#region src/daemon/dispatch/execute-dispatch-task.ts
39585
39653
  const BOOTSTRAP_IDLE_STALE_MS = 1440 * 60 * 1e3;
39586
39654
  const BOOTSTRAP_DISPATCH_STALE_THRESHOLD = 12;
39655
+ const CLI_SPACE_ACTION_RECORD_POLL_INTERVAL_MS = 25;
39587
39656
  const DEFAULT_CONVERSATION_POLICY = {};
39588
39657
  const STRUCTURED_SPACE_ACTION_BLOCK_HEADER_REGEX = /^\s*\r?\n?/u;
39589
39658
  function mergeOpenClawGatewayVisibility(input) {
@@ -40275,7 +40344,7 @@ async function handleDispatchDeliverFrame(input) {
40275
40344
  phase: "runtime_invocation_started",
40276
40345
  result: "started"
40277
40346
  });
40278
- let runtimeResult = await runDispatchRuntimeExecution({
40347
+ const runtimeExecutionPromise = Promise.resolve(runDispatchRuntimeExecution({
40279
40348
  contextId: contextIdForDispatch,
40280
40349
  executionMode: "normal",
40281
40350
  io: ledgerIo,
@@ -40356,12 +40425,15 @@ async function handleDispatchDeliverFrame(input) {
40356
40425
  runtimeEnvOverrides,
40357
40426
  runtimeControllerSupport,
40358
40427
  agentControllerConversationOverride
40359
- });
40360
- runtimeResult = applyCliSpaceActionRecordIfPresent({
40428
+ }));
40429
+ let runtimeResult = await resolveRuntimeResultFromRuntimeOrCliSpaceAction({
40430
+ contextIdForDispatch,
40361
40431
  dispatchSpaceActionContext,
40432
+ ledgerIo,
40433
+ ledgerPaths: input.ledgerPaths,
40362
40434
  parsedTask,
40363
40435
  presenter: input.presenter,
40364
- runtimeResult
40436
+ runtimeExecutionPromise
40365
40437
  });
40366
40438
  const structuredSpaceActionOutcome = await resolveRuntimeResultStructuredSpaceAction({
40367
40439
  ledgerIo,
@@ -40874,6 +40946,146 @@ function createDispatchSpaceActionRuntimeContext(input) {
40874
40946
  }
40875
40947
  };
40876
40948
  }
40949
+ async function resolveRuntimeResultFromRuntimeOrCliSpaceAction(input) {
40950
+ if (!input.dispatchSpaceActionContext) return await input.runtimeExecutionPromise;
40951
+ const cliSpaceActionAbortController = new AbortController();
40952
+ let completion;
40953
+ try {
40954
+ completion = await Promise.race([input.runtimeExecutionPromise.then((runtimeResult) => ({
40955
+ kind: "runtime",
40956
+ runtimeResult
40957
+ })), waitForCliSpaceActionRecord({
40958
+ dispatchSpaceActionContext: input.dispatchSpaceActionContext,
40959
+ signal: cliSpaceActionAbortController.signal
40960
+ })]);
40961
+ } finally {
40962
+ cliSpaceActionAbortController.abort();
40963
+ }
40964
+ if (completion.kind === "runtime") return applyCliSpaceActionRecordIfPresent({
40965
+ dispatchSpaceActionContext: input.dispatchSpaceActionContext,
40966
+ parsedTask: input.parsedTask,
40967
+ presenter: input.presenter,
40968
+ runtimeResult: completion.runtimeResult
40969
+ });
40970
+ if (completion.kind === "cli_space_action_poll_aborted") return await input.runtimeExecutionPromise;
40971
+ input.runtimeExecutionPromise.catch((error) => {
40972
+ emitRunLine({
40973
+ presenter: input.presenter,
40974
+ code: "daemon.run.runtime_ignored_after_cli_space_action",
40975
+ text: `runtime completed after CLI ATS Space Action finalized task ${input.parsedTask.taskId}`,
40976
+ payload: {
40977
+ dispatchId: input.parsedTask.dispatchId,
40978
+ errorMessage: toErrorMessage$34(error),
40979
+ targetProfileId: input.parsedTask.targetProfileId,
40980
+ taskId: input.parsedTask.taskId
40981
+ }
40982
+ });
40983
+ });
40984
+ if (completion.kind === "cli_space_action_invalid") throw new DaemonServiceRunError({
40985
+ code: "dispatch.cli_space_action.invalid",
40986
+ message: completion.errorMessage
40987
+ });
40988
+ emitRunLine({
40989
+ presenter: input.presenter,
40990
+ code: "daemon.run.cli_space_action_finalized",
40991
+ text: `CLI ATS Space Action finalized task ${input.parsedTask.taskId}`,
40992
+ payload: {
40993
+ command: completion.record.command,
40994
+ dispatchId: input.parsedTask.dispatchId,
40995
+ spaceActionType: completion.record.action.type,
40996
+ targetProfileId: input.parsedTask.targetProfileId,
40997
+ taskId: input.parsedTask.taskId
40998
+ }
40999
+ });
41000
+ return await buildCliSpaceActionRuntimeResult({
41001
+ contextIdForDispatch: input.contextIdForDispatch,
41002
+ ledgerIo: input.ledgerIo,
41003
+ ledgerPaths: input.ledgerPaths,
41004
+ parsedTask: input.parsedTask,
41005
+ record: completion.record
41006
+ });
41007
+ }
41008
+ async function waitForCliSpaceActionRecord(input) {
41009
+ while (!input.signal.aborted) {
41010
+ const readResult = readDispatchSpaceActionRecord({ context: input.dispatchSpaceActionContext });
41011
+ if (readResult.status === "loaded") return {
41012
+ kind: "cli_space_action_recorded",
41013
+ record: readResult.record
41014
+ };
41015
+ if (readResult.status === "invalid") return {
41016
+ kind: "cli_space_action_invalid",
41017
+ errorMessage: readResult.errorMessage
41018
+ };
41019
+ try {
41020
+ await setTimeout$1(CLI_SPACE_ACTION_RECORD_POLL_INTERVAL_MS, void 0, { signal: input.signal });
41021
+ } catch (error) {
41022
+ if (input.signal.aborted) return { kind: "cli_space_action_poll_aborted" };
41023
+ throw error;
41024
+ }
41025
+ }
41026
+ return { kind: "cli_space_action_poll_aborted" };
41027
+ }
41028
+ async function buildCliSpaceActionRuntimeResult(input) {
41029
+ const output = resolveStructuredActionOutputFallback({
41030
+ action: input.record.action,
41031
+ cleanedOutput: ""
41032
+ });
41033
+ const resultPayload = {
41034
+ ...buildTaskResultPayloadFromOutcome({
41035
+ agentId: input.parsedTask.targetProfileId,
41036
+ attemptId: input.parsedTask.attemptId,
41037
+ deduped: false,
41038
+ emitFailed: false,
41039
+ output,
41040
+ sourceEventId: input.parsedTask.sourceEventId,
41041
+ taskId: input.parsedTask.taskId
41042
+ }),
41043
+ spaceAction: input.record.action,
41044
+ spaceActionProvenance: "cli_action"
41045
+ };
41046
+ const state = await withLedgerStateTransaction({
41047
+ invalidStateStrategy: "backup_and_reset",
41048
+ io: input.ledgerIo,
41049
+ mutate: (state) => {
41050
+ const nextState = settleInflightTask({
41051
+ state,
41052
+ taskId: input.parsedTask.taskId
41053
+ });
41054
+ return {
41055
+ nextState,
41056
+ value: nextState
41057
+ };
41058
+ },
41059
+ paths: input.ledgerPaths
41060
+ });
41061
+ await appendLedgerHistoryEntry({
41062
+ entry: {
41063
+ message: "task finalized from CLI Space Action",
41064
+ reasonCode: "task.cli_space_action",
41065
+ status: resultPayload.status,
41066
+ taskId: input.parsedTask.taskId,
41067
+ ts: (/* @__PURE__ */ new Date()).toISOString()
41068
+ },
41069
+ io: input.ledgerIo,
41070
+ paths: input.ledgerPaths
41071
+ });
41072
+ return {
41073
+ agentContextLifecycleCounters: state.agentContextLifecycleCounters,
41074
+ deduped: false,
41075
+ dispatchResult: {
41076
+ adapterResult: {
41077
+ ...input.contextIdForDispatch ? { contextDelta: { agentControllerConversationId: input.contextIdForDispatch } } : {},
41078
+ output,
41079
+ status: "success"
41080
+ },
41081
+ dispatched: true,
41082
+ reason: "dispatched"
41083
+ },
41084
+ resultPayload,
41085
+ runtimeState: "idle",
41086
+ state
41087
+ };
41088
+ }
40877
41089
  function applyCliSpaceActionRecordIfPresent(input) {
40878
41090
  if (!input.dispatchSpaceActionContext) return input.runtimeResult;
40879
41091
  const readResult = readDispatchSpaceActionRecord({ context: input.dispatchSpaceActionContext });