@granular-software/sdk 0.4.36 → 0.4.37

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.
@@ -3931,11 +3931,22 @@ var TOKEN_REFRESH_LEEWAY_MS = 2 * 60 * 1e3;
3931
3931
  var TOKEN_REFRESH_RETRY_MS = 30 * 1e3;
3932
3932
  var MAX_TIMER_DELAY_MS = 2147483647;
3933
3933
  var DEBUG_WS = process.env.GRANULAR_DEBUG_WS === "1";
3934
+ var DEFAULT_RPC_TIMEOUT_MS = 3e4;
3935
+ var DOMAIN_PACKAGE_RPC_TIMEOUT_MS = 12e4;
3934
3936
  function debugWs(...args) {
3935
3937
  if (DEBUG_WS) {
3936
3938
  console.log(...args);
3937
3939
  }
3938
3940
  }
3941
+ function rpcTimeoutMsForMethod(method) {
3942
+ switch (method) {
3943
+ case "domain.fetchPackagePart":
3944
+ case "domain.getSummary":
3945
+ return DOMAIN_PACKAGE_RPC_TIMEOUT_MS;
3946
+ default:
3947
+ return DEFAULT_RPC_TIMEOUT_MS;
3948
+ }
3949
+ }
3939
3950
  var WSClient = class {
3940
3951
  ws = null;
3941
3952
  url;
@@ -4360,13 +4371,14 @@ var WSClient = class {
4360
4371
  return new Promise((resolve, reject) => {
4361
4372
  this.messageQueue.push({ resolve, reject, id });
4362
4373
  this.ws.send(JSON.stringify(request));
4374
+ const timeoutMs = rpcTimeoutMsForMethod(method);
4363
4375
  setTimeout(() => {
4364
4376
  const pending = this.messageQueue.find((q) => q.id === id);
4365
4377
  if (pending) {
4366
4378
  this.messageQueue = this.messageQueue.filter((q) => q.id !== id);
4367
4379
  reject(new Error(`RPC timeout: ${method}`));
4368
4380
  }
4369
- }, 3e4);
4381
+ }, timeoutMs);
4370
4382
  });
4371
4383
  }
