@tangle-network/agent-runtime 0.104.0 → 0.106.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/README.md +176 -42
  2. package/dist/activation-CRjVfOiy.js +203 -0
  3. package/dist/activation-CRjVfOiy.js.map +1 -0
  4. package/dist/activation-DyWB0K6E.d.ts +98 -0
  5. package/dist/agent.d.ts +216 -234
  6. package/dist/agent.js +615 -547
  7. package/dist/agent.js.map +1 -1
  8. package/dist/analyst-loop-DGdtVENg.js +517 -0
  9. package/dist/analyst-loop-DGdtVENg.js.map +1 -0
  10. package/dist/analyst-loop.d.ts +10 -43
  11. package/dist/analyst-loop.js +2 -11
  12. package/dist/backends-Dy8NujB4.js +713 -0
  13. package/dist/backends-Dy8NujB4.js.map +1 -0
  14. package/dist/candidate-execution/index.d.ts +4 -192
  15. package/dist/candidate-execution/index.js +5 -66
  16. package/dist/candidate-execution-DqMbv1rU.js +488 -0
  17. package/dist/candidate-execution-DqMbv1rU.js.map +1 -0
  18. package/dist/coder-yhVWbdWc.js +40 -0
  19. package/dist/coder-yhVWbdWc.js.map +1 -0
  20. package/dist/conversation-XucG_K3l.js +1265 -0
  21. package/dist/conversation-XucG_K3l.js.map +1 -0
  22. package/dist/conversation.d.ts +2 -804
  23. package/dist/conversation.js +2 -55
  24. package/dist/environment-provider-CfLUmKmf.js +1168 -0
  25. package/dist/environment-provider-CfLUmKmf.js.map +1 -0
  26. package/dist/environment-provider-IUGU3epE.d.ts +964 -0
  27. package/dist/environment-provider.d.ts +2 -78
  28. package/dist/environment-provider.js +2 -18
  29. package/dist/errors-DEAvWQPy.js +111 -0
  30. package/dist/errors-DEAvWQPy.js.map +1 -0
  31. package/dist/improvement-adapter-Cr5TBSji.d.ts +166 -0
  32. package/dist/improvement-cycle-C095kS_g.js +2624 -0
  33. package/dist/improvement-cycle-C095kS_g.js.map +1 -0
  34. package/dist/index-BGdxAwfO.d.ts +838 -0
  35. package/dist/index-C-FYUuFG.d.ts +193 -0
  36. package/dist/index-CbNM40xG.d.ts +140 -0
  37. package/dist/index-DruSKs3Z.d.ts +7224 -0
  38. package/dist/index-Efjb3nrQ.d.ts +671 -0
  39. package/dist/index.d.ts +14 -1134
  40. package/dist/index.js +2134 -1972
  41. package/dist/index.js.map +1 -1
  42. package/dist/intelligence.d.ts +776 -888
  43. package/dist/intelligence.js +1466 -1410
  44. package/dist/intelligence.js.map +1 -1
  45. package/dist/{kb-gate-CwHO0vz6.d.ts → kb-gate-C8z2juK8.d.ts} +33 -31
  46. package/dist/kb-gate-DpaSwXVx.js +80 -0
  47. package/dist/kb-gate-DpaSwXVx.js.map +1 -0
  48. package/dist/knowledge-Ku-mPP4f.js +433 -0
  49. package/dist/knowledge-Ku-mPP4f.js.map +1 -0
  50. package/dist/knowledge.d.ts +2 -150
  51. package/dist/knowledge.js +2 -40
  52. package/dist/local-harness-Dh8PJ0ot.d.ts +455 -0
  53. package/dist/loop-runner-bin-BXsFEUcf.d.ts +162 -0
  54. package/dist/loop-runner-bin-BwjQ1NfH.js +268 -0
  55. package/dist/loop-runner-bin-BwjQ1NfH.js.map +1 -0
  56. package/dist/loop-runner-bin.d.ts +2 -17
  57. package/dist/loop-runner-bin.js +2 -24
  58. package/dist/loops.d.ts +5 -4164
  59. package/dist/loops.js +8 -419
  60. package/dist/mcp/bin.d.ts +1 -1
  61. package/dist/mcp/bin.js +116 -146
  62. package/dist/mcp/bin.js.map +1 -1
  63. package/dist/mcp/index.d.ts +295 -437
  64. package/dist/mcp/index.js +1242 -1177
  65. package/dist/mcp/index.js.map +1 -1
  66. package/dist/mcp/memory-bin.d.ts +1 -1
  67. package/dist/mcp/memory-bin.js +37 -16
  68. package/dist/mcp/memory-bin.js.map +1 -1
  69. package/dist/memory-server-DL6cE2Ag.js +234 -0
  70. package/dist/memory-server-DL6cE2Ag.js.map +1 -0
  71. package/dist/model-resolution-Btd9iIKV.js +98 -0
  72. package/dist/model-resolution-Btd9iIKV.js.map +1 -0
  73. package/dist/openai-tools-B68JaOCx.d.ts +24 -0
  74. package/dist/openai-tools-CKLy1C7M.js +44 -0
  75. package/dist/openai-tools-CKLy1C7M.js.map +1 -0
  76. package/dist/otel-export-D34IQW1C.js +843 -0
  77. package/dist/otel-export-D34IQW1C.js.map +1 -0
  78. package/dist/platform.d.ts +188 -185
  79. package/dist/platform.js +185 -218
  80. package/dist/platform.js.map +1 -1
  81. package/dist/prepare-B53sPV7M.js +4292 -0
  82. package/dist/prepare-B53sPV7M.js.map +1 -0
  83. package/dist/primeintellect/index.d.ts +150 -147
  84. package/dist/primeintellect/index.js +626 -1004
  85. package/dist/primeintellect/index.js.map +1 -1
  86. package/dist/profiles.d.ts +296 -490
  87. package/dist/profiles.js +722 -749
  88. package/dist/profiles.js.map +1 -1
  89. package/dist/protected-model-port-BLPH5VVv.js +722 -0
  90. package/dist/protected-model-port-BLPH5VVv.js.map +1 -0
  91. package/dist/protected-model-port-BP6Z4eau.d.ts +283 -0
  92. package/dist/protected-redaction--F3v1oo8.js +200 -0
  93. package/dist/protected-redaction--F3v1oo8.js.map +1 -0
  94. package/dist/redact-D-u-rrcn.js +82 -0
  95. package/dist/redact-D-u-rrcn.js.map +1 -0
  96. package/dist/redact-kbIOjfjM.d.ts +452 -0
  97. package/dist/researcher-CoVqNhfI.js +433 -0
  98. package/dist/researcher-CoVqNhfI.js.map +1 -0
  99. package/dist/runtime-BG0BziaP.js +5408 -0
  100. package/dist/runtime-BG0BziaP.js.map +1 -0
  101. package/dist/sandbox-events-DeI5xX8P.js +316 -0
  102. package/dist/sandbox-events-DeI5xX8P.js.map +1 -0
  103. package/dist/spawn-journal-CwPvKUTa.js +456 -0
  104. package/dist/spawn-journal-CwPvKUTa.js.map +1 -0
  105. package/dist/structural-rollout-IXUEplky.js +1445 -0
  106. package/dist/structural-rollout-IXUEplky.js.map +1 -0
  107. package/dist/substrate-B0TYNrXn.js +27 -0
  108. package/dist/substrate-B0TYNrXn.js.map +1 -0
  109. package/dist/{substrate-Dyiqmd3o.d.ts → substrate-BcnuSHXm.d.ts} +57 -54
  110. package/dist/supervise-BiRutHS9.js +7321 -0
  111. package/dist/supervise-BiRutHS9.js.map +1 -0
  112. package/dist/supervisor-DTKhF-RV.js +3826 -0
  113. package/dist/supervisor-DTKhF-RV.js.map +1 -0
  114. package/dist/testing.d.ts +4 -3
  115. package/dist/testing.js +2478 -2637
  116. package/dist/testing.js.map +1 -1
  117. package/dist/tool-server-RcWgLIsL.js +141 -0
  118. package/dist/tool-server-RcWgLIsL.js.map +1 -0
  119. package/dist/trace-propagation-CJJC7SVB.js +57 -0
  120. package/dist/trace-propagation-CJJC7SVB.js.map +1 -0
  121. package/dist/types-BaIpb0XT.d.ts +492 -0
  122. package/dist/types-BevOjfTY.d.ts +765 -0
  123. package/dist/types-C9j4qg6l.d.ts +500 -0
  124. package/dist/types-Cja5OTGh.d.ts +190 -0
  125. package/dist/util-MVgdwuIS.js +145 -0
  126. package/dist/util-MVgdwuIS.js.map +1 -0
  127. package/dist/workspace-archive-BosQiotA.js +575 -0
  128. package/dist/workspace-archive-BosQiotA.js.map +1 -0
  129. package/package.json +43 -55
  130. package/skills/build-with-agent-runtime/SKILL.md +16 -9
  131. package/skills/loop-writer/SKILL.md +1 -1
  132. package/dist/activation-Cm-9MMX1.d.ts +0 -69
  133. package/dist/analyst-loop.js.map +0 -1
  134. package/dist/backends-BO4Jqoe2.d.ts +0 -134
  135. package/dist/candidate-execution/index.js.map +0 -1
  136. package/dist/chunk-2KGAN2HM.js +0 -1256
  137. package/dist/chunk-2KGAN2HM.js.map +0 -1
  138. package/dist/chunk-2QJSNOGK.js +0 -48
  139. package/dist/chunk-2QJSNOGK.js.map +0 -1
  140. package/dist/chunk-2UABGGUB.js +0 -474
  141. package/dist/chunk-2UABGGUB.js.map +0 -1
  142. package/dist/chunk-2VZYRB55.js +0 -834
  143. package/dist/chunk-2VZYRB55.js.map +0 -1
  144. package/dist/chunk-2X65HYMI.js +0 -3526
  145. package/dist/chunk-2X65HYMI.js.map +0 -1
  146. package/dist/chunk-3MDZX7YU.js +0 -125
  147. package/dist/chunk-3MDZX7YU.js.map +0 -1
  148. package/dist/chunk-3PPVCS75.js +0 -199
  149. package/dist/chunk-3PPVCS75.js.map +0 -1
  150. package/dist/chunk-5HNDYWUT.js +0 -6424
  151. package/dist/chunk-5HNDYWUT.js.map +0 -1
  152. package/dist/chunk-5IBQAPVB.js +0 -29
  153. package/dist/chunk-5IBQAPVB.js.map +0 -1
  154. package/dist/chunk-5QOB7H74.js +0 -387
  155. package/dist/chunk-5QOB7H74.js.map +0 -1
  156. package/dist/chunk-ATDFUJYI.js +0 -4728
  157. package/dist/chunk-ATDFUJYI.js.map +0 -1
  158. package/dist/chunk-B7K7V22Y.js +0 -699
  159. package/dist/chunk-B7K7V22Y.js.map +0 -1
  160. package/dist/chunk-DPEUKJRO.js +0 -27
  161. package/dist/chunk-DPEUKJRO.js.map +0 -1
  162. package/dist/chunk-FD2MBMOH.js +0 -494
  163. package/dist/chunk-FD2MBMOH.js.map +0 -1
  164. package/dist/chunk-FJI3EXU3.js +0 -2402
  165. package/dist/chunk-FJI3EXU3.js.map +0 -1
  166. package/dist/chunk-HAO4CXHP.js +0 -200
  167. package/dist/chunk-HAO4CXHP.js.map +0 -1
  168. package/dist/chunk-ISPWRSEC.js +0 -408
  169. package/dist/chunk-ISPWRSEC.js.map +0 -1
  170. package/dist/chunk-KRBFHMV6.js +0 -1228
  171. package/dist/chunk-KRBFHMV6.js.map +0 -1
  172. package/dist/chunk-L5DST3QC.js +0 -351
  173. package/dist/chunk-L5DST3QC.js.map +0 -1
  174. package/dist/chunk-M22PL4GH.js +0 -171
  175. package/dist/chunk-M22PL4GH.js.map +0 -1
  176. package/dist/chunk-NURIQBJQ.js +0 -72
  177. package/dist/chunk-NURIQBJQ.js.map +0 -1
  178. package/dist/chunk-PZZKQVQV.js +0 -52
  179. package/dist/chunk-PZZKQVQV.js.map +0 -1
  180. package/dist/chunk-Q2JSAVQ3.js +0 -671
  181. package/dist/chunk-Q2JSAVQ3.js.map +0 -1
  182. package/dist/chunk-SBTWKPVR.js +0 -5576
  183. package/dist/chunk-SBTWKPVR.js.map +0 -1
  184. package/dist/chunk-SGKPNBXE.js +0 -60
  185. package/dist/chunk-SGKPNBXE.js.map +0 -1
  186. package/dist/chunk-SNSMRT6H.js +0 -680
  187. package/dist/chunk-SNSMRT6H.js.map +0 -1
  188. package/dist/chunk-STJEHM5F.js +0 -1338
  189. package/dist/chunk-STJEHM5F.js.map +0 -1
  190. package/dist/chunk-UPWGXKXB.js +0 -254
  191. package/dist/chunk-UPWGXKXB.js.map +0 -1
  192. package/dist/chunk-YEJR7IXO.js +0 -68
  193. package/dist/chunk-YEJR7IXO.js.map +0 -1
  194. package/dist/chunk-ZXICDSAK.js +0 -907
  195. package/dist/chunk-ZXICDSAK.js.map +0 -1
  196. package/dist/completion-gate-Bv47cOnn.d.ts +0 -256
  197. package/dist/conversation.js.map +0 -1
  198. package/dist/coordination-CxtzMxSM.d.ts +0 -2570
  199. package/dist/environment-provider.js.map +0 -1
  200. package/dist/improve--2A0gK1-.d.ts +0 -440
  201. package/dist/improvement-adapter-HAZz-7vK.d.ts +0 -203
  202. package/dist/knowledge.js.map +0 -1
  203. package/dist/local-harness-CtK9dSny.d.ts +0 -175
  204. package/dist/loop-runner-bin-DUOGtvOl.d.ts +0 -207
  205. package/dist/loop-runner-bin.js.map +0 -1
  206. package/dist/loops.js.map +0 -1
  207. package/dist/openai-tools-fnj6SRVg.d.ts +0 -46
  208. package/dist/protected-model-port-DzkUY3HQ.d.ts +0 -271
  209. package/dist/sanitize-DEbPNtyI.d.ts +0 -328
  210. package/dist/structural-rollout-C4mabf87.d.ts +0 -446
  211. package/dist/supervise-7q9F5brx.d.ts +0 -379
  212. package/dist/types-BCoemcXU.d.ts +0 -802
  213. package/dist/types-BwoZWq-i.d.ts +0 -510
  214. package/dist/types-Cbp72qyu.d.ts +0 -491
  215. package/dist/types-CmYCMbFT.d.ts +0 -203
  216. package/dist/types-CvNwMzQt.d.ts +0 -957
  217. package/dist/worktree-fanout-WsOzt8qu.d.ts +0 -999
