@withgateway/sdk 0.1.13 → 0.1.16

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.
package/dist/index.mjs CHANGED
@@ -8,6 +8,9 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
8
8
  if (typeof require !== "undefined") return require.apply(this, arguments);
9
9
  throw Error('Dynamic require of "' + x + '" is not supported');
10
10
  });
11
+ var __esm = (fn, res) => function __init() {
12
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
13
+ };
11
14
  var __commonJS = (cb, mod) => function __require2() {
12
15
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
13
16
  };
@@ -16,12 +19,1064 @@ var __export = (target, all) => {
16
19
  __defProp(target, name, { get: all[name], enumerable: true });
17
20
  };
18
21
 
22
+ // src/worlds/session.ts
23
+ var session_exports = {};
24
+ __export(session_exports, {
25
+ WorldSession: () => WorldSession,
26
+ WorldSessionCallError: () => WorldSessionCallError,
27
+ WorldSessionTimeout: () => WorldSessionTimeout,
28
+ attachSession: () => attachSession,
29
+ openSession: () => openSession,
30
+ runSessions: () => runSessions,
31
+ schemaFromSignature: () => schemaFromSignature
32
+ });
33
+ function schemaFromSignature(spec) {
34
+ const inside = /\(([\s\S]*)\)\s*$/.exec(spec.signature.trim())?.[1] ?? "";
35
+ const properties = {};
36
+ const required = [];
37
+ const params = [];
38
+ let depth = 0;
39
+ let current = "";
40
+ for (const ch of inside) {
41
+ if ("([{".includes(ch)) depth++;
42
+ if (")]}".includes(ch)) depth--;
43
+ if (ch === "," && depth === 0) {
44
+ params.push(current);
45
+ current = "";
46
+ } else current += ch;
47
+ }
48
+ if (current.trim()) params.push(current);
49
+ for (const raw of params) {
50
+ const param = raw.trim();
51
+ if (!param || param.startsWith("*") || param === "self") continue;
52
+ const eq = param.indexOf("=");
53
+ const name = (eq === -1 ? param : param.slice(0, eq)).split(":")[0].trim();
54
+ if (!name) continue;
55
+ if (eq === -1) {
56
+ properties[name] = { type: "string" };
57
+ required.push(name);
58
+ continue;
59
+ }
60
+ const value = param.slice(eq + 1).trim();
61
+ if (/^-?\d+(\.\d+)?$/.test(value)) properties[name] = { type: "number" };
62
+ else if (value === "True" || value === "False")
63
+ properties[name] = { type: "boolean" };
64
+ else properties[name] = { type: "string" };
65
+ }
66
+ return {
67
+ type: "function",
68
+ function: {
69
+ name: spec.name,
70
+ description: spec.doc ?? spec.name,
71
+ parameters: { type: "object", properties, required }
72
+ }
73
+ };
74
+ }
75
+ async function attachSession(sessionId, options = {}) {
76
+ const state2 = await apiRequest(
77
+ options,
78
+ "GET",
79
+ `/api/public/world-sessions/${encodeURIComponent(sessionId)}`
80
+ );
81
+ return new WorldSession(sessionId, state2, options);
82
+ }
83
+ async function openSession(slugOrRef, task, opts = {}) {
84
+ const options = opts.options ?? {};
85
+ const at = slugOrRef.indexOf("@");
86
+ const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
87
+ const state2 = await apiRequest(
88
+ options,
89
+ "POST",
90
+ "/api/public/world-sessions",
91
+ {
92
+ ...opts.versionId ? { versionId: opts.versionId } : { slug },
93
+ task,
94
+ ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm }
95
+ }
96
+ );
97
+ return new WorldSession(state2.sessionId, state2, options);
98
+ }
99
+ async function runSessions(opts) {
100
+ const options = opts.options ?? {};
101
+ const concurrency = Math.max(1, opts.concurrency ?? 3);
102
+ let tasks = opts.tasks;
103
+ let versionId = opts.versionId;
104
+ if (!tasks || !versionId) {
105
+ const { openWorld: openWorld2 } = await Promise.resolve().then(() => (init_worlds(), worlds_exports));
106
+ const world = await openWorld2(opts.world, options);
107
+ tasks = tasks ?? world.tasks.map((t) => t.name);
108
+ versionId = versionId ?? world.version.versionId;
109
+ }
110
+ const results = [];
111
+ let next = 0;
112
+ await Promise.all(
113
+ Array.from({ length: Math.min(concurrency, tasks.length) }, async () => {
114
+ while (next < tasks.length) {
115
+ const task = tasks[next++];
116
+ let session = null;
117
+ try {
118
+ session = await openSession(opts.world, task, {
119
+ versionId,
120
+ options
121
+ });
122
+ await session.ready();
123
+ const toolkit = await session.toolkit();
124
+ await opts.agent({
125
+ session,
126
+ toolkit,
127
+ instruction: session.instruction ?? task,
128
+ task
129
+ });
130
+ const graded = await session.grade();
131
+ const result = {
132
+ task,
133
+ sessionId: session.sessionId,
134
+ reward: graded.reward,
135
+ rewards: graded.rewards
136
+ };
137
+ results.push(result);
138
+ opts.onResult?.(result);
139
+ } catch (error) {
140
+ const result = {
141
+ task,
142
+ sessionId: session?.sessionId ?? "",
143
+ reward: 0,
144
+ rewards: {},
145
+ // An Error with an empty message would read as "no error" to any
146
+ // caller checking truthiness; name the type instead.
147
+ error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
148
+ };
149
+ results.push(result);
150
+ opts.onResult?.(result);
151
+ } finally {
152
+ await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
153
+ }
154
+ }
155
+ })
156
+ );
157
+ const mean = results.reduce((sum, r) => sum + (r.reward ?? 0), 0) / (results.length || 1);
158
+ return {
159
+ results: results.sort((a, b) => a.task.localeCompare(b.task)),
160
+ mean,
161
+ passed: opts.failUnder === void 0 ? true : mean >= opts.failUnder
162
+ };
163
+ }
164
+ var READY_TIMEOUT_MS, READY_POLL_MS, CALL_TIMEOUT_MS, CALL_POLL_MS, sleep2, WorldSessionTimeout, WorldSessionCallError, WorldSession;
165
+ var init_session = __esm({
166
+ "src/worlds/session.ts"() {
167
+ "use strict";
168
+ init_worlds();
169
+ READY_TIMEOUT_MS = 10 * 6e4;
170
+ READY_POLL_MS = 2e3;
171
+ CALL_TIMEOUT_MS = 3 * 6e4;
172
+ CALL_POLL_MS = 400;
173
+ sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
174
+ WorldSessionTimeout = class extends Error {
175
+ };
176
+ WorldSessionCallError = class extends Error {
177
+ };
178
+ WorldSession = class {
179
+ constructor(sessionId, state2, options) {
180
+ this.sessionId = sessionId;
181
+ this.state = state2;
182
+ this.options = options;
183
+ }
184
+ get task() {
185
+ return this.state.taskName;
186
+ }
187
+ /** What this task asks for — the bundle's own instruction.md. */
188
+ get instruction() {
189
+ return this.state.instruction ?? null;
190
+ }
191
+ get versionId() {
192
+ return this.state.versionId;
193
+ }
194
+ /** The session's latest platform state (one request, no waiting). */
195
+ async status() {
196
+ const fresh = await apiRequest(
197
+ this.options,
198
+ "GET",
199
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`
200
+ );
201
+ this.state = {
202
+ instruction: this.state.instruction,
203
+ tools: this.state.tools,
204
+ ...fresh
205
+ };
206
+ return this.state;
207
+ }
208
+ /**
209
+ * Wait until the container is warm — meaning the process inside it has
210
+ * asked the platform for work, not merely that a job was accepted.
211
+ */
212
+ async ready(opts = {}) {
213
+ const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
214
+ for (; ; ) {
215
+ const state2 = await this.status();
216
+ if (state2.ready) return state2;
217
+ if (state2.state === "FAILED" || state2.state === "STOPPED")
218
+ throw new WorldSessionCallError(
219
+ `World session ${state2.state.toLowerCase()} before it was ready.`
220
+ );
221
+ if (Date.now() > deadline)
222
+ throw new WorldSessionTimeout(
223
+ `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
224
+ );
225
+ await sleep2(READY_POLL_MS);
226
+ }
227
+ }
228
+ /** Enqueue one interaction and wait for the container's answer. */
229
+ async exchange(body, timeoutMs = CALL_TIMEOUT_MS) {
230
+ const { callId } = await apiRequest(
231
+ this.options,
232
+ "POST",
233
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/calls`,
234
+ body
235
+ );
236
+ const deadline = Date.now() + timeoutMs;
237
+ for (; ; ) {
238
+ const call = await apiRequest(
239
+ this.options,
240
+ "GET",
241
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/calls/${encodeURIComponent(callId)}`
242
+ );
243
+ if (call.done) {
244
+ if (call.error) throw new WorldSessionCallError(call.error);
245
+ return call.result;
246
+ }
247
+ if (Date.now() > deadline)
248
+ throw new WorldSessionTimeout(
249
+ `Call ${callId} still ${call.state} after ${Math.round(timeoutMs / 1e3)}s \u2014 the container may have died.`
250
+ );
251
+ await sleep2(CALL_POLL_MS);
252
+ }
253
+ }
254
+ /**
255
+ * Run one of the world's tools INSIDE the container. The arguments and the
256
+ * result are the world's own contract — this layer neither validates nor
257
+ * reshapes them, so a tool error reads exactly as it would in a hosted run.
258
+ */
259
+ async call(tool, args = {}, opts = {}) {
260
+ return this.exchange({ kind: "call", tool, args }, opts.timeoutMs);
261
+ }
262
+ /**
263
+ * THE WORLD'S TOOLS, READY TO HAND TO A MODEL. Schemas come from the
264
+ * world's own signatures and implementations call into this session, so a
265
+ * caller never writes — and never has to re-sync — a tool map of their own:
266
+ *
267
+ * const { schemas, impls } = await session.toolkit();
268
+ * await runMyAgent(impls, schemas, session.instruction);
269
+ *
270
+ * A tool added to the world shows up here on the next run; a tool renamed
271
+ * in the world renames here too, instead of failing at call time against a
272
+ * hand-written map that still remembers the old name.
273
+ */
274
+ async toolkit() {
275
+ let specs = this.state.tools;
276
+ if (!specs) specs = (await this.status()).tools ?? [];
277
+ const impls = {};
278
+ for (const spec of specs) {
279
+ impls[spec.name] = (args = {}) => this.call(spec.name, args);
280
+ }
281
+ return { schemas: specs.map(schemaFromSignature), impls, specs };
282
+ }
283
+ /** Run the task's grader against the container's END STATE. */
284
+ async grade(opts = {}) {
285
+ const raw = await this.exchange(
286
+ { kind: "grade" },
287
+ opts.timeoutMs
288
+ );
289
+ const record = raw && typeof raw === "object" ? raw : {};
290
+ const rewards = {};
291
+ for (const [key, value] of Object.entries(record)) {
292
+ if (key !== "reward" && typeof value === "number") rewards[key] = value;
293
+ }
294
+ return {
295
+ reward: typeof record.reward === "number" ? record.reward : null,
296
+ rewards,
297
+ raw
298
+ };
299
+ }
300
+ /**
301
+ * End the session. `keepWarm` hands the container back to the pool instead
302
+ * of tearing it down, so the next session for the same task starts warm —
303
+ * the pooled instance still obeys the fleet's idle TTL and pinning.
304
+ */
305
+ async close(opts = {}) {
306
+ return apiRequest(
307
+ this.options,
308
+ "DELETE",
309
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`,
310
+ { keepWarm: opts.keepWarm ?? false }
311
+ );
312
+ }
313
+ };
314
+ }
315
+ });
316
+
317
+ // src/worlds/hub.ts
318
+ import { createHash as createHash2 } from "crypto";
319
+ import { gzipSync, gunzipSync } from "zlib";
320
+ import * as fs from "fs";
321
+ import * as path from "path";
322
+ function isExcludedDirComponent(component) {
323
+ return component.startsWith(".") || ROOT_EXCLUDED_DIRS.has(component) || component.endsWith(".egg-info");
324
+ }
325
+ function isIncludedInContentHash(relPath, includeAllRootFiles) {
326
+ const parts = relPath.split("/");
327
+ const name = parts[parts.length - 1];
328
+ const dirParts = parts.slice(0, -1);
329
+ if (name.startsWith(".")) return false;
330
+ if (parts.includes("__pycache__")) return false;
331
+ if (dirParts.length === 0) {
332
+ return includeAllRootFiles || name === "README.md" || name === "pyproject.toml" || name === "gateway-env.toml" || name.endsWith(".py");
333
+ }
334
+ return !dirParts.some(isExcludedDirComponent);
335
+ }
336
+ function collectArchiveRelPaths(dir) {
337
+ if (fs.existsSync(path.join(dir, ".gitignore"))) {
338
+ throw new Error(
339
+ "This bundle has a root .gitignore, which the Python CLI honors via gitignore_parser. Reimplementing that imperfectly here would silently produce a different content hash, so the TS CLI refuses. Remove the .gitignore (or bake its effect into the tree) and retry."
340
+ );
341
+ }
342
+ const out = [];
343
+ const includeAllRootFiles = fs.existsSync(path.join(dir, "gateway-env.toml"));
344
+ const walk = (rel) => {
345
+ const abs = rel ? path.join(dir, rel) : dir;
346
+ for (const entry of fs.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
347
+ const childRel = rel ? `${rel}/${entry.name}` : entry.name;
348
+ if (entry.isDirectory()) {
349
+ if (!isExcludedDirComponent(entry.name)) walk(childRel);
350
+ } else if (entry.isFile()) {
351
+ if (isIncludedInContentHash(childRel, includeAllRootFiles)) {
352
+ out.push(childRel);
353
+ }
354
+ }
355
+ }
356
+ };
357
+ walk("");
358
+ return out.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
359
+ }
360
+ function computeContentHash(dir) {
361
+ const hasher = createHash2("sha256");
362
+ for (const relPath of collectArchiveRelPaths(dir)) {
363
+ hasher.update(Buffer.from(`file:${relPath}`, "utf-8"));
364
+ hasher.update(fs.readFileSync(path.join(dir, relPath)));
365
+ }
366
+ return hasher.digest("hex");
367
+ }
368
+ function octal(value, length) {
369
+ return Buffer.from(value.toString(8).padStart(length - 1, "0") + "\0", "ascii");
370
+ }
371
+ function tarHeader(name, size) {
372
+ const header = Buffer.alloc(512);
373
+ header.write(name, 0, 100, "utf-8");
374
+ octal(420, 8).copy(header, 100);
375
+ octal(0, 8).copy(header, 108);
376
+ octal(0, 8).copy(header, 116);
377
+ octal(size, 12).copy(header, 124);
378
+ octal(0, 12).copy(header, 136);
379
+ header.write(" ", 148, "ascii");
380
+ header.write("0", 156, "ascii");
381
+ header.write("ustar", 257, "ascii");
382
+ header.write("00", 263, "ascii");
383
+ let sum = 0;
384
+ for (const byte of header) sum += byte;
385
+ header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, "ascii");
386
+ return header;
387
+ }
388
+ function tarMember(name, payload) {
389
+ const parts = [];
390
+ const nameBytes = Buffer.byteLength(name, "utf-8");
391
+ if (nameBytes > 100) {
392
+ const body = Buffer.from(name + "\0", "utf-8");
393
+ const lHeader = tarHeader("././@LongLink", body.length);
394
+ lHeader.write("L", 156, "ascii");
395
+ let sum = 0;
396
+ lHeader.write(" ", 148, "ascii");
397
+ for (const byte of lHeader) sum += byte;
398
+ lHeader.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, "ascii");
399
+ parts.push(lHeader, body);
400
+ const pad2 = body.length % 512 === 0 ? 0 : 512 - body.length % 512;
401
+ if (pad2) parts.push(Buffer.alloc(pad2));
402
+ parts.push(tarHeader(name.slice(0, 100), payload.length));
403
+ } else {
404
+ parts.push(tarHeader(name, payload.length));
405
+ }
406
+ parts.push(payload);
407
+ const pad = payload.length % 512 === 0 ? 0 : 512 - payload.length % 512;
408
+ if (pad) parts.push(Buffer.alloc(pad));
409
+ return parts;
410
+ }
411
+ function buildSourceTarball(dir) {
412
+ const parts = [];
413
+ for (const relPath of collectArchiveRelPaths(dir)) {
414
+ parts.push(...tarMember(relPath, fs.readFileSync(path.join(dir, relPath))));
415
+ }
416
+ parts.push(Buffer.alloc(1024));
417
+ return gzipSync(Buffer.concat(parts), { level: 9 });
418
+ }
419
+ function readLocalMeta(dir) {
420
+ try {
421
+ const raw = fs.readFileSync(path.join(dir, META_RELPATH), "utf-8");
422
+ const parsed = JSON.parse(raw);
423
+ return parsed && typeof parsed === "object" ? parsed : null;
424
+ } catch {
425
+ return null;
426
+ }
427
+ }
428
+ function writeLocalMeta(dir, meta) {
429
+ fs.mkdirSync(path.join(dir, ".gateway"), { recursive: true });
430
+ fs.writeFileSync(path.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
431
+ }
432
+ function snapshotTreeHashes(dir) {
433
+ const out = {};
434
+ for (const relPath of collectArchiveRelPaths(dir)) {
435
+ out[relPath] = createHash2("sha256").update(fs.readFileSync(path.join(dir, relPath))).digest("hex");
436
+ }
437
+ return out;
438
+ }
439
+ function localTreeState(dir) {
440
+ const meta = readLocalMeta(dir);
441
+ const base = meta?.baseFiles;
442
+ if (!base || typeof base !== "object") {
443
+ return { meta, dirty: [], clean: false };
444
+ }
445
+ const now = snapshotTreeHashes(dir);
446
+ const dirty = /* @__PURE__ */ new Set();
447
+ for (const [p, h] of Object.entries(now)) if (base[p] !== h) dirty.add(p);
448
+ for (const p of Object.keys(base)) if (!(p in now)) dirty.add(p);
449
+ return { meta, dirty: [...dirty].sort(), clean: dirty.size === 0 };
450
+ }
451
+ function readPyproject(dir) {
452
+ try {
453
+ const text = fs.readFileSync(path.join(dir, "pyproject.toml"), "utf-8");
454
+ const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text)?.[1] ?? "";
455
+ const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
456
+ const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
457
+ return { name, version };
458
+ } catch {
459
+ return {};
460
+ }
461
+ }
462
+ function buildFileManifest(dir) {
463
+ const entries = [];
464
+ const walk = (rel) => {
465
+ const abs = rel ? path.join(dir, rel) : dir;
466
+ for (const entry of fs.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
467
+ const childRel = rel ? `${rel}/${entry.name}` : entry.name;
468
+ if (entry.isDirectory()) {
469
+ if (!SKIP_DIRS.has(entry.name) && !entry.name.endsWith(".egg-info")) {
470
+ walk(childRel);
471
+ }
472
+ } else if (entry.isFile()) {
473
+ const payload = fs.readFileSync(path.join(dir, childRel));
474
+ entries.push({
475
+ path: childRel,
476
+ isDirectory: false,
477
+ size: payload.length,
478
+ sha256: createHash2("sha256").update(payload).digest("hex")
479
+ });
480
+ }
481
+ }
482
+ };
483
+ walk("");
484
+ return entries;
485
+ }
486
+ async function putObject(url, data, shaB64) {
487
+ const resp = await fetch(url, {
488
+ method: "PUT",
489
+ headers: {
490
+ "Content-Type": "application/gzip",
491
+ "x-amz-checksum-sha256": shaB64
492
+ },
493
+ body: new Uint8Array(data)
494
+ });
495
+ if (!resp.ok) {
496
+ throw new Error(`object PUT -> ${resp.status}: ${(await resp.text()).slice(0, 300)}`);
497
+ }
498
+ }
499
+ async function push(dir, opts = {}) {
500
+ const options = opts.options ?? {};
501
+ if (!fs.statSync(dir).isDirectory()) throw new Error(`Not a directory: ${dir}`);
502
+ const localMeta = readLocalMeta(dir) ?? {};
503
+ const branch = opts.branch ?? localMeta.branch ?? DEFAULT_BRANCH;
504
+ const baseVersionId = localMeta.versionId ?? null;
505
+ const contentHash = computeContentHash(dir);
506
+ const pyproject = readPyproject(dir);
507
+ const slug = opts.slug ?? pyproject.name ?? path.basename(path.resolve(dir));
508
+ const resolve2 = await apiRequest(
509
+ options,
510
+ "POST",
511
+ "/api/public/benchmark-containers/resolve",
512
+ { slug, name: pyproject.name ?? slug }
513
+ );
514
+ const tarball = buildSourceTarball(dir);
515
+ const bundleSha256 = sha256b64(tarball);
516
+ const version = await apiRequest(options, "POST", "/api/public/benchmark-containers/versions", {
517
+ containerId: resolve2.containerId,
518
+ contentHash,
519
+ bundleSha256,
520
+ bundleSizeBytes: tarball.length,
521
+ ...pyproject.version ? { semanticVersion: pyproject.version } : {},
522
+ pyprojectMeta: pyproject,
523
+ fileManifest: buildFileManifest(dir),
524
+ branch,
525
+ baseVersionId,
526
+ ...opts.force ? { force: true } : {},
527
+ ...opts.message ? { changeReason: opts.message } : {},
528
+ ...opts.author ? { author: opts.author } : {}
529
+ });
530
+ if (version.uploadUrl) await putObject(version.uploadUrl, tarball, bundleSha256);
531
+ await apiRequest(
532
+ options,
533
+ "POST",
534
+ `/api/public/benchmark-containers/versions/${version.versionId}/finalize`,
535
+ { branch, baseVersionId, ...opts.force ? { force: true } : {} }
536
+ );
537
+ writeLocalMeta(dir, {
538
+ ...localMeta,
539
+ slug,
540
+ ref: branch,
541
+ branch,
542
+ versionId: version.versionId,
543
+ contentHash,
544
+ semanticVersion: pyproject.version,
545
+ forkedFromVersionId: baseVersionId,
546
+ baseFiles: snapshotTreeHashes(dir)
547
+ });
548
+ return { slug, versionId: version.versionId, contentHash, branch };
549
+ }
550
+ function extractBundle(bundle, dir) {
551
+ const tar = gunzipSync(bundle);
552
+ let offset = 0;
553
+ let longName = null;
554
+ let written = 0;
555
+ while (offset + 512 <= tar.length) {
556
+ const header = tar.subarray(offset, offset + 512);
557
+ offset += 512;
558
+ if (header.every((b) => b === 0)) break;
559
+ const rawName = header.subarray(0, 100).toString("utf-8").replace(/\0.*$/, "");
560
+ const size = parseInt(header.subarray(124, 136).toString("ascii").replace(/\0.*$/, "").trim(), 8) || 0;
561
+ const typeflag = header.subarray(156, 157).toString("ascii");
562
+ const body = tar.subarray(offset, offset + size);
563
+ offset += size + (size % 512 === 0 ? 0 : 512 - size % 512);
564
+ if (typeflag === "L") {
565
+ longName = body.toString("utf-8").replace(/\0.*$/, "");
566
+ continue;
567
+ }
568
+ const name = (longName ?? rawName).replace(/^\.\//, "");
569
+ longName = null;
570
+ if (typeflag !== "0" && typeflag !== "") continue;
571
+ if (name.startsWith("/") || name.split("/").includes("..")) continue;
572
+ const target = path.join(dir, name);
573
+ fs.mkdirSync(path.dirname(target), { recursive: true });
574
+ fs.writeFileSync(target, body);
575
+ written++;
576
+ }
577
+ return written;
578
+ }
579
+ async function pull(slugRef, target, options = {}) {
580
+ const at = slugRef.indexOf("@");
581
+ const slug = at === -1 ? slugRef : slugRef.slice(0, at);
582
+ const ref = at === -1 ? "latest" : slugRef.slice(at + 1);
583
+ const details = await apiRequest(options, "POST", "/api/public/benchmark-containers/resolve-version", {
584
+ slug,
585
+ ref
586
+ });
587
+ if (!details.downloadUrl) {
588
+ throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
589
+ }
590
+ let dir = target ?? path.join(process.cwd(), slug);
591
+ if (!target) {
592
+ let i = 1;
593
+ const base = dir;
594
+ while (fs.existsSync(dir)) dir = `${base}-${i++}`;
595
+ }
596
+ fs.mkdirSync(dir, { recursive: true });
597
+ const resp = await fetch(details.downloadUrl);
598
+ if (!resp.ok) throw new Error(`bundle download -> ${resp.status}`);
599
+ const bundle = Buffer.from(await resp.arrayBuffer());
600
+ if (details.bundleSha256 && sha256b64(bundle) !== details.bundleSha256) {
601
+ throw new Error(
602
+ `bundle integrity check failed for ${slug}@${ref}: downloaded sha256 != recorded bundleSha256`
603
+ );
604
+ }
605
+ const files = extractBundle(bundle, dir);
606
+ const branch = ref === "latest" || /^\d|^[0-9a-f]{8,}$/.test(ref) ? DEFAULT_BRANCH : ref;
607
+ writeLocalMeta(dir, {
608
+ slug,
609
+ ref,
610
+ branch,
611
+ versionId: details.versionId,
612
+ contentHash: details.contentHash,
613
+ semanticVersion: details.semanticVersion ?? void 0,
614
+ baseFiles: snapshotTreeHashes(dir)
615
+ });
616
+ return { slug, dir, versionId: details.versionId, files };
617
+ }
618
+ async function propose(dir, opts) {
619
+ const meta = readLocalMeta(dir);
620
+ if (!meta?.slug || !meta.baseFiles) {
621
+ throw new Error("This dir has no pull/push base \u2014 run pull first, then edit, then propose.");
622
+ }
623
+ const state2 = localTreeState(dir);
624
+ if (state2.clean) throw new Error("Nothing to propose \u2014 the tree matches its base.");
625
+ const changes = state2.dirty.map((relPath) => {
626
+ const abs = path.join(dir, relPath);
627
+ if (!fs.existsSync(abs)) return { path: relPath, action: "delete" };
628
+ return {
629
+ path: relPath,
630
+ action: "write",
631
+ newContent: fs.readFileSync(abs, "utf-8")
632
+ };
633
+ });
634
+ const res = await apiRequest(
635
+ opts.options ?? {},
636
+ "POST",
637
+ "/api/public/benchmark-containers/proposals",
638
+ {
639
+ slug: meta.slug,
640
+ title: opts.title,
641
+ changeReason: opts.reason,
642
+ baseVersionId: meta.versionId,
643
+ ...opts.targetBranch ? { targetBranch: opts.targetBranch } : {},
644
+ changes
645
+ }
646
+ );
647
+ return res;
648
+ }
649
+ async function createRef(slug, name, kind, versionId, options = {}) {
650
+ return apiRequest(options, "POST", "/api/public/benchmark-containers/refs", {
651
+ slug,
652
+ name,
653
+ kind,
654
+ ...versionId ? { versionId } : {}
655
+ });
656
+ }
657
+ var DEFAULT_BRANCH, META_RELPATH, SKIP_DIRS, ROOT_EXCLUDED_DIRS, sha256b64;
658
+ var init_hub = __esm({
659
+ "src/worlds/hub.ts"() {
660
+ "use strict";
661
+ init_worlds();
662
+ DEFAULT_BRANCH = "main";
663
+ META_RELPATH = path.join(".gateway", ".env-metadata.json");
664
+ SKIP_DIRS = /* @__PURE__ */ new Set([
665
+ ".git",
666
+ "__pycache__",
667
+ "node_modules",
668
+ ".venv",
669
+ ".mypy_cache",
670
+ "dist",
671
+ "build",
672
+ "outputs"
673
+ ]);
674
+ ROOT_EXCLUDED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
675
+ sha256b64 = (data) => createHash2("sha256").update(data).digest("base64");
676
+ }
677
+ });
678
+
679
+ // src/worlds/validate.ts
680
+ function fmtScore(score) {
681
+ return score === null ? "no score" : score.toFixed(3);
682
+ }
683
+ function buildMarkdown(report, minScore) {
684
+ const lines = [];
685
+ lines.push(
686
+ `### World validation ${report.ok ? "passed" : "failed"} \u2014 \`${report.slug}@${report.branch}\``
687
+ );
688
+ lines.push("");
689
+ lines.push(
690
+ `Ran the pull request's own world source (version \`${report.contentHash.slice(0, 12)}\`) through run config **${report.runConfig.name}**` + (minScore !== null ? `, gating at score \u2265 ${minScore}.` : ".")
691
+ );
692
+ lines.push("");
693
+ lines.push("| Model | Status | Score | Run |");
694
+ lines.push("| --- | --- | ---: | --- |");
695
+ for (const run of report.runs) {
696
+ const r = run.report;
697
+ const link = r?.url ? `[open](${r.url})` : "\u2014";
698
+ const status = run.failure ? `\u274C ${run.failure}` : `\u2705 ${r?.status ?? "COMPLETED"}`;
699
+ lines.push(
700
+ `| \`${run.model}\` | ${status} | ${fmtScore(r?.score ?? null)} | ${link} |`
701
+ );
702
+ }
703
+ const taskRows = report.runs.flatMap(
704
+ (run) => (run.report?.samples ?? []).map((sample) => ({
705
+ model: run.model,
706
+ task: sample.task ?? "?",
707
+ reward: sample.reward
708
+ }))
709
+ );
710
+ if (taskRows.length > 0 && taskRows.length <= 40) {
711
+ lines.push("");
712
+ lines.push("<details><summary>Per-task rewards</summary>");
713
+ lines.push("");
714
+ lines.push("| Task | Model | Reward |");
715
+ lines.push("| --- | --- | ---: |");
716
+ for (const row of taskRows) {
717
+ lines.push(
718
+ `| ${row.task} | \`${row.model}\` | ${row.reward === null ? "no score" : row.reward.toFixed(2)} |`
719
+ );
720
+ }
721
+ lines.push("");
722
+ lines.push("</details>");
723
+ }
724
+ return lines.join("\n");
725
+ }
726
+ async function validateWorld(dir, opts) {
727
+ const options = opts.options ?? {};
728
+ const progress = opts.onProgress ?? (() => {
729
+ });
730
+ const minScore = opts.minScore ?? null;
731
+ const provenance = detectProvenance();
732
+ const branch = opts.branch ?? provenance.branch;
733
+ if (!branch)
734
+ throw new Error(
735
+ "No branch to validate on: pass --branch or run inside a git checkout / GitHub Actions."
736
+ );
737
+ progress(`pushing ${dir} as ${opts.slug ?? "(pyproject name)"}@${branch}\u2026`);
738
+ const pushed = await push(dir, {
739
+ slug: opts.slug,
740
+ branch,
741
+ message: `PR validation push (${provenance.commit?.slice(0, 12) ?? "local"})`,
742
+ ...provenance.actor?.name ? { author: provenance.actor.name } : {},
743
+ options
744
+ });
745
+ progress(`pushed version ${pushed.versionId} (${pushed.contentHash.slice(0, 12)})`);
746
+ const configs = await listRunConfigs(options);
747
+ const runConfig = configs.find((c) => c.id === opts.runConfig) ?? configs.find((c) => c.name === opts.runConfig);
748
+ if (!runConfig) {
749
+ const known = configs.map((c) => c.name).join(", ") || "(none)";
750
+ throw new Error(
751
+ `Run config "${opts.runConfig}" not found in this project. Have: ${known}`
752
+ );
753
+ }
754
+ progress(`dispatching run config "${runConfig.name}" (${runConfig.models.join(", ")})\u2026`);
755
+ const dispatched = await apiRequest(options, "POST", "/api/public/benchmark-containers/run-configs/dispatch", {
756
+ runConfigId: runConfig.id,
757
+ versionId: pushed.versionId,
758
+ ...provenance
759
+ });
760
+ const runs = [];
761
+ for (const run of dispatched.runs) {
762
+ if (!run.ok) {
763
+ runs.push({
764
+ model: run.model,
765
+ runId: run.runId,
766
+ report: null,
767
+ failure: `dispatch failed: ${run.error ?? "unknown error"}`
768
+ });
769
+ continue;
770
+ }
771
+ progress(`waiting on ${run.model} (run ${run.runId})\u2026`);
772
+ let report = null;
773
+ let failure = null;
774
+ try {
775
+ report = await waitForRun(run.runId, {
776
+ timeoutMinutes: opts.timeoutMinutes ?? 45,
777
+ options,
778
+ onTick: (r) => progress(
779
+ `\u2026${run.model}: ${r.status.toLowerCase()} (${r.shardsCompleted ?? 0}/${r.parallelism} shards)`
780
+ )
781
+ });
782
+ if (report.status.toUpperCase() !== "COMPLETED") {
783
+ failure = report.status;
784
+ } else if (minScore !== null) {
785
+ if (report.score === null) failure = "completed without a score";
786
+ else if (report.score < minScore)
787
+ failure = `score ${fmtScore(report.score)} < ${minScore}`;
788
+ }
789
+ } catch (error) {
790
+ failure = error instanceof Error ? error.message : String(error);
791
+ }
792
+ runs.push({ model: run.model, runId: run.runId, report, failure });
793
+ }
794
+ const base = {
795
+ ok: runs.length > 0 && runs.every((r) => r.failure === null),
796
+ slug: pushed.slug,
797
+ branch,
798
+ versionId: pushed.versionId,
799
+ contentHash: pushed.contentHash,
800
+ runConfig: { id: runConfig.id, name: runConfig.name },
801
+ runs
802
+ };
803
+ return { ...base, markdown: buildMarkdown(base, minScore) };
804
+ }
805
+ var init_validate = __esm({
806
+ "src/worlds/validate.ts"() {
807
+ "use strict";
808
+ init_worlds();
809
+ init_hub();
810
+ }
811
+ });
812
+
813
+ // src/worlds/index.ts
814
+ var worlds_exports = {};
815
+ __export(worlds_exports, {
816
+ DEFAULT_BRANCH: () => DEFAULT_BRANCH,
817
+ WorldSession: () => WorldSession,
818
+ WorldSessionCallError: () => WorldSessionCallError,
819
+ WorldSessionTimeout: () => WorldSessionTimeout,
820
+ WorldSet: () => WorldSet,
821
+ WorldTask: () => WorldTask,
822
+ apiRequest: () => apiRequest,
823
+ attachSession: () => attachSession,
824
+ buildSourceTarball: () => buildSourceTarball,
825
+ collectArchiveRelPaths: () => collectArchiveRelPaths,
826
+ computeContentHash: () => computeContentHash,
827
+ createRef: () => createRef,
828
+ deleteEnvSecret: () => deleteEnvSecret,
829
+ detectProvenance: () => detectProvenance,
830
+ getRun: () => getRun,
831
+ isIncludedInContentHash: () => isIncludedInContentHash,
832
+ listEnvSecretKeys: () => listEnvSecretKeys,
833
+ listRunConfigs: () => listRunConfigs,
834
+ localTreeState: () => localTreeState,
835
+ openSession: () => openSession,
836
+ openWorld: () => openWorld,
837
+ propose: () => propose,
838
+ pull: () => pull,
839
+ push: () => push,
840
+ readLocalMeta: () => readLocalMeta,
841
+ runSessions: () => runSessions,
842
+ schemaFromSignature: () => schemaFromSignature,
843
+ setEnvSecret: () => setEnvSecret,
844
+ snapshotTreeHashes: () => snapshotTreeHashes,
845
+ validateWorld: () => validateWorld,
846
+ waitForRun: () => waitForRun,
847
+ writeLocalMeta: () => writeLocalMeta
848
+ });
849
+ function resolveEnv(options) {
850
+ const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
851
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
852
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
853
+ if (!publicKey || !secretKey) {
854
+ throw new Error(
855
+ "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
856
+ );
857
+ }
858
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
859
+ }
860
+ async function apiRequest(options, method, path2, body) {
861
+ const { host, publicKey, secretKey } = resolveEnv(options);
862
+ const res = await fetch(`${host}${path2}`, {
863
+ method,
864
+ headers: {
865
+ "content-type": "application/json",
866
+ authorization: "Basic " + Buffer.from(`${publicKey}:${secretKey}`).toString("base64")
867
+ },
868
+ ...body === void 0 ? {} : { body: JSON.stringify(body) }
869
+ });
870
+ const text = await res.text();
871
+ if (!res.ok) {
872
+ throw new Error(
873
+ `${method} ${path2} -> ${res.status}: ${text.slice(0, 400)}`
874
+ );
875
+ }
876
+ return JSON.parse(text);
877
+ }
878
+ async function setEnvSecret(slug, key, value, options = {}) {
879
+ return apiRequest(
880
+ options,
881
+ "PUT",
882
+ "/api/public/benchmark-containers/secrets",
883
+ {
884
+ slug,
885
+ key,
886
+ value
887
+ }
888
+ );
889
+ }
890
+ async function listEnvSecretKeys(slug, options = {}) {
891
+ const res = await apiRequest(
892
+ options,
893
+ "GET",
894
+ `/api/public/benchmark-containers/secrets?slug=${encodeURIComponent(slug)}`
895
+ );
896
+ return res.secrets;
897
+ }
898
+ async function deleteEnvSecret(slug, key, options = {}) {
899
+ await apiRequest(
900
+ options,
901
+ "DELETE",
902
+ "/api/public/benchmark-containers/secrets",
903
+ {
904
+ slug,
905
+ key
906
+ }
907
+ );
908
+ }
909
+ async function listRunConfigs(options = {}) {
910
+ const res = await apiRequest(
911
+ options,
912
+ "GET",
913
+ "/api/public/benchmark-containers/run-configs"
914
+ );
915
+ return res.runConfigs;
916
+ }
917
+ async function getRun(runId, options = {}) {
918
+ return apiRequest(
919
+ options,
920
+ "GET",
921
+ `/api/public/benchmark-containers/runs/${encodeURIComponent(runId)}`
922
+ );
923
+ }
924
+ async function waitForRun(runId, opts = {}) {
925
+ const deadline = Date.now() + (opts.timeoutMinutes ?? 45) * 6e4;
926
+ for (; ; ) {
927
+ const run = await getRun(runId, opts.options ?? {});
928
+ if (run.terminal) return run;
929
+ if (Date.now() > deadline)
930
+ throw new Error(
931
+ `Run ${runId} still ${run.status} after the wait timeout.`
932
+ );
933
+ opts.onTick?.(run);
934
+ await new Promise(
935
+ (resolve2) => setTimeout(resolve2, (opts.intervalSeconds ?? 20) * 1e3)
936
+ );
937
+ }
938
+ }
939
+ function detectProvenance() {
940
+ const env = globalThis.process?.env ?? {};
941
+ const out = {};
942
+ const ref = env.GITHUB_HEAD_REF || env.GITHUB_REF_NAME;
943
+ if (ref) out.branch = ref;
944
+ if (env.GITHUB_SHA) out.commit = env.GITHUB_SHA;
945
+ const server = env.GITHUB_SERVER_URL ?? "https://github.com";
946
+ if (env.GITHUB_REPOSITORY) out.repoUrl = `${server}/${env.GITHUB_REPOSITORY}`;
947
+ if (env.GITHUB_ACTOR) {
948
+ out.actor = {
949
+ kind: "ci",
950
+ id: env.GITHUB_ACTOR,
951
+ name: env.GITHUB_ACTOR,
952
+ // GitHub serves every account's avatar at /<login>.png.
953
+ imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
954
+ };
955
+ }
956
+ if (!out.branch || !out.commit) {
957
+ try {
958
+ const { execFileSync } = __require("child_process");
959
+ const git = (args) => execFileSync("git", args, {
960
+ encoding: "utf-8",
961
+ stdio: ["ignore", "pipe", "ignore"]
962
+ }).toString().trim();
963
+ out.branch = out.branch ?? git(["rev-parse", "--abbrev-ref", "HEAD"]);
964
+ out.commit = out.commit ?? git(["rev-parse", "HEAD"]);
965
+ if (!out.repoUrl) {
966
+ const remote = git(["config", "--get", "remote.origin.url"]);
967
+ if (remote) out.repoUrl = remote.replace(/\.git$/, "");
968
+ }
969
+ if (!out.actor) {
970
+ const name = git(["config", "--get", "user.name"]);
971
+ if (name) out.actor = { kind: "user", name };
972
+ }
973
+ } catch {
974
+ }
975
+ }
976
+ return out;
977
+ }
978
+ async function openWorld(slugRef, options = {}) {
979
+ const at = slugRef.indexOf("@");
980
+ const slug = at === -1 ? slugRef : slugRef.slice(0, at);
981
+ const ref = at === -1 ? null : slugRef.slice(at + 1);
982
+ const version = await apiRequest(options, "POST", "/api/public/benchmark-containers/resolve-version", {
983
+ slug,
984
+ ...ref ? { ref } : {}
985
+ });
986
+ const world = new WorldSet(
987
+ slugRef,
988
+ slug,
989
+ ref,
990
+ {
991
+ versionId: version.versionId,
992
+ semanticVersion: version.semanticVersion ?? null,
993
+ contentHash: version.contentHash,
994
+ state: version.state
995
+ },
996
+ [],
997
+ options
998
+ );
999
+ for (const name of version.taskNames ?? []) {
1000
+ const variant = /^[a-z0-9]+-([a-z0-9]+)-/.exec(name)?.[1] ?? null;
1001
+ world.tasks.push(new WorldTask(world, name, variant));
1002
+ }
1003
+ return world;
1004
+ }
1005
+ var WorldTask, WorldSet;
1006
+ var init_worlds = __esm({
1007
+ "src/worlds/index.ts"() {
1008
+ "use strict";
1009
+ init_session();
1010
+ init_hub();
1011
+ init_validate();
1012
+ WorldTask = class {
1013
+ constructor(world, name, variant) {
1014
+ this.world = world;
1015
+ this.name = name;
1016
+ this.variant = variant;
1017
+ }
1018
+ };
1019
+ WorldSet = class {
1020
+ constructor(slugRef, slug, ref, version, tasks, options) {
1021
+ this.slugRef = slugRef;
1022
+ this.slug = slug;
1023
+ this.ref = ref;
1024
+ this.version = version;
1025
+ this.tasks = tasks;
1026
+ this.options = options;
1027
+ }
1028
+ task(variant) {
1029
+ const hit = this.tasks.find(
1030
+ (t) => t.variant === variant || t.name.includes(variant)
1031
+ );
1032
+ if (!hit) {
1033
+ const known = this.tasks.map((t) => t.variant ?? t.name).join(", ");
1034
+ throw new Error(`no task matches ${variant}; have: ${known}`);
1035
+ }
1036
+ return hit;
1037
+ }
1038
+ /**
1039
+ * Open a LIVE session against one of this world's tasks: the platform boots
1040
+ * (or reuses) a warm container, and your agent calls its tools through the
1041
+ * platform. See worlds/session.ts.
1042
+ */
1043
+ async open(task, opts = {}) {
1044
+ const { openSession: openSession2 } = await Promise.resolve().then(() => (init_session(), session_exports));
1045
+ return openSession2(this.slug, task, {
1046
+ versionId: this.version.versionId,
1047
+ reuseWarm: opts.reuseWarm,
1048
+ options: this.options
1049
+ });
1050
+ }
1051
+ /**
1052
+ * Start a HOSTED run of a run config against this world — the platform
1053
+ * executes the (Python) world in parallel containers per the config's
1054
+ * `parallelism` and files one graded run per model. Returns those runs.
1055
+ */
1056
+ async dispatch(runConfigId, opts = {}) {
1057
+ const provenance = opts.provenance ?? detectProvenance();
1058
+ return apiRequest(
1059
+ this.options,
1060
+ "POST",
1061
+ "/api/public/benchmark-containers/run-configs/dispatch",
1062
+ {
1063
+ runConfigId,
1064
+ ...opts.containerId ? { containerId: opts.containerId } : {},
1065
+ versionId: opts.versionId ?? this.version.versionId,
1066
+ ...provenance
1067
+ }
1068
+ );
1069
+ }
1070
+ };
1071
+ }
1072
+ });
1073
+
19
1074
  // package.json
20
1075
  var require_package = __commonJS({
21
1076
  "package.json"(exports, module) {
22
1077
  module.exports = {
23
1078
  name: "@withgateway/sdk",
24
- version: "0.1.13",
1079
+ version: "0.1.16",
25
1080
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
26
1081
  main: "./dist/index.js",
27
1082
  module: "./dist/index.mjs",
@@ -81,6 +1136,11 @@ var require_package = __commonJS({
81
1136
  types: "./dist/scores/index.d.ts",
82
1137
  import: "./dist/scores/index.mjs",
83
1138
  require: "./dist/scores/index.js"
1139
+ },
1140
+ "./worlds": {
1141
+ types: "./dist/worlds/index.d.ts",
1142
+ import: "./dist/worlds/index.mjs",
1143
+ require: "./dist/worlds/index.js"
84
1144
  }
85
1145
  },
86
1146
  files: [
@@ -94,6 +1154,7 @@ var require_package = __commonJS({
94
1154
  build: "tsup",
95
1155
  dev: "tsup --watch",
96
1156
  typecheck: "tsc --noEmit",
1157
+ "typecheck:examples": "tsc -p tsconfig.examples.json --noEmit",
97
1158
  test: "vitest",
98
1159
  "test:run": "vitest run",
99
1160
  lint: "eslint src --ext .ts",
@@ -154,10 +1215,29 @@ var require_package = __commonJS({
154
1215
  "@opentelemetry/sdk-trace-node": "^2.6.0",
155
1216
  "@opentelemetry/semantic-conventions": "^1.40.0",
156
1217
  "@types/node": "^20.0.0",
1218
+ openai: "^4.70.0",
157
1219
  tsup: "^8.0.0",
158
1220
  tsx: "^4.0.0",
159
1221
  typescript: "^5.0.0",
160
- vitest: "^2.0.0"
1222
+ vitest: "^3.2.6"
1223
+ },
1224
+ overrides: {
1225
+ protobufjs: "^8.4.1",
1226
+ "vite@6": "^6.4.3",
1227
+ postcss: "^8.5.18",
1228
+ "picomatch@4": "^4.0.4"
1229
+ },
1230
+ pnpm: {
1231
+ overrides: {
1232
+ protobufjs: "^8.4.1",
1233
+ vitest: "^3.2.6",
1234
+ "vite@6": "^6.4.3",
1235
+ postcss: "^8.5.18",
1236
+ "picomatch@4": "^4.0.4"
1237
+ }
1238
+ },
1239
+ bin: {
1240
+ gateway: "./dist/cli.js"
161
1241
  }
162
1242
  };
163
1243
  }
@@ -733,8 +1813,8 @@ var GatewayContext = class _GatewayContext {
733
1813
  }
734
1814
  return resolved;
735
1815
  }
736
- async requestJson(method, path, payload) {
737
- const response = await fetch(`${this.host}${path}`, {
1816
+ async requestJson(method, path2, payload) {
1817
+ const response = await fetch(`${this.host}${path2}`, {
738
1818
  method,
739
1819
  headers: {
740
1820
  Authorization: this.authHeader(),
@@ -745,7 +1825,7 @@ var GatewayContext = class _GatewayContext {
745
1825
  const text = await response.text();
746
1826
  const body = text ? safeJson(text) : {};
747
1827
  if (!response.ok) {
748
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path}`;
1828
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path2}`;
749
1829
  if (response.status === 409) {
750
1830
  throw new GatewayContextConflictError(
751
1831
  message,
@@ -754,7 +1834,7 @@ var GatewayContext = class _GatewayContext {
754
1834
  }
755
1835
  throw new GatewayContextError(message);
756
1836
  }
757
- this.log("%s %s -> %s", method, path, response.status);
1837
+ this.log("%s %s -> %s", method, path2, response.status);
758
1838
  return body;
759
1839
  }
760
1840
  authHeader() {
@@ -861,11 +1941,11 @@ function primaryPathForKind(kind, snapshot) {
861
1941
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
862
1942
  if (readFile(snapshot, preferred)) return preferred;
863
1943
  return Object.entries(snapshot.files).find(
864
- ([path, entry]) => entry.type === "file" && path.endsWith(".md")
1944
+ ([path2, entry]) => entry.type === "file" && path2.endsWith(".md")
865
1945
  )?.[0] ?? null;
866
1946
  }
867
- function parseJsonFile(snapshot, path) {
868
- const content = readFile(snapshot, path);
1947
+ function parseJsonFile(snapshot, path2) {
1948
+ const content = readFile(snapshot, path2);
869
1949
  if (!content) return {};
870
1950
  try {
871
1951
  const parsed = JSON.parse(content);
@@ -874,8 +1954,8 @@ function parseJsonFile(snapshot, path) {
874
1954
  return {};
875
1955
  }
876
1956
  }
877
- function readFile(snapshot, path) {
878
- const entry = snapshot.files[path];
1957
+ function readFile(snapshot, path2) {
1958
+ const entry = snapshot.files[path2];
879
1959
  return entry?.type === "file" ? entry.content : null;
880
1960
  }
881
1961
  function provenanceFor(snapshot, label) {
@@ -977,16 +2057,16 @@ var GatewayMemory = class _GatewayMemory {
977
2057
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
978
2058
  );
979
2059
  }
980
- async read(target, path, options = {}) {
2060
+ async read(target, path2, options = {}) {
981
2061
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
982
- const file = snapshot.files[path];
2062
+ const file = snapshot.files[path2];
983
2063
  return file?.type === "file" ? file.content : null;
984
2064
  }
985
- async write(target, path, content, options = {}) {
2065
+ async write(target, path2, content, options = {}) {
986
2066
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
987
2067
  return this.push(snapshot.repo.repoHandle, {
988
2068
  files: {
989
- [path]: {
2069
+ [path2]: {
990
2070
  content,
991
2071
  ...options.mimeType ? { mimeType: options.mimeType } : {}
992
2072
  }
@@ -996,10 +2076,10 @@ var GatewayMemory = class _GatewayMemory {
996
2076
  ...runtimeContextPayload2(options)
997
2077
  });
998
2078
  }
999
- async delete(target, path, options = {}) {
2079
+ async delete(target, path2, options = {}) {
1000
2080
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
1001
2081
  return this.push(snapshot.repo.repoHandle, {
1002
- files: { [path]: null },
2082
+ files: { [path2]: null },
1003
2083
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
1004
2084
  message: options.message,
1005
2085
  ...runtimeContextPayload2(options)
@@ -1012,8 +2092,8 @@ var GatewayMemory = class _GatewayMemory {
1012
2092
  payload
1013
2093
  );
1014
2094
  }
1015
- async requestJson(method, path, payload) {
1016
- const response = await fetch(`${this.host}${path}`, {
2095
+ async requestJson(method, path2, payload) {
2096
+ const response = await fetch(`${this.host}${path2}`, {
1017
2097
  method,
1018
2098
  headers: {
1019
2099
  Authorization: this.authHeader(),
@@ -1024,7 +2104,7 @@ var GatewayMemory = class _GatewayMemory {
1024
2104
  const text = await response.text();
1025
2105
  const body = text ? safeJson2(text) : {};
1026
2106
  if (!response.ok) {
1027
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path}`;
2107
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path2}`;
1028
2108
  if (response.status === 409) {
1029
2109
  throw new GatewayMemoryConflictError(
1030
2110
  message,
@@ -1033,7 +2113,7 @@ var GatewayMemory = class _GatewayMemory {
1033
2113
  }
1034
2114
  throw new GatewayMemoryError(message);
1035
2115
  }
1036
- this.log("%s %s -> %s", method, path, response.status);
2116
+ this.log("%s %s -> %s", method, path2, response.status);
1037
2117
  return body;
1038
2118
  }
1039
2119
  authHeader() {
@@ -1195,10 +2275,10 @@ var PersonasClient = class _PersonasClient {
1195
2275
  done: Boolean(payload.done)
1196
2276
  };
1197
2277
  }
1198
- async requestJson(method, path, payload) {
2278
+ async requestJson(method, path2, payload) {
1199
2279
  let response;
1200
2280
  try {
1201
- response = await fetch(`${this.host}${path}`, {
2281
+ response = await fetch(`${this.host}${path2}`, {
1202
2282
  method,
1203
2283
  headers: {
1204
2284
  Authorization: this.authHeader(),
@@ -1209,7 +2289,7 @@ var PersonasClient = class _PersonasClient {
1209
2289
  });
1210
2290
  } catch (error) {
1211
2291
  const reason = error instanceof Error ? error.message : String(error);
1212
- throw new PersonaError(`${method} ${path} failed: ${reason}`, {
2292
+ throw new PersonaError(`${method} ${path2} failed: ${reason}`, {
1213
2293
  cause: error
1214
2294
  });
1215
2295
  }
@@ -1217,10 +2297,10 @@ var PersonasClient = class _PersonasClient {
1217
2297
  if (!response.ok) {
1218
2298
  const detail = text.slice(0, 500);
1219
2299
  throw new PersonaError(
1220
- `${method} ${path} failed (${response.status}): ${detail}`
2300
+ `${method} ${path2} failed (${response.status}): ${detail}`
1221
2301
  );
1222
2302
  }
1223
- this.log("%s %s -> %s", method, path, response.status);
2303
+ this.log("%s %s -> %s", method, path2, response.status);
1224
2304
  return safeJson3(text);
1225
2305
  }
1226
2306
  authHeader() {
@@ -1445,10 +2525,10 @@ var SessionRecording = class _SessionRecording {
1445
2525
  };
1446
2526
  }
1447
2527
  /** Dump the full session as a JSON fixture (for regression tests). */
1448
- toFixture(path) {
1449
- mkdirSync(dirname(path), { recursive: true });
1450
- writeFileSync(path, JSON.stringify(this.toJSON(), null, 2), "utf-8");
1451
- return path;
2528
+ toFixture(path2) {
2529
+ mkdirSync(dirname(path2), { recursive: true });
2530
+ writeFileSync(path2, JSON.stringify(this.toJSON(), null, 2), "utf-8");
2531
+ return path2;
1452
2532
  }
1453
2533
  static fromApi(raw) {
1454
2534
  return new _SessionRecording({
@@ -2283,7 +3363,7 @@ function randomHex() {
2283
3363
  return crypto.randomUUID().replace(/-/g, "");
2284
3364
  }
2285
3365
  function sleep(ms) {
2286
- return new Promise((resolve) => setTimeout(resolve, ms));
3366
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
2287
3367
  }
2288
3368
  var replay = {
2289
3369
  suite,
@@ -2370,7 +3450,7 @@ var GatewaySecurity = class _GatewaySecurity {
2370
3450
  const descriptors = entries.map(
2371
3451
  ([key, tool]) => this.describeTool(key, tool, options)
2372
3452
  );
2373
- const resolve = await this.resolveToolset({
3453
+ const resolve2 = await this.resolveToolset({
2374
3454
  agent: this.agent,
2375
3455
  toolsetId: options.toolsetId,
2376
3456
  toolsetName: options.toolsetName,
@@ -2380,8 +3460,8 @@ var GatewaySecurity = class _GatewaySecurity {
2380
3460
  });
2381
3461
  const securedEntries = entries.flatMap(([key, tool]) => {
2382
3462
  const descriptor = descriptors.find((item) => item.name === key);
2383
- if (!this.shouldExposeTool(resolve, descriptor.id)) {
2384
- this.log("Hiding tool %s in %s mode", descriptor.id, resolve.mode);
3463
+ if (!this.shouldExposeTool(resolve2, descriptor.id)) {
3464
+ this.log("Hiding tool %s in %s mode", descriptor.id, resolve2.mode);
2385
3465
  return [];
2386
3466
  }
2387
3467
  return [
@@ -2391,7 +3471,7 @@ var GatewaySecurity = class _GatewaySecurity {
2391
3471
  toolId: descriptor.id,
2392
3472
  toolsetId: options.toolsetId,
2393
3473
  context: this.normalizeContext(options.context),
2394
- resolve
3474
+ resolve: resolve2
2395
3475
  })
2396
3476
  ]
2397
3477
  ];
@@ -2479,8 +3559,8 @@ var GatewaySecurity = class _GatewaySecurity {
2479
3559
  );
2480
3560
  }
2481
3561
  }
2482
- async postJson(path, payload) {
2483
- const response = await fetch(`${this.host}${path}`, {
3562
+ async postJson(path2, payload) {
3563
+ const response = await fetch(`${this.host}${path2}`, {
2484
3564
  method: "POST",
2485
3565
  headers: {
2486
3566
  Authorization: this.authHeader(),
@@ -2491,7 +3571,7 @@ var GatewaySecurity = class _GatewaySecurity {
2491
3571
  if (!response.ok) {
2492
3572
  const body = await response.text();
2493
3573
  throw new GatewaySecurityError(
2494
- `Gateway API ${response.status} on ${path}: ${body || response.statusText}`
3574
+ `Gateway API ${response.status} on ${path2}: ${body || response.statusText}`
2495
3575
  );
2496
3576
  }
2497
3577
  return await response.json();
@@ -2506,9 +3586,9 @@ var GatewaySecurity = class _GatewaySecurity {
2506
3586
  metadata: options.toolMetadata?.[key] ?? {}
2507
3587
  };
2508
3588
  }
2509
- shouldExposeTool(resolve, toolId) {
2510
- if (resolve.mode === "observe") return true;
2511
- if (resolve.blockedToolIds.includes(toolId)) return false;
3589
+ shouldExposeTool(resolve2, toolId) {
3590
+ if (resolve2.mode === "observe") return true;
3591
+ if (resolve2.blockedToolIds.includes(toolId)) return false;
2512
3592
  return true;
2513
3593
  }
2514
3594
  normalizeContext(context4) {
@@ -2672,10 +3752,10 @@ var UsersClient = class _UsersClient {
2672
3752
  totalItems: Number(meta.totalItems ?? rows.length)
2673
3753
  };
2674
3754
  }
2675
- async requestJson(method, path, payload) {
3755
+ async requestJson(method, path2, payload) {
2676
3756
  let response;
2677
3757
  try {
2678
- response = await fetch(`${this.host}${path}`, {
3758
+ response = await fetch(`${this.host}${path2}`, {
2679
3759
  method,
2680
3760
  headers: {
2681
3761
  Authorization: this.authHeader(),
@@ -2686,17 +3766,17 @@ var UsersClient = class _UsersClient {
2686
3766
  });
2687
3767
  } catch (error) {
2688
3768
  throw new UserProfileError(
2689
- `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
3769
+ `${method} ${path2} failed: ${error instanceof Error ? error.message : String(error)}`,
2690
3770
  { cause: error }
2691
3771
  );
2692
3772
  }
2693
3773
  const text = await response.text();
2694
3774
  if (!response.ok) {
2695
3775
  throw new UserProfileError(
2696
- `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
3776
+ `${method} ${path2} failed (${response.status}): ${text.slice(0, 500)}`
2697
3777
  );
2698
3778
  }
2699
- this.log("%s %s -> %s", method, path, response.status);
3779
+ this.log("%s %s -> %s", method, path2, response.status);
2700
3780
  return safeJson4(text);
2701
3781
  }
2702
3782
  authHeader() {
@@ -2900,10 +3980,10 @@ var ScoresClient = class _ScoresClient {
2900
3980
  metadata: signalMetadata
2901
3981
  });
2902
3982
  }
2903
- async request(method, path, payload) {
3983
+ async request(method, path2, payload) {
2904
3984
  let response;
2905
3985
  try {
2906
- response = await fetch(`${this.host}${path}`, {
3986
+ response = await fetch(`${this.host}${path2}`, {
2907
3987
  method,
2908
3988
  headers: {
2909
3989
  Authorization: this.authHeader(),
@@ -2914,17 +3994,17 @@ var ScoresClient = class _ScoresClient {
2914
3994
  });
2915
3995
  } catch (error) {
2916
3996
  throw new ScoreError(
2917
- `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
3997
+ `${method} ${path2} failed: ${error instanceof Error ? error.message : String(error)}`,
2918
3998
  { cause: error }
2919
3999
  );
2920
4000
  }
2921
4001
  const text = await response.text();
2922
4002
  if (!response.ok) {
2923
4003
  throw new ScoreError(
2924
- `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
4004
+ `${method} ${path2} failed (${response.status}): ${text.slice(0, 500)}`
2925
4005
  );
2926
4006
  }
2927
- this.log("%s %s -> %s", method, path, response.status);
4007
+ this.log("%s %s -> %s", method, path2, response.status);
2928
4008
  }
2929
4009
  authHeader() {
2930
4010
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -2961,8 +4041,12 @@ var scores = {
2961
4041
  success
2962
4042
  };
2963
4043
 
4044
+ // src/index.ts
4045
+ init_worlds();
4046
+ init_session();
4047
+
2964
4048
  // src/build/index.ts
2965
- import { createHash as createHash2 } from "crypto";
4049
+ import { createHash as createHash3 } from "crypto";
2966
4050
  function canonicalJson(value) {
2967
4051
  if (value === null || typeof value !== "object") {
2968
4052
  return JSON.stringify(value);
@@ -2974,7 +4058,7 @@ function canonicalJson(value) {
2974
4058
  return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
2975
4059
  }
2976
4060
  function sha256(text) {
2977
- return `sha256:${createHash2("sha256").update(text).digest("hex")}`;
4061
+ return `sha256:${createHash3("sha256").update(text).digest("hex")}`;
2978
4062
  }
2979
4063
  var registeredTools = /* @__PURE__ */ new Map();
2980
4064
  var registeredPrompts = /* @__PURE__ */ new Map();
@@ -3846,6 +4930,7 @@ var tracing = {
3846
4930
  };
3847
4931
  export {
3848
4932
  AGENT_ID_KEY,
4933
+ DEFAULT_BRANCH,
3849
4934
  DEFAULT_TIMEOUT_SECONDS,
3850
4935
  DriftError,
3851
4936
  ENV_GATEWAY_HOST,
@@ -3887,17 +4972,30 @@ export {
3887
4972
  TraceDiff,
3888
4973
  UserProfileError,
3889
4974
  UsersClient,
4975
+ WorldSession,
4976
+ WorldSessionCallError,
4977
+ WorldSessionTimeout,
4978
+ WorldSet,
4979
+ WorldTask,
3890
4980
  addExperimentAttributes,
4981
+ apiRequest,
3891
4982
  assembleManifest,
3892
4983
  assignVariant,
4984
+ attachSession,
3893
4985
  basicAuthHeader,
4986
+ buildSourceTarball,
3894
4987
  canonicalJson,
3895
4988
  clearExperimentContext,
4989
+ collectArchiveRelPaths,
3896
4990
  computeBuildHash,
4991
+ computeContentHash,
3897
4992
  computeHash,
3898
4993
  context,
4994
+ createRef,
3899
4995
  currentTraceId,
3900
4996
  defineTool,
4997
+ deleteEnvSecret,
4998
+ detectProvenance,
3901
4999
  experimentContextStorage,
3902
5000
  singleton_exports as experiments,
3903
5001
  gateway,
@@ -3906,10 +5004,21 @@ export {
3906
5004
  getRegisteredPrompts,
3907
5005
  getRegisteredSkills,
3908
5006
  getRegisteredTools,
5007
+ getRun,
3909
5008
  identify,
5009
+ isIncludedInContentHash,
5010
+ listEnvSecretKeys,
5011
+ listRunConfigs,
3910
5012
  loadSession,
5013
+ localTreeState,
3911
5014
  memory,
5015
+ openSession,
5016
+ openWorld,
3912
5017
  personas,
5018
+ propose,
5019
+ pull,
5020
+ push,
5021
+ readLocalMeta,
3913
5022
  registerArtifact,
3914
5023
  registerPrompt,
3915
5024
  registerSkill,
@@ -3917,15 +5026,22 @@ export {
3917
5026
  replay,
3918
5027
  resetBuildRegistry,
3919
5028
  resolveUserId,
5029
+ runSessions,
3920
5030
  runSuite,
3921
5031
  runWithExperimentContext,
5032
+ schemaFromSignature,
3922
5033
  scores,
3923
5034
  sessionObservationFromApi,
3924
5035
  sessions,
5036
+ setEnvSecret,
3925
5037
  setExperimentContext,
5038
+ snapshotTreeHashes,
3926
5039
  success,
3927
5040
  suite,
3928
5041
  tracing,
3929
- users
5042
+ users,
5043
+ validateWorld,
5044
+ waitForRun,
5045
+ writeLocalMeta
3930
5046
  };
3931
5047
  //# sourceMappingURL=index.mjs.map