@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.mjs CHANGED
@@ -181,10 +181,10 @@ var init_users = __esm({
181
181
  totalItems: Number(meta.totalItems ?? rows.length)
182
182
  };
183
183
  }
184
- async requestJson(method, path13, payload) {
184
+ async requestJson(method, path14, payload) {
185
185
  let response;
186
186
  try {
187
- response = await fetch(`${this.host}${path13}`, {
187
+ response = await fetch(`${this.host}${path14}`, {
188
188
  method,
189
189
  headers: {
190
190
  Authorization: this.authHeader(),
@@ -195,17 +195,17 @@ var init_users = __esm({
195
195
  });
196
196
  } catch (error) {
197
197
  throw new UserProfileError(
198
- `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
198
+ `${method} ${path14} failed: ${error instanceof Error ? error.message : String(error)}`,
199
199
  { cause: error }
200
200
  );
201
201
  }
202
202
  const text2 = await response.text();
203
203
  if (!response.ok) {
204
204
  throw new UserProfileError(
205
- `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
205
+ `${method} ${path14} failed (${response.status}): ${text2.slice(0, 500)}`
206
206
  );
207
207
  }
208
- this.log("%s %s -> %s", method, path13, response.status);
208
+ this.log("%s %s -> %s", method, path14, response.status);
209
209
  return safeJson4(text2);
210
210
  }
211
211
  authHeader() {
@@ -394,10 +394,10 @@ var init_scores = __esm({
394
394
  metadata: signalMetadata
395
395
  });
396
396
  }
397
- async request(method, path13, payload) {
397
+ async request(method, path14, payload) {
398
398
  let response;
399
399
  try {
400
- response = await fetch(`${this.host}${path13}`, {
400
+ response = await fetch(`${this.host}${path14}`, {
401
401
  method,
402
402
  headers: {
403
403
  Authorization: this.authHeader(),
@@ -408,17 +408,17 @@ var init_scores = __esm({
408
408
  });
409
409
  } catch (error) {
410
410
  throw new ScoreError(
411
- `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
411
+ `${method} ${path14} failed: ${error instanceof Error ? error.message : String(error)}`,
412
412
  { cause: error }
413
413
  );
414
414
  }
415
415
  const text2 = await response.text();
416
416
  if (!response.ok) {
417
417
  throw new ScoreError(
418
- `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
418
+ `${method} ${path14} failed (${response.status}): ${text2.slice(0, 500)}`
419
419
  );
420
420
  }
421
- this.log("%s %s -> %s", method, path13, response.status);
421
+ this.log("%s %s -> %s", method, path14, response.status);
422
422
  }
423
423
  authHeader() {
424
424
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -449,9 +449,9 @@ function surfaceKinds(s) {
449
449
  if (s.browser) kinds.push("browser");
450
450
  return kinds;
451
451
  }
452
- function uiUrl(ui, path13) {
452
+ function uiUrl(ui, path14) {
453
453
  const base = ui.url.replace(/\/+$/, "");
454
- const rel = path13.startsWith("/") ? path13 : `/${path13}`;
454
+ const rel = path14.startsWith("/") ? path14 : `/${path14}`;
455
455
  if (!ui.token) return base + rel;
456
456
  const [beforeHash, hash] = splitHash(rel);
457
457
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -1009,12 +1009,13 @@ async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
1009
1009
  );
1010
1010
  const at = slugOrRef.indexOf("@");
1011
1011
  const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
1012
+ const ref = at === -1 ? "" : slugOrRef.slice(at + 1);
1012
1013
  const state2 = await apiRequest(
1013
1014
  options,
1014
1015
  "POST",
1015
1016
  "/api/public/world-sessions",
1016
1017
  {
1017
- ...opts.versionId ? { versionId: opts.versionId } : { slug },
1018
+ ...opts.versionId ? { versionId: opts.versionId } : { slug, ...ref && { ref } },
1018
1019
  task,
1019
1020
  ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
1020
1021
  ...opts.surfaces?.length ? { surfaces: opts.surfaces } : {}
@@ -1028,8 +1029,8 @@ async function runSessions(opts) {
1028
1029
  let tasks = opts.tasks;
1029
1030
  let versionId = opts.versionId;
1030
1031
  if (!tasks || !versionId) {
1031
- const { openWorld: openWorld2 } = await Promise.resolve().then(() => (init_worlds(), worlds_exports));
1032
- const world = await openWorld2(opts.world, options);
1032
+ const { openWorld: openWorld2, openWorldVersion: openWorldVersion2 } = await Promise.resolve().then(() => (init_worlds(), worlds_exports));
1033
+ const world = versionId ? await openWorldVersion2(opts.world.split("@", 1)[0], versionId, options) : await openWorld2(opts.world, options);
1033
1034
  tasks = tasks ?? world.tasks.map((t) => t.name);
1034
1035
  versionId = versionId ?? world.version.versionId;
1035
1036
  }
@@ -1085,10 +1086,12 @@ async function runSessions(opts) {
1085
1086
  })
1086
1087
  );
1087
1088
  const mean = results.reduce((sum, r) => sum + (r.reward ?? 0), 0) / (results.length || 1);
1089
+ const failed = results.filter((r) => r.error).length;
1088
1090
  return {
1089
1091
  results: results.sort((a, b) => a.task.localeCompare(b.task)),
1090
1092
  mean,
1091
- passed: opts.failUnder === void 0 ? true : mean >= opts.failUnder
1093
+ failed,
1094
+ passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
1092
1095
  };
1093
1096
  }
1094
1097
  var 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;
@@ -1212,18 +1215,18 @@ var init_session = __esm({
1212
1215
  }
1213
1216
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
1214
1217
  browserRecorder(agentId) {
1215
- const path13 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
1218
+ const path14 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
1216
1219
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
1217
1220
  return new BrowserActionRecorder(
1218
1221
  async (events) => {
1219
- await apiRequest(this.options, "POST", path13, { events });
1222
+ await apiRequest(this.options, "POST", path14, { events });
1220
1223
  },
1221
1224
  id,
1222
1225
  {
1223
1226
  // Continue this agent's timeline where the platform has it, so the
1224
1227
  // same id reopened later never re-numbers from zero.
1225
1228
  resumeFrom: async () => {
1226
- const summary = await apiRequest(this.options, "GET", path13);
1229
+ const summary = await apiRequest(this.options, "GET", path14);
1227
1230
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
1228
1231
  }
1229
1232
  }
@@ -1986,7 +1989,7 @@ var require_package = __commonJS({
1986
1989
  "package.json"(exports, module) {
1987
1990
  module.exports = {
1988
1991
  name: "@withgateway/sdk",
1989
- version: "0.2.4",
1992
+ version: "0.2.6",
1990
1993
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
1991
1994
  main: "./dist/index.js",
1992
1995
  module: "./dist/index.mjs",
@@ -2090,6 +2093,7 @@ var require_package = __commonJS({
2090
2093
  "typecheck:examples": "tsc -p tsconfig.examples.json --noEmit",
2091
2094
  test: "vitest",
2092
2095
  "test:run": "vitest run",
2096
+ "test:integration": "vitest run --config vitest.integration.config.ts",
2093
2097
  lint: "eslint src --ext .ts",
2094
2098
  clean: "rm -rf dist",
2095
2099
  example: "tsx examples/test-ab-experiment.ts",
@@ -2808,7 +2812,7 @@ async function fileTaskRun(live, runId, model, report, grade, agentError, traced
2808
2812
  task: live.name ?? live.taskId ?? runId,
2809
2813
  rolloutIndex: 0,
2810
2814
  prompt: live.instruction,
2811
- completion: report,
2815
+ ...report === null ? {} : { completion: report },
2812
2816
  rewards: grade.rewards,
2813
2817
  info: {
2814
2818
  sessionId: runId,
@@ -2846,17 +2850,20 @@ async function fileTaskRun(live, runId, model, report, grade, agentError, traced
2846
2850
  return evaluationId;
2847
2851
  }
2848
2852
  function reportFromResult(result) {
2853
+ if (result === void 0 || result === null)
2854
+ return { report: null, extra: {} };
2849
2855
  if (typeof result === "string") return { report: result, extra: {} };
2850
- if (result && typeof result === "object") {
2851
- const { report, ...extra } = result;
2852
- return {
2853
- report: report === void 0 || report === null ? null : typeof report === "string" ? report : String(report),
2854
- extra
2855
- };
2856
- }
2857
- if (result !== void 0 && result !== null)
2858
- return { report: String(result), extra: {} };
2859
- return { report: null, extra: {} };
2856
+ if (typeof result !== "object" || Array.isArray(result))
2857
+ throw new Error(
2858
+ `agent returned ${describeValue(result)}; ${RESULT_CONTRACT}`
2859
+ );
2860
+ const { report, ...extra } = result;
2861
+ if (report === void 0 || report === null) return { report: null, extra };
2862
+ if (typeof report !== "string")
2863
+ throw new Error(
2864
+ `agent returned a report of type ${describeValue(report)}; ${RESULT_CONTRACT}`
2865
+ );
2866
+ return { report, extra };
2860
2867
  }
2861
2868
  async function runTask(ref, agent, opts) {
2862
2869
  const runId = `wtr-${randomBytes(6).toString("hex")}`;
@@ -2960,7 +2967,7 @@ async function runTask(ref, agent, opts) {
2960
2967
  }
2961
2968
  }
2962
2969
  }
2963
- var TaskRun;
2970
+ var TaskRun, describeValue, RESULT_CONTRACT;
2964
2971
  var init_taskRun = __esm({
2965
2972
  "src/worlds/taskRun.ts"() {
2966
2973
  "use strict";
@@ -3031,6 +3038,154 @@ var init_taskRun = __esm({
3031
3038
  Object.assign(this.notes, notes);
3032
3039
  }
3033
3040
  };
3041
+ describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
3042
+ RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
3043
+ }
3044
+ });
3045
+
3046
+ // src/worlds/worldDir.ts
3047
+ import { createHash as createHash3 } from "crypto";
3048
+ import * as fs2 from "fs";
3049
+ import * as path2 from "path";
3050
+ var 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;
3051
+ var init_worldDir = __esm({
3052
+ "src/worlds/worldDir.ts"() {
3053
+ "use strict";
3054
+ WORLD_MANIFEST = "gateway-env.toml";
3055
+ API_BUNDLE_MANIFEST = "gateway-api-world.toml";
3056
+ BUNDLED_WORLD_PREFIX = "server/world/";
3057
+ CONNECTOR_SPEC = "connector.toml";
3058
+ PYPROJECT = "pyproject.toml";
3059
+ WORLD_SCHEMA_MARKERS = ["schema/world.json", "schema/runtime.json", "schema/lock.json"];
3060
+ WORLD_ROOT_MARKERS = [CONNECTOR_SPEC, ...WORLD_SCHEMA_MARKERS];
3061
+ BROAD_ROOT_MARKERS = [WORLD_MANIFEST, API_BUNDLE_MANIFEST];
3062
+ SKIPPED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
3063
+ V1_DEP = /["']verifiers\s*(?:\[[^\]]*\])?\s*(?:>=|~=|==|>)\s*(?:0\.(?:[2-9]\d*|[1-9]\d+)|[1-9]\d*)/;
3064
+ V1_IMPORT = /^\s*(?:from\s+verifiers\.v1\s+import\b|from\s+verifiers\s+import\s+v1\b|import\s+verifiers\.v1\b)/m;
3065
+ V1_TASKSET = /class\s+\w+\s*\(\s*(?:(?:vf|v1|verifiers(?:\.v1)?)\.)?Taskset\s*[,)]/;
3066
+ HARBOR_TASK = /^(?:tasks\/)?([^/]+)\/task\.toml$/;
3067
+ skippedDir = (name) => name.startsWith(".") || SKIPPED_DIRS.has(name) || name.endsWith(".egg-info");
3068
+ byCodePoint = (a, b) => Buffer.compare(Buffer.from(a, "utf-8"), Buffer.from(b, "utf-8"));
3069
+ WorldDir = class _WorldDir {
3070
+ constructor(dir, worldRoot) {
3071
+ this.dir = dir;
3072
+ this.worldRoot = worldRoot;
3073
+ }
3074
+ pathsMemo;
3075
+ static open(dir) {
3076
+ const present = (rel) => fs2.existsSync(path2.join(dir, rel));
3077
+ const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
3078
+ return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
3079
+ }
3080
+ // ── Files ──────────────────────────────────────────────────────────────
3081
+ has(rel) {
3082
+ return fs2.existsSync(path2.join(this.dir, rel));
3083
+ }
3084
+ text(rel) {
3085
+ try {
3086
+ return fs2.readFileSync(path2.join(this.dir, rel), "utf-8");
3087
+ } catch {
3088
+ return null;
3089
+ }
3090
+ }
3091
+ /** A path under the authored world's root. */
3092
+ worldPath(rel) {
3093
+ return this.worldRoot + rel;
3094
+ }
3095
+ /** Every regular file the archive rules keep (`_archive.py`: no hidden,
3096
+ * dist, build, outputs, pycache or egg-info directories), as sorted
3097
+ * relpaths. Root files enter only when a Gateway manifest widens the root
3098
+ * or they are README.md, pyproject.toml or Python. */
3099
+ get archivePaths() {
3100
+ if (this.pathsMemo) return this.pathsMemo;
3101
+ const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
3102
+ const out = [];
3103
+ const walk = (rel) => {
3104
+ for (const entry of fs2.readdirSync(rel ? path2.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
3105
+ const child = rel ? `${rel}/${entry.name}` : entry.name;
3106
+ if (entry.isDirectory()) {
3107
+ if (!skippedDir(entry.name)) walk(child);
3108
+ } else if (entry.isFile() && !entry.name.startsWith(".")) {
3109
+ const rootKept = broad || ["README.md", PYPROJECT].includes(entry.name) || entry.name.endsWith(".py");
3110
+ if (rel || rootKept) out.push(child);
3111
+ }
3112
+ }
3113
+ };
3114
+ walk("");
3115
+ return this.pathsMemo = out.sort(byCodePoint);
3116
+ }
3117
+ /** A root `.gitignore` is present: the Python SDK honours it when
3118
+ * collecting, this package does not reimplement gitignore. */
3119
+ get hasRootGitignore() {
3120
+ return this.has(".gitignore");
3121
+ }
3122
+ /** SHA-256 over `utf8("file:" + relpath)` + bytes per archive path in
3123
+ * sorted order: the version's identity, byte-compatible with
3124
+ * `_archive.compute_content_hash` and Prime Intellect. */
3125
+ get contentHash() {
3126
+ const hasher = createHash3("sha256");
3127
+ for (const rel of this.archivePaths) {
3128
+ hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
3129
+ hasher.update(fs2.readFileSync(path2.join(this.dir, rel)));
3130
+ }
3131
+ return hasher.digest("hex");
3132
+ }
3133
+ // ── Markers ────────────────────────────────────────────────────────────
3134
+ get isApiBundle() {
3135
+ return this.has(API_BUNDLE_MANIFEST);
3136
+ }
3137
+ get hasManifest() {
3138
+ return this.has(this.worldPath(WORLD_MANIFEST));
3139
+ }
3140
+ get hasSchemaMarker() {
3141
+ return WORLD_SCHEMA_MARKERS.some((m) => this.has(this.worldPath(m)));
3142
+ }
3143
+ /** Harbor task dirs, relative to the tree: `tasks/<name>` when a `tasks/`
3144
+ * dir holds them, else the root `<name>` dirs. */
3145
+ taskDirs() {
3146
+ const names = /* @__PURE__ */ new Set();
3147
+ for (const rel of this.archivePaths) {
3148
+ const m = HARBOR_TASK.exec(rel);
3149
+ if (m) names.add(rel.startsWith("tasks/") ? `tasks/${m[1]}` : m[1]);
3150
+ }
3151
+ const under = [...names].filter((n) => n.startsWith("tasks/"));
3152
+ return (under.length ? under : [...names]).sort();
3153
+ }
3154
+ /** The Python package of a verifiers tree (`<pkg>/__init__.py` or
3155
+ * `src/<pkg>/__init__.py`), or null. */
3156
+ pythonPackage() {
3157
+ if (!this.has(PYPROJECT)) return null;
3158
+ for (const base of ["", "src/"]) {
3159
+ const init3 = this.archivePaths.find((rel) => {
3160
+ const m = /^([^/]+)\/__init__\.py$/.exec(rel.startsWith(base) ? rel.slice(base.length) : "");
3161
+ return m !== null && m[1] !== "tests";
3162
+ });
3163
+ if (init3) return { name: init3.slice(base.length).split("/")[0], init: init3 };
3164
+ }
3165
+ return null;
3166
+ }
3167
+ /** The package targets the `verifiers.v1` API: read from the root
3168
+ * pyproject and every archived Python module. */
3169
+ get isVerifiersV1() {
3170
+ for (const rel of this.archivePaths) {
3171
+ if (rel !== PYPROJECT && !rel.endsWith(".py")) continue;
3172
+ const text2 = this.text(rel);
3173
+ if (text2 === null) continue;
3174
+ if (rel === PYPROJECT ? V1_DEP.test(text2) : V1_IMPORT.test(text2) || text2.includes("__all__") && V1_TASKSET.test(text2))
3175
+ return true;
3176
+ }
3177
+ return false;
3178
+ }
3179
+ // ── The answer ─────────────────────────────────────────────────────────
3180
+ /** The runner's kind (`detect_env_kind`): api / mock-tools win over
3181
+ * everything, a harbor task tree next, then the verifiers API version. */
3182
+ get kind() {
3183
+ if (this.isApiBundle) return "api";
3184
+ if (this.has(WORLD_MANIFEST)) return "mock-tools";
3185
+ if (this.taskDirs().length) return "harbor";
3186
+ return this.isVerifiersV1 ? "verifiers-v1" : "verifiers";
3187
+ }
3188
+ };
3034
3189
  }
3035
3190
  });
3036
3191
 
@@ -3038,79 +3193,54 @@ var init_taskRun = __esm({
3038
3193
  var hub_exports = {};
3039
3194
  __export(hub_exports, {
3040
3195
  DEFAULT_BRANCH: () => DEFAULT_BRANCH,
3196
+ PushSlugConflict: () => PushSlugConflict,
3197
+ archiveMode: () => archiveMode,
3198
+ buildFileManifest: () => buildFileManifest,
3041
3199
  buildSourceTarball: () => buildSourceTarball,
3042
3200
  collectArchiveRelPaths: () => collectArchiveRelPaths,
3043
3201
  computeContentHash: () => computeContentHash,
3044
3202
  createRef: () => createRef,
3045
- isIncludedInContentHash: () => isIncludedInContentHash,
3203
+ extractBundle: () => extractBundle,
3046
3204
  localTreeState: () => localTreeState,
3047
3205
  propose: () => propose,
3048
3206
  pull: () => pull,
3049
3207
  push: () => push,
3050
3208
  pushSlug: () => pushSlug,
3051
3209
  readLocalMeta: () => readLocalMeta,
3210
+ resolvePushSlug: () => resolvePushSlug,
3052
3211
  snapshotTreeHashes: () => snapshotTreeHashes,
3053
3212
  writeLocalMeta: () => writeLocalMeta
3054
3213
  });
3055
- import { createHash as createHash3 } from "crypto";
3214
+ import { createHash as createHash4 } from "crypto";
3056
3215
  import { gzipSync, gunzipSync } from "zlib";
3057
- import * as fs2 from "fs";
3058
- import * as path2 from "path";
3059
- function isExcludedDirComponent(component) {
3060
- return component.startsWith(".") || ROOT_EXCLUDED_DIRS.has(component) || component.endsWith(".egg-info");
3061
- }
3062
- function isIncludedInContentHash(relPath, includeAllRootFiles) {
3063
- const parts2 = relPath.split("/");
3064
- const name = parts2[parts2.length - 1];
3065
- const dirParts = parts2.slice(0, -1);
3066
- if (name.startsWith(".")) return false;
3067
- if (parts2.includes("__pycache__")) return false;
3068
- if (dirParts.length === 0) {
3069
- return includeAllRootFiles || name === "README.md" || name === "pyproject.toml" || BROAD_ROOT_MARKERS.includes(name) || name.endsWith(".py");
3070
- }
3071
- return !dirParts.some(isExcludedDirComponent);
3072
- }
3216
+ import * as fs3 from "fs";
3217
+ import * as path3 from "path";
3073
3218
  function collectArchiveRelPaths(dir) {
3074
- if (fs2.existsSync(path2.join(dir, ".gitignore"))) {
3219
+ const world = WorldDir.open(dir);
3220
+ if (world.hasRootGitignore) {
3075
3221
  throw new Error(
3076
3222
  "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."
3077
3223
  );
3078
3224
  }
3079
- const out = [];
3080
- const includeAllRootFiles = BROAD_ROOT_MARKERS.some(
3081
- (marker) => fs2.existsSync(path2.join(dir, marker))
3082
- );
3083
- const walk = (rel) => {
3084
- const abs = rel ? path2.join(dir, rel) : dir;
3085
- for (const entry of fs2.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
3086
- const childRel = rel ? `${rel}/${entry.name}` : entry.name;
3087
- if (entry.isDirectory()) {
3088
- if (!isExcludedDirComponent(entry.name)) walk(childRel);
3089
- } else if (entry.isFile()) {
3090
- if (isIncludedInContentHash(childRel, includeAllRootFiles)) {
3091
- out.push(childRel);
3092
- }
3093
- }
3094
- }
3095
- };
3096
- walk("");
3097
- return out.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
3225
+ return world.archivePaths;
3098
3226
  }
3099
3227
  function computeContentHash(dir) {
3100
- const hasher = createHash3("sha256");
3101
- for (const relPath of collectArchiveRelPaths(dir)) {
3102
- hasher.update(Buffer.from(`file:${relPath}`, "utf-8"));
3103
- hasher.update(fs2.readFileSync(path2.join(dir, relPath)));
3104
- }
3105
- return hasher.digest("hex");
3228
+ collectArchiveRelPaths(dir);
3229
+ return WorldDir.open(dir).contentHash;
3106
3230
  }
3107
3231
  function octal(value, length) {
3108
- return Buffer.from(value.toString(8).padStart(length - 1, "0") + "\0", "ascii");
3232
+ return Buffer.from(
3233
+ value.toString(8).padStart(length - 1, "0") + "\0",
3234
+ "ascii"
3235
+ );
3236
+ }
3237
+ function archiveMode(mode) {
3238
+ return (mode & 73) !== 0 ? EXEC_MODE : FILE_MODE;
3109
3239
  }
3110
- function tarHeader(name, size) {
3240
+ function tarHeader(name, size, mode = FILE_MODE) {
3111
3241
  const header = Buffer.alloc(512);
3112
3242
  header.write(name, 0, 100, "utf-8");
3113
- octal(420, 8).copy(header, 100);
3243
+ octal(mode, 8).copy(header, 100);
3114
3244
  octal(0, 8).copy(header, 108);
3115
3245
  octal(0, 8).copy(header, 116);
3116
3246
  octal(size, 12).copy(header, 124);
@@ -3124,7 +3254,7 @@ function tarHeader(name, size) {
3124
3254
  header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, "ascii");
3125
3255
  return header;
3126
3256
  }
3127
- function tarMember(name, payload) {
3257
+ function tarMember(name, payload, mode) {
3128
3258
  const parts2 = [];
3129
3259
  const nameBytes = Buffer.byteLength(name, "utf-8");
3130
3260
  if (nameBytes > 100) {
@@ -3138,9 +3268,9 @@ function tarMember(name, payload) {
3138
3268
  parts2.push(lHeader, body);
3139
3269
  const pad2 = body.length % 512 === 0 ? 0 : 512 - body.length % 512;
3140
3270
  if (pad2) parts2.push(Buffer.alloc(pad2));
3141
- parts2.push(tarHeader(name.slice(0, 100), payload.length));
3271
+ parts2.push(tarHeader(name.slice(0, 100), payload.length, mode));
3142
3272
  } else {
3143
- parts2.push(tarHeader(name, payload.length));
3273
+ parts2.push(tarHeader(name, payload.length, mode));
3144
3274
  }
3145
3275
  parts2.push(payload);
3146
3276
  const pad = payload.length % 512 === 0 ? 0 : 512 - payload.length % 512;
@@ -3150,14 +3280,21 @@ function tarMember(name, payload) {
3150
3280
  function buildSourceTarball(dir) {
3151
3281
  const parts2 = [];
3152
3282
  for (const relPath of collectArchiveRelPaths(dir)) {
3153
- parts2.push(...tarMember(relPath, fs2.readFileSync(path2.join(dir, relPath))));
3283
+ const abs = path3.join(dir, relPath);
3284
+ parts2.push(
3285
+ ...tarMember(
3286
+ relPath,
3287
+ fs3.readFileSync(abs),
3288
+ archiveMode(fs3.statSync(abs).mode)
3289
+ )
3290
+ );
3154
3291
  }
3155
3292
  parts2.push(Buffer.alloc(1024));
3156
3293
  return gzipSync(Buffer.concat(parts2), { level: 9 });
3157
3294
  }
3158
3295
  function readLocalMeta(dir) {
3159
3296
  try {
3160
- const raw = fs2.readFileSync(path2.join(dir, META_RELPATH), "utf-8");
3297
+ const raw = fs3.readFileSync(path3.join(dir, META_RELPATH), "utf-8");
3161
3298
  const parsed = JSON.parse(raw);
3162
3299
  return parsed && typeof parsed === "object" ? parsed : null;
3163
3300
  } catch {
@@ -3165,19 +3302,31 @@ function readLocalMeta(dir) {
3165
3302
  }
3166
3303
  }
3167
3304
  function pushSlug(dir, explicit) {
3168
- if (explicit) return explicit;
3305
+ return resolvePushSlug(dir, { explicit }).slug;
3306
+ }
3307
+ function resolvePushSlug(dir, opts = {}) {
3169
3308
  const meta = readLocalMeta(dir);
3170
- if (typeof meta?.slug === "string" && meta.slug) return meta.slug;
3171
- return readPyproject(dir).name ?? path2.basename(path2.resolve(dir));
3309
+ const tracked = typeof meta?.slug === "string" && meta.slug ? meta.slug : null;
3310
+ const declared = Object.entries({ ...opts.declared ?? {}, "pyproject.toml": readPyproject(dir).name }).filter((entry) => typeof entry[1] === "string" && entry[1] !== "").map(([source, slug]) => ({ source, slug }));
3311
+ const base = { tracked, declared, conflict: null };
3312
+ if (opts.explicit) return { ...base, slug: opts.explicit, source: "--slug" };
3313
+ if (tracked) {
3314
+ const minted = (d) => tracked.startsWith(d.slug) && MINTED_SUFFIX.test(tracked.slice(d.slug.length));
3315
+ const conflict = declared.find((d) => d.slug !== tracked && !minted(d)) ?? null;
3316
+ return { ...base, slug: tracked, source: ".gateway metadata", conflict };
3317
+ }
3318
+ const first = declared[0];
3319
+ if (first) return { ...base, slug: first.slug, source: first.source };
3320
+ return { ...base, slug: path3.basename(path3.resolve(dir)), source: "directory name" };
3172
3321
  }
3173
3322
  function writeLocalMeta(dir, meta) {
3174
- fs2.mkdirSync(path2.join(dir, ".gateway"), { recursive: true });
3175
- fs2.writeFileSync(path2.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
3323
+ fs3.mkdirSync(path3.join(dir, ".gateway"), { recursive: true });
3324
+ fs3.writeFileSync(path3.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
3176
3325
  }
3177
3326
  function snapshotTreeHashes(dir) {
3178
3327
  const out = {};
3179
3328
  for (const relPath of collectArchiveRelPaths(dir)) {
3180
- out[relPath] = createHash3("sha256").update(fs2.readFileSync(path2.join(dir, relPath))).digest("hex");
3329
+ out[relPath] = createHash4("sha256").update(fs3.readFileSync(path3.join(dir, relPath))).digest("hex");
3181
3330
  }
3182
3331
  return out;
3183
3332
  }
@@ -3195,7 +3344,7 @@ function localTreeState(dir) {
3195
3344
  }
3196
3345
  function readPyproject(dir) {
3197
3346
  try {
3198
- const text2 = fs2.readFileSync(path2.join(dir, "pyproject.toml"), "utf-8");
3347
+ const text2 = fs3.readFileSync(path3.join(dir, "pyproject.toml"), "utf-8");
3199
3348
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
3200
3349
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
3201
3350
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -3205,39 +3354,27 @@ function readPyproject(dir) {
3205
3354
  }
3206
3355
  }
3207
3356
  function readWorldContractVersion(dir) {
3208
- for (const rel of ["schema/world.json", path2.join("server", "world", "schema", "world.json")]) {
3209
- try {
3210
- const parsed = JSON.parse(fs2.readFileSync(path2.join(dir, rel), "utf-8"));
3211
- const pkg = parsed["x-gateway"]?.package;
3212
- if (pkg?.version) return { name: pkg.id, version: pkg.version };
3213
- } catch {
3214
- }
3357
+ const world = WorldDir.open(dir);
3358
+ try {
3359
+ const parsed = JSON.parse(
3360
+ world.text(world.worldPath("schema/world.json")) ?? ""
3361
+ );
3362
+ const pkg = parsed["x-gateway"]?.package;
3363
+ if (pkg?.version) return { name: pkg.id, version: pkg.version };
3364
+ } catch {
3215
3365
  }
3216
3366
  return {};
3217
3367
  }
3218
3368
  function buildFileManifest(dir) {
3219
- const entries = [];
3220
- const walk = (rel) => {
3221
- const abs = rel ? path2.join(dir, rel) : dir;
3222
- for (const entry of fs2.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
3223
- const childRel = rel ? `${rel}/${entry.name}` : entry.name;
3224
- if (entry.isDirectory()) {
3225
- if (!SKIP_DIRS.has(entry.name) && !entry.name.endsWith(".egg-info")) {
3226
- walk(childRel);
3227
- }
3228
- } else if (entry.isFile()) {
3229
- const payload = fs2.readFileSync(path2.join(dir, childRel));
3230
- entries.push({
3231
- path: childRel,
3232
- isDirectory: false,
3233
- size: payload.length,
3234
- sha256: createHash3("sha256").update(payload).digest("hex")
3235
- });
3236
- }
3237
- }
3238
- };
3239
- walk("");
3240
- return entries;
3369
+ return collectArchiveRelPaths(dir).map((relPath) => {
3370
+ const payload = fs3.readFileSync(path3.join(dir, relPath));
3371
+ return {
3372
+ path: relPath,
3373
+ isDirectory: false,
3374
+ size: payload.length,
3375
+ sha256: createHash4("sha256").update(payload).digest("hex")
3376
+ };
3377
+ });
3241
3378
  }
3242
3379
  async function putObject(url, data, shaB64) {
3243
3380
  const resp = await fetch(url, {
@@ -3249,31 +3386,39 @@ async function putObject(url, data, shaB64) {
3249
3386
  body: new Uint8Array(data)
3250
3387
  });
3251
3388
  if (!resp.ok) {
3252
- throw new Error(`object PUT -> ${resp.status}: ${(await resp.text()).slice(0, 300)}`);
3389
+ throw new Error(
3390
+ `object PUT -> ${resp.status}: ${(await resp.text()).slice(0, 300)}`
3391
+ );
3253
3392
  }
3254
3393
  }
3255
3394
  async function push(dir, opts = {}) {
3256
3395
  const options = opts.options ?? {};
3257
- if (!fs2.statSync(dir).isDirectory()) throw new Error(`Not a directory: ${dir}`);
3396
+ if (!fs3.statSync(dir).isDirectory())
3397
+ throw new Error(`Not a directory: ${dir}`);
3258
3398
  const localMeta = readLocalMeta(dir) ?? {};
3259
3399
  const branch = opts.branch ?? localMeta.branch ?? DEFAULT_BRANCH;
3260
3400
  const baseVersionId = opts.baseVersionId ?? localMeta.versionId ?? null;
3401
+ const fileManifest = buildFileManifest(dir);
3402
+ if (fileManifest.length === 0)
3403
+ throw new Error(
3404
+ `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).`
3405
+ );
3261
3406
  const contentHash = computeContentHash(dir);
3262
3407
  const pyproject = readPyproject(dir);
3263
3408
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
3264
3409
  if (contractVersion) pyproject.version = contractVersion;
3265
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path2.basename(path2.resolve(dir));
3410
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path3.basename(path3.resolve(dir));
3411
+ const metadata = {
3412
+ ...opts.name !== void 0 ? { name: opts.name } : {},
3413
+ ...opts.description !== void 0 ? { description: opts.description } : {},
3414
+ ...opts.tags !== void 0 ? { tags: opts.tags } : {},
3415
+ ...opts.visibility ? { visibility: opts.visibility } : {}
3416
+ };
3266
3417
  const resolve10 = await apiRequest(
3267
3418
  options,
3268
3419
  "POST",
3269
3420
  "/api/public/benchmark-containers/resolve",
3270
- {
3271
- slug,
3272
- name: opts.name ?? pyproject.name ?? slug,
3273
- ...opts.description ? { description: opts.description } : {},
3274
- ...opts.tags?.length ? { tags: opts.tags } : {},
3275
- ...opts.visibility ? { visibility: opts.visibility } : {}
3276
- }
3421
+ { slug, ...metadata }
3277
3422
  );
3278
3423
  const tarball = buildSourceTarball(dir);
3279
3424
  const bundleSha256 = sha256b64(tarball);
@@ -3284,7 +3429,7 @@ async function push(dir, opts = {}) {
3284
3429
  bundleSizeBytes: tarball.length,
3285
3430
  ...pyproject.version ? { semanticVersion: pyproject.version } : {},
3286
3431
  pyprojectMeta: pyproject,
3287
- fileManifest: buildFileManifest(dir),
3432
+ fileManifest,
3288
3433
  branch,
3289
3434
  baseVersionId,
3290
3435
  ...opts.force ? { force: true } : {},
@@ -3292,12 +3437,18 @@ async function push(dir, opts = {}) {
3292
3437
  ...opts.author ? { author: opts.author } : {},
3293
3438
  ...opts.imageRef ? { imageRef: opts.imageRef } : {}
3294
3439
  });
3295
- if (version.uploadUrl) await putObject(version.uploadUrl, tarball, bundleSha256);
3440
+ if (version.uploadUrl)
3441
+ await putObject(version.uploadUrl, tarball, bundleSha256);
3296
3442
  const final = await apiRequest(
3297
3443
  options,
3298
3444
  "POST",
3299
3445
  `/api/public/benchmark-containers/versions/${version.versionId}/finalize`,
3300
- { branch, baseVersionId, ...opts.force ? { force: true } : {} }
3446
+ {
3447
+ branch,
3448
+ baseVersionId,
3449
+ ...opts.force ? { force: true } : {},
3450
+ ...metadata
3451
+ }
3301
3452
  );
3302
3453
  writeLocalMeta(dir, {
3303
3454
  ...localMeta,
@@ -3330,7 +3481,14 @@ function extractBundle(bundle, dir) {
3330
3481
  offset += 512;
3331
3482
  if (header.every((b) => b === 0)) break;
3332
3483
  const rawName = header.subarray(0, 100).toString("utf-8").replace(/\0.*$/, "");
3333
- const size = parseInt(header.subarray(124, 136).toString("ascii").replace(/\0.*$/, "").trim(), 8) || 0;
3484
+ const size = parseInt(
3485
+ header.subarray(124, 136).toString("ascii").replace(/\0.*$/, "").trim(),
3486
+ 8
3487
+ ) || 0;
3488
+ const mode = parseInt(
3489
+ header.subarray(100, 108).toString("ascii").replace(/\0.*$/, "").trim(),
3490
+ 8
3491
+ ) || FILE_MODE;
3334
3492
  const typeflag = header.subarray(156, 157).toString("ascii");
3335
3493
  const body = tar.subarray(offset, offset + size);
3336
3494
  offset += size + (size % 512 === 0 ? 0 : 512 - size % 512);
@@ -3342,9 +3500,10 @@ function extractBundle(bundle, dir) {
3342
3500
  longName = null;
3343
3501
  if (typeflag !== "0" && typeflag !== "") continue;
3344
3502
  if (name.startsWith("/") || name.split("/").includes("..")) continue;
3345
- const target = path2.join(dir, name);
3346
- fs2.mkdirSync(path2.dirname(target), { recursive: true });
3347
- fs2.writeFileSync(target, body);
3503
+ const target = path3.join(dir, name);
3504
+ fs3.mkdirSync(path3.dirname(target), { recursive: true });
3505
+ fs3.writeFileSync(target, body, { mode: archiveMode(mode) });
3506
+ fs3.chmodSync(target, archiveMode(mode));
3348
3507
  written++;
3349
3508
  }
3350
3509
  return written;
@@ -3360,13 +3519,13 @@ async function pull(slugRef, target, options = {}) {
3360
3519
  if (!details.downloadUrl) {
3361
3520
  throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
3362
3521
  }
3363
- let dir = target ?? path2.join(process.cwd(), slug);
3522
+ let dir = target ?? path3.join(process.cwd(), slug);
3364
3523
  if (!target) {
3365
3524
  let i = 1;
3366
3525
  const base = dir;
3367
- while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
3526
+ while (fs3.existsSync(dir)) dir = `${base}-${i++}`;
3368
3527
  }
3369
- fs2.mkdirSync(dir, { recursive: true });
3528
+ fs3.mkdirSync(dir, { recursive: true });
3370
3529
  const resp = await fetch(details.downloadUrl);
3371
3530
  if (!resp.ok) throw new Error(`bundle download -> ${resp.status}`);
3372
3531
  const bundle = Buffer.from(await resp.arrayBuffer());
@@ -3391,17 +3550,21 @@ async function pull(slugRef, target, options = {}) {
3391
3550
  async function propose(dir, opts) {
3392
3551
  const meta = readLocalMeta(dir);
3393
3552
  if (!meta?.slug || !meta.baseFiles) {
3394
- throw new Error("This dir has no pull/push base \u2014 run pull first, then edit, then propose.");
3553
+ throw new Error(
3554
+ "This dir has no pull/push base \u2014 run pull first, then edit, then propose."
3555
+ );
3395
3556
  }
3396
3557
  const state2 = localTreeState(dir);
3397
- if (state2.clean) throw new Error("Nothing to propose \u2014 the tree matches its base.");
3558
+ if (state2.clean)
3559
+ throw new Error("Nothing to propose \u2014 the tree matches its base.");
3398
3560
  const changes = state2.dirty.map((relPath) => {
3399
- const abs = path2.join(dir, relPath);
3400
- if (!fs2.existsSync(abs)) return { path: relPath, action: "delete" };
3561
+ const abs = path3.join(dir, relPath);
3562
+ if (!fs3.existsSync(abs))
3563
+ return { path: relPath, action: "delete" };
3401
3564
  return {
3402
3565
  path: relPath,
3403
3566
  action: "write",
3404
- newContent: fs2.readFileSync(abs, "utf-8")
3567
+ newContent: fs3.readFileSync(abs, "utf-8")
3405
3568
  };
3406
3569
  });
3407
3570
  const res = await apiRequest(
@@ -3427,26 +3590,29 @@ async function createRef(slug, name, kind, versionId, options = {}) {
3427
3590
  ...versionId ? { versionId } : {}
3428
3591
  });
3429
3592
  }
3430
- var DEFAULT_BRANCH, META_RELPATH, SKIP_DIRS, ROOT_EXCLUDED_DIRS, BROAD_ROOT_MARKERS, sha256b64;
3593
+ var DEFAULT_BRANCH, META_RELPATH, FILE_MODE, EXEC_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64;
3431
3594
  var init_hub = __esm({
3432
3595
  "src/worlds/hub.ts"() {
3433
3596
  "use strict";
3434
3597
  init_worlds();
3598
+ init_worldDir();
3435
3599
  DEFAULT_BRANCH = "main";
3436
- META_RELPATH = path2.join(".gateway", ".env-metadata.json");
3437
- SKIP_DIRS = /* @__PURE__ */ new Set([
3438
- ".git",
3439
- "__pycache__",
3440
- "node_modules",
3441
- ".venv",
3442
- ".mypy_cache",
3443
- "dist",
3444
- "build",
3445
- "outputs"
3446
- ]);
3447
- ROOT_EXCLUDED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
3448
- BROAD_ROOT_MARKERS = ["gateway-env.toml", "gateway-api-world.toml"];
3449
- sha256b64 = (data) => createHash3("sha256").update(data).digest("base64");
3600
+ META_RELPATH = path3.join(".gateway", ".env-metadata.json");
3601
+ FILE_MODE = 420;
3602
+ EXEC_MODE = 493;
3603
+ MINTED_SUFFIX = /^-\d+$/;
3604
+ PushSlugConflict = class extends Error {
3605
+ constructor(dir, tracked, declared) {
3606
+ super(
3607
+ `${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.`
3608
+ );
3609
+ this.dir = dir;
3610
+ this.tracked = tracked;
3611
+ this.declared = declared;
3612
+ this.name = "PushSlugConflict";
3613
+ }
3614
+ };
3615
+ sha256b64 = (data) => createHash4("sha256").update(data).digest("base64");
3450
3616
  }
3451
3617
  });
3452
3618
 
@@ -3945,7 +4111,7 @@ var init_json = __esm({
3945
4111
  });
3946
4112
 
3947
4113
  // src/worlds/transforms.ts
3948
- import { createHash as createHash4 } from "crypto";
4114
+ import { createHash as createHash5 } from "crypto";
3949
4115
  function parseTransform(name) {
3950
4116
  const simple = Object.prototype.hasOwnProperty.call(SIMPLE, name) ? SIMPLE[name] : void 0;
3951
4117
  if (simple) return simple;
@@ -3969,8 +4135,8 @@ var init_transforms = __esm({
3969
4135
  "use strict";
3970
4136
  init_json();
3971
4137
  WHITESPACE = " \n\r\f\v";
3972
- WHITESPACE_EDGES = /^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$/g;
3973
- WHITESPACE_RUN = /[ \t\n\r\f\v]+/g;
4138
+ WHITESPACE_EDGES = new RegExp(`^[${WHITESPACE}]+|[${WHITESPACE}]+$`, "g");
4139
+ WHITESPACE_RUN = new RegExp(`[${WHITESPACE}]+`, "g");
3974
4140
  NOT_DIGIT = /[^0-9]/g;
3975
4141
  STRIP_PREFIX = "strip_prefix";
3976
4142
  trim = (value) => value.replace(WHITESPACE_EDGES, "");
@@ -3983,7 +4149,7 @@ var init_transforms = __esm({
3983
4149
  return at === -1 ? "" : value.slice(at + 1).toLowerCase();
3984
4150
  };
3985
4151
  stripPrefix = (prefix) => (value) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
3986
- hex = (algorithm, value) => createHash4(algorithm).update(value, "utf8").digest("hex");
4152
+ hex = (algorithm, value) => createHash5(algorithm).update(value, "utf8").digest("hex");
3987
4153
  SIMPLE = {
3988
4154
  lower: (value) => value.toLowerCase(),
3989
4155
  upper: (value) => value.toUpperCase(),
@@ -4007,7 +4173,7 @@ var init_transforms = __esm({
4007
4173
  });
4008
4174
 
4009
4175
  // src/worlds/redaction.ts
4010
- import { createHash as createHash5 } from "crypto";
4176
+ import { createHash as createHash6 } from "crypto";
4011
4177
  function pyText(value) {
4012
4178
  if (typeof value === "string") return value;
4013
4179
  if (Number.isInteger(value)) return String(value);
@@ -4016,21 +4182,21 @@ function pyText(value) {
4016
4182
  return pyFloatRepr(value);
4017
4183
  }
4018
4184
  function hashForm(slug, plaintext) {
4019
- return "sha256:" + createHash5("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
4185
+ return "sha256:" + createHash6("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
4020
4186
  }
4021
4187
  function preserveForm(slug, plaintext) {
4022
4188
  const kind = typeof plaintext === "string" ? "str" : isInt(plaintext) ? "int" : "float";
4023
- const shaped = shapeText(slug, pyText(plaintext), kind);
4024
- return kind === "str" ? shaped : Number(shaped);
4189
+ const shaped2 = shapeText(slug, pyText(plaintext), kind);
4190
+ return kind === "str" ? shaped2 : Number(shaped2);
4025
4191
  }
4026
4192
  function shapeText(slug, text2, kind) {
4027
4193
  const seed = Buffer.from(`${slug}:${text2}`, "utf8");
4028
- let digest = createHash5("sha256").update(seed).digest();
4194
+ let digest = createHash6("sha256").update(seed).digest();
4029
4195
  let cursor = 0;
4030
4196
  const out = [];
4031
4197
  for (const char of text2) {
4032
4198
  if (cursor === digest.length) {
4033
- digest = createHash5("sha256").update(Buffer.concat([digest, seed])).digest();
4199
+ digest = createHash6("sha256").update(Buffer.concat([digest, seed])).digest();
4034
4200
  cursor = 0;
4035
4201
  }
4036
4202
  const byte = digest[cursor];
@@ -4078,8 +4244,9 @@ var init_redaction = __esm({
4078
4244
  });
4079
4245
 
4080
4246
  // src/worlds/query.ts
4081
- function queryRowsSearch(entity, opts = {}) {
4247
+ function queryRowsSearch(entity, opts = {}, ref) {
4082
4248
  const search = new URLSearchParams();
4249
+ if (ref) search.set("ref", ref);
4083
4250
  search.set("entity", entity);
4084
4251
  if (opts.where && Object.keys(opts.where).length) search.set("where", JSON.stringify(opts.where));
4085
4252
  if (opts.limit !== void 0) search.set("limit", String(opts.limit));
@@ -4089,17 +4256,15 @@ function queryRowsSearch(entity, opts = {}) {
4089
4256
  if (opts.compare) search.set("compare", "true");
4090
4257
  return `?${search.toString()}`;
4091
4258
  }
4092
- async function queryRows(slug, entity, opts = {}, options = {}) {
4093
- if (slug.includes("@"))
4094
- throw new Error(
4095
- `queryRows reads a world's head version: pass the slug without @ref (got ${slug})`
4096
- );
4259
+ async function queryRows(slugRef, entity, opts = {}, options = {}) {
4260
+ const [slug, ref] = slugRef.split("@", 2);
4261
+ if (!slug || ref === "") throw new Error(`queryRows needs a slug, optionally slug@ref (got ${slugRef})`);
4097
4262
  if (!entity) throw new Error("queryRows needs an entity");
4098
4263
  if (opts.compare && !opts.across) throw new Error("queryRows: compare needs across");
4099
4264
  return apiRequest(
4100
4265
  options,
4101
4266
  "GET",
4102
- `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts)}`
4267
+ `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
4103
4268
  );
4104
4269
  }
4105
4270
  var init_query = __esm({
@@ -4155,14 +4320,23 @@ var init_router = __esm({
4155
4320
  });
4156
4321
 
4157
4322
  // src/cli/config.ts
4158
- import * as fs3 from "fs";
4323
+ import * as fs4 from "fs";
4159
4324
  import * as os from "os";
4160
- import * as path3 from "path";
4325
+ import * as path4 from "path";
4326
+ function transportDetail(error, timeoutMs) {
4327
+ if (error instanceof Error && error.name === "AbortError") return `timed out after ${Math.round(timeoutMs / 1e3)}s`;
4328
+ const cause = error instanceof Error && error.cause instanceof Error ? error.cause : void 0;
4329
+ const code = cause && "code" in cause && typeof cause.code === "string" ? `${cause.code}: ` : "";
4330
+ const message = cause?.message ?? (error instanceof Error ? error.message : String(error));
4331
+ return `${code}${message}`;
4332
+ }
4161
4333
  async function api(creds, method, route, body, init3 = {}) {
4162
4334
  const controller = new AbortController();
4163
- const timer = setTimeout(() => controller.abort(), init3.timeoutMs ?? 12e4);
4335
+ const timeoutMs = init3.timeoutMs ?? 12e4;
4336
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
4337
+ let res;
4164
4338
  try {
4165
- const res = await fetch(`${creds.host}${route}`, {
4339
+ res = await fetch(`${creds.host}${route}`, {
4166
4340
  method,
4167
4341
  headers: {
4168
4342
  "content-type": "application/json",
@@ -4172,6 +4346,11 @@ async function api(creds, method, route, body, init3 = {}) {
4172
4346
  ...body === void 0 ? {} : { body: JSON.stringify(body) },
4173
4347
  signal: controller.signal
4174
4348
  });
4349
+ } catch (error) {
4350
+ clearTimeout(timer);
4351
+ throw new UnreachableError(creds.host, method, route, transportDetail(error, timeoutMs));
4352
+ }
4353
+ try {
4175
4354
  const text2 = await res.text();
4176
4355
  if (!res.ok) throw new ApiError(res.status, method, route, text2);
4177
4356
  return text2 ? JSON.parse(text2) : {};
@@ -4179,7 +4358,7 @@ async function api(creds, method, route, body, init3 = {}) {
4179
4358
  clearTimeout(timer);
4180
4359
  }
4181
4360
  }
4182
- var ApiError;
4361
+ var ApiError, UnreachableError;
4183
4362
  var init_config = __esm({
4184
4363
  "src/cli/config.ts"() {
4185
4364
  "use strict";
@@ -4193,14 +4372,23 @@ var init_config = __esm({
4193
4372
  this.text = text2;
4194
4373
  }
4195
4374
  };
4375
+ UnreachableError = class extends CliError {
4376
+ constructor(host, method, route, detail) {
4377
+ super(`${method} ${route}: no answer from ${host} (${detail}). Check the host and the network, then retry.`, 1);
4378
+ this.host = host;
4379
+ this.method = method;
4380
+ this.route = route;
4381
+ this.detail = detail;
4382
+ }
4383
+ };
4196
4384
  }
4197
4385
  });
4198
4386
 
4199
4387
  // src/cli/runtime/local.ts
4200
4388
  import { spawn, spawnSync } from "child_process";
4201
- import * as fs4 from "fs";
4389
+ import * as fs5 from "fs";
4202
4390
  import * as net from "net";
4203
- import * as path4 from "path";
4391
+ import * as path5 from "path";
4204
4392
  function tailOf(output) {
4205
4393
  return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
4206
4394
  }
@@ -4215,16 +4403,16 @@ function runtimeRoot() {
4215
4403
  const candidates = [];
4216
4404
  if (process.env.GATEWAY_RUNTIME_PATH)
4217
4405
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
4218
- const entry = process.argv[1] && fs4.existsSync(process.argv[1]) ? path4.dirname(fs4.realpathSync(process.argv[1])) : null;
4406
+ const entry = process.argv[1] && fs5.existsSync(process.argv[1]) ? path5.dirname(fs5.realpathSync(process.argv[1])) : null;
4219
4407
  if (entry) {
4220
- candidates.push(path4.resolve(entry, "..", "runtime"));
4221
- candidates.push(path4.resolve(entry, "..", "..", "runtime"));
4222
- candidates.push(path4.resolve(entry, "..", "..", "sdk", "src"));
4408
+ candidates.push(path5.resolve(entry, "..", "runtime"));
4409
+ candidates.push(path5.resolve(entry, "..", "..", "runtime"));
4410
+ candidates.push(path5.resolve(entry, "..", "..", "sdk", "src"));
4223
4411
  }
4224
- candidates.push(path4.resolve(process.cwd(), "runtime"));
4412
+ candidates.push(path5.resolve(process.cwd(), "runtime"));
4225
4413
  for (const root of candidates)
4226
- if (fs4.existsSync(
4227
- path4.join(root, "gatewaysdk", "environment", "schema", "platform.py")
4414
+ if (fs5.existsSync(
4415
+ path5.join(root, "gatewaysdk", "environment", "schema", "platform.py")
4228
4416
  ))
4229
4417
  return root;
4230
4418
  return null;
@@ -4392,28 +4580,28 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
4392
4580
  });
4393
4581
 
4394
4582
  // src/cli/runtime/client.ts
4395
- import * as fs5 from "fs";
4396
- import * as path5 from "path";
4583
+ import * as fs6 from "fs";
4584
+ import * as path6 from "path";
4397
4585
  function isBuildOutput(relPath) {
4398
4586
  return BUILD_OUTPUT.test(relPath);
4399
4587
  }
4400
4588
  function readWorldFiles(dir, opts = {}) {
4401
- if (!fs5.existsSync(dir) || !fs5.statSync(dir).isDirectory())
4589
+ if (!fs6.existsSync(dir) || !fs6.statSync(dir).isDirectory())
4402
4590
  throw new CliError(`Not a directory: ${dir}`);
4403
4591
  const out = [];
4404
4592
  const walk = (rel) => {
4405
- const abs = rel ? path5.join(dir, rel) : dir;
4406
- const entries = fs5.readdirSync(abs, { withFileTypes: true });
4593
+ const abs = rel ? path6.join(dir, rel) : dir;
4594
+ const entries = fs6.readdirSync(abs, { withFileTypes: true });
4407
4595
  entries.sort((a, b) => a.name.localeCompare(b.name));
4408
4596
  for (const entry of entries) {
4409
4597
  const childRel = rel ? `${rel}/${entry.name}` : entry.name;
4410
4598
  if (entry.isDirectory()) {
4411
- if (!SKIP_DIRS2.has(entry.name)) walk(childRel);
4599
+ if (!SKIP_DIRS.has(entry.name)) walk(childRel);
4412
4600
  } else if (entry.isFile()) {
4413
4601
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
4414
4602
  out.push({
4415
4603
  path: childRel,
4416
- content: fs5.readFileSync(path5.join(dir, childRel)).toString("base64")
4604
+ content: fs6.readFileSync(path6.join(dir, childRel)).toString("base64")
4417
4605
  });
4418
4606
  }
4419
4607
  }
@@ -4428,19 +4616,19 @@ function assertSafeRelPath(rel) {
4428
4616
  }
4429
4617
  function writeWorldFiles(dir, files, opts = {}) {
4430
4618
  for (const file of files) assertSafeRelPath(file.path);
4431
- if (opts.clean && fs5.existsSync(dir)) {
4619
+ if (opts.clean && fs6.existsSync(dir)) {
4432
4620
  const keep = new Set(files.map((f) => f.path));
4433
4621
  for (const existing of readWorldFiles(dir)) {
4434
- if (!keep.has(existing.path)) fs5.rmSync(path5.join(dir, existing.path));
4622
+ if (!keep.has(existing.path)) fs6.rmSync(path6.join(dir, existing.path));
4435
4623
  }
4436
4624
  pruneEmptyDirs(dir);
4437
4625
  }
4438
- fs5.mkdirSync(dir, { recursive: true });
4626
+ fs6.mkdirSync(dir, { recursive: true });
4439
4627
  const written = [];
4440
4628
  for (const file of files) {
4441
- const target = path5.join(dir, file.path);
4442
- fs5.mkdirSync(path5.dirname(target), { recursive: true });
4443
- fs5.writeFileSync(target, Buffer.from(file.content, "base64"));
4629
+ const target = path6.join(dir, file.path);
4630
+ fs6.mkdirSync(path6.dirname(target), { recursive: true });
4631
+ fs6.writeFileSync(target, Buffer.from(file.content, "base64"));
4444
4632
  written.push(file.path);
4445
4633
  }
4446
4634
  return written;
@@ -4448,14 +4636,14 @@ function writeWorldFiles(dir, files, opts = {}) {
4448
4636
  function pruneEmptyDirs(root) {
4449
4637
  const walk = (abs) => {
4450
4638
  let empty = true;
4451
- for (const entry of fs5.readdirSync(abs, { withFileTypes: true })) {
4452
- const child = path5.join(abs, entry.name);
4639
+ for (const entry of fs6.readdirSync(abs, { withFileTypes: true })) {
4640
+ const child = path6.join(abs, entry.name);
4453
4641
  if (entry.isDirectory()) {
4454
- if (SKIP_DIRS2.has(entry.name)) {
4642
+ if (SKIP_DIRS.has(entry.name)) {
4455
4643
  empty = false;
4456
4644
  continue;
4457
4645
  }
4458
- if (walk(child)) fs5.rmdirSync(child);
4646
+ if (walk(child)) fs6.rmdirSync(child);
4459
4647
  else empty = false;
4460
4648
  } else empty = false;
4461
4649
  }
@@ -4519,13 +4707,13 @@ async function runtime(creds, request, opts = {}) {
4519
4707
  clearTimeout(timer);
4520
4708
  }
4521
4709
  }
4522
- var SKIP_DIRS2, BUILD_OUTPUT, BridgeRejected;
4710
+ var SKIP_DIRS, BUILD_OUTPUT, BridgeRejected;
4523
4711
  var init_client = __esm({
4524
4712
  "src/cli/runtime/client.ts"() {
4525
4713
  "use strict";
4526
4714
  init_router();
4527
4715
  init_local();
4528
- SKIP_DIRS2 = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
4716
+ SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
4529
4717
  BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
4530
4718
  BridgeRejected = class extends CliError {
4531
4719
  constructor(detail) {
@@ -4547,8 +4735,8 @@ __export(data_exports, {
4547
4735
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
4548
4736
  showLocalWorldData: () => showLocalWorldData
4549
4737
  });
4550
- import * as fs6 from "fs";
4551
- import * as path6 from "path";
4738
+ import * as fs7 from "fs";
4739
+ import * as path7 from "path";
4552
4740
  async function importLocalWorldData(dir, rows, opts, creds) {
4553
4741
  const res = await runtime(creds, {
4554
4742
  command: "import-data",
@@ -4574,7 +4762,7 @@ function dropBuildOutputs(dir) {
4574
4762
  const dropped = [];
4575
4763
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
4576
4764
  if (!BUILD_OUTPUT2.test(file.path)) continue;
4577
- fs6.rmSync(path6.join(dir, file.path), { force: true });
4765
+ fs7.rmSync(path7.join(dir, file.path), { force: true });
4578
4766
  dropped.push(file.path);
4579
4767
  }
4580
4768
  return dropped;
@@ -4585,6 +4773,7 @@ async function checkLocalWorldData(dir, rows, creds, opts = {}) {
4585
4773
  files: readWorldFiles(dir),
4586
4774
  rows,
4587
4775
  mode: "append",
4776
+ dry_run: true,
4588
4777
  ...opts.redact && opts.redact !== "off" ? { redact: opts.redact } : {}
4589
4778
  });
4590
4779
  return {
@@ -4640,7 +4829,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
4640
4829
  const payload = await localBridge(
4641
4830
  {
4642
4831
  command: "query",
4643
- world: path6.resolve(dir),
4832
+ world: path7.resolve(dir),
4644
4833
  entity,
4645
4834
  across: true,
4646
4835
  ...opts.compare ? { compare: true } : {},
@@ -4711,9 +4900,9 @@ __export(transform_exports, {
4711
4900
  transformRecords: () => transformRecords
4712
4901
  });
4713
4902
  import { spawn as spawn2 } from "child_process";
4714
- import * as fs7 from "fs";
4903
+ import * as fs8 from "fs";
4715
4904
  import * as os2 from "os";
4716
- import * as path7 from "path";
4905
+ import * as path8 from "path";
4717
4906
  import { createRequire } from "module";
4718
4907
  function checkRows(value, where) {
4719
4908
  if (!isObject(value)) throw new CliError(`${where} must return {entity: [rows]}; got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`, 1);
@@ -4738,27 +4927,27 @@ function typescriptPrefix() {
4738
4927
  }
4739
4928
  }
4740
4929
  function command(script, wrapperDir) {
4741
- const suffix = path7.extname(script).toLowerCase();
4930
+ const suffix = path8.extname(script).toLowerCase();
4742
4931
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
4743
4932
  if (NODE_SUFFIXES.includes(suffix)) {
4744
- const wrapper = path7.join(wrapperDir, "transform-wrapper.mjs");
4745
- fs7.writeFileSync(wrapper, NODE_WRAPPER);
4933
+ const wrapper = path8.join(wrapperDir, "transform-wrapper.mjs");
4934
+ fs8.writeFileSync(wrapper, NODE_WRAPPER);
4746
4935
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
4747
4936
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
4748
4937
  }
4749
- throw new CliError(`${path7.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
4938
+ throw new CliError(`${path8.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
4750
4939
  }
4751
4940
  async function runTransform(script, records, opts = {}) {
4752
- if (!fs7.existsSync(script) || !fs7.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
4753
- const resolved = path7.resolve(script);
4754
- const name = path7.basename(script);
4941
+ if (!fs8.existsSync(script) || !fs8.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
4942
+ const resolved = path8.resolve(script);
4943
+ const name = path8.basename(script);
4755
4944
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
4756
- const scratch = fs7.mkdtempSync(path7.join(os2.tmpdir(), "gateway-transform-"));
4945
+ const scratch = fs8.mkdtempSync(path8.join(os2.tmpdir(), "gateway-transform-"));
4757
4946
  try {
4758
4947
  const { file, args } = command(resolved, scratch);
4759
4948
  const env = Object.fromEntries(Object.entries(process.env).filter(([key2]) => !SCRUBBED.test(key2)));
4760
4949
  const done = await new Promise((resolve10, reject) => {
4761
- const child = spawn2(file, args, { cwd: path7.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
4950
+ const child = spawn2(file, args, { cwd: path8.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
4762
4951
  let stdout = "";
4763
4952
  let stderr = "";
4764
4953
  let timedOut = false;
@@ -4789,7 +4978,7 @@ async function runTransform(script, records, opts = {}) {
4789
4978
  }
4790
4979
  return { rows: checkRows(value, name), stderr: done.stderr };
4791
4980
  } finally {
4792
- fs7.rmSync(scratch, { recursive: true, force: true });
4981
+ fs8.rmSync(scratch, { recursive: true, force: true });
4793
4982
  }
4794
4983
  }
4795
4984
  async function transformRecords(transform, records, opts = {}) {
@@ -4881,13 +5070,18 @@ if (fn) {
4881
5070
  });
4882
5071
 
4883
5072
  // src/cli/platform/worldData/batches.ts
4884
- import { createHash as createHash6 } from "crypto";
4885
- import * as fs8 from "fs";
5073
+ import { createHash as createHash7 } from "crypto";
5074
+ import * as fs9 from "fs";
5075
+ function isSettled(status) {
5076
+ if (!TERMINAL_STATES.has(status.state)) return false;
5077
+ if (status.state !== "applied") return true;
5078
+ return status.dryRun === true || status.publish !== "now" || !status.snapshotId;
5079
+ }
4886
5080
  function uploadHeadersFor(gz) {
4887
5081
  return {
4888
5082
  "content-type": "application/gzip",
4889
5083
  "content-length": String(gz.length),
4890
- "x-amz-checksum-sha256": createHash6("sha256").update(gz).digest("base64")
5084
+ "x-amz-checksum-sha256": createHash7("sha256").update(gz).digest("base64")
4891
5085
  };
4892
5086
  }
4893
5087
  async function forEachLimited(items, limit, fn) {
@@ -4924,7 +5118,7 @@ async function waitForBatch(client2, batchId, opts = {}) {
4924
5118
  opts.onPhase?.(phase);
4925
5119
  last = phase;
4926
5120
  }
4927
- if (TERMINAL_STATES.has(status.state)) return status;
5121
+ if (isSettled(status)) return status;
4928
5122
  await sleep3(delay);
4929
5123
  delay = Math.min(1e4, Math.round(delay * 1.5));
4930
5124
  }
@@ -4939,7 +5133,12 @@ var init_batches = __esm({
4939
5133
  "use strict";
4940
5134
  init_config();
4941
5135
  init_router();
4942
- TERMINAL_STATES = /* @__PURE__ */ new Set(["applied", "refused", "published", "failed"]);
5136
+ TERMINAL_STATES = /* @__PURE__ */ new Set([
5137
+ "applied",
5138
+ "refused",
5139
+ "published",
5140
+ "failed"
5141
+ ]);
4943
5142
  sleepMs = (ms) => new Promise((resolve10) => setTimeout(resolve10, ms));
4944
5143
  WorldDataApi = class {
4945
5144
  constructor(creds, slug, sleep3 = sleepMs) {
@@ -4953,22 +5152,61 @@ var init_batches = __esm({
4953
5152
  return api(this.creds, "POST", `${this.base}/batches`, body);
4954
5153
  }
4955
5154
  register(batchId, chunk) {
4956
- return api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks`, {
5155
+ return api(
5156
+ this.creds,
5157
+ "POST",
5158
+ `${this.base}/batches/${batchId}/chunks`,
5159
+ {
5160
+ ordinal: chunk.ordinal,
5161
+ digest: chunk.digest,
5162
+ bytes: chunk.bytes,
5163
+ rows: chunk.rows,
5164
+ ...chunk.entity ? { entity: chunk.entity } : {}
5165
+ }
5166
+ );
5167
+ }
5168
+ /**
5169
+ * Stage a project-drive rows file as one chunk, on the platform: the server
5170
+ * streams it out of drive storage into chunk storage and answers with the
5171
+ * digest and gzipped size it derived. No bytes pass through this process.
5172
+ */
5173
+ async stageDrive(batchId, chunk) {
5174
+ const staged = await api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks`, {
4957
5175
  ordinal: chunk.ordinal,
4958
- digest: chunk.digest,
4959
- bytes: chunk.bytes,
4960
- rows: chunk.rows,
5176
+ drive: chunk.path,
4961
5177
  ...chunk.entity ? { entity: chunk.entity } : {}
4962
5178
  });
5179
+ if (!staged.uploaded || !staged.digest || staged.bytes === void 0 || staged.rows === void 0)
5180
+ throw new CliError(
5181
+ `the server staged drive:${chunk.path} as chunk ${chunk.ordinal} without its digest, size and rows`,
5182
+ 1
5183
+ );
5184
+ return {
5185
+ ordinal: chunk.ordinal,
5186
+ digest: staged.digest,
5187
+ bytes: staged.bytes,
5188
+ rows: staged.rows,
5189
+ entity: chunk.entity,
5190
+ chunkId: staged.chunkId,
5191
+ uploaded: true
5192
+ };
4963
5193
  }
4964
5194
  markUploaded(batchId, chunkId) {
4965
- return api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks/${chunkId}/uploaded`);
5195
+ return api(
5196
+ this.creds,
5197
+ "POST",
5198
+ `${this.base}/batches/${batchId}/chunks/${chunkId}/uploaded`
5199
+ );
4966
5200
  }
4967
5201
  complete(batchId) {
4968
5202
  return api(this.creds, "POST", `${this.base}/batches/${batchId}/complete`);
4969
5203
  }
4970
5204
  get(batchId) {
4971
- return api(this.creds, "GET", `${this.base}/batches/${batchId}`);
5205
+ return api(
5206
+ this.creds,
5207
+ "GET",
5208
+ `${this.base}/batches/${batchId}`
5209
+ );
4972
5210
  }
4973
5211
  list(limit, cursor) {
4974
5212
  const params = new URLSearchParams();
@@ -4978,7 +5216,12 @@ var init_batches = __esm({
4978
5216
  return api(this.creds, "GET", `${this.base}/batches${q ? `?${q}` : ""}`);
4979
5217
  }
4980
5218
  publish(message) {
4981
- return api(this.creds, "POST", `${this.base}/publish`, message ? { message } : {});
5219
+ return api(
5220
+ this.creds,
5221
+ "POST",
5222
+ `${this.base}/publish`,
5223
+ message ? { message } : {}
5224
+ );
4982
5225
  }
4983
5226
  /**
4984
5227
  * PUT the gzipped chunk to its presigned URL with exactly the headers the
@@ -4990,11 +5233,23 @@ var init_batches = __esm({
4990
5233
  const delays = [500, 1500, 4e3];
4991
5234
  for (let attempt = 0; ; attempt++) {
4992
5235
  try {
4993
- const res = await fetch(uploadUrl, { method: "PUT", body: new Uint8Array(gz), headers });
5236
+ const res = await fetch(uploadUrl, {
5237
+ method: "PUT",
5238
+ body: new Uint8Array(gz),
5239
+ headers
5240
+ });
4994
5241
  if (res.ok) return;
4995
- if (res.status < 500 || attempt >= delays.length) throw new CliError(`chunk upload -> ${res.status}: ${(await res.text()).slice(0, 300)}`, 1);
5242
+ if (res.status < 500 || attempt >= delays.length)
5243
+ throw new CliError(
5244
+ `chunk upload -> ${res.status}: ${(await res.text()).slice(0, 300)}`,
5245
+ 1
5246
+ );
4996
5247
  } catch (error) {
4997
- if (error instanceof CliError || attempt >= delays.length) throw error instanceof CliError ? error : new CliError(`chunk upload failed: ${error.message}`, 1);
5248
+ if (error instanceof CliError || attempt >= delays.length)
5249
+ throw error instanceof CliError ? error : new CliError(
5250
+ `chunk upload failed: ${error.message}`,
5251
+ 1
5252
+ );
4998
5253
  }
4999
5254
  await this.sleep(delays[attempt]);
5000
5255
  }
@@ -5003,18 +5258,30 @@ var init_batches = __esm({
5003
5258
  async upload(batchId, chunk) {
5004
5259
  const registered = await this.register(batchId, chunk);
5005
5260
  if (!registered.uploaded) {
5006
- if (!registered.uploadUrl) throw new CliError(`the server registered chunk ${chunk.ordinal} without an upload URL`, 1);
5261
+ if (!registered.uploadUrl)
5262
+ throw new CliError(
5263
+ `the server registered chunk ${chunk.ordinal} without an upload URL`,
5264
+ 1
5265
+ );
5007
5266
  await this.put(registered.uploadUrl, chunk.gz, registered.uploadHeaders);
5008
5267
  await this.markUploaded(batchId, registered.chunkId);
5009
5268
  }
5010
- return { ordinal: chunk.ordinal, digest: chunk.digest, bytes: chunk.bytes, rows: chunk.rows, entity: chunk.entity, chunkId: registered.chunkId, uploaded: true };
5269
+ return {
5270
+ ordinal: chunk.ordinal,
5271
+ digest: chunk.digest,
5272
+ bytes: chunk.bytes,
5273
+ rows: chunk.rows,
5274
+ entity: chunk.entity,
5275
+ chunkId: registered.chunkId,
5276
+ uploaded: true
5277
+ };
5011
5278
  }
5012
5279
  };
5013
5280
  }
5014
5281
  });
5015
5282
 
5016
5283
  // src/cli/platform/worldData/chunker.ts
5017
- import { createHash as createHash7 } from "crypto";
5284
+ import { createHash as createHash8 } from "crypto";
5018
5285
  import { deflateRawSync } from "zlib";
5019
5286
  function chunkLine(row, declared) {
5020
5287
  if (declared !== null) return dumps(row.row) + "\n";
@@ -5023,14 +5290,20 @@ function chunkLine(row, declared) {
5023
5290
  tagged.set("row", row.row);
5024
5291
  return dumps(tagged) + "\n";
5025
5292
  }
5026
- async function* buildChunks(rows, plan) {
5293
+ async function* buildChunks(rows, plan, ordinalStart = 0) {
5027
5294
  let lines = [];
5028
5295
  let rawBytes = 0;
5029
- let ordinal = 0;
5296
+ let ordinal = ordinalStart;
5030
5297
  let seen = 0;
5031
5298
  const flush2 = () => {
5032
5299
  const raw = Buffer.concat(lines);
5033
- const built = finishChunk(ordinal++, raw, lines.length, plan.entity, seen - lines.length);
5300
+ const built = finishChunk(
5301
+ ordinal++,
5302
+ raw,
5303
+ lines.length,
5304
+ plan.entity,
5305
+ seen - lines.length
5306
+ );
5034
5307
  lines = [];
5035
5308
  rawBytes = 0;
5036
5309
  return built;
@@ -5049,12 +5322,12 @@ function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
5049
5322
  const gz = gzipDeterministic(raw, { name: chunkName(ordinal) });
5050
5323
  return {
5051
5324
  ordinal,
5052
- digest: createHash7("sha256").update(gz).digest("hex"),
5325
+ digest: createHash8("sha256").update(gz).digest("hex"),
5053
5326
  bytes: gz.length,
5054
5327
  rows,
5055
5328
  entity,
5056
5329
  rawBytes: raw.length,
5057
- contentSha256: createHash7("sha256").update(raw).digest("hex"),
5330
+ contentSha256: createHash8("sha256").update(raw).digest("hex"),
5058
5331
  firstRow,
5059
5332
  gz
5060
5333
  };
@@ -5062,7 +5335,18 @@ function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
5062
5335
  function gzipDeterministic(raw, opts = {}) {
5063
5336
  const level = opts.level ?? GZIP_LEVEL;
5064
5337
  const name = opts.name ? Buffer.concat([Buffer.from(opts.name, "latin1"), Buffer.from([0])]) : Buffer.alloc(0);
5065
- const header = Buffer.from([31, 139, 8, opts.name ? 8 : 0, 0, 0, 0, 0, level === 9 ? 2 : level === 1 ? 4 : 0, 255]);
5338
+ const header = Buffer.from([
5339
+ 31,
5340
+ 139,
5341
+ 8,
5342
+ opts.name ? 8 : 0,
5343
+ 0,
5344
+ 0,
5345
+ 0,
5346
+ 0,
5347
+ level === 9 ? 2 : level === 1 ? 4 : 0,
5348
+ 255
5349
+ ]);
5066
5350
  const body = deflateRawSync(raw, { level });
5067
5351
  const trailer = Buffer.alloc(8);
5068
5352
  trailer.writeUInt32LE(crc32(raw), 0);
@@ -5079,7 +5363,8 @@ function crc32(data) {
5079
5363
  }
5080
5364
  }
5081
5365
  let crc = 4294967295;
5082
- for (let i = 0; i < data.length; i++) crc = table[(crc ^ data[i]) & 255] ^ crc >>> 8;
5366
+ for (let i = 0; i < data.length; i++)
5367
+ crc = table[(crc ^ data[i]) & 255] ^ crc >>> 8;
5083
5368
  return (crc ^ 4294967295) >>> 0;
5084
5369
  }
5085
5370
  var DEFAULT_CHUNK_BYTES, GZIP_LEVEL, chunkName, table;
@@ -5137,9 +5422,9 @@ var init_push = __esm({
5137
5422
  });
5138
5423
 
5139
5424
  // src/worlds/ingest.ts
5140
- import * as fs9 from "fs";
5425
+ import * as fs10 from "fs";
5141
5426
  import * as os3 from "os";
5142
- import * as path8 from "path";
5427
+ import * as path9 from "path";
5143
5428
  function parseToolCallRecord(raw) {
5144
5429
  const none = { record: null, skip: null };
5145
5430
  if (!isObject2(raw)) return { ...none, refusal: "record is not an object" };
@@ -5160,7 +5445,7 @@ function parseToolCallRecord(raw) {
5160
5445
  }
5161
5446
  function readToolCallRecords(paths) {
5162
5447
  const records = [];
5163
- for (const file of paths) records.push(...toolCallRecordsIn(fs9.readFileSync(file, "utf8"), file));
5448
+ for (const file of paths) records.push(...toolCallRecordsIn(fs10.readFileSync(file, "utf8"), file));
5164
5449
  return records;
5165
5450
  }
5166
5451
  function toolCallRecordsIn(text2, where) {
@@ -5196,7 +5481,7 @@ async function emit(sink, record) {
5196
5481
  return;
5197
5482
  }
5198
5483
  if (typeof sink === "string") {
5199
- fs9.appendFileSync(sink, JSON.stringify(record) + "\n");
5484
+ fs10.appendFileSync(sink, JSON.stringify(record) + "\n");
5200
5485
  return;
5201
5486
  }
5202
5487
  if (typeof sink === "function") {
@@ -5227,14 +5512,6 @@ function captureToolCalls(tools, sink, opts = {}) {
5227
5512
  }
5228
5513
  return wrapped;
5229
5514
  }
5230
- function jsonish(value) {
5231
- if (typeof value !== "string") return value;
5232
- try {
5233
- return JSON.parse(value);
5234
- } catch {
5235
- return value;
5236
- }
5237
- }
5238
5515
  function toolCallsFromObservations(observations, opts = {}) {
5239
5516
  const known = opts.tools ? new Set(opts.tools) : null;
5240
5517
  const sinceMs = opts.since ? Date.parse(opts.since) : NaN;
@@ -5261,10 +5538,9 @@ function toolCallsFromObservations(observations, opts = {}) {
5261
5538
  out.unknownTools[tool] = (out.unknownTools[tool] ?? 0) + 1;
5262
5539
  continue;
5263
5540
  }
5264
- const input = jsonish(observation.input);
5265
- const record = { tool, args: isObject2(input) ? input : {} };
5541
+ const record = { tool, args: isObject2(observation.input) ? observation.input : {} };
5266
5542
  if (observation.level === "ERROR") record.error = observation.statusMessage || "ERROR";
5267
- else record.result = jsonish(observation.output);
5543
+ else record.result = observation.output;
5268
5544
  if (at !== void 0) record.at = at;
5269
5545
  if (opts.session) record.session = opts.session;
5270
5546
  record.source = `trace:${observation.traceId ?? "?"}/${observation.id ?? "?"}`;
@@ -5285,7 +5561,7 @@ async function ingest(world, records, options = {}) {
5285
5561
  const mode = options.mode ?? "append";
5286
5562
  const dryRun = Boolean(options.dryRun);
5287
5563
  const creds = credentialsOf(options);
5288
- const local = fs9.existsSync(world) && fs9.statSync(world).isDirectory();
5564
+ const local = fs10.existsSync(world) && fs10.statSync(world).isDirectory();
5289
5565
  const map = options.map === void 0 ? void 0 : options.map;
5290
5566
  let rows = null;
5291
5567
  let stderr = "";
@@ -5301,9 +5577,9 @@ async function ingest(world, records, options = {}) {
5301
5577
  if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
5302
5578
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
5303
5579
  const slug = world.split("@", 1)[0];
5304
- const scratch = fs9.mkdtempSync(path8.join(os3.tmpdir(), "gateway-ingest-"));
5580
+ const scratch = fs10.mkdtempSync(path9.join(os3.tmpdir(), "gateway-ingest-"));
5305
5581
  try {
5306
- const pulled = await pull2(world, path8.join(scratch, slug), options);
5582
+ const pulled = await pull2(world, path9.join(scratch, slug), options);
5307
5583
  let report;
5308
5584
  let projected;
5309
5585
  if (rows === null) {
@@ -5324,7 +5600,7 @@ async function ingest(world, records, options = {}) {
5324
5600
  if (status.entityCounts) report.entityCounts = status.entityCounts;
5325
5601
  return report;
5326
5602
  } finally {
5327
- fs9.rmSync(scratch, { recursive: true, force: true });
5603
+ fs10.rmSync(scratch, { recursive: true, force: true });
5328
5604
  }
5329
5605
  }
5330
5606
  async function ingestToolCalls(world, records, options = {}) {
@@ -5387,6 +5663,58 @@ var init_ingest = __esm({
5387
5663
  }
5388
5664
  });
5389
5665
 
5666
+ // src/worlds/sandbox.ts
5667
+ function parseSlugRef(slugRef) {
5668
+ const at = slugRef.indexOf("@");
5669
+ if (at === -1) return { slug: slugRef };
5670
+ return { slug: slugRef.slice(0, at), ref: slugRef.slice(at + 1) };
5671
+ }
5672
+ var sid, WorldSandboxClient;
5673
+ var init_sandbox = __esm({
5674
+ "src/worlds/sandbox.ts"() {
5675
+ "use strict";
5676
+ init_worlds();
5677
+ sid = encodeURIComponent;
5678
+ WorldSandboxClient = class {
5679
+ constructor(options = {}) {
5680
+ this.options = options;
5681
+ }
5682
+ open(target) {
5683
+ return apiRequest(this.options, "POST", "/api/public/benchmark-containers/sandboxes", target);
5684
+ }
5685
+ status(sandboxId) {
5686
+ return apiRequest(this.options, "GET", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}`);
5687
+ }
5688
+ async read(sandboxId, filePath) {
5689
+ const res = await apiRequest(
5690
+ this.options,
5691
+ "GET",
5692
+ `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/file?path=${encodeURIComponent(filePath)}`
5693
+ );
5694
+ return { path: res.path, size: res.size, bytes: Buffer.from(res.contentBase64, "base64") };
5695
+ }
5696
+ write(sandboxId, filePath, bytes) {
5697
+ return apiRequest(this.options, "PUT", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/file`, {
5698
+ path: filePath,
5699
+ contentBase64: bytes.toString("base64")
5700
+ });
5701
+ }
5702
+ exec(sandboxId, input) {
5703
+ return apiRequest(this.options, "POST", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/exec`, input);
5704
+ }
5705
+ check(sandboxId) {
5706
+ return apiRequest(this.options, "POST", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/check`, {});
5707
+ }
5708
+ commit(sandboxId, input) {
5709
+ return apiRequest(this.options, "POST", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}/commit`, input);
5710
+ }
5711
+ close(sandboxId) {
5712
+ return apiRequest(this.options, "DELETE", `/api/public/benchmark-containers/sandboxes/${sid(sandboxId)}`);
5713
+ }
5714
+ };
5715
+ }
5716
+ });
5717
+
5390
5718
  // src/worlds/index.ts
5391
5719
  var worlds_exports = {};
5392
5720
  __export(worlds_exports, {
@@ -5399,6 +5727,7 @@ __export(worlds_exports, {
5399
5727
  EVENT_FLUSH_INTERVAL_MS: () => EVENT_FLUSH_INTERVAL_MS,
5400
5728
  EVENT_MAX_RETRIES: () => EVENT_MAX_RETRIES,
5401
5729
  GatewayEvalsClient: () => GatewayEvalsClient,
5730
+ PushSlugConflict: () => PushSlugConflict,
5402
5731
  STRIP_PREFIX: () => STRIP_PREFIX,
5403
5732
  SURFACE_KINDS: () => SURFACE_KINDS,
5404
5733
  TRANSFORMS: () => TRANSFORMS,
@@ -5406,6 +5735,7 @@ __export(worlds_exports, {
5406
5735
  TaskSessions: () => TaskSessions,
5407
5736
  WHITESPACE: () => WHITESPACE,
5408
5737
  WorldBrowser: () => WorldBrowser,
5738
+ WorldSandboxClient: () => WorldSandboxClient,
5409
5739
  WorldSession: () => WorldSession,
5410
5740
  WorldSessionCallError: () => WorldSessionCallError,
5411
5741
  WorldSessionTimeout: () => WorldSessionTimeout,
@@ -5415,9 +5745,11 @@ __export(worlds_exports, {
5415
5745
  aggregateTaskGrade: () => aggregateTaskGrade,
5416
5746
  apiRequest: () => apiRequest,
5417
5747
  applyTransforms: () => applyTransforms,
5748
+ archiveMode: () => archiveMode,
5418
5749
  attachManifest: () => attachManifest,
5419
5750
  attachSession: () => attachSession,
5420
5751
  attachTask: () => attachTask,
5752
+ buildFileManifest: () => buildFileManifest,
5421
5753
  buildSourceTarball: () => buildSourceTarball,
5422
5754
  captureToolCalls: () => captureToolCalls,
5423
5755
  collectArchiveRelPaths: () => collectArchiveRelPaths,
@@ -5429,12 +5761,12 @@ __export(worlds_exports, {
5429
5761
  deleteTask: () => deleteTask,
5430
5762
  describeWorld: () => describeWorld,
5431
5763
  detectProvenance: () => detectProvenance,
5764
+ extractBundle: () => extractBundle,
5432
5765
  getRun: () => getRun,
5433
5766
  getTask: () => getTask,
5434
5767
  hashForm: () => hashForm,
5435
5768
  ingest: () => ingest,
5436
5769
  ingestToolCalls: () => ingestToolCalls,
5437
- isIncludedInContentHash: () => isIncludedInContentHash,
5438
5770
  isTaskIdRef: () => isTaskIdRef,
5439
5771
  isTaskManifest: () => isTaskManifest,
5440
5772
  isTaskSpec: () => isTaskSpec,
@@ -5450,6 +5782,8 @@ __export(worlds_exports, {
5450
5782
  openSession: () => openSession,
5451
5783
  openTask: () => openTask,
5452
5784
  openWorld: () => openWorld,
5785
+ openWorldVersion: () => openWorldVersion,
5786
+ parseSlugRef: () => parseSlugRef,
5453
5787
  parseToolCallRecord: () => parseToolCallRecord,
5454
5788
  parseTransform: () => parseTransform,
5455
5789
  parseWorldTaskRefs: () => parseWorldTaskRefs,
@@ -5466,6 +5800,7 @@ __export(worlds_exports, {
5466
5800
  redactActionArgs: () => redactActionArgs,
5467
5801
  redactionOf: () => redactionOf,
5468
5802
  resolveBrowserMode: () => resolveBrowserMode,
5803
+ resolvePushSlug: () => resolvePushSlug,
5469
5804
  roundForm: () => roundForm,
5470
5805
  runSessions: () => runSessions,
5471
5806
  runTask: () => runTask,
@@ -5500,9 +5835,9 @@ function resolveEnv(options) {
5500
5835
  }
5501
5836
  return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
5502
5837
  }
5503
- async function apiRequest(options, method, path13, body) {
5838
+ async function apiRequest(options, method, path14, body) {
5504
5839
  const { host, publicKey, secretKey } = resolveEnv(options);
5505
- const res = await fetch(`${host}${path13}`, {
5840
+ const res = await fetch(`${host}${path14}`, {
5506
5841
  method,
5507
5842
  headers: {
5508
5843
  "content-type": "application/json",
@@ -5511,7 +5846,7 @@ async function apiRequest(options, method, path13, body) {
5511
5846
  ...body === void 0 ? {} : { body: JSON.stringify(body) }
5512
5847
  });
5513
5848
  const text2 = await res.text();
5514
- if (!res.ok) throw new ApiError2(res.status, method, path13, text2.slice(0, 400));
5849
+ if (!res.ok) throw new ApiError2(res.status, method, path14, text2.slice(0, 400));
5515
5850
  return JSON.parse(text2);
5516
5851
  }
5517
5852
  async function setEnvSecret(slug, key2, value, options = {}) {
@@ -5640,9 +5975,21 @@ async function openWorld(slugRef, options = {}) {
5640
5975
  const at = slugRef.indexOf("@");
5641
5976
  const slug = at === -1 ? slugRef : slugRef.slice(0, at);
5642
5977
  const ref = at === -1 ? null : slugRef.slice(at + 1);
5978
+ return resolveWorld(slugRef, slug, ref, ref ? { ref } : {}, options);
5979
+ }
5980
+ async function openWorldVersion(slug, versionId, options = {}) {
5981
+ return resolveWorld(
5982
+ `${slug}@${versionId}`,
5983
+ slug,
5984
+ null,
5985
+ { versionId },
5986
+ options
5987
+ );
5988
+ }
5989
+ async function resolveWorld(slugRef, slug, ref, pin, options) {
5643
5990
  const version = await apiRequest(options, "POST", "/api/public/benchmark-containers/resolve-version", {
5644
5991
  slug,
5645
- ...ref ? { ref } : {}
5992
+ ...pin
5646
5993
  });
5647
5994
  const world = new WorldSet(
5648
5995
  slugRef,
@@ -5681,9 +6028,10 @@ var init_worlds = __esm({
5681
6028
  init_query();
5682
6029
  init_migrate();
5683
6030
  init_ingest();
6031
+ init_sandbox();
5684
6032
  ApiError2 = class extends Error {
5685
- constructor(status, method, path13, body) {
5686
- super(`${method} ${path13} -> ${status}: ${body}`);
6033
+ constructor(status, method, path14, body) {
6034
+ super(`${method} ${path14} -> ${status}: ${body}`);
5687
6035
  this.status = status;
5688
6036
  this.name = "ApiError";
5689
6037
  }
@@ -6323,8 +6671,8 @@ var GatewayContext = class _GatewayContext {
6323
6671
  }
6324
6672
  return resolved;
6325
6673
  }
6326
- async requestJson(method, path13, payload) {
6327
- const response = await fetch(`${this.host}${path13}`, {
6674
+ async requestJson(method, path14, payload) {
6675
+ const response = await fetch(`${this.host}${path14}`, {
6328
6676
  method,
6329
6677
  headers: {
6330
6678
  Authorization: this.authHeader(),
@@ -6335,7 +6683,7 @@ var GatewayContext = class _GatewayContext {
6335
6683
  const text2 = await response.text();
6336
6684
  const body = text2 ? safeJson(text2) : {};
6337
6685
  if (!response.ok) {
6338
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path13}`;
6686
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path14}`;
6339
6687
  if (response.status === 409) {
6340
6688
  throw new GatewayContextConflictError(
6341
6689
  message,
@@ -6344,7 +6692,7 @@ var GatewayContext = class _GatewayContext {
6344
6692
  }
6345
6693
  throw new GatewayContextError(message);
6346
6694
  }
6347
- this.log("%s %s -> %s", method, path13, response.status);
6695
+ this.log("%s %s -> %s", method, path14, response.status);
6348
6696
  return body;
6349
6697
  }
6350
6698
  authHeader() {
@@ -6451,11 +6799,11 @@ function primaryPathForKind(kind, snapshot) {
6451
6799
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
6452
6800
  if (readFile(snapshot, preferred)) return preferred;
6453
6801
  return Object.entries(snapshot.files).find(
6454
- ([path13, entry]) => entry.type === "file" && path13.endsWith(".md")
6802
+ ([path14, entry]) => entry.type === "file" && path14.endsWith(".md")
6455
6803
  )?.[0] ?? null;
6456
6804
  }
6457
- function parseJsonFile(snapshot, path13) {
6458
- const content = readFile(snapshot, path13);
6805
+ function parseJsonFile(snapshot, path14) {
6806
+ const content = readFile(snapshot, path14);
6459
6807
  if (!content) return {};
6460
6808
  try {
6461
6809
  const parsed = JSON.parse(content);
@@ -6464,8 +6812,8 @@ function parseJsonFile(snapshot, path13) {
6464
6812
  return {};
6465
6813
  }
6466
6814
  }
6467
- function readFile(snapshot, path13) {
6468
- const entry = snapshot.files[path13];
6815
+ function readFile(snapshot, path14) {
6816
+ const entry = snapshot.files[path14];
6469
6817
  return entry?.type === "file" ? entry.content : null;
6470
6818
  }
6471
6819
  function provenanceFor(snapshot, label) {
@@ -6567,16 +6915,16 @@ var GatewayMemory = class _GatewayMemory {
6567
6915
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
6568
6916
  );
6569
6917
  }
6570
- async read(target, path13, options = {}) {
6918
+ async read(target, path14, options = {}) {
6571
6919
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
6572
- const file = snapshot.files[path13];
6920
+ const file = snapshot.files[path14];
6573
6921
  return file?.type === "file" ? file.content : null;
6574
6922
  }
6575
- async write(target, path13, content, options = {}) {
6923
+ async write(target, path14, content, options = {}) {
6576
6924
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
6577
6925
  return this.push(snapshot.repo.repoHandle, {
6578
6926
  files: {
6579
- [path13]: {
6927
+ [path14]: {
6580
6928
  content,
6581
6929
  ...options.mimeType ? { mimeType: options.mimeType } : {}
6582
6930
  }
@@ -6586,10 +6934,10 @@ var GatewayMemory = class _GatewayMemory {
6586
6934
  ...runtimeContextPayload2(options)
6587
6935
  });
6588
6936
  }
6589
- async delete(target, path13, options = {}) {
6937
+ async delete(target, path14, options = {}) {
6590
6938
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
6591
6939
  return this.push(snapshot.repo.repoHandle, {
6592
- files: { [path13]: null },
6940
+ files: { [path14]: null },
6593
6941
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
6594
6942
  message: options.message,
6595
6943
  ...runtimeContextPayload2(options)
@@ -6602,8 +6950,8 @@ var GatewayMemory = class _GatewayMemory {
6602
6950
  payload
6603
6951
  );
6604
6952
  }
6605
- async requestJson(method, path13, payload) {
6606
- const response = await fetch(`${this.host}${path13}`, {
6953
+ async requestJson(method, path14, payload) {
6954
+ const response = await fetch(`${this.host}${path14}`, {
6607
6955
  method,
6608
6956
  headers: {
6609
6957
  Authorization: this.authHeader(),
@@ -6614,7 +6962,7 @@ var GatewayMemory = class _GatewayMemory {
6614
6962
  const text2 = await response.text();
6615
6963
  const body = text2 ? safeJson2(text2) : {};
6616
6964
  if (!response.ok) {
6617
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path13}`;
6965
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path14}`;
6618
6966
  if (response.status === 409) {
6619
6967
  throw new GatewayMemoryConflictError(
6620
6968
  message,
@@ -6623,7 +6971,7 @@ var GatewayMemory = class _GatewayMemory {
6623
6971
  }
6624
6972
  throw new GatewayMemoryError(message);
6625
6973
  }
6626
- this.log("%s %s -> %s", method, path13, response.status);
6974
+ this.log("%s %s -> %s", method, path14, response.status);
6627
6975
  return body;
6628
6976
  }
6629
6977
  authHeader() {
@@ -6785,10 +7133,10 @@ var PersonasClient = class _PersonasClient {
6785
7133
  done: Boolean(payload.done)
6786
7134
  };
6787
7135
  }
6788
- async requestJson(method, path13, payload) {
7136
+ async requestJson(method, path14, payload) {
6789
7137
  let response;
6790
7138
  try {
6791
- response = await fetch(`${this.host}${path13}`, {
7139
+ response = await fetch(`${this.host}${path14}`, {
6792
7140
  method,
6793
7141
  headers: {
6794
7142
  Authorization: this.authHeader(),
@@ -6799,7 +7147,7 @@ var PersonasClient = class _PersonasClient {
6799
7147
  });
6800
7148
  } catch (error) {
6801
7149
  const reason = error instanceof Error ? error.message : String(error);
6802
- throw new PersonaError(`${method} ${path13} failed: ${reason}`, {
7150
+ throw new PersonaError(`${method} ${path14} failed: ${reason}`, {
6803
7151
  cause: error
6804
7152
  });
6805
7153
  }
@@ -6807,10 +7155,10 @@ var PersonasClient = class _PersonasClient {
6807
7155
  if (!response.ok) {
6808
7156
  const detail = text2.slice(0, 500);
6809
7157
  throw new PersonaError(
6810
- `${method} ${path13} failed (${response.status}): ${detail}`
7158
+ `${method} ${path14} failed (${response.status}): ${detail}`
6811
7159
  );
6812
7160
  }
6813
- this.log("%s %s -> %s", method, path13, response.status);
7161
+ this.log("%s %s -> %s", method, path14, response.status);
6814
7162
  return safeJson3(text2);
6815
7163
  }
6816
7164
  authHeader() {
@@ -7035,10 +7383,10 @@ var SessionRecording = class _SessionRecording {
7035
7383
  };
7036
7384
  }
7037
7385
  /** Dump the full session as a JSON fixture (for regression tests). */
7038
- toFixture(path13) {
7039
- mkdirSync(dirname(path13), { recursive: true });
7040
- writeFileSync(path13, JSON.stringify(this.toJSON(), null, 2), "utf-8");
7041
- return path13;
7386
+ toFixture(path14) {
7387
+ mkdirSync(dirname(path14), { recursive: true });
7388
+ writeFileSync(path14, JSON.stringify(this.toJSON(), null, 2), "utf-8");
7389
+ return path14;
7042
7390
  }
7043
7391
  static fromApi(raw) {
7044
7392
  return new _SessionRecording({
@@ -8069,8 +8417,8 @@ var GatewaySecurity = class _GatewaySecurity {
8069
8417
  );
8070
8418
  }
8071
8419
  }
8072
- async postJson(path13, payload) {
8073
- const response = await fetch(`${this.host}${path13}`, {
8420
+ async postJson(path14, payload) {
8421
+ const response = await fetch(`${this.host}${path14}`, {
8074
8422
  method: "POST",
8075
8423
  headers: {
8076
8424
  Authorization: this.authHeader(),
@@ -8081,7 +8429,7 @@ var GatewaySecurity = class _GatewaySecurity {
8081
8429
  if (!response.ok) {
8082
8430
  const body = await response.text();
8083
8431
  throw new GatewaySecurityError(
8084
- `Gateway API ${response.status} on ${path13}: ${body || response.statusText}`
8432
+ `Gateway API ${response.status} on ${path14}: ${body || response.statusText}`
8085
8433
  );
8086
8434
  }
8087
8435
  return await response.json();
@@ -8171,9 +8519,9 @@ __export(files_exports, {
8171
8519
  uploadFile: () => uploadFile
8172
8520
  });
8173
8521
  init_worlds();
8174
- import { createHash as createHash8 } from "crypto";
8175
- import * as fs10 from "fs";
8176
- import * as path9 from "path";
8522
+ import { createHash as createHash9 } from "crypto";
8523
+ import * as fs11 from "fs";
8524
+ import * as path10 from "path";
8177
8525
  import { Readable, Transform } from "stream";
8178
8526
  import { pipeline } from "stream/promises";
8179
8527
  var DRIVE_PREFIX = "drive:";
@@ -8221,9 +8569,9 @@ async function readFile2(options = {}, filePath) {
8221
8569
  async function downloadFile(options = {}, filePath, outPath, onProgress) {
8222
8570
  const file = await getFile(options, filePath);
8223
8571
  const res = await openDownload(file.url);
8224
- fs10.mkdirSync(path9.dirname(path9.resolve(outPath)), { recursive: true });
8572
+ fs11.mkdirSync(path10.dirname(path10.resolve(outPath)), { recursive: true });
8225
8573
  const source = Readable.fromWeb(res.body);
8226
- await pipeline(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs10.createWriteStream(outPath));
8574
+ await pipeline(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs11.createWriteStream(outPath));
8227
8575
  return { ...file, out: outPath };
8228
8576
  }
8229
8577
  async function requestUpload(options = {}, body) {
@@ -8233,8 +8581,8 @@ async function confirmUpload(options = {}, fileId, ok = true) {
8233
8581
  return apiRequest(options, "POST", `${BASE}/confirm`, { fileId, ok });
8234
8582
  }
8235
8583
  async function sha256Base64(localPath) {
8236
- const hash = createHash8("sha256");
8237
- for await (const chunk of fs10.createReadStream(localPath)) hash.update(chunk);
8584
+ const hash = createHash9("sha256");
8585
+ for await (const chunk of fs11.createReadStream(localPath)) hash.update(chunk);
8238
8586
  return hash.digest("base64");
8239
8587
  }
8240
8588
  var CONTENT_TYPES = {
@@ -8270,7 +8618,7 @@ var CONTENT_TYPES = {
8270
8618
  ".mp4": "video/mp4"
8271
8619
  };
8272
8620
  function contentTypeFor(name) {
8273
- return CONTENT_TYPES[path9.extname(name).toLowerCase()] ?? "application/octet-stream";
8621
+ return CONTENT_TYPES[path10.extname(name).toLowerCase()] ?? "application/octet-stream";
8274
8622
  }
8275
8623
  function counting(report) {
8276
8624
  let seen = 0;
@@ -8297,7 +8645,7 @@ async function putPresigned(uploadUrl, body, headers) {
8297
8645
  if (!res.ok) throw new Error(`upload failed: storage answered HTTP ${res.status}: ${(await res.text()).slice(0, 300)}`);
8298
8646
  }
8299
8647
  async function uploadFile(options = {}, localPath, targetPath, upload = {}) {
8300
- const stat = fs10.statSync(localPath);
8648
+ const stat = fs11.statSync(localPath);
8301
8649
  if (!stat.isFile()) throw new Error(`${localPath} is not a file`);
8302
8650
  if (stat.size === 0) throw new Error(`${localPath} is empty; the drive refuses zero-byte uploads`);
8303
8651
  const contentType = upload.contentType ?? contentTypeFor(localPath);
@@ -8305,7 +8653,7 @@ async function uploadFile(options = {}, localPath, targetPath, upload = {}) {
8305
8653
  const target = drivePath(targetPath);
8306
8654
  const granted = await requestUpload(options, { path: target, contentLength: stat.size, contentType, sha256: sha2562, ...upload.overwrite ? { overwrite: true } : {} });
8307
8655
  try {
8308
- const stream = fs10.createReadStream(localPath).pipe(counting((n) => upload.onProgress?.(n, stat.size)));
8656
+ const stream = fs11.createReadStream(localPath).pipe(counting((n) => upload.onProgress?.(n, stat.size)));
8309
8657
  await putPresigned(granted.uploadUrl, Readable.toWeb(stream), { contentType, contentLength: stat.size, sha256: sha2562 });
8310
8658
  } catch (error) {
8311
8659
  await confirmUpload(options, granted.fileId, false).catch(() => void 0);
@@ -8318,7 +8666,7 @@ async function uploadBytes(options = {}, bytes, targetPath, upload = {}) {
8318
8666
  if (!bytes.length) throw new Error("nothing to upload: the content is empty");
8319
8667
  const target = drivePath(targetPath);
8320
8668
  const contentType = upload.contentType ?? contentTypeFor(target);
8321
- const sha2562 = createHash8("sha256").update(bytes).digest("base64");
8669
+ const sha2562 = createHash9("sha256").update(bytes).digest("base64");
8322
8670
  const granted = await requestUpload(options, { path: target, contentLength: bytes.length, contentType, sha256: sha2562, ...upload.overwrite ? { overwrite: true } : {} });
8323
8671
  try {
8324
8672
  await putPresigned(granted.uploadUrl, new Uint8Array(bytes), { contentType, contentLength: bytes.length, sha256: sha2562 });
@@ -8431,6 +8779,7 @@ var hooks_exports = {};
8431
8779
  __export(hooks_exports, {
8432
8780
  AliasHandler: () => AliasHandler,
8433
8781
  AliasMap: () => AliasMap,
8782
+ AuditReport: () => AuditReport,
8434
8783
  CONTEXT_KEY: () => CONTEXT_KEY,
8435
8784
  CallableTool: () => CallableTool,
8436
8785
  CommandHandler: () => CommandHandler,
@@ -8438,6 +8787,8 @@ __export(hooks_exports, {
8438
8787
  DenyHandler: () => DenyHandler,
8439
8788
  EVENTS: () => EVENTS,
8440
8789
  EVENT_ALIASES: () => EVENT_ALIASES,
8790
+ FieldSource: () => FieldSource,
8791
+ FinderSource: () => FinderSource,
8441
8792
  FunctionHandler: () => FunctionHandler,
8442
8793
  HANDLER_TYPES: () => HANDLER_TYPES,
8443
8794
  HookConfigError: () => HookConfigError,
@@ -8449,13 +8800,18 @@ __export(hooks_exports, {
8449
8800
  HttpTool: () => HttpTool,
8450
8801
  INBOUND_KINDS: () => INBOUND_KINDS,
8451
8802
  INPUT_TYPES: () => INPUT_TYPES,
8803
+ Identifier: () => Identifier,
8804
+ IdentifierCensus: () => IdentifierCensus,
8805
+ IdentifierSource: () => IdentifierSource,
8452
8806
  KINDS: () => KINDS,
8807
+ LegendSource: () => LegendSource,
8453
8808
  LiveToolError: () => LiveToolError,
8454
8809
  LiveTools: () => LiveTools,
8455
8810
  PARSE_MODES: () => PARSE_MODES,
8456
8811
  RecordHandler: () => RecordHandler,
8457
8812
  Recorder: () => Recorder,
8458
8813
  RedactHandler: () => RedactHandler,
8814
+ RedactionAudit: () => RedactionAudit,
8459
8815
  Replay: () => Replay,
8460
8816
  TOOL_KINDS: () => TOOL_KINDS,
8461
8817
  applyLimit: () => applyLimit,
@@ -8510,8 +8866,51 @@ __export(hooks_exports, {
8510
8866
  withDefaults: () => withDefaults
8511
8867
  });
8512
8868
 
8869
+ // src/hooks/caseSwap.ts
8870
+ var CASE_SWAP_MIN_CHARS = 4;
8871
+ var standingAlone = /* @__PURE__ */ new Map();
8872
+ function standingAlonePattern(needle) {
8873
+ let pattern = standingAlone.get(needle);
8874
+ if (pattern === void 0) {
8875
+ const escaped = needle.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8876
+ pattern = new RegExp(`(?<![A-Za-z0-9])${escaped}(?![A-Za-z0-9])`, "gi");
8877
+ standingAlone.set(needle, pattern);
8878
+ }
8879
+ return pattern;
8880
+ }
8881
+ var WORD = /[A-Za-z0-9]+/g;
8882
+ function shapedWord(word, seen) {
8883
+ const hasLetter = seen.toLowerCase() !== seen.toUpperCase();
8884
+ if (hasLetter && seen === seen.toUpperCase()) return word.toUpperCase();
8885
+ if (hasLetter && seen === seen.toLowerCase()) return word.toLowerCase();
8886
+ const first = seen.slice(0, 1);
8887
+ if (first !== first.toLowerCase())
8888
+ return word.slice(0, 1).toUpperCase() + word.slice(1);
8889
+ return word;
8890
+ }
8891
+ function shaped(replacement, seen) {
8892
+ const seenWords = seen.match(WORD) ?? [];
8893
+ if (seenWords.length !== (replacement.match(WORD) ?? []).length)
8894
+ return shapedWord(replacement, seen);
8895
+ let index2 = 0;
8896
+ return replacement.replace(
8897
+ WORD,
8898
+ (word) => shapedWord(word, seenWords[index2++])
8899
+ );
8900
+ }
8901
+ function swapValue(text2, needle, replacement) {
8902
+ if (!needle) return text2;
8903
+ const out = text2.split(needle).join(replacement);
8904
+ if (needle.length < CASE_SWAP_MIN_CHARS) return out;
8905
+ if (!out.toLowerCase().includes(needle.toLowerCase())) return out;
8906
+ return out.replace(
8907
+ standingAlonePattern(needle),
8908
+ (seen) => shaped(replacement, seen)
8909
+ );
8910
+ }
8911
+
8513
8912
  // src/hooks/alias.ts
8514
- import { createHash as createHash9, createHmac, randomBytes as randomBytes2 } from "crypto";
8913
+ import { createHash as createHash10, createHmac, randomBytes as randomBytes2 } from "crypto";
8515
8914
  var GIVEN = [
8516
8915
  "John",
8517
8916
  "Mary",
@@ -9280,9 +9679,9 @@ var AliasMap = class _AliasMap {
9280
9679
  });
9281
9680
  if (alias.role === "private") {
9282
9681
  for (const slug of this.slugs) {
9283
- const standIn = "sha256:" + createHash9("sha256").update(`${slug}:${canonical(alias.kind, alias.display)}`, "utf8").digest("hex");
9682
+ const standIn = "sha256:" + createHash10("sha256").update(`${slug}:${canonical(alias.kind, alias.display)}`, "utf8").digest("hex");
9284
9683
  alias.variants.push([
9285
- "sha256:" + createHash9("sha256").update(`${slug}:${alias.canonical}`, "utf8").digest("hex"),
9684
+ "sha256:" + createHash10("sha256").update(`${slug}:${alias.canonical}`, "utf8").digest("hex"),
9286
9685
  standIn
9287
9686
  ]);
9288
9687
  this.aliasDigests.add(standIn.toLowerCase());
@@ -9344,10 +9743,9 @@ var AliasMap = class _AliasMap {
9344
9743
  const [inbound] = this.pairs();
9345
9744
  let out = text2;
9346
9745
  for (const [needle, replacement, alias] of inbound) {
9347
- if (needle && out.includes(needle)) {
9348
- out = out.split(needle).join(replacement);
9349
- alias.hitsIn += 1;
9350
- }
9746
+ const swapped = swapValue(out, needle, replacement);
9747
+ if (swapped !== out) alias.hitsIn += 1;
9748
+ out = swapped;
9351
9749
  }
9352
9750
  out = out.replace(SHA256_ANY_GLOBAL, (match) => this.mintDigest(match));
9353
9751
  if (this.mintInbound.length) out = this.mintFoundIn(out, where);
@@ -9363,10 +9761,9 @@ var AliasMap = class _AliasMap {
9363
9761
  const [, outbound] = this.pairs();
9364
9762
  let out = text2;
9365
9763
  for (const [needle, replacement, alias] of outbound) {
9366
- if (needle && out.includes(needle)) {
9367
- out = out.split(needle).join(replacement);
9368
- alias.hitsOut += 1;
9369
- }
9764
+ const swapped = swapValue(out, needle, replacement);
9765
+ if (swapped !== out) alias.hitsOut += 1;
9766
+ out = swapped;
9370
9767
  }
9371
9768
  const problems = [];
9372
9769
  for (const [needle] of outbound) {
@@ -9579,7 +9976,7 @@ function mirrorHandle(handle, identity) {
9579
9976
  }
9580
9977
 
9581
9978
  // src/hooks/config.ts
9582
- import * as fs11 from "fs";
9979
+ import * as fs12 from "fs";
9583
9980
 
9584
9981
  // src/cli/platform/worldData/toml.ts
9585
9982
  var TomlError = class extends Error {
@@ -9927,9 +10324,9 @@ function alternatives(raw) {
9927
10324
  if (!parts2.length || parts2.includes("*")) return [];
9928
10325
  return parts2;
9929
10326
  }
9930
- function hasPath(node, path13) {
10327
+ function hasPath(node, path14) {
9931
10328
  let current = node;
9932
- for (const part of path13.split(".")) {
10329
+ for (const part of path14.split(".")) {
9933
10330
  if (current && typeof current === "object" && !Array.isArray(current) && part in current) {
9934
10331
  current = current[part];
9935
10332
  continue;
@@ -9970,7 +10367,7 @@ function matches(m, event) {
9970
10367
  if (m.args.length) {
9971
10368
  const haystacks = event.event === "pre_request" || event.event === "post_response" ? [event.request?.query, event.request?.body] : [event.toolInput];
9972
10369
  const present = haystacks.filter((h) => h !== void 0 && h !== null);
9973
- if (!m.args.every((path13) => present.some((h) => hasPath(h, path13))))
10370
+ if (!m.args.every((path14) => present.some((h) => hasPath(h, path14))))
9974
10371
  return false;
9975
10372
  }
9976
10373
  return true;
@@ -10413,9 +10810,9 @@ function vendorOf(config, toolName) {
10413
10810
  return void 0;
10414
10811
  }
10415
10812
  function loadHookConfig(file) {
10416
- if (!fs11.existsSync(file) || !fs11.statSync(file).isFile())
10813
+ if (!fs12.existsSync(file) || !fs12.statSync(file).isFile())
10417
10814
  throw new HookConfigError(`${file}: no such file`);
10418
- const text2 = fs11.readFileSync(file, "utf8");
10815
+ const text2 = fs12.readFileSync(file, "utf8");
10419
10816
  if (file.toLowerCase().endsWith(".json")) {
10420
10817
  let data2;
10421
10818
  try {
@@ -10795,8 +11192,8 @@ function parseHandler(event, raw, position) {
10795
11192
 
10796
11193
  // src/hooks/handlers.ts
10797
11194
  import { spawn as spawn3 } from "child_process";
10798
- import * as fs12 from "fs";
10799
- import * as path10 from "path";
11195
+ import * as fs13 from "fs";
11196
+ import * as path11 from "path";
10800
11197
  import { pathToFileURL } from "url";
10801
11198
  function parseStdout(text2, handler) {
10802
11199
  const stripped = text2.trim();
@@ -10990,8 +11387,8 @@ var RecordHandler = class {
10990
11387
  record.source = this.opts.source ?? null;
10991
11388
  if (Array.isArray(this.sink)) this.sink.push(record);
10992
11389
  else if (typeof this.sink === "string") {
10993
- fs12.mkdirSync(path10.dirname(this.sink), { recursive: true });
10994
- fs12.appendFileSync(this.sink, JSON.stringify(record) + "\n");
11390
+ fs13.mkdirSync(path11.dirname(this.sink), { recursive: true });
11391
+ fs13.appendFileSync(this.sink, JSON.stringify(record) + "\n");
10995
11392
  } else await this.sink(record);
10996
11393
  return allow();
10997
11394
  }
@@ -11196,7 +11593,7 @@ async function importCallable(target, cwd) {
11196
11593
  const hash = target.lastIndexOf("#");
11197
11594
  const modulePath = target.slice(0, hash);
11198
11595
  const exportName = target.slice(hash + 1) || "default";
11199
- const resolved = modulePath.startsWith(".") || path10.isAbsolute(modulePath) ? pathToFileURL(path10.resolve(cwd, modulePath)).href : modulePath;
11596
+ const resolved = modulePath.startsWith(".") || path11.isAbsolute(modulePath) ? pathToFileURL(path11.resolve(cwd, modulePath)).href : modulePath;
11200
11597
  let module;
11201
11598
  try {
11202
11599
  module = await import(resolved);
@@ -11259,8 +11656,8 @@ async function buildHandler(spec, aliases, opts) {
11259
11656
  }
11260
11657
 
11261
11658
  // src/hooks/live.ts
11262
- import * as fs13 from "fs";
11263
- import * as path11 from "path";
11659
+ import * as fs14 from "fs";
11660
+ import * as path12 from "path";
11264
11661
  var USER_AGENT = "gateway-hooks/1 (+https://withgateway.ai)";
11265
11662
  var TAGS = /<(script|style)[^>]*>.*?<\/\1>/gis;
11266
11663
  var STRIP = /<[^>]+>/g;
@@ -11624,9 +12021,9 @@ var CallableTool = class _CallableTool {
11624
12021
  }
11625
12022
  };
11626
12023
  function readConnection(root) {
11627
- const file = path11.join(root, "connector.toml");
11628
- if (!fs13.existsSync(file)) throw new Error(`no connector.toml at ${root}`);
11629
- const raw = parseToml(fs13.readFileSync(file, "utf8"));
12024
+ const file = path12.join(root, "connector.toml");
12025
+ if (!fs14.existsSync(file)) throw new Error(`no connector.toml at ${root}`);
12026
+ const raw = parseToml(fs14.readFileSync(file, "utf8"));
11630
12027
  const connector = raw.connector ?? {};
11631
12028
  const auth = raw.auth ?? {};
11632
12029
  const operations = {};
@@ -11640,7 +12037,7 @@ function readConnection(root) {
11640
12037
  };
11641
12038
  }
11642
12039
  return {
11643
- slug: connector.slug ?? path11.basename(root),
12040
+ slug: connector.slug ?? path12.basename(root),
11644
12041
  baseUrl: (connector.base_url ?? "").replace(/\/+$/, ""),
11645
12042
  scheme: auth.scheme ?? "none",
11646
12043
  ...typeof auth.name === "string" ? { authName: auth.name } : {},
@@ -11657,8 +12054,8 @@ var ConnectorTool = class {
11657
12054
  this.spec = spec;
11658
12055
  this.live = live;
11659
12056
  let location = spec.options.connector;
11660
- if (!path11.isAbsolute(location)) location = path11.resolve(baseDir, location);
11661
- const root = fs13.existsSync(location) && fs13.statSync(location).isFile() ? path11.dirname(location) : location;
12057
+ if (!path12.isAbsolute(location)) location = path12.resolve(baseDir, location);
12058
+ const root = fs14.existsSync(location) && fs14.statSync(location).isFile() ? path12.dirname(location) : location;
11662
12059
  try {
11663
12060
  this.connection = readConnection(root);
11664
12061
  } catch (error) {
@@ -11812,16 +12209,16 @@ var Recorder = class {
11812
12209
  this.file = file;
11813
12210
  }
11814
12211
  write(line) {
11815
- fs13.mkdirSync(path11.dirname(this.file), { recursive: true });
11816
- fs13.appendFileSync(this.file, canonical2(line) + "\n");
12212
+ fs14.mkdirSync(path12.dirname(this.file), { recursive: true });
12213
+ fs14.appendFileSync(this.file, canonical2(line) + "\n");
11817
12214
  }
11818
12215
  };
11819
12216
  var Replay = class {
11820
12217
  constructor(file) {
11821
12218
  this.file = file;
11822
- if (!fs13.existsSync(file))
12219
+ if (!fs14.existsSync(file))
11823
12220
  throw new LiveToolError(`replay: no recording at ${file}`);
11824
- fs13.readFileSync(file, "utf8").split(/\r?\n/).forEach((raw, index2) => {
12221
+ fs14.readFileSync(file, "utf8").split(/\r?\n/).forEach((raw, index2) => {
11825
12222
  if (!raw.trim()) return;
11826
12223
  let line;
11827
12224
  try {
@@ -11905,7 +12302,7 @@ var LiveTools = class _LiveTools {
11905
12302
  ...config.live.record ? { record: interpolatePath(config.live.record, run.runId, directory) } : {},
11906
12303
  ...config.live.replay ? { replay: interpolatePath(config.live.replay, run.runId, directory) } : {}
11907
12304
  };
11908
- return _LiveTools.create(run, live, path11.dirname(path11.resolve(file)));
12305
+ return _LiveTools.create(run, live, path12.dirname(path12.resolve(file)));
11909
12306
  }
11910
12307
  /** The declared tools for a model. `openai`: `{type: "function", function: {name,
11911
12308
  * description, parameters}}`; `anthropic`: `{name, description, input_schema}`. */
@@ -12002,10 +12399,225 @@ var LiveTools = class _LiveTools {
12002
12399
  }
12003
12400
  };
12004
12401
 
12402
+ // src/hooks/audit.ts
12403
+ var MIN_VALUE_CHARS = 4;
12404
+ var REVIEW_MIN_CHARS = 20;
12405
+ var ANY_TOOL = "*";
12406
+ var isObject6 = (node) => typeof node === "object" && node !== null && !Array.isArray(node);
12407
+ function* stringsOf(node) {
12408
+ if (typeof node === "string") {
12409
+ yield node;
12410
+ return;
12411
+ }
12412
+ const children = Array.isArray(node) ? node : isObject6(node) ? Object.values(node) : [];
12413
+ for (const child of children) yield* stringsOf(child);
12414
+ }
12415
+ function* fieldsOf(node) {
12416
+ if (Array.isArray(node)) {
12417
+ for (const child of node) yield* fieldsOf(child);
12418
+ return;
12419
+ }
12420
+ if (!isObject6(node)) return;
12421
+ for (const [key2, value] of Object.entries(node)) {
12422
+ yield [key2, value];
12423
+ yield* fieldsOf(value);
12424
+ }
12425
+ }
12426
+ var Identifier = class {
12427
+ constructor(kind, value, source) {
12428
+ this.kind = kind;
12429
+ this.value = value;
12430
+ this.source = source;
12431
+ }
12432
+ get key() {
12433
+ return folded(this.value);
12434
+ }
12435
+ get prefix() {
12436
+ return this.value.slice(0, 3);
12437
+ }
12438
+ };
12439
+ var IdentifierSource = class {
12440
+ ignored() {
12441
+ return [];
12442
+ }
12443
+ };
12444
+ var LegendSource = class extends IdentifierSource {
12445
+ name = "legend";
12446
+ entries;
12447
+ constructor(legend) {
12448
+ super();
12449
+ this.entries = legend.filter((entry) => entry.real);
12450
+ }
12451
+ collect() {
12452
+ return this.entries.filter((entry) => (entry.role ?? "private") !== "public").map((entry) => new Identifier(entry.kind ?? "reference", String(entry.real), this.name));
12453
+ }
12454
+ ignored() {
12455
+ const open = this.entries.filter((e) => e.role === "public").map((e) => String(e.real));
12456
+ const personas2 = this.entries.filter((e) => e.alias).map((e) => String(e.alias));
12457
+ return [...open, ...personas2];
12458
+ }
12459
+ };
12460
+ var FieldSource = class extends IdentifierSource {
12461
+ constructor(fieldKinds) {
12462
+ super();
12463
+ this.fieldKinds = fieldKinds;
12464
+ }
12465
+ name = "fields";
12466
+ kindsFor(tool) {
12467
+ return { ...this.fieldKinds[ANY_TOOL] ?? {}, ...this.fieldKinds[tool] ?? {} };
12468
+ }
12469
+ *collect(raw) {
12470
+ for (const record of raw) {
12471
+ const kinds = this.kindsFor(String(record.tool ?? ""));
12472
+ for (const [key2, value] of fieldsOf(record.result)) {
12473
+ const kind = kinds[key2];
12474
+ if (kind === void 0) continue;
12475
+ for (const text2 of stringsOf(value)) yield new Identifier(kind, text2, this.name);
12476
+ }
12477
+ }
12478
+ }
12479
+ };
12480
+ var FinderSource = class extends IdentifierSource {
12481
+ name;
12482
+ kinds;
12483
+ constructor(kinds = INBOUND_KINDS) {
12484
+ super();
12485
+ const bad = kinds.filter((kind) => !INBOUND_KINDS.includes(kind));
12486
+ if (bad.length > 0)
12487
+ throw new Error(
12488
+ `${bad.join(", ")} cannot be found by a pattern; only ${INBOUND_KINDS.join(", ")} can`
12489
+ );
12490
+ this.kinds = [...kinds];
12491
+ this.name = `finders:${this.kinds.join(",")}`;
12492
+ }
12493
+ *collect(raw) {
12494
+ for (const record of raw)
12495
+ for (const text2 of stringsOf(record.result))
12496
+ for (const kind of this.kinds)
12497
+ for (const value of findKind(kind, text2)) yield new Identifier(kind, value, "finders");
12498
+ }
12499
+ };
12500
+ var IdentifierCensus = class {
12501
+ constructor(sources) {
12502
+ this.sources = sources;
12503
+ }
12504
+ get detectors() {
12505
+ return this.sources.map((source) => source.name);
12506
+ }
12507
+ build(raw) {
12508
+ const ignored = new Set(this.sources.flatMap((s) => [...s.ignored()].map(folded)));
12509
+ const seen = /* @__PURE__ */ new Map();
12510
+ for (const source of this.sources)
12511
+ for (const identifier of source.collect(raw)) {
12512
+ if (identifier.value.length < MIN_VALUE_CHARS || ignored.has(identifier.key)) continue;
12513
+ if (!seen.has(identifier.key)) seen.set(identifier.key, identifier);
12514
+ }
12515
+ return [...seen.values()];
12516
+ }
12517
+ };
12518
+ function shuffled(items, seed) {
12519
+ const out = [...items];
12520
+ let state2 = seed >>> 0;
12521
+ const next = () => {
12522
+ state2 = state2 + 1831565813 >>> 0;
12523
+ let t = Math.imul(state2 ^ state2 >>> 15, 1 | state2);
12524
+ t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
12525
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
12526
+ };
12527
+ for (let i = out.length - 1; i > 0; i -= 1) {
12528
+ const j = Math.floor(next() * (i + 1));
12529
+ [out[i], out[j]] = [out[j], out[i]];
12530
+ }
12531
+ return out;
12532
+ }
12533
+ var AuditReport = class {
12534
+ constructor(findings, census, detectors, recordsScanned, leakedIdentifiers, cleanTexts) {
12535
+ this.findings = findings;
12536
+ this.census = census;
12537
+ this.detectors = detectors;
12538
+ this.recordsScanned = recordsScanned;
12539
+ this.leakedIdentifiers = leakedIdentifiers;
12540
+ this.cleanTexts = cleanTexts;
12541
+ }
12542
+ get ok() {
12543
+ return this.findings.length === 0;
12544
+ }
12545
+ get censusTotal() {
12546
+ return this.census.length;
12547
+ }
12548
+ /** The share of census identifiers that did NOT reach the model; 1 for an empty census. */
12549
+ get coverage() {
12550
+ if (this.census.length === 0) return 1;
12551
+ return Math.round((1 - this.leakedIdentifiers / this.census.length) * 1e4) / 1e4;
12552
+ }
12553
+ count(pick3) {
12554
+ const counts = {};
12555
+ for (const identifier of this.census) counts[pick3(identifier)] = (counts[pick3(identifier)] ?? 0) + 1;
12556
+ return counts;
12557
+ }
12558
+ /** `size` strings the model saw that nothing flagged, for a person to read. */
12559
+ reviewSample(size, seed = 0) {
12560
+ const pool = [...this.cleanTexts].sort(
12561
+ (a, b) => a.record - b.record || a.text.localeCompare(b.text)
12562
+ );
12563
+ return shuffled(pool, seed).slice(0, size);
12564
+ }
12565
+ toDict() {
12566
+ return {
12567
+ ok: this.ok,
12568
+ records_scanned: this.recordsScanned,
12569
+ detectors: this.detectors,
12570
+ census: {
12571
+ total: this.censusTotal,
12572
+ by_source: this.count((i) => i.source),
12573
+ by_kind: this.count((i) => i.kind)
12574
+ },
12575
+ leaked_identifiers: this.leakedIdentifiers,
12576
+ coverage: this.coverage,
12577
+ findings: this.findings
12578
+ };
12579
+ }
12580
+ };
12581
+ var RedactionAudit = class _RedactionAudit {
12582
+ constructor(census) {
12583
+ this.census = census;
12584
+ }
12585
+ /** The standard audit: the run's legend, declared field kinds, pattern finders. */
12586
+ static forRun(options) {
12587
+ return new _RedactionAudit(
12588
+ new IdentifierCensus([
12589
+ new LegendSource(options.legend ?? []),
12590
+ new FieldSource(options.fieldKinds ?? {}),
12591
+ new FinderSource(options.finderKinds ?? INBOUND_KINDS)
12592
+ ])
12593
+ );
12594
+ }
12595
+ run(raw, shown) {
12596
+ const census = this.census.build(raw);
12597
+ const findings = [];
12598
+ const clean = [];
12599
+ const leaked = /* @__PURE__ */ new Set();
12600
+ shown.forEach((record, index2) => {
12601
+ const tool = String(record.tool ?? "");
12602
+ const texts = [...stringsOf(record.result)];
12603
+ const haystack = folded(texts.join("\n"));
12604
+ const hits = census.filter((identifier) => haystack.includes(identifier.key));
12605
+ hits.forEach((identifier) => leaked.add(identifier.key));
12606
+ findings.push(
12607
+ ...hits.map((i) => ({ record: index2, tool, kind: i.kind, source: i.source, prefix: i.prefix }))
12608
+ );
12609
+ clean.push(
12610
+ ...texts.filter((t) => t.length >= REVIEW_MIN_CHARS && !hits.some((i) => folded(t).includes(i.key))).map((text2) => ({ record: index2, tool, text: text2 }))
12611
+ );
12612
+ });
12613
+ return new AuditReport(findings, census, this.census.detectors, shown.length, leaked.size, clean);
12614
+ }
12615
+ };
12616
+
12005
12617
  // src/hooks/run.ts
12006
12618
  import { randomBytes as randomBytes3 } from "crypto";
12007
- import * as fs14 from "fs";
12008
- import * as path12 from "path";
12619
+ import * as fs15 from "fs";
12620
+ import * as path13 from "path";
12009
12621
  var BLOCKING_EVENTS = [
12010
12622
  "pre_tool_call",
12011
12623
  "pre_request",
@@ -12055,12 +12667,12 @@ var HookRun = class _HookRun {
12055
12667
  mintInbound: options.mintInbound ?? config.aliasMintInbound
12056
12668
  });
12057
12669
  this.cwd = options.cwd ?? process.cwd();
12058
- this.runDir = options.runDir ?? path12.join(this.cwd, "runs", this.runId);
12670
+ this.runDir = options.runDir ?? path13.join(this.cwd, "runs", this.runId);
12059
12671
  const chosen = options.legend !== void 0 ? options.legend : config.aliasLegend;
12060
- this.legendPath = chosen ? path12.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
12672
+ this.legendPath = chosen ? path13.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
12061
12673
  this.sessionId = options.session;
12062
12674
  this.source = options.source;
12063
- this.configDir = config.source ? path12.dirname(path12.resolve(config.source)) : this.cwd;
12675
+ this.configDir = config.source ? path13.dirname(path13.resolve(config.source)) : this.cwd;
12064
12676
  }
12065
12677
  /** Build from a parsed config (an object in the file's shape, or a `HookConfig`);
12066
12678
  * handlers are constructed now, so a callable that does not import fails here. */
@@ -12470,8 +13082,8 @@ var HookRun = class _HookRun {
12470
13082
  throw new Error(
12471
13083
  "no legend path: set [alias] legend, pass legend to the run, or give a path"
12472
13084
  );
12473
- fs14.mkdirSync(path12.dirname(target), { recursive: true });
12474
- fs14.writeFileSync(target, JSON.stringify(this.legend(), null, 2) + "\n");
13085
+ fs15.mkdirSync(path13.dirname(target), { recursive: true });
13086
+ fs15.writeFileSync(target, JSON.stringify(this.legend(), null, 2) + "\n");
12475
13087
  return target;
12476
13088
  }
12477
13089
  writeLegendIfConfigured() {
@@ -12563,14 +13175,14 @@ var HookedSession = class {
12563
13175
  * `apiHeaders()`), `pre_request` before it and `post_response` after. Returns
12564
13176
  * `{status, body, headers}`; a 4xx/5xx is returned, not thrown, so the agent sees
12565
13177
  * the vendor's own error shape (rewritten like any result). */
12566
- async request(method, path13, opts = {}) {
13178
+ async request(method, path14, opts = {}) {
12567
13179
  if (!this.inner.api && !this.requestFn)
12568
13180
  throw new Error(
12569
13181
  "this session has no api surface (pass requestFn for a transport of your own)"
12570
13182
  );
12571
13183
  let request = {
12572
13184
  method: method.toUpperCase(),
12573
- path: path13,
13185
+ path: path14,
12574
13186
  query: { ...opts.query ?? {} },
12575
13187
  body: opts.body ?? null
12576
13188
  };
@@ -12789,10 +13401,28 @@ function toolResultText(content) {
12789
13401
  ).join("");
12790
13402
  return JSON.stringify(content ?? "");
12791
13403
  }
13404
+ function toolSummary(names) {
13405
+ return names.map((n) => `\u2192 ${n}`).join(", ");
13406
+ }
13407
+ var USAGE_ATTRIBUTES = [
13408
+ ["input_tokens", "llm.token_count.prompt"],
13409
+ ["output_tokens", "llm.token_count.completion"],
13410
+ ["cache_read_input_tokens", "llm.token_count.prompt_details.cache_read"],
13411
+ ["cache_creation_input_tokens", "llm.token_count.prompt_details.cache_write"]
13412
+ ];
13413
+ function setUsageAttributes(span, usage) {
13414
+ if (!usage || typeof usage !== "object") return;
13415
+ const u = usage;
13416
+ for (const [field, attribute] of USAGE_ATTRIBUTES) {
13417
+ const value = u[field];
13418
+ if (typeof value === "number" && Number.isFinite(value))
13419
+ span.setAttribute(attribute, value);
13420
+ }
13421
+ }
12792
13422
  var TURN_INPUT_MAX = 4e3;
12793
13423
  async function* observeClaude(stream, options) {
12794
13424
  const tracer = options.tracer ?? trace4.getTracer("@withgateway/sdk");
12795
- const model = options.model ?? "claude";
13425
+ const fallbackModel = options.model ?? "claude";
12796
13426
  const openTools = /* @__PURE__ */ new Map();
12797
13427
  let root = null;
12798
13428
  let rootCtx = null;
@@ -12810,85 +13440,102 @@ async function* observeClaude(stream, options) {
12810
13440
  let turnCtx = null;
12811
13441
  let turnIndex = 0;
12812
13442
  let turnText = "";
13443
+ let turnTools = [];
12813
13444
  let pendingTurnInput = options.input ?? "";
13445
+ let gen = null;
13446
+ let genMessageId;
13447
+ let genText = "";
13448
+ let genTools = [];
13449
+ let genUsage;
12814
13450
  let finalText = "";
13451
+ const endGeneration = () => {
13452
+ if (!gen) return;
13453
+ gen.setAttribute("output.value", genText || toolSummary(genTools));
13454
+ setUsageAttributes(gen, genUsage);
13455
+ gen.setStatus({ code: SpanStatusCode2.OK });
13456
+ gen.end();
13457
+ gen = null;
13458
+ genMessageId = void 0;
13459
+ genText = "";
13460
+ genTools = [];
13461
+ genUsage = void 0;
13462
+ };
12815
13463
  const endTurn = () => {
12816
13464
  if (!turnRoot) return;
12817
- if (turnText) turnRoot.setAttribute("output.value", turnText);
13465
+ const output = turnText || toolSummary(turnTools);
13466
+ if (output) turnRoot.setAttribute("output.value", output);
12818
13467
  turnRoot.setStatus({ code: SpanStatusCode2.OK });
12819
13468
  turnRoot.end();
12820
13469
  turnRoot = null;
12821
13470
  turnCtx = null;
12822
13471
  };
13472
+ const startTurn = () => {
13473
+ for (const span of openTools.values()) span.end();
13474
+ openTools.clear();
13475
+ endTurn();
13476
+ turnIndex += 1;
13477
+ turnText = "";
13478
+ turnTools = [];
13479
+ turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
13480
+ root: true,
13481
+ attributes: {
13482
+ "openinference.span.kind": "AGENT",
13483
+ ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
13484
+ ...options.attributes ?? {}
13485
+ }
13486
+ });
13487
+ turnCtx = trace4.setSpan(context3.active(), turnRoot);
13488
+ pendingTurnInput = "";
13489
+ };
13490
+ const startGeneration = (message) => {
13491
+ endGeneration();
13492
+ if (options.perTurn) startTurn();
13493
+ const childCtx = options.perTurn ? turnCtx : rootCtx;
13494
+ genMessageId = message.message?.id;
13495
+ gen = tracer.startSpan(
13496
+ "assistant",
13497
+ {
13498
+ attributes: {
13499
+ "openinference.span.kind": "LLM",
13500
+ "llm.model_name": message.message?.model ?? fallbackModel,
13501
+ "llm.provider": "anthropic"
13502
+ }
13503
+ },
13504
+ childCtx
13505
+ );
13506
+ };
12823
13507
  try {
12824
13508
  for await (const message of stream) {
12825
13509
  if (message?.type === "assistant") {
12826
- if (options.perTurn) {
12827
- for (const span of openTools.values()) span.end();
12828
- openTools.clear();
12829
- endTurn();
12830
- turnIndex += 1;
12831
- turnText = "";
12832
- turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
12833
- root: true,
12834
- attributes: {
12835
- "openinference.span.kind": "AGENT",
12836
- ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
12837
- ...options.attributes ?? {}
12838
- }
12839
- });
12840
- turnCtx = trace4.setSpan(context3.active(), turnRoot);
12841
- pendingTurnInput = "";
12842
- }
13510
+ const messageId = message.message?.id;
13511
+ const continuesGeneration = gen !== null && messageId !== void 0 && messageId === genMessageId;
13512
+ if (!continuesGeneration) startGeneration(message);
12843
13513
  const childCtx = options.perTurn ? turnCtx : rootCtx;
12844
- let text2 = "";
12845
- const toolUses = [];
12846
13514
  for (const block of message.message?.content ?? []) {
12847
13515
  if (block?.type === "text") {
12848
13516
  finalText += block.text;
12849
- text2 += block.text;
13517
+ turnText += block.text;
13518
+ genText += block.text;
12850
13519
  }
12851
13520
  if (block?.type === "tool_use") {
12852
- toolUses.push({
12853
- id: block.id,
12854
- name: block.name,
12855
- input: block.input
12856
- });
13521
+ const name = cleanToolName(block.name);
13522
+ genTools.push(name);
13523
+ turnTools.push(name);
13524
+ const span = tracer.startSpan(
13525
+ `tool.${name}`,
13526
+ {
13527
+ attributes: {
13528
+ "openinference.span.kind": "TOOL",
13529
+ "tool.name": name,
13530
+ "input.value": JSON.stringify(block.input)
13531
+ }
13532
+ },
13533
+ childCtx
13534
+ );
13535
+ openTools.set(block.id, span);
12857
13536
  }
12858
13537
  }
12859
- turnText += text2;
12860
- const gen = tracer.startSpan(
12861
- "assistant",
12862
- {
12863
- attributes: {
12864
- "openinference.span.kind": "LLM",
12865
- "llm.model_name": model,
12866
- "llm.provider": "anthropic",
12867
- "output.value": text2 || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
12868
- }
12869
- },
12870
- childCtx
12871
- );
12872
- const usage = message.message?.usage;
12873
- if (usage?.input_tokens != null)
12874
- gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
12875
- if (usage?.output_tokens != null)
12876
- gen.setAttribute("llm.token_count.completion", usage.output_tokens);
12877
- gen.end();
12878
- for (const t of toolUses) {
12879
- const span = tracer.startSpan(
12880
- `tool.${cleanToolName(t.name)}`,
12881
- {
12882
- attributes: {
12883
- "openinference.span.kind": "TOOL",
12884
- "tool.name": cleanToolName(t.name),
12885
- "input.value": JSON.stringify(t.input)
12886
- }
12887
- },
12888
- childCtx
12889
- );
12890
- openTools.set(t.id, span);
12891
- }
13538
+ if (message.message?.usage) genUsage = message.message.usage;
12892
13539
  }
12893
13540
  if (message?.type === "user") {
12894
13541
  const content = message.message?.content;
@@ -12913,6 +13560,7 @@ async function* observeClaude(stream, options) {
12913
13560
  yield message;
12914
13561
  }
12915
13562
  } finally {
13563
+ endGeneration();
12916
13564
  for (const span of openTools.values()) span.end();
12917
13565
  openTools.clear();
12918
13566
  if (options.perTurn) {
@@ -12975,6 +13623,7 @@ export {
12975
13623
  Persona,
12976
13624
  PersonaError,
12977
13625
  PersonasClient,
13626
+ PushSlugConflict,
12978
13627
  REPLAY_ENVIRONMENT,
12979
13628
  ReplayCase,
12980
13629
  ReplayCheck,
@@ -13001,6 +13650,7 @@ export {
13001
13650
  UsersClient,
13002
13651
  WHITESPACE,
13003
13652
  WorldBrowser,
13653
+ WorldSandboxClient,
13004
13654
  WorldSession,
13005
13655
  WorldSessionCallError,
13006
13656
  WorldSessionTimeout,
@@ -13011,12 +13661,14 @@ export {
13011
13661
  aggregateTaskGrade,
13012
13662
  apiRequest,
13013
13663
  applyTransforms,
13664
+ archiveMode,
13014
13665
  assembleManifest,
13015
13666
  assignVariant,
13016
13667
  attachManifest,
13017
13668
  attachSession,
13018
13669
  attachTask,
13019
13670
  basicAuthHeader,
13671
+ buildFileManifest,
13020
13672
  buildSourceTarball,
13021
13673
  canonicalJson,
13022
13674
  captureToolCalls,
@@ -13037,6 +13689,7 @@ export {
13037
13689
  detectProvenance,
13038
13690
  experimentContextStorage,
13039
13691
  singleton_exports as experiments,
13692
+ extractBundle,
13040
13693
  files_exports as files,
13041
13694
  gateway,
13042
13695
  getExperimentContext,
@@ -13051,7 +13704,6 @@ export {
13051
13704
  identify,
13052
13705
  ingest,
13053
13706
  ingestToolCalls,
13054
- isIncludedInContentHash,
13055
13707
  isTaskIdRef,
13056
13708
  isTaskManifest,
13057
13709
  isTaskSpec,
@@ -13069,6 +13721,8 @@ export {
13069
13721
  openSession,
13070
13722
  openTask,
13071
13723
  openWorld,
13724
+ openWorldVersion,
13725
+ parseSlugRef,
13072
13726
  parseToolCallRecord,
13073
13727
  parseTransform,
13074
13728
  parseWorldTaskRefs,
@@ -13092,6 +13746,7 @@ export {
13092
13746
  replay,
13093
13747
  resetBuildRegistry,
13094
13748
  resolveBrowserMode,
13749
+ resolvePushSlug,
13095
13750
  resolveUserId,
13096
13751
  roundForm,
13097
13752
  runSessions,