@@ -1,2402 +0,0 @@
1
- import {
2
- agentCandidateProfileAsAgentProfile,
3
- assertCandidateProfileBinding,
4
- candidateMaterializerHarness,
5
- canonicalCandidateBytes,
6
- canonicalCandidateDigest,
7
- canonicalCandidateDocument,
8
- createAgentCandidateProfileActivation,
9
- executePreparedAgentCandidate,
10
- immutableCandidateValue,
11
- omitTopLevelDigest,
12
- omitUndefinedObjectFields,
13
- parseAgentCandidateProfileActivation,
14
- parseExactAgentProfile,
15
- prepareAgentCandidateExecution,
16
- verifiedResourceTextByDigest,
17
- verifyAgentCandidateBundle,
18
- verifyCanonicalCandidateDocument
19
- } from "./chunk-SBTWKPVR.js";
20
- import {
21
- defaultStructuralRolloutPolicy,
22
- optimizerMethod
23
- } from "./chunk-STJEHM5F.js";
24
- import {
25
- assertModelAllowed,
26
- harnessInvocation,
27
- runLocalHarness,
28
- runSettledCommand
29
- } from "./chunk-2X65HYMI.js";
30
- import {
31
- runAnalystLoop
32
- } from "./chunk-FD2MBMOH.js";
33
- import {
34
- ConfigError
35
- } from "./chunk-YEJR7IXO.js";
36
-
37
- // src/improvement/agentic-generator.ts
38
- import { spawnSync } from "child_process";
39
- import { createHash } from "crypto";
40
- import { existsSync, readFileSync, rmSync } from "fs";
41
- import { join, resolve, sep } from "path";
42
- import {
43
- applyWorkspacePlan,
44
- materializeProfile
45
- } from "@tangle-network/agent-profile-materialize";
46
- var RAW_TRACE_ANALYST_ID = "raw-trace-distiller";
47
- var RAW_TRACE_AREA = "raw-trace-context";
48
- var RAW_TRACE_DIAGNOSIS_PATH = ".improve/raw-trace-diagnosis.md";
49
- var AGENTIC_PROFILE_RESOURCE_ROOT = ".agent-runtime-profile-resources";
50
- function agenticGenerator(opts = {}) {
51
- const harness = opts.harness ?? "claude";
52
- if (opts.codexReproducible && harness !== "codex") {
53
- throw new Error("agenticGenerator: codexReproducible requires harness 'codex'");
54
- }
55
- if (opts.codexReproducible && !opts.profile) {
56
- throw new Error("agenticGenerator: codexReproducible requires an explicit author profile");
57
- }
58
- if (opts.codexReadDeniedPaths && !opts.codexReproducible) {
59
- throw new Error("agenticGenerator: codexReadDeniedPaths requires codexReproducible");
60
- }
61
- if (opts.maximumCharge && !opts.codexReproducible) {
62
- throw new Error("agenticGenerator: maximumCharge requires codexReproducible");
63
- }
64
- const profileResourcePlan = opts.codexReproducible ? authorProfileResourcePlan(opts.profile) : null;
65
- const buildPrompt = opts.buildPrompt ?? defaultBuildPrompt;
66
- const run = opts.runHarness ?? runLocalHarness;
67
- const dirty = opts.isDirty ?? worktreeDirty;
68
- const verify = opts.verify;
69
- return {
70
- kind: `agentic:${harness}`,
71
- // The seed repo + (in rawTraceContext mode) the raw-trace filesystem context
72
- // are the change signal — an agentic coder proposes from them even when the
73
- // distiller yielded zero findings. Without this, the improvementDriver's
74
- // empty-findings guard short-circuits and generates ZERO candidates on the
75
- // first (and, for a single-generation run, only) proposal round.
76
- proposesWithoutFindings: true,
77
- async generate({
78
- worktreePath,
79
- report,
80
- findings,
81
- maxShots,
82
- signal,
83
- generation,
84
- candidateIndex,
85
- costLedger,
86
- costPhase
87
- }) {
88
- signal.throwIfAborted();
89
- let reproducibleCostLedger;
90
- if (opts.codexReproducible) {
91
- if (!costLedger) {
92
- throw new Error(
93
- "agenticGenerator: reproducible Codex requires the run-wide CostLedger supplied by agent-eval"
94
- );
95
- }
96
- reproducibleCostLedger = costLedger;
97
- }
98
- const basePrompt = appendProfileResourcePaths(
99
- buildPrompt({ report, findings }),
100
- profileResourcePlan
101
- );
102
- const needsRawTraceEvidence = requiresRawTraceEvidence(findings);
103
- const shots = Math.max(1, maxShots);
104
- let attemptNote = "";
105
- for (let shot = 0; shot < shots; shot++) {
106
- signal.throwIfAborted();
107
- const taskPrompt = attemptNote ? `${basePrompt}
108
-
109
- ${attemptNote}` : basePrompt;
110
- const invocation = opts.profile ? harnessInvocation(harness, opts.profile, taskPrompt, {
111
- dangerouslySkipPermissions: harness === "claude",
112
- ...opts.codexReproducible ? { codexReproducible: true } : {}
113
- }) : void 0;
114
- const exactPrompt = invocation?.prompt ?? taskPrompt;
115
- const readDeniedPaths = typeof opts.codexReadDeniedPaths === "function" ? opts.codexReadDeniedPaths(worktreePath) : opts.codexReadDeniedPaths;
116
- const startedAt = /* @__PURE__ */ new Date();
117
- let harnessResult = null;
118
- let profileWorkspaceReceipt = null;
119
- let costReceipt = null;
120
- let costCallId = null;
121
- let shotError = null;
122
- try {
123
- const execute = async (executionSignal) => {
124
- harnessResult = await withAuthorProfileResources(
125
- profileResourcePlan,
126
- worktreePath,
127
- async (receipt2) => {
128
- profileWorkspaceReceipt = receipt2;
129
- const result2 = await run({
130
- harness,
131
- cwd: worktreePath,
132
- taskPrompt,
133
- ...invocation ? { invocation: { command: invocation.command, args: invocation.args } } : {},
134
- // The candidate worktree is isolated and must be editable without an
135
- // interactive permission prompt. Other runLocalHarness callers remain
136
- // permission-safe by default.
137
- dangerouslySkipPermissions: harness === "claude",
138
- ...opts.codexReproducible ? { codexReproducible: true } : {},
139
- ...readDeniedPaths ? { codexReadDeniedPaths: readDeniedPaths } : {},
140
- timeoutMs: opts.timeoutMs,
141
- signal: executionSignal
142
- });
143
- harnessResult = result2;
144
- return result2;
145
- }
146
- );
147
- const failure = shotFailure(harnessResult, exactPrompt, opts.codexReproducible === true);
148
- if (failure) throw failure;
149
- return harnessResult;
150
- };
151
- if (reproducibleCostLedger) {
152
- const model = opts.profile?.model?.default;
153
- if (!model) {
154
- throw new Error("agenticGenerator: reproducible Codex requires profile.model.default");
155
- }
156
- const paid = await reproducibleCostLedger.runPaidCall({
157
- channel: "driver",
158
- phase: costPhase ?? "search.proposal",
159
- actor: `agentic-generator:${harness}`,
160
- model,
161
- tags: {
162
- generation: String(generation ?? -1),
163
- candidateIndex: String(candidateIndex ?? -1),
164
- shot: String(shot + 1)
165
- },
166
- signal,
167
- ...opts.maximumCharge ? { maximumCharge: opts.maximumCharge } : {},
168
- execute,
169
- receipt: (result2) => costReceiptFromHarness(result2, model),
170
- receiptFromError: () => harnessResult?.usage ? costReceiptFromHarness(harnessResult, model) : void 0
171
- });
172
- costCallId = paid.callId ?? null;
173
- costReceipt = paid.receipt ?? null;
174
- if (!paid.succeeded) throw paid.error;
175
- harnessResult = paid.value;
176
- } else {
177
- harnessResult = await execute(signal);
178
- }
179
- } catch (cause) {
180
- shotError = cause instanceof Error ? cause : new Error(String(cause));
181
- }
182
- const execution = shotExecutionSnapshot(harnessResult);
183
- const receipt = shotReceipt({
184
- generation,
185
- candidateIndex,
186
- shot,
187
- maxShots: shots,
188
- harness,
189
- profile: opts.profile,
190
- prompt: exactPrompt,
191
- startedAt,
192
- completedAt: /* @__PURE__ */ new Date(),
193
- result: execution,
194
- profileWorkspaceReceipt,
195
- costCallId,
196
- costReceipt,
197
- error: shotError
198
- });
199
- await emitShotReceipt(opts.onShotCompleted, receipt, execution, shotError);
200
- signal.throwIfAborted();
201
- if (!execution) {
202
- throw new Error("agenticGenerator: author shot completed without a harness result");
203
- }
204
- let worktreeChanged;
205
- try {
206
- worktreeChanged = dirty(worktreePath);
207
- } catch (cause) {
208
- signal.throwIfAborted();
209
- return rethrowShotSetupError(
210
- opts.onShotDisposition,
211
- receipt,
212
- worktreePath,
213
- "worktree-inspection",
214
- cause
215
- );
216
- }
217
- if (!worktreeChanged) {
218
- signal.throwIfAborted();
219
- await emitShotDisposition(opts.onShotDisposition, receipt, {
220
- kind: "clean",
221
- worktreePath
222
- });
223
- signal.throwIfAborted();
224
- attemptNote = EMPTY_TREE_NOTE;
225
- continue;
226
- }
227
- if (needsRawTraceEvidence) {
228
- let problem;
229
- try {
230
- problem = rawTraceEvidenceProblem(worktreePath, findings);
231
- } catch (cause) {
232
- signal.throwIfAborted();
233
- return rethrowShotSetupError(
234
- opts.onShotDisposition,
235
- receipt,
236
- worktreePath,
237
- "raw-trace-evidence",
238
- cause
239
- );
240
- }
241
- if (problem) {
242
- signal.throwIfAborted();
243
- await emitShotDisposition(opts.onShotDisposition, receipt, {
244
- kind: "rejected",
245
- worktreePath,
246
- stage: "raw-trace-evidence",
247
- feedback: problem
248
- });
249
- signal.throwIfAborted();
250
- attemptNote = problem;
251
- continue;
252
- }
253
- }
254
- if (!verify) {
255
- signal.throwIfAborted();
256
- await emitShotDisposition(opts.onShotDisposition, receipt, {
257
- kind: "accepted",
258
- worktreePath,
259
- verified: false
260
- });
261
- signal.throwIfAborted();
262
- return acceptedCandidate(findings);
263
- }
264
- let result;
265
- try {
266
- signal.throwIfAborted();
267
- result = await verify(worktreePath, signal);
268
- signal.throwIfAborted();
269
- } catch (cause) {
270
- signal.throwIfAborted();
271
- return rethrowShotSetupError(
272
- opts.onShotDisposition,
273
- receipt,
274
- worktreePath,
275
- "verification",
276
- cause
277
- );
278
- }
279
- if (result.ok) {
280
- signal.throwIfAborted();
281
- await emitShotDisposition(opts.onShotDisposition, receipt, {
282
- kind: "accepted",
283
- worktreePath,
284
- verified: true
285
- });
286
- signal.throwIfAborted();
287
- return acceptedCandidate(findings);
288
- }
289
- signal.throwIfAborted();
290
- await emitShotDisposition(opts.onShotDisposition, receipt, {
291
- kind: "rejected",
292
- worktreePath,
293
- stage: "verification",
294
- feedback: result.feedback ?? null
295
- });
296
- signal.throwIfAborted();
297
- attemptNote = failureNote(result.feedback);
298
- }
299
- return { applied: false, summary: "" };
300
- }
301
- };
302
- }
303
- function authorProfileResourcePlan(profile) {
304
- const resources = profile.resources;
305
- if (!resources) return null;
306
- const unsupportedKinds = [
307
- resources.tools?.length ? "tools" : null,
308
- resources.skills?.length ? "skills" : null,
309
- resources.agents?.length ? "agents" : null
310
- ].filter((kind) => kind !== null);
311
- if (unsupportedKinds.length > 0) {
312
- throw new Error(
313
- `agenticGenerator: reproducible Codex author resources support files only; unsupported: ${unsupportedKinds.join(", ")}`
314
- );
315
- }
316
- if (!resources.files || resources.files.length === 0) return null;
317
- const plan = materializeProfile(
318
- {
319
- name: profile.name,
320
- resources: { files: resources.files }
321
- },
322
- "codex"
323
- );
324
- if (plan.unsupported.length > 0) {
325
- throw new Error(
326
- `agenticGenerator: author profile files could not be materialized: ${plan.unsupported.map((item) => item.reason).join("; ")}`
327
- );
328
- }
329
- if (Object.keys(plan.env).length > 0 || plan.flags.length > 0) {
330
- throw new Error(
331
- "agenticGenerator: generic author profile files unexpectedly changed spawn values"
332
- );
333
- }
334
- const virtualRoot = resolve("/", AGENTIC_PROFILE_RESOURCE_ROOT);
335
- const seen = /* @__PURE__ */ new Set();
336
- for (const file of plan.files) {
337
- const target = resolve("/", file.relPath);
338
- if (!target.startsWith(`${virtualRoot}${sep}`)) {
339
- throw new Error(
340
- `agenticGenerator: author profile file must be below ${AGENTIC_PROFILE_RESOURCE_ROOT}: ${file.relPath}`
341
- );
342
- }
343
- if (seen.has(target)) {
344
- throw new Error(`agenticGenerator: duplicate author profile file path: ${file.relPath}`);
345
- }
346
- seen.add(target);
347
- }
348
- return plan;
349
- }
350
- function appendProfileResourcePaths(prompt, plan) {
351
- if (!plan) return prompt;
352
- return [
353
- prompt,
354
- "",
355
- "Profile resource files available for this shot:",
356
- ...plan.files.map((file) => `- ${file.relPath}`)
357
- ].join("\n");
358
- }
359
- async function withAuthorProfileResources(plan, worktreePath, run) {
360
- if (!plan) return run(null);
361
- const rootPath = resolve(worktreePath, AGENTIC_PROFILE_RESOURCE_ROOT);
362
- if (existsSync(rootPath)) {
363
- throw new Error(
364
- `agenticGenerator: ephemeral author profile root already exists: ${AGENTIC_PROFILE_RESOURCE_ROOT}`
365
- );
366
- }
367
- let value;
368
- let primaryError;
369
- try {
370
- const receipt = applyWorkspacePlan(plan, worktreePath);
371
- value = await run(receipt);
372
- } catch (cause) {
373
- primaryError = cause;
374
- }
375
- let cleanupError;
376
- try {
377
- rmSync(rootPath, { recursive: true, force: true });
378
- if (existsSync(rootPath)) {
379
- throw new Error(
380
- `agenticGenerator: ephemeral author profile root survived cleanup: ${AGENTIC_PROFILE_RESOURCE_ROOT}`
381
- );
382
- }
383
- } catch (cause) {
384
- cleanupError = cause;
385
- }
386
- if (primaryError !== void 0 && cleanupError !== void 0) {
387
- throw new AggregateError(
388
- [primaryError, cleanupError],
389
- "agenticGenerator: author shot and profile resource cleanup both failed"
390
- );
391
- }
392
- if (primaryError !== void 0) throw primaryError;
393
- if (cleanupError !== void 0) throw cleanupError;
394
- return value;
395
- }
396
- async function emitShotReceipt(callback, receipt, execution, primaryError) {
397
- try {
398
- await callback?.(receipt, execution);
399
- } catch (callbackError) {
400
- if (primaryError !== null) {
401
- throw new AggregateError(
402
- [primaryError, callbackError],
403
- "agenticGenerator: author shot failed and its receipt could not be persisted"
404
- );
405
- }
406
- throw callbackError;
407
- }
408
- if (primaryError !== null) throw primaryError;
409
- }
410
- async function emitShotDisposition(callback, receipt, disposition) {
411
- await callback?.(receipt, disposition);
412
- }
413
- async function rethrowShotSetupError(callback, receipt, worktreePath, stage, cause) {
414
- const error = cause instanceof Error ? cause : new Error(String(cause));
415
- try {
416
- await emitShotDisposition(callback, receipt, {
417
- kind: "setup-error",
418
- worktreePath,
419
- stage,
420
- error: { name: error.name, message: error.message }
421
- });
422
- } catch (callbackError) {
423
- throw new AggregateError(
424
- [cause, callbackError],
425
- "agenticGenerator: shot processing failed and its worktree disposition could not be persisted"
426
- );
427
- }
428
- throw cause;
429
- }
430
- function shotExecutionSnapshot(result) {
431
- if (result === null) return null;
432
- const usage = result.usage ? Object.freeze({ ...result.usage }) : void 0;
433
- const evidence = result.evidence ? Object.freeze({
434
- ...result.evidence,
435
- readDeniedPaths: Object.freeze([...result.evidence.readDeniedPaths]),
436
- policy: Object.freeze({ ...result.evidence.policy })
437
- }) : void 0;
438
- return Object.freeze({
439
- exitCode: result.exitCode,
440
- stdout: result.stdout,
441
- stderr: result.stderr,
442
- killedBySignal: result.killedBySignal,
443
- durationMs: result.durationMs,
444
- timedOut: result.timedOut,
445
- ...result.aborted !== void 0 ? { aborted: result.aborted } : {},
446
- ...usage ? { usage } : {},
447
- ...evidence ? { evidence } : {}
448
- });
449
- }
450
- function shotReceipt(input) {
451
- const error = input.error ? {
452
- name: input.error instanceof Error ? input.error.name : "Error",
453
- message: input.error instanceof Error ? input.error.message : String(input.error)
454
- } : null;
455
- const result = input.result;
456
- const costBasis = costBasisFor(input.costReceipt);
457
- return {
458
- generation: input.generation ?? null,
459
- candidateIndex: input.candidateIndex ?? null,
460
- shot: input.shot + 1,
461
- maxShots: input.maxShots,
462
- harness: input.harness,
463
- model: input.profile?.model?.default ?? null,
464
- reasoningEffort: input.profile?.model?.reasoningEffort ?? null,
465
- promptSha256: sha256(input.prompt),
466
- startedAt: input.startedAt.toISOString(),
467
- completedAt: input.completedAt.toISOString(),
468
- durationMs: result?.durationMs ?? input.completedAt.getTime() - input.startedAt.getTime(),
469
- exitCode: result?.exitCode ?? null,
470
- timedOut: result?.timedOut ?? false,
471
- aborted: result?.aborted ?? false,
472
- killedBySignal: result?.killedBySignal ?? null,
473
- stdoutBytes: result ? Buffer.byteLength(result.stdout) : null,
474
- stdoutSha256: result ? sha256(result.stdout) : null,
475
- stderrBytes: result ? Buffer.byteLength(result.stderr) : null,
476
- stderrSha256: result ? sha256(result.stderr) : null,
477
- usage: result?.usage ? { ...result.usage } : null,
478
- profileWorkspacePlanDigest: input.profileWorkspaceReceipt?.workspacePlanDigest ?? null,
479
- profileWorkspaceFileCount: input.profileWorkspaceReceipt?.written.length ?? 0,
480
- costCallId: input.costCallId,
481
- costBasis,
482
- costUsd: costBasis === "unknown" ? null : input.costReceipt?.costUsd ?? null,
483
- costUsdKnown: costBasis === "provider-reported",
484
- evidence: result?.evidence ? {
485
- ...result.evidence,
486
- readDeniedPaths: [...result.evidence.readDeniedPaths],
487
- policy: { ...result.evidence.policy }
488
- } : null,
489
- error
490
- };
491
- }
492
- function costBasisFor(receipt) {
493
- if (receipt === null || receipt.costUnknown) return "unknown";
494
- return receipt.actualCostUsd === void 0 ? "estimated-pricing" : "provider-reported";
495
- }
496
- function shotFailure(result, exactPrompt, codexReproducible) {
497
- if (result.timedOut) {
498
- return new Error("agenticGenerator: author shot timed out");
499
- }
500
- if (result.aborted) {
501
- return new Error("agenticGenerator: author shot was cancelled by the caller");
502
- }
503
- if (result.killedBySignal) {
504
- return new Error(`agenticGenerator: author shot was killed by ${result.killedBySignal}`);
505
- }
506
- if (result.exitCode !== 0) {
507
- return new Error(`agenticGenerator: author shot exited with code ${String(result.exitCode)}`);
508
- }
509
- if (!codexReproducible) return null;
510
- if (!result.usage || !result.evidence) {
511
- return new Error(
512
- "agenticGenerator: reproducible Codex shot completed without usage or execution evidence"
513
- );
514
- }
515
- const expectedPromptSha256 = sha256(exactPrompt).slice("sha256:".length);
516
- if (result.evidence.requestedPromptSha256 !== expectedPromptSha256) {
517
- return new Error(
518
- "agenticGenerator: reproducible Codex prompt evidence does not match the exact authored prompt"
519
- );
520
- }
521
- return null;
522
- }
523
- function costReceiptFromHarness(result, model) {
524
- if (!result.usage) {
525
- throw new Error("agenticGenerator: author shot did not report terminal token usage");
526
- }
527
- return {
528
- model,
529
- inputTokens: result.usage.inputTokens - result.usage.cachedInputTokens,
530
- outputTokens: result.usage.outputTokens,
531
- ...result.usage.cachedInputTokens > 0 ? { cachedTokens: result.usage.cachedInputTokens } : {}
532
- };
533
- }
534
- function sha256(value) {
535
- return `sha256:${createHash("sha256").update(value).digest("hex")}`;
536
- }
537
- function defaultBuildPrompt(args) {
538
- const lines = [
539
- "You are improving this codebase based on an evaluation analysis: real runs failed, an",
540
- "analyst distilled the findings below, and your change will be measured on held-out tasks",
541
- "against the unchanged baseline \u2014 only a real lift promotes it.",
542
- "",
543
- optimizerMethod,
544
- "",
545
- "THE SURFACE \u2014 what a deliverable change looks like here:",
546
- "- edit this codebase in place: the smallest coherent change set that fully tests your",
547
- " hypothesis about the dominant failure mode (see the method above \u2014 no unrelated edits,",
548
- " they confound the measurement),",
549
- "- keep the diff reviewable: a reviewer should be able to trace every hunk back to a finding,",
550
- "- do not commit \u2014 leave changes in the working tree.",
551
- "",
552
- "FINDINGS \u2014 ranked evidence from real failed runs:"
553
- ];
554
- for (const f of args.findings) {
555
- const where = f.subject ? ` [${f.subject}]` : "";
556
- lines.push(`- (${f.severity})${where} ${f.claim}`);
557
- if (f.recommended_action) lines.push(` \u2192 ${f.recommended_action}`);
558
- }
559
- if (requiresRawTraceEvidence(args.findings)) {
560
- lines.push(
561
- "",
562
- "Raw trace evidence requirement:",
563
- `- Inspect at least one raw trace path named above before editing.`,
564
- `- Write ${RAW_TRACE_DIAGNOSIS_PATH} in this worktree.`,
565
- "- Include the exact trace path(s) inspected, the failure mechanism, and the code change made.",
566
- "- A candidate without this file, or with only this file changed, is discarded."
567
- );
568
- }
569
- return lines.join("\n");
570
- }
571
- var EMPTY_TREE_NOTE = "NOTE: your previous attempt left the working tree unchanged. Make the concrete file edits now.";
572
- function failureNote(feedback) {
573
- const detail = feedback?.trim();
574
- return [
575
- "NOTE: your edits are in the working tree but verification FAILED.",
576
- "Fix the problem in place \u2014 build on your existing edits, do not revert them.",
577
- detail ? `Verifier output:
578
- ${truncate(detail, 4e3)}` : "No verifier detail was captured."
579
- ].join("\n");
580
- }
581
- function rawTraceEvidenceProblem(worktreePath, findings) {
582
- const changedPaths = worktreeChangedPaths(worktreePath);
583
- const substantive = changedPaths.filter((path) => path !== RAW_TRACE_DIAGNOSIS_PATH);
584
- if (substantive.length === 0) {
585
- return [
586
- `NOTE: raw-trace mode requires a real code/config edit in addition to ${RAW_TRACE_DIAGNOSIS_PATH}.`,
587
- "Your previous attempt only changed the diagnosis artifact. Inspect the cited traces and make the causal code change."
588
- ].join("\n");
589
- }
590
- const diagnosisPath = join(worktreePath, RAW_TRACE_DIAGNOSIS_PATH);
591
- if (!existsSync(diagnosisPath)) {
592
- return [
593
- `NOTE: raw-trace mode requires ${RAW_TRACE_DIAGNOSIS_PATH}.`,
594
- "Before retrying, inspect at least one cited spans.jsonl/cached-result.json/artifact path, then write the diagnosis file with the exact path, failure mechanism, and code change."
595
- ].join("\n");
596
- }
597
- const body = readFileSync(diagnosisPath, "utf8");
598
- const evidencePaths = traceEvidencePaths(findings);
599
- if (evidencePaths.length > 0 && !evidencePaths.some((path) => body.includes(path))) {
600
- return [
601
- `${RAW_TRACE_DIAGNOSIS_PATH} exists, but it does not cite any exact raw trace path from the findings.`,
602
- `Cite at least one of these inspected paths exactly: ${evidencePaths.slice(0, 5).join(", ")}`
603
- ].join("\n");
604
- }
605
- return null;
606
- }
607
- function requiresRawTraceEvidence(findings) {
608
- return findings.some((finding) => {
609
- const f = finding;
610
- return f.analyst_id === RAW_TRACE_ANALYST_ID || f.area === RAW_TRACE_AREA;
611
- });
612
- }
613
- function traceEvidencePaths(findings) {
614
- const out = [];
615
- for (const finding of findings) {
616
- const refs = finding.evidence_refs;
617
- if (!Array.isArray(refs)) continue;
618
- for (const ref of refs) {
619
- if (!ref || typeof ref !== "object") continue;
620
- const uri = ref.uri;
621
- if (typeof uri === "string" && uri.length > 0) out.push(uri);
622
- }
623
- }
624
- return [...new Set(out)];
625
- }
626
- function commandVerifier(command, args = [], timeoutMs = 3e5) {
627
- return async (worktreePath, signal) => {
628
- let result;
629
- try {
630
- result = await runSettledCommand({
631
- command,
632
- args,
633
- cwd: worktreePath,
634
- timeoutMs,
635
- ...signal ? { signal } : {}
636
- });
637
- } catch (err) {
638
- signal?.throwIfAborted();
639
- const code = err.code;
640
- if (code === "ENOENT") {
641
- throw new Error(
642
- `commandVerifier: '${command}' not found in PATH (setup bug, not a failed candidate)`
643
- );
644
- }
645
- const reason = err instanceof Error ? err.message : String(err);
646
- throw new Error(`commandVerifier: '${command}' failed to spawn: ${reason}`);
647
- }
648
- if (result.timedOut || result.killedBySignal) {
649
- return {
650
- ok: false,
651
- feedback: `verifier '${command}' ${result.killedBySignal ? `killed by ${result.killedBySignal}` : "timed out"} after ${timeoutMs}ms`
652
- };
653
- }
654
- if (result.exitCode === 0) return { ok: true };
655
- const out = `${result.stdout}${result.stderr}`.trim();
656
- return { ok: false, feedback: out.length > 0 ? out : `exit ${result.exitCode}` };
657
- };
658
- }
659
- function summarizeFindings(findings) {
660
- if (findings.length === 0) return "agentic improvement";
661
- if (findings.length === 1) return `agentic: ${truncate(findings[0].claim, 64)}`;
662
- return `agentic: ${findings.length} findings addressed`;
663
- }
664
- function acceptedCandidate(findings) {
665
- const summary = summarizeFindings(findings);
666
- return {
667
- applied: true,
668
- summary,
669
- label: slugify(summary.split("\n", 1)[0] ?? summary),
670
- rationale: boundedRationale(summary, findings)
671
- };
672
- }
673
- function slugify(line) {
674
- const slug = line.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
675
- return truncate(slug || "agentic-improvement", 48);
676
- }
677
- function boundedRationale(summary, findings) {
678
- if (findings.length === 0) return summary;
679
- return truncate(
680
- findings.map((finding) => `(${finding.severity}) ${finding.claim}`).join("; "),
681
- 400
682
- );
683
- }
684
- function truncate(s, n) {
685
- return s.length <= n ? s : `${s.slice(0, n - 1)}\u2026`;
686
- }
687
- function worktreeDirty(worktreePath) {
688
- return worktreeChangedPaths(worktreePath).length > 0;
689
- }
690
- function worktreeChangedPaths(worktreePath) {
691
- const result = spawnSync("git", ["status", "--porcelain", "--untracked-files=all"], {
692
- cwd: worktreePath,
693
- encoding: "utf-8"
694
- });
695
- if (result.error) {
696
- throw new Error(
697
- `agenticGenerator: git status failed to spawn in ${worktreePath}: ${result.error.message}`
698
- );
699
- }
700
- if (result.status !== 0) {
701
- throw new Error(
702
- `agenticGenerator: git status exited ${result.status} in ${worktreePath}: ${result.stderr.trim()}`
703
- );
704
- }
705
- return result.stdout.split("\n").map((line) => line.trim()).filter((line) => line.length > 0).map((line) => line.slice(3).trim());
706
- }
707
-
708
- // src/improvement/findings.ts
709
- import { makeFinding } from "@tangle-network/agent-eval";
710
- var SEVERITIES = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
711
- var LIFTED_FINDING_ANALYST_ID = "lifted-seed";
712
- function isAnalystFinding(value) {
713
- if (!value || typeof value !== "object") return false;
714
- const o = value;
715
- return o.schema_version === "1.0.0" && typeof o.finding_id === "string" && typeof o.analyst_id === "string" && typeof o.severity === "string" && SEVERITIES.has(o.severity) && typeof o.area === "string" && typeof o.claim === "string" && typeof o.confidence === "number" && Array.isArray(o.evidence_refs);
716
- }
717
- function liftedClaim(value) {
718
- if (typeof value === "string") return value.trim() || null;
719
- if (value && typeof value === "object") {
720
- const o = value;
721
- for (const key of ["recommended_action", "claim", "lesson", "notes", "text", "message"]) {
722
- const v = o[key];
723
- if (typeof v === "string" && v.trim()) return v.trim();
724
- }
725
- try {
726
- const json = JSON.stringify(value);
727
- if (json && json !== "{}" && json !== "[]") {
728
- return json.length > 400 ? `${json.slice(0, 399)}\u2026` : json;
729
- }
730
- } catch {
731
- }
732
- }
733
- return null;
734
- }
735
- function toAnalystFindings(findings, opts = {}) {
736
- const analystId = opts.analystId ?? LIFTED_FINDING_ANALYST_ID;
737
- const area = opts.area ?? "seed";
738
- const out = [];
739
- for (const f of findings) {
740
- if (isAnalystFinding(f)) {
741
- out.push(f);
742
- continue;
743
- }
744
- const claim = liftedClaim(f);
745
- if (!claim) continue;
746
- out.push(
747
- makeFinding({
748
- analyst_id: analystId,
749
- severity: "info",
750
- area,
751
- confidence: 0.5,
752
- claim,
753
- evidence_refs: [],
754
- ...f && typeof f === "object" ? { metadata: { raw: f } } : {}
755
- })
756
- );
757
- }
758
- return out;
759
- }
760
-
761
- // src/improvement/improvement-driver.ts
762
- import { spawnSync as spawnSync2 } from "child_process";
763
- import {
764
- verifyCodeSurface
765
- } from "@tangle-network/agent-eval/campaign";
766
-
767
- // src/improvement/cleanup.ts
768
- async function rethrowAfterCleanup(cause, cleanup, context) {
769
- const cleanupErrors = [];
770
- for (let attempt = 0; attempt < 2; attempt += 1) {
771
- try {
772
- await cleanup();
773
- } catch (cleanupCause) {
774
- cleanupErrors.push(cleanupCause);
775
- continue;
776
- }
777
- if (cleanupErrors.length === 0) throw cause;
778
- throw new AggregateError([cause, ...cleanupErrors], `${context}; cleanup retry succeeded`);
779
- }
780
- throw new AggregateError([cause, ...cleanupErrors], `${context}; cleanup failed`);
781
- }
782
-
783
- // src/improvement/improvement-driver.ts
784
- function improvementDriver(opts) {
785
- const baseRef = opts.baseRef ?? "main";
786
- const owned = /* @__PURE__ */ new Map();
787
- return {
788
- kind: `improvement:${opts.generator.kind}`,
789
- async propose(ctx) {
790
- const findings = resolveFindings(ctx);
791
- if (findings.length === 0 && ctx.report === void 0 && !opts.generator.proposesWithoutFindings) {
792
- return [];
793
- }
794
- const surfaces = [];
795
- const incumbent = verifiedCodeIncumbent(ctx.currentSurface);
796
- const proposalBaseRef = incumbent?.baseCommit ?? baseRef;
797
- for (let i = 0; i < ctx.populationSize; i++) {
798
- if (ctx.signal.aborted) break;
799
- const wt = await opts.worktree.create({
800
- baseRef: proposalBaseRef,
801
- label: `${opts.generator.kind}-gen${ctx.generation}-cand${i}`
802
- });
803
- owned.set(wt.path, wt);
804
- try {
805
- if (incumbent) advanceToIncumbent(wt, incumbent);
806
- const { applied, summary, label, rationale } = await opts.generator.generate({
807
- worktreePath: wt.path,
808
- report: ctx.report,
809
- findings,
810
- dataset: ctx.dataset,
811
- maxShots: ctx.maxImprovementShots ?? 1,
812
- signal: ctx.signal,
813
- generation: ctx.generation,
814
- candidateIndex: i,
815
- ...ctx.costLedger ? { costLedger: ctx.costLedger } : {},
816
- ...ctx.costPhase ? { costPhase: ctx.costPhase } : {}
817
- });
818
- if (!applied) {
819
- await opts.worktree.discard(wt);
820
- owned.delete(wt.path);
821
- continue;
822
- }
823
- const surface = await opts.worktree.finalize(wt, summary);
824
- surfaces.push(
825
- label || rationale ? { surface, label: label ?? "", rationale: rationale ?? "" } : surface
826
- );
827
- owned.delete(wt.path);
828
- owned.set(surface.worktreeRef, wt);
829
- } catch (err) {
830
- const failure = err instanceof Error ? err.message : String(err);
831
- return rethrowAfterCleanup(
832
- err,
833
- async () => {
834
- await opts.worktree.discard(wt);
835
- owned.delete(wt.path);
836
- },
837
- `improvementDriver: ${failure}`
838
- );
839
- }
840
- }
841
- return surfaces;
842
- },
843
- async cleanup(retainWorktreeRefs = []) {
844
- const retained = new Set(retainWorktreeRefs);
845
- const errors = [];
846
- for (const [worktreeRef, worktree] of owned) {
847
- if (retained.has(worktreeRef)) continue;
848
- try {
849
- await opts.worktree.discard(worktree);
850
- owned.delete(worktreeRef);
851
- } catch (cause) {
852
- errors.push(cause);
853
- }
854
- }
855
- if (errors.length > 0) {
856
- throw new AggregateError(errors, "improvementDriver: failed to discard candidate worktrees");
857
- }
858
- }
859
- };
860
- }
861
- function verifiedCodeIncumbent(surface) {
862
- if (typeof surface !== "object" || surface.kind !== "code") return void 0;
863
- verifyCodeSurface(surface);
864
- return surface;
865
- }
866
- function advanceToIncumbent(worktree, incumbent) {
867
- if (worktree.baseCommit !== incumbent.baseCommit || worktree.baseTree !== incumbent.baseTree) {
868
- throw new Error("improvementDriver: candidate worktree does not match incumbent base identity");
869
- }
870
- if (worktree.baseCommit === incumbent.candidateCommit) return;
871
- const merge = spawnSync2("git", ["merge", "--ff-only", incumbent.candidateCommit], {
872
- cwd: worktree.path,
873
- encoding: "utf8"
874
- });
875
- if (merge.error) {
876
- throw new Error(
877
- `improvementDriver: failed to start candidate from incumbent: ${merge.error.message}`
878
- );
879
- }
880
- if (merge.status !== 0) {
881
- throw new Error(
882
- `improvementDriver: could not fast-forward candidate to incumbent ${incumbent.candidateCommit}: ${merge.stderr.trim()}`
883
- );
884
- }
885
- const head = spawnSync2("git", ["rev-parse", "--verify", "HEAD"], {
886
- cwd: worktree.path,
887
- encoding: "utf8"
888
- });
889
- if (head.error || head.status !== 0 || head.stdout.trim() !== incumbent.candidateCommit) {
890
- throw new Error("improvementDriver: candidate worktree did not reach the incumbent commit");
891
- }
892
- }
893
- function resolveFindings(ctx) {
894
- const report = ctx.report;
895
- if (report && typeof report === "object" && "findings" in report) {
896
- const f = report.findings;
897
- if (Array.isArray(f) && f.length > 0) {
898
- const lifted = toAnalystFindings(f, { analystId: "report-findings", area: "report" });
899
- if (lifted.length > 0) return lifted;
900
- }
901
- }
902
- return toAnalystFindings(ctx.findings ?? [], { analystId: "loop-context", area: "seed" });
903
- }
904
-
905
- // src/improvement/raw-trace-distiller.ts
906
- import { existsSync as existsSync2, readdirSync } from "fs";
907
- import { basename, join as join2, resolve as resolve2 } from "path";
908
- import { makeFinding as makeFinding2 } from "@tangle-network/agent-eval";
909
- var ANALYST_ID = "raw-trace-distiller";
910
- var PASS_THRESHOLD = 0.999;
911
- function rawTraceDistiller(options = {}) {
912
- const maxCandidates = options.maxCandidates ?? 12;
913
- const maxCellsPerCandidate = options.maxCellsPerCandidate ?? 8;
914
- const maxFilesPerCell = options.maxFilesPerCell ?? 24;
915
- return async (input) => {
916
- const genRoot = absoluteRunDir(options.runDir ?? input.runDir);
917
- const durable = isDurable(genRoot);
918
- const ranked = [...input.candidates].map((c) => ({
919
- surfaceHash: c.surfaceHash,
920
- composite: c.composite,
921
- campaignDir: absoluteRunDir(c.campaign.runDir),
922
- cells: failingCells(c.campaign, maxCellsPerCandidate, maxFilesPerCell)
923
- })).sort((a, b) => a.composite - b.composite).slice(0, maxCandidates);
924
- const totalFailingCells = ranked.reduce((n, c) => n + c.cells.length, 0);
925
- if (totalFailingCells === 0) {
926
- if (options.fallbackFindings && options.fallbackFindings.length > 0) {
927
- return options.fallbackFindings;
928
- }
929
- return [
930
- makeFinding2({
931
- analyst_id: ANALYST_ID,
932
- severity: "info",
933
- area: "raw-trace-context",
934
- confidence: 1,
935
- claim: `Generation ${input.generation} had no failing cells. The full raw run traces are on disk under ${genRoot}.`,
936
- recommended_action: `To keep improving, grep/cat the raw traces under ${genRoot} (per-cell spans.jsonl + cached-result.json) to find the weakest passing runs, then make a targeted harness-code edit.`,
937
- evidence_refs: [{ kind: "artifact", uri: genRoot }],
938
- metadata: { generation: input.generation, runDir: genRoot, failingCells: 0 }
939
- })
940
- ];
941
- }
942
- const findings = [];
943
- findings.push(
944
- makeFinding2({
945
- analyst_id: ANALYST_ID,
946
- severity: "high",
947
- area: "raw-trace-context",
948
- confidence: 1,
949
- claim: `Generation ${input.generation} produced ${totalFailingCells} failing/low-scoring cell(s) across ${ranked.length} candidate(s). Their FULL RAW run traces are on disk under ${genRoot} \u2014 the actual event logs (spans.jsonl), scores (cached-result.json), and artifacts, not a summary.${durable ? "" : " (WARNING: this run root does not exist on disk \u2014 it looks like an in-memory run; pass a real runDir to improve() to get raw-trace context.)"}`,
950
- recommended_action: `Do NOT rely on a pre-summarized finding. Before editing, DIAGNOSE from the raw traces: run \`grep\`/\`cat\`/\`ls\` over the trace files and directories named in the following findings to see exactly what each failing run did and why it scored low, then make the smallest harness-code edit that fixes the dominant failure. Start with \`grep -rIn "error" ${genRoot}\` then \`cat\` the spans.jsonl of the worst cell.`,
951
- evidence_refs: [{ kind: "artifact", uri: genRoot }],
952
- metadata: {
953
- generation: input.generation,
954
- runDir: genRoot,
955
- failingCells: totalFailingCells,
956
- candidates: ranked.length
957
- }
958
- })
959
- );
960
- for (const cand of ranked) {
961
- if (cand.cells.length === 0) continue;
962
- const scenarioList = cand.cells.map((c) => c.scenarioId).join(", ");
963
- const fileLines = cand.cells.map((c) => {
964
- const header = ` cell ${c.scenarioId} (composite ${c.composite.toFixed(3)}${c.error ? `, error: ${truncate2(c.error, 160)}` : ""}) \u2014 dir ${c.cellDir}`;
965
- const files = c.files.map((f) => ` - ${f}`).join("\n");
966
- const more = c.truncatedFiles ? `
967
- - \u2026(ls ${c.cellDir} for the rest)` : "";
968
- return c.files.length > 0 ? `${header}
969
- ${files}${more}` : header;
970
- }).join("\n");
971
- findings.push(
972
- makeFinding2({
973
- analyst_id: ANALYST_ID,
974
- severity: cand.composite < 0.5 ? "critical" : "high",
975
- area: "raw-trace-context",
976
- confidence: 1,
977
- subject: cand.surfaceHash,
978
- claim: `Candidate ${cand.surfaceHash} scored composite ${cand.composite.toFixed(3)} with ${cand.cells.length} failing cell(s) [${scenarioList}]. Its raw traces are under ${cand.campaignDir}.`,
979
- recommended_action: `grep/cat these raw trace files to diagnose WHY this candidate failed before editing:
980
- ${fileLines}
981
- Or scan the whole candidate at once: \`grep -rIn . ${cand.campaignDir}\` and \`ls -R ${cand.campaignDir}\`.`,
982
- evidence_refs: [
983
- { kind: "artifact", uri: cand.campaignDir },
984
- ...cand.cells.flatMap(
985
- (c) => c.files.map((f) => ({ kind: "artifact", uri: f }))
986
- )
987
- ],
988
- metadata: {
989
- surfaceHash: cand.surfaceHash,
990
- composite: cand.composite,
991
- campaignDir: cand.campaignDir,
992
- cells: cand.cells.map((c) => ({
993
- scenarioId: c.scenarioId,
994
- composite: c.composite,
995
- cellDir: c.cellDir,
996
- files: c.files,
997
- ...c.error ? { error: c.error } : {}
998
- }))
999
- }
1000
- })
1001
- );
1002
- }
1003
- return findings;
1004
- };
1005
- }
1006
- function failingCells(campaign, maxCells, maxFiles) {
1007
- const campaignDir = absoluteRunDir(campaign.runDir);
1008
- const durable = isDurable(campaignDir);
1009
- const out = [];
1010
- for (const cell of campaign.cells) {
1011
- const scores = Object.values(cell.judgeScores ?? {});
1012
- const composite = scores.length === 0 ? 0 : scores.reduce((sum, s) => sum + (s.composite ?? 0), 0) / scores.length;
1013
- if (!cell.error && composite >= PASS_THRESHOLD) continue;
1014
- const cellDir = join2(campaignDir, sanitizeCellId(cell.cellId));
1015
- const artifactPaths = artifactPathsForCell(campaign.artifactsByPath, cell.cellId);
1016
- const discovered = durable ? listTraceFiles(cellDir) : [];
1017
- const canonical = [join2(cellDir, "spans.jsonl"), join2(cellDir, "cached-result.json")];
1018
- const files = dedupeSorted([...discovered, ...artifactPaths, ...canonical]);
1019
- out.push({
1020
- scenarioId: cell.scenarioId,
1021
- composite: Number(composite.toFixed(3)),
1022
- ...cell.error ? { error: cell.error } : {},
1023
- cellDir,
1024
- files: files.slice(0, maxFiles),
1025
- truncatedFiles: files.length > maxFiles
1026
- });
1027
- if (out.length >= maxCells) break;
1028
- }
1029
- return out;
1030
- }
1031
- function artifactPathsForCell(artifactsByPath, cellId) {
1032
- if (!artifactsByPath) return [];
1033
- const prefix = `${cellId}/`;
1034
- return Object.entries(artifactsByPath).filter(([key]) => key.startsWith(prefix)).map(([, absPath]) => resolve2(absPath));
1035
- }
1036
- function listTraceFiles(dir) {
1037
- const out = [];
1038
- for (const entry of safeReadDir(dir)) {
1039
- const full = join2(dir, entry.name);
1040
- if (entry.isFile()) {
1041
- out.push(full);
1042
- } else if (!entry.isSymbolicLink() && entry.isDirectory()) {
1043
- for (const sub of safeReadDir(full)) {
1044
- if (sub.isFile()) out.push(join2(full, sub.name));
1045
- }
1046
- }
1047
- }
1048
- return out;
1049
- }
1050
- function safeReadDir(dir) {
1051
- try {
1052
- return readdirSync(dir, { withFileTypes: true });
1053
- } catch {
1054
- return [];
1055
- }
1056
- }
1057
- function sanitizeCellId(cellId) {
1058
- return cellId.replace(/[^a-zA-Z0-9_-]/g, "_");
1059
- }
1060
- function isDurable(runDir) {
1061
- return !runDir.startsWith("mem://") && existsSync2(runDir);
1062
- }
1063
- function absoluteRunDir(runDir) {
1064
- return runDir.startsWith("mem://") ? runDir : resolve2(runDir);
1065
- }
1066
- function dedupeSorted(paths) {
1067
- return [...new Set(paths)].sort((a, b) => {
1068
- const da = a.slice(0, a.length - basename(a).length);
1069
- const db = b.slice(0, b.length - basename(b).length);
1070
- return da === db ? basename(a).localeCompare(basename(b)) : da.localeCompare(db);
1071
- });
1072
- }
1073
- function truncate2(s, n) {
1074
- return s.length <= n ? s : `${s.slice(0, n - 1)}\u2026`;
1075
- }
1076
-
1077
- // src/improvement/rollout-policy.ts
1078
- var ROLLOUT_POLICY_EXTENSION = "structural-rollout";
1079
- var ROLLOUT_POLICY_BOUNDS = {
1080
- k: { min: 1, max: 10, step: 2 },
1081
- repairRounds: { min: 0, max: 3, step: 1 },
1082
- testgen: { min: 0, max: 10, step: 3 }
1083
- };
1084
- var MAX_CANDIDATES_PER_GENERATION = 4;
1085
- var clamp = (v, min, max) => Math.min(max, Math.max(min, v));
1086
- var isBoundedInt = (v, min) => typeof v === "number" && Number.isInteger(v) && v >= min;
1087
- function parseRolloutPolicy(surface) {
1088
- if (typeof surface !== "string" || surface.trim().length === 0) return void 0;
1089
- let raw;
1090
- try {
1091
- raw = JSON.parse(surface);
1092
- } catch {
1093
- return void 0;
1094
- }
1095
- return normalizeRolloutPolicy(raw);
1096
- }
1097
- function normalizeRolloutPolicy(raw) {
1098
- if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return void 0;
1099
- const bag = raw;
1100
- const k = bag.k ?? defaultStructuralRolloutPolicy.k;
1101
- const repairRounds = bag.repairRounds ?? defaultStructuralRolloutPolicy.repairRounds;
1102
- const testgen = bag.testgen ?? defaultStructuralRolloutPolicy.testgen;
1103
- if (!isBoundedInt(k, 1) || !isBoundedInt(repairRounds, 0) || !isBoundedInt(testgen, 0)) {
1104
- return void 0;
1105
- }
1106
- return {
1107
- k,
1108
- repairRounds,
1109
- testgen,
1110
- ...typeof bag.diverse === "boolean" ? { diverse: bag.diverse } : {},
1111
- ...typeof bag.temperature === "number" ? { temperature: bag.temperature } : {}
1112
- };
1113
- }
1114
- function serializeRolloutPolicy(policy) {
1115
- return JSON.stringify({
1116
- k: policy.k,
1117
- repairRounds: policy.repairRounds,
1118
- testgen: policy.testgen,
1119
- ...policy.diverse !== void 0 ? { diverse: policy.diverse } : {},
1120
- ...policy.temperature !== void 0 ? { temperature: policy.temperature } : {}
1121
- });
1122
- }
1123
- function structuralRolloutPolicyFromProfile(profile) {
1124
- const bag = profile.extensions?.[ROLLOUT_POLICY_EXTENSION];
1125
- if (bag === void 0) return void 0;
1126
- return normalizeRolloutPolicy(bag);
1127
- }
1128
- function applyRolloutPolicyToProfile(profile, policy) {
1129
- const bag = {
1130
- k: policy.k,
1131
- repairRounds: policy.repairRounds,
1132
- testgen: policy.testgen,
1133
- ...policy.diverse !== void 0 ? { diverse: policy.diverse } : {},
1134
- ...policy.temperature !== void 0 ? { temperature: policy.temperature } : {}
1135
- };
1136
- return {
1137
- ...profile,
1138
- extensions: { ...profile.extensions, [ROLLOUT_POLICY_EXTENSION]: bag }
1139
- };
1140
- }
1141
- function enumerateNeighborPolicies(policy) {
1142
- const moves = [
1143
- { dial: "k", delta: 1 },
1144
- { dial: "k", delta: -1 },
1145
- { dial: "repairRounds", delta: 1 },
1146
- { dial: "repairRounds", delta: -1 },
1147
- { dial: "testgen", delta: 1 },
1148
- { dial: "testgen", delta: -1 }
1149
- ];
1150
- const seen = /* @__PURE__ */ new Set([serializeRolloutPolicy(policy)]);
1151
- const neighbors = [];
1152
- for (const move of moves) {
1153
- const bounds = ROLLOUT_POLICY_BOUNDS[move.dial];
1154
- const next = clamp(policy[move.dial] + move.delta * bounds.step, bounds.min, bounds.max);
1155
- const candidate = { ...policy, [move.dial]: next };
1156
- const key = serializeRolloutPolicy(candidate);
1157
- if (seen.has(key)) continue;
1158
- seen.add(key);
1159
- neighbors.push(candidate);
1160
- }
1161
- return neighbors;
1162
- }
1163
- function candidateLabel(base, next) {
1164
- for (const dial of ["k", "repairRounds", "testgen"]) {
1165
- if (next[dial] !== base[dial]) return `${dial} ${base[dial]}\u2192${next[dial]}`;
1166
- }
1167
- return "unchanged";
1168
- }
1169
- function rolloutPolicyProposer() {
1170
- return {
1171
- kind: "rollout-policy",
1172
- async propose(ctx) {
1173
- const policy = parseRolloutPolicy(ctx.currentSurface);
1174
- if (!policy) return [];
1175
- const neighbors = enumerateNeighborPolicies(policy);
1176
- if (neighbors.length === 0) return [];
1177
- const cap = Math.max(1, Math.min(ctx.populationSize, MAX_CANDIDATES_PER_GENERATION));
1178
- const start = ctx.generation * cap % neighbors.length;
1179
- const window = [];
1180
- for (let i = 0; i < Math.min(cap, neighbors.length); i += 1) {
1181
- window.push(neighbors[(start + i) % neighbors.length]);
1182
- }
1183
- return window.map((candidate) => ({
1184
- surface: serializeRolloutPolicy(candidate),
1185
- label: candidateLabel(policy, candidate),
1186
- rationale: "bounded single-dial neighbor of the current structuralRollout policy; the held-out gate decides (deterministic enumeration \u2014 the dial space is tiny and prompt-style reflective proposals are a measured zero here)"
1187
- }));
1188
- }
1189
- };
1190
- }
1191
-
1192
- // src/improvement/improve.ts
1193
- import { canonicalJson, makeFinding as makeFinding3 } from "@tangle-network/agent-eval";
1194
- import {
1195
- gepaProposer,
1196
- gitWorktreeAdapter,
1197
- memoryCurationProposer,
1198
- skillOptProposer
1199
- } from "@tangle-network/agent-eval/campaign";
1200
- import {
1201
- selfImprove
1202
- } from "@tangle-network/agent-eval/contract";
1203
- import {
1204
- agentProfileSchema
1205
- } from "@tangle-network/agent-interface";
1206
- var defaultReflectionModel = "deepseek-v4-flash";
1207
- function llmClientOptions(llm) {
1208
- return { baseUrl: llm?.baseUrl, apiKey: llm?.apiKey };
1209
- }
1210
- function defaultGeneratorFor(surface, llm) {
1211
- const model = llm?.model ?? defaultReflectionModel;
1212
- switch (surface) {
1213
- case "prompt":
1214
- return gepaProposer({ llm: llmClientOptions(llm), model, target: "agent system prompt" });
1215
- case "skills":
1216
- return skillOptProposer({ llm: llmClientOptions(llm), model, target: "agent skill document" });
1217
- case "memory":
1218
- return memoryCurationProposer();
1219
- case "rollout-policy":
1220
- return rolloutPolicyProposer();
1221
- default:
1222
- return void 0;
1223
- }
1224
- }
1225
- function baselineSurfaceFor(profile, surface, skills) {
1226
- switch (surface) {
1227
- case "prompt":
1228
- return profile.prompt?.systemPrompt ?? "";
1229
- case "skills":
1230
- return inlineSkill(profile, skills).content;
1231
- case "tools":
1232
- return JSON.stringify(profile.tools ?? {});
1233
- case "mcp":
1234
- return JSON.stringify(profile.mcp ?? {});
1235
- case "hooks":
1236
- return JSON.stringify(profile.hooks ?? {});
1237
- case "subagents":
1238
- return JSON.stringify(profile.subagents ?? {});
1239
- case "agent-profile":
1240
- return canonicalJson(profile);
1241
- case "memory":
1242
- return profileInstructions(profile);
1243
- case "rollout-policy": {
1244
- const policy = structuralRolloutPolicyFromProfile(profile);
1245
- return policy ? serializeRolloutPolicy(policy) : "";
1246
- }
1247
- case "code":
1248
- throw new ConfigError(
1249
- "improve(): code requires the isolated baseline created from opts.code.repoRoot"
1250
- );
1251
- }
1252
- }
1253
- var DISTILLED_NOTES_MAX_CHARS = 1500;
1254
- var DISTILLED_ERROR_MAX_CHARS = 500;
1255
- function generationFailureDistiller(staticFindings) {
1256
- const CAP = 12;
1257
- return async (input) => {
1258
- const failures = [];
1259
- for (const candidate of input.candidates) {
1260
- for (const rawCell of candidate.campaign.cells) {
1261
- const cell = rawCell;
1262
- const scenario = String(cell.scenarioId ?? "unknown");
1263
- const error = typeof cell.error === "string" ? cell.error : void 0;
1264
- const judgeScores = cell.judgeScores && typeof cell.judgeScores === "object" ? Object.values(
1265
- cell.judgeScores
1266
- ) : [];
1267
- const composite = judgeScores.length === 0 ? 0 : judgeScores.reduce((sum, j) => sum + (j.composite ?? 0), 0) / judgeScores.length;
1268
- if (!error && composite >= 0.999) continue;
1269
- const notes = judgeScores.map((j) => j.notes).filter((n) => typeof n === "string" && n.length > 0).join("; ").slice(0, DISTILLED_NOTES_MAX_CHARS);
1270
- const claim = notes || (error ? `Scenario ${scenario} failed: ${error.slice(0, DISTILLED_ERROR_MAX_CHARS)}` : "");
1271
- failures.push({
1272
- scenario,
1273
- composite: Number(composite.toFixed(3)),
1274
- notes,
1275
- ...claim ? { claim } : {},
1276
- ...error ? { error: error.slice(0, DISTILLED_ERROR_MAX_CHARS) } : {}
1277
- });
1278
- }
1279
- }
1280
- if (failures.length === 0) return staticFindings;
1281
- failures.sort((a, b) => a.composite - b.composite);
1282
- return failures.slice(0, CAP).map(
1283
- (f) => makeFinding3({
1284
- analyst_id: "generation-failure-distiller",
1285
- severity: f.error !== void 0 || f.composite < 0.5 ? "high" : "medium",
1286
- area: "generation-failure",
1287
- confidence: 1,
1288
- subject: f.scenario,
1289
- claim: `Scenario ${f.scenario} scored composite ${f.composite}${f.notes ? `: ${f.notes}` : ""}${f.error ? ` (error: ${f.error})` : ""}`,
1290
- evidence_refs: [],
1291
- metadata: {
1292
- scenario: f.scenario,
1293
- composite: f.composite,
1294
- ...f.notes ? { notes: f.notes } : {},
1295
- ...f.error !== void 0 ? { error: f.error } : {}
1296
- }
1297
- })
1298
- );
1299
- };
1300
- }
1301
- function memoryGenerationDistiller(staticFindings) {
1302
- const distillFailures = generationFailureDistiller(staticFindings);
1303
- return async (input) => {
1304
- const fresh = await distillFailures(input);
1305
- return fresh === staticFindings ? staticFindings : [...staticFindings, ...fresh];
1306
- };
1307
- }
1308
- function defaultDistillerFor(opts, findings) {
1309
- const surface = opts.surface ?? "prompt";
1310
- const durableRun = opts.runDir !== void 0 && !opts.runDir.startsWith("mem://");
1311
- const useRawTraces = opts.rawTraceContext ?? (surface === "memory" ? false : durableRun);
1312
- if (useRawTraces) return rawTraceDistiller({ fallbackFindings: findings });
1313
- if (surface === "memory") return memoryGenerationDistiller(findings);
1314
- return generationFailureDistiller(findings);
1315
- }
1316
- async function discardPreparedBaseline(worktree, baselineWorktree, cause) {
1317
- return rethrowAfterCleanup(
1318
- cause,
1319
- () => worktree.discard(baselineWorktree),
1320
- "improve(): code preparation failed"
1321
- );
1322
- }
1323
- function isCodeSurface(surface) {
1324
- return typeof surface === "object" && surface !== null && surface.kind === "code";
1325
- }
1326
- async function prepareCodeRun(code) {
1327
- const baseRef = code.baseRef ?? "main";
1328
- const worktree = code.worktree ?? gitWorktreeAdapter({
1329
- repoRoot: code.repoRoot,
1330
- ...code.worktreeDir ? { worktreeDir: code.worktreeDir } : {}
1331
- });
1332
- const baselineWorktree = await worktree.create({ baseRef, label: "incumbent-baseline" });
1333
- try {
1334
- const baseline = await worktree.finalize(baselineWorktree, "Incumbent code checkout");
1335
- let baselineDiscarded = false;
1336
- const generator = code.generator ?? agenticGenerator({
1337
- ...code.harness ? { harness: code.harness } : {},
1338
- ...code.verify ? { verify: code.verify } : {},
1339
- ...code.timeoutMs ? { timeoutMs: code.timeoutMs } : {}
1340
- });
1341
- const managed = improvementDriver({ worktree, generator, baseRef });
1342
- return {
1343
- baseline,
1344
- proposer: managed,
1345
- async cleanup(retainedWinner) {
1346
- const errors = [];
1347
- const retainedWorktreeRef = isCodeSurface(retainedWinner) ? retainedWinner.worktreeRef : void 0;
1348
- try {
1349
- await managed?.cleanup(retainedWorktreeRef ? [retainedWorktreeRef] : []);
1350
- } catch (cause) {
1351
- errors.push(cause);
1352
- }
1353
- if (!baselineDiscarded && retainedWorktreeRef !== baseline.worktreeRef) {
1354
- try {
1355
- await worktree.discard(baselineWorktree);
1356
- baselineDiscarded = true;
1357
- } catch (cause) {
1358
- errors.push(cause);
1359
- }
1360
- }
1361
- if (errors.length > 0) {
1362
- throw new AggregateError(errors, "improve(): failed to clean code improvement worktrees");
1363
- }
1364
- }
1365
- };
1366
- } catch (cause) {
1367
- return discardPreparedBaseline(worktree, baselineWorktree, cause);
1368
- }
1369
- }
1370
- function idempotentDispose(dispose) {
1371
- let disposed = false;
1372
- let inFlight;
1373
- return async () => {
1374
- if (disposed) return;
1375
- if (inFlight) return inFlight;
1376
- inFlight = (async () => {
1377
- await dispose();
1378
- disposed = true;
1379
- })();
1380
- try {
1381
- await inFlight;
1382
- } finally {
1383
- inFlight = void 0;
1384
- }
1385
- };
1386
- }
1387
- function parseWinnerJson(winner, surface) {
1388
- try {
1389
- return JSON.parse(winner);
1390
- } catch (cause) {
1391
- throw new ConfigError(
1392
- `improve(): the '${surface}' candidate is not valid JSON, so it cannot form a profile candidate: ${cause.message}`
1393
- );
1394
- }
1395
- }
1396
- function assertCandidateSurfaceKind(surface, winner) {
1397
- if (surface === "code" ? typeof winner === "string" : typeof winner !== "string") {
1398
- throw new ConfigError(
1399
- `improve(): the '${surface}' candidate returned an incompatible surface value`
1400
- );
1401
- }
1402
- }
1403
- function materializeImprovementProfileCandidate(profile, surface, winner, skills) {
1404
- if (typeof winner !== "string") return void 0;
1405
- let candidate;
1406
- switch (surface) {
1407
- case "prompt":
1408
- candidate = { ...profile, prompt: { ...profile.prompt, systemPrompt: winner } };
1409
- break;
1410
- case "skills": {
1411
- const selectedSkill = inlineSkill(profile, skills);
1412
- candidate = {
1413
- ...profile,
1414
- resources: {
1415
- ...profile.resources,
1416
- skills: profile.resources?.skills?.map(
1417
- (resource) => resource === selectedSkill ? { ...resource, content: winner } : resource
1418
- )
1419
- }
1420
- };
1421
- break;
1422
- }
1423
- case "tools":
1424
- candidate = { ...profile, tools: parseWinnerJson(winner, surface) };
1425
- break;
1426
- case "mcp":
1427
- candidate = { ...profile, mcp: parseWinnerJson(winner, surface) };
1428
- break;
1429
- case "hooks":
1430
- candidate = { ...profile, hooks: parseWinnerJson(winner, surface) };
1431
- break;
1432
- case "subagents":
1433
- candidate = { ...profile, subagents: parseWinnerJson(winner, surface) };
1434
- break;
1435
- case "agent-profile":
1436
- candidate = parseWinnerJson(winner, surface);
1437
- break;
1438
- case "memory":
1439
- candidate = {
1440
- ...profile,
1441
- resources: {
1442
- ...profile.resources,
1443
- instructions: replaceProfileInstructions(profile, winner)
1444
- }
1445
- };
1446
- break;
1447
- case "rollout-policy": {
1448
- if (winner === "") return void 0;
1449
- const policy = normalizeRolloutPolicy(parseWinnerJson(winner, surface));
1450
- if (!policy) {
1451
- throw new ConfigError(
1452
- `improve(): the shipped 'rollout-policy' winner is not a valid StructuralRolloutPolicy (integer k >= 1, repairRounds >= 0, testgen >= 0), so it cannot be applied: ${winner}`
1453
- );
1454
- }
1455
- candidate = applyRolloutPolicyToProfile(profile, policy);
1456
- break;
1457
- }
1458
- case "code":
1459
- return void 0;
1460
- }
1461
- const parsed = agentProfileSchema.safeParse(candidate);
1462
- if (!parsed.success) {
1463
- throw new ConfigError(
1464
- `improve(): the '${surface}' candidate does not produce a valid AgentProfile: ${parsed.error.message}`
1465
- );
1466
- }
1467
- return immutableCandidateValue(parsed.data);
1468
- }
1469
- function profileImprovementAgent(input) {
1470
- return (surfaceValue, scenario, ctx) => {
1471
- const profile = materializeImprovementProfileCandidate(
1472
- input.baselineProfile,
1473
- input.surface,
1474
- surfaceValue,
1475
- input.skills
1476
- );
1477
- const dispatchedProfile = profile ?? (input.surface === "rollout-policy" && surfaceValue === "" ? input.baselineProfile : void 0);
1478
- if (!dispatchedProfile) {
1479
- throw new ConfigError(
1480
- `improve(): surface '${input.surface}' cannot run through profileDispatch; use opts.agent for a non-profile surface`
1481
- );
1482
- }
1483
- return input.dispatch(dispatchedProfile, scenario, ctx);
1484
- };
1485
- }
1486
- function inlineSkill(profile, options) {
1487
- const resourceName = options?.resourceName.trim();
1488
- if (!resourceName) {
1489
- throw new ConfigError(
1490
- "improve(): surface 'skills' requires opts.skills.resourceName for one inline profile skill"
1491
- );
1492
- }
1493
- assertFailClosedResources(profile, "skills");
1494
- const matches = (profile.resources?.skills ?? []).filter(
1495
- (resource) => resource.name === resourceName
1496
- );
1497
- if (matches.length !== 1 || matches[0]?.kind !== "inline") {
1498
- throw new ConfigError(
1499
- `improve(): skill '${resourceName}' must identify exactly one inline profile resource`
1500
- );
1501
- }
1502
- return matches[0];
1503
- }
1504
- function profileInstructions(profile) {
1505
- assertFailClosedResources(profile, "memory");
1506
- const instructions = profile.resources?.instructions;
1507
- if (instructions === void 0) return "";
1508
- if (typeof instructions === "string") return instructions;
1509
- if (instructions.kind === "inline") return instructions.content;
1510
- throw new ConfigError(
1511
- "improve(): surface 'memory' requires inline profile instructions so candidate bytes are exact"
1512
- );
1513
- }
1514
- function replaceProfileInstructions(profile, content) {
1515
- const instructions = profile.resources?.instructions;
1516
- if (typeof instructions !== "object") return content;
1517
- if (instructions.kind !== "inline") {
1518
- throw new ConfigError(
1519
- "improve(): surface 'memory' requires inline profile instructions so candidate bytes are exact"
1520
- );
1521
- }
1522
- return { ...instructions, content };
1523
- }
1524
- function assertFailClosedResources(profile, surface) {
1525
- if (profile.resources?.failOnError !== true) {
1526
- throw new ConfigError(
1527
- `improve(): surface '${surface}' requires profile.resources.failOnError: true`
1528
- );
1529
- }
1530
- }
1531
- async function improve(profile, findings, opts) {
1532
- const {
1533
- surface = "prompt",
1534
- gate = "holdout",
1535
- agent,
1536
- profileDispatch,
1537
- generator,
1538
- allowedModels,
1539
- rawTraceContext,
1540
- code,
1541
- skills,
1542
- promotionGate,
1543
- analyzeGeneration,
1544
- ...sharedOptions
1545
- } = opts;
1546
- const parsedProfile = agentProfileSchema.safeParse(profile);
1547
- if (!parsedProfile.success) {
1548
- throw new ConfigError(
1549
- `improve(): input is not a valid AgentProfile: ${parsedProfile.error.message}`
1550
- );
1551
- }
1552
- if (agent !== void 0 && profileDispatch !== void 0) {
1553
- throw new ConfigError("improve(): provide exactly one of opts.agent or opts.profileDispatch");
1554
- }
1555
- if (agent === void 0 && profileDispatch === void 0) {
1556
- throw new ConfigError("improve(): provide exactly one of opts.agent or opts.profileDispatch");
1557
- }
1558
- const baselineProfile = immutableCandidateValue(parsedProfile.data);
1559
- if (profileDispatch && surface === "code") {
1560
- throw new ConfigError(
1561
- "improve(): surface 'code' cannot use profileDispatch because code candidates require isolated worktree execution; use opts.agent"
1562
- );
1563
- }
1564
- if (surface === "code" && generator) {
1565
- throw new ConfigError(
1566
- "improve(): surface 'code' forbids opts.generator because an external SurfaceProposer cannot transfer checkout ownership; pass opts.code.generator instead"
1567
- );
1568
- }
1569
- const usesReflectionModel = !generator && (surface === "prompt" || surface === "skills");
1570
- if (usesReflectionModel) {
1571
- assertModelAllowed(sharedOptions.llm?.model ?? defaultReflectionModel, allowedModels);
1572
- }
1573
- let preparedCode;
1574
- if (surface === "code") {
1575
- if (!code) {
1576
- throw new ConfigError(
1577
- "improve(): surface 'code' requires opts.code.repoRoot so the incumbent can run from an isolated checkout"
1578
- );
1579
- }
1580
- preparedCode = await prepareCodeRun(code);
1581
- }
1582
- const proposer = preparedCode?.proposer ?? generator ?? defaultGeneratorFor(surface, sharedOptions.llm);
1583
- if (!proposer) {
1584
- throw new ConfigError(
1585
- `improve(): surface '${surface}' has no default generator \u2014 pass opts.generator (a SurfaceProposer) explicitly`
1586
- );
1587
- }
1588
- const budget = gate === "none" ? { ...sharedOptions.budget, generations: 0 } : { ...sharedOptions.budget };
1589
- const effectiveAgent = profileDispatch === void 0 ? agent : profileImprovementAgent({
1590
- baselineProfile,
1591
- surface,
1592
- ...skills === void 0 ? {} : { skills },
1593
- dispatch: profileDispatch
1594
- });
1595
- if (!effectiveAgent) {
1596
- throw new ConfigError("improve(): provide exactly one of opts.agent or opts.profileDispatch");
1597
- }
1598
- let raw;
1599
- try {
1600
- raw = await selfImprove({
1601
- ...sharedOptions,
1602
- agent: effectiveAgent,
1603
- baselineSurface: preparedCode?.baseline ?? baselineSurfaceFor(baselineProfile, surface, skills),
1604
- proposer,
1605
- budget,
1606
- findings,
1607
- ...promotionGate !== void 0 ? { gate: promotionGate } : {},
1608
- ...analyzeGeneration === null ? {} : {
1609
- analyzeGeneration: analyzeGeneration ?? defaultDistillerFor(opts, findings)
1610
- }
1611
- });
1612
- } catch (cause) {
1613
- if (!preparedCode) throw cause;
1614
- return rethrowAfterCleanup(
1615
- cause,
1616
- () => preparedCode.cleanup(),
1617
- "improve(): code improvement failed"
1618
- );
1619
- }
1620
- const winnerSurface = raw.winner.surface;
1621
- assertCandidateSurfaceKind(surface, winnerSurface);
1622
- if (preparedCode) {
1623
- try {
1624
- await preparedCode.cleanup(winnerSurface);
1625
- } catch (cleanupCause) {
1626
- try {
1627
- await preparedCode.cleanup();
1628
- } catch (finalCleanupCause) {
1629
- throw new AggregateError(
1630
- [cleanupCause, finalCleanupCause],
1631
- "improve(): code result cleanup failed, including the final all-worktree retry"
1632
- );
1633
- }
1634
- throw new AggregateError(
1635
- [cleanupCause],
1636
- "improve(): code result cleanup failed; the final all-worktree retry succeeded"
1637
- );
1638
- }
1639
- }
1640
- const dispose = idempotentDispose(async () => preparedCode?.cleanup());
1641
- const candidateProfile = materializeImprovementProfileCandidate(
1642
- baselineProfile,
1643
- surface,
1644
- winnerSurface,
1645
- skills
1646
- );
1647
- const candidate = immutableCandidateValue({
1648
- surface,
1649
- value: winnerSurface,
1650
- ...candidateProfile ? { profile: candidateProfile } : {}
1651
- });
1652
- return {
1653
- candidate,
1654
- decision: raw.gateDecision,
1655
- ...raw.lift !== void 0 ? { lift: raw.lift } : {},
1656
- raw,
1657
- dispose
1658
- };
1659
- }
1660
-
1661
- // src/intelligence/improvement-surfaces.ts
1662
- import {
1663
- agentProfileDiffSchema,
1664
- agentProfileSchema as agentProfileSchema2,
1665
- defineAgentProfileDiff
1666
- } from "@tangle-network/agent-interface";
1667
- var changedSurfaceOrder = [
1668
- "prompt",
1669
- "skills",
1670
- "tools",
1671
- "mcp",
1672
- "hooks",
1673
- "subagents",
1674
- "agent-profile",
1675
- "memory",
1676
- "code",
1677
- "knowledge"
1678
- ];
1679
- var AGENT_IMPROVEMENT_PROFILE_SURFACES = [
1680
- "prompt",
1681
- "skills",
1682
- "tools",
1683
- "mcp",
1684
- "hooks",
1685
- "subagents"
1686
- ];
1687
- function deriveChangedSurfaces(baselineBundle, candidateBundle) {
1688
- if (baselineBundle.knowledge || candidateBundle.knowledge) {
1689
- assertKnowledgeCandidatePair(baselineBundle, candidateBundle);
1690
- }
1691
- assertCodeCandidatePair(baselineBundle, candidateBundle);
1692
- const baseline = improvementSurfaceValues(baselineBundle);
1693
- const candidate = improvementSurfaceValues(candidateBundle);
1694
- const changed = changedSurfaceOrder.filter(
1695
- (surface) => canonicalCandidateDigest(baseline[surface]) !== canonicalCandidateDigest(candidate[surface])
1696
- );
1697
- if (changed.length === 0) throw new Error("candidate experiment does not change an agent surface");
1698
- return changed;
1699
- }
1700
- function assertAgentImprovementActivationTargets(surfaces, experiment, intent, targets) {
1701
- const expected = new Set(surfaces);
1702
- const actual = new Set(targets.map((target) => target.surface));
1703
- const sourceArm = intent === "activate-candidate" ? "baseline" : "candidate";
1704
- if (targets.some((target) => !target.identity.trim()) || targets.some(
1705
- (target) => target.expectedBaseDigest !== agentImprovementTargetDigest(experiment, sourceArm, target.surface)
1706
- ) || targets.length !== surfaces.length || expected.size !== actual.size || [...expected].some((surface) => !actual.has(surface))) {
1707
- throw new Error("candidate activation targets must cover exactly the changed surfaces");
1708
- }
1709
- }
1710
- function buildAgentImprovementActivationTargets(surfaces, experiment, intent, identities) {
1711
- const sourceArm = intent === "activate-candidate" ? "baseline" : "candidate";
1712
- const targets = identities.map((target) => ({
1713
- ...target,
1714
- expectedBaseDigest: agentImprovementTargetDigest(experiment, sourceArm, target.surface)
1715
- }));
1716
- assertAgentImprovementActivationTargets(surfaces, experiment, intent, targets);
1717
- return targets;
1718
- }
1719
- function agentImprovementTargetDigest(experiment, arm, surface) {
1720
- if (surface === "knowledge") {
1721
- const knowledge = assertKnowledgeCandidatePair(experiment.baseline, experiment.candidate);
1722
- return arm === "baseline" ? knowledge.candidate.baseHash : knowledge.candidate.candidateHash;
1723
- }
1724
- if (surface === "code") assertCodeCandidatePair(experiment.baseline, experiment.candidate);
1725
- return canonicalCandidateDigest(improvementSurfaceValues(experiment[arm])[surface]);
1726
- }
1727
- function agentImprovementTargetInput(bundle, surface) {
1728
- return improvementSurfaceValues(bundle)[surface];
1729
- }
1730
- function isAgentImprovementProfileSurface(surface) {
1731
- return AGENT_IMPROVEMENT_PROFILE_SURFACES.includes(surface);
1732
- }
1733
- function agentImprovementProfileSurfaceInput(profile, surface) {
1734
- const parsed = parseExactAgentProfile(
1735
- omitUndefinedObjectFields(profile, "agent improvement profile"),
1736
- "agent improvement profile"
1737
- );
1738
- return immutableCandidateValue(profileSurfaceInput(parsed, surface));
1739
- }
1740
- function profileSurfaceInput(profile, surface) {
1741
- switch (surface) {
1742
- case "prompt":
1743
- return { prompt: profile.prompt ?? null };
1744
- case "skills":
1745
- return profile.resources?.skills ?? null;
1746
- case "tools":
1747
- return {
1748
- tools: profile.tools ?? null,
1749
- resources: profile.resources?.tools ?? null
1750
- };
1751
- case "mcp":
1752
- return profile.mcp ?? null;
1753
- case "hooks":
1754
- return profile.hooks ?? null;
1755
- case "subagents":
1756
- return {
1757
- subagents: profile.subagents ?? null,
1758
- resources: profile.resources?.agents ?? null
1759
- };
1760
- }
1761
- }
1762
- function agentImprovementProfileSurfaceDigest(profile, surface) {
1763
- return canonicalCandidateDigest(agentImprovementProfileSurfaceInput(profile, surface));
1764
- }
1765
- function agentImprovementTargetProfileDiffs(target, options) {
1766
- const { remove, set } = improvementSurfaceReplacement(target);
1767
- const common = {
1768
- kind: "agent-profile-diff",
1769
- ...options.source ? { source: options.source } : {},
1770
- metadata: {
1771
- ...options.metadata ?? {},
1772
- surface: target.surface
1773
- }
1774
- };
1775
- const reset = agentProfileDiffSchema.parse(
1776
- defineAgentProfileDiff({
1777
- ...common,
1778
- id: `${options.id}:${target.surface}:reset`,
1779
- title: `Replace active ${target.surface}`,
1780
- remove
1781
- })
1782
- );
1783
- if (!set) return [reset];
1784
- const replacement = agentProfileDiffSchema.parse(
1785
- defineAgentProfileDiff({
1786
- ...common,
1787
- id: `${options.id}:${target.surface}:set`,
1788
- title: `Activate measured ${target.surface}`,
1789
- set
1790
- })
1791
- );
1792
- return [reset, replacement];
1793
- }
1794
- function improvementSurfaceReplacement(target) {
1795
- const value = target.desiredInput;
1796
- switch (target.surface) {
1797
- case "prompt": {
1798
- const prompt = exactObject(value, ["prompt"], "prompt activation input").prompt;
1799
- assertDefined(prompt, "prompt activation input.prompt");
1800
- return {
1801
- remove: { prompt: true },
1802
- ...prompt === null ? {} : { set: parseProfileSet({ prompt }) }
1803
- };
1804
- }
1805
- case "skills":
1806
- assertDefined(value, "skills activation input");
1807
- return {
1808
- remove: { resources: { skills: true } },
1809
- ...value === null ? {} : { set: parseProfileSet({ resources: { skills: value } }) }
1810
- };
1811
- case "tools": {
1812
- const parsed = exactObject(value, ["tools", "resources"], "tools activation input");
1813
- assertDefined(parsed.tools, "tools activation input.tools");
1814
- assertDefined(parsed.resources, "tools activation input.resources");
1815
- const set = {
1816
- ...parsed.tools === null ? {} : { tools: parsed.tools },
1817
- ...parsed.resources === null ? {} : { resources: { tools: parsed.resources } }
1818
- };
1819
- return {
1820
- remove: { tools: true, resources: { tools: true } },
1821
- ...Object.keys(set).length === 0 ? {} : { set: parseProfileSet(set) }
1822
- };
1823
- }
1824
- case "mcp":
1825
- assertDefined(value, "mcp activation input");
1826
- return {
1827
- remove: { mcp: true },
1828
- ...value === null ? {} : { set: parseProfileSet({ mcp: value }) }
1829
- };
1830
- case "hooks":
1831
- assertDefined(value, "hooks activation input");
1832
- return {
1833
- remove: { hooks: true },
1834
- ...value === null ? {} : { set: parseProfileSet({ hooks: value }) }
1835
- };
1836
- case "subagents": {
1837
- const parsed = exactObject(value, ["subagents", "resources"], "subagents activation input");
1838
- assertDefined(parsed.subagents, "subagents activation input.subagents");
1839
- assertDefined(parsed.resources, "subagents activation input.resources");
1840
- const set = {
1841
- ...parsed.subagents === null ? {} : { subagents: parsed.subagents },
1842
- ...parsed.resources === null ? {} : { resources: { agents: parsed.resources } }
1843
- };
1844
- return {
1845
- remove: { subagents: true, resources: { agents: true } },
1846
- ...Object.keys(set).length === 0 ? {} : { set: parseProfileSet(set) }
1847
- };
1848
- }
1849
- }
1850
- }
1851
- function assertDefined(value, label) {
1852
- if (value === void 0) throw new Error(`${label} must not be undefined`);
1853
- }
1854
- function exactObject(value, keys, label) {
1855
- if (!value || typeof value !== "object" || Array.isArray(value)) {
1856
- throw new Error(`${label} must be an object`);
1857
- }
1858
- const record = value;
1859
- const actual = Object.keys(record).sort();
1860
- const expected = [...keys].sort();
1861
- if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
1862
- throw new Error(`${label} must contain exactly: ${expected.join(", ")}`);
1863
- }
1864
- return record;
1865
- }
1866
- function parseProfileSet(value) {
1867
- return agentProfileSchema2.parse(value);
1868
- }
1869
- function assertKnowledgeCandidatePair(baselineBundle, candidateBundle) {
1870
- const baseline = baselineBundle.knowledge;
1871
- const candidate = candidateBundle.knowledge;
1872
- if (!baseline && !candidate) {
1873
- throw new Error("knowledge candidate pair is not present");
1874
- }
1875
- if (!baseline || !candidate || canonicalCandidateDigest({ candidate: baseline.candidate, evaluation: baseline.evaluation }) !== canonicalCandidateDigest({ candidate: candidate.candidate, evaluation: candidate.evaluation })) {
1876
- throw new Error(
1877
- "knowledge experiment arms must share one measured candidate and evaluation identity"
1878
- );
1879
- }
1880
- return candidate;
1881
- }
1882
- function assertCodeCandidatePair(baselineBundle, candidateBundle) {
1883
- const baseline = baselineBundle.code;
1884
- const candidate = candidateBundle.code;
1885
- if (canonicalCandidateDigest(baseline) === canonicalCandidateDigest(candidate) || candidate.kind !== "git-patch") {
1886
- return;
1887
- }
1888
- const baselineTree = baseline.kind === "no-op" ? baseline.baseTree : baseline.kind === "git-patch" ? baseline.candidateTree : void 0;
1889
- const baselineRepository = baseline.kind === "disabled" ? void 0 : baseline.repository;
1890
- if (baselineTree !== candidate.baseTree || !baselineRepository || canonicalCandidateDigest(baselineRepository) !== canonicalCandidateDigest(candidate.repository)) {
1891
- throw new Error(
1892
- "code candidate must be based on the exact repository tree measured by the baseline arm"
1893
- );
1894
- }
1895
- }
1896
- function improvementSurfaceValues(bundle) {
1897
- const profile = agentCandidateProfileAsAgentProfile(bundle.profile);
1898
- return {
1899
- prompt: agentImprovementProfileSurfaceInput(profile, "prompt"),
1900
- skills: agentImprovementProfileSurfaceInput(profile, "skills"),
1901
- tools: agentImprovementProfileSurfaceInput(profile, "tools"),
1902
- mcp: agentImprovementProfileSurfaceInput(profile, "mcp"),
1903
- hooks: agentImprovementProfileSurfaceInput(profile, "hooks"),
1904
- subagents: agentImprovementProfileSurfaceInput(profile, "subagents"),
1905
- "agent-profile": { profile: opaqueProfileSlice(profile), execution: bundle.execution },
1906
- memory: {
1907
- instructions: profile.resources?.instructions ?? null,
1908
- executionPolicy: bundle.memory
1909
- },
1910
- code: bundle.code,
1911
- knowledge: bundle.knowledge ?? null
1912
- };
1913
- }
1914
- function opaqueProfileSlice(profile) {
1915
- const {
1916
- prompt: _prompt,
1917
- tools: _tools,
1918
- mcp: _mcp,
1919
- hooks: _hooks,
1920
- subagents: _subagents,
1921
- resources,
1922
- ...opaqueProfile
1923
- } = profile;
1924
- const {
1925
- instructions: _instructions,
1926
- skills: _skills,
1927
- tools: _resourceTools,
1928
- agents: _agents,
1929
- ...opaqueResources
1930
- } = resources ?? {};
1931
- return {
1932
- ...opaqueProfile,
1933
- ...Object.keys(opaqueResources).length > 0 ? { resources: opaqueResources } : {}
1934
- };
1935
- }
1936
-
1937
- // src/intelligence/improvement-cycle.ts
1938
- import { assertNoJudgeVerdict } from "@tangle-network/agent-eval/analyst";
1939
- import {
1940
- measuredComparisonFromCandidateExperiment,
1941
- runCandidateExperiment,
1942
- sealCandidateExperiment,
1943
- verifyCandidateExperiment,
1944
- verifyCandidateExperimentComparison
1945
- } from "@tangle-network/agent-eval/contract";
1946
- import {
1947
- agentCandidateMaterializationReceiptSchema,
1948
- agentCandidateRunReceiptSchema,
1949
- agentImprovementActivationSchema,
1950
- agentImprovementProposalSchema,
1951
- agentImprovementReviewSchema,
1952
- candidateExecutionEvidenceSchema
1953
- } from "@tangle-network/agent-interface";
1954
- import { materializeCandidateProfile } from "@tangle-network/agent-profile-materialize";
1955
- var AgentCandidateExperimentCellExecutionError = class extends Error {
1956
- finalization;
1957
- constructor(finalization) {
1958
- super(`candidate experiment cell failed: ${finalization.reason}`);
1959
- this.name = "AgentCandidateExperimentCellExecutionError";
1960
- this.finalization = finalization;
1961
- }
1962
- };
1963
- function sealAgentImprovementExperiment(material, improvement) {
1964
- assertRuntimeOwnedExperimentFieldsAbsent(material);
1965
- const candidateLineage = {
1966
- source: "optimizer",
1967
- parentDigests: [material.baseline.digest],
1968
- runIds: [improvement.raw.provenance.runId],
1969
- developmentSplitDigest: improvement.raw.provenance.evidence.search.splitDigest
1970
- };
1971
- return sealCandidateExperiment({ ...material, candidateLineage });
1972
- }
1973
- function assertRuntimeOwnedExperimentFieldsAbsent(material) {
1974
- if (material === null || typeof material !== "object" || Array.isArray(material)) {
1975
- throw new Error("agent improvement experiment material must be an object");
1976
- }
1977
- const supplied = ["candidateLineage", "digest"].filter((field) => Object.hasOwn(material, field));
1978
- if (supplied.length > 0) {
1979
- throw new Error(
1980
- `agent improvement experiment material must not supply Runtime-owned fields: ${supplied.join(", ")}`
1981
- );
1982
- }
1983
- }
1984
- async function runAgentCandidateExperiment(options) {
1985
- const experiment = verifyCandidateExperiment(options.experiment);
1986
- const measurements = await runCandidateExperiment({
1987
- experiment,
1988
- ...options.maxConcurrency === void 0 ? {} : { maxConcurrency: options.maxConcurrency },
1989
- ...options.signal ? { signal: options.signal } : {},
1990
- execute: async (input) => {
1991
- const placement = await options.placeCell(input);
1992
- return await executeAgentCandidateExperimentCell({ ...input, ...placement });
1993
- }
1994
- });
1995
- const evaluation = createAgentImprovementMeasuredComparison({
1996
- experiment,
1997
- measurements,
1998
- runId: options.runId,
1999
- ...options.candidate ? { candidate: options.candidate } : {},
2000
- ...options.generationsExplored === void 0 ? {} : { generationsExplored: options.generationsExplored },
2001
- ...options.searchDurationMs === void 0 ? {} : { searchDurationMs: options.searchDurationMs },
2002
- ...options.searchCostUsd === void 0 ? {} : { searchCostUsd: options.searchCostUsd },
2003
- ...options.metadata ? { metadata: options.metadata } : {}
2004
- });
2005
- return { experiment, measurements, evaluation };
2006
- }
2007
- async function executeAgentCandidateExperimentCell(options) {
2008
- const experiment = verifyCandidateExperiment(options.experiment);
2009
- const bundle = experiment[options.arm];
2010
- assertExactExperimentInput(options, experiment, bundle);
2011
- const attempt = options.attempt ?? 1;
2012
- if (attempt > options.task.attempt.maxAttempts) {
2013
- throw new Error("candidate experiment attempt exceeds the signed task policy");
2014
- }
2015
- const runCell = canonicalCandidateDocument({
2016
- kind: "agent-candidate-run-cell",
2017
- experimentDigest: experiment.digest,
2018
- arm: options.arm,
2019
- bundleDigest: bundle.digest,
2020
- suiteDigest: options.benchmarkCell.suiteDigest,
2021
- taskDigest: options.task.digest,
2022
- taskIndex: options.benchmarkCell.taskIndex,
2023
- repetition: options.benchmarkCell.repetition,
2024
- seed: options.seed,
2025
- attempt
2026
- }).value;
2027
- const verified = await verifyAgentCandidateBundle(bundle, options.ports);
2028
- const prepared = await prepareAgentCandidateExecution(
2029
- verified,
2030
- {
2031
- executionId: options.executionId,
2032
- runCell,
2033
- benchmarkSuite: experiment.benchmark.suite,
2034
- task: options.task,
2035
- executionRoots: options.executionRoots,
2036
- stagingRoots: options.stagingRoots
2037
- },
2038
- options.ports,
2039
- options.preparation
2040
- );
2041
- const finalization = await executePreparedAgentCandidate(prepared, options.execution);
2042
- if (!finalization.succeeded) {
2043
- throw new AgentCandidateExperimentCellExecutionError(finalization);
2044
- }
2045
- const evidence = canonicalCandidateDocument({
2046
- kind: "agent-candidate-execution-evidence",
2047
- materializationReceipt: prepared.materializationReceipt.value,
2048
- receipt: finalization.receipt.value
2049
- }).value;
2050
- return verifyCandidateExecutionEvidence(evidence, {
2051
- experiment,
2052
- arm: options.arm,
2053
- benchmarkCell: options.benchmarkCell,
2054
- seed: options.seed,
2055
- attempt,
2056
- resolvedResources: verifiedResourceTextByDigest(verified)
2057
- });
2058
- }
2059
- function createAgentImprovementMeasuredComparison(options) {
2060
- return verifyCandidateExperimentComparison(measuredComparisonFromCandidateExperiment(options));
2061
- }
2062
- async function proposeAgentImprovement(options) {
2063
- const analysis = await runAnalystLoop({ ...options.analysis, runId: options.runId });
2064
- const findings = assertNoJudgeVerdict(
2065
- analysis.analystResult.findings,
2066
- "proposeAgentImprovement findings"
2067
- );
2068
- const improvement = await improve(options.profile, [...findings], options.improvement);
2069
- try {
2070
- if (improvement.decision !== "ship") {
2071
- throw new Error("agent improvement search did not produce a promotable candidate");
2072
- }
2073
- const experiment = sealAgentImprovementExperiment(
2074
- await options.buildExperiment({ analysis, improvement }),
2075
- improvement
2076
- );
2077
- assertCandidateProfileBinding(options.profile, experiment.baseline.profile);
2078
- assertImprovementCandidateBinding(improvement, experiment);
2079
- const measured = await runAgentCandidateExperiment({
2080
- experiment,
2081
- runId: options.runId,
2082
- placeCell: options.placeCell,
2083
- ...options.maxConcurrency === void 0 ? {} : { maxConcurrency: options.maxConcurrency },
2084
- ...options.signal ? { signal: options.signal } : {},
2085
- ...options.candidate ? { candidate: options.candidate } : {},
2086
- ...options.metadata ? { metadata: options.metadata } : {},
2087
- generationsExplored: improvement.raw.generationsExplored,
2088
- searchDurationMs: improvement.raw.durationMs,
2089
- searchCostUsd: improvement.raw.totalCostUsd
2090
- });
2091
- const proposal = createAgentImprovementProposal({
2092
- runId: options.runId,
2093
- findings,
2094
- evaluation: measured.evaluation,
2095
- ...options.now ? { now: options.now } : {}
2096
- });
2097
- return {
2098
- analysis,
2099
- improvement,
2100
- experiment,
2101
- measurements: measured.measurements,
2102
- proposal
2103
- };
2104
- } catch (cause) {
2105
- return rethrowAfterCleanup(cause, () => improvement.dispose(), "proposeAgentImprovement failed");
2106
- }
2107
- }
2108
- function assertImprovementCandidateBinding(improvement, experiment) {
2109
- const candidate = improvement.candidate;
2110
- if (candidate.surface !== "code") {
2111
- if (!candidate.profile) {
2112
- throw new Error(`improvement surface '${candidate.surface}' did not produce an exact profile`);
2113
- }
2114
- try {
2115
- assertCandidateProfileBinding(candidate.profile, experiment.candidate.profile);
2116
- } catch (cause) {
2117
- throw new Error("candidate experiment does not contain the improvement winner", { cause });
2118
- }
2119
- return;
2120
- }
2121
- const surface = candidate.value;
2122
- const code = experiment.candidate.code;
2123
- if (typeof surface !== "object" || surface === null || surface.kind !== "code" || code.kind !== "git-patch" || code.baseCommit !== surface.baseCommit || code.baseTree !== surface.baseTree || code.candidateTree !== surface.candidateTree || code.patch.artifact.sha256 !== surface.patch.sha256 || code.patch.artifact.byteLength !== surface.patch.byteLength) {
2124
- throw new Error("candidate experiment does not contain the improvement winner");
2125
- }
2126
- if (canonicalCandidateDigest(experiment.baseline.profile) !== canonicalCandidateDigest(experiment.candidate.profile)) {
2127
- throw new Error("code improvement candidate changed the agent profile");
2128
- }
2129
- }
2130
- function createAgentImprovementProposal(options) {
2131
- const findings = assertNoJudgeVerdict(
2132
- [...options.findings],
2133
- "createAgentImprovementProposal findings"
2134
- );
2135
- const evaluation = verifyCandidateExperimentComparison(options.evaluation);
2136
- if (evaluation.decision.outcome !== "ship") {
2137
- throw new Error("agent improvement proposal requires a passing experiment");
2138
- }
2139
- if (options.runId !== evaluation.provenance.runId) {
2140
- throw new Error("proposal runId does not match its measured experiment");
2141
- }
2142
- const changedSurfaces = deriveChangedSurfaces(
2143
- evaluation.experiment.baseline,
2144
- evaluation.experiment.candidate
2145
- );
2146
- return agentImprovementProposalSchema.parse(
2147
- canonicalCandidateDocument({
2148
- kind: "agent-improvement-proposal",
2149
- runId: options.runId,
2150
- changedSurfaces,
2151
- proposedAt: (options.now ?? (() => /* @__PURE__ */ new Date()))().toISOString(),
2152
- findings: [...findings],
2153
- evaluation
2154
- }).value
2155
- );
2156
- }
2157
- function reviewAgentImprovementProposal(inputProposal, input) {
2158
- const proposal = verifyAgentImprovementProposal(inputProposal);
2159
- if (!input.reviewedBy.trim()) throw new Error("candidate review requires reviewedBy");
2160
- if (!input.reason.trim()) throw new Error("candidate review requires a reason");
2161
- if (input.decision === "approve" && proposal.evaluation.decision.outcome !== "ship") {
2162
- throw new Error("candidate cannot be approved without a passing experiment");
2163
- }
2164
- const reviewedAt = (input.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
2165
- if (Date.parse(reviewedAt) < Date.parse(proposal.proposedAt)) {
2166
- throw new Error("candidate review cannot predate its proposal");
2167
- }
2168
- return agentImprovementReviewSchema.parse(
2169
- canonicalCandidateDocument({
2170
- kind: "agent-improvement-review",
2171
- proposalDigest: proposal.digest,
2172
- decision: input.decision,
2173
- reviewedBy: input.reviewedBy,
2174
- reviewedAt,
2175
- reason: input.reason,
2176
- ...input.feedback === void 0 ? {} : { feedback: input.feedback }
2177
- }).value
2178
- );
2179
- }
2180
- function createAgentImprovementActivation(inputProposal, inputReview, options) {
2181
- const proposal = verifyAgentImprovementProposal(inputProposal);
2182
- const review = verifyAgentImprovementReview(inputReview);
2183
- if (review.decision !== "approve" || review.proposalDigest !== proposal.digest) {
2184
- throw new Error("candidate activation requires an approval for the exact proposal");
2185
- }
2186
- if (!options.fundingOwner.trim() || !options.authorizedBy.trim()) {
2187
- throw new Error("candidate activation authority must be non-empty");
2188
- }
2189
- const experiment = proposal.evaluation.experiment;
2190
- const authorizedAt = (options.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
2191
- if (Date.parse(authorizedAt) < Date.parse(review.reviewedAt)) {
2192
- throw new Error("candidate activation cannot predate its approval");
2193
- }
2194
- const targets = buildAgentImprovementActivationTargets(
2195
- proposal.changedSurfaces,
2196
- experiment,
2197
- options.intent,
2198
- options.targets
2199
- );
2200
- return agentImprovementActivationSchema.parse(
2201
- canonicalCandidateDocument({
2202
- kind: "agent-improvement-activation",
2203
- proposalDigest: proposal.digest,
2204
- reviewDigest: review.digest,
2205
- experimentDigest: experiment.digest,
2206
- candidateBundleDigest: experiment.candidate.digest,
2207
- intent: options.intent,
2208
- targets,
2209
- fundingOwner: options.fundingOwner,
2210
- authorizedBy: options.authorizedBy,
2211
- authorizedAt,
2212
- expiresAt: options.expiresAt
2213
- }).value
2214
- );
2215
- }
2216
- function verifyAgentImprovementProposal(input) {
2217
- const proposal = verifyCanonicalCandidateDocument(
2218
- agentImprovementProposalSchema.parse(input),
2219
- "agent improvement proposal"
2220
- );
2221
- const evaluation = verifyCandidateExperimentComparison(proposal.evaluation);
2222
- if (evaluation.decision.outcome !== "ship") {
2223
- throw new Error("agent improvement proposal does not contain a passing experiment");
2224
- }
2225
- if (proposal.runId !== evaluation.provenance.runId) {
2226
- throw new Error("proposal runId does not match its measured experiment");
2227
- }
2228
- const changedSurfaces = deriveChangedSurfaces(
2229
- evaluation.experiment.baseline,
2230
- evaluation.experiment.candidate
2231
- );
2232
- if (!sameOrderedValues(proposal.changedSurfaces, changedSurfaces)) {
2233
- throw new Error("proposal changed surfaces do not match its exact experiment");
2234
- }
2235
- assertNoJudgeDerivedProposalFindings(proposal.findings);
2236
- return proposal;
2237
- }
2238
- function verifyAgentImprovementReview(input) {
2239
- return verifyCanonicalCandidateDocument(
2240
- agentImprovementReviewSchema.parse(input),
2241
- "agent improvement review"
2242
- );
2243
- }
2244
- function verifyAgentImprovementActivation(input) {
2245
- const proposal = verifyAgentImprovementProposal(input.proposal);
2246
- const review = verifyAgentImprovementReview(input.review);
2247
- const activation = verifyCanonicalCandidateDocument(
2248
- agentImprovementActivationSchema.parse(input.activation),
2249
- "agent improvement activation"
2250
- );
2251
- const experiment = proposal.evaluation.experiment;
2252
- if (review.decision !== "approve" || review.proposalDigest !== proposal.digest || activation.proposalDigest !== proposal.digest || activation.reviewDigest !== review.digest || activation.experimentDigest !== experiment.digest || activation.candidateBundleDigest !== experiment.candidate.digest || Date.parse(review.reviewedAt) < Date.parse(proposal.proposedAt) || Date.parse(activation.authorizedAt) < Date.parse(review.reviewedAt)) {
2253
- throw new Error("candidate activation does not bind the measured and approved candidate");
2254
- }
2255
- assertAgentImprovementActivationTargets(
2256
- proposal.changedSurfaces,
2257
- experiment,
2258
- activation.intent,
2259
- activation.targets
2260
- );
2261
- return activation;
2262
- }
2263
- function verifyCandidateExecutionEvidence(input, options) {
2264
- const experiment = verifyCandidateExperiment(options.experiment);
2265
- const bundle = experiment[options.arm];
2266
- const task = experiment.benchmark.tasks[options.benchmarkCell.taskIndex];
2267
- const index = options.benchmarkCell.taskIndex * experiment.benchmark.suite.reps + options.benchmarkCell.repetition;
2268
- if (!task || options.benchmarkCell.suiteDigest !== experiment.benchmark.suite.digest || options.seed !== experiment.benchmark.suite.seeds[index]) {
2269
- throw new Error("candidate execution evidence points outside its signed experiment");
2270
- }
2271
- const evidence = verifyCanonicalCandidateDocument(
2272
- candidateExecutionEvidenceSchema.parse(input),
2273
- "candidate execution evidence"
2274
- );
2275
- const materialization = verifyCanonicalCandidateDocument(
2276
- agentCandidateMaterializationReceiptSchema.parse(evidence.materializationReceipt),
2277
- "candidate materialization receipt"
2278
- );
2279
- const receipt = verifyCanonicalCandidateDocument(
2280
- agentCandidateRunReceiptSchema.parse(evidence.receipt),
2281
- "candidate run receipt"
2282
- );
2283
- const plan = materialization.executionPlan;
2284
- const cell = plan.material.runCell;
2285
- const attempt = options.attempt ?? 1;
2286
- if (cell.experimentDigest !== experiment.digest || cell.arm !== options.arm || cell.bundleDigest !== bundle.digest || cell.suiteDigest !== experiment.benchmark.suite.digest || cell.taskDigest !== task.digest || cell.taskIndex !== options.benchmarkCell.taskIndex || cell.repetition !== options.benchmarkCell.repetition || cell.seed !== options.seed || cell.attempt !== attempt || canonicalCandidateDigest(omitTopLevelDigest(cell)) !== cell.digest) {
2287
- throw new Error("candidate execution receipt substituted its signed experiment cell");
2288
- }
2289
- assertCapturedInput(
2290
- materialization.benchmark.suite,
2291
- experiment.benchmark.suite,
2292
- "benchmark suite"
2293
- );
2294
- assertCapturedInput(materialization.benchmark.task, task, "benchmark task");
2295
- assertEvidenceMaterialDigest(plan, "candidate execution plan");
2296
- assertEvidenceMaterialDigest(
2297
- materialization.profileActivation.profilePlan,
2298
- "candidate profile plan"
2299
- );
2300
- const expectedProfilePlan = materializeCandidateProfile(
2301
- bundle.profile,
2302
- candidateMaterializerHarness(materialization.harness),
2303
- { resolvedResources: options.resolvedResources }
2304
- );
2305
- const activation = parseAgentCandidateProfileActivation(
2306
- materialization.profileActivation,
2307
- materialization.profileActivation.profilePlan.digest
2308
- );
2309
- const regeneratedActivation = createAgentCandidateProfileActivation(
2310
- expectedProfilePlan,
2311
- materialization.profileActivation.profilePlan
2312
- );
2313
- if (activation.digest !== regeneratedActivation.digest) {
2314
- throw new Error("candidate profile activation does not match the experiment bundle");
2315
- }
2316
- if (materialization.bundleDigest !== bundle.digest || receipt.bundleDigest !== bundle.digest || receipt.runCellDigest !== cell.digest || receipt.materializationReceiptDigest !== materialization.digest || receipt.executionPlanDigest !== plan.digest) {
2317
- throw new Error("candidate execution evidence does not bind one exact Runtime run");
2318
- }
2319
- assertEvidenceMaterialDigest(receipt.modelSettlement, "candidate model settlement");
2320
- assertEvidenceMaterialDigest(receipt.taskOutcome, "candidate task outcome");
2321
- assertEvidenceMaterialDigest(receipt.benchmarkResult, "candidate benchmark result");
2322
- return immutableCandidateValue(evidence);
2323
- }
2324
- function assertExactExperimentInput(input, experiment, bundle) {
2325
- const task = experiment.benchmark.tasks[input.benchmarkCell.taskIndex];
2326
- const index = input.benchmarkCell.taskIndex * experiment.benchmark.suite.reps + input.benchmarkCell.repetition;
2327
- if (input.experiment.digest !== experiment.digest || input.bundle.digest !== bundle.digest || !task || input.task.digest !== task.digest || input.benchmarkCell.suiteDigest !== experiment.benchmark.suite.digest || input.seed !== experiment.benchmark.suite.seeds[index]) {
2328
- throw new Error("Runtime received a substituted candidate experiment cell");
2329
- }
2330
- }
2331
- function assertCapturedInput(captured, expected, label) {
2332
- const bytes = canonicalCandidateBytes(omitTopLevelDigest(expected));
2333
- if (captured.digest !== expected.digest || captured.material.sha256 !== expected.digest || captured.material.byteLength !== bytes.byteLength) {
2334
- throw new Error(`candidate materialization substituted its ${label}`);
2335
- }
2336
- }
2337
- function assertEvidenceMaterialDigest(evidence, label) {
2338
- const bytes = canonicalCandidateBytes(evidence.material);
2339
- if (canonicalCandidateDigest(evidence.material) !== evidence.digest || evidence.artifact.sha256 !== evidence.digest || evidence.artifact.byteLength !== bytes.byteLength) {
2340
- throw new Error(`${label} digest does not match its canonical material`);
2341
- }
2342
- }
2343
- function sameOrderedValues(left, right) {
2344
- return left.length === right.length && left.every((value, index) => value === right[index]);
2345
- }
2346
- function assertNoJudgeDerivedProposalFindings(findings) {
2347
- const leaked = findings.filter((finding) => finding.derived_from_judge === true);
2348
- if (leaked.length === 0) return;
2349
- const identifiers = leaked.map(
2350
- (finding) => typeof finding.finding_id === "string" ? finding.finding_id : "<unknown>"
2351
- );
2352
- throw new Error(
2353
- `agent improvement proposal findings: judge-derived findings cannot steer an improvement: [${identifiers.join(", ")}]`
2354
- );
2355
- }
2356
-
2357
- export {
2358
- AGENTIC_PROFILE_RESOURCE_ROOT,
2359
- agenticGenerator,
2360
- defaultBuildPrompt,
2361
- rawTraceEvidenceProblem,
2362
- requiresRawTraceEvidence,
2363
- commandVerifier,
2364
- summarizeFindings,
2365
- worktreeChangedPaths,
2366
- LIFTED_FINDING_ANALYST_ID,
2367
- isAnalystFinding,
2368
- toAnalystFindings,
2369
- improvementDriver,
2370
- rawTraceDistiller,
2371
- ROLLOUT_POLICY_EXTENSION,
2372
- ROLLOUT_POLICY_BOUNDS,
2373
- parseRolloutPolicy,
2374
- normalizeRolloutPolicy,
2375
- serializeRolloutPolicy,
2376
- structuralRolloutPolicyFromProfile,
2377
- applyRolloutPolicyToProfile,
2378
- enumerateNeighborPolicies,
2379
- rolloutPolicyProposer,
2380
- improve,
2381
- AGENT_IMPROVEMENT_PROFILE_SURFACES,
2382
- buildAgentImprovementActivationTargets,
2383
- agentImprovementTargetDigest,
2384
- agentImprovementTargetInput,
2385
- isAgentImprovementProfileSurface,
2386
- agentImprovementProfileSurfaceInput,
2387
- agentImprovementProfileSurfaceDigest,
2388
- agentImprovementTargetProfileDiffs,
2389
- AgentCandidateExperimentCellExecutionError,
2390
- runAgentCandidateExperiment,
2391
- executeAgentCandidateExperimentCell,
2392
- createAgentImprovementMeasuredComparison,
2393
- proposeAgentImprovement,
2394
- createAgentImprovementProposal,
2395
- reviewAgentImprovementProposal,
2396
- createAgentImprovementActivation,
2397
- verifyAgentImprovementProposal,
2398
- verifyAgentImprovementReview,
2399
- verifyAgentImprovementActivation,
2400
- verifyCandidateExecutionEvidence
2401
- };
2402
- //# sourceMappingURL=chunk-FJI3EXU3.js.map