@parcha/agentrun-serve 0.1.0-beta.54 → 0.1.0-beta.56

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.
Files changed (234) hide show
  1. package/CHANGELOG.md +142 -2
  2. package/README.md +310 -98
  3. package/dist/adapters.d.ts +11 -0
  4. package/dist/adapters.js +49 -0
  5. package/dist/agent-package.d.ts +3 -3
  6. package/dist/agent-package.js +6 -13
  7. package/dist/agents-store.js +6 -5
  8. package/dist/answer-json.d.ts +6 -2
  9. package/dist/answer-json.js +22 -4
  10. package/dist/api-only.d.ts +16 -4
  11. package/dist/api-only.js +23 -7
  12. package/dist/calendar/tools.js +3 -2
  13. package/dist/chatgpt-plan.d.ts +16 -1
  14. package/dist/chatgpt-plan.js +116 -12
  15. package/dist/claude-login.d.ts +34 -5
  16. package/dist/claude-login.js +31 -7
  17. package/dist/claude-sign-in.d.ts +35 -0
  18. package/dist/claude-sign-in.js +90 -0
  19. package/dist/cli.js +5 -3
  20. package/dist/composio-made.d.ts +26 -1
  21. package/dist/composio-made.js +39 -2
  22. package/dist/composio-preset.d.ts +3 -2
  23. package/dist/composio-preset.js +46 -14
  24. package/dist/computer-held-routes.js +2 -3
  25. package/dist/computer-setting.d.ts +4 -2
  26. package/dist/computer-setting.js +0 -2
  27. package/dist/computers-running.d.ts +8 -0
  28. package/dist/computers-running.js +27 -0
  29. package/dist/console/assets/backend-Boh7tC4i.js +166 -0
  30. package/dist/console/assets/index-CQ3cJRzI.js +746 -0
  31. package/dist/console/assets/index-DKkaS3a0.css +1 -0
  32. package/dist/console/index.html +2 -2
  33. package/dist/docker-host-addresses.d.ts +23 -3
  34. package/dist/docker-host-addresses.js +90 -18
  35. package/dist/exa-keyless.d.ts +2 -3
  36. package/dist/exa-keyless.js +43 -127
  37. package/dist/firecrawl-keyless.d.ts +41 -0
  38. package/dist/firecrawl-keyless.js +152 -0
  39. package/dist/handed-secret.d.ts +3 -1
  40. package/dist/handed-secret.js +4 -2
  41. package/dist/harness-keys.d.ts +98 -0
  42. package/dist/harness-keys.js +247 -0
  43. package/dist/harness-recipe-entry.d.ts +14 -9
  44. package/dist/harness-recipe-entry.js +25 -14
  45. package/dist/harness-table.d.ts +16 -1
  46. package/dist/harness-table.js +19 -7
  47. package/dist/harness.d.ts +4 -0
  48. package/dist/harness.js +12 -1
  49. package/dist/host-check.d.ts +6 -0
  50. package/dist/host-check.js +10 -0
  51. package/dist/import-summary.d.ts +1 -1
  52. package/dist/import-summary.js +1 -1
  53. package/dist/import.d.ts +5 -5
  54. package/dist/import.js +54 -67
  55. package/dist/key-refusal.d.ts +21 -5
  56. package/dist/key-refusal.js +231 -37
  57. package/dist/keyless-mcp.d.ts +36 -0
  58. package/dist/keyless-mcp.js +127 -0
  59. package/dist/learn/audit.d.ts +3 -3
  60. package/dist/learn/audit.js +3 -3
  61. package/dist/learn/commit.d.ts +8 -7
  62. package/dist/learn/commit.js +60 -20
  63. package/dist/learn/compile.d.ts +4 -3
  64. package/dist/learn/compile.js +7 -5
  65. package/dist/learn/folder.d.ts +16 -3
  66. package/dist/learn/folder.js +46 -13
  67. package/dist/learn/promote.d.ts +37 -13
  68. package/dist/learn/promote.js +112 -40
  69. package/dist/learn/secrets.d.ts +20 -10
  70. package/dist/learn/secrets.js +44 -176
  71. package/dist/learning-routes.d.ts +22 -17
  72. package/dist/learning-routes.js +40 -39
  73. package/dist/learning.js +2 -2
  74. package/dist/llms.txt +1 -1
  75. package/dist/manifest.d.ts +10 -0
  76. package/dist/manifest.js +30 -0
  77. package/dist/mcp-pin-first.d.ts +4 -2
  78. package/dist/mcp-pin-first.js +5 -2
  79. package/dist/mcp-secret-writes.d.ts +16 -7
  80. package/dist/mcp-secret-writes.js +35 -27
  81. package/dist/mcp-servers-store.d.ts +30 -6
  82. package/dist/mcp-servers-store.js +112 -28
  83. package/dist/mcp-sources.d.ts +10 -2
  84. package/dist/mcp-sources.js +9 -2
  85. package/dist/mcp-store.d.ts +13 -7
  86. package/dist/mcp-store.js +12 -9
  87. package/dist/model-form.d.ts +3 -2
  88. package/dist/model-form.js +11 -7
  89. package/dist/model.d.ts +20 -4
  90. package/dist/model.js +68 -9
  91. package/dist/native/history.d.ts +1 -1
  92. package/dist/native/history.js +2 -2
  93. package/dist/ollama-provider.d.ts +92 -0
  94. package/dist/ollama-provider.js +495 -0
  95. package/dist/openapi/agentrun-v2.openapi.json +2 -2
  96. package/dist/openapi.d.ts +3 -3
  97. package/dist/openapi.js +197 -55
  98. package/dist/page.d.ts +11 -3
  99. package/dist/page.js +22 -7
  100. package/dist/place-offers.d.ts +25 -1
  101. package/dist/place-offers.js +62 -8
  102. package/dist/print.js +1 -1
  103. package/dist/purged-workflow.d.ts +35 -7
  104. package/dist/purged-workflow.js +73 -26
  105. package/dist/redact/match.d.ts +74 -0
  106. package/dist/redact/match.js +771 -0
  107. package/dist/run-computer.d.ts +2 -0
  108. package/dist/run-computer.js +2 -0
  109. package/dist/run-files.d.ts +4 -0
  110. package/dist/run-files.js +4 -0
  111. package/dist/run-keys.d.ts +2 -4
  112. package/dist/run-keys.js +6 -7
  113. package/dist/run-launch.d.ts +4 -0
  114. package/dist/run-launch.js +4 -0
  115. package/dist/run-reads-over-store.d.ts +3 -2
  116. package/dist/run-reads-over-store.js +66 -2
  117. package/dist/run-secrets.js +5 -2
  118. package/dist/run-timeline-served.d.ts +1 -1
  119. package/dist/run-timeline-served.js +4 -4
  120. package/dist/runner/adapter-loop.d.ts +9 -3
  121. package/dist/runner/adapter-loop.js +34 -15
  122. package/dist/runner/adapter-run-spec.d.ts +3 -0
  123. package/dist/runner/adapter-run-spec.js +9 -4
  124. package/dist/runner/audit.js +8 -4
  125. package/dist/runner/beside.d.ts +2 -1
  126. package/dist/runner/beside.js +13 -5
  127. package/dist/runner/claim-secrets.d.ts +5 -8
  128. package/dist/runner/claim-secrets.js +6 -0
  129. package/dist/runner/cli-run-spec.d.ts +1 -1
  130. package/dist/runner/cli-run-spec.js +41 -7
  131. package/dist/runner/harness-loops.d.ts +22 -9
  132. package/dist/runner/harness-loops.js +21 -11
  133. package/dist/runner/held.d.ts +3 -0
  134. package/dist/runner/learning-end.d.ts +1 -1
  135. package/dist/runner/learning-end.js +8 -3
  136. package/dist/runner/loop-contract.d.ts +9 -4
  137. package/dist/runner/main.js +12 -6
  138. package/dist/runner/mcp-claim.d.ts +3 -0
  139. package/dist/runner/mcp-claim.js +26 -2
  140. package/dist/runner/mcp-run.d.ts +3 -0
  141. package/dist/runner/mcp-run.js +2 -1
  142. package/dist/runner/metrics.d.ts +10 -10
  143. package/dist/runner/metrics.js +19 -38
  144. package/dist/runner/model-route.d.ts +3 -0
  145. package/dist/runner/model-route.js +6 -2
  146. package/dist/runner/presence.d.ts +4 -3
  147. package/dist/runner/presence.js +4 -2
  148. package/dist/runner/promotion.d.ts +15 -9
  149. package/dist/runner/promotion.js +25 -15
  150. package/dist/runner/run-child-host.d.ts +2 -0
  151. package/dist/runner/run-child-host.js +19 -9
  152. package/dist/runner/run-child.js +3 -1
  153. package/dist/runner/runner.d.ts +4 -0
  154. package/dist/runner/runner.js +33 -3
  155. package/dist/runner/sdk-loop.js +2 -0
  156. package/dist/runner/workflow-loop.d.ts +2 -2
  157. package/dist/runner/workflow-loop.js +1 -2
  158. package/dist/runner/workflow-spec.d.ts +1 -1
  159. package/dist/runner/workflow-spec.js +6 -6
  160. package/dist/sdk/check.d.ts +4 -1
  161. package/dist/sdk/check.js +2 -1
  162. package/dist/sdk/runs.d.ts +2 -2
  163. package/dist/sdk/runs.js +8 -6
  164. package/dist/secret-cut.js +2 -1
  165. package/dist/secret-writes.d.ts +3 -2
  166. package/dist/secret-writes.js +1 -1
  167. package/dist/server.d.ts +49 -10
  168. package/dist/server.js +324 -118
  169. package/dist/standard-webhooks.d.ts +11 -0
  170. package/dist/standard-webhooks.js +20 -2
  171. package/dist/store/dialect.d.ts +3 -0
  172. package/dist/store/dialect.js +1 -0
  173. package/dist/store/index.d.ts +2 -2
  174. package/dist/store/index.js +1 -1
  175. package/dist/store/postgres/dialect.js +3 -0
  176. package/dist/store/schema.js +5 -2
  177. package/dist/store/sqlite/store.js +133 -47
  178. package/dist/store/store.d.ts +64 -15
  179. package/dist/store/store.js +7 -0
  180. package/dist/store-reads/journal.d.ts +7 -2
  181. package/dist/store-reads/journal.js +9 -6
  182. package/dist/store-reads/learning.d.ts +12 -9
  183. package/dist/store-reads/learning.js +18 -24
  184. package/dist/store-view.d.ts +0 -5
  185. package/dist/store-view.js +10 -9
  186. package/dist/template-eval.d.ts +51 -6
  187. package/dist/template-eval.js +177 -53
  188. package/dist/template-import.js +4 -2
  189. package/dist/templates.d.ts +5 -3
  190. package/dist/templates.js +19 -40
  191. package/dist/timeline/index.d.ts +1 -1
  192. package/dist/timeline/index.js +1 -1
  193. package/dist/timeline/labels.d.ts +2 -2
  194. package/dist/timeline/labels.js +2 -2
  195. package/dist/timeline/timeline.d.ts +14 -9
  196. package/dist/timeline/timeline.js +15 -7
  197. package/dist/tool-gate.js +3 -1
  198. package/dist/tool-plane.d.ts +17 -9
  199. package/dist/tool-plane.js +38 -17
  200. package/dist/tools.js +2 -2
  201. package/dist/trigger-launch-hook.d.ts +16 -8
  202. package/dist/trigger-launch-hook.js +32 -23
  203. package/dist/trigger-targets.d.ts +18 -8
  204. package/dist/trigger-targets.js +42 -19
  205. package/dist/triggers.d.ts +85 -23
  206. package/dist/triggers.js +303 -96
  207. package/dist/validate.js +1 -1
  208. package/dist/web-fake.js +3 -3
  209. package/dist/web-fetch-direct.d.ts +4 -0
  210. package/dist/web-fetch-direct.js +8 -1
  211. package/dist/web-fetch.d.ts +12 -3
  212. package/dist/web-fetch.js +47 -14
  213. package/dist/web-search.d.ts +33 -4
  214. package/dist/web-search.js +100 -41
  215. package/dist/webhooks.js +4 -5
  216. package/dist/workspace/computer-deps.d.ts +2 -1
  217. package/dist/workspace/computer-deps.js +5 -4
  218. package/dist/workspace/computer-for-claim.d.ts +8 -2
  219. package/dist/workspace/computer-for-claim.js +28 -9
  220. package/dist/workspace/computer-janitor.d.ts +15 -7
  221. package/dist/workspace/computer-janitor.js +33 -8
  222. package/dist/workspace/computer-kept.d.ts +52 -4
  223. package/dist/workspace/computer-kept.js +118 -6
  224. package/dist/workspace/run-end-computers.d.ts +5 -1
  225. package/dist/workspace/run-end-computers.js +27 -1
  226. package/dist/wrapped.d.ts +57 -0
  227. package/dist/wrapped.js +51 -0
  228. package/launcher/agentrun-serve +5 -1
  229. package/package.json +17 -13
  230. package/templates/README.md +10 -9
  231. package/dist/console/assets/index-B_85An2t.js +0 -745
  232. package/dist/console/assets/index-n1YsbnYh.css +0 -1
  233. package/dist/import-sources.d.ts +0 -116
  234. package/dist/import-sources.js +0 -693
