@withgateway/sdk 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/cli.js +4304 -2381
  2. package/dist/cli.js.map +1 -1
  3. package/dist/cli.mjs +4266 -2343
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/files/index.js +56 -31
  6. package/dist/files/index.js.map +1 -1
  7. package/dist/files/index.mjs +52 -27
  8. package/dist/files/index.mjs.map +1 -1
  9. package/dist/hooks/index.d.mts +1 -1
  10. package/dist/hooks/index.d.ts +1 -1
  11. package/dist/hooks/index.js +280 -8
  12. package/dist/hooks/index.js.map +1 -1
  13. package/dist/hooks/index.mjs +272 -8
  14. package/dist/hooks/index.mjs.map +1 -1
  15. package/dist/{index-1xnxt0Cc.d.ts → index-BO2SNSgy.d.ts} +12 -1
  16. package/dist/{index-B0R3IgsV.d.mts → index-BaKu5ZSU.d.mts} +124 -2
  17. package/dist/{index-B0R3IgsV.d.ts → index-BaKu5ZSU.d.ts} +124 -2
  18. package/dist/{index-C1wDEwH4.d.mts → index-Cz8Q6Keo.d.mts} +12 -1
  19. package/dist/index.d.mts +4 -4
  20. package/dist/index.d.ts +4 -4
  21. package/dist/index.js +1100 -438
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +1079 -424
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/traces/index.js +45 -20
  26. package/dist/traces/index.js.map +1 -1
  27. package/dist/traces/index.mjs +41 -16
  28. package/dist/traces/index.mjs.map +1 -1
  29. package/dist/tracing/index.d.mts +1 -1
  30. package/dist/tracing/index.d.ts +1 -1
  31. package/dist/tracing/index.js +98 -61
  32. package/dist/tracing/index.js.map +1 -1
  33. package/dist/tracing/index.mjs +98 -61
  34. package/dist/tracing/index.mjs.map +1 -1
  35. package/dist/worlds/index.d.mts +179 -25
  36. package/dist/worlds/index.d.ts +179 -25
  37. package/dist/worlds/index.js +639 -284
  38. package/dist/worlds/index.js.map +1 -1
  39. package/dist/worlds/index.mjs +631 -276
  40. package/dist/worlds/index.mjs.map +1 -1
  41. package/package.json +2 -1
  42. package/runtime/gatewaysdk/environment/_bundle.py +15 -25
  43. package/runtime/gatewaysdk/environment/_materialize.py +53 -9
  44. package/runtime/gatewaysdk/environment/_toml.py +13 -0
  45. package/runtime/gatewaysdk/environment/schema/api.py +40 -25
  46. package/runtime/gatewaysdk/environment/schema/batch.py +15 -11
  47. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +24 -0
  48. package/runtime/gatewaysdk/environment/schema/compiler.py +60 -28
  49. package/runtime/gatewaysdk/environment/schema/conform.py +1 -1
  50. package/runtime/gatewaysdk/environment/schema/connector.py +1 -1
  51. package/runtime/gatewaysdk/environment/schema/connectors/google-drive/readers/merge.ingest.toml +25 -0
  52. package/runtime/gatewaysdk/environment/schema/connectors/google-drive/readers/merge.toml +17 -0
  53. package/runtime/gatewaysdk/environment/schema/data.py +21 -19
  54. package/runtime/gatewaysdk/environment/schema/extract.py +1 -5
  55. package/runtime/gatewaysdk/environment/schema/grading.py +225 -0
  56. package/runtime/gatewaysdk/environment/schema/host.py +156 -546
  57. package/runtime/gatewaysdk/environment/schema/host_surfaces.py +1 -5
  58. package/runtime/gatewaysdk/environment/schema/ingest.py +3 -11
  59. package/runtime/gatewaysdk/environment/schema/local.py +32 -126
  60. package/runtime/gatewaysdk/environment/schema/mapping.py +1 -1
  61. package/runtime/gatewaysdk/environment/schema/platform.py +30 -26
  62. package/runtime/gatewaysdk/environment/schema/proof.py +237 -0
  63. package/runtime/gatewaysdk/environment/schema/redaction.py +1 -1
  64. package/runtime/gatewaysdk/environment/schema/runtime.py +686 -0
  65. package/runtime/gatewaysdk/environment/schema/scaffold.py +9 -2
  66. package/runtime/gatewaysdk/environment/schema/skill.py +1 -0
  67. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +67 -9
  68. package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +67 -6
  69. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +4 -1
  70. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +11 -11
  71. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +17 -1
  72. package/runtime/gatewaysdk/environment/schema/snapshot.py +1 -5
  73. package/runtime/gatewaysdk/environment/schema/tools_world.py +71 -23
  74. package/runtime/gatewaysdk/environment/schema/ui.py +1 -5
  75. package/runtime/gatewaysdk/environment/schema/verbs.py +136 -0
  76. package/runtime/gatewaysdk/environment/schema/workers.py +14 -0
  77. package/runtime/gatewaysdk/environment/schema/world_tests.py +1 -1
  78. package/runtime/gatewaysdk/environment/world.py +507 -0
  79. package/skills/benchmarks-getting-started/SKILL.md +67 -9
  80. package/skills/connector-schema-authoring/connector-worlds.md +8 -1
  81. package/skills/connector-schema-authoring/world-schema.md +1 -1
  82. package/skills/hooks-getting-started/SKILL.md +67 -6
  83. package/skills/world-data-ingestion/SKILL.md +4 -1
  84. package/skills/worlds-getting-started/SKILL.md +11 -11
  85. package/skills/worlds-getting-started/references/cli-mcp-map.md +17 -1
package/dist/index.js CHANGED
@@ -195,10 +195,10 @@ var init_users = __esm({
195
195
  totalItems: Number(meta.totalItems ?? rows.length)
196
196
  };
197
197
  }
