@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
@@ -0,0 +1,2923 @@
1
+ // src/research/agent-loop.ts
2
+ import { readFileSync as readFileSync5 } from "node:fs";
3
+ import { performance } from "node:perf_hooks";
4
+ import { setTimeout as sleep } from "node:timers/promises";
5
+
6
+ // src/research/canonical-json.ts
7
+ function canonicalJson(value, options = {}) {
8
+ const { sortKeys = true, ensureAscii = true, separators: [item, colon] = [", ", ": "], floatKeys } = options;
9
+ const quote = (text) => {
10
+ const json = JSON.stringify(text);
11
+ return ensureAscii ? json.replace(/[\u007f-\uffff]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`) : json;
12
+ };
13
+ const render = (node, float) => {
14
+ if (node === null) return "null";
15
+ if (typeof node === "string") return quote(node);
16
+ if (typeof node === "boolean") return String(node);
17
+ if (typeof node === "number") return numberText(node, float);
18
+ if (Array.isArray(node)) return `[${node.map((entry) => render(entry, false)).join(item)}]`;
19
+ if (typeof node === "object") {
20
+ const record = node;
21
+ const keys = Object.keys(record).filter((key) => record[key] !== void 0);
22
+ if (sortKeys) keys.sort();
23
+ return `{${keys.map((key) => `${quote(key)}${colon}${render(record[key], floatKeys?.has(key) ?? false)}`).join(item)}}`;
24
+ }
25
+ throw new TypeError(`cannot serialize a ${typeof node} as JSON`);
26
+ };
27
+ return render(value, false);
28
+ }
29
+ function numberText(value, float) {
30
+ if (!Number.isFinite(value)) throw new RangeError(`cannot serialize ${value} as JSON`);
31
+ if (Number.isInteger(value) && !float) return Math.abs(value) < 1e21 ? String(value) : BigInt(value).toString();
32
+ const [mantissa, power] = value.toExponential().split("e");
33
+ const exponent = Number(power);
34
+ if (value !== 0 && (exponent < -4 || exponent >= 16)) {
35
+ return `${mantissa}e${exponent < 0 ? "-" : "+"}${String(Math.abs(exponent)).padStart(2, "0")}`;
36
+ }
37
+ const text = String(value);
38
+ return Number.isInteger(value) ? `${text}.0` : text;
39
+ }
40
+ var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
41
+
42
+ // src/research/errors.ts
43
+ var ValidationError = class extends Error {
44
+ name = "ValidationError";
45
+ };
46
+ function isSystemError(error) {
47
+ return error instanceof Error && typeof error.errno === "number";
48
+ }
49
+ var ToolTimeoutError = class extends Error {
50
+ name = "ToolTimeoutError";
51
+ };
52
+ var ToolExitError = class extends Error {
53
+ constructor(exitCode, output, status = "error") {
54
+ super(`${status === "empty" ? "nothing found" : "command failed"} (exit code ${exitCode})` + (output ? `
55
+ ${output}` : ""));
56
+ this.exitCode = exitCode;
57
+ this.output = output;
58
+ this.status = status;
59
+ }
60
+ exitCode;
61
+ output;
62
+ status;
63
+ name = "ToolExitError";
64
+ };
65
+ function isToolError(error) {
66
+ return error instanceof ValidationError || error instanceof ToolTimeoutError || error instanceof ToolExitError || error instanceof SyntaxError || isSystemError(error);
67
+ }
68
+
69
+ // src/research/actions.ts
70
+ function objectAt(text, start) {
71
+ let depth = 0;
72
+ let inString = false;
73
+ for (let index = start; index < text.length; index += 1) {
74
+ const char = text[index];
75
+ if (inString) {
76
+ if (char === "\\") index += 1;
77
+ else if (char === '"') inString = false;
78
+ } else if (char === '"') {
79
+ inString = true;
80
+ } else if (char === "{" || char === "[") {
81
+ depth += 1;
82
+ } else if (char === "}" || char === "]") {
83
+ depth -= 1;
84
+ if (depth === 0) return JSON.parse(text.slice(start, index + 1));
85
+ }
86
+ }
87
+ throw new SyntaxError("unterminated JSON object");
88
+ }
89
+ function parseJsonAction(content) {
90
+ let text = content.trim();
91
+ if (text.startsWith("```")) text = text.split(/\r\n|\r|\n/).slice(1, -1).join("\n").trim();
92
+ let value;
93
+ try {
94
+ value = JSON.parse(text);
95
+ } catch (error) {
96
+ if (!(error instanceof SyntaxError)) throw error;
97
+ const start = text.indexOf("{");
98
+ if (start < 0) throw new ValidationError("model did not return a JSON action");
99
+ try {
100
+ value = objectAt(text, start);
101
+ } catch (inner) {
102
+ if (!(inner instanceof SyntaxError)) throw inner;
103
+ throw new ValidationError("model did not return a valid JSON action");
104
+ }
105
+ }
106
+ if (!isRecord(value) || typeof value.action !== "string") throw new ValidationError("model action must be a JSON object with an action");
107
+ return value;
108
+ }
109
+ function callSignature(payload) {
110
+ return canonicalJson(payload, { ensureAscii: false });
111
+ }
112
+ var REPEAT_NOTICE = "(identical call already made this run \u2014 its result is above and was not re-executed. Use a different call, or finish now with the evidence already collected.)";
113
+ var PARSE_REPAIR_NOTICE = "Your last reply was not a valid JSON action, so it was discarded. Reply with EXACTLY one JSON object and nothing else \u2014 no prose before or after, no markdown fence. Use the action shapes given in your instructions. If you already have enough evidence, send the finish action now.";
114
+
115
+ // src/research/context-manager.ts
116
+ import { createHash } from "node:crypto";
117
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
118
+ import path from "node:path";
119
+
120
+ // src/research/context-policy.ts
121
+ var CHARS_PER_TOKEN = 4;
122
+ var SAFETY_MARGIN_TOKENS = 256;
123
+ var IMAGE_TOKEN_ESTIMATE = 1024;
124
+ var DEFAULT_KEEP_RECENT = 2;
125
+ var COMPACT_PREVIEW_CHARS = 180;
126
+ var COMPACT_RETAIN_RATIO = 0.8;
127
+ var CLIP_HEAD_RATIO = 2 / 3;
128
+ var OBSERVATION_PAGE_CHARS = 3e3;
129
+ var COMPACTION_TARGET_RATIO = 0.9;
130
+ var MAX_PARSE_REPAIRS = 2;
131
+ var INTEGER = /^[+-]?[0-9]+$/;
132
+ var DECIMAL = /^[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/;
133
+ function envInt(env, name, defaultValue, minimum = 0) {
134
+ const raw = env[name];
135
+ if (raw === void 0 || raw === "") return defaultValue;
136
+ if (!INTEGER.test(raw.trim())) throw new ValidationError(`${name} must be an integer, got ${JSON.stringify(raw)}`);
137
+ const value = Number(raw.trim());
138
+ if (value < minimum) throw new ValidationError(`${name} must be >= ${minimum}, got ${value}`);
139
+ return value;
140
+ }
141
+ function envFloat(env, name, defaultValue, minimum = 0) {
142
+ const raw = env[name];
143
+ if (raw === void 0 || raw === "") return defaultValue;
144
+ const value = Number(raw.trim());
145
+ if (!DECIMAL.test(raw.trim())) throw new ValidationError(`${name} must be a number, got ${JSON.stringify(raw)}`);
146
+ if (!Number.isFinite(value) || value < minimum) {
147
+ throw new ValidationError(`${name} must be finite and >= ${minimum}, got ${JSON.stringify(raw)}`);
148
+ }
149
+ return value;
150
+ }
151
+ function envBool(env, name, defaultValue) {
152
+ const raw = env[name];
153
+ if (raw === void 0 || raw === "") return defaultValue;
154
+ const normalized = raw.trim().toLowerCase();
155
+ if (["1", "true", "yes", "on"].includes(normalized)) return true;
156
+ if (["0", "false", "no", "off"].includes(normalized)) return false;
157
+ throw new ValidationError(`${name} must be true/false, got ${JSON.stringify(raw)}`);
158
+ }
159
+ function contextBudget(numCtx, numPredict, safetyMarginTokens = SAFETY_MARGIN_TOKENS) {
160
+ return Math.max(numCtx - numPredict - safetyMarginTokens, 0);
161
+ }
162
+ var ContextPolicy = class _ContextPolicy {
163
+ fields;
164
+ /**
165
+ * Fields configured from the environment. They are floats, so canonical JSON (calibration keys)
166
+ * renders them as floats: `FREELLAMA_AGENT_CHARS_PER_TOKEN=8` gives `8.0`, the default gives `4`.
167
+ */
168
+ floatKeys;
169
+ constructor(fields = {}, floatKeys = []) {
170
+ this.fields = {
171
+ chars_per_token: fields.chars_per_token ?? CHARS_PER_TOKEN,
172
+ safety_margin_tokens: fields.safety_margin_tokens ?? SAFETY_MARGIN_TOKENS,
173
+ image_token_estimate: fields.image_token_estimate ?? IMAGE_TOKEN_ESTIMATE,
174
+ keep_recent: fields.keep_recent ?? DEFAULT_KEEP_RECENT,
175
+ compact_preview_chars: fields.compact_preview_chars ?? COMPACT_PREVIEW_CHARS,
176
+ compact_retain_ratio: fields.compact_retain_ratio ?? COMPACT_RETAIN_RATIO,
177
+ clip_head_ratio: fields.clip_head_ratio ?? CLIP_HEAD_RATIO,
178
+ observation_page_chars: fields.observation_page_chars ?? OBSERVATION_PAGE_CHARS,
179
+ pinned_overflow: fields.pinned_overflow ?? "error"
180
+ };
181
+ this.floatKeys = new Set(floatKeys);
182
+ const f = this.fields;
183
+ if (!Number.isFinite(f.chars_per_token) || f.chars_per_token <= 0) throw new ValidationError("chars_per_token must be finite and > 0");
184
+ if (f.safety_margin_tokens < 0 || f.image_token_estimate < 0 || f.keep_recent < 0) {
185
+ throw new ValidationError("context token estimates and keep_recent must be >= 0");
186
+ }
187
+ if (f.compact_preview_chars <= 0 || f.observation_page_chars <= 0) throw new ValidationError("preview and page sizes must be > 0");
188
+ if (!(f.compact_retain_ratio > 0 && f.compact_retain_ratio < 1)) throw new ValidationError("compact_retain_ratio must be between 0 and 1");
189
+ if (!(f.clip_head_ratio > 0 && f.clip_head_ratio < 1)) throw new ValidationError("clip_head_ratio must be between 0 and 1");
190
+ if (f.pinned_overflow !== "error" && f.pinned_overflow !== "clip") throw new ValidationError("pinned_overflow must be 'error' or 'clip'");
191
+ }
192
+ get chars_per_token() {
193
+ return this.fields.chars_per_token;
194
+ }
195
+ get safety_margin_tokens() {
196
+ return this.fields.safety_margin_tokens;
197
+ }
198
+ get image_token_estimate() {
199
+ return this.fields.image_token_estimate;
200
+ }
201
+ get keep_recent() {
202
+ return this.fields.keep_recent;
203
+ }
204
+ get compact_preview_chars() {
205
+ return this.fields.compact_preview_chars;
206
+ }
207
+ get compact_retain_ratio() {
208
+ return this.fields.compact_retain_ratio;
209
+ }
210
+ get clip_head_ratio() {
211
+ return this.fields.clip_head_ratio;
212
+ }
213
+ get observation_page_chars() {
214
+ return this.fields.observation_page_chars;
215
+ }
216
+ get pinned_overflow() {
217
+ return this.fields.pinned_overflow;
218
+ }
219
+ static fromEnv(env = process.env) {
220
+ const floats = [
221
+ ["chars_per_token", "FREELLAMA_AGENT_CHARS_PER_TOKEN"],
222
+ ["compact_retain_ratio", "FREELLAMA_AGENT_COMPACT_RETAIN_RATIO"],
223
+ ["clip_head_ratio", "FREELLAMA_AGENT_CLIP_HEAD_RATIO"]
224
+ ];
225
+ return new _ContextPolicy({
226
+ chars_per_token: envFloat(env, "FREELLAMA_AGENT_CHARS_PER_TOKEN", CHARS_PER_TOKEN, 1e-6),
227
+ safety_margin_tokens: envInt(env, "FREELLAMA_AGENT_SAFETY_MARGIN_TOKENS", SAFETY_MARGIN_TOKENS),
228
+ image_token_estimate: envInt(env, "FREELLAMA_AGENT_IMAGE_TOKEN_ESTIMATE", IMAGE_TOKEN_ESTIMATE),
229
+ keep_recent: envInt(env, "FREELLAMA_AGENT_KEEP_RECENT", DEFAULT_KEEP_RECENT),
230
+ compact_preview_chars: envInt(env, "FREELLAMA_AGENT_COMPACT_PREVIEW_CHARS", COMPACT_PREVIEW_CHARS, 1),
231
+ compact_retain_ratio: envFloat(env, "FREELLAMA_AGENT_COMPACT_RETAIN_RATIO", COMPACT_RETAIN_RATIO, 1e-6),
232
+ clip_head_ratio: envFloat(env, "FREELLAMA_AGENT_CLIP_HEAD_RATIO", CLIP_HEAD_RATIO, 1e-6),
233
+ observation_page_chars: envInt(env, "FREELLAMA_AGENT_OBSERVATION_PAGE_CHARS", OBSERVATION_PAGE_CHARS, 1),
234
+ pinned_overflow: (env.FREELLAMA_AGENT_PINNED_OVERFLOW ?? "error").trim().toLowerCase()
235
+ }, floats.filter(([, name]) => env[name] !== void 0 && env[name] !== "").map(([key]) => key));
236
+ }
237
+ metadata() {
238
+ return { ...this.fields };
239
+ }
240
+ };
241
+ var AgentRuntimeConfig = class _AgentRuntimeConfig {
242
+ constructor(fields) {
243
+ this.fields = fields;
244
+ }
245
+ fields;
246
+ get max_turns() {
247
+ return this.fields.max_turns;
248
+ }
249
+ get num_ctx() {
250
+ return this.fields.num_ctx;
251
+ }
252
+ get num_predict() {
253
+ return this.fields.num_predict;
254
+ }
255
+ get temperature() {
256
+ return this.fields.temperature;
257
+ }
258
+ get seed() {
259
+ return this.fields.seed;
260
+ }
261
+ get request_timeout_seconds() {
262
+ return this.fields.request_timeout_seconds;
263
+ }
264
+ get retry_attempts() {
265
+ return this.fields.retry_attempts;
266
+ }
267
+ get retry_backoff_seconds() {
268
+ return this.fields.retry_backoff_seconds;
269
+ }
270
+ get tool_timeout_seconds() {
271
+ return this.fields.tool_timeout_seconds;
272
+ }
273
+ get keep_alive() {
274
+ return this.fields.keep_alive;
275
+ }
276
+ get think() {
277
+ return this.fields.think;
278
+ }
279
+ get max_parse_repairs() {
280
+ return this.fields.max_parse_repairs;
281
+ }
282
+ get parse_repair_echo_chars() {
283
+ return this.fields.parse_repair_echo_chars;
284
+ }
285
+ get context() {
286
+ return this.fields.context;
287
+ }
288
+ static fromEnv(options) {
289
+ const env = options.env ?? process.env;
290
+ const context = ContextPolicy.fromEnv(env);
291
+ const config = new _AgentRuntimeConfig({
292
+ max_turns: envInt(env, "FREELLAMA_AGENT_MAX_TURNS", 10, 1),
293
+ num_ctx: envInt(env, "FREELLAMA_AGENT_NUM_CTX", 8192, 1),
294
+ num_predict: envInt(env, "FREELLAMA_AGENT_NUM_PREDICT", 512, 1),
295
+ temperature: envFloat(env, "FREELLAMA_AGENT_TEMPERATURE", 0),
296
+ seed: envInt(env, "FREELLAMA_AGENT_SEED", 42),
297
+ request_timeout_seconds: envFloat(env, "FREELLAMA_AGENT_REQUEST_TIMEOUT_SECONDS", 600, 1e-3),
298
+ retry_attempts: envInt(env, "FREELLAMA_AGENT_RETRY_ATTEMPTS", 2, 1),
299
+ retry_backoff_seconds: envFloat(env, "FREELLAMA_AGENT_RETRY_BACKOFF_SECONDS", 5),
300
+ tool_timeout_seconds: envFloat(env, "FREELLAMA_AGENT_TOOL_TIMEOUT_SECONDS", options.defaultToolTimeoutSeconds, 1e-3),
301
+ keep_alive: (env.FREELLAMA_AGENT_KEEP_ALIVE ?? "5m").trim(),
302
+ think: envBool(env, "FREELLAMA_AGENT_THINK", false),
303
+ max_parse_repairs: envInt(env, "FREELLAMA_AGENT_MAX_PARSE_REPAIRS", MAX_PARSE_REPAIRS, 0),
304
+ parse_repair_echo_chars: envInt(env, "FREELLAMA_AGENT_PARSE_REPAIR_ECHO_CHARS", 500, 1),
305
+ context
306
+ });
307
+ if (contextBudget(config.num_ctx, config.num_predict, context.safety_margin_tokens) <= 0) {
308
+ throw new ValidationError("FREELLAMA_AGENT_NUM_CTX must exceed FREELLAMA_AGENT_NUM_PREDICT plus FREELLAMA_AGENT_SAFETY_MARGIN_TOKENS");
309
+ }
310
+ if (!config.keep_alive) throw new ValidationError("FREELLAMA_AGENT_KEEP_ALIVE must not be empty");
311
+ return config;
312
+ }
313
+ metadata() {
314
+ const { context: _context, ...rest } = this.fields;
315
+ return { ...rest };
316
+ }
317
+ };
318
+
319
+ // src/research/text.ts
320
+ var SPACE_CLASS = "\\p{White_Space}\\x1c-\\x1f";
321
+ var SPACE = new RegExp(`^[${SPACE_CLASS}]$`, "u");
322
+ var EDGE_SPACE = new RegExp(`^[${SPACE_CLASS}]+|[${SPACE_CLASS}]+$`, "gu");
323
+ var isSpace = (char) => char !== void 0 && SPACE.test(char);
324
+ var trimSpace = (text) => text.replace(EDGE_SPACE, "");
325
+ var ASTRAL = /[\uD800-\uDBFF][\uDC00-\uDFFF]/;
326
+ function cpLength(text) {
327
+ if (!ASTRAL.test(text)) return text.length;
328
+ let count = 0;
329
+ for (const _ of text) count += 1;
330
+ return count;
331
+ }
332
+ function cpAdvance(text, start, count) {
333
+ let index = start;
334
+ for (let step = 0; step < count && index < text.length; step += 1) {
335
+ const code = text.charCodeAt(index);
336
+ const pair = code >= 55296 && code <= 56319 && index + 1 < text.length && text.charCodeAt(index + 1) >= 56320 && text.charCodeAt(index + 1) <= 57343;
337
+ index += pair ? 2 : 1;
338
+ }
339
+ return index;
340
+ }
341
+ function cpSlice(text, start, end) {
342
+ if (!ASTRAL.test(text)) return text.slice(start, end);
343
+ const from = cpAdvance(text, 0, start);
344
+ if (end === void 0) return text.slice(from);
345
+ if (end <= start) return "";
346
+ return text.slice(from, cpAdvance(text, from, end - start));
347
+ }
348
+ function cpTail(text, count) {
349
+ const length = cpLength(text);
350
+ return count >= length ? text : cpSlice(text, length - count);
351
+ }
352
+ var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
353
+ function decodeUtf8(bytes, source) {
354
+ try {
355
+ return UTF8.decode(bytes);
356
+ } catch {
357
+ throw new ValidationError(`${source} is not valid UTF-8`);
358
+ }
359
+ }
360
+ function normalizeNewlines(text) {
361
+ return text.replace(/\r\n?/g, "\n");
362
+ }
363
+
364
+ // src/research/fit.ts
365
+ var COMPACT_MARKER = "[earlier observation compacted to save context";
366
+ var estimateText = (value) => canonicalJson(value, { sortKeys: false, ensureAscii: false });
367
+ function estimateTokens(text, charsPerToken = CHARS_PER_TOKEN) {
368
+ return Math.floor(cpLength(text) / charsPerToken) + 1;
369
+ }
370
+ var UNMEASURED_BYTES_PER_TOKEN_MILLI = 3e3;
371
+ function platformBytesPerTokenMilli(learnedMilli) {
372
+ if (learnedMilli === null || learnedMilli === void 0) return UNMEASURED_BYTES_PER_TOKEN_MILLI;
373
+ return Math.min(Math.max(Math.floor(learnedMilli / 10) * 9, 1e3), 8e3);
374
+ }
375
+ function platformTextTokens(text, learnedMilli) {
376
+ return Math.ceil(Buffer.byteLength(text, "utf8") * 1e3 / platformBytesPerTokenMilli(learnedMilli));
377
+ }
378
+ function messagesTokens(messages, policy = new ContextPolicy(), learnedMilli = null) {
379
+ let total = 0;
380
+ for (const message of messages) {
381
+ const { images = [], ...text } = message;
382
+ if (Array.isArray(images)) total += images.length * policy.image_token_estimate;
383
+ if (learnedMilli !== null) {
384
+ total += platformTextTokens(JSON.stringify(text), learnedMilli) + 1;
385
+ continue;
386
+ }
387
+ const { content = "", ...metadata } = text;
388
+ total += estimateTokens(typeof content === "string" ? content : estimateText(content), policy.chars_per_token);
389
+ total += estimateTokens(estimateText(metadata), policy.chars_per_token);
390
+ }
391
+ return total;
392
+ }
393
+ var ContextBudgeter = class {
394
+ policy;
395
+ platformRatio;
396
+ scale;
397
+ calibration_samples;
398
+ constructor(policy = new ContextPolicy(), options = {}) {
399
+ this.policy = policy;
400
+ this.platformRatio = options.platformRatio ?? null;
401
+ this.scale = this.platformRatio ? 1 : Math.max(options.initialScale ?? 1, 1);
402
+ this.calibration_samples = this.platformRatio ? 0 : Math.max(Math.trunc(options.calibrationSamples ?? 0), 0);
403
+ }
404
+ estimate = (messages) => this.platformRatio ? messagesTokens(messages, this.policy, this.platformRatio.bytes_per_token_milli) : Math.ceil(messagesTokens(messages, this.policy) * this.scale);
405
+ /** Calibrate the fallback estimate; returns whether the multiplier rose (and should persist). */
406
+ observe(messages, promptEvalCount) {
407
+ if (this.platformRatio) return false;
408
+ if (typeof promptEvalCount !== "number" || !Number.isFinite(promptEvalCount)) return false;
409
+ const actual = Math.trunc(promptEvalCount);
410
+ if (actual <= 0) return false;
411
+ const baseline = Math.max(messagesTokens(messages, this.policy), 1);
412
+ const previous = this.scale;
413
+ this.scale = Math.max(this.scale, actual / baseline);
414
+ this.calibration_samples += 1;
415
+ return this.scale > previous;
416
+ }
417
+ metadata() {
418
+ const ratio = this.platformRatio;
419
+ return {
420
+ token_counting: ratio ? "platform_learned_ratio" : "local_chars_per_token",
421
+ estimate_scale: Math.round(this.scale * 1e6) / 1e6,
422
+ calibration_samples: this.calibration_samples,
423
+ bytes_per_token_milli: ratio?.bytes_per_token_milli ?? null,
424
+ bytes_per_token_milli_with_margin: ratio ? platformBytesPerTokenMilli(ratio.bytes_per_token_milli) : null,
425
+ bytes_per_token_samples: ratio?.bytes_per_token_samples ?? null
426
+ };
427
+ }
428
+ };
429
+ function clip(text, limit, headRatio = CLIP_HEAD_RATIO) {
430
+ if (!(headRatio > 0 && headRatio < 1)) throw new ValidationError("head_ratio must be between 0 and 1");
431
+ const length = cpLength(text);
432
+ if (limit <= 0 || length <= limit) return text;
433
+ const marker = (dropped2) => `
434
+ \u2026 [${dropped2} characters elided] \u2026
435
+ `;
436
+ const usable = Math.max(limit - cpLength(marker(length)), 0);
437
+ if (usable === 0) return cpSlice(text, 0, limit);
438
+ const headChars = Math.trunc(usable * headRatio);
439
+ const tailChars = usable - headChars;
440
+ const dropped = length - headChars - tailChars;
441
+ const tail = tailChars ? cpTail(text, tailChars) : "";
442
+ return cpSlice(text, 0, headChars) + marker(dropped) + tail;
443
+ }
444
+ var STEP_HEADER = /^Observation \(step ([0-9]+)\):\n/;
445
+ var LOOP_REMINDER = /\n\n(?:Commands|Tool calls) remaining: [0-9]+\. Finish now if the task is answerable; do not repeat prior (?:commands|calls)\.(?=\n?$)/g;
446
+ var PAGE_FOOTER = /\n\n\[page ([0-9]+) of ([0-9]+) — [0-9]+ characters total, nothing discarded\. /;
447
+ var decimal = (digits) => BigInt(digits).toString();
448
+ function compact(content, policy) {
449
+ if (content.startsWith(COMPACT_MARKER)) return content;
450
+ const stepMatch = STEP_HEADER.exec(content);
451
+ let recovery = "";
452
+ let body = content;
453
+ if (stepMatch) {
454
+ const step = decimal(stepMatch[1]);
455
+ body = content.slice(stepMatch[0].length);
456
+ const newline = body.indexOf("\n");
457
+ const ledgerLine = newline < 0 ? body : body.slice(0, newline);
458
+ const ledger = ledgerLine.startsWith("[evidence ledger: ") && ledgerLine.endsWith("]") ? ledgerLine : "";
459
+ if (ledger && newline >= 0) body = body.slice(newline + 1);
460
+ body = body.replace(LOOP_REMINDER, "");
461
+ const footer = PAGE_FOOTER.exec(body);
462
+ const page = footer ? decimal(footer[1]) : "1";
463
+ const total = footer ? decimal(footer[2]) : "1";
464
+ if (footer) body = body.slice(0, footer.index);
465
+ recovery = `; step ${step}, page ${page} of ${total}; retrieve stored output: {"action":"page","step":${step},"page":${page}}`;
466
+ if (ledger) recovery += `; ${ledger}`;
467
+ }
468
+ const preview = clip(body, policy.compact_preview_chars, policy.clip_head_ratio);
469
+ return `${COMPACT_MARKER}${recovery}] ${preview}`;
470
+ }
471
+ var textContent = (message) => typeof message?.content === "string" ? message.content : null;
472
+ var contentLength = (message) => cpLength(textContent(message) ?? "");
473
+ function widestIndex(indexes, messages) {
474
+ let best;
475
+ let bestLength = -1;
476
+ for (const index of indexes) {
477
+ const length = contentLength(messages[index]);
478
+ if (length > bestLength) {
479
+ best = index;
480
+ bestLength = length;
481
+ }
482
+ }
483
+ return best;
484
+ }
485
+ function textCandidates(messages, from, to, minimum) {
486
+ const result = [];
487
+ for (let index = from; index < to; index += 1) {
488
+ if (textContent(messages[index]) !== null && contentLength(messages[index]) > minimum) result.push(index);
489
+ }
490
+ return result;
491
+ }
492
+ function fitToContext(messages, options) {
493
+ const policy = options.policy ?? new ContextPolicy();
494
+ const keepRecent = options.keepRecent ?? policy.keep_recent;
495
+ const budget = contextBudget(options.numCtx, options.numPredict, policy.safety_margin_tokens);
496
+ const count = options.tokenCounter ?? ((value) => messagesTokens(value, policy));
497
+ if (count(messages) <= budget) return [messages, false];
498
+ const pinned = Math.min(2, messages.length);
499
+ const trimmed = messages.map((message) => ({ ...message }));
500
+ const observations = [];
501
+ for (let index = pinned; index < trimmed.length; index += 1) {
502
+ const role = trimmed[index].role;
503
+ if ((role === "user" || role === "tool") && textContent(trimmed[index]) !== null) observations.push(index);
504
+ }
505
+ const protectedIndexes = new Set(keepRecent ? observations.slice(-keepRecent) : []);
506
+ let compacted = false;
507
+ const target = Math.floor(budget * COMPACTION_TARGET_RATIO);
508
+ for (const index of observations) {
509
+ if (count(trimmed) <= target) break;
510
+ if (protectedIndexes.has(index)) continue;
511
+ const content = textContent(trimmed[index]);
512
+ const replacement = compact(content, policy);
513
+ if (cpLength(replacement) < cpLength(content)) {
514
+ trimmed[index].content = replacement;
515
+ compacted = true;
516
+ }
517
+ }
518
+ const shrinkWidest = (candidates) => {
519
+ const widest = widestIndex(candidates, trimmed);
520
+ if (widest === void 0) return false;
521
+ const content = textContent(trimmed[widest]) ?? "";
522
+ const keep = Math.max(Math.trunc(cpLength(content) * policy.compact_retain_ratio), policy.compact_preview_chars);
523
+ if (keep >= cpLength(content)) return false;
524
+ trimmed[widest].content = clip(content, keep, policy.clip_head_ratio);
525
+ compacted = true;
526
+ return true;
527
+ };
528
+ for (let guard = 0; count(trimmed) > budget && guard < 200; guard += 1) {
529
+ if (!shrinkWidest(observations)) break;
530
+ }
531
+ for (const message of trimmed.slice(pinned, Math.max(trimmed.length - 2, pinned))) {
532
+ const thinking = message.thinking;
533
+ if (count(trimmed) <= budget) break;
534
+ if (typeof thinking === "string" && cpLength(thinking) > policy.compact_preview_chars) {
535
+ message.thinking = compact(thinking, policy);
536
+ compacted = true;
537
+ }
538
+ }
539
+ for (let guard = 0; count(trimmed) > budget && guard < 200; guard += 1) {
540
+ const candidates = textCandidates(trimmed, pinned, trimmed.length, policy.compact_preview_chars);
541
+ if (candidates.length === 0 || !shrinkWidest(candidates)) break;
542
+ }
543
+ for (let guard = 0; policy.pinned_overflow === "clip" && count(trimmed) > budget && guard < 200; guard += 1) {
544
+ const candidates = textCandidates(trimmed, 0, pinned, policy.compact_preview_chars);
545
+ if (candidates.length === 0 || !shrinkWidest(candidates)) break;
546
+ }
547
+ if (count(trimmed) > budget) {
548
+ throw new ValidationError(
549
+ `pinned system/question content and minimum history exceed context budget: ${count(trimmed)} > ${budget} estimated tokens. Increase FREELLAMA_AGENT_NUM_CTX, reduce the task/instructions, or explicitly set FREELLAMA_AGENT_PINNED_OVERFLOW=clip.`
550
+ );
551
+ }
552
+ return [trimmed, compacted];
553
+ }
554
+
555
+ // src/research/context-manager.ts
556
+ function timeNs() {
557
+ const subMillis = process.hrtime.bigint() % 1000000n;
558
+ return (BigInt(Date.now()) * 1000000n + subMillis).toString();
559
+ }
560
+ var AgentContextManager = class {
561
+ runtime;
562
+ model;
563
+ calibrationDir;
564
+ calibrationIdentity;
565
+ budgeter;
566
+ calibrationSource;
567
+ compactions = 0;
568
+ constructor(runtime, options = {}) {
569
+ this.runtime = runtime;
570
+ this.model = options.model ?? "";
571
+ const platformRatio = options.platformRatio ?? null;
572
+ this.calibrationDir = platformRatio ? null : options.calibrationDir ?? null;
573
+ this.calibrationIdentity = options.calibrationIdentity ?? "";
574
+ const loaded = this.loadCalibration();
575
+ this.budgeter = new ContextBudgeter(runtime.context, {
576
+ initialScale: loaded?.scale ?? 1,
577
+ calibrationSamples: loaded?.samples ?? 0,
578
+ platformRatio
579
+ });
580
+ this.calibrationSource = platformRatio ? "not_applied" : loaded ? "persistent_model_cache" : "current_process";
581
+ }
582
+ modelKey() {
583
+ return createHash("sha256").update(this.calibrationContractJson(), "utf8").digest("hex").slice(0, 24);
584
+ }
585
+ calibrationContract() {
586
+ return {
587
+ schema_version: 2,
588
+ model: this.model,
589
+ identity: this.calibrationIdentity,
590
+ estimator_version: 1,
591
+ chars_per_token: this.runtime.context.chars_per_token,
592
+ image_token_estimate: this.runtime.context.image_token_estimate
593
+ };
594
+ }
595
+ /** The canonical JSON the model key hashes. */
596
+ calibrationContractJson() {
597
+ return canonicalJson(this.calibrationContract(), { floatKeys: this.runtime.context.floatKeys });
598
+ }
599
+ /** One persisted calibration record: the contract plus the float `scale` and integer `samples`. */
600
+ calibrationRecordJson(scale, samples) {
601
+ const floatKeys = /* @__PURE__ */ new Set([...this.runtime.context.floatKeys, "scale"]);
602
+ return `${canonicalJson({ ...this.calibrationContract(), scale, samples }, { floatKeys })}
603
+ `;
604
+ }
605
+ /** Record files `<key>-*.json`; the key is hexadecimal, so only the literal affixes matter. */
606
+ records(directory, key) {
607
+ const prefix = `${key}-`;
608
+ return readdirSync(directory).filter((name) => name.startsWith(prefix) && name.endsWith(".json") && name.length >= prefix.length + 5);
609
+ }
610
+ loadCalibration() {
611
+ if (!this.model || this.calibrationDir === null || !existsSync(this.calibrationDir)) return null;
612
+ let bestScale = 1;
613
+ let samples = 0;
614
+ const contract = this.calibrationContract();
615
+ for (const name of this.records(this.calibrationDir, this.modelKey())) {
616
+ let record;
617
+ try {
618
+ record = JSON.parse(readFileSync(path.join(this.calibrationDir, name), "utf8"));
619
+ } catch {
620
+ continue;
621
+ }
622
+ if (!isRecord(record)) continue;
623
+ if (Object.entries(contract).some(([key, value]) => record[key] !== value)) continue;
624
+ const scale = record.scale ?? 1;
625
+ const recordSamples = record.samples ?? 0;
626
+ if (typeof scale !== "number" || typeof recordSamples !== "number") continue;
627
+ if (!Number.isFinite(scale) || scale < 1 || !(Math.trunc(recordSamples) > 0)) continue;
628
+ bestScale = Math.max(bestScale, scale);
629
+ samples = Math.max(samples, Math.trunc(recordSamples));
630
+ }
631
+ return samples ? { scale: bestScale, samples } : null;
632
+ }
633
+ persistCalibration() {
634
+ if (!this.model || this.calibrationDir === null) return;
635
+ mkdirSync(this.calibrationDir, { recursive: true });
636
+ const key = this.modelKey();
637
+ const stem = `${key}-${timeNs()}-${process.pid}`;
638
+ const temporary = path.join(this.calibrationDir, `${stem}.tmp`);
639
+ writeFileSync(temporary, this.calibrationRecordJson(this.budgeter.scale, this.budgeter.calibration_samples), "utf8");
640
+ renameSync(temporary, path.join(this.calibrationDir, `${stem}.json`));
641
+ const records = this.records(this.calibrationDir, key).sort();
642
+ for (const stale of records.slice(0, Math.max(records.length - 32, 0))) {
643
+ try {
644
+ unlinkSync(path.join(this.calibrationDir, stale));
645
+ } catch (error) {
646
+ if (error.code !== "ENOENT") throw error;
647
+ }
648
+ }
649
+ }
650
+ fit(messages) {
651
+ const [fitted, compacted] = fitToContext(messages, {
652
+ numCtx: this.runtime.num_ctx,
653
+ numPredict: this.runtime.num_predict,
654
+ policy: this.runtime.context,
655
+ tokenCounter: this.budgeter.estimate
656
+ });
657
+ this.compactions += compacted ? 1 : 0;
658
+ return fitted;
659
+ }
660
+ observe(messages, promptEvalCount) {
661
+ if (this.budgeter.observe(messages, promptEvalCount)) this.persistCalibration();
662
+ }
663
+ metadata() {
664
+ return {
665
+ ...this.runtime.context.metadata(),
666
+ ...this.budgeter.metadata(),
667
+ calibration_source: this.calibrationSource,
668
+ calibration_scope: this.calibrationIdentity ? "provided_identity_and_estimator" : "model_tag_and_estimator",
669
+ compaction_target_ratio: COMPACTION_TARGET_RATIO,
670
+ estimated_input_budget: contextBudget(this.runtime.num_ctx, this.runtime.num_predict, this.runtime.context.safety_margin_tokens),
671
+ compactions: this.compactions
672
+ };
673
+ }
674
+ };
675
+ function writeFailureResult(resultPath, answer) {
676
+ mkdirSync(path.dirname(resultPath), { recursive: true });
677
+ writeFileSync(resultPath, `${JSON.stringify({ final_answer: answer, tool_calls: [], usage: {} }, null, 2)}
678
+ `, "utf8");
679
+ }
680
+
681
+ // src/research/observations.ts
682
+ import { randomBytes } from "node:crypto";
683
+ import { closeSync, existsSync as existsSync2, mkdirSync as mkdirSync2, mkdtempSync, openSync, readFileSync as readFileSync2, readSync, renameSync as renameSync2, rmSync, unlinkSync as unlinkSync2, writeSync } from "node:fs";
684
+ import { tmpdir } from "node:os";
685
+ import path2 from "node:path";
686
+ function pageFooter(step, page, total, totalChars) {
687
+ if (total <= 1) return "";
688
+ const head = `
689
+
690
+ [page ${page} of ${total} \u2014 ${totalChars} characters total, nothing discarded. `;
691
+ if (page >= total) return `${head}End of stored output.]`;
692
+ return `${head}For the next page send exactly: {"action":"page","step":${step},"page":${page + 1}} \u2014 that re-reads stored output and does NOT re-run the command.]`;
693
+ }
694
+ function encodeStrict(text) {
695
+ if (/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(text)) {
696
+ throw new ValidationError("observation contains a lone surrogate and cannot be encoded as UTF-8");
697
+ }
698
+ return Buffer.from(text, "utf8");
699
+ }
700
+ function writeAll(fd, data) {
701
+ let offset = 0;
702
+ while (offset < data.length) offset += writeSync(fd, data, offset, data.length - offset);
703
+ }
704
+ function readAt(file, position, length) {
705
+ const fd = openSync(file, "r");
706
+ try {
707
+ const buffer = Buffer.alloc(length);
708
+ let read = 0;
709
+ while (read < length) {
710
+ const count = readSync(fd, buffer, read, length - read, position + read);
711
+ if (count === 0) break;
712
+ read += count;
713
+ }
714
+ return buffer.subarray(0, read);
715
+ } finally {
716
+ closeSync(fd);
717
+ }
718
+ }
719
+ var utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
720
+ var ObservationStore = class {
721
+ directory;
722
+ pageSize;
723
+ byStep = /* @__PURE__ */ new Map();
724
+ constructor(pageSize = OBSERVATION_PAGE_CHARS) {
725
+ if (pageSize <= 0) throw new ValidationError("observation page size must be > 0");
726
+ this.directory = mkdtempSync(path2.join(path2.resolve(tmpdir()), "freellama-observations-"));
727
+ this.pageSize = pageSize;
728
+ }
729
+ close() {
730
+ this.byStep.clear();
731
+ rmSync(this.directory, { recursive: true, force: true });
732
+ }
733
+ put(step, text, metadata = {}) {
734
+ if (step < 1) throw new ValidationError("observation step must be positive");
735
+ const file = path2.join(this.directory, `${step}.utf8`);
736
+ const indexPath = path2.join(this.directory, `${step}.index`);
737
+ let start = 0;
738
+ let offset = 0;
739
+ let pages = 0;
740
+ let characters = 0;
741
+ const output = openSync(file, "w");
742
+ const index = openSync(indexPath, "w");
743
+ try {
744
+ while (start < text.length) {
745
+ let end = cpAdvance(text, start, this.pageSize);
746
+ if (end < text.length) {
747
+ const newline = text.lastIndexOf("\n", end - 1);
748
+ if (newline >= start) end = newline + 1;
749
+ }
750
+ const slice = text.slice(start, end);
751
+ characters += cpLength(slice);
752
+ const encoded = encodeStrict(slice);
753
+ writeAll(output, encoded);
754
+ const nextOffset = offset + encoded.length;
755
+ const record = Buffer.alloc(16);
756
+ record.writeBigUInt64BE(BigInt(offset), 0);
757
+ record.writeBigUInt64BE(BigInt(nextOffset), 8);
758
+ writeAll(index, record);
759
+ offset = nextOffset;
760
+ pages += 1;
761
+ start = end;
762
+ }
763
+ if (!pages) {
764
+ writeAll(index, Buffer.alloc(16));
765
+ pages = 1;
766
+ }
767
+ } finally {
768
+ closeSync(output);
769
+ closeSync(index);
770
+ }
771
+ const bounded = {};
772
+ for (const [key, value] of Object.entries(metadata)) {
773
+ if (key === "action" || key === "status" || key === "source") bounded[key] = clip(String(value), 240);
774
+ }
775
+ this.byStep.set(step, { path: file, indexPath, characters, pages, metadata: bounded });
776
+ }
777
+ /** Explicit full materialization for consumers that need it; adapters use view/writeResult. */
778
+ get(step) {
779
+ const record = this.byStep.get(step);
780
+ return record ? utf8.decode(readFileSync2(record.path)) : null;
781
+ }
782
+ pageCount(step) {
783
+ const record = this.byStep.get(step);
784
+ if (!record) throw new Error(`no stored observation for step ${step}`);
785
+ return record.pages;
786
+ }
787
+ /** Return `[body, footer]` for one page of a stored observation. */
788
+ view(step, page = 1) {
789
+ const record = this.byStep.get(step);
790
+ if (!record) {
791
+ const known = [...this.byStep.keys()].sort((a, b) => a - b).join(", ") || "none yet";
792
+ return [`no stored output for step ${step} (known steps: ${known})`, ""];
793
+ }
794
+ const chosen = Math.max(1, Math.min(page, record.pages));
795
+ const entry = readAt(record.indexPath, (chosen - 1) * 16, 16);
796
+ const start = Number(entry.readBigUInt64BE(0));
797
+ const end = Number(entry.readBigUInt64BE(8));
798
+ const chunk = utf8.decode(readAt(record.path, start, end - start));
799
+ return [chunk, pageFooter(step, chosen, record.pages, record.characters)];
800
+ }
801
+ message(step, page = 1) {
802
+ const [body, footer] = this.view(step, page);
803
+ const record = this.byStep.get(step);
804
+ if (!record) return `Observation (step ${step}):
805
+ ${body}`;
806
+ const chosen = Math.max(1, Math.min(page, record.pages));
807
+ const ledger = { ...record.metadata, step, page: chosen, total_pages: record.pages, trust: "untrusted_tool_output" };
808
+ return `Observation (step ${step}):
809
+ [evidence ledger: ${JSON.stringify(ledger)}]
810
+ ${body}${footer}`;
811
+ }
812
+ /** Decode a stored UTF-8 file in 64 KiB reads, never splitting a code point across chunks. */
813
+ streamStored(file, consume) {
814
+ const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
815
+ const fd = openSync(file, "r");
816
+ try {
817
+ const buffer = Buffer.alloc(65536);
818
+ for (; ; ) {
819
+ const count = readSync(fd, buffer, 0, buffer.length, null);
820
+ if (count === 0) break;
821
+ const chunk = decoder.decode(buffer.subarray(0, count), { stream: true });
822
+ if (chunk) consume(chunk);
823
+ }
824
+ const rest = decoder.decode();
825
+ if (rest) consume(rest);
826
+ } finally {
827
+ closeSync(fd);
828
+ }
829
+ }
830
+ /** Atomically write the public JSON result, restoring full call results from disk. */
831
+ writeResult(resultPath, result) {
832
+ const directory = path2.dirname(resultPath);
833
+ mkdirSync2(directory, { recursive: true });
834
+ const temporary = path2.join(directory, `.${path2.basename(resultPath)}.${randomBytes(6).toString("hex")}.tmp`);
835
+ let fd = null;
836
+ try {
837
+ fd = openSync(temporary, "wx", 384);
838
+ const out = fd;
839
+ const emit = (text) => writeAll(out, Buffer.from(text, "utf8"));
840
+ emit("{");
841
+ Object.entries(result).forEach(([key, value], fieldIndex) => {
842
+ if (fieldIndex) emit(",");
843
+ emit(`${JSON.stringify(key)}:`);
844
+ if (key !== "tool_calls") {
845
+ emit(JSON.stringify(value) ?? "null");
846
+ return;
847
+ }
848
+ emit("[");
849
+ value.forEach((call, callIndex) => {
850
+ const step = callIndex + 1;
851
+ if (step > 1) emit(",");
852
+ const metadata = {};
853
+ for (const [name, item] of Object.entries(call)) if (name !== "result") metadata[name] = item;
854
+ const encoded = JSON.stringify(metadata);
855
+ emit(encoded.slice(0, -1) + (Object.keys(metadata).length ? "," : "") + '"result":"');
856
+ const record = this.byStep.get(step);
857
+ if (!record) throw new Error(`no stored observation for step ${step}`);
858
+ this.streamStored(record.path, (chunk) => emit(JSON.stringify(chunk).slice(1, -1)));
859
+ emit('"}');
860
+ });
861
+ emit("]");
862
+ });
863
+ emit("}\n");
864
+ closeSync(fd);
865
+ fd = null;
866
+ renameSync2(temporary, resultPath);
867
+ } finally {
868
+ if (fd !== null) closeSync(fd);
869
+ if (existsSync2(temporary)) unlinkSync2(temporary);
870
+ }
871
+ }
872
+ };
873
+
874
+ // src/research/progress-events.ts
875
+ var PROGRESS_ENV = "FREELLAMA_AGENT_PROGRESS";
876
+ var PROGRESS_PREFIX = "@freellama-progress ";
877
+ function progressEmitter(env, write = (line) => {
878
+ process.stderr.write(line);
879
+ }) {
880
+ if (env[PROGRESS_ENV] !== "stderr") return void 0;
881
+ return (event) => {
882
+ try {
883
+ write(`${PROGRESS_PREFIX}${JSON.stringify(event)}
884
+ `);
885
+ } catch {
886
+ }
887
+ };
888
+ }
889
+
890
+ // src/research/transport.ts
891
+ import { createHash as createHash2 } from "node:crypto";
892
+ import { readFileSync as readFileSync4 } from "node:fs";
893
+ import { homedir } from "node:os";
894
+ import path3 from "node:path";
895
+
896
+ // src/research/transport-http.ts
897
+ import { readFileSync as readFileSync3 } from "node:fs";
898
+ import http from "node:http";
899
+ import https from "node:https";
900
+ var RECEIPT_BODY_LIMIT = 64 * 1024;
901
+ var HTTPError = class extends Error {
902
+ constructor(code, reason, body = Buffer.alloc(0)) {
903
+ super(`HTTP Error ${code}: ${reason}`);
904
+ this.code = code;
905
+ this.reason = reason;
906
+ this.body = body;
907
+ }
908
+ code;
909
+ reason;
910
+ body;
911
+ name = "HTTPError";
912
+ };
913
+ var URLError = class extends Error {
914
+ constructor(reason) {
915
+ super(`request not sent: ${reason.message}`);
916
+ this.reason = reason;
917
+ }
918
+ reason;
919
+ name = "URLError";
920
+ };
921
+ var RequestTimeoutError = class extends Error {
922
+ name = "RequestTimeoutError";
923
+ constructor(message = "timed out") {
924
+ super(message);
925
+ }
926
+ };
927
+ function requestHeaders(env = process.env) {
928
+ const headers = { "content-type": "application/json" };
929
+ const tokenFile = trimSpace(env.FREELLAMA_AUTH_TOKEN_FILE ?? "");
930
+ if (!tokenFile) return headers;
931
+ const token = trimSpace(decodeUtf8(readFileSync3(tokenFile), "FREELLAMA_AUTH_TOKEN_FILE"));
932
+ if (cpLength(token) < 32 || [...token].some((character) => isSpace(character))) {
933
+ throw new ValidationError("FREELLAMA_AUTH_TOKEN_FILE must contain one token of at least 32 bytes");
934
+ }
935
+ headers.authorization = `Bearer ${token}`;
936
+ return headers;
937
+ }
938
+ function httpRequest(url, options) {
939
+ return new Promise((resolve, reject) => {
940
+ let parsed;
941
+ try {
942
+ parsed = new URL(url);
943
+ } catch {
944
+ reject(new ValidationError(`unknown url type: ${JSON.stringify(url)}`));
945
+ return;
946
+ }
947
+ const client = parsed.protocol === "https:" ? https : parsed.protocol === "http:" ? http : null;
948
+ if (client === null) {
949
+ reject(new URLError(new Error(`unknown url type: ${parsed.protocol.replace(/:$/, "")}`)));
950
+ return;
951
+ }
952
+ const headers = { ...options.headers, connection: "close" };
953
+ if (options.body) headers["content-length"] = String(options.body.length);
954
+ let sent = false;
955
+ let settled = false;
956
+ const { signal } = options;
957
+ const onAbort = () => fail(signal?.reason instanceof Error ? signal.reason : new Error("request aborted"));
958
+ const fail = (error) => {
959
+ if (settled) return;
960
+ settled = true;
961
+ signal?.removeEventListener("abort", onAbort);
962
+ request.destroy();
963
+ reject(error);
964
+ };
965
+ if (signal?.aborted) {
966
+ reject(signal.reason instanceof Error ? signal.reason : new Error("request aborted"));
967
+ return;
968
+ }
969
+ const request = client.request(parsed, { method: options.method, headers });
970
+ signal?.addEventListener("abort", onAbort, { once: true });
971
+ request.setTimeout(Math.max(options.timeoutSeconds * 1e3, 1), () => {
972
+ fail(sent ? new RequestTimeoutError() : new URLError(new RequestTimeoutError()));
973
+ });
974
+ request.on("finish", () => {
975
+ sent = true;
976
+ });
977
+ request.on("error", (error) => fail(sent ? error : new URLError(error)));
978
+ request.on("response", (response) => {
979
+ const chunks = [];
980
+ let size = 0;
981
+ const status = response.statusCode ?? 0;
982
+ const ok = status >= 200 && status < 300;
983
+ const limit = ok ? options.limit : RECEIPT_BODY_LIMIT + 1;
984
+ const finish = () => {
985
+ if (settled) return;
986
+ settled = true;
987
+ signal?.removeEventListener("abort", onAbort);
988
+ const body = Buffer.concat(chunks);
989
+ request.destroy();
990
+ if (ok) resolve(body);
991
+ else reject(new HTTPError(status, response.statusMessage ?? "", body));
992
+ };
993
+ response.on("data", (chunk) => {
994
+ if (limit !== void 0 && size >= limit) return;
995
+ const piece = limit !== void 0 ? chunk.subarray(0, limit - size) : chunk;
996
+ chunks.push(piece);
997
+ size += piece.length;
998
+ if (limit !== void 0 && size >= limit) finish();
999
+ });
1000
+ response.on("error", (error) => fail(error));
1001
+ response.on("end", finish);
1002
+ });
1003
+ request.end(options.body);
1004
+ });
1005
+ }
1006
+ function parseJsonBody(body, source) {
1007
+ return JSON.parse(decodeUtf8(body, source));
1008
+ }
1009
+ async function requestJson(url, payload, timeoutSeconds) {
1010
+ const body = Buffer.from(JSON.stringify(payload), "utf8");
1011
+ const response = await httpRequest(url, { method: "POST", headers: requestHeaders(), body, timeoutSeconds });
1012
+ return parseJsonBody(response, "chat response");
1013
+ }
1014
+
1015
+ // src/research/transport.ts
1016
+ var MANAGED_SUFFIX = "/_freellama/v1/tasks";
1017
+ var IDENTITY_LIMIT = 1024 * 1024;
1018
+ var trimSlashes = (text) => text.replace(/\/+$/, "");
1019
+ async function readCatalogJson(url, request = httpRequest) {
1020
+ const payload = await request(url, { method: "GET", headers: requestHeaders(), timeoutSeconds: 5, limit: IDENTITY_LIMIT + 1 });
1021
+ if (payload.length > IDENTITY_LIMIT) throw new ValidationError("model identity catalog exceeds size limit");
1022
+ return parseJsonBody(payload, "model identity catalog");
1023
+ }
1024
+ function catalogEntry(payload, model) {
1025
+ const entries = isRecord(payload) ? payload.models ?? [] : [];
1026
+ if (!Array.isArray(entries)) return null;
1027
+ const names = /* @__PURE__ */ new Set([model]);
1028
+ const lastSegment = model.slice(model.lastIndexOf("/") + 1);
1029
+ if (!lastSegment.includes(":")) names.add(model + ":latest");
1030
+ for (const item of entries) {
1031
+ if (!isRecord(item)) continue;
1032
+ const name = Object.hasOwn(item, "name") ? item.name : item.model;
1033
+ if (typeof name === "object" && name !== null) return "ambiguous";
1034
+ if (typeof name === "string" && names.has(name)) return item;
1035
+ }
1036
+ return null;
1037
+ }
1038
+ var DIGEST = /^(?:sha256:)?[0-9a-fA-F]{64}$/;
1039
+ var sha256 = (data) => createHash2("sha256").update(data, "utf8").digest("hex");
1040
+ async function resolveModelIdentity(endpointInput, model, systemPrompt, options = {}) {
1041
+ const endpoint = trimSlashes(endpointInput);
1042
+ const url = endpoint.endsWith(MANAGED_SUFFIX) ? endpoint.slice(0, -MANAGED_SUFFIX.length) + "/_freellama/v1/models" : endpoint + "/api/tags";
1043
+ const unknown = { identity: "", verified: false, scope: "current_process_only" };
1044
+ try {
1045
+ const entry = catalogEntry(await (options.readJson ?? readCatalogJson)(url), model);
1046
+ if (entry === "ambiguous") return unknown;
1047
+ const digestValue = entry?.digest;
1048
+ if (typeof digestValue !== "string" || !DIGEST.test(digestValue)) return unknown;
1049
+ const digest = (digestValue.startsWith("sha256:") ? digestValue.slice("sha256:".length) : digestValue).toLowerCase();
1050
+ const contract = { endpoint, model, digest, system_hash: sha256(systemPrompt) };
1051
+ return {
1052
+ identity: sha256(canonicalJson(contract)),
1053
+ verified: true,
1054
+ scope: "manifest_digest_and_system_prompt",
1055
+ digest
1056
+ };
1057
+ } catch (error) {
1058
+ if (isTransportError(error) || isSystemError(error) || error instanceof ValidationError || error instanceof SyntaxError || error instanceof TypeError) {
1059
+ return unknown;
1060
+ }
1061
+ throw error;
1062
+ }
1063
+ }
1064
+ async function resolvePlatformTokenRatio(serveEndpoint, model, options = {}) {
1065
+ try {
1066
+ const url = `${trimSlashes(serveEndpoint)}/_freellama/v1/models`;
1067
+ const entry = catalogEntry(await (options.readJson ?? readCatalogJson)(url), model);
1068
+ if (entry === null || entry === "ambiguous") return null;
1069
+ const milli = entry.bytes_per_token_milli;
1070
+ if (typeof milli !== "number" || !Number.isSafeInteger(milli) || milli <= 0) return null;
1071
+ const samples = entry.bytes_per_token_samples;
1072
+ return {
1073
+ bytes_per_token_milli: milli,
1074
+ bytes_per_token_samples: typeof samples === "number" && Number.isSafeInteger(samples) && samples > 0 ? samples : 1
1075
+ };
1076
+ } catch {
1077
+ return null;
1078
+ }
1079
+ }
1080
+ function isTransportError(error) {
1081
+ return error instanceof HTTPError || error instanceof URLError || error instanceof RequestTimeoutError;
1082
+ }
1083
+ function retryableChatError(error) {
1084
+ if (error instanceof HTTPError) {
1085
+ if (error.code !== 503 || error.body.length > RECEIPT_BODY_LIMIT) return false;
1086
+ try {
1087
+ const body = parseJsonBody(error.body, "retry receipt");
1088
+ if (!isRecord(body) || body.retry_safe === false || body.execution_outcome !== void 0 && body.execution_outcome !== "not_started") return false;
1089
+ 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));
1090
+ } catch {
1091
+ return false;
1092
+ }
1093
+ }
1094
+ return error instanceof URLError && error.reason.code === "ECONNREFUSED";
1095
+ }
1096
+ var RECEIPT_FIELDS = /* @__PURE__ */ new Set([
1097
+ "error",
1098
+ "code",
1099
+ "reason",
1100
+ "retry_after_seconds",
1101
+ "resource_admission",
1102
+ "lifecycle",
1103
+ "placement",
1104
+ "execution",
1105
+ "admission",
1106
+ "route",
1107
+ "decision",
1108
+ "quality",
1109
+ "feedback",
1110
+ "execution_outcome",
1111
+ "retry_safe",
1112
+ "result_recoverable",
1113
+ "job_id",
1114
+ "last_observed_status"
1115
+ ]);
1116
+ var PAYLOAD_FIELDS = /* @__PURE__ */ new Set([
1117
+ "prompt",
1118
+ "system",
1119
+ "system_prompt",
1120
+ "messages",
1121
+ "content",
1122
+ "input",
1123
+ "images",
1124
+ "tools",
1125
+ "request",
1126
+ "request_options",
1127
+ "headers",
1128
+ "authorization",
1129
+ "token",
1130
+ "api_key",
1131
+ "auth_token"
1132
+ ]);
1133
+ function receiptValue(value) {
1134
+ if (isRecord(value)) {
1135
+ const result = {};
1136
+ for (const [key, item] of Object.entries(value)) {
1137
+ if (!PAYLOAD_FIELDS.has(key.toLowerCase())) result[key] = receiptValue(item);
1138
+ }
1139
+ return result;
1140
+ }
1141
+ if (Array.isArray(value)) return value.map(receiptValue);
1142
+ return value;
1143
+ }
1144
+ function errorDiagnostic(error) {
1145
+ if (error instanceof Error) return `${error.name}: ${error.message}`;
1146
+ return String(error);
1147
+ }
1148
+ function chatErrorDetails(error) {
1149
+ let diagnostic = errorDiagnostic(error);
1150
+ if (!(error instanceof HTTPError)) return [diagnostic, null];
1151
+ const details = { status: error.code };
1152
+ try {
1153
+ const body = error.body.subarray(0, RECEIPT_BODY_LIMIT + 1);
1154
+ if (body.length > RECEIPT_BODY_LIMIT) return [diagnostic, details];
1155
+ const payload = parseJsonBody(body, "refusal body");
1156
+ if (!isRecord(payload) || typeof payload.error !== "string") return [diagnostic, details];
1157
+ const receipt = {};
1158
+ for (const [key, value] of Object.entries(payload)) {
1159
+ if (RECEIPT_FIELDS.has(key)) receipt[key] = receiptValue(value);
1160
+ }
1161
+ details.receipt = receipt;
1162
+ diagnostic = `HTTP ${error.code}: ${receipt.error}`;
1163
+ } catch (failure) {
1164
+ if (!(failure instanceof SyntaxError || failure instanceof ValidationError || failure instanceof RangeError)) throw failure;
1165
+ }
1166
+ return [diagnostic, details];
1167
+ }
1168
+ var PromptCacheUsage = class {
1169
+ calls = 0;
1170
+ reportedCalls = 0;
1171
+ reportedTokens = 0;
1172
+ reportedPromptTokens = 0;
1173
+ observe(response) {
1174
+ this.calls += 1;
1175
+ const cached = response.prompt_eval_cached_count;
1176
+ const prompt = response.prompt_eval_count;
1177
+ if (Number.isInteger(cached) && Number.isInteger(prompt) && cached >= 0 && cached <= prompt) {
1178
+ this.reportedCalls += 1;
1179
+ this.reportedTokens += cached;
1180
+ this.reportedPromptTokens += prompt;
1181
+ }
1182
+ }
1183
+ get tokens() {
1184
+ return this.calls > 0 && this.calls === this.reportedCalls ? this.reportedTokens : null;
1185
+ }
1186
+ metadata() {
1187
+ const tokens = this.tokens;
1188
+ return {
1189
+ status: tokens !== null ? "reported" : this.reportedCalls ? "partially_reported" : "not_reported",
1190
+ calls: this.calls,
1191
+ reported_calls: this.reportedCalls,
1192
+ reported_tokens: this.reportedCalls ? this.reportedTokens : null,
1193
+ hit_ratio: tokens !== null && this.reportedPromptTokens ? this.reportedTokens / this.reportedPromptTokens : null
1194
+ };
1195
+ }
1196
+ };
1197
+ var resolvedShift;
1198
+ function contextShift(env = process.env, reset = false) {
1199
+ if (resolvedShift !== void 0 && !reset) return resolvedShift;
1200
+ const read = (value2) => value2 === "ollama_default" || value2 === "off" ? value2 : void 0;
1201
+ let value = read(env.FREELLAMA_CONTEXT_SHIFT?.trim());
1202
+ if (!value) {
1203
+ const file = env.FREELLAMA_RUNTIME_CONFIG || path3.join(env.FREELLAMA_HOME || path3.join(homedir(), ".octocode", "freellama"), "runtime.toml");
1204
+ try {
1205
+ const topLevel = readFileSync4(file, "utf8").split(/^\s*\[/m)[0] ?? "";
1206
+ value = read(/^\s*context_shift\s*=\s*"([^"]*)"/m.exec(topLevel)?.[1]);
1207
+ } catch {
1208
+ }
1209
+ }
1210
+ resolvedShift = value ?? "off";
1211
+ return resolvedShift;
1212
+ }
1213
+ function chatRequest(endpointInput, model, messages, options, think, keepAlive, executionPreference = "auto", minPlacementEvidence = "configured") {
1214
+ const endpoint = trimSlashes(endpointInput);
1215
+ if (endpoint.endsWith(MANAGED_SUFFIX)) {
1216
+ const managedOptions = {};
1217
+ for (const [key, value] of Object.entries(options)) {
1218
+ if (key !== "num_ctx") managedOptions[key] = value;
1219
+ }
1220
+ if (!Object.hasOwn(options, "num_ctx")) throw new ValidationError("managed chat request requires num_ctx");
1221
+ return [endpoint, {
1222
+ task: "coding",
1223
+ objective: "fastest",
1224
+ model,
1225
+ context_tokens: options.num_ctx,
1226
+ execution_preference: executionPreference,
1227
+ min_placement_evidence: minPlacementEvidence,
1228
+ messages,
1229
+ keep_alive: keepAlive,
1230
+ request_options: { format: "json", think, options: managedOptions }
1231
+ }];
1232
+ }
1233
+ return [`${endpoint}/api/chat`, {
1234
+ model,
1235
+ messages,
1236
+ stream: false,
1237
+ truncate: false,
1238
+ ...contextShift() === "ollama_default" ? {} : { shift: false },
1239
+ format: "json",
1240
+ think,
1241
+ keep_alive: keepAlive,
1242
+ options
1243
+ }];
1244
+ }
1245
+ function unwrapChatResponse(payload, executionReceipts) {
1246
+ if (!isRecord(payload)) throw new TypeError("chat response must be a JSON object");
1247
+ const { response } = payload;
1248
+ if (!isRecord(response)) return payload;
1249
+ const route = payload.route ?? {};
1250
+ if (!isRecord(route)) throw new TypeError("managed chat response route must be a JSON object");
1251
+ executionReceipts.push({
1252
+ model: route.selected_model ?? null,
1253
+ execution: payload.execution ?? null,
1254
+ admission: payload.admission ?? null,
1255
+ feedback: payload.feedback ?? null
1256
+ });
1257
+ return response;
1258
+ }
1259
+
1260
+ // src/research/transport-jobs.ts
1261
+ import { STATUS_CODES } from "node:http";
1262
+
1263
+ // src/job-progress.ts
1264
+ var TERMINAL = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "expired"]);
1265
+ var SUBMIT_FALLBACK_CODES = /* @__PURE__ */ new Set(["task_job_input_too_large", "task_jobs_full", "task_jobs_input_budget"]);
1266
+ var RETENTION_FAILURE_CODES = /* @__PURE__ */ new Set(["task_job_result_too_large", "task_job_error_too_large"]);
1267
+ var FIRST_POLL_MS = 250;
1268
+ var HEARTBEAT_MS = 1e4;
1269
+ var MAX_POLL_MS = 2e3;
1270
+ var MAX_POLL_FAILURES = 5;
1271
+ var CANCEL_ATTEMPTS = 3;
1272
+ var CANCEL_BACKOFF_MS = 250;
1273
+ var CANCEL_REPORT_MS = 5e3;
1274
+ var object = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
1275
+ var number = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
1276
+ var gigabytes = (bytes) => `${(bytes / 1e9).toFixed(1)} GB`;
1277
+ var seconds = (ms) => ms < 1e4 ? `${(ms / 1e3).toFixed(1)} s` : `${Math.round(ms / 1e3)} s`;
1278
+ function modelNames(value) {
1279
+ if (!Array.isArray(value)) return [];
1280
+ return value.flatMap((item) => {
1281
+ if (typeof item === "string") return [item];
1282
+ const named = object(item)?.model ?? object(item)?.name;
1283
+ return typeof named === "string" ? [named] : [];
1284
+ });
1285
+ }
1286
+ function evictedSuffix(detail) {
1287
+ const evicted = modelNames(detail?.evicted ?? detail?.evicted_idle_models);
1288
+ return evicted.length > 0 ? `, evicted ${evicted.join(", ")}` : "";
1289
+ }
1290
+ function holdersSuffix(detail) {
1291
+ const holders = Array.isArray(detail?.memory_holders) ? detail.memory_holders : [];
1292
+ const named = holders.map((holder) => holder).filter((holder) => typeof holder.name === "string" && typeof holder.resident_bytes === "number").slice(0, 3).map((holder) => {
1293
+ const count = typeof holder.processes === "number" && holder.processes > 1 ? ` \xD7${holder.processes}` : "";
1294
+ return `${holder.name}${count} ${gigabytes(holder.resident_bytes)}`;
1295
+ });
1296
+ return named.length > 0 ? `; largest memory users: ${named.join(", ")}` : "";
1297
+ }
1298
+ function jobProgressMessage(job) {
1299
+ const status = typeof job.status === "string" ? job.status : "queued";
1300
+ const reason = typeof job.reason === "string" ? job.reason : "";
1301
+ const detail = object(job.detail);
1302
+ const model = typeof job.selected_model === "string" && job.selected_model ? job.selected_model : void 0;
1303
+ switch (status) {
1304
+ case "queued":
1305
+ return "queued";
1306
+ case "waiting_for_admission":
1307
+ return reason === "resources_reserved_waiting_for_backend_capacity" ? "memory reserved; waiting for backend capacity" : "waiting for backend capacity";
1308
+ case "waiting_for_resources": {
1309
+ const need = number(detail?.required_available_bytes);
1310
+ const available = number(detail?.effective_available_bytes);
1311
+ if (need === void 0 && available === void 0) {
1312
+ return (reason === "checking_host_resources" ? "checking host memory" : "waiting for memory") + evictedSuffix(detail);
1313
+ }
1314
+ const sizes = [need !== void 0 ? `need ${gigabytes(need)}` : void 0, available !== void 0 ? `${gigabytes(available)} available` : void 0].filter(Boolean).join(", ");
1315
+ const pressure = Array.isArray(detail?.pressure_reasons) ? detail.pressure_reasons.map((item) => typeof item === "string" ? item : JSON.stringify(item)) : [];
1316
+ return `waiting for memory: ${sizes}${pressure.length > 0 ? ` (host: ${pressure.join(", ")})` : ""}${holdersSuffix(detail)}${evictedSuffix(detail)}`;
1317
+ }
1318
+ case "waiting_for_runner":
1319
+ return "waiting for runner";
1320
+ case "loading": {
1321
+ const expected = number(detail?.expected_load_ms);
1322
+ return `loading ${model ?? "model"}${expected !== void 0 ? ` (~${seconds(expected)})` : ""}${evictedSuffix(detail)}`;
1323
+ }
1324
+ case "running":
1325
+ return model ? `running ${model}` : "running";
1326
+ default:
1327
+ return status.replace(/_/g, " ");
1328
+ }
1329
+ }
1330
+ function errorBody(error) {
1331
+ try {
1332
+ return object(JSON.parse(error instanceof Error ? error.message : String(error)));
1333
+ } catch {
1334
+ return void 0;
1335
+ }
1336
+ }
1337
+ function describeNapiError(error) {
1338
+ const message = error instanceof Error ? error.message : String(error);
1339
+ const missing = /does not exist or has expired|\bHTTP 404\b|^\s*(?:404\b|Not Found\s*$)/i.test(message);
1340
+ return { body: errorBody(error), ...missing ? { status: 404 } : {} };
1341
+ }
1342
+ function unconfirmedSubmission(error, body) {
1343
+ const message = error instanceof Error ? error.message : String(error);
1344
+ if (body && (typeof body.code === "string" || "execution_outcome" in body || "retry_safe" in body)) {
1345
+ return error instanceof Error ? error : new Error(message);
1346
+ }
1347
+ return new Error(JSON.stringify({
1348
+ ...body,
1349
+ error: typeof body?.error === "string" ? body.error : message,
1350
+ execution_outcome: "unknown",
1351
+ retry_safe: false
1352
+ }));
1353
+ }
1354
+ function submissionRefused({ body }) {
1355
+ const code = body?.code;
1356
+ return typeof code === "string" && SUBMIT_FALLBACK_CODES.has(code);
1357
+ }
1358
+ var defaultWait = (ms, signal) => new Promise((resolve) => {
1359
+ if (signal?.aborted) return resolve();
1360
+ const done = () => {
1361
+ clearTimeout(timer);
1362
+ signal?.removeEventListener("abort", done);
1363
+ resolve();
1364
+ };
1365
+ const timer = setTimeout(done, ms);
1366
+ signal?.addEventListener("abort", done, { once: true });
1367
+ });
1368
+ async function runWithJobProgress(calls, report, options = {}) {
1369
+ const { signal, wait = defaultWait, describe = describeNapiError, onJob } = options;
1370
+ 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.");
1371
+ if (signal?.aborted) throw options.aborted?.() ?? new Error("run_task was cancelled by the client before submission; no task was submitted.");
1372
+ let raw;
1373
+ try {
1374
+ raw = await calls.submit();
1375
+ } catch (error) {
1376
+ if (signal?.aborted) throw aborted();
1377
+ const described = describe(error);
1378
+ if (submissionRefused(described)) return calls.sync(described.body);
1379
+ throw options.unconfirmed?.(error, described.body) ?? unconfirmedSubmission(error, described.body);
1380
+ }
1381
+ let submitted;
1382
+ try {
1383
+ submitted = object(JSON.parse(raw));
1384
+ } catch {
1385
+ return raw;
1386
+ }
1387
+ let job = object(submitted?.job);
1388
+ const id = typeof job?.id === "string" ? job.id : void 0;
1389
+ if (submitted?.deferred !== true || !job || !id) return raw;
1390
+ onJob?.(id, true);
1391
+ let open = true;
1392
+ const settled = () => {
1393
+ if (open) onJob?.(id, false);
1394
+ open = false;
1395
+ };
1396
+ let cancellation;
1397
+ const requestCancellation = async () => {
1398
+ for (let attempt = 1; ; attempt++) {
1399
+ try {
1400
+ const reply = object(JSON.parse(await calls.cancel(id)));
1401
+ const status = object(reply?.job)?.status ?? reply?.status;
1402
+ if (!TERMINAL.has(String(status))) return "requested";
1403
+ settled();
1404
+ return "acknowledged";
1405
+ } catch (error) {
1406
+ if (describe(error).status === 404) {
1407
+ settled();
1408
+ return "unknown";
1409
+ }
1410
+ if (attempt >= CANCEL_ATTEMPTS) return "unknown";
1411
+ await wait(CANCEL_BACKOFF_MS * 2 ** (attempt - 1));
1412
+ }
1413
+ }
1414
+ };
1415
+ const cancel = () => {
1416
+ cancellation ??= requestCancellation();
1417
+ };
1418
+ const cancellationState = async () => {
1419
+ if (!cancellation) return void 0;
1420
+ let timer;
1421
+ const capped = new Promise((resolve) => {
1422
+ timer = setTimeout(() => resolve("unknown"), CANCEL_REPORT_MS);
1423
+ });
1424
+ try {
1425
+ return await Promise.race([cancellation, capped]);
1426
+ } finally {
1427
+ clearTimeout(timer);
1428
+ }
1429
+ };
1430
+ signal?.addEventListener("abort", cancel, { once: true });
1431
+ try {
1432
+ let lastMessage = "";
1433
+ let lastPhase = "";
1434
+ let lastReportAt = Date.now();
1435
+ let phaseStartedAt = lastReportAt;
1436
+ let delay = FIRST_POLL_MS;
1437
+ let failures = 0;
1438
+ for (; ; ) {
1439
+ if (signal?.aborted) {
1440
+ cancel();
1441
+ throw aborted(id, await cancellationState());
1442
+ }
1443
+ const now = Date.now();
1444
+ const phase = `${String(job.status)}|${String(job.reason)}`;
1445
+ if (phase !== lastPhase) {
1446
+ lastPhase = phase;
1447
+ phaseStartedAt = now;
1448
+ delay = FIRST_POLL_MS;
1449
+ }
1450
+ const message = jobProgressMessage(job);
1451
+ if (message !== lastMessage) {
1452
+ report(message);
1453
+ lastMessage = message;
1454
+ lastReportAt = now;
1455
+ } else if (now - lastReportAt >= HEARTBEAT_MS && !TERMINAL.has(String(job.status))) {
1456
+ report(`${message} (${seconds(now - phaseStartedAt)})`);
1457
+ lastReportAt = now;
1458
+ }
1459
+ if (TERMINAL.has(String(job.status))) break;
1460
+ await wait(delay, signal);
1461
+ if (signal?.aborted) {
1462
+ cancel();
1463
+ throw aborted(id, await cancellationState());
1464
+ }
1465
+ delay = Math.min(MAX_POLL_MS, Math.round(delay * 1.5));
1466
+ try {
1467
+ const polled = object(object(JSON.parse(await calls.get(id)))?.job);
1468
+ if (polled) job = polled;
1469
+ failures = 0;
1470
+ } catch (error) {
1471
+ if (describe(error).status === 404) {
1472
+ settled();
1473
+ const failure2 = {
1474
+ code: "task_job_unavailable",
1475
+ 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.`,
1476
+ job_id: id,
1477
+ last_observed_status: job.status,
1478
+ execution_outcome: "unknown",
1479
+ result_recoverable: false,
1480
+ retry_safe: false
1481
+ };
1482
+ throw options.failure?.(failure2, 404) ?? new Error(JSON.stringify(failure2));
1483
+ }
1484
+ failures += 1;
1485
+ if (failures >= MAX_POLL_FAILURES) {
1486
+ cancel();
1487
+ throw error;
1488
+ }
1489
+ }
1490
+ }
1491
+ settled();
1492
+ if (job.status === "completed") return JSON.stringify(job.result ?? {});
1493
+ const failure = { ...object(job.error) ?? { error: `deferred job ${id} ended ${String(job.status)}` } };
1494
+ if (typeof failure.code === "string" && RETENTION_FAILURE_CODES.has(failure.code)) {
1495
+ const lost = failure.code === "task_job_result_too_large" ? "result" : "error";
1496
+ 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.`;
1497
+ failure.job_id = id;
1498
+ failure.result_recoverable = false;
1499
+ failure.retry_safe = false;
1500
+ }
1501
+ const status = typeof failure.status === "number" ? failure.status : void 0;
1502
+ delete failure.id;
1503
+ delete failure.ok;
1504
+ if (status !== void 0) delete failure.status;
1505
+ throw options.failure?.(failure, status) ?? new Error(JSON.stringify(failure));
1506
+ } finally {
1507
+ signal?.removeEventListener("abort", cancel);
1508
+ }
1509
+ }
1510
+
1511
+ // src/research/transport-jobs.ts
1512
+ var TASKS_SUFFIX = "/_freellama/v1/tasks";
1513
+ function describeHttpError(error) {
1514
+ if (!(error instanceof HTTPError)) return {};
1515
+ try {
1516
+ const body = parseJsonBody(error.body.subarray(0, RECEIPT_BODY_LIMIT), "error body");
1517
+ return { status: error.code, ...isRecord(body) ? { body } : {} };
1518
+ } catch {
1519
+ return { status: error.code };
1520
+ }
1521
+ }
1522
+ function syncError(body, status) {
1523
+ const code = status ?? 500;
1524
+ return new HTTPError(code, STATUS_CODES[code] ?? "", Buffer.from(JSON.stringify(body), "utf8"));
1525
+ }
1526
+ async function requestJsonAsJob(url, payload, timeoutSeconds, options) {
1527
+ const request = options.request ?? httpRequest;
1528
+ const headers = requestHeaders();
1529
+ const call = async (method, target, body, signal) => {
1530
+ const sent = body === void 0 ? void 0 : Buffer.from(JSON.stringify(body), "utf8");
1531
+ const response = await request(target, { method, headers, body: sent, timeoutSeconds, signal });
1532
+ return decodeUtf8(response, "managed task response");
1533
+ };
1534
+ const sync = () => call("POST", url, payload);
1535
+ if (!url.endsWith(TASKS_SUFFIX)) return parseJsonBody(Buffer.from(await sync(), "utf8"), "chat response");
1536
+ const jobs = `${url.slice(0, -TASKS_SUFFIX.length)}/_freellama/v1/jobs`;
1537
+ const deadline = new AbortController();
1538
+ const timer = setTimeout(() => deadline.abort(), Math.max(timeoutSeconds * 1e3, 1));
1539
+ try {
1540
+ const raw = await runWithJobProgress({
1541
+ submit: () => call("POST", url, { ...payload, defer: true }),
1542
+ get: (id) => call("GET", `${jobs}/${encodeURIComponent(id)}`, void 0, deadline.signal),
1543
+ cancel: (id) => call("POST", `${jobs}/${encodeURIComponent(id)}/cancel`, null),
1544
+ sync: async () => {
1545
+ throw syncError({
1546
+ code: "cancellable_task_unavailable",
1547
+ error: "Serve refused job submission before execution; cancellable research requires available job capacity and a supported request size.",
1548
+ execution_outcome: "not_started",
1549
+ retry_safe: true
1550
+ }, 409);
1551
+ }
1552
+ }, options.report, {
1553
+ signal: deadline.signal,
1554
+ wait: options.wait,
1555
+ describe: describeHttpError,
1556
+ failure: syncError,
1557
+ aborted: () => new RequestTimeoutError(),
1558
+ // Keep transport types, but do not let an arbitrary 503 erase an uncertain submission.
1559
+ unconfirmed: (error, body) => error instanceof HTTPError && !retryableChatError(error) ? syncError({
1560
+ ...body,
1561
+ error: typeof body?.error === "string" ? body.error : error.message,
1562
+ execution_outcome: body?.execution_outcome ?? "unknown",
1563
+ retry_safe: false
1564
+ }, error.code) : error,
1565
+ onJob: options.onJob
1566
+ });
1567
+ return parseJsonBody(Buffer.from(raw, "utf8"), "chat response");
1568
+ } finally {
1569
+ clearTimeout(timer);
1570
+ }
1571
+ }
1572
+
1573
+ // src/research/workspace-path.ts
1574
+ import { lstatSync, readlinkSync, statSync } from "node:fs";
1575
+ import path4 from "node:path";
1576
+ var SymlinkLoopError = class extends ValidationError {
1577
+ name = "SymlinkLoopError";
1578
+ };
1579
+ var hasNul = (text) => text.includes("\0");
1580
+ function cleanPath(text) {
1581
+ const parts = text.split("/").filter((part) => part !== "" && part !== ".");
1582
+ if (text.startsWith("/")) return `/${parts.join("/")}`;
1583
+ return parts.join("/") || ".";
1584
+ }
1585
+ function joinPath(base, child) {
1586
+ return cleanPath(child.startsWith("/") ? child : `${base}/${child}`);
1587
+ }
1588
+ function escapesRoot(resolved, root) {
1589
+ const a = cleanPath(resolved);
1590
+ const b = cleanPath(root);
1591
+ return a !== b && !a.startsWith(b === "/" ? "/" : `${b}/`);
1592
+ }
1593
+ function readlink(target) {
1594
+ if (hasNul(target)) throw new ValidationError("embedded null byte");
1595
+ try {
1596
+ return readlinkSync(target);
1597
+ } catch {
1598
+ return null;
1599
+ }
1600
+ }
1601
+ function resolvePath(target, cwd = process.cwd()) {
1602
+ const seen = /* @__PURE__ */ new Map();
1603
+ const walk = (start, rest) => {
1604
+ let current = rest.startsWith("/") ? "" : start;
1605
+ for (const name of rest.split("/")) {
1606
+ if (!name || name === ".") continue;
1607
+ if (name === "..") {
1608
+ current = current.slice(0, Math.max(current.lastIndexOf("/"), 0));
1609
+ continue;
1610
+ }
1611
+ const next = `${current}/${name}`;
1612
+ if (seen.has(next)) {
1613
+ const cached = seen.get(next);
1614
+ if (cached === null || cached === void 0) throw new SymlinkLoopError(`symlink loop at ${JSON.stringify(next)}`);
1615
+ current = cached;
1616
+ continue;
1617
+ }
1618
+ const link = readlink(next);
1619
+ if (link === null) {
1620
+ current = next;
1621
+ } else {
1622
+ seen.set(next, null);
1623
+ current = walk(current, link);
1624
+ seen.set(next, current);
1625
+ }
1626
+ }
1627
+ return current;
1628
+ };
1629
+ const text = cleanPath(target);
1630
+ return path4.posix.normalize(walk(text.startsWith("/") ? "" : cwd, text) || "/");
1631
+ }
1632
+
1633
+ // src/research/grounding.ts
1634
+ import { existsSync as existsSync3 } from "node:fs";
1635
+ import path5 from "node:path";
1636
+ var PATH_TOKEN = /(?<![\w@/.-])(?:\.\/)?[\w@-][\w@.-]*(?:\/[\w@.-]+)+\.[A-Za-z][A-Za-z0-9]{0,5}(?=$|[\s:`'"),.;\]])/g;
1637
+ function clean(token) {
1638
+ return token.replace(/^\.\//, "").replace(/[.,;]+$/, "");
1639
+ }
1640
+ function pathsIn(text) {
1641
+ return [...new Set([...text.matchAll(PATH_TOKEN)].map((match) => clean(match[0])))];
1642
+ }
1643
+ function missingPaths(answer, workspace) {
1644
+ return pathsIn(answer).filter((candidate) => !existsSync3(path5.join(workspace, candidate)));
1645
+ }
1646
+ function evidencePaths(observations, workspace, limit = 10) {
1647
+ const found = [];
1648
+ for (const text of observations) {
1649
+ for (const candidate of pathsIn(text)) {
1650
+ if (!found.includes(candidate) && existsSync3(path5.join(workspace, candidate))) found.push(candidate);
1651
+ if (found.length >= limit) return found;
1652
+ }
1653
+ }
1654
+ return found;
1655
+ }
1656
+ function groundingRepairNotice(missing, evidence) {
1657
+ 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.";
1658
+ }
1659
+ function groundingNote(missing) {
1660
+ return `
1661
+ [freellama: cited path(s) not found in the workspace: ${missing.join(", ")}; treat this answer as unverified]`;
1662
+ }
1663
+ function toolErrorHint(message) {
1664
+ if (/rg: unrecognized flag --(?:exclude-dir|include)/.test(message)) {
1665
+ return "\nHint: rg excludes directories with --glob '!node_modules' and filters types with -t ts; or use grep -rn PATTERN . --exclude-dir=node_modules.";
1666
+ }
1667
+ return "";
1668
+ }
1669
+ function sourceRepairNotice(evidence) {
1670
+ 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(", ")}.`;
1671
+ }
1672
+
1673
+ // src/research/agent-loop.ts
1674
+ var isValidationError = (error) => error instanceof ValidationError;
1675
+ function requireEnv(env, name) {
1676
+ const value = env[name];
1677
+ if (value === void 0) throw new Error(`${name} is not set`);
1678
+ return value;
1679
+ }
1680
+ var readText = (file) => normalizeNewlines(decodeUtf8(readFileSync5(file), file));
1681
+ var pathOf = (value) => value === "" ? "." : value;
1682
+ var numberOr = (value, fallback) => typeof value === "number" && Number.isFinite(value) ? value : typeof value === "boolean" ? Number(value) : fallback;
1683
+ var echo = (action) => canonicalJson(action, { sortKeys: false, ensureAscii: false });
1684
+ async function runAdapter(spec, hooks = {}) {
1685
+ const env = hooks.env ?? process.env;
1686
+ let resultPath = null;
1687
+ try {
1688
+ const model = env.FREELLAMA_TARGET_MODEL || requireEnv(env, "FREELLAMA_BENCH_MODEL");
1689
+ const workspace = resolvePath(pathOf(requireEnv(env, "FREELLAMA_BENCH_WORKSPACE")));
1690
+ const prompt = readText(pathOf(requireEnv(env, "FREELLAMA_BENCH_PROMPT")));
1691
+ resultPath = pathOf(requireEnv(env, "FREELLAMA_AGENT_RESULT"));
1692
+ return await runLoop(spec, hooks, env, { model, workspace, prompt, resultPath });
1693
+ } catch (error) {
1694
+ const answer = `unexpected adapter error: ${errorDiagnostic(error)}`;
1695
+ process.stderr.write(`${error instanceof Error ? error.stack ?? answer : answer}
1696
+ `);
1697
+ if (resultPath !== null) {
1698
+ try {
1699
+ writeFailureResult(resultPath, answer);
1700
+ } catch {
1701
+ }
1702
+ }
1703
+ return 1;
1704
+ }
1705
+ }
1706
+ async function runLoop(spec, hooks, env, inputs) {
1707
+ const { model, workspace, prompt, resultPath } = inputs;
1708
+ const print = hooks.print ?? ((line) => process.stdout.write(`${line}
1709
+ `));
1710
+ const progress = hooks.progress ?? progressEmitter(env);
1711
+ const endpoint = (env.FREELLAMA_OLLAMA_ENDPOINT ?? "http://127.0.0.1:11434").replace(/\/+$/, "");
1712
+ const managedEndpoint = (env.FREELLAMA_AGENT_MANAGED_ENDPOINT ?? "").replace(/\/+$/, "");
1713
+ let phaseAt = { maxTurns: 0 };
1714
+ const sendJson = hooks.requestJson ?? (managedEndpoint ? (url, payload, timeoutSeconds) => requestJsonAsJob(url, payload, timeoutSeconds, {
1715
+ report: (message) => progress?.({ event: "job_phase", ...phaseAt, message }),
1716
+ onJob: (id, open) => progress?.({ event: open ? "job_open" : "job_closed", id }),
1717
+ request: hooks.httpRequest
1718
+ }) : requestJson);
1719
+ const executionPreference = env.FREELLAMA_AGENT_EXECUTION_PREFERENCE ?? "auto";
1720
+ const minPlacementEvidence = env.FREELLAMA_AGENT_MIN_PLACEMENT_EVIDENCE ?? "configured";
1721
+ let runtime;
1722
+ try {
1723
+ runtime = AgentRuntimeConfig.fromEnv({ defaultToolTimeoutSeconds: spec.defaultToolTimeoutSeconds, env });
1724
+ } catch (error) {
1725
+ if (!isValidationError(error)) throw error;
1726
+ const answer2 = `invalid research adapter configuration: ${error.message}`;
1727
+ writeFailureResult(resultPath, answer2);
1728
+ print(answer2);
1729
+ return 1;
1730
+ }
1731
+ let messages = [
1732
+ { role: "system", content: spec.systemPrompt(workspace) },
1733
+ { role: "user", content: withBriefing(prompt, spec, workspace) }
1734
+ ];
1735
+ const calls = [];
1736
+ const usage = { input_tokens: 0, output_tokens: 0, cache_read_tokens: null, cache_write_tokens: null };
1737
+ const cacheUsage = new PromptCacheUsage();
1738
+ const metrics = { load_ms: 0, prompt_eval_ms: 0, eval_ms: 0 };
1739
+ const executionReceipts = [];
1740
+ let answer = "";
1741
+ let failure = null;
1742
+ let transportError = null;
1743
+ const readCatalog = (url) => readCatalogJson(url, hooks.httpRequest);
1744
+ const platformRatio = managedEndpoint ? await (hooks.resolvePlatformTokenRatio ?? resolvePlatformTokenRatio)(managedEndpoint, model, { readJson: readCatalog }) : null;
1745
+ const calibrationDir = trimSpace(env.FREELLAMA_AGENT_TOKEN_CALIBRATION_DIR ?? "");
1746
+ const calibrationModelIdentity = calibrationDir ? await (hooks.resolveModelIdentity ?? resolveModelIdentity)(
1747
+ managedEndpoint || endpoint,
1748
+ model,
1749
+ messages[0].content,
1750
+ { readJson: readCatalog }
1751
+ ) : { verified: false, scope: "persistence_disabled", identity: "" };
1752
+ const contextManager = new AgentContextManager(runtime, {
1753
+ model,
1754
+ calibrationIdentity: calibrationModelIdentity.identity,
1755
+ calibrationDir: calibrationDir && calibrationModelIdentity.verified ? calibrationDir : null,
1756
+ platformRatio
1757
+ });
1758
+ try {
1759
+ messages = contextManager.fit(messages);
1760
+ } catch (error) {
1761
+ if (!isValidationError(error)) throw error;
1762
+ answer = `research task does not fit the configured context safely: ${error.message}`;
1763
+ writeFailureResult(resultPath, answer);
1764
+ print(answer);
1765
+ return 1;
1766
+ }
1767
+ const chatOptions = {
1768
+ temperature: runtime.temperature,
1769
+ seed: runtime.seed,
1770
+ num_ctx: runtime.num_ctx,
1771
+ num_predict: runtime.num_predict
1772
+ };
1773
+ const callModel = async () => {
1774
+ let lastError = null;
1775
+ let response = null;
1776
+ for (let attempt = 0; attempt < runtime.retry_attempts; attempt += 1) {
1777
+ try {
1778
+ const transportEndpoint = managedEndpoint ? `${managedEndpoint}/_freellama/v1/tasks` : endpoint;
1779
+ const [url, payload] = chatRequest(
1780
+ transportEndpoint,
1781
+ model,
1782
+ messages,
1783
+ chatOptions,
1784
+ runtime.think,
1785
+ runtime.keep_alive,
1786
+ executionPreference,
1787
+ minPlacementEvidence
1788
+ );
1789
+ const recorded = executionReceipts.length;
1790
+ response = unwrapChatResponse(await sendJson(url, payload, runtime.request_timeout_seconds), executionReceipts);
1791
+ if (managedEndpoint && executionReceipts.length === recorded) {
1792
+ executionReceipts.push({ model: null, execution: null, reason: "managed_execution_receipt_missing" });
1793
+ }
1794
+ break;
1795
+ } catch (error) {
1796
+ if (!isTransportError(error) || !retryableChatError(error)) throw error;
1797
+ lastError = error;
1798
+ if (attempt + 1 < runtime.retry_attempts) {
1799
+ progress?.({ event: "retry", attempt: attempt + 2, attempts: runtime.retry_attempts });
1800
+ await sleep(runtime.retry_backoff_seconds * 1e3);
1801
+ }
1802
+ }
1803
+ }
1804
+ if (response === null) throw lastError;
1805
+ usage.input_tokens = usage.input_tokens + Math.trunc(numberOr(response.prompt_eval_count, 0));
1806
+ cacheUsage.observe(response);
1807
+ usage.cache_read_tokens = cacheUsage.tokens;
1808
+ usage.output_tokens = usage.output_tokens + Math.trunc(numberOr(response.eval_count, 0));
1809
+ metrics.load_ms += numberOr(response.load_duration, 0) / 1e6;
1810
+ metrics.prompt_eval_ms += numberOr(response.prompt_eval_duration, 0) / 1e6;
1811
+ metrics.eval_ms += numberOr(response.eval_duration, 0) / 1e6;
1812
+ contextManager.observe(messages, response.prompt_eval_count);
1813
+ return response;
1814
+ };
1815
+ const refit = () => {
1816
+ try {
1817
+ messages = contextManager.fit(messages);
1818
+ return true;
1819
+ } catch (error) {
1820
+ if (!isValidationError(error)) throw error;
1821
+ failure = `research context cannot be fitted safely: ${error.message}`;
1822
+ return false;
1823
+ }
1824
+ };
1825
+ const content = (response) => {
1826
+ const message = response.message ?? {};
1827
+ if (!isRecord(message)) throw new TypeError("chat response message must be a JSON object");
1828
+ const text = message.content ?? "";
1829
+ return typeof text === "string" ? text : JSON.stringify(text);
1830
+ };
1831
+ const seenCalls = /* @__PURE__ */ new Map();
1832
+ const pageSize = runtime.context.observation_page_chars;
1833
+ const observations = hooks.observationStore ? hooks.observationStore(pageSize) : new ObservationStore(pageSize);
1834
+ let session;
1835
+ try {
1836
+ try {
1837
+ session = spec.open ? await spec.open(workspace) : void 0;
1838
+ } catch (error) {
1839
+ if (!isValidationError(error)) throw error;
1840
+ answer = `research adapter could not start: ${error.message}`;
1841
+ writeFailureResult(resultPath, answer);
1842
+ print(answer);
1843
+ return 1;
1844
+ }
1845
+ const root = session?.workspace ?? workspace;
1846
+ let parseFailures = 0;
1847
+ let exhausted = true;
1848
+ let repeats = 0;
1849
+ let groundingRepaired = false;
1850
+ let sourceRepaired = false;
1851
+ const seenOutputs = [];
1852
+ for (let turn = 0; turn < runtime.max_turns; turn += 1) {
1853
+ const at = { turn: turn + 1, maxTurns: runtime.max_turns };
1854
+ phaseAt = at;
1855
+ progress?.({ event: "model", ...at });
1856
+ let response;
1857
+ try {
1858
+ response = await callModel();
1859
+ } catch (error) {
1860
+ if (!isTransportError(error)) throw error;
1861
+ let diagnostic;
1862
+ [diagnostic, transportError] = chatErrorDetails(error);
1863
+ failure = `agent response failed: ${diagnostic}`;
1864
+ exhausted = false;
1865
+ break;
1866
+ }
1867
+ const raw = content(response);
1868
+ let action;
1869
+ try {
1870
+ action = spec.parseAction(raw);
1871
+ } catch (error) {
1872
+ if (!isValidationError(error)) throw error;
1873
+ parseFailures += 1;
1874
+ if (parseFailures > runtime.max_parse_repairs) {
1875
+ failure = `agent gave ${parseFailures} unparseable replies: ${errorDiagnostic(error)}`;
1876
+ exhausted = false;
1877
+ break;
1878
+ }
1879
+ progress?.({ event: "repair", ...at });
1880
+ messages.push({ role: "assistant", content: cpSlice(raw, 0, runtime.parse_repair_echo_chars) });
1881
+ messages.push({ role: "user", content: PARSE_REPAIR_NOTICE });
1882
+ if (!refit()) {
1883
+ exhausted = false;
1884
+ break;
1885
+ }
1886
+ continue;
1887
+ }
1888
+ parseFailures = 0;
1889
+ if (action.action === "page") {
1890
+ const wantStep = Math.trunc(numberOr(action.step, 0));
1891
+ const wantPage = Math.trunc(numberOr(action.page, 1));
1892
+ progress?.({ event: "action", ...at, action });
1893
+ messages.push({ role: "assistant", content: echo(action) });
1894
+ messages.push({ role: "user", content: observations.message(wantStep, wantPage) });
1895
+ if (!refit()) {
1896
+ exhausted = false;
1897
+ break;
1898
+ }
1899
+ continue;
1900
+ }
1901
+ if (action.action === "finish") {
1902
+ const proposed = trimSpace(String(action.answer ?? ""));
1903
+ const missing = missingPaths(proposed, root);
1904
+ if (missing.length > 0 && !groundingRepaired) {
1905
+ groundingRepaired = true;
1906
+ progress?.({ event: "repair", ...at });
1907
+ messages.push({ role: "assistant", content: echo(action) });
1908
+ messages.push({ role: "user", content: groundingRepairNotice(missing, evidencePaths(seenOutputs, root)) });
1909
+ if (!refit()) {
1910
+ answer = proposed + groundingNote(missing);
1911
+ exhausted = false;
1912
+ break;
1913
+ }
1914
+ continue;
1915
+ }
1916
+ const evidence = spec.requireSource !== false && missing.length === 0 && !sourceRepaired && pathsIn(proposed).length === 0 ? evidencePaths(seenOutputs, root) : [];
1917
+ if (evidence.length > 0) {
1918
+ sourceRepaired = true;
1919
+ progress?.({ event: "repair", ...at });
1920
+ messages.push({ role: "assistant", content: echo(action) });
1921
+ messages.push({ role: "user", content: sourceRepairNotice(evidence) });
1922
+ if (!refit()) {
1923
+ answer = proposed;
1924
+ exhausted = false;
1925
+ break;
1926
+ }
1927
+ continue;
1928
+ }
1929
+ progress?.({ event: "finish", ...at });
1930
+ answer = missing.length > 0 ? proposed + groundingNote(missing) : proposed;
1931
+ exhausted = false;
1932
+ break;
1933
+ }
1934
+ const started = performance.now();
1935
+ const call = session.toolCall(action, root, runtime);
1936
+ const signature = callSignature(call.signature);
1937
+ let observation;
1938
+ let status;
1939
+ let exitCode;
1940
+ if (seenCalls.has(signature)) {
1941
+ progress?.({ event: "action", ...at, action, repeat: true });
1942
+ observation = REPEAT_NOTICE;
1943
+ status = "repeat";
1944
+ repeats += 1;
1945
+ } else {
1946
+ progress?.({ event: "action", ...at, action });
1947
+ try {
1948
+ observation = hooks.runTool ? await hooks.runTool(call.record.arguments) : await call.run();
1949
+ status = "ok";
1950
+ } catch (error) {
1951
+ if (!isToolError(error)) throw error;
1952
+ status = error instanceof ToolExitError ? error.status : "error";
1953
+ exitCode = error instanceof ToolExitError ? error.exitCode : void 0;
1954
+ observation = `${status === "empty" ? "tool search" : "tool error"}: ${error.message}${toolErrorHint(error.message)}`;
1955
+ }
1956
+ seenCalls.set(signature, calls.length + 1);
1957
+ }
1958
+ calls.push({
1959
+ ...call.record,
1960
+ status,
1961
+ ...exitCode === void 0 ? {} : { exit_code: exitCode },
1962
+ duration_ms: Math.round((performance.now() - started) * 1e3) / 1e3
1963
+ });
1964
+ messages.push({ role: "assistant", content: echo(action) });
1965
+ const remaining = runtime.max_turns - calls.length;
1966
+ const step = calls.length;
1967
+ observations.put(step, observation, call.ledger(status));
1968
+ seenOutputs.push(observation);
1969
+ messages.push({ role: "user", content: observations.message(step) + spec.remaining(remaining) });
1970
+ if (!refit()) {
1971
+ exhausted = false;
1972
+ break;
1973
+ }
1974
+ if (repeats >= 2) break;
1975
+ }
1976
+ if (exhausted) {
1977
+ progress?.({ event: "finalize", maxTurns: runtime.max_turns });
1978
+ phaseAt = { maxTurns: runtime.max_turns };
1979
+ messages.push({ role: "user", content: spec.exhausted });
1980
+ if (refit()) {
1981
+ try {
1982
+ const response = await callModel();
1983
+ const finalAction = spec.parseAction(content(response));
1984
+ if (finalAction.action !== "finish") throw new ValidationError("forced final response was not finish");
1985
+ answer = trimSpace(String(finalAction.answer ?? ""));
1986
+ const missing = missingPaths(answer, root);
1987
+ if (missing.length > 0) answer += groundingNote(missing);
1988
+ } catch (error) {
1989
+ if (!isTransportError(error) && !isValidationError(error)) throw error;
1990
+ let diagnostic;
1991
+ [diagnostic, transportError] = chatErrorDetails(error);
1992
+ failure = `agent exceeded ${runtime.max_turns} turns and finalization failed: ${diagnostic}`;
1993
+ }
1994
+ }
1995
+ }
1996
+ if (!answer) answer = failure ?? "agent stopped without a final answer";
1997
+ const report = spec.report ? await spec.report(session) : {};
1998
+ const result = {
1999
+ ...report,
2000
+ final_answer: answer,
2001
+ tool_calls: calls,
2002
+ usage,
2003
+ provider_metrics: {
2004
+ load_ms: metrics.load_ms,
2005
+ prompt_eval_ms: metrics.prompt_eval_ms,
2006
+ eval_ms: metrics.eval_ms
2007
+ },
2008
+ model_metadata: {
2009
+ ...spec.adapterMetadata(session),
2010
+ temperature: chatOptions.temperature,
2011
+ seed: chatOptions.seed,
2012
+ num_ctx: chatOptions.num_ctx,
2013
+ num_predict: chatOptions.num_predict,
2014
+ max_turns: runtime.max_turns,
2015
+ runtime_config: runtime.metadata(),
2016
+ context_management: contextManager.metadata(),
2017
+ context_compactions: contextManager.compactions,
2018
+ transport: managedEndpoint ? "managed" : "direct_ollama",
2019
+ execution_preference: executionPreference,
2020
+ min_placement_evidence: minPlacementEvidence,
2021
+ execution_receipts: executionReceipts,
2022
+ ...transportError ? { transport_error: transportError } : {},
2023
+ cache_token_metrics: cacheUsage.metadata(),
2024
+ calibration_model_identity: calibrationModelIdentity
2025
+ }
2026
+ };
2027
+ observations.writeResult(resultPath, result);
2028
+ print(answer);
2029
+ return failure ? 1 : 0;
2030
+ } finally {
2031
+ session?.close?.();
2032
+ observations.close();
2033
+ }
2034
+ }
2035
+ function withBriefing(prompt, spec, workspace) {
2036
+ try {
2037
+ const briefing = spec.briefing?.(workspace);
2038
+ return briefing ? `${prompt}
2039
+
2040
+ ${briefing}` : prompt;
2041
+ } catch {
2042
+ return prompt;
2043
+ }
2044
+ }
2045
+
2046
+ // src/research/adapter.ts
2047
+ function isPositiveInt(value) {
2048
+ return (typeof value === "boolean" || typeof value === "number" && Number.isInteger(value)) && Number(value) >= 1;
2049
+ }
2050
+
2051
+ // src/research/proc.ts
2052
+ import { spawn, spawnSync } from "node:child_process";
2053
+ import { constants } from "node:os";
2054
+ function decodeText(bytes, source = "command output") {
2055
+ return normalizeNewlines(decodeUtf8(bytes, source));
2056
+ }
2057
+ function runProcess(argv, options) {
2058
+ const timeoutMs = Math.max(options.timeoutSeconds * 1e3, 0);
2059
+ return new Promise((resolve, reject) => {
2060
+ const child = spawn(argv[0], argv.slice(1), {
2061
+ cwd: options.cwd,
2062
+ env: options.env,
2063
+ stdio: ["ignore", "pipe", "pipe"]
2064
+ });
2065
+ const stdout = [];
2066
+ const stderr = [];
2067
+ let settled = false;
2068
+ const finish = (action) => {
2069
+ if (settled) return;
2070
+ settled = true;
2071
+ clearTimeout(timer);
2072
+ action();
2073
+ };
2074
+ const timer = setTimeout(() => {
2075
+ child.kill("SIGKILL");
2076
+ const fail = () => finish(() => reject(new ToolTimeoutError(
2077
+ `command ${JSON.stringify(argv.at(-1))} timed out after ${options.timeoutSeconds} seconds`
2078
+ )));
2079
+ if (child.exitCode !== null || child.signalCode !== null) fail();
2080
+ else child.once("exit", fail);
2081
+ }, timeoutMs);
2082
+ child.stdout.on("data", (chunk) => stdout.push(chunk));
2083
+ child.stderr.on("data", (chunk) => stderr.push(chunk));
2084
+ child.once("error", (error) => finish(() => reject(error)));
2085
+ child.once("close", (code, signal) => finish(() => {
2086
+ try {
2087
+ resolve({
2088
+ stdout: decodeText(Buffer.concat(stdout)),
2089
+ stderr: decodeText(Buffer.concat(stderr)),
2090
+ returncode: code ?? -(signal ? constants.signals[signal] ?? 1 : 1)
2091
+ });
2092
+ } catch (error) {
2093
+ reject(error);
2094
+ }
2095
+ }));
2096
+ });
2097
+ }
2098
+
2099
+ // ../rust-core/src/model_catalog.json
2100
+ var model_catalog_default = {
2101
+ snapshot: "2026-11-01",
2102
+ measured_on: "Apple M2 Pro, 32 GB, Ollama 0.40.13, with other work running",
2103
+ 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.",
2104
+ jobs: [
2105
+ { job: "classify", title: "Classify, triage, yes/no", call: 'run_task {task:"decision"}', cookbook: "cookbooks/classify", picks: [
2106
+ { 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" },
2107
+ { 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" }
2108
+ ] },
2109
+ { job: "find-code", title: "Find the code for an issue", call: 'run_task {task:"embedding", nearest:{query}}', cookbook: "cookbooks/find-code", picks: [
2110
+ { 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" },
2111
+ { 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" }
2112
+ ] },
2113
+ { job: "duplicates", title: "Find duplicates", call: 'run_task {task:"embedding", nearest:{}}', cookbook: "cookbooks/duplicates", picks: [
2114
+ { tag: "nomic-embed-text:v1.5", role: "default", file_gb: 0.3, memory_gb: 1.4, quality: "measured", evidence: "8/12 real issue pairs" }
2115
+ ] },
2116
+ { job: "ocr", title: "Read text from an image", call: 'run_task {task:"vision", files:[path]}', cookbook: "cookbooks/ocr", picks: [
2117
+ { 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" },
2118
+ { 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" }
2119
+ ] },
2120
+ { job: "image-recognition", title: "Understand what an image shows", call: 'run_task {task:"vision", files:[path], prompt}', cookbook: "cookbooks/image-recognition", picks: [
2121
+ { 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" },
2122
+ { 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" },
2123
+ { 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" }
2124
+ ] },
2125
+ { job: "image-search", title: "Find an image by describing it", call: 'run_task {task:"embedding", files:[images], nearest:{query}}', cookbook: "cookbooks/image-search", picks: [
2126
+ { 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" }
2127
+ ] },
2128
+ { job: "speech-to-text", title: "Recording to text", call: 'audio {action:"transcribe", file}', cookbook: "cookbooks/speech-to-text", picks: [
2129
+ { 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" },
2130
+ { 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" }
2131
+ ] },
2132
+ { job: "text-to-speech", title: "Text to speech", call: 'audio {action:"speak", text}', cookbook: "cookbooks/text-to-speech", picks: [
2133
+ { 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" },
2134
+ { 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" },
2135
+ { 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" }
2136
+ ] },
2137
+ { job: "coding", title: "Small coding tasks", call: 'run_task {task:"coding", files:[path]}', cookbook: "cookbooks/coding", picks: [
2138
+ { 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" },
2139
+ { 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" },
2140
+ { 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" },
2141
+ { 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" }
2142
+ ] },
2143
+ { job: "research-agent", title: "Research a repository", call: 'delegate_research {adapter:"octocode" or "github"}', cookbook: "cookbooks/research-agent", picks: [
2144
+ { 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" },
2145
+ { 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" },
2146
+ { 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" }
2147
+ ] }
2148
+ ]
2149
+ };
2150
+
2151
+ // src/research/octocode-instructions.ts
2152
+ var OCTOCODE_LOCAL_INSTRUCTIONS = `Octocode is a read-only code research CLI: it finds code in a directory so you read a few exact lines, not whole files.
2153
+ Call: octocode <tool> '{"queries":[{...}]}' (1-5 queries per call; paths relative to the working directory)
2154
+ Result: lines of findings, then "#" notes. Exit 0 found, 1 nothing found, 2 bad input (the message names the field), 3 path not found.
2155
+ More: copy a printed {"cursor":"..."} as the whole input. No cursor means the result is complete.
2156
+
2157
+ structureSearch: see the layout or list files, when you do not know where to look.
2158
+ {"operation":"tree","path":".","maxDepth":2}
2159
+ {"operation":"files","path":".","include":["**/*.rs"],"view":"paths"}
2160
+ localSearch: find a word, name or error text, when you do not know the file.
2161
+ {"path":".","matchString":["memory_holders"],"view":"files"} which files
2162
+ {"path":".","matchString":["memory_holders","holders"]} matching lines with line numbers; several words mean any of them
2163
+ localFetch: read a file an earlier result named (src/a.rs below is a placeholder); ask for the least that answers.
2164
+ {"path":"src/a.rs","view":"symbols"} outline with line numbers
2165
+ {"path":"src/a.rs","ranges":[{"startLine":40,"endLine":90}]}
2166
+ {"path":"src/a.rs","matchString":["fn run"],"contextLines":{"before":2,"after":15}}
2167
+ astSearch: find code by shape, not by text. Use it to find where something is declared.
2168
+ {"operation":"symbols","path":".","symbolFilter":{"terms":["run"],"mode":"exact"}} declarations named run
2169
+ {"operation":"match","path":".","rule":{"pattern":"eval($X)"}} every call of this shape
2170
+ lspSearch: who defines, uses or calls a symbol; exact where text search only guesses. Take path and line from an earlier result, never guess them.
2171
+ {"path":"src/a.rs","symbolName":"run","line":35} its definition
2172
+ {"operation":"references","path":"src/a.rs","symbolName":"run","line":35,"view":"files"} where it is used
2173
+ {"operation":"callers","path":"src/a.rs","symbolName":"run","line":35} also callees, hover, documentSymbols
2174
+
2175
+ Work smart:
2176
+ 1. Narrow before reading: list files or matching lines first, then read only those ranges.
2177
+ 2. Try several words at once: put synonyms in one matchString, or several queries in one call.
2178
+ 3. Build and dependency folders are skipped already; add "testCode":"exclude" to skip tests.
2179
+ 4. Nothing found proves little: widen the words or the path before saying something does not exist. If lspSearch is empty, use localSearch.
2180
+ 5. Answer with file:line evidence, and stop as soon as the question is answered.
2181
+ `;
2182
+ var OCTOCODE_GITHUB_INSTRUCTIONS = `Octocode's GitHub tools research public and private repositories without cloning them: find a repo, its code, its history, and its packages.
2183
+ Call: octocode <tool> '{"queries":[{...}]}' (1-5 queries per call)
2184
+ Result: JSON with results[].data. Exit 0 found, 1 nothing found, 2 bad input (the message names the field), 3 not found, 4 not logged in (octocode auth login), 7 rate limited (wait).
2185
+ More: follow a printed next or {"cursor":"..."}; no next and no hasMore means the result is complete.
2186
+
2187
+ ghSearchRepo: find a repository when you do not know its owner or name.
2188
+ {"operation":"search","keywords":["local llm router"],"filters":{"language":"Rust"}}
2189
+ {"operation":"get","owner":"ollama","repo":"ollama"} one repo: description, stars, default branch
2190
+ artifactSearch: find a package's repository and versions.
2191
+ {"operation":"resolve","ecosystem":"npm","packageName":"zod"} also pypi, maven
2192
+ ghStructure: see a repo's layout; operation "refs" lists branches, "languages" its languages.
2193
+ {"operation":"tree","owner":"ollama","repo":"ollama","path":"server","maxDepth":2}
2194
+ ghSearchCode: find a word or name inside a repo when you do not know the file.
2195
+ {"owner":"ollama","repo":"ollama","keywords":["SystemOneRequest"]} returns paths, lines and a commitSha
2196
+ ghGetFileContent: read a file you found; ask for the least that answers.
2197
+ {"owner":"ollama","repo":"ollama","path":"api/types.go","view":"symbols"} outline
2198
+ {"owner":"ollama","repo":"ollama","path":"api/types.go","ranges":[{"startLine":600,"endLine":640}]}
2199
+ {"owner":"ollama","repo":"ollama","path":"api/types.go","matchString":["type SystemOneRequest"]}
2200
+ ghSearchHistory: find why something changed: pull requests, issues, commits.
2201
+ {"operation":"pullRequest","owner":"ollama","repo":"ollama","keywords":["systemone"]}
2202
+ {"operation":"commit","owner":"ollama","repo":"ollama","keywords":["decision"]} or operation "issue" with "filters":{"state":"open"}
2203
+ ghGetHistoryItem: read one pull request, or issue, you found.
2204
+ {"operation":"pullRequest","owner":"ollama","repo":"ollama","number":18606,"sections":["body","files"]}
2205
+
2206
+ Work smart:
2207
+ 1. Narrow: repo, then search, then read only the ranges you need. Never read whole files first.
2208
+ 2. Reuse what a result returns: its path, commitSha (as ref) and the next step it suggests.
2209
+ 3. Search words are exact code words, not sentences: one to three identifiers beat a question.
2210
+ 4. Nothing found proves little: try another word or check the repo name before saying it does not exist.
2211
+ 5. Answer with owner/repo, path:line or PR number as evidence, and stop as soon as the question is answered.
2212
+ `;
2213
+
2214
+ // src/local-prompts.ts
2215
+ var catalog = model_catalog_default;
2216
+ function picksOf(job) {
2217
+ return catalog.jobs.find((entry) => entry.job === job)?.picks.map((pick) => pick.tag) ?? [];
2218
+ }
2219
+ var cookbook = (name) => `docs/cookbooks/${name}.md`;
2220
+ var SOURCES = {
2221
+ miniCpmOcr: ["https://huggingface.co/openbmb/MiniCPM-OCR-1.0", "https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf"],
2222
+ glmOcr: ["https://ollama.com/library/glm-ocr", "https://huggingface.co/zai-org/GLM-OCR"],
2223
+ lfm3Vl: ["https://huggingface.co/LiquidAI/LFM3-VL-2B", "https://huggingface.co/LiquidAI/LFM3-VL-2B-GGUF", "https://ollama.com/library/lfm3-vl"],
2224
+ lfm3Omni: ["https://huggingface.co/LiquidAI/LFM3-Omni-2B", "https://ollama.com/library/lfm3-omni"],
2225
+ embeddingGemma: ["https://huggingface.co/google/embeddinggemma-2", "https://ai.google.dev/gemma/docs/embeddinggemma/inference-embeddinggemma-with-sentence-transformers"],
2226
+ nomic: ["https://huggingface.co/nomic-ai/nomic-embed-text-v1.5"],
2227
+ strandsDecider: ["https://ollama.com/library/strands-decider", "https://raw.githubusercontent.com/strands-labs/strands-decider/main/README.md"],
2228
+ nimble: ["https://ollama.com/library/nimble-v3:latest"],
2229
+ bonsai9b: ["https://huggingface.co/prism-ml/Ternary-Bonsai-2-9B-gguf", "https://ollama.com/library/bonsai-2:9b"],
2230
+ bonsai27b: ["https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-gguf", "https://ollama.com/library/bonsai-2"],
2231
+ lfm3: ["https://huggingface.co/LiquidAI/LFM3-1.7B", "https://ollama.com/library/lfm3"],
2232
+ nemotronBolt: ["https://huggingface.co/nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-4GB"],
2233
+ lfm3Audio: ["https://huggingface.co/LiquidAI/LFM3-Audio-2B", "https://ollama.com/library/lfm3-audio"],
2234
+ qwen38: ["https://huggingface.co/Qwen/Qwen3.8-27B", "https://ollama.com/library/qwen3.8"],
2235
+ qwen35: ["https://huggingface.co/Qwen/Qwen3.5-4B", "https://ollama.com/library/qwen3.5"],
2236
+ phonon: ["https://ollama.com/library/phonon:en"]
2237
+ };
2238
+ var EMBEDDING_PREFIXES = {
2239
+ embeddinggemma: {
2240
+ query: "task: search result | query: ",
2241
+ document: "title: none | text: ",
2242
+ codeQuery: "task: code retrieval | query: ",
2243
+ similarity: "task: sentence similarity | query: ",
2244
+ clustering: "task: clustering | query: ",
2245
+ classification: "task: classification | query: "
2246
+ },
2247
+ // A prefix is required; the card names clustering: for removing semantic duplicates.
2248
+ "nomic-embed-text": { query: "search_query: ", document: "search_document: ", clustering: "clustering: ", classification: "classification: " }
2249
+ };
2250
+ var GEMMA = EMBEDDING_PREFIXES.embeddinggemma;
2251
+ var NOMIC = EMBEDDING_PREFIXES["nomic-embed-text"];
2252
+ var OCR_PROMPTS = { text: "Text Recognition:", table: "Table Recognition:", formula: "Formula Recognition:" };
2253
+ var OCR_TRAINED = ["minicpm-ocr", "glm-ocr"];
2254
+ var OCR_PLAIN = {
2255
+ text: "Transcribe all text in the image exactly, line by line. Output only the text.",
2256
+ table: "Transcribe the table in the image as a Markdown table. Output only the table."
2257
+ };
2258
+ var ocrPrompt = (kind, model) => {
2259
+ const name = (model ?? "").toLowerCase().replace(/^(?:hf\.co|huggingface\.co)\/[^/]+\//, "");
2260
+ return model === void 0 || OCR_TRAINED.some((family) => name.startsWith(family)) ? OCR_PROMPTS[kind] : OCR_PLAIN[kind];
2261
+ };
2262
+ var EDIT_FORMAT = `Return each change to an existing file as the file's path (copied from its "--- file:" line), then:
2263
+ <<<<<<< SEARCH
2264
+ exact current lines (5 to 30, unique in the file, indentation exact)
2265
+ =======
2266
+ replacement lines
2267
+ >>>>>>> REPLACE
2268
+ 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.`;
2269
+ var NO_TOOLS = "You have no tools and cannot open files: everything you need is in this message. Answer in plain text.";
2270
+ var WORKFLOWS = {
2271
+ "read-text": {
2272
+ job: "ocr",
2273
+ title: "Read all text in an image, exactly",
2274
+ input: "files: the images",
2275
+ prompt: (_request, model) => ocrPrompt("text", model)
2276
+ },
2277
+ "read-table": {
2278
+ job: "ocr",
2279
+ title: "Read a table in an image as Markdown",
2280
+ input: "files: the images",
2281
+ prompt: (_request, model) => ocrPrompt("table", model)
2282
+ },
2283
+ "read-fields": {
2284
+ job: "ocr",
2285
+ title: "Pull named fields from a receipt, invoice or form image as JSON",
2286
+ input: "files: the images; prompt: the field names",
2287
+ 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.`
2288
+ },
2289
+ "answer-image": {
2290
+ job: "image-recognition",
2291
+ title: "Answer a question about an image",
2292
+ input: "files: the images; prompt: the question",
2293
+ system: `Answer the question about the image.
2294
+ 1. Find the part of the image the question is about.
2295
+ 2. Read any text there exactly as printed.
2296
+ 3. Answer with just the value or label, or one or two sentences.
2297
+ If the image does not show it, answer "not visible". Never guess at what is cut off.`
2298
+ },
2299
+ "describe-screen": {
2300
+ job: "image-recognition",
2301
+ title: "Describe a screenshot for a developer: text, errors, state",
2302
+ input: "files: the screenshot; prompt: what to look for (optional)",
2303
+ system: `You describe a software screenshot for a developer, in short bullets, in this order:
2304
+ - the app or page shown;
2305
+ - headings and main text, quoted exactly;
2306
+ - any error, warning or empty state, quoted exactly;
2307
+ - the state of the controls that matter (selected, disabled, filled in);
2308
+ - anything that looks broken or misaligned.
2309
+ Describe only what is visible.`
2310
+ },
2311
+ "change-code": {
2312
+ job: "coding",
2313
+ title: "Make one small code change",
2314
+ input: "files: the file to change; prompt: the change",
2315
+ system: `${NO_TOOLS}
2316
+ You make one small code change.
2317
+ 1. Find where the change belongs in the code shown.
2318
+ 2. Follow the file's existing names, style and error handling.
2319
+ 3. Change as few lines as the task needs; touch nothing else.
2320
+ ${EDIT_FORMAT}
2321
+ Explain nothing unless asked. If the task is a question, answer it in a few lines.`
2322
+ },
2323
+ "fix-bug": {
2324
+ prefer: "capable",
2325
+ job: "coding",
2326
+ title: "Fix a bug with the smallest change",
2327
+ input: "files: the suspect code; prompt: the error message or symptom",
2328
+ system: `${NO_TOOLS}
2329
+ You fix one bug with the smallest change.
2330
+ 1. Read the error or symptom, then the code shown.
2331
+ 2. Name the cause in one sentence, quoting the line that causes it.
2332
+ 3. Change only what the fix needs.
2333
+ Answer with the cause line, then the edits.
2334
+ ${EDIT_FORMAT}
2335
+ If the code shown cannot cause the symptom, say so and name the file or function to look at instead.`
2336
+ },
2337
+ implement: {
2338
+ prefer: "capable",
2339
+ job: "coding",
2340
+ title: "Add a small feature to existing code",
2341
+ input: "files: the code; prompt: the feature",
2342
+ system: `${NO_TOOLS}
2343
+ You add one small feature to the code shown.
2344
+ 1. Find the function or place it belongs; reuse helpers the file already has.
2345
+ 2. Follow the file's names, style and error handling.
2346
+ 3. Handle the empty and error cases the feature can meet.
2347
+ ${EDIT_FORMAT}
2348
+ End with one line: what to test.`
2349
+ },
2350
+ "write-tests": {
2351
+ prefer: "capable",
2352
+ job: "coding",
2353
+ title: "Write unit tests for the code shown",
2354
+ input: "files: the code (and an existing test, for style); prompt: what to cover (optional)",
2355
+ system: `${NO_TOOLS}
2356
+ You write unit tests for the code shown; you never change that code.
2357
+ 1. List the behaviours: the normal case, edge cases (empty, zero, missing, boundary) and one failure case.
2358
+ 2. Use the test framework and import style the project already uses; copy them from the files shown.
2359
+ 3. Write one small test per behaviour, named after it, with literal expected values.
2360
+ Answer with the test file's path on one line, then the whole file in one fenced block.`
2361
+ },
2362
+ "review-code": {
2363
+ prefer: "capable",
2364
+ job: "coding",
2365
+ title: "Find real defects in code (not style)",
2366
+ input: "files: the code; prompt: what to focus on (optional)",
2367
+ system: `${NO_TOOLS}
2368
+ 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.
2369
+ For each defect write one line: path:line - the problem - the fix.
2370
+ Report at most 5, the surest first. If you find none, answer "no defects found". Never invent a line you did not see.`
2371
+ },
2372
+ "explain-code": {
2373
+ job: "coding",
2374
+ title: "Explain code to someone about to change it",
2375
+ input: "files: the code; prompt: what to explain (optional)",
2376
+ system: `${NO_TOOLS}
2377
+ Explain the code shown to a developer who is about to change it, in at most 6 bullets:
2378
+ 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.
2379
+ Name functions and variables exactly.`
2380
+ },
2381
+ "summarize-log": {
2382
+ think: false,
2383
+ prefer: "capable",
2384
+ job: "summarize",
2385
+ title: "Summarize a log: errors grouped, counted, first failure",
2386
+ input: "files: the log; prompt: what you are investigating (optional)",
2387
+ system: `${NO_TOOLS}
2388
+ You read a log for a developer. Report:
2389
+ 1. Errors and warnings, grouped by message, each with its count and first and last timestamp.
2390
+ 2. The first error in time order, quoted exactly.
2391
+ 3. The likely cause, only if the log shows it.
2392
+ Quote messages exactly, skip routine lines, at most 10 bullets.`
2393
+ },
2394
+ "summarize-text": {
2395
+ think: false,
2396
+ job: "summarize",
2397
+ title: "Summarize text for someone who will act on it",
2398
+ input: "files: the text; prompt: what you need from it (optional)",
2399
+ system: `${NO_TOOLS}
2400
+ 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.
2401
+ Quote numbers, names, versions and paths exactly. Write "not stated" when the text does not say; never guess. No introduction or closing.`
2402
+ },
2403
+ "summarize-diff": {
2404
+ think: false,
2405
+ prefer: "capable",
2406
+ job: "summarize",
2407
+ title: "Summarize a diff for a reviewer: changes and risks",
2408
+ input: "files: the diff or patch",
2409
+ system: `${NO_TOOLS}
2410
+ You summarize a code diff for a reviewer, in at most 8 bullets:
2411
+ 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.
2412
+ Name files, functions and settings exactly.`
2413
+ },
2414
+ "extract-json": {
2415
+ think: false,
2416
+ job: "summarize",
2417
+ title: "Extract named fields from text into JSON",
2418
+ input: "files: the text; prompt: the field names (or a JSON schema)",
2419
+ system: `${NO_TOOLS}
2420
+ Extract the fields the request names from the text into one JSON object.
2421
+ Use the values exactly as written in the text; use null for a field the text does not give; never invent a value.
2422
+ Output only the JSON object.`,
2423
+ format: "json"
2424
+ }
2425
+ };
2426
+ var WORKFLOW_NAMES = Object.keys(WORKFLOWS);
2427
+ var BONSAI_9B = {
2428
+ think: false,
2429
+ options: { temperature: 0.7, top_p: 0.8, top_k: 20, min_p: 0, presence_penalty: 1.5, repeat_penalty: 1 },
2430
+ 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.",
2431
+ sources: SOURCES.bonsai9b
2432
+ };
2433
+ var qwenNonThinking = (sources) => ({
2434
+ think: false,
2435
+ options: { temperature: 0.7, top_p: 0.8, top_k: 20, min_p: 0, presence_penalty: 1.5, repeat_penalty: 1 },
2436
+ 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.",
2437
+ sources
2438
+ });
2439
+ var CHAT_MODELS = {
2440
+ lfm3: {
2441
+ options: { temperature: 0.1, top_k: 50, repeat_penalty: 1.1 },
2442
+ notes: "Thinks by default; its card calls thinking off inconsistent, so it stays on. Not for agentic coding or multi-step reasoning.",
2443
+ sources: SOURCES.lfm3
2444
+ },
2445
+ "nvidia-nemotron-3.5-bolt": {
2446
+ options: { temperature: 1, top_p: 0.95 },
2447
+ // `ollama show` of the hf.co Q4_K_M tag, 2026-11-08: no thinking capability, and the template
2448
+ // defaults enable_thinking to false.
2449
+ 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.",
2450
+ sources: SOURCES.nemotronBolt
2451
+ },
2452
+ "bonsai-2": BONSAI_9B,
2453
+ // The card documents the same instruct (non-thinking) preset as the 9B; Ollama lists think
2454
+ // values false, low, medium and xhigh for this tag.
2455
+ "bonsai-2:27b": {
2456
+ ...BONSAI_9B,
2457
+ 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.",
2458
+ sources: SOURCES.bonsai27b
2459
+ },
2460
+ "qwen3.8": qwenNonThinking(SOURCES.qwen38),
2461
+ "qwen3.5": qwenNonThinking(SOURCES.qwen35)
2462
+ };
2463
+ var CHAT_SOURCES = [...SOURCES.lfm3, ...SOURCES.nemotronBolt, ...SOURCES.bonsai9b, ...SOURCES.bonsai27b, ...SOURCES.qwen38, ...SOURCES.qwen35];
2464
+ var LOCAL_JOBS = {
2465
+ classify: {
2466
+ job: "classify",
2467
+ task: "decision",
2468
+ catalogJob: "classify",
2469
+ defaultPicks: picksOf("classify"),
2470
+ models: {
2471
+ "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 },
2472
+ "nimble-v3": { notes: "Documents num_ctx 32768 (routing sizes it here); CC BY-NC 4.0, noncommercial.", sources: SOURCES.nimble }
2473
+ },
2474
+ 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.",
2475
+ sources: [...SOURCES.strandsDecider, ...SOURCES.nimble, cookbook("classify")]
2476
+ },
2477
+ "find-code": {
2478
+ job: "find-code",
2479
+ task: "embedding",
2480
+ catalogJob: "find-code",
2481
+ defaultPicks: picksOf("find-code"),
2482
+ needs: "nearest.query: the issue, or what the code does",
2483
+ prefixes: { embeddinggemma: { query: GEMMA.codeQuery, document: GEMMA.document }, "nomic-embed-text": { query: NOMIC.query, document: NOMIC.document } },
2484
+ // Measured 2026-11-08 on the find-code set (10 questions, 1,039 chunks): code retrieval with
2485
+ // title: none 8 first / 9 in top 3; search result 8/8; code retrieval with the file as title 7/9.
2486
+ 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.",
2487
+ sources: [...SOURCES.embeddingGemma, ...SOURCES.nomic, cookbook("find-code")]
2488
+ },
2489
+ duplicates: {
2490
+ job: "duplicates",
2491
+ task: "embedding",
2492
+ catalogJob: "duplicates",
2493
+ defaultPicks: picksOf("duplicates"),
2494
+ // Symmetric: every item, the query included, takes the same prefix.
2495
+ prefixes: {
2496
+ // The measured 8/12 used search_document:; the card's clustering: is documented but unmeasured here.
2497
+ "nomic-embed-text": { query: NOMIC.document, document: NOMIC.document },
2498
+ embeddinggemma: { query: GEMMA.similarity, document: GEMMA.similarity }
2499
+ },
2500
+ 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.",
2501
+ sources: [...SOURCES.nomic, ...SOURCES.embeddingGemma, cookbook("duplicates")]
2502
+ },
2503
+ ocr: {
2504
+ job: "ocr",
2505
+ task: "vision",
2506
+ catalogJob: "ocr",
2507
+ defaultPicks: picksOf("ocr"),
2508
+ prompt: ({ model }) => ocrPrompt("text", model),
2509
+ // Both publishers' Ollama Modelfiles set temperature 0; the hf.co MiniCPM tag ships no parameters.
2510
+ models: {
2511
+ "minicpm-ocr": {
2512
+ options: { temperature: 0 },
2513
+ notes: "Chinese and English; tables come back as HTML. Its Modelfile sets num_ctx 8192.",
2514
+ sources: [...SOURCES.miniCpmOcr, "https://raw.githubusercontent.com/OpenBMB/MiniCPM-OCR/main/docs/ollama_deploy.md"]
2515
+ },
2516
+ "glm-ocr": {
2517
+ options: { temperature: 0 },
2518
+ notes: "Also documents Figure Recognition:.",
2519
+ sources: [...SOURCES.glmOcr, "https://github.com/zai-org/GLM-OCR/blob/main/examples/ollama-deploy/README.md"]
2520
+ }
2521
+ },
2522
+ 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.",
2523
+ sources: [...SOURCES.miniCpmOcr, ...SOURCES.glmOcr, cookbook("ocr")]
2524
+ },
2525
+ "image-recognition": {
2526
+ job: "image-recognition",
2527
+ task: "vision",
2528
+ catalogJob: "image-recognition",
2529
+ defaultPicks: picksOf("image-recognition"),
2530
+ needs: "prompt: your question about the image",
2531
+ systemPrompt: WORKFLOWS["answer-image"].system,
2532
+ models: {
2533
+ "lfm3-vl": {
2534
+ options: { temperature: 0.1, top_p: 1, top_k: 50, min_p: 0, repeat_penalty: 1.1 },
2535
+ 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.",
2536
+ sources: SOURCES.lfm3Vl
2537
+ },
2538
+ "lfm3-omni": {
2539
+ systemPrompt: "Answer questions about the provided image. Base every answer only on what can be seen.",
2540
+ options: { temperature: 0 },
2541
+ notes: "Non-reasoning: never send think:true. Up to 2,048 tokens per image. The card gives 32K context; the Ollama tag advertises 128K.",
2542
+ sources: SOURCES.lfm3Omni
2543
+ },
2544
+ "bonsai-2": BONSAI_9B,
2545
+ "qwen3.8": qwenNonThinking(SOURCES.qwen38),
2546
+ "qwen3.5": qwenNonThinking(SOURCES.qwen35)
2547
+ },
2548
+ notes: 'Ask a specific question, not "describe this". Tested on drawn images only.',
2549
+ sources: [...SOURCES.lfm3Vl, ...SOURCES.lfm3Omni, ...SOURCES.bonsai9b, cookbook("image-recognition")]
2550
+ },
2551
+ "image-search": {
2552
+ job: "image-search",
2553
+ task: "embedding",
2554
+ catalogJob: "image-search",
2555
+ defaultPicks: picksOf("image-search"),
2556
+ needs: "nearest.query: a description of the image",
2557
+ prefixes: { embeddinggemma: { query: GEMMA.query, document: GEMMA.document } },
2558
+ notes: "Images are embedded bare: the prefixes apply to text only. Open the top match.",
2559
+ sources: [...SOURCES.embeddingGemma, cookbook("image-search")]
2560
+ },
2561
+ coding: {
2562
+ job: "coding",
2563
+ task: "coding",
2564
+ catalogJob: "coding",
2565
+ defaultPicks: picksOf("coding"),
2566
+ needs: "prompt: the change to make",
2567
+ systemPrompt: WORKFLOWS["change-code"].system,
2568
+ models: CHAT_MODELS,
2569
+ notes: "The answer is SEARCH/REPLACE blocks: apply one only where its SEARCH text matches exactly once, then run the tests.",
2570
+ sources: [...CHAT_SOURCES, cookbook("coding")]
2571
+ },
2572
+ summarize: {
2573
+ job: "summarize",
2574
+ task: "completion",
2575
+ catalogJob: "coding",
2576
+ defaultPicks: picksOf("coding"),
2577
+ prompt: () => "Summarize the text below.",
2578
+ systemPrompt: WORKFLOWS["summarize-text"].system,
2579
+ models: CHAT_MODELS,
2580
+ notes: "The coding picks: the same chat models apply (docs/cookbooks/batch-text.md). Check every number and name you reuse.",
2581
+ sources: [...CHAT_SOURCES, cookbook("batch-text")]
2582
+ }
2583
+ };
2584
+ var TOOL_JOBS = {
2585
+ "speech-to-text": {
2586
+ tool: "audio",
2587
+ defaultPicks: picksOf("speech-to-text"),
2588
+ // The audio tool calls Ollama's transcription endpoint, which builds the model's prompt; these
2589
+ // are lfm3-audio's documented prompts for a direct chat call.
2590
+ systemPrompts: {
2591
+ transcribe: (language) => language ? `Perform ASR in ${language}.` : "Perform ASR.",
2592
+ translate: (source, target) => `Translate the following speech from ${source} to ${target} in text form.`
2593
+ },
2594
+ notes: "WAV input only. Numbers and names are the usual misses; translation works best to or from English.",
2595
+ sources: [...SOURCES.lfm3Audio, cookbook("speech-to-text")]
2596
+ },
2597
+ "text-to-speech": {
2598
+ tool: "audio",
2599
+ defaultPicks: picksOf("text-to-speech"),
2600
+ notes: "Send plain text: Markdown marks may be read aloud. One voice (default); 24 kHz mono WAV.",
2601
+ sources: [...SOURCES.phonon, cookbook("text-to-speech")]
2602
+ },
2603
+ "research-agent": {
2604
+ tool: "delegate_research",
2605
+ defaultPicks: picksOf("research-agent"),
2606
+ notes: "The adapters' system prompts are below. Discard an escalate verdict and verify the citations.",
2607
+ sources: [cookbook("research-agent")]
2608
+ },
2609
+ sweep: {
2610
+ tool: "sweep",
2611
+ defaultPicks: picksOf("classify"),
2612
+ /** The decision state of one chunk, and the explain step's user message for a confirmed flag. */
2613
+ state: (label, text) => `Source: ${label}
2614
+
2615
+ ${text}`,
2616
+ explain: (prompt, label, text) => `${prompt}
2617
+
2618
+ Source: ${label}
2619
+
2620
+ ${text}`,
2621
+ // FreeLlama's own setting for the short explain note, not a model's documented value.
2622
+ explainControls: { think: false, temperature: 0 },
2623
+ notes: 'One yes/no question per concern, phrased about one chunk ("Does this code ..."); a noul answer flags at p >= 0.5.',
2624
+ sources: [cookbook("sweep")]
2625
+ }
2626
+ };
2627
+ var ACTION_PROTOCOL = (example) => `You do not run a shell: ignore the "Call: octocode ..." form, the exit codes and the login hint above. Put "cursor" inside a query to read more of a tool's result; use the page action only for stored output of your own earlier step.
2628
+ Return exactly one JSON object per turn, and nothing else:
2629
+ Run a tool, in place of the shell command above: {"action":"octocode","tool":"${example}","queries":[{...}]}
2630
+ Read another page of an earlier step's stored output: {"action":"page","step":2,"page":2}
2631
+ Finish: {"action":"finish","answer":"concise answer with its evidence"}
2632
+ You see page 1 of each result and the total; a shortened observation is recoverable with page, so never re-run a tool for it.
2633
+ Most questions need 2 to 6 calls. An invalid action is rejected and costs a turn. Never edit or write anything.`;
2634
+ function octocodeSystemPrompt(workspace) {
2635
+ return `You are a local code-research agent in a read-only workspace at ${workspace}. Paths are relative to it.
2636
+
2637
+ ${OCTOCODE_LOCAL_INSTRUCTIONS}
2638
+ ${ACTION_PROTOCOL("localSearch")}`;
2639
+ }
2640
+ var GH_ACTION = `You may also run the GitHub CLI, read-only, as {"action":"gh","args":[...]} (arguments as an array; no shell):
2641
+ {"action":"gh","args":["search","code","SystemOneRequest","--repo","ollama/ollama"]} finds code; add "--filename","types.go" or "--language","go" to narrow it.
2642
+ {"action":"gh","args":["api","repos/ollama/ollama/contents/api/types.go","--jq",".content"]} reads a file (base64), or use ghGetFileContent for decoded text.
2643
+ {"action":"gh","args":["repo","view","colinhacks/zod","--json","name,description,primaryLanguage"]}, ["pr","view","18606","--repo","ollama/ollama"], ["issue","list","--repo","owner/repo","--search","keyword"], ["release","list","--repo","owner/repo"].
2644
+ Only search, repo view/list, pr view/list/diff/checks, issue view/list, release view/list and api (GET) run. Finish with the direct answer first, then "Source: owner/repo/path" or the PR or issue URL.`;
2645
+ var CODER_SYSTEM_PROMPT = [
2646
+ "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.",
2647
+ "",
2648
+ "Actions, in order of preference:",
2649
+ '{"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.',
2650
+ '{"action":"read","path":"src/config.ts","start":40,"end":70} shows numbered lines of one file.',
2651
+ '{"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.',
2652
+ '{"action":"write","path":"src/limits.ts","content":"export const MAX = 10;\\n"} creates a file, or replaces a whole small file.',
2653
+ '{"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.',
2654
+ '{"action":"list","path":"src"} shows files and folders two levels deep.',
2655
+ '{"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.',
2656
+ '{"action":"page","step":2,"page":2} shows the next page of a long earlier result.',
2657
+ '{"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.',
2658
+ "",
2659
+ "How to work:",
2660
+ "1. Search for the names in the task, then read the lines you will change.",
2661
+ "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.",
2662
+ "3. Run an allowed command to test the change. If it fails, read the error, fix the code and run it again.",
2663
+ "4. Finish with what changed, why, and the last test result. With no allowed command, say the change is untested.",
2664
+ "Keep results small: read line ranges, not whole files, and end long shell output with | head -n 40."
2665
+ ].join("\n");
2666
+ function githubSystemPrompt() {
2667
+ return `You are a code-research agent that answers questions about GitHub repositories and packages, read-only.
2668
+
2669
+ ${OCTOCODE_GITHUB_INSTRUCTIONS}
2670
+ ${ACTION_PROTOCOL("ghSearchCode")}
2671
+ ${GH_ACTION}`;
2672
+ }
2673
+ 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.";
2674
+ var AGENT_GUIDE = `Use FreeLlama to run bounded work on local models and keep it out of your context.
2675
+
2676
+ 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.
2677
+
2678
+ 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.
2679
+
2680
+ 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".
2681
+
2682
+ Coding: the answer is SEARCH/REPLACE blocks. ${APPLY_EDITS}
2683
+
2684
+ 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].
2685
+
2686
+ 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.`;
2687
+
2688
+ // src/research/workspace-overview.ts
2689
+ import { readdirSync as readdirSync2 } from "node:fs";
2690
+ import path6 from "node:path";
2691
+ var SKIP = /* @__PURE__ */ new Set(["node_modules", "target", ".git", "dist", "build", ".venv", "venv", "vendor", "__pycache__", ".octocode", "site-packages", "coverage"]);
2692
+ var MAX_ENTRIES = 2e4;
2693
+ var MAX_DEPTH = 8;
2694
+ var SOURCE_DIRS = /* @__PURE__ */ new Set(["src", "lib", "app", "source", "pkg", "cmd", "internal", "crates"]);
2695
+ function workspaceOverview(root) {
2696
+ let top;
2697
+ try {
2698
+ top = readdirSync2(root, { withFileTypes: true });
2699
+ } catch {
2700
+ return "";
2701
+ }
2702
+ const extensions = /* @__PURE__ */ new Map();
2703
+ const sourceRoots = /* @__PURE__ */ new Map();
2704
+ let files = 0;
2705
+ let seen = 0;
2706
+ const walk = (dir, depth, sourceRoot) => {
2707
+ if (depth > MAX_DEPTH || seen > MAX_ENTRIES) return;
2708
+ let entries;
2709
+ try {
2710
+ entries = readdirSync2(dir, { withFileTypes: true });
2711
+ } catch {
2712
+ return;
2713
+ }
2714
+ for (const entry of entries) {
2715
+ if (++seen > MAX_ENTRIES) return;
2716
+ if (entry.isDirectory()) {
2717
+ if (SKIP.has(entry.name) || entry.name.startsWith(".")) continue;
2718
+ const child = path6.join(dir, entry.name);
2719
+ walk(child, depth + 1, sourceRoot ?? (SOURCE_DIRS.has(entry.name) ? path6.relative(root, child) : void 0));
2720
+ } else if (entry.isFile()) {
2721
+ files += 1;
2722
+ const extension = path6.extname(entry.name).toLowerCase() || "(none)";
2723
+ extensions.set(extension, (extensions.get(extension) ?? 0) + 1);
2724
+ if (sourceRoot) sourceRoots.set(sourceRoot, (sourceRoots.get(sourceRoot) ?? 0) + 1);
2725
+ }
2726
+ }
2727
+ };
2728
+ walk(root, 0);
2729
+ 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);
2730
+ const types = [...extensions.entries()].sort((left, right) => right[1] - left[1]).slice(0, 8).map(([extension, count]) => `${extension} ${count}`);
2731
+ const sources = [...sourceRoots.entries()].sort((left, right) => right[1] - left[1]).slice(0, 8).map(([dir, count]) => `${dir}/ (${count})`);
2732
+ 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.";
2733
+ }
2734
+
2735
+ // src/research/gh-action.ts
2736
+ var READ_ONLY = {
2737
+ search: /* @__PURE__ */ new Set(["code", "repos", "issues", "prs", "commits"]),
2738
+ repo: /* @__PURE__ */ new Set(["view", "list"]),
2739
+ pr: /* @__PURE__ */ new Set(["view", "list", "diff", "checks"]),
2740
+ issue: /* @__PURE__ */ new Set(["view", "list"]),
2741
+ release: /* @__PURE__ */ new Set(["view", "list"]),
2742
+ api: /* @__PURE__ */ new Set()
2743
+ };
2744
+ var FORBIDDEN = /^(?:-X|--method|-f|-F|--field|--raw-field|--input|-H|--header|-w|--web|--browser|-e|--edit|--hostname|--paginate-all)(?:=|$)/;
2745
+ var MAX_ARGS = 40;
2746
+ function validateGhArgs(args) {
2747
+ if (!Array.isArray(args) || args.length === 0 || args.length > MAX_ARGS || !args.every((arg) => typeof arg === "string" && !arg.includes("\n"))) {
2748
+ throw new ValidationError(`gh action requires "args": an array of 1-${MAX_ARGS} strings, e.g. ["search","code","SystemOneRequest","--repo","ollama/ollama"]`);
2749
+ }
2750
+ const words = args;
2751
+ const [group, sub] = words;
2752
+ const allowed = READ_ONLY[group];
2753
+ if (!allowed) throw new ValidationError(`gh ${group} is not allowed; read-only commands: search, repo view/list, pr view/list/diff/checks, issue view/list, release view/list, api (GET)`);
2754
+ if (allowed.size > 0 && !allowed.has(sub ?? "")) {
2755
+ throw new ValidationError(`gh ${group} ${sub ?? ""} is not read-only; allowed: ${[...allowed].map((name) => `gh ${group} ${name}`).join(", ")}`);
2756
+ }
2757
+ const bad = words.find((word) => FORBIDDEN.test(word));
2758
+ if (bad) throw new ValidationError(`gh flag ${bad} is not allowed: the gh action only reads`);
2759
+ if (group === "api" && (!sub || sub.startsWith("-") || /graphql/i.test(sub))) {
2760
+ throw new ValidationError("gh api needs a REST path such as repos/ollama/ollama/contents/api/types.go (GET only; no graphql)");
2761
+ }
2762
+ return words;
2763
+ }
2764
+ async function runGh(args, cwd, timeoutSeconds) {
2765
+ const result = await runProcess(["gh", ...validateGhArgs(args)], {
2766
+ cwd,
2767
+ // gh reads its login from the operator's config and keychain; nothing prompts.
2768
+ env: { ...process.env, GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1", NO_COLOR: "1", GH_PAGER: "cat" },
2769
+ timeoutSeconds
2770
+ });
2771
+ const output = trimSpace(result.stdout);
2772
+ if (result.returncode !== 0) throw new ToolExitError(result.returncode, `${output}
2773
+ ${trimSpace(result.stderr)}`.trim());
2774
+ return output || "(no results)";
2775
+ }
2776
+
2777
+ // src/research/entry.ts
2778
+ import path7 from "node:path";
2779
+ import { pathToFileURL } from "node:url";
2780
+ function isEntry(moduleUrl, name, script = process.argv[1]) {
2781
+ if (!script || moduleUrl !== pathToFileURL(script).href) return false;
2782
+ return path7.basename(script).replace(/\.[cm]?[jt]s$/, "") === name;
2783
+ }
2784
+
2785
+ // src/research/octocode-agent.ts
2786
+ var OCTOCODE_TOOLS = /* @__PURE__ */ new Set([
2787
+ "structureSearch",
2788
+ "localSearch",
2789
+ "localFetch",
2790
+ "astSearch",
2791
+ "lspSearch"
2792
+ ]);
2793
+ var GITHUB_TOOLS = /* @__PURE__ */ new Set([
2794
+ "ghSearchRepo",
2795
+ "artifactSearch",
2796
+ "ghStructure",
2797
+ "ghSearchCode",
2798
+ "ghGetFileContent",
2799
+ "ghSearchHistory",
2800
+ "ghGetHistoryItem"
2801
+ ]);
2802
+ var MAX_QUERIES = 5;
2803
+ var OCTOCODE_BIN = process.env.FREELLAMA_AGENT_OCTOCODE_BIN ?? "octocode";
2804
+ function safeResolve(root, value) {
2805
+ const candidate = resolvePath(joinPath(root, value));
2806
+ if (escapesRoot(candidate, root)) throw new ValidationError(`path escapes benchmark workspace: ${value}`);
2807
+ if (candidate.split("/").includes(".git")) throw new ValidationError("git metadata is outside the task surface");
2808
+ return candidate;
2809
+ }
2810
+ function queryList(queries) {
2811
+ if (isRecord(queries)) return [queries];
2812
+ return Array.isArray(queries) ? queries.filter(isRecord) : [];
2813
+ }
2814
+ function normalizeAction(value, tools) {
2815
+ const named = typeof value.action === "string" && tools.has(value.action) ? value.action : void 0;
2816
+ if (named === void 0 && value.action !== "octocode") return value;
2817
+ const { action: _action, tool: named2, queries, ...fields } = value;
2818
+ const tool = named ?? named2;
2819
+ if (tool === void 0) return value;
2820
+ const query = queries ?? (Object.keys(fields).length > 0 ? fields : void 0);
2821
+ return { action: "octocode", tool, ...query === void 0 ? {} : { queries: query } };
2822
+ }
2823
+ function actionParser(tools, gh = false) {
2824
+ const names = [...tools].sort().join(", ");
2825
+ return (content) => {
2826
+ const value = normalizeAction(parseJsonAction(content), tools);
2827
+ const action = value.action;
2828
+ if (gh && action === "gh") {
2829
+ validateGhArgs(value.args);
2830
+ return value;
2831
+ }
2832
+ if (action === "octocode") {
2833
+ const tool = value.tool;
2834
+ if (typeof tool !== "string" || !tools.has(tool)) {
2835
+ throw new ValidationError(`octocode action requires one supported tool: ${names}`);
2836
+ }
2837
+ const queries = queryList(value.queries);
2838
+ if (queries.length === 0 || queries.length > MAX_QUERIES) {
2839
+ throw new ValidationError(`octocode action requires queries: 1 to ${MAX_QUERIES} query objects`);
2840
+ }
2841
+ }
2842
+ if (action === "page" && (!isPositiveInt(value.step) || !isPositiveInt(value.page))) {
2843
+ throw new ValidationError("page action requires positive integer step and page");
2844
+ }
2845
+ if (action === "finish" && typeof value.answer !== "string") {
2846
+ throw new ValidationError("finish action requires a string answer");
2847
+ }
2848
+ if (action !== "octocode" && action !== "page" && action !== "finish") {
2849
+ throw new ValidationError(`unsupported action for octocode adapter: ${String(action)}${gh ? " (use octocode, gh, page or finish)" : ""}`);
2850
+ }
2851
+ return value;
2852
+ };
2853
+ }
2854
+ var parseAction = actionParser(OCTOCODE_TOOLS);
2855
+ async function runCli(root, tool, queries, timeoutSeconds) {
2856
+ const result = await runProcess([OCTOCODE_BIN, tool, JSON.stringify({ queries })], { cwd: root, timeoutSeconds });
2857
+ const parts = [trimSpace(result.stdout), trimSpace(result.stderr)].filter(Boolean);
2858
+ const output = parts.join("\n") || "(empty result)";
2859
+ if (result.returncode !== 0) {
2860
+ throw new ToolExitError(result.returncode, output, result.returncode === 1 ? "empty" : "error");
2861
+ }
2862
+ return output;
2863
+ }
2864
+ async function runOctocode(root, toolName, queries, timeoutSeconds) {
2865
+ if (!OCTOCODE_TOOLS.has(toolName)) throw new ValidationError(`unsupported octocode tool: ${toolName}`);
2866
+ const list = queryList(queries);
2867
+ for (const query of list) {
2868
+ if (typeof query.path === "string") safeResolve(root, query.path);
2869
+ }
2870
+ return runCli(root, toolName, list, timeoutSeconds);
2871
+ }
2872
+ async function runGithub(root, toolName, queries, timeoutSeconds) {
2873
+ if (!GITHUB_TOOLS.has(toolName)) throw new ValidationError(`unsupported GitHub tool: ${toolName}`);
2874
+ return runCli(root, toolName, queryList(queries), timeoutSeconds);
2875
+ }
2876
+ function adapterFor(options) {
2877
+ return {
2878
+ defaultToolTimeoutSeconds: 45,
2879
+ systemPrompt: options.prompt,
2880
+ ...options.briefing ? { briefing: options.briefing } : {},
2881
+ parseAction: actionParser(options.tools, options.gh),
2882
+ open: () => ({
2883
+ toolCall: (action, workspace, runtime) => {
2884
+ if (options.gh && action.action === "gh") {
2885
+ const args = validateGhArgs(action.args);
2886
+ return {
2887
+ signature: { gh: args },
2888
+ run: () => runGh(args, workspace, runtime.tool_timeout_seconds),
2889
+ record: { name: "gh", raw_name: "gh", arguments: { args } },
2890
+ ledger: (status) => ({ action: "gh", status, source: args.join(" ") })
2891
+ };
2892
+ }
2893
+ const toolName = String(action.tool ?? "");
2894
+ const queries = queryList(action.queries);
2895
+ return {
2896
+ signature: { tool: toolName, queries },
2897
+ run: () => options.run(workspace, toolName, queries, runtime.tool_timeout_seconds),
2898
+ record: {
2899
+ name: toolName ? `mcp.octocode.${toolName}` : "mcp.octocode.unknown",
2900
+ raw_name: action.action ?? "",
2901
+ arguments: { tool: toolName, queries }
2902
+ },
2903
+ ledger: (status) => ({ action: toolName, status, source: canonicalJson(queries, { sortKeys: false, ensureAscii: false }) })
2904
+ };
2905
+ }
2906
+ }),
2907
+ remaining: (count) => `
2908
+
2909
+ Tool calls remaining: ${count}. Finish now if the task is answerable; do not repeat prior calls.`,
2910
+ exhausted: "Tool budget is exhausted. Return exactly a finish JSON action now using the evidence collected. Do not request another tool.",
2911
+ adapterMetadata: () => ({ adapter: options.metadata })
2912
+ };
2913
+ }
2914
+ var OCTOCODE_ADAPTER = adapterFor({ tools: OCTOCODE_TOOLS, prompt: octocodeSystemPrompt, run: runOctocode, metadata: "octocode_cli_agent_v2", briefing: workspaceOverview });
2915
+ var GITHUB_ADAPTER = adapterFor({ tools: GITHUB_TOOLS, prompt: githubSystemPrompt, run: runGithub, metadata: "octocode_github_agent_v2", gh: true });
2916
+ if (isEntry(import.meta.url, "octocode-agent")) {
2917
+ process.exitCode = await runAdapter(OCTOCODE_ADAPTER);
2918
+ }
2919
+
2920
+ // src/research/github-agent.ts
2921
+ if (isEntry(import.meta.url, "github-agent")) {
2922
+ process.exitCode = await runAdapter(GITHUB_ADAPTER);
2923
+ }