@tangle-network/agent-app 0.63.41 → 0.63.43

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.
@@ -972,6 +972,10 @@ function isTerminalPromptEvent(event) {
972
972
  const root = asPlainRecord(event);
973
973
  return root?.type === "result" || root?.type === "done" && asPlainRecord(root.data)?.status !== "failed";
974
974
  }
975
+ function isComputerInteractionEvent(event) {
976
+ const type = asPlainRecord(event)?.type;
977
+ return type === "interaction" || type === "interaction.cancel";
978
+ }
975
979
  function detectInteractiveQuestion(event) {
976
980
  const root = asPlainRecord(event);
977
981
  if (!root) return null;
@@ -980,7 +984,11 @@ function detectInteractiveQuestion(event) {
980
984
  const props = asPlainRecord(root.properties);
981
985
  const body = props ?? data ?? root;
982
986
  if (type === "question.asked" || type === "question") return firstQuestionText(body);
983
- if (type === "interaction" && body.kind === "question") return firstQuestionText(body);
987
+ if (type === "interaction") {
988
+ const request = asPlainRecord(body.request) ?? body;
989
+ if (request.kind !== "question") return null;
990
+ return typeof request.title === "string" && request.title ? request.title : firstQuestionText(request);
991
+ }
984
992
  const part = asPlainRecord(data?.part) ?? asPlainRecord(body.part);
985
993
  const tool = typeof part?.tool === "string" && part.tool || typeof part?.name === "string" && part.name || typeof body.tool === "string" && body.tool || void 0;
986
994
  const isQ = type === "message.part.updated" && (tool === "question" || asPlainRecord(part)?.type === "question");
@@ -1306,12 +1314,13 @@ function noAnswerError(observation) {
1306
1314
  if (failure) return new SandboxRunError("sandbox_failed", `The computer reported a failed execution: ${failure}`, { cause });
1307
1315
  return new SandboxRunError("sandbox_empty", "The sandbox finished without an answer.", { cause });
1308
1316
  }
1309
- async function collectSandboxPrompt(events, message2, history) {
1317
+ async function collectSandboxPrompt(events, message2, history, observe) {
1310
1318
  let finalText = "";
1311
1319
  const textParts = /* @__PURE__ */ new Map();
1312
1320
  let anonymousTextPart = 0;
1313
1321
  const observation = { terminal: false, types: /* @__PURE__ */ new Map() };
1314
1322
  for await (const rawEvent of events) {
1323
+ observe?.(rawEvent);
1315
1324
  const event = rawEvent;
1316
1325
  if (!event.type) continue;
1317
1326
  observePromptEvent(observation, event.type, record(event.data));
@@ -1331,8 +1340,8 @@ async function collectSandboxPrompt(events, message2, history) {
1331
1340
  const answer = finalText || lastNonPromptTextPart(textParts, dispatchedPromptTexts(message2, history));
1332
1341
  return { answer, observation };
1333
1342
  }
1334
- async function runSandboxPrompt(shell, box, message2, options) {
1335
- const { answer, observation } = await collectSandboxPrompt(streamSandboxPrompt(shell, box, message2, options), message2, options?.history);
1343
+ async function runSandboxPrompt(shell, box, message2, options, observe) {
1344
+ const { answer, observation } = await collectSandboxPrompt(streamSandboxPrompt(shell, box, message2, options), message2, options?.history, observe);
1336
1345
  if (!answer || !observation.terminal || observation.failure) throw noAnswerError(observation);
1337
1346
  return answer;
1338
1347
  }
@@ -1397,8 +1406,9 @@ async function listComputerFiles(box, root) {
1397
1406
  return { succeeded: false, error: message(error) };
1398
1407
  }
1399
1408
  }
1409
+ var MTIME_TOLERANCE_MS = 1e3;
1400
1410
  async function saveComputerFiles(input) {
1401
- const { box, vault, before, executionId } = input;
1411
+ const { box, vault, since, executionId } = input;
1402
1412
  const root = vault.root.replace(/\/+$/, "");
1403
1413
  const unchecked = (error) => {
1404
1414
  console.warn(JSON.stringify({ event: "agent_app_computer_files_unchecked", executionId, error }));
@@ -1409,7 +1419,7 @@ async function saveComputerFiles(input) {
1409
1419
  text: "Workspace vault: the app could not check which files this step saved, so none is confirmed to be in it."
1410
1420
  };
1411
1421
  };
1412
- if (!before.succeeded) return unchecked(before.error);
1422
+ if (since === void 0 || !Number.isFinite(since)) return unchecked("the runtime ledger did not report when the step started");
1413
1423
  const after = await listComputerFiles(box, root);
1414
1424
  if (!after.succeeded) return unchecked(after.error);
1415
1425
  const maxFiles = vault.maxFiles ?? DEFAULT_MAX_FILES;
@@ -1417,8 +1427,7 @@ async function saveComputerFiles(input) {
1417
1427
  const saved = [];
1418
1428
  const computerOnly = [];
1419
1429
  for (const [path, file] of Object.entries(after.files)) {
1420
- const earlier = before.files[path];
1421
- if (earlier && earlier.size === file.size && earlier.mtime === file.mtime) continue;
1430
+ if (file.mtime < since - MTIME_TOLERANCE_MS) continue;
1422
1431
  if (!isWorkspaceFileExportable(path)) continue;
1423
1432
  const refusal = saved.length + computerOnly.length >= maxFiles ? `more than ${maxFiles} files changed` : refuse(path, file.size);
1424
1433
  if (refusal) {
@@ -1489,7 +1498,6 @@ function modelWithinAllowList(preferred, allowedModels) {
1489
1498
 
1490
1499
  // src/sandbox/computer-step.ts
1491
1500
  var ALLOW_LIST_BUDGET_MS = 5e3;
1492
- var RECEIPT_LISTING_LIMIT = 2e3;
1493
1501
  async function digestHex(value) {
1494
1502
  const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(value)));
1495
1503
  return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
@@ -1518,6 +1526,16 @@ function refusedForModel(error) {
1518
1526
  }
1519
1527
  return false;
1520
1528
  }
1529
+ async function runStartedAt(box, sessionId, executionIds) {
1530
+ if (!sessionId || executionIds.length === 0) return void 0;
1531
+ try {
1532
+ const started = (await box.session(sessionId).runs()).filter((run) => executionIds.includes(run.executionId)).map((run) => run.startedAt).filter(Number.isFinite);
1533
+ return started.length ? Math.min(...started) : void 0;
1534
+ } catch (error) {
1535
+ console.warn(JSON.stringify({ event: "agent_app_computer_run_ledger_unread", sessionId, error: errorText(error) }));
1536
+ return void 0;
1537
+ }
1538
+ }
1521
1539
  function createComputerStep(options) {
1522
1540
  if (!options.run && !options.shell) throw new Error("A computer step needs the product shell or its own run");
1523
1541
  const vault = options.files;
@@ -1534,39 +1552,18 @@ function createComputerStep(options) {
1534
1552
  const parent = options.parentExecutionId?.trim();
1535
1553
  const call = callId?.trim();
1536
1554
  const stepId = parent ? await digestHex([parent, session?.id, call || crypto.randomUUID()]) : void 0;
1537
- const receipts = stepId && call ? options.receipts : void 0;
1538
- const prior = await receipts?.read(stepId).catch((error) => {
1539
- console.warn(JSON.stringify({ event: "agent_app_computer_receipt_unread", stepId, error: errorText(error) }));
1540
- return null;
1541
- });
1542
1555
  const report = (state, extra) => taskOptions.emit("computer_receipt", {
1543
1556
  stepId: stepId ?? null,
1544
1557
  sessionId: session?.id ?? null,
1545
1558
  state,
1546
1559
  ...extra
1547
1560
  });
1548
- if (prior?.state === "settled" && prior.answer?.trim()) {
1549
- report("settled", { model: prior.model ?? null, saved: prior.files?.saved ?? [], repeated: true });
1550
- return prior.answer;
1551
- }
1552
1561
  const models = await modelChain(options.models, signal);
1553
1562
  let model = models[0];
1554
- const before = vault ? prior?.before ?? await listComputerFiles(box, vault.root.replace(/\/+$/, "")) : void 0;
1555
- if (receipts) {
1556
- const kept = before?.succeeded && Object.keys(before.files).length > RECEIPT_LISTING_LIMIT ? void 0 : before;
1557
- await receipts.write({
1558
- stepId,
1559
- state: "prepared",
1560
- sessionId: session?.id ?? "",
1561
- ...model ? { model } : {},
1562
- ...kept ? { before: kept } : {},
1563
- updatedAt: Date.now()
1564
- }).catch((error) => {
1565
- console.warn(JSON.stringify({ event: "agent_app_computer_receipt_unwritten", stepId, state: "prepared", error: errorText(error) }));
1566
- });
1567
- }
1568
1563
  const framed = vault ? computerFileContract(vault.root.replace(/\/+$/, ""), frame(task)) : frame(task);
1564
+ const attempted = [];
1569
1565
  const send = (attemptModel, executionId) => {
1566
+ if (executionId) attempted.push(executionId);
1570
1567
  if (options.run) {
1571
1568
  return options.run(box, framed, {
1572
1569
  ...taskOptions,
@@ -1585,16 +1582,20 @@ function createComputerStep(options) {
1585
1582
  ...executionId ? { executionId, turnId: executionId } : {},
1586
1583
  ...session ? { sessionId: session.id } : {},
1587
1584
  ...session?.isolated ? { secretProfileIsolation: true } : {},
1585
+ // The run outlives this request; a repeated call reattaches to it through the runtime's admission.
1586
+ ...session && executionId ? { detach: true } : {},
1587
+ ...options.questions ? { interactions: { ...prompt.interactions, question: true } } : { disallowQuestions: true },
1588
1588
  signal,
1589
1589
  timeoutMs,
1590
- disallowQuestions: true,
1591
1590
  ...trace ? { trace } : {},
1592
1591
  ...span ? { traceParent: span } : {},
1593
1592
  onDispatch: () => {
1594
1593
  sent = true;
1595
1594
  prompt.onDispatch?.();
1596
1595
  }
1597
- });
1596
+ }, options.questions ? (event) => {
1597
+ if (isComputerInteractionEvent(event)) taskOptions.emit("computer_interaction", event);
1598
+ } : void 0);
1598
1599
  };
1599
1600
  let answer;
1600
1601
  for (let attempt = 0; ; attempt++) {
@@ -1619,23 +1620,10 @@ function createComputerStep(options) {
1619
1620
  }
1620
1621
  }
1621
1622
  if (!answer.trim()) return answer;
1622
- const files = vault && before ? await saveComputerFiles({ box, vault, before, executionId: stepId ?? "unnamed-step" }) : void 0;
1623
+ const files = vault ? await saveComputerFiles({ box, vault, since: await runStartedAt(box, session?.id, attempted), executionId: stepId ?? "unnamed-step" }) : void 0;
1623
1624
  const final = files ? `${answer}
1624
1625
 
1625
1626
  ${files.text}` : answer;
1626
- if (receipts) {
1627
- await receipts.write({
1628
- stepId,
1629
- state: "settled",
1630
- sessionId: session?.id ?? "",
1631
- ...model ? { model } : {},
1632
- answer: final,
1633
- ...files ? { files } : {},
1634
- updatedAt: Date.now()
1635
- }).catch((error) => {
1636
- console.warn(JSON.stringify({ event: "agent_app_computer_receipt_unwritten", stepId, state: "settled", error: errorText(error) }));
1637
- });
1638
- }
1639
1627
  report("settled", { model: model ?? null, saved: files?.saved ?? [], computerOnly: files?.computerOnly.length ?? 0 });
1640
1628
  return final;
1641
1629
  };
@@ -4322,4 +4310,4 @@ export {
4322
4310
  SandboxFilesystemNotReadyError,
4323
4311
  runForegroundSandboxSingleFlight
4324
4312
  };
4325
- //# sourceMappingURL=chunk-Y4RVCT7Z.js.map
4313
+ //# sourceMappingURL=chunk-XTQLZG6M.js.map