198
- async requestJson(method, path13, payload) {
198
+ async requestJson(method, path14, payload) {
199
199
  let response;
200
200
  try {
201
- response = await fetch(`${this.host}${path13}`, {
201
+ response = await fetch(`${this.host}${path14}`, {
202
202
  method,
203
203
  headers: {
204
204
  Authorization: this.authHeader(),
@@ -209,17 +209,17 @@ var init_users = __esm({
209
209
  });
210
210
  } catch (error) {
211
211
  throw new UserProfileError(
212
- `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
212
+ `${method} ${path14} failed: ${error instanceof Error ? error.message : String(error)}`,
213
213
  { cause: error }
214
214
  );
215
215
  }
216
216
  const text2 = await response.text();
217
217
  if (!response.ok) {
218
218
  throw new UserProfileError(
219
- `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
219
+ `${method} ${path14} failed (${response.status}): ${text2.slice(0, 500)}`
220
220
  );
221
221
  }
222
- this.log("%s %s -> %s", method, path13, response.status);
222
+ this.log("%s %s -> %s", method, path14, response.status);
223
223
  return safeJson4(text2);
224
224
  }
225
225
  authHeader() {
@@ -408,10 +408,10 @@ var init_scores = __esm({
408
408
  metadata: signalMetadata
409
409
  });
410
410
  }
411
- async request(method, path13, payload) {
411
+ async request(method, path14, payload) {
412
412
  let response;
413
413
  try {
414
- response = await fetch(`${this.host}${path13}`, {
414
+ response = await fetch(`${this.host}${path14}`, {
415
415
  method,
416
416
  headers: {
417
417
  Authorization: this.authHeader(),
@@ -422,17 +422,17 @@ var init_scores = __esm({
422
422
  });
423
423
  } catch (error) {
424
424
  throw new ScoreError(
425
- `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
425
+ `${method} ${path14} failed: ${error instanceof Error ? error.message : String(error)}`,
426
426
  { cause: error }
427
427
  );
428
428
  }
429
429
  const text2 = await response.text();
430
430
  if (!response.ok) {
431
431
  throw new ScoreError(
432
- `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
432
+ `${method} ${path14} failed (${response.status}): ${text2.slice(0, 500)}`
433
433
  );
434
434
  }
435
- this.log("%s %s -> %s", method, path13, response.status);
435
+ this.log("%s %s -> %s", method, path14, response.status);
436
436
  }
437
437
  authHeader() {
438
438
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -463,9 +463,9 @@ function surfaceKinds(s) {
463
463
  if (s.browser) kinds.push("browser");
464
464
  return kinds;
465
465
  }
466
- function uiUrl(ui, path13) {
466
+ function uiUrl(ui, path14) {
467
467
  const base = ui.url.replace(/\/+$/, "");
468
- const rel = path13.startsWith("/") ? path13 : `/${path13}`;
468
+ const rel = path14.startsWith("/") ? path14 : `/${path14}`;
469
469
  if (!ui.token) return base + rel;
470
470
  const [beforeHash, hash] = splitHash(rel);
471
471
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -1022,12 +1022,13 @@ async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
1022
1022
  );
1023
1023
  const at = slugOrRef.indexOf("@");
1024
1024
  const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
1025
+ const ref = at === -1 ? "" : slugOrRef.slice(at + 1);
1025
1026
  const state2 = await apiRequest(
1026
1027
  options,
1027
1028
  "POST",
1028
1029
  "/api/public/world-sessions",
1029
1030
  {
1030
- ...opts.versionId ? { versionId: opts.versionId } : { slug },
1031
+ ...opts.versionId ? { versionId: opts.versionId } : { slug, ...ref && { ref } },
1031
1032
  task,
1032
1033
  ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
1033
1034
  ...opts.surfaces?.length ? { surfaces: opts.surfaces } : {}
@@ -1041,8 +1042,8 @@ async function runSessions(opts) {
1041
1042
  let tasks = opts.tasks;
1042
1043
  let versionId = opts.versionId;
1043
1044
  if (!tasks || !versionId) {
1044
- const { openWorld: openWorld2 } = await Promise.resolve().then(() => (init_worlds(), worlds_exports));
1045
- const world = await openWorld2(opts.world, options);
1045
+ const { openWorld: openWorld2, openWorldVersion: openWorldVersion2 } = await Promise.resolve().then(() => (init_worlds(), worlds_exports));
1046
+ const world = versionId ? await openWorldVersion2(opts.world.split("@", 1)[0], versionId, options) : await openWorld2(opts.world, options);
1046
1047
  tasks = tasks ?? world.tasks.map((t) => t.name);
1047
1048
  versionId = versionId ?? world.version.versionId;
1048
1049
  }
@@ -1098,10 +1099,12 @@ async function runSessions(opts) {
1098
1099
  })
1099
1100
  );
1100
1101
  const mean = results.reduce((sum, r) => sum + (r.reward ?? 0), 0) / (results.length || 1);
1102
+ const failed = results.filter((r) => r.error).length;
1101
1103
  return {
1102
1104
  results: results.sort((a, b) => a.task.localeCompare(b.task)),
1103
1105
  mean,
1104
- passed: opts.failUnder === void 0 ? true : mean >= opts.failUnder
1106
+ failed,
1107
+ passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
1105
1108
  };
1106
1109
  }
1107
1110
  var import_node_crypto, isTaskIdRef, isTaskSpec, taskLabel, READY_TIMEOUT_MS, READY_POLL_MS, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep2, modelToolName, WorldSessionTimeout, WorldSessionCallError, WorldSession;
@@ -1226,18 +1229,18 @@ var init_session = __esm({
1226
1229
  }
1227
1230
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
1228
1231
  browserRecorder(agentId) {
1229
- const path13 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
1232
+ const path14 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
1230
1233
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${(0, import_node_crypto.randomUUID)().replace(/-/g, "").slice(0, 8)}`;
1231
1234
  return new BrowserActionRecorder(
1232
1235
  async (events) => {
1233
- await apiRequest(this.options, "POST", path13, { events });
1236
+ await apiRequest(this.options, "POST", path14, { events });
1234
1237
  },
1235
1238
  id,
1236
1239
  {
1237
1240
  // Continue this agent's timeline where the platform has it, so the
1238
1241
  // same id reopened later never re-numbers from zero.
1239
1242
  resumeFrom: async () => {
1240
- const summary = await apiRequest(this.options, "GET", path13);
1243
+ const summary = await apiRequest(this.options, "GET", path14);
1241
1244
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
1242
1245
  }
1243
1246
  }
@@ -2000,7 +2003,7 @@ var require_package = __commonJS({
2000
2003
  "package.json"(exports2, module2) {
2001
2004
  module2.exports = {
2002
2005
  name: "@withgateway/sdk",
2003
- version: "0.2.4",
2006
+ version: "0.2.6",
2004
2007
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
2005
2008
  main: "./dist/index.js",
2006
2009
  module: "./dist/index.mjs",
@@ -2104,6 +2107,7 @@ var require_package = __commonJS({
2104
2107
  "typecheck:examples": "tsc -p tsconfig.examples.json --noEmit",
2105
2108
  test: "vitest",
2106
2109
  "test:run": "vitest run",
2110
+ "test:integration": "vitest run --config vitest.integration.config.ts",
2107
2111
  lint: "eslint src --ext .ts",
2108
2112
  clean: "rm -rf dist",
2109
2113
  example: "tsx examples/test-ab-experiment.ts",
@@ -2819,7 +2823,7 @@ async function fileTaskRun(live, runId, model, report, grade, agentError, traced
2819
2823
  task: live.name ?? live.taskId ?? runId,
2820
2824
  rolloutIndex: 0,
2821
2825
  prompt: live.instruction,
2822
- completion: report,
2826
+ ...report === null ? {} : { completion: report },
2823
2827
  rewards: grade.rewards,
2824
2828
  info: {
2825
2829
  sessionId: runId,
@@ -2857,17 +2861,20 @@ async function fileTaskRun(live, runId, model, report, grade, agentError, traced
2857
2861
  return evaluationId;
2858
2862
  }
2859
2863
  function reportFromResult(result) {
2864
+ if (result === void 0 || result === null)
2865
+ return { report: null, extra: {} };
2860
2866
  if (typeof result === "string") return { report: result, extra: {} };
2861
- if (result && typeof result === "object") {
2862
- const { report, ...extra } = result;
2863
- return {
2864
- report: report === void 0 || report === null ? null : typeof report === "string" ? report : String(report),
2865
- extra
2866
- };
2867
- }
2868
- if (result !== void 0 && result !== null)
2869
- return { report: String(result), extra: {} };
2870
- return { report: null, extra: {} };
2867
+ if (typeof result !== "object" || Array.isArray(result))
2868
+ throw new Error(
2869
+ `agent returned ${describeValue(result)}; ${RESULT_CONTRACT}`
2870
+ );
2871
+ const { report, ...extra } = result;
2872
+ if (report === void 0 || report === null) return { report: null, extra };
2873
+ if (typeof report !== "string")
2874
+ throw new Error(
2875
+ `agent returned a report of type ${describeValue(report)}; ${RESULT_CONTRACT}`
2876
+ );
2877
+ return { report, extra };
2871
2878
  }
2872
2879
  async function runTask(ref, agent, opts) {
2873
2880
  const runId = `wtr-${(0, import_node_crypto3.randomBytes)(6).toString("hex")}`;
@@ -2971,7 +2978,7 @@ async function runTask(ref, agent, opts) {
2971
2978
  }
2972
2979
  }
2973
2980
  }
2974
- var import_node_crypto3, fs, path, TaskRun;
2981
+ var import_node_crypto3, fs, path, TaskRun, describeValue, RESULT_CONTRACT;
2975
2982
  var init_taskRun = __esm({
2976
2983
  "src/worlds/taskRun.ts"() {
2977
2984
  "use strict";
@@ -3045,6 +3052,154 @@ var init_taskRun = __esm({
3045
3052
  Object.assign(this.notes, notes);
3046
3053
  }
3047
3054
  };
3055
+ describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
3056
+ RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
3057
+ }
3058
+ });
3059
+
3060
+ // src/worlds/worldDir.ts
3061
+ var import_node_crypto4, fs2, path2, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
3062
+ var init_worldDir = __esm({
3063
+ "src/worlds/worldDir.ts"() {
3064
+ "use strict";
3065
+ import_node_crypto4 = require("crypto");
3066
+ fs2 = __toESM(require("fs"));
3067
+ path2 = __toESM(require("path"));
3068
+ WORLD_MANIFEST = "gateway-env.toml";
3069
+ API_BUNDLE_MANIFEST = "gateway-api-world.toml";
3070
+ BUNDLED_WORLD_PREFIX = "server/world/";
3071
+ CONNECTOR_SPEC = "connector.toml";
3072
+ PYPROJECT = "pyproject.toml";
3073
+ WORLD_SCHEMA_MARKERS = ["schema/world.json", "schema/runtime.json", "schema/lock.json"];
3074
+ WORLD_ROOT_MARKERS = [CONNECTOR_SPEC, ...WORLD_SCHEMA_MARKERS];
3075
+ BROAD_ROOT_MARKERS = [WORLD_MANIFEST, API_BUNDLE_MANIFEST];
3076
+ SKIPPED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
3077
+ V1_DEP = /["']verifiers\s*(?:\[[^\]]*\])?\s*(?:>=|~=|==|>)\s*(?:0\.(?:[2-9]\d*|[1-9]\d+)|[1-9]\d*)/;
3078
+ V1_IMPORT = /^\s*(?:from\s+verifiers\.v1\s+import\b|from\s+verifiers\s+import\s+v1\b|import\s+verifiers\.v1\b)/m;
3079
+ V1_TASKSET = /class\s+\w+\s*\(\s*(?:(?:vf|v1|verifiers(?:\.v1)?)\.)?Taskset\s*[,)]/;
3080
+ HARBOR_TASK = /^(?:tasks\/)?([^/]+)\/task\.toml$/;
3081
+ skippedDir = (name) => name.startsWith(".") || SKIPPED_DIRS.has(name) || name.endsWith(".egg-info");
3082
+ byCodePoint = (a, b) => Buffer.compare(Buffer.from(a, "utf-8"), Buffer.from(b, "utf-8"));
3083
+ WorldDir = class _WorldDir {
3084
+ constructor(dir, worldRoot) {
3085
+ this.dir = dir;
3086
+ this.worldRoot = worldRoot;
3087
+ }
3088
+ pathsMemo;
3089
+ static open(dir) {
3090
+ const present = (rel) => fs2.existsSync(path2.join(dir, rel));
3091
+ const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
3092
+ return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
3093
+ }
3094
+ // ── Files ──────────────────────────────────────────────────────────────
3095
+ has(rel) {
3096
+ return fs2.existsSync(path2.join(this.dir, rel));
3097
+ }
3098
+ text(rel) {
3099
+ try {
3100
+ return fs2.readFileSync(path2.join(this.dir, rel), "utf-8");
3101
+ } catch {
3102
+ return null;
3103
+ }
3104
+ }
3105
+ /** A path under the authored world's root. */
3106
+ worldPath(rel) {
3107
+ return this.worldRoot + rel;
3108
+ }
3109
+ /** Every regular file the archive rules keep (`_archive.py`: no hidden,
3110
+ * dist, build, outputs, pycache or egg-info directories), as sorted
3111
+ * relpaths. Root files enter only when a Gateway manifest widens the root
3112
+ * or they are README.md, pyproject.toml or Python. */
3113
+ get archivePaths() {
3114
+ if (this.pathsMemo) return this.pathsMemo;
3115
+ const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
3116
+ const out = [];
3117
+ const walk = (rel) => {
3118
+ for (const entry of fs2.readdirSync(rel ? path2.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
3119
+ const child = rel ? `${rel}/${entry.name}` : entry.name;
3120
+ if (entry.isDirectory()) {
3121
+ if (!skippedDir(entry.name)) walk(child);
3122
+ } else if (entry.isFile() && !entry.name.startsWith(".")) {
3123
+ const rootKept = broad || ["README.md", PYPROJECT].includes(entry.name) || entry.name.endsWith(".py");
3124
+ if (rel || rootKept) out.push(child);
3125
+ }
3126
+ }
3127
+ };
3128
+ walk("");
3129
+ return this.pathsMemo = out.sort(byCodePoint);
3130
+ }
3131
+ /** A root `.gitignore` is present: the Python SDK honours it when
3132
+ * collecting, this package does not reimplement gitignore. */
3133
+ get hasRootGitignore() {
3134
+ return this.has(".gitignore");
3135
+ }
3136
+ /** SHA-256 over `utf8("file:" + relpath)` + bytes per archive path in
3137
+ * sorted order: the version's identity, byte-compatible with
3138
+ * `_archive.compute_content_hash` and Prime Intellect. */
3139
+ get contentHash() {
3140
+ const hasher = (0, import_node_crypto4.createHash)("sha256");
3141
+ for (const rel of this.archivePaths) {
3142
+ hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
3143
+ hasher.update(fs2.readFileSync(path2.join(this.dir, rel)));
3144
+ }
3145
+ return hasher.digest("hex");
3146
+ }
3147
+ // ── Markers ────────────────────────────────────────────────────────────
3148
+ get isApiBundle() {
3149
+ return this.has(API_BUNDLE_MANIFEST);
3150
+ }
3151
+ get hasManifest() {
3152
+ return this.has(this.worldPath(WORLD_MANIFEST));
3153
+ }
3154
+ get hasSchemaMarker() {
3155
+ return WORLD_SCHEMA_MARKERS.some((m) => this.has(this.worldPath(m)));
3156
+ }
3157
+ /** Harbor task dirs, relative to the tree: `tasks/<name>` when a `tasks/`
3158
+ * dir holds them, else the root `<name>` dirs. */
3159
+ taskDirs() {
3160
+ const names = /* @__PURE__ */ new Set();
3161
+ for (const rel of this.archivePaths) {
3162
+ const m = HARBOR_TASK.exec(rel);
3163
+ if (m) names.add(rel.startsWith("tasks/") ? `tasks/${m[1]}` : m[1]);
3164
+ }
3165
+ const under = [...names].filter((n) => n.startsWith("tasks/"));
3166
+ return (under.length ? under : [...names]).sort();
3167
+ }
3168
+ /** The Python package of a verifiers tree (`<pkg>/__init__.py` or
3169
+ * `src/<pkg>/__init__.py`), or null. */
3170
+ pythonPackage() {
3171
+ if (!this.has(PYPROJECT)) return null;
3172
+ for (const base of ["", "src/"]) {
3173
+ const init3 = this.archivePaths.find((rel) => {
3174
+ const m = /^([^/]+)\/__init__\.py$/.exec(rel.startsWith(base) ? rel.slice(base.length) : "");
3175
+ return m !== null && m[1] !== "tests";
3176
+ });
3177
+ if (init3) return { name: init3.slice(base.length).split("/")[0], init: init3 };
3178
+ }
3179
+ return null;
3180
+ }
3181
+ /** The package targets the `verifiers.v1` API: read from the root
3182
+ * pyproject and every archived Python module. */
3183
+ get isVerifiersV1() {
3184
+ for (const rel of this.archivePaths) {
3185
+ if (rel !== PYPROJECT && !rel.endsWith(".py")) continue;
3186
+ const text2 = this.text(rel);
3187
+ if (text2 === null) continue;
3188
+ if (rel === PYPROJECT ? V1_DEP.test(text2) : V1_IMPORT.test(text2) || text2.includes("__all__") && V1_TASKSET.test(text2))
3189
+ return true;
3190
+ }
3191
+ return false;
3192
+ }
3193
+ // ── The answer ─────────────────────────────────────────────────────────
3194
+ /** The runner's kind (`detect_env_kind`): api / mock-tools win over
3195
+ * everything, a harbor task tree next, then the verifiers API version. */
3196
+ get kind() {
3197
+ if (this.isApiBundle) return "api";
3198
+ if (this.has(WORLD_MANIFEST)) return "mock-tools";
3199
+ if (this.taskDirs().length) return "harbor";
3200
+ return this.isVerifiersV1 ? "verifiers-v1" : "verifiers";
3201
+ }
3202
+ };
3048
3203
  }
3049
3204
  });
3050
3205
 
@@ -3052,75 +3207,50 @@ var init_taskRun = __esm({
3052
3207
  var hub_exports = {};
3053
3208
  __export(hub_exports, {
3054
3209
  DEFAULT_BRANCH: () => DEFAULT_BRANCH,
3210
+ PushSlugConflict: () => PushSlugConflict,
3211
+ archiveMode: () => archiveMode,
3212
+ buildFileManifest: () => buildFileManifest,
3055
3213
  buildSourceTarball: () => buildSourceTarball,
3056
3214
  collectArchiveRelPaths: () => collectArchiveRelPaths,
3057
3215
  computeContentHash: () => computeContentHash,
3058
3216
  createRef: () => createRef,
3059
- isIncludedInContentHash: () => isIncludedInContentHash,
3217
+ extractBundle: () => extractBundle,
3060
3218
  localTreeState: () => localTreeState,
3061
3219
  propose: () => propose,
3062
3220
  pull: () => pull,
3063
3221
  push: () => push,
3064
3222
  pushSlug: () => pushSlug,
3065
3223
  readLocalMeta: () => readLocalMeta,
3224
+ resolvePushSlug: () => resolvePushSlug,
3066
3225
  snapshotTreeHashes: () => snapshotTreeHashes,
3067
3226
  writeLocalMeta: () => writeLocalMeta
3068
3227
  });
3069
- function isExcludedDirComponent(component) {
3070
- return component.startsWith(".") || ROOT_EXCLUDED_DIRS.has(component) || component.endsWith(".egg-info");
3071
- }
3072
- function isIncludedInContentHash(relPath, includeAllRootFiles) {
3073
- const parts2 = relPath.split("/");
3074
- const name = parts2[parts2.length - 1];
3075
- const dirParts = parts2.slice(0, -1);
3076
- if (name.startsWith(".")) return false;
3077
- if (parts2.includes("__pycache__")) return false;
3078
- if (dirParts.length === 0) {
3079
- return includeAllRootFiles || name === "README.md" || name === "pyproject.toml" || BROAD_ROOT_MARKERS.includes(name) || name.endsWith(".py");
3080
- }
3081
- return !dirParts.some(isExcludedDirComponent);
3082
- }
3083
3228
  function collectArchiveRelPaths(dir) {
3084
- if (fs2.existsSync(path2.join(dir, ".gitignore"))) {
3229
+ const world = WorldDir.open(dir);
3230
+ if (world.hasRootGitignore) {
3085
3231
  throw new Error(
3086
3232
  "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."
3087
3233
  );
3088
3234
  }
3089
- const out = [];
3090
- const includeAllRootFiles = BROAD_ROOT_MARKERS.some(
3091
- (marker) => fs2.existsSync(path2.join(dir, marker))
3092
- );
3093
- const walk = (rel) => {
3094
- const abs = rel ? path2.join(dir, rel) : dir;
3095
- for (const entry of fs2.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
3096
- const childRel = rel ? `${rel}/${entry.name}` : entry.name;
3097
- if (entry.isDirectory()) {
3098
- if (!isExcludedDirComponent(entry.name)) walk(childRel);
3099
- } else if (entry.isFile()) {
3100
- if (isIncludedInContentHash(childRel, includeAllRootFiles)) {
3101
- out.push(childRel);
3102
- }
3103
- }
3104
- }
3105
- };
3106
- walk("");
3107
- return out.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
3235
+ return world.archivePaths;
3108
3236
  }
3109
3237
  function computeContentHash(dir) {
3110
- const hasher = (0, import_node_crypto4.createHash)("sha256");
3111
- for (const relPath of collectArchiveRelPaths(dir)) {
3112
- hasher.update(Buffer.from(`file:${relPath}`, "utf-8"));
3113
- hasher.update(fs2.readFileSync(path2.join(dir, relPath)));
3114
- }
3115
- return hasher.digest("hex");
3238
+ collectArchiveRelPaths(dir);
3239
+ return WorldDir.open(dir).contentHash;
3116
3240
  }
3117
3241
  function octal(value, length) {
3118
- return Buffer.from(value.toString(8).padStart(length - 1, "0") + "\0", "ascii");
3242
+ return Buffer.from(
3243
+ value.toString(8).padStart(length - 1, "0") + "\0",
3244
+ "ascii"
3245
+ );
3246
+ }
3247
+ function archiveMode(mode) {
3248
+ return (mode & 73) !== 0 ? EXEC_MODE : FILE_MODE;
3119
3249
  }
3120
- function tarHeader(name, size) {
3250
+ function tarHeader(name, size, mode = FILE_MODE) {
3121
3251
  const header = Buffer.alloc(512);
3122
3252
  header.write(name, 0, 100, "utf-8");
3123
- octal(420, 8).copy(header, 100);
3253
+ octal(mode, 8).copy(header, 100);
3124
3254
  octal(0, 8).copy(header, 108);
3125
3255
  octal(0, 8).copy(header, 116);
3126
3256
  octal(size, 12).copy(header, 124);
@@ -3134,7 +3264,7 @@ function tarHeader(name, size) {
3134
3264
  header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, "ascii");
3135
3265
  return header;
3136
3266
  }
3137
- function tarMember(name, payload) {
3267
+ function tarMember(name, payload, mode) {
3138
3268
  const parts2 = [];
3139
3269
  const nameBytes = Buffer.byteLength(name, "utf-8");
3140
3270
  if (nameBytes > 100) {
@@ -3148,9 +3278,9 @@ function tarMember(name, payload) {
3148
3278
  parts2.push(lHeader, body);
3149
3279
  const pad2 = body.length % 512 === 0 ? 0 : 512 - body.length % 512;
3150
3280
  if (pad2) parts2.push(Buffer.alloc(pad2));
3151
- parts2.push(tarHeader(name.slice(0, 100), payload.length));
3281
+ parts2.push(tarHeader(name.slice(0, 100), payload.length, mode));
3152
3282
  } else {
3153
- parts2.push(tarHeader(name, payload.length));
3283
+ parts2.push(tarHeader(name, payload.length, mode));
3154
3284
  }
3155
3285
  parts2.push(payload);
3156
3286
  const pad = payload.length % 512 === 0 ? 0 : 512 - payload.length % 512;
@@ -3160,14 +3290,21 @@ function tarMember(name, payload) {
3160
3290
  function buildSourceTarball(dir) {
3161
3291
  const parts2 = [];
3162
3292
  for (const relPath of collectArchiveRelPaths(dir)) {
3163
- parts2.push(...tarMember(relPath, fs2.readFileSync(path2.join(dir, relPath))));
3293
+ const abs = path3.join(dir, relPath);
3294
+ parts2.push(
3295
+ ...tarMember(
3296
+ relPath,
3297
+ fs3.readFileSync(abs),
3298
+ archiveMode(fs3.statSync(abs).mode)
3299
+ )
3300
+ );
3164
3301
  }
3165
3302
  parts2.push(Buffer.alloc(1024));
3166
3303
  return (0, import_node_zlib.gzipSync)(Buffer.concat(parts2), { level: 9 });
3167
3304
  }
3168
3305
  function readLocalMeta(dir) {
3169
3306
  try {
3170
- const raw = fs2.readFileSync(path2.join(dir, META_RELPATH), "utf-8");
3307
+ const raw = fs3.readFileSync(path3.join(dir, META_RELPATH), "utf-8");
3171
3308
  const parsed = JSON.parse(raw);
3172
3309
  return parsed && typeof parsed === "object" ? parsed : null;
3173
3310
  } catch {
@@ -3175,19 +3312,31 @@ function readLocalMeta(dir) {
3175
3312
  }
3176
3313
  }
3177
3314
  function pushSlug(dir, explicit) {
3178
- if (explicit) return explicit;
3315
+ return resolvePushSlug(dir, { explicit }).slug;
3316
+ }
3317
+ function resolvePushSlug(dir, opts = {}) {
3179
3318
  const meta = readLocalMeta(dir);
3180
- if (typeof meta?.slug === "string" && meta.slug) return meta.slug;
3181
- return readPyproject(dir).name ?? path2.basename(path2.resolve(dir));
3319
+ const tracked = typeof meta?.slug === "string" && meta.slug ? meta.slug : null;
3320
+ const declared = Object.entries({ ...opts.declared ?? {}, "pyproject.toml": readPyproject(dir).name }).filter((entry) => typeof entry[1] === "string" && entry[1] !== "").map(([source, slug]) => ({ source, slug }));
3321
+ const base = { tracked, declared, conflict: null };
3322
+ if (opts.explicit) return { ...base, slug: opts.explicit, source: "--slug" };
3323
+ if (tracked) {
3324
+ const minted = (d) => tracked.startsWith(d.slug) && MINTED_SUFFIX.test(tracked.slice(d.slug.length));
3325
+ const conflict = declared.find((d) => d.slug !== tracked && !minted(d)) ?? null;
3326
+ return { ...base, slug: tracked, source: ".gateway metadata", conflict };
3327
+ }
3328
+ const first = declared[0];
3329
+ if (first) return { ...base, slug: first.slug, source: first.source };
3330
+ return { ...base, slug: path3.basename(path3.resolve(dir)), source: "directory name" };
3182
3331
  }
3183
3332
  function writeLocalMeta(dir, meta) {
3184
- fs2.mkdirSync(path2.join(dir, ".gateway"), { recursive: true });
3185
- fs2.writeFileSync(path2.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
3333
+ fs3.mkdirSync(path3.join(dir, ".gateway"), { recursive: true });
3334
+ fs3.writeFileSync(path3.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
3186
3335
  }
3187
3336
  function snapshotTreeHashes(dir) {
3188
3337
  const out = {};
3189
3338
  for (const relPath of collectArchiveRelPaths(dir)) {
3190
- out[relPath] = (0, import_node_crypto4.createHash)("sha256").update(fs2.readFileSync(path2.join(dir, relPath))).digest("hex");
3339
+ out[relPath] = (0, import_node_crypto5.createHash)("sha256").update(fs3.readFileSync(path3.join(dir, relPath))).digest("hex");
3191
3340
  }
3192
3341
  return out;
3193
3342
  }
@@ -3205,7 +3354,7 @@ function localTreeState(dir) {
3205
3354
  }
3206
3355
  function readPyproject(dir) {
3207
3356
  try {
3208
- const text2 = fs2.readFileSync(path2.join(dir, "pyproject.toml"), "utf-8");
3357
+ const text2 = fs3.readFileSync(path3.join(dir, "pyproject.toml"), "utf-8");
3209
3358
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
3210
3359
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
3211
3360
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -3215,39 +3364,27 @@ function readPyproject(dir) {
3215
3364
  }
3216
3365
  }
3217
3366
  function readWorldContractVersion(dir) {
3218
- for (const rel of ["schema/world.json", path2.join("server", "world", "schema", "world.json")]) {
3219
- try {
3220
- const parsed = JSON.parse(fs2.readFileSync(path2.join(dir, rel), "utf-8"));
3221
- const pkg = parsed["x-gateway"]?.package;
3222
- if (pkg?.version) return { name: pkg.id, version: pkg.version };
3223
- } catch {
3224
- }
3367
+ const world = WorldDir.open(dir);
3368
+ try {
3369
+ const parsed = JSON.parse(
3370
+ world.text(world.worldPath("schema/world.json")) ?? ""
3371
+ );
3372
+ const pkg = parsed["x-gateway"]?.package;
3373
+ if (pkg?.version) return { name: pkg.id, version: pkg.version };
3374
+ } catch {
3225
3375
  }
3226
3376
  return {};
3227
3377
  }
3228
3378
  function buildFileManifest(dir) {
3229
- const entries = [];
3230
- const walk = (rel) => {
3231
- const abs = rel ? path2.join(dir, rel) : dir;
3232
- for (const entry of fs2.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
3233
- const childRel = rel ? `${rel}/${entry.name}` : entry.name;
3234
- if (entry.isDirectory()) {
3235
- if (!SKIP_DIRS.has(entry.name) && !entry.name.endsWith(".egg-info")) {
3236
- walk(childRel);
3237
- }
3238
- } else if (entry.isFile()) {
3239
- const payload = fs2.readFileSync(path2.join(dir, childRel));
3240
- entries.push({
3241
- path: childRel,
3242
- isDirectory: false,
3243
- size: payload.length,
3244
- sha256: (0, import_node_crypto4.createHash)("sha256").update(payload).digest("hex")
3245
- });
3246
- }
3247
- }
3248
- };
3249
- walk("");
3250
- return entries;
3379
+ return collectArchiveRelPaths(dir).map((relPath) => {
3380
+ const payload = fs3.readFileSync(path3.join(dir, relPath));
3381
+ return {
3382
+ path: relPath,
3383
+ isDirectory: false,
3384
+ size: payload.length,
3385
+ sha256: (0, import_node_crypto5.createHash)("sha256").update(payload).digest("hex")
3386
+ };
3387
+ });
3251
3388
  }
3252
3389
  async function putObject(url, data, shaB64) {
3253
3390
  const resp = await fetch(url, {
@@ -3259,31 +3396,39 @@ async function putObject(url, data, shaB64) {
3259
3396
  body: new Uint8Array(data)
3260
3397
  });
3261
3398
  if (!resp.ok) {
3262
- throw new Error(`object PUT -> ${resp.status}: ${(await resp.text()).slice(0, 300)}`);
3399
+ throw new Error(
3400
+ `object PUT -> ${resp.status}: ${(await resp.text()).slice(0, 300)}`
3401
+ );
3263
3402
  }
3264
3403
  }
3265
3404
  async function push(dir, opts = {}) {
3266
3405
  const options = opts.options ?? {};
3267
- if (!fs2.statSync(dir).isDirectory()) throw new Error(`Not a directory: ${dir}`);
3406
+ if (!fs3.statSync(dir).isDirectory())
3407
+ throw new Error(`Not a directory: ${dir}`);
3268
3408
  const localMeta = readLocalMeta(dir) ?? {};
3269
3409
  const branch = opts.branch ?? localMeta.branch ?? DEFAULT_BRANCH;
3270
3410
  const baseVersionId = opts.baseVersionId ?? localMeta.versionId ?? null;
3411
+ const fileManifest = buildFileManifest(dir);
3412
+ if (fileManifest.length === 0)
3413
+ throw new Error(
3414
+ `Nothing to push: ${dir} has no files the archive rules keep (a world root marker, README.md, pyproject.toml, *.py, or any file below a subdirectory).`
3415
+ );
3271
3416
  const contentHash = computeContentHash(dir);
3272
3417
  const pyproject = readPyproject(dir);
3273
3418
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
3274
3419
  if (contractVersion) pyproject.version = contractVersion;
3275
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path2.basename(path2.resolve(dir));
3420
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path3.basename(path3.resolve(dir));
3421
+ const metadata = {
3422
+ ...opts.name !== void 0 ? { name: opts.name } : {},
3423
+ ...opts.description !== void 0 ? { description: opts.description } : {},
3424
+ ...opts.tags !== void 0 ? { tags: opts.tags } : {},
3425
+ ...opts.visibility ? { visibility: opts.visibility } : {}
3426
+ };
3276
3427
  const resolve10 = await apiRequest(
3277
3428
  options,
3278
3429
  "POST",
3279
3430
  "/api/public/benchmark-containers/resolve",
3280
- {
3281
- slug,
3282
- name: opts.name ?? pyproject.name ?? slug,
3283
- ...opts.description ? { description: opts.description } : {},
3284
- ...opts.tags?.length ? { tags: opts.tags } : {},
3285
- ...opts.visibility ? { visibility: opts.visibility } : {}
3286
- }
3431
+ { slug, ...metadata }
3287
3432
  );
3288
3433
  const tarball = buildSourceTarball(dir);
3289
3434
  const bundleSha256 = sha256b64(tarball);
@@ -3294,7 +3439,7 @@ async function push(dir, opts = {}) {
3294
3439
  bundleSizeBytes: tarball.length,
3295
3440
  ...pyproject.version ? { semanticVersion: pyproject.version } : {},
3296
3441
  pyprojectMeta: pyproject,
3297
- fileManifest: buildFileManifest(dir),
3442
+ fileManifest,
3298
3443
  branch,
3299
3444
  baseVersionId,
3300
3445
  ...opts.force ? { force: true } : {},
@@ -3302,12 +3447,18 @@ async function push(dir, opts = {}) {
3302
3447
  ...opts.author ? { author: opts.author } : {},
3303
3448
  ...opts.imageRef ? { imageRef: opts.imageRef } : {}
3304
3449
  });
3305
- if (version.uploadUrl) await putObject(version.uploadUrl, tarball, bundleSha256);
3450
+ if (version.uploadUrl)
3451
+ await putObject(version.uploadUrl, tarball, bundleSha256);
3306
3452
  const final = await apiRequest(
3307
3453
  options,
3308
3454
  "POST",
3309
3455
  `/api/public/benchmark-containers/versions/${version.versionId}/finalize`,
3310
- { branch, baseVersionId, ...opts.force ? { force: true } : {} }
3456
+ {
3457
+ branch,
3458
+ baseVersionId,
3459
+ ...opts.force ? { force: true } : {},
3460
+ ...metadata
3461
+ }
3311
3462
  );
3312
3463
  writeLocalMeta(dir, {
3313
3464
  ...localMeta,
@@ -3340,7 +3491,14 @@ function extractBundle(bundle, dir) {
3340
3491
  offset += 512;
3341
3492
  if (header.every((b) => b === 0)) break;
3342
3493
  const rawName = header.subarray(0, 100).toString("utf-8").replace(/\0.*$/, "");
3343
- const size = parseInt(header.subarray(124, 136).toString("ascii").replace(/\0.*$/, "").trim(), 8) || 0;
3494
+ const size = parseInt(
3495
+ header.subarray(124, 136).toString("ascii").replace(/\0.*$/, "").trim(),
3496
+ 8
3497
+ ) || 0;
3498
+ const mode = parseInt(
3499
+ header.subarray(100, 108).toString("ascii").replace(/\0.*$/, "").trim(),
3500
+ 8
3501
+ ) || FILE_MODE;
3344
3502
  const typeflag = header.subarray(156, 157).toString("ascii");
3345
3503
  const body = tar.subarray(offset, offset + size);
3346
3504
  offset += size + (size % 512 === 0 ? 0 : 512 - size % 512);
@@ -3352,9 +3510,10 @@ function extractBundle(bundle, dir) {
3352
3510
  longName = null;
3353
3511
  if (typeflag !== "0" && typeflag !== "") continue;
3354
3512
  if (name.startsWith("/") || name.split("/").includes("..")) continue;
3355
- const target = path2.join(dir, name);
3356
- fs2.mkdirSync(path2.dirname(target), { recursive: true });
3357
- fs2.writeFileSync(target, body);
3513
+ const target = path3.join(dir, name);
3514
+ fs3.mkdirSync(path3.dirname(target), { recursive: true });
3515
+ fs3.writeFileSync(target, body, { mode: archiveMode(mode) });
3516
+ fs3.chmodSync(target, archiveMode(mode));
3358
3517
  written++;
3359
3518
  }
3360
3519
  return written;
@@ -3370,13 +3529,13 @@ async function pull(slugRef, target, options = {}) {
3370
3529
  if (!details.downloadUrl) {
3371
3530
  throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
3372
3531
  }
3373
- let dir = target ?? path2.join(process.cwd(), slug);
3532
+ let dir = target ?? path3.join(process.cwd(), slug);
3374
3533
  if (!target) {
3375
3534
  let i = 1;
3376
3535
  const base = dir;
3377
- while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
3536
+ while (fs3.existsSync(dir)) dir = `${base}-${i++}`;
3378
3537
  }
3379
- fs2.mkdirSync(dir, { recursive: true });
3538
+ fs3.mkdirSync(dir, { recursive: true });
3380
3539
  const resp = await fetch(details.downloadUrl);
3381
3540
  if (!resp.ok) throw new Error(`bundle download -> ${resp.status}`);
3382
3541
  const bundle = Buffer.from(await resp.arrayBuffer());
@@ -3401,17 +3560,21 @@ async function pull(slugRef, target, options = {}) {
3401
3560
  async function propose(dir, opts) {
3402
3561
  const meta = readLocalMeta(dir);
3403
3562
  if (!meta?.slug || !meta.baseFiles) {
3404
- throw new Error("This dir has no pull/push base \u2014 run pull first, then edit, then propose.");
3563
+ throw new Error(
3564
+ "This dir has no pull/push base \u2014 run pull first, then edit, then propose."
3565
+ );
3405
3566
  }
3406
3567
  const state2 = localTreeState(dir);
3407
- if (state2.clean) throw new Error("Nothing to propose \u2014 the tree matches its base.");
3568
+ if (state2.clean)
3569
+ throw new Error("Nothing to propose \u2014 the tree matches its base.");
3408
3570
  const changes = state2.dirty.map((relPath) => {
3409
- const abs = path2.join(dir, relPath);
3410
- if (!fs2.existsSync(abs)) return { path: relPath, action: "delete" };
3571
+ const abs = path3.join(dir, relPath);
3572
+ if (!fs3.existsSync(abs))
3573
+ return { path: relPath, action: "delete" };
3411
3574
  return {
3412
3575
  path: relPath,
3413
3576
  action: "write",
3414
- newContent: fs2.readFileSync(abs, "utf-8")
3577
+ newContent: fs3.readFileSync(abs, "utf-8")
3415
3578
  };
3416
3579
  });
3417
3580
  const res = await apiRequest(
@@ -3437,30 +3600,33 @@ async function createRef(slug, name, kind, versionId, options = {}) {
3437
3600
  ...versionId ? { versionId } : {}
3438
3601
  });
3439
3602
  }
3440
- var import_node_crypto4, import_node_zlib, fs2, path2, DEFAULT_BRANCH, META_RELPATH, SKIP_DIRS, ROOT_EXCLUDED_DIRS, BROAD_ROOT_MARKERS, sha256b64;
3603
+ var import_node_crypto5, import_node_zlib, fs3, path3, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, EXEC_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64;
3441
3604
  var init_hub = __esm({
3442
3605
  "src/worlds/hub.ts"() {
3443
3606
  "use strict";
3444
- import_node_crypto4 = require("crypto");
3607
+ import_node_crypto5 = require("crypto");
3445
3608
  import_node_zlib = require("zlib");
3446
- fs2 = __toESM(require("fs"));
3447
- path2 = __toESM(require("path"));
3609
+ fs3 = __toESM(require("fs"));
3610
+ path3 = __toESM(require("path"));
3448
3611
  init_worlds();
3612
+ init_worldDir();
3449
3613
  DEFAULT_BRANCH = "main";
3450
- META_RELPATH = path2.join(".gateway", ".env-metadata.json");
3451
- SKIP_DIRS = /* @__PURE__ */ new Set([
3452
- ".git",
3453
- "__pycache__",
3454
- "node_modules",
3455
- ".venv",
3456
- ".mypy_cache",
3457
- "dist",
3458
- "build",
3459
- "outputs"
3460
- ]);
3461
- ROOT_EXCLUDED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
3462
- BROAD_ROOT_MARKERS = ["gateway-env.toml", "gateway-api-world.toml"];
3463
- sha256b64 = (data) => (0, import_node_crypto4.createHash)("sha256").update(data).digest("base64");
3614
+ META_RELPATH = path3.join(".gateway", ".env-metadata.json");
3615
+ FILE_MODE = 420;
3616
+ EXEC_MODE = 493;
3617
+ MINTED_SUFFIX = /^-\d+$/;
3618
+ PushSlugConflict = class extends Error {
3619
+ constructor(dir, tracked, declared) {
3620
+ super(
3621
+ `${dir} tracks slug \`${tracked}\` (${META_RELPATH}, from an earlier push) but its ${declared.source} says \`${declared.slug}\`. Nothing was pushed. Pass --slug ${declared.slug} to push there (and retrack the dir), --slug ${tracked} to keep pushing to \`${tracked}\`, or delete ${META_RELPATH} to forget the earlier push.`
3622
+ );
3623
+ this.dir = dir;
3624
+ this.tracked = tracked;
3625
+ this.declared = declared;
3626
+ this.name = "PushSlugConflict";
3627
+ }
3628
+ };
3629
+ sha256b64 = (data) => (0, import_node_crypto5.createHash)("sha256").update(data).digest("base64");
3464
3630
  }
3465
3631
  });
3466
3632
 
@@ -3976,15 +4142,15 @@ function applyTransforms(value, chain) {
3976
4142
  for (const step of steps) out = step(out);
3977
4143
  return out;
3978
4144
  }
3979
- var import_node_crypto5, WHITESPACE, WHITESPACE_EDGES, WHITESPACE_RUN, NOT_DIGIT, STRIP_PREFIX, trim, emailLocalPart, emailDomain, stripPrefix, hex, SIMPLE, TRANSFORMS, text;
4145
+ var import_node_crypto6, WHITESPACE, WHITESPACE_EDGES, WHITESPACE_RUN, NOT_DIGIT, STRIP_PREFIX, trim, emailLocalPart, emailDomain, stripPrefix, hex, SIMPLE, TRANSFORMS, text;
3980
4146
  var init_transforms = __esm({
3981
4147
  "src/worlds/transforms.ts"() {
3982
4148
  "use strict";
3983
- import_node_crypto5 = require("crypto");
4149
+ import_node_crypto6 = require("crypto");
3984
4150
  init_json();
3985
4151
  WHITESPACE = " \n\r\f\v";
3986
- WHITESPACE_EDGES = /^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$/g;
3987
- WHITESPACE_RUN = /[ \t\n\r\f\v]+/g;
4152
+ WHITESPACE_EDGES = new RegExp(`^[${WHITESPACE}]+|[${WHITESPACE}]+$`, "g");
4153
+ WHITESPACE_RUN = new RegExp(`[${WHITESPACE}]+`, "g");
3988
4154
  NOT_DIGIT = /[^0-9]/g;
3989
4155
  STRIP_PREFIX = "strip_prefix";
3990
4156
  trim = (value) => value.replace(WHITESPACE_EDGES, "");
@@ -3997,7 +4163,7 @@ var init_transforms = __esm({
3997
4163
  return at === -1 ? "" : value.slice(at + 1).toLowerCase();
3998
4164
  };
3999
4165
  stripPrefix = (prefix) => (value) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
4000
- hex = (algorithm, value) => (0, import_node_crypto5.createHash)(algorithm).update(value, "utf8").digest("hex");
4166
+ hex = (algorithm, value) => (0, import_node_crypto6.createHash)(algorithm).update(value, "utf8").digest("hex");
4001
4167
  SIMPLE = {
4002
4168
  lower: (value) => value.toLowerCase(),
4003
4169
  upper: (value) => value.toUpperCase(),
@@ -4029,21 +4195,21 @@ function pyText(value) {
4029
4195
  return pyFloatRepr(value);
4030
4196
  }
4031
4197
  function hashForm(slug, plaintext) {
4032
- return "sha256:" + (0, import_node_crypto6.createHash)("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
4198
+ return "sha256:" + (0, import_node_crypto7.createHash)("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
4033
4199
  }
4034
4200
  function preserveForm(slug, plaintext) {
4035
4201
  const kind = typeof plaintext === "string" ? "str" : isInt(plaintext) ? "int" : "float";
4036
- const shaped = shapeText(slug, pyText(plaintext), kind);
4037
- return kind === "str" ? shaped : Number(shaped);
4202
+ const shaped2 = shapeText(slug, pyText(plaintext), kind);
4203
+ return kind === "str" ? shaped2 : Number(shaped2);
4038
4204
  }
4039
4205
  function shapeText(slug, text2, kind) {
4040
4206
  const seed = Buffer.from(`${slug}:${text2}`, "utf8");
4041
- let digest = (0, import_node_crypto6.createHash)("sha256").update(seed).digest();
4207
+ let digest = (0, import_node_crypto7.createHash)("sha256").update(seed).digest();
4042
4208
  let cursor = 0;
4043
4209
  const out = [];
4044
4210
  for (const char of text2) {
4045
4211
  if (cursor === digest.length) {
4046
- digest = (0, import_node_crypto6.createHash)("sha256").update(Buffer.concat([digest, seed])).digest();
4212
+ digest = (0, import_node_crypto7.createHash)("sha256").update(Buffer.concat([digest, seed])).digest();
4047
4213
  cursor = 0;
4048
4214
  }
4049
4215
  const byte = digest[cursor];
@@ -4080,11 +4246,11 @@ function storedForms(world, field, plaintext, transform) {
4080
4246
  if (policy !== null) return [roundForm(plaintext, world.redact.round[field])];
4081
4247
  return [plaintext];
4082
4248
  }
4083
- var import_node_crypto6, isInt;
4249
+ var import_node_crypto7, isInt;
4084
4250
  var init_redaction = __esm({
4085
4251
  "src/worlds/redaction.ts"() {
4086
4252
  "use strict";
4087
- import_node_crypto6 = require("crypto");
4253
+ import_node_crypto7 = require("crypto");
4088
4254
  init_json();
4089
4255
  init_transforms();
4090
4256
  isInt = (value) => typeof value === "number" && Number.isInteger(value);
@@ -4092,8 +4258,9 @@ var init_redaction = __esm({
4092
4258
  });
4093
4259
 
4094
4260
  // src/worlds/query.ts
4095
- function queryRowsSearch(entity, opts = {}) {
4261
+ function queryRowsSearch(entity, opts = {}, ref) {
4096
4262
  const search = new URLSearchParams();
4263
+ if (ref) search.set("ref", ref);
4097
4264
  search.set("entity", entity);
4098
4265
  if (opts.where && Object.keys(opts.where).length) search.set("where", JSON.stringify(opts.where));
4099
4266
  if (opts.limit !== void 0) search.set("limit", String(opts.limit));
@@ -4103,17 +4270,15 @@ function queryRowsSearch(entity, opts = {}) {
4103
4270
  if (opts.compare) search.set("compare", "true");
4104
4271
  return `?${search.toString()}`;
4105
4272
  }
4106
- async function queryRows(slug, entity, opts = {}, options = {}) {
4107
- if (slug.includes("@"))
4108
- throw new Error(
4109
- `queryRows reads a world's head version: pass the slug without @ref (got ${slug})`
4110
- );
4273
+ async function queryRows(slugRef, entity, opts = {}, options = {}) {
4274
+ const [slug, ref] = slugRef.split("@", 2);
4275
+ if (!slug || ref === "") throw new Error(`queryRows needs a slug, optionally slug@ref (got ${slugRef})`);
4111
4276
  if (!entity) throw new Error("queryRows needs an entity");
4112
4277
  if (opts.compare && !opts.across) throw new Error("queryRows: compare needs across");
4113
4278
  return apiRequest(
4114
4279
  options,
4115
4280
  "GET",
4116
- `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts)}`
4281
+ `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
4117
4282
  );
4118
4283
  }
4119
4284
  var init_query = __esm({
@@ -4169,11 +4334,20 @@ var init_router = __esm({
4169
4334
  });
4170
4335
 
4171
4336
  // src/cli/config.ts
4337
+ function transportDetail(error, timeoutMs) {
4338
+ if (error instanceof Error && error.name === "AbortError") return `timed out after ${Math.round(timeoutMs / 1e3)}s`;
4339
+ const cause = error instanceof Error && error.cause instanceof Error ? error.cause : void 0;
4340
+ const code = cause && "code" in cause && typeof cause.code === "string" ? `${cause.code}: ` : "";
4341
+ const message = cause?.message ?? (error instanceof Error ? error.message : String(error));
4342
+ return `${code}${message}`;
4343
+ }
4172
4344
  async function api(creds, method, route, body, init3 = {}) {
4173
4345
  const controller = new AbortController();
4174
- const timer = setTimeout(() => controller.abort(), init3.timeoutMs ?? 12e4);
4346
+ const timeoutMs = init3.timeoutMs ?? 12e4;
4347
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
4348
+ let res;
4175
4349
  try {
4176
- const res = await fetch(`${creds.host}${route}`, {
4350
+ res = await fetch(`${creds.host}${route}`, {
4177
4351
  method,
4178
4352
  headers: {
4179
4353
  "content-type": "application/json",
@@ -4183,6 +4357,11 @@ async function api(creds, method, route, body, init3 = {}) {
4183
4357
  ...body === void 0 ? {} : { body: JSON.stringify(body) },
4184
4358
  signal: controller.signal
4185
4359
  });
4360
+ } catch (error) {
4361
+ clearTimeout(timer);
4362
+ throw new UnreachableError(creds.host, method, route, transportDetail(error, timeoutMs));
4363
+ }
4364
+ try {
4186
4365
  const text2 = await res.text();
4187
4366
  if (!res.ok) throw new ApiError(res.status, method, route, text2);
4188
4367
  return text2 ? JSON.parse(text2) : {};
@@ -4190,13 +4369,13 @@ async function api(creds, method, route, body, init3 = {}) {
4190
4369
  clearTimeout(timer);
4191
4370
  }
4192
4371
  }
4193
- var fs3, os, path3, ApiError;
4372
+ var fs4, os, path4, ApiError, UnreachableError;
4194
4373
  var init_config = __esm({
4195
4374
  "src/cli/config.ts"() {
4196
4375
  "use strict";
4197
- fs3 = __toESM(require("fs"));
4376
+ fs4 = __toESM(require("fs"));
4198
4377
  os = __toESM(require("os"));
4199
- path3 = __toESM(require("path"));
4378
+ path4 = __toESM(require("path"));
4200
4379
  init_router();
4201
4380
  ApiError = class extends CliError {
4202
4381
  constructor(status, method, route, text2) {
@@ -4207,6 +4386,15 @@ var init_config = __esm({
4207
4386
  this.text = text2;
4208
4387
  }
4209
4388
  };
4389
+ UnreachableError = class extends CliError {
4390
+ constructor(host, method, route, detail) {
4391
+ super(`${method} ${route}: no answer from ${host} (${detail}). Check the host and the network, then retry.`, 1);
4392
+ this.host = host;
4393
+ this.method = method;
4394
+ this.route = route;
4395
+ this.detail = detail;
4396
+ }
4397
+ };
4210
4398
  }
4211
4399
  });
4212
4400
 
@@ -4225,16 +4413,16 @@ function runtimeRoot() {
4225
4413
  const candidates = [];
4226
4414
  if (process.env.GATEWAY_RUNTIME_PATH)
4227
4415
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
4228
- const entry = process.argv[1] && fs4.existsSync(process.argv[1]) ? path4.dirname(fs4.realpathSync(process.argv[1])) : null;
4416
+ const entry = process.argv[1] && fs5.existsSync(process.argv[1]) ? path5.dirname(fs5.realpathSync(process.argv[1])) : null;
4229
4417
  if (entry) {
4230
- candidates.push(path4.resolve(entry, "..", "runtime"));
4231
- candidates.push(path4.resolve(entry, "..", "..", "runtime"));
4232
- candidates.push(path4.resolve(entry, "..", "..", "sdk", "src"));
4418
+ candidates.push(path5.resolve(entry, "..", "runtime"));
4419
+ candidates.push(path5.resolve(entry, "..", "..", "runtime"));
4420
+ candidates.push(path5.resolve(entry, "..", "..", "sdk", "src"));
4233
4421
  }
4234
- candidates.push(path4.resolve(process.cwd(), "runtime"));
4422
+ candidates.push(path5.resolve(process.cwd(), "runtime"));
4235
4423
  for (const root of candidates)
4236
- if (fs4.existsSync(
4237
- path4.join(root, "gatewaysdk", "environment", "schema", "platform.py")
4424
+ if (fs5.existsSync(
4425
+ path5.join(root, "gatewaysdk", "environment", "schema", "platform.py")
4238
4426
  ))
4239
4427
  return root;
4240
4428
  return null;
@@ -4375,14 +4563,14 @@ async function localBridge(request, local, opts = {}) {
4375
4563
  child.stdin.end(input);
4376
4564
  });
4377
4565
  }
4378
- var import_node_child_process, fs4, net, path4, MIN_PYTHON, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
4566
+ var import_node_child_process, fs5, net, path5, MIN_PYTHON, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
4379
4567
  var init_local = __esm({
4380
4568
  "src/cli/runtime/local.ts"() {
4381
4569
  "use strict";
4382
4570
  import_node_child_process = require("child_process");
4383
- fs4 = __toESM(require("fs"));
4571
+ fs5 = __toESM(require("fs"));
4384
4572
  net = __toESM(require("net"));
4385
- path4 = __toESM(require("path"));
4573
+ path5 = __toESM(require("path"));
4386
4574
  init_router();
4387
4575
  init_config();
4388
4576
  MIN_PYTHON = [3, 11];
@@ -4410,22 +4598,22 @@ function isBuildOutput(relPath) {
4410
4598
  return BUILD_OUTPUT.test(relPath);
4411
4599
  }
4412
4600
  function readWorldFiles(dir, opts = {}) {
4413
- if (!fs5.existsSync(dir) || !fs5.statSync(dir).isDirectory())
4601
+ if (!fs6.existsSync(dir) || !fs6.statSync(dir).isDirectory())
4414
4602
  throw new CliError(`Not a directory: ${dir}`);
4415
4603
  const out = [];
4416
4604
  const walk = (rel) => {
4417
- const abs = rel ? path5.join(dir, rel) : dir;
4418
- const entries = fs5.readdirSync(abs, { withFileTypes: true });
4605
+ const abs = rel ? path6.join(dir, rel) : dir;
4606
+ const entries = fs6.readdirSync(abs, { withFileTypes: true });
4419
4607
  entries.sort((a, b) => a.name.localeCompare(b.name));
4420
4608
  for (const entry of entries) {
4421
4609
  const childRel = rel ? `${rel}/${entry.name}` : entry.name;
4422
4610
  if (entry.isDirectory()) {
4423
- if (!SKIP_DIRS2.has(entry.name)) walk(childRel);
4611
+ if (!SKIP_DIRS.has(entry.name)) walk(childRel);
4424
4612
  } else if (entry.isFile()) {
4425
4613
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
4426
4614
  out.push({
4427
4615
  path: childRel,
4428
- content: fs5.readFileSync(path5.join(dir, childRel)).toString("base64")
4616
+ content: fs6.readFileSync(path6.join(dir, childRel)).toString("base64")
4429
4617
  });
4430
4618
  }
4431
4619
  }
@@ -4440,19 +4628,19 @@ function assertSafeRelPath(rel) {
4440
4628
  }
4441
4629
  function writeWorldFiles(dir, files, opts = {}) {
4442
4630
  for (const file of files) assertSafeRelPath(file.path);
4443
- if (opts.clean && fs5.existsSync(dir)) {
4631
+ if (opts.clean && fs6.existsSync(dir)) {
4444
4632
  const keep = new Set(files.map((f) => f.path));
4445
4633
  for (const existing of readWorldFiles(dir)) {
4446
- if (!keep.has(existing.path)) fs5.rmSync(path5.join(dir, existing.path));
4634
+ if (!keep.has(existing.path)) fs6.rmSync(path6.join(dir, existing.path));
4447
4635
  }
4448
4636
  pruneEmptyDirs(dir);
4449
4637
  }
4450
- fs5.mkdirSync(dir, { recursive: true });
4638
+ fs6.mkdirSync(dir, { recursive: true });
4451
4639
  const written = [];
4452
4640
  for (const file of files) {
4453
- const target = path5.join(dir, file.path);
4454
- fs5.mkdirSync(path5.dirname(target), { recursive: true });
4455
- fs5.writeFileSync(target, Buffer.from(file.content, "base64"));
4641
+ const target = path6.join(dir, file.path);
4642
+ fs6.mkdirSync(path6.dirname(target), { recursive: true });
4643
+ fs6.writeFileSync(target, Buffer.from(file.content, "base64"));
4456
4644
  written.push(file.path);
4457
4645
  }
4458
4646
  return written;
@@ -4460,14 +4648,14 @@ function writeWorldFiles(dir, files, opts = {}) {
4460
4648
  function pruneEmptyDirs(root) {
4461
4649
  const walk = (abs) => {
4462
4650
  let empty = true;
4463
- for (const entry of fs5.readdirSync(abs, { withFileTypes: true })) {
4464
- const child = path5.join(abs, entry.name);
4651
+ for (const entry of fs6.readdirSync(abs, { withFileTypes: true })) {
4652
+ const child = path6.join(abs, entry.name);
4465
4653
  if (entry.isDirectory()) {
4466
- if (SKIP_DIRS2.has(entry.name)) {
4654
+ if (SKIP_DIRS.has(entry.name)) {
4467
4655
  empty = false;
4468
4656
  continue;
4469
4657
  }
4470
- if (walk(child)) fs5.rmdirSync(child);
4658
+ if (walk(child)) fs6.rmdirSync(child);
4471
4659
  else empty = false;
4472
4660
  } else empty = false;
4473
4661
  }
@@ -4531,15 +4719,15 @@ async function runtime(creds, request, opts = {}) {
4531
4719
  clearTimeout(timer);
4532
4720
  }
4533
4721
  }
4534
- var fs5, path5, SKIP_DIRS2, BUILD_OUTPUT, BridgeRejected;
4722
+ var fs6, path6, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected;
4535
4723
  var init_client = __esm({
4536
4724
  "src/cli/runtime/client.ts"() {
4537
4725
  "use strict";
4538
- fs5 = __toESM(require("fs"));
4539
- path5 = __toESM(require("path"));
4726
+ fs6 = __toESM(require("fs"));
4727
+ path6 = __toESM(require("path"));
4540
4728
  init_router();
4541
4729
  init_local();
4542
- SKIP_DIRS2 = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
4730
+ SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
4543
4731
  BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
4544
4732
  BridgeRejected = class extends CliError {
4545
4733
  constructor(detail) {
@@ -4586,7 +4774,7 @@ function dropBuildOutputs(dir) {
4586
4774
  const dropped = [];
4587
4775
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
4588
4776
  if (!BUILD_OUTPUT2.test(file.path)) continue;
4589
- fs6.rmSync(path6.join(dir, file.path), { force: true });
4777
+ fs7.rmSync(path7.join(dir, file.path), { force: true });
4590
4778
  dropped.push(file.path);
4591
4779
  }
4592
4780
  return dropped;
@@ -4597,6 +4785,7 @@ async function checkLocalWorldData(dir, rows, creds, opts = {}) {
4597
4785
  files: readWorldFiles(dir),
4598
4786
  rows,
4599
4787
  mode: "append",
4788
+ dry_run: true,
4600
4789
  ...opts.redact && opts.redact !== "off" ? { redact: opts.redact } : {}
4601
4790
  });
4602
4791
  return {
@@ -4652,7 +4841,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
4652
4841
  const payload = await localBridge(
4653
4842
  {
4654
4843
  command: "query",
4655
- world: path6.resolve(dir),
4844
+ world: path7.resolve(dir),
4656
4845
  entity,
4657
4846
  across: true,
4658
4847
  ...opts.compare ? { compare: true } : {},
@@ -4699,12 +4888,12 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
4699
4888
  ...res.batch !== void 0 ? { batch: res.batch } : {}
4700
4889
  };
4701
4890
  }
4702
- var fs6, path6, BUILD_OUTPUT2;
4891
+ var fs7, path7, BUILD_OUTPUT2;
4703
4892
  var init_data = __esm({
4704
4893
  "src/cli/runtime/data.ts"() {
4705
4894
  "use strict";
4706
- fs6 = __toESM(require("fs"));
4707
- path6 = __toESM(require("path"));
4895
+ fs7 = __toESM(require("fs"));
4896
+ path7 = __toESM(require("path"));
4708
4897
  init_router();
4709
4898
  init_client();
4710
4899
  init_local();
@@ -4747,27 +4936,27 @@ function typescriptPrefix() {
4747
4936
  }
4748
4937
  }
4749
4938
  function command(script, wrapperDir) {
4750
- const suffix = path7.extname(script).toLowerCase();
4939
+ const suffix = path8.extname(script).toLowerCase();
4751
4940
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
4752
4941
  if (NODE_SUFFIXES.includes(suffix)) {
4753
- const wrapper = path7.join(wrapperDir, "transform-wrapper.mjs");
4754
- fs7.writeFileSync(wrapper, NODE_WRAPPER);
4942
+ const wrapper = path8.join(wrapperDir, "transform-wrapper.mjs");
4943
+ fs8.writeFileSync(wrapper, NODE_WRAPPER);
4755
4944
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
4756
4945
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
4757
4946
  }
4758
- throw new CliError(`${path7.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
4947
+ throw new CliError(`${path8.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
4759
4948
  }
4760
4949
  async function runTransform(script, records, opts = {}) {
4761
- if (!fs7.existsSync(script) || !fs7.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
4762
- const resolved = path7.resolve(script);
4763
- const name = path7.basename(script);
4950
+ if (!fs8.existsSync(script) || !fs8.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
4951
+ const resolved = path8.resolve(script);
4952
+ const name = path8.basename(script);
4764
4953
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
4765
- const scratch = fs7.mkdtempSync(path7.join(os2.tmpdir(), "gateway-transform-"));
4954
+ const scratch = fs8.mkdtempSync(path8.join(os2.tmpdir(), "gateway-transform-"));
4766
4955
  try {
4767
4956
  const { file, args } = command(resolved, scratch);
4768
4957
  const env = Object.fromEntries(Object.entries(process.env).filter(([key2]) => !SCRUBBED.test(key2)));
4769
4958
  const done = await new Promise((resolve10, reject) => {
4770
- const child = (0, import_node_child_process2.spawn)(file, args, { cwd: path7.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
4959
+ const child = (0, import_node_child_process2.spawn)(file, args, { cwd: path8.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
4771
4960
  let stdout = "";
4772
4961
  let stderr = "";
4773
4962
  let timedOut = false;
@@ -4798,21 +4987,21 @@ async function runTransform(script, records, opts = {}) {
4798
4987
  }
4799
4988
  return { rows: checkRows(value, name), stderr: done.stderr };
4800
4989
  } finally {
4801
- fs7.rmSync(scratch, { recursive: true, force: true });
4990
+ fs8.rmSync(scratch, { recursive: true, force: true });
4802
4991
  }
4803
4992
  }
4804
4993
  async function transformRecords(transform, records, opts = {}) {
4805
4994
  if (typeof transform === "function") return { rows: checkRows(await transform(records), "transform(records)"), stderr: "" };
4806
4995
  return runTransform(transform, records, opts);
4807
4996
  }
4808
- var import_node_child_process2, fs7, os2, path7, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
4997
+ var import_node_child_process2, fs8, os2, path8, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
4809
4998
  var init_transform = __esm({
4810
4999
  "src/cli/platform/worldData/transform.ts"() {
4811
5000
  "use strict";
4812
5001
  import_node_child_process2 = require("child_process");
4813
- fs7 = __toESM(require("fs"));
5002
+ fs8 = __toESM(require("fs"));
4814
5003
  os2 = __toESM(require("os"));
4815
- path7 = __toESM(require("path"));
5004
+ path8 = __toESM(require("path"));
4816
5005
  import_node_module = require("module");
4817
5006
  init_router();
4818
5007
  import_meta = {};
@@ -4896,11 +5085,16 @@ if (fn) {
4896
5085
  });
4897
5086
 
4898
5087
  // src/cli/platform/worldData/batches.ts
5088
+ function isSettled(status) {
5089
+ if (!TERMINAL_STATES.has(status.state)) return false;
5090
+ if (status.state !== "applied") return true;
5091
+ return status.dryRun === true || status.publish !== "now" || !status.snapshotId;
5092
+ }
4899
5093
  function uploadHeadersFor(gz) {
4900
5094
  return {
4901
5095
  "content-type": "application/gzip",
4902
5096
  "content-length": String(gz.length),
4903
- "x-amz-checksum-sha256": (0, import_node_crypto7.createHash)("sha256").update(gz).digest("base64")
5097
+ "x-amz-checksum-sha256": (0, import_node_crypto8.createHash)("sha256").update(gz).digest("base64")
4904
5098
  };
4905
5099
  }
4906
5100
  async function forEachLimited(items, limit, fn) {
@@ -4937,7 +5131,7 @@ async function waitForBatch(client2, batchId, opts = {}) {
4937
5131
  opts.onPhase?.(phase);
4938
5132
  last = phase;
4939
5133
  }
4940
- if (TERMINAL_STATES.has(status.state)) return status;
5134
+ if (isSettled(status)) return status;
4941
5135
  await sleep3(delay);
4942
5136
  delay = Math.min(1e4, Math.round(delay * 1.5));
4943
5137
  }
@@ -4946,15 +5140,20 @@ function phaseLine(status) {
4946
5140
  const phase = status.progress?.phase;
4947
5141
  return phase ? `${status.state} (${phase})` : status.state;
4948
5142
  }
4949
- var import_node_crypto7, fs8, TERMINAL_STATES, sleepMs, WorldDataApi;
5143
+ var import_node_crypto8, fs9, TERMINAL_STATES, sleepMs, WorldDataApi;
4950
5144
  var init_batches = __esm({
4951
5145
  "src/cli/platform/worldData/batches.ts"() {
4952
5146
  "use strict";
4953
- import_node_crypto7 = require("crypto");
4954
- fs8 = __toESM(require("fs"));
5147
+ import_node_crypto8 = require("crypto");
5148
+ fs9 = __toESM(require("fs"));
4955
5149
  init_config();
4956
5150
  init_router();
4957
- TERMINAL_STATES = /* @__PURE__ */ new Set(["applied", "refused", "published", "failed"]);
5151
+ TERMINAL_STATES = /* @__PURE__ */ new Set([
5152
+ "applied",
5153
+ "refused",
5154
+ "published",
5155
+ "failed"
5156
+ ]);
4958
5157
  sleepMs = (ms) => new Promise((resolve10) => setTimeout(resolve10, ms));
4959
5158
  WorldDataApi = class {
4960
5159
  constructor(creds, slug, sleep3 = sleepMs) {
@@ -4968,22 +5167,61 @@ var init_batches = __esm({
4968
5167
  return api(this.creds, "POST", `${this.base}/batches`, body);
4969
5168
  }
4970
5169
  register(batchId, chunk) {
4971
- return api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks`, {
5170
+ return api(
5171
+ this.creds,
5172
+ "POST",
5173
+ `${this.base}/batches/${batchId}/chunks`,
5174
+ {
5175
+ ordinal: chunk.ordinal,
5176
+ digest: chunk.digest,
5177
+ bytes: chunk.bytes,
5178
+ rows: chunk.rows,
5179
+ ...chunk.entity ? { entity: chunk.entity } : {}
5180
+ }
5181
+ );
5182
+ }
5183
+ /**
5184
+ * Stage a project-drive rows file as one chunk, on the platform: the server
5185
+ * streams it out of drive storage into chunk storage and answers with the
5186
+ * digest and gzipped size it derived. No bytes pass through this process.
5187
+ */
5188
+ async stageDrive(batchId, chunk) {
5189
+ const staged = await api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks`, {
4972
5190
  ordinal: chunk.ordinal,
4973
- digest: chunk.digest,
4974
- bytes: chunk.bytes,
4975
- rows: chunk.rows,
5191
+ drive: chunk.path,
4976
5192
  ...chunk.entity ? { entity: chunk.entity } : {}
4977
5193
  });
5194
+ if (!staged.uploaded || !staged.digest || staged.bytes === void 0 || staged.rows === void 0)
5195
+ throw new CliError(
5196
+ `the server staged drive:${chunk.path} as chunk ${chunk.ordinal} without its digest, size and rows`,
5197
+ 1
5198
+ );
5199
+ return {
5200
+ ordinal: chunk.ordinal,
5201
+ digest: staged.digest,
5202
+ bytes: staged.bytes,
5203
+ rows: staged.rows,
5204
+ entity: chunk.entity,
5205
+ chunkId: staged.chunkId,
5206
+ uploaded: true
5207
+ };
4978
5208
  }
4979
5209
  markUploaded(batchId, chunkId) {
4980
- return api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks/${chunkId}/uploaded`);
5210
+ return api(
5211
+ this.creds,
5212
+ "POST",
5213
+ `${this.base}/batches/${batchId}/chunks/${chunkId}/uploaded`
5214
+ );
4981
5215
  }
4982
5216
  complete(batchId) {
4983
5217
  return api(this.creds, "POST", `${this.base}/batches/${batchId}/complete`);
4984
5218
  }
4985
5219
  get(batchId) {
4986
- return api(this.creds, "GET", `${this.base}/batches/${batchId}`);
5220
+ return api(
5221
+ this.creds,
5222
+ "GET",
5223
+ `${this.base}/batches/${batchId}`
5224
+ );
4987
5225
  }
4988
5226
  list(limit, cursor) {
4989
5227
  const params = new URLSearchParams();
@@ -4993,7 +5231,12 @@ var init_batches = __esm({
4993
5231
  return api(this.creds, "GET", `${this.base}/batches${q ? `?${q}` : ""}`);
4994
5232
  }
4995
5233
  publish(message) {
4996
- return api(this.creds, "POST", `${this.base}/publish`, message ? { message } : {});
5234
+ return api(
5235
+ this.creds,
5236
+ "POST",
5237
+ `${this.base}/publish`,
5238
+ message ? { message } : {}
5239
+ );
4997
5240
  }
4998
5241
  /**
4999
5242
  * PUT the gzipped chunk to its presigned URL with exactly the headers the
@@ -5005,11 +5248,23 @@ var init_batches = __esm({
5005
5248
  const delays = [500, 1500, 4e3];
5006
5249
  for (let attempt = 0; ; attempt++) {
5007
5250
  try {
5008
- const res = await fetch(uploadUrl, { method: "PUT", body: new Uint8Array(gz), headers });
5251
+ const res = await fetch(uploadUrl, {
5252
+ method: "PUT",
5253
+ body: new Uint8Array(gz),
5254
+ headers
5255
+ });
5009
5256
  if (res.ok) return;
5010
- if (res.status < 500 || attempt >= delays.length) throw new CliError(`chunk upload -> ${res.status}: ${(await res.text()).slice(0, 300)}`, 1);
5257
+ if (res.status < 500 || attempt >= delays.length)
5258
+ throw new CliError(
5259
+ `chunk upload -> ${res.status}: ${(await res.text()).slice(0, 300)}`,
5260
+ 1
5261
+ );
5011
5262
  } catch (error) {
5012
- if (error instanceof CliError || attempt >= delays.length) throw error instanceof CliError ? error : new CliError(`chunk upload failed: ${error.message}`, 1);
5263
+ if (error instanceof CliError || attempt >= delays.length)
5264
+ throw error instanceof CliError ? error : new CliError(
5265
+ `chunk upload failed: ${error.message}`,
5266
+ 1
5267
+ );
5013
5268
  }
5014
5269
  await this.sleep(delays[attempt]);
5015
5270
  }
@@ -5018,11 +5273,23 @@ var init_batches = __esm({
5018
5273
  async upload(batchId, chunk) {
5019
5274
  const registered = await this.register(batchId, chunk);
5020
5275
  if (!registered.uploaded) {
5021
- if (!registered.uploadUrl) throw new CliError(`the server registered chunk ${chunk.ordinal} without an upload URL`, 1);
5276
+ if (!registered.uploadUrl)
5277
+ throw new CliError(
5278
+ `the server registered chunk ${chunk.ordinal} without an upload URL`,
5279
+ 1
5280
+ );
5022
5281
  await this.put(registered.uploadUrl, chunk.gz, registered.uploadHeaders);
5023
5282
  await this.markUploaded(batchId, registered.chunkId);
5024
5283
  }
5025
- return { ordinal: chunk.ordinal, digest: chunk.digest, bytes: chunk.bytes, rows: chunk.rows, entity: chunk.entity, chunkId: registered.chunkId, uploaded: true };
5284
+ return {
5285
+ ordinal: chunk.ordinal,
5286
+ digest: chunk.digest,
5287
+ bytes: chunk.bytes,
5288
+ rows: chunk.rows,
5289
+ entity: chunk.entity,
5290
+ chunkId: registered.chunkId,
5291
+ uploaded: true
5292
+ };
5026
5293
  }
5027
5294
  };
5028
5295
  }
@@ -5036,14 +5303,20 @@ function chunkLine(row, declared) {
5036
5303
  tagged.set("row", row.row);
5037
5304
  return dumps(tagged) + "\n";
5038
5305
  }
5039
- async function* buildChunks(rows, plan) {
5306
+ async function* buildChunks(rows, plan, ordinalStart = 0) {
5040
5307
  let lines = [];
5041
5308
  let rawBytes = 0;
5042
- let ordinal = 0;
5309
+ let ordinal = ordinalStart;
5043
5310
  let seen = 0;
5044
5311
  const flush2 = () => {
5045
5312
  const raw = Buffer.concat(lines);
5046
- const built = finishChunk(ordinal++, raw, lines.length, plan.entity, seen - lines.length);
5313
+ const built = finishChunk(
5314
+ ordinal++,
5315
+ raw,
5316
+ lines.length,
5317
+ plan.entity,
5318
+ seen - lines.length
5319
+ );
5047
5320
  lines = [];
5048
5321
  rawBytes = 0;
5049
5322
  return built;
@@ -5062,12 +5335,12 @@ function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
5062
5335
  const gz = gzipDeterministic(raw, { name: chunkName(ordinal) });
5063
5336
  return {
5064
5337
  ordinal,
5065
- digest: (0, import_node_crypto8.createHash)("sha256").update(gz).digest("hex"),
5338
+ digest: (0, import_node_crypto9.createHash)("sha256").update(gz).digest("hex"),
5066
5339
  bytes: gz.length,
5067
5340
  rows,
5068
5341
  entity,
5069
5342
  rawBytes: raw.length,
5070
- contentSha256: (0, import_node_crypto8.createHash)("sha256").update(raw).digest("hex"),
5343
+ contentSha256: (0, import_node_crypto9.createHash)("sha256").update(raw).digest("hex"),
5071
5344
  firstRow,
5072
5345
  gz
5073
5346
  };
@@ -5075,7 +5348,18 @@ function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
5075
5348
  function gzipDeterministic(raw, opts = {}) {
5076
5349
  const level = opts.level ?? GZIP_LEVEL;
5077
5350
  const name = opts.name ? Buffer.concat([Buffer.from(opts.name, "latin1"), Buffer.from([0])]) : Buffer.alloc(0);
5078
- const header = Buffer.from([31, 139, 8, opts.name ? 8 : 0, 0, 0, 0, 0, level === 9 ? 2 : level === 1 ? 4 : 0, 255]);
5351
+ const header = Buffer.from([
5352
+ 31,
5353
+ 139,
5354
+ 8,
5355
+ opts.name ? 8 : 0,
5356
+ 0,
5357
+ 0,
5358
+ 0,
5359
+ 0,
5360
+ level === 9 ? 2 : level === 1 ? 4 : 0,
5361
+ 255
5362
+ ]);
5079
5363
  const body = (0, import_node_zlib2.deflateRawSync)(raw, { level });
5080
5364
  const trailer = Buffer.alloc(8);
5081
5365
  trailer.writeUInt32LE(crc32(raw), 0);
@@ -5092,14 +5376,15 @@ function crc32(data) {
5092
5376
  }
5093
5377
  }
5094
5378
  let crc = 4294967295;
5095
- for (let i = 0; i < data.length; i++) crc = table[(crc ^ data[i]) & 255] ^ crc >>> 8;
5379
+ for (let i = 0; i < data.length; i++)
5380
+ crc = table[(crc ^ data[i]) & 255] ^ crc >>> 8;
5096
5381
  return (crc ^ 4294967295) >>> 0;
5097
5382
  }
5098
- var import_node_crypto8, import_node_zlib2, DEFAULT_CHUNK_BYTES, GZIP_LEVEL, chunkName, table;
5383
+ var import_node_crypto9, import_node_zlib2, DEFAULT_CHUNK_BYTES, GZIP_LEVEL, chunkName, table;
5099
5384
  var init_chunker = __esm({
5100
5385
  "src/cli/platform/worldData/chunker.ts"() {
5101
5386
  "use strict";
5102
- import_node_crypto8 = require("crypto");
5387
+ import_node_crypto9 = require("crypto");
5103
5388
  import_node_zlib2 = require("zlib");
5104
5389
  init_json();
5105
5390
  DEFAULT_CHUNK_BYTES = 64 * 1024 * 1024;
@@ -5172,7 +5457,7 @@ function parseToolCallRecord(raw) {
5172
5457
  }
5173
5458
  function readToolCallRecords(paths) {
5174
5459
  const records = [];
5175
- for (const file of paths) records.push(...toolCallRecordsIn(fs9.readFileSync(file, "utf8"), file));
5460
+ for (const file of paths) records.push(...toolCallRecordsIn(fs10.readFileSync(file, "utf8"), file));
5176
5461
  return records;
5177
5462
  }
5178
5463
  function toolCallRecordsIn(text2, where) {
@@ -5208,7 +5493,7 @@ async function emit(sink, record) {
5208
5493
  return;
5209
5494
  }
5210
5495
  if (typeof sink === "string") {
5211
- fs9.appendFileSync(sink, JSON.stringify(record) + "\n");
5496
+ fs10.appendFileSync(sink, JSON.stringify(record) + "\n");
5212
5497
  return;
5213
5498
  }
5214
5499
  if (typeof sink === "function") {
@@ -5239,14 +5524,6 @@ function captureToolCalls(tools, sink, opts = {}) {
5239
5524
  }
5240
5525
  return wrapped;
5241
5526
  }
5242
- function jsonish(value) {
5243
- if (typeof value !== "string") return value;
5244
- try {
5245
- return JSON.parse(value);
5246
- } catch {
5247
- return value;
5248
- }
5249
- }
5250
5527
  function toolCallsFromObservations(observations, opts = {}) {
5251
5528
  const known = opts.tools ? new Set(opts.tools) : null;
5252
5529
  const sinceMs = opts.since ? Date.parse(opts.since) : NaN;
@@ -5273,10 +5550,9 @@ function toolCallsFromObservations(observations, opts = {}) {
5273
5550
  out.unknownTools[tool] = (out.unknownTools[tool] ?? 0) + 1;
5274
5551
  continue;
5275
5552
  }
5276
- const input = jsonish(observation.input);
5277
- const record = { tool, args: isObject2(input) ? input : {} };
5553
+ const record = { tool, args: isObject2(observation.input) ? observation.input : {} };
5278
5554
  if (observation.level === "ERROR") record.error = observation.statusMessage || "ERROR";
5279
- else record.result = jsonish(observation.output);
5555
+ else record.result = observation.output;
5280
5556
  if (at !== void 0) record.at = at;
5281
5557
  if (opts.session) record.session = opts.session;
5282
5558
  record.source = `trace:${observation.traceId ?? "?"}/${observation.id ?? "?"}`;
@@ -5297,7 +5573,7 @@ async function ingest(world, records, options = {}) {
5297
5573
  const mode = options.mode ?? "append";
5298
5574
  const dryRun = Boolean(options.dryRun);
5299
5575
  const creds = credentialsOf(options);
5300
- const local = fs9.existsSync(world) && fs9.statSync(world).isDirectory();
5576
+ const local = fs10.existsSync(world) && fs10.statSync(world).isDirectory();
5301
5577
  const map = options.map === void 0 ? void 0 : options.map;
5302
5578
  let rows = null;
5303
5579
  let stderr = "";
@@ -5313,9 +5589,9 @@ async function ingest(world, records, options = {}) {
5313
5589
  if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
5314
5590
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
5315
5591
  const slug = world.split("@", 1)[0];
5316
- const scratch = fs9.mkdtempSync(path8.join(os3.tmpdir(), "gateway-ingest-"));
5592
+ const scratch = fs10.mkdtempSync(path9.join(os3.tmpdir(), "gateway-ingest-"));
5317
5593
  try {
5318
- const pulled = await pull2(world, path8.join(scratch, slug), options);
5594
+ const pulled = await pull2(world, path9.join(scratch, slug), options);
5319
5595
  let report;
5320
5596
  let projected;
5321
5597
  if (rows === null) {
@@ -5336,7 +5612,7 @@ async function ingest(world, records, options = {}) {
5336
5612
  if (status.entityCounts) report.entityCounts = status.entityCounts;
5337
5613
  return report;
5338
5614
  } finally {
5339
- fs9.rmSync(scratch, { recursive: true, force: true });
5615
+ fs10.rmSync(scratch, { recursive: true, force: true });
5340
5616
  }
5341
5617
  }
5342
5618
  async function ingestToolCalls(world, records, options = {}) {
@@ -5389,19 +5665,71 @@ async function sessionToolObservations(sessionId, options = {}) {
5389
5665
  for (const observation of trace5.observations ?? []) out.push({ traceId: trace5.id ?? null, ...observation });
5390
5666
  return out;
5391
5667
  }
5392
- var fs9, os3, path8, isObject2, isoOf;
5668
+ var fs10, os3, path9, isObject2, isoOf;
5393
5669
  var init_ingest = __esm({
5394
5670
  "src/worlds/ingest.ts"() {
5395
5671
  "use strict";
5396
- fs9 = __toESM(require("fs"));
5672
+ fs10 = __toESM(require("fs"));
5397
5673
  os3 = __toESM(require("os"));
5398
- path8 = __toESM(require("path"));
5674
+ path9 = __toESM(require("path"));
5399
5675
  init_worlds();
5400
5676
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
5401
5677
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
5402
5678
  }
5403
5679
  });
5404
5680
 
5681
+ // src/worlds/sandbox.ts
5682
+ function parseSlugRef(slugRef) {
5683
+ const at = slugRef.indexOf("@");
5684
+ if (at === -1) return { slug: slugRef };
5685
+ return { slug: slugRef.slice(0, at), ref: slugRef.slice(at + 1) };
5686
+ }
5687
+ var sid, WorldSandboxClient;
5688
+ var init_sandbox = __esm({
5689
+ "src/worlds/sandbox.ts"() {
5690
+ "use strict";
5691
+ init_worlds();
5692
+ sid = encodeURIComponent;
5693
+ WorldSandboxClient = class {
5694
+ constructor(options = {}) {
5695
+ this.options = options;
5696
+ }
5697
+ open(target) {
5698
+ return apiRequest(this.options, "POST", "/api/public/benchmark-containers/sandboxes", target);
5699
+ }
5700
+ status(sandboxId) {
5701
+ return apiRequest(this.options, "GET", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}`);
5702
+ }
5703
+ async read(sandboxId, filePath) {
5704
+ const res = await apiRequest(
5705
+ this.options,
5706
+ "GET",
5707
+ `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/file?path=${encodeURIComponent(filePath)}`
5708
+ );
5709
+ return { path: res.path, size: res.size, bytes: Buffer.from(res.contentBase64, "base64") };
5710
+ }
5711
+ write(sandboxId, filePath, bytes) {
5712
+ return apiRequest(this.options, "PUT", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/file`, {
5713
+ path: filePath,
5714
+ contentBase64: bytes.toString("base64")
5715
+ });
5716
+ }
5717
+ exec(sandboxId, input) {
5718
+ return apiRequest(this.options, "POST", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/exec`, input);
5719
+ }
5720
+ check(sandboxId) {
5721
+ return apiRequest(this.options, "POST", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/check`, {});
5722
+ }
5723
+ commit(sandboxId, input) {
5724
+ return apiRequest(this.options, "POST", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/commit`, input);
5725
+ }
5726
+ close(sandboxId) {
5727
+ return apiRequest(this.options, "DELETE", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}`);
5728
+ }
5729
+ };
5730
+ }
5731
+ });
5732
+
5405
5733
  // src/worlds/index.ts
5406
5734
  var worlds_exports = {};
5407
5735
  __export(worlds_exports, {
@@ -5414,6 +5742,7 @@ __export(worlds_exports, {
5414
5742
  EVENT_FLUSH_INTERVAL_MS: () => EVENT_FLUSH_INTERVAL_MS,
5415
5743
  EVENT_MAX_RETRIES: () => EVENT_MAX_RETRIES,
5416
5744
  GatewayEvalsClient: () => GatewayEvalsClient,
5745
+ PushSlugConflict: () => PushSlugConflict,
5417
5746
  STRIP_PREFIX: () => STRIP_PREFIX,
5418
5747
  SURFACE_KINDS: () => SURFACE_KINDS,
5419
5748
  TRANSFORMS: () => TRANSFORMS,
@@ -5421,6 +5750,7 @@ __export(worlds_exports, {
5421
5750
  TaskSessions: () => TaskSessions,
5422
5751
  WHITESPACE: () => WHITESPACE,
5423
5752
  WorldBrowser: () => WorldBrowser,
5753
+ WorldSandboxClient: () => WorldSandboxClient,
5424
5754
  WorldSession: () => WorldSession,
5425
5755
  WorldSessionCallError: () => WorldSessionCallError,
5426
5756
  WorldSessionTimeout: () => WorldSessionTimeout,
@@ -5430,9 +5760,11 @@ __export(worlds_exports, {
5430
5760
  aggregateTaskGrade: () => aggregateTaskGrade,
5431
5761
  apiRequest: () => apiRequest,
5432
5762
  applyTransforms: () => applyTransforms,
5763
+ archiveMode: () => archiveMode,
5433
5764
  attachManifest: () => attachManifest,
5434
5765
  attachSession: () => attachSession,
5435
5766
  attachTask: () => attachTask,
5767
+ buildFileManifest: () => buildFileManifest,
5436
5768
  buildSourceTarball: () => buildSourceTarball,
5437
5769
  captureToolCalls: () => captureToolCalls,
5438
5770
  collectArchiveRelPaths: () => collectArchiveRelPaths,
@@ -5444,12 +5776,12 @@ __export(worlds_exports, {
5444
5776
  deleteTask: () => deleteTask,
5445
5777
  describeWorld: () => describeWorld,
5446
5778
  detectProvenance: () => detectProvenance,
5779
+ extractBundle: () => extractBundle,
5447
5780
  getRun: () => getRun,
5448
5781
  getTask: () => getTask,
5449
5782
  hashForm: () => hashForm,
5450
5783
  ingest: () => ingest,
5451
5784
  ingestToolCalls: () => ingestToolCalls,
5452
- isIncludedInContentHash: () => isIncludedInContentHash,
5453
5785
  isTaskIdRef: () => isTaskIdRef,
5454
5786
  isTaskManifest: () => isTaskManifest,
5455
5787
  isTaskSpec: () => isTaskSpec,
@@ -5465,6 +5797,8 @@ __export(worlds_exports, {
5465
5797
  openSession: () => openSession,
5466
5798
  openTask: () => openTask,
5467
5799
  openWorld: () => openWorld,
5800
+ openWorldVersion: () => openWorldVersion,
5801
+ parseSlugRef: () => parseSlugRef,
5468
5802
  parseToolCallRecord: () => parseToolCallRecord,
5469
5803
  parseTransform: () => parseTransform,
5470
5804
  parseWorldTaskRefs: () => parseWorldTaskRefs,
@@ -5481,6 +5815,7 @@ __export(worlds_exports, {
5481
5815
  redactActionArgs: () => redactActionArgs,
5482
5816
  redactionOf: () => redactionOf,
5483
5817
  resolveBrowserMode: () => resolveBrowserMode,
5818
+ resolvePushSlug: () => resolvePushSlug,
5484
5819
  roundForm: () => roundForm,
5485
5820
  runSessions: () => runSessions,
5486
5821
  runTask: () => runTask,
@@ -5515,9 +5850,9 @@ function resolveEnv(options) {
5515
5850
  }
5516
5851
  return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
5517
5852
  }
5518
- async function apiRequest(options, method, path13, body) {
5853
+ async function apiRequest(options, method, path14, body) {
5519
5854
  const { host, publicKey, secretKey } = resolveEnv(options);
5520
- const res = await fetch(`${host}${path13}`, {
5855
+ const res = await fetch(`${host}${path14}`, {
5521
5856
  method,
5522
5857
  headers: {
5523
5858
  "content-type": "application/json",
@@ -5526,7 +5861,7 @@ async function apiRequest(options, method, path13, body) {
5526
5861
  ...body === void 0 ? {} : { body: JSON.stringify(body) }
5527
5862
  });
5528
5863
  const text2 = await res.text();
5529
- if (!res.ok) throw new ApiError2(res.status, method, path13, text2.slice(0, 400));
5864
+ if (!res.ok) throw new ApiError2(res.status, method, path14, text2.slice(0, 400));
5530
5865
  return JSON.parse(text2);
5531
5866
  }
5532
5867
  async function setEnvSecret(slug, key2, value, options = {}) {
@@ -5655,9 +5990,21 @@ async function openWorld(slugRef, options = {}) {
5655
5990
  const at = slugRef.indexOf("@");
5656
5991
  const slug = at === -1 ? slugRef : slugRef.slice(0, at);
5657
5992
  const ref = at === -1 ? null : slugRef.slice(at + 1);
5993
+ return resolveWorld(slugRef, slug, ref, ref ? { ref } : {}, options);
5994
+ }
5995
+ async function openWorldVersion(slug, versionId, options = {}) {
5996
+ return resolveWorld(
5997
+ `${slug}@${versionId}`,
5998
+ slug,
5999
+ null,
6000
+ { versionId },
6001
+ options
6002
+ );
6003
+ }
6004
+ async function resolveWorld(slugRef, slug, ref, pin, options) {
5658
6005
  const version = await apiRequest(options, "POST", "/api/public/benchmark-containers/resolve-version", {
5659
6006
  slug,
5660
- ...ref ? { ref } : {}
6007
+ ...pin
5661
6008
  });
5662
6009
  const world = new WorldSet(
5663
6010
  slugRef,
@@ -5696,9 +6043,10 @@ var init_worlds = __esm({
5696
6043
  init_query();
5697
6044
  init_migrate();
5698
6045
  init_ingest();
6046
+ init_sandbox();
5699
6047
  ApiError2 = class extends Error {
5700
- constructor(status, method, path13, body) {
5701
- super(`${method} ${path13} -> ${status}: ${body}`);
6048
+ constructor(status, method, path14, body) {
6049
+ super(`${method} ${path14} -> ${status}: ${body}`);
5702
6050
  this.status = status;
5703
6051
  this.name = "ApiError";
5704
6052
  }
@@ -5803,6 +6151,7 @@ __export(src_exports, {
5803
6151
  Persona: () => Persona,
5804
6152
  PersonaError: () => PersonaError,
5805
6153
  PersonasClient: () => PersonasClient,
6154
+ PushSlugConflict: () => PushSlugConflict,
5806
6155
  REPLAY_ENVIRONMENT: () => REPLAY_ENVIRONMENT,
5807
6156
  ReplayCase: () => ReplayCase,
5808
6157
  ReplayCheck: () => ReplayCheck,
@@ -5829,6 +6178,7 @@ __export(src_exports, {
5829
6178
  UsersClient: () => UsersClient,
5830
6179
  WHITESPACE: () => WHITESPACE,
5831
6180
  WorldBrowser: () => WorldBrowser,
6181
+ WorldSandboxClient: () => WorldSandboxClient,
5832
6182
  WorldSession: () => WorldSession,
5833
6183
  WorldSessionCallError: () => WorldSessionCallError,
5834
6184
  WorldSessionTimeout: () => WorldSessionTimeout,
@@ -5839,12 +6189,14 @@ __export(src_exports, {
5839
6189
  aggregateTaskGrade: () => aggregateTaskGrade,
5840
6190
  apiRequest: () => apiRequest,
5841
6191
  applyTransforms: () => applyTransforms,
6192
+ archiveMode: () => archiveMode,
5842
6193
  assembleManifest: () => assembleManifest,
5843
6194
  assignVariant: () => assignVariant,
5844
6195
  attachManifest: () => attachManifest,
5845
6196
  attachSession: () => attachSession,
5846
6197
  attachTask: () => attachTask,
5847
6198
  basicAuthHeader: () => basicAuthHeader,
6199
+ buildFileManifest: () => buildFileManifest,
5848
6200
  buildSourceTarball: () => buildSourceTarball,
5849
6201
  canonicalJson: () => canonicalJson,
5850
6202
  captureToolCalls: () => captureToolCalls,
@@ -5865,6 +6217,7 @@ __export(src_exports, {
5865
6217
  detectProvenance: () => detectProvenance,
5866
6218
  experimentContextStorage: () => experimentContextStorage,
5867
6219
  experiments: () => singleton_exports,
6220
+ extractBundle: () => extractBundle,
5868
6221
  files: () => files_exports,
5869
6222
  gateway: () => gateway,
5870
6223
  getExperimentContext: () => getExperimentContext,
@@ -5879,7 +6232,6 @@ __export(src_exports, {
5879
6232
  identify: () => identify,
5880
6233
  ingest: () => ingest,
5881
6234
  ingestToolCalls: () => ingestToolCalls,
5882
- isIncludedInContentHash: () => isIncludedInContentHash,
5883
6235
  isTaskIdRef: () => isTaskIdRef,
5884
6236
  isTaskManifest: () => isTaskManifest,
5885
6237
  isTaskSpec: () => isTaskSpec,
@@ -5897,6 +6249,8 @@ __export(src_exports, {
5897
6249
  openSession: () => openSession,
5898
6250
  openTask: () => openTask,
5899
6251
  openWorld: () => openWorld,
6252
+ openWorldVersion: () => openWorldVersion,
6253
+ parseSlugRef: () => parseSlugRef,
5900
6254
  parseToolCallRecord: () => parseToolCallRecord,
5901
6255
  parseTransform: () => parseTransform,
5902
6256
  parseWorldTaskRefs: () => parseWorldTaskRefs,
@@ -5920,6 +6274,7 @@ __export(src_exports, {
5920
6274
  replay: () => replay,
5921
6275
  resetBuildRegistry: () => resetBuildRegistry,
5922
6276
  resolveBrowserMode: () => resolveBrowserMode,
6277
+ resolvePushSlug: () => resolvePushSlug,
5923
6278
  resolveUserId: () => resolveUserId,
5924
6279
  roundForm: () => roundForm,
5925
6280
  runSessions: () => runSessions,
@@ -6527,8 +6882,8 @@ var GatewayContext = class _GatewayContext {
6527
6882
  }
6528
6883
  return resolved;
6529
6884
  }
6530
- async requestJson(method, path13, payload) {
6531
- const response = await fetch(`${this.host}${path13}`, {
6885
+ async requestJson(method, path14, payload) {
6886
+ const response = await fetch(`${this.host}${path14}`, {
6532
6887
  method,
6533
6888
  headers: {
6534
6889
  Authorization: this.authHeader(),
@@ -6539,7 +6894,7 @@ var GatewayContext = class _GatewayContext {
6539
6894
  const text2 = await response.text();
6540
6895
  const body = text2 ? safeJson(text2) : {};
6541
6896
  if (!response.ok) {
6542
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path13}`;
6897
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path14}`;
6543
6898
  if (response.status === 409) {
6544
6899
  throw new GatewayContextConflictError(
6545
6900
  message,
@@ -6548,7 +6903,7 @@ var GatewayContext = class _GatewayContext {
6548
6903
  }
6549
6904
  throw new GatewayContextError(message);
6550
6905
  }
6551
- this.log("%s %s -> %s", method, path13, response.status);
6906
+ this.log("%s %s -> %s", method, path14, response.status);
6552
6907
  return body;
6553
6908
  }
6554
6909
  authHeader() {
@@ -6655,11 +7010,11 @@ function primaryPathForKind(kind, snapshot) {
6655
7010
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
6656
7011
  if (readFile(snapshot, preferred)) return preferred;
6657
7012
  return Object.entries(snapshot.files).find(
6658
- ([path13, entry]) => entry.type === "file" && path13.endsWith(".md")
7013
+ ([path14, entry]) => entry.type === "file" && path14.endsWith(".md")
6659
7014
  )?.[0] ?? null;
6660
7015
  }
6661
- function parseJsonFile(snapshot, path13) {
6662
- const content = readFile(snapshot, path13);
7016
+ function parseJsonFile(snapshot, path14) {
7017
+ const content = readFile(snapshot, path14);
6663
7018
  if (!content) return {};
6664
7019
  try {
6665
7020
  const parsed = JSON.parse(content);
@@ -6668,8 +7023,8 @@ function parseJsonFile(snapshot, path13) {
6668
7023
  return {};
6669
7024
  }
6670
7025
  }
6671
- function readFile(snapshot, path13) {
6672
- const entry = snapshot.files[path13];
7026
+ function readFile(snapshot, path14) {
7027
+ const entry = snapshot.files[path14];
6673
7028
  return entry?.type === "file" ? entry.content : null;
6674
7029
  }
6675
7030
  function provenanceFor(snapshot, label) {
@@ -6771,16 +7126,16 @@ var GatewayMemory = class _GatewayMemory {
6771
7126
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
6772
7127
  );
6773
7128
  }
6774
- async read(target, path13, options = {}) {
7129
+ async read(target, path14, options = {}) {
6775
7130
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
6776
- const file = snapshot.files[path13];
7131
+ const file = snapshot.files[path14];
6777
7132
  return file?.type === "file" ? file.content : null;
6778
7133
  }
6779
- async write(target, path13, content, options = {}) {
7134
+ async write(target, path14, content, options = {}) {
6780
7135
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
6781
7136
  return this.push(snapshot.repo.repoHandle, {
6782
7137
  files: {
6783
- [path13]: {
7138
+ [path14]: {
6784
7139
  content,
6785
7140
  ...options.mimeType ? { mimeType: options.mimeType } : {}
6786
7141
  }
@@ -6790,10 +7145,10 @@ var GatewayMemory = class _GatewayMemory {
6790
7145
  ...runtimeContextPayload2(options)
6791
7146
  });
6792
7147
  }
6793
- async delete(target, path13, options = {}) {
7148
+ async delete(target, path14, options = {}) {
6794
7149
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
6795
7150
  return this.push(snapshot.repo.repoHandle, {
6796
- files: { [path13]: null },
7151
+ files: { [path14]: null },
6797
7152
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
6798
7153
  message: options.message,
6799
7154
  ...runtimeContextPayload2(options)
@@ -6806,8 +7161,8 @@ var GatewayMemory = class _GatewayMemory {
6806
7161
  payload
6807
7162
  );
6808
7163
  }
6809
- async requestJson(method, path13, payload) {
6810
- const response = await fetch(`${this.host}${path13}`, {
7164
+ async requestJson(method, path14, payload) {
7165
+ const response = await fetch(`${this.host}${path14}`, {
6811
7166
  method,
6812
7167
  headers: {
6813
7168
  Authorization: this.authHeader(),
@@ -6818,7 +7173,7 @@ var GatewayMemory = class _GatewayMemory {
6818
7173
  const text2 = await response.text();
6819
7174
  const body = text2 ? safeJson2(text2) : {};
6820
7175
  if (!response.ok) {
6821
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path13}`;
7176
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path14}`;
6822
7177
  if (response.status === 409) {
6823
7178
  throw new GatewayMemoryConflictError(
6824
7179
  message,
@@ -6827,7 +7182,7 @@ var GatewayMemory = class _GatewayMemory {
6827
7182
  }
6828
7183
  throw new GatewayMemoryError(message);
6829
7184
  }
6830
- this.log("%s %s -> %s", method, path13, response.status);
7185
+ this.log("%s %s -> %s", method, path14, response.status);
6831
7186
  return body;
6832
7187
  }
6833
7188
  authHeader() {
@@ -6989,10 +7344,10 @@ var PersonasClient = class _PersonasClient {
6989
7344
  done: Boolean(payload.done)
6990
7345
  };
6991
7346
  }
6992
- async requestJson(method, path13, payload) {
7347
+ async requestJson(method, path14, payload) {
6993
7348
  let response;
6994
7349
  try {
6995
- response = await fetch(`${this.host}${path13}`, {
7350
+ response = await fetch(`${this.host}${path14}`, {
6996
7351
  method,
6997
7352
  headers: {
6998
7353
  Authorization: this.authHeader(),
@@ -7003,7 +7358,7 @@ var PersonasClient = class _PersonasClient {
7003
7358
  });
7004
7359
  } catch (error) {
7005
7360
  const reason = error instanceof Error ? error.message : String(error);
7006
- throw new PersonaError(`${method} ${path13} failed: ${reason}`, {
7361
+ throw new PersonaError(`${method} ${path14} failed: ${reason}`, {
7007
7362
  cause: error
7008
7363
  });
7009
7364
  }
@@ -7011,10 +7366,10 @@ var PersonasClient = class _PersonasClient {
7011
7366
  if (!response.ok) {
7012
7367
  const detail = text2.slice(0, 500);
7013
7368
  throw new PersonaError(
7014
- `${method} ${path13} failed (${response.status}): ${detail}`
7369
+ `${method} ${path14} failed (${response.status}): ${detail}`
7015
7370
  );
7016
7371
  }
7017
- this.log("%s %s -> %s", method, path13, response.status);
7372
+ this.log("%s %s -> %s", method, path14, response.status);
7018
7373
  return safeJson3(text2);
7019
7374
  }
7020
7375
  authHeader() {
@@ -7239,10 +7594,10 @@ var SessionRecording = class _SessionRecording {
7239
7594
  };
7240
7595
  }
7241
7596
  /** Dump the full session as a JSON fixture (for regression tests). */
7242
- toFixture(path13) {
7243
- (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(path13), { recursive: true });
7244
- (0, import_node_fs.writeFileSync)(path13, JSON.stringify(this.toJSON(), null, 2), "utf-8");
7245
- return path13;
7597
+ toFixture(path14) {
7598
+ (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(path14), { recursive: true });
7599
+ (0, import_node_fs.writeFileSync)(path14, JSON.stringify(this.toJSON(), null, 2), "utf-8");
7600
+ return path14;
7246
7601
  }
7247
7602
  static fromApi(raw) {
7248
7603
  return new _SessionRecording({
@@ -8273,8 +8628,8 @@ var GatewaySecurity = class _GatewaySecurity {
8273
8628
  );
8274
8629
  }
8275
8630
  }
8276
- async postJson(path13, payload) {
8277
- const response = await fetch(`${this.host}${path13}`, {
8631
+ async postJson(path14, payload) {
8632
+ const response = await fetch(`${this.host}${path14}`, {
8278
8633
  method: "POST",
8279
8634
  headers: {
8280
8635
  Authorization: this.authHeader(),
@@ -8285,7 +8640,7 @@ var GatewaySecurity = class _GatewaySecurity {
8285
8640
  if (!response.ok) {
8286
8641
  const body = await response.text();
8287
8642
  throw new GatewaySecurityError(
8288
- `Gateway API ${response.status} on ${path13}: ${body || response.statusText}`
8643
+ `Gateway API ${response.status} on ${path14}: ${body || response.statusText}`
8289
8644
  );
8290
8645
  }
8291
8646
  return await response.json();
@@ -8374,9 +8729,9 @@ __export(files_exports, {
8374
8729
  uploadBytes: () => uploadBytes,
8375
8730
  uploadFile: () => uploadFile
8376
8731
  });
8377
- var import_node_crypto9 = require("crypto");
8378
- var fs10 = __toESM(require("fs"));
8379
- var path9 = __toESM(require("path"));
8732
+ var import_node_crypto10 = require("crypto");
8733
+ var fs11 = __toESM(require("fs"));
8734
+ var path10 = __toESM(require("path"));
8380
8735
  var import_node_stream = require("stream");
8381
8736
  var import_promises = require("stream/promises");
8382
8737
  init_worlds();
@@ -8425,9 +8780,9 @@ async function readFile2(options = {}, filePath) {
8425
8780
  async function downloadFile(options = {}, filePath, outPath, onProgress) {
8426
8781
  const file = await getFile(options, filePath);
8427
8782
  const res = await openDownload(file.url);
8428
- fs10.mkdirSync(path9.dirname(path9.resolve(outPath)), { recursive: true });
8783
+ fs11.mkdirSync(path10.dirname(path10.resolve(outPath)), { recursive: true });
8429
8784
  const source = import_node_stream.Readable.fromWeb(res.body);
8430
- await (0, import_promises.pipeline)(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs10.createWriteStream(outPath));
8785
+ await (0, import_promises.pipeline)(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs11.createWriteStream(outPath));
8431
8786
  return { ...file, out: outPath };
8432
8787
  }
8433
8788
  async function requestUpload(options = {}, body) {
@@ -8437,8 +8792,8 @@ async function confirmUpload(options = {}, fileId, ok = true) {
8437
8792
  return apiRequest(options, "POST", `${BASE}/confirm`, { fileId, ok });
8438
8793
  }
8439
8794
  async function sha256Base64(localPath) {
8440
- const hash = (0, import_node_crypto9.createHash)("sha256");
8441
- for await (const chunk of fs10.createReadStream(localPath)) hash.update(chunk);
8795
+ const hash = (0, import_node_crypto10.createHash)("sha256");
8796
+ for await (const chunk of fs11.createReadStream(localPath)) hash.update(chunk);
8442
8797
  return hash.digest("base64");
8443
8798
  }
8444
8799
  var CONTENT_TYPES = {
@@ -8474,7 +8829,7 @@ var CONTENT_TYPES = {
8474
8829
  ".mp4": "video/mp4"
8475
8830
  };
8476
8831
  function contentTypeFor(name) {
8477
- return CONTENT_TYPES[path9.extname(name).toLowerCase()] ?? "application/octet-stream";
8832
+ return CONTENT_TYPES[path10.extname(name).toLowerCase()] ?? "application/octet-stream";
8478
8833
  }
8479
8834
  function counting(report) {
8480
8835
  let seen = 0;
@@ -8501,7 +8856,7 @@ async function putPresigned(uploadUrl, body, headers) {
8501
8856
  if (!res.ok) throw new Error(`upload failed: storage answered HTTP ${res.status}: ${(await res.text()).slice(0, 300)}`);
8502
8857
  }
8503
8858
  async function uploadFile(options = {}, localPath, targetPath, upload = {}) {
8504
- const stat = fs10.statSync(localPath);
8859
+ const stat = fs11.statSync(localPath);
8505
8860
  if (!stat.isFile()) throw new Error(`${localPath} is not a file`);
8506
8861
  if (stat.size === 0) throw new Error(`${localPath} is empty; the drive refuses zero-byte uploads`);
8507
8862
  const contentType = upload.contentType ?? contentTypeFor(localPath);
@@ -8509,7 +8864,7 @@ async function uploadFile(options = {}, localPath, targetPath, upload = {}) {
8509
8864
  const target = drivePath(targetPath);
8510
8865
  const granted = await requestUpload(options, { path: target, contentLength: stat.size, contentType, sha256: sha2562, ...upload.overwrite ? { overwrite: true } : {} });
8511
8866
  try {
8512
- const stream = fs10.createReadStream(localPath).pipe(counting((n) => upload.onProgress?.(n, stat.size)));
8867
+ const stream = fs11.createReadStream(localPath).pipe(counting((n) => upload.onProgress?.(n, stat.size)));
8513
8868
  await putPresigned(granted.uploadUrl, import_node_stream.Readable.toWeb(stream), { contentType, contentLength: stat.size, sha256: sha2562 });
8514
8869
  } catch (error) {
8515
8870
  await confirmUpload(options, granted.fileId, false).catch(() => void 0);
@@ -8522,7 +8877,7 @@ async function uploadBytes(options = {}, bytes, targetPath, upload = {}) {
8522
8877
  if (!bytes.length) throw new Error("nothing to upload: the content is empty");
8523
8878
  const target = drivePath(targetPath);
8524
8879
  const contentType = upload.contentType ?? contentTypeFor(target);
8525
- const sha2562 = (0, import_node_crypto9.createHash)("sha256").update(bytes).digest("base64");
8880
+ const sha2562 = (0, import_node_crypto10.createHash)("sha256").update(bytes).digest("base64");
8526
8881
  const granted = await requestUpload(options, { path: target, contentLength: bytes.length, contentType, sha256: sha2562, ...upload.overwrite ? { overwrite: true } : {} });
8527
8882
  try {
8528
8883
  await putPresigned(granted.uploadUrl, new Uint8Array(bytes), { contentType, contentLength: bytes.length, sha256: sha2562 });
@@ -8635,6 +8990,7 @@ var hooks_exports = {};
8635
8990
  __export(hooks_exports, {
8636
8991
  AliasHandler: () => AliasHandler,
8637
8992
  AliasMap: () => AliasMap,
8993
+ AuditReport: () => AuditReport,
8638
8994
  CONTEXT_KEY: () => CONTEXT_KEY,
8639
8995
  CallableTool: () => CallableTool,
8640
8996
  CommandHandler: () => CommandHandler,
@@ -8642,6 +8998,8 @@ __export(hooks_exports, {
8642
8998
  DenyHandler: () => DenyHandler,
8643
8999
  EVENTS: () => EVENTS,
8644
9000
  EVENT_ALIASES: () => EVENT_ALIASES,
9001
+ FieldSource: () => FieldSource,
9002
+ FinderSource: () => FinderSource,
8645
9003
  FunctionHandler: () => FunctionHandler,
8646
9004
  HANDLER_TYPES: () => HANDLER_TYPES,
8647
9005
  HookConfigError: () => HookConfigError,
@@ -8653,13 +9011,18 @@ __export(hooks_exports, {
8653
9011
  HttpTool: () => HttpTool,
8654
9012
  INBOUND_KINDS: () => INBOUND_KINDS,
8655
9013
  INPUT_TYPES: () => INPUT_TYPES,
9014
+ Identifier: () => Identifier,
9015
+ IdentifierCensus: () => IdentifierCensus,
9016
+ IdentifierSource: () => IdentifierSource,
8656
9017
  KINDS: () => KINDS,
9018
+ LegendSource: () => LegendSource,
8657
9019
  LiveToolError: () => LiveToolError,
8658
9020
  LiveTools: () => LiveTools,
8659
9021
  PARSE_MODES: () => PARSE_MODES,
8660
9022
  RecordHandler: () => RecordHandler,
8661
9023
  Recorder: () => Recorder,
8662
9024
  RedactHandler: () => RedactHandler,
9025
+ RedactionAudit: () => RedactionAudit,
8663
9026
  Replay: () => Replay,
8664
9027
  TOOL_KINDS: () => TOOL_KINDS,
8665
9028
  applyLimit: () => applyLimit,
@@ -8714,8 +9077,51 @@ __export(hooks_exports, {
8714
9077
  withDefaults: () => withDefaults
8715
9078
  });
8716
9079
 
9080
+ // src/hooks/caseSwap.ts
9081
+ var CASE_SWAP_MIN_CHARS = 4;
9082
+ var standingAlone = /* @__PURE__ */ new Map();
9083
+ function standingAlonePattern(needle) {
9084
+ let pattern = standingAlone.get(needle);
9085
+ if (pattern === void 0) {
9086
+ const escaped = needle.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
9087
+ pattern = new RegExp(`(?<![A-Za-z0-9])${escaped}(?![A-Za-z0-9])`, "gi");
9088
+ standingAlone.set(needle, pattern);
9089
+ }
9090
+ return pattern;
9091
+ }
9092
+ var WORD = /[A-Za-z0-9]+/g;
9093
+ function shapedWord(word, seen) {
9094
+ const hasLetter = seen.toLowerCase() !== seen.toUpperCase();
9095
+ if (hasLetter && seen === seen.toUpperCase()) return word.toUpperCase();
9096
+ if (hasLetter && seen === seen.toLowerCase()) return word.toLowerCase();
9097
+ const first = seen.slice(0, 1);
9098
+ if (first !== first.toLowerCase())
9099
+ return word.slice(0, 1).toUpperCase() + word.slice(1);
9100
+ return word;
9101
+ }
9102
+ function shaped(replacement, seen) {
9103
+ const seenWords = seen.match(WORD) ?? [];
9104
+ if (seenWords.length !== (replacement.match(WORD) ?? []).length)
9105
+ return shapedWord(replacement, seen);
9106
+ let index2 = 0;
9107
+ return replacement.replace(
9108
+ WORD,
9109
+ (word) => shapedWord(word, seenWords[index2++])
9110
+ );
9111
+ }
9112
+ function swapValue(text2, needle, replacement) {
9113
+ if (!needle) return text2;
9114
+ const out = text2.split(needle).join(replacement);
9115
+ if (needle.length < CASE_SWAP_MIN_CHARS) return out;
9116
+ if (!out.toLowerCase().includes(needle.toLowerCase())) return out;
9117
+ return out.replace(
9118
+ standingAlonePattern(needle),
9119
+ (seen) => shaped(replacement, seen)
9120
+ );
9121
+ }
9122
+
8717
9123
  // src/hooks/alias.ts
8718
- var import_node_crypto10 = require("crypto");
9124
+ var import_node_crypto11 = require("crypto");
8719
9125
  var GIVEN = [
8720
9126
  "John",
8721
9127
  "Mary",
@@ -8906,7 +9312,7 @@ function findKind(kind, text2) {
8906
9312
  return out;
8907
9313
  }
8908
9314
  function newRunKey() {
8909
- return (0, import_node_crypto10.randomBytes)(32).toString("hex");
9315
+ return (0, import_node_crypto11.randomBytes)(32).toString("hex");
8910
9316
  }
8911
9317
  function canonical(kind, value) {
8912
9318
  const text2 = String(value).normalize("NFKC").trim();
@@ -8963,7 +9369,7 @@ function explodeIp(text2) {
8963
9369
  return groups.map((g) => g.padStart(4, "0")).join(":") + zone;
8964
9370
  }
8965
9371
  function index(runKey, kind, value, salt = "") {
8966
- const digest = (0, import_node_crypto10.createHmac)("sha256", Buffer.from(runKey, "hex")).update(`${kind}:${value}:${salt}`, "utf8").digest();
9372
+ const digest = (0, import_node_crypto11.createHmac)("sha256", Buffer.from(runKey, "hex")).update(`${kind}:${value}:${salt}`, "utf8").digest();
8967
9373
  return digest.readBigUInt64BE(0);
8968
9374
  }
8969
9375
  var pick = (pool, n) => pool[Number(n % BigInt(pool.length))];
@@ -9446,7 +9852,7 @@ var AliasMap = class _AliasMap {
9446
9852
  const existing = this.aliases.get(key2);
9447
9853
  if (existing) return existing.display;
9448
9854
  if (this.aliasDigests.has(digest.toLowerCase())) return digest;
9449
- const standIn = "sha256:" + (0, import_node_crypto10.createHmac)("sha256", Buffer.from(this.runKey, "hex")).update(digest, "utf8").digest("hex");
9855
+ const standIn = "sha256:" + (0, import_node_crypto11.createHmac)("sha256", Buffer.from(this.runKey, "hex")).update(digest, "utf8").digest("hex");
9450
9856
  this.aliasDigests.add(standIn.toLowerCase());
9451
9857
  const alias = {
9452
9858
  kind: "digest",
@@ -9484,9 +9890,9 @@ var AliasMap = class _AliasMap {
9484
9890
  });
9485
9891
  if (alias.role === "private") {
9486
9892
  for (const slug of this.slugs) {
9487
- const standIn = "sha256:" + (0, import_node_crypto10.createHash)("sha256").update(`${slug}:${canonical(alias.kind, alias.display)}`, "utf8").digest("hex");
9893
+ const standIn = "sha256:" + (0, import_node_crypto11.createHash)("sha256").update(`${slug}:${canonical(alias.kind, alias.display)}`, "utf8").digest("hex");
9488
9894
  alias.variants.push([
9489
- "sha256:" + (0, import_node_crypto10.createHash)("sha256").update(`${slug}:${alias.canonical}`, "utf8").digest("hex"),
9895
+ "sha256:" + (0, import_node_crypto11.createHash)("sha256").update(`${slug}:${alias.canonical}`, "utf8").digest("hex"),
9490
9896
  standIn
9491
9897
  ]);
9492
9898
  this.aliasDigests.add(standIn.toLowerCase());
@@ -9548,10 +9954,9 @@ var AliasMap = class _AliasMap {
9548
9954
  const [inbound] = this.pairs();
9549
9955
  let out = text2;
9550
9956
  for (const [needle, replacement, alias] of inbound) {
9551
- if (needle && out.includes(needle)) {
9552
- out = out.split(needle).join(replacement);
9553
- alias.hitsIn += 1;
9554
- }
9957
+ const swapped = swapValue(out, needle, replacement);
9958
+ if (swapped !== out) alias.hitsIn += 1;
9959
+ out = swapped;
9555
9960
  }
9556
9961
  out = out.replace(SHA256_ANY_GLOBAL, (match) => this.mintDigest(match));
9557
9962
  if (this.mintInbound.length) out = this.mintFoundIn(out, where);
@@ -9567,10 +9972,9 @@ var AliasMap = class _AliasMap {
9567
9972
  const [, outbound] = this.pairs();
9568
9973
  let out = text2;
9569
9974
  for (const [needle, replacement, alias] of outbound) {
9570
- if (needle && out.includes(needle)) {
9571
- out = out.split(needle).join(replacement);
9572
- alias.hitsOut += 1;
9573
- }
9975
+ const swapped = swapValue(out, needle, replacement);
9976
+ if (swapped !== out) alias.hitsOut += 1;
9977
+ out = swapped;
9574
9978
  }
9575
9979
  const problems = [];
9576
9980
  for (const [needle] of outbound) {
@@ -9783,7 +10187,7 @@ function mirrorHandle(handle, identity) {
9783
10187
  }
9784
10188
 
9785
10189
  // src/hooks/config.ts
9786
- var fs11 = __toESM(require("fs"));
10190
+ var fs12 = __toESM(require("fs"));
9787
10191
 
9788
10192
  // src/cli/platform/worldData/toml.ts
9789
10193
  var TomlError = class extends Error {
@@ -10131,9 +10535,9 @@ function alternatives(raw) {
10131
10535
  if (!parts2.length || parts2.includes("*")) return [];
10132
10536
  return parts2;
10133
10537
  }
10134
- function hasPath(node, path13) {
10538
+ function hasPath(node, path14) {
10135
10539
  let current = node;
10136
- for (const part of path13.split(".")) {
10540
+ for (const part of path14.split(".")) {
10137
10541
  if (current && typeof current === "object" && !Array.isArray(current) && part in current) {
10138
10542
  current = current[part];
10139
10543
  continue;
@@ -10174,7 +10578,7 @@ function matches(m, event) {
10174
10578
  if (m.args.length) {
10175
10579
  const haystacks = event.event === "pre_request" || event.event === "post_response" ? [event.request?.query, event.request?.body] : [event.toolInput];
10176
10580
  const present = haystacks.filter((h) => h !== void 0 && h !== null);
10177
- if (!m.args.every((path13) => present.some((h) => hasPath(h, path13))))
10581
+ if (!m.args.every((path14) => present.some((h) => hasPath(h, path14))))
10178
10582
  return false;
10179
10583
  }
10180
10584
  return true;
@@ -10617,9 +11021,9 @@ function vendorOf(config, toolName) {
10617
11021
  return void 0;
10618
11022
  }
10619
11023
  function loadHookConfig(file) {
10620
- if (!fs11.existsSync(file) || !fs11.statSync(file).isFile())
11024
+ if (!fs12.existsSync(file) || !fs12.statSync(file).isFile())
10621
11025
  throw new HookConfigError(`${file}: no such file`);
10622
- const text2 = fs11.readFileSync(file, "utf8");
11026
+ const text2 = fs12.readFileSync(file, "utf8");
10623
11027
  if (file.toLowerCase().endsWith(".json")) {
10624
11028
  let data2;
10625
11029
  try {
@@ -10999,8 +11403,8 @@ function parseHandler(event, raw, position) {
10999
11403
 
11000
11404
  // src/hooks/handlers.ts
11001
11405
  var import_node_child_process3 = require("child_process");
11002
- var fs12 = __toESM(require("fs"));
11003
- var path10 = __toESM(require("path"));
11406
+ var fs13 = __toESM(require("fs"));
11407
+ var path11 = __toESM(require("path"));
11004
11408
  var import_node_url = require("url");
11005
11409
  function parseStdout(text2, handler) {
11006
11410
  const stripped = text2.trim();
@@ -11194,8 +11598,8 @@ var RecordHandler = class {
11194
11598
  record.source = this.opts.source ?? null;
11195
11599
  if (Array.isArray(this.sink)) this.sink.push(record);
11196
11600
  else if (typeof this.sink === "string") {
11197
- fs12.mkdirSync(path10.dirname(this.sink), { recursive: true });
11198
- fs12.appendFileSync(this.sink, JSON.stringify(record) + "\n");
11601
+ fs13.mkdirSync(path11.dirname(this.sink), { recursive: true });
11602
+ fs13.appendFileSync(this.sink, JSON.stringify(record) + "\n");
11199
11603
  } else await this.sink(record);
11200
11604
  return allow();
11201
11605
  }
@@ -11400,7 +11804,7 @@ async function importCallable(target, cwd) {
11400
11804
  const hash = target.lastIndexOf("#");
11401
11805
  const modulePath = target.slice(0, hash);
11402
11806
  const exportName = target.slice(hash + 1) || "default";
11403
- const resolved = modulePath.startsWith(".") || path10.isAbsolute(modulePath) ? (0, import_node_url.pathToFileURL)(path10.resolve(cwd, modulePath)).href : modulePath;
11807
+ const resolved = modulePath.startsWith(".") || path11.isAbsolute(modulePath) ? (0, import_node_url.pathToFileURL)(path11.resolve(cwd, modulePath)).href : modulePath;
11404
11808
  let module2;
11405
11809
  try {
11406
11810
  module2 = await import(resolved);
@@ -11463,8 +11867,8 @@ async function buildHandler(spec, aliases, opts) {
11463
11867
  }
11464
11868
 
11465
11869
  // src/hooks/live.ts
11466
- var fs13 = __toESM(require("fs"));
11467
- var path11 = __toESM(require("path"));
11870
+ var fs14 = __toESM(require("fs"));
11871
+ var path12 = __toESM(require("path"));
11468
11872
  var USER_AGENT = "gateway-hooks/1 (+https://withgateway.ai)";
11469
11873
  var TAGS = /<(script|style)[^>]*>.*?<\/\1>/gis;
11470
11874
  var STRIP = /<[^>]+>/g;
@@ -11828,9 +12232,9 @@ var CallableTool = class _CallableTool {
11828
12232
  }
11829
12233
  };
11830
12234
  function readConnection(root) {
11831
- const file = path11.join(root, "connector.toml");
11832
- if (!fs13.existsSync(file)) throw new Error(`no connector.toml at ${root}`);
11833
- const raw = parseToml(fs13.readFileSync(file, "utf8"));
12235
+ const file = path12.join(root, "connector.toml");
12236
+ if (!fs14.existsSync(file)) throw new Error(`no connector.toml at ${root}`);
12237
+ const raw = parseToml(fs14.readFileSync(file, "utf8"));
11834
12238
  const connector = raw.connector ?? {};
11835
12239
  const auth = raw.auth ?? {};
11836
12240
  const operations = {};
@@ -11844,7 +12248,7 @@ function readConnection(root) {
11844
12248
  };
11845
12249
  }
11846
12250
  return {
11847
- slug: connector.slug ?? path11.basename(root),
12251
+ slug: connector.slug ?? path12.basename(root),
11848
12252
  baseUrl: (connector.base_url ?? "").replace(/\/+$/, ""),
11849
12253
  scheme: auth.scheme ?? "none",
11850
12254
  ...typeof auth.name === "string" ? { authName: auth.name } : {},
@@ -11861,8 +12265,8 @@ var ConnectorTool = class {
11861
12265
  this.spec = spec;
11862
12266
  this.live = live;
11863
12267
  let location = spec.options.connector;
11864
- if (!path11.isAbsolute(location)) location = path11.resolve(baseDir, location);
11865
- const root = fs13.existsSync(location) && fs13.statSync(location).isFile() ? path11.dirname(location) : location;
12268
+ if (!path12.isAbsolute(location)) location = path12.resolve(baseDir, location);
12269
+ const root = fs14.existsSync(location) && fs14.statSync(location).isFile() ? path12.dirname(location) : location;
11866
12270
  try {
11867
12271
  this.connection = readConnection(root);
11868
12272
  } catch (error) {
@@ -12016,16 +12420,16 @@ var Recorder = class {
12016
12420
  this.file = file;
12017
12421
  }
12018
12422
  write(line) {
12019
- fs13.mkdirSync(path11.dirname(this.file), { recursive: true });
12020
- fs13.appendFileSync(this.file, canonical2(line) + "\n");
12423
+ fs14.mkdirSync(path12.dirname(this.file), { recursive: true });
12424
+ fs14.appendFileSync(this.file, canonical2(line) + "\n");
12021
12425
  }
12022
12426
  };
12023
12427
  var Replay = class {
12024
12428
  constructor(file) {
12025
12429
  this.file = file;
12026
- if (!fs13.existsSync(file))
12430
+ if (!fs14.existsSync(file))
12027
12431
  throw new LiveToolError(`replay: no recording at ${file}`);
12028
- fs13.readFileSync(file, "utf8").split(/\r?\n/).forEach((raw, index2) => {
12432
+ fs14.readFileSync(file, "utf8").split(/\r?\n/).forEach((raw, index2) => {
12029
12433
  if (!raw.trim()) return;
12030
12434
  let line;
12031
12435
  try {
@@ -12109,7 +12513,7 @@ var LiveTools = class _LiveTools {
12109
12513
  ...config.live.record ? { record: interpolatePath(config.live.record, run.runId, directory) } : {},
12110
12514
  ...config.live.replay ? { replay: interpolatePath(config.live.replay, run.runId, directory) } : {}
12111
12515
  };
12112
- return _LiveTools.create(run, live, path11.dirname(path11.resolve(file)));
12516
+ return _LiveTools.create(run, live, path12.dirname(path12.resolve(file)));
12113
12517
  }
12114
12518
  /** The declared tools for a model. `openai`: `{type: "function", function: {name,
12115
12519
  * description, parameters}}`; `anthropic`: `{name, description, input_schema}`. */
@@ -12206,10 +12610,225 @@ var LiveTools = class _LiveTools {
12206
12610
  }
12207
12611
  };
12208
12612
 
12613
+ // src/hooks/audit.ts
12614
+ var MIN_VALUE_CHARS = 4;
12615
+ var REVIEW_MIN_CHARS = 20;
12616
+ var ANY_TOOL = "*";
12617
+ var isObject6 = (node) => typeof node === "object" && node !== null && !Array.isArray(node);
12618
+ function* stringsOf(node) {
12619
+ if (typeof node === "string") {
12620
+ yield node;
12621
+ return;
12622
+ }
12623
+ const children = Array.isArray(node) ? node : isObject6(node) ? Object.values(node) : [];
12624
+ for (const child of children) yield* stringsOf(child);
12625
+ }
12626
+ function* fieldsOf(node) {
12627
+ if (Array.isArray(node)) {
12628
+ for (const child of node) yield* fieldsOf(child);
12629
+ return;
12630
+ }
12631
+ if (!isObject6(node)) return;
12632
+ for (const [key2, value] of Object.entries(node)) {
12633
+ yield [key2, value];
12634
+ yield* fieldsOf(value);
12635
+ }
12636
+ }
12637
+ var Identifier = class {
12638
+ constructor(kind, value, source) {
12639
+ this.kind = kind;
12640
+ this.value = value;
12641
+ this.source = source;
12642
+ }
12643
+ get key() {
12644
+ return folded(this.value);
12645
+ }
12646
+ get prefix() {
12647
+ return this.value.slice(0, 3);
12648
+ }
12649
+ };
12650
+ var IdentifierSource = class {
12651
+ ignored() {
12652
+ return [];
12653
+ }
12654
+ };
12655
+ var LegendSource = class extends IdentifierSource {
12656
+ name = "legend";
12657
+ entries;
12658
+ constructor(legend) {
12659
+ super();
12660
+ this.entries = legend.filter((entry) => entry.real);
12661
+ }
12662
+ collect() {
12663
+ return this.entries.filter((entry) => (entry.role ?? "private") !== "public").map((entry) => new Identifier(entry.kind ?? "reference", String(entry.real), this.name));
12664
+ }
12665
+ ignored() {
12666
+ const open = this.entries.filter((e) => e.role === "public").map((e) => String(e.real));
12667
+ const personas2 = this.entries.filter((e) => e.alias).map((e) => String(e.alias));
12668
+ return [...open, ...personas2];
12669
+ }
12670
+ };
12671
+ var FieldSource = class extends IdentifierSource {
12672
+ constructor(fieldKinds) {
12673
+ super();
12674
+ this.fieldKinds = fieldKinds;
12675
+ }
12676
+ name = "fields";
12677
+ kindsFor(tool) {
12678
+ return { ...this.fieldKinds[ANY_TOOL] ?? {}, ...this.fieldKinds[tool] ?? {} };
12679
+ }
12680
+ *collect(raw) {
12681
+ for (const record of raw) {
12682
+ const kinds = this.kindsFor(String(record.tool ?? ""));
12683
+ for (const [key2, value] of fieldsOf(record.result)) {
12684
+ const kind = kinds[key2];
12685
+ if (kind === void 0) continue;
12686
+ for (const text2 of stringsOf(value)) yield new Identifier(kind, text2, this.name);
12687
+ }
12688
+ }
12689
+ }
12690
+ };
12691
+ var FinderSource = class extends IdentifierSource {
12692
+ name;
12693
+ kinds;
12694
+ constructor(kinds = INBOUND_KINDS) {
12695
+ super();
12696
+ const bad = kinds.filter((kind) => !INBOUND_KINDS.includes(kind));
12697
+ if (bad.length > 0)
12698
+ throw new Error(
12699
+ `${bad.join(", ")} cannot be found by a pattern; only ${INBOUND_KINDS.join(", ")} can`
12700
+ );
12701
+ this.kinds = [...kinds];
12702
+ this.name = `finders:${this.kinds.join(",")}`;
12703
+ }
12704
+ *collect(raw) {
12705
+ for (const record of raw)
12706
+ for (const text2 of stringsOf(record.result))
12707
+ for (const kind of this.kinds)
12708
+ for (const value of findKind(kind, text2)) yield new Identifier(kind, value, "finders");
12709
+ }
12710
+ };
12711
+ var IdentifierCensus = class {
12712
+ constructor(sources) {
12713
+ this.sources = sources;
12714
+ }
12715
+ get detectors() {
12716
+ return this.sources.map((source) => source.name);
12717
+ }
12718
+ build(raw) {
12719
+ const ignored = new Set(this.sources.flatMap((s) => [...s.ignored()].map(folded)));
12720
+ const seen = /* @__PURE__ */ new Map();
12721
+ for (const source of this.sources)
12722
+ for (const identifier of source.collect(raw)) {
12723
+ if (identifier.value.length < MIN_VALUE_CHARS || ignored.has(identifier.key)) continue;
12724
+ if (!seen.has(identifier.key)) seen.set(identifier.key, identifier);
12725
+ }
12726
+ return [...seen.values()];
12727
+ }
12728
+ };
12729
+ function shuffled(items, seed) {
12730
+ const out = [...items];
12731
+ let state2 = seed >>> 0;
12732
+ const next = () => {
12733
+ state2 = state2 + 1831565813 >>> 0;
12734
+ let t = Math.imul(state2 ^ state2 >>> 15, 1 | state2);
12735
+ t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
12736
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
12737
+ };
12738
+ for (let i = out.length - 1; i > 0; i -= 1) {
12739
+ const j = Math.floor(next() * (i + 1));
12740
+ [out[i], out[j]] = [out[j], out[i]];
12741
+ }
12742
+ return out;
12743
+ }
12744
+ var AuditReport = class {
12745
+ constructor(findings, census, detectors, recordsScanned, leakedIdentifiers, cleanTexts) {
12746
+ this.findings = findings;
12747
+ this.census = census;
12748
+ this.detectors = detectors;
12749
+ this.recordsScanned = recordsScanned;
12750
+ this.leakedIdentifiers = leakedIdentifiers;
12751
+ this.cleanTexts = cleanTexts;
12752
+ }
12753
+ get ok() {
12754
+ return this.findings.length === 0;
12755
+ }
12756
+ get censusTotal() {
12757
+ return this.census.length;
12758
+ }
12759
+ /** The share of census identifiers that did NOT reach the model; 1 for an empty census. */
12760
+ get coverage() {
12761
+ if (this.census.length === 0) return 1;
12762
+ return Math.round((1 - this.leakedIdentifiers / this.census.length) * 1e4) / 1e4;
12763
+ }
12764
+ count(pick3) {
12765
+ const counts = {};
12766
+ for (const identifier of this.census) counts[pick3(identifier)] = (counts[pick3(identifier)] ?? 0) + 1;
12767
+ return counts;
12768
+ }
12769
+ /** `size` strings the model saw that nothing flagged, for a person to read. */
12770
+ reviewSample(size, seed = 0) {
12771
+ const pool = [...this.cleanTexts].sort(
12772
+ (a, b) => a.record - b.record || a.text.localeCompare(b.text)
12773
+ );
12774
+ return shuffled(pool, seed).slice(0, size);
12775
+ }
12776
+ toDict() {
12777
+ return {
12778
+ ok: this.ok,
12779
+ records_scanned: this.recordsScanned,
12780
+ detectors: this.detectors,
12781
+ census: {
12782
+ total: this.censusTotal,
12783
+ by_source: this.count((i) => i.source),
12784
+ by_kind: this.count((i) => i.kind)
12785
+ },
12786
+ leaked_identifiers: this.leakedIdentifiers,
12787
+ coverage: this.coverage,
12788
+ findings: this.findings
12789
+ };
12790
+ }
12791
+ };
12792
+ var RedactionAudit = class _RedactionAudit {
12793
+ constructor(census) {
12794
+ this.census = census;
12795
+ }
12796
+ /** The standard audit: the run's legend, declared field kinds, pattern finders. */
12797
+ static forRun(options) {
12798
+ return new _RedactionAudit(
12799
+ new IdentifierCensus([
12800
+ new LegendSource(options.legend ?? []),
12801
+ new FieldSource(options.fieldKinds ?? {}),
12802
+ new FinderSource(options.finderKinds ?? INBOUND_KINDS)
12803
+ ])
12804
+ );
12805
+ }
12806
+ run(raw, shown) {
12807
+ const census = this.census.build(raw);
12808
+ const findings = [];
12809
+ const clean = [];
12810
+ const leaked = /* @__PURE__ */ new Set();
12811
+ shown.forEach((record, index2) => {
12812
+ const tool = String(record.tool ?? "");
12813
+ const texts = [...stringsOf(record.result)];
12814
+ const haystack = folded(texts.join("\n"));
12815
+ const hits = census.filter((identifier) => haystack.includes(identifier.key));
12816
+ hits.forEach((identifier) => leaked.add(identifier.key));
12817
+ findings.push(
12818
+ ...hits.map((i) => ({ record: index2, tool, kind: i.kind, source: i.source, prefix: i.prefix }))
12819
+ );
12820
+ clean.push(
12821
+ ...texts.filter((t) => t.length >= REVIEW_MIN_CHARS && !hits.some((i) => folded(t).includes(i.key))).map((text2) => ({ record: index2, tool, text: text2 }))
12822
+ );
12823
+ });
12824
+ return new AuditReport(findings, census, this.census.detectors, shown.length, leaked.size, clean);
12825
+ }
12826
+ };
12827
+
12209
12828
  // src/hooks/run.ts
12210
- var import_node_crypto11 = require("crypto");
12211
- var fs14 = __toESM(require("fs"));
12212
- var path12 = __toESM(require("path"));
12829
+ var import_node_crypto12 = require("crypto");
12830
+ var fs15 = __toESM(require("fs"));
12831
+ var path13 = __toESM(require("path"));
12213
12832
  var BLOCKING_EVENTS = [
12214
12833
  "pre_tool_call",
12215
12834
  "pre_request",
@@ -12251,7 +12870,7 @@ var HookRun = class _HookRun {
12251
12870
  source;
12252
12871
  constructor(config, options) {
12253
12872
  this.config = config;
12254
- this.runId = options.runId ?? `run-${(0, import_node_crypto11.randomBytes)(6).toString("hex")}`;
12873
+ this.runId = options.runId ?? `run-${(0, import_node_crypto12.randomBytes)(6).toString("hex")}`;
12255
12874
  this.runKey = options.runKey ?? newRunKey();
12256
12875
  this.alias = new AliasMap(this.runKey, options.slugs ?? config.aliasSlugs, {
12257
12876
  publicLayerWins: config.aliasPublicLayerWins,
@@ -12259,12 +12878,12 @@ var HookRun = class _HookRun {
12259
12878
  mintInbound: options.mintInbound ?? config.aliasMintInbound
12260
12879
  });
12261
12880
  this.cwd = options.cwd ?? process.cwd();
12262
- this.runDir = options.runDir ?? path12.join(this.cwd, "runs", this.runId);
12881
+ this.runDir = options.runDir ?? path13.join(this.cwd, "runs", this.runId);
12263
12882
  const chosen = options.legend !== void 0 ? options.legend : config.aliasLegend;
12264
- this.legendPath = chosen ? path12.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
12883
+ this.legendPath = chosen ? path13.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
12265
12884
  this.sessionId = options.session;
12266
12885
  this.source = options.source;
12267
- this.configDir = config.source ? path12.dirname(path12.resolve(config.source)) : this.cwd;
12886
+ this.configDir = config.source ? path13.dirname(path13.resolve(config.source)) : this.cwd;
12268
12887
  }
12269
12888
  /** Build from a parsed config (an object in the file's shape, or a `HookConfig`);
12270
12889
  * handlers are constructed now, so a callable that does not import fails here. */
@@ -12674,8 +13293,8 @@ var HookRun = class _HookRun {
12674
13293
  throw new Error(
12675
13294
  "no legend path: set [alias] legend, pass legend to the run, or give a path"
12676
13295
  );
12677
- fs14.mkdirSync(path12.dirname(target), { recursive: true });
12678
- fs14.writeFileSync(target, JSON.stringify(this.legend(), null, 2) + "\n");
13296
+ fs15.mkdirSync(path13.dirname(target), { recursive: true });
13297
+ fs15.writeFileSync(target, JSON.stringify(this.legend(), null, 2) + "\n");
12679
13298
  return target;
12680
13299
  }
12681
13300
  writeLegendIfConfigured() {
@@ -12767,14 +13386,14 @@ var HookedSession = class {
12767
13386
  * `apiHeaders()`), `pre_request` before it and `post_response` after. Returns
12768
13387
  * `{status, body, headers}`; a 4xx/5xx is returned, not thrown, so the agent sees
12769
13388
  * the vendor's own error shape (rewritten like any result). */
12770
- async request(method, path13, opts = {}) {
13389
+ async request(method, path14, opts = {}) {
12771
13390
  if (!this.inner.api && !this.requestFn)
12772
13391
  throw new Error(
12773
13392
  "this session has no api surface (pass requestFn for a transport of your own)"
12774
13393
  );
12775
13394
  let request = {
12776
13395
  method: method.toUpperCase(),
12777
- path: path13,
13396
+ path: path14,
12778
13397
  query: { ...opts.query ?? {} },
12779
13398
  body: opts.body ?? null
12780
13399
  };
@@ -12989,10 +13608,28 @@ function toolResultText(content) {
12989
13608
  ).join("");
12990
13609
  return JSON.stringify(content ?? "");
12991
13610
  }
13611
+ function toolSummary(names) {
13612
+ return names.map((n) => `\u2192 ${n}`).join(", ");
13613
+ }
13614
+ var USAGE_ATTRIBUTES = [
13615
+ ["input_tokens", "llm.token_count.prompt"],
13616
+ ["output_tokens", "llm.token_count.completion"],
13617
+ ["cache_read_input_tokens", "llm.token_count.prompt_details.cache_read"],
13618
+ ["cache_creation_input_tokens", "llm.token_count.prompt_details.cache_write"]
13619
+ ];
13620
+ function setUsageAttributes(span, usage) {
13621
+ if (!usage || typeof usage !== "object") return;
13622
+ const u = usage;
13623
+ for (const [field, attribute] of USAGE_ATTRIBUTES) {
13624
+ const value = u[field];
13625
+ if (typeof value === "number" && Number.isFinite(value))
13626
+ span.setAttribute(attribute, value);
13627
+ }
13628
+ }
12992
13629
  var TURN_INPUT_MAX = 4e3;
12993
13630
  async function* observeClaude(stream, options) {
12994
13631
  const tracer = options.tracer ?? import_api4.trace.getTracer("@withgateway/sdk");
12995
- const model = options.model ?? "claude";
13632
+ const fallbackModel = options.model ?? "claude";
12996
13633
  const openTools = /* @__PURE__ */ new Map();
12997
13634
  let root = null;
12998
13635
  let rootCtx = null;
@@ -13010,85 +13647,102 @@ async function* observeClaude(stream, options) {
13010
13647
  let turnCtx = null;
13011
13648
  let turnIndex = 0;
13012
13649
  let turnText = "";
13650
+ let turnTools = [];
13013
13651
  let pendingTurnInput = options.input ?? "";
13652
+ let gen = null;
13653
+ let genMessageId;
13654
+ let genText = "";
13655
+ let genTools = [];
13656
+ let genUsage;
13014
13657
  let finalText = "";
13658
+ const endGeneration = () => {
13659
+ if (!gen) return;
13660
+ gen.setAttribute("output.value", genText || toolSummary(genTools));
13661
+ setUsageAttributes(gen, genUsage);
13662
+ gen.setStatus({ code: import_api4.SpanStatusCode.OK });
13663
+ gen.end();
13664
+ gen = null;
13665
+ genMessageId = void 0;
13666
+ genText = "";
13667
+ genTools = [];
13668
+ genUsage = void 0;
13669
+ };
13015
13670
  const endTurn = () => {
13016
13671
  if (!turnRoot) return;
13017
- if (turnText) turnRoot.setAttribute("output.value", turnText);
13672
+ const output = turnText || toolSummary(turnTools);
13673
+ if (output) turnRoot.setAttribute("output.value", output);
13018
13674
  turnRoot.setStatus({ code: import_api4.SpanStatusCode.OK });
13019
13675
  turnRoot.end();
13020
13676
  turnRoot = null;
13021
13677
  turnCtx = null;
13022
13678
  };
13679
+ const startTurn = () => {
13680
+ for (const span of openTools.values()) span.end();
13681
+ openTools.clear();
13682
+ endTurn();
13683
+ turnIndex += 1;
13684
+ turnText = "";
13685
+ turnTools = [];
13686
+ turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
13687
+ root: true,
13688
+ attributes: {
13689
+ "openinference.span.kind": "AGENT",
13690
+ ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
13691
+ ...options.attributes ?? {}
13692
+ }
13693
+ });
13694
+ turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
13695
+ pendingTurnInput = "";
13696
+ };
13697
+ const startGeneration = (message) => {
13698
+ endGeneration();
13699
+ if (options.perTurn) startTurn();
13700
+ const childCtx = options.perTurn ? turnCtx : rootCtx;
13701
+ genMessageId = message.message?.id;
13702
+ gen = tracer.startSpan(
13703
+ "assistant",
13704
+ {
13705
+ attributes: {
13706
+ "openinference.span.kind": "LLM",
13707
+ "llm.model_name": message.message?.model ?? fallbackModel,
13708
+ "llm.provider": "anthropic"
13709
+ }
13710
+ },
13711
+ childCtx
13712
+ );
13713
+ };
13023
13714
  try {
13024
13715
  for await (const message of stream) {
13025
13716
  if (message?.type === "assistant") {
13026
- if (options.perTurn) {
13027
- for (const span of openTools.values()) span.end();
13028
- openTools.clear();
13029
- endTurn();
13030
- turnIndex += 1;
13031
- turnText = "";
13032
- turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
13033
- root: true,
13034
- attributes: {
13035
- "openinference.span.kind": "AGENT",
13036
- ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
13037
- ...options.attributes ?? {}
13038
- }
13039
- });
13040
- turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
13041
- pendingTurnInput = "";
13042
- }
13717
+ const messageId = message.message?.id;
13718
+ const continuesGeneration = gen !== null && messageId !== void 0 && messageId === genMessageId;
13719
+ if (!continuesGeneration) startGeneration(message);
13043
13720
  const childCtx = options.perTurn ? turnCtx : rootCtx;
13044
- let text2 = "";
13045
- const toolUses = [];
13046
13721
  for (const block of message.message?.content ?? []) {
13047
13722
  if (block?.type === "text") {
13048
13723
  finalText += block.text;
13049
- text2 += block.text;
13724
+ turnText += block.text;
13725
+ genText += block.text;
13050
13726
  }
13051
13727
  if (block?.type === "tool_use") {
13052
- toolUses.push({
13053
- id: block.id,
13054
- name: block.name,
13055
- input: block.input
13056
- });
13728
+ const name = cleanToolName(block.name);
13729
+ genTools.push(name);
13730
+ turnTools.push(name);
13731
+ const span = tracer.startSpan(
13732
+ `tool.${name}`,
13733
+ {
13734
+ attributes: {
13735
+ "openinference.span.kind": "TOOL",
13736
+ "tool.name": name,
13737
+ "input.value": JSON.stringify(block.input)
13738
+ }
13739
+ },
13740
+ childCtx
13741
+ );
13742
+ openTools.set(block.id, span);
13057
13743
  }
13058
13744
  }
13059
- turnText += text2;
13060
- const gen = tracer.startSpan(
13061
- "assistant",
13062
- {
13063
- attributes: {
13064
- "openinference.span.kind": "LLM",
13065
- "llm.model_name": model,
13066
- "llm.provider": "anthropic",
13067
- "output.value": text2 || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
13068
- }
13069
- },
13070
- childCtx
13071
- );
13072
- const usage = message.message?.usage;
13073
- if (usage?.input_tokens != null)
13074
- gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
13075
- if (usage?.output_tokens != null)
13076
- gen.setAttribute("llm.token_count.completion", usage.output_tokens);
13077
- gen.end();
13078
- for (const t of toolUses) {
13079
- const span = tracer.startSpan(
13080
- `tool.${cleanToolName(t.name)}`,
13081
- {
13082
- attributes: {
13083
- "openinference.span.kind": "TOOL",
13084
- "tool.name": cleanToolName(t.name),
13085
- "input.value": JSON.stringify(t.input)
13086
- }
13087
- },
13088
- childCtx
13089
- );
13090
- openTools.set(t.id, span);
13091
- }
13745
+ if (message.message?.usage) genUsage = message.message.usage;
13092
13746
  }
13093
13747
  if (message?.type === "user") {
13094
13748
  const content = message.message?.content;
@@ -13113,6 +13767,7 @@ async function* observeClaude(stream, options) {
13113
13767
  yield message;
13114
13768
  }
13115
13769
  } finally {
13770
+ endGeneration();
13116
13771
  for (const span of openTools.values()) span.end();
13117
13772
  openTools.clear();
13118
13773
  if (options.perTurn) {
@@ -13176,6 +13831,7 @@ var tracing = {
13176
13831
  Persona,
13177
13832
  PersonaError,
13178
13833
  PersonasClient,
13834
+ PushSlugConflict,
13179
13835
  REPLAY_ENVIRONMENT,
13180
13836
  ReplayCase,
13181
13837
  ReplayCheck,
@@ -13202,6 +13858,7 @@ var tracing = {
13202
13858
  UsersClient,
13203
13859
  WHITESPACE,
13204
13860
  WorldBrowser,
13861
+ WorldSandboxClient,
13205
13862
  WorldSession,
13206
13863
  WorldSessionCallError,
13207
13864
  WorldSessionTimeout,
@@ -13212,12 +13869,14 @@ var tracing = {
13212
13869
  aggregateTaskGrade,
13213
13870
  apiRequest,
13214
13871
  applyTransforms,
13872
+ archiveMode,
13215
13873
  assembleManifest,
13216
13874
  assignVariant,
13217
13875
  attachManifest,
13218
13876
  attachSession,
13219
13877
  attachTask,
13220
13878
  basicAuthHeader,
13879
+ buildFileManifest,
13221
13880
  buildSourceTarball,
13222
13881
  canonicalJson,
13223
13882
  captureToolCalls,
@@ -13238,6 +13897,7 @@ var tracing = {
13238
13897
  detectProvenance,
13239
13898
  experimentContextStorage,
13240
13899
  experiments,
13900
+ extractBundle,
13241
13901
  files,
13242
13902
  gateway,
13243
13903
  getExperimentContext,
@@ -13252,7 +13912,6 @@ var tracing = {
13252
13912
  identify,
13253
13913
  ingest,
13254
13914
  ingestToolCalls,
13255
- isIncludedInContentHash,
13256
13915
  isTaskIdRef,
13257
13916
  isTaskManifest,
13258
13917
  isTaskSpec,
@@ -13270,6 +13929,8 @@ var tracing = {
13270
13929
  openSession,
13271
13930
  openTask,
13272
13931
  openWorld,
13932
+ openWorldVersion,
13933
+ parseSlugRef,
13273
13934
  parseToolCallRecord,
13274
13935
  parseTransform,
13275
13936
  parseWorldTaskRefs,
@@ -13293,6 +13954,7 @@ var tracing = {
13293
13954
  replay,
13294
13955
  resetBuildRegistry,
13295
13956
  resolveBrowserMode,
13957
+ resolvePushSlug,
13296
13958
  resolveUserId,
13297
13959
  roundForm,
13298
13960
  runSessions,