@lloyal-labs/lloyal-agents 1.7.0 → 2.0.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/README.md +81 -97
- package/dist/Agent.d.ts +26 -0
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +22 -0
- package/dist/Agent.js.map +1 -1
- package/dist/AgentPolicy.d.ts +27 -10
- package/dist/AgentPolicy.d.ts.map +1 -1
- package/dist/AgentPolicy.js +78 -16
- package/dist/AgentPolicy.js.map +1 -1
- package/dist/agent-pool.d.ts +14 -2
- package/dist/agent-pool.d.ts.map +1 -1
- package/dist/agent-pool.js +415 -148
- package/dist/agent-pool.js.map +1 -1
- package/dist/context.d.ts +18 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +18 -1
- package/dist/context.js.map +1 -1
- package/dist/create-agent-pool.d.ts +33 -15
- package/dist/create-agent-pool.d.ts.map +1 -1
- package/dist/create-agent-pool.js +34 -10
- package/dist/create-agent-pool.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -1
- package/dist/index.js.map +1 -1
- package/dist/orchestrators.d.ts +161 -0
- package/dist/orchestrators.d.ts.map +1 -0
- package/dist/orchestrators.js +173 -0
- package/dist/orchestrators.js.map +1 -0
- package/dist/replay.d.ts +96 -0
- package/dist/replay.d.ts.map +1 -0
- package/dist/replay.js +108 -0
- package/dist/replay.js.map +1 -0
- package/dist/shared-root.d.ts +56 -18
- package/dist/shared-root.d.ts.map +1 -1
- package/dist/shared-root.js +79 -52
- package/dist/shared-root.js.map +1 -1
- package/dist/trace-types.d.ts +22 -2
- package/dist/trace-types.d.ts.map +1 -1
- package/dist/types.d.ts +33 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/use-agent.d.ts.map +1 -1
- package/dist/use-agent.js +13 -17
- package/dist/use-agent.js.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1 @@
|
|
|
1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,+FAA+F;IAC/F,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,8EAA8E;IAC9E,WAAW,CAAC,EAAE,MAAM,SAAS,CAAC,IAAI,CAAC,CAAC;IACpC;;;;;OAKG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,SAAS,CAAC,IAAI,CAAC,CAAC;CACpE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,EACrB,OAAO,CAAC,EAAE,EACV,QAAQ,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,SAAS,KAC5C,YAaA,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,MAAM,GAAI,WAAW,SAAS,EAAE,SAAS,SAAS,EAAE,KAAG,YAajE,CAAC;AAEJ;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,SAAS,CAAC;IAChB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,uFAAuF;IACvF,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,GAAG,GAAI,OAAO,OAAO,EAAE,KAAG,YA0CtC,CAAC"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dag = exports.fanout = exports.chain = exports.parallel = void 0;
|
|
4
|
+
const effection_1 = require("effection");
|
|
5
|
+
// ── Factories ──────────────────────────────────────────────────
|
|
6
|
+
/**
|
|
7
|
+
* Parallel orchestrator — spawn all tasks upfront, wait for all to complete.
|
|
8
|
+
* This is the default shape that `useAgentPool` used to provide implicitly.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* yield* agentPool({
|
|
13
|
+
* tools: [...],
|
|
14
|
+
* orchestrate: parallel(questions.map(q => ({ content: q, systemPrompt: RESEARCH_PROMPT }))),
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @category Agents
|
|
19
|
+
*/
|
|
20
|
+
const parallel = (tasks) => function* (ctx) {
|
|
21
|
+
const agents = yield* (0, effection_1.all)(tasks.map(t => ctx.spawn({ ...t, parent: t.parent ?? ctx.root })));
|
|
22
|
+
yield* (0, effection_1.all)(agents.map(a => ctx.waitFor(a)));
|
|
23
|
+
};
|
|
24
|
+
exports.parallel = parallel;
|
|
25
|
+
/**
|
|
26
|
+
* Chain orchestrator — sequential execution. Each step may extend the shared
|
|
27
|
+
* root with its findings before the next step forks from the extended position.
|
|
28
|
+
*
|
|
29
|
+
* The second argument maps each item to a ChainStep, so callers can compute
|
|
30
|
+
* per-task prompts and spine labels from their own data model without
|
|
31
|
+
* coupling the factory to a particular task type.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* yield* agentPool({
|
|
36
|
+
* tools: [...],
|
|
37
|
+
* parent: queryRoot,
|
|
38
|
+
* orchestrate: chain(researchTasks, (task, i) => ({
|
|
39
|
+
* task: { content: taskToContent(task), systemPrompt: renderWorker({ taskIndex: i }) },
|
|
40
|
+
* userContent: `Research task: ${task.description}`,
|
|
41
|
+
* })),
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @category Agents
|
|
46
|
+
*/
|
|
47
|
+
const chain = (items, toStep) => function* (ctx) {
|
|
48
|
+
for (const [i, item] of items.entries()) {
|
|
49
|
+
const step = toStep(item, i);
|
|
50
|
+
if (step.beforeSpawn)
|
|
51
|
+
yield* step.beforeSpawn();
|
|
52
|
+
const agent = yield* ctx.waitFor(yield* ctx.spawn({ ...step.task, parent: step.task.parent ?? ctx.root }));
|
|
53
|
+
const delta = agent.result && step.userContent
|
|
54
|
+
? yield* ctx.extendRoot(step.userContent, agent.result)
|
|
55
|
+
: 0;
|
|
56
|
+
if (step.afterExtend)
|
|
57
|
+
yield* step.afterExtend(delta, ctx.root.position);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.chain = chain;
|
|
61
|
+
/**
|
|
62
|
+
* Fanout orchestrator — landscape task first (optionally extending the spine),
|
|
63
|
+
* then N independent domain tasks in parallel. Domain tasks fork from the
|
|
64
|
+
* post-landscape root and do NOT see each other's findings.
|
|
65
|
+
*
|
|
66
|
+
* The canonical shape for multi-domain queries: one landscape survey that
|
|
67
|
+
* loads vocabulary into the spine, then one task per independent domain.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* yield* agentPool({
|
|
72
|
+
* orchestrate: fanout(
|
|
73
|
+
* { task: { content: landscapeQuery, systemPrompt: WORKER }, userContent: 'Landscape survey' },
|
|
74
|
+
* domainQueries.map(q => ({ content: q, systemPrompt: WORKER })),
|
|
75
|
+
* ),
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @category Agents
|
|
80
|
+
*/
|
|
81
|
+
const fanout = (landscape, domains) => function* (ctx) {
|
|
82
|
+
const l = yield* ctx.waitFor(yield* ctx.spawn({ ...landscape.task, parent: landscape.task.parent ?? ctx.root }));
|
|
83
|
+
if (l.result && landscape.userContent) {
|
|
84
|
+
yield* ctx.extendRoot(landscape.userContent, l.result);
|
|
85
|
+
}
|
|
86
|
+
const agents = yield* (0, effection_1.all)(domains.map(d => ctx.spawn({ ...d, parent: d.parent ?? ctx.root })));
|
|
87
|
+
yield* (0, effection_1.all)(agents.map(a => ctx.waitFor(a)));
|
|
88
|
+
};
|
|
89
|
+
exports.fanout = fanout;
|
|
90
|
+
/**
|
|
91
|
+
* DAG orchestrator — lazy spawn on dependency resolution. Independent nodes
|
|
92
|
+
* run in parallel; dependent nodes wait until their dependencies complete
|
|
93
|
+
* (and their findings extend the spine) before forking.
|
|
94
|
+
*
|
|
95
|
+
* Subsumes the design in `docs/dag-pool.md` — DAG is an orchestration
|
|
96
|
+
* pattern expressed on top of the general primitive, not a pool internals
|
|
97
|
+
* change.
|
|
98
|
+
*
|
|
99
|
+
* @category Agents
|
|
100
|
+
*/
|
|
101
|
+
const dag = (nodes) => {
|
|
102
|
+
validateDAG(nodes);
|
|
103
|
+
return function* (ctx) {
|
|
104
|
+
// Each node runs as a child Task. Dependencies are expressed by
|
|
105
|
+
// awaiting the dep's Task (`yield* depTask`) — Task<T> extends
|
|
106
|
+
// Future<T> extends Operation<T>, so this is the canonical Effection
|
|
107
|
+
// cross-task rendezvous (see frontside.com/effection/api/v4/Task).
|
|
108
|
+
//
|
|
109
|
+
// Why this beats the older recursive-spawnNode-with-Sets approach:
|
|
110
|
+
// - No mutable bookkeeping. The "node N is done" signal IS the
|
|
111
|
+
// Task itself; the runtime tracks lifetimes for free.
|
|
112
|
+
// - No race window for double-spawn. Each node spawns exactly
|
|
113
|
+
// once, by definition (one entry per `tasks.set`).
|
|
114
|
+
// - Failure propagates through the dependency edges automatically:
|
|
115
|
+
// if node A throws, every task awaiting A's Task receives the
|
|
116
|
+
// same error, and structured concurrency halts the rest.
|
|
117
|
+
const tasks = new Map();
|
|
118
|
+
function* runNode(n) {
|
|
119
|
+
// Gate: wait for every declared dep's task to complete. The map is
|
|
120
|
+
// fully populated before any node body runs (spawned tasks don't
|
|
121
|
+
// execute until the parent yields, and the spawn loop below is
|
|
122
|
+
// synchronous between iterations).
|
|
123
|
+
for (const depId of n.dependsOn ?? []) {
|
|
124
|
+
yield* tasks.get(depId);
|
|
125
|
+
}
|
|
126
|
+
const agent = yield* ctx.waitFor(yield* ctx.spawn({ ...n.task, parent: n.task.parent ?? ctx.root }));
|
|
127
|
+
if (agent.result && n.userContent) {
|
|
128
|
+
yield* ctx.extendRoot(n.userContent, agent.result);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
for (const n of nodes) {
|
|
132
|
+
tasks.set(n.id, yield* (0, effection_1.spawn)(() => runNode(n)));
|
|
133
|
+
}
|
|
134
|
+
// Await every task. Roots run first (no deps to await); descendants
|
|
135
|
+
// unblock as their deps complete. Any throw inside a node propagates
|
|
136
|
+
// here and halts the rest via structured concurrency.
|
|
137
|
+
for (const t of tasks.values())
|
|
138
|
+
yield* t;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
exports.dag = dag;
|
|
142
|
+
function validateDAG(nodes) {
|
|
143
|
+
const ids = new Set(nodes.map(n => n.id));
|
|
144
|
+
const duplicates = nodes.filter((n, i) => nodes.findIndex(m => m.id === n.id) !== i);
|
|
145
|
+
if (duplicates.length > 0) {
|
|
146
|
+
throw new Error(`dag: duplicate node ids: ${duplicates.map(n => n.id).join(', ')}`);
|
|
147
|
+
}
|
|
148
|
+
for (const n of nodes) {
|
|
149
|
+
for (const dep of n.dependsOn ?? []) {
|
|
150
|
+
if (!ids.has(dep))
|
|
151
|
+
throw new Error(`dag: node '${n.id}' depends on unknown node '${dep}'`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Cycle detection via DFS
|
|
155
|
+
const visiting = new Set();
|
|
156
|
+
const visited = new Set();
|
|
157
|
+
const byId = new Map(nodes.map(n => [n.id, n]));
|
|
158
|
+
function visit(id, path) {
|
|
159
|
+
if (visited.has(id))
|
|
160
|
+
return;
|
|
161
|
+
if (visiting.has(id))
|
|
162
|
+
throw new Error(`dag: cycle detected: ${[...path, id].join(' -> ')}`);
|
|
163
|
+
visiting.add(id);
|
|
164
|
+
const node = byId.get(id);
|
|
165
|
+
for (const dep of node?.dependsOn ?? [])
|
|
166
|
+
visit(dep, [...path, id]);
|
|
167
|
+
visiting.delete(id);
|
|
168
|
+
visited.add(id);
|
|
169
|
+
}
|
|
170
|
+
for (const n of nodes)
|
|
171
|
+
visit(n.id, []);
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=orchestrators.js.map
|
|
@@ -0,0 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C,EAAE,8BAA8B,GAAG,GAAG,CAAC,CAAC;QAC7F,CAAC;IACH,CAAC;IACD,0BAA0B;IAC1B,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,SAAS,KAAK,CAAC,EAAU,EAAE,IAAc;QACvC,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,OAAO;QAC5B,IAAI,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC5F,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACjB,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1B,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,SAAS,IAAI,EAAE;YAAE,KAAK,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;QACnE,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACpB,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAClB,CAAC;IACD,KAAK,MAAM,CAAC,IAAI,KAAK;QAAE,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AACzC,CAAC"}
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Operation } from 'effection';
|
|
2
|
+
import { Branch } from '@lloyal-labs/sdk';
|
|
3
|
+
import type { TraceEvent } from './trace-types';
|
|
4
|
+
/**
|
|
5
|
+
* Serialized state needed to reconstruct a Branch deterministically.
|
|
6
|
+
*
|
|
7
|
+
* `rootPrompt` is the tokenized prompt that initialized a shared root — the
|
|
8
|
+
* full formatted chat template including system prompt and any tool schemas.
|
|
9
|
+
* Captured from `prompt:format` events with `role: 'sharedRoot'`.
|
|
10
|
+
*
|
|
11
|
+
* `turns` is an ordered list of user/assistant turn pairs that were prefilled
|
|
12
|
+
* on top of the root via {@link extendSpine}. Empty for pool-start replay
|
|
13
|
+
* (any orchestration shape that doesn't extend the spine — e.g. `parallel`);
|
|
14
|
+
* populated for spine-extending orchestrations (`chain`, `fanout`, `dag`).
|
|
15
|
+
*
|
|
16
|
+
* @category Agents
|
|
17
|
+
*/
|
|
18
|
+
export interface BranchCheckpoint {
|
|
19
|
+
rootPrompt: string;
|
|
20
|
+
turns: Array<{
|
|
21
|
+
userContent: string;
|
|
22
|
+
assistantContent: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Extract the shared-root prompt from a trace, with no spine extensions.
|
|
27
|
+
*
|
|
28
|
+
* Useful for replaying parallel orchestrations, or for forking a fresh
|
|
29
|
+
* agent off the same pool-start state for A/B experiments.
|
|
30
|
+
*
|
|
31
|
+
* @param events - parsed JSONL trace events, in emission order
|
|
32
|
+
* @throws If no `prompt:format` event with `role: 'sharedRoot'` is found.
|
|
33
|
+
*
|
|
34
|
+
* @category Agents
|
|
35
|
+
*/
|
|
36
|
+
export declare function extractRootCheckpoint(events: TraceEvent[]): BranchCheckpoint;
|
|
37
|
+
/**
|
|
38
|
+
* Extract a full spine checkpoint — root prompt plus every `spine:extend`
|
|
39
|
+
* event in emission order.
|
|
40
|
+
*
|
|
41
|
+
* When `opts.poolTraceId` is set, only spine extensions under that pool's
|
|
42
|
+
* scope are included (useful when a trace contains multiple nested or
|
|
43
|
+
* sequential pools with independent spines — typically the research pool
|
|
44
|
+
* vs. a later synthesis pool, both extending their own roots).
|
|
45
|
+
*
|
|
46
|
+
* @param events - parsed JSONL trace events, in emission order
|
|
47
|
+
* @param opts.poolTraceId - filter extensions to this pool's scope
|
|
48
|
+
* @throws If no shared-root prompt is found.
|
|
49
|
+
*
|
|
50
|
+
* @category Agents
|
|
51
|
+
*/
|
|
52
|
+
export declare function extractSpineCheckpoint(events: TraceEvent[], opts?: {
|
|
53
|
+
poolTraceId?: number;
|
|
54
|
+
}): BranchCheckpoint;
|
|
55
|
+
/**
|
|
56
|
+
* Materialize a Branch reflecting the checkpointed state.
|
|
57
|
+
*
|
|
58
|
+
* Creates a fresh root at position 0 in the active `SessionContext`, prefills
|
|
59
|
+
* the tokenized root prompt, then applies each turn delta via `buildTurnDelta`
|
|
60
|
+
* + `store.prefill`. Registers an `ensure()` so the subtree is pruned when
|
|
61
|
+
* the caller's scope exits — lifetime follows the enclosing `scoped()` or
|
|
62
|
+
* `resource()`, matching how `withSharedRoot` manages its own root.
|
|
63
|
+
*
|
|
64
|
+
* Pass the returned branch as `parent` to `agentPool` to run a replacement
|
|
65
|
+
* stage (synth re-run, single-agent replay with modified prompt, etc.) against
|
|
66
|
+
* the reconstructed KV state.
|
|
67
|
+
*
|
|
68
|
+
* @example Replay a pool-start (parallel orchestration) with a modified task
|
|
69
|
+
* ```ts
|
|
70
|
+
* const events = parseTrace(tracePath);
|
|
71
|
+
* const checkpoint = extractRootCheckpoint(events);
|
|
72
|
+
* const root = yield* reconstructBranch(checkpoint);
|
|
73
|
+
* yield* agentPool({
|
|
74
|
+
* parent: root,
|
|
75
|
+
* orchestrate: parallel([{ content: modifiedTask, systemPrompt: modifiedSys }]),
|
|
76
|
+
* ...
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example Replay a spine-chain (research) state with a different synth prompt
|
|
81
|
+
* ```ts
|
|
82
|
+
* const events = parseTrace(tracePath);
|
|
83
|
+
* const checkpoint = extractSpineCheckpoint(events);
|
|
84
|
+
* const queryRoot = yield* reconstructBranch(checkpoint);
|
|
85
|
+
* yield* agentPool({
|
|
86
|
+
* parent: queryRoot,
|
|
87
|
+
* orchestrate: parallel([{ content: SYNTHESIZE.user }]),
|
|
88
|
+
* systemPrompt: SYNTHESIZE.system,
|
|
89
|
+
* ...
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @category Agents
|
|
94
|
+
*/
|
|
95
|
+
export declare function reconstructBranch(checkpoint: BranchCheckpoint): Operation<Branch>;
|
|
96
|
+
//# sourceMappingURL=replay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAkB,MAAM,kBAAkB,CAAC;AAE1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,gBAAgB,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACjE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,gBAAgB,CAW5E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,UAAU,EAAE,EACpB,IAAI,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAO,GAClC,gBAAgB,CASlB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAiB,iBAAiB,CAAC,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAC,MAAM,CAAC,CAgBlF"}
|
package/dist/replay.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractRootCheckpoint = extractRootCheckpoint;
|
|
4
|
+
exports.extractSpineCheckpoint = extractSpineCheckpoint;
|
|
5
|
+
exports.reconstructBranch = reconstructBranch;
|
|
6
|
+
const effection_1 = require("effection");
|
|
7
|
+
const sdk_1 = require("@lloyal-labs/sdk");
|
|
8
|
+
const context_1 = require("./context");
|
|
9
|
+
/**
|
|
10
|
+
* Extract the shared-root prompt from a trace, with no spine extensions.
|
|
11
|
+
*
|
|
12
|
+
* Useful for replaying parallel orchestrations, or for forking a fresh
|
|
13
|
+
* agent off the same pool-start state for A/B experiments.
|
|
14
|
+
*
|
|
15
|
+
* @param events - parsed JSONL trace events, in emission order
|
|
16
|
+
* @throws If no `prompt:format` event with `role: 'sharedRoot'` is found.
|
|
17
|
+
*
|
|
18
|
+
* @category Agents
|
|
19
|
+
*/
|
|
20
|
+
function extractRootCheckpoint(events) {
|
|
21
|
+
const sharedRoot = events.find((e) => e.type === 'prompt:format' && e.role === 'sharedRoot');
|
|
22
|
+
if (!sharedRoot) {
|
|
23
|
+
throw new Error('extractRootCheckpoint: no prompt:format event with role=sharedRoot found in trace');
|
|
24
|
+
}
|
|
25
|
+
return { rootPrompt: sharedRoot.promptText, turns: [] };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Extract a full spine checkpoint — root prompt plus every `spine:extend`
|
|
29
|
+
* event in emission order.
|
|
30
|
+
*
|
|
31
|
+
* When `opts.poolTraceId` is set, only spine extensions under that pool's
|
|
32
|
+
* scope are included (useful when a trace contains multiple nested or
|
|
33
|
+
* sequential pools with independent spines — typically the research pool
|
|
34
|
+
* vs. a later synthesis pool, both extending their own roots).
|
|
35
|
+
*
|
|
36
|
+
* @param events - parsed JSONL trace events, in emission order
|
|
37
|
+
* @param opts.poolTraceId - filter extensions to this pool's scope
|
|
38
|
+
* @throws If no shared-root prompt is found.
|
|
39
|
+
*
|
|
40
|
+
* @category Agents
|
|
41
|
+
*/
|
|
42
|
+
function extractSpineCheckpoint(events, opts = {}) {
|
|
43
|
+
const root = extractRootCheckpoint(events);
|
|
44
|
+
const turns = [];
|
|
45
|
+
for (const e of events) {
|
|
46
|
+
if (e.type !== 'spine:extend')
|
|
47
|
+
continue;
|
|
48
|
+
if (opts.poolTraceId != null && e.parentTraceId !== opts.poolTraceId)
|
|
49
|
+
continue;
|
|
50
|
+
turns.push({ userContent: e.userContent, assistantContent: e.assistantContent });
|
|
51
|
+
}
|
|
52
|
+
return { rootPrompt: root.rootPrompt, turns };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Materialize a Branch reflecting the checkpointed state.
|
|
56
|
+
*
|
|
57
|
+
* Creates a fresh root at position 0 in the active `SessionContext`, prefills
|
|
58
|
+
* the tokenized root prompt, then applies each turn delta via `buildTurnDelta`
|
|
59
|
+
* + `store.prefill`. Registers an `ensure()` so the subtree is pruned when
|
|
60
|
+
* the caller's scope exits — lifetime follows the enclosing `scoped()` or
|
|
61
|
+
* `resource()`, matching how `withSharedRoot` manages its own root.
|
|
62
|
+
*
|
|
63
|
+
* Pass the returned branch as `parent` to `agentPool` to run a replacement
|
|
64
|
+
* stage (synth re-run, single-agent replay with modified prompt, etc.) against
|
|
65
|
+
* the reconstructed KV state.
|
|
66
|
+
*
|
|
67
|
+
* @example Replay a pool-start (parallel orchestration) with a modified task
|
|
68
|
+
* ```ts
|
|
69
|
+
* const events = parseTrace(tracePath);
|
|
70
|
+
* const checkpoint = extractRootCheckpoint(events);
|
|
71
|
+
* const root = yield* reconstructBranch(checkpoint);
|
|
72
|
+
* yield* agentPool({
|
|
73
|
+
* parent: root,
|
|
74
|
+
* orchestrate: parallel([{ content: modifiedTask, systemPrompt: modifiedSys }]),
|
|
75
|
+
* ...
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @example Replay a spine-chain (research) state with a different synth prompt
|
|
80
|
+
* ```ts
|
|
81
|
+
* const events = parseTrace(tracePath);
|
|
82
|
+
* const checkpoint = extractSpineCheckpoint(events);
|
|
83
|
+
* const queryRoot = yield* reconstructBranch(checkpoint);
|
|
84
|
+
* yield* agentPool({
|
|
85
|
+
* parent: queryRoot,
|
|
86
|
+
* orchestrate: parallel([{ content: SYNTHESIZE.user }]),
|
|
87
|
+
* systemPrompt: SYNTHESIZE.system,
|
|
88
|
+
* ...
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @category Agents
|
|
93
|
+
*/
|
|
94
|
+
function* reconstructBranch(checkpoint) {
|
|
95
|
+
const ctx = yield* context_1.Ctx.expect();
|
|
96
|
+
const store = yield* context_1.Store.expect();
|
|
97
|
+
const root = sdk_1.Branch.create(ctx, 0, {});
|
|
98
|
+
yield* (0, effection_1.ensure)(() => { if (!root.disposed)
|
|
99
|
+
root.pruneSubtreeSync(); });
|
|
100
|
+
const rootTokens = ctx.tokenizeSync(checkpoint.rootPrompt, false);
|
|
101
|
+
yield* (0, effection_1.call)(() => root.prefill(rootTokens));
|
|
102
|
+
for (const turn of checkpoint.turns) {
|
|
103
|
+
const delta = (0, sdk_1.buildTurnDelta)(ctx, turn.userContent, turn.assistantContent);
|
|
104
|
+
yield* (0, effection_1.call)(() => store.prefill([[root, delta]]));
|
|
105
|
+
}
|
|
106
|
+
return root;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=replay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.js","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":";;AAoCA,sDAWC;AAiBD,wDAYC;AA0CD,8CAgBC;AAtID,yCAAyC;AAEzC,0CAA0D;AAC1D,uCAAuC;AAsBvC;;;;;;;;;;GAUG;AACH,SAAgB,qBAAqB,CAAC,MAAoB;IACxD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAC5B,CAAC,CAAC,EAAu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,KAAK,CAAC,CAAC,IAAA,kBAAM,EAAC,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ;QAAE,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAEtE,MAAM,UAAU,GAAG,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAClE,KAAK,CAAC,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;IAE5C,KAAK,MAAM,IAAI,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC;QACpC,MAAM,KAAK,GAAG,IAAA,oBAAc,EAAC,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC;QAC3E,KAAK,CAAC,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACpD,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/shared-root.d.ts
CHANGED
|
@@ -7,10 +7,6 @@ import type { SamplingParams } from "./types";
|
|
|
7
7
|
* @category Agents
|
|
8
8
|
*/
|
|
9
9
|
export interface SharedRootOptions {
|
|
10
|
-
/** System prompt to tokenize and prefill into the shared root */
|
|
11
|
-
systemPrompt: string;
|
|
12
|
-
/** JSON-serialized tool schemas for tool-aware prompt formatting */
|
|
13
|
-
tools?: string;
|
|
14
10
|
/** Sampling parameters for the root branch */
|
|
15
11
|
params?: SamplingParams;
|
|
16
12
|
/**
|
|
@@ -24,32 +20,74 @@ export interface SharedRootOptions {
|
|
|
24
20
|
*
|
|
25
21
|
* When provided, the root inherits the parent's full KV state —
|
|
26
22
|
* every tool call, tool result, and generated token the parent
|
|
27
|
-
* accumulated.
|
|
28
|
-
*
|
|
29
|
-
* complete attention state (Continuous Context).
|
|
23
|
+
* accumulated. Sub-agents forking from this root attend over the
|
|
24
|
+
* parent's complete attention state (Continuous Context).
|
|
30
25
|
*
|
|
31
26
|
* When omitted, creates a fresh root at position 0 (cold start).
|
|
32
27
|
*/
|
|
33
28
|
parent?: Branch;
|
|
29
|
+
/**
|
|
30
|
+
* When set, prefill the chat-format `[system + tools]` header onto the
|
|
31
|
+
* root once at setup. Every agent forking from the root inherits these
|
|
32
|
+
* tokens via `forkSync`'s metadata-only KV prefix-share — the role and
|
|
33
|
+
* tool schemas appear ONCE in physical KV regardless of how many agents
|
|
34
|
+
* the pool spawns.
|
|
35
|
+
*
|
|
36
|
+
* The resulting `FormatConfig` (parser/grammar/format/triggers) is set
|
|
37
|
+
* on the {@link RootFmt} context so `setupAgent` can detect shared mode,
|
|
38
|
+
* skip its own system+tools formatting, and inherit the dispatch-side
|
|
39
|
+
* fmt from the root.
|
|
40
|
+
*
|
|
41
|
+
* Use this for orchestrators where every agent shares the same role —
|
|
42
|
+
* chain-mode research pools, fanout-style same-role pools, etc. Mixed-
|
|
43
|
+
* role workflows (research → compare → synthesize) keep using per-spec
|
|
44
|
+
* `SpawnSpec.systemPrompt` and don't pass this option.
|
|
45
|
+
*/
|
|
46
|
+
systemPrompt?: string;
|
|
47
|
+
/**
|
|
48
|
+
* JSON-serialized tool schemas to embed in the chat-format header
|
|
49
|
+
* prefilled at setup. Format matches `FormatChatOptions.tools` — output
|
|
50
|
+
* of `createToolkit(...).toolsJson`. Only applied when `systemPrompt` is
|
|
51
|
+
* also set; ignored otherwise.
|
|
52
|
+
*/
|
|
53
|
+
toolsJson?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Whether to enable thinking-mode tokens (e.g. `<think>` blocks) when
|
|
56
|
+
* formatting the shared root header. Threaded through to the chat-format
|
|
57
|
+
* call AND stored on the `RootFmt` FormatConfig so `setupAgent`'s
|
|
58
|
+
* shared-mode shortcut copies a parser/grammar/triggers configuration
|
|
59
|
+
* consistent with the per-agent suffix formatting.
|
|
60
|
+
*
|
|
61
|
+
* Should match the `enableThinking` value the caller passes to the agent
|
|
62
|
+
* pool — divergent values produce inconsistent grammar between the
|
|
63
|
+
* prefilled root and per-agent suffixes.
|
|
64
|
+
*
|
|
65
|
+
* @default false
|
|
66
|
+
*/
|
|
67
|
+
enableThinking?: boolean;
|
|
34
68
|
}
|
|
35
69
|
/**
|
|
36
70
|
* Scoped shared root branch with guaranteed cleanup
|
|
37
71
|
*
|
|
38
|
-
* Creates (or forks) a root branch
|
|
39
|
-
*
|
|
40
|
-
*
|
|
72
|
+
* Creates (or forks) a root branch for the pool's agents to fork from.
|
|
73
|
+
* The root is pruned via try/finally when the body returns or throws,
|
|
74
|
+
* regardless of whether children still exist.
|
|
75
|
+
*
|
|
76
|
+
* Each agent's chat format (system + user + generation prompt) is rendered
|
|
77
|
+
* fresh inside `setupAgent`, so this root carries no chat context itself —
|
|
78
|
+
* it exists as the pool's branching point and as the spine that
|
|
79
|
+
* `ctx.extendRoot` writes onto between tasks.
|
|
41
80
|
*
|
|
42
|
-
* **Cold path** (no `parent`): creates root at position 0
|
|
43
|
-
*
|
|
81
|
+
* **Cold path** (no `parent`): creates a root at position 0 with no prefill.
|
|
82
|
+
* Agents fork at position 0; their full chat context lives in their own suffix.
|
|
44
83
|
*
|
|
45
|
-
* **Warm path** (`parent` provided): forks from parent
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* attend over the calling agent's accumulated evidence.
|
|
84
|
+
* **Warm path** (`parent` provided): forks from parent and prefills a turn
|
|
85
|
+
* separator so subsequent agent suffixes land on a clean turn boundary.
|
|
86
|
+
* Sub-agents inherit the parent's full KV state via the fork.
|
|
49
87
|
*
|
|
50
|
-
* @param opts -
|
|
88
|
+
* @param opts - Sampling parameters and optional parent branch
|
|
51
89
|
* @param body - Operation that receives the root branch and prefix length.
|
|
52
|
-
* Typically calls {@link
|
|
90
|
+
* Typically calls {@link useAgentPool} inside.
|
|
53
91
|
* @returns The body's return value
|
|
54
92
|
*
|
|
55
93
|
* @category Agents
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-root.d.ts","sourceRoot":"","sources":["../src/shared-root.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"shared-root.d.ts","sourceRoot":"","sources":["../src/shared-root.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,8CAA8C;IAC9C,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;;;;;;;;OASG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;;;;;;;;;;OAgBG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;;;;;OAYG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAiB,cAAc,CAAC,CAAC,EAC/B,IAAI,EAAE,iBAAiB,EACvB,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,GAC/D,SAAS,CAAC,CAAC,CAAC,CAsHd"}
|