@bastani/atomic 0.9.16-alpha.6 → 0.9.16-alpha.8

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 (214) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/builtin/intercom/CHANGELOG.md +6 -0
  3. package/dist/builtin/intercom/README.md +4 -4
  4. package/dist/builtin/intercom/broker/broker.ts +617 -22
  5. package/dist/builtin/intercom/broker/client-message-validation.ts +9 -0
  6. package/dist/builtin/intercom/broker/client.ts +200 -10
  7. package/dist/builtin/intercom/broker/pending-send-registry.ts +4 -0
  8. package/dist/builtin/intercom/broker/send-handler.ts +90 -36
  9. package/dist/builtin/intercom/broker/send-signature.ts +20 -8
  10. package/dist/builtin/intercom/group.ts +85 -0
  11. package/dist/builtin/intercom/index.bundle.mjs +680 -53
  12. package/dist/builtin/intercom/package.json +1 -1
  13. package/dist/builtin/intercom/session-target.ts +54 -0
  14. package/dist/builtin/intercom/skills/intercom/SKILL.md +18 -3
  15. package/dist/builtin/intercom/source-ownership.ts +31 -0
  16. package/dist/builtin/intercom/types.ts +115 -0
  17. package/dist/builtin/mcp/index.bundle.mjs +109 -35
  18. package/dist/builtin/mcp/package.json +1 -1
  19. package/dist/builtin/subagents/CHANGELOG.md +6 -0
  20. package/dist/builtin/subagents/package.json +1 -1
  21. package/dist/builtin/subagents/skills/qlty/SKILL.md +123 -0
  22. package/dist/builtin/subagents/skills/qlty/references/coding-with-ai-agents.md +64 -0
  23. package/dist/builtin/subagents/skills/qlty/references/commands.md +291 -0
  24. package/dist/builtin/subagents/skills/qlty/references/plugins-and-extensions.md +228 -0
  25. package/dist/builtin/subagents/skills/qlty/references/quickstart.md +110 -0
  26. package/dist/builtin/subagents/src/extension/index.bundle.mjs +173 -51
  27. package/dist/builtin/web-access/index.bundle.mjs +109 -35
  28. package/dist/builtin/web-access/package.json +1 -1
  29. package/dist/builtin/workflows/CHANGELOG.md +23 -0
  30. package/dist/builtin/workflows/README.md +40 -37
  31. package/dist/builtin/workflows/builtin/adversarial-verification.js +13 -0
  32. package/dist/builtin/workflows/builtin/{shared-prompts.ts → chunk-0x6e303p.js} +137 -133
  33. package/dist/builtin/workflows/builtin/chunk-29wrp38a.js +199 -0
  34. package/dist/builtin/workflows/builtin/chunk-2dqb5s2q.js +602 -0
  35. package/dist/builtin/workflows/builtin/chunk-4febxsv4.js +264 -0
  36. package/dist/builtin/workflows/builtin/chunk-5n10x7j2.js +69730 -0
  37. package/dist/builtin/workflows/builtin/chunk-5wgwscd0.js +4 -0
  38. package/dist/builtin/workflows/builtin/chunk-6fqs7c01.js +155 -0
  39. package/dist/builtin/workflows/builtin/chunk-7430zyas.js +126 -0
  40. package/dist/builtin/workflows/builtin/chunk-7at6dnkr.js +33 -0
  41. package/dist/builtin/workflows/builtin/chunk-82ha8p41.js +175 -0
  42. package/dist/builtin/workflows/builtin/chunk-bfkmzv9h.js +156 -0
  43. package/dist/builtin/workflows/builtin/chunk-c53y8bdh.js +345 -0
  44. package/dist/builtin/workflows/builtin/chunk-cdtd3m3w.js +529 -0
  45. package/dist/builtin/workflows/builtin/chunk-cg9tmks0.js +206 -0
  46. package/dist/builtin/workflows/builtin/chunk-hdpj1dkw.js +2447 -0
  47. package/dist/builtin/workflows/builtin/chunk-hzzn6adg.js +221 -0
  48. package/dist/builtin/workflows/builtin/chunk-nqr34qp3.js +1061 -0
  49. package/dist/builtin/workflows/builtin/chunk-qwzvgxnq.js +404 -0
  50. package/dist/builtin/workflows/builtin/chunk-qx2ptjs3.js +215 -0
  51. package/dist/builtin/workflows/builtin/chunk-v04c5qmd.js +1207 -0
  52. package/dist/builtin/workflows/builtin/chunk-wpckd35c.js +12 -0
  53. package/dist/builtin/workflows/builtin/chunk-x2ghn0je.js +4110 -0
  54. package/dist/builtin/workflows/builtin/chunk-zf28603f.js +1080 -0
  55. package/dist/builtin/workflows/builtin/classify-and-act.js +12 -0
  56. package/dist/builtin/workflows/builtin/fan-out-and-synthesize.js +10 -0
  57. package/dist/builtin/workflows/builtin/generate-and-filter.js +12 -0
  58. package/dist/builtin/workflows/builtin/goal.js +16 -0
  59. package/dist/builtin/workflows/builtin/index.js +49 -0
  60. package/dist/builtin/workflows/builtin/loop-until-done.js +12 -0
  61. package/dist/builtin/workflows/builtin/open-claude-design.js +11 -0
  62. package/dist/builtin/workflows/builtin/ralph.js +16 -0
  63. package/dist/builtin/workflows/builtin/steering-context.d.ts +6 -0
  64. package/dist/builtin/workflows/builtin/steering-context.js +7 -0
  65. package/dist/builtin/workflows/builtin/tournament.js +13 -0
  66. package/dist/builtin/workflows/package.json +10 -7
  67. package/dist/builtin/workflows/src/authoring/typebox-defaults.d.ts +41 -0
  68. package/dist/builtin/workflows/src/extension/index.bundle.mjs +1250 -428
  69. package/dist/builtin/workflows/src/{index.bundle.mjs → index.js} +956 -427
  70. package/dist/builtin/workflows/src/shared/authoring-contract-stage.d.ts +403 -0
  71. package/dist/builtin/workflows/src/shared/authoring-contract-ui.d.ts +327 -0
  72. package/dist/builtin/workflows/src/shared/budget.d.ts +67 -0
  73. package/dist/builtin/workflows/src/shared/workflow-authoring-types.d.ts +60 -0
  74. package/dist/builtin/workflows/src/shared/workflow-heartbeat-contract.d.ts +21 -0
  75. package/dist/core/agent-session-custom-message-commit.d.ts.map +1 -1
  76. package/dist/core/agent-session-custom-message-commit.js +12 -5
  77. package/dist/core/agent-session-custom-message-commit.js.map +1 -1
  78. package/dist/core/agent-session-persistent-custom-messages.d.ts.map +1 -1
  79. package/dist/core/agent-session-persistent-custom-messages.js +2 -0
  80. package/dist/core/agent-session-persistent-custom-messages.js.map +1 -1
  81. package/dist/core/agent-session.d.ts.map +1 -1
  82. package/dist/core/agent-session.js +2 -1
  83. package/dist/core/agent-session.js.map +1 -1
  84. package/dist/core/auth-storage.d.ts +2 -0
  85. package/dist/core/auth-storage.d.ts.map +1 -1
  86. package/dist/core/auth-storage.js +4 -0
  87. package/dist/core/auth-storage.js.map +1 -1
  88. package/dist/core/builtin-install-layout.d.ts +1 -1
  89. package/dist/core/builtin-install-layout.d.ts.map +1 -1
  90. package/dist/core/builtin-install-layout.js +1 -1
  91. package/dist/core/builtin-install-layout.js.map +1 -1
  92. package/dist/core/codex-fast-mode.d.ts +10 -7
  93. package/dist/core/codex-fast-mode.d.ts.map +1 -1
  94. package/dist/core/codex-fast-mode.js +31 -13
  95. package/dist/core/codex-fast-mode.js.map +1 -1
  96. package/dist/core/extensions/context-types.d.ts +35 -0
  97. package/dist/core/extensions/context-types.d.ts.map +1 -1
  98. package/dist/core/extensions/context-types.js.map +1 -1
  99. package/dist/core/extensions/index.d.ts +1 -1
  100. package/dist/core/extensions/index.d.ts.map +1 -1
  101. package/dist/core/extensions/index.js.map +1 -1
  102. package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
  103. package/dist/core/extensions/loader-virtual-modules.js +6 -1
  104. package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
  105. package/dist/core/model-runtime.d.ts +2 -0
  106. package/dist/core/model-runtime.d.ts.map +1 -1
  107. package/dist/core/model-runtime.js +4 -0
  108. package/dist/core/model-runtime.js.map +1 -1
  109. package/dist/core/runtime-credentials.d.ts +1 -0
  110. package/dist/core/runtime-credentials.d.ts.map +1 -1
  111. package/dist/core/runtime-credentials.js +8 -0
  112. package/dist/core/runtime-credentials.js.map +1 -1
  113. package/dist/core/sdk.d.ts.map +1 -1
  114. package/dist/core/sdk.js +5 -5
  115. package/dist/core/sdk.js.map +1 -1
  116. package/dist/core/session-manager-storage.d.ts +2 -0
  117. package/dist/core/session-manager-storage.d.ts.map +1 -1
  118. package/dist/core/session-manager-storage.js +4 -2
  119. package/dist/core/session-manager-storage.js.map +1 -1
  120. package/dist/core/slash-commands.js +1 -1
  121. package/dist/core/slash-commands.js.map +1 -1
  122. package/dist/core/workflow-stage-admission.d.ts +6 -1
  123. package/dist/core/workflow-stage-admission.d.ts.map +1 -1
  124. package/dist/core/workflow-stage-admission.js +8 -9
  125. package/dist/core/workflow-stage-admission.js.map +1 -1
  126. package/dist/index.d.ts +0 -1
  127. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  128. package/dist/modes/interactive/components/footer.js +1 -1
  129. package/dist/modes/interactive/components/footer.js.map +1 -1
  130. package/dist/modes/interactive/interactive-autocomplete.js +1 -1
  131. package/dist/modes/interactive/interactive-autocomplete.js.map +1 -1
  132. package/dist/modes/interactive/interactive-startup.js +1 -1
  133. package/dist/modes/interactive/interactive-startup.js.map +1 -1
  134. package/dist/modes/interactive-engine/isolated-runtime.d.ts +4 -0
  135. package/dist/modes/interactive-engine/isolated-runtime.d.ts.map +1 -1
  136. package/dist/modes/interactive-engine/isolated-runtime.js +69 -10
  137. package/dist/modes/interactive-engine/isolated-runtime.js.map +1 -1
  138. package/dist/modes/rpc/rpc-client-api.d.ts +18 -2
  139. package/dist/modes/rpc/rpc-client-api.d.ts.map +1 -1
  140. package/dist/modes/rpc/rpc-client-api.js +33 -5
  141. package/dist/modes/rpc/rpc-client-api.js.map +1 -1
  142. package/dist/modes/rpc/rpc-command-handler.d.ts.map +1 -1
  143. package/dist/modes/rpc/rpc-command-handler.js +8 -4
  144. package/dist/modes/rpc/rpc-command-handler.js.map +1 -1
  145. package/dist/modes/rpc/rpc-types.d.ts +13 -0
  146. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  147. package/dist/modes/rpc/rpc-types.js.map +1 -1
  148. package/docs/intercom.md +15 -6
  149. package/docs/packages.md +2 -2
  150. package/docs/providers.md +8 -3
  151. package/docs/quickstart.md +3 -2
  152. package/docs/rpc.md +21 -4
  153. package/docs/settings.md +6 -4
  154. package/docs/skills.md +4 -0
  155. package/docs/subagents.md +1 -1
  156. package/docs/usage.md +1 -1
  157. package/docs/workflows.md +112 -239
  158. package/npm-shrinkwrap.json +32 -32
  159. package/package.json +9 -13
  160. package/dist/builtin/workflows/ambient.d.ts +0 -61
  161. package/dist/builtin/workflows/builtin/adversarial-verification-prompts.ts +0 -26
  162. package/dist/builtin/workflows/builtin/adversarial-verification-runner.ts +0 -391
  163. package/dist/builtin/workflows/builtin/adversarial-verification.ts +0 -34
  164. package/dist/builtin/workflows/builtin/classify-and-act-prompts.ts +0 -14
  165. package/dist/builtin/workflows/builtin/classify-and-act-runner.ts +0 -108
  166. package/dist/builtin/workflows/builtin/classify-and-act.ts +0 -40
  167. package/dist/builtin/workflows/builtin/fan-out-and-synthesize-prompts.ts +0 -14
  168. package/dist/builtin/workflows/builtin/fan-out-and-synthesize-runner.ts +0 -96
  169. package/dist/builtin/workflows/builtin/fan-out-and-synthesize.ts +0 -37
  170. package/dist/builtin/workflows/builtin/generate-and-filter-prompts.ts +0 -41
  171. package/dist/builtin/workflows/builtin/generate-and-filter-runner.ts +0 -90
  172. package/dist/builtin/workflows/builtin/generate-and-filter.ts +0 -31
  173. package/dist/builtin/workflows/builtin/goal-artifacts.ts +0 -64
  174. package/dist/builtin/workflows/builtin/goal-convergence.ts +0 -87
  175. package/dist/builtin/workflows/builtin/goal-ledger.ts +0 -139
  176. package/dist/builtin/workflows/builtin/goal-models.ts +0 -72
  177. package/dist/builtin/workflows/builtin/goal-orchestrator-prompts.ts +0 -94
  178. package/dist/builtin/workflows/builtin/goal-prompts.ts +0 -211
  179. package/dist/builtin/workflows/builtin/goal-reducer.ts +0 -176
  180. package/dist/builtin/workflows/builtin/goal-reports.ts +0 -76
  181. package/dist/builtin/workflows/builtin/goal-reverify.ts +0 -305
  182. package/dist/builtin/workflows/builtin/goal-review.ts +0 -139
  183. package/dist/builtin/workflows/builtin/goal-runner.ts +0 -504
  184. package/dist/builtin/workflows/builtin/goal-schemas.ts +0 -89
  185. package/dist/builtin/workflows/builtin/goal-types.ts +0 -164
  186. package/dist/builtin/workflows/builtin/goal.ts +0 -78
  187. package/dist/builtin/workflows/builtin/index.ts +0 -16
  188. package/dist/builtin/workflows/builtin/loop-until-done-prompts.ts +0 -87
  189. package/dist/builtin/workflows/builtin/loop-until-done-runner.ts +0 -282
  190. package/dist/builtin/workflows/builtin/loop-until-done.ts +0 -51
  191. package/dist/builtin/workflows/builtin/open-claude-design-live-protocol.ts +0 -305
  192. package/dist/builtin/workflows/builtin/open-claude-design-phases.ts +0 -337
  193. package/dist/builtin/workflows/builtin/open-claude-design-runner.ts +0 -347
  194. package/dist/builtin/workflows/builtin/open-claude-design-setup.ts +0 -313
  195. package/dist/builtin/workflows/builtin/open-claude-design-utils.ts +0 -311
  196. package/dist/builtin/workflows/builtin/open-claude-design.ts +0 -57
  197. package/dist/builtin/workflows/builtin/pattern-artifact-root.ts +0 -28
  198. package/dist/builtin/workflows/builtin/progress-scoring.ts +0 -230
  199. package/dist/builtin/workflows/builtin/ralph-core.ts +0 -449
  200. package/dist/builtin/workflows/builtin/ralph-forked-prompts.ts +0 -100
  201. package/dist/builtin/workflows/builtin/ralph-models.ts +0 -183
  202. package/dist/builtin/workflows/builtin/ralph-review-gate.ts +0 -103
  203. package/dist/builtin/workflows/builtin/ralph-reviewer-prompt.ts +0 -100
  204. package/dist/builtin/workflows/builtin/ralph-runner.ts +0 -455
  205. package/dist/builtin/workflows/builtin/ralph.ts +0 -82
  206. package/dist/builtin/workflows/builtin/review-convergence.ts +0 -229
  207. package/dist/builtin/workflows/builtin/selection-math.ts +0 -156
  208. package/dist/builtin/workflows/builtin/steering-context.ts +0 -51
  209. package/dist/builtin/workflows/builtin/tournament-prompts.ts +0 -70
  210. package/dist/builtin/workflows/builtin/tournament-runner.ts +0 -400
  211. package/dist/builtin/workflows/builtin/tournament.ts +0 -73
  212. package/dist/builtin/workflows/builtin/verification-criteria.ts +0 -330
  213. package/dist/builtin/workflows/builtin/verification-prompts.ts +0 -206
  214. package/dist/builtin/workflows/builtin/verification-usage.ts +0 -44