@@ -15,7 +15,11 @@
15
15
  // to be replaced. The claim that makes one says its kind (`made`), which the adapter loop records in the run's
16
16
  // journal. When a later claim's replaces an earlier claim's (`replaced`: one this claim let go of, or one the run's
17
17
  // journal says an earlier claim made, hadComputer), the ready line says so (and that the kept one's time had passed,
18
- // `lapsed`), and the run's tools and model are told (run-tools.ts, the adapter loop's computer note).
18
+ // `lapsed`), and the run's tools and model are told (run-tools.ts, the adapter loop's computer note). A kept one only
19
+ // the runner that kept it can reach again (a Docker container, a machine that dials that runner: computer-kept.ts
20
+ // pinnedOf) names that runner and its incarnation on its row (`ClaimLaunch.holder`), and the store's claim rule lets no
21
+ // other runner claim the run while that runner is live and the keep lasts; one taken after it is gone and not reached
22
+ // here is said to be lost with it (`lost`).
19
23
  //
20
24
  // A sandbox the claim made can be lost mid-run (its vendor deleted it, or no longer has it): the call that finds it lost
21
25
  // fails, saying so and that it was not run again (code gone, computer-lost.ts lostInCallWords), and is never sent to the
@@ -51,7 +55,7 @@ import { pairedProvider, withPaired } from "./attached-computers.js";
51
55
  import { computerHold, heldHands } from "./computer-hold.js";
52
56
  import { endingDeletes, figuresOf, intervalWrites } from "./computer-intervals.js";
53
57
  import { computeMeter } from "../compute-cost.js";
54
- import { deleteKept, endKept, KEEP_MS, readKept, takeKept, writeKept } from "./computer-kept.js";
58
+ import { deleteKept, endKept, KEEP_MS, keptBy, pinnedOf, readKept, takeKept, writeKept } from "./computer-kept.js";
55
59
  import { storeLedger } from "./computer-ledger.js";
