@withgateway/sdk 0.1.14 → 0.1.17

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
@@ -314,9 +314,524 @@ var init_session = __esm({
314
314
  }
315
315
  });
316
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 host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
761
+ let projectId = null;
762
+ try {
763
+ const projects = await apiRequest(
764
+ options,
765
+ "GET",
766
+ "/api/public/projects"
767
+ );
768
+ projectId = projects.data?.[0]?.id ?? null;
769
+ } catch {
770
+ projectId = null;
771
+ }
772
+ const fallbackUrl = (report) => {
773
+ if (report.url) return report.url;
774
+ const evaluationId = report.evaluations[0]?.id;
775
+ return projectId && evaluationId ? `${host}/project/${projectId}/environment/runs/${encodeURIComponent(evaluationId)}` : null;
776
+ };
777
+ const runs = [];
778
+ for (const run of dispatched.runs) {
779
+ if (!run.ok) {
780
+ runs.push({
781
+ model: run.model,
782
+ runId: run.runId,
783
+ report: null,
784
+ failure: `dispatch failed: ${run.error ?? "unknown error"}`
785
+ });
786
+ continue;
787
+ }
788
+ progress(`waiting on ${run.model} (run ${run.runId})\u2026`);
789
+ let report = null;
790
+ let failure = null;
791
+ try {
792
+ report = await waitForRun(run.runId, {
793
+ timeoutMinutes: opts.timeoutMinutes ?? 45,
794
+ options,
795
+ onTick: (r) => progress(
796
+ `\u2026${run.model}: ${r.status.toLowerCase()} (${r.shardsCompleted ?? 0}/${r.parallelism} shards)`
797
+ )
798
+ });
799
+ report = { ...report, url: fallbackUrl(report) };
800
+ if (report.status.toUpperCase() !== "COMPLETED") {
801
+ failure = report.status;
802
+ } else if (minScore !== null) {
803
+ if (report.score === null) failure = "completed without a score";
804
+ else if (report.score < minScore)
805
+ failure = `score ${fmtScore(report.score)} < ${minScore}`;
806
+ }
807
+ } catch (error) {
808
+ failure = error instanceof Error ? error.message : String(error);
809
+ }
810
+ runs.push({ model: run.model, runId: run.runId, report, failure });
811
+ }
812
+ const base = {
813
+ ok: runs.length > 0 && runs.every((r) => r.failure === null),
814
+ slug: pushed.slug,
815
+ branch,
816
+ versionId: pushed.versionId,
817
+ contentHash: pushed.contentHash,
818
+ runConfig: { id: runConfig.id, name: runConfig.name },
819
+ runs
820
+ };
821
+ return { ...base, markdown: buildMarkdown(base, minScore) };
822
+ }
823
+ var init_validate = __esm({
824
+ "src/worlds/validate.ts"() {
825
+ "use strict";
826
+ init_worlds();
827
+ init_hub();
828
+ }
829
+ });
830
+
317
831
  // src/worlds/index.ts
318
832
  var worlds_exports = {};
