@rivet-dev/agentos-runtime-core 0.0.0-agent-acp-codex-session-benchmark.62b1f46

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 (272) hide show
  1. package/README.md +7 -0
  2. package/commands/[ +0 -0
  3. package/commands/_stubs +0 -0
  4. package/commands/arch +0 -0
  5. package/commands/awk +0 -0
  6. package/commands/b2sum +0 -0
  7. package/commands/base32 +0 -0
  8. package/commands/base64 +0 -0
  9. package/commands/basename +0 -0
  10. package/commands/basenc +0 -0
  11. package/commands/bash +0 -0
  12. package/commands/cat +0 -0
  13. package/commands/chcon +0 -0
  14. package/commands/chgrp +0 -0
  15. package/commands/chmod +0 -0
  16. package/commands/chown +0 -0
  17. package/commands/chroot +0 -0
  18. package/commands/cksum +0 -0
  19. package/commands/codex +0 -0
  20. package/commands/codex-exec +0 -0
  21. package/commands/column +0 -0
  22. package/commands/comm +0 -0
  23. package/commands/cp +0 -0
  24. package/commands/curl +0 -0
  25. package/commands/cut +0 -0
  26. package/commands/date +0 -0
  27. package/commands/dd +0 -0
  28. package/commands/df +0 -0
  29. package/commands/diff +0 -0
  30. package/commands/dir +0 -0
  31. package/commands/dircolors +0 -0
  32. package/commands/dirname +0 -0
  33. package/commands/du +0 -0
  34. package/commands/echo +0 -0
  35. package/commands/egrep +0 -0
  36. package/commands/env +0 -0
  37. package/commands/envsubst +0 -0
  38. package/commands/expand +0 -0
  39. package/commands/expr +0 -0
  40. package/commands/factor +0 -0
  41. package/commands/false +0 -0
  42. package/commands/fd +0 -0
  43. package/commands/fgrep +0 -0
  44. package/commands/file +0 -0
  45. package/commands/find +0 -0
  46. package/commands/fmt +0 -0
  47. package/commands/fold +0 -0
  48. package/commands/git +0 -0
  49. package/commands/git-remote-http +0 -0
  50. package/commands/git-remote-https +0 -0
  51. package/commands/grep +0 -0
  52. package/commands/groups +0 -0
  53. package/commands/gunzip +0 -0
  54. package/commands/gzip +0 -0
  55. package/commands/head +0 -0
  56. package/commands/hostid +0 -0
  57. package/commands/hostname +0 -0
  58. package/commands/http-test +0 -0
  59. package/commands/http_get +0 -0
  60. package/commands/id +0 -0
  61. package/commands/install +0 -0
  62. package/commands/join +0 -0
  63. package/commands/jq +0 -0
  64. package/commands/kill +0 -0
  65. package/commands/link +0 -0
  66. package/commands/ln +0 -0
  67. package/commands/logname +0 -0
  68. package/commands/ls +0 -0
  69. package/commands/md5sum +0 -0
  70. package/commands/mkdir +0 -0
  71. package/commands/mkfifo +0 -0
  72. package/commands/mknod +0 -0
  73. package/commands/mktemp +0 -0
  74. package/commands/more +0 -0
  75. package/commands/mv +0 -0
  76. package/commands/nice +0 -0
  77. package/commands/nl +0 -0
  78. package/commands/nohup +0 -0
  79. package/commands/nproc +0 -0
  80. package/commands/numfmt +0 -0
  81. package/commands/od +0 -0
  82. package/commands/paste +0 -0
  83. package/commands/pathchk +0 -0
  84. package/commands/pinky +0 -0
  85. package/commands/printenv +0 -0
  86. package/commands/printf +0 -0
  87. package/commands/ptx +0 -0
  88. package/commands/pwd +0 -0
  89. package/commands/readlink +0 -0
  90. package/commands/realpath +0 -0
  91. package/commands/rev +0 -0
  92. package/commands/rg +0 -0
  93. package/commands/rm +0 -0
  94. package/commands/rmdir +0 -0
  95. package/commands/runcon +0 -0
  96. package/commands/sed +0 -0
  97. package/commands/seq +0 -0
  98. package/commands/sh +0 -0
  99. package/commands/sha1sum +0 -0
  100. package/commands/sha224sum +0 -0
  101. package/commands/sha256sum +0 -0
  102. package/commands/sha384sum +0 -0
  103. package/commands/sha512sum +0 -0
  104. package/commands/shred +0 -0
  105. package/commands/shuf +0 -0
  106. package/commands/sleep +0 -0
  107. package/commands/sort +0 -0
  108. package/commands/spawn-test-host +0 -0
  109. package/commands/split +0 -0
  110. package/commands/sqlite3 +0 -0
  111. package/commands/stat +0 -0
  112. package/commands/stdbuf +0 -0
  113. package/commands/strings +0 -0
  114. package/commands/stty +0 -0
  115. package/commands/sum +0 -0
  116. package/commands/sync +0 -0
  117. package/commands/tac +0 -0
  118. package/commands/tail +0 -0
  119. package/commands/tar +0 -0
  120. package/commands/tee +0 -0
  121. package/commands/test +0 -0
  122. package/commands/timeout +0 -0
  123. package/commands/touch +0 -0
  124. package/commands/tr +0 -0
  125. package/commands/tree +0 -0
  126. package/commands/true +0 -0
  127. package/commands/truncate +0 -0
  128. package/commands/tsort +0 -0
  129. package/commands/tty +0 -0
  130. package/commands/uname +0 -0
  131. package/commands/unexpand +0 -0
  132. package/commands/uniq +0 -0
  133. package/commands/unlink +0 -0
  134. package/commands/unzip +0 -0
  135. package/commands/uptime +0 -0
  136. package/commands/users +0 -0
  137. package/commands/vdir +0 -0
  138. package/commands/wc +0 -0
  139. package/commands/which +0 -0
  140. package/commands/who +0 -0
  141. package/commands/whoami +0 -0
  142. package/commands/xargs +0 -0
  143. package/commands/xu +0 -0
  144. package/commands/yes +0 -0
  145. package/commands/yq +0 -0
  146. package/commands/zcat +0 -0
  147. package/commands/zip +0 -0
  148. package/dist/binary.d.ts +4 -0
  149. package/dist/binary.js +25 -0
  150. package/dist/bytes.d.ts +2 -0
  151. package/dist/bytes.js +6 -0
  152. package/dist/callbacks.d.ts +41 -0
  153. package/dist/callbacks.js +94 -0
  154. package/dist/correlation.d.ts +7 -0
  155. package/dist/correlation.js +52 -0
  156. package/dist/descriptors.d.ts +81 -0
  157. package/dist/descriptors.js +57 -0
  158. package/dist/event-buffer.d.ts +98 -0
  159. package/dist/event-buffer.js +333 -0
  160. package/dist/ext.d.ts +7 -0
  161. package/dist/ext.js +13 -0
  162. package/dist/filesystem.d.ts +33 -0
  163. package/dist/filesystem.js +52 -0
  164. package/dist/frame-payload-codec.d.ts +8 -0
  165. package/dist/frame-payload-codec.js +14 -0
  166. package/dist/frame-rpc.d.ts +45 -0
  167. package/dist/frame-rpc.js +98 -0
  168. package/dist/frame-stream.d.ts +34 -0
  169. package/dist/frame-stream.js +109 -0
  170. package/dist/framing.d.ts +8 -0
  171. package/dist/framing.js +23 -0
  172. package/dist/generated/AcpLimitsConfig.d.ts +4 -0
  173. package/dist/generated/AcpLimitsConfig.js +2 -0
  174. package/dist/generated/CreateVmConfig.d.ts +26 -0
  175. package/dist/generated/CreateVmConfig.js +1 -0
  176. package/dist/generated/FsPermissionRule.d.ts +6 -0
  177. package/dist/generated/FsPermissionRule.js +1 -0
  178. package/dist/generated/FsPermissionRuleSet.d.ts +6 -0
  179. package/dist/generated/FsPermissionRuleSet.js +1 -0
  180. package/dist/generated/FsPermissionScope.d.ts +3 -0
  181. package/dist/generated/FsPermissionScope.js +1 -0
  182. package/dist/generated/HttpLimitsConfig.d.ts +3 -0
  183. package/dist/generated/HttpLimitsConfig.js +2 -0
  184. package/dist/generated/JsModuleResolution.d.ts +1 -0
  185. package/dist/generated/JsModuleResolution.js +2 -0
  186. package/dist/generated/JsRuntimeConfig.d.ts +31 -0
  187. package/dist/generated/JsRuntimeConfig.js +1 -0
  188. package/dist/generated/JsRuntimeLimitsConfig.d.ts +8 -0
  189. package/dist/generated/JsRuntimeLimitsConfig.js +2 -0
  190. package/dist/generated/JsRuntimePlatform.d.ts +1 -0
  191. package/dist/generated/JsRuntimePlatform.js +2 -0
  192. package/dist/generated/MountPluginDescriptor.d.ts +4 -0
  193. package/dist/generated/MountPluginDescriptor.js +2 -0
  194. package/dist/generated/NativeRootFilesystemConfig.d.ts +5 -0
  195. package/dist/generated/NativeRootFilesystemConfig.js +1 -0
  196. package/dist/generated/PatternPermissionRule.d.ts +6 -0
  197. package/dist/generated/PatternPermissionRule.js +1 -0
  198. package/dist/generated/PatternPermissionRuleSet.d.ts +6 -0
  199. package/dist/generated/PatternPermissionRuleSet.js +1 -0
  200. package/dist/generated/PatternPermissionScope.d.ts +3 -0
  201. package/dist/generated/PatternPermissionScope.js +1 -0
  202. package/dist/generated/PermissionMode.d.ts +1 -0
  203. package/dist/generated/PermissionMode.js +2 -0
  204. package/dist/generated/PermissionsPolicy.d.ts +10 -0
  205. package/dist/generated/PermissionsPolicy.js +1 -0
  206. package/dist/generated/PluginLimitsConfig.d.ts +4 -0
  207. package/dist/generated/PluginLimitsConfig.js +2 -0
  208. package/dist/generated/PythonLimitsConfig.d.ts +6 -0
  209. package/dist/generated/PythonLimitsConfig.js +2 -0
  210. package/dist/generated/ResourceLimitsConfig.d.ts +24 -0
  211. package/dist/generated/ResourceLimitsConfig.js +2 -0
  212. package/dist/generated/RootFilesystemConfig.d.ts +9 -0
  213. package/dist/generated/RootFilesystemConfig.js +1 -0
  214. package/dist/generated/RootFilesystemEntry.d.ts +13 -0
  215. package/dist/generated/RootFilesystemEntry.js +1 -0
  216. package/dist/generated/RootFilesystemEntryEncoding.d.ts +1 -0
  217. package/dist/generated/RootFilesystemEntryEncoding.js +2 -0
  218. package/dist/generated/RootFilesystemEntryKind.d.ts +1 -0
  219. package/dist/generated/RootFilesystemEntryKind.js +2 -0
  220. package/dist/generated/RootFilesystemLowerDescriptor.d.ts +7 -0
  221. package/dist/generated/RootFilesystemLowerDescriptor.js +1 -0
  222. package/dist/generated/RootFilesystemMode.d.ts +1 -0
  223. package/dist/generated/RootFilesystemMode.js +2 -0
  224. package/dist/generated/ToolLimitsConfig.d.ts +10 -0
  225. package/dist/generated/ToolLimitsConfig.js +2 -0
  226. package/dist/generated/VmDnsConfig.d.ts +6 -0
  227. package/dist/generated/VmDnsConfig.js +2 -0
  228. package/dist/generated/VmLimitsConfig.d.ts +18 -0
  229. package/dist/generated/VmLimitsConfig.js +1 -0
  230. package/dist/generated/VmListenPolicyConfig.d.ts +5 -0
  231. package/dist/generated/VmListenPolicyConfig.js +2 -0
  232. package/dist/generated/WasmLimitsConfig.d.ts +7 -0
  233. package/dist/generated/WasmLimitsConfig.js +2 -0
  234. package/dist/generated-protocol.d.ts +1452 -0
  235. package/dist/generated-protocol.js +3936 -0
  236. package/dist/index.d.ts +27 -0
  237. package/dist/index.js +26 -0
  238. package/dist/json.d.ts +2 -0
  239. package/dist/json.js +20 -0
  240. package/dist/native-client.d.ts +47 -0
  241. package/dist/native-client.js +136 -0
  242. package/dist/numbers.d.ts +1 -0
  243. package/dist/numbers.js +8 -0
  244. package/dist/ownership.d.ts +18 -0
  245. package/dist/ownership.js +77 -0
  246. package/dist/permissions.d.ts +29 -0
  247. package/dist/permissions.js +68 -0
  248. package/dist/process.d.ts +22 -0
  249. package/dist/process.js +97 -0
  250. package/dist/protocol-client.d.ts +67 -0
  251. package/dist/protocol-client.js +270 -0
  252. package/dist/protocol-frames.d.ts +68 -0
  253. package/dist/protocol-frames.js +139 -0
  254. package/dist/protocol-maps.d.ts +31 -0
  255. package/dist/protocol-maps.js +273 -0
  256. package/dist/protocol-schema.d.ts +10 -0
  257. package/dist/protocol-schema.js +11 -0
  258. package/dist/request-payloads.d.ts +207 -0
  259. package/dist/request-payloads.js +357 -0
  260. package/dist/response-payloads.d.ts +268 -0
  261. package/dist/response-payloads.js +402 -0
  262. package/dist/sidecar-client.d.ts +19 -0
  263. package/dist/sidecar-client.js +1 -0
  264. package/dist/sidecar-errors.d.ts +44 -0
  265. package/dist/sidecar-errors.js +62 -0
  266. package/dist/sidecar-process.d.ts +434 -0
  267. package/dist/sidecar-process.js +1252 -0
  268. package/dist/state.d.ts +42 -0
  269. package/dist/state.js +45 -0
  270. package/dist/vm-config.d.ts +31 -0
  271. package/dist/vm-config.js +1 -0
  272. package/package.json +189 -0
@@ -0,0 +1,270 @@
1
+ import { normalizeSidecarEventMatcher, SidecarEventBuffer, SidecarEventBufferOverflow, sidecarEventWaitAbortError, } from "./event-buffer.js";
2
+ import { FrameRpcTransport } from "./frame-rpc.js";
3
+ import { ownershipMatchesSelector, ownershipSelectorKey, } from "./ownership.js";
4
+ import { classifySidecarWrittenProtocolFrame, decodeProtocolFramePayload, encodeProtocolFramePayload, HostProtocolFrameFactory, resolveSidecarRequestFramePayload, } from "./protocol-frames.js";
5
+ import { SidecarRequestRejected, SidecarSilenceTimeout, } from "./sidecar-errors.js";
6
+ /**
7
+ * How long the host tolerates TOTAL inbound silence (no responses, events,
8
+ * sidecar requests, or heartbeats) before declaring the sidecar dead. The
9
+ * sidecar heartbeats every 10s from a dedicated thread, so this allows two
10
+ * missed beats plus margin; it bounds "sidecar is dead or wedged", never "this
11
+ * request is slow" — individual requests have no deadline of their own.
12
+ */
13
+ const DEFAULT_SIDECAR_SILENCE_TIMEOUT_MS = 30_000;
14
+ export class SidecarProtocolClient {
15
+ eventBuffer;
16
+ eventListeners = new Set();
17
+ silenceTimeoutMs;
18
+ silenceTimer = null;
19
+ lastInboundAtMs = 0;
20
+ payloadCodec;
21
+ stderrText;
22
+ hostFrameFactory = new HostProtocolFrameFactory();
23
+ frameTransport;
24
+ closedError = null;
25
+ eventWaiters = new Set();
26
+ sidecarRequestHandlers = new Map();
27
+ constructor(options) {
28
+ this.silenceTimeoutMs =
29
+ options.silenceTimeoutMs ?? DEFAULT_SIDECAR_SILENCE_TIMEOUT_MS;
30
+ this.eventBuffer = new SidecarEventBuffer(options.eventBufferCapacity);
31
+ this.payloadCodec = options.payloadCodec ?? "bare";
32
+ this.stderrText = options.stderrText ?? (() => "");
33
+ this.frameTransport = new FrameRpcTransport({
34
+ frameTransport: options.frameTransport,
35
+ stdin: options.stdin,
36
+ stdout: options.stdout,
37
+ encodeFrame: (frame) => encodeProtocolFramePayload(frame, this.payloadCodec),
38
+ decodeFrame: (payload) => decodeProtocolFramePayload(payload, this.payloadCodec),
39
+ classifyFrame: classifySidecarWrittenProtocolFrame,
40
+ });
41
+ this.frameTransport.onEvent((event) => {
42
+ this.dispatchEvent(event);
43
+ });
44
+ this.frameTransport.onSidecarRequest((request) => {
45
+ void this.dispatchSidecarRequest(request);
46
+ });
47
+ this.frameTransport.onError((error) => {
48
+ this.failPermanently(options.frameError?.(error) ?? error);
49
+ });
50
+ this.frameTransport.onEnd(() => {
51
+ this.failPermanently(options.streamEndedError?.() ??
52
+ new Error("sidecar protocol stream ended"));
53
+ });
54
+ this.frameTransport.onFrameActivity(() => {
55
+ this.lastInboundAtMs = performance.now();
56
+ });
57
+ this.startSilenceWatchdog(options.onSilenceExpired);
58
+ }
59
+ /**
60
+ * Arm the silence watchdog: ANY inbound frame resets the clock (see the
61
+ * `onFrameActivity` tap above), and the sidecar heartbeats every 10s even
62
+ * while busy, so sustained silence for the full window means the process
63
+ * is dead or wedged — not slow. The check interval is unref'd so an idle
64
+ * host process can still exit naturally.
65
+ */
66
+ startSilenceWatchdog(onExpired) {
67
+ this.lastInboundAtMs = performance.now();
68
+ const checkIntervalMs = Math.max(Math.min(this.silenceTimeoutMs / 4, 1_000), 10);
69
+ this.silenceTimer = setInterval(() => {
70
+ const silenceMs = performance.now() - this.lastInboundAtMs;
71
+ if (silenceMs < this.silenceTimeoutMs) {
72
+ return;
73
+ }
74
+ this.stopSilenceWatchdog();
75
+ const error = new SidecarSilenceTimeout({
76
+ silenceMs,
77
+ stderr: this.stderrText(),
78
+ });
79
+ try {
80
+ onExpired?.();
81
+ }
82
+ finally {
83
+ this.failPermanently(error);
84
+ }
85
+ }, checkIntervalMs);
86
+ this.silenceTimer.unref?.();
87
+ }
88
+ stopSilenceWatchdog() {
89
+ if (this.silenceTimer !== null) {
90
+ clearInterval(this.silenceTimer);
91
+ this.silenceTimer = null;
92
+ }
93
+ }
94
+ registerSidecarRequestHandler(ownership, handler) {
95
+ const key = ownershipSelectorKey(ownership);
96
+ if (this.sidecarRequestHandlers.has(key)) {
97
+ throw new Error(`sidecar request handler already registered for ${key}`);
98
+ }
99
+ const registration = { ownership, handler };
100
+ this.sidecarRequestHandlers.set(key, registration);
101
+ return () => {
102
+ if (this.sidecarRequestHandlers.get(key) === registration) {
103
+ this.sidecarRequestHandlers.delete(key);
104
+ }
105
+ };
106
+ }
107
+ onEvent(handler, ownership) {
108
+ const registration = { handler, ownership };
109
+ this.eventListeners.add(registration);
110
+ return () => {
111
+ this.eventListeners.delete(registration);
112
+ };
113
+ }
114
+ async sendRequest(input) {
115
+ if (this.closedError) {
116
+ throw this.closedError;
117
+ }
118
+ const request = this.hostFrameFactory.createRequestFrame(input);
119
+ // No per-request deadline: only the caller knows whether an operation is
120
+ // legitimately long (a whole agent turn is one request). A dead or
121
+ // wedged sidecar rejects this via the silence watchdog instead.
122
+ const response = await this.frameTransport.sendFrame(request.request_id, request, (frame) => {
123
+ this.validateResponse(frame);
124
+ input.onResponse?.(frame);
125
+ });
126
+ this.validateResponse(response);
127
+ return response;
128
+ }
129
+ validateResponse(response) {
130
+ if (response.payload.type === "rejected") {
131
+ throw new SidecarRequestRejected({
132
+ code: response.payload.code,
133
+ message: response.payload.message,
134
+ response,
135
+ });
136
+ }
137
+ }
138
+ async waitForEvent(matcher, timeoutMs, options) {
139
+ if (this.closedError instanceof SidecarEventBufferOverflow) {
140
+ throw this.closedError;
141
+ }
142
+ const normalizedMatcher = normalizeSidecarEventMatcher(matcher);
143
+ const bufferedEvent = this.eventBuffer.take(normalizedMatcher);
144
+ if (bufferedEvent) {
145
+ return bufferedEvent;
146
+ }
147
+ if (this.closedError) {
148
+ throw this.closedError;
149
+ }
150
+ if (options?.signal?.aborted) {
151
+ throw sidecarEventWaitAbortError(options.signal.reason);
152
+ }
153
+ return await new Promise((resolve, reject) => {
154
+ let abortListener = null;
155
+ const waiter = {
156
+ matches: normalizedMatcher.matches,
157
+ resolve: (event) => {
158
+ if (waiter.timer !== null) {
159
+ clearTimeout(waiter.timer);
160
+ }
161
+ if (abortListener) {
162
+ options?.signal?.removeEventListener("abort", abortListener);
163
+ abortListener = null;
164
+ }
165
+ this.eventWaiters.delete(waiter);
166
+ resolve(event);
167
+ },
168
+ reject: (error) => {
169
+ if (waiter.timer !== null) {
170
+ clearTimeout(waiter.timer);
171
+ }
172
+ if (abortListener) {
173
+ options?.signal?.removeEventListener("abort", abortListener);
174
+ abortListener = null;
175
+ }
176
+ this.eventWaiters.delete(waiter);
177
+ reject(error);
178
+ },
179
+ timer: timeoutMs === undefined
180
+ ? null
181
+ : setTimeout(() => {
182
+ this.eventWaiters.delete(waiter);
183
+ reject(new Error(`timed out waiting for sidecar event\nstderr:\n${this.stderrText()}`));
184
+ }, timeoutMs),
185
+ };
186
+ if (options?.signal) {
187
+ abortListener = () => {
188
+ waiter.reject(sidecarEventWaitAbortError(options.signal?.reason));
189
+ };
190
+ options.signal.addEventListener("abort", abortListener, { once: true });
191
+ }
192
+ this.eventWaiters.add(waiter);
193
+ });
194
+ }
195
+ failPermanently(error, options) {
196
+ if (this.closedError) {
197
+ if (!options?.replaceExisting?.(this.closedError, error)) {
198
+ return;
199
+ }
200
+ }
201
+ this.closedError = error;
202
+ this.stopSilenceWatchdog();
203
+ this.rejectPending(error);
204
+ }
205
+ dispose() {
206
+ this.stopSilenceWatchdog();
207
+ this.sidecarRequestHandlers.clear();
208
+ this.eventListeners.clear();
209
+ this.frameTransport.dispose();
210
+ }
211
+ async writeFrame(frame) {
212
+ await this.frameTransport.writeFrame(frame);
213
+ }
214
+ async dispatchSidecarRequest(request) {
215
+ const handler = this.sidecarRequestHandlers.get(ownershipSelectorKey(request.ownership))?.handler;
216
+ const payload = await resolveSidecarRequestFramePayload(request, handler ?? null);
217
+ try {
218
+ await this.writeFrame(this.hostFrameFactory.createSidecarResponseFrame({
219
+ request,
220
+ payload,
221
+ }));
222
+ }
223
+ catch (error) {
224
+ const normalized = error instanceof Error ? error : new Error(String(error));
225
+ this.failPermanently(normalized);
226
+ }
227
+ }
228
+ dispatchEvent(event) {
229
+ // Transport-level liveness beats from the sidecar. Their arrival already
230
+ // reset the silence watchdog at the frame layer; they carry no meaning
231
+ // for consumers and must never reach the bounded event buffer, where a
232
+ // long-idle VM would accumulate one every 10s until overflow.
233
+ if (event.payload.type === "structured" &&
234
+ event.payload.name === "heartbeat") {
235
+ return;
236
+ }
237
+ for (const listener of this.eventListeners) {
238
+ if (!ownershipMatchesSelector(listener.ownership, event.ownership)) {
239
+ continue;
240
+ }
241
+ try {
242
+ listener.handler(event);
243
+ }
244
+ catch {
245
+ // Event listeners are best-effort observers and must not break framing.
246
+ }
247
+ }
248
+ for (const waiter of this.eventWaiters) {
249
+ if (!waiter.matches(event)) {
250
+ continue;
251
+ }
252
+ waiter.resolve(event);
253
+ return;
254
+ }
255
+ this.bufferEvent(event);
256
+ }
257
+ bufferEvent(event) {
258
+ const overflow = this.eventBuffer.buffer(event);
259
+ if (overflow) {
260
+ this.failPermanently(overflow);
261
+ }
262
+ }
263
+ rejectPending(error) {
264
+ this.frameTransport.rejectAll(error);
265
+ for (const waiter of this.eventWaiters) {
266
+ waiter.reject(error);
267
+ }
268
+ this.eventWaiters.clear();
269
+ }
270
+ }
@@ -0,0 +1,68 @@
1
+ import { type TransportPayloadCodec } from "./frame-payload-codec.js";
2
+ import type { ClassifiedFrame } from "./frame-rpc.js";
3
+ import { type LiveSidecarRequestPayload, type LiveSidecarResponsePayload } from "./callbacks.js";
4
+ import { type LiveSidecarEventPayload } from "./event-buffer.js";
5
+ import * as protocol from "./generated-protocol.js";
6
+ import { type LiveOwnershipScope } from "./ownership.js";
7
+ import { SIDECAR_PROTOCOL_SCHEMA } from "./protocol-schema.js";
8
+ import { type LiveRequestPayload } from "./request-payloads.js";
9
+ import { type LiveResponsePayload } from "./response-payloads.js";
10
+ export interface LiveRequestFrame {
11
+ frame_type: "request";
12
+ schema: typeof SIDECAR_PROTOCOL_SCHEMA;
13
+ request_id: number;
14
+ ownership: LiveOwnershipScope;
15
+ payload: LiveRequestPayload;
16
+ }
17
+ export interface LiveEventFrame {
18
+ frame_type: "event";
19
+ schema: typeof SIDECAR_PROTOCOL_SCHEMA;
20
+ ownership: LiveOwnershipScope;
21
+ payload: LiveSidecarEventPayload;
22
+ }
23
+ export interface LiveSidecarRequestFrame {
24
+ frame_type: "sidecar_request";
25
+ schema: typeof SIDECAR_PROTOCOL_SCHEMA;
26
+ request_id: number;
27
+ ownership: LiveOwnershipScope;
28
+ payload: LiveSidecarRequestPayload;
29
+ }
30
+ export interface LiveResponseFrame {
31
+ frame_type: "response";
32
+ schema: typeof SIDECAR_PROTOCOL_SCHEMA;
33
+ request_id: number;
34
+ ownership: LiveOwnershipScope;
35
+ payload: LiveResponsePayload;
36
+ }
37
+ export interface LiveSidecarResponseFrame {
38
+ frame_type: "sidecar_response";
39
+ schema: typeof SIDECAR_PROTOCOL_SCHEMA;
40
+ request_id: number;
41
+ ownership: LiveOwnershipScope;
42
+ payload: LiveSidecarResponsePayload;
43
+ }
44
+ export type LiveProtocolFrame = LiveRequestFrame | LiveResponseFrame | LiveEventFrame | LiveSidecarRequestFrame | LiveSidecarResponseFrame;
45
+ export type LiveSidecarWrittenProtocolFrame = LiveResponseFrame | LiveEventFrame | LiveSidecarRequestFrame;
46
+ export type ProtocolFramePayloadCodec = TransportPayloadCodec;
47
+ export type ClassifiedSidecarWrittenProtocolFrame = ClassifiedFrame<LiveResponseFrame, LiveEventFrame, LiveSidecarRequestFrame>;
48
+ export type LiveSidecarRequestHandler = (request: LiveSidecarRequestFrame) => Promise<LiveSidecarResponsePayload> | LiveSidecarResponsePayload;
49
+ export declare class HostProtocolFrameFactory {
50
+ private nextRequestId;
51
+ createRequestFrame(input: {
52
+ ownership: LiveOwnershipScope;
53
+ payload: LiveRequestPayload;
54
+ }): LiveRequestFrame;
55
+ createSidecarResponseFrame(input: {
56
+ request: LiveSidecarRequestFrame;
57
+ payload: LiveSidecarResponsePayload;
58
+ }): LiveSidecarResponseFrame;
59
+ }
60
+ export declare function resolveSidecarRequestFramePayload(request: LiveSidecarRequestFrame, handler: LiveSidecarRequestHandler | null): Promise<LiveSidecarResponsePayload>;
61
+ export declare function toGeneratedProtocolFrame(frame: LiveProtocolFrame): protocol.ProtocolFrame;
62
+ export declare function encodeBareProtocolFrame(frame: LiveProtocolFrame): Uint8Array;
63
+ export declare function decodeBareProtocolFrame(payload: Uint8Array): LiveSidecarWrittenProtocolFrame;
64
+ export declare function encodeProtocolFramePayload(frame: LiveProtocolFrame, codec: ProtocolFramePayloadCodec): Uint8Array;
65
+ export declare function decodeProtocolFramePayload(payload: Uint8Array, codec: ProtocolFramePayloadCodec): LiveSidecarWrittenProtocolFrame;
66
+ export declare function classifySidecarWrittenProtocolFrame(frame: LiveSidecarWrittenProtocolFrame): ClassifiedSidecarWrittenProtocolFrame;
67
+ export declare function fromGeneratedSidecarWrittenProtocolFrame(frame: protocol.ProtocolFrame): LiveSidecarWrittenProtocolFrame;
68
+ export declare function toLiveProtocolSchema(schema: protocol.ProtocolSchema): typeof SIDECAR_PROTOCOL_SCHEMA;
@@ -0,0 +1,139 @@
1
+ import { toExactUint8Array } from "./bytes.js";
2
+ import { decodeJsonFramePayload, encodeJsonFramePayload, } from "./frame-payload-codec.js";
3
+ import { errorSidecarResponsePayload, fromGeneratedSidecarRequestPayload, isMatchingSidecarResponsePayload, toGeneratedSidecarResponsePayload, } from "./callbacks.js";
4
+ import { fromGeneratedEventPayload, } from "./event-buffer.js";
5
+ import * as protocol from "./generated-protocol.js";
6
+ import { bigIntToSafeNumber } from "./numbers.js";
7
+ import { fromGeneratedOwnershipScope, toGeneratedOwnershipScope, } from "./ownership.js";
8
+ import { SIDECAR_PROTOCOL_SCHEMA, validateSidecarProtocolSchema, } from "./protocol-schema.js";
9
+ import { toGeneratedRequestPayload, } from "./request-payloads.js";
10
+ import { fromGeneratedResponsePayload, } from "./response-payloads.js";
11
+ export class HostProtocolFrameFactory {
12
+ nextRequestId = 1;
13
+ createRequestFrame(input) {
14
+ return {
15
+ frame_type: "request",
16
+ schema: SIDECAR_PROTOCOL_SCHEMA,
17
+ request_id: this.nextRequestId++,
18
+ ownership: input.ownership,
19
+ payload: input.payload,
20
+ };
21
+ }
22
+ createSidecarResponseFrame(input) {
23
+ return {
24
+ frame_type: "sidecar_response",
25
+ schema: SIDECAR_PROTOCOL_SCHEMA,
26
+ request_id: input.request.request_id,
27
+ ownership: input.request.ownership,
28
+ payload: input.payload,
29
+ };
30
+ }
31
+ }
32
+ export async function resolveSidecarRequestFramePayload(request, handler) {
33
+ try {
34
+ if (!handler) {
35
+ throw new Error(`no sidecar request handler registered for ${request.payload.type}`);
36
+ }
37
+ const payload = await handler(request);
38
+ if (!isMatchingSidecarResponsePayload(request.payload, payload)) {
39
+ throw new Error(`sidecar handler returned ${payload.type} for ${request.payload.type}`);
40
+ }
41
+ return payload;
42
+ }
43
+ catch (error) {
44
+ return errorSidecarResponsePayload(request.payload, error);
45
+ }
46
+ }
47
+ export function toGeneratedProtocolFrame(frame) {
48
+ switch (frame.frame_type) {
49
+ case "request":
50
+ return {
51
+ tag: "RequestFrame",
52
+ val: {
53
+ schema: frame.schema,
54
+ requestId: BigInt(frame.request_id),
55
+ ownership: toGeneratedOwnershipScope(frame.ownership),
56
+ payload: toGeneratedRequestPayload(frame.payload),
57
+ },
58
+ };
59
+ case "sidecar_response":
60
+ return {
61
+ tag: "SidecarResponseFrame",
62
+ val: {
63
+ schema: frame.schema,
64
+ requestId: BigInt(frame.request_id),
65
+ ownership: toGeneratedOwnershipScope(frame.ownership),
66
+ payload: toGeneratedSidecarResponsePayload(frame.payload),
67
+ },
68
+ };
69
+ case "response":
70
+ case "event":
71
+ case "sidecar_request":
72
+ throw new Error(`BARE encoding is only implemented for host-written frames, received ${frame.frame_type}`);
73
+ }
74
+ }
75
+ export function encodeBareProtocolFrame(frame) {
76
+ return protocol.encodeProtocolFrame(toGeneratedProtocolFrame(frame));
77
+ }
78
+ export function decodeBareProtocolFrame(payload) {
79
+ return fromGeneratedSidecarWrittenProtocolFrame(protocol.decodeProtocolFrame(toExactUint8Array(payload)));
80
+ }
81
+ export function encodeProtocolFramePayload(frame, codec) {
82
+ if (codec === "json") {
83
+ return encodeJsonFramePayload(frame);
84
+ }
85
+ return encodeBareProtocolFrame(frame);
86
+ }
87
+ export function decodeProtocolFramePayload(payload, codec) {
88
+ if (codec === "json") {
89
+ return decodeJsonFramePayload(payload);
90
+ }
91
+ return decodeBareProtocolFrame(payload);
92
+ }
93
+ export function classifySidecarWrittenProtocolFrame(frame) {
94
+ switch (frame.frame_type) {
95
+ case "response":
96
+ return {
97
+ kind: "response",
98
+ requestId: frame.request_id,
99
+ frame,
100
+ };
101
+ case "event":
102
+ return { kind: "event", frame };
103
+ case "sidecar_request":
104
+ return { kind: "sidecarRequest", frame };
105
+ }
106
+ }
107
+ export function fromGeneratedSidecarWrittenProtocolFrame(frame) {
108
+ switch (frame.tag) {
109
+ case "ResponseFrame":
110
+ return {
111
+ frame_type: "response",
112
+ schema: toLiveProtocolSchema(frame.val.schema),
113
+ request_id: bigIntToSafeNumber(frame.val.requestId, "response request id"),
114
+ ownership: fromGeneratedOwnershipScope(frame.val.ownership),
115
+ payload: fromGeneratedResponsePayload(frame.val.payload),
116
+ };
117
+ case "EventFrame":
118
+ return {
119
+ frame_type: "event",
120
+ schema: toLiveProtocolSchema(frame.val.schema),
121
+ ownership: fromGeneratedOwnershipScope(frame.val.ownership),
122
+ payload: fromGeneratedEventPayload(frame.val.payload),
123
+ };
124
+ case "SidecarRequestFrame":
125
+ return {
126
+ frame_type: "sidecar_request",
127
+ schema: toLiveProtocolSchema(frame.val.schema),
128
+ request_id: bigIntToSafeNumber(frame.val.requestId, "sidecar request id"),
129
+ ownership: fromGeneratedOwnershipScope(frame.val.ownership),
130
+ payload: fromGeneratedSidecarRequestPayload(frame.val.payload),
131
+ };
132
+ case "RequestFrame":
133
+ case "SidecarResponseFrame":
134
+ throw new Error(`unsupported BARE protocol frame tag: ${frame.tag}`);
135
+ }
136
+ }
137
+ export function toLiveProtocolSchema(schema) {
138
+ return validateSidecarProtocolSchema(schema);
139
+ }
@@ -0,0 +1,31 @@
1
+ import * as protocol from "./generated-protocol.js";
2
+ export type LiveGuestRuntimeKind = "java_script" | "python" | "web_assembly";
3
+ export type LiveDisposeReason = "requested" | "connection_closed" | "host_shutdown";
4
+ export type LiveRootFilesystemMode = "ephemeral" | "read_only";
5
+ export type LiveRootFilesystemEntryKind = "file" | "directory" | "symlink";
6
+ export type LiveRootFilesystemEntryEncoding = "utf8" | "base64";
7
+ export type LiveWasmPermissionTier = "full" | "read-write" | "read-only" | "isolated";
8
+ export type LivePermissionMode = "allow" | "ask" | "deny";
9
+ export type LiveGuestFilesystemOperation = "read_file" | "write_file" | "create_dir" | "mkdir" | "exists" | "stat" | "lstat" | "read_dir" | "read_dir_recursive" | "remove_file" | "remove_dir" | "remove" | "copy" | "move" | "rename" | "realpath" | "symlink" | "read_link" | "link" | "chmod" | "chown" | "utimes" | "truncate" | "pread" | "pwrite";
10
+ export type LiveFilesystemOperation = "read" | "write" | "stat" | "read_dir" | "mkdir" | "remove" | "rename";
11
+ export type LiveVmLifecycleState = "creating" | "ready" | "disposing" | "disposed" | "failed";
12
+ export type LiveStreamChannel = "stdout" | "stderr";
13
+ export type LiveProcessSnapshotStatus = "running" | "exited" | "stopped";
14
+ export type LiveSignalDispositionAction = "default" | "ignore" | "user";
15
+ export declare function toGeneratedPermissionMode(mode: LivePermissionMode): protocol.PermissionMode;
16
+ export declare function toGeneratedGuestRuntimeKind(runtime: LiveGuestRuntimeKind): protocol.GuestRuntimeKind;
17
+ export declare function toGeneratedDisposeReason(reason: LiveDisposeReason): protocol.DisposeReason;
18
+ export declare function toGeneratedRootFilesystemMode(mode: LiveRootFilesystemMode): protocol.RootFilesystemMode;
19
+ export declare function toGeneratedRootFilesystemEntryKind(kind: LiveRootFilesystemEntryKind): protocol.RootFilesystemEntryKind;
20
+ export declare function toGeneratedRootFilesystemEntryEncoding(encoding: LiveRootFilesystemEntryEncoding): protocol.RootFilesystemEntryEncoding;
21
+ export declare function toGeneratedWasmPermissionTier(tier: LiveWasmPermissionTier): protocol.WasmPermissionTier;
22
+ export declare function toGeneratedGuestFilesystemOperation(operation: LiveGuestFilesystemOperation): protocol.GuestFilesystemOperation;
23
+ export declare function toGeneratedFilesystemOperation(operation: LiveFilesystemOperation): protocol.FilesystemOperation;
24
+ export declare function fromGeneratedFilesystemOperation(operation: protocol.FilesystemOperation): LiveFilesystemOperation;
25
+ export declare function fromGeneratedVmLifecycleState(state: protocol.VmLifecycleState): LiveVmLifecycleState;
26
+ export declare function fromGeneratedStreamChannel(channel: protocol.StreamChannel): LiveStreamChannel;
27
+ export declare function fromGeneratedProcessSnapshotStatus(status: protocol.ProcessSnapshotStatus): LiveProcessSnapshotStatus;
28
+ export declare function fromGeneratedSignalDispositionAction(action: protocol.SignalDispositionAction): LiveSignalDispositionAction;
29
+ export declare function fromGeneratedRootFilesystemEntryKind(kind: protocol.RootFilesystemEntryKind): LiveRootFilesystemEntryKind;
30
+ export declare function fromGeneratedRootFilesystemEntryEncoding(encoding: protocol.RootFilesystemEntryEncoding): LiveRootFilesystemEntryEncoding;
31
+ export declare function fromGeneratedGuestFilesystemOperation(operation: protocol.GuestFilesystemOperation): LiveGuestFilesystemOperation;