@tangle-network/agent-app 0.60.45 → 0.60.47

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.
@@ -1704,6 +1704,8 @@ var DEFAULT_ABSENT_DISPATCH_DEADLINE_MS = 10 * 6e4;
1704
1704
  var DEFAULT_RECEIPT_DEADLINE_MS = 10 * 6e4;
1705
1705
  var MESSAGE_READ_SKEW_MS = 10 * 6e4;
1706
1706
  var INTERRUPTED_RESULT_REPLAY_MAX_EVENTS = 2e3;
1707
+ var RUNTIME_RESTART_TERMINAL_EVENT_ID = "interrupted-done";
1708
+ var RUNTIME_RESTARTED_ERROR = "Execution interrupted: the agent runtime restarted before the run produced a terminal event. The run cannot be resumed; retry it.";
1707
1709
  function replayableExecution(run) {
1708
1710
  return typeof run.eventCount === "number" && run.eventCount <= INTERRUPTED_RESULT_REPLAY_MAX_EVENTS;
1709
1711
  }
@@ -1788,6 +1790,9 @@ function interruptedMessages(messages, turnId) {
1788
1790
  (message) => message.role === "assistant" && message.metadata?.turnId === turnId && (message.metadata.interrupted === true || message.metadata.status === "interrupted")
1789
1791
  );
1790
1792
  }
1793
+ function turnAssistantMessages(messages, turnId) {
1794
+ return messages.filter((message) => message.role === "assistant" && message.metadata?.turnId === turnId);
1795
+ }
1791
1796
  function consistent(values) {
1792
1797
  if (values.length === 0 || values.some((value) => value === void 0)) return void 0;
1793
1798
  const first = values[0];
@@ -1830,9 +1835,13 @@ function abortedToolCall(parts) {
1830
1835
  }
1831
1836
  return void 0;
1832
1837
  }
1833
- function sessionHoldsLiveWork(status) {
1838
+ function admittedWorkIsLive(status, runsByTurnId, admittedTurnIds, dispatchMayBePending) {
1834
1839
  if (status.status !== "queued" && status.status !== "running") return false;
1835
- return status.raw?.state !== "stale";
1840
+ if (status.raw?.state === "stale") return false;
1841
+ return admittedTurnIds.some((turnId) => {
1842
+ const run = runsByTurnId.get(turnId);
1843
+ return run ? run.status === "active" : dispatchMayBePending;
1844
+ });
1836
1845
  }
1837
1846
  function missingTurnReceipt(turnId, error) {
1838
1847
  return { turnId, state: "failed", text: "", parts: [], usage: {}, error };
@@ -1868,8 +1877,11 @@ async function observeNativeCompletion(options) {
1868
1877
  ]));
1869
1878
  }
1870
1879
  const exactSession = session;
