@octocodeai/freellama-mcp-server 0.6.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CONFIGURATION.md +11 -3
  2. package/DEVELOPING.md +6 -6
  3. package/README.md +224 -402
  4. package/dist/index.js +11714 -4251
  5. package/dist/research/bash-agent.js +1495 -71
  6. package/dist/research/coder-agent.js +4668 -0
  7. package/dist/research/github-agent.js +2923 -0
  8. package/dist/research/octocode-agent.js +1407 -116
  9. package/docs/ARCHITECTURE.md +101 -45
  10. package/docs/BUILD_AND_NOTES.md +31 -0
  11. package/docs/CLI.md +76 -27
  12. package/docs/COMPARISON.md +40 -0
  13. package/docs/CPU_GPU_ROUTING.md +127 -25
  14. package/docs/DEFERRED_JOBS.md +79 -0
  15. package/docs/FEATURES.md +303 -0
  16. package/docs/LOCAL_MODELS.md +190 -0
  17. package/docs/MODEL_FORMS_AND_RESULTS.md +55 -0
  18. package/docs/MODEL_INTROSPECTION.md +128 -0
  19. package/docs/MODEL_SELECTION.md +74 -22
  20. package/docs/MONITORING.md +115 -33
  21. package/docs/PRODUCTION.md +76 -13
  22. package/docs/README.md +11 -6
  23. package/docs/SCOPES_AND_WARMING.md +55 -7
  24. package/docs/TESTING.md +24 -2
  25. package/docs/TUNING.md +86 -0
  26. package/docs/cookbooks/README.md +22 -0
  27. package/docs/cookbooks/batch-text.md +19 -0
  28. package/docs/cookbooks/classify.md +25 -0
  29. package/docs/cookbooks/coding.md +17 -0
  30. package/docs/cookbooks/duplicates.md +17 -0
  31. package/docs/cookbooks/find-code.md +21 -0
  32. package/docs/cookbooks/image-recognition.md +15 -0
  33. package/docs/cookbooks/image-search.md +13 -0
  34. package/docs/cookbooks/jobs.md +60 -0
  35. package/docs/cookbooks/memory-and-caching.md +31 -0
  36. package/docs/cookbooks/ocr.md +17 -0
  37. package/docs/cookbooks/research-agent.md +27 -0
  38. package/docs/cookbooks/speech-to-text.md +19 -0
  39. package/docs/cookbooks/sweep.md +66 -0
  40. package/docs/cookbooks/text-to-speech.md +15 -0
  41. package/docs/octocode/GITHUB.md +30 -0
  42. package/docs/octocode/LOCAL.md +29 -0
  43. package/native/index.d.ts +1 -11
  44. package/native/index.js +18 -34
  45. package/package.json +13 -15
@@ -1,13 +1,10 @@
1
- // src/research/bash-agent.ts
2
- import { pathToFileURL } from "node:url";
3
-
4
1
  // src/research/adapter.ts
5
2
  function isPositiveInt(value) {
6
3
  return (typeof value === "boolean" || typeof value === "number" && Number.isInteger(value)) && Number(value) >= 1;
7
4
  }
8
5
 
9
6
  // src/research/agent-loop.ts
10
- import { readFileSync as readFileSync4 } from "node:fs";
7
+ import { readFileSync as readFileSync5 } from "node:fs";
11
8
  import { performance } from "node:perf_hooks";
12
9
  import { setTimeout as sleep } from "node:timers/promises";
13
10
 