@@ -0,0 +1,602 @@
1
+ import {
2
+ fold_usage
3
+ } from "./chunk-7at6dnkr.js";
4
+ import {
5
+ build_scoring_prompt,
6
+ scoring_prompt_reads,
7
+ warm_first_fan_out
8
+ } from "./chunk-7430zyas.js";
9
+ import {
10
+ stableArtifactRoot
11
+ } from "./chunk-wpckd35c.js";
12
+ import {
13
+ VERIFICATION_SCALE,
14
+ normalize_criteria,
15
+ parse_rubric
16
+ } from "./chunk-cg9tmks0.js";
17
+ import {
18
+ workflow
19
+ } from "./chunk-bfkmzv9h.js";
20
+ import {
21
+ withSteeringPropagationContext
22
+ } from "./chunk-0x6e303p.js";
23
+
24
+ // dist/builtin/workflows/builtin/tournament.ts
25
+ import { Type as Type2 } from "typebox";
26
+
27
+ // dist/builtin/workflows/builtin/tournament-runner.ts
28
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
29
+ import { join, isAbsolute, relative, resolve } from "node:path";
30
+ import { Type } from "typebox";
31
+
32
+ // dist/builtin/workflows/builtin/selection-math.ts
33
+ var UINT32_RANGE = 4294967296;
34
+ var MULBERRY_INCREMENT = 1831565813;
35
+ function seeded_rng(seed) {
36
+ let state = seed >>> 0;
37
+ return () => {
38
+ state = state + MULBERRY_INCREMENT >>> 0;
39
+ let t = state;
40
+ t = Math.imul(t ^ t >>> 15, t | 1);
41
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
42
+ return ((t ^ t >>> 14) >>> 0) / UINT32_RANGE;
43
+ };
44
+ }
45
+ function ring_cycle(n, rng) {
46
+ const permutation = Array.from({ length: n }, (_, index) => index);
47
+ for (let index = n - 1;index > 0; index -= 1) {
48
+ const other = Math.floor(rng() * (index + 1));
49
+ [permutation[index], permutation[other]] = [permutation[other], permutation[index]];
50
+ }
51
+ return permutation.map((a, index) => ({ a, b: permutation[(index + 1) % n] }));
52
+ }
53
+ function pair_key(a, b) {
54
+ return a < b ? `${a}:${b}` : `${b}:${a}`;
55
+ }
56
+ function plan_comparisons(input) {
57
+ const { n, pivots: pivotCount, repeats, seed } = input;
58
+ if (n < 2)
59
+ throw new RangeError("n must be at least 2");
60
+ if (pivotCount < 1)
61
+ throw new RangeError("pivots must be at least 1");
62
+ if (repeats < 1)
63
+ throw new RangeError("repeats must be at least 1");
64
+ const ring = ring_cycle(n, seeded_rng(seed));
65
+ return {
66
+ ring,
67
+ pivotRounds: (pivots) => {
68
+ const scheduled = new Set(ring.map(({ a, b }) => pair_key(a, b)));
69
+ const pivotSet = new Set(pivots);
70
+ const pairs = [];
71
+ const add = (a, b) => {
72
+ if (a === b)
73
+ return;
74
+ const key = pair_key(a, b);
75
+ if (scheduled.has(key))
76
+ return;
77
+ scheduled.add(key);
78
+ pairs.push({ a, b });
79
+ };
80
+ for (let candidate = 0;candidate < n; candidate += 1) {
81
+ if (pivotSet.has(candidate))
82
+ continue;
83
+ for (const pivot of pivots)
84
+ add(candidate, pivot);
85
+ }
86
+ for (let first = 0;first < pivots.length; first += 1) {
87
+ for (let second = first + 1;second < pivots.length; second += 1) {
88
+ add(pivots[first], pivots[second]);
89
+ }
90
+ }
91
+ return pairs;
92
+ },
93
+ jobs: (pairs, criterionIds) => {
94
+ const jobs = [];
95
+ for (const pair of pairs) {
96
+ for (const criterionId of criterionIds) {
97
+ for (let rep = 0;rep < repeats; rep += 1) {
98
+ jobs.push({ a: pair.a, b: pair.b, criterionId, rep, swapped: rep % 2 === 1 });
99
+ }
100
+ }
101
+ }
102
+ return jobs;
103
+ }
104
+ };
105
+ }
106
+ function normalize_score(score) {
107
+ return (score - VERIFICATION_SCALE.min) / (VERIFICATION_SCALE.max - VERIFICATION_SCALE.min);
108
+ }
109
+ function soft_win(scoreA, scoreB) {
110
+ const difference = normalize_score(scoreA) - normalize_score(scoreB);
111
+ return 1 / (1 + Math.exp(-difference));
112
+ }
113
+ function accumulate(prefs, w, c) {
114
+ for (const { a, b, p } of prefs) {
115
+ w[a] += p;
116
+ c[a] += 1;
117
+ w[b] += 1 - p;
118
+ c[b] += 1;
119
+ }
120
+ }
121
+ function mean_preference(w, c, index) {
122
+ return c[index] === 0 ? 0 : w[index] / c[index];
123
+ }
124
+ function candidate_order(w, c) {
125
+ return Array.from({ length: w.length }, (_, index) => index).sort((left, right) => {
126
+ const difference = mean_preference(w, c, right) - mean_preference(w, c, left);
127
+ return difference === 0 ? left - right : difference;
128
+ });
129
+ }
130
+ function select_pivots(w, c, k) {
131
+ return candidate_order(w, c).slice(0, k);
132
+ }
133
+ function rank_candidates(w, c) {
134
+ return candidate_order(w, c).map((index) => ({ index, meanPreference: mean_preference(w, c, index) }));
135
+ }
136
+
137
+ // dist/builtin/workflows/builtin/tournament-prompts.ts
138
+ var GROUNDED_REPORTING = "Before reporting progress, audit each claim against a tool result from this session. Report only work you can point to evidence for; say so explicitly when something is unverified.";
139
+ var READABLE_REPORT = "Lead with the outcome. Keep facts, decisions, caveats, and next steps; drop background and repetition. Use complete, readable sentences rather than compressed fragments.";
140
+ var DEFAULT_TOURNAMENT_CRITERIA = [
141
+ { name: "Correctness", description: "Satisfies the task without material errors." },
142
+ { name: "Completeness", description: "Covers required outcomes and important edge cases." },
143
+ {
144
+ name: "Evidence and task fit",
145
+ description: "Supports claims with observable evidence or checks and is directly usable without irrelevant work."
146
+ }
147
+ ];
148
+ function taggedPrompt(sections) {
149
+ return sections.map(([tag, content]) => `<${tag}>
150
+ ${content.trim()}
151
+ </${tag}>`).join(`
152
+
153
+ `);
154
+ }
155
+ function renderTournamentAttemptPrompt(task, attempt) {
156
+ return taggedPrompt([
157
+ ["role", "You are an independent solution author competing on solution quality."],
158
+ ["attempt", `Produce attempt ${attempt} without assuming another attempt's approach or conclusions.`],
159
+ ["success_criteria", "A judge can evaluate this artifact directly against correctness, completeness, evidence, and task fit."],
160
+ ["requirements", [
161
+ "Deliver a complete, self-contained solution rather than commentary about how to solve it.",
162
+ "Ground important claims in observable evidence or executable checks.",
163
+ "State assumptions, limitations, and validation performed.",
164
+ "Optimize for correctness and usefulness, not length.",
165
+ GROUNDED_REPORTING
166
+ ].join(`
167
+ `)],
168
+ ["stop_rules", "Stop when the solution is complete, supported, validated where practical, and its residual risks are stated."],
169
+ ["output_format", `Markdown with Solution, Evidence and validation, Assumptions, and Residual risks. ${READABLE_REPORT}`],
170
+ ["objective", task]
171
+ ]);
172
+ }
173
+ function renderComparisonsReducerPrompt(options) {
174
+ const rankingText = options.ranking.map((entry, index) => `${index + 1}. ${entry.label} (mean preference ${entry.meanPreference})`).join(`
175
+ `);
176
+ return taggedPrompt([
177
+ ["artifacts", [
178
+ `Comparisons ledger: ${options.comparisonsPath}`,
179
+ `Winning artifact (${options.winnerLabel}): ${options.winnerPath}`,
180
+ "Read the ledger and winning artifact before reporting."
181
+ ].join(`
182
+ `)],
183
+ ["role", "You are the soft-scored tournament reducer and final reporter."],
184
+ ["ranking", rankingText],
185
+ ["requirements", [
186
+ "Return the winning solution faithfully; do not silently combine losing material into it.",
187
+ "Report the full ranking exactly as recorded in the comparisons ledger.",
188
+ "Identify notable score disagreements, invalid reports, or other limitations recorded by judges.",
189
+ "Cite the comparisons ledger and winning artifact paths.",
190
+ GROUNDED_REPORTING
191
+ ].join(`
192
+ `)],
193
+ ["success_criteria", "A reader can use the winning solution and recompute the ranking from the durable comparisons ledger."],
194
+ ["stop_rules", "Stop after faithfully presenting the winner, full ranking, decision evidence, and residual risks."],
195
+ ["output_format", `Markdown with Winner, Full ranking, Decision trail, Evidence, Notable disagreements, and Residual risks. ${READABLE_REPORT}`],
196
+ ["objective", options.task]
197
+ ]);
198
+ }
199
+
200
+ // dist/builtin/workflows/builtin/tournament-runner.ts
201
+ var judgeScoreSchema = Type.Object({
202
+ criterion_id: Type.String(),
203
+ score_a: Type.Integer({ minimum: VERIFICATION_SCALE.min, maximum: VERIFICATION_SCALE.max }),
204
+ score_b: Type.Integer({ minimum: VERIFICATION_SCALE.min, maximum: VERIFICATION_SCALE.max }),
205
+ evidence: Type.Array(Type.String())
206
+ }, { additionalProperties: false });
207
+ var JUDGE_GROUND_TRUTH_NOTE = "No external ground truth is supplied; score each presented solution against the task and criterion.";
208
+ var JUDGE_OUTPUT_FORMAT = "Return only JSON with criterion_id (string), score_a (integer 1–20 for presented slot 1), score_b (integer 1–20 for presented slot 2), and evidence (array of strings).";
209
+ function serializableObject(value) {
210
+ if (value === null || Array.isArray(value) || typeof value !== "object")
211
+ return;
212
+ return value;
213
+ }
214
+ function stringArray(value) {
215
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
216
+ }
217
+ function judgeReport(value) {
218
+ const object = serializableObject(value);
219
+ if (object === undefined)
220
+ return;
221
+ const criterionId = object.criterion_id;
222
+ const scoreA = object.score_a;
223
+ const scoreB = object.score_b;
224
+ const evidence = object.evidence;
225
+ if (typeof criterionId !== "string" || typeof scoreA !== "number" || !Number.isInteger(scoreA) || scoreA < VERIFICATION_SCALE.min || scoreA > VERIFICATION_SCALE.max || typeof scoreB !== "number" || !Number.isInteger(scoreB) || scoreB < VERIFICATION_SCALE.min || scoreB > VERIFICATION_SCALE.max || !stringArray(evidence))
226
+ return;
227
+ return { criterion_id: criterionId, score_a: scoreA, score_b: scoreB, evidence };
228
+ }
229
+ function resultFor(results, stageName) {
230
+ return results.find((result) => result.stageName === stageName || result.name === stageName);
231
+ }
232
+ function scoredComparison(record) {
233
+ return typeof record.score_a === "number" && typeof record.score_b === "number" && record.invalid !== true;
234
+ }
235
+ function stageName(job) {
236
+ return `judge-${job.a}-${job.b}-${job.criterionId}-r${job.rep}`;
237
+ }
238
+ function criterionSlug(id) {
239
+ const slug = id.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 40).replace(/_+$/g, "");
240
+ return slug || "criterion";
241
+ }
242
+ function judgeArtifactPath(judgesDir, job, criterionIndex) {
243
+ const filename = `judge-${job.a}-${job.b}-c${criterionIndex}-${criterionSlug(job.criterionId)}-r${job.rep}.json`;
244
+ const root = resolve(judgesDir);
245
+ const path = resolve(judgesDir, filename);
246
+ const fromRoot = relative(root, path);
247
+ if (fromRoot.startsWith("..") || isAbsolute(fromRoot)) {
248
+ throw new Error(`tournament: judge artifact escaped its directory: ${path}`);
249
+ }
250
+ return path;
251
+ }
252
+ function tournamentCriteria(input) {
253
+ if (input === undefined)
254
+ return normalize_criteria(DEFAULT_TOURNAMENT_CRITERIA);
255
+ if (typeof input === "string")
256
+ return parse_rubric(input).criteria;
257
+ return normalize_criteria(input);
258
+ }
259
+ function modelAssignment(entrants, models) {
260
+ if (models === undefined)
261
+ return;
262
+ const assignment = {};
263
+ if (models.length === 0)
264
+ return assignment;
265
+ for (const [index, entrant] of entrants.entries())
266
+ assignment[entrant.label] = models[index % models.length];
267
+ return assignment;
268
+ }
269
+ function combineUsage(totals) {
270
+ const combined = {
271
+ calls: 0,
272
+ input: 0,
273
+ output: 0,
274
+ cacheRead: 0,
275
+ cacheWrite: 0,
276
+ cost: 0,
277
+ turns: 0
278
+ };
279
+ for (const total of totals) {
280
+ combined.calls += total.calls;
281
+ combined.input += total.input;
282
+ combined.output += total.output;
283
+ combined.cacheRead += total.cacheRead;
284
+ combined.cacheWrite += total.cacheWrite;
285
+ combined.cost += total.cost;
286
+ combined.turns += total.turns;
287
+ }
288
+ const cacheDenominator = combined.input + combined.cacheRead;
289
+ return {
290
+ ...combined,
291
+ cacheHitRate: cacheDenominator === 0 ? 0 : combined.cacheRead / cacheDenominator
292
+ };
293
+ }
294
+ async function runTournament(ctx) {
295
+ const artifactDir = await stableArtifactRoot(ctx, "tournament");
296
+ const attemptsDir = join(artifactDir, "attempts");
297
+ const numAttempts = ctx.inputs.num_attempts ?? 4;
298
+ const maxConcurrency = ctx.inputs.max_concurrency ?? 4;
299
+ const nEvaluations = ctx.inputs.n_evaluations ?? 2;
300
+ const pivotCount = ctx.inputs.pivots ?? 1;
301
+ const seed = ctx.inputs.seed ?? 0;
302
+ const judgesDir = join(artifactDir, "judges");
303
+ await mkdir(attemptsDir, { recursive: true });
304
+ await mkdir(judgesDir, { recursive: true });
305
+ const attemptArtifactPaths = Array.from({ length: numAttempts }, (_, index) => join(attemptsDir, `attempt-${index + 1}.md`));
306
+ const models = ctx.inputs.models;
307
+ const attemptResults = await ctx.parallel(attemptArtifactPaths.map((path, index) => ({
308
+ name: `attempt-${index + 1}`,
309
+ prompt: renderTournamentAttemptPrompt(ctx.inputs.prompt, index + 1),
310
+ context: "fresh",
311
+ output: path,
312
+ outputMode: "file-only",
313
+ ...models !== undefined && models.length > 0 ? { model: models[index % models.length] } : {}
314
+ })), { concurrency: maxConcurrency, failFast: true });
315
+ const entrants = await Promise.all(attemptArtifactPaths.map(async (path, index) => ({
316
+ label: `attempt-${index + 1}`,
317
+ path,
318
+ body: await readFile(path, "utf8")
319
+ })));
320
+ const criteria = tournamentCriteria(ctx.inputs.criteria);
321
+ const criterionIds = criteria.map((criterion) => criterion.id);
322
+ const plan = plan_comparisons({
323
+ n: entrants.length,
324
+ pivots: pivotCount,
325
+ repeats: nEvaluations,
326
+ seed
327
+ });
328
+ const weights = Array.from({ length: entrants.length }, () => 0);
329
+ const counts = Array.from({ length: entrants.length }, () => 0);
330
+ const comparisons = [];
331
+ const pairs = [];
332
+ const judgeArtifactPaths = [];
333
+ let executed = 0;
334
+ const runPhase = async (phase, jobs) => {
335
+ const stages = jobs.map((job) => {
336
+ const slot1 = job.swapped ? job.b : job.a;
337
+ const slot2 = job.swapped ? job.a : job.b;
338
+ const first = entrants[slot1];
339
+ const second = entrants[slot2];
340
+ const head = {
341
+ task: ctx.inputs.prompt,
342
+ groundTruthNote: JUDGE_GROUND_TRUTH_NOTE,
343
+ candidates: [
344
+ { path: first.path, body: first.body },
345
+ { path: second.path, body: second.body }
346
+ ],
347
+ scaleAnchors: VERIFICATION_SCALE.anchors,
348
+ outputFormat: JUDGE_OUTPUT_FORMAT
349
+ };
350
+ const name = stageName(job);
351
+ const criterionIndex = criterionIds.indexOf(job.criterionId);
352
+ const path = judgeArtifactPath(judgesDir, job, criterionIndex);
353
+ judgeArtifactPaths.push(path);
354
+ return {
355
+ job,
356
+ name,
357
+ path,
358
+ slot1,
359
+ slot2,
360
+ step: {
361
+ name,
362
+ prompt: build_scoring_prompt(head, criteria.find((criterion) => criterion.id === job.criterionId)),
363
+ context: "fresh",
364
+ reads: scoring_prompt_reads(head),
365
+ schema: judgeScoreSchema
366
+ }
367
+ };
368
+ });
369
+ if (stages.length === 0)
370
+ return { preferences: [], results: [] };
371
+ executed += stages.length;
372
+ const results = await warm_first_fan_out(ctx, stages.map((stage) => stage.step), (_step, index) => `${stages[index].slot1}:${stages[index].slot2}`, { concurrency: maxConcurrency, failFast: false });
373
+ const phaseResults = [...results];
374
+ for (const stage of stages) {
375
+ let report = judgeReport(resultFor(results, stage.name)?.structured);
376
+ if (report === undefined) {
377
+ executed += 1;
378
+ const retried = await ctx.task(`${stage.name}-reask`, {
379
+ prompt: `${stage.step.prompt}
380
+ The previous report was invalid. Return the required JSON schema exactly once.`,
381
+ context: "fresh",
382
+ reads: stage.step.reads,
383
+ schema: judgeScoreSchema
384
+ });
385
+ phaseResults.push(retried);
386
+ report = judgeReport(retried.structured);
387
+ }
388
+ if (report === undefined) {
389
+ await writeFile(stage.path, `${JSON.stringify({ invalid: true }, null, 2)}
390
+ `);
391
+ comparisons.push({
392
+ a: stage.job.a,
393
+ b: stage.job.b,
394
+ phase,
395
+ criterion_id: stage.job.criterionId,
396
+ rep: stage.job.rep,
397
+ swapped: stage.job.swapped,
398
+ invalid: true,
399
+ judge_artifact_path: stage.path
400
+ });
401
+ continue;
402
+ }
403
+ await writeFile(stage.path, `${JSON.stringify(report, null, 2)}
404
+ `);
405
+ const scoreA = stage.job.swapped ? report.score_b : report.score_a;
406
+ const scoreB = stage.job.swapped ? report.score_a : report.score_b;
407
+ comparisons.push({
408
+ a: stage.job.a,
409
+ b: stage.job.b,
410
+ phase,
411
+ criterion_id: stage.job.criterionId,
412
+ rep: stage.job.rep,
413
+ swapped: stage.job.swapped,
414
+ score_a: scoreA,
415
+ score_b: scoreB,
416
+ p_ab: soft_win(scoreA, scoreB),
417
+ judge_artifact_path: stage.path
418
+ });
419
+ }
420
+ const phaseRecords = comparisons.filter((record) => record.phase === phase);
421
+ const grouped = new Map;
422
+ for (const record of phaseRecords) {
423
+ const key = `${record.a}:${record.b}`;
424
+ const group = grouped.get(key);
425
+ if (group === undefined)
426
+ grouped.set(key, [record]);
427
+ else
428
+ group.push(record);
429
+ }
430
+ const preferences = [];
431
+ for (const records of grouped.values()) {
432
+ const first = records[0];
433
+ const valid = records.filter(scoredComparison);
434
+ if (valid.length === 0) {
435
+ pairs.push({ a: first.a, b: first.b, phase, valid_reports: 0, p_ab: 0.5, invalid: true });
436
+ preferences.push({ a: first.a, b: first.b, p: 0.5 });
437
+ continue;
438
+ }
439
+ const meanScoreA = valid.reduce((sum, record) => sum + record.score_a, 0) / valid.length;
440
+ const meanScoreB = valid.reduce((sum, record) => sum + record.score_b, 0) / valid.length;
441
+ const p = soft_win(meanScoreA, meanScoreB);
442
+ pairs.push({
443
+ a: first.a,
444
+ b: first.b,
445
+ phase,
446
+ valid_reports: valid.length,
447
+ mean_score_a: meanScoreA,
448
+ mean_score_b: meanScoreB,
449
+ p_ab: p
450
+ });
451
+ preferences.push({ a: first.a, b: first.b, p });
452
+ }
453
+ return { preferences, results: phaseResults };
454
+ };
455
+ const ringJobs = plan.jobs(plan.ring, criterionIds);
456
+ const ringPhase = await runPhase("ring", ringJobs);
457
+ accumulate(ringPhase.preferences, weights, counts);
458
+ const pivotIndices = select_pivots(weights, counts, pivotCount);
459
+ const pivotJobs = plan.jobs(plan.pivotRounds(pivotIndices), criterionIds);
460
+ const pivotPhase = await runPhase("pivot", pivotJobs);
461
+ accumulate(pivotPhase.preferences, weights, counts);
462
+ const ranking = rank_candidates(weights, counts).map((entry) => ({
463
+ label: entrants[entry.index].label,
464
+ index: entry.index,
465
+ meanPreference: entry.meanPreference
466
+ }));
467
+ const outputRanking = ranking.map(({ label, meanPreference }) => ({ label, meanPreference }));
468
+ const budgetPivots = Math.min(pivotCount, entrants.length);
469
+ const comparisonBudget = (entrants.length + budgetPivots * (entrants.length - budgetPivots) + budgetPivots * (budgetPivots - 1) / 2) * criteria.length * nEvaluations;
470
+ const comparisonPath = join(artifactDir, "comparisons.json");
471
+ const assignment = modelAssignment(entrants, models);
472
+ const phaseUsage = {
473
+ attempts: fold_usage(attemptResults),
474
+ ring: fold_usage(ringPhase.results),
475
+ pivots: fold_usage(pivotPhase.results)
476
+ };
477
+ const baseLedger = {
478
+ task: ctx.inputs.prompt,
479
+ seed,
480
+ params: {
481
+ n: entrants.length,
482
+ pivots: pivotCount,
483
+ n_evaluations: nEvaluations,
484
+ criteria
485
+ },
486
+ comparisons,
487
+ pairs,
488
+ w: weights,
489
+ c: counts,
490
+ ranking,
491
+ budget: { planned: comparisonBudget, executed },
492
+ ...assignment === undefined ? {} : { model_assignment: assignment }
493
+ };
494
+ const writeLedger = async (reducerUsage) => {
495
+ const usage = {
496
+ ...phaseUsage,
497
+ reducer: reducerUsage,
498
+ total: combineUsage([phaseUsage.attempts, phaseUsage.ring, phaseUsage.pivots, reducerUsage])
499
+ };
500
+ await writeFile(comparisonPath, `${JSON.stringify({ ...baseLedger, usage }, null, 2)}
501
+ `);
502
+ };
503
+ await writeLedger(fold_usage([]));
504
+ const winner = ranking[0];
505
+ const winnerEntrant = entrants[winner.index];
506
+ const resultPath = join(artifactDir, "winner.md");
507
+ const reducer = await ctx.task("comparisons-reducer", {
508
+ prompt: renderComparisonsReducerPrompt({
509
+ task: ctx.inputs.prompt,
510
+ comparisonsPath: comparisonPath,
511
+ ranking: outputRanking,
512
+ winnerLabel: winner.label,
513
+ winnerPath: winnerEntrant.path
514
+ }),
515
+ context: "fresh",
516
+ reads: [comparisonPath, winnerEntrant.path, ...judgeArtifactPaths],
517
+ output: resultPath,
518
+ outputMode: "file-only"
519
+ });
520
+ await writeLedger(fold_usage([reducer]));
521
+ return {
522
+ result: reducer.text,
523
+ winner: winner.label,
524
+ winner_artifact_path: winnerEntrant.path,
525
+ result_path: resultPath,
526
+ attempt_artifact_paths: attemptArtifactPaths,
527
+ judge_artifact_paths: judgeArtifactPaths,
528
+ comparisons_path: comparisonPath,
529
+ ranking: outputRanking,
530
+ seed,
531
+ artifact_dir: artifactDir
532
+ };
533
+ }
534
+
535
+ // dist/builtin/workflows/builtin/tournament.ts
536
+ var tournament_default = workflow({
537
+ name: "tournament",
538
+ description: "Run independent whole-task attempts through a soft-scored pivot-pairing schedule and return an auditable ranking.",
539
+ heartbeatIntervalMinutes: 15,
540
+ inputs: {
541
+ prompt: Type2.String({ description: "Task every competing agent must attempt independently." }),
542
+ num_attempts: Type2.Integer({
543
+ minimum: 2,
544
+ maximum: 8,
545
+ default: 4,
546
+ description: "Number of independent whole-task attempts (2-8)."
547
+ }),
548
+ max_concurrency: Type2.Integer({
549
+ minimum: 1,
550
+ maximum: 8,
551
+ default: 4,
552
+ description: "Maximum simultaneously active attempts or pairwise judges (1-8)."
553
+ }),
554
+ n_evaluations: Type2.Integer({
555
+ minimum: 1,
556
+ default: 2,
557
+ description: "Number of repeated evaluations per criterion and directed pair."
558
+ }),
559
+ pivots: Type2.Integer({
560
+ minimum: 1,
561
+ default: 1,
562
+ description: "Number of pivot candidates used for the second comparison phase."
563
+ }),
564
+ seed: Type2.Integer({
565
+ default: 0,
566
+ description: "Seed for the deterministic comparison schedule."
567
+ }),
568
+ criteria: Type2.Optional(Type2.Union([
569
+ Type2.String(),
570
+ Type2.Record(Type2.String(), Type2.String()),
571
+ Type2.Array(Type2.String()),
572
+ Type2.Array(Type2.Object({
573
+ id: Type2.Optional(Type2.String()),
574
+ name: Type2.Optional(Type2.String()),
575
+ description: Type2.String()
576
+ }, { additionalProperties: true }))
577
+ ], {
578
+ description: "Optional V1 judge criteria; accepts a markdown rubric, record, string list, or CriterionInput list; omitted uses the default three-criterion rubric."
579
+ })),
580
+ models: Type2.Optional(Type2.Array(Type2.String(), {
581
+ description: "Optional ordered model ids assigned round-robin to attempt slots."
582
+ }))
583
+ },
584
+ outputs: {
585
+ result: Type2.String({ description: "Compact reference to the final reducer report; read `result_path` for the full report." }),
586
+ winner: Type2.String({ description: "Stable attempt label selected as the top-ranked tournament winner." }),
587
+ winner_artifact_path: Type2.String({ description: "Path to the original winning attempt artifact." }),
588
+ result_path: Type2.String({ description: "Path to the final reducer report artifact." }),
589
+ attempt_artifact_paths: Type2.Array(Type2.String(), { description: "Paths to every independent attempt artifact." }),
590
+ judge_artifact_paths: Type2.Array(Type2.String(), { description: "Paths to all per-job structured judge artifacts." }),
591
+ comparisons_path: Type2.String({ description: "Path to the durable JSON soft-scored comparisons ledger." }),
592
+ ranking: Type2.Array(Type2.Object({
593
+ label: Type2.String(),
594
+ meanPreference: Type2.Number()
595
+ }, { additionalProperties: false }), { description: "All attempts ordered by mean preference." }),
596
+ seed: Type2.Integer({ description: "Seed used for the deterministic comparison schedule." }),
597
+ artifact_dir: Type2.String({ description: "Run-specific directory containing tournament artifacts." })
598
+ },
599
+ run: async (ctx) => await runTournament(withSteeringPropagationContext(ctx))
600
+ });
601
+
602
+ export { tournament_default };