@blaxel/core 0.2.87-preview.169 → 0.2.87-preview.170

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.
@@ -97,64 +97,44 @@ export function createH2Fetch(session) {
97
97
  };
98
98
  }
99
99
  /**
100
- * Creates a fetch()-compatible function backed by the H2 session pool.
100
+ * The single HTTP/2 request gateway (ENG-2679).
101
101
  *
102
- * The pool validates idle sessions before reuse. If no usable H2 session is
103
- * available, the request falls back to regular fetch before any H2 frames
104
- * are sent.
102
+ * Every pool-backed request funnels through here, no matter which entry point
103
+ * it came from: the generated client's fetch (`createPoolBackedH2Fetch`),
104
+ * `SandboxAction.h2Fetch`, and the interpreter's direct path (both via
105
+ * `h2RequestDirectFromPool`). It owns the shared request lifecycle in one place:
106
+ * 1. take a per-domain open-stream slot,
107
+ * 2. get a live session from the pool,
108
+ * 3. send the request on it,
109
+ * 4. evict the session if the send fails after a stream was opened,
110
+ * 5. fall back to globalThis.fetch when the pool has no usable session.
111
+ *
112
+ * This is the chokepoint where reliability behavior that must protect EVERY
113
+ * consumer belongs: the open-stream concurrency limit today, and retry,
114
+ * timeouts, typed errors, and observability in later phases. Adding it once here
115
+ * (instead of re-implementing it per entry point) is what stops the recurring
116
+ * "fixed on one path, still broken on another" regressions.
117
+ *
118
+ * `send` performs the actual wire send on a live session; the caller supplies it
119
+ * so the gateway stays agnostic to the `Request` vs `(url, init)` call shapes,
120
+ * and it receives the slot-release and request-created hooks. `fallback` runs
121
+ * only when the pool has no usable session (a fresh connection, no shared
122
+ * stream is opened).
105
123
  */