@@ -57,8 +54,21 @@ function isSystemError(error) {
57
54
  var ToolTimeoutError = class extends Error {
58
55
  name = "ToolTimeoutError";
59
56
  };
57
+ var ToolExitError = class extends Error {
58
+ constructor(exitCode, output, status = "error") {
59
+ super(`${status === "empty" ? "nothing found" : "command failed"} (exit code ${exitCode})` + (output ? `
60
+ ${output}` : ""));
61
+ this.exitCode = exitCode;
62
+ this.output = output;
63
+ this.status = status;
64
+ }
65
+ exitCode;
66
+ output;
67
+ status;
68
+ name = "ToolExitError";
69
+ };
60
70
  function isToolError(error) {
61
- return error instanceof ValidationError || error instanceof ToolTimeoutError || error instanceof SyntaxError || isSystemError(error);
71
+ return error instanceof ValidationError || error instanceof ToolTimeoutError || error instanceof ToolExitError || error instanceof SyntaxError || isSystemError(error);
62
72
  }
63
73
 
64
74
  // src/research/actions.ts
@@ -362,27 +372,44 @@ var estimateText = (value) => canonicalJson(value, { sortKeys: false, ensureAsci
362
372
  function estimateTokens(text, charsPerToken = CHARS_PER_TOKEN) {
363
373
  return Math.floor(cpLength(text) / charsPerToken) + 1;
364
374
  }
365
- function messagesTokens(messages, policy = new ContextPolicy()) {
375
+ var UNMEASURED_BYTES_PER_TOKEN_MILLI = 3e3;
376
+ function platformBytesPerTokenMilli(learnedMilli) {
377
+ if (learnedMilli === null || learnedMilli === void 0) return UNMEASURED_BYTES_PER_TOKEN_MILLI;
378
+ return Math.min(Math.max(Math.floor(learnedMilli / 10) * 9, 1e3), 8e3);
379
+ }
380
+ function platformTextTokens(text, learnedMilli) {
381
+ return Math.ceil(Buffer.byteLength(text, "utf8") * 1e3 / platformBytesPerTokenMilli(learnedMilli));
382
+ }
383
+ function messagesTokens(messages, policy = new ContextPolicy(), learnedMilli = null) {
366
384
  let total = 0;
367
385
  for (const message of messages) {
368
- const { content = "", images = [], ...metadata } = message;
369
- total += estimateTokens(typeof content === "string" ? content : estimateText(content), policy.chars_per_token);
386
+ const { images = [], ...text } = message;
370
387
  if (Array.isArray(images)) total += images.length * policy.image_token_estimate;
388
+ if (learnedMilli !== null) {
389
+ total += platformTextTokens(JSON.stringify(text), learnedMilli) + 1;
390
+ continue;
391
+ }
392
+ const { content = "", ...metadata } = text;
393
+ total += estimateTokens(typeof content === "string" ? content : estimateText(content), policy.chars_per_token);
371
394
  total += estimateTokens(estimateText(metadata), policy.chars_per_token);
372
395
  }
373
396
  return total;
374
397
  }
375
398
  var ContextBudgeter = class {
376
399
  policy;
400
+ platformRatio;
377
401
  scale;
378
402
  calibration_samples;
379
403
  constructor(policy = new ContextPolicy(), options = {}) {
380
404
  this.policy = policy;
381
- this.scale = Math.max(options.initialScale ?? 1, 1);
382
- this.calibration_samples = Math.max(Math.trunc(options.calibrationSamples ?? 0), 0);
405
+ this.platformRatio = options.platformRatio ?? null;
406
+ this.scale = this.platformRatio ? 1 : Math.max(options.initialScale ?? 1, 1);
407
+ this.calibration_samples = this.platformRatio ? 0 : Math.max(Math.trunc(options.calibrationSamples ?? 0), 0);
383
408
  }
384
- estimate = (messages) => Math.ceil(messagesTokens(messages, this.policy) * this.scale);
409
+ estimate = (messages) => this.platformRatio ? messagesTokens(messages, this.policy, this.platformRatio.bytes_per_token_milli) : Math.ceil(messagesTokens(messages, this.policy) * this.scale);
410
+ /** Calibrate the fallback estimate; returns whether the multiplier rose (and should persist). */
385
411
  observe(messages, promptEvalCount) {
412
+ if (this.platformRatio) return false;
386
413
  if (typeof promptEvalCount !== "number" || !Number.isFinite(promptEvalCount)) return false;
387
414
  const actual = Math.trunc(promptEvalCount);
388
415
  if (actual <= 0) return false;
@@ -393,10 +420,14 @@ var ContextBudgeter = class {
393
420
  return this.scale > previous;
394
421
  }
395
422
  metadata() {
423
+ const ratio = this.platformRatio;
396
424
  return {
397
- token_counting: this.calibration_samples ? "model_calibrated_estimate" : "configured_estimate",
425
+ token_counting: ratio ? "platform_learned_ratio" : "local_chars_per_token",
398
426
  estimate_scale: Math.round(this.scale * 1e6) / 1e6,
399
- calibration_samples: this.calibration_samples
427
+ calibration_samples: this.calibration_samples,
428
+ bytes_per_token_milli: ratio?.bytes_per_token_milli ?? null,
429
+ bytes_per_token_milli_with_margin: ratio ? platformBytesPerTokenMilli(ratio.bytes_per_token_milli) : null,
430
+ bytes_per_token_samples: ratio?.bytes_per_token_samples ?? null
400
431
  };
401
432
  }
402
433
  };
@@ -444,10 +475,10 @@ function compact(content, policy) {
444
475
  }
445
476
  var textContent = (message) => typeof message?.content === "string" ? message.content : null;
446
477
  var contentLength = (message) => cpLength(textContent(message) ?? "");
447
- function widestIndex(indexes, messages) {
478
+ function widestIndex(indexes2, messages) {
448
479
  let best;
449
480
  let bestLength = -1;
450
- for (const index of indexes) {
481
+ for (const index of indexes2) {
451
482
  const length = contentLength(messages[index]);
452
483
  if (length > bestLength) {
453
484
  best = index;
@@ -542,11 +573,16 @@ var AgentContextManager = class {
542
573
  constructor(runtime, options = {}) {
543
574
  this.runtime = runtime;
544
575
  this.model = options.model ?? "";
545
- this.calibrationDir = options.calibrationDir ?? null;
576
+ const platformRatio = options.platformRatio ?? null;
577
+ this.calibrationDir = platformRatio ? null : options.calibrationDir ?? null;
546
578
  this.calibrationIdentity = options.calibrationIdentity ?? "";
547
579
  const loaded = this.loadCalibration();
548
- this.budgeter = new ContextBudgeter(runtime.context, { initialScale: loaded?.scale ?? 1, calibrationSamples: loaded?.samples ?? 0 });
549
- this.calibrationSource = loaded ? "persistent_model_cache" : "current_process";
580
+ this.budgeter = new ContextBudgeter(runtime.context, {
581
+ initialScale: loaded?.scale ?? 1,
582
+ calibrationSamples: loaded?.samples ?? 0,
583
+ platformRatio
584
+ });
585
+ this.calibrationSource = platformRatio ? "not_applied" : loaded ? "persistent_model_cache" : "current_process";
550
586
  }
551
587
  modelKey() {
552
588
  return createHash("sha256").update(this.calibrationContractJson(), "utf8").digest("hex").slice(0, 24);
@@ -840,8 +876,27 @@ ${body}${footer}`;
840
876
  }
841
877
  };
842
878
 
879
+ // src/research/progress-events.ts
880
+ var PROGRESS_ENV = "FREELLAMA_AGENT_PROGRESS";
881
+ var PROGRESS_PREFIX = "@freellama-progress ";
882
+ function progressEmitter(env, write = (line) => {
883
+ process.stderr.write(line);
884
+ }) {
885
+ if (env[PROGRESS_ENV] !== "stderr") return void 0;
886
+ return (event) => {
887
+ try {
888
+ write(`${PROGRESS_PREFIX}${JSON.stringify(event)}
889
+ `);
890
+ } catch {
891
+ }
892
+ };
893
+ }
894
+
843
895
  // src/research/transport.ts
844
896
  import { createHash as createHash2 } from "node:crypto";
897
+ import { readFileSync as readFileSync4 } from "node:fs";
898
+ import { homedir } from "node:os";
899
+ import path3 from "node:path";
845
900
 
846
901
  // src/research/transport-http.ts
847
902
  import { readFileSync as readFileSync3 } from "node:fs";
@@ -903,13 +958,21 @@ function httpRequest(url, options) {
903
958
  if (options.body) headers["content-length"] = String(options.body.length);
904
959
  let sent = false;
905
960
  let settled = false;
961
+ const { signal } = options;
962
+ const onAbort = () => fail(signal?.reason instanceof Error ? signal.reason : new Error("request aborted"));
906
963
  const fail = (error) => {
907
964
  if (settled) return;
908
965
  settled = true;
966
+ signal?.removeEventListener("abort", onAbort);
909
967
  request.destroy();
910
968
  reject(error);
911
969
  };
970
+ if (signal?.aborted) {
971
+ reject(signal.reason instanceof Error ? signal.reason : new Error("request aborted"));
972
+ return;
973
+ }
912
974
  const request = client.request(parsed, { method: options.method, headers });
975
+ signal?.addEventListener("abort", onAbort, { once: true });
913
976
  request.setTimeout(Math.max(options.timeoutSeconds * 1e3, 1), () => {
914
977
  fail(sent ? new RequestTimeoutError() : new URLError(new RequestTimeoutError()));
915
978
  });
@@ -926,6 +989,7 @@ function httpRequest(url, options) {
926
989
  const finish = () => {
927
990
  if (settled) return;
928
991
  settled = true;
992
+ signal?.removeEventListener("abort", onAbort);
929
993
  const body = Buffer.concat(chunks);
930
994
  request.destroy();
931
995
  if (ok) resolve(body);
@@ -957,11 +1021,25 @@ async function requestJson(url, payload, timeoutSeconds) {
957
1021
  var MANAGED_SUFFIX = "/_freellama/v1/tasks";
958
1022
  var IDENTITY_LIMIT = 1024 * 1024;
959
1023
  var trimSlashes = (text) => text.replace(/\/+$/, "");
960
- async function readIdentityJson(url) {
961
- const payload = await httpRequest(url, { method: "GET", headers: requestHeaders(), timeoutSeconds: 5, limit: IDENTITY_LIMIT + 1 });
1024
+ async function readCatalogJson(url, request = httpRequest) {
1025
+ const payload = await request(url, { method: "GET", headers: requestHeaders(), timeoutSeconds: 5, limit: IDENTITY_LIMIT + 1 });
962
1026
  if (payload.length > IDENTITY_LIMIT) throw new ValidationError("model identity catalog exceeds size limit");
963
1027
  return parseJsonBody(payload, "model identity catalog");
964
1028
  }
1029
+ function catalogEntry(payload, model) {
1030
+ const entries = isRecord(payload) ? payload.models ?? [] : [];
1031
+ if (!Array.isArray(entries)) return null;
1032
+ const names = /* @__PURE__ */ new Set([model]);
1033
+ const lastSegment = model.slice(model.lastIndexOf("/") + 1);
1034
+ if (!lastSegment.includes(":")) names.add(model + ":latest");
1035
+ for (const item of entries) {
1036
+ if (!isRecord(item)) continue;
1037
+ const name = Object.hasOwn(item, "name") ? item.name : item.model;
1038
+ if (typeof name === "object" && name !== null) return "ambiguous";
1039
+ if (typeof name === "string" && names.has(name)) return item;
1040
+ }
1041
+ return null;
1042
+ }
965
1043
  var DIGEST = /^(?:sha256:)?[0-9a-fA-F]{64}$/;
966
1044
  var sha256 = (data) => createHash2("sha256").update(data, "utf8").digest("hex");
967
1045
  async function resolveModelIdentity(endpointInput, model, systemPrompt, options = {}) {
@@ -969,22 +1047,8 @@ async function resolveModelIdentity(endpointInput, model, systemPrompt, options
969
1047
  const url = endpoint.endsWith(MANAGED_SUFFIX) ? endpoint.slice(0, -MANAGED_SUFFIX.length) + "/_freellama/v1/models" : endpoint + "/api/tags";
970
1048
  const unknown = { identity: "", verified: false, scope: "current_process_only" };
971
1049
  try {
972
- const payload = await (options.readJson ?? readIdentityJson)(url);
973
- const entries = isRecord(payload) ? payload.models ?? [] : [];
974
- if (!Array.isArray(entries)) return unknown;
975
- const names = /* @__PURE__ */ new Set([model]);
976
- const lastSegment = model.slice(model.lastIndexOf("/") + 1);
977
- if (!lastSegment.includes(":")) names.add(model + ":latest");
978
- let entry = null;
979
- for (const item of entries) {
980
- if (!isRecord(item)) continue;
981
- const name = Object.hasOwn(item, "name") ? item.name : item.model;
982
- if (typeof name === "object" && name !== null) return unknown;
983
- if (typeof name === "string" && names.has(name)) {
984
- entry = item;
985
- break;
986
- }
987
- }
1050
+ const entry = catalogEntry(await (options.readJson ?? readCatalogJson)(url), model);
1051
+ if (entry === "ambiguous") return unknown;
988
1052
  const digestValue = entry?.digest;
989
1053
  if (typeof digestValue !== "string" || !DIGEST.test(digestValue)) return unknown;
990
1054
  const digest = (digestValue.startsWith("sha256:") ? digestValue.slice("sha256:".length) : digestValue).toLowerCase();
@@ -1002,11 +1066,36 @@ async function resolveModelIdentity(endpointInput, model, systemPrompt, options
1002
1066
  throw error;
1003
1067
  }
1004
1068
  }
1069
+ async function resolvePlatformTokenRatio(serveEndpoint, model, options = {}) {
1070
+ try {
1071
+ const url = `${trimSlashes(serveEndpoint)}/_freellama/v1/models`;
1072
+ const entry = catalogEntry(await (options.readJson ?? readCatalogJson)(url), model);
1073
+ if (entry === null || entry === "ambiguous") return null;
1074
+ const milli = entry.bytes_per_token_milli;
1075
+ if (typeof milli !== "number" || !Number.isSafeInteger(milli) || milli <= 0) return null;
1076
+ const samples = entry.bytes_per_token_samples;
1077
+ return {
1078
+ bytes_per_token_milli: milli,
1079
+ bytes_per_token_samples: typeof samples === "number" && Number.isSafeInteger(samples) && samples > 0 ? samples : 1
1080
+ };
1081
+ } catch {
1082
+ return null;
1083
+ }
1084
+ }
1005
1085
  function isTransportError(error) {
1006
1086
  return error instanceof HTTPError || error instanceof URLError || error instanceof RequestTimeoutError;
1007
1087
  }
1008
1088
  function retryableChatError(error) {
1009
- if (error instanceof HTTPError) return error.code === 503;
1089
+ if (error instanceof HTTPError) {
1090
+ if (error.code !== 503 || error.body.length > RECEIPT_BODY_LIMIT) return false;
1091
+ try {
1092
+ const body = parseJsonBody(error.body, "retry receipt");
1093
+ if (!isRecord(body) || body.retry_safe === false || body.execution_outcome !== void 0 && body.execution_outcome !== "not_started") return false;
1094
+ return body.retry_safe === true && body.execution_outcome === "not_started" || ["admission_timeout", "resource_admission_unavailable", "upstream_circuit_open", "model_lifecycle_busy"].includes(String(body.code));
1095
+ } catch {
1096
+ return false;
1097
+ }
1098
+ }
1010
1099
  return error instanceof URLError && error.reason.code === "ECONNREFUSED";
1011
1100
  }
1012
1101
  var RECEIPT_FIELDS = /* @__PURE__ */ new Set([
@@ -1022,7 +1111,12 @@ var RECEIPT_FIELDS = /* @__PURE__ */ new Set([
1022
1111
  "route",
1023
1112
  "decision",
1024
1113
  "quality",
1025
- "feedback"
1114
+ "feedback",
1115
+ "execution_outcome",
1116
+ "retry_safe",
1117
+ "result_recoverable",
1118
+ "job_id",
1119
+ "last_observed_status"
1026
1120
  ]);
1027
1121
  var PAYLOAD_FIELDS = /* @__PURE__ */ new Set([
1028
1122
  "prompt",
@@ -1105,6 +1199,22 @@ var PromptCacheUsage = class {
1105
1199
  };
1106
1200
  }
1107
1201
  };
1202
+ var resolvedShift;
1203
+ function contextShift(env = process.env, reset = false) {
1204
+ if (resolvedShift !== void 0 && !reset) return resolvedShift;
1205
+ const read = (value2) => value2 === "ollama_default" || value2 === "off" ? value2 : void 0;
1206
+ let value = read(env.FREELLAMA_CONTEXT_SHIFT?.trim());
1207
+ if (!value) {
1208
+ const file = env.FREELLAMA_RUNTIME_CONFIG || path3.join(env.FREELLAMA_HOME || path3.join(homedir(), ".octocode", "freellama"), "runtime.toml");
1209
+ try {
1210
+ const topLevel = readFileSync4(file, "utf8").split(/^\s*\[/m)[0] ?? "";
1211
+ value = read(/^\s*context_shift\s*=\s*"([^"]*)"/m.exec(topLevel)?.[1]);
1212
+ } catch {
1213
+ }
1214
+ }
1215
+ resolvedShift = value ?? "off";
1216
+ return resolvedShift;
1217
+ }
1108
1218
  function chatRequest(endpointInput, model, messages, options, think, keepAlive, executionPreference = "auto", minPlacementEvidence = "configured") {
1109
1219
  const endpoint = trimSlashes(endpointInput);
1110
1220
  if (endpoint.endsWith(MANAGED_SUFFIX)) {
@@ -1130,7 +1240,7 @@ function chatRequest(endpointInput, model, messages, options, think, keepAlive,
1130
1240
  messages,
1131
1241
  stream: false,
1132
1242
  truncate: false,
1133
- shift: false,
1243
+ ...contextShift() === "ollama_default" ? {} : { shift: false },
1134
1244
  format: "json",
1135
1245
  think,
1136
1246
  keep_alive: keepAlive,
@@ -1152,9 +1262,322 @@ function unwrapChatResponse(payload, executionReceipts) {
1152
1262
  return response;
1153
1263
  }
1154
1264
 
1265
+ // src/research/transport-jobs.ts
1266
+ import { STATUS_CODES } from "node:http";
1267
+
1268
+ // src/job-progress.ts
1269
+ var TERMINAL = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "expired"]);
1270
+ var SUBMIT_FALLBACK_CODES = /* @__PURE__ */ new Set(["task_job_input_too_large", "task_jobs_full", "task_jobs_input_budget"]);
1271
+ var RETENTION_FAILURE_CODES = /* @__PURE__ */ new Set(["task_job_result_too_large", "task_job_error_too_large"]);
1272
+ var FIRST_POLL_MS = 250;
1273
+ var HEARTBEAT_MS = 1e4;
1274
+ var MAX_POLL_MS = 2e3;
1275
+ var MAX_POLL_FAILURES = 5;
1276
+ var CANCEL_ATTEMPTS = 3;
1277
+ var CANCEL_BACKOFF_MS = 250;
1278
+ var CANCEL_REPORT_MS = 5e3;
1279
+ var object = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
1280
+ var number = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
1281
+ var gigabytes = (bytes) => `${(bytes / 1e9).toFixed(1)} GB`;
1282
+ var seconds = (ms) => ms < 1e4 ? `${(ms / 1e3).toFixed(1)} s` : `${Math.round(ms / 1e3)} s`;
1283
+ function modelNames(value) {
1284
+ if (!Array.isArray(value)) return [];
1285
+ return value.flatMap((item) => {
1286
+ if (typeof item === "string") return [item];
1287
+ const named = object(item)?.model ?? object(item)?.name;
1288
+ return typeof named === "string" ? [named] : [];
1289
+ });
1290
+ }
1291
+ function evictedSuffix(detail) {
1292
+ const evicted = modelNames(detail?.evicted ?? detail?.evicted_idle_models);
1293
+ return evicted.length > 0 ? `, evicted ${evicted.join(", ")}` : "";
1294
+ }
1295
+ function holdersSuffix(detail) {
1296
+ const holders = Array.isArray(detail?.memory_holders) ? detail.memory_holders : [];
1297
+ const named = holders.map((holder) => holder).filter((holder) => typeof holder.name === "string" && typeof holder.resident_bytes === "number").slice(0, 3).map((holder) => {
1298
+ const count = typeof holder.processes === "number" && holder.processes > 1 ? ` \xD7${holder.processes}` : "";
1299
+ return `${holder.name}${count} ${gigabytes(holder.resident_bytes)}`;
1300
+ });
1301
+ return named.length > 0 ? `; largest memory users: ${named.join(", ")}` : "";
1302
+ }
1303
+ function jobProgressMessage(job) {
1304
+ const status = typeof job.status === "string" ? job.status : "queued";
1305
+ const reason = typeof job.reason === "string" ? job.reason : "";
1306
+ const detail = object(job.detail);
1307
+ const model = typeof job.selected_model === "string" && job.selected_model ? job.selected_model : void 0;
1308
+ switch (status) {
1309
+ case "queued":
1310
+ return "queued";
1311
+ case "waiting_for_admission":
1312
+ return reason === "resources_reserved_waiting_for_backend_capacity" ? "memory reserved; waiting for backend capacity" : "waiting for backend capacity";
1313
+ case "waiting_for_resources": {
1314
+ const need = number(detail?.required_available_bytes);
1315
+ const available = number(detail?.effective_available_bytes);
1316
+ if (need === void 0 && available === void 0) {
1317
+ return (reason === "checking_host_resources" ? "checking host memory" : "waiting for memory") + evictedSuffix(detail);
1318
+ }
1319
+ const sizes = [need !== void 0 ? `need ${gigabytes(need)}` : void 0, available !== void 0 ? `${gigabytes(available)} available` : void 0].filter(Boolean).join(", ");
1320
+ const pressure = Array.isArray(detail?.pressure_reasons) ? detail.pressure_reasons.map((item) => typeof item === "string" ? item : JSON.stringify(item)) : [];
1321
+ return `waiting for memory: ${sizes}${pressure.length > 0 ? ` (host: ${pressure.join(", ")})` : ""}${holdersSuffix(detail)}${evictedSuffix(detail)}`;
1322
+ }
1323
+ case "waiting_for_runner":
1324
+ return "waiting for runner";
1325
+ case "loading": {
1326
+ const expected = number(detail?.expected_load_ms);
1327
+ return `loading ${model ?? "model"}${expected !== void 0 ? ` (~${seconds(expected)})` : ""}${evictedSuffix(detail)}`;
1328
+ }
1329
+ case "running":
1330
+ return model ? `running ${model}` : "running";
1331
+ default:
1332
+ return status.replace(/_/g, " ");
1333
+ }
1334
+ }
1335
+ function errorBody(error) {
1336
+ try {
1337
+ return object(JSON.parse(error instanceof Error ? error.message : String(error)));
1338
+ } catch {
1339
+ return void 0;
1340
+ }
1341
+ }
1342
+ function describeNapiError(error) {
1343
+ const message = error instanceof Error ? error.message : String(error);
1344
+ const missing = /does not exist or has expired|\bHTTP 404\b|^\s*(?:404\b|Not Found\s*$)/i.test(message);
1345
+ return { body: errorBody(error), ...missing ? { status: 404 } : {} };
1346
+ }
1347
+ function unconfirmedSubmission(error, body) {
1348
+ const message = error instanceof Error ? error.message : String(error);
1349
+ if (body && (typeof body.code === "string" || "execution_outcome" in body || "retry_safe" in body)) {
1350
+ return error instanceof Error ? error : new Error(message);
1351
+ }
1352
+ return new Error(JSON.stringify({
1353
+ ...body,
1354
+ error: typeof body?.error === "string" ? body.error : message,
1355
+ execution_outcome: "unknown",
1356
+ retry_safe: false
1357
+ }));
1358
+ }
1359
+ function submissionRefused({ body }) {
1360
+ const code = body?.code;
1361
+ return typeof code === "string" && SUBMIT_FALLBACK_CODES.has(code);
1362
+ }
1363
+ var defaultWait = (ms, signal) => new Promise((resolve) => {
1364
+ if (signal?.aborted) return resolve();
1365
+ const done = () => {
1366
+ clearTimeout(timer);
1367
+ signal?.removeEventListener("abort", done);
1368
+ resolve();
1369
+ };
1370
+ const timer = setTimeout(done, ms);
1371
+ signal?.addEventListener("abort", done, { once: true });
1372
+ });
1373
+ async function runWithJobProgress(calls, report, options = {}) {
1374
+ const { signal, wait = defaultWait, describe = describeNapiError, onJob } = options;
1375
+ const aborted = (id2, cancellation2) => options.aborted?.(id2, cancellation2) ?? new Error(id2 ? cancellation2 === "unknown" ? `run_task was cancelled by the client; cancellation acknowledgement is unknown for deferred job ${id2}, which may still be running. Upstream stop is not confirmed.` : `run_task was cancelled by the client; cancellation was requested for deferred job ${id2}${cancellation2 === "acknowledged" ? " and acknowledged by serve" : ""}. Upstream stop is not confirmed.` : "run_task was cancelled by the client before a job handle was received; execution outcome is unknown. The task was not replayed.");
1376
+ if (signal?.aborted) throw options.aborted?.() ?? new Error("run_task was cancelled by the client before submission; no task was submitted.");
1377
+ let raw;
1378
+ try {
1379
+ raw = await calls.submit();
1380
+ } catch (error) {
1381
+ if (signal?.aborted) throw aborted();
1382
+ const described = describe(error);
1383
+ if (submissionRefused(described)) return calls.sync(described.body);
1384
+ throw options.unconfirmed?.(error, described.body) ?? unconfirmedSubmission(error, described.body);
1385
+ }
1386
+ let submitted;
1387
+ try {
1388
+ submitted = object(JSON.parse(raw));
1389
+ } catch {
1390
+ return raw;
1391
+ }
1392
+ let job = object(submitted?.job);
1393
+ const id = typeof job?.id === "string" ? job.id : void 0;
1394
+ if (submitted?.deferred !== true || !job || !id) return raw;
1395
+ onJob?.(id, true);
1396
+ let open = true;
1397
+ const settled = () => {
1398
+ if (open) onJob?.(id, false);
1399
+ open = false;
1400
+ };
1401
+ let cancellation;
1402
+ const requestCancellation = async () => {
1403
+ for (let attempt = 1; ; attempt++) {
1404
+ try {
1405
+ const reply = object(JSON.parse(await calls.cancel(id)));
1406
+ const status = object(reply?.job)?.status ?? reply?.status;
1407
+ if (!TERMINAL.has(String(status))) return "requested";
1408
+ settled();
1409
+ return "acknowledged";
1410
+ } catch (error) {
1411
+ if (describe(error).status === 404) {
1412
+ settled();
1413
+ return "unknown";
1414
+ }
1415
+ if (attempt >= CANCEL_ATTEMPTS) return "unknown";
1416
+ await wait(CANCEL_BACKOFF_MS * 2 ** (attempt - 1));
1417
+ }
1418
+ }
1419
+ };
1420
+ const cancel = () => {
1421
+ cancellation ??= requestCancellation();
1422
+ };
1423
+ const cancellationState = async () => {
1424
+ if (!cancellation) return void 0;
1425
+ let timer;
1426
+ const capped = new Promise((resolve) => {
1427
+ timer = setTimeout(() => resolve("unknown"), CANCEL_REPORT_MS);
1428
+ });
1429
+ try {
1430
+ return await Promise.race([cancellation, capped]);
1431
+ } finally {
1432
+ clearTimeout(timer);
1433
+ }
1434
+ };
1435
+ signal?.addEventListener("abort", cancel, { once: true });
1436
+ try {
1437
+ let lastMessage = "";
1438
+ let lastPhase = "";
1439
+ let lastReportAt = Date.now();
1440
+ let phaseStartedAt = lastReportAt;
1441
+ let delay = FIRST_POLL_MS;
1442
+ let failures = 0;
1443
+ for (; ; ) {
1444
+ if (signal?.aborted) {
1445
+ cancel();
1446
+ throw aborted(id, await cancellationState());
1447
+ }
1448
+ const now = Date.now();
1449
+ const phase = `${String(job.status)}|${String(job.reason)}`;
1450
+ if (phase !== lastPhase) {
1451
+ lastPhase = phase;
1452
+ phaseStartedAt = now;
1453
+ delay = FIRST_POLL_MS;
1454
+ }
1455
+ const message = jobProgressMessage(job);
1456
+ if (message !== lastMessage) {
1457
+ report(message);
1458
+ lastMessage = message;
1459
+ lastReportAt = now;
1460
+ } else if (now - lastReportAt >= HEARTBEAT_MS && !TERMINAL.has(String(job.status))) {
1461
+ report(`${message} (${seconds(now - phaseStartedAt)})`);
1462
+ lastReportAt = now;
1463
+ }
1464
+ if (TERMINAL.has(String(job.status))) break;
1465
+ await wait(delay, signal);
1466
+ if (signal?.aborted) {
1467
+ cancel();
1468
+ throw aborted(id, await cancellationState());
1469
+ }
1470
+ delay = Math.min(MAX_POLL_MS, Math.round(delay * 1.5));
1471
+ try {
1472
+ const polled = object(object(JSON.parse(await calls.get(id)))?.job);
1473
+ if (polled) job = polled;
1474
+ failures = 0;
1475
+ } catch (error) {
1476
+ if (describe(error).status === 404) {
1477
+ settled();
1478
+ const failure2 = {
1479
+ code: "task_job_unavailable",
1480
+ error: `Deferred job ${id} is no longer available; its execution outcome is unknown. The task was not run again. Its result cannot be recovered from the missing job record; verify the original execution before submitting new work.`,
1481
+ job_id: id,
1482
+ last_observed_status: job.status,
1483
+ execution_outcome: "unknown",
1484
+ result_recoverable: false,
1485
+ retry_safe: false
1486
+ };
1487
+ throw options.failure?.(failure2, 404) ?? new Error(JSON.stringify(failure2));
1488
+ }
1489
+ failures += 1;
1490
+ if (failures >= MAX_POLL_FAILURES) {
1491
+ cancel();
1492
+ throw error;
1493
+ }
1494
+ }
1495
+ }
1496
+ settled();
1497
+ if (job.status === "completed") return JSON.stringify(job.result ?? {});
1498
+ const failure = { ...object(job.error) ?? { error: `deferred job ${id} ended ${String(job.status)}` } };
1499
+ if (typeof failure.code === "string" && RETENTION_FAILURE_CODES.has(failure.code)) {
1500
+ const lost = failure.code === "task_job_result_too_large" ? "result" : "error";
1501
+ failure.error = `${String(failure.error)} Deferred job ${id} was not run again. Its original ${lost} was not retained and cannot be recovered from this job; resubmission would execute new work.`;
1502
+ failure.job_id = id;
1503
+ failure.result_recoverable = false;
1504
+ failure.retry_safe = false;
1505
+ }
1506
+ const status = typeof failure.status === "number" ? failure.status : void 0;
1507
+ delete failure.id;
1508
+ delete failure.ok;
1509
+ if (status !== void 0) delete failure.status;
1510
+ throw options.failure?.(failure, status) ?? new Error(JSON.stringify(failure));
1511
+ } finally {
1512
+ signal?.removeEventListener("abort", cancel);
1513
+ }
1514
+ }
1515
+
1516
+ // src/research/transport-jobs.ts
1517
+ var TASKS_SUFFIX = "/_freellama/v1/tasks";
1518
+ function describeHttpError(error) {
1519
+ if (!(error instanceof HTTPError)) return {};
1520
+ try {
1521
+ const body = parseJsonBody(error.body.subarray(0, RECEIPT_BODY_LIMIT), "error body");
1522
+ return { status: error.code, ...isRecord(body) ? { body } : {} };
1523
+ } catch {
1524
+ return { status: error.code };
1525
+ }
1526
+ }
1527
+ function syncError(body, status) {
1528
+ const code = status ?? 500;
1529
+ return new HTTPError(code, STATUS_CODES[code] ?? "", Buffer.from(JSON.stringify(body), "utf8"));
1530
+ }
1531
+ async function requestJsonAsJob(url, payload, timeoutSeconds, options) {
1532
+ const request = options.request ?? httpRequest;
1533
+ const headers = requestHeaders();
1534
+ const call = async (method, target, body, signal) => {
1535
+ const sent = body === void 0 ? void 0 : Buffer.from(JSON.stringify(body), "utf8");
1536
+ const response = await request(target, { method, headers, body: sent, timeoutSeconds, signal });
1537
+ return decodeUtf8(response, "managed task response");
1538
+ };
1539
+ const sync = () => call("POST", url, payload);
1540
+ if (!url.endsWith(TASKS_SUFFIX)) return parseJsonBody(Buffer.from(await sync(), "utf8"), "chat response");
1541
+ const jobs = `${url.slice(0, -TASKS_SUFFIX.length)}/_freellama/v1/jobs`;
1542
+ const deadline = new AbortController();
1543
+ const timer = setTimeout(() => deadline.abort(), Math.max(timeoutSeconds * 1e3, 1));
1544
+ try {
1545
+ const raw = await runWithJobProgress({
1546
+ submit: () => call("POST", url, { ...payload, defer: true }),
1547
+ get: (id) => call("GET", `${jobs}/${encodeURIComponent(id)}`, void 0, deadline.signal),
1548
+ cancel: (id) => call("POST", `${jobs}/${encodeURIComponent(id)}/cancel`, null),
1549
+ sync: async () => {
1550
+ throw syncError({
1551
+ code: "cancellable_task_unavailable",
1552
+ error: "Serve refused job submission before execution; cancellable research requires available job capacity and a supported request size.",
1553
+ execution_outcome: "not_started",
1554
+ retry_safe: true
1555
+ }, 409);
1556
+ }
1557
+ }, options.report, {
1558
+ signal: deadline.signal,
1559
+ wait: options.wait,
1560
+ describe: describeHttpError,
1561
+ failure: syncError,
1562
+ aborted: () => new RequestTimeoutError(),
1563
+ // Keep transport types, but do not let an arbitrary 503 erase an uncertain submission.
1564
+ unconfirmed: (error, body) => error instanceof HTTPError && !retryableChatError(error) ? syncError({
1565
+ ...body,
1566
+ error: typeof body?.error === "string" ? body.error : error.message,
1567
+ execution_outcome: body?.execution_outcome ?? "unknown",
1568
+ retry_safe: false
1569
+ }, error.code) : error,
1570
+ onJob: options.onJob
1571
+ });
1572
+ return parseJsonBody(Buffer.from(raw, "utf8"), "chat response");
1573
+ } finally {
1574
+ clearTimeout(timer);
1575
+ }
1576
+ }
1577
+
1155
1578
  // src/research/workspace-path.ts
1156
1579
  import { lstatSync, readlinkSync, statSync } from "node:fs";
1157
- import path3 from "node:path";
1580
+ import path4 from "node:path";
1158
1581
  var SymlinkLoopError = class extends ValidationError {
1159
1582
  name = "SymlinkLoopError";
1160
1583
  };
@@ -1241,7 +1664,47 @@ function resolvePath(target, cwd = process.cwd()) {
1241
1664
  return current;
1242
1665
  };
1243
1666
  const text = cleanPath(target);
1244
- return path3.posix.normalize(walk(text.startsWith("/") ? "" : cwd, text) || "/");
1667
+ return path4.posix.normalize(walk(text.startsWith("/") ? "" : cwd, text) || "/");
1668
+ }
1669
+
1670
+ // src/research/grounding.ts
1671
+ import { existsSync as existsSync3 } from "node:fs";
1672
+ import path5 from "node:path";
1673
+ var PATH_TOKEN = /(?<![\w@/.-])(?:\.\/)?[\w@-][\w@.-]*(?:\/[\w@.-]+)+\.[A-Za-z][A-Za-z0-9]{0,5}(?=$|[\s:`'"),.;\]])/g;
1674
+ function clean(token) {
1675
+ return token.replace(/^\.\//, "").replace(/[.,;]+$/, "");
1676
+ }
1677
+ function pathsIn(text) {
1678
+ return [...new Set([...text.matchAll(PATH_TOKEN)].map((match) => clean(match[0])))];
1679
+ }
1680
+ function missingPaths(answer, workspace) {
1681
+ return pathsIn(answer).filter((candidate) => !existsSync3(path5.join(workspace, candidate)));
1682
+ }
1683
+ function evidencePaths(observations, workspace, limit = 10) {
1684
+ const found = [];
1685
+ for (const text of observations) {
1686
+ for (const candidate of pathsIn(text)) {
1687
+ if (!found.includes(candidate) && existsSync3(path5.join(workspace, candidate))) found.push(candidate);
1688
+ if (found.length >= limit) return found;
1689
+ }
1690
+ }
1691
+ return found;
1692
+ }
1693
+ function groundingRepairNotice(missing, evidence) {
1694
+ return `These paths in your answer do not exist in the workspace: ${missing.join(", ")}. ` + (evidence.length > 0 ? `Paths your own commands returned: ${evidence.join(", ")}. ` : "") + "Return a finish action again that cites only paths that exist; if the evidence does not establish the answer, say so.";
1695
+ }
1696
+ function groundingNote(missing) {
1697
+ return `
1698
+ [freellama: cited path(s) not found in the workspace: ${missing.join(", ")}; treat this answer as unverified]`;
1699
+ }
1700
+ function toolErrorHint(message) {
1701
+ if (/rg: unrecognized flag --(?:exclude-dir|include)/.test(message)) {
1702
+ return "\nHint: rg excludes directories with --glob '!node_modules' and filters types with -t ts; or use grep -rn PATTERN . --exclude-dir=node_modules.";
1703
+ }
1704
+ return "";
1705
+ }
1706
+ function sourceRepairNotice(evidence) {
1707
+ return `Your answer names no source file. Keep the answer and end it with "Source: path:line" copied from your results, for example one of: ${evidence.slice(0, 6).join(", ")}.`;
1245
1708
  }
1246
1709
 
1247
1710
  // src/research/agent-loop.ts
@@ -1251,7 +1714,7 @@ function requireEnv(env, name) {
1251
1714
  if (value === void 0) throw new Error(`${name} is not set`);
1252
1715
  return value;
1253
1716
  }
1254
- var readText = (file) => normalizeNewlines(decodeUtf8(readFileSync4(file), file));
1717
+ var readText = (file) => normalizeNewlines(decodeUtf8(readFileSync5(file), file));
1255
1718
  var pathOf = (value) => value === "" ? "." : value;
1256
1719
  var numberOr = (value, fallback) => typeof value === "number" && Number.isFinite(value) ? value : typeof value === "boolean" ? Number(value) : fallback;
1257
1720
  var echo = (action) => canonicalJson(action, { sortKeys: false, ensureAscii: false });
@@ -1281,9 +1744,15 @@ async function runLoop(spec, hooks, env, inputs) {
1281
1744
  const { model, workspace, prompt, resultPath } = inputs;
1282
1745
  const print = hooks.print ?? ((line) => process.stdout.write(`${line}
1283
1746
  `));
1284
- const sendJson = hooks.requestJson ?? requestJson;
1747
+ const progress = hooks.progress ?? progressEmitter(env);
1285
1748
  const endpoint = (env.FREELLAMA_OLLAMA_ENDPOINT ?? "http://127.0.0.1:11434").replace(/\/+$/, "");
1286
1749
  const managedEndpoint = (env.FREELLAMA_AGENT_MANAGED_ENDPOINT ?? "").replace(/\/+$/, "");
1750
+ let phaseAt = { maxTurns: 0 };
1751
+ const sendJson = hooks.requestJson ?? (managedEndpoint ? (url, payload, timeoutSeconds) => requestJsonAsJob(url, payload, timeoutSeconds, {
1752
+ report: (message) => progress?.({ event: "job_phase", ...phaseAt, message }),
1753
+ onJob: (id, open) => progress?.({ event: open ? "job_open" : "job_closed", id }),
1754
+ request: hooks.httpRequest
1755
+ }) : requestJson);
1287
1756
  const executionPreference = env.FREELLAMA_AGENT_EXECUTION_PREFERENCE ?? "auto";
1288
1757
  const minPlacementEvidence = env.FREELLAMA_AGENT_MIN_PLACEMENT_EVIDENCE ?? "configured";
1289
1758
  let runtime;
@@ -1298,7 +1767,7 @@ async function runLoop(spec, hooks, env, inputs) {
1298
1767
  }
1299
1768
  let messages = [
1300
1769
  { role: "system", content: spec.systemPrompt(workspace) },
1301
- { role: "user", content: prompt }
1770
+ { role: "user", content: withBriefing(prompt, spec, workspace) }
1302
1771
  ];
1303
1772
  const calls = [];
1304
1773
  const usage = { input_tokens: 0, output_tokens: 0, cache_read_tokens: null, cache_write_tokens: null };
@@ -1308,12 +1777,20 @@ async function runLoop(spec, hooks, env, inputs) {
1308
1777
  let answer = "";
1309
1778
  let failure = null;
1310
1779
  let transportError = null;
1780
+ const readCatalog = (url) => readCatalogJson(url, hooks.httpRequest);
1781
+ const platformRatio = managedEndpoint ? await (hooks.resolvePlatformTokenRatio ?? resolvePlatformTokenRatio)(managedEndpoint, model, { readJson: readCatalog }) : null;
1311
1782
  const calibrationDir = trimSpace(env.FREELLAMA_AGENT_TOKEN_CALIBRATION_DIR ?? "");
1312
- const calibrationModelIdentity = calibrationDir ? await (hooks.resolveModelIdentity ?? resolveModelIdentity)(managedEndpoint || endpoint, model, messages[0].content) : { verified: false, scope: "persistence_disabled", identity: "" };
1783
+ const calibrationModelIdentity = calibrationDir ? await (hooks.resolveModelIdentity ?? resolveModelIdentity)(
1784
+ managedEndpoint || endpoint,
1785
+ model,
1786
+ messages[0].content,
1787
+ { readJson: readCatalog }
1788
+ ) : { verified: false, scope: "persistence_disabled", identity: "" };
1313
1789
  const contextManager = new AgentContextManager(runtime, {
1314
1790
  model,
1315
1791
  calibrationIdentity: calibrationModelIdentity.identity,
1316
- calibrationDir: calibrationDir && calibrationModelIdentity.verified ? calibrationDir : null
1792
+ calibrationDir: calibrationDir && calibrationModelIdentity.verified ? calibrationDir : null,
1793
+ platformRatio
1317
1794
  });
1318
1795
  try {
1319
1796
  messages = contextManager.fit(messages);
@@ -1346,12 +1823,19 @@ async function runLoop(spec, hooks, env, inputs) {
1346
1823
  executionPreference,
1347
1824
  minPlacementEvidence
1348
1825
  );
1826
+ const recorded = executionReceipts.length;
1349
1827
  response = unwrapChatResponse(await sendJson(url, payload, runtime.request_timeout_seconds), executionReceipts);
1828
+ if (managedEndpoint && executionReceipts.length === recorded) {
1829
+ executionReceipts.push({ model: null, execution: null, reason: "managed_execution_receipt_missing" });
1830
+ }
1350
1831
  break;
1351
1832
  } catch (error) {
1352
1833
  if (!isTransportError(error) || !retryableChatError(error)) throw error;
1353
1834
  lastError = error;
1354
- if (attempt + 1 < runtime.retry_attempts) await sleep(runtime.retry_backoff_seconds * 1e3);
1835
+ if (attempt + 1 < runtime.retry_attempts) {
1836
+ progress?.({ event: "retry", attempt: attempt + 2, attempts: runtime.retry_attempts });
1837
+ await sleep(runtime.retry_backoff_seconds * 1e3);
1838
+ }
1355
1839
  }
1356
1840
  }
1357
1841
  if (response === null) throw lastError;
@@ -1386,10 +1870,26 @@ async function runLoop(spec, hooks, env, inputs) {
1386
1870
  const observations = hooks.observationStore ? hooks.observationStore(pageSize) : new ObservationStore(pageSize);
1387
1871
  let session;
1388
1872
  try {
1389
- session = spec.open ? await spec.open(workspace) : void 0;
1873
+ try {
1874
+ session = spec.open ? await spec.open(workspace) : void 0;
1875
+ } catch (error) {
1876
+ if (!isValidationError(error)) throw error;
1877
+ answer = `research adapter could not start: ${error.message}`;
1878
+ writeFailureResult(resultPath, answer);
1879
+ print(answer);
1880
+ return 1;
1881
+ }
1882
+ const root = session?.workspace ?? workspace;
1390
1883
  let parseFailures = 0;
1391
1884
  let exhausted = true;
1885
+ let repeats = 0;
1886
+ let groundingRepaired = false;
1887
+ let sourceRepaired = false;
1888
+ const seenOutputs = [];
1392
1889
  for (let turn = 0; turn < runtime.max_turns; turn += 1) {
1890
+ const at = { turn: turn + 1, maxTurns: runtime.max_turns };
1891
+ phaseAt = at;
1892
+ progress?.({ event: "model", ...at });
1393
1893
  let response;
1394
1894
  try {
1395
1895
  response = await callModel();
@@ -1413,6 +1913,7 @@ async function runLoop(spec, hooks, env, inputs) {
1413
1913
  exhausted = false;
1414
1914
  break;
1415
1915
  }
1916
+ progress?.({ event: "repair", ...at });
1416
1917
  messages.push({ role: "assistant", content: cpSlice(raw, 0, runtime.parse_repair_echo_chars) });
1417
1918
  messages.push({ role: "user", content: PARSE_REPAIR_NOTICE });
1418
1919
  if (!refit()) {
@@ -1425,6 +1926,7 @@ async function runLoop(spec, hooks, env, inputs) {
1425
1926
  if (action.action === "page") {
1426
1927
  const wantStep = Math.trunc(numberOr(action.step, 0));
1427
1928
  const wantPage = Math.trunc(numberOr(action.page, 1));
1929
+ progress?.({ event: "action", ...at, action });
1428
1930
  messages.push({ role: "assistant", content: echo(action) });
1429
1931
  messages.push({ role: "user", content: observations.message(wantStep, wantPage) });
1430
1932
  if (!refit()) {
@@ -1434,45 +1936,83 @@ async function runLoop(spec, hooks, env, inputs) {
1434
1936
  continue;
1435
1937
  }
1436
1938
  if (action.action === "finish") {
1437
- answer = trimSpace(String(action.answer ?? ""));
1939
+ const proposed = trimSpace(String(action.answer ?? ""));
1940
+ const missing = missingPaths(proposed, root);
1941
+ if (missing.length > 0 && !groundingRepaired) {
1942
+ groundingRepaired = true;
1943
+ progress?.({ event: "repair", ...at });
1944
+ messages.push({ role: "assistant", content: echo(action) });
1945
+ messages.push({ role: "user", content: groundingRepairNotice(missing, evidencePaths(seenOutputs, root)) });
1946
+ if (!refit()) {
1947
+ answer = proposed + groundingNote(missing);
1948
+ exhausted = false;
1949
+ break;
1950
+ }
1951
+ continue;
1952
+ }
1953
+ const evidence = spec.requireSource !== false && missing.length === 0 && !sourceRepaired && pathsIn(proposed).length === 0 ? evidencePaths(seenOutputs, root) : [];
1954
+ if (evidence.length > 0) {
1955
+ sourceRepaired = true;
1956
+ progress?.({ event: "repair", ...at });
1957
+ messages.push({ role: "assistant", content: echo(action) });
1958
+ messages.push({ role: "user", content: sourceRepairNotice(evidence) });
1959
+ if (!refit()) {
1960
+ answer = proposed;
1961
+ exhausted = false;
1962
+ break;
1963
+ }
1964
+ continue;
1965
+ }
1966
+ progress?.({ event: "finish", ...at });
1967
+ answer = missing.length > 0 ? proposed + groundingNote(missing) : proposed;
1438
1968
  exhausted = false;
1439
1969
  break;
1440
1970
  }
1441
1971
  const started = performance.now();
1442
- const call = session.toolCall(action, workspace, runtime);
1972
+ const call = session.toolCall(action, root, runtime);
1443
1973
  const signature = callSignature(call.signature);
1444
1974
  let observation;
1445
1975
  let status;
1976
+ let exitCode;
1446
1977
  if (seenCalls.has(signature)) {
1978
+ progress?.({ event: "action", ...at, action, repeat: true });
1447
1979
  observation = REPEAT_NOTICE;
1448
1980
  status = "repeat";
1981
+ repeats += 1;
1449
1982
  } else {
1983
+ progress?.({ event: "action", ...at, action });
1450
1984
  try {
1451
1985
  observation = hooks.runTool ? await hooks.runTool(call.record.arguments) : await call.run();
1452
1986
  status = "ok";
1453
1987
  } catch (error) {
1454
1988
  if (!isToolError(error)) throw error;
1455
- observation = `tool error: ${error.message}`;
1456
- status = "error";
1989
+ status = error instanceof ToolExitError ? error.status : "error";
1990
+ exitCode = error instanceof ToolExitError ? error.exitCode : void 0;
1991
+ observation = `${status === "empty" ? "tool search" : "tool error"}: ${error.message}${toolErrorHint(error.message)}`;
1457
1992
  }
1458
1993
  seenCalls.set(signature, calls.length + 1);
1459
1994
  }
1460
1995
  calls.push({
1461
1996
  ...call.record,
1462
1997
  status,
1998
+ ...exitCode === void 0 ? {} : { exit_code: exitCode },
1463
1999
  duration_ms: Math.round((performance.now() - started) * 1e3) / 1e3
1464
2000
  });
1465
2001
  messages.push({ role: "assistant", content: echo(action) });
1466
2002
  const remaining = runtime.max_turns - calls.length;
1467
2003
  const step = calls.length;
1468
2004
  observations.put(step, observation, call.ledger(status));
2005
+ seenOutputs.push(observation);
1469
2006
  messages.push({ role: "user", content: observations.message(step) + spec.remaining(remaining) });
1470
2007
  if (!refit()) {
1471
2008
  exhausted = false;
1472
2009
  break;
1473
2010
  }
2011
+ if (repeats >= 2) break;
1474
2012
  }
1475
2013
  if (exhausted) {
2014
+ progress?.({ event: "finalize", maxTurns: runtime.max_turns });
2015
+ phaseAt = { maxTurns: runtime.max_turns };
1476
2016
  messages.push({ role: "user", content: spec.exhausted });
1477
2017
  if (refit()) {
1478
2018
  try {
@@ -1480,6 +2020,8 @@ async function runLoop(spec, hooks, env, inputs) {
1480
2020
  const finalAction = spec.parseAction(content(response));
1481
2021
  if (finalAction.action !== "finish") throw new ValidationError("forced final response was not finish");
1482
2022
  answer = trimSpace(String(finalAction.answer ?? ""));
2023
+ const missing = missingPaths(answer, root);
2024
+ if (missing.length > 0) answer += groundingNote(missing);
1483
2025
  } catch (error) {
1484
2026
  if (!isTransportError(error) && !isValidationError(error)) throw error;
1485
2027
  let diagnostic;
@@ -1489,7 +2031,9 @@ async function runLoop(spec, hooks, env, inputs) {
1489
2031
  }
1490
2032
  }
1491
2033
  if (!answer) answer = failure ?? "agent stopped without a final answer";
2034
+ const report = spec.report ? await spec.report(session) : {};
1492
2035
  const result = {
2036
+ ...report,
1493
2037
  final_answer: answer,
1494
2038
  tool_calls: calls,
1495
2039
  usage,
@@ -1525,14 +2069,540 @@ async function runLoop(spec, hooks, env, inputs) {
1525
2069
  observations.close();
1526
2070
  }
1527
2071
  }
2072
+ function withBriefing(prompt, spec, workspace) {
2073
+ try {
2074
+ const briefing = spec.briefing?.(workspace);
2075
+ return briefing ? `${prompt}
1528
2076
 
1529
- // src/research/prompts.ts
1530
- var BASH_SYSTEM_PROMPT = 'You are a local code-research agent in a read-only workspace rooted at the current directory. Use raw POSIX shell commands without editors, special tools, or network access. Return exactly one JSON object per turn:\n\n{"action":"shell","command":"one shell command, e.g. grep -n \\"class Group\\" click/src/click/core.py"}\n{"action":"page","step":2,"page":2}\n{"action":"finish","answer":"concise final answer with repository-relative evidence"}\n\nThese are the complete action schemas. Use only `shell`, `page`, or `finish`; every shown field is\nrequired with the shown type. An invalid shape is rejected and costs one bounded repair turn.\n\nFull command output is stored in pages; you see page 1 and the total. Context fitting can shorten\nan observation in the conversation. Request its stored page to recover the evidence without\nre-running the command. Claim "not found" only after checking the relevant search scope and pages.\n\nThe shell is READ-ONLY and restricted. Available tools: ls, find, grep, rg, cat, head, tail, wc,\nsort, uniq, cut, tr, nl, sed (bounded read-only inline programs), jq, diff, xargs\n(only echo, printf, true, or false targets), tree, file, stat, and read-only git\n(log, show, grep, ls-files, diff, blame, status). There is no awk, python, network, output\nredirection, script-file loading, recursive symlink following, paths outside the workspace,\n$(...) substitution, $VARIABLES, or loops: write paths and patterns literally and\nchain tools with pipes, one pipeline per turn. A refused command costs a turn, so follow these\nrules. Be decisive: most tasks need 2-6 commands. Call finish as soon as the requested facts are\nestablished.\n\nSCOPE YOUR SEARCHES. A real workspace holds far more than its source, and an unscoped grep drowns\nthe answer in vendored and generated files. Always exclude them:\n grep -rn "PATTERN" . --exclude-dir={node_modules,target,.venv,venv,dist,build,.git,vendor,__pycache__,.octocode,site-packages}\nFor implementation questions, prefer src/, packages/*/src/, lib/ and the repo root before fixtures,\nmocks, or examples. If the question targets tests or examples, inspect those files. Name the file\nthat supplies the answer.\nIf a search returns nothing, widen the pattern before concluding the thing does not exist: absence\nof a grep hit is weak evidence, and "not found" is only a real answer once you have looked in the\nsource directories.\nFor a production default, find its declaration before relying on test occurrences. Declarations look like\n`default=`, `unwrap_or(`, `const `, `static `, or a settings schema initializer.\nFor a test-specific default or fixture, inspect its test declaration.';
2077
+ ${briefing}` : prompt;
2078
+ } catch {
2079
+ return prompt;
2080
+ }
2081
+ }
2082
+
2083
+ // ../rust-core/src/model_catalog.json
2084
+ var model_catalog_default = {
2085
+ snapshot: "2026-11-01",
2086
+ measured_on: "Apple M2 Pro, 32 GB, Ollama 0.40.13, with other work running",
2087
+ how_to_read: "quality is measured (run here with known answers), published (the publisher's figure), or untested. memory_gb is what FreeLlama asks to have available, including its reserve; null means not previewed. Discovery never authorizes a pull: ask for one exact tag and its size first.",
2088
+ jobs: [
2089
+ { job: "classify", title: "Classify, triage, yes/no", call: 'run_task {task:"decision"}', cookbook: "cookbooks/classify", picks: [
2090
+ { tag: "strands-decider:2b-nvfp4", role: "default", file_gb: 1.3, memory_gb: 3.8, quality: "measured", evidence: "53/56 in all 3 rounds, 7 ms median; published 182/231 JevBench" },
2091
+ { tag: "nimble-v3:9b-q4_K_M", role: "alternative", file_gb: 5.6, memory_gb: 11.5, quality: "measured", evidence: "51/56 in all 3 rounds, 1.5 s median: no better here despite published 198/231 JevBench" }
2092
+ ] },
2093
+ { job: "find-code", title: "Find the code for an issue", call: 'run_task {task:"embedding", nearest:{query}}', cookbook: "cookbooks/find-code", picks: [
2094
+ { tag: "embeddinggemma-2:740m-q8_0", role: "default", file_gb: 1.3, memory_gb: 1.4, quality: "measured", evidence: "right file first 9/10, top 3 9/10; 899 chunks indexed in 67 s" },
2095
+ { tag: "nomic-embed-text:v1.5", role: "alternative", file_gb: 0.3, memory_gb: 1.4, quality: "measured", evidence: "right file first 6/10, top 3 8/10; faster, 881 chunks in 42 s" }
2096
+ ] },
2097
+ { job: "duplicates", title: "Find duplicates", call: 'run_task {task:"embedding", nearest:{}}', cookbook: "cookbooks/duplicates", picks: [
2098
+ { tag: "nomic-embed-text:v1.5", role: "default", file_gb: 0.3, memory_gb: 1.4, quality: "measured", evidence: "8/12 real issue pairs" }
2099
+ ] },
2100
+ { job: "ocr", title: "Read text from an image", call: 'run_task {task:"vision", files:[path]}', cookbook: "cookbooks/ocr", picks: [
2101
+ { tag: "hf.co/openbmb/MiniCPM-OCR-1.0-gguf:Q4_K_M", role: "default", file_gb: 1.4, memory_gb: 3.8, quality: "measured", evidence: "30/30 in all 3 rounds, 257 ms median, clean rendered text" },
2102
+ { tag: "glm-ocr:latest", role: "alternative", file_gb: 2.2, memory_gb: 5.8, quality: "measured", evidence: "30/30 in all 3 rounds, 1,885 ms median" }
2103
+ ] },
2104
+ { job: "image-recognition", title: "Understand what an image shows", call: 'run_task {task:"vision", files:[path], prompt}', cookbook: "cookbooks/image-recognition", picks: [
2105
+ { tag: "lfm3-vl:2b-q4_K_M", role: "default", file_gb: 1.5, memory_gb: 4.1, quality: "measured", evidence: "20/20 drawn images in all 3 rounds, 299 ms median" },
2106
+ { tag: "lfm3-omni:2b-q4_K_M", role: "alternative", file_gb: 2, memory_gb: 5.5, quality: "measured", evidence: "18/20 in all 3 rounds, 269 ms median; also does audio" },
2107
+ { tag: "bonsai-2:9b", role: "alternative", file_gb: 3.2, memory_gb: 10.3, quality: "measured", evidence: "17/20 in all 3 rounds, 1.4 s median" }
2108
+ ] },
2109
+ { job: "image-search", title: "Find an image by describing it", call: 'run_task {task:"embedding", files:[images], nearest:{query}}', cookbook: "cookbooks/image-search", picks: [
2110
+ { tag: "embeddinggemma-2:740m-q8_0", role: "default", file_gb: 1.3, memory_gb: 1.4, quality: "measured", evidence: "5/5 on five drawn screens" }
2111
+ ] },
2112
+ { job: "speech-to-text", title: "Recording to text", call: 'audio {action:"transcribe", file}', cookbook: "cookbooks/speech-to-text", picks: [
2113
+ { tag: "lfm3-audio:2b-q4_K_M", role: "default", file_gb: 1.7, memory_gb: 5.5, quality: "measured", evidence: "1.7% word error on generated speech, 276 ms median, translation worked" },
2114
+ { tag: "lfm3-omni:2b-q4_K_M", role: "alternative", file_gb: 2, memory_gb: 5.5, quality: "measured", evidence: "1.7% word error, 280 ms median, translation worked, also does images" }
2115
+ ] },
2116
+ { job: "text-to-speech", title: "Text to speech", call: 'audio {action:"speak", text}', cookbook: "cookbooks/text-to-speech", picks: [
2117
+ { tag: "phonon:en", role: "default", file_gb: 0.1, memory_gb: null, quality: "measured", evidence: "0.8-1.7% word error when transcribed back, 102 ms median: about 9x faster" },
2118
+ { tag: "lfm3-audio:2b-q4_K_M", role: "alternative", file_gb: 1.7, memory_gb: 5.5, quality: "measured", evidence: "0% word error, 950 ms median; the judge is an LFM3 model and may favour it" },
2119
+ { tag: "lfm3-omni:2b-q4_K_M", role: "alternative", file_gb: 2, memory_gb: 5.5, quality: "measured", evidence: "0% word error, 950 ms median; same judge caveat" }
2120
+ ] },
2121
+ { job: "coding", title: "Small coding tasks", call: 'run_task {task:"coding", files:[path]}', cookbook: "cookbooks/coding", picks: [
2122
+ { tag: "lfm3:1.7b-q4_K_M", role: "default", file_gb: 1.1, memory_gb: 3.3, quality: "measured", evidence: "21, 20, 20 of 25 small questions, 579 ms median" },
2123
+ { tag: "hf.co/nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-4GB:Q4_K_M", role: "stronger", file_gb: 6.2, memory_gb: 13.5, quality: "measured", evidence: "23, 22, 23 of 25, 267 ms median" },
2124
+ { tag: "bonsai-2:9b", role: "alternative", file_gb: 3.2, memory_gb: 10.3, quality: "measured", evidence: "19, 19, 17 of 25, 475 ms median" },
2125
+ { tag: "bonsai-2:27b", role: "stronger", file_gb: 8.3, memory_gb: 16.3, quality: "published", evidence: "publisher: SWE-bench Verified 60.8%, LiveCodeBench 90.1%; not measured here, never had 16.3 GB available" }
2126
+ ] },
2127
+ { job: "research-agent", title: "Research a repository", call: 'delegate_research {adapter:"octocode" or "github"}', cookbook: "cookbooks/research-agent", picks: [
2128
+ { tag: "bonsai-2:9b", role: "default", file_gb: 3.2, memory_gb: 10.3, quality: "measured", evidence: "4, 5, 5 of 8 local and 3, 5, 5 of 6 GitHub over 3 rounds, 82 s median per question" },
2129
+ { tag: "hf.co/nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-4GB:Q4_K_M", role: "stronger", file_gb: 6.2, memory_gb: 13.5, quality: "measured", evidence: "6/8 local and 5/6 GitHub in all 3 rounds, 96 s median per question" },
2130
+ { tag: "lfm3:1.7b-q4_K_M", role: "small", file_gb: 1.1, memory_gb: 3.3, quality: "measured", evidence: "6, 5, 5 of 8 local but 1/6 GitHub, 20 s median per question" }
2131
+ ] }
2132
+ ]
2133
+ };
2134
+
2135
+ // src/local-prompts.ts
2136
+ var catalog = model_catalog_default;
2137
+ function picksOf(job) {
2138
+ return catalog.jobs.find((entry) => entry.job === job)?.picks.map((pick) => pick.tag) ?? [];
2139
+ }
2140
+ var cookbook = (name) => `docs/cookbooks/${name}.md`;
2141
+ var SOURCES = {
2142
+ miniCpmOcr: ["https://huggingface.co/openbmb/MiniCPM-OCR-1.0", "https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf"],
2143
+ glmOcr: ["https://ollama.com/library/glm-ocr", "https://huggingface.co/zai-org/GLM-OCR"],
2144
+ lfm3Vl: ["https://huggingface.co/LiquidAI/LFM3-VL-2B", "https://huggingface.co/LiquidAI/LFM3-VL-2B-GGUF", "https://ollama.com/library/lfm3-vl"],
2145
+ lfm3Omni: ["https://huggingface.co/LiquidAI/LFM3-Omni-2B", "https://ollama.com/library/lfm3-omni"],
2146
+ embeddingGemma: ["https://huggingface.co/google/embeddinggemma-2", "https://ai.google.dev/gemma/docs/embeddinggemma/inference-embeddinggemma-with-sentence-transformers"],
2147
+ nomic: ["https://huggingface.co/nomic-ai/nomic-embed-text-v1.5"],
2148
+ strandsDecider: ["https://ollama.com/library/strands-decider", "https://raw.githubusercontent.com/strands-labs/strands-decider/main/README.md"],
2149
+ nimble: ["https://ollama.com/library/nimble-v3:latest"],
2150
+ bonsai9b: ["https://huggingface.co/prism-ml/Ternary-Bonsai-2-9B-gguf", "https://ollama.com/library/bonsai-2:9b"],
2151
+ bonsai27b: ["https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-gguf", "https://ollama.com/library/bonsai-2"],
2152
+ lfm3: ["https://huggingface.co/LiquidAI/LFM3-1.7B", "https://ollama.com/library/lfm3"],
2153
+ nemotronBolt: ["https://huggingface.co/nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-4GB"],
2154
+ lfm3Audio: ["https://huggingface.co/LiquidAI/LFM3-Audio-2B", "https://ollama.com/library/lfm3-audio"],
2155
+ qwen38: ["https://huggingface.co/Qwen/Qwen3.8-27B", "https://ollama.com/library/qwen3.8"],
2156
+ qwen35: ["https://huggingface.co/Qwen/Qwen3.5-4B", "https://ollama.com/library/qwen3.5"],
2157
+ phonon: ["https://ollama.com/library/phonon:en"]
2158
+ };
2159
+ var EMBEDDING_PREFIXES = {
2160
+ embeddinggemma: {
2161
+ query: "task: search result | query: ",
2162
+ document: "title: none | text: ",
2163
+ codeQuery: "task: code retrieval | query: ",
2164
+ similarity: "task: sentence similarity | query: ",
2165
+ clustering: "task: clustering | query: ",
2166
+ classification: "task: classification | query: "
2167
+ },
2168
+ // A prefix is required; the card names clustering: for removing semantic duplicates.
2169
+ "nomic-embed-text": { query: "search_query: ", document: "search_document: ", clustering: "clustering: ", classification: "classification: " }
2170
+ };
2171
+ var GEMMA = EMBEDDING_PREFIXES.embeddinggemma;
2172
+ var NOMIC = EMBEDDING_PREFIXES["nomic-embed-text"];
2173
+ var OCR_PROMPTS = { text: "Text Recognition:", table: "Table Recognition:", formula: "Formula Recognition:" };
2174
+ var OCR_TRAINED = ["minicpm-ocr", "glm-ocr"];
2175
+ var OCR_PLAIN = {
2176
+ text: "Transcribe all text in the image exactly, line by line. Output only the text.",
2177
+ table: "Transcribe the table in the image as a Markdown table. Output only the table."
2178
+ };
2179
+ var ocrPrompt = (kind, model) => {
2180
+ const name = (model ?? "").toLowerCase().replace(/^(?:hf\.co|huggingface\.co)\/[^/]+\//, "");
2181
+ return model === void 0 || OCR_TRAINED.some((family) => name.startsWith(family)) ? OCR_PROMPTS[kind] : OCR_PLAIN[kind];
2182
+ };
2183
+ var EDIT_FORMAT = `Return each change to an existing file as the file's path (copied from its "--- file:" line), then:
2184
+ <<<<<<< SEARCH
2185
+ exact current lines (5 to 30, unique in the file, indentation exact)
2186
+ =======
2187
+ replacement lines
2188
+ >>>>>>> REPLACE
2189
+ Copy SEARCH lines character for character; prefer several small blocks to one large one. For a new file, or one under 30 lines, give its path on one line and the whole file in one fenced block. If the file was not shown, give the code in one fenced block and say which file and function it belongs in.`;
2190
+ var NO_TOOLS = "You have no tools and cannot open files: everything you need is in this message. Answer in plain text.";
2191
+ var WORKFLOWS = {
2192
+ "read-text": {
2193
+ job: "ocr",
2194
+ title: "Read all text in an image, exactly",
2195
+ input: "files: the images",
2196
+ prompt: (_request, model) => ocrPrompt("text", model)
2197
+ },
2198
+ "read-table": {
2199
+ job: "ocr",
2200
+ title: "Read a table in an image as Markdown",
2201
+ input: "files: the images",
2202
+ prompt: (_request, model) => ocrPrompt("table", model)
2203
+ },
2204
+ "read-fields": {
2205
+ job: "ocr",
2206
+ title: "Pull named fields from a receipt, invoice or form image as JSON",
2207
+ input: "files: the images; prompt: the field names",
2208
+ prompt: (request) => `Extract these fields from the image: ${request ?? "all labelled fields"}. Format the output as a JSON object with these keys, values exactly as printed; use "###" for a field the image does not show.`
2209
+ },
2210
+ "answer-image": {
2211
+ job: "image-recognition",
2212
+ title: "Answer a question about an image",
2213
+ input: "files: the images; prompt: the question",
2214
+ system: `Answer the question about the image.
2215
+ 1. Find the part of the image the question is about.
2216
+ 2. Read any text there exactly as printed.
2217
+ 3. Answer with just the value or label, or one or two sentences.
2218
+ If the image does not show it, answer "not visible". Never guess at what is cut off.`
2219
+ },
2220
+ "describe-screen": {
2221
+ job: "image-recognition",
2222
+ title: "Describe a screenshot for a developer: text, errors, state",
2223
+ input: "files: the screenshot; prompt: what to look for (optional)",
2224
+ system: `You describe a software screenshot for a developer, in short bullets, in this order:
2225
+ - the app or page shown;
2226
+ - headings and main text, quoted exactly;
2227
+ - any error, warning or empty state, quoted exactly;
2228
+ - the state of the controls that matter (selected, disabled, filled in);
2229
+ - anything that looks broken or misaligned.
2230
+ Describe only what is visible.`
2231
+ },
2232
+ "change-code": {
2233
+ job: "coding",
2234
+ title: "Make one small code change",
2235
+ input: "files: the file to change; prompt: the change",
2236
+ system: `${NO_TOOLS}
2237
+ You make one small code change.
2238
+ 1. Find where the change belongs in the code shown.
2239
+ 2. Follow the file's existing names, style and error handling.
2240
+ 3. Change as few lines as the task needs; touch nothing else.
2241
+ ${EDIT_FORMAT}
2242
+ Explain nothing unless asked. If the task is a question, answer it in a few lines.`
2243
+ },
2244
+ "fix-bug": {
2245
+ prefer: "capable",
2246
+ job: "coding",
2247
+ title: "Fix a bug with the smallest change",
2248
+ input: "files: the suspect code; prompt: the error message or symptom",
2249
+ system: `${NO_TOOLS}
2250
+ You fix one bug with the smallest change.
2251
+ 1. Read the error or symptom, then the code shown.
2252
+ 2. Name the cause in one sentence, quoting the line that causes it.
2253
+ 3. Change only what the fix needs.
2254
+ Answer with the cause line, then the edits.
2255
+ ${EDIT_FORMAT}
2256
+ If the code shown cannot cause the symptom, say so and name the file or function to look at instead.`
2257
+ },
2258
+ implement: {
2259
+ prefer: "capable",
2260
+ job: "coding",
2261
+ title: "Add a small feature to existing code",
2262
+ input: "files: the code; prompt: the feature",
2263
+ system: `${NO_TOOLS}
2264
+ You add one small feature to the code shown.
2265
+ 1. Find the function or place it belongs; reuse helpers the file already has.
2266
+ 2. Follow the file's names, style and error handling.
2267
+ 3. Handle the empty and error cases the feature can meet.
2268
+ ${EDIT_FORMAT}
2269
+ End with one line: what to test.`
2270
+ },
2271
+ "write-tests": {
2272
+ prefer: "capable",
2273
+ job: "coding",
2274
+ title: "Write unit tests for the code shown",
2275
+ input: "files: the code (and an existing test, for style); prompt: what to cover (optional)",
2276
+ system: `${NO_TOOLS}
2277
+ You write unit tests for the code shown; you never change that code.
2278
+ 1. List the behaviours: the normal case, edge cases (empty, zero, missing, boundary) and one failure case.
2279
+ 2. Use the test framework and import style the project already uses; copy them from the files shown.
2280
+ 3. Write one small test per behaviour, named after it, with literal expected values.
2281
+ Answer with the test file's path on one line, then the whole file in one fenced block.`
2282
+ },
2283
+ "review-code": {
2284
+ prefer: "capable",
2285
+ job: "coding",
2286
+ title: "Find real defects in code (not style)",
2287
+ input: "files: the code; prompt: what to focus on (optional)",
2288
+ system: `${NO_TOOLS}
2289
+ You review the code shown for real defects only: wrong logic, unhandled errors, off-by-one, null or undefined access, leaks, injection, races. Ignore style and naming.
2290
+ For each defect write one line: path:line - the problem - the fix.
2291
+ Report at most 5, the surest first. If you find none, answer "no defects found". Never invent a line you did not see.`
2292
+ },
2293
+ "explain-code": {
2294
+ job: "coding",
2295
+ title: "Explain code to someone about to change it",
2296
+ input: "files: the code; prompt: what to explain (optional)",
2297
+ system: `${NO_TOOLS}
2298
+ Explain the code shown to a developer who is about to change it, in at most 6 bullets:
2299
+ what it does (one sentence); its inputs and outputs; side effects (files, network, shared state); what calls it, if visible; the one thing most likely to break when it changes.
2300
+ Name functions and variables exactly.`
2301
+ },
2302
+ "summarize-log": {
2303
+ think: false,
2304
+ prefer: "capable",
2305
+ job: "summarize",
2306
+ title: "Summarize a log: errors grouped, counted, first failure",
2307
+ input: "files: the log; prompt: what you are investigating (optional)",
2308
+ system: `${NO_TOOLS}
2309
+ You read a log for a developer. Report:
2310
+ 1. Errors and warnings, grouped by message, each with its count and first and last timestamp.
2311
+ 2. The first error in time order, quoted exactly.
2312
+ 3. The likely cause, only if the log shows it.
2313
+ Quote messages exactly, skip routine lines, at most 10 bullets.`
2314
+ },
2315
+ "summarize-text": {
2316
+ think: false,
2317
+ job: "summarize",
2318
+ title: "Summarize text for someone who will act on it",
2319
+ input: "files: the text; prompt: what you need from it (optional)",
2320
+ system: `${NO_TOOLS}
2321
+ Summarize the text for someone who will act on it, in short bullets: the decisions, the numbers, the deadlines and the open questions, plus whatever the request asks for.
2322
+ Quote numbers, names, versions and paths exactly. Write "not stated" when the text does not say; never guess. No introduction or closing.`
2323
+ },
2324
+ "summarize-diff": {
2325
+ think: false,
2326
+ prefer: "capable",
2327
+ job: "summarize",
2328
+ title: "Summarize a diff for a reviewer: changes and risks",
2329
+ input: "files: the diff or patch",
2330
+ system: `${NO_TOOLS}
2331
+ You summarize a code diff for a reviewer, in at most 8 bullets:
2332
+ one line per file on what changed; any change in behaviour; risks such as a removed check, a changed default, a new dependency or a missing test.
2333
+ Name files, functions and settings exactly.`
2334
+ },
2335
+ "extract-json": {
2336
+ think: false,
2337
+ job: "summarize",
2338
+ title: "Extract named fields from text into JSON",
2339
+ input: "files: the text; prompt: the field names (or a JSON schema)",
2340
+ system: `${NO_TOOLS}
2341
+ Extract the fields the request names from the text into one JSON object.
2342
+ Use the values exactly as written in the text; use null for a field the text does not give; never invent a value.
2343
+ Output only the JSON object.`,
2344
+ format: "json"
2345
+ }
2346
+ };
2347
+ var WORKFLOW_NAMES = Object.keys(WORKFLOWS);
2348
+ var BONSAI_9B = {
2349
+ think: false,
2350
+ options: { temperature: 0.7, top_p: 0.8, top_k: 20, min_p: 0, presence_penalty: 1.5, repeat_penalty: 1 },
2351
+ notes: "With think:true use the card's thinking preset (temperature 1.0, top_p 0.95, presence_penalty 1.0) and allow at least 8,192 output tokens; never greedy decoding with thinking on.",
2352
+ sources: SOURCES.bonsai9b
2353
+ };
2354
+ var qwenNonThinking = (sources) => ({
2355
+ think: false,
2356
+ options: { temperature: 0.7, top_p: 0.8, top_k: 20, min_p: 0, presence_penalty: 1.5, repeat_penalty: 1 },
2357
+ notes: "Thinks by default; with think:true use the card's thinking preset (temperature 1.0, top_p 0.95, top_k 20; Qwen3.8 presence_penalty 0, Qwen3.5 1.5, or 0 with temperature 0.6 for precise coding) and a large output budget (Qwen3.5 32,768 tokens). Raise presence_penalty (up to 2) if it repeats.",
2358
+ sources
2359
+ });
2360
+ var CHAT_MODELS = {
2361
+ lfm3: {
2362
+ options: { temperature: 0.1, top_k: 50, repeat_penalty: 1.1 },
2363
+ notes: "Thinks by default; its card calls thinking off inconsistent, so it stays on. Not for agentic coding or multi-step reasoning.",
2364
+ sources: SOURCES.lfm3
2365
+ },
2366
+ "nvidia-nemotron-3.5-bolt": {
2367
+ options: { temperature: 1, top_p: 0.95 },
2368
+ // `ollama show` of the hf.co Q4_K_M tag, 2026-11-08: no thinking capability, and the template
2369
+ // defaults enable_thinking to false.
2370
+ notes: "The card says it reasons by default, but its GGUF template turns thinking off and Ollama reports no thinking capability, so it answers directly; think:true is refused.",
2371
+ sources: SOURCES.nemotronBolt
2372
+ },
2373
+ "bonsai-2": BONSAI_9B,
2374
+ // The card documents the same instruct (non-thinking) preset as the 9B; Ollama lists think
2375
+ // values false, low, medium and xhigh for this tag.
2376
+ "bonsai-2:27b": {
2377
+ ...BONSAI_9B,
2378
+ notes: "With think:true (or low, medium, xhigh) use the card's thinking preset (temperature 1.0, top_p 0.95, top_k 20, min_p 0.05, presence_penalty 0) and allow about 16,384 output tokens.",
2379
+ sources: SOURCES.bonsai27b
2380
+ },
2381
+ "qwen3.8": qwenNonThinking(SOURCES.qwen38),
2382
+ "qwen3.5": qwenNonThinking(SOURCES.qwen35)
2383
+ };
2384
+ var CHAT_SOURCES = [...SOURCES.lfm3, ...SOURCES.nemotronBolt, ...SOURCES.bonsai9b, ...SOURCES.bonsai27b, ...SOURCES.qwen38, ...SOURCES.qwen35];
2385
+ var LOCAL_JOBS = {
2386
+ classify: {
2387
+ job: "classify",
2388
+ task: "decision",
2389
+ catalogJob: "classify",
2390
+ defaultPicks: picksOf("classify"),
2391
+ models: {
2392
+ "strands-decider": { notes: "State and questions together must fit 4,096 tokens; weak on long, multi-step documents. Its README: answers at confidence 0.9 or more are right about 95% of the time; confirm the rest.", sources: SOURCES.strandsDecider },
2393
+ "nimble-v3": { notes: "Documents num_ctx 32768 (routing sizes it here); CC BY-NC 4.0, noncommercial.", sources: SOURCES.nimble }
2394
+ },
2395
+ notes: "A decision model reads state and questions, no prompt. Ask short questions about the state, one concern each, and give a choice question an explicit fallback option such as other.",
2396
+ sources: [...SOURCES.strandsDecider, ...SOURCES.nimble, cookbook("classify")]
2397
+ },
2398
+ "find-code": {
2399
+ job: "find-code",
2400
+ task: "embedding",
2401
+ catalogJob: "find-code",
2402
+ defaultPicks: picksOf("find-code"),
2403
+ needs: "nearest.query: the issue, or what the code does",
2404
+ prefixes: { embeddinggemma: { query: GEMMA.codeQuery, document: GEMMA.document }, "nomic-embed-text": { query: NOMIC.query, document: NOMIC.document } },
2405
+ // Measured 2026-11-08 on the find-code set (10 questions, 1,039 chunks): code retrieval with
2406
+ // title: none 8 first / 9 in top 3; search result 8/8; code retrieval with the file as title 7/9.
2407
+ notes: "task: code retrieval (EmbeddingGemma's code-search prefix) measured best here: right file first 8/10, in the top 3 9/10. Read the top ranges before relying on them.",
2408
+ sources: [...SOURCES.embeddingGemma, ...SOURCES.nomic, cookbook("find-code")]
2409
+ },
2410
+ duplicates: {
2411
+ job: "duplicates",
2412
+ task: "embedding",
2413
+ catalogJob: "duplicates",
2414
+ defaultPicks: picksOf("duplicates"),
2415
+ // Symmetric: every item, the query included, takes the same prefix.
2416
+ prefixes: {
2417
+ // The measured 8/12 used search_document:; the card's clustering: is documented but unmeasured here.
2418
+ "nomic-embed-text": { query: NOMIC.document, document: NOMIC.document },
2419
+ embeddinggemma: { query: GEMMA.similarity, document: GEMMA.similarity }
2420
+ },
2421
+ notes: "Uses the measured search_document: (8/12 real issue pairs); nomic's card also documents clustering: for deduplication, unmeasured here. A high score is a candidate, never a verdict.",
2422
+ sources: [...SOURCES.nomic, ...SOURCES.embeddingGemma, cookbook("duplicates")]
2423
+ },
2424
+ ocr: {
2425
+ job: "ocr",
2426
+ task: "vision",
2427
+ catalogJob: "ocr",
2428
+ defaultPicks: picksOf("ocr"),
2429
+ prompt: ({ model }) => ocrPrompt("text", model),
2430
+ // Both publishers' Ollama Modelfiles set temperature 0; the hf.co MiniCPM tag ships no parameters.
2431
+ models: {
2432
+ "minicpm-ocr": {
2433
+ options: { temperature: 0 },
2434
+ notes: "Chinese and English; tables come back as HTML. Its Modelfile sets num_ctx 8192.",
2435
+ sources: [...SOURCES.miniCpmOcr, "https://raw.githubusercontent.com/OpenBMB/MiniCPM-OCR/main/docs/ollama_deploy.md"]
2436
+ },
2437
+ "glm-ocr": {
2438
+ options: { temperature: 0 },
2439
+ notes: "Also documents Figure Recognition:.",
2440
+ sources: [...SOURCES.glmOcr, "https://github.com/zai-org/GLM-OCR/blob/main/examples/ollama-deploy/README.md"]
2441
+ }
2442
+ },
2443
+ notes: "The OCR picks were trained on Text Recognition:, Table Recognition: and Formula Recognition:; a general vision model gets the same request in plain words. Crop to the text first.",
2444
+ sources: [...SOURCES.miniCpmOcr, ...SOURCES.glmOcr, cookbook("ocr")]
2445
+ },
2446
+ "image-recognition": {
2447
+ job: "image-recognition",
2448
+ task: "vision",
2449
+ catalogJob: "image-recognition",
2450
+ defaultPicks: picksOf("image-recognition"),
2451
+ needs: "prompt: your question about the image",
2452
+ systemPrompt: WORKFLOWS["answer-image"].system,
2453
+ models: {
2454
+ "lfm3-vl": {
2455
+ options: { temperature: 0.1, top_p: 1, top_k: 50, min_p: 0, repeat_penalty: 1.1 },
2456
+ notes: "Thinks before it answers by default (Ollama lists think values true and false; off is unmeasured here); images go in at native resolution, so a large one costs many tokens.",
2457
+ sources: SOURCES.lfm3Vl
2458
+ },
2459
+ "lfm3-omni": {
2460
+ systemPrompt: "Answer questions about the provided image. Base every answer only on what can be seen.",
2461
+ options: { temperature: 0 },
2462
+ notes: "Non-reasoning: never send think:true. Up to 2,048 tokens per image. The card gives 32K context; the Ollama tag advertises 128K.",
2463
+ sources: SOURCES.lfm3Omni
2464
+ },
2465
+ "bonsai-2": BONSAI_9B,
2466
+ "qwen3.8": qwenNonThinking(SOURCES.qwen38),
2467
+ "qwen3.5": qwenNonThinking(SOURCES.qwen35)
2468
+ },
2469
+ notes: 'Ask a specific question, not "describe this". Tested on drawn images only.',
2470
+ sources: [...SOURCES.lfm3Vl, ...SOURCES.lfm3Omni, ...SOURCES.bonsai9b, cookbook("image-recognition")]
2471
+ },
2472
+ "image-search": {
2473
+ job: "image-search",
2474
+ task: "embedding",
2475
+ catalogJob: "image-search",
2476
+ defaultPicks: picksOf("image-search"),
2477
+ needs: "nearest.query: a description of the image",
2478
+ prefixes: { embeddinggemma: { query: GEMMA.query, document: GEMMA.document } },
2479
+ notes: "Images are embedded bare: the prefixes apply to text only. Open the top match.",
2480
+ sources: [...SOURCES.embeddingGemma, cookbook("image-search")]
2481
+ },
2482
+ coding: {
2483
+ job: "coding",
2484
+ task: "coding",
2485
+ catalogJob: "coding",
2486
+ defaultPicks: picksOf("coding"),
2487
+ needs: "prompt: the change to make",
2488
+ systemPrompt: WORKFLOWS["change-code"].system,
2489
+ models: CHAT_MODELS,
2490
+ notes: "The answer is SEARCH/REPLACE blocks: apply one only where its SEARCH text matches exactly once, then run the tests.",
2491
+ sources: [...CHAT_SOURCES, cookbook("coding")]
2492
+ },
2493
+ summarize: {
2494
+ job: "summarize",
2495
+ task: "completion",
2496
+ catalogJob: "coding",
2497
+ defaultPicks: picksOf("coding"),
2498
+ prompt: () => "Summarize the text below.",
2499
+ systemPrompt: WORKFLOWS["summarize-text"].system,
2500
+ models: CHAT_MODELS,
2501
+ notes: "The coding picks: the same chat models apply (docs/cookbooks/batch-text.md). Check every number and name you reuse.",
2502
+ sources: [...CHAT_SOURCES, cookbook("batch-text")]
2503
+ }
2504
+ };
2505
+ var TOOL_JOBS = {
2506
+ "speech-to-text": {
2507
+ tool: "audio",
2508
+ defaultPicks: picksOf("speech-to-text"),
2509
+ // The audio tool calls Ollama's transcription endpoint, which builds the model's prompt; these
2510
+ // are lfm3-audio's documented prompts for a direct chat call.
2511
+ systemPrompts: {
2512
+ transcribe: (language) => language ? `Perform ASR in ${language}.` : "Perform ASR.",
2513
+ translate: (source, target) => `Translate the following speech from ${source} to ${target} in text form.`
2514
+ },
2515
+ notes: "WAV input only. Numbers and names are the usual misses; translation works best to or from English.",
2516
+ sources: [...SOURCES.lfm3Audio, cookbook("speech-to-text")]
2517
+ },
2518
+ "text-to-speech": {
2519
+ tool: "audio",
2520
+ defaultPicks: picksOf("text-to-speech"),
2521
+ notes: "Send plain text: Markdown marks may be read aloud. One voice (default); 24 kHz mono WAV.",
2522
+ sources: [...SOURCES.phonon, cookbook("text-to-speech")]
2523
+ },
2524
+ "research-agent": {
2525
+ tool: "delegate_research",
2526
+ defaultPicks: picksOf("research-agent"),
2527
+ notes: "The adapters' system prompts are below. Discard an escalate verdict and verify the citations.",
2528
+ sources: [cookbook("research-agent")]
2529
+ },
2530
+ sweep: {
2531
+ tool: "sweep",
2532
+ defaultPicks: picksOf("classify"),
2533
+ /** The decision state of one chunk, and the explain step's user message for a confirmed flag. */
2534
+ state: (label, text) => `Source: ${label}
2535
+
2536
+ ${text}`,
2537
+ explain: (prompt, label, text) => `${prompt}
2538
+
2539
+ Source: ${label}
2540
+
2541
+ ${text}`,
2542
+ // FreeLlama's own setting for the short explain note, not a model's documented value.
2543
+ explainControls: { think: false, temperature: 0 },
2544
+ notes: 'One yes/no question per concern, phrased about one chunk ("Does this code ..."); a noul answer flags at p >= 0.5.',
2545
+ sources: [cookbook("sweep")]
2546
+ }
2547
+ };
2548
+ var BASH_SYSTEM_PROMPT = `You answer questions about the code in this read-only workspace (the current directory) by looking, not guessing. Reply with exactly one JSON object per turn and nothing else.
2549
+
2550
+ Actions, in order of preference:
2551
+ {"action":"search","pattern":"EMBEDDINGS_PER_JOB"} finds text in files and prints path:line:text. Literal and case-sensitive; add "regex":true for a|b or \\s, "ignoreCase":true, "glob":"*.rs", or "path":"packages/mcp" to narrow it.
2552
+ {"action":"read","path":"packages/mcp/src/tools/run-task.ts","start":310,"end":340} shows numbered lines of one file.
2553
+ {"action":"list","path":"packages"} shows files and folders two levels deep.
2554
+ {"action":"shell","command":"rg -n -w build_decision_request -g '*.rs'"} runs one read-only pipeline when the actions above cannot. Use rg (rg -n PATTERN PATH; -w whole word, -l file names only, -t rust or -t ts, -g '!tests/') or grep -rn PATTERN PATH --exclude-dir=node_modules, plus find, head, sed -n '10,40p' FILE, wc and git log/show/grep/blame. No network, redirection, $(...), variables or loops.
2555
+ {"action":"page","step":2,"page":2} shows the next page of a long earlier result.
2556
+ {"action":"finish","answer":"32. Source: packages/mcp/src/tools/run-task.ts:322"} ends the task.
2557
+
2558
+ How to research:
2559
+ 1. Search for the exact name in the question (a function, constant, type, flag or error text). A path in the question may be partial; search finds the real one.
2560
+ 2. Read the lines around the best hit to confirm it.
2561
+ 3. Finish once the answer is confirmed, usually after 2 to 4 actions. Never repeat an action: its result is already above.
2562
+ Keep every result small: search before reading, read 30 to 80 lines around a hit rather than a whole file, use rg -l or rg -c to see where matches are before printing them, and end long shell output with | head -n 40 or | tail -n 40.
2563
+ For "which files call X", search "X(" and list every file except the one that defines X. For "is there an X", a search with no matches over the whole workspace means no.
2564
+
2565
+ Answer format: the direct answer first (the value, name, list or yes/no the question asks for), then "Source: path:line" for each file it comes from, copied from the tool output. Answer "not found" only after a search of the whole workspace returned no matches.`;
2566
+ var CODER_SYSTEM_PROMPT = [
2567
+ "You change code in an isolated copy of the user's project (the current directory) and test the change. The user's own files are never touched: they review your diff afterwards. Reply with exactly one JSON object per turn and nothing else.",
2568
+ "",
2569
+ "Actions, in order of preference:",
2570
+ '{"action":"search","pattern":"parsePort"} finds text in files and prints path:line:text. Literal and case-sensitive; add "regex":true, "ignoreCase":true, "glob":"*.ts" or "path":"src" to narrow it.',
2571
+ '{"action":"read","path":"src/config.ts","start":40,"end":70} shows numbered lines of one file.',
2572
+ '{"action":"edit","path":"src/config.ts","search":" const port = 8080;\\n return port;","replace":" const port = Number(env.PORT ?? 8080);\\n return port;"} replaces text that occurs exactly once in the file. Copy "search" from the read output without the line numbers and with its exact indentation: 5 to 30 lines, enough to be unique.',
2573
+ '{"action":"write","path":"src/limits.ts","content":"export const MAX = 10;\\n"} creates a file, or replaces a whole small file.',
2574
+ '{"action":"run","command":"yarn test"} runs one of the allowed commands listed with the task, exactly as written there, and shows its exit code and output.',
2575
+ '{"action":"list","path":"src"} shows files and folders two levels deep.',
2576
+ '{"action":"shell","command":"git diff --stat"} runs one read-only pipeline (rg, grep, find, head, sed -n, wc, git log/diff/show). It cannot change files; edit and write do.',
2577
+ '{"action":"page","step":2,"page":2} shows the next page of a long earlier result.',
2578
+ '{"action":"finish","answer":"Changed src/config.ts to read PORT from the environment, because the port was fixed. Test: yarn test passed."} ends the task.',
2579
+ "",
2580
+ "How to work:",
2581
+ "1. Search for the names in the task, then read the lines you will change.",
2582
+ "2. Edit with a search block copied exactly from the read output. If an edit fails, read those lines again and copy them; never guess.",
2583
+ "3. Run an allowed command to test the change. If it fails, read the error, fix the code and run it again.",
2584
+ "4. Finish with what changed, why, and the last test result. With no allowed command, say the change is untested.",
2585
+ "Keep results small: read line ranges, not whole files, and end long shell output with | head -n 40."
2586
+ ].join("\n");
2587
+ var APPLY_EDITS = "Apply the edits yourself: for each block, check that its SEARCH text occurs exactly once in the named file, indentation included; replace it with the REPLACE text; skip a block that matches zero or several times. A path followed by one fenced block is a whole new file. Then run the tests: the edits are a small model's candidates.";
2588
+ var AGENT_GUIDE = `Use FreeLlama to run bounded work on local models and keep it out of your context.
2589
+
2590
+ Offload when the answer is cheap to check and the work is 10 or more similar items, or an input over about 2,000 tokens you would otherwise read whole. Do it yourself: judgment you must stand behind, edits across files, one short item.
2591
+
2592
+ Name a workflow and run_task does the rest: it picks the first catalog model for the job that is installed and fits now and gives it step-by-step instructions for the task. Workflows: read-text, read-table, read-fields (images); answer-image, describe-screen; change-code, fix-bug, implement, write-tests, review-code, explain-code; summarize-log, summarize-text, summarize-diff, extract-json. For labels, code search and duplicates use job classify, find-code or duplicates. Any field you pass wins; job_profile says what was filled in. Name inputs with files; for one answer per file send tasks:[{id, independent:true, task:{files:[path]}}] with job at the top level. For a whole code tree, log or site use sweep; for a question about a repository, delegate_research.
2593
+
2594
+ Verify: local answers are candidates. Each run_task result ends with check: lines worked out from it (uncertain answers, close matches, facts not in the source, edits that do not match the file); act on those first, and discard a research result whose verification.recommendation is "escalate".
2595
+
2596
+ Coding: the answer is SEARCH/REPLACE blocks. ${APPLY_EDITS}
2597
+
2598
+ Hand a result on: saveAs:"<name>" writes the answer to handoffs/<name>.md and returns handoff_file; a later run_task reads it with files:[handoff_file].
2599
+
2600
+ Approval: pull a model only after the user approves one exact tag and its size (ollama_pull approvedSizeBytes). Unload only a model you warmed or the user named; delete only a tag the user names. A memory refusal names what holds memory: tell the user and never stop their processes.`;
1531
2601
 
1532
2602
  // src/research/sandbox.ts
1533
- import { accessSync, constants as fsConstants, mkdirSync as mkdirSync3, mkdtempSync as mkdtempSync2, realpathSync, rmSync as rmSync2, symlinkSync, writeFileSync as writeFileSync2 } from "node:fs";
2603
+ import { accessSync, constants as fsConstants, existsSync as existsSync4, mkdirSync as mkdirSync3, mkdtempSync as mkdtempSync2, realpathSync, rmSync as rmSync2, symlinkSync, writeFileSync as writeFileSync2 } from "node:fs";
1534
2604
  import { tmpdir as tmpdir2 } from "node:os";
1535
- import path4 from "node:path";
2605
+ import path6 from "node:path";
1536
2606
 
1537
2607
  // src/research/proc.ts
1538
2608
  import { spawn, spawnSync } from "node:child_process";
@@ -1830,6 +2900,9 @@ var POSIX_WORDCHARS = "\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\
1830
2900
  function within2(char, set) {
1831
2901
  return set.includes(char);
1832
2902
  }
2903
+ function shlexTokens(text, options) {
2904
+ return [...shlexIter(text, options)];
2905
+ }
1833
2906
  function* shlexIter(text, options) {
1834
2907
  const posix = options.posix;
1835
2908
  const punctuation = options.punctuationChars ?? "";
@@ -2116,10 +3189,43 @@ function assertCommandConfined(root, commandText) {
2116
3189
  }
2117
3190
  var OPERATOR_CHARS = new Set(";&|()<>");
2118
3191
  var SEPARATORS = /* @__PURE__ */ new Set(["|", "||", "&&", ";"]);
3192
+ var LITERAL_OPEN = "\uE000";
3193
+ var LITERAL_CLOSE = "\uE001";
3194
+ function protectLiteralParens(commandText) {
3195
+ let out = "";
3196
+ let quote;
3197
+ for (let index = 0; index < commandText.length; index++) {
3198
+ const char = commandText[index];
3199
+ if (quote === void 0 && char === "\\" && (commandText[index + 1] === "(" || commandText[index + 1] === ")")) {
3200
+ out += commandText[index + 1] === "(" ? LITERAL_OPEN : LITERAL_CLOSE;
3201
+ index += 1;
3202
+ continue;
3203
+ }
3204
+ if (quote === void 0 && char === "\\") {
3205
+ out += char + (commandText[index + 1] ?? "");
3206
+ index += 1;
3207
+ continue;
3208
+ }
3209
+ if (quote === void 0 && (char === "'" || char === '"')) quote = char;
3210
+ else if (quote === char) quote = void 0;
3211
+ else if (quote !== void 0 && (char === "(" || char === ")")) {
3212
+ out += char === "(" ? LITERAL_OPEN : LITERAL_CLOSE;
3213
+ continue;
3214
+ }
3215
+ out += char;
3216
+ }
3217
+ return out;
3218
+ }
3219
+ var restoreParens = (token) => token.replaceAll(LITERAL_OPEN, "(").replaceAll(LITERAL_CLOSE, ")");
2119
3220
  function segments(commandText) {
2120
- const tokens = shlexIter(commandText, { posix: true, punctuationChars: ";&|()<>", whitespaceSplit: true, commenters: "" });
3221
+ const tokens = [...shlexIter(protectLiteralParens(commandText), { posix: true, punctuationChars: ";&|()<>", whitespaceSplit: true, commenters: "" })];
2121
3222
  const result = [[]];
2122
- for (const token of tokens) {
3223
+ for (const raw of tokens) {
3224
+ if (raw.includes(LITERAL_OPEN) || raw.includes(LITERAL_CLOSE)) {
3225
+ result[result.length - 1].push(restoreParens(raw));
3226
+ continue;
3227
+ }
3228
+ const token = raw;
2123
3229
  if (SEPARATORS.has(token)) {
2124
3230
  result.push([]);
2125
3231
  } else if (token && [...token].every((char) => OPERATOR_CHARS.has(char))) {
@@ -2175,7 +3281,7 @@ function checkTool(words) {
2175
3281
  }
2176
3282
  const target = xargsCommand(args);
2177
3283
  if (!["echo", "printf", "true", "false"].includes(target[0])) {
2178
- throw new ValidationError("command blocked: xargs filesystem arguments cannot be confined");
3284
+ throw new ValidationError(`command blocked: xargs can only feed echo/printf here (its file arguments cannot be confined). Search file contents directly instead: grep -rln --include='*.ts' "PATTERN" . or rg -l "PATTERN"`);
2179
3285
  }
2180
3286
  checkTool(target);
2181
3287
  }
@@ -2239,15 +3345,16 @@ function validateCommand(root, commandTextInput) {
2239
3345
  }
2240
3346
 
2241
3347
  // src/research/sandbox.ts
2242
- function macosProfile(workspace, scratch) {
3348
+ function macosProfile(workspace, scratch, readPaths = []) {
2243
3349
  const quote = (value) => `"${value.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
3350
+ const extra = readPaths.map((dir) => ` (subpath ${quote(dir)})`).join("");
2244
3351
  return `(version 1)
2245
3352
  (allow default)
2246
3353
  (deny network*)
2247
3354
  (deny file-write*)
2248
3355
  (allow file-write* (literal "/dev/null") (literal "/dev/tty") (literal "/dev/dtracehelper") (subpath ${quote(scratch)}))
2249
3356
  (deny file-read-data (subpath "/Users") (subpath "/Volumes") (subpath "/private/var/root"))
2250
- (allow file-read-data (subpath ${quote(workspace)}) (subpath ${quote(scratch)}))
3357
+ (allow file-read-data (subpath ${quote(workspace)}) (subpath ${quote(scratch)})${extra})
2251
3358
  `;
2252
3359
  }
2253
3360
  function which(name, envPath = process.env.PATH) {
@@ -2275,18 +3382,21 @@ var ReadOnlyShell = class {
2275
3382
  env = {};
2276
3383
  bash = "/bin/bash";
2277
3384
  scratch = null;
2278
- constructor(workspace) {
3385
+ /** Directories outside the workspace the shell may also read, such as a worktree's git directory. */
3386
+ readPaths;
3387
+ constructor(workspace, options = {}) {
2279
3388
  this.workspace = workspace;
3389
+ this.readPaths = options.readPaths ?? [];
2280
3390
  }
2281
3391
  enter() {
2282
3392
  this.workspace = resolvePath(this.workspace);
2283
- const scratch = mkdtempSync2(path4.join(path4.resolve(tmpdir2()), "freellama-shell-"));
3393
+ const scratch = mkdtempSync2(path6.join(path6.resolve(tmpdir2()), "freellama-shell-"));
2284
3394
  this.scratch = scratch;
2285
- const binDir = path4.join(scratch, "bin");
3395
+ const binDir = path6.join(scratch, "bin");
2286
3396
  mkdirSync3(binDir);
2287
3397
  for (const tool of READ_ONLY_TOOLS) {
2288
3398
  const located = which(tool);
2289
- if (located) symlinkSync(realpathSync(located), path4.join(binDir, tool));
3399
+ if (located) symlinkSync(realpathSync(located), path6.join(binDir, tool));
2290
3400
  }
2291
3401
  this.bash = which("bash") ?? "/bin/bash";
2292
3402
  const env = {};
@@ -2320,8 +3430,8 @@ var ReadOnlyShell = class {
2320
3430
  if ((process.env.FREELLAMA_AGENT_OS_SANDBOX ?? "auto") === "off") return [];
2321
3431
  const sandboxExec = which("sandbox-exec");
2322
3432
  if (sandboxExec) {
2323
- const profile = path4.join(scratch, "profile.sb");
2324
- writeFileSync2(profile, macosProfile(this.workspace, scratch), "utf8");
3433
+ const profile = path6.join(scratch, "profile.sb");
3434
+ writeFileSync2(profile, macosProfile(this.workspace, scratch, this.readPaths), "utf8");
2325
3435
  const prefix = [sandboxExec, "-f", profile];
2326
3436
  if (probe2([...prefix, this.bash, "-c", "true"], this.env)) {
2327
3437
  this.kind = "sandbox-exec+restricted-bash";
@@ -2338,6 +3448,7 @@ var ReadOnlyShell = class {
2338
3448
  "--ro-bind",
2339
3449
  this.workspace,
2340
3450
  this.workspace,
3451
+ ...this.readPaths.flatMap((dir) => ["--ro-bind", dir, dir]),
2341
3452
  "--ro-bind",
2342
3453
  binDir,
2343
3454
  binDir,
@@ -2355,18 +3466,324 @@ var ReadOnlyShell = class {
2355
3466
  }
2356
3467
  return [];
2357
3468
  }
3469
+ /**
3470
+ * Run one read-only tool with an argument vector, no shell: the structured `search` action uses
3471
+ * it so a pattern such as `${x}` or `(` is data, never shell syntax. Same OS sandbox, PATH and
3472
+ * scrubbed environment as `run`; exit 1 of a search means no matches.
3473
+ */
3474
+ /** Whether `tool` was found on the caller's PATH when the shell was entered. */
3475
+ hasTool(tool) {
3476
+ return this.scratch !== null && existsSync4(path6.join(this.scratch, "bin", tool));
3477
+ }
3478
+ async runTool(argv, timeoutSeconds) {
3479
+ const [tool, ...args] = argv;
3480
+ if (!tool || !READ_ONLY_TOOLS.includes(tool) || this.scratch === null) {
3481
+ throw new ValidationError(`tool ${tool ?? "(none)"} is not available to structured actions`);
3482
+ }
3483
+ const result = await runProcess(
3484
+ [...this.prefix, path6.join(this.scratch, "bin", tool), ...args],
3485
+ { cwd: this.workspace, env: this.env, timeoutSeconds }
3486
+ );
3487
+ if (result.returncode === 1 && !trimSpace(result.stderr)) return "(no matches)";
3488
+ if (result.returncode !== 0) throw new ToolExitError(result.returncode, result.stdout + result.stderr);
3489
+ return trimSpace(result.stdout) ? result.stdout : "(no matches)";
3490
+ }
2358
3491
  async run(commandText, timeoutSeconds) {
2359
3492
  const command = validateCommand(this.workspace, commandText);
3493
+ const tokens = shlexTokens(command, { posix: true, punctuationChars: ";&|()<>", whitespaceSplit: true, commenters: "" });
3494
+ if (tokens.some((token) => token === ";" || token === "&&" || token === "||")) {
3495
+ throw new ValidationError("command blocked: send one pipeline per turn so each execution outcome can be recorded");
3496
+ }
2360
3497
  const result = await runProcess(
2361
- [...this.prefix, this.bash, "--restricted", "--noprofile", "--norc", "-c", command],
3498
+ [...this.prefix, this.bash, "--restricted", "--noprofile", "--norc", "-o", "pipefail", "-c", command],
2362
3499
  { cwd: this.workspace, env: this.env, timeoutSeconds }
2363
3500
  );
2364
3501
  let output = result.stdout + result.stderr;
3502
+ if (result.returncode !== 0) {
3503
+ const parts = segments(command);
3504
+ const words = parts.length === 1 ? parts[0] : [];
3505
+ const isSearch = ["rg", "grep", "egrep", "fgrep"].includes(words[0] ?? "") || words[0] === "git" && words[1] === "grep";
3506
+ const isDiff = words[0] === "diff" || words[0] === "git" && words[1] === "diff";
3507
+ if (result.returncode === 1 && isSearch && !trimSpace(result.stderr)) {
3508
+ throw new ToolExitError(result.returncode, output, "empty");
3509
+ }
3510
+ if (!(result.returncode === 1 && isDiff && !trimSpace(result.stderr))) {
3511
+ throw new ToolExitError(result.returncode, output);
3512
+ }
3513
+ }
2365
3514
  if (!trimSpace(output)) output = `(no output, exit code ${result.returncode})`;
2366
3515
  return output;
2367
3516
  }
2368
3517
  };
2369
3518
 
3519
+ // src/research/workspace-overview.ts
3520
+ import { readdirSync as readdirSync3 } from "node:fs";
3521
+ import path7 from "node:path";
3522
+ var SKIP = /* @__PURE__ */ new Set(["node_modules", "target", ".git", "dist", "build", ".venv", "venv", "vendor", "__pycache__", ".octocode", "site-packages", "coverage"]);
3523
+ var MAX_ENTRIES = 2e4;
3524
+ var MAX_DEPTH = 8;
3525
+ var SOURCE_DIRS = /* @__PURE__ */ new Set(["src", "lib", "app", "source", "pkg", "cmd", "internal", "crates"]);
3526
+ function workspaceOverview(root) {
3527
+ let top;
3528
+ try {
3529
+ top = readdirSync3(root, { withFileTypes: true });
3530
+ } catch {
3531
+ return "";
3532
+ }
3533
+ const extensions = /* @__PURE__ */ new Map();
3534
+ const sourceRoots = /* @__PURE__ */ new Map();
3535
+ let files = 0;
3536
+ let seen = 0;
3537
+ const walk = (dir, depth, sourceRoot) => {
3538
+ if (depth > MAX_DEPTH || seen > MAX_ENTRIES) return;
3539
+ let entries;
3540
+ try {
3541
+ entries = readdirSync3(dir, { withFileTypes: true });
3542
+ } catch {
3543
+ return;
3544
+ }
3545
+ for (const entry of entries) {
3546
+ if (++seen > MAX_ENTRIES) return;
3547
+ if (entry.isDirectory()) {
3548
+ if (SKIP.has(entry.name) || entry.name.startsWith(".")) continue;
3549
+ const child = path7.join(dir, entry.name);
3550
+ walk(child, depth + 1, sourceRoot ?? (SOURCE_DIRS.has(entry.name) ? path7.relative(root, child) : void 0));
3551
+ } else if (entry.isFile()) {
3552
+ files += 1;
3553
+ const extension = path7.extname(entry.name).toLowerCase() || "(none)";
3554
+ extensions.set(extension, (extensions.get(extension) ?? 0) + 1);
3555
+ if (sourceRoot) sourceRoots.set(sourceRoot, (sourceRoots.get(sourceRoot) ?? 0) + 1);
3556
+ }
3557
+ }
3558
+ };
3559
+ walk(root, 0);
3560
+ const listing = top.filter((entry) => !SKIP.has(entry.name) && !entry.name.startsWith(".")).sort((left, right) => Number(right.isDirectory()) - Number(left.isDirectory()) || left.name.localeCompare(right.name)).slice(0, 25).map((entry) => entry.isDirectory() ? `${entry.name}/` : entry.name);
3561
+ const types = [...extensions.entries()].sort((left, right) => right[1] - left[1]).slice(0, 8).map(([extension, count]) => `${extension} ${count}`);
3562
+ const sources = [...sourceRoots.entries()].sort((left, right) => right[1] - left[1]).slice(0, 8).map(([dir, count]) => `${dir}/ (${count})`);
3563
+ return `Workspace overview (generated, not a search result): top level: ${listing.join(", ") || "(empty)"}${top.length > 25 ? ", \u2026" : ""}. ${files}${seen > MAX_ENTRIES ? "+" : ""} files; most common types: ${types.join(", ") || "none"}. ` + (sources.length > 0 ? `Source folders: ${sources.join(", ")}. Paths are relative to the workspace root. ` : "") + "Search the file types that exist here, and search contents before concluding something is absent.";
3564
+ }
3565
+
3566
+ // src/research/structured-actions.ts
3567
+ import { existsSync as existsSync5, readdirSync as readdirSync4, readFileSync as readFileSync6, realpathSync as realpathSync2, statSync as statSync2 } from "node:fs";
3568
+ import path8 from "node:path";
3569
+ var SKIPPED_DIRS = ["node_modules", "target", "dist", "build", ".git", ".venv", "venv", "vendor", "__pycache__", ".octocode", "site-packages", "coverage"];
3570
+ var READ_DEFAULT_LINES = 120;
3571
+ var READ_MAX_LINES = 400;
3572
+ var LIST_MAX_ENTRIES = 300;
3573
+ var SEARCH_MAX_PER_FILE = 20;
3574
+ function confined(workspace, rel) {
3575
+ const raw = (rel ?? ".").trim() || ".";
3576
+ if (path8.isAbsolute(raw) && !raw.startsWith(workspace)) throw new ValidationError(`path is outside the workspace: ${raw}`);
3577
+ const absolute = path8.resolve(workspace, raw);
3578
+ const real = existsSync5(absolute) ? realpathSync2(absolute) : absolute;
3579
+ if (escapesRoot(real, workspace)) throw new ValidationError(`path is outside the workspace: ${raw}`);
3580
+ return path8.relative(workspace, real) || ".";
3581
+ }
3582
+ function validateSearch(value) {
3583
+ if (typeof value.pattern !== "string" || !value.pattern.trim()) throw new ValidationError("search action requires a non-empty string pattern");
3584
+ if (value.pattern.length > 300) throw new ValidationError("search pattern is longer than 300 characters");
3585
+ for (const key of ["path", "glob"]) {
3586
+ if (value[key] !== void 0 && typeof value[key] !== "string") throw new ValidationError(`search ${key} must be a string`);
3587
+ }
3588
+ for (const key of ["regex", "ignoreCase"]) {
3589
+ if (value[key] !== void 0 && typeof value[key] !== "boolean") throw new ValidationError(`search ${key} must be true or false`);
3590
+ }
3591
+ }
3592
+ function validateRead(value) {
3593
+ if (typeof value.path !== "string" || !value.path.trim()) throw new ValidationError("read action requires a file path");
3594
+ for (const key of ["start", "end"]) {
3595
+ const n = value[key];
3596
+ if (n !== void 0 && (typeof n !== "number" || !Number.isInteger(n) || n < 1)) throw new ValidationError(`read ${key} must be a positive integer line number`);
3597
+ }
3598
+ }
3599
+ function searchArgv(workspace, action, ripgrep) {
3600
+ const target = confined(workspace, action.path);
3601
+ if (ripgrep) {
3602
+ return [
3603
+ "rg",
3604
+ "-n",
3605
+ "--no-heading",
3606
+ "--color",
3607
+ "never",
3608
+ "--max-columns",
3609
+ "300",
3610
+ "--max-count",
3611
+ String(SEARCH_MAX_PER_FILE),
3612
+ ...action.regex ? [] : ["-F"],
3613
+ ...action.ignoreCase ? ["-i"] : [],
3614
+ ...action.glob ? ["-g", action.glob] : [],
3615
+ ...SKIPPED_DIRS.flatMap((dir) => ["-g", `!${dir}/`]),
3616
+ "-e",
3617
+ action.pattern,
3618
+ "--",
3619
+ target
3620
+ ];
3621
+ }
3622
+ return [
3623
+ "grep",
3624
+ "-rnI",
3625
+ ...action.regex ? ["-E"] : ["-F"],
3626
+ ...action.ignoreCase ? ["-i"] : [],
3627
+ ...action.glob ? [`--include=${action.glob}`] : [],
3628
+ ...SKIPPED_DIRS.map((dir) => `--exclude-dir=${dir}`),
3629
+ "-e",
3630
+ action.pattern,
3631
+ "--",
3632
+ target
3633
+ ];
3634
+ }
3635
+ function readLines(workspace, action) {
3636
+ const resolved = resolveNamedPath(workspace, action.path, "file");
3637
+ if (resolved.path === void 0) throw new ValidationError(`no such file: ${resolved.note}; search or list to find the right path`);
3638
+ const rel = resolved.path;
3639
+ const absolute = path8.join(workspace, rel);
3640
+ if (!statSync2(absolute).isFile()) throw new ValidationError(`${rel} is a directory; use list`);
3641
+ const bytes = readFileSync6(absolute);
3642
+ if (bytes.includes(0)) throw new ValidationError(`${rel} is binary`);
3643
+ const text = bytes.toString("utf8");
3644
+ const lines = (text.endsWith("\n") ? text.slice(0, -1) : text).split("\n");
3645
+ const start = Math.min(action.start ?? 1, Math.max(lines.length, 1));
3646
+ const end = Math.min(action.end ?? start + READ_DEFAULT_LINES - 1, start + READ_MAX_LINES - 1, lines.length);
3647
+ const width = String(end).length;
3648
+ const body = lines.slice(start - 1, end).map((line, index) => `${String(start + index).padStart(width)} ${line}`).join("\n");
3649
+ const more = end < lines.length ? `
3650
+ (${rel} has ${lines.length} lines; read start=${end + 1} for more)` : "";
3651
+ return `${resolved.note ? `${resolved.note}
3652
+ ` : ""}${rel}:${start}-${end}
3653
+ ${body}${more}`;
3654
+ }
3655
+ function listEntries(workspace, rel) {
3656
+ const resolved = resolveNamedPath(workspace, rel, "dir");
3657
+ if (resolved.path === void 0) throw new ValidationError(`not a directory: ${resolved.note}`);
3658
+ const base = resolved.path;
3659
+ const root = path8.join(workspace, base);
3660
+ if (!statSync2(root).isDirectory()) throw new ValidationError(`not a directory: ${base}; read it instead`);
3661
+ const out = [];
3662
+ const walk = (dir, depth) => {
3663
+ let entries;
3664
+ try {
3665
+ entries = readdirSync4(dir, { withFileTypes: true }).sort((left, right) => left.name.localeCompare(right.name));
3666
+ } catch {
3667
+ return;
3668
+ }
3669
+ for (const entry of entries) {
3670
+ if (out.length >= LIST_MAX_ENTRIES) return;
3671
+ if (entry.name.startsWith(".") || SKIPPED_DIRS.includes(entry.name)) continue;
3672
+ const shown = path8.relative(workspace, path8.join(dir, entry.name));
3673
+ out.push(entry.isDirectory() ? `${shown}/` : shown);
3674
+ if (entry.isDirectory() && depth < 1) walk(path8.join(dir, entry.name), depth + 1);
3675
+ }
3676
+ };
3677
+ walk(root, 0);
3678
+ const listing = out.length === 0 ? `(${base} is empty)` : out.join("\n") + (out.length >= LIST_MAX_ENTRIES ? "\n(listing truncated; list a subdirectory)" : "");
3679
+ return resolved.note ? `${resolved.note}
3680
+ ${listing}` : listing;
3681
+ }
3682
+ var INDEX_MAX_ENTRIES = 5e4;
3683
+ var indexes = /* @__PURE__ */ new Map();
3684
+ function workspaceIndex(workspace) {
3685
+ const cached = indexes.get(workspace);
3686
+ if (cached) return cached;
3687
+ const files = [];
3688
+ const dirs = [];
3689
+ const walk = (dir) => {
3690
+ let entries;
3691
+ try {
3692
+ entries = readdirSync4(dir, { withFileTypes: true });
3693
+ } catch {
3694
+ return;
3695
+ }
3696
+ for (const entry of entries) {
3697
+ if (files.length + dirs.length >= INDEX_MAX_ENTRIES) return;
3698
+ if (entry.name.startsWith(".") || SKIPPED_DIRS.includes(entry.name)) continue;
3699
+ const absolute = path8.join(dir, entry.name);
3700
+ const rel = path8.relative(workspace, absolute);
3701
+ if (entry.isDirectory()) {
3702
+ dirs.push(rel);
3703
+ walk(absolute);
3704
+ } else if (entry.isFile()) {
3705
+ files.push(rel);
3706
+ }
3707
+ }
3708
+ };
3709
+ walk(workspace);
3710
+ const index = { files, dirs };
3711
+ indexes.set(workspace, index);
3712
+ return index;
3713
+ }
3714
+ function pathCandidates(workspace, rel, kind = "any") {
3715
+ const wanted = rel.replace(/^\.\//, "").replace(/\/+$/, "");
3716
+ if (!wanted || wanted === ".") return [];
3717
+ const { files, dirs } = workspaceIndex(workspace);
3718
+ const pool = kind === "file" ? files : kind === "dir" ? dirs : [...files, ...dirs];
3719
+ const suffix = `/${wanted}`;
3720
+ return pool.filter((candidate) => candidate === wanted || candidate.endsWith(suffix)).sort((a, b) => a.length - b.length);
3721
+ }
3722
+ function resolveNamedPath(workspace, rel, kind) {
3723
+ const named = confined(workspace, rel);
3724
+ if (named === "." || existsSync5(path8.join(workspace, named))) return { path: named };
3725
+ const candidates = pathCandidates(workspace, named, kind);
3726
+ if (candidates.length === 1) return { path: candidates[0], note: `(${named} does not exist; using ${candidates[0]})` };
3727
+ if (candidates.length > 1) {
3728
+ return { path: void 0, note: `${named} does not exist; matching paths: ${candidates.slice(0, 8).join(", ")}${candidates.length > 8 ? ", \u2026" : ""}` };
3729
+ }
3730
+ return { path: void 0, note: `${named} does not exist and no path ends with it` };
3731
+ }
3732
+ function looksLikeRegex(pattern) {
3733
+ return /\||\.\*|\\[bswdBSWD]|\[[^\]]+\]|^\^|\$$|\(\?|\.\+/.test(pattern);
3734
+ }
3735
+
3736
+ // src/research/structured-call.ts
3737
+ async function structuredSearch(shell, action, timeoutSeconds) {
3738
+ const notes = [];
3739
+ const resolved = resolveNamedPath(shell.workspace, action.path, "any");
3740
+ let target = resolved.path;
3741
+ if (target === void 0) {
3742
+ notes.push(`(${resolved.note}; searched the whole workspace)`);
3743
+ target = ".";
3744
+ } else if (resolved.note) {
3745
+ notes.push(resolved.note);
3746
+ }
3747
+ const ripgrep = shell.hasTool("rg");
3748
+ const run = (regex) => shell.runTool(searchArgv(shell.workspace, { ...action, path: target, regex }, ripgrep), timeoutSeconds);
3749
+ let output = await run(action.regex);
3750
+ if (output === "(no matches)" && action.regex === void 0 && looksLikeRegex(action.pattern)) {
3751
+ try {
3752
+ const asRegex = await run(true);
3753
+ if (asRegex !== "(no matches)") {
3754
+ notes.push("(no literal match; searched as a regular expression)");
3755
+ output = asRegex;
3756
+ }
3757
+ } catch {
3758
+ }
3759
+ }
3760
+ return notes.length > 0 ? `${notes.join("\n")}
3761
+ ${output}` : output;
3762
+ }
3763
+ function structuredCall(shell, action, timeoutSeconds) {
3764
+ const name = String(action.action);
3765
+ const args = Object.fromEntries(Object.entries(action).filter(([key]) => key !== "action"));
3766
+ const run = async () => {
3767
+ if (name === "read") return readLines(shell.workspace, args);
3768
+ if (name === "list") return listEntries(shell.workspace, args.path);
3769
+ return structuredSearch(shell, args, timeoutSeconds);
3770
+ };
3771
+ return {
3772
+ signature: { [name]: args },
3773
+ run,
3774
+ record: { name, raw_name: name, arguments: args },
3775
+ ledger: (status) => ({ action: name, status, source: JSON.stringify(args) })
3776
+ };
3777
+ }
3778
+
3779
+ // src/research/entry.ts
3780
+ import path9 from "node:path";
3781
+ import { pathToFileURL } from "node:url";
3782
+ function isEntry(moduleUrl, name, script = process.argv[1]) {
3783
+ if (!script || moduleUrl !== pathToFileURL(script).href) return false;
3784
+ return path9.basename(script).replace(/\.[cm]?[jt]s$/, "") === name;
3785
+ }
3786
+
2370
3787
  // src/research/bash-agent.ts
2371
3788
  function parseAction(content) {
2372
3789
  const value = parseJsonAction(content);
@@ -2381,8 +3798,11 @@ function parseAction(content) {
2381
3798
  if (action === "finish" && typeof value.answer !== "string") {
2382
3799
  throw new ValidationError("finish action requires a string answer");
2383
3800
  }
2384
- if (action !== "shell" && action !== "page" && action !== "finish") {
2385
- throw new ValidationError(`unsupported action for bash adapter: ${String(action)}`);
3801
+ if (action === "search") validateSearch(value);
3802
+ if (action === "read") validateRead(value);
3803
+ if (action === "list" && value.path !== void 0 && typeof value.path !== "string") throw new ValidationError("list path must be a string");
3804
+ if (!["shell", "search", "read", "list", "page", "finish"].includes(String(action))) {
3805
+ throw new ValidationError(`unsupported action for bash adapter: ${String(action)} (use search, read, list, shell, page or finish)`);
2386
3806
  }
2387
3807
  return value;
2388
3808
  }
@@ -2398,12 +3818,16 @@ async function runShell(root, commandText, timeoutSeconds, shell) {
2398
3818
  var BASH_ADAPTER = {
2399
3819
  defaultToolTimeoutSeconds: 30,
2400
3820
  systemPrompt: () => BASH_SYSTEM_PROMPT,
3821
+ briefing: workspaceOverview,
2401
3822
  parseAction,
2402
3823
  open: (workspace) => {
2403
3824
  const shell = new ReadOnlyShell(workspace).enter();
2404
3825
  return {
2405
3826
  shell,
2406
3827
  toolCall: (action, _workspace, runtime) => {
3828
+ if (action.action === "search" || action.action === "read" || action.action === "list") {
3829
+ return structuredCall(shell, action, runtime.tool_timeout_seconds);
3830
+ }
2407
3831
  const commandText = String(action.command ?? "");
2408
3832
  return {
2409
3833
  signature: { command: commandText },
@@ -2424,7 +3848,7 @@ Commands remaining: ${count}. Finish now if the task is answerable; do not repea
2424
3848
  sandbox: session?.shell?.kind ?? "restricted-bash"
2425
3849
  })
2426
3850
  };
2427
- if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
3851
+ if (isEntry(import.meta.url, "bash-agent")) {
2428
3852
  process.exitCode = await runAdapter(BASH_ADAPTER);
2429
3853
  }
2430
3854
  export {