1880
+ const dispatchMayBePending = now - options.registeredAt < (options.absentDispatchDeadlineMs ?? DEFAULT_ABSENT_DISPATCH_DEADLINE_MS);
1881
+ let runsByTurnId;
1882
+ const readRuns = async () => runsByTurnId ??= new Map((await exactSession.runs()).map((run) => [run.executionId, run]));
1871
1883
  if (admission.state === "open") {
1872
- if (sessionHoldsLiveWork(status)) {
1884
+ if (admittedWorkIsLive(status, await readRuns(), admission.admittedTurnIds, dispatchMayBePending)) {
1873
1885
  admission = await options.admissionStore.renew(options.executionId, new Date(now)) ?? admission;
1874
1886
  } else if (leaseUntil <= now) {
1875
1887
  admission = await options.admissionStore.closeExpired(options.executionId, new Date(now)) ?? admission;
@@ -1880,18 +1892,15 @@ async function observeNativeCompletion(options) {
1880
1892
  if (admittedTurnIds.length === 0 || admittedTurnIds[0] !== options.turnId) {
1881
1893
  throw new Error("Native completion admission has an invalid turn sequence");
1882
1894
  }
1883
- const runs = await exactSession.runs();
1884
- const runsByTurnId = new Map(runs.map((run) => [run.executionId, run]));
1885
- const missingExecution = admittedTurnIds.find((turnId) => !runsByTurnId.has(turnId));
1886
- if (missingExecution && now - options.registeredAt < (options.absentDispatchDeadlineMs ?? DEFAULT_ABSENT_DISPATCH_DEADLINE_MS)) {
1887
- return { state: "running" };
1888
- }
1889
- if (admittedTurnIds.some((turnId) => runsByTurnId.get(turnId)?.status === "active")) return { state: "running" };
1895
+ const ledger = await readRuns();
1896
+ const missingExecution = admittedTurnIds.find((turnId) => !ledger.has(turnId));
1897
+ if (missingExecution && dispatchMayBePending) return { state: "running" };
1898
+ if (admittedTurnIds.some((turnId) => ledger.get(turnId)?.status === "active")) return { state: "running" };
1890
1899
  const since = Math.max(0, options.registeredAt - MESSAGE_READ_SKEW_MS);
1891
1900
  let messages;
1892
1901
  const recovered = [];
1893
1902
  for (const turnId of admittedTurnIds) {
1894
- const run = runsByTurnId.get(turnId);
1903
+ const run = ledger.get(turnId);
1895
1904
  if (!run) {
1896
1905
  recovered.push(missingTurnReceipt(turnId, "Native Sandbox execution ledger is missing the admitted turn"));
1897
1906
  continue;
@@ -1912,7 +1921,13 @@ async function observeNativeCompletion(options) {
1912
1921
  });
1913
1922
  continue;
1914
1923
  }
1915
- const interrupted = run.status === "failed" || run.status === "cancelled" ? interruptedMessages(messages ??= await exactSession.messages({ limit: 1e3, since }), turnId) : [];
1924
+ const restarted = run.status === "failed" && run.lastEventId === RUNTIME_RESTART_TERMINAL_EVENT_ID;
1925
+ let interrupted = run.status === "failed" || run.status === "cancelled" ? interruptedMessages(messages ??= await exactSession.messages({ limit: 1e3, since }), turnId) : [];
1926
+ if (restarted && interrupted.length === 0) interrupted = turnAssistantMessages(messages ?? [], turnId);
1927
+ if (restarted && interrupted.length !== 1) {
1928
+ recovered.push({ turnId, state: "failed", text: "", parts: [], usage: {}, error: RUNTIME_RESTARTED_ERROR });
1929
+ continue;
1930
+ }
1916
1931
  if ((run.status === "failed" || run.status === "cancelled") && interrupted.length === 1) {
1917
1932
  const message = recoverSandboxAssistantMessage(interrupted[0]);
1918
1933
  const result = replayableExecution(run) ? await exactSession.result({ executionId: turnId }) : void 0;
@@ -1923,7 +1938,7 @@ async function observeNativeCompletion(options) {
1923
1938
  parts: message.parts ?? [],
1924
1939
  usage: { ...message.usage, ...usageFromResult(result) },
1925
1940
  ...attributionFromResult(result),
1926
- error: (typeof result?.error === "string" ? result.error : void 0) ?? interrupted[0].metadata?.interruptReason ?? status.failureReason?.message
1941
+ error: (typeof result?.error === "string" ? result.error : void 0) ?? interrupted[0].metadata?.interruptReason ?? (restarted ? RUNTIME_RESTARTED_ERROR : void 0) ?? status.failureReason?.message
1927
1942
  });
1928
1943
  continue;
1929
1944
  }
@@ -3392,6 +3407,7 @@ export {
3392
3407
  OOXML_WORD_MACRO_ENABLED_MIME,
3393
3408
  OOXML_WORD_MIME,
3394
3409
  PROMOTE_MAX_FILE_BYTES,
3410
+ RUNTIME_RESTARTED_ERROR,
3395
3411
  TOOL_ABORTED_ERROR,
3396
3412
  UPLOAD_INLINE_MAX_BYTES,
3397
3413
  UPLOAD_MAX_FILE_BYTES,