@parcha/agentrun-serve 0.0.0-stage → 0.1.0-beta.30

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 (273) hide show
  1. package/CHANGELOG.md +748 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +5 -0
  4. package/README.md +2190 -2
  5. package/dist/adapters.d.ts +234 -0
  6. package/dist/adapters.js +585 -0
  7. package/dist/agents.d.ts +130 -0
  8. package/dist/agents.js +308 -0
  9. package/dist/app.d.ts +7 -0
  10. package/dist/app.js +384 -0
  11. package/dist/background.d.ts +28 -0
  12. package/dist/background.js +376 -0
  13. package/dist/bound-files.d.ts +4 -0
  14. package/dist/bound-files.js +164 -0
  15. package/dist/box/record.d.ts +6 -0
  16. package/dist/box/record.js +41 -0
  17. package/dist/box/sweep.d.ts +53 -0
  18. package/dist/box/sweep.js +193 -0
  19. package/dist/box-egress.d.ts +98 -0
  20. package/dist/box-egress.js +259 -0
  21. package/dist/box-image.d.ts +98 -0
  22. package/dist/box-image.js +285 -0
  23. package/dist/box-relay-vm.d.ts +80 -0
  24. package/dist/box-relay-vm.js +389 -0
  25. package/dist/box-relay.d.ts +45 -0
  26. package/dist/box-relay.js +227 -0
  27. package/dist/boxes.d.ts +72 -0
  28. package/dist/boxes.js +189 -0
  29. package/dist/browser-cost.d.ts +41 -0
  30. package/dist/browser-cost.js +158 -0
  31. package/dist/cap.d.ts +332 -0
  32. package/dist/cap.js +992 -0
  33. package/dist/catalog.d.ts +20 -0
  34. package/dist/catalog.js +53 -0
  35. package/dist/cli.d.ts +2 -0
  36. package/dist/cli.js +167 -0
  37. package/dist/coding-tools.d.ts +35 -0
  38. package/dist/coding-tools.js +87 -0
  39. package/dist/console/THIRD-PARTY-NOTICES.txt +1721 -0
  40. package/dist/console/assets/index-34jfpIbQ.js +481 -0
  41. package/dist/console/assets/index-DUHbWcIP.css +1 -0
  42. package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
  43. package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-CIEtRijs.woff +0 -0
  44. package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-BB5xRk30.woff +0 -0
  45. package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-EVf6-Yzo.woff2 +0 -0
  46. package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-BHyURHCj.woff +0 -0
  47. package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-C_uLvvQ5.woff2 +0 -0
  48. package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-Dj6eZy5f.woff2 +0 -0
  49. package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-DpeVePZf.woff +0 -0
  50. package/dist/console/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
  51. package/dist/console/assets/jetbrains-mono-greek-400-normal-CNojz0t3.woff +0 -0
  52. package/dist/console/assets/jetbrains-mono-greek-600-normal-3KAm9r92.woff +0 -0
  53. package/dist/console/assets/jetbrains-mono-greek-600-normal-H7WoG9Et.woff2 +0 -0
  54. package/dist/console/assets/jetbrains-mono-latin-400-normal-B11XCQ5g.woff +0 -0
  55. package/dist/console/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
  56. package/dist/console/assets/jetbrains-mono-latin-600-normal-C8RAYTDA.woff2 +0 -0
  57. package/dist/console/assets/jetbrains-mono-latin-600-normal-DXv3JcnD.woff +0 -0
  58. package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
  59. package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-DHMOLxAi.woff +0 -0
  60. package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-BfB_LPfz.woff2 +0 -0
  61. package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-DiQlUoE6.woff +0 -0
  62. package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-BeU3T8wf.woff +0 -0
  63. package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-ByoDsISC.woff2 +0 -0
  64. package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-CSpzN4cI.woff +0 -0
  65. package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-Dtizs43-.woff2 +0 -0
  66. package/dist/console/assets/schibsted-grotesk-latin-400-normal-Cph9ettZ.woff2 +0 -0
  67. package/dist/console/assets/schibsted-grotesk-latin-400-normal-DfGZKnlb.woff +0 -0
  68. package/dist/console/assets/schibsted-grotesk-latin-600-normal-BGBUNdzT.woff2 +0 -0
  69. package/dist/console/assets/schibsted-grotesk-latin-600-normal-COje25QW.woff +0 -0
  70. package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-4l4lq8zr.woff2 +0 -0
  71. package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-BGzXWJeR.woff +0 -0
  72. package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-8_E36CQj.woff2 +0 -0
  73. package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-C27an7Zc.woff +0 -0
  74. package/dist/console/index.html +15 -0
  75. package/dist/content-filter.d.ts +7 -0
  76. package/dist/content-filter.js +23 -0
  77. package/dist/daytona-api.d.ts +21 -0
  78. package/dist/daytona-api.js +101 -0
  79. package/dist/decision-rows.d.ts +19 -0
  80. package/dist/decision-rows.js +123 -0
  81. package/dist/decisions.d.ts +94 -0
  82. package/dist/decisions.js +184 -0
  83. package/dist/effort-guidance.d.ts +5 -0
  84. package/dist/effort-guidance.js +14 -0
  85. package/dist/environment.d.ts +5 -0
  86. package/dist/environment.js +11 -0
  87. package/dist/errors.d.ts +74 -0
  88. package/dist/errors.js +69 -0
  89. package/dist/exa-keyless.d.ts +35 -0
  90. package/dist/exa-keyless.js +202 -0
  91. package/dist/fetch-page.d.ts +114 -0
  92. package/dist/fetch-page.js +182 -0
  93. package/dist/files.d.ts +65 -0
  94. package/dist/files.js +158 -0
  95. package/dist/grade.d.ts +93 -0
  96. package/dist/grade.js +328 -0
  97. package/dist/graph-served.d.ts +23 -0
  98. package/dist/graph-served.js +75 -0
  99. package/dist/graph-view.d.ts +102 -0
  100. package/dist/graph-view.js +219 -0
  101. package/dist/harness-app.d.ts +40 -0
  102. package/dist/harness-app.js +708 -0
  103. package/dist/harness-opencode/adapter.d.ts +45 -0
  104. package/dist/harness-opencode/adapter.js +272 -0
  105. package/dist/harness-opencode/binary.d.ts +18 -0
  106. package/dist/harness-opencode/binary.js +87 -0
  107. package/dist/harness-opencode/config.d.ts +24 -0
  108. package/dist/harness-opencode/config.js +61 -0
  109. package/dist/harness-opencode/harness.d.ts +77 -0
  110. package/dist/harness-opencode/harness.js +620 -0
  111. package/dist/harness-opencode/host/mcp.d.ts +40 -0
  112. package/dist/harness-opencode/host/mcp.js +208 -0
  113. package/dist/harness-opencode/host/policy.d.ts +71 -0
  114. package/dist/harness-opencode/host/policy.js +419 -0
  115. package/dist/harness-opencode/host/server.d.ts +55 -0
  116. package/dist/harness-opencode/host/server.js +165 -0
  117. package/dist/harness-opencode/index.d.ts +9 -0
  118. package/dist/harness-opencode/index.js +6 -0
  119. package/dist/harness-opencode/verify.d.ts +32 -0
  120. package/dist/harness-opencode/verify.js +75 -0
  121. package/dist/harness-opencode/versions.d.ts +33 -0
  122. package/dist/harness-opencode/versions.js +64 -0
  123. package/dist/harness.d.ts +56 -0
  124. package/dist/harness.js +77 -0
  125. package/dist/home-path.d.ts +27 -0
  126. package/dist/home-path.js +78 -0
  127. package/dist/host-tools.d.ts +30 -0
  128. package/dist/host-tools.js +154 -0
  129. package/dist/host.d.ts +39 -0
  130. package/dist/host.js +85 -0
  131. package/dist/hosted-search.d.ts +27 -0
  132. package/dist/hosted-search.js +133 -0
  133. package/dist/import-sources.d.ts +104 -0
  134. package/dist/import-sources.js +629 -0
  135. package/dist/import-summary.d.ts +25 -0
  136. package/dist/import-summary.js +75 -0
  137. package/dist/import-worker.d.ts +26 -0
  138. package/dist/import-worker.js +41 -0
  139. package/dist/import.d.ts +145 -0
  140. package/dist/import.js +1222 -0
  141. package/dist/index.d.ts +7 -0
  142. package/dist/index.js +6 -0
  143. package/dist/key-check.d.ts +37 -0
  144. package/dist/key-check.js +112 -0
  145. package/dist/key-refusal.d.ts +51 -0
  146. package/dist/key-refusal.js +191 -0
  147. package/dist/learn/check.d.ts +108 -0
  148. package/dist/learn/check.js +277 -0
  149. package/dist/learn/grade.d.ts +80 -0
  150. package/dist/learn/grade.js +445 -0
  151. package/dist/learn/index.d.ts +7 -0
  152. package/dist/learn/index.js +11 -0
  153. package/dist/learn/invisible.d.ts +5 -0
  154. package/dist/learn/invisible.js +42 -0
  155. package/dist/learn/notes.d.ts +59 -0
  156. package/dist/learn/notes.js +147 -0
  157. package/dist/learn/reflect.d.ts +49 -0
  158. package/dist/learn/reflect.js +213 -0
  159. package/dist/learn/secrets.d.ts +18 -0
  160. package/dist/learn/secrets.js +131 -0
  161. package/dist/learn/skills.d.ts +97 -0
  162. package/dist/learn/skills.js +271 -0
  163. package/dist/learned-workflow.d.ts +59 -0
  164. package/dist/learned-workflow.js +191 -0
  165. package/dist/learning-routes.d.ts +196 -0
  166. package/dist/learning-routes.js +609 -0
  167. package/dist/learning-run.d.ts +306 -0
  168. package/dist/learning-run.js +819 -0
  169. package/dist/learning-words.d.ts +76 -0
  170. package/dist/learning-words.js +174 -0
  171. package/dist/learning.d.ts +418 -0
  172. package/dist/learning.js +946 -0
  173. package/dist/llm.d.ts +40 -0
  174. package/dist/llm.js +37 -0
  175. package/dist/llms.txt +37 -0
  176. package/dist/mcp-servers.d.ts +118 -0
  177. package/dist/mcp-servers.js +450 -0
  178. package/dist/mcp.d.ts +51 -0
  179. package/dist/mcp.js +392 -0
  180. package/dist/model.d.ts +186 -0
  181. package/dist/model.js +485 -0
  182. package/dist/monid.d.ts +74 -0
  183. package/dist/monid.js +277 -0
  184. package/dist/openapi/agentrun-v2.openapi.json +4806 -0
  185. package/dist/openapi.d.ts +10 -0
  186. package/dist/openapi.js +1727 -0
  187. package/dist/owner.d.ts +55 -0
  188. package/dist/owner.js +222 -0
  189. package/dist/packs.d.ts +71 -0
  190. package/dist/packs.js +180 -0
  191. package/dist/page.d.ts +32 -0
  192. package/dist/page.js +133 -0
  193. package/dist/paid-calls.d.ts +50 -0
  194. package/dist/paid-calls.js +110 -0
  195. package/dist/pause.d.ts +70 -0
  196. package/dist/pause.js +97 -0
  197. package/dist/pdf-pages.d.ts +44 -0
  198. package/dist/pdf-pages.js +147 -0
  199. package/dist/place.d.ts +69 -0
  200. package/dist/place.js +137 -0
  201. package/dist/print.d.ts +20 -0
  202. package/dist/print.js +29 -0
  203. package/dist/propose.d.ts +40 -0
  204. package/dist/propose.js +112 -0
  205. package/dist/quiet-sqlite-warning.d.ts +1 -0
  206. package/dist/quiet-sqlite-warning.js +14 -0
  207. package/dist/request-start.d.ts +22 -0
  208. package/dist/request-start.js +350 -0
  209. package/dist/result.d.ts +3 -0
  210. package/dist/result.js +5 -0
  211. package/dist/rows.d.ts +46 -0
  212. package/dist/rows.js +121 -0
  213. package/dist/run-box.d.ts +186 -0
  214. package/dist/run-box.js +590 -0
  215. package/dist/run-manifest.d.ts +27 -0
  216. package/dist/run-manifest.js +60 -0
  217. package/dist/runnable.d.ts +83 -0
  218. package/dist/runnable.js +170 -0
  219. package/dist/runs.d.ts +1062 -0
  220. package/dist/runs.js +3020 -0
  221. package/dist/secret-cut.d.ts +27 -0
  222. package/dist/secret-cut.js +109 -0
  223. package/dist/secrets.d.ts +62 -0
  224. package/dist/secrets.js +158 -0
  225. package/dist/server-lock.d.ts +44 -0
  226. package/dist/server-lock.js +140 -0
  227. package/dist/server.d.ts +722 -0
  228. package/dist/server.js +2118 -0
  229. package/dist/sessions.d.ts +4 -0
  230. package/dist/sessions.js +105 -0
  231. package/dist/sign-ins.d.ts +99 -0
  232. package/dist/sign-ins.js +288 -0
  233. package/dist/skills.d.ts +43 -0
  234. package/dist/skills.js +96 -0
  235. package/dist/stats.d.ts +106 -0
  236. package/dist/stats.js +103 -0
  237. package/dist/tool-gate.d.ts +266 -0
  238. package/dist/tool-gate.js +554 -0
  239. package/dist/tool-keys.d.ts +187 -0
  240. package/dist/tool-keys.js +315 -0
  241. package/dist/tool-plane.d.ts +228 -0
  242. package/dist/tool-plane.js +400 -0
  243. package/dist/tools.d.ts +202 -0
  244. package/dist/tools.js +372 -0
  245. package/dist/trigger-secrets.d.ts +32 -0
  246. package/dist/trigger-secrets.js +143 -0
  247. package/dist/triggers.d.ts +439 -0
  248. package/dist/triggers.js +863 -0
  249. package/dist/turn-end.d.ts +24 -0
  250. package/dist/turn-end.js +15 -0
  251. package/dist/turns.d.ts +34 -0
  252. package/dist/turns.js +202 -0
  253. package/dist/uploads.d.ts +104 -0
  254. package/dist/uploads.js +474 -0
  255. package/dist/usage-lines.d.ts +26 -0
  256. package/dist/usage-lines.js +28 -0
  257. package/dist/usage.d.ts +29 -0
  258. package/dist/usage.js +58 -0
  259. package/dist/validate.d.ts +57 -0
  260. package/dist/validate.js +184 -0
  261. package/dist/web-fetch.d.ts +64 -0
  262. package/dist/web-fetch.js +262 -0
  263. package/dist/web-search.d.ts +54 -0
  264. package/dist/web-search.js +136 -0
  265. package/dist/webhooks.d.ts +189 -0
  266. package/dist/webhooks.js +503 -0
  267. package/dist/workspace-paths.d.ts +33 -0
  268. package/dist/workspace-paths.js +104 -0
  269. package/dist/workspace.d.ts +88 -0
  270. package/dist/workspace.js +369 -0
  271. package/launcher/agentrun-serve +109 -0
  272. package/package.json +100 -4
  273. package/skills/agentrun-serve/SKILL.md +128 -0