319
833
  __export(worlds_exports, {
834
+ DEFAULT_BRANCH: () => DEFAULT_BRANCH,
320
835
  WorldSession: () => WorldSession,
321
836
  WorldSessionCallError: () => WorldSessionCallError,
322
837
  WorldSessionTimeout: () => WorldSessionTimeout,
@@ -324,17 +839,30 @@ __export(worlds_exports, {
324
839
  WorldTask: () => WorldTask,
325
840
  apiRequest: () => apiRequest,
326
841
  attachSession: () => attachSession,
842
+ buildSourceTarball: () => buildSourceTarball,
843
+ collectArchiveRelPaths: () => collectArchiveRelPaths,
844
+ computeContentHash: () => computeContentHash,
845
+ createRef: () => createRef,
327
846
  deleteEnvSecret: () => deleteEnvSecret,
328
847
  detectProvenance: () => detectProvenance,
329
848
  getRun: () => getRun,
849
+ isIncludedInContentHash: () => isIncludedInContentHash,
330
850
  listEnvSecretKeys: () => listEnvSecretKeys,
331
851
  listRunConfigs: () => listRunConfigs,
852
+ localTreeState: () => localTreeState,
332
853
  openSession: () => openSession,
333
854
  openWorld: () => openWorld,
855
+ propose: () => propose,
856
+ pull: () => pull,
857
+ push: () => push,
858
+ readLocalMeta: () => readLocalMeta,
334
859
  runSessions: () => runSessions,
335
860
  schemaFromSignature: () => schemaFromSignature,
336
861
  setEnvSecret: () => setEnvSecret,
337
- waitForRun: () => waitForRun
862
+ snapshotTreeHashes: () => snapshotTreeHashes,
863
+ validateWorld: () => validateWorld,
864
+ waitForRun: () => waitForRun,
865
+ writeLocalMeta: () => writeLocalMeta
338
866
  });
339
867
  function resolveEnv(options) {
340
868
  const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
@@ -347,9 +875,9 @@ function resolveEnv(options) {
347
875
  }
348
876
  return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
349
877
  }
350
- async function apiRequest(options, method, path, body) {
878
+ async function apiRequest(options, method, path2, body) {
351
879
  const { host, publicKey, secretKey } = resolveEnv(options);
352
- const res = await fetch(`${host}${path}`, {
880
+ const res = await fetch(`${host}${path2}`, {
353
881
  method,
354
882
  headers: {
355
883
  "content-type": "application/json",
@@ -360,7 +888,7 @@ async function apiRequest(options, method, path, body) {
360
888
  const text = await res.text();
361
889
  if (!res.ok) {
362
890
  throw new Error(
363
- `${method} ${path} -> ${res.status}: ${text.slice(0, 400)}`
891
+ `${method} ${path2} -> ${res.status}: ${text.slice(0, 400)}`
364
892
  );
365
893
  }
366
894
  return JSON.parse(text);
@@ -422,7 +950,7 @@ async function waitForRun(runId, opts = {}) {
422
950
  );
423
951
  opts.onTick?.(run);
424
952
  await new Promise(
425
- (resolve) => setTimeout(resolve, (opts.intervalSeconds ?? 20) * 1e3)
953
+ (resolve2) => setTimeout(resolve2, (opts.intervalSeconds ?? 20) * 1e3)
426
954
  );
427
955
  }
428
956
  }
@@ -497,6 +1025,8 @@ var init_worlds = __esm({
497
1025
  "src/worlds/index.ts"() {
498
1026
  "use strict";
499
1027
  init_session();
1028
+ init_hub();
1029
+ init_validate();
500
1030
  WorldTask = class {
501
1031
  constructor(world, name, variant) {
502
1032
  this.world = world;
@@ -564,7 +1094,7 @@ var require_package = __commonJS({
564
1094
  "package.json"(exports, module) {
565
1095
  module.exports = {
566
1096
  name: "@withgateway/sdk",
567
- version: "0.1.14",
1097
+ version: "0.1.17",
568
1098
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
569
1099
  main: "./dist/index.js",
570
1100
  module: "./dist/index.mjs",
@@ -1301,8 +1831,8 @@ var GatewayContext = class _GatewayContext {
1301
1831
  }
1302
1832
  return resolved;
1303
1833
  }
1304
- async requestJson(method, path, payload) {
1305
- const response = await fetch(`${this.host}${path}`, {
1834
+ async requestJson(method, path2, payload) {
1835
+ const response = await fetch(`${this.host}${path2}`, {
1306
1836
  method,
1307
1837
  headers: {
1308
1838
  Authorization: this.authHeader(),
@@ -1313,7 +1843,7 @@ var GatewayContext = class _GatewayContext {
1313
1843
  const text = await response.text();
1314
1844
  const body = text ? safeJson(text) : {};
1315
1845
  if (!response.ok) {
1316
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path}`;
1846
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path2}`;
1317
1847
  if (response.status === 409) {
1318
1848
  throw new GatewayContextConflictError(
1319
1849
  message,
@@ -1322,7 +1852,7 @@ var GatewayContext = class _GatewayContext {
1322
1852
  }
1323
1853
  throw new GatewayContextError(message);
1324
1854
  }
1325
- this.log("%s %s -> %s", method, path, response.status);
1855
+ this.log("%s %s -> %s", method, path2, response.status);
1326
1856
  return body;
1327
1857
  }
1328
1858
  authHeader() {
@@ -1429,11 +1959,11 @@ function primaryPathForKind(kind, snapshot) {
1429
1959
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
1430
1960
  if (readFile(snapshot, preferred)) return preferred;
1431
1961
  return Object.entries(snapshot.files).find(
1432
- ([path, entry]) => entry.type === "file" && path.endsWith(".md")
1962
+ ([path2, entry]) => entry.type === "file" && path2.endsWith(".md")
1433
1963
  )?.[0] ?? null;
1434
1964
  }
1435
- function parseJsonFile(snapshot, path) {
1436
- const content = readFile(snapshot, path);
1965
+ function parseJsonFile(snapshot, path2) {
1966
+ const content = readFile(snapshot, path2);
1437
1967
  if (!content) return {};
1438
1968
  try {
1439
1969
  const parsed = JSON.parse(content);
@@ -1442,8 +1972,8 @@ function parseJsonFile(snapshot, path) {
1442
1972
  return {};
1443
1973
  }
1444
1974
  }
1445
- function readFile(snapshot, path) {
1446
- const entry = snapshot.files[path];
1975
+ function readFile(snapshot, path2) {
1976
+ const entry = snapshot.files[path2];
1447
1977
  return entry?.type === "file" ? entry.content : null;
1448
1978
  }
1449
1979
  function provenanceFor(snapshot, label) {
@@ -1545,16 +2075,16 @@ var GatewayMemory = class _GatewayMemory {
1545
2075
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
1546
2076
  );
1547
2077
  }
1548
- async read(target, path, options = {}) {
2078
+ async read(target, path2, options = {}) {
1549
2079
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
1550
- const file = snapshot.files[path];
2080
+ const file = snapshot.files[path2];
1551
2081
  return file?.type === "file" ? file.content : null;
1552
2082
  }
1553
- async write(target, path, content, options = {}) {
2083
+ async write(target, path2, content, options = {}) {
1554
2084
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
1555
2085
  return this.push(snapshot.repo.repoHandle, {
1556
2086
  files: {
1557
- [path]: {
2087
+ [path2]: {
1558
2088
  content,
1559
2089
  ...options.mimeType ? { mimeType: options.mimeType } : {}
1560
2090
  }
@@ -1564,10 +2094,10 @@ var GatewayMemory = class _GatewayMemory {
1564
2094
  ...runtimeContextPayload2(options)
1565
2095
  });
1566
2096
  }
1567
- async delete(target, path, options = {}) {
2097
+ async delete(target, path2, options = {}) {
1568
2098
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
1569
2099
  return this.push(snapshot.repo.repoHandle, {
1570
- files: { [path]: null },
2100
+ files: { [path2]: null },
1571
2101
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
1572
2102
  message: options.message,
1573
2103
  ...runtimeContextPayload2(options)
@@ -1580,8 +2110,8 @@ var GatewayMemory = class _GatewayMemory {
1580
2110
  payload
1581
2111
  );
1582
2112
  }
1583
- async requestJson(method, path, payload) {
1584
- const response = await fetch(`${this.host}${path}`, {
2113
+ async requestJson(method, path2, payload) {
2114
+ const response = await fetch(`${this.host}${path2}`, {
1585
2115
  method,
1586
2116
  headers: {
1587
2117
  Authorization: this.authHeader(),
@@ -1592,7 +2122,7 @@ var GatewayMemory = class _GatewayMemory {
1592
2122
  const text = await response.text();
1593
2123
  const body = text ? safeJson2(text) : {};
1594
2124
  if (!response.ok) {
1595
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path}`;
2125
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path2}`;
1596
2126
  if (response.status === 409) {
1597
2127
  throw new GatewayMemoryConflictError(
1598
2128
  message,
@@ -1601,7 +2131,7 @@ var GatewayMemory = class _GatewayMemory {
1601
2131
  }
1602
2132
  throw new GatewayMemoryError(message);
1603
2133
  }
1604
- this.log("%s %s -> %s", method, path, response.status);
2134
+ this.log("%s %s -> %s", method, path2, response.status);
1605
2135
  return body;
1606
2136
  }
1607
2137
  authHeader() {
@@ -1763,10 +2293,10 @@ var PersonasClient = class _PersonasClient {
1763
2293
  done: Boolean(payload.done)
1764
2294
  };
1765
2295
  }
1766
- async requestJson(method, path, payload) {
2296
+ async requestJson(method, path2, payload) {
1767
2297
  let response;
1768
2298
  try {
1769
- response = await fetch(`${this.host}${path}`, {
2299
+ response = await fetch(`${this.host}${path2}`, {
1770
2300
  method,
1771
2301
  headers: {
1772
2302
  Authorization: this.authHeader(),
@@ -1777,7 +2307,7 @@ var PersonasClient = class _PersonasClient {
1777
2307
  });
1778
2308
  } catch (error) {
1779
2309
  const reason = error instanceof Error ? error.message : String(error);
1780
- throw new PersonaError(`${method} ${path} failed: ${reason}`, {
2310
+ throw new PersonaError(`${method} ${path2} failed: ${reason}`, {
1781
2311
  cause: error
1782
2312
  });
1783
2313
  }
@@ -1785,10 +2315,10 @@ var PersonasClient = class _PersonasClient {
1785
2315
  if (!response.ok) {
1786
2316
  const detail = text.slice(0, 500);
1787
2317
  throw new PersonaError(
1788
- `${method} ${path} failed (${response.status}): ${detail}`
2318
+ `${method} ${path2} failed (${response.status}): ${detail}`
1789
2319
  );
1790
2320
  }
1791
- this.log("%s %s -> %s", method, path, response.status);
2321
+ this.log("%s %s -> %s", method, path2, response.status);
1792
2322
  return safeJson3(text);
1793
2323
  }
1794
2324
  authHeader() {
@@ -2013,10 +2543,10 @@ var SessionRecording = class _SessionRecording {
2013
2543
  };
2014
2544
  }
2015
2545
  /** Dump the full session as a JSON fixture (for regression tests). */
2016
- toFixture(path) {
2017
- mkdirSync(dirname(path), { recursive: true });
2018
- writeFileSync(path, JSON.stringify(this.toJSON(), null, 2), "utf-8");
2019
- return path;
2546
+ toFixture(path2) {
2547
+ mkdirSync(dirname(path2), { recursive: true });
2548
+ writeFileSync(path2, JSON.stringify(this.toJSON(), null, 2), "utf-8");
2549
+ return path2;
2020
2550
  }
2021
2551
  static fromApi(raw) {
2022
2552
  return new _SessionRecording({
@@ -2851,7 +3381,7 @@ function randomHex() {
2851
3381
  return crypto.randomUUID().replace(/-/g, "");
2852
3382
  }
2853
3383
  function sleep(ms) {
2854
- return new Promise((resolve) => setTimeout(resolve, ms));
3384
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
2855
3385
  }
2856
3386
  var replay = {
2857
3387
  suite,
@@ -2938,7 +3468,7 @@ var GatewaySecurity = class _GatewaySecurity {
2938
3468
  const descriptors = entries.map(
2939
3469
  ([key, tool]) => this.describeTool(key, tool, options)
2940
3470
  );
2941
- const resolve = await this.resolveToolset({
3471
+ const resolve2 = await this.resolveToolset({
2942
3472
  agent: this.agent,
2943
3473
  toolsetId: options.toolsetId,
2944
3474
  toolsetName: options.toolsetName,
@@ -2948,8 +3478,8 @@ var GatewaySecurity = class _GatewaySecurity {
2948
3478
  });
2949
3479
  const securedEntries = entries.flatMap(([key, tool]) => {
2950
3480
  const descriptor = descriptors.find((item) => item.name === key);
2951
- if (!this.shouldExposeTool(resolve, descriptor.id)) {
2952
- this.log("Hiding tool %s in %s mode", descriptor.id, resolve.mode);
3481
+ if (!this.shouldExposeTool(resolve2, descriptor.id)) {
3482
+ this.log("Hiding tool %s in %s mode", descriptor.id, resolve2.mode);
2953
3483
  return [];
2954
3484
  }
2955
3485
  return [
@@ -2959,7 +3489,7 @@ var GatewaySecurity = class _GatewaySecurity {
2959
3489
  toolId: descriptor.id,
2960
3490
  toolsetId: options.toolsetId,
2961
3491
  context: this.normalizeContext(options.context),
2962
- resolve
3492
+ resolve: resolve2
2963
3493
  })
2964
3494
  ]
2965
3495
  ];
@@ -3047,8 +3577,8 @@ var GatewaySecurity = class _GatewaySecurity {
3047
3577
  );
3048
3578
  }
3049
3579
  }
3050
- async postJson(path, payload) {
3051
- const response = await fetch(`${this.host}${path}`, {
3580
+ async postJson(path2, payload) {
3581
+ const response = await fetch(`${this.host}${path2}`, {
3052
3582
  method: "POST",
3053
3583
  headers: {
3054
3584
  Authorization: this.authHeader(),
@@ -3059,7 +3589,7 @@ var GatewaySecurity = class _GatewaySecurity {
3059
3589
  if (!response.ok) {
3060
3590
  const body = await response.text();
3061
3591
  throw new GatewaySecurityError(
3062
- `Gateway API ${response.status} on ${path}: ${body || response.statusText}`
3592
+ `Gateway API ${response.status} on ${path2}: ${body || response.statusText}`
3063
3593
  );
3064
3594
  }
3065
3595
  return await response.json();
@@ -3074,9 +3604,9 @@ var GatewaySecurity = class _GatewaySecurity {
3074
3604
  metadata: options.toolMetadata?.[key] ?? {}
3075
3605
  };
3076
3606
  }
3077
- shouldExposeTool(resolve, toolId) {
3078
- if (resolve.mode === "observe") return true;
3079
- if (resolve.blockedToolIds.includes(toolId)) return false;
3607
+ shouldExposeTool(resolve2, toolId) {
3608
+ if (resolve2.mode === "observe") return true;
3609
+ if (resolve2.blockedToolIds.includes(toolId)) return false;
3080
3610
  return true;
3081
3611
  }
3082
3612
  normalizeContext(context4) {
@@ -3240,10 +3770,10 @@ var UsersClient = class _UsersClient {
3240
3770
  totalItems: Number(meta.totalItems ?? rows.length)
3241
3771
  };
3242
3772
  }
3243
- async requestJson(method, path, payload) {
3773
+ async requestJson(method, path2, payload) {
3244
3774
  let response;
3245
3775
  try {
3246
- response = await fetch(`${this.host}${path}`, {
3776
+ response = await fetch(`${this.host}${path2}`, {
3247
3777
  method,
3248
3778
  headers: {
3249
3779
  Authorization: this.authHeader(),
@@ -3254,17 +3784,17 @@ var UsersClient = class _UsersClient {
3254
3784
  });
3255
3785
  } catch (error) {
3256
3786
  throw new UserProfileError(
3257
- `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
3787
+ `${method} ${path2} failed: ${error instanceof Error ? error.message : String(error)}`,
3258
3788
  { cause: error }
3259
3789
  );
3260
3790
  }
3261
3791
  const text = await response.text();
3262
3792
  if (!response.ok) {
3263
3793
  throw new UserProfileError(
3264
- `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
3794
+ `${method} ${path2} failed (${response.status}): ${text.slice(0, 500)}`
3265
3795
  );
3266
3796
  }
3267
- this.log("%s %s -> %s", method, path, response.status);
3797
+ this.log("%s %s -> %s", method, path2, response.status);
3268
3798
  return safeJson4(text);
3269
3799
  }
3270
3800
  authHeader() {
@@ -3468,10 +3998,10 @@ var ScoresClient = class _ScoresClient {
3468
3998
  metadata: signalMetadata
3469
3999
  });
3470
4000
  }
3471
- async request(method, path, payload) {
4001
+ async request(method, path2, payload) {
3472
4002
  let response;
3473
4003
  try {
3474
- response = await fetch(`${this.host}${path}`, {
4004
+ response = await fetch(`${this.host}${path2}`, {
3475
4005
  method,
3476
4006
  headers: {
3477
4007
  Authorization: this.authHeader(),
@@ -3482,17 +4012,17 @@ var ScoresClient = class _ScoresClient {
3482
4012
  });
3483
4013
  } catch (error) {
3484
4014
  throw new ScoreError(
3485
- `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
4015
+ `${method} ${path2} failed: ${error instanceof Error ? error.message : String(error)}`,
3486
4016
  { cause: error }
3487
4017
  );
3488
4018
  }
3489
4019
  const text = await response.text();
3490
4020
  if (!response.ok) {
3491
4021
  throw new ScoreError(
3492
- `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
4022
+ `${method} ${path2} failed (${response.status}): ${text.slice(0, 500)}`
3493
4023
  );
3494
4024
  }
3495
- this.log("%s %s -> %s", method, path, response.status);
4025
+ this.log("%s %s -> %s", method, path2, response.status);
3496
4026
  }
3497
4027
  authHeader() {
3498
4028
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -3534,7 +4064,7 @@ init_worlds();
3534
4064
  init_session();
3535
4065
 
3536
4066
  // src/build/index.ts
3537
- import { createHash as createHash2 } from "crypto";
4067
+ import { createHash as createHash3 } from "crypto";
3538
4068
  function canonicalJson(value) {
3539
4069
  if (value === null || typeof value !== "object") {
3540
4070
  return JSON.stringify(value);
@@ -3546,7 +4076,7 @@ function canonicalJson(value) {
3546
4076
  return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
3547
4077
  }
3548
4078
  function sha256(text) {
3549
- return `sha256:${createHash2("sha256").update(text).digest("hex")}`;
4079
+ return `sha256:${createHash3("sha256").update(text).digest("hex")}`;
3550
4080
  }
3551
4081
  var registeredTools = /* @__PURE__ */ new Map();
3552
4082
  var registeredPrompts = /* @__PURE__ */ new Map();
@@ -4418,6 +4948,7 @@ var tracing = {
4418
4948
  };
4419
4949
  export {
4420
4950
  AGENT_ID_KEY,
4951
+ DEFAULT_BRANCH,
4421
4952
  DEFAULT_TIMEOUT_SECONDS,
4422
4953
  DriftError,
4423
4954
  ENV_GATEWAY_HOST,
@@ -4470,11 +5001,15 @@ export {
4470
5001
  assignVariant,
4471
5002
  attachSession,
4472
5003
  basicAuthHeader,
5004
+ buildSourceTarball,
4473
5005
  canonicalJson,
4474
5006
  clearExperimentContext,
5007
+ collectArchiveRelPaths,
4475
5008
  computeBuildHash,
5009
+ computeContentHash,
4476
5010
  computeHash,
4477
5011
  context,
5012
+ createRef,
4478
5013
  currentTraceId,
4479
5014
  defineTool,
4480
5015
  deleteEnvSecret,
@@ -4489,13 +5024,19 @@ export {
4489
5024
  getRegisteredTools,
4490
5025
  getRun,
4491
5026
  identify,
5027
+ isIncludedInContentHash,
4492
5028
  listEnvSecretKeys,
4493
5029
  listRunConfigs,
4494
5030
  loadSession,
5031
+ localTreeState,
4495
5032
  memory,
4496
5033
  openSession,
4497
5034
  openWorld,
4498
5035
  personas,
5036
+ propose,
5037
+ pull,
5038
+ push,
5039
+ readLocalMeta,
4499
5040
  registerArtifact,
4500
5041
  registerPrompt,
4501
5042
  registerSkill,
@@ -4512,10 +5053,13 @@ export {
4512
5053
  sessions,
4513
5054
  setEnvSecret,
4514
5055
  setExperimentContext,
5056
+ snapshotTreeHashes,
4515
5057
  success,
4516
5058
  suite,
4517
5059
  tracing,
4518
5060
  users,
4519
- waitForRun
5061
+ validateWorld,
5062
+ waitForRun,
5063
+ writeLocalMeta
4520
5064
  };
4521
5065
  //# sourceMappingURL=index.mjs.map