@octocodeai/freellama-mcp-server 0.7.0 → 0.10.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.
- package/CONFIGURATION.md +11 -3
- package/DEVELOPING.md +6 -6
- package/README.md +267 -403
- package/dist/index.js +7186 -1601
- package/dist/research/bash-agent.js +1504 -71
- package/dist/research/coder-agent.js +4677 -0
- package/dist/research/github-agent.js +2932 -0
- package/dist/research/octocode-agent.js +1416 -116
- package/docs/ARCHITECTURE.md +101 -45
- package/docs/BUILD_AND_NOTES.md +31 -0
- package/docs/CLI.md +76 -27
- package/docs/COMPARISON.md +40 -0
- package/docs/CPU_GPU_ROUTING.md +127 -25
- package/docs/DEFERRED_JOBS.md +79 -0
- package/docs/FEATURES.md +303 -0
- package/docs/LOCAL_MODELS.md +190 -0
- package/docs/MODEL_FORMS_AND_RESULTS.md +55 -0
- package/docs/MODEL_INTROSPECTION.md +128 -0
- package/docs/MODEL_SELECTION.md +75 -23
- package/docs/MONITORING.md +115 -33
- package/docs/PRODUCTION.md +74 -11
- package/docs/README.md +11 -6
- package/docs/SCOPES_AND_WARMING.md +55 -7
- package/docs/TESTING.md +24 -2
- package/docs/TUNING.md +86 -0
- package/docs/cookbooks/README.md +22 -0
- package/docs/cookbooks/batch-text.md +19 -0
- package/docs/cookbooks/classify.md +25 -0
- package/docs/cookbooks/coding.md +17 -0
- package/docs/cookbooks/duplicates.md +17 -0
- package/docs/cookbooks/find-code.md +21 -0
- package/docs/cookbooks/image-recognition.md +15 -0
- package/docs/cookbooks/image-search.md +13 -0
- package/docs/cookbooks/jobs.md +60 -0
- package/docs/cookbooks/memory-and-caching.md +31 -0
- package/docs/cookbooks/ocr.md +17 -0
- package/docs/cookbooks/research-agent.md +27 -0
- package/docs/cookbooks/speech-to-text.md +19 -0
- package/docs/cookbooks/sweep.md +66 -0
- package/docs/cookbooks/text-to-speech.md +15 -0
- package/docs/octocode/GITHUB.md +30 -0
- package/docs/octocode/LOCAL.md +29 -0
- package/native/index.d.ts +0 -11
- package/native/index.js +18 -34
- package/package.json +6 -9
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
// src/research/octocode-agent.ts
|
|
2
|
-
import { pathToFileURL } from "node:url";
|
|
3
|
-
|
|
4
1
|
// src/research/adapter.ts
|
|
5
2
|
function isPositiveInt(value) {
|
|
6
3
|
return (typeof value === "boolean" || typeof value === "number" && Number.isInteger(value)) && Number(value) >= 1;
|
|
7
4
|
}
|
|
8
5
|
|
|
9
6
|
// src/research/agent-loop.ts
|
|
10
|
-
import { readFileSync as
|
|
7
|
+
import { readFileSync as readFileSync5 } from "node:fs";
|
|
11
8
|
import { performance } from "node:perf_hooks";
|
|
12
9
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
13
10
|
|
|
@@ -57,8 +54,21 @@ function isSystemError(error) {
|
|
|
57
54
|
var ToolTimeoutError = class extends Error {
|
|
58
55
|
name = "ToolTimeoutError";
|
|
59
56
|
};
|
|
57
|
+
var ToolExitError = class extends Error {
|
|
58
|
+
constructor(exitCode, output, status = "error") {
|
|
59
|
+
super(`${status === "empty" ? "nothing found" : "command failed"} (exit code ${exitCode})` + (output ? `
|
|
60
|
+
${output}` : ""));
|
|
61
|
+
this.exitCode = exitCode;
|
|
62
|
+
this.output = output;
|
|
63
|
+
this.status = status;
|
|
64
|
+
}
|
|
65
|
+
exitCode;
|
|
66
|
+
output;
|
|
67
|
+
status;
|
|
68
|
+
name = "ToolExitError";
|
|
69
|
+
};
|
|
60
70
|
function isToolError(error) {
|
|
61
|
-
return error instanceof ValidationError || error instanceof ToolTimeoutError || error instanceof SyntaxError || isSystemError(error);
|
|
71
|
+
return error instanceof ValidationError || error instanceof ToolTimeoutError || error instanceof ToolExitError || error instanceof SyntaxError || isSystemError(error);
|
|
62
72
|
}
|
|
63
73
|
|
|
64
74
|
// src/research/actions.ts
|
|
@@ -362,27 +372,44 @@ var estimateText = (value) => canonicalJson(value, { sortKeys: false, ensureAsci
|
|
|
362
372
|
function estimateTokens(text, charsPerToken = CHARS_PER_TOKEN) {
|
|
363
373
|
return Math.floor(cpLength(text) / charsPerToken) + 1;
|
|
364
374
|
}
|
|
365
|
-
|
|
375
|
+
var UNMEASURED_BYTES_PER_TOKEN_MILLI = 3e3;
|
|
376
|
+
function platformBytesPerTokenMilli(learnedMilli) {
|
|
377
|
+
if (learnedMilli === null || learnedMilli === void 0) return UNMEASURED_BYTES_PER_TOKEN_MILLI;
|
|
378
|
+
return Math.min(Math.max(Math.floor(learnedMilli / 10) * 9, 1e3), 8e3);
|
|
379
|
+
}
|
|
380
|
+
function platformTextTokens(text, learnedMilli) {
|
|
381
|
+
return Math.ceil(Buffer.byteLength(text, "utf8") * 1e3 / platformBytesPerTokenMilli(learnedMilli));
|
|
382
|
+
}
|
|
383
|
+
function messagesTokens(messages, policy = new ContextPolicy(), learnedMilli = null) {
|
|
366
384
|
let total = 0;
|
|
367
385
|
for (const message of messages) {
|
|
368
|
-
const {
|
|
369
|
-
total += estimateTokens(typeof content === "string" ? content : estimateText(content), policy.chars_per_token);
|
|
386
|
+
const { images = [], ...text } = message;
|
|
370
387
|
if (Array.isArray(images)) total += images.length * policy.image_token_estimate;
|
|
388
|
+
if (learnedMilli !== null) {
|
|
389
|
+
total += platformTextTokens(JSON.stringify(text), learnedMilli) + 1;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const { content = "", ...metadata } = text;
|
|
393
|
+
total += estimateTokens(typeof content === "string" ? content : estimateText(content), policy.chars_per_token);
|
|
371
394
|
total += estimateTokens(estimateText(metadata), policy.chars_per_token);
|
|
372
395
|
}
|
|
373
396
|
return total;
|
|
374
397
|
}
|
|
375
398
|
var ContextBudgeter = class {
|
|
376
399
|
policy;
|
|
400
|
+
platformRatio;
|
|
377
401
|
scale;
|
|
378
402
|
calibration_samples;
|
|
379
403
|
constructor(policy = new ContextPolicy(), options = {}) {
|
|
380
404
|
this.policy = policy;
|
|
381
|
-
this.
|
|
382
|
-
this.
|
|
405
|
+
this.platformRatio = options.platformRatio ?? null;
|
|
406
|
+
this.scale = this.platformRatio ? 1 : Math.max(options.initialScale ?? 1, 1);
|
|
407
|
+
this.calibration_samples = this.platformRatio ? 0 : Math.max(Math.trunc(options.calibrationSamples ?? 0), 0);
|
|
383
408
|
}
|
|
384
|
-
estimate = (messages) => Math.ceil(messagesTokens(messages, this.policy) * this.scale);
|
|
409
|
+
estimate = (messages) => this.platformRatio ? messagesTokens(messages, this.policy, this.platformRatio.bytes_per_token_milli) : Math.ceil(messagesTokens(messages, this.policy) * this.scale);
|
|
410
|
+
/** Calibrate the fallback estimate; returns whether the multiplier rose (and should persist). */
|
|
385
411
|
observe(messages, promptEvalCount) {
|
|
412
|
+
if (this.platformRatio) return false;
|
|
386
413
|
if (typeof promptEvalCount !== "number" || !Number.isFinite(promptEvalCount)) return false;
|
|
387
414
|
const actual = Math.trunc(promptEvalCount);
|
|
388
415
|
if (actual <= 0) return false;
|
|
@@ -393,10 +420,14 @@ var ContextBudgeter = class {
|
|
|
393
420
|
return this.scale > previous;
|
|
394
421
|
}
|
|
395
422
|
metadata() {
|
|
423
|
+
const ratio = this.platformRatio;
|
|
396
424
|
return {
|
|
397
|
-
token_counting:
|
|
425
|
+
token_counting: ratio ? "platform_learned_ratio" : "local_chars_per_token",
|
|
398
426
|
estimate_scale: Math.round(this.scale * 1e6) / 1e6,
|
|
399
|
-
calibration_samples: this.calibration_samples
|
|
427
|
+
calibration_samples: this.calibration_samples,
|
|
428
|
+
bytes_per_token_milli: ratio?.bytes_per_token_milli ?? null,
|
|
429
|
+
bytes_per_token_milli_with_margin: ratio ? platformBytesPerTokenMilli(ratio.bytes_per_token_milli) : null,
|
|
430
|
+
bytes_per_token_samples: ratio?.bytes_per_token_samples ?? null
|
|
400
431
|
};
|
|
401
432
|
}
|
|
402
433
|
};
|
|
@@ -542,11 +573,16 @@ var AgentContextManager = class {
|
|
|
542
573
|
constructor(runtime, options = {}) {
|
|
543
574
|
this.runtime = runtime;
|
|
544
575
|
this.model = options.model ?? "";
|
|
545
|
-
|
|
576
|
+
const platformRatio = options.platformRatio ?? null;
|
|
577
|
+
this.calibrationDir = platformRatio ? null : options.calibrationDir ?? null;
|
|
546
578
|
this.calibrationIdentity = options.calibrationIdentity ?? "";
|
|
547
579
|
const loaded = this.loadCalibration();
|
|
548
|
-
this.budgeter = new ContextBudgeter(runtime.context, {
|
|
549
|
-
|
|
580
|
+
this.budgeter = new ContextBudgeter(runtime.context, {
|
|
581
|
+
initialScale: loaded?.scale ?? 1,
|
|
582
|
+
calibrationSamples: loaded?.samples ?? 0,
|
|
583
|
+
platformRatio
|
|
584
|
+
});
|
|
585
|
+
this.calibrationSource = platformRatio ? "not_applied" : loaded ? "persistent_model_cache" : "current_process";
|
|
550
586
|
}
|
|
551
587
|
modelKey() {
|
|
552
588
|
return createHash("sha256").update(this.calibrationContractJson(), "utf8").digest("hex").slice(0, 24);
|
|
@@ -840,8 +876,27 @@ ${body}${footer}`;
|
|
|
840
876
|
}
|
|
841
877
|
};
|
|
842
878
|
|
|
879
|
+
// src/research/progress-events.ts
|
|
880
|
+
var PROGRESS_ENV = "FREELLAMA_AGENT_PROGRESS";
|
|
881
|
+
var PROGRESS_PREFIX = "@freellama-progress ";
|
|
882
|
+
function progressEmitter(env, write = (line) => {
|
|
883
|
+
process.stderr.write(line);
|
|
884
|
+
}) {
|
|
885
|
+
if (env[PROGRESS_ENV] !== "stderr") return void 0;
|
|
886
|
+
return (event) => {
|
|
887
|
+
try {
|
|
888
|
+
write(`${PROGRESS_PREFIX}${JSON.stringify(event)}
|
|
889
|
+
`);
|
|
890
|
+
} catch {
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
|
|
843
895
|
// src/research/transport.ts
|
|
844
896
|
import { createHash as createHash2 } from "node:crypto";
|
|
897
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
898
|
+
import { homedir } from "node:os";
|
|
899
|
+
import path3 from "node:path";
|
|
845
900
|
|
|
846
901
|
// src/research/transport-http.ts
|
|
847
902
|
import { readFileSync as readFileSync3 } from "node:fs";
|
|
@@ -903,13 +958,21 @@ function httpRequest(url, options) {
|
|
|
903
958
|
if (options.body) headers["content-length"] = String(options.body.length);
|
|
904
959
|
let sent = false;
|
|
905
960
|
let settled = false;
|
|
961
|
+
const { signal } = options;
|
|
962
|
+
const onAbort = () => fail(signal?.reason instanceof Error ? signal.reason : new Error("request aborted"));
|
|
906
963
|
const fail = (error) => {
|
|
907
964
|
if (settled) return;
|
|
908
965
|
settled = true;
|
|
966
|
+
signal?.removeEventListener("abort", onAbort);
|
|
909
967
|
request.destroy();
|
|
910
968
|
reject(error);
|
|
911
969
|
};
|
|
970
|
+
if (signal?.aborted) {
|
|
971
|
+
reject(signal.reason instanceof Error ? signal.reason : new Error("request aborted"));
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
912
974
|
const request = client.request(parsed, { method: options.method, headers });
|
|
975
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
913
976
|
request.setTimeout(Math.max(options.timeoutSeconds * 1e3, 1), () => {
|
|
914
977
|
fail(sent ? new RequestTimeoutError() : new URLError(new RequestTimeoutError()));
|
|
915
978
|
});
|
|
@@ -926,6 +989,7 @@ function httpRequest(url, options) {
|
|
|
926
989
|
const finish = () => {
|
|
927
990
|
if (settled) return;
|
|
928
991
|
settled = true;
|
|
992
|
+
signal?.removeEventListener("abort", onAbort);
|
|
929
993
|
const body = Buffer.concat(chunks);
|
|
930
994
|
request.destroy();
|
|
931
995
|
if (ok) resolve(body);
|
|
@@ -957,11 +1021,25 @@ async function requestJson(url, payload, timeoutSeconds) {
|
|
|
957
1021
|
var MANAGED_SUFFIX = "/_freellama/v1/tasks";
|
|
958
1022
|
var IDENTITY_LIMIT = 1024 * 1024;
|
|
959
1023
|
var trimSlashes = (text) => text.replace(/\/+$/, "");
|
|
960
|
-
async function
|
|
961
|
-
const payload = await
|
|
1024
|
+
async function readCatalogJson(url, request = httpRequest) {
|
|
1025
|
+
const payload = await request(url, { method: "GET", headers: requestHeaders(), timeoutSeconds: 5, limit: IDENTITY_LIMIT + 1 });
|
|
962
1026
|
if (payload.length > IDENTITY_LIMIT) throw new ValidationError("model identity catalog exceeds size limit");
|
|
963
1027
|
return parseJsonBody(payload, "model identity catalog");
|
|
964
1028
|
}
|
|
1029
|
+
function catalogEntry(payload, model) {
|
|
1030
|
+
const entries = isRecord(payload) ? payload.models ?? [] : [];
|
|
1031
|
+
if (!Array.isArray(entries)) return null;
|
|
1032
|
+
const names = /* @__PURE__ */ new Set([model]);
|
|
1033
|
+
const lastSegment = model.slice(model.lastIndexOf("/") + 1);
|
|
1034
|
+
if (!lastSegment.includes(":")) names.add(model + ":latest");
|
|
1035
|
+
for (const item of entries) {
|
|
1036
|
+
if (!isRecord(item)) continue;
|
|
1037
|
+
const name = Object.hasOwn(item, "name") ? item.name : item.model;
|
|
1038
|
+
if (typeof name === "object" && name !== null) return "ambiguous";
|
|
1039
|
+
if (typeof name === "string" && names.has(name)) return item;
|
|
1040
|
+
}
|
|
1041
|
+
return null;
|
|
1042
|
+
}
|
|
965
1043
|
var DIGEST = /^(?:sha256:)?[0-9a-fA-F]{64}$/;
|
|
966
1044
|
var sha256 = (data) => createHash2("sha256").update(data, "utf8").digest("hex");
|
|
967
1045
|
async function resolveModelIdentity(endpointInput, model, systemPrompt, options = {}) {
|
|
@@ -969,22 +1047,8 @@ async function resolveModelIdentity(endpointInput, model, systemPrompt, options
|
|
|
969
1047
|
const url = endpoint.endsWith(MANAGED_SUFFIX) ? endpoint.slice(0, -MANAGED_SUFFIX.length) + "/_freellama/v1/models" : endpoint + "/api/tags";
|
|
970
1048
|
const unknown = { identity: "", verified: false, scope: "current_process_only" };
|
|
971
1049
|
try {
|
|
972
|
-
const
|
|
973
|
-
|
|
974
|
-
if (!Array.isArray(entries)) return unknown;
|
|
975
|
-
const names = /* @__PURE__ */ new Set([model]);
|
|
976
|
-
const lastSegment = model.slice(model.lastIndexOf("/") + 1);
|
|
977
|
-
if (!lastSegment.includes(":")) names.add(model + ":latest");
|
|
978
|
-
let entry = null;
|
|
979
|
-
for (const item of entries) {
|
|
980
|
-
if (!isRecord(item)) continue;
|
|
981
|
-
const name = Object.hasOwn(item, "name") ? item.name : item.model;
|
|
982
|
-
if (typeof name === "object" && name !== null) return unknown;
|
|
983
|
-
if (typeof name === "string" && names.has(name)) {
|
|
984
|
-
entry = item;
|
|
985
|
-
break;
|
|
986
|
-
}
|
|
987
|
-
}
|
|
1050
|
+
const entry = catalogEntry(await (options.readJson ?? readCatalogJson)(url), model);
|
|
1051
|
+
if (entry === "ambiguous") return unknown;
|
|
988
1052
|
const digestValue = entry?.digest;
|
|
989
1053
|
if (typeof digestValue !== "string" || !DIGEST.test(digestValue)) return unknown;
|
|
990
1054
|
const digest = (digestValue.startsWith("sha256:") ? digestValue.slice("sha256:".length) : digestValue).toLowerCase();
|
|
@@ -1002,11 +1066,36 @@ async function resolveModelIdentity(endpointInput, model, systemPrompt, options
|
|
|
1002
1066
|
throw error;
|
|
1003
1067
|
}
|
|
1004
1068
|
}
|
|
1069
|
+
async function resolvePlatformTokenRatio(serveEndpoint, model, options = {}) {
|
|
1070
|
+
try {
|
|
1071
|
+
const url = `${trimSlashes(serveEndpoint)}/_freellama/v1/models`;
|
|
1072
|
+
const entry = catalogEntry(await (options.readJson ?? readCatalogJson)(url), model);
|
|
1073
|
+
if (entry === null || entry === "ambiguous") return null;
|
|
1074
|
+
const milli = entry.bytes_per_token_milli;
|
|
1075
|
+
if (typeof milli !== "number" || !Number.isSafeInteger(milli) || milli <= 0) return null;
|
|
1076
|
+
const samples = entry.bytes_per_token_samples;
|
|
1077
|
+
return {
|
|
1078
|
+
bytes_per_token_milli: milli,
|
|
1079
|
+
bytes_per_token_samples: typeof samples === "number" && Number.isSafeInteger(samples) && samples > 0 ? samples : 1
|
|
1080
|
+
};
|
|
1081
|
+
} catch {
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1005
1085
|
function isTransportError(error) {
|
|
1006
1086
|
return error instanceof HTTPError || error instanceof URLError || error instanceof RequestTimeoutError;
|
|
1007
1087
|
}
|
|
1008
1088
|
function retryableChatError(error) {
|
|
1009
|
-
if (error instanceof HTTPError)
|
|
1089
|
+
if (error instanceof HTTPError) {
|
|
1090
|
+
if (error.code !== 503 || error.body.length > RECEIPT_BODY_LIMIT) return false;
|
|
1091
|
+
try {
|
|
1092
|
+
const body = parseJsonBody(error.body, "retry receipt");
|
|
1093
|
+
if (!isRecord(body) || body.retry_safe === false || body.execution_outcome !== void 0 && body.execution_outcome !== "not_started") return false;
|
|
1094
|
+
return body.retry_safe === true && body.execution_outcome === "not_started" || ["admission_timeout", "resource_admission_unavailable", "upstream_circuit_open", "model_lifecycle_busy"].includes(String(body.code));
|
|
1095
|
+
} catch {
|
|
1096
|
+
return false;
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1010
1099
|
return error instanceof URLError && error.reason.code === "ECONNREFUSED";
|
|
1011
1100
|
}
|
|
1012
1101
|
var RECEIPT_FIELDS = /* @__PURE__ */ new Set([
|
|
@@ -1022,7 +1111,12 @@ var RECEIPT_FIELDS = /* @__PURE__ */ new Set([
|
|
|
1022
1111
|
"route",
|
|
1023
1112
|
"decision",
|
|
1024
1113
|
"quality",
|
|
1025
|
-
"feedback"
|
|
1114
|
+
"feedback",
|
|
1115
|
+
"execution_outcome",
|
|
1116
|
+
"retry_safe",
|
|
1117
|
+
"result_recoverable",
|
|
1118
|
+
"job_id",
|
|
1119
|
+
"last_observed_status"
|
|
1026
1120
|
]);
|
|
1027
1121
|
var PAYLOAD_FIELDS = /* @__PURE__ */ new Set([
|
|
1028
1122
|
"prompt",
|
|
@@ -1105,6 +1199,22 @@ var PromptCacheUsage = class {
|
|
|
1105
1199
|
};
|
|
1106
1200
|
}
|
|
1107
1201
|
};
|
|
1202
|
+
var resolvedShift;
|
|
1203
|
+
function contextShift(env = process.env, reset = false) {
|
|
1204
|
+
if (resolvedShift !== void 0 && !reset) return resolvedShift;
|
|
1205
|
+
const read = (value2) => value2 === "ollama_default" || value2 === "off" ? value2 : void 0;
|
|
1206
|
+
let value = read(env.FREELLAMA_CONTEXT_SHIFT?.trim());
|
|
1207
|
+
if (!value) {
|
|
1208
|
+
const file = env.FREELLAMA_RUNTIME_CONFIG || path3.join(env.FREELLAMA_HOME || path3.join(homedir(), ".octocode", "freellama"), "runtime.toml");
|
|
1209
|
+
try {
|
|
1210
|
+
const topLevel = readFileSync4(file, "utf8").split(/^\s*\[/m)[0] ?? "";
|
|
1211
|
+
value = read(/^\s*context_shift\s*=\s*"([^"]*)"/m.exec(topLevel)?.[1]);
|
|
1212
|
+
} catch {
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
resolvedShift = value ?? "off";
|
|
1216
|
+
return resolvedShift;
|
|
1217
|
+
}
|
|
1108
1218
|
function chatRequest(endpointInput, model, messages, options, think, keepAlive, executionPreference = "auto", minPlacementEvidence = "configured") {
|
|
1109
1219
|
const endpoint = trimSlashes(endpointInput);
|
|
1110
1220
|
if (endpoint.endsWith(MANAGED_SUFFIX)) {
|
|
@@ -1130,7 +1240,7 @@ function chatRequest(endpointInput, model, messages, options, think, keepAlive,
|
|
|
1130
1240
|
messages,
|
|
1131
1241
|
stream: false,
|
|
1132
1242
|
truncate: false,
|
|
1133
|
-
shift: false,
|
|
1243
|
+
...contextShift() === "ollama_default" ? {} : { shift: false },
|
|
1134
1244
|
format: "json",
|
|
1135
1245
|
think,
|
|
1136
1246
|
keep_alive: keepAlive,
|
|
@@ -1152,9 +1262,322 @@ function unwrapChatResponse(payload, executionReceipts) {
|
|
|
1152
1262
|
return response;
|
|
1153
1263
|
}
|
|
1154
1264
|
|
|
1265
|
+
// src/research/transport-jobs.ts
|
|
1266
|
+
import { STATUS_CODES } from "node:http";
|
|
1267
|
+
|
|
1268
|
+
// src/job-progress.ts
|
|
1269
|
+
var TERMINAL = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "expired"]);
|
|
1270
|
+
var SUBMIT_FALLBACK_CODES = /* @__PURE__ */ new Set(["task_job_input_too_large", "task_jobs_full", "task_jobs_input_budget"]);
|
|
1271
|
+
var RETENTION_FAILURE_CODES = /* @__PURE__ */ new Set(["task_job_result_too_large", "task_job_error_too_large"]);
|
|
1272
|
+
var FIRST_POLL_MS = 250;
|
|
1273
|
+
var HEARTBEAT_MS = 1e4;
|
|
1274
|
+
var MAX_POLL_MS = 2e3;
|
|
1275
|
+
var MAX_POLL_FAILURES = 5;
|
|
1276
|
+
var CANCEL_ATTEMPTS = 3;
|
|
1277
|
+
var CANCEL_BACKOFF_MS = 250;
|
|
1278
|
+
var CANCEL_REPORT_MS = 5e3;
|
|
1279
|
+
var object = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
1280
|
+
var number = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
1281
|
+
var gigabytes = (bytes) => `${(bytes / 1e9).toFixed(1)} GB`;
|
|
1282
|
+
var seconds = (ms) => ms < 1e4 ? `${(ms / 1e3).toFixed(1)} s` : `${Math.round(ms / 1e3)} s`;
|
|
1283
|
+
function modelNames(value) {
|
|
1284
|
+
if (!Array.isArray(value)) return [];
|
|
1285
|
+
return value.flatMap((item) => {
|
|
1286
|
+
if (typeof item === "string") return [item];
|
|
1287
|
+
const named = object(item)?.model ?? object(item)?.name;
|
|
1288
|
+
return typeof named === "string" ? [named] : [];
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1291
|
+
function evictedSuffix(detail) {
|
|
1292
|
+
const evicted = modelNames(detail?.evicted ?? detail?.evicted_idle_models);
|
|
1293
|
+
return evicted.length > 0 ? `, evicted ${evicted.join(", ")}` : "";
|
|
1294
|
+
}
|
|
1295
|
+
function holdersSuffix(detail) {
|
|
1296
|
+
const holders = Array.isArray(detail?.memory_holders) ? detail.memory_holders : [];
|
|
1297
|
+
const named = holders.map((holder) => holder).filter((holder) => typeof holder.name === "string" && typeof holder.resident_bytes === "number").slice(0, 3).map((holder) => {
|
|
1298
|
+
const count = typeof holder.processes === "number" && holder.processes > 1 ? ` \xD7${holder.processes}` : "";
|
|
1299
|
+
return `${holder.name}${count} ${gigabytes(holder.resident_bytes)}`;
|
|
1300
|
+
});
|
|
1301
|
+
return named.length > 0 ? `; largest memory users: ${named.join(", ")}` : "";
|
|
1302
|
+
}
|
|
1303
|
+
function jobProgressMessage(job) {
|
|
1304
|
+
const status = typeof job.status === "string" ? job.status : "queued";
|
|
1305
|
+
const reason = typeof job.reason === "string" ? job.reason : "";
|
|
1306
|
+
const detail = object(job.detail);
|
|
1307
|
+
const model = typeof job.selected_model === "string" && job.selected_model ? job.selected_model : void 0;
|
|
1308
|
+
switch (status) {
|
|
1309
|
+
case "queued":
|
|
1310
|
+
return "queued";
|
|
1311
|
+
case "waiting_for_admission":
|
|
1312
|
+
return reason === "resources_reserved_waiting_for_backend_capacity" ? "memory reserved; waiting for backend capacity" : "waiting for backend capacity";
|
|
1313
|
+
case "waiting_for_resources": {
|
|
1314
|
+
const need = number(detail?.required_available_bytes);
|
|
1315
|
+
const available = number(detail?.effective_available_bytes);
|
|
1316
|
+
if (need === void 0 && available === void 0) {
|
|
1317
|
+
return (reason === "checking_host_resources" ? "checking host memory" : "waiting for memory") + evictedSuffix(detail);
|
|
1318
|
+
}
|
|
1319
|
+
const sizes = [need !== void 0 ? `need ${gigabytes(need)}` : void 0, available !== void 0 ? `${gigabytes(available)} available` : void 0].filter(Boolean).join(", ");
|
|
1320
|
+
const pressure = Array.isArray(detail?.pressure_reasons) ? detail.pressure_reasons.map((item) => typeof item === "string" ? item : JSON.stringify(item)) : [];
|
|
1321
|
+
return `waiting for memory: ${sizes}${pressure.length > 0 ? ` (host: ${pressure.join(", ")})` : ""}${holdersSuffix(detail)}${evictedSuffix(detail)}`;
|
|
1322
|
+
}
|
|
1323
|
+
case "waiting_for_runner":
|
|
1324
|
+
return "waiting for runner";
|
|
1325
|
+
case "loading": {
|
|
1326
|
+
const expected = number(detail?.expected_load_ms);
|
|
1327
|
+
return `loading ${model ?? "model"}${expected !== void 0 ? ` (~${seconds(expected)})` : ""}${evictedSuffix(detail)}`;
|
|
1328
|
+
}
|
|
1329
|
+
case "running":
|
|
1330
|
+
return model ? `running ${model}` : "running";
|
|
1331
|
+
default:
|
|
1332
|
+
return status.replace(/_/g, " ");
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
function errorBody(error) {
|
|
1336
|
+
try {
|
|
1337
|
+
return object(JSON.parse(error instanceof Error ? error.message : String(error)));
|
|
1338
|
+
} catch {
|
|
1339
|
+
return void 0;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
function describeNapiError(error) {
|
|
1343
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1344
|
+
const missing = /does not exist or has expired|\bHTTP 404\b|^\s*(?:404\b|Not Found\s*$)/i.test(message);
|
|
1345
|
+
return { body: errorBody(error), ...missing ? { status: 404 } : {} };
|
|
1346
|
+
}
|
|
1347
|
+
function unconfirmedSubmission(error, body) {
|
|
1348
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1349
|
+
if (body && (typeof body.code === "string" || "execution_outcome" in body || "retry_safe" in body)) {
|
|
1350
|
+
return error instanceof Error ? error : new Error(message);
|
|
1351
|
+
}
|
|
1352
|
+
return new Error(JSON.stringify({
|
|
1353
|
+
...body,
|
|
1354
|
+
error: typeof body?.error === "string" ? body.error : message,
|
|
1355
|
+
execution_outcome: "unknown",
|
|
1356
|
+
retry_safe: false
|
|
1357
|
+
}));
|
|
1358
|
+
}
|
|
1359
|
+
function submissionRefused({ body }) {
|
|
1360
|
+
const code = body?.code;
|
|
1361
|
+
return typeof code === "string" && SUBMIT_FALLBACK_CODES.has(code);
|
|
1362
|
+
}
|
|
1363
|
+
var defaultWait = (ms, signal) => new Promise((resolve) => {
|
|
1364
|
+
if (signal?.aborted) return resolve();
|
|
1365
|
+
const done = () => {
|
|
1366
|
+
clearTimeout(timer);
|
|
1367
|
+
signal?.removeEventListener("abort", done);
|
|
1368
|
+
resolve();
|
|
1369
|
+
};
|
|
1370
|
+
const timer = setTimeout(done, ms);
|
|
1371
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
1372
|
+
});
|
|
1373
|
+
async function runWithJobProgress(calls, report, options = {}) {
|
|
1374
|
+
const { signal, wait = defaultWait, describe = describeNapiError, onJob } = options;
|
|
1375
|
+
const aborted = (id2, cancellation2) => options.aborted?.(id2, cancellation2) ?? new Error(id2 ? cancellation2 === "unknown" ? `run_task was cancelled by the client; cancellation acknowledgement is unknown for deferred job ${id2}, which may still be running. Upstream stop is not confirmed.` : `run_task was cancelled by the client; cancellation was requested for deferred job ${id2}${cancellation2 === "acknowledged" ? " and acknowledged by serve" : ""}. Upstream stop is not confirmed.` : "run_task was cancelled by the client before a job handle was received; execution outcome is unknown. The task was not replayed.");
|
|
1376
|
+
if (signal?.aborted) throw options.aborted?.() ?? new Error("run_task was cancelled by the client before submission; no task was submitted.");
|
|
1377
|
+
let raw;
|
|
1378
|
+
try {
|
|
1379
|
+
raw = await calls.submit();
|
|
1380
|
+
} catch (error) {
|
|
1381
|
+
if (signal?.aborted) throw aborted();
|
|
1382
|
+
const described = describe(error);
|
|
1383
|
+
if (submissionRefused(described)) return calls.sync(described.body);
|
|
1384
|
+
throw options.unconfirmed?.(error, described.body) ?? unconfirmedSubmission(error, described.body);
|
|
1385
|
+
}
|
|
1386
|
+
let submitted;
|
|
1387
|
+
try {
|
|
1388
|
+
submitted = object(JSON.parse(raw));
|
|
1389
|
+
} catch {
|
|
1390
|
+
return raw;
|
|
1391
|
+
}
|
|
1392
|
+
let job = object(submitted?.job);
|
|
1393
|
+
const id = typeof job?.id === "string" ? job.id : void 0;
|
|
1394
|
+
if (submitted?.deferred !== true || !job || !id) return raw;
|
|
1395
|
+
onJob?.(id, true);
|
|
1396
|
+
let open = true;
|
|
1397
|
+
const settled = () => {
|
|
1398
|
+
if (open) onJob?.(id, false);
|
|
1399
|
+
open = false;
|
|
1400
|
+
};
|
|
1401
|
+
let cancellation;
|
|
1402
|
+
const requestCancellation = async () => {
|
|
1403
|
+
for (let attempt = 1; ; attempt++) {
|
|
1404
|
+
try {
|
|
1405
|
+
const reply = object(JSON.parse(await calls.cancel(id)));
|
|
1406
|
+
const status = object(reply?.job)?.status ?? reply?.status;
|
|
1407
|
+
if (!TERMINAL.has(String(status))) return "requested";
|
|
1408
|
+
settled();
|
|
1409
|
+
return "acknowledged";
|
|
1410
|
+
} catch (error) {
|
|
1411
|
+
if (describe(error).status === 404) {
|
|
1412
|
+
settled();
|
|
1413
|
+
return "unknown";
|
|
1414
|
+
}
|
|
1415
|
+
if (attempt >= CANCEL_ATTEMPTS) return "unknown";
|
|
1416
|
+
await wait(CANCEL_BACKOFF_MS * 2 ** (attempt - 1));
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
};
|
|
1420
|
+
const cancel = () => {
|
|
1421
|
+
cancellation ??= requestCancellation();
|
|
1422
|
+
};
|
|
1423
|
+
const cancellationState = async () => {
|
|
1424
|
+
if (!cancellation) return void 0;
|
|
1425
|
+
let timer;
|
|
1426
|
+
const capped = new Promise((resolve) => {
|
|
1427
|
+
timer = setTimeout(() => resolve("unknown"), CANCEL_REPORT_MS);
|
|
1428
|
+
});
|
|
1429
|
+
try {
|
|
1430
|
+
return await Promise.race([cancellation, capped]);
|
|
1431
|
+
} finally {
|
|
1432
|
+
clearTimeout(timer);
|
|
1433
|
+
}
|
|
1434
|
+
};
|
|
1435
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
1436
|
+
try {
|
|
1437
|
+
let lastMessage = "";
|
|
1438
|
+
let lastPhase = "";
|
|
1439
|
+
let lastReportAt = Date.now();
|
|
1440
|
+
let phaseStartedAt = lastReportAt;
|
|
1441
|
+
let delay = FIRST_POLL_MS;
|
|
1442
|
+
let failures = 0;
|
|
1443
|
+
for (; ; ) {
|
|
1444
|
+
if (signal?.aborted) {
|
|
1445
|
+
cancel();
|
|
1446
|
+
throw aborted(id, await cancellationState());
|
|
1447
|
+
}
|
|
1448
|
+
const now = Date.now();
|
|
1449
|
+
const phase = `${String(job.status)}|${String(job.reason)}`;
|
|
1450
|
+
if (phase !== lastPhase) {
|
|
1451
|
+
lastPhase = phase;
|
|
1452
|
+
phaseStartedAt = now;
|
|
1453
|
+
delay = FIRST_POLL_MS;
|
|
1454
|
+
}
|
|
1455
|
+
const message = jobProgressMessage(job);
|
|
1456
|
+
if (message !== lastMessage) {
|
|
1457
|
+
report(message);
|
|
1458
|
+
lastMessage = message;
|
|
1459
|
+
lastReportAt = now;
|
|
1460
|
+
} else if (now - lastReportAt >= HEARTBEAT_MS && !TERMINAL.has(String(job.status))) {
|
|
1461
|
+
report(`${message} (${seconds(now - phaseStartedAt)})`);
|
|
1462
|
+
lastReportAt = now;
|
|
1463
|
+
}
|
|
1464
|
+
if (TERMINAL.has(String(job.status))) break;
|
|
1465
|
+
await wait(delay, signal);
|
|
1466
|
+
if (signal?.aborted) {
|
|
1467
|
+
cancel();
|
|
1468
|
+
throw aborted(id, await cancellationState());
|
|
1469
|
+
}
|
|
1470
|
+
delay = Math.min(MAX_POLL_MS, Math.round(delay * 1.5));
|
|
1471
|
+
try {
|
|
1472
|
+
const polled = object(object(JSON.parse(await calls.get(id)))?.job);
|
|
1473
|
+
if (polled) job = polled;
|
|
1474
|
+
failures = 0;
|
|
1475
|
+
} catch (error) {
|
|
1476
|
+
if (describe(error).status === 404) {
|
|
1477
|
+
settled();
|
|
1478
|
+
const failure2 = {
|
|
1479
|
+
code: "task_job_unavailable",
|
|
1480
|
+
error: `Deferred job ${id} is no longer available; its execution outcome is unknown. The task was not run again. Its result cannot be recovered from the missing job record; verify the original execution before submitting new work.`,
|
|
1481
|
+
job_id: id,
|
|
1482
|
+
last_observed_status: job.status,
|
|
1483
|
+
execution_outcome: "unknown",
|
|
1484
|
+
result_recoverable: false,
|
|
1485
|
+
retry_safe: false
|
|
1486
|
+
};
|
|
1487
|
+
throw options.failure?.(failure2, 404) ?? new Error(JSON.stringify(failure2));
|
|
1488
|
+
}
|
|
1489
|
+
failures += 1;
|
|
1490
|
+
if (failures >= MAX_POLL_FAILURES) {
|
|
1491
|
+
cancel();
|
|
1492
|
+
throw error;
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
settled();
|
|
1497
|
+
if (job.status === "completed") return JSON.stringify(job.result ?? {});
|
|
1498
|
+
const failure = { ...object(job.error) ?? { error: `deferred job ${id} ended ${String(job.status)}` } };
|
|
1499
|
+
if (typeof failure.code === "string" && RETENTION_FAILURE_CODES.has(failure.code)) {
|
|
1500
|
+
const lost = failure.code === "task_job_result_too_large" ? "result" : "error";
|
|
1501
|
+
failure.error = `${String(failure.error)} Deferred job ${id} was not run again. Its original ${lost} was not retained and cannot be recovered from this job; resubmission would execute new work.`;
|
|
1502
|
+
failure.job_id = id;
|
|
1503
|
+
failure.result_recoverable = false;
|
|
1504
|
+
failure.retry_safe = false;
|
|
1505
|
+
}
|
|
1506
|
+
const status = typeof failure.status === "number" ? failure.status : void 0;
|
|
1507
|
+
delete failure.id;
|
|
1508
|
+
delete failure.ok;
|
|
1509
|
+
if (status !== void 0) delete failure.status;
|
|
1510
|
+
throw options.failure?.(failure, status) ?? new Error(JSON.stringify(failure));
|
|
1511
|
+
} finally {
|
|
1512
|
+
signal?.removeEventListener("abort", cancel);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
// src/research/transport-jobs.ts
|
|
1517
|
+
var TASKS_SUFFIX = "/_freellama/v1/tasks";
|
|
1518
|
+
function describeHttpError(error) {
|
|
1519
|
+
if (!(error instanceof HTTPError)) return {};
|
|
1520
|
+
try {
|
|
1521
|
+
const body = parseJsonBody(error.body.subarray(0, RECEIPT_BODY_LIMIT), "error body");
|
|
1522
|
+
return { status: error.code, ...isRecord(body) ? { body } : {} };
|
|
1523
|
+
} catch {
|
|
1524
|
+
return { status: error.code };
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
function syncError(body, status) {
|
|
1528
|
+
const code = status ?? 500;
|
|
1529
|
+
return new HTTPError(code, STATUS_CODES[code] ?? "", Buffer.from(JSON.stringify(body), "utf8"));
|
|
1530
|
+
}
|
|
1531
|
+
async function requestJsonAsJob(url, payload, timeoutSeconds, options) {
|
|
1532
|
+
const request = options.request ?? httpRequest;
|
|
1533
|
+
const headers = requestHeaders();
|
|
1534
|
+
const call = async (method, target, body, signal) => {
|
|
1535
|
+
const sent = body === void 0 ? void 0 : Buffer.from(JSON.stringify(body), "utf8");
|
|
1536
|
+
const response = await request(target, { method, headers, body: sent, timeoutSeconds, signal });
|
|
1537
|
+
return decodeUtf8(response, "managed task response");
|
|
1538
|
+
};
|
|
1539
|
+
const sync = () => call("POST", url, payload);
|
|
1540
|
+
if (!url.endsWith(TASKS_SUFFIX)) return parseJsonBody(Buffer.from(await sync(), "utf8"), "chat response");
|
|
1541
|
+
const jobs = `${url.slice(0, -TASKS_SUFFIX.length)}/_freellama/v1/jobs`;
|
|
1542
|
+
const deadline = new AbortController();
|
|
1543
|
+
const timer = setTimeout(() => deadline.abort(), Math.max(timeoutSeconds * 1e3, 1));
|
|
1544
|
+
try {
|
|
1545
|
+
const raw = await runWithJobProgress({
|
|
1546
|
+
submit: () => call("POST", url, { ...payload, defer: true }),
|
|
1547
|
+
get: (id) => call("GET", `${jobs}/${encodeURIComponent(id)}`, void 0, deadline.signal),
|
|
1548
|
+
cancel: (id) => call("POST", `${jobs}/${encodeURIComponent(id)}/cancel`, null),
|
|
1549
|
+
sync: async () => {
|
|
1550
|
+
throw syncError({
|
|
1551
|
+
code: "cancellable_task_unavailable",
|
|
1552
|
+
error: "Serve refused job submission before execution; cancellable research requires available job capacity and a supported request size.",
|
|
1553
|
+
execution_outcome: "not_started",
|
|
1554
|
+
retry_safe: true
|
|
1555
|
+
}, 409);
|
|
1556
|
+
}
|
|
1557
|
+
}, options.report, {
|
|
1558
|
+
signal: deadline.signal,
|
|
1559
|
+
wait: options.wait,
|
|
1560
|
+
describe: describeHttpError,
|
|
1561
|
+
failure: syncError,
|
|
1562
|
+
aborted: () => new RequestTimeoutError(),
|
|
1563
|
+
// Keep transport types, but do not let an arbitrary 503 erase an uncertain submission.
|
|
1564
|
+
unconfirmed: (error, body) => error instanceof HTTPError && !retryableChatError(error) ? syncError({
|
|
1565
|
+
...body,
|
|
1566
|
+
error: typeof body?.error === "string" ? body.error : error.message,
|
|
1567
|
+
execution_outcome: body?.execution_outcome ?? "unknown",
|
|
1568
|
+
retry_safe: false
|
|
1569
|
+
}, error.code) : error,
|
|
1570
|
+
onJob: options.onJob
|
|
1571
|
+
});
|
|
1572
|
+
return parseJsonBody(Buffer.from(raw, "utf8"), "chat response");
|
|
1573
|
+
} finally {
|
|
1574
|
+
clearTimeout(timer);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1155
1578
|
// src/research/workspace-path.ts
|
|
1156
1579
|
import { lstatSync, readlinkSync, statSync } from "node:fs";
|
|
1157
|
-
import
|
|
1580
|
+
import path4 from "node:path";
|
|
1158
1581
|
var SymlinkLoopError = class extends ValidationError {
|
|
1159
1582
|
name = "SymlinkLoopError";
|
|
1160
1583
|
};
|
|
@@ -1209,7 +1632,53 @@ function resolvePath(target, cwd = process.cwd()) {
|
|
|
1209
1632
|
return current;
|
|
1210
1633
|
};
|
|
1211
1634
|
const text = cleanPath(target);
|
|
1212
|
-
return
|
|
1635
|
+
return path4.posix.normalize(walk(text.startsWith("/") ? "" : cwd, text) || "/");
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
// src/research/grounding.ts
|
|
1639
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
1640
|
+
import path5 from "node:path";
|
|
1641
|
+
var PATH_TOKEN = /(?<![\w@/.-])(?:\.\/)?[\w@-][\w@.-]*(?:\/[\w@.-]+)+\.[A-Za-z][A-Za-z0-9]{0,5}(?=$|[\s:`'"),.;\]])/g;
|
|
1642
|
+
function clean(token) {
|
|
1643
|
+
return token.replace(/^\.\//, "").replace(/[.,;]+$/, "");
|
|
1644
|
+
}
|
|
1645
|
+
function pathsIn(text) {
|
|
1646
|
+
return [...new Set([...text.matchAll(PATH_TOKEN)].map((match) => clean(match[0])))];
|
|
1647
|
+
}
|
|
1648
|
+
function missingPaths(answer, workspace) {
|
|
1649
|
+
return pathsIn(answer).filter((candidate) => !existsSync3(path5.join(workspace, candidate)));
|
|
1650
|
+
}
|
|
1651
|
+
function evidencePaths(observations, workspace, limit = 10) {
|
|
1652
|
+
const found = [];
|
|
1653
|
+
for (const text of observations) {
|
|
1654
|
+
for (const candidate of pathsIn(text)) {
|
|
1655
|
+
if (!found.includes(candidate) && existsSync3(path5.join(workspace, candidate))) found.push(candidate);
|
|
1656
|
+
if (found.length >= limit) return found;
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
return found;
|
|
1660
|
+
}
|
|
1661
|
+
function unseenPaths(answer, observations) {
|
|
1662
|
+
return pathsIn(answer).filter((candidate) => !observations.some((text) => text.includes(candidate)));
|
|
1663
|
+
}
|
|
1664
|
+
function observedPaths(observations, limit = 10) {
|
|
1665
|
+
return [...new Set(observations.flatMap((text) => pathsIn(text)))].slice(0, limit);
|
|
1666
|
+
}
|
|
1667
|
+
function groundingRepairNotice(missing, evidence, observed = false) {
|
|
1668
|
+
return `These paths in your answer ${observed ? "did not appear in any tool result" : "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 ${observed ? "your tool results showed" : "that exist"}; if the evidence does not establish the answer, say so.`;
|
|
1669
|
+
}
|
|
1670
|
+
function groundingNote(missing, observed = false) {
|
|
1671
|
+
return `
|
|
1672
|
+
[freellama: cited path(s) ${observed ? "not seen in any tool result" : "not found in the workspace"}: ${missing.join(", ")}; treat this answer as unverified]`;
|
|
1673
|
+
}
|
|
1674
|
+
function toolErrorHint(message) {
|
|
1675
|
+
if (/rg: unrecognized flag --(?:exclude-dir|include)/.test(message)) {
|
|
1676
|
+
return "\nHint: rg excludes directories with --glob '!node_modules' and filters types with -t ts; or use grep -rn PATTERN . --exclude-dir=node_modules.";
|
|
1677
|
+
}
|
|
1678
|
+
return "";
|
|
1679
|
+
}
|
|
1680
|
+
function sourceRepairNotice(evidence) {
|
|
1681
|
+
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(", ")}.`;
|
|
1213
1682
|
}
|
|
1214
1683
|
|
|
1215
1684
|
// src/research/agent-loop.ts
|
|
@@ -1219,7 +1688,7 @@ function requireEnv(env, name) {
|
|
|
1219
1688
|
if (value === void 0) throw new Error(`${name} is not set`);
|
|
1220
1689
|
return value;
|
|
1221
1690
|
}
|
|
1222
|
-
var readText = (file) => normalizeNewlines(decodeUtf8(
|
|
1691
|
+
var readText = (file) => normalizeNewlines(decodeUtf8(readFileSync5(file), file));
|
|
1223
1692
|
var pathOf = (value) => value === "" ? "." : value;
|
|
1224
1693
|
var numberOr = (value, fallback) => typeof value === "number" && Number.isFinite(value) ? value : typeof value === "boolean" ? Number(value) : fallback;
|
|
1225
1694
|
var echo = (action) => canonicalJson(action, { sortKeys: false, ensureAscii: false });
|
|
@@ -1249,9 +1718,15 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1249
1718
|
const { model, workspace, prompt, resultPath } = inputs;
|
|
1250
1719
|
const print = hooks.print ?? ((line) => process.stdout.write(`${line}
|
|
1251
1720
|
`));
|
|
1252
|
-
const
|
|
1721
|
+
const progress = hooks.progress ?? progressEmitter(env);
|
|
1253
1722
|
const endpoint = (env.FREELLAMA_OLLAMA_ENDPOINT ?? "http://127.0.0.1:11434").replace(/\/+$/, "");
|
|
1254
1723
|
const managedEndpoint = (env.FREELLAMA_AGENT_MANAGED_ENDPOINT ?? "").replace(/\/+$/, "");
|
|
1724
|
+
let phaseAt = { maxTurns: 0 };
|
|
1725
|
+
const sendJson = hooks.requestJson ?? (managedEndpoint ? (url, payload, timeoutSeconds) => requestJsonAsJob(url, payload, timeoutSeconds, {
|
|
1726
|
+
report: (message) => progress?.({ event: "job_phase", ...phaseAt, message }),
|
|
1727
|
+
onJob: (id, open) => progress?.({ event: open ? "job_open" : "job_closed", id }),
|
|
1728
|
+
request: hooks.httpRequest
|
|
1729
|
+
}) : requestJson);
|
|
1255
1730
|
const executionPreference = env.FREELLAMA_AGENT_EXECUTION_PREFERENCE ?? "auto";
|
|
1256
1731
|
const minPlacementEvidence = env.FREELLAMA_AGENT_MIN_PLACEMENT_EVIDENCE ?? "configured";
|
|
1257
1732
|
let runtime;
|
|
@@ -1266,7 +1741,7 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1266
1741
|
}
|
|
1267
1742
|
let messages = [
|
|
1268
1743
|
{ role: "system", content: spec.systemPrompt(workspace) },
|
|
1269
|
-
{ role: "user", content: prompt }
|
|
1744
|
+
{ role: "user", content: withBriefing(prompt, spec, workspace) }
|
|
1270
1745
|
];
|
|
1271
1746
|
const calls = [];
|
|
1272
1747
|
const usage = { input_tokens: 0, output_tokens: 0, cache_read_tokens: null, cache_write_tokens: null };
|
|
@@ -1276,12 +1751,20 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1276
1751
|
let answer = "";
|
|
1277
1752
|
let failure = null;
|
|
1278
1753
|
let transportError = null;
|
|
1754
|
+
const readCatalog = (url) => readCatalogJson(url, hooks.httpRequest);
|
|
1755
|
+
const platformRatio = managedEndpoint ? await (hooks.resolvePlatformTokenRatio ?? resolvePlatformTokenRatio)(managedEndpoint, model, { readJson: readCatalog }) : null;
|
|
1279
1756
|
const calibrationDir = trimSpace(env.FREELLAMA_AGENT_TOKEN_CALIBRATION_DIR ?? "");
|
|
1280
|
-
const calibrationModelIdentity = calibrationDir ? await (hooks.resolveModelIdentity ?? resolveModelIdentity)(
|
|
1757
|
+
const calibrationModelIdentity = calibrationDir ? await (hooks.resolveModelIdentity ?? resolveModelIdentity)(
|
|
1758
|
+
managedEndpoint || endpoint,
|
|
1759
|
+
model,
|
|
1760
|
+
messages[0].content,
|
|
1761
|
+
{ readJson: readCatalog }
|
|
1762
|
+
) : { verified: false, scope: "persistence_disabled", identity: "" };
|
|
1281
1763
|
const contextManager = new AgentContextManager(runtime, {
|
|
1282
1764
|
model,
|
|
1283
1765
|
calibrationIdentity: calibrationModelIdentity.identity,
|
|
1284
|
-
calibrationDir: calibrationDir && calibrationModelIdentity.verified ? calibrationDir : null
|
|
1766
|
+
calibrationDir: calibrationDir && calibrationModelIdentity.verified ? calibrationDir : null,
|
|
1767
|
+
platformRatio
|
|
1285
1768
|
});
|
|
1286
1769
|
try {
|
|
1287
1770
|
messages = contextManager.fit(messages);
|
|
@@ -1314,12 +1797,19 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1314
1797
|
executionPreference,
|
|
1315
1798
|
minPlacementEvidence
|
|
1316
1799
|
);
|
|
1800
|
+
const recorded = executionReceipts.length;
|
|
1317
1801
|
response = unwrapChatResponse(await sendJson(url, payload, runtime.request_timeout_seconds), executionReceipts);
|
|
1802
|
+
if (managedEndpoint && executionReceipts.length === recorded) {
|
|
1803
|
+
executionReceipts.push({ model: null, execution: null, reason: "managed_execution_receipt_missing" });
|
|
1804
|
+
}
|
|
1318
1805
|
break;
|
|
1319
1806
|
} catch (error) {
|
|
1320
1807
|
if (!isTransportError(error) || !retryableChatError(error)) throw error;
|
|
1321
1808
|
lastError = error;
|
|
1322
|
-
if (attempt + 1 < runtime.retry_attempts)
|
|
1809
|
+
if (attempt + 1 < runtime.retry_attempts) {
|
|
1810
|
+
progress?.({ event: "retry", attempt: attempt + 2, attempts: runtime.retry_attempts });
|
|
1811
|
+
await sleep(runtime.retry_backoff_seconds * 1e3);
|
|
1812
|
+
}
|
|
1323
1813
|
}
|
|
1324
1814
|
}
|
|
1325
1815
|
if (response === null) throw lastError;
|
|
@@ -1354,10 +1844,29 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1354
1844
|
const observations = hooks.observationStore ? hooks.observationStore(pageSize) : new ObservationStore(pageSize);
|
|
1355
1845
|
let session;
|
|
1356
1846
|
try {
|
|
1357
|
-
|
|
1847
|
+
try {
|
|
1848
|
+
session = spec.open ? await spec.open(workspace) : void 0;
|
|
1849
|
+
} catch (error) {
|
|
1850
|
+
if (!isValidationError(error)) throw error;
|
|
1851
|
+
answer = `research adapter could not start: ${error.message}`;
|
|
1852
|
+
writeFailureResult(resultPath, answer);
|
|
1853
|
+
print(answer);
|
|
1854
|
+
return 1;
|
|
1855
|
+
}
|
|
1856
|
+
const root = session?.workspace ?? workspace;
|
|
1358
1857
|
let parseFailures = 0;
|
|
1359
1858
|
let exhausted = true;
|
|
1859
|
+
let repeats = 0;
|
|
1860
|
+
let groundingRepaired = false;
|
|
1861
|
+
let sourceRepaired = false;
|
|
1862
|
+
const seenOutputs = [];
|
|
1863
|
+
const observed = spec.grounding === "observed";
|
|
1864
|
+
const ungrounded = (text) => observed ? unseenPaths(text, seenOutputs) : missingPaths(text, root);
|
|
1865
|
+
const evidenceFor = () => observed ? observedPaths(seenOutputs) : evidencePaths(seenOutputs, root);
|
|
1360
1866
|
for (let turn = 0; turn < runtime.max_turns; turn += 1) {
|
|
1867
|
+
const at = { turn: turn + 1, maxTurns: runtime.max_turns };
|
|
1868
|
+
phaseAt = at;
|
|
1869
|
+
progress?.({ event: "model", ...at });
|
|
1361
1870
|
let response;
|
|
1362
1871
|
try {
|
|
1363
1872
|
response = await callModel();
|
|
@@ -1381,6 +1890,7 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1381
1890
|
exhausted = false;
|
|
1382
1891
|
break;
|
|
1383
1892
|
}
|
|
1893
|
+
progress?.({ event: "repair", ...at });
|
|
1384
1894
|
messages.push({ role: "assistant", content: cpSlice(raw, 0, runtime.parse_repair_echo_chars) });
|
|
1385
1895
|
messages.push({ role: "user", content: PARSE_REPAIR_NOTICE });
|
|
1386
1896
|
if (!refit()) {
|
|
@@ -1393,6 +1903,7 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1393
1903
|
if (action.action === "page") {
|
|
1394
1904
|
const wantStep = Math.trunc(numberOr(action.step, 0));
|
|
1395
1905
|
const wantPage = Math.trunc(numberOr(action.page, 1));
|
|
1906
|
+
progress?.({ event: "action", ...at, action });
|
|
1396
1907
|
messages.push({ role: "assistant", content: echo(action) });
|
|
1397
1908
|
messages.push({ role: "user", content: observations.message(wantStep, wantPage) });
|
|
1398
1909
|
if (!refit()) {
|
|
@@ -1402,45 +1913,83 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1402
1913
|
continue;
|
|
1403
1914
|
}
|
|
1404
1915
|
if (action.action === "finish") {
|
|
1405
|
-
|
|
1916
|
+
const proposed = trimSpace(String(action.answer ?? ""));
|
|
1917
|
+
const missing = ungrounded(proposed);
|
|
1918
|
+
if (missing.length > 0 && !groundingRepaired) {
|
|
1919
|
+
groundingRepaired = true;
|
|
1920
|
+
progress?.({ event: "repair", ...at });
|
|
1921
|
+
messages.push({ role: "assistant", content: echo(action) });
|
|
1922
|
+
messages.push({ role: "user", content: groundingRepairNotice(missing, evidenceFor(), observed) });
|
|
1923
|
+
if (!refit()) {
|
|
1924
|
+
answer = proposed + groundingNote(missing, observed);
|
|
1925
|
+
exhausted = false;
|
|
1926
|
+
break;
|
|
1927
|
+
}
|
|
1928
|
+
continue;
|
|
1929
|
+
}
|
|
1930
|
+
const evidence = spec.requireSource !== false && missing.length === 0 && !sourceRepaired && pathsIn(proposed).length === 0 ? evidenceFor() : [];
|
|
1931
|
+
if (evidence.length > 0) {
|
|
1932
|
+
sourceRepaired = true;
|
|
1933
|
+
progress?.({ event: "repair", ...at });
|
|
1934
|
+
messages.push({ role: "assistant", content: echo(action) });
|
|
1935
|
+
messages.push({ role: "user", content: sourceRepairNotice(evidence) });
|
|
1936
|
+
if (!refit()) {
|
|
1937
|
+
answer = proposed;
|
|
1938
|
+
exhausted = false;
|
|
1939
|
+
break;
|
|
1940
|
+
}
|
|
1941
|
+
continue;
|
|
1942
|
+
}
|
|
1943
|
+
progress?.({ event: "finish", ...at });
|
|
1944
|
+
answer = missing.length > 0 ? proposed + groundingNote(missing, observed) : proposed;
|
|
1406
1945
|
exhausted = false;
|
|
1407
1946
|
break;
|
|
1408
1947
|
}
|
|
1409
1948
|
const started = performance.now();
|
|
1410
|
-
const call = session.toolCall(action,
|
|
1949
|
+
const call = session.toolCall(action, root, runtime);
|
|
1411
1950
|
const signature = callSignature(call.signature);
|
|
1412
1951
|
let observation;
|
|
1413
1952
|
let status;
|
|
1953
|
+
let exitCode;
|
|
1414
1954
|
if (seenCalls.has(signature)) {
|
|
1955
|
+
progress?.({ event: "action", ...at, action, repeat: true });
|
|
1415
1956
|
observation = REPEAT_NOTICE;
|
|
1416
1957
|
status = "repeat";
|
|
1958
|
+
repeats += 1;
|
|
1417
1959
|
} else {
|
|
1960
|
+
progress?.({ event: "action", ...at, action });
|
|
1418
1961
|
try {
|
|
1419
1962
|
observation = hooks.runTool ? await hooks.runTool(call.record.arguments) : await call.run();
|
|
1420
1963
|
status = "ok";
|
|
1421
1964
|
} catch (error) {
|
|
1422
1965
|
if (!isToolError(error)) throw error;
|
|
1423
|
-
|
|
1424
|
-
|
|
1966
|
+
status = error instanceof ToolExitError ? error.status : "error";
|
|
1967
|
+
exitCode = error instanceof ToolExitError ? error.exitCode : void 0;
|
|
1968
|
+
observation = `${status === "empty" ? "tool search" : "tool error"}: ${error.message}${toolErrorHint(error.message)}`;
|
|
1425
1969
|
}
|
|
1426
1970
|
seenCalls.set(signature, calls.length + 1);
|
|
1427
1971
|
}
|
|
1428
1972
|
calls.push({
|
|
1429
1973
|
...call.record,
|
|
1430
1974
|
status,
|
|
1975
|
+
...exitCode === void 0 ? {} : { exit_code: exitCode },
|
|
1431
1976
|
duration_ms: Math.round((performance.now() - started) * 1e3) / 1e3
|
|
1432
1977
|
});
|
|
1433
1978
|
messages.push({ role: "assistant", content: echo(action) });
|
|
1434
1979
|
const remaining = runtime.max_turns - calls.length;
|
|
1435
1980
|
const step = calls.length;
|
|
1436
1981
|
observations.put(step, observation, call.ledger(status));
|
|
1982
|
+
seenOutputs.push(observation);
|
|
1437
1983
|
messages.push({ role: "user", content: observations.message(step) + spec.remaining(remaining) });
|
|
1438
1984
|
if (!refit()) {
|
|
1439
1985
|
exhausted = false;
|
|
1440
1986
|
break;
|
|
1441
1987
|
}
|
|
1988
|
+
if (repeats >= 2) break;
|
|
1442
1989
|
}
|
|
1443
1990
|
if (exhausted) {
|
|
1991
|
+
progress?.({ event: "finalize", maxTurns: runtime.max_turns });
|
|
1992
|
+
phaseAt = { maxTurns: runtime.max_turns };
|
|
1444
1993
|
messages.push({ role: "user", content: spec.exhausted });
|
|
1445
1994
|
if (refit()) {
|
|
1446
1995
|
try {
|
|
@@ -1448,6 +1997,8 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1448
1997
|
const finalAction = spec.parseAction(content(response));
|
|
1449
1998
|
if (finalAction.action !== "finish") throw new ValidationError("forced final response was not finish");
|
|
1450
1999
|
answer = trimSpace(String(finalAction.answer ?? ""));
|
|
2000
|
+
const missing = ungrounded(answer);
|
|
2001
|
+
if (missing.length > 0) answer += groundingNote(missing, observed);
|
|
1451
2002
|
} catch (error) {
|
|
1452
2003
|
if (!isTransportError(error) && !isValidationError(error)) throw error;
|
|
1453
2004
|
let diagnostic;
|
|
@@ -1457,7 +2008,9 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1457
2008
|
}
|
|
1458
2009
|
}
|
|
1459
2010
|
if (!answer) answer = failure ?? "agent stopped without a final answer";
|
|
2011
|
+
const report = spec.report ? await spec.report(session) : {};
|
|
1460
2012
|
const result = {
|
|
2013
|
+
...report,
|
|
1461
2014
|
final_answer: answer,
|
|
1462
2015
|
tool_calls: calls,
|
|
1463
2016
|
usage,
|
|
@@ -1493,6 +2046,16 @@ async function runLoop(spec, hooks, env, inputs) {
|
|
|
1493
2046
|
observations.close();
|
|
1494
2047
|
}
|
|
1495
2048
|
}
|
|
2049
|
+
function withBriefing(prompt, spec, workspace) {
|
|
2050
|
+
try {
|
|
2051
|
+
const briefing = spec.briefing?.(workspace);
|
|
2052
|
+
return briefing ? `${prompt}
|
|
2053
|
+
|
|
2054
|
+
${briefing}` : prompt;
|
|
2055
|
+
} catch {
|
|
2056
|
+
return prompt;
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
1496
2059
|
|
|
1497
2060
|
// src/research/proc.ts
|
|
1498
2061
|
import { spawn, spawnSync } from "node:child_process";
|
|
@@ -1542,98 +2105,835 @@ function runProcess(argv, options) {
|
|
|
1542
2105
|
});
|
|
1543
2106
|
}
|
|
1544
2107
|
|
|
1545
|
-
// src/
|
|
2108
|
+
// ../rust-core/src/model_catalog.json
|
|
2109
|
+
var model_catalog_default = {
|
|
2110
|
+
snapshot: "2026-11-01",
|
|
2111
|
+
measured_on: "Apple M2 Pro, 32 GB, Ollama 0.40.13, with other work running",
|
|
2112
|
+
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.",
|
|
2113
|
+
jobs: [
|
|
2114
|
+
{ job: "classify", title: "Classify, triage, yes/no", call: 'run_task {task:"decision"}', cookbook: "cookbooks/classify", picks: [
|
|
2115
|
+
{ 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" },
|
|
2116
|
+
{ 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" }
|
|
2117
|
+
] },
|
|
2118
|
+
{ job: "find-code", title: "Find the code for an issue", call: 'run_task {task:"embedding", nearest:{query}}', cookbook: "cookbooks/find-code", picks: [
|
|
2119
|
+
{ 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" },
|
|
2120
|
+
{ 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" }
|
|
2121
|
+
] },
|
|
2122
|
+
{ job: "duplicates", title: "Find duplicates", call: 'run_task {task:"embedding", nearest:{}}', cookbook: "cookbooks/duplicates", picks: [
|
|
2123
|
+
{ tag: "nomic-embed-text:v1.5", role: "default", file_gb: 0.3, memory_gb: 1.4, quality: "measured", evidence: "8/12 real issue pairs" }
|
|
2124
|
+
] },
|
|
2125
|
+
{ job: "ocr", title: "Read text from an image", call: 'run_task {task:"vision", files:[path]}', cookbook: "cookbooks/ocr", picks: [
|
|
2126
|
+
{ 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" },
|
|
2127
|
+
{ 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" }
|
|
2128
|
+
] },
|
|
2129
|
+
{ job: "image-recognition", title: "Understand what an image shows", call: 'run_task {task:"vision", files:[path], prompt}', cookbook: "cookbooks/image-recognition", picks: [
|
|
2130
|
+
{ 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" },
|
|
2131
|
+
{ 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" },
|
|
2132
|
+
{ 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" }
|
|
2133
|
+
] },
|
|
2134
|
+
{ job: "image-search", title: "Find an image by describing it", call: 'run_task {task:"embedding", files:[images], nearest:{query}}', cookbook: "cookbooks/image-search", picks: [
|
|
2135
|
+
{ 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" }
|
|
2136
|
+
] },
|
|
2137
|
+
{ job: "speech-to-text", title: "Recording to text", call: 'audio {action:"transcribe", file}', cookbook: "cookbooks/speech-to-text", picks: [
|
|
2138
|
+
{ 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" },
|
|
2139
|
+
{ 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" }
|
|
2140
|
+
] },
|
|
2141
|
+
{ job: "text-to-speech", title: "Text to speech", call: 'audio {action:"speak", text}', cookbook: "cookbooks/text-to-speech", picks: [
|
|
2142
|
+
{ 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" },
|
|
2143
|
+
{ 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" },
|
|
2144
|
+
{ 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" }
|
|
2145
|
+
] },
|
|
2146
|
+
{ job: "coding", title: "Small coding tasks", call: 'run_task {task:"coding", files:[path]}', cookbook: "cookbooks/coding", picks: [
|
|
2147
|
+
{ 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" },
|
|
2148
|
+
{ 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" },
|
|
2149
|
+
{ 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" },
|
|
2150
|
+
{ 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" }
|
|
2151
|
+
] },
|
|
2152
|
+
{ job: "research-agent", title: "Research a repository", call: 'delegate_research {adapter:"octocode" or "github"}', cookbook: "cookbooks/research-agent", picks: [
|
|
2153
|
+
{ 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" },
|
|
2154
|
+
{ 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" },
|
|
2155
|
+
{ 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" }
|
|
2156
|
+
] }
|
|
2157
|
+
]
|
|
2158
|
+
};
|
|
2159
|
+
|
|
2160
|
+
// src/research/octocode-instructions.ts
|
|
2161
|
+
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.
|
|
2162
|
+
Call: octocode <tool> '{"queries":[{...}]}' (1-5 queries per call; paths relative to the working directory)
|
|
2163
|
+
Result: lines of findings, then "#" notes. Exit 0 found, 1 nothing found, 2 bad input (the message names the field), 3 path not found.
|
|
2164
|
+
More: copy a printed {"cursor":"..."} as the whole input. No cursor means the result is complete.
|
|
2165
|
+
|
|
2166
|
+
structureSearch: see the layout or list files, when you do not know where to look.
|
|
2167
|
+
{"operation":"tree","path":".","maxDepth":2}
|
|
2168
|
+
{"operation":"files","path":".","include":["**/*.rs"],"view":"paths"}
|
|
2169
|
+
localSearch: find a word, name or error text, when you do not know the file.
|
|
2170
|
+
{"path":".","matchString":["memory_holders"],"view":"files"} which files
|
|
2171
|
+
{"path":".","matchString":["memory_holders","holders"]} matching lines with line numbers; several words mean any of them
|
|
2172
|
+
localFetch: read a file an earlier result named (src/a.rs below is a placeholder); ask for the least that answers.
|
|
2173
|
+
{"path":"src/a.rs","view":"symbols"} outline with line numbers
|
|
2174
|
+
{"path":"src/a.rs","ranges":[{"startLine":40,"endLine":90}]}
|
|
2175
|
+
{"path":"src/a.rs","matchString":["fn run"],"contextLines":{"before":2,"after":15}}
|
|
2176
|
+
astSearch: find code by shape, not by text. Use it to find where something is declared.
|
|
2177
|
+
{"operation":"symbols","path":".","symbolFilter":{"terms":["run"],"mode":"exact"}} declarations named run
|
|
2178
|
+
{"operation":"match","path":".","rule":{"pattern":"eval($X)"}} every call of this shape
|
|
2179
|
+
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.
|
|
2180
|
+
{"path":"src/a.rs","symbolName":"run","line":35} its definition
|
|
2181
|
+
{"operation":"references","path":"src/a.rs","symbolName":"run","line":35,"view":"files"} where it is used
|
|
2182
|
+
{"operation":"callers","path":"src/a.rs","symbolName":"run","line":35} also callees, hover, documentSymbols
|
|
2183
|
+
|
|
2184
|
+
Work smart:
|
|
2185
|
+
1. Narrow before reading: list files or matching lines first, then read only those ranges.
|
|
2186
|
+
2. Try several words at once: put synonyms in one matchString, or several queries in one call.
|
|
2187
|
+
3. Build and dependency folders are skipped already; add "testCode":"exclude" to skip tests.
|
|
2188
|
+
4. Nothing found proves little: widen the words or the path before saying something does not exist. If lspSearch is empty, use localSearch.
|
|
2189
|
+
5. Answer with file:line evidence, and stop as soon as the question is answered.
|
|
2190
|
+
`;
|
|
2191
|
+
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.
|
|
2192
|
+
Call: octocode <tool> '{"queries":[{...}]}' (1-5 queries per call)
|
|
2193
|
+
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).
|
|
2194
|
+
More: follow a printed next or {"cursor":"..."}; no next and no hasMore means the result is complete.
|
|
2195
|
+
|
|
2196
|
+
ghSearchRepo: find a repository when you do not know its owner or name.
|
|
2197
|
+
{"operation":"search","keywords":["local llm router"],"filters":{"language":"Rust"}}
|
|
2198
|
+
{"operation":"get","owner":"ollama","repo":"ollama"} one repo: description, stars, default branch
|
|
2199
|
+
artifactSearch: find a package's repository and versions.
|
|
2200
|
+
{"operation":"resolve","ecosystem":"npm","packageName":"zod"} also pypi, maven
|
|
2201
|
+
ghStructure: see a repo's layout; operation "refs" lists branches, "languages" its languages.
|
|
2202
|
+
{"operation":"tree","owner":"ollama","repo":"ollama","path":"server","maxDepth":2}
|
|
2203
|
+
ghSearchCode: find a word or name inside a repo when you do not know the file.
|
|
2204
|
+
{"owner":"ollama","repo":"ollama","keywords":["SystemOneRequest"]} returns paths, lines and a commitSha
|
|
2205
|
+
ghGetFileContent: read a file you found; ask for the least that answers.
|
|
2206
|
+
{"owner":"ollama","repo":"ollama","path":"api/types.go","view":"symbols"} outline
|
|
2207
|
+
{"owner":"ollama","repo":"ollama","path":"api/types.go","ranges":[{"startLine":600,"endLine":640}]}
|
|
2208
|
+
{"owner":"ollama","repo":"ollama","path":"api/types.go","matchString":["type SystemOneRequest"]}
|
|
2209
|
+
ghSearchHistory: find why something changed: pull requests, issues, commits.
|
|
2210
|
+
{"operation":"pullRequest","owner":"ollama","repo":"ollama","keywords":["systemone"]}
|
|
2211
|
+
{"operation":"commit","owner":"ollama","repo":"ollama","keywords":["decision"]} or operation "issue" with "filters":{"state":"open"}
|
|
2212
|
+
ghGetHistoryItem: read one pull request, or issue, you found.
|
|
2213
|
+
{"operation":"pullRequest","owner":"ollama","repo":"ollama","number":18606,"sections":["body","files"]}
|
|
2214
|
+
|
|
2215
|
+
Work smart:
|
|
2216
|
+
1. Narrow: repo, then search, then read only the ranges you need. Never read whole files first.
|
|
2217
|
+
2. Reuse what a result returns: its path, commitSha (as ref) and the next step it suggests.
|
|
2218
|
+
3. Search words are exact code words, not sentences: one to three identifiers beat a question.
|
|
2219
|
+
4. Nothing found proves little: try another word or check the repo name before saying it does not exist.
|
|
2220
|
+
5. Answer with owner/repo, path:line or PR number as evidence, and stop as soon as the question is answered.
|
|
2221
|
+
`;
|
|
2222
|
+
|
|
2223
|
+
// src/local-prompts.ts
|
|
2224
|
+
var catalog = model_catalog_default;
|
|
2225
|
+
function picksOf(job) {
|
|
2226
|
+
return catalog.jobs.find((entry) => entry.job === job)?.picks.map((pick) => pick.tag) ?? [];
|
|
2227
|
+
}
|
|
2228
|
+
var cookbook = (name) => `docs/cookbooks/${name}.md`;
|
|
2229
|
+
var SOURCES = {
|
|
2230
|
+
miniCpmOcr: ["https://huggingface.co/openbmb/MiniCPM-OCR-1.0", "https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf"],
|
|
2231
|
+
glmOcr: ["https://ollama.com/library/glm-ocr", "https://huggingface.co/zai-org/GLM-OCR"],
|
|
2232
|
+
lfm3Vl: ["https://huggingface.co/LiquidAI/LFM3-VL-2B", "https://huggingface.co/LiquidAI/LFM3-VL-2B-GGUF", "https://ollama.com/library/lfm3-vl"],
|
|
2233
|
+
lfm3Omni: ["https://huggingface.co/LiquidAI/LFM3-Omni-2B", "https://ollama.com/library/lfm3-omni"],
|
|
2234
|
+
embeddingGemma: ["https://huggingface.co/google/embeddinggemma-2", "https://ai.google.dev/gemma/docs/embeddinggemma/inference-embeddinggemma-with-sentence-transformers"],
|
|
2235
|
+
nomic: ["https://huggingface.co/nomic-ai/nomic-embed-text-v1.5"],
|
|
2236
|
+
strandsDecider: ["https://ollama.com/library/strands-decider", "https://raw.githubusercontent.com/strands-labs/strands-decider/main/README.md"],
|
|
2237
|
+
nimble: ["https://ollama.com/library/nimble-v3:latest"],
|
|
2238
|
+
bonsai9b: ["https://huggingface.co/prism-ml/Ternary-Bonsai-2-9B-gguf", "https://ollama.com/library/bonsai-2:9b"],
|
|
2239
|
+
bonsai27b: ["https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-gguf", "https://ollama.com/library/bonsai-2"],
|
|
2240
|
+
lfm3: ["https://huggingface.co/LiquidAI/LFM3-1.7B", "https://ollama.com/library/lfm3"],
|
|
2241
|
+
nemotronBolt: ["https://huggingface.co/nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-4GB"],
|
|
2242
|
+
lfm3Audio: ["https://huggingface.co/LiquidAI/LFM3-Audio-2B", "https://ollama.com/library/lfm3-audio"],
|
|
2243
|
+
qwen38: ["https://huggingface.co/Qwen/Qwen3.8-27B", "https://ollama.com/library/qwen3.8"],
|
|
2244
|
+
qwen35: ["https://huggingface.co/Qwen/Qwen3.5-4B", "https://ollama.com/library/qwen3.5"],
|
|
2245
|
+
phonon: ["https://ollama.com/library/phonon:en"]
|
|
2246
|
+
};
|
|
2247
|
+
var EMBEDDING_PREFIXES = {
|
|
2248
|
+
embeddinggemma: {
|
|
2249
|
+
query: "task: search result | query: ",
|
|
2250
|
+
document: "title: none | text: ",
|
|
2251
|
+
codeQuery: "task: code retrieval | query: ",
|
|
2252
|
+
similarity: "task: sentence similarity | query: ",
|
|
2253
|
+
clustering: "task: clustering | query: ",
|
|
2254
|
+
classification: "task: classification | query: "
|
|
2255
|
+
},
|
|
2256
|
+
// A prefix is required; the card names clustering: for removing semantic duplicates.
|
|
2257
|
+
"nomic-embed-text": { query: "search_query: ", document: "search_document: ", clustering: "clustering: ", classification: "classification: " }
|
|
2258
|
+
};
|
|
2259
|
+
var GEMMA = EMBEDDING_PREFIXES.embeddinggemma;
|
|
2260
|
+
var NOMIC = EMBEDDING_PREFIXES["nomic-embed-text"];
|
|
2261
|
+
var OCR_PROMPTS = { text: "Text Recognition:", table: "Table Recognition:", formula: "Formula Recognition:" };
|
|
2262
|
+
var OCR_TRAINED = ["minicpm-ocr", "glm-ocr"];
|
|
2263
|
+
var OCR_PLAIN = {
|
|
2264
|
+
text: "Transcribe all text in the image exactly, line by line. Output only the text.",
|
|
2265
|
+
table: "Transcribe the table in the image as a Markdown table. Output only the table."
|
|
2266
|
+
};
|
|
2267
|
+
var ocrPrompt = (kind, model) => {
|
|
2268
|
+
const name = (model ?? "").toLowerCase().replace(/^(?:hf\.co|huggingface\.co)\/[^/]+\//, "");
|
|
2269
|
+
return model === void 0 || OCR_TRAINED.some((family) => name.startsWith(family)) ? OCR_PROMPTS[kind] : OCR_PLAIN[kind];
|
|
2270
|
+
};
|
|
2271
|
+
var EDIT_FORMAT = `Return each change to an existing file as the file's path (copied from its "--- file:" line), then:
|
|
2272
|
+
<<<<<<< SEARCH
|
|
2273
|
+
exact current lines (5 to 30, unique in the file, indentation exact)
|
|
2274
|
+
=======
|
|
2275
|
+
replacement lines
|
|
2276
|
+
>>>>>>> REPLACE
|
|
2277
|
+
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.`;
|
|
2278
|
+
var NO_TOOLS = "You have no tools and cannot open files: everything you need is in this message. Answer in plain text.";
|
|
2279
|
+
var WORKFLOWS = {
|
|
2280
|
+
"read-text": {
|
|
2281
|
+
job: "ocr",
|
|
2282
|
+
title: "Read all text in an image, exactly",
|
|
2283
|
+
input: "files: the images",
|
|
2284
|
+
prompt: (_request, model) => ocrPrompt("text", model)
|
|
2285
|
+
},
|
|
2286
|
+
"read-table": {
|
|
2287
|
+
job: "ocr",
|
|
2288
|
+
title: "Read a table in an image as Markdown",
|
|
2289
|
+
input: "files: the images",
|
|
2290
|
+
prompt: (_request, model) => ocrPrompt("table", model)
|
|
2291
|
+
},
|
|
2292
|
+
"read-fields": {
|
|
2293
|
+
job: "ocr",
|
|
2294
|
+
title: "Pull named fields from a receipt, invoice or form image as JSON",
|
|
2295
|
+
input: "files: the images; prompt: the field names",
|
|
2296
|
+
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.`
|
|
2297
|
+
},
|
|
2298
|
+
"answer-image": {
|
|
2299
|
+
job: "image-recognition",
|
|
2300
|
+
title: "Answer a question about an image",
|
|
2301
|
+
input: "files: the images; prompt: the question",
|
|
2302
|
+
system: `Answer the question about the image.
|
|
2303
|
+
1. Find the part of the image the question is about.
|
|
2304
|
+
2. Read any text there exactly as printed.
|
|
2305
|
+
3. Answer with just the value or label, or one or two sentences.
|
|
2306
|
+
If the image does not show it, answer "not visible". Never guess at what is cut off.`
|
|
2307
|
+
},
|
|
2308
|
+
"describe-screen": {
|
|
2309
|
+
job: "image-recognition",
|
|
2310
|
+
title: "Describe a screenshot for a developer: text, errors, state",
|
|
2311
|
+
input: "files: the screenshot; prompt: what to look for (optional)",
|
|
2312
|
+
system: `You describe a software screenshot for a developer, in short bullets, in this order:
|
|
2313
|
+
- the app or page shown;
|
|
2314
|
+
- headings and main text, quoted exactly;
|
|
2315
|
+
- any error, warning or empty state, quoted exactly;
|
|
2316
|
+
- the state of the controls that matter (selected, disabled, filled in);
|
|
2317
|
+
- anything that looks broken or misaligned.
|
|
2318
|
+
Describe only what is visible.`
|
|
2319
|
+
},
|
|
2320
|
+
"change-code": {
|
|
2321
|
+
job: "coding",
|
|
2322
|
+
title: "Make one small code change",
|
|
2323
|
+
input: "files: the file to change; prompt: the change",
|
|
2324
|
+
system: `${NO_TOOLS}
|
|
2325
|
+
You make one small code change.
|
|
2326
|
+
1. Find where the change belongs in the code shown.
|
|
2327
|
+
2. Follow the file's existing names, style and error handling.
|
|
2328
|
+
3. Change as few lines as the task needs; touch nothing else.
|
|
2329
|
+
${EDIT_FORMAT}
|
|
2330
|
+
Explain nothing unless asked. If the task is a question, answer it in a few lines.`
|
|
2331
|
+
},
|
|
2332
|
+
"fix-bug": {
|
|
2333
|
+
prefer: "capable",
|
|
2334
|
+
job: "coding",
|
|
2335
|
+
title: "Fix a bug with the smallest change",
|
|
2336
|
+
input: "files: the suspect code; prompt: the error message or symptom",
|
|
2337
|
+
system: `${NO_TOOLS}
|
|
2338
|
+
You fix one bug with the smallest change.
|
|
2339
|
+
1. Read the error or symptom, then the code shown.
|
|
2340
|
+
2. Name the cause in one sentence, quoting the line that causes it.
|
|
2341
|
+
3. Change only what the fix needs.
|
|
2342
|
+
Answer with the cause line, then the edits.
|
|
2343
|
+
${EDIT_FORMAT}
|
|
2344
|
+
If the code shown cannot cause the symptom, say so and name the file or function to look at instead.`
|
|
2345
|
+
},
|
|
2346
|
+
implement: {
|
|
2347
|
+
prefer: "capable",
|
|
2348
|
+
job: "coding",
|
|
2349
|
+
title: "Add a small feature to existing code",
|
|
2350
|
+
input: "files: the code; prompt: the feature",
|
|
2351
|
+
system: `${NO_TOOLS}
|
|
2352
|
+
You add one small feature to the code shown.
|
|
2353
|
+
1. Find the function or place it belongs; reuse helpers the file already has.
|
|
2354
|
+
2. Follow the file's names, style and error handling.
|
|
2355
|
+
3. Handle the empty and error cases the feature can meet.
|
|
2356
|
+
${EDIT_FORMAT}
|
|
2357
|
+
End with one line: what to test.`
|
|
2358
|
+
},
|
|
2359
|
+
"write-tests": {
|
|
2360
|
+
prefer: "capable",
|
|
2361
|
+
job: "coding",
|
|
2362
|
+
title: "Write unit tests for the code shown",
|
|
2363
|
+
input: "files: the code (and an existing test, for style); prompt: what to cover (optional)",
|
|
2364
|
+
system: `${NO_TOOLS}
|
|
2365
|
+
You write unit tests for the code shown; you never change that code.
|
|
2366
|
+
1. List the behaviours: the normal case, edge cases (empty, zero, missing, boundary) and one failure case.
|
|
2367
|
+
2. Use the test framework and import style the project already uses; copy them from the files shown.
|
|
2368
|
+
3. Write one small test per behaviour, named after it, with literal expected values.
|
|
2369
|
+
Answer with the test file's path on one line, then the whole file in one fenced block.`
|
|
2370
|
+
},
|
|
2371
|
+
"review-code": {
|
|
2372
|
+
prefer: "capable",
|
|
2373
|
+
job: "coding",
|
|
2374
|
+
title: "Find real defects in code (not style)",
|
|
2375
|
+
input: "files: the code; prompt: what to focus on (optional)",
|
|
2376
|
+
system: `${NO_TOOLS}
|
|
2377
|
+
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.
|
|
2378
|
+
For each defect write one line: path:line - the problem - the fix.
|
|
2379
|
+
Report at most 5, the surest first. If you find none, answer "no defects found". Never invent a line you did not see.`
|
|
2380
|
+
},
|
|
2381
|
+
"explain-code": {
|
|
2382
|
+
job: "coding",
|
|
2383
|
+
title: "Explain code to someone about to change it",
|
|
2384
|
+
input: "files: the code; prompt: what to explain (optional)",
|
|
2385
|
+
system: `${NO_TOOLS}
|
|
2386
|
+
Explain the code shown to a developer who is about to change it, in at most 6 bullets:
|
|
2387
|
+
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.
|
|
2388
|
+
Name functions and variables exactly.`
|
|
2389
|
+
},
|
|
2390
|
+
"summarize-log": {
|
|
2391
|
+
think: false,
|
|
2392
|
+
prefer: "capable",
|
|
2393
|
+
job: "summarize",
|
|
2394
|
+
title: "Summarize a log: errors grouped, counted, first failure",
|
|
2395
|
+
input: "files: the log; prompt: what you are investigating (optional)",
|
|
2396
|
+
system: `${NO_TOOLS}
|
|
2397
|
+
You read a log for a developer. Report:
|
|
2398
|
+
1. Errors and warnings, grouped by message, each with its count and first and last timestamp.
|
|
2399
|
+
2. The first error in time order, quoted exactly.
|
|
2400
|
+
3. The likely cause, only if the log shows it.
|
|
2401
|
+
Quote messages exactly, skip routine lines, at most 10 bullets.`
|
|
2402
|
+
},
|
|
2403
|
+
"summarize-text": {
|
|
2404
|
+
think: false,
|
|
2405
|
+
job: "summarize",
|
|
2406
|
+
title: "Summarize text for someone who will act on it",
|
|
2407
|
+
input: "files: the text; prompt: what you need from it (optional)",
|
|
2408
|
+
system: `${NO_TOOLS}
|
|
2409
|
+
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.
|
|
2410
|
+
Quote numbers, names, versions and paths exactly. Write "not stated" when the text does not say; never guess. No introduction or closing.`
|
|
2411
|
+
},
|
|
2412
|
+
"summarize-diff": {
|
|
2413
|
+
think: false,
|
|
2414
|
+
prefer: "capable",
|
|
2415
|
+
job: "summarize",
|
|
2416
|
+
title: "Summarize a diff for a reviewer: changes and risks",
|
|
2417
|
+
input: "files: the diff or patch",
|
|
2418
|
+
system: `${NO_TOOLS}
|
|
2419
|
+
You summarize a code diff for a reviewer, in at most 8 bullets:
|
|
2420
|
+
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.
|
|
2421
|
+
Name files, functions and settings exactly.`
|
|
2422
|
+
},
|
|
2423
|
+
"extract-json": {
|
|
2424
|
+
think: false,
|
|
2425
|
+
job: "summarize",
|
|
2426
|
+
title: "Extract named fields from text into JSON",
|
|
2427
|
+
input: "files: the text; prompt: the field names (or a JSON schema)",
|
|
2428
|
+
system: `${NO_TOOLS}
|
|
2429
|
+
Extract the fields the request names from the text into one JSON object.
|
|
2430
|
+
Use the values exactly as written in the text; use null for a field the text does not give; never invent a value.
|
|
2431
|
+
Output only the JSON object.`,
|
|
2432
|
+
format: "json"
|
|
2433
|
+
}
|
|
2434
|
+
};
|
|
2435
|
+
var WORKFLOW_NAMES = Object.keys(WORKFLOWS);
|
|
2436
|
+
var BONSAI_9B = {
|
|
2437
|
+
think: false,
|
|
2438
|
+
options: { temperature: 0.7, top_p: 0.8, top_k: 20, min_p: 0, presence_penalty: 1.5, repeat_penalty: 1 },
|
|
2439
|
+
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.",
|
|
2440
|
+
sources: SOURCES.bonsai9b
|
|
2441
|
+
};
|
|
2442
|
+
var qwenNonThinking = (sources) => ({
|
|
2443
|
+
think: false,
|
|
2444
|
+
options: { temperature: 0.7, top_p: 0.8, top_k: 20, min_p: 0, presence_penalty: 1.5, repeat_penalty: 1 },
|
|
2445
|
+
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.",
|
|
2446
|
+
sources
|
|
2447
|
+
});
|
|
2448
|
+
var CHAT_MODELS = {
|
|
2449
|
+
lfm3: {
|
|
2450
|
+
options: { temperature: 0.1, top_k: 50, repeat_penalty: 1.1 },
|
|
2451
|
+
notes: "Thinks by default; its card calls thinking off inconsistent, so it stays on. Not for agentic coding or multi-step reasoning.",
|
|
2452
|
+
sources: SOURCES.lfm3
|
|
2453
|
+
},
|
|
2454
|
+
"nvidia-nemotron-3.5-bolt": {
|
|
2455
|
+
options: { temperature: 1, top_p: 0.95 },
|
|
2456
|
+
// `ollama show` of the hf.co Q4_K_M tag, 2026-11-08: no thinking capability, and the template
|
|
2457
|
+
// defaults enable_thinking to false.
|
|
2458
|
+
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.",
|
|
2459
|
+
sources: SOURCES.nemotronBolt
|
|
2460
|
+
},
|
|
2461
|
+
"bonsai-2": BONSAI_9B,
|
|
2462
|
+
// The card documents the same instruct (non-thinking) preset as the 9B; Ollama lists think
|
|
2463
|
+
// values false, low, medium and xhigh for this tag.
|
|
2464
|
+
"bonsai-2:27b": {
|
|
2465
|
+
...BONSAI_9B,
|
|
2466
|
+
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.",
|
|
2467
|
+
sources: SOURCES.bonsai27b
|
|
2468
|
+
},
|
|
2469
|
+
"qwen3.8": qwenNonThinking(SOURCES.qwen38),
|
|
2470
|
+
"qwen3.5": qwenNonThinking(SOURCES.qwen35)
|
|
2471
|
+
};
|
|
2472
|
+
var CHAT_SOURCES = [...SOURCES.lfm3, ...SOURCES.nemotronBolt, ...SOURCES.bonsai9b, ...SOURCES.bonsai27b, ...SOURCES.qwen38, ...SOURCES.qwen35];
|
|
2473
|
+
var LOCAL_JOBS = {
|
|
2474
|
+
classify: {
|
|
2475
|
+
job: "classify",
|
|
2476
|
+
task: "decision",
|
|
2477
|
+
catalogJob: "classify",
|
|
2478
|
+
defaultPicks: picksOf("classify"),
|
|
2479
|
+
models: {
|
|
2480
|
+
"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 },
|
|
2481
|
+
"nimble-v3": { notes: "Documents num_ctx 32768 (routing sizes it here); CC BY-NC 4.0, noncommercial.", sources: SOURCES.nimble }
|
|
2482
|
+
},
|
|
2483
|
+
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.",
|
|
2484
|
+
sources: [...SOURCES.strandsDecider, ...SOURCES.nimble, cookbook("classify")]
|
|
2485
|
+
},
|
|
2486
|
+
"find-code": {
|
|
2487
|
+
job: "find-code",
|
|
2488
|
+
task: "embedding",
|
|
2489
|
+
catalogJob: "find-code",
|
|
2490
|
+
defaultPicks: picksOf("find-code"),
|
|
2491
|
+
needs: "nearest.query: the issue, or what the code does",
|
|
2492
|
+
prefixes: { embeddinggemma: { query: GEMMA.codeQuery, document: GEMMA.document }, "nomic-embed-text": { query: NOMIC.query, document: NOMIC.document } },
|
|
2493
|
+
// Measured 2026-11-08 on the find-code set (10 questions, 1,039 chunks): code retrieval with
|
|
2494
|
+
// title: none 8 first / 9 in top 3; search result 8/8; code retrieval with the file as title 7/9.
|
|
2495
|
+
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.",
|
|
2496
|
+
sources: [...SOURCES.embeddingGemma, ...SOURCES.nomic, cookbook("find-code")]
|
|
2497
|
+
},
|
|
2498
|
+
duplicates: {
|
|
2499
|
+
job: "duplicates",
|
|
2500
|
+
task: "embedding",
|
|
2501
|
+
catalogJob: "duplicates",
|
|
2502
|
+
defaultPicks: picksOf("duplicates"),
|
|
2503
|
+
// Symmetric: every item, the query included, takes the same prefix.
|
|
2504
|
+
prefixes: {
|
|
2505
|
+
// The measured 8/12 used search_document:; the card's clustering: is documented but unmeasured here.
|
|
2506
|
+
"nomic-embed-text": { query: NOMIC.document, document: NOMIC.document },
|
|
2507
|
+
embeddinggemma: { query: GEMMA.similarity, document: GEMMA.similarity }
|
|
2508
|
+
},
|
|
2509
|
+
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.",
|
|
2510
|
+
sources: [...SOURCES.nomic, ...SOURCES.embeddingGemma, cookbook("duplicates")]
|
|
2511
|
+
},
|
|
2512
|
+
ocr: {
|
|
2513
|
+
job: "ocr",
|
|
2514
|
+
task: "vision",
|
|
2515
|
+
catalogJob: "ocr",
|
|
2516
|
+
defaultPicks: picksOf("ocr"),
|
|
2517
|
+
prompt: ({ model }) => ocrPrompt("text", model),
|
|
2518
|
+
// Both publishers' Ollama Modelfiles set temperature 0; the hf.co MiniCPM tag ships no parameters.
|
|
2519
|
+
models: {
|
|
2520
|
+
"minicpm-ocr": {
|
|
2521
|
+
options: { temperature: 0 },
|
|
2522
|
+
notes: "Chinese and English; tables come back as HTML. Its Modelfile sets num_ctx 8192.",
|
|
2523
|
+
sources: [...SOURCES.miniCpmOcr, "https://raw.githubusercontent.com/OpenBMB/MiniCPM-OCR/main/docs/ollama_deploy.md"]
|
|
2524
|
+
},
|
|
2525
|
+
"glm-ocr": {
|
|
2526
|
+
options: { temperature: 0 },
|
|
2527
|
+
notes: "Also documents Figure Recognition:.",
|
|
2528
|
+
sources: [...SOURCES.glmOcr, "https://github.com/zai-org/GLM-OCR/blob/main/examples/ollama-deploy/README.md"]
|
|
2529
|
+
}
|
|
2530
|
+
},
|
|
2531
|
+
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.",
|
|
2532
|
+
sources: [...SOURCES.miniCpmOcr, ...SOURCES.glmOcr, cookbook("ocr")]
|
|
2533
|
+
},
|
|
2534
|
+
"image-recognition": {
|
|
2535
|
+
job: "image-recognition",
|
|
2536
|
+
task: "vision",
|
|
2537
|
+
catalogJob: "image-recognition",
|
|
2538
|
+
defaultPicks: picksOf("image-recognition"),
|
|
2539
|
+
needs: "prompt: your question about the image",
|
|
2540
|
+
systemPrompt: WORKFLOWS["answer-image"].system,
|
|
2541
|
+
models: {
|
|
2542
|
+
"lfm3-vl": {
|
|
2543
|
+
options: { temperature: 0.1, top_p: 1, top_k: 50, min_p: 0, repeat_penalty: 1.1 },
|
|
2544
|
+
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.",
|
|
2545
|
+
sources: SOURCES.lfm3Vl
|
|
2546
|
+
},
|
|
2547
|
+
"lfm3-omni": {
|
|
2548
|
+
systemPrompt: "Answer questions about the provided image. Base every answer only on what can be seen.",
|
|
2549
|
+
options: { temperature: 0 },
|
|
2550
|
+
notes: "Non-reasoning: never send think:true. Up to 2,048 tokens per image. The card gives 32K context; the Ollama tag advertises 128K.",
|
|
2551
|
+
sources: SOURCES.lfm3Omni
|
|
2552
|
+
},
|
|
2553
|
+
"bonsai-2": BONSAI_9B,
|
|
2554
|
+
"qwen3.8": qwenNonThinking(SOURCES.qwen38),
|
|
2555
|
+
"qwen3.5": qwenNonThinking(SOURCES.qwen35)
|
|
2556
|
+
},
|
|
2557
|
+
notes: 'Ask a specific question, not "describe this". Tested on drawn images only.',
|
|
2558
|
+
sources: [...SOURCES.lfm3Vl, ...SOURCES.lfm3Omni, ...SOURCES.bonsai9b, cookbook("image-recognition")]
|
|
2559
|
+
},
|
|
2560
|
+
"image-search": {
|
|
2561
|
+
job: "image-search",
|
|
2562
|
+
task: "embedding",
|
|
2563
|
+
catalogJob: "image-search",
|
|
2564
|
+
defaultPicks: picksOf("image-search"),
|
|
2565
|
+
needs: "nearest.query: a description of the image",
|
|
2566
|
+
prefixes: { embeddinggemma: { query: GEMMA.query, document: GEMMA.document } },
|
|
2567
|
+
notes: "Images are embedded bare: the prefixes apply to text only. Open the top match.",
|
|
2568
|
+
sources: [...SOURCES.embeddingGemma, cookbook("image-search")]
|
|
2569
|
+
},
|
|
2570
|
+
coding: {
|
|
2571
|
+
job: "coding",
|
|
2572
|
+
task: "coding",
|
|
2573
|
+
catalogJob: "coding",
|
|
2574
|
+
defaultPicks: picksOf("coding"),
|
|
2575
|
+
needs: "prompt: the change to make",
|
|
2576
|
+
systemPrompt: WORKFLOWS["change-code"].system,
|
|
2577
|
+
models: CHAT_MODELS,
|
|
2578
|
+
notes: "The answer is SEARCH/REPLACE blocks: apply one only where its SEARCH text matches exactly once, then run the tests.",
|
|
2579
|
+
sources: [...CHAT_SOURCES, cookbook("coding")]
|
|
2580
|
+
},
|
|
2581
|
+
summarize: {
|
|
2582
|
+
job: "summarize",
|
|
2583
|
+
task: "completion",
|
|
2584
|
+
catalogJob: "coding",
|
|
2585
|
+
defaultPicks: picksOf("coding"),
|
|
2586
|
+
prompt: () => "Summarize the text below.",
|
|
2587
|
+
systemPrompt: WORKFLOWS["summarize-text"].system,
|
|
2588
|
+
models: CHAT_MODELS,
|
|
2589
|
+
notes: "The coding picks: the same chat models apply (docs/cookbooks/batch-text.md). Check every number and name you reuse.",
|
|
2590
|
+
sources: [...CHAT_SOURCES, cookbook("batch-text")]
|
|
2591
|
+
}
|
|
2592
|
+
};
|
|
2593
|
+
var TOOL_JOBS = {
|
|
2594
|
+
"speech-to-text": {
|
|
2595
|
+
tool: "audio",
|
|
2596
|
+
defaultPicks: picksOf("speech-to-text"),
|
|
2597
|
+
// The audio tool calls Ollama's transcription endpoint, which builds the model's prompt; these
|
|
2598
|
+
// are lfm3-audio's documented prompts for a direct chat call.
|
|
2599
|
+
systemPrompts: {
|
|
2600
|
+
transcribe: (language) => language ? `Perform ASR in ${language}.` : "Perform ASR.",
|
|
2601
|
+
translate: (source, target) => `Translate the following speech from ${source} to ${target} in text form.`
|
|
2602
|
+
},
|
|
2603
|
+
notes: "WAV input only. Numbers and names are the usual misses; translation works best to or from English.",
|
|
2604
|
+
sources: [...SOURCES.lfm3Audio, cookbook("speech-to-text")]
|
|
2605
|
+
},
|
|
2606
|
+
"text-to-speech": {
|
|
2607
|
+
tool: "audio",
|
|
2608
|
+
defaultPicks: picksOf("text-to-speech"),
|
|
2609
|
+
notes: "Send plain text: Markdown marks may be read aloud. One voice (default); 24 kHz mono WAV.",
|
|
2610
|
+
sources: [...SOURCES.phonon, cookbook("text-to-speech")]
|
|
2611
|
+
},
|
|
2612
|
+
"research-agent": {
|
|
2613
|
+
tool: "delegate_research",
|
|
2614
|
+
defaultPicks: picksOf("research-agent"),
|
|
2615
|
+
notes: "The adapters' system prompts are below. Discard an escalate verdict and verify the citations.",
|
|
2616
|
+
sources: [cookbook("research-agent")]
|
|
2617
|
+
},
|
|
2618
|
+
sweep: {
|
|
2619
|
+
tool: "sweep",
|
|
2620
|
+
defaultPicks: picksOf("classify"),
|
|
2621
|
+
/** The decision state of one chunk, and the explain step's user message for a confirmed flag. */
|
|
2622
|
+
state: (label, text) => `Source: ${label}
|
|
2623
|
+
|
|
2624
|
+
${text}`,
|
|
2625
|
+
explain: (prompt, label, text) => `${prompt}
|
|
2626
|
+
|
|
2627
|
+
Source: ${label}
|
|
2628
|
+
|
|
2629
|
+
${text}`,
|
|
2630
|
+
// FreeLlama's own setting for the short explain note, not a model's documented value.
|
|
2631
|
+
explainControls: { think: false, temperature: 0 },
|
|
2632
|
+
notes: 'One yes/no question per concern, phrased about one chunk ("Does this code ..."); a noul answer flags at p >= 0.5.',
|
|
2633
|
+
sources: [cookbook("sweep")]
|
|
2634
|
+
}
|
|
2635
|
+
};
|
|
2636
|
+
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.
|
|
2637
|
+
Return exactly one JSON object per turn, and nothing else:
|
|
2638
|
+
Run a tool, in place of the shell command above: {"action":"octocode","tool":"${example}","queries":[{...}]}
|
|
2639
|
+
Read another page of an earlier step's stored output: {"action":"page","step":2,"page":2}
|
|
2640
|
+
Finish: {"action":"finish","answer":"concise answer with its evidence"}
|
|
2641
|
+
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.
|
|
2642
|
+
Most questions need 2 to 6 calls. An invalid action is rejected and costs a turn. Never edit or write anything.`;
|
|
1546
2643
|
function octocodeSystemPrompt(workspace) {
|
|
1547
|
-
return
|
|
2644
|
+
return `You are a local code-research agent in a read-only workspace at ${workspace}. Paths are relative to it.
|
|
2645
|
+
|
|
2646
|
+
${OCTOCODE_LOCAL_INSTRUCTIONS}
|
|
2647
|
+
${ACTION_PROTOCOL("localSearch")}`;
|
|
2648
|
+
}
|
|
2649
|
+
var GH_ACTION = `You may also run the GitHub CLI, read-only, as {"action":"gh","args":[...]} (arguments as an array; no shell):
|
|
2650
|
+
{"action":"gh","args":["search","code","SystemOneRequest","--repo","ollama/ollama"]} finds code; add "--filename","types.go" or "--language","go" to narrow it.
|
|
2651
|
+
{"action":"gh","args":["api","repos/ollama/ollama/contents/api/types.go","--jq",".content"]} reads a file (base64), or use ghGetFileContent for decoded text.
|
|
2652
|
+
{"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"].
|
|
2653
|
+
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.`;
|
|
2654
|
+
var CODER_SYSTEM_PROMPT = [
|
|
2655
|
+
"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.",
|
|
2656
|
+
"",
|
|
2657
|
+
"Actions, in order of preference:",
|
|
2658
|
+
'{"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.',
|
|
2659
|
+
'{"action":"read","path":"src/config.ts","start":40,"end":70} shows numbered lines of one file.',
|
|
2660
|
+
'{"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.',
|
|
2661
|
+
'{"action":"write","path":"src/limits.ts","content":"export const MAX = 10;\\n"} creates a file, or replaces a whole small file.',
|
|
2662
|
+
'{"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.',
|
|
2663
|
+
'{"action":"list","path":"src"} shows files and folders two levels deep.',
|
|
2664
|
+
'{"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.',
|
|
2665
|
+
'{"action":"page","step":2,"page":2} shows the next page of a long earlier result.',
|
|
2666
|
+
'{"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.',
|
|
2667
|
+
"",
|
|
2668
|
+
"How to work:",
|
|
2669
|
+
"1. Search for the names in the task, then read the lines you will change.",
|
|
2670
|
+
"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.",
|
|
2671
|
+
"3. Run an allowed command to test the change. If it fails, read the error, fix the code and run it again.",
|
|
2672
|
+
"4. Finish with what changed, why, and the last test result. With no allowed command, say the change is untested.",
|
|
2673
|
+
"Keep results small: read line ranges, not whole files, and end long shell output with | head -n 40."
|
|
2674
|
+
].join("\n");
|
|
2675
|
+
function githubSystemPrompt() {
|
|
2676
|
+
return `You are a code-research agent that answers questions about GitHub repositories and packages, read-only.
|
|
2677
|
+
|
|
2678
|
+
${OCTOCODE_GITHUB_INSTRUCTIONS}
|
|
2679
|
+
${ACTION_PROTOCOL("ghSearchCode")}
|
|
2680
|
+
${GH_ACTION}`;
|
|
2681
|
+
}
|
|
2682
|
+
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.";
|
|
2683
|
+
var AGENT_GUIDE = `Use FreeLlama to run bounded work on local models and keep it out of your context.
|
|
2684
|
+
|
|
2685
|
+
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.
|
|
2686
|
+
|
|
2687
|
+
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.
|
|
2688
|
+
|
|
2689
|
+
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".
|
|
2690
|
+
|
|
2691
|
+
Coding: the answer is SEARCH/REPLACE blocks. ${APPLY_EDITS}
|
|
2692
|
+
|
|
2693
|
+
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]. Answers over 8,000 characters come back as a summary with check: lines and an output_file holding the whole answer; output:"inline" returns the whole text, outputPath:"<path>" writes (or replaces) that file inside your project.
|
|
2694
|
+
|
|
2695
|
+
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.`;
|
|
2696
|
+
|
|
2697
|
+
// src/research/workspace-overview.ts
|
|
2698
|
+
import { readdirSync as readdirSync2 } from "node:fs";
|
|
2699
|
+
import path6 from "node:path";
|
|
2700
|
+
var SKIP = /* @__PURE__ */ new Set(["node_modules", "target", ".git", "dist", "build", ".venv", "venv", "vendor", "__pycache__", ".octocode", "site-packages", "coverage"]);
|
|
2701
|
+
var MAX_ENTRIES = 2e4;
|
|
2702
|
+
var MAX_DEPTH = 8;
|
|
2703
|
+
var SOURCE_DIRS = /* @__PURE__ */ new Set(["src", "lib", "app", "source", "pkg", "cmd", "internal", "crates"]);
|
|
2704
|
+
function workspaceOverview(root) {
|
|
2705
|
+
let top;
|
|
2706
|
+
try {
|
|
2707
|
+
top = readdirSync2(root, { withFileTypes: true });
|
|
2708
|
+
} catch {
|
|
2709
|
+
return "";
|
|
2710
|
+
}
|
|
2711
|
+
const extensions = /* @__PURE__ */ new Map();
|
|
2712
|
+
const sourceRoots = /* @__PURE__ */ new Map();
|
|
2713
|
+
let files = 0;
|
|
2714
|
+
let seen = 0;
|
|
2715
|
+
const walk = (dir, depth, sourceRoot) => {
|
|
2716
|
+
if (depth > MAX_DEPTH || seen > MAX_ENTRIES) return;
|
|
2717
|
+
let entries;
|
|
2718
|
+
try {
|
|
2719
|
+
entries = readdirSync2(dir, { withFileTypes: true });
|
|
2720
|
+
} catch {
|
|
2721
|
+
return;
|
|
2722
|
+
}
|
|
2723
|
+
for (const entry of entries) {
|
|
2724
|
+
if (++seen > MAX_ENTRIES) return;
|
|
2725
|
+
if (entry.isDirectory()) {
|
|
2726
|
+
if (SKIP.has(entry.name) || entry.name.startsWith(".")) continue;
|
|
2727
|
+
const child = path6.join(dir, entry.name);
|
|
2728
|
+
walk(child, depth + 1, sourceRoot ?? (SOURCE_DIRS.has(entry.name) ? path6.relative(root, child) : void 0));
|
|
2729
|
+
} else if (entry.isFile()) {
|
|
2730
|
+
files += 1;
|
|
2731
|
+
const extension = path6.extname(entry.name).toLowerCase() || "(none)";
|
|
2732
|
+
extensions.set(extension, (extensions.get(extension) ?? 0) + 1);
|
|
2733
|
+
if (sourceRoot) sourceRoots.set(sourceRoot, (sourceRoots.get(sourceRoot) ?? 0) + 1);
|
|
2734
|
+
}
|
|
2735
|
+
}
|
|
2736
|
+
};
|
|
2737
|
+
walk(root, 0);
|
|
2738
|
+
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);
|
|
2739
|
+
const types = [...extensions.entries()].sort((left, right) => right[1] - left[1]).slice(0, 8).map(([extension, count]) => `${extension} ${count}`);
|
|
2740
|
+
const sources = [...sourceRoots.entries()].sort((left, right) => right[1] - left[1]).slice(0, 8).map(([dir, count]) => `${dir}/ (${count})`);
|
|
2741
|
+
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.";
|
|
2742
|
+
}
|
|
2743
|
+
|
|
2744
|
+
// src/research/gh-action.ts
|
|
2745
|
+
var READ_ONLY = {
|
|
2746
|
+
search: /* @__PURE__ */ new Set(["code", "repos", "issues", "prs", "commits"]),
|
|
2747
|
+
repo: /* @__PURE__ */ new Set(["view", "list"]),
|
|
2748
|
+
pr: /* @__PURE__ */ new Set(["view", "list", "diff", "checks"]),
|
|
2749
|
+
issue: /* @__PURE__ */ new Set(["view", "list"]),
|
|
2750
|
+
release: /* @__PURE__ */ new Set(["view", "list"]),
|
|
2751
|
+
api: /* @__PURE__ */ new Set()
|
|
2752
|
+
};
|
|
2753
|
+
var FORBIDDEN = /^(?:-X|--method|-f|-F|--field|--raw-field|--input|-H|--header|-w|--web|--browser|-e|--edit|--hostname|--paginate-all)(?:=|$)/;
|
|
2754
|
+
var MAX_ARGS = 40;
|
|
2755
|
+
function validateGhArgs(args) {
|
|
2756
|
+
if (!Array.isArray(args) || args.length === 0 || args.length > MAX_ARGS || !args.every((arg) => typeof arg === "string" && !arg.includes("\n"))) {
|
|
2757
|
+
throw new ValidationError(`gh action requires "args": an array of 1-${MAX_ARGS} strings, e.g. ["search","code","SystemOneRequest","--repo","ollama/ollama"]`);
|
|
2758
|
+
}
|
|
2759
|
+
const words = args;
|
|
2760
|
+
const [group, sub] = words;
|
|
2761
|
+
const allowed = READ_ONLY[group];
|
|
2762
|
+
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)`);
|
|
2763
|
+
if (allowed.size > 0 && !allowed.has(sub ?? "")) {
|
|
2764
|
+
throw new ValidationError(`gh ${group} ${sub ?? ""} is not read-only; allowed: ${[...allowed].map((name) => `gh ${group} ${name}`).join(", ")}`);
|
|
2765
|
+
}
|
|
2766
|
+
const bad = words.find((word) => FORBIDDEN.test(word));
|
|
2767
|
+
if (bad) throw new ValidationError(`gh flag ${bad} is not allowed: the gh action only reads`);
|
|
2768
|
+
if (group === "api" && (!sub || sub.startsWith("-") || /graphql/i.test(sub))) {
|
|
2769
|
+
throw new ValidationError("gh api needs a REST path such as repos/ollama/ollama/contents/api/types.go (GET only; no graphql)");
|
|
2770
|
+
}
|
|
2771
|
+
return words;
|
|
2772
|
+
}
|
|
2773
|
+
async function runGh(args, cwd, timeoutSeconds) {
|
|
2774
|
+
const result = await runProcess(["gh", ...validateGhArgs(args)], {
|
|
2775
|
+
cwd,
|
|
2776
|
+
// gh reads its login from the operator's config and keychain; nothing prompts.
|
|
2777
|
+
env: { ...process.env, GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1", NO_COLOR: "1", GH_PAGER: "cat" },
|
|
2778
|
+
timeoutSeconds
|
|
2779
|
+
});
|
|
2780
|
+
const output = trimSpace(result.stdout);
|
|
2781
|
+
if (result.returncode !== 0) throw new ToolExitError(result.returncode, `${output}
|
|
2782
|
+
${trimSpace(result.stderr)}`.trim());
|
|
2783
|
+
return output || "(no results)";
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
// src/research/entry.ts
|
|
2787
|
+
import path7 from "node:path";
|
|
2788
|
+
import { pathToFileURL } from "node:url";
|
|
2789
|
+
function isEntry(moduleUrl, name, script = process.argv[1]) {
|
|
2790
|
+
if (!script || moduleUrl !== pathToFileURL(script).href) return false;
|
|
2791
|
+
return path7.basename(script).replace(/\.[cm]?[jt]s$/, "") === name;
|
|
1548
2792
|
}
|
|
1549
2793
|
|
|
1550
2794
|
// src/research/octocode-agent.ts
|
|
1551
2795
|
var OCTOCODE_TOOLS = /* @__PURE__ */ new Set([
|
|
1552
|
-
"
|
|
1553
|
-
"
|
|
1554
|
-
"
|
|
1555
|
-
"
|
|
1556
|
-
"
|
|
2796
|
+
"structureSearch",
|
|
2797
|
+
"localSearch",
|
|
2798
|
+
"localFetch",
|
|
2799
|
+
"astSearch",
|
|
2800
|
+
"lspSearch"
|
|
1557
2801
|
]);
|
|
1558
|
-
var
|
|
1559
|
-
|
|
1560
|
-
|
|
2802
|
+
var GITHUB_TOOLS = /* @__PURE__ */ new Set([
|
|
2803
|
+
"ghSearchRepo",
|
|
2804
|
+
"artifactSearch",
|
|
2805
|
+
"ghStructure",
|
|
2806
|
+
"ghSearchCode",
|
|
2807
|
+
"ghGetFileContent",
|
|
2808
|
+
"ghSearchHistory",
|
|
2809
|
+
"ghGetHistoryItem"
|
|
2810
|
+
]);
|
|
2811
|
+
var MAX_QUERIES = 5;
|
|
2812
|
+
var OCTOCODE_BIN = process.env.FREELLAMA_AGENT_OCTOCODE_BIN ?? "octocode";
|
|
1561
2813
|
function safeResolve(root, value) {
|
|
1562
2814
|
const candidate = resolvePath(joinPath(root, value));
|
|
1563
2815
|
if (escapesRoot(candidate, root)) throw new ValidationError(`path escapes benchmark workspace: ${value}`);
|
|
1564
2816
|
if (candidate.split("/").includes(".git")) throw new ValidationError("git metadata is outside the task surface");
|
|
1565
2817
|
return candidate;
|
|
1566
2818
|
}
|
|
1567
|
-
function
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
2819
|
+
function queryList(queries) {
|
|
2820
|
+
if (isRecord(queries)) return [queries];
|
|
2821
|
+
return Array.isArray(queries) ? queries.filter(isRecord) : [];
|
|
2822
|
+
}
|
|
2823
|
+
function normalizeAction(value, tools) {
|
|
2824
|
+
const named = typeof value.action === "string" && tools.has(value.action) ? value.action : void 0;
|
|
2825
|
+
if (named === void 0 && value.action !== "octocode") return value;
|
|
2826
|
+
const { action: _action, tool: named2, queries, ...fields } = value;
|
|
2827
|
+
const tool = named ?? named2;
|
|
2828
|
+
if (tool === void 0) return value;
|
|
2829
|
+
const query = queries ?? (Object.keys(fields).length > 0 ? fields : void 0);
|
|
2830
|
+
return { action: "octocode", tool, ...query === void 0 ? {} : { queries: query } };
|
|
2831
|
+
}
|
|
2832
|
+
function actionParser(tools, gh = false) {
|
|
2833
|
+
const names = [...tools].sort().join(", ");
|
|
2834
|
+
return (content) => {
|
|
2835
|
+
const value = normalizeAction(parseJsonAction(content), tools);
|
|
2836
|
+
const action = value.action;
|
|
2837
|
+
if (gh && action === "gh") {
|
|
2838
|
+
validateGhArgs(value.args);
|
|
2839
|
+
return value;
|
|
1574
2840
|
}
|
|
1575
|
-
if (
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
2841
|
+
if (action === "octocode") {
|
|
2842
|
+
const tool = value.tool;
|
|
2843
|
+
if (typeof tool !== "string" || !tools.has(tool)) {
|
|
2844
|
+
throw new ValidationError(`octocode action requires one supported tool: ${names}`);
|
|
2845
|
+
}
|
|
2846
|
+
const queries = queryList(value.queries);
|
|
2847
|
+
if (queries.length === 0 || queries.length > MAX_QUERIES) {
|
|
2848
|
+
throw new ValidationError(`octocode action requires queries: 1 to ${MAX_QUERIES} query objects`);
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
if (action === "page" && (!isPositiveInt(value.step) || !isPositiveInt(value.page))) {
|
|
2852
|
+
throw new ValidationError("page action requires positive integer step and page");
|
|
2853
|
+
}
|
|
2854
|
+
if (action === "finish" && typeof value.answer !== "string") {
|
|
2855
|
+
throw new ValidationError("finish action requires a string answer");
|
|
2856
|
+
}
|
|
2857
|
+
if (action !== "octocode" && action !== "page" && action !== "finish") {
|
|
2858
|
+
throw new ValidationError(`unsupported action for octocode adapter: ${String(action)}${gh ? " (use octocode, gh, page or finish)" : ""}`);
|
|
2859
|
+
}
|
|
2860
|
+
return value;
|
|
2861
|
+
};
|
|
2862
|
+
}
|
|
2863
|
+
var parseAction = actionParser(OCTOCODE_TOOLS);
|
|
2864
|
+
async function runCli(root, tool, queries, timeoutSeconds) {
|
|
2865
|
+
const result = await runProcess([OCTOCODE_BIN, tool, JSON.stringify({ queries })], { cwd: root, timeoutSeconds });
|
|
2866
|
+
const parts = [trimSpace(result.stdout), trimSpace(result.stderr)].filter(Boolean);
|
|
2867
|
+
const output = parts.join("\n") || "(empty result)";
|
|
2868
|
+
if (result.returncode !== 0) {
|
|
2869
|
+
throw new ToolExitError(result.returncode, output, result.returncode === 1 ? "empty" : "error");
|
|
1585
2870
|
}
|
|
1586
|
-
return
|
|
2871
|
+
return output;
|
|
1587
2872
|
}
|
|
1588
2873
|
async function runOctocode(root, toolName, queries, timeoutSeconds) {
|
|
1589
2874
|
if (!OCTOCODE_TOOLS.has(toolName)) throw new ValidationError(`unsupported octocode tool: ${toolName}`);
|
|
1590
|
-
const
|
|
1591
|
-
for (const
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
const command = ["npx", "--yes", OCTOCODE_PACKAGE, "tools", toolName, "--queries", JSON.stringify(resolved), "--compact"];
|
|
1596
|
-
const result = await runProcess(command, { cwd: root, timeoutSeconds });
|
|
1597
|
-
let output = result.stdout + (result.returncode !== 0 ? `
|
|
1598
|
-
STDERR:
|
|
1599
|
-
${result.stderr}` : "");
|
|
1600
|
-
if (!trimSpace(output)) output = "(empty result)";
|
|
1601
|
-
return output;
|
|
2875
|
+
const list = queryList(queries);
|
|
2876
|
+
for (const query of list) {
|
|
2877
|
+
if (typeof query.path === "string") safeResolve(root, query.path);
|
|
2878
|
+
}
|
|
2879
|
+
return runCli(root, toolName, list, timeoutSeconds);
|
|
1602
2880
|
}
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
2881
|
+
async function runGithub(root, toolName, queries, timeoutSeconds) {
|
|
2882
|
+
if (!GITHUB_TOOLS.has(toolName)) throw new ValidationError(`unsupported GitHub tool: ${toolName}`);
|
|
2883
|
+
return runCli(root, toolName, queryList(queries), timeoutSeconds);
|
|
2884
|
+
}
|
|
2885
|
+
function adapterFor(options) {
|
|
2886
|
+
return {
|
|
2887
|
+
defaultToolTimeoutSeconds: 45,
|
|
2888
|
+
systemPrompt: options.prompt,
|
|
2889
|
+
...options.briefing ? { briefing: options.briefing } : {},
|
|
2890
|
+
parseAction: actionParser(options.tools, options.gh),
|
|
2891
|
+
open: () => ({
|
|
2892
|
+
toolCall: (action, workspace, runtime) => {
|
|
2893
|
+
if (options.gh && action.action === "gh") {
|
|
2894
|
+
const args = validateGhArgs(action.args);
|
|
2895
|
+
return {
|
|
2896
|
+
signature: { gh: args },
|
|
2897
|
+
run: () => runGh(args, workspace, runtime.tool_timeout_seconds),
|
|
2898
|
+
record: { name: "gh", raw_name: "gh", arguments: { args } },
|
|
2899
|
+
ledger: (status) => ({ action: "gh", status, source: args.join(" ") })
|
|
2900
|
+
};
|
|
2901
|
+
}
|
|
2902
|
+
const toolName = String(action.tool ?? "");
|
|
2903
|
+
const queries = queryList(action.queries);
|
|
2904
|
+
return {
|
|
2905
|
+
signature: { tool: toolName, queries },
|
|
2906
|
+
run: () => options.run(workspace, toolName, queries, runtime.tool_timeout_seconds),
|
|
2907
|
+
record: {
|
|
2908
|
+
name: toolName ? `mcp.octocode.${toolName}` : "mcp.octocode.unknown",
|
|
2909
|
+
raw_name: action.action ?? "",
|
|
2910
|
+
arguments: { tool: toolName, queries }
|
|
2911
|
+
},
|
|
2912
|
+
ledger: (status) => ({ action: toolName, status, source: canonicalJson(queries, { sortKeys: false, ensureAscii: false }) })
|
|
2913
|
+
};
|
|
2914
|
+
}
|
|
2915
|
+
}),
|
|
2916
|
+
remaining: (count) => `
|
|
1624
2917
|
|
|
1625
2918
|
Tool calls remaining: ${count}. Finish now if the task is answerable; do not repeat prior calls.`,
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
};
|
|
1629
|
-
|
|
2919
|
+
exhausted: "Tool budget is exhausted. Return exactly a finish JSON action now using the evidence collected. Do not request another tool.",
|
|
2920
|
+
adapterMetadata: () => ({ adapter: options.metadata })
|
|
2921
|
+
};
|
|
2922
|
+
}
|
|
2923
|
+
var OCTOCODE_ADAPTER = adapterFor({ tools: OCTOCODE_TOOLS, prompt: octocodeSystemPrompt, run: runOctocode, metadata: "octocode_cli_agent_v2", briefing: workspaceOverview });
|
|
2924
|
+
var GITHUB_ADAPTER = adapterFor({ tools: GITHUB_TOOLS, prompt: githubSystemPrompt, run: runGithub, metadata: "octocode_github_agent_v2", gh: true });
|
|
2925
|
+
if (isEntry(import.meta.url, "octocode-agent")) {
|
|
1630
2926
|
process.exitCode = await runAdapter(OCTOCODE_ADAPTER);
|
|
1631
2927
|
}
|
|
1632
2928
|
export {
|
|
2929
|
+
GITHUB_ADAPTER,
|
|
2930
|
+
GITHUB_TOOLS,
|
|
1633
2931
|
OCTOCODE_ADAPTER,
|
|
1634
|
-
|
|
2932
|
+
OCTOCODE_BIN,
|
|
1635
2933
|
OCTOCODE_TOOLS,
|
|
1636
2934
|
parseAction,
|
|
2935
|
+
queryList,
|
|
2936
|
+
runGithub,
|
|
1637
2937
|
runOctocode,
|
|
1638
2938
|
safeResolve
|
|
1639
2939
|
};
|