@@ -0,0 +1,24 @@
1
+ /** `{ kind: TURN_END, data: TurnEndData }`: a turn of the session `key` is over. Written in the same commit as the turn's
2
+ * last entries. #1424's first form held only `key` and `status`, and closes the message sent last before it. The
3
+ * timeline reads it too (the next message is not a steer), so the kind is the timeline's. */
4
+ export { TURN_END } from "@parcha/pi-durable-agent/timeline";
5
+ /** The root's record of a message the door accepted (`{ key, id, content }`), and of its end (`{ key, id, sent }`):
6
+ * `sent` to the session, or dropped by a stop. The bridge writes a TURN_END naming the message once its turn is over;
7
+ * for a message an older bridge sent and wrote no end for, the run's next process writes it as it opens. */
8
+ export declare const MESSAGE = "adapter.message";
9
+ export declare const MESSAGE_END = "adapter.message_end";
10
+ /** What a turn's end says: the message it took (the MESSAGE id), the session's own outcome (`failed` too when the bridge
11
+ * wrote the turn's error, in `error`), the entry of the turn's answer (its last request with text and no call; null
12
+ * when the turn showed no text), and whether a request it made is still unbooked (its cost then comes later, as a
13
+ * PRICED_LATE entry). */
14
+ export type TurnEndData = {
15
+ key: string;
16
+ message?: string;
17
+ status: "completed" | "interrupted" | "failed";
18
+ answer?: number | null;
19
+ error?: string;
20
+ cost?: "booked" | "pending";
21
+ };
22
+ /** The `api` of every assistant message serve's bridge writes for a claude-agent or codex request (harness-app.ts): its
23
+ * stop reason says nothing of the turn's end (a cost, a cut, an interruption and an answer read alike). */
24
+ export declare const ADAPTER_API = "agentrun-adapter";
@@ -0,0 +1,15 @@
1
+ // The end of a claude-agent or codex turn on a run's root conversation (harness-app.ts writes it; turns.ts reads it), and
2
+ // the root's records of the messages the turns take. Data entries: no model, so the timeline shows no row for them and
3
+ // the meters do not count them.
4
+ /** `{ kind: TURN_END, data: TurnEndData }`: a turn of the session `key` is over. Written in the same commit as the turn's
5
+ * last entries. #1424's first form held only `key` and `status`, and closes the message sent last before it. The
6
+ * timeline reads it too (the next message is not a steer), so the kind is the timeline's. */
7
+ export { TURN_END } from "@parcha/pi-durable-agent/timeline";
8
+ /** The root's record of a message the door accepted (`{ key, id, content }`), and of its end (`{ key, id, sent }`):
9
+ * `sent` to the session, or dropped by a stop. The bridge writes a TURN_END naming the message once its turn is over;
10
+ * for a message an older bridge sent and wrote no end for, the run's next process writes it as it opens. */
11
+ export const MESSAGE = "adapter.message";
12
+ export const MESSAGE_END = "adapter.message_end";
13
+ /** The `api` of every assistant message serve's bridge writes for a claude-agent or codex request (harness-app.ts): its
14
+ * stop reason says nothing of the turn's end (a cost, a cut, an interruption and an answer read alike). */
15
+ export const ADAPTER_API = "agentrun-adapter";
@@ -0,0 +1,34 @@
1
+ /** How a message's turn ended. `answer`: the entry of its answer, null when the turn showed no text. `asked`: a pi turn
2
+ * that ended on a tool asking the person (ask_person ends the turn, and the run waits for their answer). */
3
+ export type TurnEnd = {
4
+ status: "answered" | "asked" | "failed" | "interrupted";
5
+ answer: number | null;
6
+ error?: string;
7
+ };
8
+ /** The request id prefix of a run's end-of-run turn (learning-run.ts): the server's message, never the person's. */
9
+ export declare const REFLECT_REQUEST = "learn-reflect-";
10
+ export declare class Turns {
11
+ #private;
12
+ /** Reads `entries` (the root's, in order) and `submissions` (its input submissions, as pi-durable records them, or as
13
+ * the door's snapshot carries them with `answer_text`), at `now`. `skip`: entries of no turn of the run's own (its
14
+ * end-of-run turn's). Reading the same records again changes nothing: a turn's first end stands. */
15
+ note(entries: readonly unknown[], submissions?: readonly unknown[], skip?: (id: number) => boolean, now?: number): void;
16
+ /** A reconnect's snapshot (the door's `inputs`): the run's inputs from the newest settled one on, and every earlier one
17
+ * not settled yet. It is the whole state, where an event batch carries only changes: a message this ledger holds
18
+ * open that is missing from it, from before its newest settled input, settled while the stream was down. That one
19
+ * is closed, its own end unread and never the run's (the settled one after it is newer). One still queued, a
20
+ * follow-up a steer settled ahead of, is in the snapshot and stays open. */
21
+ snapshot(inputs: readonly unknown[], skip?: (id: number) => boolean, now?: number): void;
22
+ /** How many messages the run took whose turns have not ended. */
23
+ get open(): number;
24
+ /** The newest message's turn's end, once no message is open. */
25
+ end(): TurnEnd | undefined;
26
+ /** The whole text of entry `id`, an answer read here; undefined for one not read. */
27
+ text(id: number): string | undefined;
28
+ /** The whole text of the newest answer with text the run gave: what its cap keeps, and the answer of a run read by its
29
+ * rows. */
30
+ get newestText(): string | undefined;
31
+ /** When the turn of a message still open last wrote (its message taken, or an entry or a settlement since), as ms;
32
+ * undefined when none is open. */
33
+ progressAt(): number | undefined;
34
+ }
package/dist/turns.js ADDED
@@ -0,0 +1,202 @@
1
+ // The person's messages a run took, and how each one's turn ended, read from the run's own records, never from the
2
+ // order of its rows or from when its door said idle (runs.ts decide()):
3
+ // pi the input submission of each message, which pi-durable settles in the commit that appends the turn's
4
+ // last entry: `done` with the answer's entry, or `unanswered` (aborted, or the model's error);
5
+ // claude-agent, the bridge's records on the root (turn-end.ts): MESSAGE as the door accepts a message, MESSAGE_END
6
+ // codex as it is sent (or dropped by a stop), and TURN_END, in the commit of the turn's last entries.
7
+ // A message is open from the moment the run took it until its turn's end closes it, so a follow-up queued behind a turn
8
+ // keeps the run open whatever the door says meanwhile.
9
+ import { newestSettledInput } from "@parcha/pi-durable-disk/internals";
10
+ import { ADAPTER_API, MESSAGE, MESSAGE_END, TURN_END } from "./turn-end.js";
11
+ /** The request id prefix of a run's end-of-run turn (learning-run.ts): the server's message, never the person's. */
12
+ export const REFLECT_REQUEST = "learn-reflect-";
13
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
14
+ /** An answer's whole text: its text blocks, as a run's answer has always read. */
15
+ const textOf = (content) => typeof content === "string" ? content : Array.isArray(content) ? content.filter((b) => isObject(b) && b.type === "text").map((b) => String(b.text ?? "")).join("\n").trim() : "";
16
+ const later = (a, b) => (a.placed !== undefined && b.placed !== undefined ? a.placed > b.placed : a.order > b.order);
17
+ export class Turns {
18
+ #messages = new Map();
19
+ #texts = new Map();
20
+ /** The assistant entries that asked for tools: a turn settled on one ended on a tool's own end (ask_person's). */
21
+ #calls = new Set();
22
+ /** The bridge's message whose turn runs (the one sent last), and its newest answer since then. */
23
+ #sent;
24
+ #since;
25
+ /** The newest error the bridge wrote on the root since that message was sent: an older bridge's turn end has none. */
26
+ #sinceError;
27
+ #newestText;
28
+ #seen = 0;
29
+ #progress;
30
+ /** Reads `entries` (the root's, in order) and `submissions` (its input submissions, as pi-durable records them, or as
31
+ * the door's snapshot carries them with `answer_text`), at `now`. `skip`: entries of no turn of the run's own (its
32
+ * end-of-run turn's). Reading the same records again changes nothing: a turn's first end stands. */
33
+ note(entries, submissions = [], skip, now = Date.now()) {
34
+ for (const value of entries) {
35
+ if (!isObject(value) || typeof value.kind !== "string")
36
+ continue;
37
+ const id = typeof value.id === "number" ? value.id : undefined;
38
+ if (id !== undefined && skip?.(id))
39
+ continue;
40
+ // An entry not read before (a snapshot reads every one again): only such an entry moves what is read by place, the
41
+ // message sent last and its answer since. Any new entry while a message is open is its turn's progress.
42
+ const fresh = id === undefined || id > this.#seen;
43
+ if (id !== undefined && fresh) {
44
+ this.#seen = id;
45
+ if (this.open > 0)
46
+ this.#progress = now;
47
+ }
48
+ const data = isObject(value.data) ? value.data : undefined;
49
+ const first = Array.isArray(value.model) && isObject(value.model[0]) ? value.model[0] : undefined;
50
+ if (value.kind === "pi.assistant" && id !== undefined && Array.isArray(first?.content) && first.content.some((b) => isObject(b) && b.type === "toolCall"))
51
+ this.#calls.add(id);
52
+ // An answer's text: a stop's, or a stop at the length limit (pi ends the turn on both, settled on that entry).
53
+ if (value.kind === "pi.assistant" && (first?.stopReason === "stop" || first?.stopReason === "length") && id !== undefined) {
54
+ const text = textOf(first.content);
55
+ this.#texts.set(id, text);
56
+ if (text && fresh)
57
+ this.#newestText = text;
58
+ if (text && fresh && first.api === ADAPTER_API)
59
+ this.#since = id;
60
+ }
61
+ else if (value.kind === "pi.assistant" && first?.api === ADAPTER_API && first.stopReason === "error" && typeof first.errorMessage === "string") {
62
+ if (fresh)
63
+ this.#sinceError = first.errorMessage;
64
+ }
65
+ else if (value.kind === MESSAGE && typeof data?.id === "string") {
66
+ this.#take(`m:${data.id}`, now, id ?? this.#seen);
67
+ }
68
+ else if (value.kind === MESSAGE_END && typeof data?.id === "string") {
69
+ const message = this.#messages.get(`m:${data.id}`);
70
+ if (!message || message.sent)
71
+ continue;
72
+ // Dropped by a stop before it was sent: no turn took it.
73
+ if (data.sent !== true) {
74
+ if (!message.end)
75
+ this.#messages.delete(message.key);
76
+ continue;
77
+ }
78
+ message.sent = true;
79
+ if (!fresh)
80
+ continue;
81
+ // Turns are taken one at a time: one sent means the one sent before it had its turn, which is over (an older
82
+ // bridge wrote no end for it).
83
+ if (this.#sent && !this.#sent.end)
84
+ this.#sent.end = { status: "answered", answer: this.#since ?? null };
85
+ this.#sent = message;
86
+ this.#since = undefined;
87
+ this.#sinceError = undefined;
88
+ }
89
+ else if (value.kind === TURN_END && data) {
90
+ // #1424's first form names no message: it ends the turn of the message sent last, read in place.
91
+ const message = typeof data.message === "string" ? this.#messages.get(`m:${data.message}`) : fresh ? this.#sent : undefined;
92
+ if (!message || message.end)
93
+ continue;
94
+ const status = data.status === "completed" ? "answered" : data.status === "interrupted" ? "interrupted" : "failed";
95
+ const answer = "answer" in data ? (typeof data.answer === "number" ? data.answer : null) : (this.#since ?? null);
96
+ // #1424's bridge wrote a failed turn's error on the root, then its end with none (and a relaunch writes one for a
97
+ // turn whose end it never wrote): the error is the bridge's newest since that message was sent.
98
+ const error = typeof data.error === "string" ? data.error : status === "failed" && message === this.#sent ? this.#sinceError : undefined;
99
+ message.end = { status, answer: status === "answered" ? answer : null, ...(error === undefined ? {} : { error }) };
100
+ }
101
+ }
102
+ for (const value of submissions)
103
+ this.#settle(value, skip, now);
104
+ }
105
+ /** A reconnect's snapshot (the door's `inputs`): the run's inputs from the newest settled one on, and every earlier one
106
+ * not settled yet. It is the whole state, where an event batch carries only changes: a message this ledger holds
107
+ * open that is missing from it, from before its newest settled input, settled while the stream was down. That one
108
+ * is closed, its own end unread and never the run's (the settled one after it is newer). One still queued, a
109
+ * follow-up a steer settled ahead of, is in the snapshot and stays open. */
110
+ snapshot(inputs, skip, now = Date.now()) {
111
+ this.note([], inputs, skip, now);
112
+ const records = inputs.filter((value) => isObject(value) && value.type === "input" && typeof value.id === "number" && typeof value.status === "string");
113
+ const held = new Set(records.map((value) => `s:${value.id}`));
114
+ // The newest settled input as the door's snapshot chose it, by the same rule: the run's own, never the end-of-run turn's.
115
+ const newest = newestSettledInput(records, REFLECT_REQUEST);
116
+ if (newest < 0)
117
+ return;
118
+ const through = records[newest].id;
119
+ for (const message of this.#messages.values())
120
+ if (message.key.startsWith("s:") && !message.end && message.order < through && !held.has(message.key))
121
+ message.end = { status: "answered", answer: null };
122
+ }
123
+ /** One input submission: a message taken while queued or placed, closed once it is settled. */
124
+ #settle(value, skip, now) {
125
+ if (!isObject(value) || value.type !== "input" || typeof value.id !== "number")
126
+ return;
127
+ if ((typeof value.requestId === "string" && value.requestId.startsWith(REFLECT_REQUEST)) || (typeof value.entry === "number" && skip?.(value.entry)))
128
+ return;
129
+ const key = `s:${value.id}`;
130
+ const known = this.#messages.get(key);
131
+ // Withdrawn before it was placed (a stop): no turn took it.
132
+ if (value.status === "unanswered" && typeof value.entry !== "number") {
133
+ if (known && !known.end)
134
+ this.#messages.delete(key);
135
+ return;
136
+ }
137
+ const message = known ?? this.#take(key, now, value.id);
138
+ if (typeof value.entry === "number")
139
+ message.placed = value.entry;
140
+ if (message.end)
141
+ return;
142
+ // Only a move of its status is its turn's progress: a reconnect's snapshot carries the same record again.
143
+ const moved = message.status !== value.status;
144
+ message.status = value.status;
145
+ if (value.status === "done") {
146
+ const answer = typeof value.answer === "number" ? value.answer : null;
147
+ if (answer !== null && typeof value.answer_text === "string")
148
+ this.#texts.set(answer, value.answer_text);
149
+ // Settled on the entry that asked for its tools: a tool ended the turn (ask_person), so the person is asked, not
150
+ // answered. The door's snapshot says so for an entry it does not hold (`answer_calls`).
151
+ message.end = answer !== null && (this.#calls.has(answer) || value.answer_calls === true) ? { status: "asked", answer: null } : { status: "answered", answer };
152
+ }
153
+ else if (value.status === "unanswered") {
154
+ message.end = value.reason === "aborted" ? { status: "interrupted", answer: null } : { status: "failed", answer: null, error: typeof value.detail === "string" ? value.detail : String(value.reason ?? "the turn failed") };
155
+ }
156
+ else if (moved && this.open > 0)
157
+ this.#progress = now;
158
+ }
159
+ #take(key, now, order) {
160
+ const known = this.#messages.get(key);
161
+ if (known)
162
+ return known;
163
+ const message = { key, order };
164
+ this.#messages.set(key, message);
165
+ this.#progress = now;
166
+ return message;
167
+ }
168
+ /** The message whose turn ran last: by where pi placed each input when both were placed, else by when the run took it. */
169
+ #newest() {
170
+ let newest;
171
+ for (const message of this.#messages.values())
172
+ if (!newest || later(message, newest))
173
+ newest = message;
174
+ return newest;
175
+ }
176
+ /** How many messages the run took whose turns have not ended. */
177
+ get open() {
178
+ let n = 0;
179
+ for (const message of this.#messages.values())
180
+ if (!message.end)
181
+ n++;
182
+ return n;
183
+ }
184
+ /** The newest message's turn's end, once no message is open. */
185
+ end() {
186
+ return this.open ? undefined : this.#newest()?.end;
187
+ }
188
+ /** The whole text of entry `id`, an answer read here; undefined for one not read. */
189
+ text(id) {
190
+ return this.#texts.get(id);
191
+ }
192
+ /** The whole text of the newest answer with text the run gave: what its cap keeps, and the answer of a run read by its
193
+ * rows. */
194
+ get newestText() {
195
+ return this.#newestText;
196
+ }
197
+ /** When the turn of a message still open last wrote (its message taken, or an entry or a settlement since), as ms;
198
+ * undefined when none is open. */
199
+ progressAt() {
200
+ return this.open ? this.#progress : undefined;
201
+ }
202
+ }
@@ -0,0 +1,104 @@
1
+ import { type Json } from "./validate.js";
2
+ /** `limits.max_upload_bytes`: the largest file a reservation names and a PUT sends. */
3
+ export declare const MAX_UPLOAD_BYTES: number;
4
+ /** `limits.max_files_per_run`: the most files one run names (the contract's `RunCreate.file_ids` takes at most 100). */
5
+ export declare const MAX_FILES_PER_RUN = 20;
6
+ /** Where a run finds its files: `work/inputs/<path>`, each file's `path` with its folders. */
7
+ export declare const INPUTS_DIR = "inputs";
8
+ /** The data directory's folder of uploads. */
9
+ export declare const UPLOADS_DIR = "files";
10
+ /** Where a file's bytes go: its own PUT, as a path from the server's root, so it resolves against the server's address
11
+ * as given (`http://host:port`) or as the contract's `/v2` server URL alike. */
12
+ export declare const uploadUrl: (id: string) => string;
13
+ /** Test seams, tests only: as a run's copy starts, with the run's work/, before any file is copied; and as a file sent in
14
+ * one call writes its record, with the file's folder, its bytes in place. */
15
+ export declare const uploadTestHooks: {
16
+ beforeCopy?: (work: string) => Promise<void> | void;
17
+ beforeRecord?: (dir: string) => Promise<void> | void;
18
+ };
19
+ /** The contract's FileUpload: the file, and where to send its bytes (for a file sent in one call, ready, a PUT of the
20
+ * same bytes, which changes nothing). */
21
+ export type FileUploadAnswer = {
22
+ file: UploadedFile;
23
+ upload: {
24
+ method: "PUT";
25
+ url: string;
26
+ expires_at: string;
27
+ };
28
+ };
29
+ /** A file sent in one call, its bytes in and checked, before it is made: `fingerprint` is what an Idempotency-Key is
30
+ * matched on (its path, type, length and hash), `commit` makes it, ready, and `discard` drops the bytes of one not made. */
31
+ export type Received = {
32
+ fingerprint: Json;
33
+ commit(): Promise<FileUploadAnswer>;
34
+ discard(): Promise<void>;
35
+ };
36
+ /** The contract's File: `pending` until its bytes arrive, then `ready`, its `sha256` the bytes' own. */
37
+ export type UploadedFile = {
38
+ file_id: string;
39
+ path: string;
40
+ size_bytes: number;
41
+ content_type: string;
42
+ sha256: string | null;
43
+ status: "pending" | "ready";
44
+ created_at: string;
45
+ expires_at: null;
46
+ };
47
+ /** Why `path` is not the contract's FilePathString (relative, `/` between segments, no empty segment and none that starts
48
+ * with `.`, no backslash or NUL, at most 1024 characters), or a segment is longer than a name on disk; undefined when it
49
+ * is one. */
50
+ export declare function pathProblem(path: string): string | undefined;
51
+ /** `name` for the first, then `name-2`, `name-3`, ... before its extension (`report-2.pdf`), cut to fit a name on disk:
52
+ * its stem first, down to one character, then its extension. */
53
+ export declare function suffixed(name: string, n: number): string;
54
+ export declare class Uploads {
55
+ private readonly root;
56
+ /** The commit of each file's bytes in progress: one at a time per file, so of two PUTs at once one makes it ready and
57
+ * the other is checked against it. */
58
+ private readonly commits;
59
+ /** `root`: `<data>/files`. */
60
+ constructor(root: string);
61
+ /** Why `path` cannot be a file's path here: not the contract's FilePathString, a segment longer than a name on disk,
62
+ * or a run's copy of it (`<data>/runs/<run id>/work/inputs/<path>`, room for a suffix on each segment included) longer
63
+ * than a path the system opens. Undefined when it can. */
64
+ private problemOf;
65
+ /** The file `id`, or null when there is none. */
66
+ get(id: string): Promise<UploadedFile | null>;
67
+ /** Every file's id, newest first: a UUIDv7 sorts by its time. The files' list reads them a page at a time. */
68
+ ids(): Promise<string[]>;
69
+ /** At a start, under the data directory's lock (one server per data directory, so no PUT of this server is in flight):
70
+ * removes the temp files a crash left, by the exact names this module writes them under. That is `.incoming.*.tmp`
71
+ * under the root, and `bytes.*.tmp` in a folder named as a file's id. Each goes only when lstat says it is a regular
72
+ * file: a link or anything else named like one stays, nothing is followed, and only those folders are entered, one
73
+ * level. A file's bytes and its record are never touched: a folder left with only its temp keeps the rest, as after a
74
+ * failed PUT. A root that is not a real directory (a link to storage another server may share) is not swept at all: the
75
+ * lock covers this data directory, not where a link goes. Answers how many it removed. */
76
+ sweep(): Promise<number>;
77
+ /** Deletes the file `id`, pending or ready, or 404. A run that copied it keeps its copy. In turn with a PUT's commit, so
78
+ * a PUT that ends after the delete answers 404 and makes nothing. */
79
+ remove(id: string): Promise<void>;
80
+ /** The answer to a reservation, and to a file sent in one call. `upload.expires_at` is advisory (UPLOAD_WINDOW_MS). */
81
+ private answer;
82
+ /** POST /v2/files?path=...: the body is the file's bytes, whatever its type but a multipart form (415, with the call
83
+ * that works). The query takes `path`, and `content_type` and `sha256` when given; the length is the body's and the hash
84
+ * its own. The type is `content_type`, else the request's when it names one (not octet-stream, not curl's form type),
85
+ * else the path's extension's, else octet-stream. The bytes stream to a dot-named file of their own under the root,
86
+ * never held whole; over MAX_UPLOAD_BYTES is 413, a `sha256` they do not have 422. Nothing is made until `commit`. */
87
+ take(query: URLSearchParams, requestType: string | undefined, body: AsyncIterable<Buffer>): Promise<Received>;
88
+ /** POST /v2/files: the body checked as the contract's FileCreate, then a pending file; with where to send its bytes. */
89
+ reserve(body: Json): Promise<FileUploadAnswer>;
90
+ /** PUT /v2/files/{file_id}: `body` is the bytes, streamed to a file of their own beside the record, never held whole.
91
+ * Their length must be the reserved `size_bytes` and their hash the reserved `sha256` when one was given (422 at that
92
+ * field), and they are at most MAX_UPLOAD_BYTES (413). Then they are renamed into place and the file is ready. A
93
+ * ready file's bytes are checked the same way against its own and kept: the same bytes again change nothing. */
94
+ receive(id: string, body: AsyncIterable<Buffer>): Promise<UploadedFile>;
95
+ /** `work` in turn for file `id`. The chain holds only settled turns, so one that failed never fails the next. */
96
+ private exclusive;
97
+ /** A run's `file_ids`: each a file that is ready, none twice, at most MAX_FILES_PER_RUN, or 422 at `file_ids`. */
98
+ readyFiles(ids: readonly unknown[]): Promise<UploadedFile[]>;
99
+ /** Copies `files` into `work/inputs/`, each at its `path` with its folders, in order, before the run's process exists:
100
+ * a name taken already (the same path twice, or a file where another's folder goes) gets the next free suffix
101
+ * (`notes-2.txt`, `docs-2/`), the same for the same files in the same order. Each copy is a new file, made as the
102
+ * agent store makes a run's skill files (its mode from the umask): never a link to the upload. */
103
+ copyInto(work: string, files: readonly UploadedFile[]): Promise<void>;
104
+ }