@caupulican/pi-adaptative 0.80.96 → 0.80.98
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/CHANGELOG.md +35 -0
- package/dist/core/agent-session.d.ts +19 -2
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +185 -6
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/autonomy/envelope-enforcement.d.ts +17 -0
- package/dist/core/autonomy/envelope-enforcement.d.ts.map +1 -0
- package/dist/core/autonomy/envelope-enforcement.js +80 -0
- package/dist/core/autonomy/envelope-enforcement.js.map +1 -0
- package/dist/core/context/brain-curator.d.ts +7 -0
- package/dist/core/context/brain-curator.d.ts.map +1 -1
- package/dist/core/context/brain-curator.js +6 -0
- package/dist/core/context/brain-curator.js.map +1 -1
- package/dist/core/context/context-composition.d.ts.map +1 -1
- package/dist/core/context/context-composition.js +1 -1
- package/dist/core/context/context-composition.js.map +1 -1
- package/dist/core/delegation/session-worker-result.d.ts +8 -2
- package/dist/core/delegation/session-worker-result.d.ts.map +1 -1
- package/dist/core/delegation/session-worker-result.js +18 -1
- package/dist/core/delegation/session-worker-result.js.map +1 -1
- package/dist/core/learning/observation-store.d.ts +20 -0
- package/dist/core/learning/observation-store.d.ts.map +1 -0
- package/dist/core/learning/observation-store.js +101 -0
- package/dist/core/learning/observation-store.js.map +1 -0
- package/dist/core/model-router/executor-route.d.ts +8 -0
- package/dist/core/model-router/executor-route.d.ts.map +1 -0
- package/dist/core/model-router/executor-route.js +33 -0
- package/dist/core/model-router/executor-route.js.map +1 -0
- package/dist/core/model-router/tool-escalation.d.ts +2 -0
- package/dist/core/model-router/tool-escalation.d.ts.map +1 -1
- package/dist/core/model-router/tool-escalation.js +6 -0
- package/dist/core/model-router/tool-escalation.js.map +1 -1
- package/dist/core/research/research-runner.d.ts +8 -1
- package/dist/core/research/research-runner.d.ts.map +1 -1
- package/dist/core/research/research-runner.js +13 -1
- package/dist/core/research/research-runner.js.map +1 -1
- package/dist/core/research/workspace-collector.d.ts +25 -0
- package/dist/core/research/workspace-collector.d.ts.map +1 -0
- package/dist/core/research/workspace-collector.js +286 -0
- package/dist/core/research/workspace-collector.js.map +1 -0
- package/dist/core/settings-manager.d.ts +2 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +3 -0
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.d.ts +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.js +5 -0
- package/dist/modes/interactive/components/fitness-role-selector.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +7 -1
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +147 -0
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +21 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +12 -12
- package/package.json +4 -4
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
import { cloneWorkerResultForStorage, isWorkerResult } from "./worker-result.js";
|
|
2
2
|
export const WORKER_RESULT_CUSTOM_TYPE = "worker_result";
|
|
3
|
-
export function appendWorkerResultSnapshot(sessionManager, result) {
|
|
3
|
+
export function appendWorkerResultSnapshot(sessionManager, result, request) {
|
|
4
4
|
const payload = {
|
|
5
5
|
version: 1,
|
|
6
6
|
result: cloneWorkerResultForStorage(result),
|
|
7
|
+
...(request ? { request: structuredClone(request) } : {}),
|
|
7
8
|
};
|
|
8
9
|
return sessionManager.appendCustomEntry(WORKER_RESULT_CUSTOM_TYPE, payload);
|
|
9
10
|
}
|
|
11
|
+
/** Requests persisted alongside results (absent for pre-G2 entries). */
|
|
12
|
+
export function getWorkerRequestSnapshots(entries) {
|
|
13
|
+
const requests = [];
|
|
14
|
+
for (const entry of entries) {
|
|
15
|
+
if (entry.type !== "custom" || entry.customType !== WORKER_RESULT_CUSTOM_TYPE)
|
|
16
|
+
continue;
|
|
17
|
+
const payload = entry.data;
|
|
18
|
+
if (!isPlainRecord(payload) || payload.version !== 1)
|
|
19
|
+
continue;
|
|
20
|
+
const request = payload.request;
|
|
21
|
+
if (isPlainRecord(request) && typeof request.id === "string") {
|
|
22
|
+
requests.push(structuredClone(request));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return requests;
|
|
26
|
+
}
|
|
10
27
|
function isPlainRecord(value) {
|
|
11
28
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
12
29
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-worker-result.js","sourceRoot":"","sources":["../../../src/core/delegation/session-worker-result.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEjF,MAAM,CAAC,MAAM,yBAAyB,GAAG,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"session-worker-result.js","sourceRoot":"","sources":["../../../src/core/delegation/session-worker-result.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEjF,MAAM,CAAC,MAAM,yBAAyB,GAAG,eAAe,CAAC;AAWzD,MAAM,UAAU,0BAA0B,CACzC,cAAyD,EACzD,MAAoB,EACpB,OAAuB,EACd;IACT,MAAM,OAAO,GAAgC;QAC5C,OAAO,EAAE,CAAC;Q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,UAAU,wBAAwB,CAAC,OAAgC,EAAkB;IAC1F,MAAM,OAAO,GAAmB,EAAE,CAAC;IAEnC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,UAAU,KAAK,yBAAyB,EAAE,CAAC;YAC/E,SAAS;QACV,CAAC;QAED,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC;QAC3B,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC;YAAE,SAAS;QACtC,IAAI,OAAO,CAAC,OAAO,KAAK,CAAC;YAAE,SAAS;QACpC,IAAI,CAAC,CAAC,QAAQ,IAAI,OAAO,CAAC;YAAE,SAAS;QACrC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC9B,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAC5B,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC,MAAM,CAAC,CAAC,CAAC;QACnD,CAAC;IACF,CAAC;IAED,OAAO,OAAO,CAAC;AAAA,CACf","sourcesContent":["import type { WorkerRequest, WorkerResult } from \"../autonomy/contracts.ts\";\nimport type { SessionEntry, SessionManager } from \"../session-manager.ts\";\nimport { cloneWorkerResultForStorage, isWorkerResult } from \"./worker-result.ts\";\n\nexport const WORKER_RESULT_CUSTOM_TYPE = \"worker_result\";\n\nexport interface WorkerResultSnapshotPayload {\n\tversion: 1;\n\tresult: WorkerResult;\n\t/** The originating request (G2): persisted so a result is auditable against exactly what was\n\t * asked — instructions, route, and the capability envelope that bounded it. Optional for\n\t * backward compatibility with pre-G2 entries. */\n\trequest?: WorkerRequest;\n}\n\nexport function appendWorkerResultSnapshot(\n\tsessionManager: Pick<SessionManager, \"appendCustomEntry\">,\n\tresult: WorkerResult,\n\trequest?: WorkerRequest,\n): string {\n\tconst payload: WorkerResultSnapshotPayload = {\n\t\tversion: 1,\n\t\tresult: cloneWorkerResultForStorage(result),\n\t\t...(request ? { request: structuredClone(request) } : {}),\n\t};\n\treturn sessionManager.appendCustomEntry(WORKER_RESULT_CUSTOM_TYPE, payload);\n}\n\n/** Requests persisted alongside results (absent for pre-G2 entries). */\nexport function getWorkerRequestSnapshots(entries: readonly SessionEntry[]): WorkerRequest[] {\n\tconst requests: WorkerRequest[] = [];\n\tfor (const entry of entries) {\n\t\tif (entry.type !== \"custom\" || entry.customType !== WORKER_RESULT_CUSTOM_TYPE) continue;\n\t\tconst payload = entry.data;\n\t\tif (!isPlainRecord(payload) || payload.version !== 1) continue;\n\t\tconst request = payload.request;\n\t\tif (isPlainRecord(request) && typeof request.id === \"string\") {\n\t\t\trequests.push(structuredClone(request) as unknown as WorkerRequest);\n\t\t}\n\t}\n\treturn requests;\n}\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst prototype = Object.getPrototypeOf(value);\n\treturn prototype === Object.prototype || prototype === null;\n}\n\nexport function getWorkerResultSnapshots(entries: readonly SessionEntry[]): WorkerResult[] {\n\tconst results: WorkerResult[] = [];\n\n\tfor (const entry of entries) {\n\t\tif (entry.type !== \"custom\" || entry.customType !== WORKER_RESULT_CUSTOM_TYPE) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst payload = entry.data;\n\t\tif (!isPlainRecord(payload)) continue;\n\t\tif (payload.version !== 1) continue;\n\t\tif (!(\"result\" in payload)) continue;\n\t\tconst result = payload.result;\n\t\tif (isWorkerResult(result)) {\n\t\t\tresults.push(cloneWorkerResultForStorage(result));\n\t\t}\n\t}\n\n\treturn results;\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable evidence key for a durable-change proposal: sha256 of the layer plus its summary,
|
|
3
|
+
* lowercased and whitespace-collapsed, truncated to 24 hex chars. Normalization means the same
|
|
4
|
+
* lesson re-observed with reworded whitespace/casing accumulates onto one key instead of scattering.
|
|
5
|
+
*/
|
|
6
|
+
export declare function observationKey(layer: string, summary: string): string;
|
|
7
|
+
export declare class ObservationStore {
|
|
8
|
+
private readonly filePath;
|
|
9
|
+
constructor(filePath: string);
|
|
10
|
+
static forAgentDir(agentDir: string): ObservationStore;
|
|
11
|
+
private load;
|
|
12
|
+
private save;
|
|
13
|
+
/** Evict least-recently-incremented keys until the store is back within {@link MAX_KEYS}. */
|
|
14
|
+
private evict;
|
|
15
|
+
/** Record one more observation of `key` and return the new (capped) count. */
|
|
16
|
+
increment(key: string, at?: string): number;
|
|
17
|
+
/** Current observation count for `key` (0 if never observed). */
|
|
18
|
+
get(key: string): number;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=observation-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observation-store.d.ts","sourceRoot":"","sources":["../../../src/core/learning/observation-store.ts"],"names":[],"mappings":"AAgCA;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAGrE;AAED,qBAAa,gBAAgB;IAC5B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAElC,YAAY,QAAQ,EAAE,MAAM,EAE3B;IAED,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,gBAAgB,CAErD;IAED,OAAO,CAAC,IAAI;IAoCZ,OAAO,CAAC,IAAI;IAKZ,6FAA6F;IAC7F,OAAO,CAAC,KAAK;IAab,8EAA8E;IAC9E,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAQ1C;IAED,iEAAiE;IACjE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvB;CACD","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/**\n * Durable, BOUNDED evidence-strength store for the learning gate (G6). The gate auto-applies a\n * durable change only once it has been *observed* enough times; a single reflection pass sees a\n * lesson once, so without persistence every proposal would look brand-new and never accumulate the\n * repeated evidence the gate requires. This store counts how many times the SAME lesson (keyed by\n * its durable layer + normalized summary) has been proposed across passes and sessions.\n *\n * File layout mirrors {@link ../models/fitness-store.ts}: a versioned JSON object under\n * `<agentDir>/state/`, best-effort writes, and a corrupt/missing file recovers as a fresh store.\n */\n\n/** Cap per-key counts so a hot lesson can't grow unbounded (the gate only needs a small threshold). */\nconst MAX_COUNT = 100;\n/** Cap the number of tracked keys; least-recently-incremented keys are evicted past this bound. */\nconst MAX_KEYS = 500;\n\ninterface ObservationEntry {\n\tcount: number;\n\t/** ISO timestamp of the most recent increment; drives least-recently-incremented eviction. */\n\tlastAt: string;\n}\n\ninterface ObservationStoreFile {\n\tversion: 1;\n\t/** observationKey -> accumulated evidence for that lesson. */\n\tobservations: Record<string, ObservationEntry>;\n}\n\n/**\n * Stable evidence key for a durable-change proposal: sha256 of the layer plus its summary,\n * lowercased and whitespace-collapsed, truncated to 24 hex chars. Normalization means the same\n * lesson re-observed with reworded whitespace/casing accumulates onto one key instead of scattering.\n */\nexport function observationKey(layer: string, summary: string): string {\n\tconst normalized = summary.toLowerCase().replace(/\\s+/g, \" \").trim();\n\treturn createHash(\"sha256\").update(`${layer}\\n${normalized}`).digest(\"hex\").slice(0, 24);\n}\n\nexport class ObservationStore {\n\tprivate readonly filePath: string;\n\n\tconstructor(filePath: string) {\n\t\tthis.filePath = filePath;\n\t}\n\n\tstatic forAgentDir(agentDir: string): ObservationStore {\n\t\treturn new ObservationStore(join(agentDir, \"state\", \"learning-observations.json\"));\n\t}\n\n\tprivate load(): ObservationStoreFile {\n\t\ttry {\n\t\t\tif (!existsSync(this.filePath)) return { version: 1, observations: {} };\n\t\t\tconst parsed = JSON.parse(readFileSync(this.filePath, \"utf-8\")) as ObservationStoreFile;\n\t\t\tif (\n\t\t\t\tparsed &&\n\t\t\t\tparsed.version === 1 &&\n\t\t\t\tparsed.observations &&\n\t\t\t\ttypeof parsed.observations === \"object\" &&\n\t\t\t\t!Array.isArray(parsed.observations)\n\t\t\t) {\n\t\t\t\t// Sanitize per-entry so a partially-mangled file still yields a usable store rather than\n\t\t\t\t// leaking NaN/undefined counts into the gate.\n\t\t\t\tconst clean: ObservationStoreFile = { version: 1, observations: {} };\n\t\t\t\tfor (const [key, value] of Object.entries(parsed.observations)) {\n\t\t\t\t\tif (\n\t\t\t\t\t\tvalue &&\n\t\t\t\t\t\ttypeof value === \"object\" &&\n\t\t\t\t\t\ttypeof (value as ObservationEntry).count === \"number\" &&\n\t\t\t\t\t\tNumber.isFinite((value as ObservationEntry).count) &&\n\t\t\t\t\t\ttypeof (value as ObservationEntry).lastAt === \"string\"\n\t\t\t\t\t) {\n\t\t\t\t\t\tclean.observations[key] = {\n\t\t\t\t\t\t\tcount: Math.min(Math.max(0, Math.floor((value as ObservationEntry).count)), MAX_COUNT),\n\t\t\t\t\t\t\tlastAt: (value as ObservationEntry).lastAt,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn clean;\n\t\t\t}\n\t\t} catch {\n\t\t\t// Unreadable/corrupt store: start fresh in memory; the next increment rewrites the file.\n\t\t}\n\t\treturn { version: 1, observations: {} };\n\t}\n\n\tprivate save(file: ObservationStoreFile): void {\n\t\tmkdirSync(dirname(this.filePath), { recursive: true });\n\t\twriteFileSync(this.filePath, `${JSON.stringify(file, null, \"\\t\")}\\n`, \"utf-8\");\n\t}\n\n\t/** Evict least-recently-incremented keys until the store is back within {@link MAX_KEYS}. */\n\tprivate evict(file: ObservationStoreFile): void {\n\t\tconst keys = Object.keys(file.observations);\n\t\tif (keys.length <= MAX_KEYS) return;\n\t\tkeys.sort((a, b) => {\n\t\t\tconst la = file.observations[a]!.lastAt;\n\t\t\tconst lb = file.observations[b]!.lastAt;\n\t\t\treturn la < lb ? -1 : la > lb ? 1 : 0;\n\t\t});\n\t\tfor (const key of keys.slice(0, keys.length - MAX_KEYS)) {\n\t\t\tdelete file.observations[key];\n\t\t}\n\t}\n\n\t/** Record one more observation of `key` and return the new (capped) count. */\n\tincrement(key: string, at?: string): number {\n\t\tconst file = this.load();\n\t\tconst now = at ?? new Date().toISOString();\n\t\tconst count = Math.min((file.observations[key]?.count ?? 0) + 1, MAX_COUNT);\n\t\tfile.observations[key] = { count, lastAt: now };\n\t\tthis.evict(file);\n\t\tthis.save(file);\n\t\treturn count;\n\t}\n\n\t/** Current observation count for `key` (0 if never observed). */\n\tget(key: string): number {\n\t\treturn this.load().observations[key]?.count ?? 0;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
/**
|
|
5
|
+
* Durable, BOUNDED evidence-strength store for the learning gate (G6). The gate auto-applies a
|
|
6
|
+
* durable change only once it has been *observed* enough times; a single reflection pass sees a
|
|
7
|
+
* lesson once, so without persistence every proposal would look brand-new and never accumulate the
|
|
8
|
+
* repeated evidence the gate requires. This store counts how many times the SAME lesson (keyed by
|
|
9
|
+
* its durable layer + normalized summary) has been proposed across passes and sessions.
|
|
10
|
+
*
|
|
11
|
+
* File layout mirrors {@link ../models/fitness-store.ts}: a versioned JSON object under
|
|
12
|
+
* `<agentDir>/state/`, best-effort writes, and a corrupt/missing file recovers as a fresh store.
|
|
13
|
+
*/
|
|
14
|
+
/** Cap per-key counts so a hot lesson can't grow unbounded (the gate only needs a small threshold). */
|
|
15
|
+
const MAX_COUNT = 100;
|
|
16
|
+
/** Cap the number of tracked keys; least-recently-incremented keys are evicted past this bound. */
|
|
17
|
+
const MAX_KEYS = 500;
|
|
18
|
+
/**
|
|
19
|
+
* Stable evidence key for a durable-change proposal: sha256 of the layer plus its summary,
|
|
20
|
+
* lowercased and whitespace-collapsed, truncated to 24 hex chars. Normalization means the same
|
|
21
|
+
* lesson re-observed with reworded whitespace/casing accumulates onto one key instead of scattering.
|
|
22
|
+
*/
|
|
23
|
+
export function observationKey(layer, summary) {
|
|
24
|
+
const normalized = summary.toLowerCase().replace(/\s+/g, " ").trim();
|
|
25
|
+
return createHash("sha256").update(`${layer}\n${normalized}`).digest("hex").slice(0, 24);
|
|
26
|
+
}
|
|
27
|
+
export class ObservationStore {
|
|
28
|
+
filePath;
|
|
29
|
+
constructor(filePath) {
|
|
30
|
+
this.filePath = filePath;
|
|
31
|
+
}
|
|
32
|
+
static forAgentDir(agentDir) {
|
|
33
|
+
return new ObservationStore(join(agentDir, "state", "learning-observations.json"));
|
|
34
|
+
}
|
|
35
|
+
load() {
|
|
36
|
+
try {
|
|
37
|
+
if (!existsSync(this.filePath))
|
|
38
|
+
return { version: 1, observations: {} };
|
|
39
|
+
const parsed = JSON.parse(readFileSync(this.filePath, "utf-8"));
|
|
40
|
+
if (parsed &&
|
|
41
|
+
parsed.version === 1 &&
|
|
42
|
+
parsed.observations &&
|
|
43
|
+
typeof parsed.observations === "object" &&
|
|
44
|
+
!Array.isArray(parsed.observations)) {
|
|
45
|
+
// Sanitize per-entry so a partially-mangled file still yields a usable store rather than
|
|
46
|
+
// leaking NaN/undefined counts into the gate.
|
|
47
|
+
const clean = { version: 1, observations: {} };
|
|
48
|
+
for (const [key, value] of Object.entries(parsed.observations)) {
|
|
49
|
+
if (value &&
|
|
50
|
+
typeof value === "object" &&
|
|
51
|
+
typeof value.count === "number" &&
|
|
52
|
+
Number.isFinite(value.count) &&
|
|
53
|
+
typeof value.lastAt === "string") {
|
|
54
|
+
clean.observations[key] = {
|
|
55
|
+
count: Math.min(Math.max(0, Math.floor(value.count)), MAX_COUNT),
|
|
56
|
+
lastAt: value.lastAt,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return clean;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Unreadable/corrupt store: start fresh in memory; the next increment rewrites the file.
|
|
65
|
+
}
|
|
66
|
+
return { version: 1, observations: {} };
|
|
67
|
+
}
|
|
68
|
+
save(file) {
|
|
69
|
+
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
70
|
+
writeFileSync(this.filePath, `${JSON.stringify(file, null, "\t")}\n`, "utf-8");
|
|
71
|
+
}
|
|
72
|
+
/** Evict least-recently-incremented keys until the store is back within {@link MAX_KEYS}. */
|
|
73
|
+
evict(file) {
|
|
74
|
+
const keys = Object.keys(file.observations);
|
|
75
|
+
if (keys.length <= MAX_KEYS)
|
|
76
|
+
return;
|
|
77
|
+
keys.sort((a, b) => {
|
|
78
|
+
const la = file.observations[a].lastAt;
|
|
79
|
+
const lb = file.observations[b].lastAt;
|
|
80
|
+
return la < lb ? -1 : la > lb ? 1 : 0;
|
|
81
|
+
});
|
|
82
|
+
for (const key of keys.slice(0, keys.length - MAX_KEYS)) {
|
|
83
|
+
delete file.observations[key];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Record one more observation of `key` and return the new (capped) count. */
|
|
87
|
+
increment(key, at) {
|
|
88
|
+
const file = this.load();
|
|
89
|
+
const now = at ?? new Date().toISOString();
|
|
90
|
+
const count = Math.min((file.observations[key]?.count ?? 0) + 1, MAX_COUNT);
|
|
91
|
+
file.observations[key] = { count, lastAt: now };
|
|
92
|
+
this.evict(file);
|
|
93
|
+
this.save(file);
|
|
94
|
+
return count;
|
|
95
|
+
}
|
|
96
|
+
/** Current observation count for `key` (0 if never observed). */
|
|
97
|
+
get(key) {
|
|
98
|
+
return this.load().observations[key]?.count ?? 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=observation-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observation-store.js","sourceRoot":"","sources":["../../../src/core/learning/observation-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC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AC;QAC5E,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;QAChD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACjB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChB,OAAO,KAAK,CAAC;IAAA,CACb;IAED,iEAAiE;IACjE,GAAG,CAAC,GAAW,EAAU;QACxB,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC;IAAA,CACjD;CACD","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/**\n * Durable, BOUNDED evidence-strength store for the learning gate (G6). The gate auto-applies a\n * durable change only once it has been *observed* enough times; a single reflection pass sees a\n * lesson once, so without persistence every proposal would look brand-new and never accumulate the\n * repeated evidence the gate requires. This store counts how many times the SAME lesson (keyed by\n * its durable layer + normalized summary) has been proposed across passes and sessions.\n *\n * File layout mirrors {@link ../models/fitness-store.ts}: a versioned JSON object under\n * `<agentDir>/state/`, best-effort writes, and a corrupt/missing file recovers as a fresh store.\n */\n\n/** Cap per-key counts so a hot lesson can't grow unbounded (the gate only needs a small threshold). */\nconst MAX_COUNT = 100;\n/** Cap the number of tracked keys; least-recently-incremented keys are evicted past this bound. */\nconst MAX_KEYS = 500;\n\ninterface ObservationEntry {\n\tcount: number;\n\t/** ISO timestamp of the most recent increment; drives least-recently-incremented eviction. */\n\tlastAt: string;\n}\n\ninterface ObservationStoreFile {\n\tversion: 1;\n\t/** observationKey -> accumulated evidence for that lesson. */\n\tobservations: Record<string, ObservationEntry>;\n}\n\n/**\n * Stable evidence key for a durable-change proposal: sha256 of the layer plus its summary,\n * lowercased and whitespace-collapsed, truncated to 24 hex chars. Normalization means the same\n * lesson re-observed with reworded whitespace/casing accumulates onto one key instead of scattering.\n */\nexport function observationKey(layer: string, summary: string): string {\n\tconst normalized = summary.toLowerCase().replace(/\\s+/g, \" \").trim();\n\treturn createHash(\"sha256\").update(`${layer}\\n${normalized}`).digest(\"hex\").slice(0, 24);\n}\n\nexport class ObservationStore {\n\tprivate readonly filePath: string;\n\n\tconstructor(filePath: string) {\n\t\tthis.filePath = filePath;\n\t}\n\n\tstatic forAgentDir(agentDir: string): ObservationStore {\n\t\treturn new ObservationStore(join(agentDir, \"state\", \"learning-observations.json\"));\n\t}\n\n\tprivate load(): ObservationStoreFile {\n\t\ttry {\n\t\t\tif (!existsSync(this.filePath)) return { version: 1, observations: {} };\n\t\t\tconst parsed = JSON.parse(readFileSync(this.filePath, \"utf-8\")) as ObservationStoreFile;\n\t\t\tif (\n\t\t\t\tparsed &&\n\t\t\t\tparsed.version === 1 &&\n\t\t\t\tparsed.observations &&\n\t\t\t\ttypeof parsed.observations === \"object\" &&\n\t\t\t\t!Array.isArray(parsed.observations)\n\t\t\t) {\n\t\t\t\t// Sanitize per-entry so a partially-mangled file still yields a usable store rather than\n\t\t\t\t// leaking NaN/undefined counts into the gate.\n\t\t\t\tconst clean: ObservationStoreFile = { version: 1, observations: {} };\n\t\t\t\tfor (const [key, value] of Object.entries(parsed.observations)) {\n\t\t\t\t\tif (\n\t\t\t\t\t\tvalue &&\n\t\t\t\t\t\ttypeof value === \"object\" &&\n\t\t\t\t\t\ttypeof (value as ObservationEntry).count === \"number\" &&\n\t\t\t\t\t\tNumber.isFinite((value as ObservationEntry).count) &&\n\t\t\t\t\t\ttypeof (value as ObservationEntry).lastAt === \"string\"\n\t\t\t\t\t) {\n\t\t\t\t\t\tclean.observations[key] = {\n\t\t\t\t\t\t\tcount: Math.min(Math.max(0, Math.floor((value as ObservationEntry).count)), MAX_COUNT),\n\t\t\t\t\t\t\tlastAt: (value as ObservationEntry).lastAt,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn clean;\n\t\t\t}\n\t\t} catch {\n\t\t\t// Unreadable/corrupt store: start fresh in memory; the next increment rewrites the file.\n\t\t}\n\t\treturn { version: 1, observations: {} };\n\t}\n\n\tprivate save(file: ObservationStoreFile): void {\n\t\tmkdirSync(dirname(this.filePath), { recursive: true });\n\t\twriteFileSync(this.filePath, `${JSON.stringify(file, null, \"\\t\")}\\n`, \"utf-8\");\n\t}\n\n\t/** Evict least-recently-incremented keys until the store is back within {@link MAX_KEYS}. */\n\tprivate evict(file: ObservationStoreFile): void {\n\t\tconst keys = Object.keys(file.observations);\n\t\tif (keys.length <= MAX_KEYS) return;\n\t\tkeys.sort((a, b) => {\n\t\t\tconst la = file.observations[a]!.lastAt;\n\t\t\tconst lb = file.observations[b]!.lastAt;\n\t\t\treturn la < lb ? -1 : la > lb ? 1 : 0;\n\t\t});\n\t\tfor (const key of keys.slice(0, keys.length - MAX_KEYS)) {\n\t\t\tdelete file.observations[key];\n\t\t}\n\t}\n\n\t/** Record one more observation of `key` and return the new (capped) count. */\n\tincrement(key: string, at?: string): number {\n\t\tconst file = this.load();\n\t\tconst now = at ?? new Date().toISOString();\n\t\tconst count = Math.min((file.observations[key]?.count ?? 0) + 1, MAX_COUNT);\n\t\tfile.observations[key] = { count, lastAt: now };\n\t\tthis.evict(file);\n\t\tthis.save(file);\n\t\treturn count;\n\t}\n\n\t/** Current observation count for `key` (0 if never observed). */\n\tget(key: string): number {\n\t\treturn this.load().observations[key]?.count ?? 0;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ToolkitScript } from "../toolkit/script-registry.ts";
|
|
2
|
+
export interface ExecutorRouteVerdict {
|
|
3
|
+
execute: boolean;
|
|
4
|
+
scriptName?: string;
|
|
5
|
+
reason: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function classifyExecutorTurn(prompt: string, scripts: readonly ToolkitScript[]): ExecutorRouteVerdict;
|
|
8
|
+
//# sourceMappingURL=executor-route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor-route.d.ts","sourceRoot":"","sources":["../../../src/core/model-router/executor-route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAmBvF,MAAM,WAAW,oBAAoB;IACpC,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;CACf;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,aAAa,EAAE,GAAG,oBAAoB,CAc5G","sourcesContent":["import { matchToolkitScript, type ToolkitScript } from \"../toolkit/script-registry.ts\";\n\n/**\n * Executor-lane classifier (G16): decides when a USER turn is a direct toolkit command that a\n * small local executor model can own end-to-end (\"restore db staging\", \"run the status report\"),\n * instead of spending the frontier model on a one-tool reflex.\n *\n * Deliberately conservative — ALL of:\n * - the deterministic Level-0 matcher scores an EXACT/direct hit (the same margin rule that\n * gates tool-side matching; ambiguity never routes to the executor, it stays with the big\n * model + reflex brain), and\n * - the prompt LOOKS like a command: single line, short, no code fences/paths of substance.\n * Everything else falls through to normal routing. The executor model itself is gated by the\n * caller (configured + resolved + tool-call fitness), and failures escalate via the existing\n * cheap-tier escalation path.\n */\n\nconst EXECUTOR_MAX_PROMPT_CHARS = 120;\n\nexport interface ExecutorRouteVerdict {\n\texecute: boolean;\n\tscriptName?: string;\n\treason: string;\n}\n\nexport function classifyExecutorTurn(prompt: string, scripts: readonly ToolkitScript[]): ExecutorRouteVerdict {\n\tconst trimmed = prompt.trim();\n\tif (scripts.length === 0) return { execute: false, reason: \"no_toolkit_scripts\" };\n\tif (trimmed.length === 0 || trimmed.length > EXECUTOR_MAX_PROMPT_CHARS) {\n\t\treturn { execute: false, reason: \"not_command_shaped\" };\n\t}\n\tif (trimmed.includes(\"\\n\") || trimmed.includes(\"```\")) {\n\t\treturn { execute: false, reason: \"not_command_shaped\" };\n\t}\n\tconst match = matchToolkitScript(trimmed, scripts);\n\tif (match.kind !== \"exact\") {\n\t\treturn { execute: false, reason: match.kind === \"ambiguous\" ? \"ambiguous_match\" : \"no_match\" };\n\t}\n\treturn { execute: true, scriptName: match.script.name, reason: \"level0_direct_hit\" };\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { matchToolkitScript } from "../toolkit/script-registry.js";
|
|
2
|
+
/**
|
|
3
|
+
* Executor-lane classifier (G16): decides when a USER turn is a direct toolkit command that a
|
|
4
|
+
* small local executor model can own end-to-end ("restore db staging", "run the status report"),
|
|
5
|
+
* instead of spending the frontier model on a one-tool reflex.
|
|
6
|
+
*
|
|
7
|
+
* Deliberately conservative — ALL of:
|
|
8
|
+
* - the deterministic Level-0 matcher scores an EXACT/direct hit (the same margin rule that
|
|
9
|
+
* gates tool-side matching; ambiguity never routes to the executor, it stays with the big
|
|
10
|
+
* model + reflex brain), and
|
|
11
|
+
* - the prompt LOOKS like a command: single line, short, no code fences/paths of substance.
|
|
12
|
+
* Everything else falls through to normal routing. The executor model itself is gated by the
|
|
13
|
+
* caller (configured + resolved + tool-call fitness), and failures escalate via the existing
|
|
14
|
+
* cheap-tier escalation path.
|
|
15
|
+
*/
|
|
16
|
+
const EXECUTOR_MAX_PROMPT_CHARS = 120;
|
|
17
|
+
export function classifyExecutorTurn(prompt, scripts) {
|
|
18
|
+
const trimmed = prompt.trim();
|
|
19
|
+
if (scripts.length === 0)
|
|
20
|
+
return { execute: false, reason: "no_toolkit_scripts" };
|
|
21
|
+
if (trimmed.length === 0 || trimmed.length > EXECUTOR_MAX_PROMPT_CHARS) {
|
|
22
|
+
return { execute: false, reason: "not_command_shaped" };
|
|
23
|
+
}
|
|
24
|
+
if (trimmed.includes("\n") || trimmed.includes("```")) {
|
|
25
|
+
return { execute: false, reason: "not_command_shaped" };
|
|
26
|
+
}
|
|
27
|
+
const match = matchToolkitScript(trimmed, scripts);
|
|
28
|
+
if (match.kind !== "exact") {
|
|
29
|
+
return { execute: false, reason: match.kind === "ambiguous" ? "ambiguous_match" : "no_match" };
|
|
30
|
+
}
|
|
31
|
+
return { execute: true, scriptName: match.script.name, reason: "level0_direct_hit" };
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=executor-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor-route.js","sourceRoot":"","sources":["../../../src/core/model-router/executor-route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAsB,MAAM,+BAA+B,CAAC;AAEvF;;;;;;;;;;;;;GAaG;AAEH,MAAM,yBAAyB,GAAG,GAAG,CAAC;AAQtC,MAAM,UAAU,oBAAoB,CAAC,MAAc,EAAE,OAAiC,EAAwB;IAC7G,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;IAC9B,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,CAAC;IAClF,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,yBAAyB,EAAE,CAAC;QACxE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,CAAC;IACzD,CAAC;IACD,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACvD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,CAAC;IACzD,CAAC;IACD,MAAM,KAAK,GAAG,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACnD,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC5B,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IAChG,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,mBAAmB,EAAE,CAAC;AAAA,CACrF","sourcesContent":["import { matchToolkitScript, type ToolkitScript } from \"../toolkit/script-registry.ts\";\n\n/**\n * Executor-lane classifier (G16): decides when a USER turn is a direct toolkit command that a\n * small local executor model can own end-to-end (\"restore db staging\", \"run the status report\"),\n * instead of spending the frontier model on a one-tool reflex.\n *\n * Deliberately conservative — ALL of:\n * - the deterministic Level-0 matcher scores an EXACT/direct hit (the same margin rule that\n * gates tool-side matching; ambiguity never routes to the executor, it stays with the big\n * model + reflex brain), and\n * - the prompt LOOKS like a command: single line, short, no code fences/paths of substance.\n * Everything else falls through to normal routing. The executor model itself is gated by the\n * caller (configured + resolved + tool-call fitness), and failures escalate via the existing\n * cheap-tier escalation path.\n */\n\nconst EXECUTOR_MAX_PROMPT_CHARS = 120;\n\nexport interface ExecutorRouteVerdict {\n\texecute: boolean;\n\tscriptName?: string;\n\treason: string;\n}\n\nexport function classifyExecutorTurn(prompt: string, scripts: readonly ToolkitScript[]): ExecutorRouteVerdict {\n\tconst trimmed = prompt.trim();\n\tif (scripts.length === 0) return { execute: false, reason: \"no_toolkit_scripts\" };\n\tif (trimmed.length === 0 || trimmed.length > EXECUTOR_MAX_PROMPT_CHARS) {\n\t\treturn { execute: false, reason: \"not_command_shaped\" };\n\t}\n\tif (trimmed.includes(\"\\n\") || trimmed.includes(\"```\")) {\n\t\treturn { execute: false, reason: \"not_command_shaped\" };\n\t}\n\tconst match = matchToolkitScript(trimmed, scripts);\n\tif (match.kind !== \"exact\") {\n\t\treturn { execute: false, reason: match.kind === \"ambiguous\" ? \"ambiguous_match\" : \"no_match\" };\n\t}\n\treturn { execute: true, scriptName: match.script.name, reason: \"level0_direct_hit\" };\n}\n"]}
|
|
@@ -3,5 +3,7 @@ export declare function shouldEscalateModelRouterTool(options: {
|
|
|
3
3
|
tier: ModelTier;
|
|
4
4
|
toolName: string;
|
|
5
5
|
args?: unknown;
|
|
6
|
+
/** The route's reasonCode; executor-lane turns carry "executor_direct". */
|
|
7
|
+
reasonCode?: string;
|
|
6
8
|
}): boolean;
|
|
7
9
|
//# sourceMappingURL=tool-escalation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-escalation.d.ts","sourceRoot":"","sources":["../../../src/core/model-router/tool-escalation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AA2F1D,wBAAgB,6BAA6B,CAAC,OAAO,EAAE;
|
|
1
|
+
{"version":3,"file":"tool-escalation.d.ts","sourceRoot":"","sources":["../../../src/core/model-router/tool-escalation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AA2F1D,wBAAgB,6BAA6B,CAAC,OAAO,EAAE;IACtD,IAAI,EAAE,SAAS,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,2EAA2E;IAC3E,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB,GAAG,OAAO,CAeV","sourcesContent":["import type { ModelTier } from \"../autonomy/contracts.ts\";\n\nconst READ_ONLY_TOOL_NAMES = new Set([\n\t\"read\",\n\t\"grep\",\n\t\"find\",\n\t\"ls\",\n\t\"list\",\n\t\"search\",\n\t\"glob\",\n\t\"view_file\",\n\t\"list_dir\",\n\t\"grep_search\",\n\t\"search_web\",\n\t\"read_url_content\",\n\t\"read_browser_page\",\n]);\n\nconst SHELL_TOOL_NAMES = new Set([\"bash\", \"exec\", \"execute\", \"run\", \"run_command\", \"shell\"]);\n\nconst READ_ONLY_COMMANDS = new Set([\n\t\"awk\",\n\t\"cat\",\n\t\"date\",\n\t\"df\",\n\t\"du\",\n\t\"env\",\n\t\"git\",\n\t\"grep\",\n\t\"head\",\n\t\"jq\",\n\t\"ls\",\n\t\"node\",\n\t\"npm\",\n\t\"pnpm\",\n\t\"pwd\",\n\t\"rg\",\n\t\"sed\",\n\t\"tail\",\n\t\"test\",\n\t\"tsc\",\n\t\"wc\",\n\t\"which\",\n\t\"yarn\",\n]);\n\nconst READ_ONLY_GIT_SUBCOMMANDS = new Set([\"branch\", \"diff\", \"log\", \"rev-parse\", \"show\", \"status\", \"tag\"]);\nconst READ_ONLY_NPM_SUBCOMMANDS = new Set([\"info\", \"list\", \"ls\", \"outdated\", \"view\", \"whoami\"]);\nconst MUTATING_SHELL_TOKEN_RE =\n\t/(^|\\s)(>|>>|2>|&>|tee\\b|rm\\b|mv\\b|cp\\b|mkdir\\b|touch\\b|chmod\\b|chown\\b|install\\b|commit\\b|push\\b|publish\\b|deploy\\b|apply\\b|add\\b|checkout\\b|switch\\b|reset\\b|clean\\b|stash\\b|merge\\b|rebase\\b|npm\\s+(?:i|install|ci|update|publish|run)\\b|pnpm\\s+(?:i|install|update|publish|run)\\b|yarn\\s+(?:add|install|upgrade|publish|run)\\b)/i;\nconst MUTATING_TOOL_NAME_RE =\n\t/(bash|exec|execute|run|shell|write|edit|patch|replace|delete|remove|move|rename|create|mkdir|touch|install|commit|push|publish|deploy|apply)/i;\n\nfunction getShellCommand(args: unknown): string | undefined {\n\tif (!args || typeof args !== \"object\") return undefined;\n\tconst record = args as Record<string, unknown>;\n\tconst command = record.command ?? record.cmd ?? record.shellCommand;\n\treturn typeof command === \"string\" ? command.trim() : undefined;\n}\n\nfunction commandName(segment: string): string | undefined {\n\tconst first = segment.trim().match(/^[A-Za-z0-9_./-]+/)?.[0];\n\tif (!first) return undefined;\n\tconst parts = first.split(\"/\");\n\treturn parts[parts.length - 1]?.toLowerCase();\n}\n\nfunction commandArg(segment: string, index: number): string | undefined {\n\treturn segment.trim().split(/\\s+/)[index]?.toLowerCase();\n}\n\nfunction isReadOnlyShellSegment(segment: string): boolean {\n\tconst name = commandName(segment);\n\tif (!name || !READ_ONLY_COMMANDS.has(name)) return false;\n\tif (name === \"git\") {\n\t\tconst subcommand = commandArg(segment, 1);\n\t\treturn Boolean(subcommand && READ_ONLY_GIT_SUBCOMMANDS.has(subcommand));\n\t}\n\tif (name === \"npm\" || name === \"pnpm\" || name === \"yarn\") {\n\t\tconst subcommand = commandArg(segment, 1);\n\t\treturn Boolean(subcommand && READ_ONLY_NPM_SUBCOMMANDS.has(subcommand));\n\t}\n\treturn true;\n}\n\nfunction isReadOnlyShellCommand(command: string): boolean {\n\tif (!command || MUTATING_SHELL_TOKEN_RE.test(command)) return false;\n\tconst segments = command.split(/\\s*&&\\s*/).map((segment) => segment.trim());\n\treturn segments.length > 0 && segments.every(isReadOnlyShellSegment);\n}\n\nexport function shouldEscalateModelRouterTool(options: {\n\ttier: ModelTier;\n\ttoolName: string;\n\targs?: unknown;\n\t/** The route's reasonCode; executor-lane turns carry \"executor_direct\". */\n\treasonCode?: string;\n}): boolean {\n\tif (options.tier !== \"cheap\") return false;\n\tconst toolName = options.toolName.trim().toLowerCase();\n\t// Executor-lane turns (G16) exist to run exactly one tool: run_toolkit_script, which enforces\n\t// its own safety (danger confirmation, structural exit-code contract). Escalating on it would\n\t// abort every executor turn at the moment it does its job. Any OTHER mutating tool still\n\t// escalates to the expensive model as usual.\n\tif (options.reasonCode === \"executor_direct\" && toolName === \"run_toolkit_script\") return false;\n\tif (!toolName) return true;\n\tif (READ_ONLY_TOOL_NAMES.has(toolName)) return false;\n\tif (SHELL_TOOL_NAMES.has(toolName)) {\n\t\tconst command = getShellCommand(options.args);\n\t\treturn command ? !isReadOnlyShellCommand(command) : true;\n\t}\n\treturn MUTATING_TOOL_NAME_RE.test(toolName) || !toolName.startsWith(\"read_\");\n}\n"]}
|
|
@@ -84,6 +84,12 @@ export function shouldEscalateModelRouterTool(options) {
|
|
|
84
84
|
if (options.tier !== "cheap")
|
|
85
85
|
return false;
|
|
86
86
|
const toolName = options.toolName.trim().toLowerCase();
|
|
87
|
+
// Executor-lane turns (G16) exist to run exactly one tool: run_toolkit_script, which enforces
|
|
88
|
+
// its own safety (danger confirmation, structural exit-code contract). Escalating on it would
|
|
89
|
+
// abort every executor turn at the moment it does its job. Any OTHER mutating tool still
|
|
90
|
+
// escalates to the expensive model as usual.
|
|
91
|
+
if (options.reasonCode === "executor_direct" && toolName === "run_toolkit_script")
|
|
92
|
+
return false;
|
|
87
93
|
if (!toolName)
|
|
88
94
|
return true;
|
|
89
95
|
if (READ_ONLY_TOOL_NAMES.has(toolName))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-escalation.js","sourceRoot":"","sources":["../../../src/core/model-router/tool-escalation.ts"],"names":[],"mappings":"AAEA,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAC;IACpC,MAAM;IACN,MAAM;IACN,MAAM;IACN,IAAI;IACJ,MAAM;IACN,QAAQ;IACR,MAAM;IACN,WAAW;IACX,UAAU;IACV,aAAa;IACb,YAAY;IACZ,kBAAkB;IAClB,mBAAmB;CACnB,CAAC,CAAC;AAEH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC,CAAC;AAE7F,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC;IAClC,KAAK;IACL,KAAK;IACL,MAAM;IACN,IAAI;IACJ,IAAI;IACJ,KAAK;IACL,KAAK;IACL,MAAM;IACN,MAAM;IACN,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,KAAK;IACL,MAAM;IACN,KAAK;IACL,IAAI;IACJ,KAAK;IACL,MAAM;IACN,MAAM;IACN,KAAK;IACL,IAAI;IACJ,OAAO;IACP,MAAM;CACN,CAAC,CAAC;AAEH,MAAM,yBAAyB,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE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I,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AAAA,CAC7E","sourcesContent":["import type { ModelTier } from \"../autonomy/contracts.ts\";\n\nconst READ_ONLY_TOOL_NAMES = new Set([\n\t\"read\",\n\t\"grep\",\n\t\"find\",\n\t\"ls\",\n\t\"list\",\n\t\"search\",\n\t\"glob\",\n\t\"view_file\",\n\t\"list_dir\",\n\t\"grep_search\",\n\t\"search_web\",\n\t\"read_url_content\",\n\t\"read_browser_page\",\n]);\n\nconst SHELL_TOOL_NAMES = new Set([\"bash\", \"exec\", \"execute\", \"run\", \"run_command\", \"shell\"]);\n\nconst READ_ONLY_COMMANDS = new Set([\n\t\"awk\",\n\t\"cat\",\n\t\"date\",\n\t\"df\",\n\t\"du\",\n\t\"env\",\n\t\"git\",\n\t\"grep\",\n\t\"head\",\n\t\"jq\",\n\t\"ls\",\n\t\"node\",\n\t\"npm\",\n\t\"pnpm\",\n\t\"pwd\",\n\t\"rg\",\n\t\"sed\",\n\t\"tail\",\n\t\"test\",\n\t\"tsc\",\n\t\"wc\",\n\t\"which\",\n\t\"yarn\",\n]);\n\nconst READ_ONLY_GIT_SUBCOMMANDS = new Set([\"branch\", \"diff\", \"log\", \"rev-parse\", \"show\", \"status\", \"tag\"]);\nconst READ_ONLY_NPM_SUBCOMMANDS = new Set([\"info\", \"list\", \"ls\", \"outdated\", \"view\", \"whoami\"]);\nconst MUTATING_SHELL_TOKEN_RE =\n\t/(^|\\s)(>|>>|2>|&>|tee\\b|rm\\b|mv\\b|cp\\b|mkdir\\b|touch\\b|chmod\\b|chown\\b|install\\b|commit\\b|push\\b|publish\\b|deploy\\b|apply\\b|add\\b|checkout\\b|switch\\b|reset\\b|clean\\b|stash\\b|merge\\b|rebase\\b|npm\\s+(?:i|install|ci|update|publish|run)\\b|pnpm\\s+(?:i|install|update|publish|run)\\b|yarn\\s+(?:add|install|upgrade|publish|run)\\b)/i;\nconst MUTATING_TOOL_NAME_RE =\n\t/(bash|exec|execute|run|shell|write|edit|patch|replace|delete|remove|move|rename|create|mkdir|touch|install|commit|push|publish|deploy|apply)/i;\n\nfunction getShellCommand(args: unknown): string | undefined {\n\tif (!args || typeof args !== \"object\") return undefined;\n\tconst record = args as Record<string, unknown>;\n\tconst command = record.command ?? record.cmd ?? record.shellCommand;\n\treturn typeof command === \"string\" ? command.trim() : undefined;\n}\n\nfunction commandName(segment: string): string | undefined {\n\tconst first = segment.trim().match(/^[A-Za-z0-9_./-]+/)?.[0];\n\tif (!first) return undefined;\n\tconst parts = first.split(\"/\");\n\treturn parts[parts.length - 1]?.toLowerCase();\n}\n\nfunction commandArg(segment: string, index: number): string | undefined {\n\treturn segment.trim().split(/\\s+/)[index]?.toLowerCase();\n}\n\nfunction isReadOnlyShellSegment(segment: string): boolean {\n\tconst name = commandName(segment);\n\tif (!name || !READ_ONLY_COMMANDS.has(name)) return false;\n\tif (name === \"git\") {\n\t\tconst subcommand = commandArg(segment, 1);\n\t\treturn Boolean(subcommand && READ_ONLY_GIT_SUBCOMMANDS.has(subcommand));\n\t}\n\tif (name === \"npm\" || name === \"pnpm\" || name === \"yarn\") {\n\t\tconst subcommand = commandArg(segment, 1);\n\t\treturn Boolean(subcommand && READ_ONLY_NPM_SUBCOMMANDS.has(subcommand));\n\t}\n\treturn true;\n}\n\nfunction isReadOnlyShellCommand(command: string): boolean {\n\tif (!command || MUTATING_SHELL_TOKEN_RE.test(command)) return false;\n\tconst segments = command.split(/\\s*&&\\s*/).map((segment) => segment.trim());\n\treturn segments.length > 0 && segments.every(isReadOnlyShellSegment);\n}\n\nexport function shouldEscalateModelRouterTool(options: {\n\ttier: ModelTier;\n\ttoolName: string;\n\targs?: unknown;\n\t/** The route's reasonCode; executor-lane turns carry \"executor_direct\". */\n\treasonCode?: string;\n}): boolean {\n\tif (options.tier !== \"cheap\") return false;\n\tconst toolName = options.toolName.trim().toLowerCase();\n\t// Executor-lane turns (G16) exist to run exactly one tool: run_toolkit_script, which enforces\n\t// its own safety (danger confirmation, structural exit-code contract). Escalating on it would\n\t// abort every executor turn at the moment it does its job. Any OTHER mutating tool still\n\t// escalates to the expensive model as usual.\n\tif (options.reasonCode === \"executor_direct\" && toolName === \"run_toolkit_script\") return false;\n\tif (!toolName) return true;\n\tif (READ_ONLY_TOOL_NAMES.has(toolName)) return false;\n\tif (SHELL_TOOL_NAMES.has(toolName)) {\n\t\tconst command = getShellCommand(options.args);\n\t\treturn command ? !isReadOnlyShellCommand(command) : true;\n\t}\n\treturn MUTATING_TOOL_NAME_RE.test(toolName) || !toolName.startsWith(\"read_\");\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CapabilityEnvelope, EvidenceBundle, GateOutcome } from "../autonomy/contracts.ts";
|
|
1
|
+
import type { CapabilityEnvelope, EvidenceBundle, EvidenceRef, GateOutcome } from "../autonomy/contracts.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Pure orchestration for one autonomous research pass: gate -> bounded isolated completion ->
|
|
4
4
|
* parse -> evidence bundle. The model executor is injected so this stays provider-free and
|
|
@@ -20,6 +20,12 @@ export interface ResearchRunnerOptions {
|
|
|
20
20
|
context?: string;
|
|
21
21
|
/** Stripped research envelope - never the foreground/architect envelope. */
|
|
22
22
|
envelope: CapabilityEnvelope;
|
|
23
|
+
/**
|
|
24
|
+
* Pointer-first workspace sources (repo-relative path + bounded excerpt, never file bodies) that
|
|
25
|
+
* inform the pass. They are rendered into the user prompt and carried into the evidence bundle;
|
|
26
|
+
* omitted / empty reproduces the pre-collector behavior exactly.
|
|
27
|
+
*/
|
|
28
|
+
sources?: readonly EvidenceRef[];
|
|
23
29
|
/** Budget for this pass; a post-hoc breach marks the run budget_exhausted (spend stays visible). */
|
|
24
30
|
maxUsd: number;
|
|
25
31
|
maxSources: number;
|
|
@@ -46,6 +52,7 @@ export interface ResearchRunResult {
|
|
|
46
52
|
export declare function buildResearchUserPrompt(args: {
|
|
47
53
|
query: string;
|
|
48
54
|
context?: string;
|
|
55
|
+
sources?: readonly EvidenceRef[];
|
|
49
56
|
maxFindings: number;
|
|
50
57
|
}): string;
|
|
51
58
|
export interface ParsedResearchFindings {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"research-runner.d.ts","sourceRoot":"","sources":["../../../src/core/research/research-runner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAwB,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAItH;;;;;;;GAOG;AAEH,2GAA2G;AAC3G,eAAO,MAAM,2BAA2B,QAM5B,CAAC;AAEb,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,iGAAiG;IACjG,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,oGAAoG;IACpG,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,cAAc,EAAE,MAAM,CAAC;IACvB,wFAAwF;IACxF,QAAQ,EAAE,CAAC,IAAI,EAAE;QAAE,YAAY,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,KAAK,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACpH,qDAAqD;IACrD,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,QAAQ,GAAG,UAAU,GAAG,SAAS,GAAG,kBAAkB,CAAC;AAErG,MAAM,WAAW,iBAAiB;IACjC,MAAM,EAAE,iBAAiB,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,uBAAuB,CAAC,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAO9G;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,EAAE,KAAK,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC1D;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,sBAAsB,GAAG,SAAS,CAwC3G;AA+BD,wBAAsB,WAAW,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAwD5F","sourcesContent":["import { runBoundedCompletion } from \"../autonomy/bounded-completion.ts\";\nimport type { CapabilityEnvelope, EvidenceBundle, EvidenceRef, Finding, GateOutcome } from \"../autonomy/contracts.ts\";\nimport { createEvidenceBundle } from \"./evidence-bundle.ts\";\nimport { evaluateResearchRequest } from \"./research-gate.ts\";\n\n/**\n * Pure orchestration for one autonomous research pass: gate -> bounded isolated completion ->\n * parse -> evidence bundle. The model executor is injected so this stays provider-free and\n * session-free; production wires `AgentSession.runIsolatedCompletion` in.\n *\n * The lane is read-only by construction: the executor receives text prompts only, and the output\n * is an `EvidenceBundle` whose model-synthesized findings are marked untrusted.\n */\n\n/** Static across calls so callers can use `cacheRetention: \"short\"` and only pay for the variable tail. */\nexport const RESEARCH_LANE_SYSTEM_PROMPT = [\n\t\"You are a read-only research lane for a coding agent.\",\n\t\"You receive a research query plus bounded context and produce findings that help satisfy open goal requirements.\",\n\t\"Respond with STRICT JSON only - no prose, no markdown fences:\",\n\t'{\"findings\":[{\"summary\":\"<one concrete, actionable finding>\",\"confidence\":<0..1>}]}',\n\t\"Base findings only on the provided context. Never invent file paths, APIs, or facts.\",\n].join(\"\\n\");\n\nexport interface ResearchCompletion {\n\ttext: string;\n\tcostUsd: number;\n\tstopReason: string;\n}\n\nexport interface ResearchRunnerOptions {\n\tquery: string;\n\t/** Bounded, pre-redacted context handed to the research model (goal text, open requirements). */\n\tcontext?: string;\n\t/** Stripped research envelope - never the foreground/architect envelope. */\n\tenvelope: CapabilityEnvelope;\n\t/** Budget for this pass; a post-hoc breach marks the run budget_exhausted (spend stays visible). */\n\tmaxUsd: number;\n\tmaxSources: number;\n\tmaxFindings: number;\n\t/** Wall-clock budget in milliseconds; 0 disables. */\n\tmaxWallClockMs: number;\n\t/** Executes one isolated completion. Production: AgentSession.runIsolatedCompletion. */\n\tcomplete: (args: { systemPrompt: string; userPrompt: string; signal?: AbortSignal }) => Promise<ResearchCompletion>;\n\t/** External cancellation (e.g. session disposal). */\n\tsignal?: AbortSignal;\n}\n\nexport type ResearchRunStatus = \"succeeded\" | \"failed\" | \"canceled\" | \"timeout\" | \"budget_exhausted\";\n\nexport interface ResearchRunResult {\n\tstatus: ResearchRunStatus;\n\treasonCode: string;\n\tgateOutcome: GateOutcome;\n\tbundle?: EvidenceBundle;\n\tcostUsd: number;\n}\n\nexport function buildResearchUserPrompt(args: { query: string; context?: string; maxFindings: number }): string {\n\tconst parts = [`Research query: ${args.query}`];\n\tif (args.context && args.context.length > 0) {\n\t\tparts.push(\"\", \"Context:\", args.context);\n\t}\n\tparts.push(\"\", `Return at most ${args.maxFindings} findings.`);\n\treturn parts.join(\"\\n\");\n}\n\nexport interface ParsedResearchFindings {\n\tfindings: Array<{ summary: string; confidence?: number }>;\n}\n\nexport function parseResearchFindings(text: string, maxFindings: number): ParsedResearchFindings | undefined {\n\tconst trimmed = text.trim();\n\tconst candidates: string[] = [trimmed];\n\tconst fenced = /```(?:json)?\\s*([\\s\\S]*?)```/.exec(trimmed);\n\tif (fenced?.[1]) candidates.push(fenced[1].trim());\n\tconst start = trimmed.indexOf(\"{\");\n\tconst end = trimmed.lastIndexOf(\"}\");\n\tif (start >= 0 && end > start) candidates.push(trimmed.slice(start, end + 1));\n\n\tfor (const candidate of candidates) {\n\t\tlet parsed: unknown;\n\t\ttry {\n\t\t\tparsed = JSON.parse(candidate);\n\t\t} catch {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) continue;\n\t\tconst findingsRaw = (parsed as { findings?: unknown }).findings;\n\t\tif (!Array.isArray(findingsRaw)) continue;\n\n\t\tconst findings: Array<{ summary: string; confidence?: number }> = [];\n\t\tfor (const item of findingsRaw) {\n\t\t\tif (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n\t\t\tconst summary = (item as { summary?: unknown }).summary;\n\t\t\tif (typeof summary !== \"string\" || summary.trim().length === 0) continue;\n\t\t\tconst confidenceRaw = (item as { confidence?: unknown }).confidence;\n\t\t\tconst confidence =\n\t\t\t\ttypeof confidenceRaw === \"number\" && Number.isFinite(confidenceRaw)\n\t\t\t\t\t? Math.min(Math.max(confidenceRaw, 0), 1)\n\t\t\t\t\t: undefined;\n\t\t\tfindings.push({ summary: summary.trim(), confidence });\n\t\t\tif (findings.length >= maxFindings) break;\n\t\t}\n\t\t// A well-formed-but-empty findings array is a valid \"nothing found\"; a findings array whose\n\t\t// every item is malformed is not.\n\t\tif (findings.length > 0 || findingsRaw.length === 0) {\n\t\t\treturn { findings };\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction truncateExcerpt(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, Math.max(0, maxChars - 1))}…`;\n}\n\nfunction buildBundle(options: ResearchRunnerOptions, parsed: ParsedResearchFindings): EvidenceBundle {\n\tconst contextRef: EvidenceRef = {\n\t\tid: \"src-context\",\n\t\tkind: \"user\",\n\t\ttitle: \"Goal/context provided to the research lane\",\n\t\ttrusted: true,\n\t\texcerpt: truncateExcerpt(options.context && options.context.length > 0 ? options.context : options.query, 2000),\n\t};\n\tconst synthesisRef: EvidenceRef = {\n\t\tid: \"src-synthesis\",\n\t\tkind: \"tool\",\n\t\ttitle: \"Research-model synthesis\",\n\t\ttrusted: false,\n\t};\n\tconst sources = [contextRef, synthesisRef].slice(0, Math.max(1, options.maxSources));\n\tconst findings: Finding[] = parsed.findings.slice(0, options.maxFindings).map((finding, index) => ({\n\t\tid: `finding-${index + 1}`,\n\t\tsummary: finding.summary,\n\t\tevidenceIds: [synthesisRef.id],\n\t\t...(finding.confidence !== undefined ? { confidence: finding.confidence } : {}),\n\t}));\n\treturn createEvidenceBundle({ query: options.query, sources, findings });\n}\n\nexport async function runResearch(options: ResearchRunnerOptions): Promise<ResearchRunResult> {\n\tconst gateOutcome = evaluateResearchRequest({\n\t\tenvelope: options.envelope,\n\t\tsourceKind: \"tool\",\n\t\testimatedUsd: options.maxUsd,\n\t});\n\tif (gateOutcome.outcome !== \"allow\") {\n\t\t// Skip-and-record, never prompt: gate denials inform diagnostics instead of blocking anything.\n\t\tconst status: ResearchRunStatus = gateOutcome.reasonCode === \"over_budget\" ? \"budget_exhausted\" : \"failed\";\n\t\treturn { status, reasonCode: gateOutcome.reasonCode, gateOutcome, costUsd: 0 };\n\t}\n\n\tconst bounded = await runBoundedCompletion({\n\t\tmaxWallClockMs: options.maxWallClockMs,\n\t\tsignal: options.signal,\n\t\texecute: (signal) =>\n\t\t\toptions.complete({\n\t\t\t\tsystemPrompt: RESEARCH_LANE_SYSTEM_PROMPT,\n\t\t\t\tuserPrompt: buildResearchUserPrompt(options),\n\t\t\t\tsignal,\n\t\t\t}),\n\t});\n\tif (bounded.failure) {\n\t\treturn {\n\t\t\tstatus: bounded.failure.status,\n\t\t\treasonCode: bounded.failure.reasonCode,\n\t\t\tgateOutcome,\n\t\t\tcostUsd: bounded.completion?.costUsd ?? 0,\n\t\t};\n\t}\n\tconst completion = bounded.completion;\n\tif (!completion) {\n\t\treturn { status: \"failed\", reasonCode: \"completion_error\", gateOutcome, costUsd: 0 };\n\t}\n\tif (completion.stopReason === \"error\" || completion.stopReason === \"aborted\") {\n\t\treturn { status: \"failed\", reasonCode: \"model_error\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst parsed = parseResearchFindings(completion.text, options.maxFindings);\n\tif (!parsed) {\n\t\treturn { status: \"failed\", reasonCode: \"unparseable_output\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst bundle = buildBundle(options, parsed);\n\tconst overBudget = options.maxUsd > 0 && completion.costUsd > options.maxUsd;\n\treturn {\n\t\tstatus: overBudget ? \"budget_exhausted\" : \"succeeded\",\n\t\treasonCode: overBudget\n\t\t\t? \"cost_budget_exceeded\"\n\t\t\t: parsed.findings.length === 0\n\t\t\t\t? \"no_findings\"\n\t\t\t\t: \"research_completed\",\n\t\tgateOutcome,\n\t\tbundle,\n\t\tcostUsd: completion.costUsd,\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"research-runner.d.ts","sourceRoot":"","sources":["../../../src/core/research/research-runner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,WAAW,EAAW,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAItH;;;;;;;GAOG;AAEH,2GAA2G;AAC3G,eAAO,MAAM,2BAA2B,QAM5B,CAAC;AAEb,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,iGAAiG;IACjG,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,QAAQ,EAAE,kBAAkB,CAAC;IAC7B;;;;OAIG;IACH,OAAO,CAAC,EAAE,SAAS,WAAW,EAAE,CAAC;IACjC,oGAAoG;IACpG,MAAM,EAAE,MAAM,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ounded-completion.ts\";\nimport type { CapabilityEnvelope, EvidenceBundle, EvidenceRef, Finding, GateOutcome } from \"../autonomy/contracts.ts\";\nimport { createEvidenceBundle } from \"./evidence-bundle.ts\";\nimport { evaluateResearchRequest } from \"./research-gate.ts\";\n\n/**\n * Pure orchestration for one autonomous research pass: gate -> bounded isolated completion ->\n * parse -> evidence bundle. The model executor is injected so this stays provider-free and\n * session-free; production wires `AgentSession.runIsolatedCompletion` in.\n *\n * The lane is read-only by construction: the executor receives text prompts only, and the output\n * is an `EvidenceBundle` whose model-synthesized findings are marked untrusted.\n */\n\n/** Static across calls so callers can use `cacheRetention: \"short\"` and only pay for the variable tail. */\nexport const RESEARCH_LANE_SYSTEM_PROMPT = [\n\t\"You are a read-only research lane for a coding agent.\",\n\t\"You receive a research query plus bounded context and produce findings that help satisfy open goal requirements.\",\n\t\"Respond with STRICT JSON only - no prose, no markdown fences:\",\n\t'{\"findings\":[{\"summary\":\"<one concrete, actionable finding>\",\"confidence\":<0..1>}]}',\n\t\"Base findings only on the provided context. Never invent file paths, APIs, or facts.\",\n].join(\"\\n\");\n\nexport interface ResearchCompletion {\n\ttext: string;\n\tcostUsd: number;\n\tstopReason: string;\n}\n\nexport interface ResearchRunnerOptions {\n\tquery: string;\n\t/** Bounded, pre-redacted context handed to the research model (goal text, open requirements). */\n\tcontext?: string;\n\t/** Stripped research envelope - never the foreground/architect envelope. */\n\tenvelope: CapabilityEnvelope;\n\t/**\n\t * Pointer-first workspace sources (repo-relative path + bounded excerpt, never file bodies) that\n\t * inform the pass. They are rendered into the user prompt and carried into the evidence bundle;\n\t * omitted / empty reproduces the pre-collector behavior exactly.\n\t */\n\tsources?: readonly EvidenceRef[];\n\t/** Budget for this pass; a post-hoc breach marks the run budget_exhausted (spend stays visible). */\n\tmaxUsd: number;\n\tmaxSources: number;\n\tmaxFindings: number;\n\t/** Wall-clock budget in milliseconds; 0 disables. */\n\tmaxWallClockMs: number;\n\t/** Executes one isolated completion. Production: AgentSession.runIsolatedCompletion. */\n\tcomplete: (args: { systemPrompt: string; userPrompt: string; signal?: AbortSignal }) => Promise<ResearchCompletion>;\n\t/** External cancellation (e.g. session disposal). */\n\tsignal?: AbortSignal;\n}\n\nexport type ResearchRunStatus = \"succeeded\" | \"failed\" | \"canceled\" | \"timeout\" | \"budget_exhausted\";\n\nexport interface ResearchRunResult {\n\tstatus: ResearchRunStatus;\n\treasonCode: string;\n\tgateOutcome: GateOutcome;\n\tbundle?: EvidenceBundle;\n\tcostUsd: number;\n}\n\nexport function buildResearchUserPrompt(args: {\n\tquery: string;\n\tcontext?: string;\n\tsources?: readonly EvidenceRef[];\n\tmaxFindings: number;\n}): string {\n\tconst parts = [`Research query: ${args.query}`];\n\tif (args.context && args.context.length > 0) {\n\t\tparts.push(\"\", \"Context:\", args.context);\n\t}\n\tif (args.sources && args.sources.length > 0) {\n\t\tparts.push(\"\", \"Workspace sources (pointer-first; open the file to read full context):\");\n\t\tfor (const source of args.sources) {\n\t\t\tconst pointer = source.title ?? source.uri ?? source.id;\n\t\t\tparts.push(source.excerpt ? `- ${pointer}: ${source.excerpt}` : `- ${pointer}`);\n\t\t}\n\t}\n\tparts.push(\"\", `Return at most ${args.maxFindings} findings.`);\n\treturn parts.join(\"\\n\");\n}\n\nexport interface ParsedResearchFindings {\n\tfindings: Array<{ summary: string; confidence?: number }>;\n}\n\nexport function parseResearchFindings(text: string, maxFindings: number): ParsedResearchFindings | undefined {\n\tconst trimmed = text.trim();\n\tconst candidates: string[] = [trimmed];\n\tconst fenced = /```(?:json)?\\s*([\\s\\S]*?)```/.exec(trimmed);\n\tif (fenced?.[1]) candidates.push(fenced[1].trim());\n\tconst start = trimmed.indexOf(\"{\");\n\tconst end = trimmed.lastIndexOf(\"}\");\n\tif (start >= 0 && end > start) candidates.push(trimmed.slice(start, end + 1));\n\n\tfor (const candidate of candidates) {\n\t\tlet parsed: unknown;\n\t\ttry {\n\t\t\tparsed = JSON.parse(candidate);\n\t\t} catch {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) continue;\n\t\tconst findingsRaw = (parsed as { findings?: unknown }).findings;\n\t\tif (!Array.isArray(findingsRaw)) continue;\n\n\t\tconst findings: Array<{ summary: string; confidence?: number }> = [];\n\t\tfor (const item of findingsRaw) {\n\t\t\tif (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n\t\t\tconst summary = (item as { summary?: unknown }).summary;\n\t\t\tif (typeof summary !== \"string\" || summary.trim().length === 0) continue;\n\t\t\tconst confidenceRaw = (item as { confidence?: unknown }).confidence;\n\t\t\tconst confidence =\n\t\t\t\ttypeof confidenceRaw === \"number\" && Number.isFinite(confidenceRaw)\n\t\t\t\t\t? Math.min(Math.max(confidenceRaw, 0), 1)\n\t\t\t\t\t: undefined;\n\t\t\tfindings.push({ summary: summary.trim(), confidence });\n\t\t\tif (findings.length >= maxFindings) break;\n\t\t}\n\t\t// A well-formed-but-empty findings array is a valid \"nothing found\"; a findings array whose\n\t\t// every item is malformed is not.\n\t\tif (findings.length > 0 || findingsRaw.length === 0) {\n\t\t\treturn { findings };\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction truncateExcerpt(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, Math.max(0, maxChars - 1))}…`;\n}\n\nfunction buildBundle(options: ResearchRunnerOptions, parsed: ParsedResearchFindings): EvidenceBundle {\n\tconst contextRef: EvidenceRef = {\n\t\tid: \"src-context\",\n\t\tkind: \"user\",\n\t\ttitle: \"Goal/context provided to the research lane\",\n\t\ttrusted: true,\n\t\texcerpt: truncateExcerpt(options.context && options.context.length > 0 ? options.context : options.query, 2000),\n\t};\n\tconst synthesisRef: EvidenceRef = {\n\t\tid: \"src-synthesis\",\n\t\tkind: \"tool\",\n\t\ttitle: \"Research-model synthesis\",\n\t\ttrusted: false,\n\t};\n\t// context + synthesis are the fixed provenance anchors (findings cite src-synthesis); workspace\n\t// sources fill whatever budget is left between them, so the anchors are never squeezed out.\n\tconst budget = Math.max(1, options.maxSources);\n\tconst workspaceRoom = Math.max(0, budget - 2);\n\tconst workspaceSources = (options.sources ?? []).slice(0, workspaceRoom);\n\tconst sources = [contextRef, ...workspaceSources, synthesisRef].slice(0, budget);\n\tconst findings: Finding[] = parsed.findings.slice(0, options.maxFindings).map((finding, index) => ({\n\t\tid: `finding-${index + 1}`,\n\t\tsummary: finding.summary,\n\t\tevidenceIds: [synthesisRef.id],\n\t\t...(finding.confidence !== undefined ? { confidence: finding.confidence } : {}),\n\t}));\n\treturn createEvidenceBundle({ query: options.query, sources, findings });\n}\n\nexport async function runResearch(options: ResearchRunnerOptions): Promise<ResearchRunResult> {\n\tconst gateOutcome = evaluateResearchRequest({\n\t\tenvelope: options.envelope,\n\t\tsourceKind: \"tool\",\n\t\testimatedUsd: options.maxUsd,\n\t});\n\tif (gateOutcome.outcome !== \"allow\") {\n\t\t// Skip-and-record, never prompt: gate denials inform diagnostics instead of blocking anything.\n\t\tconst status: ResearchRunStatus = gateOutcome.reasonCode === \"over_budget\" ? \"budget_exhausted\" : \"failed\";\n\t\treturn { status, reasonCode: gateOutcome.reasonCode, gateOutcome, costUsd: 0 };\n\t}\n\n\tconst bounded = await runBoundedCompletion({\n\t\tmaxWallClockMs: options.maxWallClockMs,\n\t\tsignal: options.signal,\n\t\texecute: (signal) =>\n\t\t\toptions.complete({\n\t\t\t\tsystemPrompt: RESEARCH_LANE_SYSTEM_PROMPT,\n\t\t\t\tuserPrompt: buildResearchUserPrompt(options),\n\t\t\t\tsignal,\n\t\t\t}),\n\t});\n\tif (bounded.failure) {\n\t\treturn {\n\t\t\tstatus: bounded.failure.status,\n\t\t\treasonCode: bounded.failure.reasonCode,\n\t\t\tgateOutcome,\n\t\t\tcostUsd: bounded.completion?.costUsd ?? 0,\n\t\t};\n\t}\n\tconst completion = bounded.completion;\n\tif (!completion) {\n\t\treturn { status: \"failed\", reasonCode: \"completion_error\", gateOutcome, costUsd: 0 };\n\t}\n\tif (completion.stopReason === \"error\" || completion.stopReason === \"aborted\") {\n\t\treturn { status: \"failed\", reasonCode: \"model_error\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst parsed = parseResearchFindings(completion.text, options.maxFindings);\n\tif (!parsed) {\n\t\treturn { status: \"failed\", reasonCode: \"unparseable_output\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst bundle = buildBundle(options, parsed);\n\tconst overBudget = options.maxUsd > 0 && completion.costUsd > options.maxUsd;\n\treturn {\n\t\tstatus: overBudget ? \"budget_exhausted\" : \"succeeded\",\n\t\treasonCode: overBudget\n\t\t\t? \"cost_budget_exceeded\"\n\t\t\t: parsed.findings.length === 0\n\t\t\t\t? \"no_findings\"\n\t\t\t\t: \"research_completed\",\n\t\tgateOutcome,\n\t\tbundle,\n\t\tcostUsd: completion.costUsd,\n\t};\n}\n"]}
|
|
@@ -22,6 +22,13 @@ export function buildResearchUserPrompt(args) {
|
|
|
22
22
|
if (args.context && args.context.length > 0) {
|
|
23
23
|
parts.push("", "Context:", args.context);
|
|
24
24
|
}
|
|
25
|
+
if (args.sources && args.sources.length > 0) {
|
|
26
|
+
parts.push("", "Workspace sources (pointer-first; open the file to read full context):");
|
|
27
|
+
for (const source of args.sources) {
|
|
28
|
+
const pointer = source.title ?? source.uri ?? source.id;
|
|
29
|
+
parts.push(source.excerpt ? `- ${pointer}: ${source.excerpt}` : `- ${pointer}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
25
32
|
parts.push("", `Return at most ${args.maxFindings} findings.`);
|
|
26
33
|
return parts.join("\n");
|
|
27
34
|
}
|
|
@@ -90,7 +97,12 @@ function buildBundle(options, parsed) {
|
|
|
90
97
|
title: "Research-model synthesis",
|
|
91
98
|
trusted: false,
|
|
92
99
|
};
|
|
93
|
-
|
|
100
|
+
// context + synthesis are the fixed provenance anchors (findings cite src-synthesis); workspace
|
|
101
|
+
// sources fill whatever budget is left between them, so the anchors are never squeezed out.
|
|
102
|
+
const budget = Math.max(1, options.maxSources);
|
|
103
|
+
const workspaceRoom = Math.max(0, budget - 2);
|
|
104
|
+
const workspaceSources = (options.sources ?? []).slice(0, workspaceRoom);
|
|
105
|
+
const sources = [contextRef, ...workspaceSources, synthesisRef].slice(0, budget);
|
|
94
106
|
const findings = parsed.findings.slice(0, options.maxFindings).map((finding, index) => ({
|
|
95
107
|
id: `finding-${index + 1}`,
|
|
96
108
|
summary: finding.summary,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"research-runner.js","sourceRoot":"","sources":["../../../src/core/research/research-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAEzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAE7D;;;;;;;GAOG;AAEH,2GAA2G;AAC3G,MAAM,CAAC,MAAM,2BAA2B,GAAG;IAC1C,uDAAuD;IACvD,kHAAkH;IAClH,+DAA+D;IAC/D,qFAAqF;IACrF,sFAAsF;CACtF,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAoCb,MAAM,UAAU,uBAAuB,CAAC,IAA8D,EAAU;IAC/G,MAAM,KAAK,GAAG,CAAC,mBAAmB,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IAChD,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7C,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,kBAAkB,IAAI,CAAC,WAAW,YAAY,CAAC,CAAC;IAC/D,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACxB;AAMD,MAAM,UAAU,qBAAqB,CAAC,I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C;QAC9E,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC;IAClG,CAAC;IAED,MAAM,MAAM,GAAG,qBAAqB,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAC3E,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,oBAAoB,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC;IACzG,CAAC;IAED,MAAM,MAAM,GAAG,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAC7E,OAAO;QACN,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,WAAW;QACrD,UAAU,EAAE,UAAU;YACrB,CAAC,CAAC,sBAAsB;YACxB,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;gBAC7B,CAAC,CAAC,aAAa;gBACf,CAAC,CAAC,oBAAoB;QACxB,WAAW;QACX,MAAM;QACN,OAAO,EAAE,UAAU,CAAC,OAAO;KAC3B,CAAC;AAAA,CACF","sourcesContent":["import { runBoundedCompletion } from \"../autonomy/bounded-completion.ts\";\nimport type { CapabilityEnvelope, EvidenceBundle, EvidenceRef, Finding, GateOutcome } from \"../autonomy/contracts.ts\";\nimport { createEvidenceBundle } from \"./evidence-bundle.ts\";\nimport { evaluateResearchRequest } from \"./research-gate.ts\";\n\n/**\n * Pure orchestration for one autonomous research pass: gate -> bounded isolated completion ->\n * parse -> evidence bundle. The model executor is injected so this stays provider-free and\n * session-free; production wires `AgentSession.runIsolatedCompletion` in.\n *\n * The lane is read-only by construction: the executor receives text prompts only, and the output\n * is an `EvidenceBundle` whose model-synthesized findings are marked untrusted.\n */\n\n/** Static across calls so callers can use `cacheRetention: \"short\"` and only pay for the variable tail. */\nexport const RESEARCH_LANE_SYSTEM_PROMPT = [\n\t\"You are a read-only research lane for a coding agent.\",\n\t\"You receive a research query plus bounded context and produce findings that help satisfy open goal requirements.\",\n\t\"Respond with STRICT JSON only - no prose, no markdown fences:\",\n\t'{\"findings\":[{\"summary\":\"<one concrete, actionable finding>\",\"confidence\":<0..1>}]}',\n\t\"Base findings only on the provided context. Never invent file paths, APIs, or facts.\",\n].join(\"\\n\");\n\nexport interface ResearchCompletion {\n\ttext: string;\n\tcostUsd: number;\n\tstopReason: string;\n}\n\nexport interface ResearchRunnerOptions {\n\tquery: string;\n\t/** Bounded, pre-redacted context handed to the research model (goal text, open requirements). */\n\tcontext?: string;\n\t/** Stripped research envelope - never the foreground/architect envelope. */\n\tenvelope: CapabilityEnvelope;\n\t/** Budget for this pass; a post-hoc breach marks the run budget_exhausted (spend stays visible). */\n\tmaxUsd: number;\n\tmaxSources: number;\n\tmaxFindings: number;\n\t/** Wall-clock budget in milliseconds; 0 disables. */\n\tmaxWallClockMs: number;\n\t/** Executes one isolated completion. Production: AgentSession.runIsolatedCompletion. */\n\tcomplete: (args: { systemPrompt: string; userPrompt: string; signal?: AbortSignal }) => Promise<ResearchCompletion>;\n\t/** External cancellation (e.g. session disposal). */\n\tsignal?: AbortSignal;\n}\n\nexport type ResearchRunStatus = \"succeeded\" | \"failed\" | \"canceled\" | \"timeout\" | \"budget_exhausted\";\n\nexport interface ResearchRunResult {\n\tstatus: ResearchRunStatus;\n\treasonCode: string;\n\tgateOutcome: GateOutcome;\n\tbundle?: EvidenceBundle;\n\tcostUsd: number;\n}\n\nexport function buildResearchUserPrompt(args: { query: string; context?: string; maxFindings: number }): string {\n\tconst parts = [`Research query: ${args.query}`];\n\tif (args.context && args.context.length > 0) {\n\t\tparts.push(\"\", \"Context:\", args.context);\n\t}\n\tparts.push(\"\", `Return at most ${args.maxFindings} findings.`);\n\treturn parts.join(\"\\n\");\n}\n\nexport interface ParsedResearchFindings {\n\tfindings: Array<{ summary: string; confidence?: number }>;\n}\n\nexport function parseResearchFindings(text: string, maxFindings: number): ParsedResearchFindings | undefined {\n\tconst trimmed = text.trim();\n\tconst candidates: string[] = [trimmed];\n\tconst fenced = /```(?:json)?\\s*([\\s\\S]*?)```/.exec(trimmed);\n\tif (fenced?.[1]) candidates.push(fenced[1].trim());\n\tconst start = trimmed.indexOf(\"{\");\n\tconst end = trimmed.lastIndexOf(\"}\");\n\tif (start >= 0 && end > start) candidates.push(trimmed.slice(start, end + 1));\n\n\tfor (const candidate of candidates) {\n\t\tlet parsed: unknown;\n\t\ttry {\n\t\t\tparsed = JSON.parse(candidate);\n\t\t} catch {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) continue;\n\t\tconst findingsRaw = (parsed as { findings?: unknown }).findings;\n\t\tif (!Array.isArray(findingsRaw)) continue;\n\n\t\tconst findings: Array<{ summary: string; confidence?: number }> = [];\n\t\tfor (const item of findingsRaw) {\n\t\t\tif (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n\t\t\tconst summary = (item as { summary?: unknown }).summary;\n\t\t\tif (typeof summary !== \"string\" || summary.trim().length === 0) continue;\n\t\t\tconst confidenceRaw = (item as { confidence?: unknown }).confidence;\n\t\t\tconst confidence =\n\t\t\t\ttypeof confidenceRaw === \"number\" && Number.isFinite(confidenceRaw)\n\t\t\t\t\t? Math.min(Math.max(confidenceRaw, 0), 1)\n\t\t\t\t\t: undefined;\n\t\t\tfindings.push({ summary: summary.trim(), confidence });\n\t\t\tif (findings.length >= maxFindings) break;\n\t\t}\n\t\t// A well-formed-but-empty findings array is a valid \"nothing found\"; a findings array whose\n\t\t// every item is malformed is not.\n\t\tif (findings.length > 0 || findingsRaw.length === 0) {\n\t\t\treturn { findings };\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction truncateExcerpt(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, Math.max(0, maxChars - 1))}…`;\n}\n\nfunction buildBundle(options: ResearchRunnerOptions, parsed: ParsedResearchFindings): EvidenceBundle {\n\tconst contextRef: EvidenceRef = {\n\t\tid: \"src-context\",\n\t\tkind: \"user\",\n\t\ttitle: \"Goal/context provided to the research lane\",\n\t\ttrusted: true,\n\t\texcerpt: truncateExcerpt(options.context && options.context.length > 0 ? options.context : options.query, 2000),\n\t};\n\tconst synthesisRef: EvidenceRef = {\n\t\tid: \"src-synthesis\",\n\t\tkind: \"tool\",\n\t\ttitle: \"Research-model synthesis\",\n\t\ttrusted: false,\n\t};\n\tconst sources = [contextRef, synthesisRef].slice(0, Math.max(1, options.maxSources));\n\tconst findings: Finding[] = parsed.findings.slice(0, options.maxFindings).map((finding, index) => ({\n\t\tid: `finding-${index + 1}`,\n\t\tsummary: finding.summary,\n\t\tevidenceIds: [synthesisRef.id],\n\t\t...(finding.confidence !== undefined ? { confidence: finding.confidence } : {}),\n\t}));\n\treturn createEvidenceBundle({ query: options.query, sources, findings });\n}\n\nexport async function runResearch(options: ResearchRunnerOptions): Promise<ResearchRunResult> {\n\tconst gateOutcome = evaluateResearchRequest({\n\t\tenvelope: options.envelope,\n\t\tsourceKind: \"tool\",\n\t\testimatedUsd: options.maxUsd,\n\t});\n\tif (gateOutcome.outcome !== \"allow\") {\n\t\t// Skip-and-record, never prompt: gate denials inform diagnostics instead of blocking anything.\n\t\tconst status: ResearchRunStatus = gateOutcome.reasonCode === \"over_budget\" ? \"budget_exhausted\" : \"failed\";\n\t\treturn { status, reasonCode: gateOutcome.reasonCode, gateOutcome, costUsd: 0 };\n\t}\n\n\tconst bounded = await runBoundedCompletion({\n\t\tmaxWallClockMs: options.maxWallClockMs,\n\t\tsignal: options.signal,\n\t\texecute: (signal) =>\n\t\t\toptions.complete({\n\t\t\t\tsystemPrompt: RESEARCH_LANE_SYSTEM_PROMPT,\n\t\t\t\tuserPrompt: buildResearchUserPrompt(options),\n\t\t\t\tsignal,\n\t\t\t}),\n\t});\n\tif (bounded.failure) {\n\t\treturn {\n\t\t\tstatus: bounded.failure.status,\n\t\t\treasonCode: bounded.failure.reasonCode,\n\t\t\tgateOutcome,\n\t\t\tcostUsd: bounded.completion?.costUsd ?? 0,\n\t\t};\n\t}\n\tconst completion = bounded.completion;\n\tif (!completion) {\n\t\treturn { status: \"failed\", reasonCode: \"completion_error\", gateOutcome, costUsd: 0 };\n\t}\n\tif (completion.stopReason === \"error\" || completion.stopReason === \"aborted\") {\n\t\treturn { status: \"failed\", reasonCode: \"model_error\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst parsed = parseResearchFindings(completion.text, options.maxFindings);\n\tif (!parsed) {\n\t\treturn { status: \"failed\", reasonCode: \"unparseable_output\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst bundle = buildBundle(options, parsed);\n\tconst overBudget = options.maxUsd > 0 && completion.costUsd > options.maxUsd;\n\treturn {\n\t\tstatus: overBudget ? \"budget_exhausted\" : \"succeeded\",\n\t\treasonCode: overBudget\n\t\t\t? \"cost_budget_exceeded\"\n\t\t\t: parsed.findings.length === 0\n\t\t\t\t? \"no_findings\"\n\t\t\t\t: \"research_completed\",\n\t\tgateOutcome,\n\t\tbundle,\n\t\tcostUsd: completion.costUsd,\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"research-runner.js","sourceRoot":"","sources":["../../../src/core/research/research-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAEzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAE7D;;;;;;;GAOG;AAEH,2GAA2G;AAC3G,MAAM,CAAC,MAAM,2BAA2B,GAAG;IAC1C,uDAAuD;IAC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,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;IACtF,CAAC;IACD,IAAI,UAAU,CAAC,UAAU,KAAK,OAAO,IAAI,UAAU,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QAC9E,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC;IAClG,CAAC;IAED,MAAM,MAAM,GAAG,qBAAqB,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAC3E,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,oBAAoB,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC;IACzG,CAAC;IAED,MAAM,MAAM,GAAG,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAC7E,OAAO;QACN,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,WAAW;QACrD,UAAU,EAAE,UAAU;YACrB,CAAC,CAAC,sBAAsB;YACxB,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;gBAC7B,CAAC,CAAC,aAAa;gBACf,CAAC,CAAC,oBAAoB;QACxB,WAAW;QACX,MAAM;QACN,OAAO,EAAE,UAAU,CAAC,OAAO;KAC3B,CAAC;AAAA,CACF","sourcesContent":["import { runBoundedCompletion } from \"../autonomy/bounded-completion.ts\";\nimport type { CapabilityEnvelope, EvidenceBundle, EvidenceRef, Finding, GateOutcome } from \"../autonomy/contracts.ts\";\nimport { createEvidenceBundle } from \"./evidence-bundle.ts\";\nimport { evaluateResearchRequest } from \"./research-gate.ts\";\n\n/**\n * Pure orchestration for one autonomous research pass: gate -> bounded isolated completion ->\n * parse -> evidence bundle. The model executor is injected so this stays provider-free and\n * session-free; production wires `AgentSession.runIsolatedCompletion` in.\n *\n * The lane is read-only by construction: the executor receives text prompts only, and the output\n * is an `EvidenceBundle` whose model-synthesized findings are marked untrusted.\n */\n\n/** Static across calls so callers can use `cacheRetention: \"short\"` and only pay for the variable tail. */\nexport const RESEARCH_LANE_SYSTEM_PROMPT = [\n\t\"You are a read-only research lane for a coding agent.\",\n\t\"You receive a research query plus bounded context and produce findings that help satisfy open goal requirements.\",\n\t\"Respond with STRICT JSON only - no prose, no markdown fences:\",\n\t'{\"findings\":[{\"summary\":\"<one concrete, actionable finding>\",\"confidence\":<0..1>}]}',\n\t\"Base findings only on the provided context. Never invent file paths, APIs, or facts.\",\n].join(\"\\n\");\n\nexport interface ResearchCompletion {\n\ttext: string;\n\tcostUsd: number;\n\tstopReason: string;\n}\n\nexport interface ResearchRunnerOptions {\n\tquery: string;\n\t/** Bounded, pre-redacted context handed to the research model (goal text, open requirements). */\n\tcontext?: string;\n\t/** Stripped research envelope - never the foreground/architect envelope. */\n\tenvelope: CapabilityEnvelope;\n\t/**\n\t * Pointer-first workspace sources (repo-relative path + bounded excerpt, never file bodies) that\n\t * inform the pass. They are rendered into the user prompt and carried into the evidence bundle;\n\t * omitted / empty reproduces the pre-collector behavior exactly.\n\t */\n\tsources?: readonly EvidenceRef[];\n\t/** Budget for this pass; a post-hoc breach marks the run budget_exhausted (spend stays visible). */\n\tmaxUsd: number;\n\tmaxSources: number;\n\tmaxFindings: number;\n\t/** Wall-clock budget in milliseconds; 0 disables. */\n\tmaxWallClockMs: number;\n\t/** Executes one isolated completion. Production: AgentSession.runIsolatedCompletion. */\n\tcomplete: (args: { systemPrompt: string; userPrompt: string; signal?: AbortSignal }) => Promise<ResearchCompletion>;\n\t/** External cancellation (e.g. session disposal). */\n\tsignal?: AbortSignal;\n}\n\nexport type ResearchRunStatus = \"succeeded\" | \"failed\" | \"canceled\" | \"timeout\" | \"budget_exhausted\";\n\nexport interface ResearchRunResult {\n\tstatus: ResearchRunStatus;\n\treasonCode: string;\n\tgateOutcome: GateOutcome;\n\tbundle?: EvidenceBundle;\n\tcostUsd: number;\n}\n\nexport function buildResearchUserPrompt(args: {\n\tquery: string;\n\tcontext?: string;\n\tsources?: readonly EvidenceRef[];\n\tmaxFindings: number;\n}): string {\n\tconst parts = [`Research query: ${args.query}`];\n\tif (args.context && args.context.length > 0) {\n\t\tparts.push(\"\", \"Context:\", args.context);\n\t}\n\tif (args.sources && args.sources.length > 0) {\n\t\tparts.push(\"\", \"Workspace sources (pointer-first; open the file to read full context):\");\n\t\tfor (const source of args.sources) {\n\t\t\tconst pointer = source.title ?? source.uri ?? source.id;\n\t\t\tparts.push(source.excerpt ? `- ${pointer}: ${source.excerpt}` : `- ${pointer}`);\n\t\t}\n\t}\n\tparts.push(\"\", `Return at most ${args.maxFindings} findings.`);\n\treturn parts.join(\"\\n\");\n}\n\nexport interface ParsedResearchFindings {\n\tfindings: Array<{ summary: string; confidence?: number }>;\n}\n\nexport function parseResearchFindings(text: string, maxFindings: number): ParsedResearchFindings | undefined {\n\tconst trimmed = text.trim();\n\tconst candidates: string[] = [trimmed];\n\tconst fenced = /```(?:json)?\\s*([\\s\\S]*?)```/.exec(trimmed);\n\tif (fenced?.[1]) candidates.push(fenced[1].trim());\n\tconst start = trimmed.indexOf(\"{\");\n\tconst end = trimmed.lastIndexOf(\"}\");\n\tif (start >= 0 && end > start) candidates.push(trimmed.slice(start, end + 1));\n\n\tfor (const candidate of candidates) {\n\t\tlet parsed: unknown;\n\t\ttry {\n\t\t\tparsed = JSON.parse(candidate);\n\t\t} catch {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) continue;\n\t\tconst findingsRaw = (parsed as { findings?: unknown }).findings;\n\t\tif (!Array.isArray(findingsRaw)) continue;\n\n\t\tconst findings: Array<{ summary: string; confidence?: number }> = [];\n\t\tfor (const item of findingsRaw) {\n\t\t\tif (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n\t\t\tconst summary = (item as { summary?: unknown }).summary;\n\t\t\tif (typeof summary !== \"string\" || summary.trim().length === 0) continue;\n\t\t\tconst confidenceRaw = (item as { confidence?: unknown }).confidence;\n\t\t\tconst confidence =\n\t\t\t\ttypeof confidenceRaw === \"number\" && Number.isFinite(confidenceRaw)\n\t\t\t\t\t? Math.min(Math.max(confidenceRaw, 0), 1)\n\t\t\t\t\t: undefined;\n\t\t\tfindings.push({ summary: summary.trim(), confidence });\n\t\t\tif (findings.length >= maxFindings) break;\n\t\t}\n\t\t// A well-formed-but-empty findings array is a valid \"nothing found\"; a findings array whose\n\t\t// every item is malformed is not.\n\t\tif (findings.length > 0 || findingsRaw.length === 0) {\n\t\t\treturn { findings };\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction truncateExcerpt(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, Math.max(0, maxChars - 1))}…`;\n}\n\nfunction buildBundle(options: ResearchRunnerOptions, parsed: ParsedResearchFindings): EvidenceBundle {\n\tconst contextRef: EvidenceRef = {\n\t\tid: \"src-context\",\n\t\tkind: \"user\",\n\t\ttitle: \"Goal/context provided to the research lane\",\n\t\ttrusted: true,\n\t\texcerpt: truncateExcerpt(options.context && options.context.length > 0 ? options.context : options.query, 2000),\n\t};\n\tconst synthesisRef: EvidenceRef = {\n\t\tid: \"src-synthesis\",\n\t\tkind: \"tool\",\n\t\ttitle: \"Research-model synthesis\",\n\t\ttrusted: false,\n\t};\n\t// context + synthesis are the fixed provenance anchors (findings cite src-synthesis); workspace\n\t// sources fill whatever budget is left between them, so the anchors are never squeezed out.\n\tconst budget = Math.max(1, options.maxSources);\n\tconst workspaceRoom = Math.max(0, budget - 2);\n\tconst workspaceSources = (options.sources ?? []).slice(0, workspaceRoom);\n\tconst sources = [contextRef, ...workspaceSources, synthesisRef].slice(0, budget);\n\tconst findings: Finding[] = parsed.findings.slice(0, options.maxFindings).map((finding, index) => ({\n\t\tid: `finding-${index + 1}`,\n\t\tsummary: finding.summary,\n\t\tevidenceIds: [synthesisRef.id],\n\t\t...(finding.confidence !== undefined ? { confidence: finding.confidence } : {}),\n\t}));\n\treturn createEvidenceBundle({ query: options.query, sources, findings });\n}\n\nexport async function runResearch(options: ResearchRunnerOptions): Promise<ResearchRunResult> {\n\tconst gateOutcome = evaluateResearchRequest({\n\t\tenvelope: options.envelope,\n\t\tsourceKind: \"tool\",\n\t\testimatedUsd: options.maxUsd,\n\t});\n\tif (gateOutcome.outcome !== \"allow\") {\n\t\t// Skip-and-record, never prompt: gate denials inform diagnostics instead of blocking anything.\n\t\tconst status: ResearchRunStatus = gateOutcome.reasonCode === \"over_budget\" ? \"budget_exhausted\" : \"failed\";\n\t\treturn { status, reasonCode: gateOutcome.reasonCode, gateOutcome, costUsd: 0 };\n\t}\n\n\tconst bounded = await runBoundedCompletion({\n\t\tmaxWallClockMs: options.maxWallClockMs,\n\t\tsignal: options.signal,\n\t\texecute: (signal) =>\n\t\t\toptions.complete({\n\t\t\t\tsystemPrompt: RESEARCH_LANE_SYSTEM_PROMPT,\n\t\t\t\tuserPrompt: buildResearchUserPrompt(options),\n\t\t\t\tsignal,\n\t\t\t}),\n\t});\n\tif (bounded.failure) {\n\t\treturn {\n\t\t\tstatus: bounded.failure.status,\n\t\t\treasonCode: bounded.failure.reasonCode,\n\t\t\tgateOutcome,\n\t\t\tcostUsd: bounded.completion?.costUsd ?? 0,\n\t\t};\n\t}\n\tconst completion = bounded.completion;\n\tif (!completion) {\n\t\treturn { status: \"failed\", reasonCode: \"completion_error\", gateOutcome, costUsd: 0 };\n\t}\n\tif (completion.stopReason === \"error\" || completion.stopReason === \"aborted\") {\n\t\treturn { status: \"failed\", reasonCode: \"model_error\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst parsed = parseResearchFindings(completion.text, options.maxFindings);\n\tif (!parsed) {\n\t\treturn { status: \"failed\", reasonCode: \"unparseable_output\", gateOutcome, costUsd: completion.costUsd };\n\t}\n\n\tconst bundle = buildBundle(options, parsed);\n\tconst overBudget = options.maxUsd > 0 && completion.costUsd > options.maxUsd;\n\treturn {\n\t\tstatus: overBudget ? \"budget_exhausted\" : \"succeeded\",\n\t\treasonCode: overBudget\n\t\t\t? \"cost_budget_exceeded\"\n\t\t\t: parsed.findings.length === 0\n\t\t\t\t? \"no_findings\"\n\t\t\t\t: \"research_completed\",\n\t\tgateOutcome,\n\t\tbundle,\n\t\tcostUsd: completion.costUsd,\n\t};\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type ExecFileException } from "node:child_process";
|
|
2
|
+
/** Structural DI seam: only the callback overload the collector actually uses — demanding
|
|
3
|
+
* node's full `typeof execFile` (with `__promisify__`) makes plain test mocks unassignable. */
|
|
4
|
+
export type WorkspaceExecFileFn = (command: string, args: readonly string[], options: {
|
|
5
|
+
cwd?: string;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
maxBuffer?: number;
|
|
8
|
+
encoding?: string;
|
|
9
|
+
windowsHide?: boolean;
|
|
10
|
+
}, callback: (error: ExecFileException | null, stdout: string, stderr: string) => void) => unknown;
|
|
11
|
+
import type { EvidenceRef } from "../autonomy/contracts.ts";
|
|
12
|
+
export interface CollectWorkspaceSourcesArgs {
|
|
13
|
+
/** Free text (goal + requirement text) that search terms are derived from. */
|
|
14
|
+
query: string;
|
|
15
|
+
/** Session working directory; ripgrep runs here and paths are reported relative to it. */
|
|
16
|
+
cwd: string;
|
|
17
|
+
/** Hard cap on returned sources; also the lane's source budget. */
|
|
18
|
+
maxSources: number;
|
|
19
|
+
/** Injected for tests; defaults to node's `execFile`. */
|
|
20
|
+
execFileFn?: WorkspaceExecFileFn;
|
|
21
|
+
}
|
|
22
|
+
/** Split on non-word runs, lowercase, drop stopwords/short/dupes, keep source order, cap at MAX_TERMS. */
|
|
23
|
+
export declare function deriveSearchTerms(query: string): string[];
|
|
24
|
+
export declare function collectWorkspaceSources(args: CollectWorkspaceSourcesArgs): Promise<EvidenceRef[]>;
|
|
25
|
+
//# sourceMappingURL=workspace-collector.d.ts.map
|