@tangle-network/agent-runtime 0.70.0 → 0.70.1
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/dist/agent.d.ts +3 -3
- package/dist/agent.js +5 -3
- package/dist/agent.js.map +1 -1
- package/dist/analyst-loop.d.ts +51 -0
- package/dist/analyst-loop.js +11 -0
- package/dist/analyst-loop.js.map +1 -0
- package/dist/chunk-BGOLR66M.js +214 -0
- package/dist/chunk-BGOLR66M.js.map +1 -0
- package/dist/{chunk-EDCVUZZC.js → chunk-CRAH5EY2.js} +2 -2
- package/dist/{chunk-L5ZFBVT6.js → chunk-HY4YOQM2.js} +2 -2
- package/dist/{chunk-ZNQVMMR5.js → chunk-LJZ5GC6C.js} +3 -3
- package/dist/chunk-P5OKDSLB.js +580 -0
- package/dist/chunk-P5OKDSLB.js.map +1 -0
- package/dist/chunk-VLF5RHEQ.js +143 -0
- package/dist/chunk-VLF5RHEQ.js.map +1 -0
- package/dist/{chunk-QXWGSDAQ.js → chunk-YFOPWG74.js} +13 -142
- package/dist/chunk-YFOPWG74.js.map +1 -0
- package/dist/{improvement-adapter-BVuMragr.d.ts → improvement-adapter-CioiEE2z.d.ts} +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +19 -16
- package/dist/index.js.map +1 -1
- package/dist/{loop-runner-bin-B0NeLTRd.d.ts → loop-runner-bin-a8bu4O5-.d.ts} +1 -1
- package/dist/loop-runner-bin.d.ts +2 -2
- package/dist/loop-runner-bin.js +5 -3
- package/dist/loops.d.ts +1 -1
- package/dist/loops.js +5 -3
- package/dist/mcp/bin.js +7 -6
- package/dist/mcp/bin.js.map +1 -1
- package/dist/mcp/index.js +12 -11
- package/dist/mcp/index.js.map +1 -1
- package/dist/platform.d.ts +255 -0
- package/dist/platform.js +229 -0
- package/dist/platform.js.map +1 -0
- package/dist/{types-DJu6TBGp.d.ts → types-BC3bZpH0.d.ts} +15 -1
- package/package.json +11 -1
- package/dist/chunk-BYZCXQHF.js +0 -474
- package/dist/chunk-BYZCXQHF.js.map +0 -1
- package/dist/chunk-QXWGSDAQ.js.map +0 -1
- /package/dist/{chunk-EDCVUZZC.js.map → chunk-CRAH5EY2.js.map} +0 -0
- /package/dist/{chunk-L5ZFBVT6.js.map → chunk-HY4YOQM2.js.map} +0 -0
- /package/dist/{chunk-ZNQVMMR5.js.map → chunk-LJZ5GC6C.js.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/loop-runner.ts","../src/loop-runner-bin.ts"],"sourcesContent":["/**\n * @experimental\n *\n * `runDelegatedLoop` — the configured delegated loop-runner.\n *\n * One typed entrypoint a worker agent (or a scheduled routine) calls to run a\n * disciplined loop in a chosen MODE, over agent-runtime's hardened engines:\n *\n * code → build-in-a-loop via the coder delegate (no-op + secret floor,\n * optional reviewer gate, winner-selection)\n * review → code mode with a REQUIRED reviewer (the gate is the point)\n * research → research-in-a-loop with valid-only KB growth (createKbGate)\n * audit → analyze trace/run data → findings (runAnalystLoop, caller-wired)\n * self-improve → closed-loop text/config optimization (selfImprove, held-out gated)\n *\n * It is intentionally a thin façade: the value is that EVERY product reuses the\n * one hardened engine instead of forking delegation logic. The dispatcher owns\n * mode routing, timing, fail-loud on an unregistered mode, and a uniform result\n * shape; each mode's engine is a pre-configured runner in the registry (build it\n * with the factories below, or inject your own / a stub).\n */\n\nimport type { Scenario } from '@tangle-network/agent-eval/campaign'\nimport {\n type SelfImproveOptions,\n type SelfImproveResult,\n selfImprove,\n} from '@tangle-network/agent-eval/contract'\nimport { runAnalystLoop } from './analyst-loop'\nimport type { RunAnalystLoopOpts, RunAnalystLoopResult } from './analyst-loop/types'\nimport { ConfigError } from './errors'\nimport {\n type CoderReviewer,\n type DelegateRunCtx,\n type DetachedWinnerSelection,\n detachedSessionDelegate,\n} from './mcp/delegates'\nimport type { CoderOutput } from './mcp/detached-coder'\nimport { type CreateKbGateOptions, createKbGate, type FactCandidate } from './mcp/kb-gate'\nimport type { DelegateCodeArgs } from './mcp/types'\nimport {\n type AuthoredHarness,\n type Budget,\n createExecutorRegistry,\n definePersona,\n runPersonified,\n type SandboxClient,\n type WinnerStrategy,\n type WorktreeFanoutOptions,\n type WorktreePatchArtifact,\n worktreeFanout,\n} from './runtime'\n\n/** @experimental Every delegated-loop mode, for validation + CLI surfaces. */\nexport const DELEGATED_LOOP_MODES = ['code', 'review', 'research', 'audit', 'self-improve'] as const\n\n/** @experimental */\nexport type DelegatedLoopMode = (typeof DELEGATED_LOOP_MODES)[number]\n\n/** @experimental Type guard for an untrusted mode string (CLI / config input). */\nexport function isDelegatedLoopMode(value: unknown): value is DelegatedLoopMode {\n return typeof value === 'string' && (DELEGATED_LOOP_MODES as readonly string[]).includes(value)\n}\n\n/** @experimental A pre-configured loop for one mode. Returns the mode's raw\n * output; the dispatcher wraps it in a {@link DelegatedLoopResult}. */\nexport type DelegatedLoopRunner<T = unknown> = (signal: AbortSignal) => Promise<T>\n\n/** @experimental Mode → configured runner. Partial: only register the modes a\n * given product/routine actually uses. */\nexport type DelegatedLoopRegistry = Partial<Record<DelegatedLoopMode, DelegatedLoopRunner>>\n\n/** @experimental Uniform result — never throws from a registered runner; a\n * thrown engine becomes `{ ok: false, error }` so a routine can record + move on. */\nexport interface DelegatedLoopResult<T = unknown> {\n mode: DelegatedLoopMode\n ok: boolean\n output?: T\n error?: string\n durationMs: number\n}\n\n/** @experimental */\nexport interface RunDelegatedLoopOptions {\n signal?: AbortSignal\n /** Clock override for deterministic tests. */\n now?: () => number\n}\n\n/**\n * @experimental\n *\n * Dispatch a configured loop by mode. Fails loud (throws `ConfigError`) when no\n * runner is registered for the mode — a routine pointed at an unwired mode is a\n * config bug, not a silent no-op. A runner that throws is captured as\n * `{ ok: false }` so unattended runs record the failure rather than crash.\n */\nexport async function runDelegatedLoop<T = unknown>(\n mode: DelegatedLoopMode,\n registry: DelegatedLoopRegistry,\n options: RunDelegatedLoopOptions = {},\n): Promise<DelegatedLoopResult<T>> {\n const runner = registry[mode] as DelegatedLoopRunner<T> | undefined\n if (!runner) {\n throw new ConfigError(\n `runDelegatedLoop: no runner registered for mode '${mode}' (registered: ${\n Object.keys(registry).join(', ') || 'none'\n })`,\n )\n }\n const now = options.now ?? Date.now\n const signal = options.signal ?? new AbortController().signal\n const start = now()\n try {\n const output = await runner(signal)\n return { mode, ok: true, output, durationMs: now() - start }\n } catch (err) {\n return {\n mode,\n ok: false,\n error: err instanceof Error ? err.message : String(err),\n durationMs: now() - start,\n }\n }\n}\n\n/** @experimental Options for the default `code`/`review` runner. */\nexport interface CoderLoopRunnerOptions {\n sandboxClient: SandboxClient\n /** What to build — the delegate args (goal, repoRoot, variants, config, …). */\n args: DelegateCodeArgs\n /** Adversarial reviewer. Pass one to run `review` mode (an approval gate over the candidate). */\n reviewer?: CoderReviewer\n /** Winner-selection strategy. Default `highest-score`. */\n winnerSelection?: DetachedWinnerSelection\n /** Harnesses for `variants > 1` fanout. */\n fanoutHarnesses?: string[]\n}\n\n/**\n * @experimental Build a `code`/`review`-mode runner over the sandbox-session coder delegate. Pass a\n * `reviewer` to run `review` mode — an approval gate over the validated candidate.\n */\nexport function coderLoopRunner(options: CoderLoopRunnerOptions): DelegatedLoopRunner<CoderOutput> {\n const delegate = detachedSessionDelegate({\n sandboxClient: options.sandboxClient,\n ...(options.reviewer ? { reviewer: options.reviewer } : {}),\n ...(options.winnerSelection ? { winnerSelection: options.winnerSelection } : {}),\n ...(options.fanoutHarnesses ? { fanoutHarnesses: options.fanoutHarnesses } : {}),\n })\n return async (signal) => {\n const ctx: DelegateRunCtx = { signal, report: () => {} }\n return delegate(options.args, ctx)\n }\n}\n\n/** @experimental Options for the local-repo `code` runner over the GENERIC recursive path. */\nexport interface WorktreeLoopRunnerOptions {\n /** Absolute path to the local git checkout each worktree is cut from. */\n repoRoot: string\n /** The instruction handed to every authored harness (composed under each profile's systemPrompt). */\n taskPrompt: string\n /** The supervisor-authored harness profiles — one fanout item (one worktree-CLI leaf) each. */\n harnesses: ReadonlyArray<AuthoredHarness>\n /** Conserved budget pool bounding the fanout (equal-k holds by construction). */\n budget: Budget\n /** Shell command run in each worktree to derive the tests-PASS signal. */\n testCmd?: string\n /** Shell command run in each worktree to derive the typecheck-PASS signal. */\n typecheckCmd?: string\n /** Which verification signals the deliverable REQUIRES present-and-passing (default none). */\n require?: ReadonlyArray<'tests' | 'typecheck'>\n /** Diff-size cap (lines). */\n maxDiffLines?: number\n /** Literal path prefixes the patch must not touch (the secret-floor is always on regardless). */\n forbiddenPaths?: string[]\n /** Winner-selection strategy among gated candidates. Default `highest-score`. */\n winnerStrategy?: WinnerStrategy\n /** Test seams forwarded to the worktree-CLI leaves so the runner drives offline. */\n runGit?: WorktreeFanoutOptions['runGit']\n runHarness?: WorktreeFanoutOptions['runHarness']\n runCommand?: WorktreeFanoutOptions['runCommand']\n}\n\n/**\n * @experimental\n *\n * `code` mode on the GENERIC recursive path: author one `AgentProfile` per harness, run them as a\n * `worktreeFanout` (N `createWorktreeCliExecutor` leaves, each `gateOnDeliverable`) through\n * `runPersonified` on the keystone Supervisor. This is the local-repo counterpart to\n * {@link coderLoopRunner} (which drives the in-box harness over a `SandboxClient`): no `runLoop`\n * driver, no role-coupled delegate — the harness list is the fanout, the gate is `patchDelivered`,\n * the winner is the shared valid-only selector (NOT `defaultSelectWinner`, whose non-valid fallback\n * would surface an ungated patch). Equal-k holds by the conserved budget pool. Returns the winning\n * patch artifact, or throws when no candidate is delivered (fail loud, never a vacuous done).\n */\nexport function worktreeLoopRunner(\n options: WorktreeLoopRunnerOptions,\n): DelegatedLoopRunner<WorktreePatchArtifact> {\n const shape = worktreeFanout<string>({\n repoRoot: options.repoRoot,\n taskPrompt: options.taskPrompt,\n harnesses: options.harnesses,\n ...(options.testCmd !== undefined ? { testCmd: options.testCmd } : {}),\n ...(options.typecheckCmd !== undefined ? { typecheckCmd: options.typecheckCmd } : {}),\n ...(options.require !== undefined ? { require: options.require } : {}),\n ...(options.maxDiffLines !== undefined ? { maxDiffLines: options.maxDiffLines } : {}),\n ...(options.forbiddenPaths !== undefined ? { forbiddenPaths: options.forbiddenPaths } : {}),\n ...(options.winnerStrategy !== undefined ? { winnerStrategy: options.winnerStrategy } : {}),\n ...(options.runGit ? { runGit: options.runGit } : {}),\n ...(options.runHarness ? { runHarness: options.runHarness } : {}),\n ...(options.runCommand ? { runCommand: options.runCommand } : {}),\n })\n // The persona's only role here is to carry the fanout shape onto the Supervisor; each item's\n // executor is BYO (the gated worktree-CLI leaf), so the registry only needs to pass BYO through.\n const persona = definePersona<WorktreePatchArtifact>({\n name: 'worktree-coder',\n root: { profile: { name: 'worktree-coder' }, harness: null },\n directive: 'deliver a minimal validated patch on a fresh worktree',\n context: { role: 'coder' },\n executors: { registry: createExecutorRegistry() },\n })\n return async (signal) => {\n const result = await runPersonified<string, WorktreePatchArtifact>({\n persona,\n shape,\n task: options.taskPrompt,\n budget: options.budget,\n signal,\n })\n if (result.kind !== 'winner' || result.out.kind !== 'done') {\n const blockers =\n result.kind === 'winner' && result.out.kind === 'blocked'\n ? result.out.blockers.join('; ')\n : `supervisor settled ${result.kind}`\n throw new Error(`worktreeLoopRunner: no delivered patch (${blockers})`)\n }\n return result.out.deliverable\n }\n}\n\n/** @experimental A fact rejected at the KB gate — surfaced, never dropped. */\nexport interface VetoedFact {\n candidate: FactCandidate\n vetoedBy?: string\n reason?: string\n}\n\n/** @experimental */\nexport interface ResearchLoopResult {\n /** Facts that passed the fail-closed gate — safe to write to the KB. */\n accepted: FactCandidate[]\n /** Facts the gate vetoed in the final round — escalate, do not silently drop. */\n vetoed: VetoedFact[]\n /** Research rounds actually run. */\n rounds: number\n}\n\n/** @experimental Options for the default `research` runner. */\nexport interface ResearchLoopRunnerOptions {\n /**\n * The research engine (the consumer's web/doc searcher + extractor). Called\n * each round with the prior round's vetoes so it can re-research the gaps.\n * Returns fact candidates carrying their grounding (`verbatimPassage` +\n * `sourceText`).\n */\n research: (round: number, vetoed: VetoedFact[]) => Promise<FactCandidate[]>\n /** Gate config (extra judges, self-artifact kinds, …). The floor is always on. */\n gate?: CreateKbGateOptions\n /** Max research rounds (correct-on-veto remediation). Default 1. */\n maxRounds?: number\n}\n\n/**\n * @experimental `research` mode — research-in-a-loop with valid-only KB growth.\n *\n * Each round: research → gate every candidate (fail-closed; passage MUST be in\n * the source) → accept the clean ones → re-research the vetoed ones next round,\n * up to `maxRounds`. Vetoed facts in the final round are RETURNED (escalate,\n * never silently dropped) so the caller audits vs retries.\n */\nexport function researchLoopRunner(\n o: ResearchLoopRunnerOptions,\n): DelegatedLoopRunner<ResearchLoopResult> {\n const gate = createKbGate(o.gate)\n const maxRounds = Math.max(1, Math.trunc(o.maxRounds ?? 1))\n return async (signal) => {\n const accepted: FactCandidate[] = []\n let vetoed: VetoedFact[] = []\n let rounds = 0\n for (let round = 0; round < maxRounds; round += 1) {\n if (signal.aborted) break\n rounds += 1\n const candidates = await o.research(round, vetoed)\n if (candidates.length === 0) break\n vetoed = []\n for (const c of candidates) {\n const v = await gate(c)\n if (v.accepted) accepted.push(c)\n else vetoed.push({ candidate: c, vetoedBy: v.vetoedBy, reason: v.reason })\n }\n if (vetoed.length === 0) break\n }\n return { accepted, vetoed, rounds }\n }\n}\n\n/** @experimental `self-improve` mode — agent-eval's one-call closed loop (held-out gated). */\nexport function selfImproveLoopRunner<TScenario extends Scenario, TArtifact>(\n options: SelfImproveOptions<TScenario, TArtifact>,\n): DelegatedLoopRunner<SelfImproveResult<TScenario, TArtifact>> {\n return async () => selfImprove<TScenario, TArtifact>(options)\n}\n\n/** @experimental `audit` mode — analyst loop over captured trace/run data. */\nexport function auditLoopRunner<TProposal = unknown, TEdit = unknown>(\n options: RunAnalystLoopOpts,\n): DelegatedLoopRunner<RunAnalystLoopResult<TProposal, TEdit>> {\n return async () => runAnalystLoop<TProposal, TEdit>(options)\n}\n","#!/usr/bin/env node\n/**\n * @experimental\n *\n * `agent-runtime-loop` — the schedulable entrypoint for the configured\n * delegated loop-runner. A cron job / routine / Makefile target invokes:\n *\n * agent-runtime-loop --mode research --config ./loops.config.js\n *\n * The config module wires the registry (with full access to env / creds —\n * which is why the deps live there, not in this generic bin). It must default-\n * export a `DelegatedLoopRegistry`, or a `() => DelegatedLoopRegistry | Promise<…>`.\n * The bin runs the selected mode, prints the `DelegatedLoopResult` as JSON, and\n * exits 0 on `ok`, 1 on a recorded failure, 2 on a usage/config error.\n */\n\nimport {\n DELEGATED_LOOP_MODES,\n type DelegatedLoopMode,\n type DelegatedLoopRegistry,\n type DelegatedLoopResult,\n isDelegatedLoopMode,\n runDelegatedLoop,\n} from './loop-runner'\n\n/** @experimental Parsed CLI invocation. */\nexport interface LoopRunnerCliArgs {\n mode: string\n /** Loads the registry — the bin wires this from `--config`; tests inject a stub. */\n loadRegistry: () => Promise<DelegatedLoopRegistry> | DelegatedLoopRegistry\n now?: () => number\n}\n\n/** @experimental */\nexport interface LoopRunnerCliResult {\n exitCode: number\n result?: DelegatedLoopResult\n error?: string\n}\n\n/**\n * @experimental\n *\n * Pure CLI core (no process / argv / IO) so it's unit-testable: validate the\n * mode, load the registry, dispatch, map to an exit code (0 ok / 1 failed /\n * 2 usage). Exported for embedding in custom runners + tests.\n */\nexport async function runLoopRunnerCli(args: LoopRunnerCliArgs): Promise<LoopRunnerCliResult> {\n if (!isDelegatedLoopMode(args.mode)) {\n return {\n exitCode: 2,\n error: `unknown mode '${args.mode}' (expected one of: ${DELEGATED_LOOP_MODES.join(', ')})`,\n }\n }\n let registry: DelegatedLoopRegistry\n try {\n registry = await args.loadRegistry()\n } catch (err) {\n return { exitCode: 2, error: `failed to load registry: ${errMsg(err)}` }\n }\n if (!registry[args.mode]) {\n return {\n exitCode: 2,\n error: `config registers no runner for mode '${args.mode}' (registered: ${\n Object.keys(registry).join(', ') || 'none'\n })`,\n }\n }\n // runDelegatedLoop throws only on a missing runner (guarded above); a failing\n // engine is captured as { ok: false } → exit 1, not a crash.\n const result = await runDelegatedLoop(args.mode as DelegatedLoopMode, registry, {\n ...(args.now ? { now: args.now } : {}),\n })\n return { exitCode: result.ok ? 0 : 1, result }\n}\n\n/** Parse `--mode X --config Y` from an argv tail (`process.argv.slice(2)`). */\nexport function parseLoopRunnerArgv(argv: string[]): { mode?: string; config?: string } {\n const out: { mode?: string; config?: string } = {}\n for (let i = 0; i < argv.length; i += 1) {\n const a = argv[i]\n if (a === '--mode') out.mode = argv[++i]\n else if (a === '--config') out.config = argv[++i]\n else if (a?.startsWith('--mode=')) out.mode = a.slice('--mode='.length)\n else if (a?.startsWith('--config=')) out.config = a.slice('--config='.length)\n }\n return out\n}\n\n/** Normalize a config module's default export → a registry. */\nfunction resolveRegistry(mod: unknown): DelegatedLoopRegistry {\n const def = (mod as { default?: unknown })?.default ?? mod\n const value = typeof def === 'function' ? (def as () => unknown)() : def\n return value as DelegatedLoopRegistry\n}\n\nfunction errMsg(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n\n/** The argv → IO → exit shell. Kept thin; logic lives in `runLoopRunnerCli`. */\nasync function main(): Promise<void> {\n const { mode, config } = parseLoopRunnerArgv(process.argv.slice(2))\n if (!mode || !config) {\n process.stderr.write(\n 'usage: agent-runtime-loop --mode <mode> --config <module>\\n' +\n ` modes: ${DELEGATED_LOOP_MODES.join(' | ')}\\n` +\n ' config: a JS/TS module default-exporting a DelegatedLoopRegistry (or a factory)\\n',\n )\n process.exit(2)\n }\n const { pathToFileURL } = await import('node:url')\n const { resolve } = await import('node:path')\n const cli = await runLoopRunnerCli({\n mode,\n loadRegistry: async () => resolveRegistry(await import(pathToFileURL(resolve(config)).href)),\n })\n process.stdout.write(`${JSON.stringify(cli.result ?? { error: cli.error }, null, 2)}\\n`)\n if (cli.error) process.stderr.write(`${cli.error}\\n`)\n process.exit(cli.exitCode)\n}\n\n// Run only when executed as the bin — never when imported for the testable\n// core, and never when bundled into a runtime that has no `process.argv`\n// (e.g. Cloudflare Workers, where `process` is a shim without `argv`). Reading\n// `process.argv[1]` directly would throw at module load there; `process.argv?.`\n// keeps the guard a no-op instead of crashing the Worker on startup.\nconst invokedScript = typeof process !== 'undefined' ? process.argv?.[1] : undefined\nif (invokedScript && /loop-runner-bin\\.(js|ts|mjs)$/.test(invokedScript)) {\n void main()\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAuBA;AAAA,EAGE;AAAA,OACK;AA2BA,IAAM,uBAAuB,CAAC,QAAQ,UAAU,YAAY,SAAS,cAAc;AAMnF,SAAS,oBAAoB,OAA4C;AAC9E,SAAO,OAAO,UAAU,YAAa,qBAA2C,SAAS,KAAK;AAChG;AAmCA,eAAsB,iBACpB,MACA,UACA,UAAmC,CAAC,GACH;AACjC,QAAM,SAAS,SAAS,IAAI;AAC5B,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI;AAAA,MACR,oDAAoD,IAAI,kBACtD,OAAO,KAAK,QAAQ,EAAE,KAAK,IAAI,KAAK,MACtC;AAAA,IACF;AAAA,EACF;AACA,QAAM,MAAM,QAAQ,OAAO,KAAK;AAChC,QAAM,SAAS,QAAQ,UAAU,IAAI,gBAAgB,EAAE;AACvD,QAAM,QAAQ,IAAI;AAClB,MAAI;AACF,UAAM,SAAS,MAAM,OAAO,MAAM;AAClC,WAAO,EAAE,MAAM,IAAI,MAAM,QAAQ,YAAY,IAAI,IAAI,MAAM;AAAA,EAC7D,SAAS,KAAK;AACZ,WAAO;AAAA,MACL;AAAA,MACA,IAAI;AAAA,MACJ,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,MACtD,YAAY,IAAI,IAAI;AAAA,IACtB;AAAA,EACF;AACF;AAmBO,SAAS,gBAAgB,SAAmE;AACjG,QAAM,WAAW,wBAAwB;AAAA,IACvC,eAAe,QAAQ;AAAA,IACvB,GAAI,QAAQ,WAAW,EAAE,UAAU,QAAQ,SAAS,IAAI,CAAC;AAAA,IACzD,GAAI,QAAQ,kBAAkB,EAAE,iBAAiB,QAAQ,gBAAgB,IAAI,CAAC;AAAA,IAC9E,GAAI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
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-YFOPWG74.js";
|
|
18
18
|
|
|
19
19
|
// src/mcp/openai-tools.ts
|
|
20
20
|
function buildTool(name, description, parameters) {
|
|
@@ -61,4 +61,4 @@ export {
|
|
|
61
61
|
mcpToolsForRuntimeMcp,
|
|
62
62
|
mcpToolsForRuntimeMcpSubset
|
|
63
63
|
};
|
|
64
|
-
//# sourceMappingURL=chunk-
|
|
64
|
+
//# sourceMappingURL=chunk-CRAH5EY2.js.map
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
runDetachedTurn,
|
|
8
8
|
runLoop,
|
|
9
9
|
selectValidWinner
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-YFOPWG74.js";
|
|
11
11
|
import {
|
|
12
12
|
coderProfile,
|
|
13
13
|
coderTaskToPrompt
|
|
@@ -472,4 +472,4 @@ export {
|
|
|
472
472
|
coderTaskFromArgs,
|
|
473
473
|
settleDetachedCoderTurn
|
|
474
474
|
};
|
|
475
|
-
//# sourceMappingURL=chunk-
|
|
475
|
+
//# sourceMappingURL=chunk-HY4YOQM2.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createFleetWorkspaceExecutor,
|
|
3
3
|
createSiblingSandboxExecutor
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-HY4YOQM2.js";
|
|
5
5
|
import {
|
|
6
6
|
runWorktreeHarness
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-YFOPWG74.js";
|
|
8
8
|
import {
|
|
9
9
|
buildLoopOtelSpans,
|
|
10
10
|
createOtelExporter
|
|
@@ -275,4 +275,4 @@ export {
|
|
|
275
275
|
createPropagatingTraceEmitter,
|
|
276
276
|
traceContextToEnv
|
|
277
277
|
};
|
|
278
|
-
//# sourceMappingURL=chunk-
|
|
278
|
+
//# sourceMappingURL=chunk-LJZ5GC6C.js.map
|
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AnalystError,
|
|
3
|
+
extractLlmCallEvent
|
|
4
|
+
} from "./chunk-VLF5RHEQ.js";
|
|
5
|
+
|
|
6
|
+
// src/analyst-loop/iterations-to-trace-store.ts
|
|
7
|
+
import {
|
|
8
|
+
DEFAULT_TRACE_ANALYST_BUDGETS,
|
|
9
|
+
TRACE_ANALYST_TRUNCATION_MARKER_PREFIX
|
|
10
|
+
} from "@tangle-network/agent-eval";
|
|
11
|
+
var bytesOf = (v) => Buffer.byteLength(JSON.stringify(v) ?? "", "utf8");
|
|
12
|
+
var iso = (ms) => new Date(ms).toISOString();
|
|
13
|
+
function normalizeSignature(message) {
|
|
14
|
+
return message.replace(/0x[0-9a-fA-F]+/g, "HEX").replace(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g, "UUID").replace(/(\/[\w.-]+){2,}/g, "PATH").replace(/\b\d+(\.\d+)?(ms|s|m|h)\b/g, "DUR").replace(/\b\d+\b/g, "#").replace(/\s+/g, " ").trim().slice(0, 200);
|
|
15
|
+
}
|
|
16
|
+
function spanKindFor(event, agentRunName) {
|
|
17
|
+
const llm = extractLlmCallEvent(event, agentRunName);
|
|
18
|
+
if (llm) return { kind: "LLM", model: llm.model ?? null, tool: null };
|
|
19
|
+
const type = String(event?.type ?? "");
|
|
20
|
+
if (/tool/i.test(type)) {
|
|
21
|
+
const d = event?.data;
|
|
22
|
+
const tool = typeof d?.name === "string" ? d.name : typeof d?.tool === "string" ? d.tool : type;
|
|
23
|
+
return { kind: "TOOL", model: null, tool };
|
|
24
|
+
}
|
|
25
|
+
return { kind: "SPAN", model: null, tool: null };
|
|
26
|
+
}
|
|
27
|
+
function errorMessageOf(event) {
|
|
28
|
+
const type = String(event?.type ?? "");
|
|
29
|
+
const d = event?.data;
|
|
30
|
+
if (/error|fail/i.test(type) || d?.error) {
|
|
31
|
+
const m = d?.error ?? d?.message;
|
|
32
|
+
return typeof m === "string" ? m : `${type} error`;
|
|
33
|
+
}
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
function projectIteration(iter) {
|
|
37
|
+
const traceId = iter.events.find((e) => e?.data?.sandboxId)?.data?.sandboxId ?? `iter-${iter.index}`;
|
|
38
|
+
const start = iso(iter.startedAt);
|
|
39
|
+
const end = iso(iter.endedAt || iter.startedAt);
|
|
40
|
+
const durationMs = Math.max(0, (iter.endedAt || iter.startedAt) - iter.startedAt);
|
|
41
|
+
const rootId = `${traceId}:root`;
|
|
42
|
+
const iterErrored = Boolean(iter.error) || iter.verdict?.valid === false;
|
|
43
|
+
const spans = [
|
|
44
|
+
{
|
|
45
|
+
trace_id: traceId,
|
|
46
|
+
span_id: rootId,
|
|
47
|
+
parent_span_id: null,
|
|
48
|
+
name: iter.agentRunName,
|
|
49
|
+
kind: "AGENT",
|
|
50
|
+
start_time: start,
|
|
51
|
+
end_time: end,
|
|
52
|
+
duration_ms: durationMs,
|
|
53
|
+
status: iter.error ? "ERROR" : "OK",
|
|
54
|
+
status_message: iter.error?.message,
|
|
55
|
+
service_name: "agent-runtime",
|
|
56
|
+
agent_name: iter.agentRunName,
|
|
57
|
+
model_name: null,
|
|
58
|
+
tool_name: null,
|
|
59
|
+
attributes: {
|
|
60
|
+
"iteration.index": iter.index,
|
|
61
|
+
"verdict.valid": iter.verdict?.valid,
|
|
62
|
+
"verdict.score": iter.verdict?.score,
|
|
63
|
+
"output.preview": iter.output === void 0 ? void 0 : String(iter.output).slice(0, 2e3)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
];
|
|
67
|
+
const models = /* @__PURE__ */ new Set();
|
|
68
|
+
const tools = /* @__PURE__ */ new Set();
|
|
69
|
+
iter.events.forEach((event, i) => {
|
|
70
|
+
const { kind, model, tool } = spanKindFor(event, iter.agentRunName);
|
|
71
|
+
if (model) models.add(model);
|
|
72
|
+
if (tool) tools.add(tool);
|
|
73
|
+
const errMsg = errorMessageOf(event);
|
|
74
|
+
spans.push({
|
|
75
|
+
trace_id: traceId,
|
|
76
|
+
span_id: `${traceId}:e${i}`,
|
|
77
|
+
parent_span_id: rootId,
|
|
78
|
+
name: String(event?.type ?? "event"),
|
|
79
|
+
kind,
|
|
80
|
+
start_time: start,
|
|
81
|
+
end_time: end,
|
|
82
|
+
duration_ms: 0,
|
|
83
|
+
status: errMsg ? "ERROR" : "OK",
|
|
84
|
+
status_message: errMsg,
|
|
85
|
+
service_name: "agent-runtime",
|
|
86
|
+
agent_name: iter.agentRunName,
|
|
87
|
+
model_name: model,
|
|
88
|
+
tool_name: tool,
|
|
89
|
+
attributes: event?.data ?? {}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
const hasErrors = spans.some((s) => s.status === "ERROR") || iterErrored;
|
|
93
|
+
const summary = {
|
|
94
|
+
trace_id: traceId,
|
|
95
|
+
service_name: "agent-runtime",
|
|
96
|
+
agent_name: iter.agentRunName,
|
|
97
|
+
span_count: spans.length,
|
|
98
|
+
has_errors: hasErrors,
|
|
99
|
+
start_time: start,
|
|
100
|
+
end_time: end,
|
|
101
|
+
duration_ms: durationMs,
|
|
102
|
+
raw_jsonl_bytes: bytesOf(spans),
|
|
103
|
+
models: [...models],
|
|
104
|
+
tools: [...tools]
|
|
105
|
+
};
|
|
106
|
+
return { summary, spans, rawBytes: summary.raw_jsonl_bytes };
|
|
107
|
+
}
|
|
108
|
+
function matchesFilters(t, f) {
|
|
109
|
+
if (!f) return true;
|
|
110
|
+
if (f.has_errors !== void 0 && t.summary.has_errors !== f.has_errors) return false;
|
|
111
|
+
if (f.service_names?.length && !f.service_names.includes(t.summary.service_name ?? ""))
|
|
112
|
+
return false;
|
|
113
|
+
if (f.agent_names?.length && !f.agent_names.includes(t.summary.agent_name ?? "")) return false;
|
|
114
|
+
if (f.model_names?.length && !f.model_names.some((m) => t.summary.models.includes(m)))
|
|
115
|
+
return false;
|
|
116
|
+
if (f.tool_names?.length && !f.tool_names.some((tn) => t.summary.tools.includes(tn))) return false;
|
|
117
|
+
if (f.start_time_after && t.summary.start_time < f.start_time_after) return false;
|
|
118
|
+
if (f.start_time_before && t.summary.start_time > f.start_time_before) return false;
|
|
119
|
+
if (f.regex_pattern && !new RegExp(f.regex_pattern).test(JSON.stringify(t.spans))) return false;
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
function capAttributes(attributes, perAttrCap) {
|
|
123
|
+
let truncated = 0;
|
|
124
|
+
const capped = {};
|
|
125
|
+
for (const [k, v] of Object.entries(attributes)) {
|
|
126
|
+
const s = typeof v === "string" ? v : JSON.stringify(v);
|
|
127
|
+
if (typeof s === "string" && s.length > perAttrCap) {
|
|
128
|
+
truncated += 1;
|
|
129
|
+
capped[k] = `${TRACE_ANALYST_TRUNCATION_MARKER_PREFIX} ${s.length}b]${s.slice(0, perAttrCap)}`;
|
|
130
|
+
} else {
|
|
131
|
+
capped[k] = v;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return { capped, truncated };
|
|
135
|
+
}
|
|
136
|
+
function iterationsToTraceStore(iterations, budgets = DEFAULT_TRACE_ANALYST_BUDGETS) {
|
|
137
|
+
if (iterations.length === 0) {
|
|
138
|
+
throw new AnalystError("iterationsToTraceStore: no iterations to analyze (empty round)");
|
|
139
|
+
}
|
|
140
|
+
const traces = iterations.map((it) => projectIteration(it));
|
|
141
|
+
const byId = new Map(traces.map((t) => [t.summary.trace_id, t]));
|
|
142
|
+
const buildClusters = (set) => {
|
|
143
|
+
const map = /* @__PURE__ */ new Map();
|
|
144
|
+
for (const t of set) {
|
|
145
|
+
for (const s of t.spans) {
|
|
146
|
+
if (s.status !== "ERROR" || !s.status_message) continue;
|
|
147
|
+
const sig = normalizeSignature(s.status_message);
|
|
148
|
+
const c = map.get(sig) ?? {
|
|
149
|
+
signature: sig,
|
|
150
|
+
status_message_sample: s.status_message,
|
|
151
|
+
span_name: s.name,
|
|
152
|
+
tool_name: s.tool_name,
|
|
153
|
+
trace_count: 0,
|
|
154
|
+
span_count: 0,
|
|
155
|
+
prevalence: 0,
|
|
156
|
+
exemplar_trace_ids: [],
|
|
157
|
+
exemplar_span_ids: []
|
|
158
|
+
};
|
|
159
|
+
c.span_count += 1;
|
|
160
|
+
if (!c.exemplar_trace_ids.includes(t.summary.trace_id) && c.exemplar_trace_ids.length < 10) {
|
|
161
|
+
c.exemplar_trace_ids.push(t.summary.trace_id);
|
|
162
|
+
c.trace_count += 1;
|
|
163
|
+
}
|
|
164
|
+
if (c.exemplar_span_ids.length < 10) c.exemplar_span_ids.push(s.span_id);
|
|
165
|
+
map.set(sig, c);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const errorTraces = set.filter((t) => t.summary.has_errors).length || 1;
|
|
169
|
+
const clusters = [...map.values()].map((c) => ({
|
|
170
|
+
...c,
|
|
171
|
+
prevalence: c.trace_count / errorTraces
|
|
172
|
+
}));
|
|
173
|
+
return clusters.sort((a, b) => b.trace_count - a.trace_count);
|
|
174
|
+
};
|
|
175
|
+
return {
|
|
176
|
+
async getOverview(filters) {
|
|
177
|
+
const set = traces.filter((t) => matchesFilters(t, filters));
|
|
178
|
+
const services = /* @__PURE__ */ new Set();
|
|
179
|
+
const agents = /* @__PURE__ */ new Set();
|
|
180
|
+
const models = /* @__PURE__ */ new Set();
|
|
181
|
+
const tools = /* @__PURE__ */ new Set();
|
|
182
|
+
let errorSpans = 0;
|
|
183
|
+
for (const t of set) {
|
|
184
|
+
if (t.summary.service_name) services.add(t.summary.service_name);
|
|
185
|
+
if (t.summary.agent_name) agents.add(t.summary.agent_name);
|
|
186
|
+
for (const m of t.summary.models) models.add(m);
|
|
187
|
+
for (const tn of t.summary.tools) tools.add(tn);
|
|
188
|
+
errorSpans += t.spans.filter((s) => s.status === "ERROR").length;
|
|
189
|
+
}
|
|
190
|
+
const times = set.map((t) => t.summary.start_time).sort();
|
|
191
|
+
return {
|
|
192
|
+
total_traces: set.length,
|
|
193
|
+
raw_jsonl_bytes: set.reduce((n, t) => n + t.rawBytes, 0),
|
|
194
|
+
services: [...services],
|
|
195
|
+
agents: [...agents],
|
|
196
|
+
models: [...models],
|
|
197
|
+
tool_names: [...tools],
|
|
198
|
+
sample_trace_ids: set.slice(0, 20).map((t) => t.summary.trace_id),
|
|
199
|
+
errors: {
|
|
200
|
+
trace_count: set.filter((t) => t.summary.has_errors).length,
|
|
201
|
+
span_count: errorSpans
|
|
202
|
+
},
|
|
203
|
+
error_clusters: buildClusters(set),
|
|
204
|
+
time_range: times.length ? { earliest: times[0], latest: times[times.length - 1] } : null
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
async queryTraces(opts) {
|
|
208
|
+
const set = traces.filter((t) => matchesFilters(t, opts.filters));
|
|
209
|
+
const offset = opts.offset ?? 0;
|
|
210
|
+
const page = set.slice(offset, offset + opts.limit);
|
|
211
|
+
return {
|
|
212
|
+
traces: page.map((t) => t.summary),
|
|
213
|
+
total: set.length,
|
|
214
|
+
has_more: offset + opts.limit < set.length
|
|
215
|
+
};
|
|
216
|
+
},
|
|
217
|
+
async countTraces(filters) {
|
|
218
|
+
return traces.filter((t) => matchesFilters(t, filters)).length;
|
|
219
|
+
},
|
|
220
|
+
async viewTrace(opts) {
|
|
221
|
+
const t = byId.get(opts.trace_id);
|
|
222
|
+
if (!t) return { trace_id: opts.trace_id, spans: [] };
|
|
223
|
+
const cap = opts.per_attribute_byte_cap ?? budgets.perAttributeViewBudget;
|
|
224
|
+
const projected = t.spans.map((s) => ({
|
|
225
|
+
...s,
|
|
226
|
+
attributes: capAttributes(s.attributes, cap).capped
|
|
227
|
+
}));
|
|
228
|
+
if (bytesOf(projected) > budgets.perCallByteCeiling) {
|
|
229
|
+
const names = /* @__PURE__ */ new Map();
|
|
230
|
+
for (const s of t.spans) names.set(s.name, (names.get(s.name) ?? 0) + 1);
|
|
231
|
+
return {
|
|
232
|
+
trace_id: opts.trace_id,
|
|
233
|
+
oversized: {
|
|
234
|
+
span_count: t.spans.length,
|
|
235
|
+
top_span_names: [...names.entries()].sort((a, b) => b[1] - a[1]).slice(0, 20),
|
|
236
|
+
span_response_bytes_max: Math.max(...t.spans.map((s) => bytesOf(s))),
|
|
237
|
+
error_span_count: t.spans.filter((s) => s.status === "ERROR").length
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
return { trace_id: opts.trace_id, spans: projected };
|
|
242
|
+
},
|
|
243
|
+
async viewSpans(opts) {
|
|
244
|
+
const t = byId.get(opts.trace_id);
|
|
245
|
+
const cap = opts.per_attribute_byte_cap ?? budgets.perAttributeSpanBudget;
|
|
246
|
+
const want = new Set(opts.span_ids);
|
|
247
|
+
const found = (t?.spans ?? []).filter((s) => want.has(s.span_id));
|
|
248
|
+
let truncated = 0;
|
|
249
|
+
const spans = found.map((s) => {
|
|
250
|
+
const { capped, truncated: n } = capAttributes(s.attributes, cap);
|
|
251
|
+
truncated += n;
|
|
252
|
+
return { ...s, attributes: capped };
|
|
253
|
+
});
|
|
254
|
+
const foundIds = new Set(found.map((s) => s.span_id));
|
|
255
|
+
return {
|
|
256
|
+
trace_id: opts.trace_id,
|
|
257
|
+
spans,
|
|
258
|
+
missing_span_ids: opts.span_ids.filter((id) => !foundIds.has(id)),
|
|
259
|
+
truncated_attribute_count: truncated
|
|
260
|
+
};
|
|
261
|
+
},
|
|
262
|
+
async searchTrace(opts) {
|
|
263
|
+
const t = byId.get(opts.trace_id);
|
|
264
|
+
const max = opts.max_matches ?? 50;
|
|
265
|
+
const hits = [];
|
|
266
|
+
for (const s of t?.spans ?? []) {
|
|
267
|
+
for (const hit of searchSpanAttrs(s, opts.regex_pattern, budgets.perMatchTextBudget)) {
|
|
268
|
+
if (hits.length >= max) break;
|
|
269
|
+
hits.push(hit);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
trace_id: opts.trace_id,
|
|
274
|
+
hits,
|
|
275
|
+
total_matches: hits.length,
|
|
276
|
+
has_more: hits.length >= max
|
|
277
|
+
};
|
|
278
|
+
},
|
|
279
|
+
async searchSpan(opts) {
|
|
280
|
+
const t = byId.get(opts.trace_id);
|
|
281
|
+
const max = opts.max_matches ?? 50;
|
|
282
|
+
const span = (t?.spans ?? []).find((s) => s.span_id === opts.span_id);
|
|
283
|
+
const hits = span ? searchSpanAttrs(span, opts.regex_pattern, budgets.perMatchTextBudget).slice(0, max) : [];
|
|
284
|
+
return {
|
|
285
|
+
trace_id: opts.trace_id,
|
|
286
|
+
span_id: opts.span_id,
|
|
287
|
+
hits,
|
|
288
|
+
total_matches: hits.length,
|
|
289
|
+
has_more: false
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function searchSpanAttrs(span, pattern, textCap) {
|
|
295
|
+
const re = new RegExp(pattern, "g");
|
|
296
|
+
const hits = [];
|
|
297
|
+
for (const [k, v] of Object.entries(span.attributes)) {
|
|
298
|
+
const text = typeof v === "string" ? v : JSON.stringify(v);
|
|
299
|
+
if (typeof text !== "string") continue;
|
|
300
|
+
re.lastIndex = 0;
|
|
301
|
+
const m = re.exec(text);
|
|
302
|
+
if (!m) continue;
|
|
303
|
+
const at = m.index;
|
|
304
|
+
hits.push({
|
|
305
|
+
trace_id: span.trace_id,
|
|
306
|
+
span_id: span.span_id,
|
|
307
|
+
span_name: span.name,
|
|
308
|
+
span_kind: span.kind,
|
|
309
|
+
attribute_path: `attributes.${k}`,
|
|
310
|
+
matched_text: m[0].slice(0, textCap),
|
|
311
|
+
context_before: text.slice(Math.max(0, at - textCap / 2), at),
|
|
312
|
+
context_after: text.slice(at + m[0].length, at + m[0].length + textCap / 2),
|
|
313
|
+
match_offset: at
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return hits;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/analyst-loop/run-analyst-loop.ts
|
|
320
|
+
import { diffFindings } from "@tangle-network/agent-eval";
|
|
321
|
+
async function runAnalystLoop(opts) {
|
|
322
|
+
const log = opts.log ?? defaultLog;
|
|
323
|
+
const strategy = opts.priorFindingsStrategy ?? "per-kind";
|
|
324
|
+
const emit = makeEmitter(opts.onEvent);
|
|
325
|
+
const startedAt = Date.now();
|
|
326
|
+
const baselineRunId = resolveBaselineRunId(opts);
|
|
327
|
+
const priorAll = baselineRunId ? opts.findingsStore?.loadRun(baselineRunId) ?? [] : [];
|
|
328
|
+
log("baseline resolved", { baselineRunId, prior_findings: priorAll.length });
|
|
329
|
+
await emit({
|
|
330
|
+
type: "baseline-resolved",
|
|
331
|
+
runId: opts.runId,
|
|
332
|
+
baselineRunId,
|
|
333
|
+
priorFindingCount: priorAll.length
|
|
334
|
+
});
|
|
335
|
+
const priorFindings = buildPriorFindingsInput(priorAll, strategy, opts.registry.list());
|
|
336
|
+
const analystResult = await runRegistry(opts, priorFindings, emit);
|
|
337
|
+
log("analyst run complete", {
|
|
338
|
+
findings: analystResult.findings.length,
|
|
339
|
+
cost_usd: analystResult.total_cost_usd,
|
|
340
|
+
per_analyst: analystResult.per_analyst.map((s) => ({
|
|
341
|
+
id: s.analyst_id,
|
|
342
|
+
status: s.status,
|
|
343
|
+
n: s.findings_count
|
|
344
|
+
}))
|
|
345
|
+
});
|
|
346
|
+
if (opts.findingsStore && analystResult.findings.length > 0) {
|
|
347
|
+
await opts.findingsStore.append(opts.runId, analystResult.findings);
|
|
348
|
+
await emit({
|
|
349
|
+
type: "findings-persisted",
|
|
350
|
+
runId: opts.runId,
|
|
351
|
+
count: analystResult.findings.length
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
let diff = null;
|
|
355
|
+
if (baselineRunId && analystResult.findings.length > 0) {
|
|
356
|
+
diff = diffFindings(
|
|
357
|
+
priorAll.map((f) => ({ ...f })),
|
|
358
|
+
analystResult.findings.map((f) => ({ ...f, run_id: opts.runId }))
|
|
359
|
+
);
|
|
360
|
+
log("diff vs baseline", {
|
|
361
|
+
appeared: diff.appeared.length,
|
|
362
|
+
disappeared: diff.disappeared.length,
|
|
363
|
+
persisted: diff.persisted.length,
|
|
364
|
+
changed: diff.changed.length
|
|
365
|
+
});
|
|
366
|
+
await emit({
|
|
367
|
+
type: "diff-computed",
|
|
368
|
+
runId: opts.runId,
|
|
369
|
+
baselineRunId,
|
|
370
|
+
appeared: diff.appeared.length,
|
|
371
|
+
disappeared: diff.disappeared.length,
|
|
372
|
+
persisted: diff.persisted.length,
|
|
373
|
+
changed: diff.changed.length
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
let knowledge = null;
|
|
377
|
+
if (opts.knowledgeAdapter) {
|
|
378
|
+
knowledge = await runKnowledgeAdapter(opts, analystResult.findings, log, emit);
|
|
379
|
+
}
|
|
380
|
+
let improvement = null;
|
|
381
|
+
if (opts.improvementAdapter) {
|
|
382
|
+
improvement = await runImprovementAdapter(opts, analystResult.findings, log, emit);
|
|
383
|
+
}
|
|
384
|
+
await emit({
|
|
385
|
+
type: "loop-completed",
|
|
386
|
+
runId: opts.runId,
|
|
387
|
+
durationMs: Date.now() - startedAt
|
|
388
|
+
});
|
|
389
|
+
return {
|
|
390
|
+
runId: opts.runId,
|
|
391
|
+
baselineRunId,
|
|
392
|
+
analystResult,
|
|
393
|
+
diff,
|
|
394
|
+
knowledge,
|
|
395
|
+
improvement
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function makeEmitter(onEvent) {
|
|
399
|
+
if (!onEvent) return async () => {
|
|
400
|
+
};
|
|
401
|
+
return async (event) => {
|
|
402
|
+
await onEvent(event);
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
async function runRegistry(opts, priorFindings, emit) {
|
|
406
|
+
const reg = opts.registry;
|
|
407
|
+
if (typeof reg.runStream === "function" && opts.onEvent) {
|
|
408
|
+
let final = null;
|
|
409
|
+
for await (const ev of reg.runStream(opts.runId, opts.inputs, { priorFindings })) {
|
|
410
|
+
await emit({ type: "analyst", runId: opts.runId, event: ev });
|
|
411
|
+
if (ev.type === "run-completed") final = ev.result;
|
|
412
|
+
}
|
|
413
|
+
if (!final) {
|
|
414
|
+
throw new Error("runAnalystLoop: registry.runStream ended without run-completed event");
|
|
415
|
+
}
|
|
416
|
+
return final;
|
|
417
|
+
}
|
|
418
|
+
return opts.registry.run(opts.runId, opts.inputs, { priorFindings });
|
|
419
|
+
}
|
|
420
|
+
function resolveBaselineRunId(opts) {
|
|
421
|
+
if (opts.baselineRunId === null) return null;
|
|
422
|
+
if (typeof opts.baselineRunId === "string") return opts.baselineRunId;
|
|
423
|
+
if (!opts.findingsStore) return null;
|
|
424
|
+
const all = opts.findingsStore.loadAll();
|
|
425
|
+
let last = null;
|
|
426
|
+
for (const row of all) {
|
|
427
|
+
if (row.run_id === opts.runId) continue;
|
|
428
|
+
last = row.run_id;
|
|
429
|
+
}
|
|
430
|
+
return last;
|
|
431
|
+
}
|
|
432
|
+
function buildPriorFindingsInput(prior, strategy, registry) {
|
|
433
|
+
if (strategy === "none" || prior.length === 0) return void 0;
|
|
434
|
+
const stripped = prior.map(({ run_id: _run_id, ...rest }) => rest);
|
|
435
|
+
if (strategy === "wildcard") {
|
|
436
|
+
return { "*": stripped };
|
|
437
|
+
}
|
|
438
|
+
void registry;
|
|
439
|
+
return stripped;
|
|
440
|
+
}
|
|
441
|
+
async function runKnowledgeAdapter(opts, findings, log, emit) {
|
|
442
|
+
const adapter = opts.knowledgeAdapter;
|
|
443
|
+
const batch = await adapter.proposeFromFindings(findings);
|
|
444
|
+
log("knowledge.proposeFromFindings", {
|
|
445
|
+
proposals: batch.proposals.length,
|
|
446
|
+
skipped: batch.skipped,
|
|
447
|
+
errors: batch.errors.length
|
|
448
|
+
});
|
|
449
|
+
await emit({
|
|
450
|
+
type: "knowledge-proposed",
|
|
451
|
+
runId: opts.runId,
|
|
452
|
+
proposalCount: batch.proposals.length,
|
|
453
|
+
skipped: batch.skipped,
|
|
454
|
+
errors: batch.errors.length
|
|
455
|
+
});
|
|
456
|
+
const auto = opts.autoApply?.knowledge ?? false;
|
|
457
|
+
const threshold = opts.autoApply?.knowledgeConfidenceThreshold ?? 0.85;
|
|
458
|
+
if (!auto || !adapter.apply) {
|
|
459
|
+
await emit({
|
|
460
|
+
type: "knowledge-applied",
|
|
461
|
+
runId: opts.runId,
|
|
462
|
+
writtenCount: 0,
|
|
463
|
+
withheldForReview: batch.proposals.length
|
|
464
|
+
});
|
|
465
|
+
return {
|
|
466
|
+
proposals: batch.proposals,
|
|
467
|
+
applied: [],
|
|
468
|
+
skipped: batch.skipped,
|
|
469
|
+
errors: batch.errors,
|
|
470
|
+
withheld_for_review: batch.proposals.length
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const findingsById = new Map(findings.map((f) => [f.finding_id, f]));
|
|
474
|
+
const safe = [];
|
|
475
|
+
let withheld = 0;
|
|
476
|
+
for (const p of batch.proposals) {
|
|
477
|
+
const src = p.sourceFindingId ? findingsById.get(p.sourceFindingId) : void 0;
|
|
478
|
+
if (!src) {
|
|
479
|
+
withheld += 1;
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (src.confidence < threshold) {
|
|
483
|
+
withheld += 1;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
safe.push(p);
|
|
487
|
+
}
|
|
488
|
+
const result = await adapter.apply(safe);
|
|
489
|
+
log("knowledge.apply", {
|
|
490
|
+
applied: result.written.length,
|
|
491
|
+
withheld_for_review: withheld,
|
|
492
|
+
warnings: result.warnings.length
|
|
493
|
+
});
|
|
494
|
+
await emit({
|
|
495
|
+
type: "knowledge-applied",
|
|
496
|
+
runId: opts.runId,
|
|
497
|
+
writtenCount: result.written.length,
|
|
498
|
+
withheldForReview: withheld
|
|
499
|
+
});
|
|
500
|
+
return {
|
|
501
|
+
proposals: batch.proposals,
|
|
502
|
+
applied: result.written,
|
|
503
|
+
skipped: batch.skipped,
|
|
504
|
+
errors: batch.errors,
|
|
505
|
+
withheld_for_review: withheld
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
async function runImprovementAdapter(opts, findings, log, emit) {
|
|
509
|
+
const adapter = opts.improvementAdapter;
|
|
510
|
+
const batch = await adapter.proposeFromFindings(findings);
|
|
511
|
+
log("improvement.proposeFromFindings", {
|
|
512
|
+
edits: batch.edits.length,
|
|
513
|
+
skipped: batch.skipped,
|
|
514
|
+
errors: batch.errors.length
|
|
515
|
+
});
|
|
516
|
+
await emit({
|
|
517
|
+
type: "improvement-proposed",
|
|
518
|
+
runId: opts.runId,
|
|
519
|
+
editCount: batch.edits.length,
|
|
520
|
+
skipped: batch.skipped,
|
|
521
|
+
errors: batch.errors.length
|
|
522
|
+
});
|
|
523
|
+
const auto = opts.autoApply?.improvement ?? false;
|
|
524
|
+
const threshold = opts.autoApply?.improvementConfidenceThreshold ?? 0.9;
|
|
525
|
+
if (!auto || !adapter.apply) {
|
|
526
|
+
await emit({
|
|
527
|
+
type: "improvement-applied",
|
|
528
|
+
runId: opts.runId,
|
|
529
|
+
appliedCount: 0,
|
|
530
|
+
withheldForReview: batch.edits.length
|
|
531
|
+
});
|
|
532
|
+
return {
|
|
533
|
+
edits: batch.edits,
|
|
534
|
+
applied: [],
|
|
535
|
+
skipped: batch.skipped,
|
|
536
|
+
errors: batch.errors,
|
|
537
|
+
withheld_for_review: batch.edits.length
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
const findingsById = new Map(findings.map((f) => [f.finding_id, f]));
|
|
541
|
+
const safe = [];
|
|
542
|
+
let withheld = 0;
|
|
543
|
+
for (const e of batch.edits) {
|
|
544
|
+
const src = e.sourceFindingId ? findingsById.get(e.sourceFindingId) : void 0;
|
|
545
|
+
if (!src || src.confidence < threshold) {
|
|
546
|
+
withheld += 1;
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
safe.push(e);
|
|
550
|
+
}
|
|
551
|
+
const result = await adapter.apply(safe);
|
|
552
|
+
log("improvement.apply", {
|
|
553
|
+
applied: result.applied.length,
|
|
554
|
+
withheld_for_review: withheld,
|
|
555
|
+
warnings: result.warnings.length
|
|
556
|
+
});
|
|
557
|
+
await emit({
|
|
558
|
+
type: "improvement-applied",
|
|
559
|
+
runId: opts.runId,
|
|
560
|
+
appliedCount: result.applied.length,
|
|
561
|
+
withheldForReview: withheld
|
|
562
|
+
});
|
|
563
|
+
return {
|
|
564
|
+
edits: batch.edits,
|
|
565
|
+
applied: result.applied,
|
|
566
|
+
skipped: batch.skipped,
|
|
567
|
+
errors: batch.errors,
|
|
568
|
+
withheld_for_review: withheld
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
function defaultLog(msg, fields) {
|
|
572
|
+
if (fields) console.log(`[analyst-loop] ${msg}`, fields);
|
|
573
|
+
else console.log(`[analyst-loop] ${msg}`);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export {
|
|
577
|
+
iterationsToTraceStore,
|
|
578
|
+
runAnalystLoop
|
|
579
|
+
};
|
|
580
|
+
//# sourceMappingURL=chunk-P5OKDSLB.js.map
|