56
60
  import { providerKeys, registryHere, resolveComputer } from "./resolve-computer.js";
57
61
  import { claimHolds, fencedStore, holdSynced, storedFolder } from "./synced-folder.js";
@@ -650,9 +654,16 @@ store, deps) {
650
654
  let kept = store.settings && launch.epoch > 1 ? await readKept(store.settings, launch.runId).catch(() => undefined) : undefined;
651
655
  /** The kept computer's time passed before this claim: the new one says so (Replaced.lapsed). */
652
656
  let lapsed = false;
657
+ /** The kept computer was pinned to another runner, gone by this claim, and this claim could not reach it: the new one
658
+ * says so (Replaced.lost). */
659
+ let lost = false;
653
660
  /** What a park of this claim keeps (ClaimComputer.park): a desktop, a phone or a sandbox it made through its provider, or took
654
- * back, with its provider, its connection's close and the claim epoch its labels carry. */
661
+ * back, with its provider, its connection's close and the claim epoch its labels carry; `pinned`: only this runner can
662
+ * reach it again (computer-kept.ts pinnedOf), so the park names this claim's holder on its row. */
655
663
  let keepable;
664
+ /** Whether a computer of `entry` made with `configuration` is reachable again only by this runner: its options as
665
+ * vendorProvider makes it (the settings' options under the configuration's). */
666
+ const pinnedHere = (entry, configuration) => pinnedOf(entry, { ...settingOptions(entry.id, deps.settings ?? {}, table), ...configuration.options });
656
667
  /** A vendor's provider (or a local one's), made with the keys and the configuration the setting selected: that
657
668
  * configuration is built by its own create() options (Orgo's Windows: { os: "windows" }), beside the keys'. */
658
669
  const vendorProvider = async (entry, keys, configuration) => {
@@ -770,6 +781,10 @@ store, deps) {
770
781
  return undefined;
771
782
  if (persistent || paired.some((row) => row.id === entry.id) || (deps.launch && (deps.launches?.(entry.id) ?? true)) || record.provider !== entry.id || record.kind !== kind || record.folder !== folder)
772
783
  return undefined;
784
+ // Kept pinned to another runner (or another process of this runner's name): the claim rule let this claim have the
785
+ // run only once that runner was gone. Taken off its row, it is tried here all the same (a runner on the same host,
786
+ // sharing its Docker engine, finds it); one this claim cannot reach is lost with that runner, said so.
787
+ const elsewhere = record.runner !== undefined && !keptBy(record, launch.holder);
773
788
  try {
774
789
  // Its time over, or its end took it first (ending or ended): a new one, said so.
775
790
  if (record.ending || record.ended || now() >= record.until || !(await takeKept(store.settings, launch.runId, record))) {
@@ -778,8 +793,10 @@ store, deps) {
778
793
  }
779
794
  const provider = await vendorProvider(entry, keys, configuration);
780
795
  const taken = await keptLaunch(provider, record, signal, () => writes.ended(entry.id, record.id));
781
- if (!taken)
796
+ if (!taken) {
797
+ lost = elsewhere;
782
798
  return undefined;
799
+ }
783
800
  // Metered again from here, with what its interval says it was made with: the time it was kept is no claim's.
784
801
  const row = meter ? (await store.computerIntervals?.forRun(launch.runId).catch(() => undefined))?.find((one) => one.provider === entry.id && one.providerId === record.id) : undefined;
785
802
  if (row)
@@ -788,7 +805,7 @@ store, deps) {
788
805
  // A sandbox taken back is started again when it is lost as one this claim made (lostAware): the new one is this
789
806
  // claim's, made from this claim's spec, its folder mounted.
790
807
  const again = record.kind === "sandbox" ? lostAware(taken, renewalOf(provider, entry, () => specOf(entry, provider, kind, configuration, deps.signal, gpu), folder)) : taken;
791
- keepable = { provider, entry: entry.id, kind: record.kind, computer: again, epoch: record.epoch };
808
+ keepable = { provider, entry: entry.id, kind: record.kind, computer: again, pinned: pinnedHere(entry, configuration), epoch: record.epoch };
792
809
  ownKind = record.kind;
793
810
  const screen = record.kind === "sandbox" ? undefined : record.kind;
794
811
  made = { id: again.id, machine: again.machine, ...(again.watch && screen ? { watch: again.watch, gone: goneOf(provider, again.id, runLabels(launch.runId, record.epoch), screen) } : {}) };
@@ -798,6 +815,8 @@ store, deps) {
798
815
  return { ...again, epoch: record.epoch };
799
816
  }
800
817
  catch {
818
+ // Taken off its row (or not), and not reached here: lost with its runner where it was pinned to another.
819
+ lost = elsewhere && !lapsed;
801
820
  return undefined;
802
821
  }
803
822
  },
@@ -861,9 +880,9 @@ store, deps) {
861
880
  if (!pairedRow)
862
881
  ownKind = kind;
863
882
  if (!pairedRow && (kind === "desktop" || kind === "phone" || kind === "sandbox"))
864
- keepable = { provider, entry: entry.id, kind, computer: held, epoch: launch.epoch };
865
- if (!pairedRow && (releasedOlder || journalSays))
866
- replaced = { kind: kind, folderKept: deps.mode === 2 && mount !== undefined, ...(lapsed ? { lapsed: true } : {}) };
883
+ keepable = { provider, entry: entry.id, kind, computer: held, pinned: pinnedHere(entry, configuration), epoch: launch.epoch };
884
+ if (!pairedRow && (releasedOlder || journalSays || lost))
885
+ replaced = { kind: kind, folderKept: deps.mode === 2 && mount !== undefined, ...(lost ? { lost: true } : lapsed ? { lapsed: true } : {}) };
867
886
  }
868
887
  made = { id: launched.id, machine: launched.machine, ...(launched.watch ? { watch: launched.watch, ...(kind === "desktop" || kind === "phone" ? { gone: goneOf(provider, launched.id, listedUnder, kind) } : {}) } : {}) };
869
888
  madeBy = entry;
@@ -1026,7 +1045,7 @@ store, deps) {
1026
1045
  }
1027
1046
  let row;
1028
1047
  try {
1029
- row = await writeKept(settings, launch.runId, { provider: keeps.entry, kind: keeps.kind, id: ref.id, name: ref.name, epoch: keeps.epoch, until: now() + KEEP_MS, ...(resolved.folder ? { folder: resolved.folder } : {}) });
1048
+ row = await writeKept(settings, launch.runId, { provider: keeps.entry, kind: keeps.kind, id: ref.id, name: ref.name, epoch: keeps.epoch, until: now() + KEEP_MS, ...(resolved.folder ? { folder: resolved.folder } : {}), ...(keeps.pinned && launch.holder ? { runner: launch.holder.runner, incarnation: launch.holder.incarnation } : {}) });
1030
1049
  }