106
- export function createPoolBackedH2Fetch(pool, domain) {
107
- return async (input) => {
108
- // Acquire the slot here, but hand its release to the send path: it is held
109
- // for the OPEN-STREAM lifetime and freed on the stream terminal. Paths that
110
- // never open a stream on the shared session (fetch fallbacks go over a
111
- // different connection) release it immediately so they do not count against
112
- // the open-stream budget. `rel` is idempotent, so the explicit releases
113
- // below are safe even though the send path may also release.
114
- const rel = await acquireH2Slot(domain);
115
- try {
116
- const session = await pool.get(domain);
117
- if (session) {
118
- let h2RequestCreated = false;
119
- try {
120
- return await _h2Request(session, input, {
121
- onH2RequestCreated: () => {
122
- h2RequestCreated = true;
123
- },
124
- releaseSlot: rel,
125
- });
126
- }
127
- catch (err) {
128
- if (h2RequestCreated) {
129
- pool.evictSession(domain, session);
130
- }
131
- throw err;
132
- }
133
- }
134
- // No usable session: this fetch does not open a stream on the shared
135
- // session, so free the slot before falling back.
136
- rel();
137
- return await globalThis.fetch(input);
138
- }
139
- catch (err) {
140
- // Pre-send throw (e.g. pool.get() or Request body read): release the slot
141
- // so a failed request never pins it. Idempotent if already released.
142
- rel();
143
- throw err;
144
- }
145
- };
146
- }
147
- export async function h2RequestDirectFromPool(pool, domain, url, init) {
148
- // See createPoolBackedH2Fetch: the slot is held for the OPEN-STREAM lifetime
149
- // and released by the send path on the stream terminal; non-stream fallbacks
150
- // release it immediately. `rel` is idempotent.
124
+ async function h2GatewayRequest(pool, domain, send, fallback) {
125
+ // Take the slot here, but hand its release to the send path: it is held for
126
+ // the OPEN-STREAM lifetime and freed on the stream terminal (ENG-2678). A
127
+ // request that never opens a stream on the shared session (the fallback goes
128
+ // over a different connection) releases it immediately so it does not count
129
+ // against the open-stream budget. `rel` is idempotent, so releasing here when
130
+ // the send path may also release is safe.
151
131
  const rel = await acquireH2Slot(domain);
152
132
  try {
153
133
  const session = await pool.get(domain);
154
134
  if (session) {
155
135
  let h2RequestCreated = false;
156
136
  try {
157
- return await h2RequestDirectInternal(session, url, init, {
137
+ return await send(session, {
158
138
  onH2RequestCreated: () => {
159
139
  h2RequestCreated = true;
160
140
  },
@@ -162,21 +142,44 @@ export async function h2RequestDirectFromPool(pool, domain, url, init) {
162
142
  });
163
143
  }
164
144
  catch (err) {
145
+ // A failure AFTER a stream opened means the session is suspect: drop it
146
+ // so the next caller gets a fresh one.
165
147
  if (h2RequestCreated) {
166
148
  pool.evictSession(domain, session);
167
149
  }
168
150
  throw err;
169
151
  }
170
152
  }
171
- // No usable session: fetch over a different connection, no stream opened.
153
+ // No usable session: free the slot before falling back over a different
154
+ // connection (no stream opens on the shared session).
172
155
  rel();
173
- return await globalThis.fetch(url, init);
156
+ return await fallback();
174
157
  }
175
158
  catch (err) {
159
+ // Pre-send throw (pool.get(), Request body read, or the fallback itself):
160
+ // release the slot so a failed request never pins it. Idempotent.
176
161
  rel();
177
162
  throw err;
178
163
  }
179
164
  }
165
+ /**
166
+ * Creates a fetch()-compatible function backed by the H2 session pool, routed
167
+ * through the single gateway. Used as the generated client's `fetch`.
168
+ *
169
+ * If no usable H2 session is available, the request falls back to regular fetch
170
+ * before any H2 frames are sent.
171
+ */
172
+ export function createPoolBackedH2Fetch(pool, domain) {
173
+ return (input) => h2GatewayRequest(pool, domain, (session, options) => _h2Request(session, input, options), () => globalThis.fetch(input));
174
+ }
175
+ /**
176
+ * Pool-backed H2 request taking raw url + init (skips Request allocation),
177
+ * routed through the same single gateway. Used by `SandboxAction.h2Fetch` and
178
+ * the code interpreter.
179
+ */
180
+ export function h2RequestDirectFromPool(pool, domain, url, init) {
181
+ return h2GatewayRequest(pool, domain, (session, options) => h2RequestDirectInternal(session, url, init, options), () => globalThis.fetch(url, init));
182
+ }
180
183
  /**
181
184
  * Low-level H2 request that takes raw URL + init, skipping Request construction.
182
185
  * Used by SandboxAction.h2Fetch() for direct calls from subsystems.
@@ -5,8 +5,8 @@ import { authentication } from "../authentication/index.js";
5
5
  import { env } from "../common/env.js";
6
6
  import { fs, os, path } from "../common/node.js";
7
7
  // Build info - these placeholders are replaced at build time by build:replace-imports
8
- const BUILD_VERSION = "0.2.87-preview.169";
9
- const BUILD_COMMIT = "5357b12d98548db2b306537be7d96d2888ac8cb1";
8
+ const BUILD_VERSION = "0.2.87-preview.170";
9
+ const BUILD_COMMIT = "0988680b8e320e4f8a4e0007eec9a30cb043aac2";
10
10
  const BUILD_SENTRY_DSN = "https://fd5e60e1c9820e1eef5ccebb84a07127@o4508714045276160.ingest.us.sentry.io/4510465864564736";
11
11
  const BLAXEL_API_VERSION = "2026-04-16";
12
12
  // Cache for config.yaml tracking value