@hmbown/kytchen-reasoning 0.1.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.
- package/dist/autocode.d.ts +89 -0
- package/dist/autocode.d.ts.map +1 -0
- package/dist/autocode.js +282 -0
- package/dist/autocode.js.map +1 -0
- package/dist/autocode.test.d.ts +2 -0
- package/dist/autocode.test.d.ts.map +1 -0
- package/dist/autocode.test.js +149 -0
- package/dist/autocode.test.js.map +1 -0
- package/dist/coherence.d.ts +123 -0
- package/dist/coherence.d.ts.map +1 -0
- package/dist/coherence.js +289 -0
- package/dist/coherence.js.map +1 -0
- package/dist/coherence.test.d.ts +2 -0
- package/dist/coherence.test.d.ts.map +1 -0
- package/dist/coherence.test.js +14 -0
- package/dist/coherence.test.js.map +1 -0
- package/dist/dialectic.d.ts +126 -0
- package/dist/dialectic.d.ts.map +1 -0
- package/dist/dialectic.js +317 -0
- package/dist/dialectic.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/recursive.d.ts +214 -0
- package/dist/recursive.d.ts.map +1 -0
- package/dist/recursive.js +570 -0
- package/dist/recursive.js.map +1 -0
- package/dist/recursive.test.d.ts +2 -0
- package/dist/recursive.test.d.ts.map +1 -0
- package/dist/recursive.test.js +94 -0
- package/dist/recursive.test.js.map +1 -0
- package/dist/router.d.ts +28 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.golden.test.d.ts +2 -0
- package/dist/router.golden.test.d.ts.map +1 -0
- package/dist/router.golden.test.js +84 -0
- package/dist/router.golden.test.js.map +1 -0
- package/dist/router.js +272 -0
- package/dist/router.js.map +1 -0
- package/dist/router.test.d.ts +2 -0
- package/dist/router.test.d.ts.map +1 -0
- package/dist/router.test.js +86 -0
- package/dist/router.test.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Valid state transitions. Used by the engine to enforce
|
|
3
|
+
* that the loop progresses through the correct sequence.
|
|
4
|
+
*/
|
|
5
|
+
const STATE_TRANSITIONS = {
|
|
6
|
+
idle: ["proposing"],
|
|
7
|
+
proposing: ["critiquing"],
|
|
8
|
+
critiquing: ["synthesizing"],
|
|
9
|
+
synthesizing: ["evaluating"],
|
|
10
|
+
evaluating: ["proposing", "done"],
|
|
11
|
+
done: [],
|
|
12
|
+
};
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Internal helpers
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
function now() {
|
|
17
|
+
return new Date().toISOString();
|
|
18
|
+
}
|
|
19
|
+
function emptySnapshot() {
|
|
20
|
+
return {
|
|
21
|
+
state: "idle",
|
|
22
|
+
currentRound: 0,
|
|
23
|
+
turns: [],
|
|
24
|
+
pendingThesis: null,
|
|
25
|
+
pendingAntithesis: null,
|
|
26
|
+
pendingSynthesis: null,
|
|
27
|
+
latestConfidence: 0,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Parse the evaluator's raw text response into a structured verdict.
|
|
32
|
+
* Accepts a JSON block (possibly wrapped in markdown fences).
|
|
33
|
+
* Falls back to a conservative rejection if parsing fails so the
|
|
34
|
+
* critic can never be bypassed by a malformed response.
|
|
35
|
+
*/
|
|
36
|
+
function parseEvaluatorVerdict(raw) {
|
|
37
|
+
// Strip optional markdown code fences
|
|
38
|
+
const stripped = raw
|
|
39
|
+
.replace(/^```(?:json)?\s*/i, "")
|
|
40
|
+
.replace(/\s*```\s*$/, "")
|
|
41
|
+
.trim();
|
|
42
|
+
try {
|
|
43
|
+
const parsed = JSON.parse(stripped);
|
|
44
|
+
if (typeof parsed === "object" &&
|
|
45
|
+
parsed !== null &&
|
|
46
|
+
"confidence" in parsed &&
|
|
47
|
+
"accept" in parsed) {
|
|
48
|
+
const obj = parsed;
|
|
49
|
+
const confidence = Number(obj["confidence"]);
|
|
50
|
+
const accept = Boolean(obj["accept"]);
|
|
51
|
+
const rationale = typeof obj["rationale"] === "string"
|
|
52
|
+
? obj["rationale"]
|
|
53
|
+
: "No rationale provided.";
|
|
54
|
+
// Clamp confidence to [0, 1]
|
|
55
|
+
return {
|
|
56
|
+
confidence: Math.max(0, Math.min(1, confidence)),
|
|
57
|
+
accept,
|
|
58
|
+
rationale,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// fall through to conservative default
|
|
64
|
+
}
|
|
65
|
+
// Conservative fallback: never accept on parse failure.
|
|
66
|
+
// This ensures the critic cannot be bypassed by a proposer
|
|
67
|
+
// that crafts a malformed evaluator response.
|
|
68
|
+
return {
|
|
69
|
+
confidence: 0,
|
|
70
|
+
accept: false,
|
|
71
|
+
rationale: "Evaluator response could not be parsed; defaulting to rejection.",
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
// System prompts per role
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
function proposerSystemPrompt() {
|
|
78
|
+
return [
|
|
79
|
+
"You are the PROPOSER in a dialectical reasoning process.",
|
|
80
|
+
"Your role is to formulate a clear, well-reasoned thesis that addresses the given goal.",
|
|
81
|
+
"If prior rounds exist, build upon the previous synthesis to strengthen the argument.",
|
|
82
|
+
"Be precise, structured, and evidence-oriented.",
|
|
83
|
+
].join("\n");
|
|
84
|
+
}
|
|
85
|
+
function criticSystemPrompt() {
|
|
86
|
+
return [
|
|
87
|
+
"You are the CRITIC (antithesis) in a dialectical reasoning process.",
|
|
88
|
+
"Your role is to rigorously challenge the thesis.",
|
|
89
|
+
"Identify logical flaws, missing evidence, unstated assumptions, edge cases, and counter-arguments.",
|
|
90
|
+
"Do NOT accept the thesis at face value. Your job is adversarial scrutiny.",
|
|
91
|
+
"Be specific and constructive -- point to concrete weaknesses.",
|
|
92
|
+
].join("\n");
|
|
93
|
+
}
|
|
94
|
+
function synthesizerSystemPrompt() {
|
|
95
|
+
return [
|
|
96
|
+
"You are the SYNTHESIZER in a dialectical reasoning process.",
|
|
97
|
+
"You receive a thesis and its antithesis (critique).",
|
|
98
|
+
"Your role is to produce a synthesis that resolves the tension between them.",
|
|
99
|
+
"Incorporate the strongest elements of the thesis while addressing the critique.",
|
|
100
|
+
"The synthesis should be strictly stronger than the original thesis.",
|
|
101
|
+
].join("\n");
|
|
102
|
+
}
|
|
103
|
+
function evaluatorSystemPrompt() {
|
|
104
|
+
return [
|
|
105
|
+
"You are the EVALUATOR GATE in a dialectical reasoning process.",
|
|
106
|
+
"You assess whether the synthesis adequately resolves the tension between thesis and antithesis.",
|
|
107
|
+
"",
|
|
108
|
+
"Respond with a JSON object (no other text) with the following fields:",
|
|
109
|
+
' "confidence": a number between 0 and 1 indicating your confidence in the synthesis,',
|
|
110
|
+
' "accept": a boolean indicating whether to accept the synthesis as final,',
|
|
111
|
+
' "rationale": a brief explanation of your verdict.',
|
|
112
|
+
"",
|
|
113
|
+
"Be rigorous. Only accept when the synthesis genuinely resolves the core objections.",
|
|
114
|
+
"A proposer claiming completeness is NOT sufficient -- evaluate on the merits.",
|
|
115
|
+
].join("\n");
|
|
116
|
+
}
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
// DialecticalEngine
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
export class DialecticalEngine {
|
|
121
|
+
config;
|
|
122
|
+
generate;
|
|
123
|
+
constructor(config) {
|
|
124
|
+
this.config = config;
|
|
125
|
+
this.generate =
|
|
126
|
+
config.generate ??
|
|
127
|
+
(async (_sys, user) => `[stub] Response to: ${user.slice(0, 120)}`);
|
|
128
|
+
}
|
|
129
|
+
// -----------------------------------------------------------------------
|
|
130
|
+
// Public predicates
|
|
131
|
+
// -----------------------------------------------------------------------
|
|
132
|
+
shouldContinue(round, confidence) {
|
|
133
|
+
return (round < this.config.maxRounds &&
|
|
134
|
+
confidence < this.config.confidenceThreshold);
|
|
135
|
+
}
|
|
136
|
+
isApplicable(mode) {
|
|
137
|
+
return mode === "dialectical" || mode === "dialectical-recursive";
|
|
138
|
+
}
|
|
139
|
+
// -----------------------------------------------------------------------
|
|
140
|
+
// State machine helpers
|
|
141
|
+
// -----------------------------------------------------------------------
|
|
142
|
+
/**
|
|
143
|
+
* Validate and execute a state transition.
|
|
144
|
+
* Throws if the transition is not allowed by the state machine.
|
|
145
|
+
*/
|
|
146
|
+
transition(snapshot, to) {
|
|
147
|
+
const allowed = STATE_TRANSITIONS[snapshot.state];
|
|
148
|
+
if (!allowed.includes(to)) {
|
|
149
|
+
throw new Error(`Invalid dialectical state transition: ${snapshot.state} → ${to}. ` +
|
|
150
|
+
`Allowed: [${allowed.join(", ")}]`);
|
|
151
|
+
}
|
|
152
|
+
return { ...snapshot, state: to };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Return a fresh snapshot for a new run.
|
|
156
|
+
*/
|
|
157
|
+
createSnapshot() {
|
|
158
|
+
return emptySnapshot();
|
|
159
|
+
}
|
|
160
|
+
// -----------------------------------------------------------------------
|
|
161
|
+
// Role execution
|
|
162
|
+
// -----------------------------------------------------------------------
|
|
163
|
+
async executeProposer(goal, context, previousSynthesis) {
|
|
164
|
+
const userPrompt = previousSynthesis
|
|
165
|
+
? [
|
|
166
|
+
`Goal: ${goal}`,
|
|
167
|
+
"",
|
|
168
|
+
`Context: ${context}`,
|
|
169
|
+
"",
|
|
170
|
+
"Previous synthesis (build upon this):",
|
|
171
|
+
previousSynthesis,
|
|
172
|
+
].join("\n")
|
|
173
|
+
: [`Goal: ${goal}`, "", `Context: ${context}`].join("\n");
|
|
174
|
+
const content = await this.generate(proposerSystemPrompt(), userPrompt);
|
|
175
|
+
return { role: "proposer", content, timestamp: now() };
|
|
176
|
+
}
|
|
177
|
+
async executeCritic(goal, thesis) {
|
|
178
|
+
const userPrompt = [
|
|
179
|
+
`Goal: ${goal}`,
|
|
180
|
+
"",
|
|
181
|
+
"Thesis to critique:",
|
|
182
|
+
thesis,
|
|
183
|
+
].join("\n");
|
|
184
|
+
const content = await this.generate(criticSystemPrompt(), userPrompt);
|
|
185
|
+
return { role: "critic", content, timestamp: now() };
|
|
186
|
+
}
|
|
187
|
+
async executeSynthesizer(goal, thesis, antithesis) {
|
|
188
|
+
const userPrompt = [
|
|
189
|
+
`Goal: ${goal}`,
|
|
190
|
+
"",
|
|
191
|
+
"Thesis:",
|
|
192
|
+
thesis,
|
|
193
|
+
"",
|
|
194
|
+
"Antithesis (critique):",
|
|
195
|
+
antithesis,
|
|
196
|
+
].join("\n");
|
|
197
|
+
const content = await this.generate(synthesizerSystemPrompt(), userPrompt);
|
|
198
|
+
return { role: "synthesizer", content, timestamp: now() };
|
|
199
|
+
}
|
|
200
|
+
async executeEvaluator(goal, thesis, antithesis, synthesis) {
|
|
201
|
+
const userPrompt = [
|
|
202
|
+
`Goal: ${goal}`,
|
|
203
|
+
"",
|
|
204
|
+
"Thesis:",
|
|
205
|
+
thesis,
|
|
206
|
+
"",
|
|
207
|
+
"Antithesis:",
|
|
208
|
+
antithesis,
|
|
209
|
+
"",
|
|
210
|
+
"Synthesis:",
|
|
211
|
+
synthesis,
|
|
212
|
+
].join("\n");
|
|
213
|
+
const content = await this.generate(evaluatorSystemPrompt(), userPrompt);
|
|
214
|
+
const verdict = parseEvaluatorVerdict(content);
|
|
215
|
+
return {
|
|
216
|
+
artifact: { role: "evaluator", content, timestamp: now() },
|
|
217
|
+
verdict,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
// -----------------------------------------------------------------------
|
|
221
|
+
// Main loop
|
|
222
|
+
// -----------------------------------------------------------------------
|
|
223
|
+
/**
|
|
224
|
+
* Execute the full dialectical reasoning loop.
|
|
225
|
+
*
|
|
226
|
+
* Rounds: thesis → antithesis → synthesis → evaluation
|
|
227
|
+
* Stops when the evaluator's confidence meets the threshold,
|
|
228
|
+
* the evaluator explicitly accepts, or max rounds are exhausted.
|
|
229
|
+
*
|
|
230
|
+
* The effective max rounds is the lesser of `config.maxRounds`
|
|
231
|
+
* and `input.maxTurns` (when provided).
|
|
232
|
+
*/
|
|
233
|
+
async run(input) {
|
|
234
|
+
const effectiveMaxRounds = Math.min(this.config.maxRounds, input.maxTurns > 0 ? input.maxTurns : this.config.maxRounds);
|
|
235
|
+
let snapshot = this.createSnapshot();
|
|
236
|
+
let previousSynthesis = null;
|
|
237
|
+
for (let round = 0; round < effectiveMaxRounds; round++) {
|
|
238
|
+
// ---- Proposing ----
|
|
239
|
+
snapshot = this.transition(snapshot.state === "idle" ? snapshot : { ...snapshot, state: "evaluating" }, "proposing");
|
|
240
|
+
const proposerArtifact = await this.executeProposer(input.goal, input.context, previousSynthesis);
|
|
241
|
+
snapshot = {
|
|
242
|
+
...snapshot,
|
|
243
|
+
currentRound: round,
|
|
244
|
+
pendingThesis: proposerArtifact.content,
|
|
245
|
+
};
|
|
246
|
+
// ---- Critiquing ----
|
|
247
|
+
snapshot = this.transition(snapshot, "critiquing");
|
|
248
|
+
const criticArtifact = await this.executeCritic(input.goal, proposerArtifact.content);
|
|
249
|
+
snapshot = {
|
|
250
|
+
...snapshot,
|
|
251
|
+
pendingAntithesis: criticArtifact.content,
|
|
252
|
+
};
|
|
253
|
+
// ---- Synthesizing ----
|
|
254
|
+
snapshot = this.transition(snapshot, "synthesizing");
|
|
255
|
+
const synthesizerArtifact = await this.executeSynthesizer(input.goal, proposerArtifact.content, criticArtifact.content);
|
|
256
|
+
snapshot = {
|
|
257
|
+
...snapshot,
|
|
258
|
+
pendingSynthesis: synthesizerArtifact.content,
|
|
259
|
+
};
|
|
260
|
+
// ---- Evaluating ----
|
|
261
|
+
snapshot = this.transition(snapshot, "evaluating");
|
|
262
|
+
const { artifact: evaluatorArtifact, verdict } = await this.executeEvaluator(input.goal, proposerArtifact.content, criticArtifact.content, synthesizerArtifact.content);
|
|
263
|
+
// Build the turn record with all artifacts for auditability
|
|
264
|
+
const turn = {
|
|
265
|
+
round,
|
|
266
|
+
thesis: proposerArtifact.content,
|
|
267
|
+
antithesis: criticArtifact.content,
|
|
268
|
+
synthesis: synthesizerArtifact.content,
|
|
269
|
+
confidence: verdict.confidence,
|
|
270
|
+
artifacts: [
|
|
271
|
+
proposerArtifact,
|
|
272
|
+
criticArtifact,
|
|
273
|
+
synthesizerArtifact,
|
|
274
|
+
evaluatorArtifact,
|
|
275
|
+
],
|
|
276
|
+
};
|
|
277
|
+
snapshot = {
|
|
278
|
+
...snapshot,
|
|
279
|
+
turns: [...snapshot.turns, turn],
|
|
280
|
+
latestConfidence: verdict.confidence,
|
|
281
|
+
pendingThesis: null,
|
|
282
|
+
pendingAntithesis: null,
|
|
283
|
+
pendingSynthesis: null,
|
|
284
|
+
};
|
|
285
|
+
previousSynthesis = synthesizerArtifact.content;
|
|
286
|
+
// ---- Gate decision ----
|
|
287
|
+
// The evaluator gate is deterministic: accept only when both the
|
|
288
|
+
// confidence threshold is met AND the evaluator explicitly accepts.
|
|
289
|
+
// This prevents the proposer from bypassing the critic by claiming
|
|
290
|
+
// completeness in its own output.
|
|
291
|
+
if (verdict.accept &&
|
|
292
|
+
verdict.confidence >= this.config.confidenceThreshold) {
|
|
293
|
+
snapshot = this.transition(snapshot, "done");
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
// If this was the last round, transition to done regardless
|
|
297
|
+
if (round === effectiveMaxRounds - 1) {
|
|
298
|
+
snapshot = this.transition(snapshot, "done");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// Ensure terminal state
|
|
302
|
+
if (snapshot.state !== "done") {
|
|
303
|
+
snapshot = { ...snapshot, state: "done" };
|
|
304
|
+
}
|
|
305
|
+
const finalSynthesis = previousSynthesis ?? `No synthesis produced for: ${input.goal}`;
|
|
306
|
+
const finalConfidence = snapshot.latestConfidence;
|
|
307
|
+
return {
|
|
308
|
+
artifact: {
|
|
309
|
+
synthesis: finalSynthesis,
|
|
310
|
+
rounds: snapshot.turns,
|
|
311
|
+
confidence: finalConfidence,
|
|
312
|
+
},
|
|
313
|
+
mode: "dialectical",
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
//# sourceMappingURL=dialectic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dialectic.js","sourceRoot":"","sources":["../src/dialectic.ts"],"names":[],"mappings":"AAqBA;;;GAGG;AACH,MAAM,iBAAiB,GAAoE;IACzF,IAAI,EAAE,CAAC,WAAW,CAAC;IACnB,SAAS,EAAE,CAAC,YAAY,CAAC;IACzB,UAAU,EAAE,CAAC,cAAc,CAAC;IAC5B,YAAY,EAAE,CAAC,YAAY,CAAC;IAC5B,UAAU,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC;IACjC,IAAI,EAAE,EAAE;CACT,CAAC;AA4HF,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E,SAAS,GAAG;IACV,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,aAAa;IACpB,OAAO;QACL,KAAK,EAAE,MAAM;QACb,YAAY,EAAE,CAAC;QACf,KAAK,EAAE,EAAE;QACT,aAAa,EAAE,IAAI;QACnB,iBAAiB,EAAE,IAAI;QACvB,gBAAgB,EAAE,IAAI;QACtB,gBAAgB,EAAE,CAAC;KACpB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,qBAAqB,CAAC,GAAW;IACxC,sCAAsC;IACtC,MAAM,QAAQ,GAAG,GAAG;SACjB,OAAO,CAAC,mBAAmB,EAAE,EAAE,CAAC;SAChC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC;SACzB,IAAI,EAAE,CAAC;IAEV,IAAI,CAAC;QACH,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC7C,IACE,OAAO,MAAM,KAAK,QAAQ;YAC1B,MAAM,KAAK,IAAI;YACf,YAAY,IAAI,M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AAK,KAAK,kBAAkB,GAAG,CAAC,EAAE,CAAC;gBACrC,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QAED,wBAAwB;QACxB,IAAI,QAAQ,CAAC,KAAK,KAAK,MAAM,EAAE,CAAC;YAC9B,QAAQ,GAAG,EAAE,GAAG,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAC5C,CAAC;QAED,MAAM,cAAc,GAClB,iBAAiB,IAAI,8BAA8B,KAAK,CAAC,IAAI,EAAE,CAAC;QAClE,MAAM,eAAe,GAAG,QAAQ,CAAC,gBAAgB,CAAC;QAElD,OAAO;YACL,QAAQ,EAAE;gBACR,SAAS,EAAE,cAAc;gBACzB,MAAM,EAAE,QAAQ,CAAC,KAAK;gBACtB,UAAU,EAAE,eAAe;aAC5B;YACD,IAAI,EAAE,aAAa;SACpB,CAAC;IACJ,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unique identifier for a task node in the recursive tree.
|
|
3
|
+
* Format: `rq-<random>` for root tasks, `rq-<random>-<childIndex>` for children.
|
|
4
|
+
*/
|
|
5
|
+
export type TaskId = string;
|
|
6
|
+
/** A single piece of evidence gathered during sub-query execution. */
|
|
7
|
+
export interface EvidenceItem {
|
|
8
|
+
/** Stable identifier scoped to the task that produced it. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** The task that produced this evidence. */
|
|
11
|
+
sourceTaskId: TaskId;
|
|
12
|
+
/** Human-readable content of the evidence. */
|
|
13
|
+
content: string;
|
|
14
|
+
/** Confidence score in [0, 1]. */
|
|
15
|
+
confidence: number;
|
|
16
|
+
/** ISO-8601 timestamp of when this evidence was recorded. */
|
|
17
|
+
timestamp: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Explicit completion state so that consumers can distinguish a fully
|
|
21
|
+
* resolved outcome from one that was cut short by a recursion guard.
|
|
22
|
+
*/
|
|
23
|
+
export type FinalizationStatus = "completed" | "depth_limit_reached" | "breadth_limit_reached" | "iteration_limit_reached" | "time_budget_exceeded" | "interrupted" | "execution_error" | "partial";
|
|
24
|
+
/** A single node in the recursive task tree. */
|
|
25
|
+
export interface RecursiveTask {
|
|
26
|
+
/** Stable, globally unique identifier. */
|
|
27
|
+
id: TaskId;
|
|
28
|
+
/** The prompt / sub-query to execute. */
|
|
29
|
+
prompt: string;
|
|
30
|
+
/** Zero-indexed depth in the tree (root = 0). */
|
|
31
|
+
depth: number;
|
|
32
|
+
/** Parent task ID, or `null` for root tasks. */
|
|
33
|
+
parentId: TaskId | null;
|
|
34
|
+
/** Ordered list of child task IDs. */
|
|
35
|
+
childIds: TaskId[];
|
|
36
|
+
/** Monotonically increasing index among siblings. */
|
|
37
|
+
siblingIndex: number;
|
|
38
|
+
}
|
|
39
|
+
/** Outcome produced when a single task finishes (or is cut short). */
|
|
40
|
+
export interface RecursiveOutcome {
|
|
41
|
+
/** The task this outcome belongs to. */
|
|
42
|
+
taskId: TaskId;
|
|
43
|
+
/** Depth at which the task ran. */
|
|
44
|
+
depth: number;
|
|
45
|
+
/** Whether this outcome represents a fully resolved result. */
|
|
46
|
+
finalized: boolean;
|
|
47
|
+
/** Explicit marker describing how/why the task finished. */
|
|
48
|
+
finalizationStatus: FinalizationStatus;
|
|
49
|
+
/** Textual summary of the outcome. */
|
|
50
|
+
summary: string;
|
|
51
|
+
/** Evidence items accumulated during this task's execution. */
|
|
52
|
+
evidence: EvidenceItem[];
|
|
53
|
+
/** Outcomes of child sub-queries, in spawn order. */
|
|
54
|
+
childOutcomes: RecursiveOutcome[];
|
|
55
|
+
}
|
|
56
|
+
/** Tuning knobs for bounded recursive execution. */
|
|
57
|
+
export interface RecursiveConfig {
|
|
58
|
+
/** Maximum depth of the task tree (root = depth 0). */
|
|
59
|
+
maxDepth: number;
|
|
60
|
+
/** Maximum total iterations (task executions) across the entire tree. */
|
|
61
|
+
maxIterations: number;
|
|
62
|
+
/**
|
|
63
|
+
* Maximum number of child sub-queries a single task may spawn.
|
|
64
|
+
* Defaults to `maxIterations` when not set (effectively unbounded
|
|
65
|
+
* within the iteration cap).
|
|
66
|
+
*/
|
|
67
|
+
maxBreadth?: number;
|
|
68
|
+
/** Optional hard wall-clock budget for the full recursive execution. */
|
|
69
|
+
maxWallTimeMs?: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Result returned by the caller-supplied execution callback.
|
|
73
|
+
*
|
|
74
|
+
* The callback is intentionally provider-agnostic: the orchestrator does not
|
|
75
|
+
* care whether the work is done by an LLM, a tool pipeline, or a test stub.
|
|
76
|
+
*/
|
|
77
|
+
export interface SubQueryResult {
|
|
78
|
+
/** Textual summary produced by executing the sub-query. */
|
|
79
|
+
summary: string;
|
|
80
|
+
/** Evidence gathered during execution. */
|
|
81
|
+
evidence: EvidenceItem[];
|
|
82
|
+
/**
|
|
83
|
+
* Optional list of follow-up sub-queries the execution wants to spawn.
|
|
84
|
+
* The orchestrator will respect depth/breadth/iteration limits when
|
|
85
|
+
* deciding which (if any) of these are actually executed.
|
|
86
|
+
*/
|
|
87
|
+
subQueries?: string[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A provider-agnostic callback that executes a single sub-query.
|
|
91
|
+
*
|
|
92
|
+
* Receives the task to execute and its parent context (list of evidence
|
|
93
|
+
* accumulated so far in the parent scope). Returns the execution result
|
|
94
|
+
* including any follow-up sub-queries the task wants to spawn.
|
|
95
|
+
*/
|
|
96
|
+
export type ExecuteCallback = (task: Readonly<RecursiveTask>, parentEvidence: readonly EvidenceItem[], context: Readonly<SubAgentExecutionContext>) => Promise<SubQueryResult>;
|
|
97
|
+
export interface SubAgentExecutionContext {
|
|
98
|
+
iteration: number;
|
|
99
|
+
maxIterations: number;
|
|
100
|
+
startedAt: string;
|
|
101
|
+
remainingMs?: number;
|
|
102
|
+
signal?: AbortSignal;
|
|
103
|
+
}
|
|
104
|
+
/** Full trace of the recursive decomposition for observability. */
|
|
105
|
+
export interface RecursiveTrace {
|
|
106
|
+
/** Root outcome (contains the entire child tree). */
|
|
107
|
+
rootOutcome: RecursiveOutcome;
|
|
108
|
+
/** Flat map of every task node for random access. */
|
|
109
|
+
taskMap: ReadonlyMap<TaskId, RecursiveTask>;
|
|
110
|
+
/** Total number of task executions performed. */
|
|
111
|
+
totalIterations: number;
|
|
112
|
+
/** Maximum depth actually reached during execution. */
|
|
113
|
+
maxDepthReached: number;
|
|
114
|
+
}
|
|
115
|
+
export type RecursiveLifecycleEventType = "execution.started" | "execution.completed" | "task.spawned" | "task.started" | "task.completed" | "task.merged" | "task.limited" | "task.interrupted";
|
|
116
|
+
export interface RecursiveLifecycleEvent {
|
|
117
|
+
type: RecursiveLifecycleEventType;
|
|
118
|
+
timestamp: string;
|
|
119
|
+
taskId?: TaskId;
|
|
120
|
+
parentTaskId?: TaskId | null;
|
|
121
|
+
depth?: number;
|
|
122
|
+
prompt?: string;
|
|
123
|
+
iteration?: number;
|
|
124
|
+
maxIterations?: number;
|
|
125
|
+
maxDepth?: number;
|
|
126
|
+
maxBreadth?: number;
|
|
127
|
+
maxWallTimeMs?: number;
|
|
128
|
+
maxDepthReached?: number;
|
|
129
|
+
remainingMs?: number;
|
|
130
|
+
finalizationStatus?: FinalizationStatus;
|
|
131
|
+
summary?: string;
|
|
132
|
+
childCount?: number;
|
|
133
|
+
evidenceCount?: number;
|
|
134
|
+
message?: string;
|
|
135
|
+
}
|
|
136
|
+
export interface RecursiveRunOptions {
|
|
137
|
+
maxDepth?: number;
|
|
138
|
+
maxIterations?: number;
|
|
139
|
+
maxBreadth?: number;
|
|
140
|
+
maxWallTimeMs?: number;
|
|
141
|
+
signal?: AbortSignal;
|
|
142
|
+
now?: () => number;
|
|
143
|
+
onLifecycleEvent?: (event: RecursiveLifecycleEvent) => void | Promise<void>;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Resets the internal ID counter. Intended for tests only.
|
|
147
|
+
* @internal
|
|
148
|
+
*/
|
|
149
|
+
export declare function _resetIdCounter(): void;
|
|
150
|
+
/**
|
|
151
|
+
* Recursive reasoning orchestrator with bounded sub-query execution.
|
|
152
|
+
*
|
|
153
|
+
* The orchestrator manages a tree of sub-queries, enforcing hard limits
|
|
154
|
+
* on depth, breadth, and total iterations. It is provider-agnostic: the
|
|
155
|
+
* actual execution of each sub-query is delegated to a caller-supplied
|
|
156
|
+
* callback.
|
|
157
|
+
*
|
|
158
|
+
* ## Lifecycle
|
|
159
|
+
*
|
|
160
|
+
* 1. The caller invokes {@link run} with a root prompt and an execution
|
|
161
|
+
* callback.
|
|
162
|
+
* 2. The orchestrator creates a root task and executes it via the callback.
|
|
163
|
+
* 3. If the callback returns follow-up sub-queries, the orchestrator
|
|
164
|
+
* spawns child tasks (subject to limits) and recurses.
|
|
165
|
+
* 4. Child outcomes are merged back into the parent deterministically.
|
|
166
|
+
* 5. A full {@link RecursiveTrace} is returned for observability.
|
|
167
|
+
*
|
|
168
|
+
* ## Graceful degradation
|
|
169
|
+
*
|
|
170
|
+
* When any limit is reached, the orchestrator does **not** throw. Instead
|
|
171
|
+
* it produces a partial outcome with an appropriate
|
|
172
|
+
* {@link FinalizationStatus} and a fallback summary. Callers can inspect
|
|
173
|
+
* the trace to see which sub-queries were curtailed.
|
|
174
|
+
*/
|
|
175
|
+
export declare class RecursiveOrchestrator {
|
|
176
|
+
private readonly config;
|
|
177
|
+
constructor(config: RecursiveConfig);
|
|
178
|
+
/**
|
|
179
|
+
* Returns `true` if a new task can be spawned given the current depth
|
|
180
|
+
* and cumulative iteration count.
|
|
181
|
+
*/
|
|
182
|
+
canSpawn(depth: number, iterations: number): boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Produces a fallback outcome when a task cannot be fully executed.
|
|
185
|
+
* The outcome is explicitly marked as non-finalized with the
|
|
186
|
+
* appropriate status.
|
|
187
|
+
*/
|
|
188
|
+
summarizeFallback(task: RecursiveTask, status?: FinalizationStatus): RecursiveOutcome;
|
|
189
|
+
/**
|
|
190
|
+
* Executes a recursive reasoning pass starting from the given root
|
|
191
|
+
* prompt.
|
|
192
|
+
*
|
|
193
|
+
* @param rootPrompt - The top-level query to reason about.
|
|
194
|
+
* @param execute - Provider-agnostic callback for running a single
|
|
195
|
+
* sub-query. The orchestrator calls this for every
|
|
196
|
+
* task node in the tree.
|
|
197
|
+
* @returns A full trace of the recursive decomposition.
|
|
198
|
+
*/
|
|
199
|
+
run(rootPrompt: string, execute: ExecuteCallback, options?: RecursiveRunOptions): Promise<RecursiveTrace>;
|
|
200
|
+
/**
|
|
201
|
+
* Recursively executes a single task and all of its spawned children.
|
|
202
|
+
*/
|
|
203
|
+
private executeTask;
|
|
204
|
+
/**
|
|
205
|
+
* Builds a partial outcome when a task wants to spawn children but the
|
|
206
|
+
* depth limit has been reached.
|
|
207
|
+
*/
|
|
208
|
+
private buildDepthLimitNote;
|
|
209
|
+
private isInterrupted;
|
|
210
|
+
private remainingMs;
|
|
211
|
+
private isTimeBudgetExceeded;
|
|
212
|
+
private emitLifecycle;
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=recursive.d.ts.map
|
|
@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