1031
1050
  catch {
1032
1051
  // No row, nothing to find it by: it goes as at the run's end.
@@ -2,7 +2,7 @@ import { type Result, type Swept } from "@parcha/agentrun-computer";
2
2
  import { type KeyTable, type ProviderSettings } from "../computer-keys.js";
3
3
  import { type Store } from "../store/index.js";
4
4
  import type { ComputerDeps } from "./computer-deps.js";
5
- import { type KeptSweep } from "./computer-kept.js";
5
+ import { type KeptHolder, type KeptSweep } from "./computer-kept.js";
6
6
  /** How often the janitor sweeps after its first sweep at the runner's start. */
7
7
  export declare const JANITOR_EVERY_MS: number;
8
8
  /** The longest close() waits for a sweep it cut: a vendor call that ignores the cut is left to finish on its own. */
@@ -13,7 +13,11 @@ export declare const JANITOR_PROVIDER_MS: number;
13
13
  export interface JanitorDeps {
14
14
  /** The store: whose runs a computer's run label names, and the phone provider's ledger; with `settings` and `agents`,
15
15
  * this server's id and its agents, whose own computers it then sweeps by their owner. */
16
- readonly store: Pick<Store, "runs" | "computerLedger"> & Partial<Pick<Store, "settings" | "agents" | "computerIntervals">>;
16
+ readonly store: Pick<Store, "runs" | "computerLedger"> & Partial<Pick<Store, "settings" | "agents" | "computerIntervals" | "runners">>;
17
+ /** The runner this janitor sweeps for (its name and presence incarnation): a computer kept at a park pinned to another
18
+ * runner still there is that runner's to end, never this one's: its row (computer-kept.ts sweepKept) and its machine
19
+ * in a provider's list (sparedKept). Absent: every pinned row whose runner is there is left. */
20
+ readonly self?: KeptHolder;
17
21
  /** The run's computers as the runner composed them (computer-deps.ts computerDepsFrom): the registry, the provider
18
22
  * makers and the key reader. */
19
23
  readonly computers: Pick<ComputerDeps, "kinds" | "providers" | "keyOf"> & {
@@ -38,15 +42,19 @@ export type ProviderSweep = {
38
42
  readonly swept: Result<Swept>;
39
43
  };
40
44
  /** One sweep of every provider this server can list: each provider whose keys it has and whose computers the janitor
41
- * ends, its computers past their deadline that are this server's and unheld ended. A provider that fails is answered
42
- * in its words; nothing throws. */
43
- export declare function sweepServerComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now" | "providerMs"> & {
45
+ * ends, its computers past their deadline that are this server's and unheld ended. A computer kept at a park pinned to
46
+ * another runner still there is left out of every list (computer-kept.ts sparedKept, by the kept pass's own rule): a
47
+ * provider both runners list (a dial vendor's account, a shared Docker engine) would otherwise end, once its run is
48
+ * over, a machine the kept pass left to its runner. Where the kept rows can't be read, each provider is answered as not
49
+ * swept this pass. A provider that fails is answered in its words; nothing throws. */
50
+ export declare function sweepServerComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now" | "providerMs" | "self"> & {
44
51
  readonly log?: (line: string) => void;
45
52
  }, signal?: AbortSignal): Promise<ProviderSweep[]>;
46
53
  /** The pass over the desktops and phones kept at a run's park (computer-kept.ts sweepKept): one past its time, or whose
47
54
  * run ended, is ended through its provider, made with this server's keys, whatever its lifetime (a vendor's own end is
48
- * hours away). Undefined without the store's settings, where none is kept. Never throws. */
49
- export declare function sweepKeptComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now"> & {
55
+ * hours away); one pinned to another runner still there (the store's runners) is left to that runner. Undefined without
56
+ * the store's settings, where none is kept. Never throws. */
57
+ export declare function sweepKeptComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now" | "self"> & {
50
58
  readonly log?: (line: string) => void;
51
59
  }): Promise<KeptSweep | undefined>;
52
60
  /** The janitor: one sweep now, then one every `everyMs`, never two at once, and a kept pass at the earliest time a kept
@@ -17,7 +17,10 @@
17
17
  // not asked, since it reads labels alone and can't tell this server's parts from another's.
18
18
  //
19
19
  // A desktop or a phone kept at a run's park (computer-kept.ts) goes by its own row, on any provider: each sweep ends one
20
- // past its time or whose run ended, and the janitor wakes again at the earliest time still ahead.
20
+ // past its time or whose run ended, and the janitor wakes again at the earliest time still ahead. One pinned to another
21
+ // runner still there is that runner's (computer-kept.ts heldElsewhere): the kept pass leaves its row, and the provider
22
+ // sweep leaves its machine out of every list by the same rule (sparedKept), so a run that ended on this runner never has
23
+ // this janitor end a machine another runner keeps.
21
24
  //
22
25
  // A provider whose sweep fails is logged, once per distinct failure, and the others are swept. It runs only where its
23
26
  // start asks for it (the CLI's runner and the runner subcommand), never under --faux, and never two sweeps at once. Its
@@ -32,7 +35,7 @@ import { KEY_TABLE, providerOptions } from "../computer-keys.js";
32
35
  import { FAUX_ENV } from "../model.js";
33
36
  import { FINAL_STATES } from "../store/index.js";
34
37
  import { deleteUnderHold, ownerGoneOf, serverIdOf, wantsOn } from "./agent-computer.js";
35
- import { sweepKept } from "./computer-kept.js";
38
+ import { sparedKept, sweepKept } from "./computer-kept.js";
36
39
  import { storeLedger } from "./computer-ledger.js";
37
40
  import { providerKeys } from "./resolve-computer.js";
38
41
  import { intervalWrites } from "./computer-intervals.js";
@@ -79,7 +82,7 @@ async function sweepable(store, info, byHash, serverId) {
79
82
  * now: a server that stopped before its run's end let go of an earlier claim's computer (run-end-computers.ts) leaves
80
83
  * it for this sweep, never for its day-long deadline. Each call goes to the provider itself, so its own state is used
81
84
  * as it is. */
82
- function thisServers(provider, store, byHash, serverId, at, ended) {
85
+ function thisServers(provider, store, byHash, serverId, at, ended, spared) {
83
86
  // The agents' own computers the list kept, by id: their delete goes under the agent's hold (deleteUnderHold), which
84
87
  // also clears the agent's record of it, so its view no longer shows it deleting.
85
88
  const ownedBy = new Map();
@@ -113,6 +116,9 @@ function thisServers(provider, store, byHash, serverId, at, ended) {
113
116
  return listed;
114
117
  const kept = [];
115
118
  for (const info of listed.value) {
119
+ // A machine kept at a park for another runner still there (sparedKept): that runner's to end, never this sweep's.
120
+ if (spared(info.ref.id))
121
+ continue;
116
122
  const mine = await sweepable(store, info, byHash, serverId);
117
123
  if (!mine)
118
124
  continue;
@@ -144,8 +150,11 @@ async function bounded(ms, signal, work) {
144
150
  }
145
151
  }
146
152
  /** One sweep of every provider this server can list: each provider whose keys it has and whose computers the janitor
147
- * ends, its computers past their deadline that are this server's and unheld ended. A provider that fails is answered
148
- * in its words; nothing throws. */
153
+ * ends, its computers past their deadline that are this server's and unheld ended. A computer kept at a park pinned to
154
+ * another runner still there is left out of every list (computer-kept.ts sparedKept, by the kept pass's own rule): a
155
+ * provider both runners list (a dial vendor's account, a shared Docker engine) would otherwise end, once its run is
156
+ * over, a machine the kept pass left to its runner. Where the kept rows can't be read, each provider is answered as not
157
+ * swept this pass. A provider that fails is answered in its words; nothing throws. */
149
158
  export async function sweepServerComputers(deps, signal) {
150
159
  const { store, computers } = deps;
151
160
  const writes = intervalWrites(store, deps.log, deps.now);
@@ -154,6 +163,16 @@ export async function sweepServerComputers(deps, signal) {
154
163
  const byHash = runsByHash(store);
155
164
  // This server's id and its agents: its agents' own computers are swept by their owner (ownerGoneOf).
156
165
  const { settings, agents } = store;
166
+ // The kept machines this sweep leaves, read once for every provider; with no settings, nothing is kept.
167
+ let spared = () => false;
168
+ if (settings) {
169
+ try {
170
+ spared = await sparedKept({ settings, ...(store.runners ? { runners: store.runners } : {}), ...(deps.self ? { self: deps.self } : {}) });
171
+ }
172
+ catch (error) {
173
+ spared = { unread: `the computers kept at a park could not be read, so none of its computers is swept this pass: ${error instanceof Error ? error.message : String(error)}` };
174
+ }
175
+ }
157
176
  const serverId = settings && agents ? await serverIdOf({ settings }) : undefined;
158
177
  const out = [];
159
178
  for (const entry of computers.kinds.PROVIDERS) {
@@ -172,8 +191,13 @@ export async function sweepServerComputers(deps, signal) {
172
191
  // A vendor that ends its own computers needs no janitor.
173
192
  if (provider.capabilities().lifetime !== "janitor")
174
193
  continue;
194
+ if (typeof spared !== "function") {
195
+ out.push({ provider: entry.id, swept: { ok: false, error: { code: "unreachable", message: spared.unread } } });
196
+ continue;
197
+ }
198
+ const leaves = spared;
175
199
  const ownerGone = serverId !== undefined && settings && agents ? ownerGoneOf({ agents, settings, runs: store.runs }, serverId, entry.id) : undefined;
176
- out.push({ provider: entry.id, swept: await bounded(deps.providerMs ?? JANITOR_PROVIDER_MS, signal, (cut) => sweep(thisServers(provider, store, byHash, serverId, at, (id) => writes.ended(entry.id, id)), { now: new Date(at), signal: cut, ...(ownerGone ? { ownerGone } : {}) })) });
200
+ out.push({ provider: entry.id, swept: await bounded(deps.providerMs ?? JANITOR_PROVIDER_MS, signal, (cut) => sweep(thisServers(provider, store, byHash, serverId, at, (id) => writes.ended(entry.id, id), (id) => leaves(entry.id, id)), { now: new Date(at), signal: cut, ...(ownerGone ? { ownerGone } : {}) })) });
177
201
  }
178
202
  catch (error) {
179
203
  out.push({ provider: entry.id, swept: { ok: false, error: { code: "refused", message: error instanceof Error ? error.message : String(error) } } });
@@ -183,7 +207,8 @@ export async function sweepServerComputers(deps, signal) {
183
207
  }
184
208
  /** The pass over the desktops and phones kept at a run's park (computer-kept.ts sweepKept): one past its time, or whose
185
209
  * run ended, is ended through its provider, made with this server's keys, whatever its lifetime (a vendor's own end is
186
- * hours away). Undefined without the store's settings, where none is kept. Never throws. */
210
+ * hours away); one pinned to another runner still there (the store's runners) is left to that runner. Undefined without
211
+ * the store's settings, where none is kept. Never throws. */
187
212
  export async function sweepKeptComputers(deps) {
188
213
  const { store, computers } = deps;
189
214
  const writes = intervalWrites(store, deps.log, deps.now);
@@ -197,7 +222,7 @@ export async function sweepKeptComputers(deps) {
197
222
  const options = providerOptions(id, await keysOf(id), computers.settings ?? {}, computers.keyTable ?? KEY_TABLE);
198
223
  return options.ok ? make(options.options, { ledger: storeLedger(store.computerLedger, id) }) : undefined;
199
224
  };
200
- return sweepKept({ settings: store.settings, runs: store.runs, now: (deps.now ?? Date.now)(), providerOf, deleted: (provider, id) => writes.ended(provider, id) });
225
+ return sweepKept({ settings: store.settings, runs: store.runs, now: (deps.now ?? Date.now)(), providerOf, deleted: (provider, id) => writes.ended(provider, id), ...(store.runners ? { runners: store.runners } : {}), ...(deps.self ? { self: deps.self } : {}) });
201
226
  }
202
227
  /** The janitor: one sweep now, then one every `everyMs`, never two at once, and a kept pass at the earliest time a kept
203
228
  * computer is due (sweepKeptComputers; each delete bounded, KEPT_DELETE_MS), so a park's keep ends on time though the
@@ -1,5 +1,5 @@
1
- import type { ComputerProvider, MountKind } from "@parcha/agentrun-computer";
2
- import { type Settings, type Store } from "../store/index.js";
1
+ import { type ComputerProvider, type MountKind, type ProviderEntry } from "@parcha/agentrun-computer";
2
+ import { type Runners, type Settings, type Store } from "../store/index.js";
3
3
  /** How long a park keeps the run's computer for its resume: 10 minutes. */
4
4
  export declare const KEEP_MS: number;
5
5
  /** KEEP_MS in words. */
@@ -17,9 +17,13 @@ export type ParkedEnd = {
17
17
  export declare const parkedWords: (kind: string, cause: ParkCause) => string;
18
18
  /** Why the run's computer at its resume is a new one: the one kept at its park was ended at its time. */
19
19
  export declare const computerLapsedWords: (kind: string) => string;
20
+ /** Why the run's computer at its resume is a new one: the one kept at its park was pinned to the runner that kept it
21
+ * (pinnedOf), that runner was gone by the resume, and the claiming runner could not reach it. Never names the runner:
22
+ * a runner's name carries its host's. */
23
+ export declare const computerLostWords: (kind: string) => string;
20
24
  /** The live view's words once the kept computer's time passed, the run still parked. */
21
25
  export declare const lapsedWords: (kind: string) => string;
22
- /** The settings key prefix of the kept computers, one row per run. */
26
+ /** The settings key prefix of the kept computers, one row per run (the store's, which its claim rule reads). */
23
27
  export declare const KEPT_PREFIX = "computer_kept:";
24
28
  /** The settings key of `runId`'s kept computer. */
25
29
  export declare const keptKeyOf: (runId: string) => string;
@@ -36,9 +40,45 @@ export type Kept = {
36
40
  /** The kind of the run's folder it holds (a bind, an Archil mount or a synced folder); none: it holds none. A claim
37
41
  * takes it back only to hold the folder the same way (computer-for-claim.ts reattach). */
38
42
  readonly folder?: MountKind;
43
+ /** The runner that kept it and that runner's process (its presence incarnation), where only that runner can reach it
44
+ * again (pinnedOf): both or neither. Absent: any runner takes it back. */
45
+ readonly runner?: string;
46
+ readonly incarnation?: string;
39
47
  readonly ending?: true;
40
48
  readonly ended?: true;
41
49
  };
50
+ /** The runner a claim runs in: its name (the claim's runner) and its process's presence incarnation. */
51
+ export type KeptHolder = {
52
+ readonly runner: string;
53
+ readonly incarnation: string;
54
+ };
55
+ /** Whether a computer of `entry`, made with `options` (its settings' options under the configuration's, as the claim makes
56
+ * its provider), can be reached again only by the runner that kept it: a local provider's (a container on that
57
+ * runner's Docker engine), or one whose machines dial this server (the computer package's connectOf "dial": its socket
58
+ * is in that runner's attach hub, its token names that runner). A connect the computer package refuses reads as exec,
59
+ * as resolve-computer.ts reads it. */
60
+ export declare function pinnedOf(entry: Pick<ProviderEntry, "label" | "connect" | "local">, options?: Readonly<Record<string, unknown>>): boolean;
61
+ /** Whether `kept` names `holder` as the runner that keeps it (the same name and the same process). */
62
+ export declare const keptBy: (kept: Pick<Kept, "runner" | "incarnation">, holder: KeptHolder | undefined) => boolean;
63
+ /** Who holds what, for one janitor pass: the runners there now (Runners.live) and the runner the pass runs in (`self`). */
64
+ export type KeptHolders = {
65
+ /** Absent: none is read, and no row counts as held elsewhere. */
66
+ readonly runners?: Pick<Runners, "live">;
67
+ readonly self?: KeptHolder;
68
+ };
69
+ /** The one rule both janitor sweeps leave a kept machine by (sweepKept, and the provider sweep through sparedKept): its
70
+ * row is pinned to a runner other than `self` that is still there under the row's incarnation (seen within
71
+ * PRESENCE_STALE_MS). Only that runner's engine or hub has the machine, so a delete from here either finds nothing (and
72
+ * marks the row ended, leaking it) or, where both runners list it (a dial vendor's account, a shared Docker engine),
73
+ * ends a machine its runner still keeps. The runners are read once per maker's call. A read that fails counts every
74
+ * pinned row as held elsewhere: ending another runner's machine blind would leak it or take it. */
75
+ export declare function heldElsewhere(holders: KeptHolders): (kept: Pick<Kept, "runner" | "incarnation">) => Promise<boolean>;
76
+ /** The kept machines a provider sweep leaves this pass, by provider id and computer id: each row not yet ended whose
77
+ * machine is held elsewhere (heldElsewhere, the kept pass's own rule). Throws when the rows can't be read: the sweep
78
+ * then can't tell which machines are kept, and must not guess. */
79
+ export declare function sparedKept(deps: KeptHolders & {
80
+ readonly settings: Pick<KeptSettings, "all">;
81
+ }): Promise<(provider: string, id: string) => boolean>;
42
82
  /** How long one delete of a kept computer may take at the janitor's pass: past it the pass goes on, the row is left
43
83
  * ending, and the next pass deletes it again (a Docker command has no timeout of its own). */
44
84
  export declare const KEPT_DELETE_MS = 60000;
@@ -71,7 +111,10 @@ export type KeptSweep = {
71
111
  * deleted (unless its time already ended it) and the row removed; one past its time, or left ending, is ended (endKept),
72
112
  * each delete bounded by `deleteMs` (KEPT_DELETE_MS). The rest are left, the earliest of their times answered as `next`.
73
113
  * `providerOf`: the provider a row names, made with this server's keys, or undefined when this server can't make it (its
74
- * keys gone): that row is left and said. Never throws.
114
+ * keys gone): that row is left and said. A row pinned to a runner other than this one (`self`) that is still there under
115
+ * the row's incarnation (`runners`, seen within PRESENCE_STALE_MS) is left whatever its time or its run: only that
116
+ * runner's engine or hub has its machine, so a delete from here would find nothing, mark it ended and leak the machine.
117
+ * That runner's own timer, or its janitor, ends it; once it is gone the row is ended here as any other. Never throws.
75
118
  */
76
119
  export declare function sweepKept(deps: {
77
120
  readonly settings: KeptSettings;
@@ -82,6 +125,11 @@ export declare function sweepKept(deps: {
82
125
  /** Told of each kept computer whose delete answered ok, by its provider and id (its interval's end,
83
126
  * computer-intervals.ts). Never throws. */
84
127
  readonly deleted?: (provider: string, id: string) => Promise<void>;
128
+ /** The runners there now (Runners.live): a row pinned to one of them, not `self`, is left. Absent: none is read, and
129
+ * every row is swept as before. */
130
+ readonly runners?: Pick<Runners, "live">;
131
+ /** The runner this sweep runs in (its name and presence incarnation): the rows pinned to it are its own to end. */
132
+ readonly self?: KeptHolder;
85
133
  }): Promise<KeptSweep>;
86
134
  /** Deletes the kept computer through its provider; one already gone (its run's end let it go, run-end-computers.ts, or
87
135
  * its vendor ended it) is as good as deleted. Throws the provider's words on any other failure. True when the
@@ -1,4 +1,46 @@
1
- import { FINAL_STATES } from "../store/index.js";
1
+ // A desktop, a phone or a sandbox kept through its run's park. A run's own computer goes with its claim, and a park
2
+ // releases the claim; so a run that parked for a person (a tool's approval, a cap raise, a question) would resume on a new
3
+ // machine, with nothing the agent set up on the old one. Instead, the claim that parks keeps a desktop, a phone or a
4
+ // sandbox it made (or took back) for KEEP_MS: an answer within that time resumes the run on the same machine, its screen,
5
+ // files and programs as they were (computer-for-claim.ts: the claim's park, and the resumed claim's re-attach). After it,
6
+ // the kept computer is ended, and a later resume makes a new one, said as any replaced computer is, and why.
7
+ //
8
+ // One server setting per run holds it (`computer_kept:<run id>`): its provider, its kind, its ref, the claim epoch its
9
+ // labels carry, and the time it is kept until. The claim that parks writes it; the claim that resumes in time takes it
10
+ // off and attaches the computer. At its time it is ended by the parking runner's own timer and, whatever became of
11
+ // that process (a server's restart), by the janitor (computer-janitor.ts), which reads every row at each sweep and wakes
12
+ // at the earliest time still ahead. Ending one marks its row ending (no claim takes it after), deletes the computer, then
13
+ // marks the row ended, so the resume says why its computer is new; a row left ending (its process died, or its delete
14
+ // failed or hung mid-way) is deleted again at the janitor's next pass. A row whose run is final is removed, its computer
15
+ // deleted first.
16
+ //
17
+ // A sandbox's run folder is the run's either way (bound from this machine, or on Archil, as before); what the keep adds is
18
+ // the rest of the machine (its other files, its processes). A synced folder's delta lands on the disk at the park; what
19
+ // its programs write after it is read back whole by the claim that takes it back. One whose keep lapses is deleted, by
20
+ // the parking runner's own timer or the janitor, with no read-back: those writes are lost, as a runner lost loses an
21
+ // unanswered call's. A claim takes one back only to hold the run's folder the same way (its row's `folder`). An agent's
22
+ // own computer and a paired one outlive every claim already, and are never kept here; a runner lost, a crash or a
23
+ // server's stop still gives a new one.
24
+ //
25
+ // While the run is parked, its owner's live view says what it waits for and that the computer is kept, until when, then
26
+ // that it wasn't kept once its time passed (parkedWords, lapsedWords): the viewer's page, as the claim's park closes it
27
+ // (computer-live.ts), and the console's Screen tab, until when from the park's row on the run's timeline
28
+ // (computerKeptWords) and its lapse from GET /v2/runs/{id}/computer (computer-view.ts lapsedOf).
29
+ //
30
+ // A machine only the runner that kept it can reach again is pinned to that runner (pinnedOf): a container on its Docker
31
+ // engine (a local provider), or a machine that dials this server, whose socket is in that runner's attach hub. Its row
32
+ // names the runner and its process (`runner`, `incarnation`: the presence incarnation, runner/presence.ts), and while
33
+ // that runner is there under that incarnation and the keep lasts, the claim rule gives the run to that runner alone
34
+ // (store KEPT_PREFIX: no other runner's poll picks it, nothing is claimed and given back). A stop or cancel is exempt:
35
+ // any runner claims the run at once and ends it with no loop, never taking the row (runner.ts claimMore), and the
36
+ // row's runner ends the machine at its time, or at its janitor's pass once the run is over. Once it is gone (its row stale
37
+ // for PRESENCE_STALE_MS, a new incarnation, a refusal) any runner claims the run; a claim that then finds the machine gone
38
+ // says so (computerLostWords), and its janitor never ends a row pinned to another runner still there (sweepKept): that
39
+ // runner's own timer or janitor does. A machine any runner reaches by its id (an exec vendor's sandbox, a phone) is not
40
+ // pinned, and moves.
41
+ import { connectOf } from "@parcha/agentrun-computer";
42
+ import { PRESENCE_STALE_MS } from "../runner/presence.js";
43
+ import { FINAL_STATES, KEPT_PREFIX as KEPT_ROWS } from "../store/index.js";
2
44
  /** How long a park keeps the run's computer for its resume: 10 minutes. */
3
45
  export const KEEP_MS = 10 * 60_000;
4
46
  /** KEEP_MS in words. */
@@ -16,12 +58,64 @@ const WAITING = {
16
58
  export const parkedWords = (kind, cause) => `${WAITING[cause]} The ${kind} is kept for ${KEEP_WORDS}.`;
17
59
  /** Why the run's computer at its resume is a new one: the one kept at its park was ended at its time. */
18
60
  export const computerLapsedWords = (kind) => `The ${kind} wasn't kept: ${KEEP_WORDS} passed.`;
61
+ /** Why the run's computer at its resume is a new one: the one kept at its park was pinned to the runner that kept it
62
+ * (pinnedOf), that runner was gone by the resume, and the claiming runner could not reach it. Never names the runner:
63
+ * a runner's name carries its host's. */
64
+ export const computerLostWords = (kind) => `The ${kind} wasn't kept: the runner that kept it is gone.`;
19
65
  /** The live view's words once the kept computer's time passed, the run still parked. */
20
66
  export const lapsedWords = (kind) => `${computerLapsedWords(kind)} A new one starts when the run goes on.`;
21
- /** The settings key prefix of the kept computers, one row per run. */
22
- export const KEPT_PREFIX = "computer_kept:";
67
+ /** The settings key prefix of the kept computers, one row per run (the store's, which its claim rule reads). */
68
+ export const KEPT_PREFIX = KEPT_ROWS;
23
69
  /** The settings key of `runId`'s kept computer. */
24
70
  export const keptKeyOf = (runId) => `${KEPT_PREFIX}${runId}`;
71
+ /** Whether a computer of `entry`, made with `options` (its settings' options under the configuration's, as the claim makes
72
+ * its provider), can be reached again only by the runner that kept it: a local provider's (a container on that
73
+ * runner's Docker engine), or one whose machines dial this server (the computer package's connectOf "dial": its socket
74
+ * is in that runner's attach hub, its token names that runner). A connect the computer package refuses reads as exec,
75
+ * as resolve-computer.ts reads it. */
76
+ export function pinnedOf(entry, options = {}) {
77
+ if (entry.local === true)
78
+ return true;
79
+ try {
80
+ return connectOf(entry, options) === "dial";
81
+ }
82
+ catch {
83
+ return false;
84
+ }
85
+ }
86
+ /** Whether `kept` names `holder` as the runner that keeps it (the same name and the same process). */
87
+ export const keptBy = (kept, holder) => holder !== undefined && kept.runner === holder.runner && kept.incarnation === holder.incarnation;
88
+ /** The one rule both janitor sweeps leave a kept machine by (sweepKept, and the provider sweep through sparedKept): its
89
+ * row is pinned to a runner other than `self` that is still there under the row's incarnation (seen within
90
+ * PRESENCE_STALE_MS). Only that runner's engine or hub has the machine, so a delete from here either finds nothing (and
91
+ * marks the row ended, leaking it) or, where both runners list it (a dial vendor's account, a shared Docker engine),
92
+ * ends a machine its runner still keeps. The runners are read once per maker's call. A read that fails counts every
93
+ * pinned row as held elsewhere: ending another runner's machine blind would leak it or take it. */
94
+ export function heldElsewhere(holders) {
95
+ let there;
96
+ return async (kept) => {
97
+ const { runners } = holders;
98
+ if (kept.runner === undefined || keptBy(kept, holders.self) || !runners)
99
+ return false;
100
+ there ??= runners.live(PRESENCE_STALE_MS).then((live) => new Set(live.map((one) => `${one.runner}\n${one.incarnation}`)), () => "unknown");
101
+ const live = await there;
102
+ return live === "unknown" || live.has(`${kept.runner}\n${kept.incarnation}`);
103
+ };
104
+ }
105
+ /** The kept machines a provider sweep leaves this pass, by provider id and computer id: each row not yet ended whose
106
+ * machine is held elsewhere (heldElsewhere, the kept pass's own rule). Throws when the rows can't be read: the sweep
107
+ * then can't tell which machines are kept, and must not guess. */
108
+ export async function sparedKept(deps) {
109
+ const rows = await deps.settings.all({ prefix: KEPT_PREFIX });
110
+ const elsewhere = heldElsewhere(deps);
111
+ const spared = new Set();
112
+ for (const row of rows) {
113
+ const kept = keptOf(row.value);
114
+ if (kept && !kept.ended && (await elsewhere(kept)))
115
+ spared.add(`${kept.provider}\n${kept.id}`);
116
+ }
117
+ return (provider, id) => spared.has(`${provider}\n${id}`);
118
+ }
25
119
  /** How long one delete of a kept computer may take at the janitor's pass: past it the pass goes on, the row is left
26
120
  * ending, and the next pass deletes it again (a Docker command has no timeout of its own). */
27
121
  export const KEPT_DELETE_MS = 60_000;
@@ -29,14 +123,19 @@ export const KEPT_DELETE_MS = 60_000;
29
123
  export function keptOf(value) {
30
124
  if (!value || typeof value !== "object")
31
125
  return undefined;
32
- const { provider, kind, id, name, epoch, until, folder, ending, ended } = value;
126
+ const { provider, kind, id, name, epoch, until, folder, runner, incarnation, ending, ended } = value;
33
127
  if (typeof provider !== "string" || (kind !== "desktop" && kind !== "phone" && kind !== "sandbox") || typeof id !== "string" || typeof name !== "string")
34
128
  return undefined;
35
129
  if (typeof epoch !== "number" || !Number.isSafeInteger(epoch) || typeof until !== "number" || !Number.isFinite(until))
36
130
  return undefined;
37
131
  if (folder !== undefined && folder !== "bind" && folder !== "archil" && folder !== "sync")
38
132
  return undefined;
39
- const held = folder === undefined ? {} : { folder: folder };
133
+ // Its runner and incarnation together, or neither.
134
+ if ((runner === undefined) !== (incarnation === undefined))
135
+ return undefined;
136
+ if (runner !== undefined && (typeof runner !== "string" || typeof incarnation !== "string"))
137
+ return undefined;
138
+ const held = { ...(folder === undefined ? {} : { folder: folder }), ...(typeof runner === "string" && typeof incarnation === "string" ? { runner, incarnation } : {}) };
40
139
  if (ended === true)
41
140
  return { provider, kind, id, name, epoch, until, ...held, ended: true };
42
141
  return { provider, kind, id, name, epoch, until, ...held, ...(ending === true ? { ending: true } : {}) };
@@ -45,6 +144,7 @@ export function keptOf(value) {
45
144
  const rowOf = (kept, state) => ({
46
145
  provider: kept.provider, kind: kept.kind, id: kept.id, name: kept.name, epoch: kept.epoch, until: kept.until,
47
146
  ...(kept.folder === undefined ? {} : { folder: kept.folder }),
147
+ ...(kept.runner === undefined || kept.incarnation === undefined ? {} : { runner: kept.runner, incarnation: kept.incarnation }),
48
148
  ...(state === "ending" ? { ending: true } : state === "ended" ? { ended: true } : {}),
49
149
  });
50
150
  /** `runId`'s kept computer, as its row reads now; undefined when there is none. */
@@ -79,7 +179,10 @@ export async function endKept(settings, runId, kept, end) {
79
179
  * deleted (unless its time already ended it) and the row removed; one past its time, or left ending, is ended (endKept),
80
180
  * each delete bounded by `deleteMs` (KEPT_DELETE_MS). The rest are left, the earliest of their times answered as `next`.
81
181
  * `providerOf`: the provider a row names, made with this server's keys, or undefined when this server can't make it (its
82
- * keys gone): that row is left and said. Never throws.
182
+ * keys gone): that row is left and said. A row pinned to a runner other than this one (`self`) that is still there under
183
+ * the row's incarnation (`runners`, seen within PRESENCE_STALE_MS) is left whatever its time or its run: only that
184
+ * runner's engine or hub has its machine, so a delete from here would find nothing, mark it ended and leak the machine.
185
+ * That runner's own timer, or its janitor, ends it; once it is gone the row is ended here as any other. Never throws.
83
186
  */
84
187
  export async function sweepKept(deps) {
85
188
  const ended = [];
@@ -92,6 +195,9 @@ export async function sweepKept(deps) {
92
195
  catch (error) {
93
196
  return { ended, failed: [`the kept computers could not be read: ${words(error)}`] };
94
197
  }
198
+ // The runners there now, read once for the pass, only where a row is pinned to another runner (heldElsewhere: the
199
+ // provider sweep leaves the same machines by the same rule, computer-janitor.ts).
200
+ const othersThere = heldElsewhere(deps);
95
201
  for (const row of rows) {
96
202
  const runId = row.key.slice(KEPT_PREFIX.length);
97
203
  const kept = keptOf(row.value);
@@ -107,6 +213,12 @@ export async function sweepKept(deps) {
107
213
  await deps.settings.removeIf(row.key, kept);
108
214
  continue;
109
215
  }
216
+ if (await othersThere(kept)) {
217
+ // Its runner's own: woken again at its time while that is ahead, by then that runner may be gone.
218
+ if (!kept.ending && deps.now < kept.until)
219
+ next = Math.min(next ?? kept.until, kept.until);
220
+ continue;
221
+ }
110
222
  if (!over && !kept.ending && deps.now < kept.until) {
111
223
  next = Math.min(next ?? kept.until, kept.until);
112
224
  continue;
@@ -1,5 +1,6 @@
1
1
  import type { Store } from "../store/index.js";
2
2
  import type { ComputerDeps } from "./computer-deps.js";
3
+ import { type KeptHolder } from "./computer-kept.js";
3
4
  /** The longest a run's end may spend on its computers, every try with its pauses: past it the provider's calls are cut
4
5
  * (one that takes no signal, a release, left to finish on its own) and the run's end goes on, said in the log. */
5
6
  export declare const RUN_END_COMPUTERS_MS = 120000;
@@ -8,7 +9,7 @@ export declare const RUN_END_RETRY_MS = 5000;
8
9
  export interface RunEndComputersDeps {
9
10
  /** The run's row and launch (its revision and place) and the agent's revision, where its computer setting is; the
10
11
  * computer ledger, for a provider that keeps one. */
11
- readonly store: Pick<Store, "agents" | "computerLedger"> & {
12
+ readonly store: Pick<Store, "agents" | "computerLedger" | "settings"> & {
12
13
  readonly runs: Pick<Store["runs"], "get" | "launch">;
13
14
  readonly computerIntervals?: Store["computerIntervals"];
14
15
  };
@@ -17,6 +18,9 @@ export interface RunEndComputersDeps {
17
18
  readonly computers: Pick<ComputerDeps, "kinds" | "providers" | "keyOf" | "keyTable" | "settings" | "launch" | "launches">;
18
19
  /** Where what was let go, and a provider that failed, are said. */
19
20
  readonly log: (line: string) => void;
21
+ /** The runner this end runs in (its name and presence incarnation), read at each end: a computer kept pinned to it is
22
+ * its own to let go here. Absent: every kept row that names a runner is another's. */
23
+ readonly self?: () => KeptHolder | undefined;
20
24
  /** The bound on every try. Default RUN_END_COMPUTERS_MS. */
21
25
  readonly timeoutMs?: number;
22
26
  /** The pause between tries. Default RUN_END_RETRY_MS. */
@@ -15,10 +15,16 @@
15
15
  // at all (its key is not in this runner's environment or the store) is not. Either is said in the runner's log, and the
16
16
  // run's end stands. The janitor (computer-janitor.ts) ends a computer of a run that is over on its next sweep too, for a
17
17
  // server that stopped before this ran, or a vendor that stayed down past the bound.
18
+ //
19
+ // Nor a computer the run's park kept pinned to a runner other than this one while that keep lasts (computer-kept.ts: its
20
+ // row names that runner, and is neither ending nor ended): a stop or cancel ends the run on whichever runner claims it,
21
+ // and the machine is the keeping runner's to end, at its time or its janitor's pass. A vendor's list (a dial machine's)
22
+ // shows it here too; it is left out of the list, so nothing under its id is deleted from here.
18
23
  import { releaseOlder } from "@parcha/agentrun-computer";
19
24
  import { KEY_TABLE, providerOptions } from "../computer-keys.js";
20
25
  import { readLaunchBody } from "../run-launch.js";
21
26
  import { settingOf } from "./computer-for-claim.js";
27
+ import { keptBy, readKept } from "./computer-kept.js";
22
28
  import { storeLedger } from "./computer-ledger.js";
23
29
  import { providerKeys } from "./resolve-computer.js";
24
30
  import { endingDeletes, intervalWrites } from "./computer-intervals.js";
@@ -46,6 +52,19 @@ const pause = (ms, signal) => new Promise((resolve) => {
46
52
  }, ms);
47
53
  signal.addEventListener("abort", done, { once: true });
48
54
  });
55
+ /** `provider` with the computer `id` left out of its lists: releaseOlder never sees it, so never deletes it. */
56
+ const sparing = (provider, id) => new Proxy(provider, {
57
+ get(target, key) {
58
+ if (key !== "list") {
59
+ const value = Reflect.get(target, key, target);
60
+ return typeof value === "function" ? value.bind(target) : value;
61
+ }
62
+ return async (...args) => {
63
+ const listed = await target.list(...args);
64
+ return listed.ok ? { ...listed, value: listed.value.filter((info) => info.ref.id !== id) } : listed;
65
+ };
66
+ },
67
+ });
49
68
  /** The run's end for its computers (RunEndComputersDeps): given the claim that ended the run, every computer of the run
50
69
  * at or below its epoch deleted, a provider that did not answer asked again until the bound. Never throws: a failure
51
70
  * is said in the log. */
@@ -79,6 +98,12 @@ export function runEndComputers(deps) {
79
98
  // A throw from here on is the vendor's or the daemon's (Docker's architecture asked): asked again.
80
99
  return endingDeletes(await make(options.options, { ledger: storeLedger(store.computerLedger, entry.id) }), entry.id, writes);
81
100
  };
101
+ /** The id of the run's computer its park kept pinned to a runner other than this one, while the keep lasts; a read
102
+ * that fails throws, and is asked again (a delete from here must not guess). */
103
+ const keptElsewhere = async (runId) => {
104
+ const kept = await readKept(store.settings, runId);
105
+ return kept && kept.runner !== undefined && !kept.ending && !kept.ended && !keptBy(kept, deps.self?.()) ? kept.id : undefined;
106
+ };
82
107
  return async ({ runId, epoch }) => {
83
108
  if (epoch <= 1)
84
109
  return;
@@ -99,8 +124,9 @@ export function runEndComputers(deps) {
99
124
  const provider = await providerOf(runId, epoch);
100
125
  if (!provider)
101
126
  return;
127
+ const spared = await keptElsewhere(runId);
102
128
  // Every claim's at or below this one's: a computer of a later epoch fences the call (a later claim holds the run).
103
- const released = await releaseOlder(provider, runId, epoch + 1, { signal });
129
+ const released = await releaseOlder(spared === undefined ? provider : sparing(provider, spared), runId, epoch + 1, { signal });
104
130
  if (released.ok) {
105
131
  const names = released.value.released.map((ref) => ref.name);
106
132
  if (names.length > 0)