@withgateway/sdk 0.2.3 → 0.2.5

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 (62) hide show
  1. package/dist/cli.js +13267 -5869
  2. package/dist/cli.js.map +1 -1
  3. package/dist/cli.mjs +13272 -5874
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/files/index.js +83 -31
  6. package/dist/files/index.js.map +1 -1
  7. package/dist/files/index.mjs +79 -28
  8. package/dist/files/index.mjs.map +1 -1
  9. package/dist/hooks/index.d.mts +1 -0
  10. package/dist/hooks/index.d.ts +1 -0
  11. package/dist/hooks/index.js +4355 -0
  12. package/dist/hooks/index.js.map +1 -0
  13. package/dist/hooks/index.mjs +4243 -0
  14. package/dist/hooks/index.mjs.map +1 -0
  15. package/dist/{index-Dpo-t8iO.d.ts → index-1xnxt0Cc.d.ts} +58 -2
  16. package/dist/index-B0R3IgsV.d.mts +1279 -0
  17. package/dist/index-B0R3IgsV.d.ts +1279 -0
  18. package/dist/{index-B5HUQQDE.d.mts → index-C1wDEwH4.d.mts} +58 -2
  19. package/dist/index.d.mts +4 -3
  20. package/dist/index.d.ts +4 -3
  21. package/dist/index.js +6698 -1606
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +6793 -1713
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/traces/index.js +72 -20
  26. package/dist/traces/index.js.map +1 -1
  27. package/dist/traces/index.mjs +68 -17
  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 +47 -3
  32. package/dist/tracing/index.js.map +1 -1
  33. package/dist/tracing/index.mjs +42 -2
  34. package/dist/tracing/index.mjs.map +1 -1
  35. package/dist/worlds/index.d.mts +390 -27
  36. package/dist/worlds/index.d.ts +390 -27
  37. package/dist/worlds/index.js +2291 -366
  38. package/dist/worlds/index.js.map +1 -1
  39. package/dist/worlds/index.mjs +2281 -356
  40. package/dist/worlds/index.mjs.map +1 -1
  41. package/package.json +7 -1
  42. package/runtime/gatewaysdk/environment/_bundle.py +14 -24
  43. package/runtime/gatewaysdk/environment/_materialize.py +3 -3
  44. package/runtime/gatewaysdk/environment/schema/api.py +153 -20
  45. package/runtime/gatewaysdk/environment/schema/compiler.py +14 -0
  46. package/runtime/gatewaysdk/environment/schema/data.py +21 -19
  47. package/runtime/gatewaysdk/environment/schema/grading.py +225 -0
  48. package/runtime/gatewaysdk/environment/schema/host.py +171 -535
  49. package/runtime/gatewaysdk/environment/schema/ingest.py +3 -11
  50. package/runtime/gatewaysdk/environment/schema/local.py +32 -126
  51. package/runtime/gatewaysdk/environment/schema/platform.py +14 -22
  52. package/runtime/gatewaysdk/environment/schema/proof.py +237 -0
  53. package/runtime/gatewaysdk/environment/schema/runtime.py +686 -0
  54. package/runtime/gatewaysdk/environment/schema/skill.py +1 -0
  55. package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +156 -0
  56. package/runtime/gatewaysdk/environment/schema/snapshot.py +6 -5
  57. package/runtime/gatewaysdk/environment/schema/tasks.py +12 -2
  58. package/runtime/gatewaysdk/environment/schema/tools_world.py +75 -23
  59. package/runtime/gatewaysdk/environment/schema/verbs.py +136 -0
  60. package/runtime/gatewaysdk/environment/schema/workers.py +44 -13
  61. package/runtime/gatewaysdk/environment/world.py +391 -0
  62. package/skills/hooks-getting-started/SKILL.md +156 -0
@@ -11,6 +11,9 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
11
11
  var __esm = (fn, res) => function __init() {
12
12
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
13
13
  };