4372
4384
  async handleIncomingRpc(request) {
@@ -4480,10 +4492,48 @@ function normalizePromptText(value) {
4480
4492
  function extractPromptTokens(value) {
4481
4493
  return normalizePromptText(value).split(/\s+/).map((token) => token.trim()).filter((token) => token.length > 0);
4482
4494
  }
4495
+ function parseJsonPromptChoiceOption(option) {
4496
+ const trimmed = option.trim();
4497
+ if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) return null;
4498
+ try {
4499
+ const parsed = JSON.parse(trimmed);
4500
+ return asRecord(parsed);
4501
+ } catch {
4502
+ return null;
4503
+ }
4504
+ }
4505
+ function normalizePromptChoiceOption(option) {
4506
+ if (typeof option === "string") {
4507
+ const record2 = parseJsonPromptChoiceOption(option);
4508
+ if (!record2) {
4509
+ return { value: option, label: option };
4510
+ }
4511
+ const value2 = typeof record2.value === "string" ? record2.value : typeof record2.id === "string" ? record2.id : typeof record2.label === "string" ? record2.label : JSON.stringify(record2);
4512
+ return {
4513
+ value: value2,
4514
+ label: typeof record2.label === "string" ? record2.label : value2,
4515
+ description: typeof record2.description === "string" ? record2.description : void 0
4516
+ };
4517
+ }
4518
+ const record = option;
4519
+ if (!record) {
4520
+ return { value: "", label: "" };
4521
+ }
4522
+ const nestedJson = (typeof record.value === "string" ? parseJsonPromptChoiceOption(record.value) : null) || (typeof record.label === "string" ? parseJsonPromptChoiceOption(record.label) : null);
4523
+ if (nestedJson) {
4524
+ return normalizePromptChoiceOption(nestedJson);
4525
+ }
4526
+ const value = typeof record.value === "string" ? record.value : typeof record.label === "string" ? record.label : JSON.stringify(record);
4527
+ return {
4528
+ value,
4529
+ label: typeof record.label === "string" ? record.label : value,
4530
+ description: typeof record.description === "string" ? record.description : void 0
4531
+ };
4532
+ }
4483
4533
  function scorePromptChoiceMatch(answer, answerTokens, option) {
4484
- const value = typeof option === "string" ? option : typeof option?.value === "string" ? option.value : "";
4485
- const label = typeof option === "string" ? option : typeof option?.label === "string" ? option.label : "";
4486
- const description = typeof option === "string" ? "" : typeof option?.description === "string" ? option.description : "";
4534
+ const choice = normalizePromptChoiceOption(option);
4535
+ const { value, label } = choice;
4536
+ const description = choice.description || "";
4487
4537
  const haystack = normalizePromptText([value, label, description].filter(Boolean).join(" "));
4488
4538
  if (!haystack) return { score: 0, resolvedValue: value || label || null };
4489
4539
  let score = 0;
@@ -4519,7 +4569,9 @@ function normalizePrompt(rawValue) {
4519
4569
  type: normalizePromptType(source === raw ? raw : { ...raw, ...source }),
4520
4570
  title: typeof source.title === "string" ? source.title : "Input required",
4521
4571
  message: typeof source.message === "string" ? source.message : "",
4522
- options: Array.isArray(source.options) ? source.options : void 0,
4572
+ options: Array.isArray(source.options) ? source.options.map(
4573
+ (option) => typeof option === "string" || asRecord(option) ? normalizePromptChoiceOption(option) : option
4574
+ ) : void 0,
4523
4575
  defaultValue: source.defaultValue,
4524
4576
  placeholder: typeof source.placeholder === "string" ? source.placeholder : void 0,
4525
4577
  allowEmpty: typeof source.allowEmpty === "boolean" ? source.allowEmpty : void 0,
@@ -4547,6 +4599,22 @@ function resolvePromptAnswer(prompt, answer) {
4547
4599
  }
4548
4600
 
4549
4601
  // src/session.ts
4602
+ var PROMPT_TRANSCRIPT_APPEND_TIMEOUT_MS = 5e3;
4603
+ function withPromptTranscriptTimeout(promise) {
4604
+ let timeout = null;
4605
+ return Promise.race([
4606
+ promise,
4607
+ new Promise((_, reject) => {
4608
+ timeout = setTimeout(() => {
4609
+ reject(new Error("Timed out appending prompt answer transcript."));
4610
+ }, PROMPT_TRANSCRIPT_APPEND_TIMEOUT_MS);
4611
+ })
4612
+ ]).finally(() => {
4613
+ if (timeout) {
4614
+ clearTimeout(timeout);
4615
+ }
4616
+ });
4617
+ }
4550
4618
  var Session = class {
4551
4619
  client;
4552
4620
  clientId;
@@ -4563,6 +4631,8 @@ var Session = class {
4563
4631
  lastKnownTools = /* @__PURE__ */ new Map();
4564
4632
  /** Last seen live prompts, keyed by prompt id, for answer normalization */
4565
4633
  promptCache = /* @__PURE__ */ new Map();
4634
+ /** Prompt ids locally answered before the document sync catches up. */
4635
+ hiddenPromptIds = /* @__PURE__ */ new Set();
4566
4636
  constructor(client, clientId) {
4567
4637
  this.client = client;
4568
4638
  this.clientId = clientId || `client_${Date.now()}`;
@@ -4698,8 +4768,9 @@ var Session = class {
4698
4768
  * `effect.invoke` RPC back to the sandbox effect host, where the registered handlers
4699
4769
  * execute locally and return the result to the sandbox.
4700
4770
  */
4701
- async submitJob(code, domainRevision) {
4702
- let revision = domainRevision || this.currentDomainRevision || this.extractDomainRevisionFromDoc(this.client.doc) || void 0;
4771
+ async submitJob(code, domainRevisionOrOptions) {
4772
+ const options = typeof domainRevisionOrOptions === "string" ? { domainRevision: domainRevisionOrOptions } : domainRevisionOrOptions || {};
4773
+ let revision = options.domainRevision || this.currentDomainRevision || this.extractDomainRevisionFromDoc(this.client.doc) || void 0;
4703
4774
  if (!revision) {
4704
4775
  try {
4705
4776
  const summary = await this.getDomain();
@@ -4714,7 +4785,9 @@ var Session = class {
4714
4785
  }
4715
4786
  const result = await this.client.call("job.submit", {
4716
4787
  domainRevision: revision,
4717
- code
4788
+ code,
4789
+ metadata: options.metadata,
4790
+ agent: options.agent
4718
4791
  });
4719
4792
  if (!result.jobId) {
4720
4793
  throw new Error("Failed to submit job: no jobId returned");
@@ -4755,25 +4828,39 @@ var Session = class {
4755
4828
  const prompt = this.promptCache.get(promptId);
4756
4829
  const resolvedAnswer = resolvePromptAnswer(prompt, answer);
4757
4830
  this.promptCache.delete(promptId);
4758
- await this.client.call("prompt.answer", {
4759
- promptId,
4760
- answer: resolvedAnswer,
4761
- value: resolvedAnswer
4762
- });
4831
+ this.hiddenPromptIds.add(promptId);
4832
+ try {
4833
+ await this.client.call("prompt.answer", {
4834
+ promptId,
4835
+ answer: resolvedAnswer,
4836
+ value: resolvedAnswer
4837
+ });
4838
+ } catch (error) {
4839
+ this.hiddenPromptIds.delete(promptId);
4840
+ if (prompt) {
4841
+ this.promptCache.set(promptId, prompt);
4842
+ }
4843
+ throw error;
4844
+ }
4763
4845
  try {
4764
4846
  const content = this.stringifyConversationValue(resolvedAnswer);
4765
4847
  if (content.trim()) {
4766
- await this.appendConversationMessage({
4767
- role: "user",
4768
- content,
4769
- promptId
4770
- });
4848
+ await withPromptTranscriptTimeout(
4849
+ this.appendConversationMessage({
4850
+ role: "user",
4851
+ content,
4852
+ promptId
4853
+ })
4854
+ );
4771
4855
  }
4772
4856
  } catch {
4773
4857
  }
4774
4858
  }
4775
4859
  async appendConversationMessage(input) {
4776
- return this.client.call("conversation.append", input);
4860
+ return this.client.call(
4861
+ "conversation.append",
4862
+ input
4863
+ );
4777
4864
  }
4778
4865
  /**
4779
4866
  * Get the current list of available effects.
@@ -4782,9 +4869,53 @@ var Session = class {
4782
4869
  getEffects() {
4783
4870
  const doc = this.client.doc;
4784
4871
  const toolMap = /* @__PURE__ */ new Map();
4785
- const domainPkg = doc.domain?.packages?.domain;
4786
- if (domainPkg?.tools && Array.isArray(domainPkg.tools)) {
4787
- for (const tool of domainPkg.tools) {
4872
+ const domainPackages = doc.domain?.packages;
4873
+ const packageCandidates = domainPackages && typeof domainPackages === "object" ? [
4874
+ domainPackages.domain,
4875
+ domainPackages["@sandbox/domain"],
4876
+ ...Object.values(domainPackages)
4877
+ ].filter(Boolean) : [];
4878
+ for (const domainPkg of packageCandidates) {
4879
+ if (domainPkg?.tools && Array.isArray(domainPkg.tools)) {
4880
+ for (const tool of domainPkg.tools) {
4881
+ if (!tool?.name || toolMap.has(tool.name)) continue;
4882
+ toolMap.set(tool.name, {
4883
+ name: tool.name,
4884
+ description: tool.description,
4885
+ inputSchema: tool.inputSchema,
4886
+ outputSchema: tool.outputSchema,
4887
+ className: tool.className || void 0,
4888
+ static: tool.static || false,
4889
+ ready: false,
4890
+ publishedAt: void 0
4891
+ });
4892
+ }
4893
+ }
4894
+ if (!domainPkg?.classes || typeof domainPkg.classes !== "object") {
4895
+ continue;
4896
+ }
4897
+ for (const [className, classDef] of Object.entries(
4898
+ domainPkg.classes
4899
+ )) {
4900
+ const methods = Array.isArray(classDef?.methods) ? classDef.methods : [];
4901
+ for (const method of methods) {
4902
+ if (!method?.name || toolMap.has(method.name)) continue;
4903
+ toolMap.set(method.name, {
4904
+ name: method.name,
4905
+ description: method.description,
4906
+ inputSchema: method.inputSchema,
4907
+ outputSchema: method.outputSchema,
4908
+ className: method.className || classDef?.name || className,
4909
+ static: method.static || false,
4910
+ ready: false,
4911
+ publishedAt: void 0
4912
+ });
4913
+ }
4914
+ }
4915
+ }
4916
+ const legacyDomainPkg = doc.domain?.packages?.domain;
4917
+ if (legacyDomainPkg?.tools && Array.isArray(legacyDomainPkg.tools)) {
4918
+ for (const tool of legacyDomainPkg.tools) {
4788
4919
  if (!tool?.name) continue;
4789
4920
  toolMap.set(tool.name, {
4790
4921
  name: tool.name,
@@ -4798,6 +4929,27 @@ var Session = class {
4798
4929
  });
4799
4930
  }
4800
4931
  }
4932
+ if (legacyDomainPkg?.classes && typeof legacyDomainPkg.classes === "object") {
4933
+ for (const [className, classDef] of Object.entries(
4934
+ legacyDomainPkg.classes
4935
+ )) {
4936
+ const methods = Array.isArray(classDef?.methods) ? classDef.methods : [];
4937
+ for (const method of methods) {
4938
+ if (!method?.name || toolMap.has(method.name)) continue;
4939
+ toolMap.set(method.name, {
4940
+ name: method.name,
4941
+ description: method.description,
4942
+ inputSchema: method.inputSchema,
4943
+ outputSchema: method.outputSchema,
4944
+ className: method.className || classDef?.name || className,
4945
+ static: method.static || false,
4946
+ ready: false,
4947
+ publishedAt: void 0
4948
+ });
4949
+ }
4950
+ }
4951
+ }
4952
+ const hasPolicyFilteredDomainTools = toolMap.size > 0;
4801
4953
  const catalogs = doc.catalog?.rawToolCatalogs || {};
4802
4954
  for (const [clientId, catalog] of Object.entries(catalogs)) {
4803
4955
  const cat = catalog;
@@ -4805,6 +4957,7 @@ var Session = class {
4805
4957
  for (const tool of cat.tools) {
4806
4958
  if (!tool?.name) continue;
4807
4959
  const existing = toolMap.get(tool.name);
4960
+ if (hasPolicyFilteredDomainTools && !existing) continue;
4808
4961
  if (existing?.publishedAt && cat.publishedAt && existing.publishedAt > cat.publishedAt)
4809
4962
  continue;
4810
4963
  const isLocal = clientId === this.clientId;
@@ -4824,6 +4977,24 @@ var Session = class {
4824
4977
  }
4825
4978
  return Array.from(toolMap.values());
4826
4979
  }
4980
+ /**
4981
+ * Return the currently open prompt payloads known to this session.
4982
+ *
4983
+ * These come from live `prompt` / `prompt.request` websocket events and
4984
+ * preserve the exact shape used by `answerPrompt(...)`.
4985
+ */
4986
+ getPrompts() {
4987
+ return Array.from(this.promptCache.values()).map((prompt) => ({
4988
+ ...prompt,
4989
+ options: Array.isArray(prompt.options) ? prompt.options.map(
4990
+ (option) => typeof option === "string" ? option : { ...option }
4991
+ ) : void 0,
4992
+ metadata: prompt.metadata ? { ...prompt.metadata } : void 0
4993
+ }));
4994
+ }
4995
+ getHiddenPromptIds() {
4996
+ return Array.from(this.hiddenPromptIds);
4997
+ }
4827
4998
  /**
4828
4999
  * Backwards-compatible alias for `getEffects()`.
4829
5000
  */
@@ -4923,11 +5094,7 @@ var Session = class {
4923
5094
  if (!normalizedDocs) {
4924
5095
  return normalizedTypes;
4925
5096
  }
4926
- return [
4927
- normalizedTypes,
4928
- "Generated usage notes from ./sandbox-tools docs:",
4929
- normalizedDocs
4930
- ].join("\n\n");
5097
+ return [normalizedTypes, "[Docs]", normalizedDocs].join("\n\n");
4931
5098
  }
4932
5099
  if (normalizedDocs) {
4933
5100
  return normalizedDocs;
@@ -5149,6 +5316,7 @@ import { ${allImports} } from "./sandbox-tools";
5149
5316
  const emitPrompt = (payload) => {
5150
5317
  const prompt = normalizePrompt(payload);
5151
5318
  if (!prompt) return;
5319
+ this.hiddenPromptIds.delete(prompt.id);
5152
5320
  this.promptCache.set(prompt.id, prompt);
5153
5321
  this.emit("prompt", prompt);
5154
5322
  };
@@ -5331,6 +5499,7 @@ var JobImplementation = class {
5331
5499
  eventListeners = /* @__PURE__ */ new Map();
5332
5500
  bufferedAgentMessages = [];
5333
5501
  bufferedAgentMessageIds = /* @__PURE__ */ new Set();
5502
+ resultSettled = false;
5334
5503
  metadata;
5335
5504
  constructor(id, client, initialState) {
5336
5505
  this.id = id;
@@ -5355,7 +5524,9 @@ var JobImplementation = class {
5355
5524
  if (execData.error) {
5356
5525
  this.finalize("failed", void 0, execData.error);
5357
5526
  } else {
5358
- this.finalize("succeeded", execData.result);
5527
+ this.finalize("succeeded", execData.result, void 0, {
5528
+ hasResult: Object.prototype.hasOwnProperty.call(execData, "result")
5529
+ });
5359
5530
  }
5360
5531
  this.emit("status", this.status);
5361
5532
  }
@@ -5391,9 +5562,6 @@ var JobImplementation = class {
5391
5562
  if (normalizedStatus === "failed" || normalizedStatus === "timeout" || normalizedStatus === "canceled") {
5392
5563
  this.finalize(normalizedStatus);
5393
5564
  }
5394
- if (normalizedStatus === "succeeded") {
5395
- this.finalize("succeeded");
5396
- }
5397
5565
  this.emit("status", normalizedStatus);
5398
5566
  });
5399
5567
  this.client.on(`job.${id}.stdout`, (line) => {
@@ -5413,7 +5581,7 @@ var JobImplementation = class {
5413
5581
  this.emit("stderr", line);
5414
5582
  });
5415
5583
  this.client.on(`job.${id}.result`, (result) => {
5416
- this.finalize("succeeded", result);
5584
+ this.finalize("succeeded", result, void 0, { hasResult: true });
5417
5585
  });
5418
5586
  this.client.on(`job.${id}.error`, (error) => {
5419
5587
  this.finalize("failed", void 0, error);
@@ -5434,7 +5602,9 @@ var JobImplementation = class {
5434
5602
  this.client.on("job.completed", (data) => {
5435
5603
  const jobData = data;
5436
5604
  if (jobData.jobId === id) {
5437
- this.finalize("succeeded", jobData.result);
5605
+ this.finalize("succeeded", jobData.result, void 0, {
5606
+ hasResult: true
5607
+ });
5438
5608
  this.emit("status", this.status);
5439
5609
  }
5440
5610
  });
@@ -5559,7 +5729,7 @@ var JobImplementation = class {
5559
5729
  this.metadata.status = "running";
5560
5730
  }
5561
5731
  }
5562
- finalize(status, result, error) {
5732
+ finalize(status, result, error, options = {}) {
5563
5733
  if (!this.metadata.startedAt) {
5564
5734
  this.metadata.startedAt = Date.now();
5565
5735
  }
@@ -5567,14 +5737,18 @@ var JobImplementation = class {
5567
5737
  this.metadata.status = status;
5568
5738
  this.metadata.completedAt = this.metadata.completedAt || Date.now();
5569
5739
  this.metadata.durationMs = this.metadata.completedAt - this.metadata.startedAt;
5570
- if (result !== void 0) {
5740
+ if (!this.resultSettled && (options.hasResult || result !== void 0)) {
5571
5741
  this.metadata.result = sanitizeFeedbackValue(result);
5742
+ this.resultSettled = true;
5572
5743
  this._resolveResult(result);
5573
5744
  }
5574
- if (error !== void 0) {
5575
- const message = error instanceof Error ? error.message : String(error);
5745
+ if (!this.resultSettled && (error !== void 0 || status === "failed" || status === "timeout" || status === "canceled")) {
5746
+ const fallbackError = new Error(`Job ${this.id} ${status}.`);
5747
+ const cause = error ?? fallbackError;
5748
+ const message = cause instanceof Error ? cause.message : String(cause);
5576
5749
  this.metadata.error = truncateFeedbackString(message);
5577
- this._rejectResult(error);
5750
+ this.resultSettled = true;
5751
+ this._rejectResult(cause);
5578
5752
  }
5579
5753
  }
5580
5754
  upsertToolCall(next) {
@@ -5657,6 +5831,17 @@ function humanTextFromStdout(stdout) {
5657
5831
  }
5658
5832
  return null;
5659
5833
  }
5834
+ function responseTextFromAgentMessages(agentMessages) {
5835
+ for (const message of [...agentMessages].reverse()) {
5836
+ const record = asRecord2(message);
5837
+ if (!record) continue;
5838
+ for (const key of RESPONSE_KEYS) {
5839
+ const normalized = normalizeText(record[key]);
5840
+ if (normalized) return normalized;
5841
+ }
5842
+ }
5843
+ return null;
5844
+ }
5660
5845
  function pushString(target, value) {
5661
5846
  if (typeof value === "string" && value.trim()) {
5662
5847
  target.add(value.trim());
@@ -5682,6 +5867,41 @@ function collectReferencesFromRecord(record, refs) {
5682
5867
  for (const key of VARIABLE_ARRAY_KEY_CANDIDATES)
5683
5868
  pushStringArray(refs.variableNames, record[key]);
5684
5869
  }
5870
+ function stringValue(record, keys) {
5871
+ for (const key of keys) {
5872
+ const value = record[key];
5873
+ if (typeof value === "string" && value.trim()) {
5874
+ return value.trim();
5875
+ }
5876
+ }
5877
+ return null;
5878
+ }
5879
+ function findEntryPathForRecord(record, heap) {
5880
+ const directPath = stringValue(record, ["entryPath", "path"]);
5881
+ if (directPath && heap.entriesByPath?.[directPath]) {
5882
+ return directPath;
5883
+ }
5884
+ const id = stringValue(record, ["id", "_id", "recordId", "objectId"]);
5885
+ if (!id) {
5886
+ return null;
5887
+ }
5888
+ const className = stringValue(record, [
5889
+ "className",
5890
+ "_className",
5891
+ "__className",
5892
+ "prototype",
5893
+ "type"
5894
+ ]);
5895
+ const entries = Object.values(heap.entriesByPath || {});
5896
+ const exact = entries.find(
5897
+ (entry) => entry.id === id && (!className || entry.className === className || entry.prototypes?.includes(className))
5898
+ );
5899
+ if (exact?.path) {
5900
+ return exact.path;
5901
+ }
5902
+ const idOnlyMatches = entries.filter((entry) => entry.id === id);
5903
+ return idOnlyMatches.length === 1 ? idOnlyMatches[0].path : null;
5904
+ }
5685
5905
  function scanForHeapReferences(value, heap, refs, depth = 0, seen = /* @__PURE__ */ new Set()) {
5686
5906
  if (value === null || value === void 0 || depth > 4 || seen.has(value))
5687
5907
  return;
@@ -5702,6 +5922,8 @@ function scanForHeapReferences(value, heap, refs, depth = 0, seen = /* @__PURE__
5702
5922
  const record = asRecord2(value);
5703
5923
  if (!record) return;
5704
5924
  seen.add(value);
5925
+ const entryPath = findEntryPathForRecord(record, heap);
5926
+ if (entryPath) refs.entryPaths.add(entryPath);
5705
5927
  collectReferencesFromRecord(record, refs);
5706
5928
  for (const key of UI_CONTAINER_KEYS) {
5707
5929
  const nested = asRecord2(record[key]);
@@ -5810,6 +6032,7 @@ function resolveJobPresentation({
5810
6032
  jobId,
5811
6033
  result,
5812
6034
  stdout = [],
6035
+ agentMessages = [],
5813
6036
  sessionHeap,
5814
6037
  allowExplicitArtifacts = true
5815
6038
  }) {
@@ -5842,7 +6065,7 @@ function resolveJobPresentation({
5842
6065
  const hasExplicitArtifacts = allowExplicitArtifacts && (explicitEntries.length > 0 || explicitLists.length > 0);
5843
6066
  const lists = hasExplicitArtifacts ? explicitLists : jobLists;
5844
6067
  const entries = hasExplicitArtifacts ? explicitEntries : changedEntries;
5845
- const responseText = extractResponseText(result, stdout) || fallbackResponseText(entries, lists);
6068
+ const responseText = extractResponseText(result, stdout) || responseTextFromAgentMessages(agentMessages) || fallbackResponseText(entries, lists);
5846
6069
  return {
5847
6070
  responseText,
5848
6071
  entries,
@@ -10318,6 +10541,67 @@ external_exports.object({
10318
10541
  transitions: external_exports.array(StateMachineTransitionSchema),
10319
10542
  finalStates: external_exports.array(external_exports.string()).optional()
10320
10543
  }).strict();
10544
+ var POLICY_OPERATORS = [
10545
+ "eq",
10546
+ "neq",
10547
+ "gt",
10548
+ "gte",
10549
+ "lt",
10550
+ "lte",
10551
+ "contains",
10552
+ "not_contains",
10553
+ "starts_with",
10554
+ "ends_with",
10555
+ "exists"
10556
+ ];
10557
+ var PolicyPredicateSchema = external_exports.object({
10558
+ path: external_exports.union([external_exports.string(), external_exports.array(external_exports.string())]).optional(),
10559
+ field: external_exports.string().optional(),
10560
+ input: external_exports.string().optional(),
10561
+ operator: external_exports.enum([...POLICY_OPERATORS]),
10562
+ stringValue: external_exports.string().optional(),
10563
+ numberValue: external_exports.number().optional(),
10564
+ booleanValue: external_exports.boolean().optional(),
10565
+ value: external_exports.union([external_exports.string(), external_exports.number(), external_exports.boolean(), external_exports.null()]).optional()
10566
+ }).strict();
10567
+ var PolicyStateMachinePredicateSchema = external_exports.object({
10568
+ machine: external_exports.string().min(1),
10569
+ operator: external_exports.enum([...POLICY_OPERATORS]),
10570
+ state: external_exports.string().optional(),
10571
+ stringValue: external_exports.string().optional()
10572
+ }).strict();
10573
+ var PolicyConditionSchema = external_exports.lazy(
10574
+ () => external_exports.object({
10575
+ all: external_exports.array(PolicyConditionSchema).optional(),
10576
+ any: external_exports.array(PolicyConditionSchema).optional(),
10577
+ not: PolicyConditionSchema.optional(),
10578
+ input: PolicyPredicateSchema.optional(),
10579
+ object: PolicyPredicateSchema.optional(),
10580
+ stateMachine: PolicyStateMachinePredicateSchema.optional()
10581
+ }).strict().refine(
10582
+ (data) => [
10583
+ data.all,
10584
+ data.any,
10585
+ data.not,
10586
+ data.input,
10587
+ data.object,
10588
+ data.stateMachine
10589
+ ].filter((value) => value !== void 0).length === 1,
10590
+ {
10591
+ message: "Policy condition must define exactly one of all, any, not, input, object, or stateMachine"
10592
+ }
10593
+ )
10594
+ );
10595
+ var PolicyRuleSchema = external_exports.object({
10596
+ id: external_exports.string().min(1).optional(),
10597
+ reason: external_exports.string().optional(),
10598
+ when: PolicyConditionSchema
10599
+ }).strict();
10600
+ var PoliciesSchema = external_exports.object({
10601
+ allowWhen: external_exports.array(PolicyRuleSchema).optional(),
10602
+ confirmWhen: external_exports.array(PolicyRuleSchema).optional(),
10603
+ denyWhen: external_exports.array(PolicyRuleSchema).optional()
10604
+ }).strict();
10321
10605
  external_exports.object({
10322
10606
  postCondition: external_exports.union([
10323
10607
  external_exports.string(),
@@ -10347,7 +10631,8 @@ external_exports.object({
10347
10631
  reason: external_exports.string().optional(),
10348
10632
  mode: external_exports.string().optional()
10349
10633
  }).strict()
10350
- ]).optional()
10634
+ ]).optional(),
10635
+ policies: PoliciesSchema.optional()
10351
10636
  }).strict();
10352
10637
 
10353
10638
  // ../metamodel-core/src/index.ts
@@ -11379,6 +11664,148 @@ var noteMetamodelPackage = defineMetamodelPackage({
11379
11664
  }
11380
11665
  });
11381
11666
 
11667
+ // ../policy-engine/src/index.ts
11668
+ function isRecord(value) {
11669
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
11670
+ }
11671
+ function normalizePath(value) {
11672
+ if (Array.isArray(value)) {
11673
+ return value.map((part) => String(part)).filter(Boolean);
11674
+ }
11675
+ if (typeof value === "string") {
11676
+ return value.includes(".") ? value.split(".").filter(Boolean) : [value];
11677
+ }
11678
+ return [];
11679
+ }
11680
+ function firstDefinedValue(spec) {
11681
+ if ("value" in spec) return spec.value;
11682
+ if ("stringValue" in spec) return spec.stringValue;
11683
+ if ("numberValue" in spec) return spec.numberValue;
11684
+ if ("booleanValue" in spec) return spec.booleanValue;
11685
+ if ("state" in spec) return spec.state;
11686
+ return void 0;
11687
+ }
11688
+ function normalizeCondition(input) {
11689
+ if (input === void 0 || input === null) return { kind: "always" };
11690
+ if (!isRecord(input)) {
11691
+ throw new Error("Policy condition must be an object");
11692
+ }
11693
+ if (Array.isArray(input.all)) {
11694
+ return {
11695
+ kind: "all",
11696
+ conditions: input.all.map((item) => normalizeCondition(item))
11697
+ };
11698
+ }
11699
+ if (Array.isArray(input.any)) {
11700
+ return {
11701
+ kind: "any",
11702
+ conditions: input.any.map((item) => normalizeCondition(item))
11703
+ };
11704
+ }
11705
+ if (input.not !== void 0) {
11706
+ return { kind: "not", condition: normalizeCondition(input.not) };
11707
+ }
11708
+ for (const source of ["input", "object", "stateMachine"]) {
11709
+ const raw = input[source];
11710
+ if (!isRecord(raw)) continue;
11711
+ const operator = raw.operator;
11712
+ if (operator !== "eq" && operator !== "neq" && operator !== "gt" && operator !== "gte" && operator !== "lt" && operator !== "lte" && operator !== "contains" && operator !== "not_contains" && operator !== "starts_with" && operator !== "ends_with" && operator !== "exists") {
11713
+ throw new Error(`Unsupported policy operator: ${String(operator)}`);
11714
+ }
11715
+ if (source === "stateMachine") {
11716
+ const machine = typeof raw.machine === "string" ? raw.machine : "";
11717
+ if (!machine) throw new Error("stateMachine condition requires machine");
11718
+ return {
11719
+ kind: "predicate",
11720
+ source,
11721
+ path: [machine],
11722
+ machine,
11723
+ operator,
11724
+ value: firstDefinedValue(raw)
11725
+ };
11726
+ }
11727
+ const path2 = normalizePath(raw.path ?? raw.field ?? raw.input);
11728
+ if (path2.length === 0) {
11729
+ throw new Error(`${source} condition requires a path`);
11730
+ }
11731
+ return {
11732
+ kind: "predicate",
11733
+ source,
11734
+ path: path2,
11735
+ operator,
11736
+ value: firstDefinedValue(raw)
11737
+ };
11738
+ }
11739
+ throw new Error(
11740
+ "Policy condition must contain all, any, not, input, object, or stateMachine"
11741
+ );
11742
+ }
11743
+ function summarizeCondition(condition) {
11744
+ switch (condition.kind) {
11745
+ case "always":
11746
+ return "always";
11747
+ case "all":
11748
+ return condition.conditions.map(summarizeCondition).join(" and ");
11749
+ case "any":
11750
+ return condition.conditions.map(summarizeCondition).join(" or ");
11751
+ case "not":
11752
+ return `not (${summarizeCondition(condition.condition)})`;
11753
+ case "predicate": {
11754
+ const path2 = condition.source === "stateMachine" ? `stateMachine.${condition.machine || condition.path.join(".")}` : `${condition.source}.${condition.path.join(".")}`;
11755
+ if (condition.operator === "exists") return `${path2} exists`;
11756
+ return `${path2} ${condition.operator} ${String(condition.value)}`;
11757
+ }
11758
+ }
11759
+ }
11760
+
11761
+ // ../metamodel-policy/src/index.ts
11762
+ function escapeGraphqlString(value) {
11763
+ return JSON.stringify(value);
11764
+ }
11765
+ function buildPolicyMutations(effectKey, spec) {
11766
+ const policies = spec.policies;
11767
+ if (!policies) return [];
11768
+ const mutations = [];
11769
+ const addRules = (key, outcome) => {
11770
+ const rules = policies[key] || [];
11771
+ rules.forEach((rule, index) => {
11772
+ const condition = normalizeCondition(rule.when);
11773
+ const summary = rule.reason || summarizeCondition(condition);
11774
+ const id = rule.id || `${effectKey}:${outcome}:${index + 1}`;
11775
+ mutations.push({
11776
+ label: `set policy ${outcome} on ${effectKey}`,
11777
+ query: `mutation { set_policy_rule(effect_key: ${escapeGraphqlString(effectKey)}, policy_id: ${escapeGraphqlString(id)}, outcome: ${escapeGraphqlString(outcome)}, reason: ${escapeGraphqlString(summary)}, condition_json: ${escapeGraphqlString(JSON.stringify(condition))}) }`
11778
+ });
11779
+ });
11780
+ };
11781
+ addRules("allowWhen", "allow");
11782
+ addRules("confirmWhen", "confirm");
11783
+ addRules("denyWhen", "deny");
11784
+ return mutations;
11785
+ }
11786
+ var policyMetamodelPackage = defineMetamodelPackage({
11787
+ id: "policy",
11788
+ manifest: {
11789
+ buildEffectMutations: buildPolicyMutations
11790
+ },
11791
+ summary: {
11792
+ selections: {
11793
+ methodFields: ["policies"]
11794
+ },
11795
+ readMethodSummary(rawMethod) {
11796
+ return rawMethod.policies ? { metamodels: { policies: rawMethod.policies } } : {};
11797
+ }
11798
+ },
11799
+ docs: {
11800
+ effectRows: [
11801
+ {
11802
+ key: "policies",
11803
+ description: "Universal effect policies with allowWhen, confirmWhen, and denyWhen structural conditions."
11804
+ }
11805
+ ]
11806
+ }
11807
+ });
11808
+
11382
11809
  // ../metamodel-required/src/index.ts
11383
11810
  function buildRequiredFieldMutations(fieldPath, required) {
11384
11811
  if (!required) return [];
@@ -12153,7 +12580,8 @@ var DEFAULT_METAMODEL_PACKAGES = [
12153
12580
  searchableMetamodelPackage,
12154
12581
  validationRuleMetamodelPackage,
12155
12582
  stateMachineMetamodelPackage,
12156
- effectBehaviorsMetamodelPackage
12583
+ effectBehaviorsMetamodelPackage,
12584
+ policyMetamodelPackage
12157
12585
  ];
12158
12586
  createMetamodelRegistry(
12159
12587
  DEFAULT_METAMODEL_PACKAGES
@@ -12220,6 +12648,12 @@ var DEFAULT_DIRECT_RECORD_OBJECTS_RETRY_COUNT = 3;
12220
12648
  var DEFAULT_DIRECT_RECORD_OBJECTS_RETRY_DELAY_MS = 1e3;
12221
12649
  var LOCAL_CONTROL_REQUEST_RETRY_COUNT = 4;
12222
12650
  var LOCAL_CONTROL_REQUEST_RETRY_DELAY_MS = 500;
12651
+ var SESSION_DATA_REQUEST_RETRY_COUNT = 4;
12652
+ var SESSION_DATA_REQUEST_RETRY_DELAY_MS = 500;
12653
+ var EFFECT_HOST_CONNECT_TIMEOUT_MS = 15e3;
12654
+ var EFFECT_CATALOG_SYNC_TIMEOUT_MS = 3e4;
12655
+ var EFFECT_CATALOG_SYNC_RETRY_COUNT = 3;
12656
+ var EFFECT_CATALOG_SYNC_RETRY_DELAY_MS = 1e3;
12223
12657
  function planRecordObjectsChunks(records, batchSize) {
12224
12658
  const total = records.length;
12225
12659
  const size = Math.max(1, Math.min(batchSize, total));
@@ -12234,6 +12668,19 @@ function planRecordObjectsChunks(records, batchSize) {
12234
12668
  function sleep(ms) {
12235
12669
  return new Promise((resolve) => setTimeout(resolve, ms));
12236
12670
  }
12671
+ function withTimeout(promise, timeoutMs, label) {
12672
+ let timer = null;
12673
+ const timeout = new Promise((_, reject) => {
12674
+ timer = setTimeout(() => {
12675
+ reject(new Error(`${label} timed out after ${timeoutMs}ms`));
12676
+ }, timeoutMs);
12677
+ });
12678
+ return Promise.race([promise, timeout]).finally(() => {
12679
+ if (timer) {
12680
+ clearTimeout(timer);
12681
+ }
12682
+ });
12683
+ }
12237
12684
  function isLocalControlUrl(url) {
12238
12685
  try {
12239
12686
  const parsed = new URL(url);
@@ -12247,7 +12694,19 @@ function isRetryableLocalWorkerRestart(status, body, url) {
12247
12694
  }
12248
12695
  function isRetryableRecordObjectsError(error) {
12249
12696
  const message = error instanceof Error ? error.message : String(error);
12250
- return /worker restarted mid-request|econnreset|network|socket connection was closed unexpectedly|timed out/i.test(
12697
+ return /worker restarted mid-request|econnreset|network|socket connection was closed unexpectedly|timed out|bad gateway|too many requests|gateway timeout|control plane api error \((?:429|500|502|503|504)\)|graphql api error \((?:429|500|502|503|504)\)|failed to record batch/i.test(
12698
+ message
12699
+ );
12700
+ }
12701
+ function isRetryableEffectRegistrationError(error) {
12702
+ const message = error instanceof Error ? error.message : String(error);
12703
+ return /timed out|websocket disconnected|websocket not connected|rpc timeout|worker restarted mid-request|network connection lost|bad gateway|gateway timeout|too many requests|(?:control plane|granular|graphql) api error \((?:429|500|502|503|504)\)/i.test(
12704
+ message
12705
+ );
12706
+ }
12707
+ function isRetryableSessionDataError(error) {
12708
+ const message = error instanceof Error ? error.message : String(error);
12709
+ return /network connection lost|worker restarted mid-request|econnreset|socket connection was closed unexpectedly|bad gateway|gateway timeout|service unavailable|session data api error \((?:429|500|502|503|504)\)/i.test(
12251
12710
  message
12252
12711
  );
12253
12712
  }
@@ -12269,16 +12728,28 @@ function computeEffectRegistrationKey(effect) {
12269
12728
  effect.versionSelector
12270
12729
  )}`;
12271
12730
  }
12272
- function buildEffectHostUrl(apiUrl, sandboxId, effectClientId, clientId) {
12273
- const url = new URL(apiUrl);
12274
- if (url.pathname.endsWith("/granular/ws/connect")) {
12731
+ function buildEffectHostUrl(apiUrl, sandboxId, effectClientId, clientId, effectHostUrl) {
12732
+ const overrideUrl = effectHostUrl || process.env.GRANULAR_EFFECT_HOST_URL || process.env.EFFECT_HOST_URL;
12733
+ const api = new URL(apiUrl);
12734
+ const localRuntimeBase = process.env.RUNTIME_ORCHESTRATOR_URL || (isLocalControlUrl(apiUrl) ? `${api.protocol}//${api.hostname}:8791` : "");
12735
+ const url = new URL(overrideUrl || localRuntimeBase || apiUrl);
12736
+ if (url.protocol === "https:") {
12737
+ url.protocol = "wss:";
12738
+ } else if (url.protocol === "http:") {
12739
+ url.protocol = "ws:";
12740
+ }
12741
+ if (!overrideUrl && isLocalControlUrl(apiUrl) && api.pathname.endsWith("/granular")) {
12742
+ url.pathname = "/granular/orchestrator/effects/connect";
12743
+ } else if (url.pathname.endsWith("/granular/ws/connect")) {
12275
12744
  url.pathname = url.pathname.replace(/\/ws\/connect$/, "/effects/connect");
12276
12745
  } else if (url.pathname.endsWith("/granular")) {
12277
- url.pathname = `${url.pathname.replace(/\/$/, "")}/effects/connect`;
12746
+ url.pathname = isLocalControlUrl(url.toString()) ? "/granular/orchestrator/effects/connect" : `${url.pathname.replace(/\/$/, "")}/effects/connect`;
12278
12747
  } else if (url.pathname.endsWith("/v2/ws/connect")) {
12279
12748
  url.pathname = url.pathname.replace(/\/ws\/connect$/, "/effects/connect");
12280
12749
  } else if (url.pathname.endsWith("/v2/ws")) {
12281
12750
  url.pathname = url.pathname.replace(/\/ws$/, "/effects/connect");
12751
+ } else if (url.pathname === "/" && isLocalControlUrl(url.toString()) && (url.port === "8791" || !overrideUrl && Boolean(localRuntimeBase))) {
12752
+ url.pathname = "/granular/orchestrator/effects/connect";
12282
12753
  } else if (url.pathname.endsWith("/ws/connect")) {
12283
12754
  url.pathname = url.pathname.replace(/\/ws\/connect$/, "/effects/connect");
12284
12755
  } else if (url.pathname.endsWith("/ws")) {
@@ -12313,6 +12784,79 @@ function normalizeHeapSnapshot(raw) {
12313
12784
  updatedAt: typeof heap.updatedAt === "number" ? heap.updatedAt : Date.now()
12314
12785
  };
12315
12786
  }
12787
+ function normalizeGraphPathSegment(value) {
12788
+ return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
12789
+ }
12790
+ function extractRecordIdFromGraphPath(path2, className) {
12791
+ const normalizedPrefix = `${normalizeGraphPathSegment(className)}_`;
12792
+ if (path2.startsWith(normalizedPrefix)) {
12793
+ return path2.slice(normalizedPrefix.length);
12794
+ }
12795
+ const legacyPrefix = `${className}_`;
12796
+ if (path2.startsWith(legacyPrefix)) {
12797
+ return path2.slice(legacyPrefix.length);
12798
+ }
12799
+ return path2;
12800
+ }
12801
+ function toRecordSearchResult(className, node) {
12802
+ const path2 = typeof node.path === "string" ? node.path : "";
12803
+ if (!path2) return null;
12804
+ const fields = Array.isArray(node.submodels) ? node.submodels.flatMap(
12805
+ (submodel) => {
12806
+ const name = typeof submodel?.label === "string" && submodel.label.trim() ? submodel.label : typeof submodel?.path === "string" ? submodel.path.split(":").pop() || submodel.path : "";
12807
+ if (!name) return [];
12808
+ if (typeof submodel.string_value === "string") {
12809
+ return [{ name, type: "string", value: submodel.string_value }];
12810
+ }
12811
+ if (typeof submodel.number_value === "number") {
12812
+ return [{ name, type: "number", value: submodel.number_value }];
12813
+ }
12814
+ if (typeof submodel.boolean_value === "boolean") {
12815
+ return [
12816
+ {
12817
+ name,
12818
+ type: "boolean",
12819
+ value: submodel.boolean_value
12820
+ }
12821
+ ];
12822
+ }
12823
+ return [];
12824
+ }
12825
+ ) : [];
12826
+ return {
12827
+ path: path2,
12828
+ className,
12829
+ id: extractRecordIdFromGraphPath(path2, className),
12830
+ label: typeof node.label === "string" && node.label.trim() ? node.label : extractRecordIdFromGraphPath(path2, className),
12831
+ description: typeof node.description === "string" && node.description.trim() ? node.description : null,
12832
+ fields
12833
+ };
12834
+ }
12835
+ function normalizeRecordSearchText(value) {
12836
+ return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, " ").replace(/\s+/g, " ").trim();
12837
+ }
12838
+ function rankRecordSearchResult(result, query, index) {
12839
+ const normalizedQuery = normalizeRecordSearchText(query);
12840
+ if (!normalizedQuery) {
12841
+ return index;
12842
+ }
12843
+ const label = normalizeRecordSearchText(result.label || "");
12844
+ const id = normalizeRecordSearchText(result.id || "");
12845
+ const path2 = normalizeRecordSearchText(result.path || "");
12846
+ const className = normalizeRecordSearchText(result.className || "");
12847
+ const searchable = [label, id, path2, className].filter(Boolean);
12848
+ if (label === normalizedQuery) return index;
12849
+ if (id === normalizedQuery || path2 === normalizedQuery) return 100 + index;
12850
+ if (label.startsWith(normalizedQuery)) return 200 + index;
12851
+ if (searchable.some((value) => value.startsWith(normalizedQuery))) {
12852
+ return 300 + index;
12853
+ }
12854
+ if (label.includes(normalizedQuery)) return 400 + index;
12855
+ if (searchable.some((value) => value.includes(normalizedQuery))) {
12856
+ return 500 + index;
12857
+ }
12858
+ return 900 + index;
12859
+ }
12316
12860
  function deriveRuntimeBaseUrl(apiEndpoint) {
12317
12861
  try {
12318
12862
  const endpoint = new URL(apiEndpoint);
@@ -12401,7 +12945,7 @@ function normalizeEnvironmentData(environment) {
12401
12945
  setup: normalizeEnvironmentSetupSummary(environment.setup)
12402
12946
  };
12403
12947
  }
12404
- var Environment = class {
12948
+ var Environment = class _Environment {
12405
12949
  granular;
12406
12950
  envData;
12407
12951
  _apiKey;
@@ -12596,28 +13140,30 @@ var Environment = class {
12596
13140
  return response.json();
12597
13141
  }
12598
13142
  // ==================== ID ↔ GRAPH PATH MAPPING ====================
13143
+ static normalizeGraphPathSegment(value) {
13144
+ return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^A-Za-z0-9]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "").toLowerCase();
13145
+ }
12599
13146
  /**
12600
- * Convert a class name + real-world ID into a unique graph path.
13147
+ * Convert a class name + application record ID into Granular's graph path.
12601
13148
  *
12602
- * Two objects of *different* classes may share the same real-world ID,
12603
- * so the graph path must incorporate the class to guarantee uniqueness.
12604
- *
12605
- * Format: `{className}_{id}` — deterministic, human-readable.
12606
- *
12607
- * **Convention**: class names should be simple identifiers without
12608
- * underscores (e.g. `author`, `book`). This ensures the prefix is
12609
- * unambiguously parseable by `extractIdFromGraphPath`.
13149
+ * This mirrors the record-write path normalization used by the control plane.
13150
+ * Keep the original customer/system ID in `real_id`; graph paths are stable
13151
+ * internal addresses, not the source of truth for business identity.
12610
13152
  */
12611
13153
  static toGraphPath(className, id) {
12612
- return `${className}_${id}`;
13154
+ return `${_Environment.normalizeGraphPathSegment(className)}_${_Environment.normalizeGraphPathSegment(id)}`;
12613
13155
  }
12614
13156
  /**
12615
- * Extract the real-world ID from a graph path, given the class name.
13157
+ * Best-effort extraction of an ID-like suffix from a graph path.
12616
13158
  *
12617
- * Strips the `{className}_` prefix. Returns the raw path if the
12618
- * expected prefix is not found.
13159
+ * Prefer the record's `real_id` field whenever exact customer/system IDs
13160
+ * matter, because graph path normalization is intentionally lossy.
12619
13161
  */
12620
13162
  static extractIdFromGraphPath(graphPath, className) {
13163
+ const normalizedPrefix = `${_Environment.normalizeGraphPathSegment(className)}_`;
13164
+ if (graphPath.startsWith(normalizedPrefix)) {
13165
+ return graphPath.substring(normalizedPrefix.length);
13166
+ }
12621
13167
  const prefix = `${className}_`;
12622
13168
  return graphPath.startsWith(prefix) ? graphPath.substring(prefix.length) : graphPath;
12623
13169
  }
@@ -12664,6 +13210,62 @@ var Environment = class {
12664
13210
  }
12665
13211
  return response.json();
12666
13212
  }
13213
+ async searchRecords(query, options = {}) {
13214
+ const normalizedQuery = query.replace(/\s+/g, " ").trim();
13215
+ const limit = Math.max(1, Math.min(50, Math.floor(options.limit ?? 12)));
13216
+ const offset = Math.max(0, Math.floor(options.offset ?? 0));
13217
+ const response = await this.graphql(
13218
+ `
13219
+ query RecordMentionSearch(
13220
+ $query: String
13221
+ $limit: Int
13222
+ $offset: Int
13223
+ $classNames: [String!]
13224
+ ) {
13225
+ record_search(
13226
+ query: $query
13227
+ limit: $limit
13228
+ offset: $offset
13229
+ class_names: $classNames
13230
+ ) {
13231
+ className
13232
+ model {
13233
+ path
13234
+ label
13235
+ description
13236
+ submodels {
13237
+ path
13238
+ label
13239
+ string_value
13240
+ number_value
13241
+ boolean_value
13242
+ }
13243
+ }
13244
+ }
13245
+ }
13246
+ `,
13247
+ {
13248
+ query: normalizedQuery,
13249
+ limit,
13250
+ offset,
13251
+ classNames: options.classNames?.length ? options.classNames : []
13252
+ }
13253
+ );
13254
+ const seen = /* @__PURE__ */ new Set();
13255
+ const results = (response.data?.record_search || []).flatMap((entry) => {
13256
+ const className = entry.className?.trim();
13257
+ const item = className && entry.model ? toRecordSearchResult(className, entry.model) : null;
13258
+ if (!item || seen.has(item.path)) {
13259
+ return [];
13260
+ }
13261
+ seen.add(item.path);
13262
+ return [item];
13263
+ });
13264
+ return results.map((result, index) => ({
13265
+ result,
13266
+ rank: rankRecordSearchResult(result, normalizedQuery, index)
13267
+ })).sort((left, right) => left.rank - right.rank).map((item) => item.result).slice(0, limit);
13268
+ }
12667
13269
  // ==================== RELATIONSHIP METHODS ====================
12668
13270
  /**
12669
13271
  * Define a relationship between two model types.
@@ -13429,7 +14031,8 @@ var Environment = class {
13429
14031
  body: JSON.stringify({
13430
14032
  records,
13431
14033
  batchSize: options.batchSize,
13432
- setupRunId: options.setupRunId
14034
+ setupRunId: options.setupRunId,
14035
+ writeMode: options.writeMode
13433
14036
  })
13434
14037
  }
13435
14038
  );
@@ -13481,11 +14084,13 @@ var Environment = class {
13481
14084
  };
13482
14085
  var EnvironmentSession = class extends Session {
13483
14086
  environment;
14087
+ sessionDataRoutePrefix;
13484
14088
  /** The last known graph container status, updated by checkReadiness() or on heartbeat */
13485
14089
  graphContainerStatus = null;
13486
- constructor(client, environment, clientId) {
14090
+ constructor(client, environment, clientId, options = {}) {
13487
14091
  super(client, clientId);
13488
14092
  this.environment = environment;
14093
+ this.sessionDataRoutePrefix = options.sessionDataRoutePrefix || "/orchestrator/ws/sessions";
13489
14094
  }
13490
14095
  get environmentId() {
13491
14096
  return this.environment.environmentId;
@@ -13530,7 +14135,7 @@ var EnvironmentSession = class extends Session {
13530
14135
  const doc = this.document;
13531
14136
  return normalizeHeapSnapshot(doc?.heap);
13532
14137
  }
13533
- async sessionDataRequest(path2, query) {
14138
+ async sessionDataRequest(path2, query, init2 = {}) {
13534
14139
  const searchParams = new URLSearchParams();
13535
14140
  for (const [key, value] of Object.entries(query || {})) {
13536
14141
  if (value !== null && typeof value !== "undefined" && value !== "") {
@@ -13538,23 +14143,39 @@ var EnvironmentSession = class extends Session {
13538
14143
  }
13539
14144
  }
13540
14145
  const queryString = searchParams.toString();
13541
- const response = await fetch(
13542
- `${this.environment.runtimeBaseUrl}/orchestrator/ws/sessions/${encodeURIComponent(this.sessionId)}${path2}${queryString ? `?${queryString}` : ""}`,
13543
- {
13544
- method: "GET",
13545
- headers: {
13546
- Authorization: `Bearer ${this.environment.authToken}`,
13547
- "Content-Type": "application/json"
14146
+ const url = `${this.environment.runtimeBaseUrl}${this.sessionDataRoutePrefix}/${encodeURIComponent(this.sessionId)}${path2}${queryString ? `?${queryString}` : ""}`;
14147
+ const body = typeof init2.body === "undefined" ? void 0 : JSON.stringify(init2.body);
14148
+ for (let attempt = 1; attempt <= SESSION_DATA_REQUEST_RETRY_COUNT; attempt += 1) {
14149
+ try {
14150
+ const response = await fetch(url, {
14151
+ method: init2.method || "GET",
14152
+ headers: {
14153
+ Authorization: `Bearer ${this.environment.authToken}`,
14154
+ "Content-Type": "application/json"
14155
+ },
14156
+ ...typeof body === "undefined" ? {} : { body }
14157
+ });
14158
+ if (response.ok) {
14159
+ return response.json();
14160
+ }
14161
+ const errorText = await response.text();
14162
+ const error = new Error(
14163
+ `Session data API Error (${response.status}): ${errorText}`
14164
+ );
14165
+ if (isLocalControlUrl(url) && isRetryableSessionDataError(error) && attempt < SESSION_DATA_REQUEST_RETRY_COUNT) {
14166
+ await sleep(SESSION_DATA_REQUEST_RETRY_DELAY_MS * attempt);
14167
+ continue;
14168
+ }
14169
+ throw error;
14170
+ } catch (error) {
14171
+ if (isLocalControlUrl(url) && isRetryableSessionDataError(error) && attempt < SESSION_DATA_REQUEST_RETRY_COUNT) {
14172
+ await sleep(SESSION_DATA_REQUEST_RETRY_DELAY_MS * attempt);
14173
+ continue;
13548
14174
  }
14175
+ throw error;
13549
14176
  }
13550
- );
13551
- if (!response.ok) {
13552
- const errorText = await response.text();
13553
- throw new Error(
13554
- `Session data API Error (${response.status}): ${errorText}`
13555
- );
13556
14177
  }
13557
- return response.json();
14178
+ throw new Error(`Session data API Error: exhausted retries for ${url}`);
13558
14179
  }
13559
14180
  async collectAllSessionItems(listPage) {
13560
14181
  const items = [];
@@ -13612,6 +14233,17 @@ var EnvironmentSession = class extends Session {
13612
14233
  get: (name) => this.sessionDataRequest(
13613
14234
  `/heap/lists/${encodeURIComponent(name)}`
13614
14235
  )
14236
+ },
14237
+ variables: {
14238
+ list: (options = {}) => this.sessionDataRequest("/heap/variables", options),
14239
+ get: (name) => this.sessionDataRequest(
14240
+ `/heap/variables/${encodeURIComponent(name)}`
14241
+ ),
14242
+ delete: (name) => this.sessionDataRequest(
14243
+ `/heap/variables/${encodeURIComponent(name)}`,
14244
+ void 0,
14245
+ { method: "DELETE" }
14246
+ )
13615
14247
  }
13616
14248
  };
13617
14249
  }
@@ -13680,6 +14312,19 @@ var EnvironmentSession = class extends Session {
13680
14312
  async graphql(query, variables) {
13681
14313
  return this.environment.graphql(query, variables);
13682
14314
  }
14315
+ async searchRecords(query, options = {}) {
14316
+ return this.environment.searchRecords(query, options);
14317
+ }
14318
+ async mentionRecord(input) {
14319
+ return this.sessionDataRequest(
14320
+ "/records/mention",
14321
+ void 0,
14322
+ {
14323
+ method: "POST",
14324
+ body: input
14325
+ }
14326
+ );
14327
+ }
13683
14328
  async defineRelationship(options) {
13684
14329
  return this.environment.defineRelationship(options);
13685
14330
  }
@@ -13827,6 +14472,7 @@ var Granular = class _Granular {
13827
14472
  WebSocketCtor;
13828
14473
  onUnexpectedClose;
13829
14474
  onReconnectError;
14475
+ effectHostUrl;
13830
14476
  debugHttp = process.env.GRANULAR_DEBUG_HTTP === "1";
13831
14477
  /** Sandbox-level effect registry: sandboxId → (effectKey@selector → ToolWithHandler) */
13832
14478
  sandboxEffects = /* @__PURE__ */ new Map();
@@ -13855,6 +14501,7 @@ var Granular = class _Granular {
13855
14501
  this.WebSocketCtor = options.WebSocketCtor;
13856
14502
  this.onUnexpectedClose = options.onUnexpectedClose;
13857
14503
  this.onReconnectError = options.onReconnectError;
14504
+ this.effectHostUrl = options.effectHostUrl;
13858
14505
  this.httpUrl = this.apiUrl.replace(/^wss:\/\//, "https://").replace(/^ws:\/\//, "http://").replace(/\/ws$/, "");
13859
14506
  }
13860
14507
  /**
@@ -14025,6 +14672,30 @@ var Granular = class _Granular {
14025
14672
  permissions: options.permissions || options.user?.permissions || []
14026
14673
  });
14027
14674
  }
14675
+ /**
14676
+ * Run a registered environment importer against an environment that was
14677
+ * opened outside this SDK instance, for example by a delegated browser flow.
14678
+ *
14679
+ * This uses the same setup-run and queued record-import plumbing as
14680
+ * `openEnvironment()`: importer stages, expected object counts, and queued
14681
+ * import counters remain visible through `environment.setup` and
14682
+ * `getRecordImportSummary()`.
14683
+ */
14684
+ async runEnvironmentImporterForEnvironment(environmentId, options = {}) {
14685
+ const environmentData = await this.environments.get(environmentId);
14686
+ const environment = this.bindEnvironmentHandle(environmentData);
14687
+ const requestedOntology = options.ontology || environmentData.ontologyId || environmentData.sandboxId;
14688
+ return this.runEnvironmentImporter(
14689
+ {
14690
+ environment: environmentData,
14691
+ requestedOntology,
14692
+ sandboxId: environmentData.sandboxId,
14693
+ subjectId: environmentData.subjectId,
14694
+ setupTriggerReason: options.reason || "new_environment"
14695
+ },
14696
+ environment
14697
+ );
14698
+ }
14028
14699
  resolveRequestedTag(options, methodName) {
14029
14700
  const tag = options.tag?.trim() || options.tagName?.trim() || options.environment?.trim();
14030
14701
  if (!tag) {
@@ -14268,15 +14939,25 @@ var Granular = class _Granular {
14268
14939
  return ontologyImporter;
14269
14940
  }
14270
14941
  async maybeRunEnvironmentImporter(resolved, environment) {
14271
- if (!resolved.setupTriggerReason) {
14272
- return;
14942
+ const setupTriggerReason = resolved.setupTriggerReason;
14943
+ if (!setupTriggerReason) {
14944
+ return null;
14273
14945
  }
14946
+ return this.runEnvironmentImporter(
14947
+ {
14948
+ ...resolved,
14949
+ setupTriggerReason
14950
+ },
14951
+ environment
14952
+ );
14953
+ }
14954
+ async runEnvironmentImporter(resolved, environment) {
14274
14955
  const importer = this.resolveEnvironmentImporter(
14275
14956
  resolved.requestedOntology,
14276
14957
  resolved.sandboxId
14277
14958
  );
14278
14959
  if (!importer) {
14279
- return;
14960
+ return null;
14280
14961
  }
14281
14962
  const setupRun = await this.request(
14282
14963
  `/control/environments/${environment.environmentId}/setup-runs`,
@@ -14316,16 +14997,24 @@ var Granular = class _Granular {
14316
14997
  },
14317
14998
  importRecords: async (records, options) => environment.enqueueRecordImport(records, {
14318
14999
  batchSize: options?.batchSize,
15000
+ writeMode: options?.writeMode,
14319
15001
  setupRunId
14320
15002
  })
14321
15003
  };
14322
15004
  try {
14323
15005
  await importer(importerContext);
14324
- await updateSetupRun({ markHookCompleted: true });
15006
+ const completedSetupRun = await this.request(
15007
+ `/control/environment-setup-runs/${setupRunId}`,
15008
+ {
15009
+ method: "PATCH",
15010
+ body: JSON.stringify({ markHookCompleted: true })
15011
+ }
15012
+ );
14325
15013
  const refreshedEnvironment = await this.environments.get(
14326
15014
  environment.environmentId
14327
15015
  );
14328
15016
  environment.syncEnvironmentData(refreshedEnvironment);
15017
+ return completedSetupRun;
14329
15018
  } catch (error) {
14330
15019
  await updateSetupRun({
14331
15020
  status: "failed",
@@ -14373,27 +15062,45 @@ var Granular = class _Granular {
14373
15062
  return effects;
14374
15063
  }
14375
15064
  serializeEffect(effect) {
14376
- return {
15065
+ const serialized = {
14377
15066
  effectKey: computeEffectKey2(effect),
14378
15067
  name: effect.name,
14379
15068
  description: effect.description,
14380
15069
  inputSchema: effect.inputSchema,
14381
- outputSchema: effect.outputSchema,
14382
15070
  stability: effect.stability || "stable",
14383
- provenance: effect.provenance || { source: "custom" },
14384
- tags: effect.tags,
14385
- className: effect.className,
14386
- static: effect.static,
14387
- versionSelector: effect.versionSelector
15071
+ provenance: effect.provenance || { source: "custom" }
14388
15072
  };
15073
+ if (effect.outputSchema !== void 0) {
15074
+ serialized.outputSchema = effect.outputSchema;
15075
+ }
15076
+ if (effect.tags !== void 0) {
15077
+ serialized.tags = effect.tags;
15078
+ }
15079
+ if (effect.className !== void 0) {
15080
+ serialized.className = effect.className;
15081
+ }
15082
+ if (effect.static !== void 0) {
15083
+ serialized.static = effect.static;
15084
+ }
15085
+ if (effect.versionSelector !== void 0) {
15086
+ serialized.versionSelector = effect.versionSelector;
15087
+ }
15088
+ if (effect.metamodels !== void 0) {
15089
+ serialized.metamodels = effect.metamodels;
15090
+ }
15091
+ return serialized;
14389
15092
  }
14390
15093
  async publishSandboxEffectCatalog(host) {
14391
15094
  const effects = Array.from(
14392
15095
  this.getSandboxEffectMap(host.sandboxId).values()
14393
15096
  ).map((effect) => this.serializeEffect(effect));
14394
- const result = await host.wsClient.call("effects.publishCatalog", {
14395
- effects
14396
- });
15097
+ const result = await withTimeout(
15098
+ host.wsClient.call("effects.publishCatalog", {
15099
+ effects
15100
+ }),
15101
+ EFFECT_CATALOG_SYNC_TIMEOUT_MS,
15102
+ `effects.publishCatalog for sandbox ${host.sandboxId}`
15103
+ );
14397
15104
  const acceptedCount = typeof result?.acceptedCount === "number" ? result.acceptedCount : 0;
14398
15105
  const rejected = Array.isArray(result?.rejected) ? result.rejected : [];
14399
15106
  if (acceptedCount === 0 && rejected.length > 0) {
@@ -14412,8 +15119,26 @@ var Granular = class _Granular {
14412
15119
  }
14413
15120
  }
14414
15121
  async syncSandboxEffectCatalog(sandboxId) {
14415
- const host = await this.ensureSandboxEffectHost(sandboxId);
14416
- await this.publishSandboxEffectCatalog(host);
15122
+ let lastError;
15123
+ for (let attempt = 1; attempt <= EFFECT_CATALOG_SYNC_RETRY_COUNT; attempt += 1) {
15124
+ try {
15125
+ const host = await this.ensureSandboxEffectHost(sandboxId);
15126
+ await this.publishSandboxEffectCatalog(host);
15127
+ return;
15128
+ } catch (error) {
15129
+ lastError = error;
15130
+ this.disconnectSandboxEffectHost(sandboxId);
15131
+ if (attempt === EFFECT_CATALOG_SYNC_RETRY_COUNT || !isRetryableEffectRegistrationError(error)) {
15132
+ throw error;
15133
+ }
15134
+ console.warn(
15135
+ `[Granular] Retrying effect registration for sandbox ${sandboxId} after transient failure (${attempt}/${EFFECT_CATALOG_SYNC_RETRY_COUNT - 1} retries used):`,
15136
+ error
15137
+ );
15138
+ await sleep(EFFECT_CATALOG_SYNC_RETRY_DELAY_MS * attempt);
15139
+ }
15140
+ }
15141
+ throw lastError instanceof Error ? lastError : new Error(String(lastError));
14417
15142
  }
14418
15143
  recoverEffectHost(host, error) {
14419
15144
  if (host.recovering) {
@@ -14506,7 +15231,8 @@ var Granular = class _Granular {
14506
15231
  this.apiUrl,
14507
15232
  sandboxId,
14508
15233
  effectClientId,
14509
- clientId
15234
+ clientId,
15235
+ this.effectHostUrl
14510
15236
  ),
14511
15237
  sessionId: `effect-host:${effectClientId}`,
14512
15238
  token: this.apiKey,
@@ -14542,7 +15268,11 @@ var Granular = class _Granular {
14542
15268
  wsClient.on("disconnect", () => {
14543
15269
  this.stopEffectHostHeartbeat(host);
14544
15270
  });
14545
- await wsClient.connect();
15271
+ await withTimeout(
15272
+ wsClient.connect(),
15273
+ EFFECT_HOST_CONNECT_TIMEOUT_MS,
15274
+ `effect host WebSocket connect for sandbox ${sandboxId}`
15275
+ );
14546
15276
  await this.synchronizeEffectHost(host);
14547
15277
  this.sandboxEffectHosts.set(sandboxId, host);
14548
15278
  return host;
@@ -14665,7 +15395,7 @@ var Granular = class _Granular {
14665
15395
  /**
14666
15396
  * Ensure a permission profile exists for a sandbox, creating it if needed.
14667
15397
  * If profileName matches an existing profile name, returns its ID.
14668
- * Otherwise, creates a new profile with default allow-all rules.
15398
+ * Otherwise, creates a v1 source-profile file shape with an allow default.
14669
15399
  */
14670
15400
  async ensurePermissionProfile(sandboxId, profileName) {
14671
15401
  try {
@@ -14679,8 +15409,11 @@ var Granular = class _Granular {
14679
15409
  const created = await this.permissionProfiles.create(sandboxId, {
14680
15410
  name: profileName,
14681
15411
  rules: {
14682
- effects: { allow: ["*"] },
14683
- resources: { allow: ["*"] }
15412
+ schemaVersion: 1,
15413
+ name: profileName,
15414
+ description: profileName === "allow-all" ? "Every declared action is visible unless a manifest policy denies it." : `Generated permission profile ${profileName}`,
15415
+ defaults: { actionPolicy: "allow" },
15416
+ actions: []
14684
15417
  }
14685
15418
  });
14686
15419
  return created.permissionProfileId;
@@ -14753,33 +15486,63 @@ var Granular = class _Granular {
14753
15486
  * Permission Profile management for sandboxes
14754
15487
  */
14755
15488
  get permissionProfiles() {
15489
+ const profileSourceFromRecord = (record) => {
15490
+ const profile = record.profile || record.rules || {};
15491
+ return {
15492
+ ...profile,
15493
+ schemaVersion: profile.schemaVersion || 1,
15494
+ name: profile.name || record.name,
15495
+ description: profile.description || record.description
15496
+ };
15497
+ };
14756
15498
  return {
14757
15499
  list: async (sandboxId) => {
14758
15500
  const result = await this.request(
14759
- `/control/sandboxes/${sandboxId}/permission-profiles`
15501
+ `/control/sandboxes/${sandboxId}/permission-profile-sources`
14760
15502
  );
14761
15503
  return result.items;
14762
15504
  },
14763
15505
  get: async (sandboxId, profileId) => {
14764
- return this.request(
14765
- `/control/sandboxes/${sandboxId}/permission-profiles/${profileId}`
15506
+ const result = await this.request(
15507
+ `/control/sandboxes/${sandboxId}/permission-profile-sources`
14766
15508
  );
15509
+ const profile = result.items.find(
15510
+ (item) => item.permissionProfileId === profileId || item.name === profileId
15511
+ );
15512
+ if (!profile) {
15513
+ throw new Error(`Permission profile source not found: ${profileId}`);
15514
+ }
15515
+ return profile;
14767
15516
  },
14768
15517
  create: async (sandboxId, data) => {
14769
- return this.request(
14770
- `/control/sandboxes/${sandboxId}/permission-profiles`,
15518
+ const profile = {
15519
+ ...data.rules,
15520
+ schemaVersion: 1,
15521
+ name: data.name
15522
+ };
15523
+ const existingProfiles = await this.permissionProfiles.list(sandboxId);
15524
+ const profiles = [
15525
+ ...existingProfiles.filter((existing) => existing.name !== data.name).map((existing) => profileSourceFromRecord(existing)),
15526
+ profile
15527
+ ];
15528
+ const result = await this.request(
15529
+ `/control/sandboxes/${sandboxId}/permission-profile-sources`,
14771
15530
  {
14772
- method: "POST",
14773
- body: JSON.stringify(data)
15531
+ method: "PUT",
15532
+ body: JSON.stringify({ profiles })
14774
15533
  }
14775
15534
  );
15535
+ const synced = result.items.find((item) => item.name === data.name) || result.items[0];
15536
+ if (!synced) {
15537
+ throw new Error(
15538
+ `Permission profile source sync did not return ${data.name}`
15539
+ );
15540
+ }
15541
+ return synced;
14776
15542
  },
14777
- delete: async (sandboxId, profileId) => {
14778
- return this.request(
14779
- `/control/sandboxes/${sandboxId}/permission-profiles/${profileId}`,
14780
- {
14781
- method: "DELETE"
14782
- }
15543
+ delete: async (_sandboxId, _profileId) => {
15544
+ throw new Error(
15545
+ "Permission profile sources are updated by syncing the desired source set."
14783
15546
  );
14784
15547
  }
14785
15548
  };
@@ -15058,21 +15821,8 @@ function uniqueStrings(values, maxCount) {
15058
15821
  }
15059
15822
  return output;
15060
15823
  }
15061
- function formatScalar(value) {
15062
- if (typeof value === "string") return JSON.stringify(value);
15063
- if (typeof value === "number" || typeof value === "boolean")
15064
- return String(value);
15065
- if (value === null) return "null";
15066
- return "unknown";
15067
- }
15068
- function describeHeapEntry(entry, previewFieldLimit = 3) {
15069
- const headline = entry.label || entry.id || entry.path || "Unknown";
15070
- const pathLabel = entry.path && entry.path !== headline ? ` <${entry.path}>` : "";
15071
- const classLabel = entry.className || "unknown";
15072
- const preview = asArray2(entry.fields).filter(
15073
- (field) => field?.name && field.name !== "_realId" && field.name !== "real_id"
15074
- ).slice(0, previewFieldLimit).map((field) => `${field.name}=${formatScalar(field.value)}`).join(", ");
15075
- return preview ? `${headline}${pathLabel} [${classLabel}] ${preview}` : `${headline}${pathLabel} [${classLabel}]`;
15824
+ function renderConstBlock(name, value) {
15825
+ return `const ${name} = ${JSON.stringify(value, null, 2)} as const;`;
15076
15826
  }
15077
15827
  function hashString(value) {
15078
15828
  if (!value) return null;
@@ -15083,101 +15833,6 @@ function hashString(value) {
15083
15833
  }
15084
15834
  return (hash >>> 0).toString(16).padStart(8, "0");
15085
15835
  }
15086
- function hasSubstantiveAwaitAfterPrompt(code, marker) {
15087
- const startIndex = code.indexOf(marker);
15088
- if (startIndex === -1) return true;
15089
- const segment = code.slice(startIndex + marker.length);
15090
- const callMatches = segment.matchAll(
15091
- /await\s+([A-Za-z0-9_$.]+)\.([A-Za-z0-9_]+)\s*\(/g
15092
- );
15093
- for (const match of callMatches) {
15094
- const receiver = match[1] || "";
15095
- const method = match[2] || "";
15096
- if (receiver === "loop" || receiver === "heap") continue;
15097
- if (method.startsWith("get_") || method.startsWith("get")) continue;
15098
- return true;
15099
- }
15100
- return false;
15101
- }
15102
- function reviewGeneratedJobCode(code) {
15103
- const normalized = typeof code === "string" ? code : "";
15104
- if (!normalized.trim()) return [];
15105
- const issues = [];
15106
- if (/require\s*\(\s*['"]\.\/sandbox-tools['"]\s*\)/.test(normalized)) {
15107
- issues.push({
15108
- code: "commonjs_require",
15109
- severity: "error",
15110
- message: "Use ESM imports like `import { Customer, loop } from './sandbox-tools';` instead of require('./sandbox-tools'). Generated jobs must be plain runnable JavaScript for the sandbox runtime."
15111
- });
15112
- }
15113
- const placeholderPatterns = [
15114
- /ready to make the change next/i,
15115
- /ready to .* next/i,
15116
- /ready to .* now/i,
15117
- /i can make the change now/i,
15118
- /i can do that next/i,
15119
- /i'?m ready to continue/i,
15120
- /have your approval .* ready to make/i,
15121
- /approved\./i
15122
- ];
15123
- if (normalized.includes("await loop.confirm(")) {
15124
- const postConfirm = normalized.slice(
15125
- normalized.indexOf("await loop.confirm(")
15126
- );
15127
- const hasPlaceholder = placeholderPatterns.some(
15128
- (pattern) => pattern.test(postConfirm)
15129
- );
15130
- const hasSubstantiveAwait = hasSubstantiveAwaitAfterPrompt(
15131
- normalized,
15132
- "await loop.confirm("
15133
- );
15134
- if (!hasSubstantiveAwait || hasPlaceholder) {
15135
- issues.push({
15136
- code: "placeholder_after_confirm",
15137
- severity: "error",
15138
- message: "After await loop.confirm(...) returns true, the job must perform the approved mutation in the same resumed run. Do not stop with placeholder text like 'Approved, I can make the change now.'"
15139
- });
15140
- }
15141
- }
15142
- if (normalized.includes("await loop.ask_user(")) {
15143
- const postPrompt = normalized.slice(
15144
- normalized.indexOf("await loop.ask_user(")
15145
- );
15146
- const hasPlaceholder = placeholderPatterns.some(
15147
- (pattern) => pattern.test(postPrompt)
15148
- );
15149
- const hasSubstantiveAwait = hasSubstantiveAwaitAfterPrompt(
15150
- normalized,
15151
- "await loop.ask_user("
15152
- );
15153
- if (hasPlaceholder && !hasSubstantiveAwait) {
15154
- issues.push({
15155
- code: "placeholder_after_ask_user",
15156
- severity: "error",
15157
- message: "After await loop.ask_user(...) returns a usable answer, continue the workflow in the same resumed run instead of stopping with placeholder text about doing the work later."
15158
- });
15159
- }
15160
- }
15161
- const hasConversationalReturn = /return\s+[`'"]/.test(normalized) || /\breply\s*:/.test(normalized) || /\bagent_message\s*\(/.test(normalized) || /\bagent_text_message\s*\(/.test(normalized);
15162
- const returnsObjectLiteral = /return\s+\{[\s\S]*?\}/.test(normalized);
15163
- const returnsShowPayload = /return\s+\{[\s\S]*?\bshow\s*:/.test(normalized);
15164
- const closesLoop = /loop\.close_loop\s*\(/.test(normalized);
15165
- if (!hasConversationalReturn && returnsObjectLiteral && !closesLoop) {
15166
- issues.push({
15167
- code: "missing_user_reply",
15168
- severity: "error",
15169
- message: "User-facing jobs must end with a natural-language answer. Return a short string, an object with a top-level `reply` string, or post text with agent_text_message(...). Do not end with bare structured JSON."
15170
- });
15171
- }
15172
- if (returnsShowPayload) {
15173
- issues.push({
15174
- code: "return_show_not_for_ui",
15175
- severity: "error",
15176
- message: "Do not use the final return value to send UI record refs through `show`. Use agent_heap_objects(...) for heap-backed UI, then return plain text if you still want a final textual answer."
15177
- });
15178
- }
15179
- return issues;
15180
- }
15181
15836
  function extractFocusHintsFromActionSummary(actionSummaryLines) {
15182
15837
  const variableNames = [];
15183
15838
  const listNames = [];
@@ -15205,6 +15860,247 @@ function extractFocusHintsFromActionSummary(actionSummaryLines) {
15205
15860
  function normalizeActionSummaryForPrompt(line) {
15206
15861
  return line.replace(/\blimit=/g, "perPage=").replace(/\blimit:/g, "perPage:");
15207
15862
  }
15863
+ function collectConversationReferents(liveDoc) {
15864
+ const conversation = asRecord4(liveDoc?.conversation);
15865
+ const persistedReferents = asArray2(conversation?.referents).map((value) => asRecord4(value)).filter((value) => Boolean(value));
15866
+ if (persistedReferents.length > 0) {
15867
+ return persistedReferents.slice().sort((left, right) => (right.ts || 0) - (left.ts || 0));
15868
+ }
15869
+ const heap = asRecord4(liveDoc?.heap);
15870
+ const entriesByPath = asRecord4(heap?.entriesByPath) || {};
15871
+ const listsByName = asRecord4(heap?.listsByName) || {};
15872
+ const variablesByName = asRecord4(heap?.variablesByName) || {};
15873
+ const messages = asArray2(conversation?.messages).map((value) => asRecord4(value)).filter((value) => Boolean(value)).slice().sort((left, right) => (Number(right.ts) || 0) - (Number(left.ts) || 0));
15874
+ const referents = [];
15875
+ const seen = /* @__PURE__ */ new Set();
15876
+ const pushReferent = (referent) => {
15877
+ if (!referent?.kind || !referent.ref) return;
15878
+ const key = `${referent.kind}:${referent.ref}`;
15879
+ if (seen.has(key)) return;
15880
+ seen.add(key);
15881
+ referents.push(referent);
15882
+ };
15883
+ for (const message of messages) {
15884
+ if (message.role !== "assistant") continue;
15885
+ const show = asRecord4(message.show);
15886
+ if (!show) continue;
15887
+ const ts = Number(message.ts) || 0;
15888
+ const messageId = typeof message.id === "string" ? message.id : void 0;
15889
+ const jobId = typeof message.jobId === "string" ? message.jobId : void 0;
15890
+ const entryPaths = uniqueStrings(asArray2(show.entryPaths));
15891
+ const entryClassCounts = /* @__PURE__ */ new Map();
15892
+ const entryMetadata = entryPaths.map((entryPath) => {
15893
+ const entry = asRecord4(entriesByPath[entryPath]);
15894
+ const className = typeof entry?.className === "string" ? entry.className : void 0;
15895
+ if (className) {
15896
+ entryClassCounts.set(
15897
+ className,
15898
+ (entryClassCounts.get(className) || 0) + 1
15899
+ );
15900
+ }
15901
+ return { entryPath, entry, className };
15902
+ });
15903
+ const displayGroupId = entryMetadata.length > 1 ? `message:${messageId || jobId || ts}:entries` : void 0;
15904
+ for (const [
15905
+ index,
15906
+ { entryPath, entry, className }
15907
+ ] of entryMetadata.entries()) {
15908
+ pushReferent({
15909
+ id: `entry:${entryPath}`,
15910
+ kind: "entry",
15911
+ ref: entryPath,
15912
+ role: "assistant",
15913
+ source: "heap_objects",
15914
+ entryPath,
15915
+ recordId: typeof entry?.id === "string" ? entry.id : void 0,
15916
+ className,
15917
+ label: typeof entry?.label === "string" && entry.label.trim() ? entry.label.trim() : typeof entry?.id === "string" && entry.id.trim() ? entry.id.trim() : entryPath,
15918
+ ...displayGroupId ? {
15919
+ displayGroupId,
15920
+ displayGroupIndex: index,
15921
+ displayGroupSize: entryMetadata.length,
15922
+ ...className && (entryClassCounts.get(className) || 0) > 1 ? { displayGroupSameTypeSize: entryClassCounts.get(className) } : {}
15923
+ } : {},
15924
+ messageId,
15925
+ jobId,
15926
+ ts
15927
+ });
15928
+ }
15929
+ for (const listName of uniqueStrings(asArray2(show.listNames))) {
15930
+ const list = asRecord4(listsByName[listName]);
15931
+ pushReferent({
15932
+ id: `list:${listName}`,
15933
+ kind: "list",
15934
+ ref: listName,
15935
+ role: "assistant",
15936
+ source: "heap_objects",
15937
+ listName,
15938
+ className: typeof list?.className === "string" ? list.className : void 0,
15939
+ count: Array.isArray(list?.paths) ? list.paths.length : null,
15940
+ messageId,
15941
+ jobId,
15942
+ ts
15943
+ });
15944
+ }
15945
+ for (const variableName of uniqueStrings(
15946
+ asArray2(show.variableNames)
15947
+ )) {
15948
+ const variable = asRecord4(variablesByName[variableName]);
15949
+ const entryPath = typeof variable?.entryPath === "string" ? variable.entryPath : void 0;
15950
+ const listName = typeof variable?.listName === "string" ? variable.listName : void 0;
15951
+ const entry = entryPath ? asRecord4(entriesByPath[entryPath]) : null;
15952
+ const list = listName ? asRecord4(listsByName[listName]) : null;
15953
+ pushReferent({
15954
+ id: `variable:${variableName}`,
15955
+ kind: "variable",
15956
+ ref: variableName,
15957
+ role: "assistant",
15958
+ source: "heap_objects",
15959
+ variableName,
15960
+ variableKind: typeof variable?.kind === "string" ? variable.kind : void 0,
15961
+ entryPath,
15962
+ recordId: typeof entry?.id === "string" ? entry.id : void 0,
15963
+ listName,
15964
+ className: typeof variable?.className === "string" ? variable.className : typeof entry?.className === "string" ? entry.className : typeof list?.className === "string" ? list.className : void 0,
15965
+ label: typeof entry?.label === "string" && entry.label.trim() ? entry.label.trim() : typeof entry?.id === "string" && entry.id.trim() ? entry.id.trim() : null,
15966
+ count: variable?.kind === "list" && Array.isArray(list?.paths) ? list.paths.length : null,
15967
+ scalarValue: variable?.kind === "scalar" && (typeof variable.value === "string" || typeof variable.value === "number" || typeof variable.value === "boolean" || variable.value === null) ? variable.value : void 0,
15968
+ messageId,
15969
+ jobId,
15970
+ ts
15971
+ });
15972
+ }
15973
+ }
15974
+ return referents;
15975
+ }
15976
+ function projectConversationReferentFocus(liveDoc) {
15977
+ const heap = asRecord4(liveDoc?.heap);
15978
+ const listsByName = asRecord4(heap?.listsByName) || {};
15979
+ const referents = collectConversationReferents(liveDoc);
15980
+ const entryPaths = [];
15981
+ const listNames = [];
15982
+ const variableNames = [];
15983
+ let entryCount = 0;
15984
+ let listCount = 0;
15985
+ let variableCount = 0;
15986
+ for (const referent of referents) {
15987
+ if (referent.kind === "entry" && typeof referent.entryPath === "string" && entryCount < 8) {
15988
+ entryCount += 1;
15989
+ entryPaths.push(referent.entryPath);
15990
+ continue;
15991
+ }
15992
+ if (referent.kind === "list" && typeof referent.listName === "string" && listCount < 4) {
15993
+ listCount += 1;
15994
+ listNames.push(referent.listName);
15995
+ const list = asRecord4(listsByName[referent.listName]);
15996
+ entryPaths.push(...asArray2(list?.paths).slice(0, 4));
15997
+ continue;
15998
+ }
15999
+ if (referent.kind === "variable" && typeof referent.variableName === "string" && variableCount < 4) {
16000
+ variableCount += 1;
16001
+ variableNames.push(referent.variableName);
16002
+ if (typeof referent.entryPath === "string") {
16003
+ entryPaths.push(referent.entryPath);
16004
+ }
16005
+ if (typeof referent.listName === "string") {
16006
+ listNames.push(referent.listName);
16007
+ const list = asRecord4(listsByName[referent.listName]);
16008
+ entryPaths.push(...asArray2(list?.paths).slice(0, 4));
16009
+ }
16010
+ }
16011
+ }
16012
+ return {
16013
+ entryPaths: uniqueStrings(entryPaths, 8),
16014
+ listNames: uniqueStrings(listNames, 4),
16015
+ variableNames: uniqueStrings(variableNames, 4)
16016
+ };
16017
+ }
16018
+ function selectConversationReferentsForPrompt(referents) {
16019
+ const selected = [];
16020
+ const seen = /* @__PURE__ */ new Set();
16021
+ let entryCount = 0;
16022
+ let listCount = 0;
16023
+ let variableCount = 0;
16024
+ for (const referent of referents) {
16025
+ if (!referent.kind || !referent.ref) continue;
16026
+ const key = `${referent.kind}:${referent.ref}`;
16027
+ if (seen.has(key)) continue;
16028
+ if (referent.kind === "entry") {
16029
+ if (entryCount >= 8) continue;
16030
+ entryCount += 1;
16031
+ } else if (referent.kind === "list") {
16032
+ if (listCount >= 4) continue;
16033
+ listCount += 1;
16034
+ } else if (referent.kind === "variable") {
16035
+ if (variableCount >= 4) continue;
16036
+ variableCount += 1;
16037
+ }
16038
+ seen.add(key);
16039
+ selected.push(referent);
16040
+ }
16041
+ return selected;
16042
+ }
16043
+ function projectConversationReferentSummary(liveDoc) {
16044
+ const referents = selectConversationReferentsForPrompt(
16045
+ collectConversationReferents(liveDoc)
16046
+ );
16047
+ const compact = referents.map((referent) => {
16048
+ if (referent.kind === "entry" && referent.entryPath) {
16049
+ return {
16050
+ kind: "entry",
16051
+ role: referent.role || null,
16052
+ source: referent.source || null,
16053
+ path: referent.entryPath,
16054
+ id: referent.recordId || null,
16055
+ type: referent.className || "unknown",
16056
+ label: referent.label || referent.entryPath,
16057
+ group: referent.displayGroupId ? {
16058
+ id: referent.displayGroupId,
16059
+ index: typeof referent.displayGroupIndex === "number" ? referent.displayGroupIndex : null,
16060
+ size: typeof referent.displayGroupSize === "number" ? referent.displayGroupSize : null,
16061
+ sameTypeSize: typeof referent.displayGroupSameTypeSize === "number" ? referent.displayGroupSameTypeSize : null
16062
+ } : void 0
16063
+ };
16064
+ }
16065
+ if (referent.kind === "entry" && referent.recordId) {
16066
+ return {
16067
+ kind: "entry",
16068
+ role: referent.role || null,
16069
+ source: referent.source || null,
16070
+ id: referent.recordId,
16071
+ type: referent.className || "unknown",
16072
+ label: referent.label || referent.recordId
16073
+ };
16074
+ }
16075
+ if (referent.kind === "list" && referent.listName) {
16076
+ return {
16077
+ kind: "list",
16078
+ role: referent.role || null,
16079
+ source: referent.source || null,
16080
+ name: referent.listName,
16081
+ type: referent.className || "unknown",
16082
+ count: typeof referent.count === "number" ? referent.count : null
16083
+ };
16084
+ }
16085
+ if (referent.kind === "variable" && referent.variableName) {
16086
+ return {
16087
+ kind: "variable",
16088
+ role: referent.role || null,
16089
+ source: referent.source || null,
16090
+ name: referent.variableName,
16091
+ valueKind: referent.variableKind || null,
16092
+ type: referent.className || null,
16093
+ path: referent.entryPath || null,
16094
+ list: referent.listName || null,
16095
+ label: referent.label || null,
16096
+ count: typeof referent.count === "number" ? referent.count : null,
16097
+ value: referent.variableKind === "scalar" ? referent.scalarValue ?? null : void 0
16098
+ };
16099
+ }
16100
+ return null;
16101
+ }).filter(Boolean);
16102
+ return renderConstBlock("recentReferences", compact);
16103
+ }
15208
16104
  function getCurrentClosureId(liveDoc) {
15209
16105
  const loop = asRecord4(liveDoc?.loop);
15210
16106
  return typeof loop?.currentClosureId === "string" ? loop.currentClosureId : null;
@@ -15424,56 +16320,24 @@ function projectWorkflowFocus(liveDoc, pendingPrompts = [], options) {
15424
16320
  }
15425
16321
  function projectWorkflowSummary(liveDoc, pendingPrompts = [], options) {
15426
16322
  const focus = projectWorkflowFocus(liveDoc, pendingPrompts, options);
15427
- const lines = [];
15428
- lines.push("Workflow Boundary:");
15429
- if (focus.boundaryReason === "request_start") {
15430
- lines.push(
15431
- "- Start from work recorded after the current user request began."
15432
- );
15433
- } else if (focus.boundaryReason === "last_closed_loop" && focus.latestClosureId) {
15434
- lines.push(`- Start from work recorded after ${focus.latestClosureId}.`);
15435
- } else {
15436
- lines.push(
15437
- "- No prior closed loop recorded; use the latest user request as the boundary."
15438
- );
15439
- }
15440
- lines.push("", "Recent Actions:");
15441
- if (focus.recentActionSummary.length === 0) {
15442
- lines.push("- none");
15443
- } else {
15444
- for (const line of focus.recentActionSummary) {
15445
- lines.push(line.startsWith("- ") ? line : `- ${line}`);
15446
- }
15447
- }
15448
- lines.push("", "Working Set Hints:");
15449
- if (focus.variableNames.length === 0 && focus.listNames.length === 0 && focus.entryPaths.length === 0) {
15450
- lines.push("- none");
15451
- } else {
15452
- if (focus.variableNames.length > 0) {
15453
- lines.push(`- variables: ${focus.variableNames.join(", ")}`);
15454
- }
15455
- if (focus.listNames.length > 0) {
15456
- lines.push(`- lists: ${focus.listNames.join(", ")}`);
15457
- }
15458
- if (focus.entryPaths.length > 0) {
15459
- lines.push(`- entries: ${focus.entryPaths.join(", ")}`);
15460
- }
15461
- }
15462
- lines.push("", "Open Workflow Handles:");
15463
- if (focus.activeTaskIds.length === 0 && focus.openDecisionIds.length === 0 && focus.openPromptIds.length === 0) {
15464
- lines.push("- none");
15465
- } else {
15466
- if (focus.activeTaskIds.length > 0) {
15467
- lines.push(`- tasks: ${focus.activeTaskIds.join(", ")}`);
15468
- }
15469
- if (focus.openDecisionIds.length > 0) {
15470
- lines.push(`- decisions: ${focus.openDecisionIds.join(", ")}`);
15471
- }
15472
- if (focus.openPromptIds.length > 0) {
15473
- lines.push(`- prompts: ${focus.openPromptIds.join(", ")}`);
16323
+ return renderConstBlock("workflowContext", {
16324
+ boundary: {
16325
+ timestamp: focus.boundaryTimestamp,
16326
+ reason: focus.boundaryReason,
16327
+ latestClosureId: focus.latestClosureId || null
16328
+ },
16329
+ recentActions: focus.recentActionSummary,
16330
+ workingSet: {
16331
+ variables: focus.variableNames,
16332
+ lists: focus.listNames,
16333
+ entries: focus.entryPaths
16334
+ },
16335
+ openHandles: {
16336
+ tasks: focus.activeTaskIds,
16337
+ decisions: focus.openDecisionIds,
16338
+ prompts: focus.openPromptIds
15474
16339
  }
15475
- }
15476
- return lines.join("\n");
16340
+ });
15477
16341
  }
15478
16342
  function hasOpenPrompt(liveDoc, pendingPrompts) {
15479
16343
  if (pendingPrompts.length > 0) return true;
@@ -15489,7 +16353,6 @@ function hasOpenPrompt(liveDoc, pendingPrompts) {
15489
16353
  return false;
15490
16354
  }
15491
16355
  function projectLoopSummary(liveDoc, pendingPrompts = [], options) {
15492
- const lines = [];
15493
16356
  const loop = asRecord4(liveDoc?.loop);
15494
16357
  const boundary = getWorkflowBoundary(liveDoc, options);
15495
16358
  const tasks = toSortedRecords(loop?.tasksById).filter((task) => {
@@ -15511,22 +16374,12 @@ function projectLoopSummary(liveDoc, pendingPrompts = [], options) {
15511
16374
  5
15512
16375
  );
15513
16376
  const hiddenTaskCount = Math.max(0, activeTasks.length - visibleTasks.length);
15514
- lines.push("Tasks:");
15515
- if (visibleTasks.length === 0) {
15516
- lines.push("- none");
15517
- } else {
15518
- lines.push("- Reuse existing taskId values exactly as written below.");
15519
- for (const task of visibleTasks) {
15520
- const title = typeof task.title === "string" ? task.title : "Untitled task";
15521
- const taskId = typeof task.taskId === "string" ? task.taskId : "unknown";
15522
- const status = typeof task.status === "string" ? task.status : "pending";
15523
- const summary = typeof task.summary === "string" && task.summary.trim() ? ` \u2014 ${task.summary.trim()}` : "";
15524
- lines.push(`- [${status}] ${title} (${taskId})${summary}`);
15525
- }
15526
- if (hiddenTaskCount > 0) {
15527
- lines.push(`- ${hiddenTaskCount} more active task(s) omitted`);
15528
- }
15529
- }
16377
+ const compactTasks = visibleTasks.map((task) => ({
16378
+ id: typeof task.taskId === "string" ? task.taskId : "unknown",
16379
+ title: typeof task.title === "string" ? task.title : "Untitled task",
16380
+ status: typeof task.status === "string" ? task.status : "pending",
16381
+ summary: typeof task.summary === "string" && task.summary.trim() ? task.summary.trim() : null
16382
+ }));
15530
16383
  const decisions = toSortedRecords(loop?.decisionsById).filter((decision) => {
15531
16384
  const updatedAt = Number(decision.updatedAt) || Number(decision.createdAt) || 0;
15532
16385
  if (boundary.reason === "request_start") {
@@ -15540,33 +16393,29 @@ function projectLoopSummary(liveDoc, pendingPrompts = [], options) {
15540
16393
  (decision) => decision.status === "open"
15541
16394
  );
15542
16395
  const visibleDecisions = (openDecisions.length > 0 ? openDecisions : decisions.slice(0, 1)).slice(0, 3);
15543
- lines.push("", "Recent Decisions:");
15544
- if (visibleDecisions.length === 0) {
15545
- lines.push("- none");
15546
- } else {
15547
- lines.push("- Reuse existing decisionId values exactly as written below.");
15548
- for (const decision of visibleDecisions) {
15549
- const status = typeof decision.status === "string" ? decision.status : "resolved";
15550
- const title = typeof decision.title === "string" && decision.title.trim() ? decision.title.trim() : "Decision";
15551
- const decisionId = typeof decision.decisionId === "string" ? decision.decisionId : "unknown";
15552
- if (status === "open") {
15553
- const candidatePreview = asArray2(decision.candidates).slice(0, 3).map((candidate) => {
15554
- const record = asRecord4(candidate);
15555
- if (!record) return null;
15556
- const candidateId = typeof record.id === "string" ? record.id : "unknown";
15557
- const candidateLabel = typeof record.label === "string" && record.label.trim() ? record.label.trim() : candidateId;
15558
- return candidateLabel === candidateId ? candidateId : `${candidateLabel} (${candidateId})`;
15559
- }).filter((value) => Boolean(value)).join(", ");
15560
- lines.push(
15561
- `- [open] ${title} (${decisionId})${candidatePreview ? ` \u2014 candidates: ${candidatePreview}` : ""}`
15562
- );
15563
- } else {
15564
- const selected = asRecord4(decision.selected);
15565
- const label = typeof selected?.label === "string" ? selected.label : typeof selected?.id === "string" ? selected.id : "unknown";
15566
- lines.push(`- [resolved] ${title} (${decisionId}) -> ${label}`);
16396
+ const compactDecisions = visibleDecisions.map((decision) => {
16397
+ const status = typeof decision.status === "string" ? decision.status : "resolved";
16398
+ const selected = asRecord4(decision.selected);
16399
+ return {
16400
+ id: typeof decision.decisionId === "string" ? decision.decisionId : "unknown",
16401
+ title: typeof decision.title === "string" && decision.title.trim() ? decision.title.trim() : "Decision",
16402
+ status,
16403
+ candidates: status === "open" ? asArray2(decision.candidates).slice(0, 5).map((candidate) => {
16404
+ const record = asRecord4(candidate);
16405
+ if (!record) return null;
16406
+ return {
16407
+ id: typeof record.id === "string" ? record.id : "unknown",
16408
+ label: typeof record.label === "string" && record.label.trim() ? record.label.trim() : null,
16409
+ description: typeof record.description === "string" && record.description.trim() ? record.description.trim() : null,
16410
+ metadata: asRecord4(record.metadata)
16411
+ };
16412
+ }).filter(Boolean) : [],
16413
+ selected: status === "open" ? null : {
16414
+ id: typeof selected?.id === "string" ? selected.id : null,
16415
+ label: typeof selected?.label === "string" ? selected.label : null
15567
16416
  }
15568
- }
15569
- }
16417
+ };
16418
+ });
15570
16419
  const openPrompts = [
15571
16420
  ...pendingPrompts.map((prompt) => ({
15572
16421
  id: prompt.id,
@@ -15586,29 +16435,29 @@ function projectLoopSummary(liveDoc, pendingPrompts = [], options) {
15586
16435
  (pendingPrompt) => pendingPrompt.id === promptId
15587
16436
  ) : false);
15588
16437
  }) : openPrompts;
15589
- lines.push("", "Open Prompts:");
15590
- if (visiblePrompts.length === 0) {
15591
- lines.push("- none");
15592
- } else {
15593
- for (const prompt of visiblePrompts.slice(0, 3)) {
15594
- const title = typeof prompt.title === "string" && prompt.title.trim() ? prompt.title.trim() : "Input required";
15595
- const type = typeof prompt.type === "string" ? prompt.type : "input";
15596
- const message = typeof prompt.message === "string" && prompt.message.trim() ? ` \u2014 ${prompt.message.trim()}` : "";
15597
- lines.push(`- [${type}] ${title}${message}`);
15598
- }
15599
- }
16438
+ const compactPrompts = visiblePrompts.slice(0, 3).map((prompt) => {
16439
+ const promptRecord = asRecord4(prompt) || {};
16440
+ return {
16441
+ id: typeof promptRecord.id === "string" ? promptRecord.id : typeof promptRecord.promptId === "string" ? promptRecord.promptId : null,
16442
+ type: typeof promptRecord.type === "string" ? promptRecord.type : "input",
16443
+ title: typeof promptRecord.title === "string" && promptRecord.title.trim() ? promptRecord.title.trim() : "Input required",
16444
+ message: typeof promptRecord.message === "string" && promptRecord.message.trim() ? promptRecord.message.trim() : null
16445
+ };
16446
+ });
15600
16447
  const currentClosureId = getCurrentClosureId(liveDoc);
15601
16448
  const closureRecord = currentClosureId ? asRecord4(asRecord4(loop?.closuresById)?.[currentClosureId]) : null;
15602
16449
  const visibleClosure = closureRecord && (boundary.reason !== "request_start" || (Number(closureRecord.createdAt) || 0) >= boundary.timestamp) ? closureRecord : null;
15603
- lines.push("", "Loop Closure:");
15604
- if (visibleClosure) {
15605
- const status = typeof visibleClosure.status === "string" ? visibleClosure.status : "completed";
15606
- const summary = typeof visibleClosure.summary === "string" ? visibleClosure.summary : "No summary";
15607
- lines.push(`- current: [${status}] ${summary} (${currentClosureId})`);
15608
- } else {
15609
- lines.push("- none");
15610
- }
15611
- return lines.join("\n");
16450
+ return renderConstBlock("workflowState", {
16451
+ tasks: compactTasks,
16452
+ hiddenActiveTaskCount: hiddenTaskCount,
16453
+ decisions: compactDecisions,
16454
+ openPrompts: compactPrompts,
16455
+ closure: visibleClosure ? {
16456
+ id: currentClosureId,
16457
+ status: typeof visibleClosure.status === "string" ? visibleClosure.status : "completed",
16458
+ summary: typeof visibleClosure.summary === "string" ? visibleClosure.summary : null
16459
+ } : null
16460
+ });
15612
16461
  }
15613
16462
  function projectHeapSummary(heap, options) {
15614
16463
  const heapRecord = asRecord4(heap) || {};
@@ -15653,55 +16502,72 @@ function projectHeapSummary(heap, options) {
15653
16502
  referencedPaths.add(path2);
15654
16503
  }
15655
16504
  const visibleLists = Object.values(listsByName).map((value) => asRecord4(value)).filter((value) => Boolean(value)).filter(
15656
- (list) => variables.some((variable) => variable.listName === list.name) || Boolean(list.name && focusedListNames.has(list.name))
16505
+ (list) => variables.some(
16506
+ (variable) => Boolean(variable?.listName === list.name)
16507
+ ) || Boolean(list.name && focusedListNames.has(list.name))
15657
16508
  ).sort((left, right) => (right.updatedAt || 0) - (left.updatedAt || 0)).slice(0, maxLists);
15658
16509
  const visibleEntries = Object.values(entriesByPath).map((value) => asRecord4(value)).filter((value) => Boolean(value)).filter((entry) => entry.path && referencedPaths.has(entry.path)).sort((left, right) => (right.updatedAt || 0) - (left.updatedAt || 0)).slice(0, maxEntries);
15659
- const lines = [];
15660
- lines.push("Variables:");
15661
- if (variables.length === 0) {
15662
- lines.push("- none");
15663
- } else {
15664
- for (const variable of variables) {
15665
- if (variable.kind === "scalar") {
15666
- lines.push(
15667
- `- ${variable.name}: scalar = ${formatScalar(variable.value)}`
15668
- );
15669
- continue;
15670
- }
15671
- if (variable.kind === "entry") {
15672
- const entry = variable.entryPath ? asRecord4(
15673
- entriesByPath[variable.entryPath]
15674
- ) : null;
15675
- lines.push(
15676
- `- ${variable.name}: entry<${variable.className || entry?.className || "unknown"}> -> ${entry ? describeHeapEntry(entry) : variable.entryPath || "missing"}`
15677
- );
15678
- continue;
15679
- }
15680
- const list = variable.listName ? asRecord4(listsByName[variable.listName]) : null;
15681
- lines.push(
15682
- `- ${variable.name}: list<${variable.className || list?.className || "unknown"}> -> ${(list?.paths || []).length} item(s)`
15683
- );
15684
- }
15685
- }
15686
- lines.push("", "Named Lists:");
15687
- if (visibleLists.length === 0) {
15688
- lines.push("- none");
15689
- } else {
15690
- for (const list of visibleLists) {
15691
- lines.push(
15692
- `- ${list.name}: ${list.className || "unknown"}[${(list.paths || []).length}]`
15693
- );
15694
- }
15695
- }
15696
- lines.push("", "Active Entries:");
15697
- if (visibleEntries.length === 0) {
15698
- lines.push("- none");
15699
- } else {
15700
- for (const entry of visibleEntries) {
15701
- lines.push(`- ${describeHeapEntry(entry)}`);
15702
- }
15703
- }
15704
- return lines.join("\n");
16510
+ return renderConstBlock("savedData", {
16511
+ variables: Object.fromEntries(
16512
+ variables.filter((variable) => typeof variable.name === "string").map((variable) => {
16513
+ if (variable.kind === "scalar") {
16514
+ return [
16515
+ variable.name,
16516
+ { kind: "scalar", value: variable.value ?? null }
16517
+ ];
16518
+ }
16519
+ if (variable.kind === "entry") {
16520
+ const entry = variable.entryPath ? asRecord4(
16521
+ entriesByPath[variable.entryPath]
16522
+ ) : null;
16523
+ return [
16524
+ variable.name,
16525
+ {
16526
+ kind: "entry",
16527
+ type: variable.className || entry?.className || "unknown",
16528
+ path: variable.entryPath || null,
16529
+ label: entry?.label || entry?.id || null
16530
+ }
16531
+ ];
16532
+ }
16533
+ const list = variable.listName ? asRecord4(listsByName[variable.listName]) : null;
16534
+ return [
16535
+ variable.name,
16536
+ {
16537
+ kind: "list",
16538
+ type: variable.className || list?.className || "unknown",
16539
+ list: variable.listName || null,
16540
+ count: (list?.paths || []).length
16541
+ }
16542
+ ];
16543
+ })
16544
+ ),
16545
+ lists: Object.fromEntries(
16546
+ visibleLists.filter((list) => typeof list.name === "string").map((list) => [
16547
+ list.name,
16548
+ {
16549
+ type: list.className || "unknown",
16550
+ count: (list.paths || []).length
16551
+ }
16552
+ ])
16553
+ ),
16554
+ entries: Object.fromEntries(
16555
+ visibleEntries.filter((entry) => typeof entry.path === "string").map((entry) => [
16556
+ entry.path,
16557
+ {
16558
+ type: entry.className || "unknown",
16559
+ id: entry.id || null,
16560
+ label: entry.label || entry.id || null,
16561
+ fields: asArray2(entry.fields).filter(
16562
+ (field) => field?.name && field.name !== "_realId" && field.name !== "real_id"
16563
+ ).slice(0, 3).map((field) => ({
16564
+ name: field.name,
16565
+ value: field.value ?? null
16566
+ }))
16567
+ }
16568
+ ])
16569
+ )
16570
+ });
15705
16571
  }
15706
16572
  function createHarnessVerifierSnapshot(input) {
15707
16573
  const workflowFocus = projectWorkflowFocus(
@@ -15798,8 +16664,8 @@ function buildContinuationInstruction(resultPreview) {
15798
16664
  "If the user names a concrete record that is not already in the heap, resolve it from the graph before saying it is missing: try a broad search, then a small set of normalized/fuzzy variants or a paged scan when the domain supports it.",
15799
16665
  "If the request needs all matching records, use iterate(...) or page until hasMore is false. A single list(...) or page(...) call is only one page.",
15800
16666
  "If this request clearly spans multiple steps and there are no active tasks yet, create 2-4 short user-visible tasks now.",
15801
- "Reuse any existing taskId and decisionId values exactly as they appear in AGENT LOOP STATE.",
15802
- "When progress depends on the user's choice, missing detail, or approval, use loop.ask_user(...) or loop.confirm(...) so the job pauses and resumes through the live workflow.",
16667
+ "Reuse any existing taskId and decisionId values exactly as they appear in [State].",
16668
+ "When progress depends on the user's choice, missing detail, or confirmation, use loop.ask_user(...) or loop.confirm(...) so the job pauses and resumes through the live workflow.",
15803
16669
  "After a resumed ask_user or confirm call, continue the same job and perform the newly authorized action when the answer is sufficient. Do not stop with placeholder text like 'I'm ready to do it next.'",
15804
16670
  "If you ask the user a new question in this job, do not also close the loop in the same job.",
15805
16671
  "Write the smallest straightforward code for the current step. Avoid defensive fallback branches for hypothetical states that are not currently true.",
@@ -15810,39 +16676,101 @@ ${resultPreview}` : null
15810
16676
  ].filter(Boolean).join("\n\n");
15811
16677
  }
15812
16678
  function buildGranularAgentDomainBlock(domainDocumentation) {
15813
- return domainDocumentation?.trim() || "No domain reference available. The graph may not be ready yet.";
16679
+ return domainDocumentation?.trim() || "No domain contract available. The graph may not be ready yet.";
15814
16680
  }
15815
16681
  function buildGranularAgentSessionBlock(sessionContext) {
15816
- if (!sessionContext) return "No session metadata available.";
15817
- const rows = [
15818
- ["sandboxId", sessionContext.sandboxId],
15819
- ["environmentId", sessionContext.environmentId],
15820
- ["userName", sessionContext.userName]
15821
- ];
15822
- const activeRows = rows.filter(([, value]) => Boolean(value));
15823
- if (activeRows.length === 0) return "No session metadata available.";
15824
- return activeRows.map(([key, value]) => `${key}: ${value}`).join("\n");
16682
+ return renderConstBlock("session", {
16683
+ runtimeId: sessionContext?.sandboxId || null,
16684
+ environmentId: sessionContext?.environmentId || null,
16685
+ userName: sessionContext?.userName || null,
16686
+ domainRevision: sessionContext?.domainRevision || null
16687
+ });
15825
16688
  }
15826
16689
  function buildGranularAgentHeapBlock(heapSummary) {
15827
- return heapSummary?.trim() || "Heap is empty for this session.";
16690
+ return heapSummary?.trim() || renderConstBlock("savedData", {
16691
+ variables: {},
16692
+ lists: {},
16693
+ entries: {}
16694
+ });
15828
16695
  }
15829
16696
  function buildGranularAgentReferentBlock(referentSummary) {
15830
- return referentSummary?.trim() || "No recent referents recorded from prior assistant replies.";
16697
+ return referentSummary?.trim() || renderConstBlock("recentReferences", []);
15831
16698
  }
15832
16699
  function buildGranularAgentLoopBlock(loopSummary) {
15833
- return loopSummary?.trim() || "No active loop state recorded for this session.";
16700
+ return loopSummary?.trim() || renderConstBlock("workflowState", {
16701
+ tasks: [],
16702
+ decisions: [],
16703
+ openPrompts: [],
16704
+ closure: null
16705
+ });
15834
16706
  }
15835
16707
  function buildGranularAgentWorkflowBlock(workflowSummary) {
15836
- return workflowSummary?.trim() || "No current workflow snapshot recorded for this request yet.";
16708
+ return workflowSummary?.trim() || renderConstBlock("workflowContext", {
16709
+ boundary: null,
16710
+ recentActions: [],
16711
+ workingSet: {
16712
+ variables: [],
16713
+ lists: [],
16714
+ entries: []
16715
+ },
16716
+ openHandles: {
16717
+ tasks: [],
16718
+ decisions: [],
16719
+ prompts: []
16720
+ }
16721
+ });
15837
16722
  }
15838
- function buildGranularAgentToolBlock(tools) {
16723
+ function resolvePromptCapabilities(capabilities) {
16724
+ return {
16725
+ executeCode: capabilities?.executeCode !== false,
16726
+ readEntities: capabilities?.readEntities !== false,
16727
+ workflowHelpers: Array.isArray(capabilities?.workflowHelpers) ? capabilities.workflowHelpers : [
16728
+ "ask_user",
16729
+ "confirm",
16730
+ "open_decision",
16731
+ "close_decision",
16732
+ "create_task",
16733
+ "update_task",
16734
+ "complete_task",
16735
+ "close_loop"
16736
+ ],
16737
+ savedData: capabilities?.savedData !== false,
16738
+ showRecords: capabilities?.showRecords !== false
16739
+ };
16740
+ }
16741
+ function buildGranularAgentToolBlock(tools, capabilityOverrides) {
16742
+ const resolvedCapabilities = resolvePromptCapabilities(capabilityOverrides);
16743
+ const normalizedTools = (tools || []).filter((tool) => tool?.name).slice().sort((left, right) => {
16744
+ const leftScope = `${left.className || "global"}:${left.static ? "static" : "instance"}`;
16745
+ const rightScope = `${right.className || "global"}:${right.static ? "static" : "instance"}`;
16746
+ return leftScope.localeCompare(rightScope) || left.name.localeCompare(right.name);
16747
+ });
16748
+ const writeActions = normalizedTools.filter((tool) => tool.ready !== false).map((tool) => {
16749
+ const scope = tool.className ? `${tool.static ? "class" : "record"}:${tool.className}` : "global";
16750
+ return {
16751
+ name: tool.name,
16752
+ scope,
16753
+ description: tool.description?.trim() || null
16754
+ };
16755
+ });
16756
+ const capabilities = {
16757
+ executeCode: resolvedCapabilities.executeCode,
16758
+ readEntities: resolvedCapabilities.readEntities,
16759
+ writeActions,
16760
+ workflowHelpers: resolvedCapabilities.workflowHelpers,
16761
+ savedData: resolvedCapabilities.savedData,
16762
+ showRecords: resolvedCapabilities.showRecords
16763
+ };
16764
+ return renderConstBlock("capabilities", capabilities);
16765
+ }
16766
+ function buildGranularAgentActionIndex(tools) {
15839
16767
  const normalizedTools = (tools || []).filter((tool) => tool?.name).slice().sort((left, right) => {
15840
16768
  const leftScope = `${left.className || "global"}:${left.static ? "static" : "instance"}`;
15841
16769
  const rightScope = `${right.className || "global"}:${right.static ? "static" : "instance"}`;
15842
16770
  return leftScope.localeCompare(rightScope) || left.name.localeCompare(right.name);
15843
16771
  });
15844
16772
  if (normalizedTools.length === 0) {
15845
- return "No live effects are available in this session yet.";
16773
+ return "No domain write actions are available.";
15846
16774
  }
15847
16775
  const globalTools = normalizedTools.filter((tool) => !tool.className);
15848
16776
  const staticTools = normalizedTools.filter(
@@ -15851,9 +16779,7 @@ function buildGranularAgentToolBlock(tools) {
15851
16779
  const instanceTools = normalizedTools.filter(
15852
16780
  (tool) => Boolean(tool.className && !tool.static)
15853
16781
  );
15854
- const lines = [
15855
- "Treat this block as the planning map. Use DOMAIN REFERENCE below for exact signatures and query examples."
15856
- ];
16782
+ const lines = ["Available actions by scope:"];
15857
16783
  const appendGroup = (title, group) => {
15858
16784
  lines.push(`- ${title}:`);
15859
16785
  if (group.length === 0) {
@@ -15862,187 +16788,466 @@ function buildGranularAgentToolBlock(tools) {
15862
16788
  }
15863
16789
  for (const tool of group.slice(0, 10)) {
15864
16790
  const availability = tool.ready === false ? " [not ready]" : "";
16791
+ const schema = formatActionSchemaSummary(tool);
15865
16792
  const description = tool.description?.trim() ? ` - ${tool.description.trim()}` : "";
15866
- lines.push(` ${tool.name}${availability}${description}`);
16793
+ lines.push(` ${tool.name}${availability}${schema}${description}`);
15867
16794
  }
15868
16795
  if (group.length > 10) {
15869
16796
  lines.push(` +${group.length - 10} more`);
15870
16797
  }
15871
16798
  };
15872
- appendGroup("Global effects", globalTools);
15873
- appendGroup("Class-level effects", staticTools);
15874
- appendGroup("Record-level effects", instanceTools);
16799
+ appendGroup("Global", globalTools);
16800
+ appendGroup("Class-level", staticTools);
16801
+ appendGroup("Record-level", instanceTools);
15875
16802
  return lines.join("\n");
15876
16803
  }
15877
- function buildGranularAgentCheckpointBlock(checkpoint) {
15878
- if (!checkpoint) {
15879
- return "No previous execution checkpoint recorded for this request yet.";
15880
- }
15881
- const lines = [];
15882
- if (typeof checkpoint.iteration === "number") {
15883
- lines.push(`iteration: ${checkpoint.iteration}`);
15884
- }
15885
- if (checkpoint.latestJobStatus) {
15886
- lines.push(`latestJobStatus: ${checkpoint.latestJobStatus}`);
15887
- }
15888
- if (checkpoint.controllerOutcome) {
15889
- lines.push(`controllerOutcome: ${checkpoint.controllerOutcome}`);
16804
+ function normalizeJsonSchema(value) {
16805
+ if (typeof value === "string") {
16806
+ try {
16807
+ return asRecord4(JSON.parse(value));
16808
+ } catch {
16809
+ return null;
16810
+ }
15890
16811
  }
15891
- if (checkpoint.controllerReason) {
15892
- lines.push(`controllerReason: ${checkpoint.controllerReason}`);
16812
+ return asRecord4(value);
16813
+ }
16814
+ function jsonSchemaTypeName(schema) {
16815
+ const record = normalizeJsonSchema(schema);
16816
+ if (!record) return "unknown";
16817
+ const type = record.type;
16818
+ if (typeof type === "string") {
16819
+ if (type === "array") return "array";
16820
+ if (type === "object") return "object";
16821
+ return type;
15893
16822
  }
15894
- if (typeof checkpoint.noProgressCount === "number") {
15895
- lines.push(`noProgressCount: ${checkpoint.noProgressCount}`);
16823
+ return "unknown";
16824
+ }
16825
+ function summarizeObjectSchema(schema) {
16826
+ const record = normalizeJsonSchema(schema);
16827
+ const properties = asRecord4(record?.properties);
16828
+ if (!properties || Object.keys(properties).length === 0) {
16829
+ return record ? "{}" : null;
16830
+ }
16831
+ const required = new Set(asArray2(record?.required));
16832
+ const fields = Object.entries(properties).slice(0, 8).map(([name, property]) => {
16833
+ const marker = required.has(name) ? "*" : "?";
16834
+ return `${name}${marker}: ${jsonSchemaTypeName(property)}`;
16835
+ });
16836
+ const remaining = Object.keys(properties).length - fields.length;
16837
+ return remaining > 0 ? `${fields.join(", ")}, +${remaining}` : fields.join(", ");
16838
+ }
16839
+ function formatActionSchemaSummary(tool) {
16840
+ const input = summarizeObjectSchema(tool.inputSchema);
16841
+ const output = summarizeObjectSchema(tool.outputSchema);
16842
+ const parts = [];
16843
+ if (input) parts.push(`input { ${input} }`);
16844
+ if (output) parts.push(`output { ${output} }`);
16845
+ return parts.length ? ` (${parts.join("; ")})` : "";
16846
+ }
16847
+ function splitDomainDocumentation(domainDocumentation) {
16848
+ const normalized = domainDocumentation?.trim() || "";
16849
+ if (!normalized) return { types: "", docs: "" };
16850
+ const docsSectionMatch = normalized.match(/\n\s*\[Docs\]\s*\n/i);
16851
+ if (docsSectionMatch?.index !== void 0) {
16852
+ return {
16853
+ types: normalized.slice(0, docsSectionMatch.index).trim(),
16854
+ docs: normalized.slice(docsSectionMatch.index + docsSectionMatch[0].length).trim()
16855
+ };
15896
16856
  }
15897
- if (checkpoint.latestJobError?.trim()) {
15898
- lines.push(`latestJobError: ${checkpoint.latestJobError.trim()}`);
16857
+ const legacyMarker = "Generated usage notes from ./sandbox-tools docs:";
16858
+ const legacyIndex = normalized.indexOf(legacyMarker);
16859
+ if (legacyIndex !== -1) {
16860
+ return {
16861
+ types: normalized.slice(0, legacyIndex).trim(),
16862
+ docs: normalized.slice(legacyIndex + legacyMarker.length).trim()
16863
+ };
15899
16864
  }
15900
- if (Array.isArray(checkpoint.latestActionSummary) && checkpoint.latestActionSummary.length > 0) {
15901
- lines.push("latestActionSummary:");
15902
- for (const line of checkpoint.latestActionSummary.slice(0, 8)) {
15903
- const normalizedLine = normalizeActionSummaryForPrompt(line);
15904
- lines.push(
15905
- normalizedLine.startsWith("- ") ? normalizedLine : `- ${normalizedLine}`
15906
- );
16865
+ return { types: normalized, docs: "" };
16866
+ }
16867
+ function buildGranularAgentCheckpointBlock(checkpoint) {
16868
+ if (!checkpoint) {
16869
+ return renderConstBlock("previousCodeResult", null);
16870
+ }
16871
+ return renderConstBlock("previousCodeResult", {
16872
+ iteration: typeof checkpoint.iteration === "number" ? checkpoint.iteration : null,
16873
+ latestJobStatus: checkpoint.latestJobStatus || null,
16874
+ controllerOutcome: checkpoint.controllerOutcome || null,
16875
+ controllerReason: checkpoint.controllerReason || null,
16876
+ noProgressCount: typeof checkpoint.noProgressCount === "number" ? checkpoint.noProgressCount : null,
16877
+ latestJobError: checkpoint.latestJobError?.trim() || null,
16878
+ latestActionSummary: Array.isArray(checkpoint.latestActionSummary) ? checkpoint.latestActionSummary.slice(0, 8).map(normalizeActionSummaryForPrompt) : [],
16879
+ latestJobResult: checkpoint.latestJobResult?.trim() || null
16880
+ });
16881
+ }
16882
+ function parseSummaryOutcome(summary) {
16883
+ const outcome = {};
16884
+ for (const part of summary.split(",")) {
16885
+ const trimmed = part.trim();
16886
+ const match = /^([A-Za-z0-9_]+)=(.+)$/.exec(trimmed);
16887
+ if (!match) continue;
16888
+ const [, key, rawValue] = match;
16889
+ const unquoted = rawValue.replace(/^"|"$/g, "");
16890
+ if (/^-?\d+(?:\.\d+)?$/.test(unquoted)) {
16891
+ outcome[key] = Number(unquoted);
16892
+ } else if (unquoted === "true" || unquoted === "false") {
16893
+ outcome[key] = unquoted === "true";
16894
+ } else {
16895
+ outcome[key] = unquoted;
15907
16896
  }
15908
16897
  }
15909
- if (checkpoint.latestJobResult?.trim()) {
15910
- lines.push(`latestJobResult:
15911
- ${checkpoint.latestJobResult.trim()}`);
16898
+ return outcome;
16899
+ }
16900
+ function buildKnownFactsFromCheckpoint(checkpoint) {
16901
+ const summaries = Array.isArray(checkpoint?.latestActionSummary) ? checkpoint.latestActionSummary.map(normalizeActionSummaryForPrompt) : [];
16902
+ const facts = [];
16903
+ for (const summary of summaries) {
16904
+ const countedMatch = /^-\s*Counted\s+([A-Za-z0-9_]+).*?->\s*value=(\d+)/.exec(summary);
16905
+ if (countedMatch) {
16906
+ facts.push({
16907
+ entity: countedMatch[1],
16908
+ query: {},
16909
+ totalCount: Number(countedMatch[2])
16910
+ });
16911
+ continue;
16912
+ }
16913
+ const listedMatch = /^-\s*Listed\s+([A-Za-z0-9_]+).*?->\s*(.+)$/.exec(
16914
+ summary
16915
+ );
16916
+ if (!listedMatch) continue;
16917
+ const outcome = parseSummaryOutcome(listedMatch[2]);
16918
+ const count = typeof outcome.totalCount === "number" ? outcome.totalCount : typeof outcome.count === "number" ? outcome.count : void 0;
16919
+ if (typeof count !== "number") continue;
16920
+ const fact = {
16921
+ entity: listedMatch[1],
16922
+ query: {},
16923
+ totalCount: count
16924
+ };
16925
+ if (typeof outcome.hasMore === "boolean") {
16926
+ fact.lastPageHasMore = outcome.hasMore;
16927
+ fact.loadedAllItems = !outcome.hasMore;
16928
+ } else if (typeof outcome.count === "number" && outcome.count === count) {
16929
+ fact.loadedAllItems = true;
16930
+ }
16931
+ facts.push(fact);
15912
16932
  }
15913
- return lines.length > 0 ? lines.join("\n") : "No previous execution checkpoint recorded for this request yet.";
16933
+ return facts.slice(0, 8);
15914
16934
  }
15915
16935
  function buildGranularAgentSystemPrompt(input) {
16936
+ const outputMode = input.outputMode || "agentMessages";
16937
+ const promptCapabilities = resolvePromptCapabilities(input.capabilities);
16938
+ const domainSections = splitDomainDocumentation(input.domainDocumentation);
15916
16939
  const sessionBlock = buildGranularAgentSessionBlock(input.sessionContext);
15917
- const toolBlock = buildGranularAgentToolBlock(input.tools);
15918
- const domainBlock = buildGranularAgentDomainBlock(input.domainDocumentation);
16940
+ const toolBlock = buildGranularAgentToolBlock(
16941
+ input.tools,
16942
+ input.capabilities
16943
+ );
16944
+ const actionIndex = buildGranularAgentActionIndex(input.tools);
16945
+ const domainBlock = buildGranularAgentDomainBlock(domainSections.types);
15919
16946
  const workflowBlock = buildGranularAgentWorkflowBlock(input.workflowSummary);
15920
16947
  const checkpointBlock = buildGranularAgentCheckpointBlock(input.checkpoint);
15921
16948
  const heapBlock = buildGranularAgentHeapBlock(input.heapSummary);
15922
16949
  const referentBlock = buildGranularAgentReferentBlock(input.referentSummary);
15923
16950
  const loopBlock = buildGranularAgentLoopBlock(input.loopSummary);
15924
- return `You are an AI assistant for a live Granular session.
15925
- You can help the user understand the domain, answer questions, or generate and execute code against the live session.
15926
- Your tone must be natural and human-like.
16951
+ const knownFactsBlock = renderConstBlock(
16952
+ "knownFacts",
16953
+ buildKnownFactsFromCheckpoint(input.checkpoint)
16954
+ );
16955
+ const outputRules = outputMode === "returnValue" ? promptCapabilities.showRecords ? `- End every user-facing job by returning either a short natural-language string or an object like \`{ reply, show }\`.
16956
+ - Use \`{ reply, show }\` when the host UI should render records, heap variables, or lists from session state.
16957
+ - For multi-record display, prefer a saved list/listName so the UI can render a table; use entryPaths for a few individual records.
16958
+ - When the user asks to show, list, display, open, or "show them" for records you found, include those heap-backed records in \`show\`; do not answer only with a count or text summary.
16959
+ - For count-only questions such as "how many", "how many X do I have", or "what is the total number of X", call the entity \`.count(...)\` or use page \`totalCount\` only when a page is already needed for other reasons. Answer with text only. Do not fetch, save, or display sample records just to ground a numeric count.
16960
+ - Do not call \`agent_text_message(...)\` or \`agent_heap_objects(...)\` unless the host explicitly opts into those side-channel message helpers.` : `- End every user-facing job by returning a short natural-language string.` : promptCapabilities.showRecords ? `- Every job that answers the user must emit \`agent_text_message(...)\` and/or \`agent_heap_objects(...)\`.
16961
+ - \`agent_text_message(...)\` displays text directly to the user in the host UI. Treat it as the user-facing progress and reply channel, not as a debug log.
16962
+ - For long-running or multi-step jobs, send several short \`agent_text_message(...)\` updates as useful milestones are reached so the user can see what is happening instead of waiting in silence.
16963
+ - Write \`agent_text_message(...)\` content in a friendly, readable product-assistant style: concrete, concise, and natural. Avoid robotic status dumps, raw implementation names, and unexplained IDs unless the ID helps the user.
16964
+ - When \`agent_text_message(...)\` mentions a grounded record that should remain clickable/referable, wrap only the visible record label in a self-closing inline reference tag: \`<granular-object class="class_name" id="stable_id_or_path" label="Visible label" />\`. Use the actual class name and stable id/path from the runtime record or effect result; do not invent ids, field names, or snake/camel-case aliases that are not present in the type declarations or returned object.
16965
+ - Treat \`agent_heap_objects(...)\` as the UI display call for user-visible records, not as a general storage helper. Do not wrap records under an \`items\` key.
16966
+ - When records should remain reusable for follow-ups, first save the runtime record or ordered record array with \`await heap.setVar("stable_selection_name", value)\`, then display that saved selection exactly once with \`await agent_heap_objects({ variableNames: ["stable_selection_name"] })\`.
16967
+ - \`heap.setVar(...)\` only accepts scalar values, runtime records/sandbox instances, or arrays of runtime records/sandbox instances. Do not save plain action/effect result objects. If an action returns an id/path for a created record that should remain referable, fetch the created record first, then save/display that fetched record.
16968
+ - Do not use \`agent_heap_objects({ entries: [...] })\` or \`agent_heap_objects({ saveAs, entries })\` as a shortcut for ordered pages, queues, search results, or ranked lists; those forms can create duplicate or poorly labelled displays. Save the selection with \`heap.setVar(...)\` and display it via \`variableNames\` instead.
16969
+ - Use \`entryPaths\` only for a few already-known individual records and \`listNames\` only for a host-created list that you intentionally want to show. Do not display both an entry/list selection and a heap variable for the same records.
16970
+ - When the user asks to show, list, display, open, or "show them" for records you found, call \`agent_heap_objects(...)\`; do not answer only with a count or text summary.
16971
+ - For count-only questions such as "how many", "how many X do I have", or "what is the total number of X", call the entity \`.count(...)\` or use page \`totalCount\` only when a page is already needed for other reasons. Answer with \`agent_text_message(...)\` only. Do not call \`agent_heap_objects(...)\`, \`saveAs\`, or \`heap.setVar(...)\` unless the user also asked to see records or a later requested action needs a reusable record selection.
16972
+ - Any job that identifies a specific record in the visible answer must also display that grounded record with \`agent_heap_objects(...)\` when the user should see/open it, or save it with \`heap.setVar(...)\` when it is only needed for follow-up resolution.
16973
+ - For ordered record slices, pages, queues, search results, or ranked lists, save the slice with \`heap.setVar(...)\` and then call \`agent_heap_objects({ variableNames: [...] })\` once. Use a stable name that preserves the slice identity and ordering so later references such as "the second item" or "back on the first slice" resolve to the correct earlier slice, not merely the most recent record.
16974
+ - Do not rely on the final return value for UI output. Do not return ad-hoc \`reply\` / \`show\` payloads instead of explicit agent message calls.` : `- Every job that answers the user must emit \`agent_text_message(...)\`.
16975
+ - \`agent_text_message(...)\` displays text directly to the user in the host UI. Treat it as the user-facing progress and reply channel, not as a debug log.
16976
+ - For long-running or multi-step jobs, send several short \`agent_text_message(...)\` updates as useful milestones are reached so the user can see what is happening instead of waiting in silence.
16977
+ - Write \`agent_text_message(...)\` content in a friendly, readable product-assistant style: concrete, concise, and natural. Avoid robotic status dumps, raw implementation names, and unexplained IDs unless the ID helps the user.
16978
+ - When \`agent_text_message(...)\` mentions a grounded record that should remain clickable/referable, wrap only the visible record label in a self-closing inline reference tag: \`<granular-object class="class_name" id="stable_id_or_path" label="Visible label" />\`. Use the actual class name and stable id/path from the runtime record or effect result; do not invent ids, field names, or snake/camel-case aliases that are not present in the type declarations or returned object.`;
16979
+ const codeRules = promptCapabilities.executeCode ? `Code:
16980
+ - Use when the request needs session data, saved data, workflow state, record display, or available actions.
16981
+ - When using code, assistant text must be empty or one brief summary.
16982
+ - Code must be plain runnable JavaScript with top-level await.
16983
+ - Import needed classes and helpers from "./sandbox-tools".
16984
+ - Use static top-level imports such as \`import { Foo, agent_text_message } from "./sandbox-tools";\`. Do not use dynamic \`await import("./sandbox-tools")\`.
16985
+ - Keep generated jobs as straightforward top-level scripts. Small local helper functions are allowed when they make the code clearer, but avoid hiding domain actions, prompts, or relationship traversal inside broad generic helpers.
16986
+ - Do not nest template literals: never put a backtick string inside another template string or inside a \`\${...}\` expression. Build conditional text in variables first, or use simple string concatenation. For multi-line replies, prefer a \`lines\` array and \`.join("\\n")\`.
16987
+ - Do not write an action branch that finds multiple candidates, emits a "please choose" message, and returns. When the current request asks for an action, the same branch must call \`await loop.ask_user(...)\`, resolve the answer, and continue to the requested action before the job finishes.
16988
+ - User-visible output must use the provided message or record-display helpers.
16989
+ - After calling an action or effect, inspect the returned object and base the user-facing answer on its actual fields.
16990
+ - When calling an action, use the exact input property names from the action schema. Do not invent synonym keys for required inputs.
16991
+ - After a mutation succeeds, ground the answer in the affected record by emitting or saving the record for UI display and naming a stable user-visible identifier when one exists. Do not answer only "done" or "sent".
16992
+ - Never call \`process.exit(...)\`; emit a message and use \`return;\` to stop early.
16993
+ - Add short \`//\` planning comments before meaningful blocks. The user will see these comments concatenated as a planning trace while the job is being drafted, so they should read together like a properly written plan.
16994
+ - In \`//\` planning comments, clearly explain the logic of what the job is about to do: the sequence of steps, why each step matters, and any important decision points or branches.
16995
+ - Write \`//\` planning comments for the user, not for engineers: make them friendly, plain-language, and easy to understand.
16996
+ - Keep \`//\` planning comments in future tense, but vary the phrasing so they do not become a repetitive list of sentences that all start the same way.
16997
+ - Make the \`//\` planning trace feel connected: use natural transitions for sequence, dependency, contrast, and branching when useful. If the next step depends on what the job finds, say that in plain language.
16998
+ - Avoid technical terms, implementation names, code concepts, hidden helper names, and complex domain jargon in \`//\` planning comments unless the user already used that wording.
16999
+ - Each \`//\` planning comment should provide valuable feedback about the plan or next visible step. Do not add filler such as "Starting", "Running", or "Processing".
17000
+ ${outputRules}` : `Code:
17001
+ - Code execution is unavailable. Use text only, or ask the user for missing information.`;
17002
+ const workflowRules = promptCapabilities.workflowHelpers.length > 0 ? `Workflow:
17003
+ - Use workflow helpers when missing input should pause and resume the workflow.
17004
+ - If code discovers missing required input after a read, use \`await loop.ask_user(...)\`; do not just tell the user to provide it.
17005
+ - Do not ask the user for data the job can discover from grounded records, relationships, saved session state, or visible read-only actions. Ask only when the missing value is truly unavailable, ambiguous, or requires a human decision.
17006
+ - When ambiguity blocks a requested action, import \`loop\` and use \`await loop.ask_user({ type: "choice", ... })\` with grounded options so the same job can resume and complete the action. A plain text request such as "please choose one" is not a workflow and leaves the action unhandled.
17007
+ - If a requested action has 2 to 5 plausible grounded targets, the job is not complete after showing them. Do not stop after \`agent_text_message(...)\` or \`agent_heap_objects(...)\`; import \`loop\`, ask for a grounded choice with \`await loop.ask_user(...)\`, then call the action on the selected record after the job resumes.
17008
+ - If a lookup before a mutation returns multiple plausible target records, do not mutate the first sorted or first returned record. Ask for a grounded choice unless the user supplied a unique identifier, ordinal, or selector that leaves exactly one target.
17009
+ - Use choice only for 2 to 5 short grounded options.
17010
+ - For record choices, set each option value to a stable scalar such as the record \`_graphPath\` or \`id\`, not a label-only value.
17011
+ - After \`await loop.ask_user(...)\` returns from a choice prompt, tolerate either the option value, the option object, or a human-readable label by matching against value, id/path, label, and description before failing. If a returned label is a prefix or substring of exactly one option label, treat it as that option.
17012
+ - Use \`loop.confirm(...)\` for yes/no confirmation only when the user explicitly asks for confirmation, action or permission metadata requires it, policy requires it, or material uncertainty remains after grounding.
17013
+ - Do not add a generic yes/no confirmation after the user has already made a grounded choice, unless one of those confirmation conditions still applies.
17014
+ - Do not add confirmation only because an allowed mutation is visible to other people, customer-facing, or consequential. If the user clearly requested the mutation and the grounded target, action, and condition are unique, perform the mutation unless confirmation is required by the user, policy, action metadata, or remaining material uncertainty.
17015
+ - A conditional request such as "if this is true, do that" is authorization to perform the requested action after you verify the condition. Once the condition, target, and action are grounded uniquely, call the action directly; do not ask "should I perform/post/send this?" unless the user, policy, action metadata, or unresolved material uncertainty requires confirmation. The visibility or impact of an allowed action is not by itself unresolved uncertainty.
17016
+ - If the user explicitly asks you to stop for confirmation, natural-language text such as "please confirm" is not enough: call \`await loop.confirm(...)\` before the mutation, then perform the approved mutation in the same resumed job when it returns true.
17017
+ - Reuse existing task, decision, and closure ids from [State].
17018
+ - If a user request matches both a domain record/action and a workflow helper, prefer the domain capability.` : "";
17019
+ return `[Harness]
17020
+ You are an assistant for a live user session. Use plain, natural language.
15927
17021
 
15928
- Call the \`execute_code\` effect ONLY when the user's intent matches the domain's capabilities and requires executing code against the live session. If the user is just asking a general question or if their request doesn't match the available effects or domain types, respond with text to explain.
15929
- When you call \`execute_code\`, additional assistant text must be either:
15930
- - empty, or
15931
- - a brief summary of the actions the generated code will perform.
15932
- Do not include any other kind of commentary when calling \`execute_code\`.
15933
- - If the next step needs to create or update workflow state in the live session, you must call \`execute_code\`. This includes \`loop.ask_user(...)\`, \`loop.confirm(...)\`, \`loop.open_decision(...)\`, \`loop.close_decision(...)\`, \`loop.create_task(...)\`, \`loop.update_task(...)\`, \`loop.complete_task(...)\`, and \`loop.close_loop(...)\`.
15934
- - If the next step is an interactive clarification that should be resumable in the live workflow, you must call \`execute_code\`. A missing preference, rule, metric, target, or option selection is not a plain-text reply when the answer should drive the next live step.
15935
- - If you can offer a short grounded shortlist, that clarification should usually be \`loop.ask_user({ type: 'choice', ... })\` instead of a plain-text question with bullet options.
15936
- - Never simulate a live prompt, confirmation, decision, task change, or loop closure in plain text. Plain-text replies are only for conversational answers that do not need to mutate session state.
17022
+ Mode selection:
17023
+ Text only:
17024
+ - Use for general explanations, unsupported requests, or requests that do not need session data.
17025
+ - Do not use text only when the user asks you to check, look up, search, inspect, update, schedule, or otherwise use session data or tools.
17026
+ - Do not answer with a promise like "I'll check" or "I'll do that next"; if the request needs tools, choose a job and run them now.
17027
+ - Do not expose internal names, helper names, file paths, parameter names, or code.
17028
+ - In code jobs, never use \`console.log(JSON.stringify({ action, reply, code }))\` as a user reply. Use the provided message helpers or final return contract.
15937
17029
 
15938
- \u2500\u2500\u2500 STREAMING COMMENT RULES \u2500\u2500\u2500
15939
- - While you are writing code, add short single-line comments with the prefix \`// \` before meaningful blocks.
15940
- - These comments should explain the intent in friendly product language, not in implementation jargon.
15941
- - Comments are shown live as a reasoning trace, so keep them brief, concrete, and useful.
15942
- - Do not mention method names, file paths, or internal identifiers in those comments.
15943
- - Use only single-line \`//\` comments for this purpose. Do not use block comments.
15944
- - If you are replying with text only, you may also include a few leading \`// \` comment lines before the final answer.
15945
- - End text-only replies with the plain user-facing answer on normal lines, without a comment prefix.
17030
+ ${codeRules}
15946
17031
 
15947
- \u2500\u2500\u2500 RESPONSE STYLE RULES \u2500\u2500\u2500
15948
- - Use plain, friendly product language.
15949
- - Never mention internal implementation details in user-facing text:
15950
- class names, effect names, method names, function names, file paths, parameter names, or code snippets.
15951
- - Never expose dotted identifiers such as \`Class.method\` in user-facing text.
15952
- - Do not say "sandbox" in user-facing text unless the user is explicitly asking about the runtime environment itself.
15953
- - If you need clarification, ask in everyday language.
15954
- - If the missing information should pause the live workflow for later continuation, ask through \`loop.ask_user(...)\` in generated code rather than with a plain-text question.
15955
- - If you are asking the user to pick from explicit options, prefer a live \`loop.ask_user({ type: 'choice', ... })\` prompt over a direct reply that lists those options in text.
15956
- - Keep replies concise and clear.
15957
- - This is a conversation UI, not an API console. Favor human answers over machine-shaped payloads.
17032
+ ${workflowRules}
15958
17033
 
15959
- \u2500\u2500\u2500 SESSION CONTEXT \u2500\u2500\u2500
15960
- ${sessionBlock}
17034
+ High-priority execution rules:
17035
+ - Treat a human reference as something to ground, not as missing data. When the user names or describes a record, group, queue, parent, relationship, or prior result and asks to inspect, decide, update, schedule, approve, send, or otherwise act on session data, run a code job to ground it before asking the user for more details.
17036
+ - For a human-described primary anchor, a no-match answer is only justified after more than one distinct grounding attempt, such as owner/container grounding, relationship traversal, exact id/path lookup, or shorter target-local search. Before the primary no-match return, retry that same anchor with fewer text constraints or a distinct grounding strategy; do not stop after one zero-result list/find/page call.
17037
+ - A confirmation requirement is not a reason to stay text-only. Do all safe read-only grounding and availability/status checks first, then call \`loop.confirm(...)\` or \`loop.ask_user(...)\` before the mutation.
17038
+ - In any code branch where a requested action or mutation has multiple possible targets, import \`loop\` statically and use \`await loop.ask_user(...)\` in that branch. This includes ambiguity discovered after a query returns several records. A branch that only shows candidates, asks in text, and returns leaves the requested action unfinished.
17039
+ - Before any mutation, know whether the target is one record or several. A singular phrase like "the item" is not proof of uniqueness after a query finds multiple matching records. If the user did not give an exact identifier or explicit selection criterion, call \`loop.ask_user({ type: "choice", ... })\` with grounded choices; do not choose by age, amount, priority, order, or convenience on your own. Resolve the target before any yes/no confirmation.
17040
+ - Treat partial names, first words, aliases, and shorthand labels as partial references. Use search/contains or grounded relationship traversal first; do not report no match after only an exact \`equal_to\` name filter.
17041
+ - When a partial name, alias, or shorthand resolves to a stored record, include that record's stored display value in the visible answer at least once. Prefer exact fields such as name, title, number, label, or other user-facing identifier over the user's shorthand.
17042
+ - If the user says the label/name may be wrong, or gives a nickname/quoted phrase, do not stop after one direct target search. Ground the stable anchor in the request first, such as the named owner, container, parent, account, project, location, or other higher-level record; then traverse its declared relationships, inspect related candidate records, and only then report no match or ask for help.
17043
+ - If the user says "still", "current", "latest", "where", "check", "if", or asks you to decide whether a condition is true, first identify the record that can prove the condition. When that condition names a related object, the code order must be: load the action target or anchor, traverse to the related evidence record, call its visible status/lookup action when available, then decide whether to mutate. Do not branch, return, or reject the condition from parent/action-target status before that evidence step.
17044
+ - When a condition names an anchored noun phrase whose final noun is an entity type, the final entity type is the evidence record to test. Use the earlier words only to ground or traverse to that record; do not test the anchor record as a substitute.
17045
+ - Do not treat prior read-only summaries, cached parent fields, action-target fields, or stored related-record fields as fresh evidence for a later conditional mutation when a related evidence object and visible lookup/status action can be reached.
17046
+ - When the user asks whether a suitable or available candidate exists for assignment, scheduling, routing, or ownership, call the visible availability/matching/search action on the candidate entity when one exists. Existing relationships or current assignments are context, not proof of current availability.
17047
+ - When a request names an owner, parent, account, project, location, or other container plus a target item, plan it as two steps: ground the owner/container, then discover the target through declared relationships, relationship filters, or short target-local search. Do not combine owner/container words with target words in one target-class search, and do not require owner/container words to appear in target-local fields such as title or summary.
17048
+ - When the target or evidence record is reached through relationships, use the declared relationship index/getter list as a graph and walk getters whose target types lead toward the needed entity. If a target has a one-record parent field and the user named the grandparent/owner, start from the grandparent/owner and traverse down through getters; do not put the grandparent condition inside the target's parent filter. If a relationship is documented as one-record or many-to-one, never use \`some\` on it.
17049
+ - After refusing a bypass, external-send, export, or restricted-data request, a follow-up that refers to the same item/case/record inherits that boundary even if no record was saved. Do not perform a different mutation, search for replacement candidates, or ask which restricted referent to use; refuse unless a visible allowed workflow explicitly authorizes the new request.
17050
+ - If the previous answer mentioned, displayed, or contrasted multiple plausible records and the next mutation uses only "it", "that", "that one", "the item", or similar, ask which grounded record to use. Even if one record seems more actionable, the pronoun alone is ambiguous, and a confirmation prompt is not a substitute for a grounded choice prompt.
17051
+ - A saved list, heap object collection, table, or record-display artifact with multiple possible mutation targets counts as multiple plausible records even when the visible text only gave counts. Do not pick the first, last, or most recent item from that collection for a pronoun like "that one"; ask for a grounded choice first.
17052
+ - For "first N", "next N", "top N", queue, slice, newest/oldest, or ranked-list requests, use the runtime paging surface on the target record type when it exists. Relationship getters can help discover context, but a local \`.slice(0, N)\` over a relationship array is not a paged queue result.
17053
+ - When selecting a single "top", "best", "urgent", or "most relevant" record from a broad set, do not rely on lexicographic sorting of label fields or the first page while more results exist. Narrow with grounded filters or gather enough candidates first, then rank from explicit record fields.
17054
+ - Do not remove candidates returned by an availability/search action solely because they are already assigned, current, or previously related, unless the user asked for a different candidate. If the action returned them as available or matching, they remain valid candidates.
17055
+ - In filters, use \`some\` only on relationship fields that are declared as many/collection fields. Singular relationship fields must use \`path\`, \`id\`, or \`is\`; if unsure, follow declared getters from an already grounded record instead.
15961
17056
 
15962
- \u2500\u2500\u2500 CAPABILITY SNAPSHOT \u2500\u2500\u2500
15963
- ${toolBlock}
17057
+ Intent resolution:
17058
+ - If intent is explicit, act directly.
17059
+ - For pronouns and discourse references like this, it, that, those, them, their, the previous one, or the selected ones, inspect recentReferences first. Do not use recentReferences array order as a selector when several same-type records could satisfy the phrase.
17060
+ - For follow-up phrases like same item, that record, the one you showed, or the previous result, read the single type-compatible recentReference before doing a fresh search. If the follow-up names a related target or evidence type, use the recent record only as the anchor and traverse declared relationships toward that type before searching the target class directly or deciding a condition.
17061
+ - If recentReferences contains an exact entry path for the follow-up target, call the matching class \`.get({ path })\` first only when that entry is the single plausible type-compatible referent or the user identified it with a unique identifier, ordinal, or descriptive selector. A phrase like "that one" is still a bare pronoun when multiple same-type records were displayed or saved together.
17062
+ - recentReferences includes user-mentioned records, assistant inline object references, and assistant heap object messages; prefer the latest type-compatible reference only when it is the single plausible referent for the phrase and not merely the last item from a multi-record display or saved list.
17063
+ - Record paths are opaque ids. Never synthesize a path from a label, name, title, or user phrase; copy an exact path from [State] or discover the record with a query.
17064
+ - If there is exactly one latest type-compatible reference for a phrase like "that same item", use it directly; do not ask the user to restate the item when you can already name or fetch it. This does not apply when the user refers to an earlier slice/list by ordinal wording, or when the prior answer intentionally contrasted several records.
17065
+ - For explicit continuity phrases like "that same item", "same record", or "the previous result", do not ask the user which record they mean. Use the recent reference first; if no saved reference exists, rerun the prior narrow grounding lookup from the conversation text instead of answering text-only that the record is not grounded.
17066
+ - If a follow-up mutation uses only a pronoun such as "it" or "that" after the prior turn mentioned multiple same-type records, ask the user to choose from grounded options before mutating.
17067
+ - If the prior turn displayed or summarized two or more plausible records and the next mutation says only "it", "that", or "on it", do not infer the target from your own ranking; call \`loop.ask_user({ type: "choice", ... })\` with the grounded records first, then mutate only the chosen record.
17068
+ - If the prior turn intentionally contrasted multiple records that could all receive the requested mutation, a lone pronoun is ambiguous even when one record was listed first or looked more urgent.
17069
+ - If a follow-up mutation uses a bare pronoun and recentReferences contains a matching \`group.id\` with \`group.sameTypeSize\` greater than 1, the target is unresolved. The next code must ask for a grounded choice with \`loop.ask_user(...)\`; never call a mutation on one grouped path first.
17070
+ - For follow-up words like "other", "another", or "remaining" after the user selected one candidate from a previous choice, resolve within the active contrast from that choice and the user's answer. Exclude the selected item, preserve descriptors such as larger, smaller, next, older, different, or same status, and do not take the first leftover from a wider saved list when the contrast narrows the intended set.
17071
+ - Before any mutation, prove the target resolves to exactly one grounded record. If the request describes a set, category, relationship, prior result group, or other non-unique scope, gather the candidate records first; when more than one candidate remains, ask the user to choose before calling the action.
17072
+ - For ambiguous choice prompts before a mutation, every option that describes a different candidate must carry a distinct grounded record value/path. After the answer, do not fall back to the first candidate if matching fails; ask again or stop without mutating.
17073
+ - The [State] constants are prompt context, not runtime variables. Never reference \`savedData\`, \`recentReferences\`, \`workflowContext\`, \`workflowState\`, or \`capabilities\` as variables in generated code. When using a recent reference, copy its path string into code and fetch it with \`Class.get({ path: "..." })\`, or call \`heap.getEntry("...")\` when the class is not obvious.
17074
+ - Never write placeholder grounding code such as \`const path = null\`, \`const groundedPath = ""\`, or \`const recordPath = ""\`. If no saved reference is available, delete that branch entirely and execute the fallback lookup directly.
17075
+ - Never call \`.get({ path: "" })\`; an empty path is not a saved reference.
17076
+ - For ordinal references to earlier pages, slices, lists, or ranked results, use the saved list/recent references first. If no saved list is available, rerun the exact same ordered query and select the ordinal index from its returned \`items\`; never invent a record path from a label or ordinal.
17077
+ - \`.get({ path })\` returns \`null\` when a path is not found; it does not throw for normal misses. Check for null before using a search fallback.
17078
+ - If multiple recent references could satisfy the phrase and the action or target would materially differ, ask for a grounded choice before any confirmation or mutation.
17079
+ - If the entity, field, target, scope, ranking, or action is ambiguous, create 2 to 5 plausible interpretations.
17080
+ - Probe plausible interpretations with cheap read-only queries before deciding.
17081
+ - A zero-result first query is not enough to report failure for a human reference; continue in the same job with another grounded strategy such as partial search, owner/container grounding, or relationship traversal before reporting no match.
17082
+ - If a direct target search returns zero and the request contains a stable anchor such as a named related record or higher-level container, ground that anchor and inspect related records before reporting no match.
17083
+ - One strong match means proceed.
17084
+ - Several plausible matches means call \`loop.ask_user({ type: "choice", ... })\` with grounded choices.
17085
+ - No grounded match means ask for missing information.
17086
+ - For consequential changes, resolve first, confirm when needed, then act.
17087
+ - Do not ask the user to resend a request because you need to verify data. If the request needs verification, run a job that verifies it now. If a follow-up reference is not available, rerun the prior narrow grounding lookup or ask a specific grounded question.
17088
+ - If the user asks a read-only advisory question such as "Should we message the team?" and also says not to update/send/act yet, provide the recommendation from grounded data. Do not pause with \`loop.ask_user\` or \`loop.confirm\`.
17089
+ - If the user asks for specific fields, read those fields from the grounded record and include every requested value in the visible answer. If saved state identifies the record but does not include the requested fields, fetch the record before answering. Only say a field is unavailable after checking the documented field/property on the fetched record.
17090
+ - If the user asks for blocked work and sensitive/restricted work as separate things, keep those candidate sets separate. Exclude sensitive or restricted-workflow records from the ordinary blocked operational candidate unless the user explicitly asks for blocked sensitive work.
17091
+
17092
+ Use exploratory probing when:
17093
+ - the user gives a human reference instead of an exact id or path
17094
+ - a noun could refer to multiple entity types
17095
+ - a name, number, label, date, or amount is given without a clear field
17096
+ - ranking words are used without a clear metric
17097
+ - a requested change has an unclear target
17098
+ - the first reasonable lookup returns zero results
17099
+ - the first reasonable lookup returns several plausible results
15964
17100
 
15965
- \u2500\u2500\u2500 DOMAIN REFERENCE (from ./sandbox-tools) \u2500\u2500\u2500
15966
- Import classes and effect functions from \`./sandbox-tools\` in generated code.
15967
- Use the TypeScript declarations for exact signatures. When present, the generated usage notes below them show query patterns and examples.
17101
+ Do not explore when:
17102
+ - the entity, field, filter, and action are explicit
17103
+ - the request is a general explanation
17104
+ - the request is unsupported by available capabilities
17105
+ - the next step is already a required workflow answer or confirmation
17106
+
17107
+ [Types]
17108
+ Import classes, helpers, and available actions from "./sandbox-tools".
17109
+ Use the domain contract below as the exact code-facing contract. Generated docs, relationship indexes, and action indexes are authoritative for valid fields, getters, actions, and filter shapes.
15968
17110
 
15969
17111
  ${domainBlock}
15970
17112
 
15971
- \u2500\u2500\u2500 EXECUTION CHECKPOINT \u2500\u2500\u2500
17113
+ [Docs]
17114
+ Query policy:
17115
+ - Use filter, search, sort, count, page, list, and iterate on entity classes.
17116
+ - Push filtering and sorting into entity queries. Do not fetch a page only to filter or sort locally.
17117
+ - Valid filter fields are defined by each entity filter type.
17118
+ - Valid sort fields are defined by each entity sort field type.
17119
+ - Search is class-wide text retrieval, not a field-scoped operator.
17120
+ - Entity classes do not have a \`.search(...)\` method. Use \`.find({ search })\`, \`.page({ search, ... })\`, or \`.list({ search, ... })\`.
17121
+ - Entity \`.list(...)\` returns an array of records; use \`matches[0]\`, \`matches.length\`, and direct iteration. Entity \`.page(...)\` returns \`{ items, page, perPage, totalCount, hasMore }\`; only page results have \`.items\`.
17122
+ - For natural queue slices, infer pagination even when the user does not say "page": "first five" means \`page: 1, perPage: 5\`; a follow-up "next five" for the same queue means \`page: 2, perPage: 5\` with the same sort and grounded filter.
17123
+ - For first/next/top queue slices, page the target item class directly with a structured relationship filter. Relationship getters and local \`.slice(0, 5)\` are useful for exploration but do not prove runtime pagination.
17124
+ - Combine search and filter when both free-text matching and exact constraints are needed.
17125
+ - For exact categorical states, prefer positive filters with \`equal_to\` or \`in\`. Do not express a requested state through substring negation of a different state with \`not_contains\`; categorical labels can contain other labels and disappear from the result.
17126
+ - Do not use \`not_in\`; the runtime filter surface does not support it. Use \`in\` with explicit allowed values, or fetch a bounded candidate page and filter excluded values locally before showing the final slice.
17127
+ - Boolean filters use \`equal_to: true\` or \`equal_to: false\`.
17128
+ - Use \`equal_to\` on names only when you know the full stored value. A shortened name, first word, fragment, alias, or nickname is not an exact name; use search/contains first and then ground the exact record. If an exact-name query returns zero for a human-supplied name, retry with search/contains in the same job before reporting that nothing exists.
17129
+ - Keep full-text search strings short and distinctive. Prefer one concrete name/id or 1 to 3 salient terms, then use filters, relationships, or local ranking for the rest.
17130
+ - Do not search a target entity for only a related-record name while also filtering by that relationship. First ground the related record, then use a relationship filter/getter, and use target-entity search only for the target's own identifier, title, label, description, or other target-local fields.
17131
+ - When the user combines a concrete entity name with generic task words like a priority, workflow state, risk, summary, or requested outcome, do not put the whole phrase into one full-text search. Search/filter the concrete name first, then apply status, priority, relationship, amount, date, or ranking constraints.
17132
+ - Treat urgency as priority unless the domain explicitly documents urgent as a status. For an urgent operational item, do not require \`status = "urgent"\`; inspect status/blocker after grounding likely priority matches.
17133
+ - Do not sort a free-text priority, severity, or rank-like label field and assume the first row is most important. Rank candidates locally from explicit field values and continue paging or narrow the query when the result says more records exist.
17134
+ - When looking for blocked or blocking work, treat phrases such as "no blocker", "not blocked", "without blocker", "none", and "clear" as negative evidence. Do not select a record only because its summary/title contains the substring "block"; prefer explicit blocker/status fields and keep scanning for a true blocker.
17135
+ - For broad "open", "active", or "top" operational records, avoid guessing a tiny fixed status list unless the domain documents one. Prefer relationship grounding plus a supported positive filter/list, or locally exclude clearly terminal states such as resolved, closed, complete, completed, paid, canceled, or archived after fetching a bounded sorted candidate page.
17136
+ - When a requested object is normally reached through relationships, follow the declared relationship chain from the grounded parent or related record before giving up on a direct search. If the target entity type is named, prefer chains whose declared return types lead to that target type.
17137
+ - Prefer generated instance relationship getters from a grounded record over hand-written deep nested relationship filters.
17138
+ - When you have grounded a parent record and need its related records, call the declared parent getter such as \`parent.get_related_records()\` instead of writing a nested relationship filter on the target class.
17139
+ - When deciding whether a related object is still in a current state, traverse to the related evidence record and call its visible lookup/status action when available. Do not decide, stop, post, or treat the condition as false from only stored fields, the parent record's status/title/summary, or prior displayed text.
17140
+ - For queues owned by a higher-level record, ground that record and keep the exact query plan: target class, direct relationship path or getter chain, filters, sort, page, and perPage. Save the displayed page and reuse the same plan for follow-up pages instead of inventing a new relationship filter.
17141
+ - Relationship getters return exactly their named entity type. If you call a getter for units/sites, those are not operational items; call the next declared item/work getter on each unit/site, or query the item class directly before reading item fields.
17142
+ - Relationship getters return only their declared related entity type. Do not treat one relationship result as another entity type because the request mentions it; walk the declared relationship chain exactly, or use a grounded direct query for the target entity.
17143
+ - Only call relationship getters that are declared for the class of the record you currently have. If the needed target is not a direct getter on that class, walk through the declared intermediate getter first; never skip a relationship hop by inventing a convenience getter.
17144
+ - Relationship getters are async. Always \`await record.get_related_records()\` before checking whether the result is an array, iterating it, or reading fields from its records.
17145
+ - Relationship filter fields are selectors, not hydrated nested objects. To read a field from a related record, call the declared relationship getter and use the returned record; do not read \`record.relationship.someField\` from the original record.
17146
+ - Do not filter relationship fields with scalar text operators. For example, do not write \`related: { contains: "Example" }\` or pass a parent path into a child filter; ground the related record first, then use the correctly typed relationship getter or \`id\`/\`path\` filter.
17147
+ - Relationship path filters must use a path for the relationship's target type. If a target item has a related parent/container field and the user named a higher-level parent, first follow the parent's declared getter to the correct related record, then use that related record's \`_graphPath\`; never put the wrong record type's path into a child relationship filter.
17148
+ - Do not use a target record's local text fields to prove ownership by a named parent/container. A filter such as \`title/summary contains parentName\` is not a relationship. Ground the parent/container and traverse getters or use the documented relationship field.
17149
+ - Do not write transitive relationship filters such as \`container: { is: { parent: ... } }\` for queue slices. Use a direct relationship path filter from the already grounded related record, such as \`container: { path: container._graphPath }\`.
17150
+ - Do not invent broad relationship filter fields on a target class unless that field is present in the generated filter type. For owned queues, ground the parent first, use declared relationship getters to reach the owned related records, or use the exact documented relationship field.
17151
+ - Do not optional-chain relationship getters to guess at hidden relationships. If a getter is not declared in the TypeScript contract, it does not exist.
17152
+ - Generated relationship getters return arrays of related records, not page objects. Iterate the returned array directly; do not read \`.items\` from a relationship getter result.
17153
+ - If an explicit target entity is not found through an expected relationship chain, try a grounded direct query/search for that target entity before reporting that no target exists.
17154
+ - For operational blocker, risk, status, or "what is happening" questions, inspect the relevant record's scalar fields such as status, priority, blocker, summary, latest update/message, due date, amount, and other domain-specific descriptive fields before answering.
17155
+ - For read-only readiness, risk, health, or status summaries, call any visible read-only assessment/status action on the grounded primary record before ad-hoc aggregation when such an action semantically matches the request. Use the returned fields in the reply and supplement with counts or record reads only when useful.
17156
+ - Do not hide required visible read-only assessment/status actions inside broad try/catch blocks. The runtime action surface should show that the assessment action ran.
17157
+ - Treat action/effect results as structured values, not necessarily arrays. Before indexing, iterating, checking \`.length\`, or calling array methods, normalize the result first: use the result itself only when \`Array.isArray(result)\`; otherwise read the exact array field shown in the output schema, or a documented array field such as \`items\`, \`matches\`, \`results\`, \`records\`, \`entries\`, \`candidates\`, \`options\`, or a domain-specific array field. Never convert a non-array object result to \`[]\` before checking its documented fields.
17158
+ - When a visible search, lookup, availability, or assessment action returns candidates or matches, treat those returned records as already scoped by the action inputs unless the output schema gives reliable fields for further narrowing. When matching returned candidates to grounded records, use the output schema's actual identifier fields, including \`id\`, \`path\`, or fields ending in \`Id\`; do not assume candidates have \`_graphPath\`. Do not discard all returned candidates by re-filtering on guessed property names.
17159
+ - For scheduling actions, convert relative wording into concrete ISO timestamps before mutating records.
17160
+ - When a decision depends on fresh external state and a visible read-only status/lookup action exists on the grounded record, call it before deciding, mutating, or refusing based on stale stored fields.
17161
+ - When deciding whether something is blocked, held, delayed, or still active, combine the fresh lookup result with relevant status, blocker, summary, checkpoint, reason, and latest update fields. Do not use a tiny hand-written status allowlist as the only authority; words such as hold, held, pending, delayed, awaiting, blocked, customs, review, and exception are blocking evidence unless the domain explicitly says otherwise.
17162
+ - One page does not prove there are no more records. For all, every, export, or broad scans, use iteration or page until there are no more results.
17163
+ - When selecting a single "best", "urgent", "top", or "most relevant" record from a broad page, do not assume the first returned page is complete if \`hasMore\` is true. Continue paging, use iteration, or add a stronger grounded filter before selecting.
17164
+ - For exploratory work, use count for totals and page with small perPage for samples; use iteration only after the interpretation is chosen.
17165
+
17166
+ Lookup ladder:
17167
+ 1. Check recent references and saved session data.
17168
+ 2. Try exact id or path when the user gave an id-like value.
17169
+ 3. If the request names a parent/container plus a target, ground the parent/container and traverse declared relationships to target candidates.
17170
+ 4. Try exact filters on fields whose names or aliases match the user words.
17171
+ 5. Try class-wide search with short target-local terms, not the whole user phrase.
17172
+ 6. Try relationship filters when the user mentions connected concepts and the filter shape is documented.
17173
+ 7. If the user names a parent/container and says the label may be approximate, inspect related target records before reporting no match.
17174
+ 8. If still empty, try one small set of normalized, prefix, or fuzzy variants when search supports it.
17175
+ 9. If still empty or ambiguous, ask the user for steering.
17176
+
17177
+ Exploration budget:
17178
+ - For a simple ambiguous reference, try up to 3 strategies.
17179
+ - For a broad ambiguous task, try up to 5 strategies.
17180
+ - Probe with small pages.
17181
+ - Do not run exhaustive scans during probing unless the user explicitly asks for all records or the selected task requires aggregation.
17182
+ - Stop early when a strong unique match is found.
17183
+
17184
+ Strong unique match:
17185
+ - exactly one record matches an exact id or path
17186
+ - exactly one record matches an exact filter on a likely identifier field
17187
+ - exactly one recent reference or saved value fits the request
17188
+ - one interpretation has results and all other reasonable interpretations have none
17189
+
17190
+ Ask the user when:
17191
+ - multiple exact matches exist
17192
+ - several entity types match the same phrase
17193
+ - the best match comes only from broad search and other plausible matches exist
17194
+ - the ranking or metric is unclear
17195
+ - the target is unique but the requested action is unclear
17196
+
17197
+ Relationship filters:
17198
+ - One-record relationships use \`is\`.
17199
+ - Multi-record relationships use \`some\`.
17200
+ - Never guess relationship cardinality from wording. Check the generated TypeScript filter type for the field before writing a relationship filter; if you are not sure, use declared relationship getters from already grounded records instead of a relationship filter.
17201
+ - If a relationship filter type or field is one-record/singular, never use \`some\` on that field. Match by \`id\`, \`path\`, or \`is\`, or fetch the related record and continue through declared getters when you need to traverse farther.
17202
+ - Do not invent nested operators under relationship fields. A one-record relationship filter accepts only its documented operators such as \`id\`, \`path\`, \`is\`, \`null\`, and \`not_null\`; deeper conditions must go under \`is\` or be handled by fetching records and following getters.
17203
+ - Never use \`some\` on one-record fields. If the generated TypeScript type says \`OneRelationFilter\`, valid operators are \`id\`, \`path\`, \`is\`, \`null\`, and \`not_null\`; \`some\` is invalid.
17204
+ - Use \`some\` only when the generated TypeScript type says \`ManyRelationFilter\`.
17205
+ - For a singular relationship that points to an intermediate record, nested filters still use \`is\` at the singular hop. Do not use \`some\` because the nested condition names another related record.
17206
+ - Use \`{ relationship: { id: "record_id" } }\` or \`{ relationship: { path: "class_record_id" } }\` when matching a known related record.
17207
+ - When you already fetched the related record, use \`{ relationship: { path: record._graphPath } }\` or \`{ relationship: { id: record.id } }\`; do not wrap a known id/path under \`is\`.
17208
+ - The path used in a relationship filter must be the path of the relationship target. For same-queue follow-ups from an item/batch/ticket, fetch that item's related unit/site/depot first and use the related unit/site/depot path; do not use the item path as a unit/site/depot path.
17209
+ - Use \`{ relationship: { is: { field: { equal_to: value } } } }\` only for nested field filters. Never put \`id\` or \`path\` inside \`is\`.
17210
+ - Do not write \`{ relationship: { some: ... } }\` unless the generated filter type for that exact relationship says it is a many/collection relationship. For one-record, parent, owner, or many-to-one relationships, use \`path\`, \`id\`, \`is\`, or getter traversal.
17211
+ - Do not pass a full record instance into a filter; if you already fetched a record, filter by its id or path instead.
17212
+ ${domainSections.docs ? `
17213
+ Domain notes:
17214
+ ${domainSections.docs}
17215
+ ` : ""}
17216
+
17217
+ Actions:
17218
+ ${actionIndex}
17219
+ - Actions listed under "Record-level" are instance methods. First fetch or find the specific record, then call the action on that instance, e.g. \`const item = await Item.get({ path }); await item.action_name(...)\`.
17220
+ - Actions listed under "Class-level" are class/static methods. Call them on the imported class, e.g. \`await Item.action_name(...)\`.
17221
+ - The action index is the visibility contract. If an action is listed for a class, call it directly on fetched/listed instances of that class; do not use \`typeof record.action_name === "function"\` as a discovery gate. If an action is not listed, do not call it.
17222
+ - Never call a record-level action as \`Class.action_name(...)\`; that method will not exist.
17223
+ - For action inputs, use the exact property names from the generated TypeScript method signature or the input schema shown in the action list. Do not invent synonym fields for required inputs.
17224
+ - Match user verbs to visible action names semantically. If a visible action clearly satisfies the user's requested operation, ground the target record and call that action instead of refusing because the wording differs.
17225
+ - When several visible actions or targets plausibly match the request, search/list the plausible grounded candidates, ask for a grounded choice when more than one remains, confirm if needed, then call only the selected visible action.
17226
+ - If the user asks to find records in a workflow state first, do not pre-filter away plausible records with unrelated secondary flags unless the ontology explicitly documents that relationship; gather the grounded candidates, ask when more than one remains, then confirm/action the selected record when appropriate.
17227
+ - If the user asks for an action that is not visible in the action list, refuse explicitly. Do not answer with only a read-only summary, do not ask for confirmation, and do not attempt hidden, guessed, or similarly named methods.
17228
+ - If the user asks to bypass permissions, skip a normal workflow, use a raw HTTP side channel, or export/send restricted data externally, do not ask for confirmation or missing details. Refuse and explain the allowed workflow boundary.
17229
+ - A restricted or denied referent stays restricted in follow-up turns. If the user later says "that same item", "fine then", or similar after a sensitive/bypass request, do not perform a mutation on that referent unless a visible allowed workflow explicitly authorizes it.
17230
+ - When summarizing records found by a query, include or display stable user-visible identifiers such as number, name, title, label, date, amount, or status. Do not answer only with counts when the user asked what you found.
17231
+
17232
+ [State]
17233
+ ${toolBlock}
17234
+
17235
+ ${sessionBlock}
17236
+
15972
17237
  ${checkpointBlock}
15973
17238
 
15974
- \u2500\u2500\u2500 WORKFLOW SNAPSHOT \u2500\u2500\u2500
15975
17239
  ${workflowBlock}
15976
17240
 
15977
- \u2500\u2500\u2500 RECENT REFERENTS \u2500\u2500\u2500
15978
17241
  ${referentBlock}
15979
17242
 
15980
- \u2500\u2500\u2500 SESSION HEAP \u2500\u2500\u2500
15981
17243
  ${heapBlock}
15982
17244
 
15983
- \u2500\u2500\u2500 AGENT LOOP STATE \u2500\u2500\u2500
15984
17245
  ${loopBlock}
15985
17246
 
15986
- \u2500\u2500\u2500 LOOP PLAYBOOK \u2500\u2500\u2500
15987
- - Continue from the latest structured state. Treat WORKFLOW SNAPSHOT, EXECUTION CHECKPOINT, RECENT REFERENTS, SESSION HEAP, and AGENT LOOP STATE as the working memory for this request.
15988
- - Use CAPABILITY SNAPSHOT to choose the next step, then use DOMAIN REFERENCE for exact signatures and query shapes.
15989
- - Take the minimum next step that directly helps the user. Avoid duplicate work, speculative cleanup, or extra fetching that is not needed yet.
15990
- - Use RECENT REFERENTS to resolve follow-up references across turns, such as "that invoice", "that customer", "those products", or "the other one".
15991
- - Treat user-provided names, numbers, and labels as human references, not exact keys. Resolve them with code: check recent referents/heap first, then query the graph with the broadest supported \`search\` or \`filter\`, then retry with a few normalized/fuzzy/prefix variants when the first pass is empty or ambiguous. Only say a record does not exist after a reasonable lookup across the relevant class.
15992
- - If one strong match exists, use it. If several plausible matches remain, use \`loop.ask_user({ type: 'choice', ... })\` with the grounded candidates instead of guessing.
15993
- - If the request has more than one reasonable interpretation, ask the user to clarify instead of guessing.
15994
- - For comparisons, rankings, selections, or summaries, first identify the rule you are using. If that rule is not clear from the user request and DOMAIN REFERENCE, ask the user before choosing anything.
15995
- - When the ranking, comparison, or selection rule is unclear, the minimum next step is the clarification itself. Do not run a placeholder query for a provisional winner before asking.
15996
- - If a user request matches both a domain type/effect and a loop helper, prioritize the domain type/effect. For example, if DOMAIN REFERENCE contains a \`Task\` class and the user asks to create a task, create the domain task record; do not call \`loop.create_task(...)\` unless you are only tracking your own workflow.
15997
- - Reuse exact \`taskId\`, \`decisionId\`, and \`closureId\` values from AGENT LOOP STATE. Never invent or rewrite them.
15998
- - If the request is ambiguous or clearly multi-step, create 2-4 short user-visible tasks and keep them updated as the workflow advances.
15999
- - Use \`loop.ask_user({ type: 'choice', options: [...] })\` when you have a short, grounded shortlist the user can choose from. Otherwise use \`type: 'input'\`.
16000
- - For an unclear ranking, comparison, or selection rule, prefer \`type: 'choice'\` when you can offer a short grounded list of plausible interpretations from the domain or nearby context.
16001
- - When \`type: 'choice'\` fits, do not ask the same question as plain text with bullets such as "Common options:" or "Choose one of these:".
16002
- - Use \`loop.confirm(...)\` for consequential approval unless the user already clearly instructed you to perform that exact action now.
16003
- - Await \`loop.ask_user(...)\` and \`loop.confirm(...)\`. After the job resumes, continue in the same job whenever the answer is enough to act.
16004
- - Use \`loop.open_decision(...)\` to persist grounded candidates, \`loop.close_decision(...)\` to resolve one, and \`loop.close_loop(...)\` when the workflow is completed, canceled, or blocked.
16005
- - If you ask a new question in the current job, do not also close the loop in that same job.
16006
-
16007
- \u2500\u2500\u2500 LOOP HELPER REFERENCE \u2500\u2500\u2500
16008
- - \`loop.ask_user(...)\`: pause the current job for missing input; use \`type: 'choice'\` only for a short grounded shortlist.
16009
- - \`loop.confirm(...)\`: pause for yes/no approval before a consequential action, then branch on the returned boolean.
16010
- - \`loop.open_decision(...)\`: save explicit candidates that later jobs can revisit; each candidate needs an \`id\`.
16011
- - \`loop.close_decision(...)\`: resolve an open decision with a stored \`selectedId\` and optional rationale.
16012
- - \`loop.create_task(...)\`, \`loop.update_task(...)\`, \`loop.complete_task(...)\`: keep a short resumable task list for the agent's workflow; these are not domain \`Task\` records.
16013
- - \`loop.close_loop(...)\`: record the workflow outcome when it is completed, canceled, or blocked.
17247
+ ${knownFactsBlock}
16014
17248
 
16015
- \u2500\u2500\u2500 CODE RULES \u2500\u2500\u2500
16016
- - Import from \`./sandbox-tools\`.
16017
- - If you use \`heap\`, \`loop\`, \`agent_text_message\`, or \`agent_heap_objects\`, import them explicitly from \`./sandbox-tools\`.
16018
- - Write top-level executable code with \`await\` at top level.
16019
- - The generated job body must be plain runnable JavaScript. Do not use TypeScript-only syntax.
16020
- - Follow the exact classes, methods, and parameter shapes in DOMAIN REFERENCE. Do not invent helpers or unsupported arguments.
16021
- - Use \`ClassName.get({ path })\` only for known graph paths when you want a direct graph fetch.
16022
- - Use \`ClassName.count()\` for totals, \`ClassName.page({ page, perPage, saveAs })\` when you need \`items\` plus \`totalCount\` or \`hasMore\`, \`ClassName.list({ page, perPage, saveAs })\` for one page of records, and \`ClassName.iterate({ perPage, maxItems })\` for large scans.
16023
- - \`perPage\` defaults to \`100\` and is capped at \`100\`.
16024
- - A single \`list(...)\` or \`page(...)\` call never proves there are no more records. For "all", "every", exports, broad scans, or exhaustive searches, use \`iterate(...)\` when available or loop \`page(...)\` until \`hasMore\` is false.
16025
- - Push \`filter\`, \`search\`, and \`sort\` into graph queries instead of fetching a page and processing it locally.
16026
- - A property appearing on a record does not make it valid in \`filter\` or \`sort\`; only use fields and operators that are explicitly exposed in DOMAIN REFERENCE.
16027
- - Choose \`sort.field\` verbatim from the sortable fields listed in DOMAIN REFERENCE. Do not sort by relationship names, related-record collections, counts, totals, or other derived metrics unless they are explicitly listed as sortable.
16028
- - If ordering alone answers the request, use \`sort\` without inventing a \`filter\`.
16029
- - Do not invent proxy metrics, fallback heuristics, or made-up tie-breakers to resolve ambiguity. If the rule is unclear, ask the user with \`loop.ask_user(...)\`.
16030
- - Do not fetch, sort, or show a provisional record just to have something to display while the real ranking or selection rule is still ambiguous.
16031
- - Call instance methods on instances, static methods on classes, and global effects by name.
16032
- - Use \`heap.getEntry(path)\` for remembered heap entries, \`heap.getList(name)\` for remembered lists, and \`heap.getVar(name)\` only for named variables.
16033
- - Use \`heap.setVar(...)\` and \`heap.deleteVar(...)\` only when they help the next step.
16034
- - Prefer \`heap.setVar(...)\` for scalars or one selected instance. Prefer \`ClassName.list({ saveAs })\` for reusable typed lists. Empty arrays are allowed.
16035
- - Only store sandbox instances, typed lists, or scalars in the heap. If a helper returns plain JSON, keep it local or store only the chosen scalar.
16036
- - Use the \`loop\` helpers to manage workflow state: \`ask_user\`, \`confirm\`, \`open_decision\`, \`close_decision\`, \`create_task\`, \`update_task\`, \`complete_task\`, and \`close_loop\`.
16037
- - Use \`type: 'choice'\` only for short grounded options. Use \`type: 'input'\` when the answer should stay open-ended.
16038
- - \`loop.confirm(...)\` is for consequential approval. Do not ask for approval in plain text.
16039
- - After \`await loop.ask_user(...)\` or \`await loop.confirm(...)\`, continue in the same resumed job when the answer is enough to act.
16040
- - Every job that answers the user must emit \`agent_text_message(...)\` and/or \`agent_heap_objects(...)\`.
16041
- - Use \`agent_text_message(...)\` for user-visible text.
16042
- - Use \`agent_heap_objects(...)\` for user-visible records. You may pass sandbox instances directly, or heap-backed \`entryPaths\`, \`listNames\`, and \`variableNames\` when you already have them. Use \`saveAs\` or \`heap.setVar(...)\` when you need a reusable named selection.
16043
- - Do not rely on the final return value for UI output. Do not return ad-hoc \`reply\` / \`show\` payloads instead of explicit agent message calls.
16044
- - Keep the code small and direct. Avoid speculative branches, broad casts, and raw JSON dumps unless the user asked for them.
16045
- - Use \`console.log()\` only for intermediate diagnostics, not for the final user-facing answer.`;
17249
+ [Request]
17250
+ ${input.request?.trim() || "Use the latest user message in the conversation."}`;
16046
17251
  }
16047
17252
 
16048
17253
  // src/agent-evals.ts
@@ -16102,6 +17307,143 @@ function asArray3(value) {
16102
17307
  if (!value) return [];
16103
17308
  return Array.isArray(value) ? value : [value];
16104
17309
  }
17310
+ var GPT_54_TOKEN_PRICING_USD_PER_MILLION = {
17311
+ input: 0.75,
17312
+ cachedInput: 0.075,
17313
+ output: 4.5
17314
+ };
17315
+ function emptyTokenUsage() {
17316
+ return {
17317
+ calls: 0,
17318
+ inputTokens: 0,
17319
+ cachedInputTokens: 0,
17320
+ uncachedInputTokens: 0,
17321
+ outputTokens: 0,
17322
+ totalTokens: 0,
17323
+ inputCostUsd: 0,
17324
+ cachedInputCostUsd: 0,
17325
+ outputCostUsd: 0,
17326
+ totalCostUsd: 0,
17327
+ missingUsageCalls: 0
17328
+ };
17329
+ }
17330
+ function numberField(record, key) {
17331
+ const value = record?.[key];
17332
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
17333
+ }
17334
+ function calculateTokenCost(input) {
17335
+ const inputCostUsd = input.uncachedInputTokens * GPT_54_TOKEN_PRICING_USD_PER_MILLION.input / 1e6;
17336
+ const cachedInputCostUsd = input.cachedInputTokens * GPT_54_TOKEN_PRICING_USD_PER_MILLION.cachedInput / 1e6;
17337
+ const outputCostUsd = input.outputTokens * GPT_54_TOKEN_PRICING_USD_PER_MILLION.output / 1e6;
17338
+ return {
17339
+ inputCostUsd,
17340
+ cachedInputCostUsd,
17341
+ outputCostUsd,
17342
+ totalCostUsd: inputCostUsd + cachedInputCostUsd + outputCostUsd
17343
+ };
17344
+ }
17345
+ function extractTokenUsageFromRaw(raw) {
17346
+ const usage = asRecord5(asRecord5(raw)?.usage);
17347
+ if (!usage) return null;
17348
+ const inputTokens = numberField(usage, "prompt_tokens") || numberField(usage, "input_tokens");
17349
+ const outputTokens = numberField(usage, "completion_tokens") || numberField(usage, "output_tokens");
17350
+ const details = asRecord5(usage.prompt_tokens_details) || asRecord5(usage.input_tokens_details);
17351
+ const cachedInputTokens = Math.min(
17352
+ inputTokens,
17353
+ numberField(details, "cached_tokens") || numberField(details, "cached_input_tokens")
17354
+ );
17355
+ const uncachedInputTokens = Math.max(inputTokens - cachedInputTokens, 0);
17356
+ const totalTokens = numberField(usage, "total_tokens") || inputTokens + outputTokens;
17357
+ const costs = calculateTokenCost({
17358
+ uncachedInputTokens,
17359
+ cachedInputTokens,
17360
+ outputTokens
17361
+ });
17362
+ return {
17363
+ calls: 1,
17364
+ inputTokens,
17365
+ cachedInputTokens,
17366
+ uncachedInputTokens,
17367
+ outputTokens,
17368
+ totalTokens,
17369
+ ...costs,
17370
+ missingUsageCalls: 0
17371
+ };
17372
+ }
17373
+ function addTokenUsage(aggregate, usage) {
17374
+ if (!usage) {
17375
+ return {
17376
+ ...aggregate,
17377
+ missingUsageCalls: aggregate.missingUsageCalls + 1
17378
+ };
17379
+ }
17380
+ const inputTokens = aggregate.inputTokens + usage.inputTokens;
17381
+ const cachedInputTokens = aggregate.cachedInputTokens + usage.cachedInputTokens;
17382
+ const uncachedInputTokens = aggregate.uncachedInputTokens + usage.uncachedInputTokens;
17383
+ const outputTokens = aggregate.outputTokens + usage.outputTokens;
17384
+ const costs = calculateTokenCost({
17385
+ uncachedInputTokens,
17386
+ cachedInputTokens,
17387
+ outputTokens
17388
+ });
17389
+ return {
17390
+ calls: aggregate.calls + usage.calls,
17391
+ inputTokens,
17392
+ cachedInputTokens,
17393
+ uncachedInputTokens,
17394
+ outputTokens,
17395
+ totalTokens: aggregate.totalTokens + usage.totalTokens,
17396
+ ...costs,
17397
+ missingUsageCalls: aggregate.missingUsageCalls + usage.missingUsageCalls
17398
+ };
17399
+ }
17400
+ function aggregateTokenUsage(usages) {
17401
+ return usages.reduce(
17402
+ (aggregate, usage) => addTokenUsage(aggregate, usage),
17403
+ emptyTokenUsage()
17404
+ );
17405
+ }
17406
+ function aggregateConversationTokenUsage(conversation) {
17407
+ return aggregateTokenUsage(
17408
+ conversation.logTurns.flatMap(
17409
+ (turn) => turn.iterations.map((iteration) => iteration.tokenUsage)
17410
+ )
17411
+ );
17412
+ }
17413
+ function tokenUsageForGenerationOutput(generation) {
17414
+ const attempts = generation.generationAttempts?.length ? generation.generationAttempts : [{ raw: generation.raw }];
17415
+ return aggregateTokenUsage(
17416
+ attempts.map((attempt) => extractTokenUsageFromRaw(attempt.raw))
17417
+ );
17418
+ }
17419
+ function formatUsd(value) {
17420
+ return `$${value.toFixed(6)}`;
17421
+ }
17422
+ function formatTokenUsage(usage) {
17423
+ if (!usage || usage.calls === 0 && usage.missingUsageCalls === 0) {
17424
+ return ["- LLM calls with usage data: 0", "- Total cost: $0.000000"];
17425
+ }
17426
+ return [
17427
+ `- LLM calls with usage data: ${usage.calls}`,
17428
+ `- LLM calls missing usage data: ${usage.missingUsageCalls}`,
17429
+ `- Input tokens: ${usage.inputTokens}`,
17430
+ `- Cached input tokens: ${usage.cachedInputTokens}`,
17431
+ `- Uncached input tokens: ${usage.uncachedInputTokens}`,
17432
+ `- Output tokens: ${usage.outputTokens}`,
17433
+ `- Total tokens: ${usage.totalTokens}`,
17434
+ `- Input cost: ${formatUsd(usage.inputCostUsd)}`,
17435
+ `- Cached input cost: ${formatUsd(usage.cachedInputCostUsd)}`,
17436
+ `- Output cost: ${formatUsd(usage.outputCostUsd)}`,
17437
+ `- Total cost: ${formatUsd(usage.totalCostUsd)}`,
17438
+ `- Pricing basis: GPT-5.4 at $${GPT_54_TOKEN_PRICING_USD_PER_MILLION.input}/M input, $${GPT_54_TOKEN_PRICING_USD_PER_MILLION.cachedInput}/M cached input, $${GPT_54_TOKEN_PRICING_USD_PER_MILLION.output}/M output.`
17439
+ ];
17440
+ }
17441
+ function getJobAgentMessages(liveDoc, jobId) {
17442
+ const jobsById = asRecord5(asRecord5(liveDoc.jobs)?.byId);
17443
+ const job = asRecord5(jobsById?.[jobId]);
17444
+ const agentMessages = job?.agentMessages;
17445
+ return Array.isArray(agentMessages) ? agentMessages : [];
17446
+ }
16105
17447
  function buildScenarioSteps(scenario) {
16106
17448
  if (scenario.steps?.length) {
16107
17449
  return scenario.steps;
@@ -16217,19 +17559,99 @@ function extractJsonObject(text) {
16217
17559
  const start = text.indexOf("{");
16218
17560
  const end = text.lastIndexOf("}");
16219
17561
  if (start === -1 || end === -1 || end < start) return null;
17562
+ const candidate = text.slice(start, end + 1);
16220
17563
  try {
16221
- return JSON.parse(text.slice(start, end + 1));
17564
+ return JSON.parse(candidate);
16222
17565
  } catch {
16223
- return null;
17566
+ const fallback = {};
17567
+ for (const fieldName of ["action", "reply", "code"]) {
17568
+ const field = extractJsonStringField(candidate, fieldName);
17569
+ if (field?.complete) {
17570
+ fallback[fieldName] = field.value;
17571
+ }
17572
+ }
17573
+ return Object.keys(fallback).length > 0 ? fallback : null;
16224
17574
  }
16225
17575
  }
17576
+ function extractJsonStringField(source, fieldName) {
17577
+ const keyIndex = source.indexOf(JSON.stringify(fieldName));
17578
+ if (keyIndex === -1) return null;
17579
+ const colonIndex = source.indexOf(":", keyIndex + fieldName.length + 2);
17580
+ if (colonIndex === -1) return null;
17581
+ let cursor = colonIndex + 1;
17582
+ while (cursor < source.length && /\s/.test(source[cursor] || "")) cursor += 1;
17583
+ if (source[cursor] !== '"') return null;
17584
+ cursor += 1;
17585
+ let value = "";
17586
+ while (cursor < source.length) {
17587
+ const char = source[cursor];
17588
+ if (char === '"') return { value, complete: true };
17589
+ if (char !== "\\") {
17590
+ value += char;
17591
+ cursor += 1;
17592
+ continue;
17593
+ }
17594
+ if (cursor + 1 >= source.length) return { value, complete: false };
17595
+ const escaped = source[cursor + 1];
17596
+ if (escaped === "n") value += "\n";
17597
+ else if (escaped === "r") value += "\r";
17598
+ else if (escaped === "t") value += " ";
17599
+ else if (escaped === "b") value += "\b";
17600
+ else if (escaped === "f") value += "\f";
17601
+ else if (escaped === '"' || escaped === "\\" || escaped === "/") {
17602
+ value += escaped;
17603
+ } else if (escaped === "u") {
17604
+ const hex = source.slice(cursor + 2, cursor + 6);
17605
+ if (hex.length < 4 || !/^[0-9a-fA-F]{4}$/.test(hex)) {
17606
+ return { value, complete: false };
17607
+ }
17608
+ value += String.fromCharCode(Number.parseInt(hex, 16));
17609
+ cursor += 6;
17610
+ continue;
17611
+ } else {
17612
+ value += escaped;
17613
+ }
17614
+ cursor += 2;
17615
+ }
17616
+ return { value, complete: false };
17617
+ }
16226
17618
  function modelOutputInstruction() {
16227
17619
  return [
16228
17620
  "Return only a JSON object with this shape:",
16229
17621
  '{ "action": "reply" | "job", "reply": string, "code": string }',
16230
17622
  'Use "action":"reply" only when a plain conversational answer is enough and no live session state should change.',
17623
+ 'Do not use "action":"reply" to promise future tool work; if the user asks to check, find, look up, inspect, update, post, send, approve, schedule, reschedule, calculate, or confirm around a domain action, use "action":"job".',
17624
+ 'Do not use "action":"reply" to say a record is not grounded yet; if the request names or describes a domain record, use "action":"job" and ground it from session state, relationships, searches, or visible read-only actions first.',
17625
+ 'Before claiming you lack access, inspect the visible action list. If a visible read-only search, lookup, list, guidance, note, policy, or knowledge action can satisfy a "check", "find", "look up", or "whether we have guidance" request, choose "action":"job" and call it.',
17626
+ "Generated code must not report no matches for the primary human-described anchor after a single zero-result list/find/page call. Before that primary no-match return, retry the primary anchor with fewer text constraints or a distinct fallback such as owner/container grounding, relationship traversal, exact-id/path lookup, or shorter target-local search.",
16231
17627
  'Use "action":"job" when the next step should run code or mutate workflow state.',
16232
17628
  'When action is "job", include runnable code in "code".',
17629
+ "Generated code must not reference prompt-only symbols such as savedData, recentReferences, workflowContext, workflowState, or capabilities. Copy concrete paths/ids from the prompt into strings, fetch records with imports from ./sandbox-tools, or use documented runtime helpers.",
17630
+ "Generated code must import every class and helper it uses from ./sandbox-tools; do not leave undeclared identifiers in the job.",
17631
+ "Generated action calls must use the exact input property names from the visible action schema. Do not invent synonym keys for required inputs.",
17632
+ "If multiple possible targets or a needed human decision blocks a requested operation, put the pause inside code with loop.ask_user(...) or loop.confirm(...); listing candidates or asking only in reply text and returning is incomplete, including when ambiguity is discovered after a query returns several records.",
17633
+ "When a lookup before a mutation returns multiple plausible target records, generated code must ask for a grounded choice; do not mutate results[0], the earliest sorted record, or any other default pick unless the user supplied a unique identifier, ordinal, or selector.",
17634
+ "A bare pronoun such as it, that, or that one is not a unique mutation target when recentReferences, savedData, or the prior visible answer contains multiple compatible records. Do not let one exact recentReference path override that multi-record ambiguity; generated code must ask for a grounded choice before mutating.",
17635
+ "If a follow-up names the same/previous record and also names a related target or evidence type in a condition, use the same/previous record only as the anchor; traverse to the named related type before deciding or mutating.",
17636
+ "For owner/container plus target requests, generated code must ground the owner/container first, then discover the target through relationships, relationship filters, or short target-local search; do not combine owner/container words with target words in one target-class query or require owner/container words to appear in target-local title/summary fields.",
17637
+ "For requested categorical states, generated code should use positive exact filters or explicit local checks; do not use substring negation of another state as a proxy for the requested state.",
17638
+ "For conditional mutations based on a related evidence record, generated code order must be: load the action target or anchor, traverse to the related evidence record, call any visible status/lookup action, then decide whether to mutate. Do not decide, mutate, return, or reject the condition from parent/action-target fields before that evidence step.",
17639
+ "When a condition names an anchored noun phrase whose final noun is an entity type, the final entity type is the evidence record to test; use the earlier words only to ground or traverse to that record.",
17640
+ "Do not treat prior read-only summaries, cached parent fields, action-target fields, or stored related-record fields as fresh evidence for a later conditional mutation when a related evidence object and visible lookup/status action can be reached.",
17641
+ "Generated code must await declared relationship getters before checking arrays, iterating, or reading related-record fields.",
17642
+ "Generated code must call only relationship getters declared on the current record's class; if the target is not direct, walk the declared intermediate getter first instead of inventing a convenience getter.",
17643
+ "Generated code must use declared relationship getters before reading fields from related records; relationship filter fields are not guaranteed to be hydrated nested objects.",
17644
+ "For suitable/available candidate requests, generated code must call a visible availability, matching, or search action when one exists instead of relying only on current relationships or assignments.",
17645
+ "A follow-up to a refused bypass, export, external-send, or restricted-data request must keep the refusal boundary for the same referent; choose a reply refusal instead of running a mutation job.",
17646
+ "Visible answers for grounded named records should include the stored display name or identifier, not only the user's shorthand.",
17647
+ "When the user asks for specific fields, the reply must include every requested field or explicitly say which grounded field is unavailable after fetching the grounded record if saved state is partial.",
17648
+ "When matching action-returned candidates to grounded records, use the output schema's actual identifier fields, including id, path, or fields ending in Id; do not assume returned candidates have _graphPath.",
17649
+ "When resolving a choice answer, accept an unambiguous prefix or substring of an option label; do not fail just because the returned label is abbreviated.",
17650
+ "Do not discard availability/search results solely because a candidate is already assigned or related, unless the user asked for a different candidate.",
17651
+ "After verifying a user-authorized conditional mutation, call the action directly; do not add loop.confirm(...) solely because the mutation is visible to other people, customer-facing, or consequential. Confirm only when the user, policy, action metadata, or unresolved material uncertainty requires it.",
17652
+ "When a job identifies a specific record in its visible answer, display it with agent_heap_objects(...) when the user should see or open it; otherwise save it with await heap.setVar(...) only when it is needed for follow-up resolution.",
17653
+ "heap.setVar(...) accepts scalars, runtime records, sandbox instances, or arrays of those values; do not save plain action/effect result objects. Fetch a created record by its returned id/path before saving or displaying it.",
17654
+ "For requested record fields, read the documented properties from the fetched record before saying a value is unavailable.",
16233
17655
  'When action is "reply", include the user-facing answer in "reply".'
16234
17656
  ].join("\n");
16235
17657
  }
@@ -16238,7 +17660,7 @@ function createOpenAIChatTurnGenerator(options) {
16238
17660
  /\/$/,
16239
17661
  ""
16240
17662
  );
16241
- const model = options.model || "gpt-5-mini";
17663
+ const model = options.model || "gpt-5.4";
16242
17664
  return async (input) => {
16243
17665
  const messages = [
16244
17666
  {
@@ -16252,8 +17674,12 @@ ${modelOutputInstruction()}`
16252
17674
  ];
16253
17675
  const payload = {
16254
17676
  model,
16255
- messages
17677
+ messages,
17678
+ response_format: { type: "json_object" }
16256
17679
  };
17680
+ if (input.onTextDelta) {
17681
+ payload.stream = true;
17682
+ }
16257
17683
  if (typeof options.temperature === "number") {
16258
17684
  payload.temperature = options.temperature;
16259
17685
  }
@@ -16279,19 +17705,63 @@ ${modelOutputInstruction()}`
16279
17705
  `OpenAI chat generation failed: ${response.status} ${errorText}`
16280
17706
  );
16281
17707
  }
16282
- const raw = await response.json();
16283
- const content = asRecord5(
16284
- asRecord5(raw.choices?.[0])?.message
16285
- )?.content;
16286
- const text = typeof content === "string" ? content : Array.isArray(content) ? content.map((part) => asRecord5(part)?.text || "").join("") : "";
17708
+ let raw;
17709
+ let text = "";
17710
+ if (input.onTextDelta && response.body) {
17711
+ const onTextDelta = input.onTextDelta;
17712
+ const reader = response.body.getReader();
17713
+ const decoder = new TextDecoder();
17714
+ let buffer = "";
17715
+ const processStreamLine = async (line) => {
17716
+ const trimmedLine = line.trimEnd();
17717
+ if (!trimmedLine.startsWith("data:")) return;
17718
+ const data = trimmedLine.slice("data:".length).trim();
17719
+ if (!data || data === "[DONE]") return;
17720
+ const event = JSON.parse(data);
17721
+ const delta = asRecord5(
17722
+ asRecord5(event.choices?.[0])?.delta
17723
+ )?.content;
17724
+ const deltaText = typeof delta === "string" ? delta : Array.isArray(delta) ? delta.map((part) => asRecord5(part)?.text || "").join("") : "";
17725
+ if (!deltaText) return;
17726
+ text += deltaText;
17727
+ await onTextDelta(deltaText);
17728
+ };
17729
+ while (true) {
17730
+ const { value, done } = await reader.read();
17731
+ if (done) break;
17732
+ buffer += decoder.decode(value, { stream: true });
17733
+ while (true) {
17734
+ const lineEnd = buffer.indexOf("\n");
17735
+ if (lineEnd === -1) break;
17736
+ const line = buffer.slice(0, lineEnd);
17737
+ buffer = buffer.slice(lineEnd + 1);
17738
+ await processStreamLine(line);
17739
+ }
17740
+ }
17741
+ buffer += decoder.decode();
17742
+ if (buffer.trim()) {
17743
+ await processStreamLine(buffer);
17744
+ }
17745
+ raw = { streamed: true };
17746
+ } else {
17747
+ const json = await response.json();
17748
+ raw = json;
17749
+ const content = asRecord5(
17750
+ asRecord5(json.choices?.[0])?.message
17751
+ )?.content;
17752
+ text = typeof content === "string" ? content : Array.isArray(content) ? content.map((part) => asRecord5(part)?.text || "").join("") : "";
17753
+ }
16287
17754
  const parsed = extractJsonObject(text);
16288
17755
  if (!parsed) {
16289
17756
  if (attempt < 3) {
16290
17757
  await sleep2(300 * attempt);
16291
17758
  continue;
16292
17759
  }
16293
- throw new Error(`Model output was not valid JSON:
16294
- ${text}`);
17760
+ return {
17761
+ reply: text.trim(),
17762
+ code: void 0,
17763
+ raw
17764
+ };
16295
17765
  }
16296
17766
  return {
16297
17767
  reply: typeof parsed.reply === "string" ? parsed.reply : void 0,
@@ -16318,7 +17788,7 @@ async function ensureDir(dir) {
16318
17788
  async function sleep2(ms) {
16319
17789
  await new Promise((resolve) => setTimeout(resolve, ms));
16320
17790
  }
16321
- async function withTimeout(promise, ms, label) {
17791
+ async function withTimeout2(promise, ms, label) {
16322
17792
  let timeoutId;
16323
17793
  try {
16324
17794
  return await Promise.race([
@@ -16337,9 +17807,27 @@ async function withTimeout(promise, ms, label) {
16337
17807
  function getActionSummary(liveDoc, jobId) {
16338
17808
  const jobsById = asRecord5(asRecord5(liveDoc?.jobs)?.byId) || {};
16339
17809
  const job = asRecord5(jobsById[jobId]);
16340
- return Array.isArray(job?.actionSummary) ? job.actionSummary.filter(
17810
+ const summary = Array.isArray(job?.actionSummary) ? job.actionSummary.filter(
16341
17811
  (line) => typeof line === "string"
16342
17812
  ) : [];
17813
+ const trace = Array.isArray(job?.actionTrace) ? job.actionTrace.map((event) => asRecord5(event)).filter((event) => Boolean(event)) : [];
17814
+ const traceLines = trace.map((event) => {
17815
+ const kind = typeof event.kind === "string" ? event.kind : "";
17816
+ const action = typeof event.action === "string" ? event.action : "";
17817
+ const target = typeof event.target === "string" ? event.target : "";
17818
+ if (!action) return "";
17819
+ if (kind === "effect_call") {
17820
+ return `- Called ${target || action}`;
17821
+ }
17822
+ if (kind === "loop_op" && action === "ask_user") {
17823
+ return "- Asked the user for input";
17824
+ }
17825
+ if (kind === "loop_op" && action === "confirm") {
17826
+ return "- Requested confirmation";
17827
+ }
17828
+ return "";
17829
+ }).filter((line) => Boolean(line));
17830
+ return Array.from(/* @__PURE__ */ new Set([...summary, ...traceLines]));
16343
17831
  }
16344
17832
  function normalizeHeapSnapshot2(heap) {
16345
17833
  return {
@@ -16364,21 +17852,30 @@ ${checkpoint.latestJobResult}` : null
16364
17852
  async function waitForJobOutcome(input) {
16365
17853
  const stdout = [];
16366
17854
  const stderr = [];
17855
+ let lastLiveDoc = null;
17856
+ let lastPromptCount = 0;
17857
+ let lastMessageCount = 0;
17858
+ let lastJobSummary = null;
16367
17859
  input.job.on("stdout", (line) => stdout.push(String(line)));
16368
17860
  input.job.on("stderr", (line) => stderr.push(String(line)));
16369
17861
  const startedAt = Date.now();
16370
17862
  while (Date.now() - startedAt < input.timeoutMs) {
16371
17863
  const liveDoc = cloneJson(input.environment.document);
17864
+ lastLiveDoc = liveDoc;
16372
17865
  const prompts = filterPromptsByBoundary(
16373
17866
  liveDoc,
16374
17867
  getOpenPromptsFromDoc(liveDoc),
16375
17868
  input.boundaryTimestamp
16376
17869
  );
17870
+ lastPromptCount = prompts.length;
17871
+ const messages = asArray3(asRecord5(liveDoc.conversation)?.messages);
17872
+ lastMessageCount = messages.length;
17873
+ lastJobSummary = asRecord5(asRecord5(liveDoc.jobs)?.byId)?.[input.job.id] || null;
16377
17874
  if (prompts.length > 0) {
16378
17875
  return { kind: "prompt", prompts, liveDoc, stdout, stderr };
16379
17876
  }
16380
17877
  try {
16381
- const result = await withTimeout(
17878
+ const result = await withTimeout2(
16382
17879
  input.job.result,
16383
17880
  input.pollIntervalMs,
16384
17881
  `job ${input.job.id} tick`
@@ -16389,37 +17886,70 @@ async function waitForJobOutcome(input) {
16389
17886
  if (!/timed out after/.test(message)) throw error;
16390
17887
  }
16391
17888
  }
16392
- throw new Error(`Job ${input.job.id} timed out after ${input.timeoutMs}ms`);
17889
+ const diagnostics = {
17890
+ elapsedMs: Date.now() - startedAt,
17891
+ promptCount: lastPromptCount,
17892
+ messageCount: lastMessageCount,
17893
+ stdoutTail: stdout.slice(-5),
17894
+ stderrTail: stderr.slice(-5),
17895
+ job: lastJobSummary,
17896
+ hasLiveDoc: Boolean(lastLiveDoc)
17897
+ };
17898
+ throw new Error(
17899
+ `Job ${input.job.id} timed out after ${input.timeoutMs}ms. Diagnostics: ${JSON.stringify(diagnostics)}`
17900
+ );
16393
17901
  }
16394
17902
  async function generateTurnWithRepair(generator, input) {
16395
- let output = await generator(input);
16396
- let request = input.request;
16397
- let attempt = input.attempt;
16398
- for (let repairRound = 0; repairRound < 3; repairRound += 1) {
16399
- if (!output.code) return output;
16400
- const issues = reviewGeneratedJobCode(output.code);
16401
- if (issues.length === 0) return output;
16402
- request = [
16403
- request,
16404
- "",
16405
- "Regenerate the job code and fix these issues:",
16406
- ...issues.map((issue) => `- ${issue.message}`),
16407
- "Return the full corrected job code."
16408
- ].join("\n");
16409
- attempt += 1;
16410
- output = await generator({
16411
- ...input,
16412
- attempt,
16413
- request,
16414
- repairIssues: issues
16415
- });
16416
- }
16417
- return output;
17903
+ const output = await generator(input);
17904
+ const generationAttempts = [
17905
+ {
17906
+ attempt: input.attempt,
17907
+ request: input.request,
17908
+ repairIssues: input.repairIssues,
17909
+ reply: output.reply,
17910
+ code: output.code,
17911
+ raw: output.raw
17912
+ }
17913
+ ];
17914
+ return { ...output, generationAttempts };
16418
17915
  }
16419
17916
  async function writeJson(filePath, value) {
16420
17917
  await writeFile(filePath, `${JSON.stringify(value, null, 2)}
16421
17918
  `);
16422
17919
  }
17920
+ function describeScenarioBehavior(result) {
17921
+ if (result.scenario.description?.trim()) {
17922
+ return result.scenario.description.trim();
17923
+ }
17924
+ const steps = result.steps?.length ? result.steps : buildScenarioSteps(result.scenario).map((step, index) => ({
17925
+ id: step.id || `step-${index + 1}`,
17926
+ request: step.request
17927
+ }));
17928
+ const stepSummary = steps.map((step, index) => {
17929
+ const request = step.request.replace(/\s+/g, " ").trim();
17930
+ return `${index + 1}. ${step.id}: ${request}`;
17931
+ }).join(" ");
17932
+ return [
17933
+ `This report tests scenario \`${result.scenario.id}\` across ${steps.length} user turn${steps.length === 1 ? "" : "s"}.`,
17934
+ "It verifies that the agent grounds the natural-language request in the current ontology/session context, generates the expected job or refusal, and that the runtime executes or blocks the resulting behavior correctly.",
17935
+ stepSummary
17936
+ ].filter(Boolean).join(" ");
17937
+ }
17938
+ function describeJobSource(result) {
17939
+ if (result.scenario.jobSource === "hardcoded") {
17940
+ return "Hardcoded deterministic job code supplied by the test.";
17941
+ }
17942
+ if (result.scenario.jobSource === "mixed") {
17943
+ return "Mixed: LLM-generated agent jobs plus hardcoded setup/inspection jobs supplied by the test.";
17944
+ }
17945
+ const hasGeneratedCode = Boolean(
17946
+ result.finalCode || result.steps?.some((step) => step.finalCode)
17947
+ );
17948
+ if (hasGeneratedCode) {
17949
+ return "LLM-generated agent job code. Setup, direct inspections, and assertions are hardcoded by the test harness.";
17950
+ }
17951
+ return "LLM-generated agent response. Setup, direct inspections, and assertions are hardcoded by the test harness.";
17952
+ }
16423
17953
  function buildResultReport(result) {
16424
17954
  const stepSection = result.steps?.length ? [
16425
17955
  "## Steps",
@@ -16436,6 +17966,15 @@ function buildResultReport(result) {
16436
17966
  const lines = [
16437
17967
  `# Scenario Report: ${result.scenario.id}`,
16438
17968
  "",
17969
+ "## Behavior Under Test",
17970
+ describeScenarioBehavior(result),
17971
+ "",
17972
+ "## Job Source",
17973
+ describeJobSource(result),
17974
+ "",
17975
+ "## Token Usage And Cost",
17976
+ ...formatTokenUsage(result.tokenUsage),
17977
+ "",
16439
17978
  "## Request",
16440
17979
  result.scenario.request || result.steps?.[0]?.request || "_No single request_",
16441
17980
  "",
@@ -16462,9 +18001,21 @@ function buildResultReport(result) {
16462
18001
  `;
16463
18002
  }
16464
18003
  function buildSuiteIndex(results) {
18004
+ const totalUsage = aggregateTokenUsage(
18005
+ results.map((result) => result.tokenUsage)
18006
+ );
16465
18007
  const lines = [
16466
18008
  "# Agent Eval Report Index",
16467
18009
  "",
18010
+ "## Token Usage And Cost",
18011
+ ...formatTokenUsage(totalUsage),
18012
+ "",
18013
+ "## Session Logs",
18014
+ "",
18015
+ "- [Readable chronological logs](./logs/README.md)",
18016
+ "",
18017
+ "## Scenario Reports",
18018
+ "",
16468
18019
  ...results.map(
16469
18020
  (result) => `- [${result.scenario.id}](./${result.scenario.id}/REPORT.md) - ${result.status}`
16470
18021
  )
@@ -16472,6 +18023,191 @@ function buildSuiteIndex(results) {
16472
18023
  return `${lines.join("\n")}
16473
18024
  `;
16474
18025
  }
18026
+ function buildLogsIndex(results) {
18027
+ const totalUsage = aggregateTokenUsage(
18028
+ results.map((result) => result.tokenUsage)
18029
+ );
18030
+ const lines = [
18031
+ "# Agent Eval Session Logs",
18032
+ "",
18033
+ "Each file is a chronological session report with user requests, agent responses, generated code, runtime actions/results, and system prompts at the end.",
18034
+ "",
18035
+ "## Token Usage And Cost",
18036
+ ...formatTokenUsage(totalUsage),
18037
+ "",
18038
+ ...results.map((result) => {
18039
+ const logName = `${slugify(result.scenario.id)}.md`;
18040
+ return `- [${result.scenario.id}](./${logName}) - ${result.status} - ${formatUsd(result.tokenUsage?.totalCostUsd || 0)}`;
18041
+ })
18042
+ ];
18043
+ return `${lines.join("\n")}
18044
+ `;
18045
+ }
18046
+ function fenced(value, language = "") {
18047
+ const fence = value.includes("```") ? "````" : "```";
18048
+ return `${fence}${language}
18049
+ ${value}
18050
+ ${fence}`;
18051
+ }
18052
+ function jsonBlock(value) {
18053
+ return fenced(JSON.stringify(value, null, 2), "json");
18054
+ }
18055
+ function buildSessionLogReport(input) {
18056
+ const { conversation, result, error } = input;
18057
+ const systemPrompts = conversation.logTurns.flatMap(
18058
+ (turn) => turn.iterations.map((iteration) => ({
18059
+ turn,
18060
+ iteration
18061
+ }))
18062
+ );
18063
+ const lines = [
18064
+ `# Session Log: ${conversation.label}`,
18065
+ "",
18066
+ "## Behavior Under Test",
18067
+ result ? describeScenarioBehavior(result) : "This session log captures the chronological agent/runtime behavior for a scenario that did not complete a structured result.",
18068
+ "",
18069
+ "## Job Source",
18070
+ result ? describeJobSource(result) : "LLM-generated agent jobs when generation completed; setup and harness assertions are hardcoded by the test harness.",
18071
+ "",
18072
+ "## Token Usage And Cost",
18073
+ ...formatTokenUsage(
18074
+ result?.tokenUsage || aggregateConversationTokenUsage(conversation)
18075
+ ),
18076
+ "",
18077
+ "## Metadata",
18078
+ `- Session id: \`${conversation.environment.sessionId}\``,
18079
+ `- Environment id: \`${conversation.environment.environmentId}\``,
18080
+ `- Sandbox id: \`${conversation.environment.sandboxId}\``,
18081
+ `- Status: ${result?.status || (error ? "failed" : "unknown")}`,
18082
+ ...result?.error || error ? [`- Error: ${result?.error || error}`] : [],
18083
+ "",
18084
+ "## Conversation"
18085
+ ];
18086
+ for (const turn of conversation.logTurns) {
18087
+ lines.push("", `### Turn ${turn.turnNumber}: ${turn.turnId}`, "");
18088
+ lines.push("**User**", "");
18089
+ lines.push(turn.request, "");
18090
+ for (const iteration of turn.iterations) {
18091
+ lines.push(`#### Agent Generation ${iteration.iteration}`, "");
18092
+ lines.push(
18093
+ "**Token Usage And Cost**",
18094
+ "",
18095
+ ...formatTokenUsage(iteration.tokenUsage),
18096
+ ""
18097
+ );
18098
+ if ((iteration.generationAttempts?.length || 0) > 1) {
18099
+ lines.push("**Generation Attempts**", "");
18100
+ for (const attempt of iteration.generationAttempts || []) {
18101
+ lines.push(`Attempt ${attempt.attempt}`, "");
18102
+ if (attempt.repairIssues?.length) {
18103
+ lines.push("Repair issues:", "");
18104
+ for (const issue of attempt.repairIssues) {
18105
+ lines.push(`- ${issue.code}: ${issue.message}`);
18106
+ }
18107
+ lines.push("");
18108
+ }
18109
+ lines.push("Request", "", fenced(attempt.request, "text"), "");
18110
+ if (attempt.reply?.trim()) {
18111
+ lines.push("Draft reply", "", attempt.reply.trim(), "");
18112
+ }
18113
+ if (attempt.code?.trim()) {
18114
+ lines.push("Code", "", fenced(attempt.code.trim(), "ts"), "");
18115
+ }
18116
+ }
18117
+ }
18118
+ if (iteration.generationReply?.trim()) {
18119
+ lines.push("**Draft Reply**", "", iteration.generationReply.trim(), "");
18120
+ }
18121
+ if (iteration.generatedCode?.trim()) {
18122
+ lines.push(
18123
+ "**Generated Code**",
18124
+ "",
18125
+ fenced(iteration.generatedCode.trim(), "ts"),
18126
+ ""
18127
+ );
18128
+ } else {
18129
+ lines.push("**Generated Code**", "", "_No code generated._", "");
18130
+ }
18131
+ if (iteration.promptInteractions?.length) {
18132
+ lines.push("**Structured User Input**", "");
18133
+ for (const interaction of iteration.promptInteractions) {
18134
+ lines.push(
18135
+ `- ${interaction.type}: ${interaction.message || interaction.title} -> \`${JSON.stringify(interaction.answer)}\``
18136
+ );
18137
+ }
18138
+ lines.push("");
18139
+ }
18140
+ if (iteration.actionSummary?.length) {
18141
+ lines.push("**Runtime Actions**", "");
18142
+ for (const action of iteration.actionSummary) lines.push(`- ${action}`);
18143
+ lines.push("");
18144
+ }
18145
+ if (iteration.responseText?.trim()) {
18146
+ lines.push("**Agent Response**", "", iteration.responseText.trim(), "");
18147
+ }
18148
+ if (iteration.continuation) {
18149
+ lines.push(
18150
+ "**Harness Continuation**",
18151
+ "",
18152
+ jsonBlock(iteration.continuation),
18153
+ ""
18154
+ );
18155
+ }
18156
+ if (iteration.result !== void 0) {
18157
+ lines.push("**Runtime Result**", "", jsonBlock(iteration.result), "");
18158
+ }
18159
+ if (iteration.error) {
18160
+ lines.push("**Error**", "", iteration.error, "");
18161
+ }
18162
+ }
18163
+ if (turn.completed) {
18164
+ lines.push(
18165
+ "**Turn Final Response**",
18166
+ "",
18167
+ turn.completed.responseText || "_No reply_",
18168
+ ""
18169
+ );
18170
+ if (turn.completed.actionSummary.length) {
18171
+ lines.push("**Turn Final Actions**", "");
18172
+ for (const action of turn.completed.actionSummary)
18173
+ lines.push(`- ${action}`);
18174
+ lines.push("");
18175
+ }
18176
+ }
18177
+ if (turn.error) {
18178
+ lines.push("**Turn Error**", "", turn.error, "");
18179
+ }
18180
+ }
18181
+ lines.push("", "## System Prompts", "");
18182
+ if (!systemPrompts.length) {
18183
+ lines.push("_No system prompts captured._", "");
18184
+ } else {
18185
+ for (const { turn, iteration } of systemPrompts) {
18186
+ lines.push(
18187
+ `### Turn ${turn.turnNumber}, Generation ${iteration.iteration}`,
18188
+ "",
18189
+ fenced(iteration.systemPrompt, "text"),
18190
+ ""
18191
+ );
18192
+ }
18193
+ }
18194
+ return `${lines.join("\n")}
18195
+ `;
18196
+ }
18197
+ async function writeSessionLogReport(input) {
18198
+ const logsDir = path.join(input.artifactDir, "logs");
18199
+ await ensureDir(logsDir);
18200
+ await writeFile(
18201
+ path.join(logsDir, `${slugify(input.conversation.label)}.md`),
18202
+ buildSessionLogReport(input)
18203
+ );
18204
+ }
18205
+ function findTurnLog(conversation, turnDir) {
18206
+ return conversation.logTurns.find((turn) => turn.turnDir === turnDir);
18207
+ }
18208
+ function latestIterationLog(turn) {
18209
+ return turn?.iterations[turn.iterations.length - 1];
18210
+ }
16475
18211
  async function applySetup(setup, context) {
16476
18212
  if (!setup) return;
16477
18213
  if (setup.manifest) {
@@ -16556,7 +18292,7 @@ async function runAgentEvalSuite(options) {
16556
18292
  inspect: async (code) => {
16557
18293
  const session = conversation.environment;
16558
18294
  const job = await session.submitJob(code);
16559
- return withTimeout(
18295
+ return withTimeout2(
16560
18296
  job.result,
16561
18297
  9e4,
16562
18298
  `inspection job ${job.id}`
@@ -16631,6 +18367,7 @@ async function runAgentEvalSuite(options) {
16631
18367
  actionSummary: lastStep.actionSummary,
16632
18368
  promptInteractions: lastStep.promptInteractions,
16633
18369
  verification: lastStep.inspectionResults.length <= 1 ? lastStep.inspectionResults[0] ?? null : lastStep.inspectionResults,
18370
+ tokenUsage: aggregateConversationTokenUsage(conversation),
16634
18371
  steps: stepResults,
16635
18372
  turnDir: conversation.artifactDir
16636
18373
  };
@@ -16646,9 +18383,22 @@ async function runAgentEvalSuite(options) {
16646
18383
  path.join(conversation.artifactDir, "REPORT.md"),
16647
18384
  buildResultReport(result)
16648
18385
  );
18386
+ await writeSessionLogReport({
18387
+ artifactDir: options.harness.artifactDir,
18388
+ conversation,
18389
+ result
18390
+ });
16649
18391
  finalResult = result;
16650
18392
  } catch (error) {
16651
18393
  const failureMessage = error instanceof Error ? error.message : String(error);
18394
+ const failedTurn = conversation.logTurns[conversation.logTurns.length - 1];
18395
+ if (failedTurn && !failedTurn.completed) {
18396
+ failedTurn.error = failureMessage;
18397
+ const failedIteration = latestIterationLog(failedTurn);
18398
+ if (failedIteration && !failedIteration.responseText) {
18399
+ failedIteration.error = failureMessage;
18400
+ }
18401
+ }
16652
18402
  if (attempt < 2 && isTransientEvalError(error)) {
16653
18403
  await options.harness.closeConversation(conversation);
16654
18404
  continue;
@@ -16661,6 +18411,7 @@ async function runAgentEvalSuite(options) {
16661
18411
  actionSummary: [],
16662
18412
  promptInteractions: [],
16663
18413
  verification: null,
18414
+ tokenUsage: aggregateConversationTokenUsage(conversation),
16664
18415
  turnDir: path.join(options.harness.artifactDir, scenario.id),
16665
18416
  error: failureMessage
16666
18417
  };
@@ -16671,6 +18422,12 @@ async function runAgentEvalSuite(options) {
16671
18422
  path.join(failed.turnDir, "REPORT.md"),
16672
18423
  buildResultReport(failed)
16673
18424
  );
18425
+ await writeSessionLogReport({
18426
+ artifactDir: options.harness.artifactDir,
18427
+ conversation,
18428
+ result: failed,
18429
+ error: failureMessage
18430
+ });
16674
18431
  finalResult = failed;
16675
18432
  } finally {
16676
18433
  await options.harness.closeConversation(conversation);
@@ -16685,6 +18442,7 @@ async function runAgentEvalSuite(options) {
16685
18442
  actionSummary: [],
16686
18443
  promptInteractions: [],
16687
18444
  verification: null,
18445
+ tokenUsage: emptyTokenUsage(),
16688
18446
  turnDir: path.join(options.harness.artifactDir, scenario.id),
16689
18447
  error: "Scenario ended without a result."
16690
18448
  };
@@ -16699,6 +18457,11 @@ async function runAgentEvalSuite(options) {
16699
18457
  path.join(options.harness.artifactDir, "REPORT_INDEX.md"),
16700
18458
  buildSuiteIndex(results)
16701
18459
  );
18460
+ await ensureDir(path.join(options.harness.artifactDir, "logs"));
18461
+ await writeFile(
18462
+ path.join(options.harness.artifactDir, "logs", "README.md"),
18463
+ buildLogsIndex(results)
18464
+ );
16702
18465
  return { artifactDir: options.harness.artifactDir, results };
16703
18466
  }
16704
18467
  function createAgentEvalHarness(options) {
@@ -16731,6 +18494,10 @@ function createAgentEvalHarness(options) {
16731
18494
  promptEvents.push({ prompt, receivedAt: Date.now() });
16732
18495
  };
16733
18496
  environment.on("prompt", promptHandler);
18497
+ for (let attempt = 0; attempt < 12; attempt += 1) {
18498
+ if (environment.getEffects().length > 0) break;
18499
+ await sleep2(250);
18500
+ }
16734
18501
  await ensureDir(path.join(artifactDir, slugify(label)));
16735
18502
  return {
16736
18503
  label,
@@ -16738,7 +18505,8 @@ function createAgentEvalHarness(options) {
16738
18505
  history: [],
16739
18506
  promptEvents,
16740
18507
  artifactDir: path.join(artifactDir, slugify(label)),
16741
- turnCount: 0
18508
+ turnCount: 0,
18509
+ logTurns: []
16742
18510
  };
16743
18511
  }
16744
18512
  async function closeConversation(conversation) {
@@ -16752,7 +18520,7 @@ function createAgentEvalHarness(options) {
16752
18520
  }
16753
18521
  async function runCheckJob(code, session) {
16754
18522
  const job = await session.submitJob(code);
16755
- return withTimeout(job.result, jobTimeoutMs, `check job ${job.id}`);
18523
+ return withTimeout2(job.result, jobTimeoutMs, `check job ${job.id}`);
16756
18524
  }
16757
18525
  function buildCheckContext(conversation, completed, turnDir) {
16758
18526
  const liveDoc = cloneJson(conversation.environment.document);
@@ -16776,14 +18544,26 @@ function createAgentEvalHarness(options) {
16776
18544
  };
16777
18545
  }
16778
18546
  async function runInspection(conversation, inspection, completed, turnDir) {
16779
- const result = await runCheckJob(inspection.code, conversation.environment);
16780
- const text = JSON.stringify(result, null, 2);
16781
- assertMatches(
16782
- `Verification for ${conversation.label}`,
16783
- text,
16784
- inspection.includes,
16785
- inspection.excludes
16786
- );
18547
+ let result = null;
18548
+ let lastError = null;
18549
+ for (let attempt = 0; attempt < 10; attempt += 1) {
18550
+ result = await runCheckJob(inspection.code, conversation.environment);
18551
+ const text = JSON.stringify(result, null, 2);
18552
+ try {
18553
+ assertMatches(
18554
+ `Verification for ${conversation.label}`,
18555
+ text,
18556
+ inspection.includes,
18557
+ inspection.excludes
18558
+ );
18559
+ lastError = null;
18560
+ break;
18561
+ } catch (error) {
18562
+ lastError = error;
18563
+ await sleep2(250);
18564
+ }
18565
+ }
18566
+ if (lastError) throw lastError;
16787
18567
  if (inspection.check) {
16788
18568
  await inspection.check({
16789
18569
  ...buildCheckContext(conversation, completed, turnDir),
@@ -16809,6 +18589,11 @@ function createAgentEvalHarness(options) {
16809
18589
  message: prompt.message,
16810
18590
  answer
16811
18591
  });
18592
+ const turnLog = findTurnLog(pending.conversation, pending.turnDir);
18593
+ const iterationLog = latestIterationLog(turnLog);
18594
+ if (iterationLog) {
18595
+ iterationLog.promptInteractions = pending.promptInteractions;
18596
+ }
16812
18597
  const resumed = await waitForJobOutcome({
16813
18598
  environment: pending.conversation.environment,
16814
18599
  job: pending.job,
@@ -16832,6 +18617,7 @@ function createAgentEvalHarness(options) {
16832
18617
  jobId: pending.job.id,
16833
18618
  result: resumed.result,
16834
18619
  stdout: [...pending.stdout, ...resumed.stdout],
18620
+ agentMessages: getJobAgentMessages(liveDoc, pending.job.id),
16835
18621
  sessionHeap: normalizeHeapSnapshot2(asRecord5(liveDoc?.heap))
16836
18622
  });
16837
18623
  const responseText = presentation.responseText || pending.finalReply || "Done.";
@@ -16849,6 +18635,23 @@ function createAgentEvalHarness(options) {
16849
18635
  promptInteractions: pending.promptInteractions,
16850
18636
  result: resumed.result
16851
18637
  });
18638
+ const actionSummary = getActionSummary(liveDoc, pending.job.id);
18639
+ if (iterationLog) {
18640
+ iterationLog.responseText = responseText;
18641
+ iterationLog.terminalKind = getCurrentClosureId(liveDoc) ? "closure" : "reply";
18642
+ iterationLog.actionSummary = actionSummary;
18643
+ iterationLog.promptInteractions = pending.promptInteractions;
18644
+ iterationLog.result = resumed.result;
18645
+ }
18646
+ if (turnLog) {
18647
+ turnLog.completed = {
18648
+ responseText,
18649
+ terminalKind: getCurrentClosureId(liveDoc) ? "closure" : "reply",
18650
+ actionSummary,
18651
+ promptInteractions: pending.promptInteractions,
18652
+ result: resumed.result
18653
+ };
18654
+ }
16852
18655
  return {
16853
18656
  conversation: pending.conversation,
16854
18657
  request: pending.request,
@@ -16856,7 +18659,7 @@ function createAgentEvalHarness(options) {
16856
18659
  responseText,
16857
18660
  terminalKind: getCurrentClosureId(liveDoc) ? "closure" : "reply",
16858
18661
  finalCode: pending.finalCode,
16859
- actionSummary: getActionSummary(liveDoc, pending.job.id),
18662
+ actionSummary,
16860
18663
  promptInteractions: pending.promptInteractions,
16861
18664
  verification: null,
16862
18665
  result: resumed.result
@@ -16868,6 +18671,14 @@ function createAgentEvalHarness(options) {
16868
18671
  const turnId = `turn-${String(turnNumber).padStart(2, "0")}-${slugify(input.request.slice(0, 48))}`;
16869
18672
  const turnDir = path.join(conversation.artifactDir, turnId);
16870
18673
  await ensureDir(turnDir);
18674
+ const turnLog = {
18675
+ turnNumber,
18676
+ turnId,
18677
+ request: input.request,
18678
+ turnDir,
18679
+ iterations: []
18680
+ };
18681
+ conversation.logTurns.push(turnLog);
16871
18682
  if (input.prepareRecords?.length) {
16872
18683
  await conversation.environment.recordObjects(input.prepareRecords);
16873
18684
  }
@@ -16906,6 +18717,24 @@ function createAgentEvalHarness(options) {
16906
18717
  const workflowFocus = projectWorkflowFocus(liveDoc, pendingPrompts, {
16907
18718
  boundaryTimestamp
16908
18719
  });
18720
+ const referentFocus = projectConversationReferentFocus(liveDoc);
18721
+ const heapFocus = {
18722
+ variableNames: [
18723
+ ...workflowFocus.variableNames,
18724
+ ...referentFocus.variableNames
18725
+ ],
18726
+ listNames: [...workflowFocus.listNames, ...referentFocus.listNames],
18727
+ entryPaths: [...workflowFocus.entryPaths, ...referentFocus.entryPaths]
18728
+ };
18729
+ const tools = conversation.environment.getEffects().map((tool) => ({
18730
+ name: tool.name,
18731
+ description: tool.description,
18732
+ className: tool.className,
18733
+ static: tool.static,
18734
+ ready: tool.ready,
18735
+ inputSchema: tool.inputSchema,
18736
+ outputSchema: tool.outputSchema
18737
+ }));
16909
18738
  const systemPrompt = buildGranularAgentSystemPrompt({
16910
18739
  domainDocumentation: await conversation.environment.getDomainDocumentation(),
16911
18740
  sessionContext: {
@@ -16913,37 +18742,44 @@ function createAgentEvalHarness(options) {
16913
18742
  environmentId: conversation.environment.environmentId,
16914
18743
  domainRevision: conversation.environment.domainRevision
16915
18744
  },
16916
- heapSummary: projectHeapSummary(liveDoc, {
16917
- focus: workflowFocus
18745
+ heapSummary: projectHeapSummary(asRecord5(liveDoc?.heap), {
18746
+ focus: heapFocus
16918
18747
  }),
18748
+ referentSummary: projectConversationReferentSummary(liveDoc),
16919
18749
  loopSummary: projectLoopSummary(liveDoc, pendingPrompts, {
16920
18750
  boundaryTimestamp
16921
18751
  }),
16922
18752
  workflowSummary: projectWorkflowSummary(liveDoc, pendingPrompts, {
16923
18753
  boundaryTimestamp
16924
18754
  }),
16925
- tools: conversation.environment.getEffects().map((tool) => ({
16926
- name: tool.name,
16927
- description: tool.description,
16928
- className: tool.className,
16929
- static: tool.static,
16930
- ready: tool.ready
16931
- })),
18755
+ tools,
16932
18756
  checkpoint: latestCheckpoint
16933
18757
  });
16934
18758
  const request = iteration === 0 ? input.request : buildContinuationInstruction(
16935
18759
  buildContinuationPreview(latestCheckpoint, noProgressCount)
16936
18760
  );
16937
- const generation = await withTimeout(
18761
+ const generation = await withTimeout2(
16938
18762
  generateTurnWithRepair(options.generator, {
16939
18763
  systemPrompt,
16940
18764
  history: buildHistory(conversation.history),
16941
18765
  request,
16942
- attempt: 1
18766
+ attempt: 1,
18767
+ tools
16943
18768
  }),
16944
18769
  chatTimeoutMs,
16945
18770
  `chat generation for ${conversation.label} iteration ${iteration + 1}`
16946
18771
  );
18772
+ const iterationLog = {
18773
+ iteration: iteration + 1,
18774
+ request,
18775
+ systemPrompt,
18776
+ generationReply: generation.reply,
18777
+ generatedCode: generation.code,
18778
+ rawGeneration: generation.raw,
18779
+ generationAttempts: generation.generationAttempts,
18780
+ tokenUsage: tokenUsageForGenerationOutput(generation)
18781
+ };
18782
+ turnLog.iterations.push(iterationLog);
16947
18783
  await writeJson(
16948
18784
  path.join(turnDir, `iteration-${iteration + 1}-generation.json`),
16949
18785
  generation
@@ -16970,11 +18806,39 @@ function createAgentEvalHarness(options) {
16970
18806
  turnDir
16971
18807
  );
16972
18808
  }
18809
+ iterationLog.responseText = responseText2;
18810
+ iterationLog.terminalKind = "reply";
18811
+ iterationLog.actionSummary = [];
18812
+ iterationLog.promptInteractions = [];
18813
+ iterationLog.result = completed.result;
18814
+ turnLog.completed = {
18815
+ responseText: responseText2,
18816
+ terminalKind: "reply",
18817
+ actionSummary: [],
18818
+ promptInteractions: [],
18819
+ result: completed.result
18820
+ };
16973
18821
  await writeJson(path.join(turnDir, "result.json"), completed);
16974
18822
  return completed;
16975
18823
  }
16976
18824
  const session = conversation.environment;
16977
- const job = await session.submitJob(generation.code);
18825
+ const job = await session.submitJob(generation.code, {
18826
+ agent: {
18827
+ userRequest: input.request,
18828
+ generationRequest: request,
18829
+ systemPrompt,
18830
+ history: buildHistory(conversation.history),
18831
+ scenarioLabel: conversation.label,
18832
+ turnId,
18833
+ iteration: iteration + 1,
18834
+ tools,
18835
+ generationReply: generation.reply,
18836
+ rawGeneration: generation.raw,
18837
+ repairIssues: generation.generationAttempts?.flatMap(
18838
+ (attempt) => attempt.repairIssues || []
18839
+ )
18840
+ }
18841
+ });
16978
18842
  const outcome = await waitForJobOutcome({
16979
18843
  environment: conversation.environment,
16980
18844
  job,
@@ -17030,6 +18894,13 @@ function createAgentEvalHarness(options) {
17030
18894
  turnDir
17031
18895
  );
17032
18896
  }
18897
+ turnLog.completed = {
18898
+ responseText: resumed.responseText,
18899
+ terminalKind: resumed.terminalKind,
18900
+ actionSummary: resumed.actionSummary,
18901
+ promptInteractions: resumed.promptInteractions,
18902
+ result: resumed.result
18903
+ };
17033
18904
  return resumed;
17034
18905
  }
17035
18906
  }
@@ -17047,6 +18918,7 @@ function createAgentEvalHarness(options) {
17047
18918
  jobId: job.id,
17048
18919
  result: outcome.result,
17049
18920
  stdout: outcome.stdout,
18921
+ agentMessages: getJobAgentMessages(settledLiveDoc, job.id),
17050
18922
  sessionHeap
17051
18923
  });
17052
18924
  const responseText = presentation.responseText || generation.reply?.trim() || "Done.";
@@ -17100,6 +18972,12 @@ function createAgentEvalHarness(options) {
17100
18972
  result: outcome.result
17101
18973
  }
17102
18974
  );
18975
+ iterationLog.responseText = responseText;
18976
+ iterationLog.terminalKind = getCurrentClosureId(settledLiveDoc) ? "closure" : "reply";
18977
+ iterationLog.actionSummary = latestCheckpoint.latestActionSummary || [];
18978
+ iterationLog.promptInteractions = [];
18979
+ iterationLog.continuation = continuation;
18980
+ iterationLog.result = outcome.result;
17103
18981
  if (!continuation.shouldContinue) {
17104
18982
  const completed = {
17105
18983
  conversation,
@@ -17121,6 +18999,13 @@ function createAgentEvalHarness(options) {
17121
18999
  turnDir
17122
19000
  );
17123
19001
  }
19002
+ turnLog.completed = {
19003
+ responseText,
19004
+ terminalKind: completed.terminalKind,
19005
+ actionSummary: completed.actionSummary,
19006
+ promptInteractions: [],
19007
+ result: outcome.result
19008
+ };
17124
19009
  await writeJson(path.join(turnDir, "result.json"), completed);
17125
19010
  return completed;
17126
19011
  }