bare-agent 0.59.0 → 0.60.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,106 +0,0 @@
1
- 'use strict';
2
-
3
- // NB-3 — the synthesis/reduce step (RLM_PRD §4.3 / §10 step 4). A Family-A worker synthesizes in its own
4
- // closing turn; Family B's `runPlan` returns results[] with nothing to combine them — this is that reducer,
5
- // AND the seam a Family-A caller uses to OVERRIDE the model's synthesis when it shouldn't be trusted.
6
- //
7
- // §9.1 RESOLVED: numeric/AGGREGATION reduces must be DETERMINISTIC CODE — LLM arithmetic over found partials
8
- // carried ~10–15% error even at FULL retrieval (spikes 1 & 2). So the `reduce` FUNCTION form is the
9
- // aggregation path (the caller sums / maxes / dedups in JS); the Loop-driven `merge` strategy is reserved for
10
- // genuinely SUBJECTIVE synthesis (combine prose, reconcile overlapping findings) where there is no exact
11
- // answer to compute. `concat` is the lossless no-LLM default. The merge path is low-risk Loop reuse (the Loop
12
- // is already live-proven) — the riskiest NB-3 assumption was "trust-code-over-LLM-arithmetic," already
13
- // validated; this file does not re-POC it.
14
-
15
- /** @typedef {import('../types').Provider} Provider */
16
-
17
- const { Loop } = require('./loop');
18
- const { HaltError } = require('./errors');
19
-
20
- const MERGE_PROMPT =
21
- 'You are a synthesis engine. You are given a TASK and several PARTIAL RESULTS, each produced by a separate ' +
22
- 'worker over its own slice of the problem. Combine them into ONE coherent, de-duplicated answer to the ' +
23
- 'task. Preserve every distinct fact; drop exact duplicates; reconcile overlaps; do NOT invent anything not ' +
24
- 'present in the partials. Output only the combined answer.';
25
-
26
- const asText = (/** @type {any} */ r) => (typeof r === 'string' ? r : JSON.stringify(r));
27
-
28
- /**
29
- * Deterministic lossless join — no LLM. The safe default when partials are independent and order-able.
30
- * @param {any[]} results
31
- * @returns {string}
32
- */
33
- function concatReduce(results) {
34
- return results.map((r, i) => `### Part ${i + 1}\n${asText(r)}`).join('\n\n');
35
- }
36
-
37
- /**
38
- * Subjective merge — an ISOLATED Loop (fresh context window, a harsh synthesis persona) combines the partials
39
- * into one answer. Use for prose / overlapping-findings synthesis; NEVER for arithmetic (§9.1 → use a
40
- * `reduce` fn). Budget visibility: forwards usage to `onLlmResult`. A governance HaltError propagates clean
41
- * (the caller decides), mirroring the Evaluator's agentic path.
42
- * @param {string} task
43
- * @param {any[]} results
44
- * @param {{provider: Provider, contract?: string|null, onLlmResult?: Function, policy?: Function}} opts
45
- * @returns {Promise<string>}
46
- * @throws {HaltError} a governance cap halted the synthesis Loop.
47
- */
48
- async function mergeReduce(task, results, opts) {
49
- if (!opts || !opts.provider) throw new Error('[synthesize] strategy "merge" requires a provider');
50
- const loop = new Loop({
51
- provider: opts.provider,
52
- system: MERGE_PROMPT,
53
- policy: opts.policy || undefined,
54
- onLlmResult: opts.onLlmResult || undefined,
55
- throwOnError: false, // a synthesis fault surfaces below; a halt is re-thrown as a clean HaltError
56
- });
57
- const dod = opts.contract ? `\n\nDEFINITION OF DONE:\n${opts.contract}` : '';
58
- const body =
59
- `TASK:\n${task}${dod}\n\nPARTIAL RESULTS:\n` +
60
- results.map((r, i) => `[${i + 1}] ${asText(r)}`).join('\n\n');
61
- const out = await loop.run([{ role: 'user', content: body }]);
62
- if (typeof out.error === 'string' && out.error.startsWith('halt:')) {
63
- throw new HaltError('[synthesize] merge halted by governance', { rule: out.error.slice('halt:'.length) });
64
- }
65
- // A non-halt fault (e.g. provider error, deny short-circuit, the hard round limit) is non-fatal here — fall
66
- // back to the LOSSLESS concat rather than losing the partials entirely. recurse still reports honest
67
- // completeness via its own paths.
68
- //
69
- // Branch on `out.error`, NOT on the falsiness of `out.text` (BA-5). Since the Loop now preserves the text a
70
- // bounded run produced, a faulted merge returns its PARTIAL, aborted prose — so `out.text || concat` would
71
- // silently ship that fragment as the synthesized answer and every child result would be lost. Proven
72
- // reachable: the merge Loop registers no tools, so a hallucinated tool call is fed back as
73
- // `[Loop] Unknown tool` and the round loop CONTINUES — round 1 emits prose, round 2 dies, and the fallback
74
- // never fires. `error` is the sole success signal; text-falsiness never was one.
75
- if (out.error) return concatReduce(results);
76
- return out.text || concatReduce(results);
77
- }
78
-
79
- /**
80
- * The reducer dispatcher used by `recurse` (and, at build step 5, Family B over `runPlan` results[]).
81
- * @param {string} task
82
- * @param {any[]} results - The worker/child result values (NOT transcripts — copy-on-return holds upstream).
83
- * @param {object} [opts]
84
- * @param {Function} [opts.reduce] - A deterministic code-reduce: `({ task, results }) => any`. The §9.1
85
- * aggregation path; takes precedence over `strategy`.
86
- * @param {'concat'|'merge'} [opts.strategy='concat'] - Built-in strategy when no `reduce` fn is given.
87
- * @param {Provider} [opts.provider] - Required for `merge`.
88
- * @param {string|null} [opts.contract]
89
- * @param {Function} [opts.onLlmResult]
90
- * @param {Function} [opts.policy]
91
- * @param {any} [opts.text] - The worker's own synthesized text (Family A), available to a `reduce` fn.
92
- * @param {any[]} [opts.children] - The receipts nodes, available to a `reduce` fn.
93
- * @param {any} [opts.ctx]
94
- * @returns {Promise<any>}
95
- */
96
- async function synthesize(task, results, opts = {}) {
97
- if (typeof opts.reduce === 'function') {
98
- return opts.reduce({ task, results, text: opts.text, children: opts.children, ctx: opts.ctx });
99
- }
100
- const strategy = opts.strategy || 'concat';
101
- if (strategy === 'merge') return mergeReduce(task, results, /** @type {any} */ (opts));
102
- if (strategy === 'concat') return concatReduce(results);
103
- throw new Error(`[synthesize] unknown strategy: ${strategy} (expected 'concat' | 'merge', or a reduce function)`);
104
- }
105
-
106
- module.exports = { synthesize, concatReduce, mergeReduce, MERGE_PROMPT };