14
+ var __commonJS = (cb, mod) => function __require2() {
15
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
16
+ };
14
17
  var __export = (target, all) => {
15
18
  for (var name in all)
16
19
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -24,9 +27,9 @@ function surfaceKinds(s) {
24
27
  if (s.browser) kinds.push("browser");
25
28
  return kinds;
26
29
  }
27
- function uiUrl(ui, path8) {
30
+ function uiUrl(ui, path10) {
28
31
  const base = ui.url.replace(/\/+$/, "");
29
- const rel = path8.startsWith("/") ? path8 : `/${path8}`;
32
+ const rel = path10.startsWith("/") ? path10 : `/${path10}`;
30
33
  if (!ui.token) return base + rel;
31
34
  const [beforeHash, hash] = splitHash(rel);
32
35
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -502,6 +505,7 @@ __export(session_exports, {
502
505
  attachSession: () => attachSession,
503
506
  isTaskIdRef: () => isTaskIdRef,
504
507
  isTaskSpec: () => isTaskSpec,
508
+ modelToolName: () => modelToolName,
505
509
  openSession: () => openSession,
506
510
  runSessions: () => runSessions,
507
511
  schemaFromSignature: () => schemaFromSignature,
@@ -560,12 +564,12 @@ function schemaFromSignature(spec) {
560
564
  };
561
565
  }
562
566
  async function attachSession(sessionId, options = {}) {
563
- const state = await apiRequest(
567
+ const state2 = await apiRequest(
564
568
  options,
565
569
  "GET",
566
570
  `/api/public/world-sessions/${encodeURIComponent(sessionId)}`
567
571
  );
568
- return new WorldSession(sessionId, state, options);
572
+ return new WorldSession(sessionId, state2, options);
569
573
  }
570
574
  async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
571
575
  const { task, ...opts } = typeof taskOrOpts === "object" && "task" in taskOrOpts ? taskOrOpts : { task: taskOrOpts, ...positionalOpts };
@@ -583,18 +587,19 @@ async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
583
587
  );
584
588
  const at = slugOrRef.indexOf("@");
585
589
  const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
586
- const state = await apiRequest(
590
+ const ref = at === -1 ? "" : slugOrRef.slice(at + 1);
591
+ const state2 = await apiRequest(
587
592
  options,
588
593
  "POST",
589
594
  "/api/public/world-sessions",
590
595
  {
591
- ...opts.versionId ? { versionId: opts.versionId } : { slug },
596
+ ...opts.versionId ? { versionId: opts.versionId } : { slug, ...ref && { ref } },
592
597
  task,
593
598
  ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
594
599
  ...opts.surfaces?.length ? { surfaces: opts.surfaces } : {}
595
600
  }
596
601
  );
597
- return new WorldSession(state.sessionId, state, options);
602
+ return new WorldSession(state2.sessionId, state2, options);
598
603
  }
599
604
  async function runSessions(opts) {
600
605
  const options = opts.options ?? {};
@@ -786,18 +791,18 @@ var init_session = __esm({
786
791
  }
787
792
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
788
793
  browserRecorder(agentId) {
789
- const path8 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
794
+ const path10 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
790
795
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
791
796
  return new BrowserActionRecorder(
792
797
  async (events) => {
793
- await apiRequest(this.options, "POST", path8, { events });
798
+ await apiRequest(this.options, "POST", path10, { events });
794
799
  },
795
800
  id,
796
801
  {
797
802
  // Continue this agent's timeline where the platform has it, so the
798
803
  // same id reopened later never re-numbers from zero.
799
804
  resumeFrom: async () => {
800
- const summary = await apiRequest(this.options, "GET", path8);
805
+ const summary = await apiRequest(this.options, "GET", path10);
801
806
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
802
807
  }
803
808
  }
@@ -830,15 +835,15 @@ var init_session = __esm({
830
835
  async ready(opts = {}) {
831
836
  const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
832
837
  for (; ; ) {
833
- const state = await this.status();
834
- if (state.ready) return state;
835
- if (state.state === "FAILED" || state.state === "STOPPED")
838
+ const state2 = await this.status();
839
+ if (state2.ready) return state2;
840
+ if (state2.state === "FAILED" || state2.state === "STOPPED")
836
841
  throw new WorldSessionCallError(
837
- `World session ${state.state.toLowerCase()} before it was ready.`
842
+ `World session ${state2.state.toLowerCase()} before it was ready.`
838
843
  );
839
844
  if (Date.now() > deadline)
840
845
  throw new WorldSessionTimeout(
841
- `World session still ${state.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
846
+ `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
842
847
  );
843
848
  await sleep(READY_POLL_MS);
844
849
  }
@@ -914,10 +919,12 @@ var init_session = __esm({
914
919
  * A rubric grade is finalized by the platform's judge, not the container:
915
920
  * the platform answers the enqueue with `pending: true`, and this waits on
916
921
  * the judge's budget (15 min) instead of a call's. `timeoutMs` overrides
917
- * either. */
922
+ * either. `report` is the agent's own account of what it did and found,
923
+ * handed to the judge as its claim beside the evidence (the state and the
924
+ * calls); assertions and python graders ignore it. */
918
925
  async grade(opts = {}) {
919
926
  const raw = await this.exchange(
920
- { kind: "grade" },
927
+ opts.report !== void 0 ? { kind: "grade", report: opts.report } : { kind: "grade" },
921
928
  opts.timeoutMs
922
929
  );
923
930
  const record = raw && typeof raw === "object" ? raw : {};
@@ -967,156 +974,2085 @@ var init_session = __esm({
967
974
  async reset(opts = {}) {
968
975
  await this.exchange({ kind: "reset" }, opts.timeoutMs);
969
976
  }
970
- /** The session's call log, oldest first: every tool call, seed, reset,
971
- * grade with its result — the record of the episode. */
972
- async export() {
973
- const calls = [];
974
- let cursor;
975
- do {
976
- const params = new URLSearchParams({ limit: String(EXPORT_PAGE) });
977
- if (cursor) params.set("cursor", cursor);
978
- const page = await apiRequest(
979
- this.options,
980
- "GET",
981
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/calls?${params.toString()}`
977
+ /** The session's call log, oldest first: every tool call, seed, reset,
978
+ * grade with its result — the record of the episode. */
979
+ async export() {
980
+ const calls = [];
981
+ let cursor;
982
+ do {
983
+ const params = new URLSearchParams({ limit: String(EXPORT_PAGE) });
984
+ if (cursor) params.set("cursor", cursor);
985
+ const page = await apiRequest(
986
+ this.options,
987
+ "GET",
988
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/calls?${params.toString()}`
989
+ );
990
+ for (const call of page.calls) {
991
+ calls.push({
992
+ seq: Number(call.seq),
993
+ kind: String(call.kind ?? "call"),
994
+ tool: typeof call.tool === "string" ? call.tool : null,
995
+ args: call.args ?? null,
996
+ result: call.result ?? null,
997
+ error: typeof call.error === "string" ? call.error : null,
998
+ completedAt: typeof call.completedAt === "string" ? call.completedAt : null
999
+ });
1000
+ }
1001
+ cursor = page.nextCursor ?? void 0;
1002
+ } while (cursor);
1003
+ return calls;
1004
+ }
1005
+ /**
1006
+ * Promote this session's inline task into a stored task of the project
1007
+ * (version 1, pinned to the session's world unless `world` says otherwise),
1008
+ * so a task written for one end user can be reused and its grades
1009
+ * compared. The session then records the new task's id. A session on a
1010
+ * bundled task has nothing to save; one on a stored task is already saved.
1011
+ */
1012
+ async saveTask(opts = {}) {
1013
+ const task = await apiRequest(
1014
+ this.options,
1015
+ "POST",
1016
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/task`,
1017
+ {
1018
+ ...opts.name !== void 0 ? { name: opts.name } : {},
1019
+ ...opts.world !== void 0 ? { world: opts.world } : {},
1020
+ ...opts.message !== void 0 ? { message: opts.message } : {}
1021
+ }
1022
+ );
1023
+ this.current = {
1024
+ ...this.current,
1025
+ taskName: task.name,
1026
+ task: {
1027
+ ...this.taskInfo,
1028
+ source: "stored",
1029
+ name: task.name,
1030
+ id: task.id,
1031
+ version: task.version
1032
+ }
1033
+ };
1034
+ return task;
1035
+ }
1036
+ /**
1037
+ * End the session. `keepWarm` hands the container back to the pool instead
1038
+ * of tearing it down, so the next session for the same task starts warm —
1039
+ * the pooled instance still obeys the fleet's idle TTL and pinning.
1040
+ */
1041
+ async close(opts = {}) {
1042
+ return apiRequest(
1043
+ this.options,
1044
+ "DELETE",
1045
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`,
1046
+ { keepWarm: opts.keepWarm ?? false }
1047
+ );
1048
+ }
1049
+ };
1050
+ }
1051
+ });
1052
+
1053
+ // src/worlds/tasks.ts
1054
+ async function createTask(input, options = {}) {
1055
+ return apiRequest(
1056
+ options,
1057
+ "POST",
1058
+ TASKS,
1059
+ compact({
1060
+ spec: input.spec,
1061
+ name: input.name,
1062
+ world: input.world,
1063
+ message: input.message
1064
+ })
1065
+ );
1066
+ }
1067
+ async function updateTask(taskId, input, options = {}) {
1068
+ if (input.spec === void 0 && input.name === void 0 && input.world === void 0)
1069
+ throw new Error("updateTask: nothing to update \u2014 pass spec, name or world");
1070
+ return apiRequest(
1071
+ options,
1072
+ "PATCH",
1073
+ taskRoute(taskId),
1074
+ compact({
1075
+ spec: input.spec,
1076
+ name: input.name,
1077
+ world: input.world,
1078
+ message: input.message
1079
+ })
1080
+ );
1081
+ }
1082
+ async function listTasks(filter = {}, options = {}) {
1083
+ const params = new URLSearchParams();
1084
+ if (filter.world) params.set("world", filter.world);
1085
+ if (filter.page !== void 0) params.set("page", String(filter.page));
1086
+ if (filter.limit !== void 0) params.set("limit", String(filter.limit));
1087
+ for (const [key, value] of Object.entries(filter.metadata ?? {}))
1088
+ params.set(`metadata.${key}`, value);
1089
+ const query = params.toString();
1090
+ return apiRequest(
1091
+ options,
1092
+ "GET",
1093
+ query ? `${TASKS}?${query}` : TASKS
1094
+ );
1095
+ }
1096
+ async function getTask(taskId, opts = {}, options = {}) {
1097
+ const query = opts.version === void 0 ? "" : `?version=${String(opts.version)}`;
1098
+ return apiRequest(
1099
+ options,
1100
+ "GET",
1101
+ `${taskRoute(taskId)}${query}`
1102
+ );
1103
+ }
1104
+ async function deleteTask(taskId, options = {}) {
1105
+ return apiRequest(options, "DELETE", taskRoute(taskId));
1106
+ }
1107
+ async function validateTask(taskId, target = {}, options = {}) {
1108
+ return apiRequest(
1109
+ options,
1110
+ "POST",
1111
+ `${taskRoute(taskId)}/validate`,
1112
+ compact({
1113
+ version: target.version,
1114
+ slug: target.slug,
1115
+ versionId: target.versionId
1116
+ })
1117
+ );
1118
+ }
1119
+ var TASKS, taskRoute, compact;
1120
+ var init_tasks = __esm({
1121
+ "src/worlds/tasks.ts"() {
1122
+ "use strict";
1123
+ init_worlds();
1124
+ TASKS = "/api/public/world-tasks";
1125
+ taskRoute = (id) => `${TASKS}/${encodeURIComponent(id)}`;
1126
+ compact = (body) => Object.fromEntries(Object.entries(body).filter(([, v]) => v !== void 0));
1127
+ }
1128
+ });
1129
+
1130
+ // src/worlds/taskSessions.ts
1131
+ function aggregateTaskGrade(worlds) {
1132
+ const rewards = {};
1133
+ const ungraded = [];
1134
+ const graded = [];
1135
+ for (const [alias, grade] of Object.entries(worlds)) {
1136
+ if (typeof grade.reward === "number") {
1137
+ rewards[alias] = grade.reward;
1138
+ graded.push(grade.reward);
1139
+ } else ungraded.push(alias);
1140
+ for (const [key, value] of Object.entries(grade.rewards))
1141
+ rewards[`${alias}.${key}`] = value;
1142
+ }
1143
+ const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
1144
+ return { reward, rewards, worlds, ungraded };
1145
+ }
1146
+ function fromResponse(answer, options) {
1147
+ const sessions = {};
1148
+ const worlds = {};
1149
+ for (const [alias, state2] of Object.entries(answer.sessions)) {
1150
+ sessions[alias] = new WorldSession(state2.sessionId, state2, options);
1151
+ worlds[alias] = { slug: state2.slug, ref: state2.ref };
1152
+ }
1153
+ return new TaskSessions(
1154
+ answer.taskId,
1155
+ answer.taskVersion,
1156
+ answer.name,
1157
+ answer.instruction,
1158
+ sessions,
1159
+ worlds,
1160
+ answer.hash ?? null
1161
+ );
1162
+ }
1163
+ async function openTask(ref, opts = {}) {
1164
+ const options = opts.options ?? {};
1165
+ const task = typeof ref === "string" ? {
1166
+ id: ref,
1167
+ ...opts.version !== void 0 ? { version: opts.version } : {}
1168
+ } : isTaskIdRef(ref) && opts.version !== void 0 ? { ...ref, version: opts.version } : ref;
1169
+ if (isTaskIdRef(task) ? !task.id : !task.instruction)
1170
+ throw new Error(
1171
+ "openTask needs a task: a stored task id, { id, version? } or a spec with worlds"
1172
+ );
1173
+ if (!isTaskIdRef(task) && !task.worlds)
1174
+ throw new Error(
1175
+ "openTask needs a spec that names its worlds; a single-world spec opens with openSession"
1176
+ );
1177
+ const answer = await apiRequest(
1178
+ options,
1179
+ "POST",
1180
+ TASK_SESSIONS,
1181
+ {
1182
+ task,
1183
+ ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
1184
+ ...opts.surfaces?.length ? { surfaces: opts.surfaces } : {}
1185
+ }
1186
+ );
1187
+ return fromResponse(answer, options);
1188
+ }
1189
+ async function listTaskSessions(taskId, options = {}) {
1190
+ const answer = await apiRequest(options, "GET", `${TASK_SESSIONS}?taskId=${encodeURIComponent(taskId)}`);
1191
+ return answer.sessions;
1192
+ }
1193
+ async function attachTask(taskId, options = {}) {
1194
+ const live = await listTaskSessions(taskId, options);
1195
+ if (live.length === 0)
1196
+ throw new Error(
1197
+ `task ${taskId} has no live sessions; run \`task up\` first`
1198
+ );
1199
+ const byAlias = /* @__PURE__ */ new Map();
1200
+ for (const session of live) {
1201
+ const alias = session.alias ?? session.sessionId;
1202
+ byAlias.set(alias, [...byAlias.get(alias) ?? [], session]);
1203
+ }
1204
+ const doubled = [...byAlias.entries()].filter(([, rows]) => rows.length > 1);
1205
+ if (doubled.length > 0)
1206
+ throw new Error(
1207
+ `task ${taskId} has more than one live session for ${doubled.map(
1208
+ ([alias, rows]) => `'${alias}' (${rows.map((r) => r.sessionId).join(", ")})`
1209
+ ).join(", ")}; pass the manifest from \`task up\`, or close the extras`
1210
+ );
1211
+ return attachManifest(
1212
+ {
1213
+ taskId,
1214
+ taskVersion: live[0].taskVersion,
1215
+ name: null,
1216
+ instruction: null,
1217
+ sessions: Object.fromEntries(
1218
+ [...byAlias.entries()].map(([alias, [row]]) => [
1219
+ alias,
1220
+ {
1221
+ slug: row.slug ?? "",
1222
+ ref: null,
1223
+ versionId: row.versionId,
1224
+ sessionId: row.sessionId,
1225
+ ready: row.ready,
1226
+ state: row.state,
1227
+ surfaces: ["tools"]
1228
+ }
1229
+ ])
1230
+ )
1231
+ },
1232
+ options
1233
+ );
1234
+ }
1235
+ async function attachManifest(manifest, options = {}) {
1236
+ const entries = await Promise.all(
1237
+ Object.entries(manifest.sessions).map(
1238
+ async ([alias, entry]) => [alias, await attachSession(entry.sessionId, options)]
1239
+ )
1240
+ );
1241
+ const sessions = Object.fromEntries(entries);
1242
+ const instruction = manifest.instruction ?? entries[0]?.[1].instruction ?? null;
1243
+ return new TaskSessions(
1244
+ manifest.taskId,
1245
+ manifest.taskVersion,
1246
+ manifest.name,
1247
+ instruction,
1248
+ sessions,
1249
+ Object.fromEntries(
1250
+ Object.entries(manifest.sessions).map(([alias, entry]) => [
1251
+ alias,
1252
+ { slug: entry.slug, ref: entry.ref }
1253
+ ])
1254
+ )
1255
+ );
1256
+ }
1257
+ function isTaskManifest(value) {
1258
+ return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
1259
+ }
1260
+ var TASK_SESSIONS, manifestSession, TaskSessions;
1261
+ var init_taskSessions = __esm({
1262
+ "src/worlds/taskSessions.ts"() {
1263
+ "use strict";
1264
+ init_worlds();
1265
+ init_browser();
1266
+ init_session();
1267
+ TASK_SESSIONS = "/api/public/world-task-sessions";
1268
+ manifestSession = (s, state2, world) => ({
1269
+ slug: world.slug,
1270
+ ref: world.ref,
1271
+ versionId: state2.versionId,
1272
+ sessionId: state2.sessionId,
1273
+ ready: state2.ready,
1274
+ state: state2.state,
1275
+ surfaces: surfaceKinds(s),
1276
+ ...s.api ? {
1277
+ api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} }
1278
+ } : {},
1279
+ ...s.ui ? {
1280
+ ui: {
1281
+ url: s.ui.url,
1282
+ entry: uiEntryUrl(s.ui),
1283
+ apiPath: s.ui.apiPath ?? null
1284
+ }
1285
+ } : {},
1286
+ ...s.browser ? { browser: { wsUrl: s.browser.wsUrl, viewport: s.browser.viewport } } : {}
1287
+ });
1288
+ TaskSessions = class {
1289
+ constructor(taskId, taskVersion, name, instruction, sessions, worlds, hash = null) {
1290
+ this.taskId = taskId;
1291
+ this.taskVersion = taskVersion;
1292
+ this.name = name;
1293
+ this.instruction = instruction;
1294
+ this.sessions = sessions;
1295
+ this.worlds = worlds;
1296
+ this.hash = hash;
1297
+ }
1298
+ /** This alias's `{slug, ref}` as recorded at open/attach — the world a
1299
+ * `TaskRun` filing needs to name, without exposing the whole map. */
1300
+ worldRef(alias) {
1301
+ return this.worlds[alias] ?? { slug: "", ref: null };
1302
+ }
1303
+ /** The aliases, in the spec's order. */
1304
+ get aliases() {
1305
+ return Object.keys(this.sessions);
1306
+ }
1307
+ /** The alias's session, or a clear error naming the aliases there are. */
1308
+ session(alias) {
1309
+ const session = this.sessions[alias];
1310
+ if (!session)
1311
+ throw new Error(
1312
+ `task has no world '${alias}'; its worlds are ${this.aliases.join(", ")}`
1313
+ );
1314
+ return session;
1315
+ }
1316
+ async each(fn) {
1317
+ const entries = await Promise.all(
1318
+ Object.entries(this.sessions).map(
1319
+ async ([alias, session]) => [alias, await fn(session, alias)]
1320
+ )
1321
+ );
1322
+ return Object.fromEntries(entries);
1323
+ }
1324
+ /** Wait until every world answers ready. A world that never does throws
1325
+ * `WorldSessionTimeout` naming it; the others are left up. */
1326
+ async ready(opts = {}) {
1327
+ await this.each((session) => session.ready(opts));
1328
+ }
1329
+ /** Every world's current state, keyed by alias. */
1330
+ async status() {
1331
+ return this.each((session) => session.status());
1332
+ }
1333
+ /** The manifest: where every world answers, keyed by alias. */
1334
+ async manifest() {
1335
+ const sessions = await this.each(
1336
+ async (session, alias) => manifestSession(
1337
+ await session.surfaces(),
1338
+ {
1339
+ sessionId: session.sessionId,
1340
+ ready: session.isReady,
1341
+ state: (await session.status()).state,
1342
+ versionId: session.versionId
1343
+ },
1344
+ this.worlds[alias]
1345
+ )
1346
+ );
1347
+ return {
1348
+ taskId: this.taskId,
1349
+ taskVersion: this.taskVersion,
1350
+ name: this.name,
1351
+ instruction: this.instruction,
1352
+ sessions
1353
+ };
1354
+ }
1355
+ /** Grade every world and aggregate — see `TaskGrade`. One `report` (the
1356
+ * agent's own account of the whole investigation) goes to every world's
1357
+ * judge; a world graded by assertions or python ignores it. */
1358
+ async grade(opts = {}) {
1359
+ return aggregateTaskGrade(
1360
+ await this.each((session) => session.grade(opts))
1361
+ );
1362
+ }
1363
+ /** Every world's call log, keyed by alias. */
1364
+ async export() {
1365
+ return this.each((session) => session.export());
1366
+ }
1367
+ /** Close every world. Closes are attempted for all of them even when one
1368
+ * fails; the error then names every world that did not close. */
1369
+ async close(opts = {}) {
1370
+ const settled = await Promise.allSettled(
1371
+ Object.entries(this.sessions).map(
1372
+ async ([alias, session]) => [alias, await session.close(opts)]
1373
+ )
1374
+ );
1375
+ const failed = [];
1376
+ const closed = {};
1377
+ settled.forEach((result, i) => {
1378
+ const alias = this.aliases[i];
1379
+ if (result.status === "fulfilled") closed[alias] = result.value[1];
1380
+ else
1381
+ failed.push(
1382
+ `${alias}: ${result.reason instanceof Error ? result.reason.message : String(result.reason)}`
1383
+ );
1384
+ });
1385
+ if (failed.length > 0)
1386
+ throw new Error(
1387
+ `${failed.length} of ${this.aliases.length} worlds did not close \u2014 ${failed.join("; ")}`
1388
+ );
1389
+ return closed;
1390
+ }
1391
+ };
1392
+ }
1393
+ });
1394
+
1395
+ // src/worlds/evals.ts
1396
+ var EVALUATIONS, GatewayEvalsClient;
1397
+ var init_evals = __esm({
1398
+ "src/worlds/evals.ts"() {
1399
+ "use strict";
1400
+ init_worlds();
1401
+ EVALUATIONS = "/api/public/benchmark-containers/evaluations";
1402
+ GatewayEvalsClient = class {
1403
+ constructor(options = {}) {
1404
+ this.options = options;
1405
+ }
1406
+ /** Anchor a new evaluation to `versionId`; returns its id. */
1407
+ async createEvaluation(versionId, opts = {}) {
1408
+ const body = { versionId };
1409
+ if (opts.model !== void 0) body.model = opts.model;
1410
+ if (opts.rolloutsPerExample !== void 0)
1411
+ body.rolloutsPerExample = opts.rolloutsPerExample;
1412
+ if (opts.metadata !== void 0) body.metadata = opts.metadata;
1413
+ const answer = await apiRequest(
1414
+ this.options,
1415
+ "POST",
1416
+ EVALUATIONS,
1417
+ body
1418
+ );
1419
+ return answer.evaluationId;
1420
+ }
1421
+ /** Push one batch of samples; returns how many the platform inserted. */
1422
+ async pushSamples(evaluationId, samples) {
1423
+ const answer = await apiRequest(
1424
+ this.options,
1425
+ "POST",
1426
+ `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
1427
+ { samples }
1428
+ );
1429
+ return answer.inserted ?? samples.length;
1430
+ }
1431
+ /** Close the evaluation out; returns its final status. */
1432
+ async finalizeEvaluation(evaluationId, opts = {}) {
1433
+ const body = {
1434
+ status: opts.status ?? "COMPLETED"
1435
+ };
1436
+ if (opts.metrics !== void 0) body.metrics = opts.metrics;
1437
+ const answer = await apiRequest(
1438
+ this.options,
1439
+ "POST",
1440
+ `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
1441
+ body
1442
+ );
1443
+ return answer.status;
1444
+ }
1445
+ };
1446
+ }
1447
+ });
1448
+
1449
+ // src/users/index.ts
1450
+ function parseEvent(payload) {
1451
+ return {
1452
+ id: String(payload.id ?? ""),
1453
+ userId: String(payload.userId ?? ""),
1454
+ event: String(payload.event ?? ""),
1455
+ value: typeof payload.value === "number" ? payload.value : null,
1456
+ metadata: payload.metadata && typeof payload.metadata === "object" && !Array.isArray(payload.metadata) ? payload.metadata : {},
1457
+ traceId: typeof payload.traceId === "string" ? payload.traceId : null,
1458
+ sessionId: typeof payload.sessionId === "string" ? payload.sessionId : null,
1459
+ createdAt: typeof payload.createdAt === "string" ? new Date(payload.createdAt) : /* @__PURE__ */ new Date(0)
1460
+ };
1461
+ }
1462
+ function parseProfile(payload) {
1463
+ return {
1464
+ userId: String(payload.userId ?? ""),
1465
+ displayName: typeof payload.displayName === "string" ? payload.displayName : null,
1466
+ email: typeof payload.email === "string" ? payload.email : null,
1467
+ attributes: parseAttributes(payload.attributes),
1468
+ notes: typeof payload.notes === "string" ? payload.notes : null
1469
+ };
1470
+ }
1471
+ function parseAttributes(value) {
1472
+ if (!value || typeof value !== "object" || Array.isArray(value)) return {};
1473
+ const attributes = {};
1474
+ for (const [key, entry] of Object.entries(value)) {
1475
+ attributes[key] = String(entry);
1476
+ }
1477
+ return attributes;
1478
+ }
1479
+ function safeJson(value) {
1480
+ try {
1481
+ const parsed = JSON.parse(value);
1482
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
1483
+ } catch {
1484
+ return {};
1485
+ }
1486
+ }
1487
+ var UserProfileError, UsersClient, users;
1488
+ var init_users = __esm({
1489
+ "src/users/index.ts"() {
1490
+ "use strict";
1491
+ UserProfileError = class extends Error {
1492
+ constructor(message, options) {
1493
+ super(message);
1494
+ this.name = "UserProfileError";
1495
+ this.cause = options?.cause;
1496
+ }
1497
+ };
1498
+ UsersClient = class _UsersClient {
1499
+ host;
1500
+ publicKey;
1501
+ secretKey;
1502
+ timeoutSeconds;
1503
+ debug;
1504
+ constructor(config) {
1505
+ this.host = config.host.replace(/\/$/, "");
1506
+ this.publicKey = config.publicKey;
1507
+ this.secretKey = config.secretKey;
1508
+ this.timeoutSeconds = config.timeoutSeconds ?? 15;
1509
+ this.debug = config.debug ?? false;
1510
+ }
1511
+ /** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
1512
+ static fromEnv(config = {}) {
1513
+ const host = config.host ?? process.env.GATEWAY_HOST;
1514
+ const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
1515
+ const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
1516
+ const missing = [
1517
+ ["host", host],
1518
+ ["publicKey", publicKey],
1519
+ ["secretKey", secretKey]
1520
+ ].filter(([, value]) => !value).map(([name]) => name);
1521
+ if (missing.length > 0) {
1522
+ throw new UserProfileError(
1523
+ `Missing Gateway user-profile configuration: ${missing.join(", ")}`
1524
+ );
1525
+ }
1526
+ return new _UsersClient({
1527
+ ...config,
1528
+ host,
1529
+ publicKey,
1530
+ secretKey
1531
+ });
1532
+ }
1533
+ /** Fetch a user's stored profile. */
1534
+ async get(userId) {
1535
+ return parseProfile(
1536
+ await this.requestJson(
1537
+ "GET",
1538
+ `/api/public/user-profiles/${encodeURIComponent(userId)}`
1539
+ )
1540
+ );
1541
+ }
1542
+ /**
1543
+ * Upsert a user's profile. `attributes` merge with the stored set unless
1544
+ * `merge: false` (which replaces it).
1545
+ */
1546
+ async update(userId, update = {}) {
1547
+ const body = {
1548
+ userId,
1549
+ attributes: update.attributes ?? {},
1550
+ mergeAttributes: update.merge ?? true,
1551
+ ...update.displayName !== void 0 ? { displayName: update.displayName } : {},
1552
+ ...update.email !== void 0 ? { email: update.email } : {},
1553
+ ...update.notes !== void 0 ? { notes: update.notes } : {}
1554
+ };
1555
+ return parseProfile(
1556
+ await this.requestJson("POST", "/api/public/user-profiles", body)
1557
+ );
1558
+ }
1559
+ /**
1560
+ * Record a VALUE EVENT for a user — the per-user ROI stream. `value` is
1561
+ * whatever the event is worth in your terms (minutes saved, revenue…);
1562
+ * Gateway sums it in the hours-saved/ROI rollups, and evals and task-set
1563
+ * replays can assert against the events. Link `traceId`/`sessionId` to tie
1564
+ * the event to the run that produced it.
1565
+ *
1566
+ * @example
1567
+ * ```ts
1568
+ * await client.trackEvent("user-123", "checkout_completed", {
1569
+ * value: 25,
1570
+ * metadata: { cartSize: 3 },
1571
+ * sessionId,
1572
+ * });
1573
+ * ```
1574
+ */
1575
+ async trackEvent(userId, event, options = {}) {
1576
+ const body = {
1577
+ userId,
1578
+ event,
1579
+ metadata: options.metadata ?? {},
1580
+ ...options.value !== void 0 ? { value: options.value } : {},
1581
+ ...options.traceId !== void 0 ? { traceId: options.traceId } : {},
1582
+ ...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {}
1583
+ };
1584
+ return parseEvent(
1585
+ await this.requestJson("POST", "/api/public/user-events", body)
1586
+ );
1587
+ }
1588
+ /** List a project's user events, optionally filtered by user/event/time. */
1589
+ async listEvents(filter = {}) {
1590
+ const params = new URLSearchParams();
1591
+ if (filter.userId) params.set("userId", filter.userId);
1592
+ if (filter.event) params.set("event", filter.event);
1593
+ if (filter.fromTimestamp)
1594
+ params.set("fromTimestamp", filter.fromTimestamp.toISOString());
1595
+ if (filter.toTimestamp)
1596
+ params.set("toTimestamp", filter.toTimestamp.toISOString());
1597
+ if (filter.page) params.set("page", String(filter.page));
1598
+ if (filter.limit) params.set("limit", String(filter.limit));
1599
+ const qs = params.toString();
1600
+ const payload = await this.requestJson(
1601
+ "GET",
1602
+ `/api/public/user-events${qs ? `?${qs}` : ""}`
1603
+ );
1604
+ const rows = Array.isArray(payload.data) ? payload.data : [];
1605
+ const meta = payload.meta && typeof payload.meta === "object" ? payload.meta : {};
1606
+ return {
1607
+ data: rows.map((row) => parseEvent(row)),
1608
+ totalItems: Number(meta.totalItems ?? rows.length)
1609
+ };
1610
+ }
1611
+ async requestJson(method, path10, payload) {
1612
+ let response;
1613
+ try {
1614
+ response = await fetch(`${this.host}${path10}`, {
1615
+ method,
1616
+ headers: {
1617
+ Authorization: this.authHeader(),
1618
+ "Content-Type": "application/json"
1619
+ },
1620
+ ...payload ? { body: JSON.stringify(payload) } : {},
1621
+ signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
1622
+ });
1623
+ } catch (error) {
1624
+ throw new UserProfileError(
1625
+ `${method} ${path10} failed: ${error instanceof Error ? error.message : String(error)}`,
1626
+ { cause: error }
1627
+ );
1628
+ }
1629
+ const text2 = await response.text();
1630
+ if (!response.ok) {
1631
+ throw new UserProfileError(
1632
+ `${method} ${path10} failed (${response.status}): ${text2.slice(0, 500)}`
1633
+ );
1634
+ }
1635
+ this.log("%s %s -> %s", method, path10, response.status);
1636
+ return safeJson(text2);
1637
+ }
1638
+ authHeader() {
1639
+ return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
1640
+ "base64"
1641
+ )}`;
1642
+ }
1643
+ log(message, ...args) {
1644
+ if (this.debug) {
1645
+ console.log(`[gateway.users] ${message}`, ...args);
1646
+ }
1647
+ }
1648
+ };
1649
+ users = {
1650
+ fromEnv: UsersClient.fromEnv
1651
+ };
1652
+ }
1653
+ });
1654
+
1655
+ // src/tracing/identity.ts
1656
+ import { AsyncLocalStorage } from "async_hooks";
1657
+ import { isSpanContextValid, trace } from "@opentelemetry/api";
1658
+ function resolveUserId() {
1659
+ const contextual = currentUserStorage.getStore();
1660
+ if (contextual) return contextual;
1661
+ const envUser = process.env.GATEWAY_USER_ID?.trim();
1662
+ return envUser ? envUser : void 0;
1663
+ }
1664
+ var USER_ID_ATTRIBUTE, currentUserStorage, IdentitySpanProcessor;
1665
+ var init_identity = __esm({
1666
+ "src/tracing/identity.ts"() {
1667
+ "use strict";
1668
+ init_users();
1669
+ USER_ID_ATTRIBUTE = "gateway.user.id";
1670
+ currentUserStorage = new AsyncLocalStorage();
1671
+ IdentitySpanProcessor = class {
1672
+ onStart(span, _parentContext) {
1673
+ const userId = resolveUserId();
1674
+ if (userId) {
1675
+ span.setAttribute(USER_ID_ATTRIBUTE, userId);
1676
+ }
1677
+ }
1678
+ onEnd(_span) {
1679
+ }
1680
+ async shutdown() {
1681
+ }
1682
+ async forceFlush() {
1683
+ }
1684
+ };
1685
+ }
1686
+ });
1687
+
1688
+ // src/tracing/session.ts
1689
+ var session_exports2 = {};
1690
+ __export(session_exports2, {
1691
+ SESSION_ID_ATTRIBUTE: () => SESSION_ID_ATTRIBUTE,
1692
+ SessionSpanProcessor: () => SessionSpanProcessor,
1693
+ getAmbientSessionId: () => getAmbientSessionId,
1694
+ withSession: () => withSession
1695
+ });
1696
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
1697
+ function getAmbientSessionId() {
1698
+ return currentSessionStorage.getStore();
1699
+ }
1700
+ function withSession(sessionId, fn) {
1701
+ return new Promise((resolve6, reject) => {
1702
+ currentSessionStorage.run(sessionId, () => {
1703
+ Promise.resolve().then(fn).then(resolve6, reject);
1704
+ });
1705
+ });
1706
+ }
1707
+ var SESSION_ID_ATTRIBUTE, currentSessionStorage, SessionSpanProcessor;
1708
+ var init_session2 = __esm({
1709
+ "src/tracing/session.ts"() {
1710
+ "use strict";
1711
+ SESSION_ID_ATTRIBUTE = "gateway.session.id";
1712
+ currentSessionStorage = new AsyncLocalStorage2();
1713
+ SessionSpanProcessor = class {
1714
+ onStart(span, _parentContext) {
1715
+ const sessionId = getAmbientSessionId();
1716
+ if (sessionId) {
1717
+ span.setAttribute(SESSION_ID_ATTRIBUTE, sessionId);
1718
+ }
1719
+ }
1720
+ onEnd(_span) {
1721
+ }
1722
+ async shutdown() {
1723
+ }
1724
+ async forceFlush() {
1725
+ }
1726
+ };
1727
+ }
1728
+ });
1729
+
1730
+ // src/scores/index.ts
1731
+ import { isSpanContextValid as isSpanContextValid2, trace as trace2 } from "@opentelemetry/api";
1732
+ function currentTraceId() {
1733
+ try {
1734
+ const span = trace2.getActiveSpan();
1735
+ if (!span) return void 0;
1736
+ const ctx = span.spanContext();
1737
+ if (!ctx || !isSpanContextValid2(ctx)) return void 0;
1738
+ return ctx.traceId;
1739
+ } catch {
1740
+ return void 0;
1741
+ }
1742
+ }
1743
+ function coerceSignalValue(value) {
1744
+ if (typeof value === "boolean") {
1745
+ return { numericValue: value ? 1 : 0, inferredType: "BOOLEAN" };
1746
+ }
1747
+ return { numericValue: value, inferredType: "NUMERIC" };
1748
+ }
1749
+ async function success(name, value = true, options = {}) {
1750
+ let client;
1751
+ try {
1752
+ client = ScoresClient.fromEnv();
1753
+ } catch (error) {
1754
+ console.warn(`[gateway.scores] success '${name}' skipped`, error);
1755
+ return;
1756
+ }
1757
+ await client.success(name, value, options);
1758
+ }
1759
+ var SUCCESS_SIGNAL_KEY, SUCCESS_METRIC_NAME_KEY, AGENT_ID_KEY, ScoreError, ScoresClient, scores;
1760
+ var init_scores = __esm({
1761
+ "src/scores/index.ts"() {
1762
+ "use strict";
1763
+ SUCCESS_SIGNAL_KEY = "gateway.success.signal";
1764
+ SUCCESS_METRIC_NAME_KEY = "gateway.success.metricName";
1765
+ AGENT_ID_KEY = "gateway.agent.id";
1766
+ ScoreError = class extends Error {
1767
+ constructor(message, options) {
1768
+ super(message);
1769
+ this.name = "ScoreError";
1770
+ this.cause = options?.cause;
1771
+ }
1772
+ };
1773
+ ScoresClient = class _ScoresClient {
1774
+ host;
1775
+ publicKey;
1776
+ secretKey;
1777
+ timeoutSeconds;
1778
+ debug;
1779
+ constructor(config) {
1780
+ this.host = config.host.replace(/\/$/, "");
1781
+ this.publicKey = config.publicKey;
1782
+ this.secretKey = config.secretKey;
1783
+ this.timeoutSeconds = config.timeoutSeconds ?? 15;
1784
+ this.debug = config.debug ?? false;
1785
+ }
1786
+ /** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
1787
+ static fromEnv(config = {}) {
1788
+ const host = config.host ?? process.env.GATEWAY_HOST;
1789
+ const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
1790
+ const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
1791
+ const missing = [
1792
+ ["host", host],
1793
+ ["publicKey", publicKey],
1794
+ ["secretKey", secretKey]
1795
+ ].filter(([, value]) => !value).map(([name]) => name);
1796
+ if (missing.length > 0) {
1797
+ throw new ScoreError(
1798
+ `Missing Gateway scores configuration: ${missing.join(", ")}`
1799
+ );
1800
+ }
1801
+ return new _ScoresClient({
1802
+ ...config,
1803
+ host,
1804
+ publicKey,
1805
+ secretKey
1806
+ });
1807
+ }
1808
+ /**
1809
+ * Post a score, throwing {@link ScoreError} on any failure. Builds the exact
1810
+ * `POST /api/public/scores` body the Python SDK sends: exactly one of
1811
+ * `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.
1812
+ * When no target is provided, the active OTel trace id is used; if none is
1813
+ * available the call is a no-op (returns without posting), matching Python.
1814
+ */
1815
+ async postScore(name, value, options = {}) {
1816
+ const body = this.buildScoreBody(name, value, options);
1817
+ if (!body) {
1818
+ this.log(
1819
+ "score '%s' dropped: no sessionId, traceId or datasetRunId available",
1820
+ name
1821
+ );
1822
+ return;
1823
+ }
1824
+ await this.request("POST", "/api/public/scores", body);
1825
+ }
1826
+ /**
1827
+ * Build the score request body, or null when no target can be resolved.
1828
+ * Split out so it is unit-testable and shared by score()/success().
1829
+ */
1830
+ buildScoreBody(name, value, options = {}) {
1831
+ const traceId = options.traceId ?? currentTraceId();
1832
+ const target = {};
1833
+ if (options.sessionId) {
1834
+ target.sessionId = options.sessionId;
1835
+ } else if (options.datasetRunId) {
1836
+ target.datasetRunId = options.datasetRunId;
1837
+ } else if (traceId) {
1838
+ target.traceId = traceId;
1839
+ if (options.observationId) target.observationId = options.observationId;
1840
+ } else {
1841
+ return null;
1842
+ }
1843
+ const metadata = {
1844
+ source: "gatewaysdk",
1845
+ ...options.metadata ?? {}
1846
+ };
1847
+ return {
1848
+ name,
1849
+ value,
1850
+ dataType: options.dataType ?? "NUMERIC",
1851
+ environment: options.environment ?? "default",
1852
+ ...options.comment ? { comment: options.comment } : {},
1853
+ metadata,
1854
+ ...target
1855
+ };
1856
+ }
1857
+ /**
1858
+ * Post a score, FAIL-SOFT: warns and resolves on any failure so a broken
1859
+ * transport never throws into (or rejects out of) the caller.
1860
+ */
1861
+ async score(name, value, options = {}) {
1862
+ try {
1863
+ await this.postScore(name, value, options);
1864
+ } catch (error) {
1865
+ this.warn(`score '${name}' failed`, error);
1866
+ }
1867
+ }
1868
+ /**
1869
+ * Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged
1870
+ * with `gateway.success.signal` + `gateway.success.metricName` (and the agent
1871
+ * identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN
1872
+ * score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.
1873
+ *
1874
+ * The metadata shape matches the Python SDK's `Run.success()` exactly, so the
1875
+ * platform's Success Metrics aggregation counts signals from either SDK
1876
+ * without change.
1877
+ *
1878
+ * @example
1879
+ * ```ts
1880
+ * await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
1881
+ * await scores.success("handle_time_s", 42, { agentId: "support-triage", sessionId });
1882
+ * ```
1883
+ */
1884
+ async success(name, value = true, options = {}) {
1885
+ const { numericValue, inferredType } = coerceSignalValue(value);
1886
+ const { agentId, metricName, metadata, dataType, ...rest } = options;
1887
+ const signalMetadata = {
1888
+ ...metadata ?? {},
1889
+ [SUCCESS_SIGNAL_KEY]: true,
1890
+ [SUCCESS_METRIC_NAME_KEY]: metricName ?? name,
1891
+ ...agentId ? { [AGENT_ID_KEY]: agentId } : {}
1892
+ };
1893
+ await this.score(name, numericValue, {
1894
+ ...rest,
1895
+ dataType: dataType ?? inferredType,
1896
+ metadata: signalMetadata
1897
+ });
1898
+ }
1899
+ async request(method, path10, payload) {
1900
+ let response;
1901
+ try {
1902
+ response = await fetch(`${this.host}${path10}`, {
1903
+ method,
1904
+ headers: {
1905
+ Authorization: this.authHeader(),
1906
+ "Content-Type": "application/json"
1907
+ },
1908
+ body: JSON.stringify(payload),
1909
+ signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
1910
+ });
1911
+ } catch (error) {
1912
+ throw new ScoreError(
1913
+ `${method} ${path10} failed: ${error instanceof Error ? error.message : String(error)}`,
1914
+ { cause: error }
1915
+ );
1916
+ }
1917
+ const text2 = await response.text();
1918
+ if (!response.ok) {
1919
+ throw new ScoreError(
1920
+ `${method} ${path10} failed (${response.status}): ${text2.slice(0, 500)}`
1921
+ );
1922
+ }
1923
+ this.log("%s %s -> %s", method, path10, response.status);
1924
+ }
1925
+ authHeader() {
1926
+ return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
1927
+ "base64"
1928
+ )}`;
1929
+ }
1930
+ log(message, ...args) {
1931
+ if (this.debug) {
1932
+ console.log(`[gateway.scores] ${message}`, ...args);
1933
+ }
1934
+ }
1935
+ warn(message, ...args) {
1936
+ console.warn(`[gateway.scores] ${message}`, ...args);
1937
+ }
1938
+ };
1939
+ scores = {
1940
+ fromEnv: ScoresClient.fromEnv,
1941
+ success
1942
+ };
1943
+ }
1944
+ });
1945
+
1946
+ // package.json
1947
+ var require_package = __commonJS({
1948
+ "package.json"(exports, module) {
1949
+ module.exports = {
1950
+ name: "@withgateway/sdk",
1951
+ version: "0.2.5",
1952
+ description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
1953
+ main: "./dist/index.js",
1954
+ module: "./dist/index.mjs",
1955
+ types: "./dist/index.d.ts",
1956
+ exports: {
1957
+ ".": {
1958
+ types: "./dist/index.d.ts",
1959
+ import: "./dist/index.mjs",
1960
+ require: "./dist/index.js"
1961
+ },
1962
+ "./experiments": {
1963
+ types: "./dist/experiments/index.d.ts",
1964
+ import: "./dist/experiments/index.mjs",
1965
+ require: "./dist/experiments/index.js"
1966
+ },
1967
+ "./context": {
1968
+ types: "./dist/context/index.d.ts",
1969
+ import: "./dist/context/index.mjs",
1970
+ require: "./dist/context/index.js"
1971
+ },
1972
+ "./memory": {
1973
+ types: "./dist/memory/index.d.ts",
1974
+ import: "./dist/memory/index.mjs",
1975
+ require: "./dist/memory/index.js"
1976
+ },
1977
+ "./personas": {
1978
+ types: "./dist/personas/index.d.ts",
1979
+ import: "./dist/personas/index.mjs",
1980
+ require: "./dist/personas/index.js"
1981
+ },
1982
+ "./replay": {
1983
+ types: "./dist/replay/index.d.ts",
1984
+ import: "./dist/replay/index.mjs",
1985
+ require: "./dist/replay/index.js"
1986
+ },
1987
+ "./sessions": {
1988
+ types: "./dist/sessions/index.d.ts",
1989
+ import: "./dist/sessions/index.mjs",
1990
+ require: "./dist/sessions/index.js"
1991
+ },
1992
+ "./tracing": {
1993
+ types: "./dist/tracing/index.d.ts",
1994
+ import: "./dist/tracing/index.mjs",
1995
+ require: "./dist/tracing/index.js"
1996
+ },
1997
+ "./security": {
1998
+ types: "./dist/security/index.d.ts",
1999
+ import: "./dist/security/index.mjs",
2000
+ require: "./dist/security/index.js"
2001
+ },
2002
+ "./users": {
2003
+ types: "./dist/users/index.d.ts",
2004
+ import: "./dist/users/index.mjs",
2005
+ require: "./dist/users/index.js"
2006
+ },
2007
+ "./scores": {
2008
+ types: "./dist/scores/index.d.ts",
2009
+ import: "./dist/scores/index.mjs",
2010
+ require: "./dist/scores/index.js"
2011
+ },
2012
+ "./worlds": {
2013
+ types: "./dist/worlds/index.d.ts",
2014
+ import: "./dist/worlds/index.mjs",
2015
+ require: "./dist/worlds/index.js"
2016
+ },
2017
+ "./files": {
2018
+ types: "./dist/files/index.d.ts",
2019
+ import: "./dist/files/index.mjs",
2020
+ require: "./dist/files/index.js"
2021
+ },
2022
+ "./traces": {
2023
+ types: "./dist/traces/index.d.ts",
2024
+ import: "./dist/traces/index.mjs",
2025
+ require: "./dist/traces/index.js"
2026
+ },
2027
+ "./hooks": {
2028
+ types: "./dist/hooks/index.d.ts",
2029
+ import: "./dist/hooks/index.mjs",
2030
+ require: "./dist/hooks/index.js"
2031
+ },
2032
+ "./cli": {
2033
+ types: "./dist/cli.d.ts",
2034
+ import: "./dist/cli.mjs",
2035
+ require: "./dist/cli.js"
2036
+ }
2037
+ },
2038
+ files: [
2039
+ "dist",
2040
+ "skills",
2041
+ "runtime",
2042
+ "README.md"
2043
+ ],
2044
+ publishConfig: {
2045
+ access: "public"
2046
+ },
2047
+ scripts: {
2048
+ prebuild: "node scripts/copy-skills.mjs && node scripts/copy-runtime.mjs",
2049
+ build: "tsup",
2050
+ dev: "tsup --watch",
2051
+ typecheck: "tsc --noEmit",
2052
+ "typecheck:examples": "tsc -p tsconfig.examples.json --noEmit",
2053
+ test: "vitest",
2054
+ "test:run": "vitest run",
2055
+ "test:integration": "vitest run --config vitest.integration.config.ts",
2056
+ lint: "eslint src --ext .ts",
2057
+ clean: "rm -rf dist",
2058
+ example: "tsx examples/test-ab-experiment.ts",
2059
+ "test:crm": "tsx examples/test-crm-ab-experiment.ts",
2060
+ "test:crm:balanced": "tsx examples/test-crm-ab-experiment.ts --balanced",
2061
+ "test:crm:full": "tsx examples/test-crm-ab-experiment.ts --users 12 --all-tasks --concurrency 3"
2062
+ },
2063
+ keywords: [
2064
+ "gateway",
2065
+ "ab-testing",
2066
+ "experiments",
2067
+ "opentelemetry",
2068
+ "tracing",
2069
+ "observability",
2070
+ "llm"
2071
+ ],
2072
+ author: "Gateway Team",
2073
+ license: "MIT",
2074
+ engines: {
2075
+ node: ">=18.0.0"
2076
+ },
2077
+ peerDependencies: {
2078
+ "@opentelemetry/api": "^1.0.0",
2079
+ "@opentelemetry/exporter-trace-otlp-http": ">=0.50.0",
2080
+ "@opentelemetry/resources": ">=1.0.0",
2081
+ "@opentelemetry/sdk-trace-base": ">=1.0.0",
2082
+ "@opentelemetry/sdk-trace-node": ">=1.0.0",
2083
+ playwright: ">=1.40.0"
2084
+ },
2085
+ peerDependenciesMeta: {
2086
+ "@opentelemetry/api": {
2087
+ optional: true
2088
+ },
2089
+ "@opentelemetry/sdk-trace-base": {
2090
+ optional: true
2091
+ },
2092
+ "@opentelemetry/sdk-trace-node": {
2093
+ optional: true
2094
+ },
2095
+ "@opentelemetry/exporter-trace-otlp-http": {
2096
+ optional: true
2097
+ },
2098
+ "@opentelemetry/resources": {
2099
+ optional: true
2100
+ },
2101
+ playwright: {
2102
+ optional: true
2103
+ }
2104
+ },
2105
+ optionalDependencies: {
2106
+ "@arizeai/openinference-instrumentation-anthropic": "^0.1.15",
2107
+ "@arizeai/openinference-instrumentation-openai": "^4.1.5"
2108
+ },
2109
+ devDependencies: {
2110
+ "@anthropic-ai/sdk": "^0.79.0",
2111
+ "@opentelemetry/api": "^1.9.0",
2112
+ "@opentelemetry/exporter-trace-otlp-http": "^0.212.0",
2113
+ "@opentelemetry/resources": "^2.6.0",
2114
+ "@opentelemetry/sdk-trace-base": "^1.30.1",
2115
+ "@opentelemetry/sdk-trace-node": "^2.6.0",
2116
+ "@opentelemetry/semantic-conventions": "^1.40.0",
2117
+ "@types/node": "^20.0.0",
2118
+ openai: "^4.70.0",
2119
+ tsup: "^8.0.0",
2120
+ tsx: "^4.0.0",
2121
+ typescript: "^5.0.0",
2122
+ vitest: "^3.2.6"
2123
+ },
2124
+ overrides: {
2125
+ protobufjs: "^8.4.1",
2126
+ "vite@6": "^6.4.3",
2127
+ postcss: "^8.5.18",
2128
+ "picomatch@4": "^4.0.4"
2129
+ },
2130
+ pnpm: {
2131
+ overrides: {
2132
+ protobufjs: "^8.4.1",
2133
+ vitest: "^3.2.6",
2134
+ "vite@6": "^6.4.3",
2135
+ postcss: "^8.5.18",
2136
+ "picomatch@4": "^4.0.4"
2137
+ }
2138
+ },
2139
+ bin: {
2140
+ gateway: "./dist/cli.js"
2141
+ }
2142
+ };
2143
+ }
2144
+ });
2145
+
2146
+ // src/build/index.ts
2147
+ import { createHash } from "crypto";
2148
+ function canonicalJson(value) {
2149
+ if (value === null || typeof value !== "object") {
2150
+ return JSON.stringify(value);
2151
+ }
2152
+ if (Array.isArray(value)) {
2153
+ return `[${value.map(canonicalJson).join(",")}]`;
2154
+ }
2155
+ const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
2156
+ return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
2157
+ }
2158
+ function sha256(text2) {
2159
+ return `sha256:${createHash("sha256").update(text2).digest("hex")}`;
2160
+ }
2161
+ function getRegisteredTools() {
2162
+ return [...registeredTools.values()].sort(
2163
+ (a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0
2164
+ );
2165
+ }
2166
+ function getRegisteredArtifacts() {
2167
+ return [...registeredArtifacts.values()].sort(
2168
+ (a, b) => `${a.kind}:${a.name}` < `${b.kind}:${b.name}` ? -1 : 1
2169
+ );
2170
+ }
2171
+ function getRegisteredSkills() {
2172
+ return Object.fromEntries(
2173
+ [...registeredSkills.entries()].sort(
2174
+ ([a], [b]) => a < b ? -1 : a > b ? 1 : 0
2175
+ )
2176
+ );
2177
+ }
2178
+ function getRegisteredPrompts() {
2179
+ return Object.fromEntries(
2180
+ [...registeredPrompts.entries()].sort(
2181
+ ([a], [b]) => a < b ? -1 : a > b ? 1 : 0
2182
+ )
2183
+ );
2184
+ }
2185
+ function sdkVersion() {
2186
+ try {
2187
+ const pkg = require_package();
2188
+ return `@withgateway/sdk@${pkg.version ?? "unknown"}`;
2189
+ } catch {
2190
+ return "@withgateway/sdk";
2191
+ }
2192
+ }
2193
+ function paasGitContext() {
2194
+ const env = process.env;
2195
+ if (env.VERCEL_GIT_COMMIT_SHA) {
2196
+ const isGithub = (env.VERCEL_GIT_PROVIDER ?? "github") === "github";
2197
+ return {
2198
+ provider: "vercel",
2199
+ sha: env.VERCEL_GIT_COMMIT_SHA,
2200
+ repo: isGithub && env.VERCEL_GIT_REPO_OWNER && env.VERCEL_GIT_REPO_SLUG ? `${env.VERCEL_GIT_REPO_OWNER}/${env.VERCEL_GIT_REPO_SLUG}` : void 0,
2201
+ branch: env.VERCEL_GIT_COMMIT_REF,
2202
+ login: env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN
2203
+ };
2204
+ }
2205
+ if (env.RENDER_GIT_COMMIT) {
2206
+ return {
2207
+ provider: "render",
2208
+ sha: env.RENDER_GIT_COMMIT,
2209
+ repo: env.RENDER_GIT_REPO_SLUG,
2210
+ // already "owner/repo"
2211
+ branch: env.RENDER_GIT_BRANCH
2212
+ };
2213
+ }
2214
+ if (env.RAILWAY_GIT_COMMIT_SHA) {
2215
+ return {
2216
+ provider: "railway",
2217
+ sha: env.RAILWAY_GIT_COMMIT_SHA,
2218
+ repo: env.RAILWAY_GIT_REPO_OWNER && env.RAILWAY_GIT_REPO_NAME ? `${env.RAILWAY_GIT_REPO_OWNER}/${env.RAILWAY_GIT_REPO_NAME}` : void 0,
2219
+ branch: env.RAILWAY_GIT_BRANCH,
2220
+ login: env.RAILWAY_GIT_AUTHOR
2221
+ };
2222
+ }
2223
+ if (env.NETLIFY && env.COMMIT_REF) {
2224
+ const repoMatch = /github\.com[/:]([^/]+\/[^/.]+)/.exec(
2225
+ env.REPOSITORY_URL ?? ""
2226
+ );
2227
+ return {
2228
+ provider: "netlify",
2229
+ sha: env.COMMIT_REF,
2230
+ repo: repoMatch?.[1],
2231
+ branch: env.BRANCH
2232
+ };
2233
+ }
2234
+ const bareSha = env.HEROKU_SLUG_COMMIT ?? env.SOURCE_VERSION ?? env.GIT_COMMIT ?? env.COMMIT_SHA;
2235
+ if (bareSha) {
2236
+ return {
2237
+ provider: env.HEROKU_SLUG_COMMIT || env.SOURCE_VERSION ? "heroku" : "generic",
2238
+ sha: bareSha
2239
+ };
2240
+ }
2241
+ return null;
2242
+ }
2243
+ function codeRef() {
2244
+ const paas = paasGitContext();
2245
+ const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA ?? process.env.CI_COMMIT_SHA ?? // GitLab
2246
+ process.env.CIRCLE_SHA1 ?? paas?.sha;
2247
+ if (!sha) return void 0;
2248
+ const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH ?? paas?.repo;
2249
+ return repo ? `git:${repo}@${sha}` : `git:${sha}`;
2250
+ }
2251
+ function assembleManifest(agentId) {
2252
+ return {
2253
+ agentId,
2254
+ ...codeRef() ? { codeRef: codeRef() } : {},
2255
+ sdk: sdkVersion(),
2256
+ tools: getRegisteredTools(),
2257
+ prompts: getRegisteredPrompts(),
2258
+ skills: getRegisteredSkills(),
2259
+ artifacts: getRegisteredArtifacts()
2260
+ };
2261
+ }
2262
+ function computeBuildHash(manifest) {
2263
+ const identity = {
2264
+ ...manifest,
2265
+ tools: manifest.tools.map(({ source: _source, ...tool }) => tool),
2266
+ artifacts: manifest.artifacts.map(
2267
+ ({ source: _source, ...artifact }) => artifact
2268
+ )
2269
+ };
2270
+ return sha256(canonicalJson(identity));
2271
+ }
2272
+ function attributionFromEnv() {
2273
+ const attribution = { source: "runtime" };
2274
+ if (process.env.GITHUB_ACTIONS) attribution.source = "ci";
2275
+ if (process.env.GITHUB_ACTOR) attribution.githubLogin = process.env.GITHUB_ACTOR;
2276
+ const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA;
2277
+ if (sha) attribution.commitSha = sha;
2278
+ if (process.env.GITHUB_REPOSITORY) attribution.repo = process.env.GITHUB_REPOSITORY;
2279
+ const branch = process.env.GITHUB_HEAD_REF ?? process.env.GITHUB_REF_NAME;
2280
+ if (branch) attribution.branch = branch;
2281
+ const paas = paasGitContext();
2282
+ if (paas) {
2283
+ if (attribution.source === "runtime") attribution.source = `paas:${paas.provider}`;
2284
+ if (!attribution.commitSha && paas.sha) attribution.commitSha = paas.sha;
2285
+ if (!attribution.repo && paas.repo) attribution.repo = paas.repo;
2286
+ if (!attribution.branch && paas.branch) attribution.branch = paas.branch;
2287
+ if (!attribution.githubLogin && paas.login) attribution.githubLogin = paas.login;
2288
+ }
2289
+ const ref = process.env.GITHUB_REF ?? "";
2290
+ const prMatch = /refs\/pull\/(\d+)\//.exec(ref);
2291
+ if (prMatch) attribution.prNumber = Number(prMatch[1]);
2292
+ if (process.env.GATEWAY_PR_TITLE) {
2293
+ attribution.prTitle = process.env.GATEWAY_PR_TITLE.slice(0, 300);
2294
+ }
2295
+ if (process.env.GATEWAY_PR_BODY) {
2296
+ attribution.prDescription = process.env.GATEWAY_PR_BODY.slice(0, 2e3);
2297
+ }
2298
+ if (attribution.repo && attribution.prNumber && !("prUrl" in attribution)) {
2299
+ attribution.prUrl = `https://github.com/${String(attribution.repo)}/pull/${String(attribution.prNumber)}`;
2300
+ }
2301
+ return attribution;
2302
+ }
2303
+ function trackActiveBuild(context) {
2304
+ activeBuilds.push(context);
2305
+ }
2306
+ async function registerBuild(params) {
2307
+ const auth = Buffer.from(`${params.publicKey}:${params.secretKey}`).toString(
2308
+ "base64"
2309
+ );
2310
+ const response = await fetch(
2311
+ `${params.host.replace(/\/$/, "")}/api/public/agent-builds`,
2312
+ {
2313
+ method: "POST",
2314
+ headers: {
2315
+ Authorization: `Basic ${auth}`,
2316
+ "Content-Type": "application/json"
2317
+ },
2318
+ body: JSON.stringify({
2319
+ agentId: params.agentId,
2320
+ buildHash: params.buildHash,
2321
+ ...params.version ? { semanticVer: params.version } : {},
2322
+ manifest: params.manifest,
2323
+ attribution: attributionFromEnv()
2324
+ }),
2325
+ signal: AbortSignal.timeout(15e3)
2326
+ }
2327
+ );
2328
+ if (!response.ok) {
2329
+ throw new Error(
2330
+ `build registration failed (${response.status}): ${(await response.text()).slice(0, 300)}`
2331
+ );
2332
+ }
2333
+ }
2334
+ var registeredTools, registeredPrompts, registeredSkills, registeredArtifacts, activeBuilds;
2335
+ var init_build = __esm({
2336
+ "src/build/index.ts"() {
2337
+ "use strict";
2338
+ registeredTools = /* @__PURE__ */ new Map();
2339
+ registeredPrompts = /* @__PURE__ */ new Map();
2340
+ registeredSkills = /* @__PURE__ */ new Map();
2341
+ registeredArtifacts = /* @__PURE__ */ new Map();
2342
+ activeBuilds = [];
2343
+ }
2344
+ });
2345
+
2346
+ // src/tracing/init.ts
2347
+ var init_exports = {};
2348
+ __export(init_exports, {
2349
+ flush: () => flush,
2350
+ init: () => init,
2351
+ initIsolated: () => initIsolated,
2352
+ isGatewayConfigured: () => isGatewayConfigured,
2353
+ isInitialized: () => isInitialized,
2354
+ shutdown: () => shutdown
2355
+ });
2356
+ function getEnv(key) {
2357
+ const value = process.env[key];
2358
+ return value ? value : void 0;
2359
+ }
2360
+ function log(message, ...args) {
2361
+ if (state.debug) {
2362
+ console.log(`[gatewaysdk.tracing] ${message}`, ...args);
2363
+ }
2364
+ }
2365
+ function warn(message, ...args) {
2366
+ console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
2367
+ }
2368
+ function buildProvider(config, logFn) {
2369
+ const host = config.host ?? getEnv(ENV_VARS.HOST);
2370
+ const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
2371
+ const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
2372
+ const endpoint = config.endpoint ?? getEnv(ENV_VARS.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
2373
+ const missing = [];
2374
+ if (!host) missing.push("host (or GATEWAY_HOST env var)");
2375
+ if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
2376
+ if (!secretKey) missing.push("secretKey (or GATEWAY_SECRET_KEY env var)");
2377
+ if (missing.length > 0) {
2378
+ throw new Error(
2379
+ `Missing required Gateway configuration: ${missing.join(", ")}. Provide these values via init() parameters or environment variables.`
2380
+ );
2381
+ }
2382
+ let NodeTracerProvider;
2383
+ let BatchSpanProcessor;
2384
+ let OTLPTraceExporter;
2385
+ let createResource;
2386
+ let ATTR_SERVICE_NAME;
2387
+ try {
2388
+ const traceNode = __require("@opentelemetry/sdk-trace-node");
2389
+ const traceBase = __require("@opentelemetry/sdk-trace-base");
2390
+ const otlpHttp = __require("@opentelemetry/exporter-trace-otlp-http");
2391
+ const resources = __require("@opentelemetry/resources");
2392
+ NodeTracerProvider = traceNode.NodeTracerProvider;
2393
+ BatchSpanProcessor = traceBase.BatchSpanProcessor;
2394
+ OTLPTraceExporter = otlpHttp.OTLPTraceExporter;
2395
+ if (resources.Resource) {
2396
+ createResource = (attrs) => new resources.Resource(attrs);
2397
+ } else if (resources.resourceFromAttributes) {
2398
+ createResource = resources.resourceFromAttributes;
2399
+ } else {
2400
+ createResource = () => resources.defaultResource();
2401
+ }
2402
+ try {
2403
+ const semconv = __require("@opentelemetry/semantic-conventions");
2404
+ ATTR_SERVICE_NAME = semconv.ATTR_SERVICE_NAME ?? semconv.SEMRESATTRS_SERVICE_NAME ?? "service.name";
2405
+ } catch {
2406
+ ATTR_SERVICE_NAME = "service.name";
2407
+ }
2408
+ } catch {
2409
+ throw new Error(
2410
+ "OpenTelemetry packages not found. Install them with:\nnpm install @opentelemetry/sdk-trace-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources"
2411
+ );
2412
+ }
2413
+ const resourceAttrs = {
2414
+ [ATTR_SERVICE_NAME]: config.serviceName ?? "gatewaysdk-tracing"
2415
+ };
2416
+ if (config.environment) {
2417
+ resourceAttrs["gateway.environment"] = config.environment;
2418
+ }
2419
+ if (config.version) {
2420
+ resourceAttrs["gateway.version"] = config.version;
2421
+ }
2422
+ let buildInfo = null;
2423
+ if (config.agent) {
2424
+ const manifest = assembleManifest(config.agent);
2425
+ const buildHash = computeBuildHash(manifest);
2426
+ buildInfo = { manifest, buildHash };
2427
+ resourceAttrs["gateway.agent.id"] = config.agent;
2428
+ resourceAttrs["gateway.build.hash"] = buildHash;
2429
+ if (config.version) {
2430
+ resourceAttrs["gateway.agent.version"] = config.version;
2431
+ }
2432
+ logFn(
2433
+ "Agent build identity: %s @ %s (%d tools, %d prompts)",
2434
+ config.agent,
2435
+ buildHash.slice(0, 15),
2436
+ manifest.tools.length,
2437
+ Object.keys(manifest.prompts).length
2438
+ );
2439
+ }
2440
+ let isLocalhost = false;
2441
+ if (endpoint) {
2442
+ try {
2443
+ const parsed = new URL(endpoint);
2444
+ isLocalhost = parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1";
2445
+ } catch {
2446
+ isLocalhost = false;
2447
+ }
2448
+ }
2449
+ if (endpoint && !endpoint.startsWith("https://") && !isLocalhost) {
2450
+ throw new Error(
2451
+ `Refusing to send credentials over insecure HTTP to ${endpoint}. Use HTTPS or localhost for development.`
2452
+ );
2453
+ }
2454
+ const credentials = `${publicKey}:${secretKey}`;
2455
+ const encoded = Buffer.from(credentials).toString("base64");
2456
+ const authHeader = `Basic ${encoded}`;
2457
+ const exporter = new OTLPTraceExporter({
2458
+ url: endpoint,
2459
+ headers: {
2460
+ Authorization: authHeader
2461
+ }
2462
+ });
2463
+ logFn("Configured Gateway exporter: endpoint=%s", endpoint);
2464
+ const spanProcessors = [];
2465
+ if (config.customSpanProcessors) {
2466
+ for (const sp of config.customSpanProcessors) {
2467
+ spanProcessors.push(sp);
2468
+ logFn(
2469
+ "Added custom span processor: %s",
2470
+ sp.constructor?.name ?? "unknown"
2471
+ );
2472
+ }
2473
+ }
2474
+ spanProcessors.push(new IdentitySpanProcessor());
2475
+ spanProcessors.push(new SessionSpanProcessor());
2476
+ const batchConfig = {};
2477
+ if (config.batch?.maxQueueSize) {
2478
+ batchConfig.maxQueueSize = config.batch.maxQueueSize;
2479
+ }
2480
+ if (config.batch?.scheduledDelayMillis) {
2481
+ batchConfig.scheduledDelayMillis = config.batch.scheduledDelayMillis;
2482
+ }
2483
+ if (config.batch?.maxExportBatchSize) {
2484
+ batchConfig.maxExportBatchSize = config.batch.maxExportBatchSize;
2485
+ }
2486
+ spanProcessors.push(new BatchSpanProcessor(exporter, batchConfig));
2487
+ let provider = new NodeTracerProvider({
2488
+ resource: createResource(resourceAttrs)
2489
+ });
2490
+ if (typeof provider.addSpanProcessor === "function") {
2491
+ for (const sp of spanProcessors) {
2492
+ provider.addSpanProcessor(sp);
2493
+ }
2494
+ } else {
2495
+ provider = new NodeTracerProvider({
2496
+ resource: createResource(resourceAttrs),
2497
+ spanProcessors
2498
+ });
2499
+ }
2500
+ return {
2501
+ provider,
2502
+ buildInfo,
2503
+ connection: { host, publicKey, secretKey }
2504
+ };
2505
+ }
2506
+ function scheduleBuildRegistration(config, built) {
2507
+ if (!config.agent || !built.buildInfo || config.registerBuild === false) {
2508
+ return;
2509
+ }
2510
+ const { manifest, buildHash } = built.buildInfo;
2511
+ trackActiveBuild({
2512
+ host: built.connection.host,
2513
+ publicKey: built.connection.publicKey,
2514
+ secretKey: built.connection.secretKey,
2515
+ agentId: config.agent,
2516
+ version: config.version,
2517
+ lastRegisteredHash: buildHash
2518
+ });
2519
+ void registerBuild({
2520
+ host: built.connection.host,
2521
+ publicKey: built.connection.publicKey,
2522
+ secretKey: built.connection.secretKey,
2523
+ agentId: config.agent,
2524
+ version: config.version,
2525
+ manifest,
2526
+ buildHash
2527
+ }).catch((err) => {
2528
+ warn("Build registration failed (agent keeps running):", err);
2529
+ });
2530
+ }
2531
+ function init(config = {}) {
2532
+ if (state.initialized) {
2533
+ log("Tracing already initialized. Call shutdown() first to reinitialize.");
2534
+ return;
2535
+ }
2536
+ state.debug = config.debug ?? false;
2537
+ const built = buildProvider(config, log);
2538
+ const provider = built.provider;
2539
+ provider.register();
2540
+ scheduleBuildRegistration(config, built);
2541
+ state.provider = provider;
2542
+ state.initialized = true;
2543
+ log(
2544
+ "GatewaySDK tracing initialized successfully (service=%s)",
2545
+ config.serviceName ?? "gatewaysdk-tracing"
2546
+ );
2547
+ }
2548
+ function initIsolated(config = {}) {
2549
+ const debugLog = config.debug ? (message, ...args) => console.log(`[gatewaysdk.tracing:isolated] ${message}`, ...args) : () => {
2550
+ };
2551
+ const built = buildProvider(config, debugLog);
2552
+ const provider = built.provider;
2553
+ scheduleBuildRegistration(config, built);
2554
+ const tracer = provider.getTracer("@withgateway/sdk");
2555
+ const scores2 = new ScoresClient({
2556
+ host: built.connection.host,
2557
+ publicKey: built.connection.publicKey,
2558
+ secretKey: built.connection.secretKey,
2559
+ debug: config.debug ?? false
2560
+ });
2561
+ debugLog(
2562
+ "Isolated tracing provider created (service=%s)",
2563
+ config.serviceName ?? "gatewaysdk-tracing"
2564
+ );
2565
+ return {
2566
+ tracer,
2567
+ scores: scores2,
2568
+ async flush() {
2569
+ try {
2570
+ await provider.forceFlush();
2571
+ } catch (e) {
2572
+ warn("Error during isolated flush:", e);
2573
+ }
2574
+ },
2575
+ async shutdown() {
2576
+ try {
2577
+ await provider.shutdown();
2578
+ } catch (e) {
2579
+ warn("Error during isolated shutdown:", e);
2580
+ }
2581
+ }
2582
+ };
2583
+ }
2584
+ async function flush() {
2585
+ if (!state.provider) {
2586
+ log("No provider to flush");
2587
+ return;
2588
+ }
2589
+ try {
2590
+ await state.provider.forceFlush();
2591
+ log("Flush completed successfully");
2592
+ } catch (e) {
2593
+ warn("Error during flush:", e);
2594
+ }
2595
+ }
2596
+ async function shutdown() {
2597
+ if (!state.initialized || !state.provider) {
2598
+ log("Tracing not initialized, nothing to shutdown");
2599
+ return;
2600
+ }
2601
+ try {
2602
+ await state.provider.shutdown();
2603
+ log("Shutdown complete");
2604
+ } catch (e) {
2605
+ warn("Error during shutdown:", e);
2606
+ }
2607
+ state.provider = null;
2608
+ state.initialized = false;
2609
+ }
2610
+ function isInitialized() {
2611
+ return state.initialized;
2612
+ }
2613
+ function isGatewayConfigured() {
2614
+ const host = getEnv(ENV_VARS.HOST);
2615
+ const publicKey = getEnv(ENV_VARS.PUBLIC_KEY);
2616
+ const secretKey = getEnv(ENV_VARS.SECRET_KEY);
2617
+ return !!(host && publicKey && secretKey);
2618
+ }
2619
+ var ENV_VARS, DEFAULT_OTLP_PATH, state;
2620
+ var init_init = __esm({
2621
+ "src/tracing/init.ts"() {
2622
+ "use strict";
2623
+ init_identity();
2624
+ init_session2();
2625
+ init_scores();
2626
+ init_build();
2627
+ ENV_VARS = {
2628
+ HOST: "GATEWAY_HOST",
2629
+ PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
2630
+ SECRET_KEY: "GATEWAY_SECRET_KEY",
2631
+ ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
2632
+ };
2633
+ DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
2634
+ state = {
2635
+ initialized: false,
2636
+ provider: null
2637
+ };
2638
+ }
2639
+ });
2640
+
2641
+ // src/worlds/taskRun.ts
2642
+ import { randomBytes } from "crypto";
2643
+ import * as fs from "fs";
2644
+ import * as path from "path";
2645
+ function worldRef(live, alias) {
2646
+ const { slug, ref } = live.worldRef(alias);
2647
+ const session = live.sessions[alias];
2648
+ return {
2649
+ slug,
2650
+ ref,
2651
+ versionId: session.versionId,
2652
+ sessionId: session.sessionId
2653
+ };
2654
+ }
2655
+ async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options) {
2656
+ const aliases = live.aliases;
2657
+ const anchor = aliases[0];
2658
+ const worldsMeta = Object.fromEntries(
2659
+ aliases.map((alias) => [alias, worldRef(live, alias)])
2660
+ );
2661
+ const evals = new GatewayEvalsClient(options);
2662
+ const evaluationId = await evals.createEvaluation(
2663
+ worldRef(live, anchor).versionId,
2664
+ {
2665
+ model,
2666
+ rolloutsPerExample: 1,
2667
+ metadata: {
2668
+ source: "worlds-task-run",
2669
+ task: {
2670
+ id: live.taskId,
2671
+ version: live.taskVersion,
2672
+ name: live.name,
2673
+ hash: live.hash
2674
+ },
2675
+ anchor,
2676
+ worlds: worldsMeta,
2677
+ runId
2678
+ }
2679
+ }
2680
+ );
2681
+ const reward = grade.reward;
2682
+ const sample = {
2683
+ exampleId: live.name ?? live.taskId ?? runId,
2684
+ task: live.name ?? live.taskId ?? runId,
2685
+ rolloutIndex: 0,
2686
+ prompt: live.instruction,
2687
+ completion: report,
2688
+ rewards: grade.rewards,
2689
+ info: {
2690
+ sessionId: runId,
2691
+ worlds: Object.fromEntries(
2692
+ aliases.map((alias) => [
2693
+ alias,
2694
+ {
2695
+ ...worldRef(live, alias),
2696
+ reward: grade.worlds[alias]?.reward ?? null
2697
+ }
2698
+ ])
2699
+ ),
2700
+ ungraded: grade.ungraded,
2701
+ traced,
2702
+ runDir,
2703
+ ...agentError ? { agentError } : {}
2704
+ }
2705
+ };
2706
+ if (typeof reward === "number") {
2707
+ sample.reward = reward;
2708
+ sample.score = reward;
2709
+ sample.correct = reward >= 1;
2710
+ }
2711
+ await evals.pushSamples(evaluationId, [sample]);
2712
+ await evals.finalizeEvaluation(evaluationId, {
2713
+ status: agentError ? "FAILED" : "COMPLETED",
2714
+ metrics: {
2715
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
2716
+ reward,
2717
+ ...Object.fromEntries(
2718
+ Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
2719
+ )
2720
+ }
2721
+ });
2722
+ return evaluationId;
2723
+ }
2724
+ function reportFromResult(result) {
2725
+ if (typeof result === "string") return { report: result, extra: {} };
2726
+ if (result && typeof result === "object") {
2727
+ const { report, ...extra } = result;
2728
+ return {
2729
+ report: report === void 0 || report === null ? null : typeof report === "string" ? report : String(report),
2730
+ extra
2731
+ };
2732
+ }
2733
+ if (result !== void 0 && result !== null)
2734
+ return { report: String(result), extra: {} };
2735
+ return { report: null, extra: {} };
2736
+ }
2737
+ async function runTask(ref, agent, opts) {
2738
+ const runId = `wtr-${randomBytes(6).toString("hex")}`;
2739
+ const outDir = opts.outDir ?? "runs";
2740
+ const runDir = path.join(outDir, runId);
2741
+ fs.mkdirSync(runDir, { recursive: true });
2742
+ const options = opts.options ?? {};
2743
+ const live = await openTask(ref, {
2744
+ surfaces: opts.surfaces,
2745
+ reuseWarm: opts.reuseWarm,
2746
+ options
2747
+ });
2748
+ const started = Date.now();
2749
+ try {
2750
+ await live.ready();
2751
+ let traced = false;
2752
+ if (opts.trace !== false) {
2753
+ try {
2754
+ const { isGatewayConfigured: isGatewayConfigured2, init: init2 } = await Promise.resolve().then(() => (init_init(), init_exports));
2755
+ if (isGatewayConfigured2()) {
2756
+ init2();
2757
+ traced = true;
2758
+ }
2759
+ } catch {
2760
+ traced = false;
2761
+ }
2762
+ }
2763
+ const taskRun = new TaskRun(
2764
+ runId,
2765
+ live.instruction ?? "",
2766
+ opts.model,
2767
+ live.sessions,
2768
+ await live.manifest()
2769
+ );
2770
+ let agentError = null;
2771
+ let report = null;
2772
+ let extra = {};
2773
+ try {
2774
+ const invoke = () => agent(taskRun);
2775
+ const result = traced ? await (await Promise.resolve().then(() => (init_session2(), session_exports2))).withSession(runId, invoke) : await invoke();
2776
+ ({ report, extra } = reportFromResult(result));
2777
+ } catch (error) {
2778
+ const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
2779
+ agentError = message.slice(0, 2e3);
2780
+ }
2781
+ const grade = report !== null ? await live.grade({ report }) : await live.grade();
2782
+ fs.writeFileSync(
2783
+ path.join(runDir, "manifest.json"),
2784
+ JSON.stringify(taskRun.manifest, null, 2)
2785
+ );
2786
+ fs.writeFileSync(
2787
+ path.join(runDir, "grades.json"),
2788
+ JSON.stringify(grade, null, 2)
2789
+ );
2790
+ if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
2791
+ const summary = {
2792
+ runId,
2793
+ taskId: live.taskId,
2794
+ taskVersion: live.taskVersion,
2795
+ name: live.name,
2796
+ instruction: taskRun.instruction,
2797
+ model: opts.model,
2798
+ aliases: live.aliases,
2799
+ reward: grade.reward,
2800
+ rewards: grade.rewards,
2801
+ ungraded: grade.ungraded,
2802
+ agentError,
2803
+ report,
2804
+ traced,
2805
+ runDir,
2806
+ durationSeconds: Math.round((Date.now() - started) / 10) / 100,
2807
+ notes: taskRun.notes,
2808
+ filed: false,
2809
+ ...extra
2810
+ };
2811
+ if (opts.file !== false) {
2812
+ summary.evaluationId = await fileTaskRun(
2813
+ live,
2814
+ runId,
2815
+ opts.model,
2816
+ report,
2817
+ grade,
2818
+ agentError,
2819
+ traced,
2820
+ runDir,
2821
+ options
2822
+ );
2823
+ summary.filed = true;
2824
+ }
2825
+ fs.writeFileSync(
2826
+ path.join(runDir, "run.json"),
2827
+ JSON.stringify(summary, null, 2)
2828
+ );
2829
+ return summary;
2830
+ } finally {
2831
+ if (!opts.keepUp) {
2832
+ try {
2833
+ await live.close({ keepWarm: false });
2834
+ } catch {
2835
+ }
2836
+ }
2837
+ }
2838
+ }
2839
+ var TaskRun;
2840
+ var init_taskRun = __esm({
2841
+ "src/worlds/taskRun.ts"() {
2842
+ "use strict";
2843
+ init_browser();
2844
+ init_evals();
2845
+ init_session();
2846
+ init_taskSessions();
2847
+ TaskRun = class {
2848
+ constructor(runId, instruction, model, sessions, manifest) {
2849
+ this.runId = runId;
2850
+ this.instruction = instruction;
2851
+ this.model = model;
2852
+ this.sessions = sessions;
2853
+ this.manifest = manifest;
2854
+ }
2855
+ notes = {};
2856
+ /** The alias's session, or a clear error naming the aliases there are. */
2857
+ session(alias) {
2858
+ const session = this.sessions[alias];
2859
+ if (!session)
2860
+ throw new Error(
2861
+ `task run has no world '${alias}'; its worlds are ${Object.keys(this.sessions).join(", ")}`
2862
+ );
2863
+ return session;
2864
+ }
2865
+ /** `alias`'s HTTP API. Raises the same error the session itself would
2866
+ * when it was not opened with `surfaces: ["api"]`. */
2867
+ async api(alias) {
2868
+ const surfaces = await this.session(alias).surfaces();
2869
+ if (!surfaces.api)
2870
+ throw new WorldSurfaceUnavailable(
2871
+ "api",
2872
+ `open '${alias}' with surfaces: ['api']`
982
2873
  );
983
- for (const call of page.calls) {
984
- calls.push({
985
- seq: Number(call.seq),
986
- kind: String(call.kind ?? "call"),
987
- tool: typeof call.tool === "string" ? call.tool : null,
988
- args: call.args ?? null,
989
- result: call.result ?? null,
990
- error: typeof call.error === "string" ? call.error : null,
991
- completedAt: typeof call.completedAt === "string" ? call.completedAt : null
992
- });
993
- }
994
- cursor = page.nextCursor ?? void 0;
995
- } while (cursor);
996
- return calls;
2874
+ return { url: surfaces.api.url, token: surfaces.api.token };
997
2875
  }
998
- /**
999
- * Promote this session's inline task into a stored task of the project
1000
- * (version 1, pinned to the session's world unless `world` says otherwise),
1001
- * so a task written for one end user can be reused and its grades
1002
- * compared. The session then records the new task's id. A session on a
1003
- * bundled task has nothing to save; one on a stored task is already saved.
1004
- */
1005
- async saveTask(opts = {}) {
1006
- const task = await apiRequest(
1007
- this.options,
1008
- "POST",
1009
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/task`,
1010
- {
1011
- ...opts.name !== void 0 ? { name: opts.name } : {},
1012
- ...opts.world !== void 0 ? { world: opts.world } : {},
1013
- ...opts.message !== void 0 ? { message: opts.message } : {}
1014
- }
1015
- );
1016
- this.current = {
1017
- ...this.current,
1018
- taskName: task.name,
1019
- task: {
1020
- ...this.taskInfo,
1021
- source: "stored",
1022
- name: task.name,
1023
- id: task.id,
1024
- version: task.version
2876
+ /** Every world's tools merged into one toolkit, each name prefixed
2877
+ * `<alias>.<tool>` so a model can tell which world a call goes to. A
2878
+ * world that exposes no tools contributes nothing — there is nothing to
2879
+ * merge. */
2880
+ async toolkit() {
2881
+ const schemas = [];
2882
+ const impls = {};
2883
+ const specs = [];
2884
+ for (const [alias, session] of Object.entries(this.sessions)) {
2885
+ const kit = await session.toolkit();
2886
+ if (!kit.specs.length) continue;
2887
+ for (const spec of kit.specs) {
2888
+ const name = `${alias}.${spec.name}`;
2889
+ const prefixed = {
2890
+ name,
2891
+ signature: spec.signature,
2892
+ doc: spec.doc,
2893
+ inputSchema: spec.inputSchema
2894
+ };
2895
+ specs.push(prefixed);
2896
+ const schema = schemaFromSignature(prefixed);
2897
+ schema.function.name = name;
2898
+ schemas.push(schema);
2899
+ impls[name] = kit.impls[modelToolName(spec.name)];
1025
2900
  }
1026
- };
1027
- return task;
2901
+ }
2902
+ return { schemas, impls, specs };
1028
2903
  }
1029
- /**
1030
- * End the session. `keepWarm` hands the container back to the pool instead
1031
- * of tearing it down, so the next session for the same task starts warm —
1032
- * the pooled instance still obeys the fleet's idle TTL and pinning.
1033
- */
1034
- async close(opts = {}) {
1035
- return apiRequest(
1036
- this.options,
1037
- "DELETE",
1038
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`,
1039
- { keepWarm: opts.keepWarm ?? false }
1040
- );
2904
+ /** Log arbitrary notes into this run's `run.json` (merged; a later call
2905
+ * overrides a key an earlier one set). */
2906
+ record(notes) {
2907
+ Object.assign(this.notes, notes);
1041
2908
  }
1042
2909
  };
1043
2910
  }
1044
2911
  });
1045
2912
 
1046
- // src/worlds/tasks.ts
1047
- async function createTask(input, options = {}) {
1048
- return apiRequest(
1049
- options,
1050
- "POST",
1051
- TASKS,
1052
- compact({
1053
- spec: input.spec,
1054
- name: input.name,
1055
- world: input.world,
1056
- message: input.message
1057
- })
1058
- );
1059
- }
1060
- async function updateTask(taskId, input, options = {}) {
1061
- if (input.spec === void 0 && input.name === void 0 && input.world === void 0)
1062
- throw new Error("updateTask: nothing to update \u2014 pass spec, name or world");
1063
- return apiRequest(
1064
- options,
1065
- "PATCH",
1066
- taskRoute(taskId),
1067
- compact({
1068
- spec: input.spec,
1069
- name: input.name,
1070
- world: input.world,
1071
- message: input.message
1072
- })
1073
- );
1074
- }
1075
- async function listTasks(filter = {}, options = {}) {
1076
- const params = new URLSearchParams();
1077
- if (filter.world) params.set("world", filter.world);
1078
- if (filter.page !== void 0) params.set("page", String(filter.page));
1079
- if (filter.limit !== void 0) params.set("limit", String(filter.limit));
1080
- for (const [key, value] of Object.entries(filter.metadata ?? {}))
1081
- params.set(`metadata.${key}`, value);
1082
- const query = params.toString();
1083
- return apiRequest(
1084
- options,
1085
- "GET",
1086
- query ? `${TASKS}?${query}` : TASKS
1087
- );
1088
- }
1089
- async function getTask(taskId, opts = {}, options = {}) {
1090
- const query = opts.version === void 0 ? "" : `?version=${String(opts.version)}`;
1091
- return apiRequest(
1092
- options,
1093
- "GET",
1094
- `${taskRoute(taskId)}${query}`
1095
- );
1096
- }
1097
- async function deleteTask(taskId, options = {}) {
1098
- return apiRequest(options, "DELETE", taskRoute(taskId));
1099
- }
1100
- async function validateTask(taskId, target = {}, options = {}) {
1101
- return apiRequest(
1102
- options,
1103
- "POST",
1104
- `${taskRoute(taskId)}/validate`,
1105
- compact({
1106
- version: target.version,
1107
- slug: target.slug,
1108
- versionId: target.versionId
1109
- })
1110
- );
1111
- }
1112
- var TASKS, taskRoute, compact;
1113
- var init_tasks = __esm({
1114
- "src/worlds/tasks.ts"() {
2913
+ // src/worlds/worldDir.ts
2914
+ import { createHash as createHash2 } from "crypto";
2915
+ import * as fs2 from "fs";
2916
+ import * as path2 from "path";
2917
+ 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;
2918
+ var init_worldDir = __esm({
2919
+ "src/worlds/worldDir.ts"() {
1115
2920
  "use strict";
1116
- init_worlds();
1117
- TASKS = "/api/public/world-tasks";
1118
- taskRoute = (id) => `${TASKS}/${encodeURIComponent(id)}`;
1119
- compact = (body) => Object.fromEntries(Object.entries(body).filter(([, v]) => v !== void 0));
2921
+ WORLD_MANIFEST = "gateway-env.toml";
2922
+ API_BUNDLE_MANIFEST = "gateway-api-world.toml";
2923
+ BUNDLED_WORLD_PREFIX = "server/world/";
2924
+ CONNECTOR_SPEC = "connector.toml";
2925
+ PYPROJECT = "pyproject.toml";
2926
+ WORLD_SCHEMA_MARKERS = ["schema/world.json", "schema/runtime.json", "schema/lock.json"];
2927
+ WORLD_ROOT_MARKERS = [CONNECTOR_SPEC, ...WORLD_SCHEMA_MARKERS];
2928
+ BROAD_ROOT_MARKERS = [WORLD_MANIFEST, API_BUNDLE_MANIFEST];
2929
+ SKIPPED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
2930
+ V1_DEP = /["']verifiers\s*(?:\[[^\]]*\])?\s*(?:>=|~=|==|>)\s*(?:0\.(?:[2-9]\d*|[1-9]\d+)|[1-9]\d*)/;
2931
+ V1_IMPORT = /^\s*(?:from\s+verifiers\.v1\s+import\b|from\s+verifiers\s+import\s+v1\b|import\s+verifiers\.v1\b)/m;
2932
+ V1_TASKSET = /class\s+\w+\s*\(\s*(?:(?:vf|v1|verifiers(?:\.v1)?)\.)?Taskset\s*[,)]/;
2933
+ HARBOR_TASK = /^(?:tasks\/)?([^/]+)\/task\.toml$/;
2934
+ skippedDir = (name) => name.startsWith(".") || SKIPPED_DIRS.has(name) || name.endsWith(".egg-info");
2935
+ byCodePoint = (a, b) => Buffer.compare(Buffer.from(a, "utf-8"), Buffer.from(b, "utf-8"));
2936
+ WorldDir = class _WorldDir {
2937
+ constructor(dir, worldRoot) {
2938
+ this.dir = dir;
2939
+ this.worldRoot = worldRoot;
2940
+ }
2941
+ pathsMemo;
2942
+ static open(dir) {
2943
+ const present = (rel) => fs2.existsSync(path2.join(dir, rel));
2944
+ const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
2945
+ return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
2946
+ }
2947
+ // ── Files ──────────────────────────────────────────────────────────────
2948
+ has(rel) {
2949
+ return fs2.existsSync(path2.join(this.dir, rel));
2950
+ }
2951
+ text(rel) {
2952
+ try {
2953
+ return fs2.readFileSync(path2.join(this.dir, rel), "utf-8");
2954
+ } catch {
2955
+ return null;
2956
+ }
2957
+ }
2958
+ /** A path under the authored world's root. */
2959
+ worldPath(rel) {
2960
+ return this.worldRoot + rel;
2961
+ }
2962
+ /** Every regular file the archive rules keep (`_archive.py`: no hidden,
2963
+ * dist, build, outputs, pycache or egg-info directories), as sorted
2964
+ * relpaths. Root files enter only when a Gateway manifest widens the root
2965
+ * or they are README.md, pyproject.toml or Python. */
2966
+ get archivePaths() {
2967
+ if (this.pathsMemo) return this.pathsMemo;
2968
+ const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
2969
+ const out = [];
2970
+ const walk = (rel) => {
2971
+ for (const entry of fs2.readdirSync(rel ? path2.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
2972
+ const child = rel ? `${rel}/${entry.name}` : entry.name;
2973
+ if (entry.isDirectory()) {
2974
+ if (!skippedDir(entry.name)) walk(child);
2975
+ } else if (entry.isFile() && !entry.name.startsWith(".")) {
2976
+ const rootKept = broad || ["README.md", PYPROJECT].includes(entry.name) || entry.name.endsWith(".py");
2977
+ if (rel || rootKept) out.push(child);
2978
+ }
2979
+ }
2980
+ };
2981
+ walk("");
2982
+ return this.pathsMemo = out.sort(byCodePoint);
2983
+ }
2984
+ /** A root `.gitignore` is present: the Python SDK honours it when
2985
+ * collecting, this package does not reimplement gitignore. */
2986
+ get hasRootGitignore() {
2987
+ return this.has(".gitignore");
2988
+ }
2989
+ /** SHA-256 over `utf8("file:" + relpath)` + bytes per archive path in
2990
+ * sorted order: the version's identity, byte-compatible with
2991
+ * `_archive.compute_content_hash` and Prime Intellect. */
2992
+ get contentHash() {
2993
+ const hasher = createHash2("sha256");
2994
+ for (const rel of this.archivePaths) {
2995
+ hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
2996
+ hasher.update(fs2.readFileSync(path2.join(this.dir, rel)));
2997
+ }
2998
+ return hasher.digest("hex");
2999
+ }
3000
+ // ── Markers ────────────────────────────────────────────────────────────
3001
+ get isApiBundle() {
3002
+ return this.has(API_BUNDLE_MANIFEST);
3003
+ }
3004
+ get hasManifest() {
3005
+ return this.has(this.worldPath(WORLD_MANIFEST));
3006
+ }
3007
+ get hasSchemaMarker() {
3008
+ return WORLD_SCHEMA_MARKERS.some((m) => this.has(this.worldPath(m)));
3009
+ }
3010
+ /** Harbor task dirs, relative to the tree: `tasks/<name>` when a `tasks/`
3011
+ * dir holds them, else the root `<name>` dirs. */
3012
+ taskDirs() {
3013
+ const names = /* @__PURE__ */ new Set();
3014
+ for (const rel of this.archivePaths) {
3015
+ const m = HARBOR_TASK.exec(rel);
3016
+ if (m) names.add(rel.startsWith("tasks/") ? `tasks/${m[1]}` : m[1]);
3017
+ }
3018
+ const under = [...names].filter((n) => n.startsWith("tasks/"));
3019
+ return (under.length ? under : [...names]).sort();
3020
+ }
3021
+ /** The Python package of a verifiers tree (`<pkg>/__init__.py` or
3022
+ * `src/<pkg>/__init__.py`), or null. */
3023
+ pythonPackage() {
3024
+ if (!this.has(PYPROJECT)) return null;
3025
+ for (const base of ["", "src/"]) {
3026
+ const init2 = this.archivePaths.find((rel) => {
3027
+ const m = /^([^/]+)\/__init__\.py$/.exec(rel.startsWith(base) ? rel.slice(base.length) : "");
3028
+ return m !== null && m[1] !== "tests";
3029
+ });
3030
+ if (init2) return { name: init2.slice(base.length).split("/")[0], init: init2 };
3031
+ }
3032
+ return null;
3033
+ }
3034
+ /** The package targets the `verifiers.v1` API: read from the root
3035
+ * pyproject and every archived Python module. */
3036
+ get isVerifiersV1() {
3037
+ for (const rel of this.archivePaths) {
3038
+ if (rel !== PYPROJECT && !rel.endsWith(".py")) continue;
3039
+ const text2 = this.text(rel);
3040
+ if (text2 === null) continue;
3041
+ if (rel === PYPROJECT ? V1_DEP.test(text2) : V1_IMPORT.test(text2) || text2.includes("__all__") && V1_TASKSET.test(text2))
3042
+ return true;
3043
+ }
3044
+ return false;
3045
+ }
3046
+ // ── The answer ─────────────────────────────────────────────────────────
3047
+ /** The runner's kind (`detect_env_kind`): api / mock-tools win over
3048
+ * everything, a harbor task tree next, then the verifiers API version. */
3049
+ get kind() {
3050
+ if (this.isApiBundle) return "api";
3051
+ if (this.has(WORLD_MANIFEST)) return "mock-tools";
3052
+ if (this.taskDirs().length) return "harbor";
3053
+ return this.isVerifiersV1 ? "verifiers-v1" : "verifiers";
3054
+ }
3055
+ };
1120
3056
  }
1121
3057
  });
1122
3058
 
@@ -1128,66 +3064,31 @@ __export(hub_exports, {
1128
3064
  collectArchiveRelPaths: () => collectArchiveRelPaths,
1129
3065
  computeContentHash: () => computeContentHash,
1130
3066
  createRef: () => createRef,
1131
- isIncludedInContentHash: () => isIncludedInContentHash,
1132
3067
  localTreeState: () => localTreeState,
1133
3068
  propose: () => propose,
1134
3069
  pull: () => pull,
1135
3070
  push: () => push,
3071
+ pushSlug: () => pushSlug,
1136
3072
  readLocalMeta: () => readLocalMeta,
1137
3073
  snapshotTreeHashes: () => snapshotTreeHashes,
1138
3074
  writeLocalMeta: () => writeLocalMeta
1139
3075
  });
1140
- import { createHash } from "crypto";
3076
+ import { createHash as createHash3 } from "crypto";
1141
3077
  import { gzipSync, gunzipSync } from "zlib";
1142
- import * as fs from "fs";
1143
- import * as path from "path";
1144
- function isExcludedDirComponent(component) {
1145
- return component.startsWith(".") || ROOT_EXCLUDED_DIRS.has(component) || component.endsWith(".egg-info");
1146
- }
1147
- function isIncludedInContentHash(relPath, includeAllRootFiles) {
1148
- const parts = relPath.split("/");
1149
- const name = parts[parts.length - 1];
1150
- const dirParts = parts.slice(0, -1);
1151
- if (name.startsWith(".")) return false;
1152
- if (parts.includes("__pycache__")) return false;
1153
- if (dirParts.length === 0) {
1154
- return includeAllRootFiles || name === "README.md" || name === "pyproject.toml" || BROAD_ROOT_MARKERS.includes(name) || name.endsWith(".py");
1155
- }
1156
- return !dirParts.some(isExcludedDirComponent);
1157
- }
3078
+ import * as fs3 from "fs";
3079
+ import * as path3 from "path";
1158
3080
  function collectArchiveRelPaths(dir) {
1159
- if (fs.existsSync(path.join(dir, ".gitignore"))) {
3081
+ const world = WorldDir.open(dir);
3082
+ if (world.hasRootGitignore) {
1160
3083
  throw new Error(
1161
3084
  "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."
1162
3085
  );
1163
3086
  }
1164
- const out = [];
1165
- const includeAllRootFiles = BROAD_ROOT_MARKERS.some(
1166
- (marker) => fs.existsSync(path.join(dir, marker))
1167
- );
1168
- const walk = (rel) => {
1169
- const abs = rel ? path.join(dir, rel) : dir;
1170
- for (const entry of fs.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
1171
- const childRel = rel ? `${rel}/${entry.name}` : entry.name;
1172
- if (entry.isDirectory()) {
1173
- if (!isExcludedDirComponent(entry.name)) walk(childRel);
1174
- } else if (entry.isFile()) {
1175
- if (isIncludedInContentHash(childRel, includeAllRootFiles)) {
1176
- out.push(childRel);
1177
- }
1178
- }
1179
- }
1180
- };
1181
- walk("");
1182
- return out.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
3087
+ return world.archivePaths;
1183
3088
  }
1184
3089
  function computeContentHash(dir) {
1185
- const hasher = createHash("sha256");
1186
- for (const relPath of collectArchiveRelPaths(dir)) {
1187
- hasher.update(Buffer.from(`file:${relPath}`, "utf-8"));
1188
- hasher.update(fs.readFileSync(path.join(dir, relPath)));
1189
- }
1190
- return hasher.digest("hex");
3090
+ collectArchiveRelPaths(dir);
3091
+ return WorldDir.open(dir).contentHash;
1191
3092
  }
1192
3093
  function octal(value, length) {
1193
3094
  return Buffer.from(value.toString(8).padStart(length - 1, "0") + "\0", "ascii");
@@ -1235,28 +3136,34 @@ function tarMember(name, payload) {
1235
3136
  function buildSourceTarball(dir) {
1236
3137
  const parts = [];
1237
3138
  for (const relPath of collectArchiveRelPaths(dir)) {
1238
- parts.push(...tarMember(relPath, fs.readFileSync(path.join(dir, relPath))));
3139
+ parts.push(...tarMember(relPath, fs3.readFileSync(path3.join(dir, relPath))));
1239
3140
  }
1240
3141
  parts.push(Buffer.alloc(1024));
1241
3142
  return gzipSync(Buffer.concat(parts), { level: 9 });
1242
3143
  }
1243
3144
  function readLocalMeta(dir) {
1244
3145
  try {
1245
- const raw = fs.readFileSync(path.join(dir, META_RELPATH), "utf-8");
3146
+ const raw = fs3.readFileSync(path3.join(dir, META_RELPATH), "utf-8");
1246
3147
  const parsed = JSON.parse(raw);
1247
3148
  return parsed && typeof parsed === "object" ? parsed : null;
1248
3149
  } catch {
1249
3150
  return null;
1250
3151
  }
1251
3152
  }
3153
+ function pushSlug(dir, explicit) {
3154
+ if (explicit) return explicit;
3155
+ const meta = readLocalMeta(dir);
3156
+ if (typeof meta?.slug === "string" && meta.slug) return meta.slug;
3157
+ return readPyproject(dir).name ?? path3.basename(path3.resolve(dir));
3158
+ }
1252
3159
  function writeLocalMeta(dir, meta) {
1253
- fs.mkdirSync(path.join(dir, ".gateway"), { recursive: true });
1254
- fs.writeFileSync(path.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
3160
+ fs3.mkdirSync(path3.join(dir, ".gateway"), { recursive: true });
3161
+ fs3.writeFileSync(path3.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
1255
3162
  }
1256
3163
  function snapshotTreeHashes(dir) {
1257
3164
  const out = {};
1258
3165
  for (const relPath of collectArchiveRelPaths(dir)) {
1259
- out[relPath] = createHash("sha256").update(fs.readFileSync(path.join(dir, relPath))).digest("hex");
3166
+ out[relPath] = createHash3("sha256").update(fs3.readFileSync(path3.join(dir, relPath))).digest("hex");
1260
3167
  }
1261
3168
  return out;
1262
3169
  }
@@ -1274,7 +3181,7 @@ function localTreeState(dir) {
1274
3181
  }
1275
3182
  function readPyproject(dir) {
1276
3183
  try {
1277
- const text2 = fs.readFileSync(path.join(dir, "pyproject.toml"), "utf-8");
3184
+ const text2 = fs3.readFileSync(path3.join(dir, "pyproject.toml"), "utf-8");
1278
3185
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
1279
3186
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
1280
3187
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -1284,33 +3191,32 @@ function readPyproject(dir) {
1284
3191
  }
1285
3192
  }
1286
3193
  function readWorldContractVersion(dir) {
1287
- for (const rel of ["schema/world.json", path.join("server", "world", "schema", "world.json")]) {
1288
- try {
1289
- const parsed = JSON.parse(fs.readFileSync(path.join(dir, rel), "utf-8"));
1290
- const pkg = parsed["x-gateway"]?.package;
1291
- if (pkg?.version) return { name: pkg.id, version: pkg.version };
1292
- } catch {
1293
- }
3194
+ const world = WorldDir.open(dir);
3195
+ try {
3196
+ const parsed = JSON.parse(world.text(world.worldPath("schema/world.json")) ?? "");
3197
+ const pkg = parsed["x-gateway"]?.package;
3198
+ if (pkg?.version) return { name: pkg.id, version: pkg.version };
3199
+ } catch {
1294
3200
  }
1295
3201
  return {};
1296
3202
  }
1297
3203
  function buildFileManifest(dir) {
1298
3204
  const entries = [];
1299
3205
  const walk = (rel) => {
1300
- const abs = rel ? path.join(dir, rel) : dir;
1301
- for (const entry of fs.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
3206
+ const abs = rel ? path3.join(dir, rel) : dir;
3207
+ for (const entry of fs3.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
1302
3208
  const childRel = rel ? `${rel}/${entry.name}` : entry.name;
1303
3209
  if (entry.isDirectory()) {
1304
3210
  if (!SKIP_DIRS.has(entry.name) && !entry.name.endsWith(".egg-info")) {
1305
3211
  walk(childRel);
1306
3212
  }
1307
3213
  } else if (entry.isFile()) {
1308
- const payload = fs.readFileSync(path.join(dir, childRel));
3214
+ const payload = fs3.readFileSync(path3.join(dir, childRel));
1309
3215
  entries.push({
1310
3216
  path: childRel,
1311
3217
  isDirectory: false,
1312
3218
  size: payload.length,
1313
- sha256: createHash("sha256").update(payload).digest("hex")
3219
+ sha256: createHash3("sha256").update(payload).digest("hex")
1314
3220
  });
1315
3221
  }
1316
3222
  }
@@ -1333,7 +3239,7 @@ async function putObject(url, data, shaB64) {
1333
3239
  }
1334
3240
  async function push(dir, opts = {}) {
1335
3241
  const options = opts.options ?? {};
1336
- if (!fs.statSync(dir).isDirectory()) throw new Error(`Not a directory: ${dir}`);
3242
+ if (!fs3.statSync(dir).isDirectory()) throw new Error(`Not a directory: ${dir}`);
1337
3243
  const localMeta = readLocalMeta(dir) ?? {};
1338
3244
  const branch = opts.branch ?? localMeta.branch ?? DEFAULT_BRANCH;
1339
3245
  const baseVersionId = opts.baseVersionId ?? localMeta.versionId ?? null;
@@ -1341,18 +3247,18 @@ async function push(dir, opts = {}) {
1341
3247
  const pyproject = readPyproject(dir);
1342
3248
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
1343
3249
  if (contractVersion) pyproject.version = contractVersion;
1344
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path.basename(path.resolve(dir));
3250
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path3.basename(path3.resolve(dir));
3251
+ const metadata = {
3252
+ name: opts.name ?? pyproject.name ?? slug,
3253
+ ...opts.description !== void 0 ? { description: opts.description } : {},
3254
+ ...opts.tags !== void 0 ? { tags: opts.tags } : {},
3255
+ ...opts.visibility ? { visibility: opts.visibility } : {}
3256
+ };
1345
3257
  const resolve6 = await apiRequest(
1346
3258
  options,
1347
3259
  "POST",
1348
3260
  "/api/public/benchmark-containers/resolve",
1349
- {
1350
- slug,
1351
- name: opts.name ?? pyproject.name ?? slug,
1352
- ...opts.description ? { description: opts.description } : {},
1353
- ...opts.tags?.length ? { tags: opts.tags } : {},
1354
- ...opts.visibility ? { visibility: opts.visibility } : {}
1355
- }
3261
+ { slug, ...metadata }
1356
3262
  );
1357
3263
  const tarball = buildSourceTarball(dir);
1358
3264
  const bundleSha256 = sha256b64(tarball);
@@ -1376,7 +3282,7 @@ async function push(dir, opts = {}) {
1376
3282
  options,
1377
3283
  "POST",
1378
3284
  `/api/public/benchmark-containers/versions/${version.versionId}/finalize`,
1379
- { branch, baseVersionId, ...opts.force ? { force: true } : {} }
3285
+ { branch, baseVersionId, ...opts.force ? { force: true } : {}, ...metadata }
1380
3286
  );
1381
3287
  writeLocalMeta(dir, {
1382
3288
  ...localMeta,
@@ -1421,9 +3327,9 @@ function extractBundle(bundle, dir) {
1421
3327
  longName = null;
1422
3328
  if (typeflag !== "0" && typeflag !== "") continue;
1423
3329
  if (name.startsWith("/") || name.split("/").includes("..")) continue;
1424
- const target = path.join(dir, name);
1425
- fs.mkdirSync(path.dirname(target), { recursive: true });
1426
- fs.writeFileSync(target, body);
3330
+ const target = path3.join(dir, name);
3331
+ fs3.mkdirSync(path3.dirname(target), { recursive: true });
3332
+ fs3.writeFileSync(target, body);
1427
3333
  written++;
1428
3334
  }
1429
3335
  return written;
@@ -1439,13 +3345,13 @@ async function pull(slugRef, target, options = {}) {
1439
3345
  if (!details.downloadUrl) {
1440
3346
  throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
1441
3347
  }
1442
- let dir = target ?? path.join(process.cwd(), slug);
3348
+ let dir = target ?? path3.join(process.cwd(), slug);
1443
3349
  if (!target) {
1444
3350
  let i = 1;
1445
3351
  const base = dir;
1446
- while (fs.existsSync(dir)) dir = `${base}-${i++}`;
3352
+ while (fs3.existsSync(dir)) dir = `${base}-${i++}`;
1447
3353
  }
1448
- fs.mkdirSync(dir, { recursive: true });
3354
+ fs3.mkdirSync(dir, { recursive: true });
1449
3355
  const resp = await fetch(details.downloadUrl);
1450
3356
  if (!resp.ok) throw new Error(`bundle download -> ${resp.status}`);
1451
3357
  const bundle = Buffer.from(await resp.arrayBuffer());
@@ -1472,15 +3378,15 @@ async function propose(dir, opts) {
1472
3378
  if (!meta?.slug || !meta.baseFiles) {
1473
3379
  throw new Error("This dir has no pull/push base \u2014 run pull first, then edit, then propose.");
1474
3380
  }
1475
- const state = localTreeState(dir);
1476
- if (state.clean) throw new Error("Nothing to propose \u2014 the tree matches its base.");
1477
- const changes = state.dirty.map((relPath) => {
1478
- const abs = path.join(dir, relPath);
1479
- if (!fs.existsSync(abs)) return { path: relPath, action: "delete" };
3381
+ const state2 = localTreeState(dir);
3382
+ if (state2.clean) throw new Error("Nothing to propose \u2014 the tree matches its base.");
3383
+ const changes = state2.dirty.map((relPath) => {
3384
+ const abs = path3.join(dir, relPath);
3385
+ if (!fs3.existsSync(abs)) return { path: relPath, action: "delete" };
1480
3386
  return {
1481
3387
  path: relPath,
1482
3388
  action: "write",
1483
- newContent: fs.readFileSync(abs, "utf-8")
3389
+ newContent: fs3.readFileSync(abs, "utf-8")
1484
3390
  };
1485
3391
  });
1486
3392
  const res = await apiRequest(
@@ -1506,13 +3412,14 @@ async function createRef(slug, name, kind, versionId, options = {}) {
1506
3412
  ...versionId ? { versionId } : {}
1507
3413
  });
1508
3414
  }
1509
- var DEFAULT_BRANCH, META_RELPATH, SKIP_DIRS, ROOT_EXCLUDED_DIRS, BROAD_ROOT_MARKERS, sha256b64;
3415
+ var DEFAULT_BRANCH, META_RELPATH, SKIP_DIRS, sha256b64;
1510
3416
  var init_hub = __esm({
1511
3417
  "src/worlds/hub.ts"() {
1512
3418
  "use strict";
1513
3419
  init_worlds();
3420
+ init_worldDir();
1514
3421
  DEFAULT_BRANCH = "main";
1515
- META_RELPATH = path.join(".gateway", ".env-metadata.json");
3422
+ META_RELPATH = path3.join(".gateway", ".env-metadata.json");
1516
3423
  SKIP_DIRS = /* @__PURE__ */ new Set([
1517
3424
  ".git",
1518
3425
  "__pycache__",
@@ -1523,9 +3430,7 @@ var init_hub = __esm({
1523
3430
  "build",
1524
3431
  "outputs"
1525
3432
  ]);
1526
- ROOT_EXCLUDED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
1527
- BROAD_ROOT_MARKERS = ["gateway-env.toml", "gateway-api-world.toml"];
1528
- sha256b64 = (data) => createHash("sha256").update(data).digest("base64");
3433
+ sha256b64 = (data) => createHash3("sha256").update(data).digest("base64");
1529
3434
  }
1530
3435
  });
1531
3436
 
@@ -1598,23 +3503,22 @@ async function validateWorld(dir, opts) {
1598
3503
  throw new Error(
1599
3504
  "No branch to validate on: pass --branch or run inside a git checkout / GitHub Actions."
1600
3505
  );
1601
- progress(`pushing ${dir} as ${opts.slug ?? "(pyproject name)"}@${branch}\u2026`);
3506
+ const slug = pushSlug(dir, opts.slug);
3507
+ progress(`pushing ${dir} as ${slug}@${branch}\u2026`);
1602
3508
  let tipVersionId = null;
1603
- if (opts.slug) {
1604
- try {
1605
- const tip = await apiRequest(
1606
- options,
1607
- "POST",
1608
- "/api/public/benchmark-containers/resolve-version",
1609
- { slug: opts.slug, ref: branch }
1610
- );
1611
- tipVersionId = tip.versionId ?? null;
1612
- } catch {
1613
- tipVersionId = null;
1614
- }
3509
+ try {
3510
+ const tip = await apiRequest(
3511
+ options,
3512
+ "POST",
3513
+ "/api/public/benchmark-containers/resolve-version",
3514
+ { slug, ref: branch }
3515
+ );
3516
+ tipVersionId = tip.versionId ?? null;
3517
+ } catch {
3518
+ tipVersionId = null;
1615
3519
  }
1616
3520
  const pushed = await push(dir, {
1617
- slug: opts.slug,
3521
+ slug,
1618
3522
  branch,
1619
3523
  baseVersionId: tipVersionId,
1620
3524
  message: `PR validation push (${provenance.commit?.slice(0, 12) ?? "local"})`,
@@ -2025,7 +3929,7 @@ var init_json = __esm({
2025
3929
  });
2026
3930
 
2027
3931
  // src/worlds/transforms.ts
2028
- import { createHash as createHash2 } from "crypto";
3932
+ import { createHash as createHash4 } from "crypto";
2029
3933
  function parseTransform(name) {
2030
3934
  const simple = Object.prototype.hasOwnProperty.call(SIMPLE, name) ? SIMPLE[name] : void 0;
2031
3935
  if (simple) return simple;
@@ -2049,8 +3953,8 @@ var init_transforms = __esm({
2049
3953
  "use strict";
2050
3954
  init_json();
2051
3955
  WHITESPACE = " \n\r\f\v";
2052
- WHITESPACE_EDGES = /^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$/g;
2053
- WHITESPACE_RUN = /[ \t\n\r\f\v]+/g;
3956
+ WHITESPACE_EDGES = new RegExp(`^[${WHITESPACE}]+|[${WHITESPACE}]+$`, "g");
3957
+ WHITESPACE_RUN = new RegExp(`[${WHITESPACE}]+`, "g");
2054
3958
  NOT_DIGIT = /[^0-9]/g;
2055
3959
  STRIP_PREFIX = "strip_prefix";
2056
3960
  trim = (value) => value.replace(WHITESPACE_EDGES, "");
@@ -2063,7 +3967,7 @@ var init_transforms = __esm({
2063
3967
  return at === -1 ? "" : value.slice(at + 1).toLowerCase();
2064
3968
  };
2065
3969
  stripPrefix = (prefix) => (value) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
2066
- hex = (algorithm, value) => createHash2(algorithm).update(value, "utf8").digest("hex");
3970
+ hex = (algorithm, value) => createHash4(algorithm).update(value, "utf8").digest("hex");
2067
3971
  SIMPLE = {
2068
3972
  lower: (value) => value.toLowerCase(),
2069
3973
  upper: (value) => value.toUpperCase(),
@@ -2087,7 +3991,7 @@ var init_transforms = __esm({
2087
3991
  });
2088
3992
 
2089
3993
  // src/worlds/redaction.ts
2090
- import { createHash as createHash3 } from "crypto";
3994
+ import { createHash as createHash5 } from "crypto";
2091
3995
  function pyText(value) {
2092
3996
  if (typeof value === "string") return value;
2093
3997
  if (Number.isInteger(value)) return String(value);
@@ -2096,7 +4000,7 @@ function pyText(value) {
2096
4000
  return pyFloatRepr(value);
2097
4001
  }
2098
4002
  function hashForm(slug, plaintext) {
2099
- return "sha256:" + createHash3("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
4003
+ return "sha256:" + createHash5("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
2100
4004
  }
2101
4005
  function preserveForm(slug, plaintext) {
2102
4006
  const kind = typeof plaintext === "string" ? "str" : isInt(plaintext) ? "int" : "float";
@@ -2105,12 +4009,12 @@ function preserveForm(slug, plaintext) {
2105
4009
  }
2106
4010
  function shapeText(slug, text2, kind) {
2107
4011
  const seed = Buffer.from(`${slug}:${text2}`, "utf8");
2108
- let digest = createHash3("sha256").update(seed).digest();
4012
+ let digest = createHash5("sha256").update(seed).digest();
2109
4013
  let cursor = 0;
2110
4014
  const out = [];
2111
4015
  for (const char of text2) {
2112
4016
  if (cursor === digest.length) {
2113
- digest = createHash3("sha256").update(Buffer.concat([digest, seed])).digest();
4017
+ digest = createHash5("sha256").update(Buffer.concat([digest, seed])).digest();
2114
4018
  cursor = 0;
2115
4019
  }
2116
4020
  const byte = digest[cursor];
@@ -2158,8 +4062,9 @@ var init_redaction = __esm({
2158
4062
  });
2159
4063
 
2160
4064
  // src/worlds/query.ts
2161
- function queryRowsSearch(entity, opts = {}) {
4065
+ function queryRowsSearch(entity, opts = {}, ref) {
2162
4066
  const search = new URLSearchParams();
4067
+ if (ref) search.set("ref", ref);
2163
4068
  search.set("entity", entity);
2164
4069
  if (opts.where && Object.keys(opts.where).length) search.set("where", JSON.stringify(opts.where));
2165
4070
  if (opts.limit !== void 0) search.set("limit", String(opts.limit));
@@ -2169,17 +4074,15 @@ function queryRowsSearch(entity, opts = {}) {
2169
4074
  if (opts.compare) search.set("compare", "true");
2170
4075
  return `?${search.toString()}`;
2171
4076
  }
2172
- async function queryRows(slug, entity, opts = {}, options = {}) {
2173
- if (slug.includes("@"))
2174
- throw new Error(
2175
- `queryRows reads a world's head version: pass the slug without @ref (got ${slug})`
2176
- );
4077
+ async function queryRows(slugRef, entity, opts = {}, options = {}) {
4078
+ const [slug, ref] = slugRef.split("@", 2);
4079
+ if (!slug || ref === "") throw new Error(`queryRows needs a slug, optionally slug@ref (got ${slugRef})`);
2177
4080
  if (!entity) throw new Error("queryRows needs an entity");
2178
4081
  if (opts.compare && !opts.across) throw new Error("queryRows: compare needs across");
2179
4082
  return apiRequest(
2180
4083
  options,
2181
4084
  "GET",
2182
- `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts)}`
4085
+ `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
2183
4086
  );
2184
4087
  }
2185
4088
  var init_query = __esm({
@@ -2235,12 +4138,12 @@ var init_router = __esm({
2235
4138
  });
2236
4139
 
2237
4140
  // src/cli/config.ts
2238
- import * as fs2 from "fs";
4141
+ import * as fs4 from "fs";
2239
4142
  import * as os from "os";
2240
- import * as path2 from "path";
2241
- async function api(creds, method, route, body, init = {}) {
4143
+ import * as path4 from "path";
4144
+ async function api(creds, method, route, body, init2 = {}) {
2242
4145
  const controller = new AbortController();
2243
- const timer = setTimeout(() => controller.abort(), init.timeoutMs ?? 12e4);
4146
+ const timer = setTimeout(() => controller.abort(), init2.timeoutMs ?? 12e4);
2244
4147
  try {
2245
4148
  const res = await fetch(`${creds.host}${route}`, {
2246
4149
  method,
@@ -2278,9 +4181,9 @@ var init_config = __esm({
2278
4181
 
2279
4182
  // src/cli/runtime/local.ts
2280
4183
  import { spawn, spawnSync } from "child_process";
2281
- import * as fs3 from "fs";
4184
+ import * as fs5 from "fs";
2282
4185
  import * as net from "net";
2283
- import * as path3 from "path";
4186
+ import * as path5 from "path";
2284
4187
  function tailOf(output) {
2285
4188
  return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
2286
4189
  }
@@ -2295,16 +4198,16 @@ function runtimeRoot() {
2295
4198
  const candidates = [];
2296
4199
  if (process.env.GATEWAY_RUNTIME_PATH)
2297
4200
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
2298
- const entry = process.argv[1] && fs3.existsSync(process.argv[1]) ? path3.dirname(fs3.realpathSync(process.argv[1])) : null;
4201
+ const entry = process.argv[1] && fs5.existsSync(process.argv[1]) ? path5.dirname(fs5.realpathSync(process.argv[1])) : null;
2299
4202
  if (entry) {
2300
- candidates.push(path3.resolve(entry, "..", "runtime"));
2301
- candidates.push(path3.resolve(entry, "..", "..", "runtime"));
2302
- candidates.push(path3.resolve(entry, "..", "..", "sdk", "src"));
4203
+ candidates.push(path5.resolve(entry, "..", "runtime"));
4204
+ candidates.push(path5.resolve(entry, "..", "..", "runtime"));
4205
+ candidates.push(path5.resolve(entry, "..", "..", "sdk", "src"));
2303
4206
  }
2304
- candidates.push(path3.resolve(process.cwd(), "runtime"));
4207
+ candidates.push(path5.resolve(process.cwd(), "runtime"));
2305
4208
  for (const root of candidates)
2306
- if (fs3.existsSync(
2307
- path3.join(root, "gatewaysdk", "environment", "schema", "platform.py")
4209
+ if (fs5.existsSync(
4210
+ path5.join(root, "gatewaysdk", "environment", "schema", "platform.py")
2308
4211
  ))
2309
4212
  return root;
2310
4213
  return null;
@@ -2472,18 +4375,18 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
2472
4375
  });
2473
4376
 
2474
4377
  // src/cli/runtime/client.ts
2475
- import * as fs4 from "fs";
2476
- import * as path4 from "path";
4378
+ import * as fs6 from "fs";
4379
+ import * as path6 from "path";
2477
4380
  function isBuildOutput(relPath) {
2478
4381
  return BUILD_OUTPUT.test(relPath);
2479
4382
  }
2480
4383
  function readWorldFiles(dir, opts = {}) {
2481
- if (!fs4.existsSync(dir) || !fs4.statSync(dir).isDirectory())
4384
+ if (!fs6.existsSync(dir) || !fs6.statSync(dir).isDirectory())
2482
4385
  throw new CliError(`Not a directory: ${dir}`);
2483
4386
  const out = [];
2484
4387
  const walk = (rel) => {
2485
- const abs = rel ? path4.join(dir, rel) : dir;
2486
- const entries = fs4.readdirSync(abs, { withFileTypes: true });
4388
+ const abs = rel ? path6.join(dir, rel) : dir;
4389
+ const entries = fs6.readdirSync(abs, { withFileTypes: true });
2487
4390
  entries.sort((a, b) => a.name.localeCompare(b.name));
2488
4391
  for (const entry of entries) {
2489
4392
  const childRel = rel ? `${rel}/${entry.name}` : entry.name;
@@ -2493,7 +4396,7 @@ function readWorldFiles(dir, opts = {}) {
2493
4396
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
2494
4397
  out.push({
2495
4398
  path: childRel,
2496
- content: fs4.readFileSync(path4.join(dir, childRel)).toString("base64")
4399
+ content: fs6.readFileSync(path6.join(dir, childRel)).toString("base64")
2497
4400
  });
2498
4401
  }
2499
4402
  }
@@ -2508,19 +4411,19 @@ function assertSafeRelPath(rel) {
2508
4411
  }
2509
4412
  function writeWorldFiles(dir, files, opts = {}) {
2510
4413
  for (const file of files) assertSafeRelPath(file.path);
2511
- if (opts.clean && fs4.existsSync(dir)) {
4414
+ if (opts.clean && fs6.existsSync(dir)) {
2512
4415
  const keep = new Set(files.map((f) => f.path));
2513
4416
  for (const existing of readWorldFiles(dir)) {
2514
- if (!keep.has(existing.path)) fs4.rmSync(path4.join(dir, existing.path));
4417
+ if (!keep.has(existing.path)) fs6.rmSync(path6.join(dir, existing.path));
2515
4418
  }
2516
4419
  pruneEmptyDirs(dir);
2517
4420
  }
2518
- fs4.mkdirSync(dir, { recursive: true });
4421
+ fs6.mkdirSync(dir, { recursive: true });
2519
4422
  const written = [];
2520
4423
  for (const file of files) {
2521
- const target = path4.join(dir, file.path);
2522
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
2523
- fs4.writeFileSync(target, Buffer.from(file.content, "base64"));
4424
+ const target = path6.join(dir, file.path);
4425
+ fs6.mkdirSync(path6.dirname(target), { recursive: true });
4426
+ fs6.writeFileSync(target, Buffer.from(file.content, "base64"));
2524
4427
  written.push(file.path);
2525
4428
  }
2526
4429
  return written;
@@ -2528,14 +4431,14 @@ function writeWorldFiles(dir, files, opts = {}) {
2528
4431
  function pruneEmptyDirs(root) {
2529
4432
  const walk = (abs) => {
2530
4433
  let empty = true;
2531
- for (const entry of fs4.readdirSync(abs, { withFileTypes: true })) {
2532
- const child = path4.join(abs, entry.name);
4434
+ for (const entry of fs6.readdirSync(abs, { withFileTypes: true })) {
4435
+ const child = path6.join(abs, entry.name);
2533
4436
  if (entry.isDirectory()) {
2534
4437
  if (SKIP_DIRS2.has(entry.name)) {
2535
4438
  empty = false;
2536
4439
  continue;
2537
4440
  }
2538
- if (walk(child)) fs4.rmdirSync(child);
4441
+ if (walk(child)) fs6.rmdirSync(child);
2539
4442
  else empty = false;
2540
4443
  } else empty = false;
2541
4444
  }
@@ -2627,8 +4530,8 @@ __export(data_exports, {
2627
4530
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
2628
4531
  showLocalWorldData: () => showLocalWorldData
2629
4532
  });
2630
- import * as fs5 from "fs";
2631
- import * as path5 from "path";
4533
+ import * as fs7 from "fs";
4534
+ import * as path7 from "path";
2632
4535
  async function importLocalWorldData(dir, rows, opts, creds) {
2633
4536
  const res = await runtime(creds, {
2634
4537
  command: "import-data",
@@ -2654,7 +4557,7 @@ function dropBuildOutputs(dir) {
2654
4557
  const dropped = [];
2655
4558
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
2656
4559
  if (!BUILD_OUTPUT2.test(file.path)) continue;
2657
- fs5.rmSync(path5.join(dir, file.path), { force: true });
4560
+ fs7.rmSync(path7.join(dir, file.path), { force: true });
2658
4561
  dropped.push(file.path);
2659
4562
  }
2660
4563
  return dropped;
@@ -2665,6 +4568,7 @@ async function checkLocalWorldData(dir, rows, creds, opts = {}) {
2665
4568
  files: readWorldFiles(dir),
2666
4569
  rows,
2667
4570
  mode: "append",
4571
+ dry_run: true,
2668
4572
  ...opts.redact && opts.redact !== "off" ? { redact: opts.redact } : {}
2669
4573
  });
2670
4574
  return {
@@ -2720,7 +4624,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
2720
4624
  const payload = await localBridge(
2721
4625
  {
2722
4626
  command: "query",
2723
- world: path5.resolve(dir),
4627
+ world: path7.resolve(dir),
2724
4628
  entity,
2725
4629
  across: true,
2726
4630
  ...opts.compare ? { compare: true } : {},
@@ -2791,9 +4695,9 @@ __export(transform_exports, {
2791
4695
  transformRecords: () => transformRecords
2792
4696
  });
2793
4697
  import { spawn as spawn2 } from "child_process";
2794
- import * as fs6 from "fs";
4698
+ import * as fs8 from "fs";
2795
4699
  import * as os2 from "os";
2796
- import * as path6 from "path";
4700
+ import * as path8 from "path";
2797
4701
  import { createRequire } from "module";
2798
4702
  function checkRows(value, where) {
2799
4703
  if (!isObject(value)) throw new CliError(`${where} must return {entity: [rows]}; got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`, 1);
@@ -2818,27 +4722,27 @@ function typescriptPrefix() {
2818
4722
  }
2819
4723
  }
2820
4724
  function command(script, wrapperDir) {
2821
- const suffix = path6.extname(script).toLowerCase();
4725
+ const suffix = path8.extname(script).toLowerCase();
2822
4726
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
2823
4727
  if (NODE_SUFFIXES.includes(suffix)) {
2824
- const wrapper = path6.join(wrapperDir, "transform-wrapper.mjs");
2825
- fs6.writeFileSync(wrapper, NODE_WRAPPER);
4728
+ const wrapper = path8.join(wrapperDir, "transform-wrapper.mjs");
4729
+ fs8.writeFileSync(wrapper, NODE_WRAPPER);
2826
4730
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
2827
4731
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
2828
4732
  }
2829
- throw new CliError(`${path6.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
4733
+ throw new CliError(`${path8.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
2830
4734
  }
2831
4735
  async function runTransform(script, records, opts = {}) {
2832
- if (!fs6.existsSync(script) || !fs6.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
2833
- const resolved = path6.resolve(script);
2834
- const name = path6.basename(script);
4736
+ if (!fs8.existsSync(script) || !fs8.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
4737
+ const resolved = path8.resolve(script);
4738
+ const name = path8.basename(script);
2835
4739
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
2836
- const scratch = fs6.mkdtempSync(path6.join(os2.tmpdir(), "gateway-transform-"));
4740
+ const scratch = fs8.mkdtempSync(path8.join(os2.tmpdir(), "gateway-transform-"));
2837
4741
  try {
2838
4742
  const { file, args } = command(resolved, scratch);
2839
4743
  const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !SCRUBBED.test(key)));
2840
4744
  const done = await new Promise((resolve6, reject) => {
2841
- const child = spawn2(file, args, { cwd: path6.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
4745
+ const child = spawn2(file, args, { cwd: path8.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
2842
4746
  let stdout = "";
2843
4747
  let stderr = "";
2844
4748
  let timedOut = false;
@@ -2869,7 +4773,7 @@ async function runTransform(script, records, opts = {}) {
2869
4773
  }
2870
4774
  return { rows: checkRows(value, name), stderr: done.stderr };
2871
4775
  } finally {
2872
- fs6.rmSync(scratch, { recursive: true, force: true });
4776
+ fs8.rmSync(scratch, { recursive: true, force: true });
2873
4777
  }
2874
4778
  }
2875
4779
  async function transformRecords(transform, records, opts = {}) {
@@ -2961,13 +4865,18 @@ if (fn) {
2961
4865
  });
2962
4866
 
2963
4867
  // src/cli/platform/worldData/batches.ts
2964
- import { createHash as createHash4 } from "crypto";
2965
- import * as fs7 from "fs";
4868
+ import { createHash as createHash6 } from "crypto";
4869
+ import * as fs9 from "fs";
4870
+ function isSettled(status) {
4871
+ if (!TERMINAL_STATES.has(status.state)) return false;
4872
+ if (status.state !== "applied") return true;
4873
+ return status.dryRun === true || status.publish !== "now" || !status.snapshotId;
4874
+ }
2966
4875
  function uploadHeadersFor(gz) {
2967
4876
  return {
2968
4877
  "content-type": "application/gzip",
2969
4878
  "content-length": String(gz.length),
2970
- "x-amz-checksum-sha256": createHash4("sha256").update(gz).digest("base64")
4879
+ "x-amz-checksum-sha256": createHash6("sha256").update(gz).digest("base64")
2971
4880
  };
2972
4881
  }
2973
4882
  async function forEachLimited(items, limit, fn) {
@@ -3004,7 +4913,7 @@ async function waitForBatch(client, batchId, opts = {}) {
3004
4913
  opts.onPhase?.(phase);
3005
4914
  last = phase;
3006
4915
  }
3007
- if (TERMINAL_STATES.has(status.state)) return status;
4916
+ if (isSettled(status)) return status;
3008
4917
  await sleep2(delay);
3009
4918
  delay = Math.min(1e4, Math.round(delay * 1.5));
3010
4919
  }
@@ -3094,7 +5003,7 @@ var init_batches = __esm({
3094
5003
  });
3095
5004
 
3096
5005
  // src/cli/platform/worldData/chunker.ts
3097
- import { createHash as createHash5 } from "crypto";
5006
+ import { createHash as createHash7 } from "crypto";
3098
5007
  import { deflateRawSync } from "zlib";
3099
5008
  function chunkLine(row, declared) {
3100
5009
  if (declared !== null) return dumps(row.row) + "\n";
@@ -3108,7 +5017,7 @@ async function* buildChunks(rows, plan) {
3108
5017
  let rawBytes = 0;
3109
5018
  let ordinal = 0;
3110
5019
  let seen = 0;
3111
- const flush = () => {
5020
+ const flush2 = () => {
3112
5021
  const raw = Buffer.concat(lines);
3113
5022
  const built = finishChunk(ordinal++, raw, lines.length, plan.entity, seen - lines.length);
3114
5023
  lines = [];
@@ -3118,23 +5027,23 @@ async function* buildChunks(rows, plan) {
3118
5027
  for await (const row of rows) {
3119
5028
  const line = Buffer.from(chunkLine(row, plan.entity), "utf8");
3120
5029
  const full = lines.length > 0 && (plan.chunkRows !== null && lines.length >= plan.chunkRows || rawBytes + line.length > plan.chunkBytes);
3121
- if (full) yield flush();
5030
+ if (full) yield flush2();
3122
5031
  lines.push(line);
3123
5032
  rawBytes += line.length;
3124
5033
  seen++;
3125
5034
  }
3126
- if (lines.length) yield flush();
5035
+ if (lines.length) yield flush2();
3127
5036
  }
3128
5037
  function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
3129
5038
  const gz = gzipDeterministic(raw, { name: chunkName(ordinal) });
3130
5039
  return {
3131
5040
  ordinal,
3132
- digest: createHash5("sha256").update(gz).digest("hex"),
5041
+ digest: createHash7("sha256").update(gz).digest("hex"),
3133
5042
  bytes: gz.length,
3134
5043
  rows,
3135
5044
  entity,
3136
5045
  rawBytes: raw.length,
3137
- contentSha256: createHash5("sha256").update(raw).digest("hex"),
5046
+ contentSha256: createHash7("sha256").update(raw).digest("hex"),
3138
5047
  firstRow,
3139
5048
  gz
3140
5049
  };
@@ -3217,9 +5126,9 @@ var init_push = __esm({
3217
5126
  });
3218
5127
 
3219
5128
  // src/worlds/ingest.ts
3220
- import * as fs8 from "fs";
5129
+ import * as fs10 from "fs";
3221
5130
  import * as os3 from "os";
3222
- import * as path7 from "path";
5131
+ import * as path9 from "path";
3223
5132
  function parseToolCallRecord(raw) {
3224
5133
  const none = { record: null, skip: null };
3225
5134
  if (!isObject2(raw)) return { ...none, refusal: "record is not an object" };
@@ -3240,7 +5149,7 @@ function parseToolCallRecord(raw) {
3240
5149
  }
3241
5150
  function readToolCallRecords(paths) {
3242
5151
  const records = [];
3243
- for (const file of paths) records.push(...toolCallRecordsIn(fs8.readFileSync(file, "utf8"), file));
5152
+ for (const file of paths) records.push(...toolCallRecordsIn(fs10.readFileSync(file, "utf8"), file));
3244
5153
  return records;
3245
5154
  }
3246
5155
  function toolCallRecordsIn(text2, where) {
@@ -3276,7 +5185,7 @@ async function emit(sink, record) {
3276
5185
  return;
3277
5186
  }
3278
5187
  if (typeof sink === "string") {
3279
- fs8.appendFileSync(sink, JSON.stringify(record) + "\n");
5188
+ fs10.appendFileSync(sink, JSON.stringify(record) + "\n");
3280
5189
  return;
3281
5190
  }
3282
5191
  if (typeof sink === "function") {
@@ -3307,14 +5216,6 @@ function captureToolCalls(tools, sink, opts = {}) {
3307
5216
  }
3308
5217
  return wrapped;
3309
5218
  }
3310
- function jsonish(value) {
3311
- if (typeof value !== "string") return value;
3312
- try {
3313
- return JSON.parse(value);
3314
- } catch {
3315
- return value;
3316
- }
3317
- }
3318
5219
  function toolCallsFromObservations(observations, opts = {}) {
3319
5220
  const known = opts.tools ? new Set(opts.tools) : null;
3320
5221
  const sinceMs = opts.since ? Date.parse(opts.since) : NaN;
@@ -3341,10 +5242,9 @@ function toolCallsFromObservations(observations, opts = {}) {
3341
5242
  out.unknownTools[tool] = (out.unknownTools[tool] ?? 0) + 1;
3342
5243
  continue;
3343
5244
  }
3344
- const input = jsonish(observation.input);
3345
- const record = { tool, args: isObject2(input) ? input : {} };
5245
+ const record = { tool, args: isObject2(observation.input) ? observation.input : {} };
3346
5246
  if (observation.level === "ERROR") record.error = observation.statusMessage || "ERROR";
3347
- else record.result = jsonish(observation.output);
5247
+ else record.result = observation.output;
3348
5248
  if (at !== void 0) record.at = at;
3349
5249
  if (opts.session) record.session = opts.session;
3350
5250
  record.source = `trace:${observation.traceId ?? "?"}/${observation.id ?? "?"}`;
@@ -3365,7 +5265,7 @@ async function ingest(world, records, options = {}) {
3365
5265
  const mode = options.mode ?? "append";
3366
5266
  const dryRun = Boolean(options.dryRun);
3367
5267
  const creds = credentialsOf(options);
3368
- const local = fs8.existsSync(world) && fs8.statSync(world).isDirectory();
5268
+ const local = fs10.existsSync(world) && fs10.statSync(world).isDirectory();
3369
5269
  const map = options.map === void 0 ? void 0 : options.map;
3370
5270
  let rows = null;
3371
5271
  let stderr = "";
@@ -3381,9 +5281,9 @@ async function ingest(world, records, options = {}) {
3381
5281
  if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
3382
5282
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
3383
5283
  const slug = world.split("@", 1)[0];
3384
- const scratch = fs8.mkdtempSync(path7.join(os3.tmpdir(), "gateway-ingest-"));
5284
+ const scratch = fs10.mkdtempSync(path9.join(os3.tmpdir(), "gateway-ingest-"));
3385
5285
  try {
3386
- const pulled = await pull2(world, path7.join(scratch, slug), options);
5286
+ const pulled = await pull2(world, path9.join(scratch, slug), options);
3387
5287
  let report;
3388
5288
  let projected;
3389
5289
  if (rows === null) {
@@ -3404,7 +5304,7 @@ async function ingest(world, records, options = {}) {
3404
5304
  if (status.entityCounts) report.entityCounts = status.entityCounts;
3405
5305
  return report;
3406
5306
  } finally {
3407
- fs8.rmSync(scratch, { recursive: true, force: true });
5307
+ fs10.rmSync(scratch, { recursive: true, force: true });
3408
5308
  }
3409
5309
  }
3410
5310
  async function ingestToolCalls(world, records, options = {}) {
@@ -3453,8 +5353,8 @@ async function sessionToolObservations(sessionId, options = {}) {
3453
5353
  `/api/public/sessions/${encodeURIComponent(sessionId)}?includeIO=true`
3454
5354
  );
3455
5355
  const out = [];
3456
- for (const trace of session.traces ?? [])
3457
- for (const observation of trace.observations ?? []) out.push({ traceId: trace.id ?? null, ...observation });
5356
+ for (const trace3 of session.traces ?? [])
5357
+ for (const observation of trace3.observations ?? []) out.push({ traceId: trace3.id ?? null, ...observation });
3458
5358
  return out;
3459
5359
  }
3460
5360
  var isObject2, isoOf;
@@ -3478,9 +5378,12 @@ __export(worlds_exports, {
3478
5378
  EVENT_BATCH_SIZE: () => EVENT_BATCH_SIZE,
3479
5379
  EVENT_FLUSH_INTERVAL_MS: () => EVENT_FLUSH_INTERVAL_MS,
3480
5380
  EVENT_MAX_RETRIES: () => EVENT_MAX_RETRIES,
5381
+ GatewayEvalsClient: () => GatewayEvalsClient,
3481
5382
  STRIP_PREFIX: () => STRIP_PREFIX,
3482
5383
  SURFACE_KINDS: () => SURFACE_KINDS,
3483
5384
  TRANSFORMS: () => TRANSFORMS,
5385
+ TaskRun: () => TaskRun,
5386
+ TaskSessions: () => TaskSessions,
3484
5387
  WHITESPACE: () => WHITESPACE,
3485
5388
  WorldBrowser: () => WorldBrowser,
3486
5389
  WorldSession: () => WorldSession,
@@ -3489,9 +5392,12 @@ __export(worlds_exports, {
3489
5392
  WorldSet: () => WorldSet,
3490
5393
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
3491
5394
  WorldTask: () => WorldTask,
5395
+ aggregateTaskGrade: () => aggregateTaskGrade,
3492
5396
  apiRequest: () => apiRequest,
3493
5397
  applyTransforms: () => applyTransforms,
5398
+ attachManifest: () => attachManifest,
3494
5399
  attachSession: () => attachSession,
5400
+ attachTask: () => attachTask,
3495
5401
  buildSourceTarball: () => buildSourceTarball,
3496
5402
  captureToolCalls: () => captureToolCalls,
3497
5403
  collectArchiveRelPaths: () => collectArchiveRelPaths,
@@ -3508,17 +5414,20 @@ __export(worlds_exports, {
3508
5414
  hashForm: () => hashForm,
3509
5415
  ingest: () => ingest,
3510
5416
  ingestToolCalls: () => ingestToolCalls,
3511
- isIncludedInContentHash: () => isIncludedInContentHash,
3512
5417
  isTaskIdRef: () => isTaskIdRef,
5418
+ isTaskManifest: () => isTaskManifest,
3513
5419
  isTaskSpec: () => isTaskSpec,
3514
5420
  listEnvSecretKeys: () => listEnvSecretKeys,
3515
5421
  listRunConfigs: () => listRunConfigs,
5422
+ listTaskSessions: () => listTaskSessions,
3516
5423
  listTasks: () => listTasks,
3517
5424
  localTreeState: () => localTreeState,
3518
5425
  migrateProjectWorlds: () => migrateProjectWorlds,
3519
5426
  migrateWorld: () => migrateWorld,
5427
+ modelToolName: () => modelToolName,
3520
5428
  openBrowser: () => openBrowser,
3521
5429
  openSession: () => openSession,
5430
+ openTask: () => openTask,
3522
5431
  openWorld: () => openWorld,
3523
5432
  parseToolCallRecord: () => parseToolCallRecord,
3524
5433
  parseTransform: () => parseTransform,
@@ -3527,6 +5436,7 @@ __export(worlds_exports, {
3527
5436
  propose: () => propose,
3528
5437
  pull: () => pull,
3529
5438
  push: () => push,
5439
+ pushSlug: () => pushSlug,
3530
5440
  pyText: () => pyText,
3531
5441
  queryRows: () => queryRows,
3532
5442
  queryRowsSearch: () => queryRowsSearch,
@@ -3537,6 +5447,7 @@ __export(worlds_exports, {
3537
5447
  resolveBrowserMode: () => resolveBrowserMode,
3538
5448
  roundForm: () => roundForm,
3539
5449
  runSessions: () => runSessions,
5450
+ runTask: () => runTask,
3540
5451
  schemaFromSignature: () => schemaFromSignature,
3541
5452
  sessionToolObservations: () => sessionToolObservations,
3542
5453
  setEnvSecret: () => setEnvSecret,
@@ -3568,9 +5479,9 @@ function resolveEnv(options) {
3568
5479
  }
3569
5480
  return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
3570
5481
  }
3571
- async function apiRequest(options, method, path8, body) {
5482
+ async function apiRequest(options, method, path10, body) {
3572
5483
  const { host, publicKey, secretKey } = resolveEnv(options);
3573
- const res = await fetch(`${host}${path8}`, {
5484
+ const res = await fetch(`${host}${path10}`, {
3574
5485
  method,
3575
5486
  headers: {
3576
5487
  "content-type": "application/json",
@@ -3579,7 +5490,7 @@ async function apiRequest(options, method, path8, body) {
3579
5490
  ...body === void 0 ? {} : { body: JSON.stringify(body) }
3580
5491
  });
3581
5492
  const text2 = await res.text();
3582
- if (!res.ok) throw new ApiError2(res.status, method, path8, text2.slice(0, 400));
5493
+ if (!res.ok) throw new ApiError2(res.status, method, path10, text2.slice(0, 400));
3583
5494
  return JSON.parse(text2);
3584
5495
  }
3585
5496
  async function setEnvSecret(slug, key, value, options = {}) {
@@ -3736,6 +5647,9 @@ var init_worlds = __esm({
3736
5647
  "src/worlds/index.ts"() {
3737
5648
  init_session();
3738
5649
  init_tasks();
5650
+ init_taskSessions();
5651
+ init_taskRun();
5652
+ init_evals();
3739
5653
  init_browser();
3740
5654
  init_hub();
3741
5655
  init_validate();
@@ -3746,8 +5660,8 @@ var init_worlds = __esm({
3746
5660
  init_migrate();
3747
5661
  init_ingest();
3748
5662
  ApiError2 = class extends Error {
3749
- constructor(status, method, path8, body) {
3750
- super(`${method} ${path8} -> ${status}: ${body}`);
5663
+ constructor(status, method, path10, body) {
5664
+ super(`${method} ${path10} -> ${status}: ${body}`);
3751
5665
  this.status = status;
3752
5666
  this.name = "ApiError";
3753
5667
  }
@@ -3826,9 +5740,12 @@ export {
3826
5740
  EVENT_BATCH_SIZE,
3827
5741
  EVENT_FLUSH_INTERVAL_MS,
3828
5742
  EVENT_MAX_RETRIES,
5743
+ GatewayEvalsClient,
3829
5744
  STRIP_PREFIX,
3830
5745
  SURFACE_KINDS,
3831
5746
  TRANSFORMS,
5747
+ TaskRun,
5748
+ TaskSessions,
3832
5749
  WHITESPACE,
3833
5750
  WorldBrowser,
3834
5751
  WorldSession,
@@ -3837,9 +5754,12 @@ export {
3837
5754
  WorldSet,
3838
5755
  WorldSurfaceUnavailable,
3839
5756
  WorldTask,
5757
+ aggregateTaskGrade,
3840
5758
  apiRequest,
3841
5759
  applyTransforms,
5760
+ attachManifest,
3842
5761
  attachSession,
5762
+ attachTask,
3843
5763
  buildSourceTarball,
3844
5764
  captureToolCalls,
3845
5765
  collectArchiveRelPaths,
@@ -3856,17 +5776,20 @@ export {
3856
5776
  hashForm,
3857
5777
  ingest,
3858
5778
  ingestToolCalls,
3859
- isIncludedInContentHash,
3860
5779
  isTaskIdRef,
5780
+ isTaskManifest,
3861
5781
  isTaskSpec,
3862
5782
  listEnvSecretKeys,
3863
5783
  listRunConfigs,
5784
+ listTaskSessions,
3864
5785
  listTasks,
3865
5786
  localTreeState,
3866
5787
  migrateProjectWorlds,
3867
5788
  migrateWorld,
5789
+ modelToolName,
3868
5790
  openBrowser,
3869
5791
  openSession,
5792
+ openTask,
3870
5793
  openWorld,
3871
5794
  parseToolCallRecord,
3872
5795
  parseTransform,
@@ -3875,6 +5798,7 @@ export {
3875
5798
  propose,
3876
5799
  pull,
3877
5800
  push,
5801
+ pushSlug,
3878
5802
  pyText,
3879
5803
  queryRows,
3880
5804
  queryRowsSearch,
@@ -3885,6 +5809,7 @@ export {
3885
5809
  resolveBrowserMode,
3886
5810
  roundForm,
3887
5811
  runSessions,
5812
+ runTask,
3888
5813
  schemaFromSignature,
3889
5814
  sessionToolObservations,
3890
5815
  setEnvSecret,