@withgateway/sdk 0.1.14 → 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
@@ -314,9 +314,506 @@ 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 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
+
317
813
  // src/worlds/index.ts
318
814
  var worlds_exports = {};
319
815
  __export(worlds_exports, {
816
+ DEFAULT_BRANCH: () => DEFAULT_BRANCH,
320
817
  WorldSession: () => WorldSession,
321
818
  WorldSessionCallError: () => WorldSessionCallError,
322
819
  WorldSessionTimeout: () => WorldSessionTimeout,
@@ -324,17 +821,30 @@ __export(worlds_exports, {
324
821
  WorldTask: () => WorldTask,
325
822
  apiRequest: () => apiRequest,
326
823
  attachSession: () => attachSession,
824
+ buildSourceTarball: () => buildSourceTarball,
825
+ collectArchiveRelPaths: () => collectArchiveRelPaths,
826
+ computeContentHash: () => computeContentHash,
827
+ createRef: () => createRef,
327
828
  deleteEnvSecret: () => deleteEnvSecret,
328
829
  detectProvenance: () => detectProvenance,
329
830
  getRun: () => getRun,
831
+ isIncludedInContentHash: () => isIncludedInContentHash,
330
832
  listEnvSecretKeys: () => listEnvSecretKeys,
331
833
  listRunConfigs: () => listRunConfigs,
834
+ localTreeState: () => localTreeState,
332
835
  openSession: () => openSession,
333
836
  openWorld: () => openWorld,
837
+ propose: () => propose,
838
+ pull: () => pull,
839
+ push: () => push,
840
+ readLocalMeta: () => readLocalMeta,
334
841
  runSessions: () => runSessions,
335
842
  schemaFromSignature: () => schemaFromSignature,
336
843
  setEnvSecret: () => setEnvSecret,
337
- waitForRun: () => waitForRun
844
+ snapshotTreeHashes: () => snapshotTreeHashes,
845
+ validateWorld: () => validateWorld,
846
+ waitForRun: () => waitForRun,
847
+ writeLocalMeta: () => writeLocalMeta
338
848
  });
339
849
  function resolveEnv(options) {
340
850
  const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
@@ -347,9 +857,9 @@ function resolveEnv(options) {
347
857
  }
348
858
  return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
349
859
  }
350
- async function apiRequest(options, method, path, body) {
860
+ async function apiRequest(options, method, path2, body) {
351
861
  const { host, publicKey, secretKey } = resolveEnv(options);
352
- const res = await fetch(`${host}${path}`, {
862
+ const res = await fetch(`${host}${path2}`, {
353
863
  method,
354
864
  headers: {
355
865
  "content-type": "application/json",
@@ -360,7 +870,7 @@ async function apiRequest(options, method, path, body) {
360
870
  const text = await res.text();
361
871
  if (!res.ok) {
362
872
  throw new Error(
363
- `${method} ${path} -> ${res.status}: ${text.slice(0, 400)}`
873
+ `${method} ${path2} -> ${res.status}: ${text.slice(0, 400)}`
364
874
  );
365
875
  }
366
876
  return JSON.parse(text);
@@ -422,7 +932,7 @@ async function waitForRun(runId, opts = {}) {
422
932
  );
423
933
  opts.onTick?.(run);
424
934
  await new Promise(
425
- (resolve) => setTimeout(resolve, (opts.intervalSeconds ?? 20) * 1e3)
935
+ (resolve2) => setTimeout(resolve2, (opts.intervalSeconds ?? 20) * 1e3)
426
936
  );
427
937
  }
428
938
  }
@@ -497,6 +1007,8 @@ var init_worlds = __esm({
497
1007
  "src/worlds/index.ts"() {
498
1008
  "use strict";
499
1009
  init_session();
1010
+ init_hub();
1011
+ init_validate();
500
1012
  WorldTask = class {
501
1013
  constructor(world, name, variant) {
502
1014
  this.world = world;
@@ -564,7 +1076,7 @@ var require_package = __commonJS({
564
1076
  "package.json"(exports, module) {
565
1077
  module.exports = {
566
1078
  name: "@withgateway/sdk",
567
- version: "0.1.14",
1079
+ version: "0.1.16",
568
1080
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
569
1081
  main: "./dist/index.js",
570
1082
  module: "./dist/index.mjs",
@@ -1301,8 +1813,8 @@ var GatewayContext = class _GatewayContext {
1301
1813
  }
1302
1814
  return resolved;
1303
1815
  }
1304
- async requestJson(method, path, payload) {
1305
- const response = await fetch(`${this.host}${path}`, {
1816
+ async requestJson(method, path2, payload) {
1817
+ const response = await fetch(`${this.host}${path2}`, {
1306
1818
  method,
1307
1819
  headers: {
1308
1820
  Authorization: this.authHeader(),
@@ -1313,7 +1825,7 @@ var GatewayContext = class _GatewayContext {
1313
1825
  const text = await response.text();
1314
1826
  const body = text ? safeJson(text) : {};
1315
1827
  if (!response.ok) {
1316
- 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}`;
1317
1829
  if (response.status === 409) {
1318
1830
  throw new GatewayContextConflictError(
1319
1831
  message,
@@ -1322,7 +1834,7 @@ var GatewayContext = class _GatewayContext {
1322
1834
  }
1323
1835
  throw new GatewayContextError(message);
1324
1836
  }
1325
- this.log("%s %s -> %s", method, path, response.status);
1837
+ this.log("%s %s -> %s", method, path2, response.status);
1326
1838
  return body;
1327
1839
  }
1328
1840
  authHeader() {
@@ -1429,11 +1941,11 @@ function primaryPathForKind(kind, snapshot) {
1429
1941
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
1430
1942
  if (readFile(snapshot, preferred)) return preferred;
1431
1943
  return Object.entries(snapshot.files).find(
1432
- ([path, entry]) => entry.type === "file" && path.endsWith(".md")
1944
+ ([path2, entry]) => entry.type === "file" && path2.endsWith(".md")
1433
1945
  )?.[0] ?? null;
1434
1946
  }
1435
- function parseJsonFile(snapshot, path) {
1436
- const content = readFile(snapshot, path);
1947
+ function parseJsonFile(snapshot, path2) {
1948
+ const content = readFile(snapshot, path2);
1437
1949
  if (!content) return {};
1438
1950
  try {
1439
1951
  const parsed = JSON.parse(content);
@@ -1442,8 +1954,8 @@ function parseJsonFile(snapshot, path) {
1442
1954
  return {};
1443
1955
  }
1444
1956
  }
1445
- function readFile(snapshot, path) {
1446
- const entry = snapshot.files[path];
1957
+ function readFile(snapshot, path2) {
1958
+ const entry = snapshot.files[path2];
1447
1959
  return entry?.type === "file" ? entry.content : null;
1448
1960
  }
1449
1961
  function provenanceFor(snapshot, label) {
@@ -1545,16 +2057,16 @@ var GatewayMemory = class _GatewayMemory {
1545
2057
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
1546
2058
  );
1547
2059
  }
1548
- async read(target, path, options = {}) {
2060
+ async read(target, path2, options = {}) {
1549
2061
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
1550
- const file = snapshot.files[path];
2062
+ const file = snapshot.files[path2];
1551
2063
  return file?.type === "file" ? file.content : null;
1552
2064
  }
1553
- async write(target, path, content, options = {}) {
2065
+ async write(target, path2, content, options = {}) {
1554
2066
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
1555
2067
  return this.push(snapshot.repo.repoHandle, {
1556
2068
  files: {
1557
- [path]: {
2069
+ [path2]: {
1558
2070
  content,
1559
2071
  ...options.mimeType ? { mimeType: options.mimeType } : {}
1560
2072
  }
@@ -1564,10 +2076,10 @@ var GatewayMemory = class _GatewayMemory {
1564
2076
  ...runtimeContextPayload2(options)
1565
2077
  });
1566
2078
  }
1567
- async delete(target, path, options = {}) {
2079
+ async delete(target, path2, options = {}) {
1568
2080
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
1569
2081
  return this.push(snapshot.repo.repoHandle, {
1570
- files: { [path]: null },
2082
+ files: { [path2]: null },
1571
2083
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
1572
2084
  message: options.message,
1573
2085
  ...runtimeContextPayload2(options)
@@ -1580,8 +2092,8 @@ var GatewayMemory = class _GatewayMemory {
1580
2092
  payload
1581
2093
  );
1582
2094
  }
1583
- async requestJson(method, path, payload) {
1584
- const response = await fetch(`${this.host}${path}`, {
2095
+ async requestJson(method, path2, payload) {
2096
+ const response = await fetch(`${this.host}${path2}`, {
1585
2097
  method,
1586
2098
  headers: {
1587
2099
  Authorization: this.authHeader(),
@@ -1592,7 +2104,7 @@ var GatewayMemory = class _GatewayMemory {
1592
2104
  const text = await response.text();
1593
2105
  const body = text ? safeJson2(text) : {};
1594
2106
  if (!response.ok) {
1595
- 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}`;
1596
2108
  if (response.status === 409) {
1597
2109
  throw new GatewayMemoryConflictError(
1598
2110
  message,
@@ -1601,7 +2113,7 @@ var GatewayMemory = class _GatewayMemory {
1601
2113
  }
1602
2114
  throw new GatewayMemoryError(message);
1603
2115
  }
1604
- this.log("%s %s -> %s", method, path, response.status);
2116
+ this.log("%s %s -> %s", method, path2, response.status);
1605
2117
  return body;
1606
2118
  }
1607
2119
  authHeader() {
@@ -1763,10 +2275,10 @@ var PersonasClient = class _PersonasClient {
1763
2275
  done: Boolean(payload.done)
1764
2276
  };
1765
2277
  }
1766
- async requestJson(method, path, payload) {
2278
+ async requestJson(method, path2, payload) {
1767
2279
  let response;
1768
2280
  try {
1769
- response = await fetch(`${this.host}${path}`, {
2281
+ response = await fetch(`${this.host}${path2}`, {
1770
2282
  method,
1771
2283
  headers: {
1772
2284
  Authorization: this.authHeader(),
@@ -1777,7 +2289,7 @@ var PersonasClient = class _PersonasClient {
1777
2289
  });
1778
2290
  } catch (error) {
1779
2291
  const reason = error instanceof Error ? error.message : String(error);
1780
- throw new PersonaError(`${method} ${path} failed: ${reason}`, {
2292
+ throw new PersonaError(`${method} ${path2} failed: ${reason}`, {
1781
2293
  cause: error
1782
2294
  });
1783
2295
  }
@@ -1785,10 +2297,10 @@ var PersonasClient = class _PersonasClient {
1785
2297
  if (!response.ok) {
1786
2298
  const detail = text.slice(0, 500);
1787
2299
  throw new PersonaError(
1788
- `${method} ${path} failed (${response.status}): ${detail}`
2300
+ `${method} ${path2} failed (${response.status}): ${detail}`
1789
2301
  );
1790
2302
  }
1791
- this.log("%s %s -> %s", method, path, response.status);
2303
+ this.log("%s %s -> %s", method, path2, response.status);
1792
2304
  return safeJson3(text);
1793
2305
  }
1794
2306
  authHeader() {
@@ -2013,10 +2525,10 @@ var SessionRecording = class _SessionRecording {
2013
2525
  };
2014
2526
  }
2015
2527
  /** 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;
2528
+ toFixture(path2) {
2529
+ mkdirSync(dirname(path2), { recursive: true });
2530
+ writeFileSync(path2, JSON.stringify(this.toJSON(), null, 2), "utf-8");
2531
+ return path2;
2020
2532
  }
2021
2533
  static fromApi(raw) {
2022
2534
  return new _SessionRecording({
@@ -2851,7 +3363,7 @@ function randomHex() {
2851
3363
  return crypto.randomUUID().replace(/-/g, "");
2852
3364
  }
2853
3365
  function sleep(ms) {
2854
- return new Promise((resolve) => setTimeout(resolve, ms));
3366
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
2855
3367
  }
2856
3368
  var replay = {
2857
3369
  suite,
@@ -2938,7 +3450,7 @@ var GatewaySecurity = class _GatewaySecurity {
2938
3450
  const descriptors = entries.map(
2939
3451
  ([key, tool]) => this.describeTool(key, tool, options)
2940
3452
  );
2941
- const resolve = await this.resolveToolset({
3453
+ const resolve2 = await this.resolveToolset({
2942
3454
  agent: this.agent,
2943
3455
  toolsetId: options.toolsetId,
2944
3456
  toolsetName: options.toolsetName,
@@ -2948,8 +3460,8 @@ var GatewaySecurity = class _GatewaySecurity {
2948
3460
  });
2949
3461
  const securedEntries = entries.flatMap(([key, tool]) => {
2950
3462
  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);
3463
+ if (!this.shouldExposeTool(resolve2, descriptor.id)) {
3464
+ this.log("Hiding tool %s in %s mode", descriptor.id, resolve2.mode);
2953
3465
  return [];
2954
3466
  }
2955
3467
  return [
@@ -2959,7 +3471,7 @@ var GatewaySecurity = class _GatewaySecurity {
2959
3471
  toolId: descriptor.id,
2960
3472
  toolsetId: options.toolsetId,
2961
3473
  context: this.normalizeContext(options.context),
2962
- resolve
3474
+ resolve: resolve2
2963
3475
  })
2964
3476
  ]
2965
3477
  ];
@@ -3047,8 +3559,8 @@ var GatewaySecurity = class _GatewaySecurity {
3047
3559
  );
3048
3560
  }
3049
3561
  }
3050
- async postJson(path, payload) {
3051
- const response = await fetch(`${this.host}${path}`, {
3562
+ async postJson(path2, payload) {
3563
+ const response = await fetch(`${this.host}${path2}`, {
3052
3564
  method: "POST",
3053
3565
  headers: {
3054
3566
  Authorization: this.authHeader(),
@@ -3059,7 +3571,7 @@ var GatewaySecurity = class _GatewaySecurity {
3059
3571
  if (!response.ok) {
3060
3572
  const body = await response.text();
3061
3573
  throw new GatewaySecurityError(
3062
- `Gateway API ${response.status} on ${path}: ${body || response.statusText}`
3574
+ `Gateway API ${response.status} on ${path2}: ${body || response.statusText}`
3063
3575
  );
3064
3576
  }
3065
3577
  return await response.json();
@@ -3074,9 +3586,9 @@ var GatewaySecurity = class _GatewaySecurity {
3074
3586
  metadata: options.toolMetadata?.[key] ?? {}
3075
3587
  };
3076
3588
  }
3077
- shouldExposeTool(resolve, toolId) {
3078
- if (resolve.mode === "observe") return true;
3079
- 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;
3080
3592
  return true;
3081
3593
  }
3082
3594
  normalizeContext(context4) {
@@ -3240,10 +3752,10 @@ var UsersClient = class _UsersClient {
3240
3752
  totalItems: Number(meta.totalItems ?? rows.length)
3241
3753
  };
3242
3754
  }
3243
- async requestJson(method, path, payload) {
3755
+ async requestJson(method, path2, payload) {
3244
3756
  let response;
3245
3757
  try {
3246
- response = await fetch(`${this.host}${path}`, {
3758
+ response = await fetch(`${this.host}${path2}`, {
3247
3759
  method,
3248
3760
  headers: {
3249
3761
  Authorization: this.authHeader(),
@@ -3254,17 +3766,17 @@ var UsersClient = class _UsersClient {
3254
3766
  });
3255
3767
  } catch (error) {
3256
3768
  throw new UserProfileError(
3257
- `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
3769
+ `${method} ${path2} failed: ${error instanceof Error ? error.message : String(error)}`,
3258
3770
  { cause: error }
3259
3771
  );
3260
3772
  }
3261
3773
  const text = await response.text();
3262
3774
  if (!response.ok) {
3263
3775
  throw new UserProfileError(
3264
- `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
3776
+ `${method} ${path2} failed (${response.status}): ${text.slice(0, 500)}`
3265
3777
  );
3266
3778
  }
3267
- this.log("%s %s -> %s", method, path, response.status);
3779
+ this.log("%s %s -> %s", method, path2, response.status);
3268
3780
  return safeJson4(text);
3269
3781
  }
3270
3782
  authHeader() {
@@ -3468,10 +3980,10 @@ var ScoresClient = class _ScoresClient {
3468
3980
  metadata: signalMetadata
3469
3981
  });
3470
3982
  }
3471
- async request(method, path, payload) {
3983
+ async request(method, path2, payload) {
3472
3984
  let response;
3473
3985
  try {
3474
- response = await fetch(`${this.host}${path}`, {
3986
+ response = await fetch(`${this.host}${path2}`, {
3475
3987
  method,
3476
3988
  headers: {
3477
3989
  Authorization: this.authHeader(),
@@ -3482,17 +3994,17 @@ var ScoresClient = class _ScoresClient {
3482
3994
  });
3483
3995
  } catch (error) {
3484
3996
  throw new ScoreError(
3485
- `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
3997
+ `${method} ${path2} failed: ${error instanceof Error ? error.message : String(error)}`,
3486
3998
  { cause: error }
3487
3999
  );
3488
4000
  }
3489
4001
  const text = await response.text();
3490
4002
  if (!response.ok) {
3491
4003
  throw new ScoreError(
3492
- `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
4004
+ `${method} ${path2} failed (${response.status}): ${text.slice(0, 500)}`
3493
4005
  );
3494
4006
  }
3495
- this.log("%s %s -> %s", method, path, response.status);
4007
+ this.log("%s %s -> %s", method, path2, response.status);
3496
4008
  }
3497
4009
  authHeader() {
3498
4010
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -3534,7 +4046,7 @@ init_worlds();
3534
4046
  init_session();
3535
4047
 
3536
4048
  // src/build/index.ts
3537
- import { createHash as createHash2 } from "crypto";
4049
+ import { createHash as createHash3 } from "crypto";
3538
4050
  function canonicalJson(value) {
3539
4051
  if (value === null || typeof value !== "object") {
3540
4052
  return JSON.stringify(value);
@@ -3546,7 +4058,7 @@ function canonicalJson(value) {
3546
4058
  return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
3547
4059
  }
3548
4060
  function sha256(text) {
3549
- return `sha256:${createHash2("sha256").update(text).digest("hex")}`;
4061
+ return `sha256:${createHash3("sha256").update(text).digest("hex")}`;
3550
4062
  }
3551
4063
  var registeredTools = /* @__PURE__ */ new Map();
3552
4064
  var registeredPrompts = /* @__PURE__ */ new Map();
@@ -4418,6 +4930,7 @@ var tracing = {
4418
4930
  };
4419
4931
  export {
4420
4932
  AGENT_ID_KEY,
4933
+ DEFAULT_BRANCH,
4421
4934
  DEFAULT_TIMEOUT_SECONDS,
4422
4935
  DriftError,
4423
4936
  ENV_GATEWAY_HOST,
@@ -4470,11 +4983,15 @@ export {
4470
4983
  assignVariant,
4471
4984
  attachSession,
4472
4985
  basicAuthHeader,
4986
+ buildSourceTarball,
4473
4987
  canonicalJson,
4474
4988
  clearExperimentContext,
4989
+ collectArchiveRelPaths,
4475
4990
  computeBuildHash,
4991
+ computeContentHash,
4476
4992
  computeHash,
4477
4993
  context,
4994
+ createRef,
4478
4995
  currentTraceId,
4479
4996
  defineTool,
4480
4997
  deleteEnvSecret,
@@ -4489,13 +5006,19 @@ export {
4489
5006
  getRegisteredTools,
4490
5007
  getRun,
4491
5008
  identify,
5009
+ isIncludedInContentHash,
4492
5010
  listEnvSecretKeys,
4493
5011
  listRunConfigs,
4494
5012
  loadSession,
5013
+ localTreeState,
4495
5014
  memory,
4496
5015
  openSession,
4497
5016
  openWorld,
4498
5017
  personas,
5018
+ propose,
5019
+ pull,
5020
+ push,
5021
+ readLocalMeta,
4499
5022
  registerArtifact,
4500
5023
  registerPrompt,
4501
5024
  registerSkill,
@@ -4512,10 +5035,13 @@ export {
4512
5035
  sessions,
4513
5036
  setEnvSecret,
4514
5037
  setExperimentContext,
5038
+ snapshotTreeHashes,
4515
5039
  success,
4516
5040
  suite,
4517
5041
  tracing,
4518
5042
  users,
4519
- waitForRun
5043
+ validateWorld,
5044
+ waitForRun,
5045
+ writeLocalMeta
4520
5046
  };
4521
5047
  //# sourceMappingURL=index.mjs.map