@oona/vlo 0.0.0-stage → 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/agent.js ADDED
@@ -0,0 +1,324 @@
1
+ import { d as activity, f as signal } from "./domain-Bs8TeDCv.js";
2
+ import { z } from "zod";
3
+ import { d as WorkflowFailure, m as cmd } from "./src-DVCYkPA7.js";
4
+ // src/agent/decision.ts
5
+ /**
6
+ * `decision` — the decider is an ordinary activity whose output is a typed, journaled
7
+ * action. The tool registry fixes its output schema, so an LLM worker is
8
+ * constrained to names the loop can actually dispatch.
9
+ */
10
+ /** Reserved tool name: the decider is finished and `final` (or the last candidate) is the answer. */
11
+ const DONE = "done";
12
+ const JSON_VALUE = z.lazy(() => z.union([
13
+ z.null(),
14
+ z.boolean(),
15
+ z.number(),
16
+ z.string(),
17
+ z.array(JSON_VALUE),
18
+ z.record(z.string(), JSON_VALUE)
19
+ ]));
20
+ function decision(name, options) {
21
+ const tools = Object.keys(options.tools);
22
+ if (tools.length === 0) throw new TypeError("decision: no tools given");
23
+ if (tools.includes("done")) throw new TypeError(`decision: "${DONE}" is a reserved tool name`);
24
+ return activity(name, {
25
+ input: options.input,
26
+ output: z.object({
27
+ tool: z.enum([...tools, DONE]),
28
+ args: z.unknown().optional(),
29
+ rationale: z.string().optional(),
30
+ final: JSON_VALUE.optional()
31
+ }),
32
+ ...options.retry !== void 0 ? { retry: options.retry } : {},
33
+ ...options.queue !== void 0 ? { queue: options.queue } : {},
34
+ ...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {}
35
+ });
36
+ }
37
+ // src/agent/budget.ts
38
+ /**
39
+ * The budget: ceilings on what one run may spend, and the signal that raises them.
40
+ *
41
+ * Exhaustion parks the run rather than failing it, so the work already paid
42
+ * for survives while a human decides whether to keep going.
43
+ */
44
+ const DEFAULT_TOPUP_SIGNAL = "agent.budget_topup";
45
+ const TOPUP_SCHEMA = z.object({
46
+ tokens: z.number().nonnegative().optional(),
47
+ costMicroUsd: z.number().nonnegative().optional(),
48
+ steps: z.number().int().nonnegative().optional()
49
+ });
50
+ /** The signal a parked agent waits on. Pass the result to `agentLoop({ topup })`. */
51
+ function budgetTopup(name = DEFAULT_TOPUP_SIGNAL) {
52
+ return signal(name, TOPUP_SCHEMA);
53
+ }
54
+ /** True once any configured ceiling is reached. Absent ceilings never bind. */
55
+ function isExhausted(budget, spent, iteration) {
56
+ if (budget.tokens !== void 0 && spent.tokens >= budget.tokens) return true;
57
+ if (budget.costMicroUsd !== void 0 && spent.cost_micro_usd >= budget.costMicroUsd) return true;
58
+ if (budget.steps !== void 0 && iteration >= budget.steps) return true;
59
+ return false;
60
+ }
61
+ /**
62
+ * Raises the ceilings a top-up names. A ceiling that was never set stays unbounded —
63
+ * granting 100 tokens to an unlimited budget must not turn it into a limit of 100.
64
+ */
65
+ function grant(budget, topup) {
66
+ return {
67
+ ...budget.tokens !== void 0 ? { tokens: budget.tokens + (topup.tokens ?? 0) } : {},
68
+ ...budget.costMicroUsd !== void 0 ? { costMicroUsd: budget.costMicroUsd + (topup.costMicroUsd ?? 0) } : {},
69
+ ...budget.steps !== void 0 ? { steps: budget.steps + (topup.steps ?? 0) } : {}
70
+ };
71
+ }
72
+ // src/agent/state.ts
73
+ const NO_USAGE = {
74
+ cpu_ms: 0,
75
+ tokens: 0,
76
+ cost_micro_usd: 0
77
+ };
78
+ /** The run total the kernel banked, including the event that carries it. */
79
+ function spentOf(event) {
80
+ return event.usage_total;
81
+ }
82
+ /** These shapes are JSON by construction; TypeScript cannot see it through optional keys. */
83
+ function asJson(value) {
84
+ return value;
85
+ }
86
+ // src/agent/loop.ts
87
+ /**
88
+ * `agentLoop` — decide → tool → evaluate, as a pure reducer.
89
+ *
90
+ * Nothing loops at runtime: every arrow below is one journaled event folded into state, so
91
+ * the run survives a crash mid-thought and replays deterministically. The budget is a
92
+ * reducer decision over the usage the kernel banks on each event — exhaustion parks the run
93
+ * on a top-up signal instead of failing it.
94
+ */
95
+ const DEFAULT_MAX_ITERATIONS = 50;
96
+ const DEFAULT_MEMORY_LIMIT = 20;
97
+ /** One step id per phase per iteration, so a replay asks the kernel for the same step. */
98
+ const STEP_ID = {
99
+ decide: (i) => `decide#${i}`,
100
+ tool: (i) => `tool#${i}`,
101
+ evaluate: (i) => `evaluate#${i}`,
102
+ topup: (i) => `topup#${i}`
103
+ };
104
+ function agentLoop(options) {
105
+ const cfg = compile(options);
106
+ return {
107
+ init: (goal) => ({
108
+ phase: "decide",
109
+ goal,
110
+ iteration: 0,
111
+ memory: [],
112
+ budget: cfg.budget,
113
+ spent: NO_USAGE
114
+ }),
115
+ on: {
116
+ started: (state) => decide(cfg, state),
117
+ task_done: (state, event) => onTaskDone(cfg, state, event),
118
+ signal: (state, event) => onSignal(cfg, state, event)
119
+ }
120
+ };
121
+ }
122
+ function compile(options) {
123
+ const tools = new Map(Object.entries(options.tools).map(([name, target]) => [name, erase(target)]));
124
+ if (tools.size === 0) throw new TypeError("agentLoop: no tools given");
125
+ const maxIterations = options.maxIterations ?? 50;
126
+ const memoryLimit = options.memoryLimit ?? 20;
127
+ if (maxIterations < 1) throw new TypeError("agentLoop: maxIterations must be >= 1");
128
+ if (memoryLimit < 1) throw new TypeError("agentLoop: memoryLimit must be >= 1");
129
+ return {
130
+ decide: erase(options.decide),
131
+ tools,
132
+ evaluate: options.evaluate === void 0 ? void 0 : erase(options.evaluate),
133
+ threshold: options.threshold,
134
+ budget: options.budget,
135
+ topup: options.topup ?? budgetTopup(),
136
+ maxIterations,
137
+ memoryLimit
138
+ };
139
+ }
140
+ const erase = (target) => target;
141
+ function decide(cfg, state) {
142
+ const at = {
143
+ ...clearPending(state),
144
+ phase: "decide"
145
+ };
146
+ const input = asJson({
147
+ goal: at.goal,
148
+ memory: at.memory,
149
+ candidate: at.candidate ?? null,
150
+ score: at.score ?? null
151
+ });
152
+ return [at, [cmd.task(cfg.decide, input, { id: STEP_ID.decide(at.iteration) })]];
153
+ }
154
+ /**
155
+ * A result the loop is not waiting for — a duplicate delivery, or one from an iteration
156
+ * already left behind — is folded and ignored, exactly as the `sequence` helper does.
157
+ */
158
+ function onTaskDone(cfg, state, event) {
159
+ if (state.phase === "parked" || state.phase === "done") return [state, []];
160
+ if (event.step_id !== STEP_ID[state.phase](state.iteration)) return [state, []];
161
+ const at = {
162
+ ...state,
163
+ spent: spentOf(event)
164
+ };
165
+ if (event.status !== "succeeded") return observeFailure(cfg, at, event);
166
+ if (state.phase === "decide") return onDecided(cfg, at, event);
167
+ if (state.phase === "tool") return onToolDone(cfg, at, event);
168
+ return onEvaluated(cfg, at, event);
169
+ }
170
+ /** Agents route around failures: a task that exhausted its retries becomes an observation. */
171
+ function observeFailure(cfg, state, event) {
172
+ return advance(cfg, remember(cfg, state, {
173
+ tool: state.pending?.tool ?? event.activity,
174
+ error: event.error ?? event.status
175
+ }));
176
+ }
177
+ function onDecided(cfg, state, event) {
178
+ const decision = readDecision(event.output ?? null);
179
+ if (decision.tool === "done") return complete(state, decision.final ?? state.candidate ?? null, false);
180
+ const target = cfg.tools.get(decision.tool);
181
+ if (target === void 0) {
182
+ const error = `unknown tool "${decision.tool}"`;
183
+ return advance(cfg, remember(cfg, state, {
184
+ tool: decision.tool,
185
+ error
186
+ }));
187
+ }
188
+ const parsed = target.input.safeParse(decision.args ?? null);
189
+ if (!parsed.success) {
190
+ const error = `invalid args: ${parsed.error.issues.map((issue) => issue.message).join("; ")}`;
191
+ return advance(cfg, remember(cfg, state, {
192
+ tool: decision.tool,
193
+ args: asJson(decision.args ?? null),
194
+ error
195
+ }));
196
+ }
197
+ const args = parsed.data;
198
+ return [{
199
+ ...state,
200
+ phase: "tool",
201
+ pending: {
202
+ tool: decision.tool,
203
+ args
204
+ }
205
+ }, [cmd.task(target, args, { id: STEP_ID.tool(state.iteration) })]];
206
+ }
207
+ function onToolDone(cfg, state, event) {
208
+ const output = event.output ?? null;
209
+ const pending = state.pending;
210
+ const at = {
211
+ ...remember(cfg, state, {
212
+ tool: pending?.tool ?? event.activity,
213
+ ...pending !== void 0 ? { args: pending.args } : {},
214
+ output
215
+ }),
216
+ candidate: output
217
+ };
218
+ if (cfg.evaluate === void 0) return advance(cfg, at);
219
+ const input = asJson({
220
+ goal: at.goal,
221
+ candidate: output
222
+ });
223
+ return [{
224
+ ...at,
225
+ phase: "evaluate"
226
+ }, [cmd.task(cfg.evaluate, input, { id: STEP_ID.evaluate(state.iteration) })]];
227
+ }
228
+ function onEvaluated(cfg, state, event) {
229
+ const score = readScore(event.output ?? null);
230
+ const at = {
231
+ ...state,
232
+ score,
233
+ memory: scoreLast(state.memory, score)
234
+ };
235
+ if (cfg.threshold !== void 0 && score >= cfg.threshold) return complete(at, at.candidate ?? null, false);
236
+ return advance(cfg, at);
237
+ }
238
+ function onSignal(cfg, state, event) {
239
+ if (state.phase !== "parked" || event.name !== cfg.topup.name) return [state, []];
240
+ const granted = cfg.topup.schema.safeParse(event.payload);
241
+ return decide(cfg, {
242
+ ...state,
243
+ spent: spentOf(event),
244
+ budget: grant(state.budget, granted.success ? granted.data : {})
245
+ });
246
+ }
247
+ /** One cycle is over: charge it against the ceilings, then decide again — or park. */
248
+ function advance(cfg, state) {
249
+ const at = {
250
+ ...state,
251
+ iteration: state.iteration + 1
252
+ };
253
+ if (at.iteration >= cfg.maxIterations) return complete(at, at.candidate ?? null, true);
254
+ if (isExhausted(at.budget, at.spent, at.iteration)) return park(cfg, at);
255
+ return decide(cfg, at);
256
+ }
257
+ function park(cfg, state) {
258
+ const at = {
259
+ ...clearPending(state),
260
+ phase: "parked"
261
+ };
262
+ const parked = asJson({ parked: {
263
+ iteration: at.iteration,
264
+ budget: at.budget,
265
+ spent: at.spent,
266
+ candidate: at.candidate ?? null,
267
+ score: at.score ?? null
268
+ } });
269
+ return [at, [cmd.emit("agent", parked), cmd.awaitSignal(cfg.topup, { id: STEP_ID.topup(at.iteration) })]];
270
+ }
271
+ function complete(state, candidate, exhausted) {
272
+ const result = {
273
+ candidate,
274
+ ...state.score !== void 0 ? { score: state.score } : {},
275
+ iterations: exhausted ? state.iteration : state.iteration + 1,
276
+ spent: state.spent,
277
+ ...exhausted ? { exhausted: true } : {}
278
+ };
279
+ return [{
280
+ ...state,
281
+ phase: "done",
282
+ candidate
283
+ }, [cmd.complete(asJson(result))]];
284
+ }
285
+ function remember(cfg, state, observation) {
286
+ return {
287
+ ...state,
288
+ memory: [...state.memory, observation].slice(-cfg.memoryLimit)
289
+ };
290
+ }
291
+ function scoreLast(memory, score) {
292
+ const last = memory[memory.length - 1];
293
+ if (last === void 0) return [];
294
+ return [...memory.slice(0, -1), {
295
+ ...last,
296
+ score
297
+ }];
298
+ }
299
+ /** A new decision supersedes whatever the previous one dispatched. */
300
+ function clearPending(state) {
301
+ const next = { ...state };
302
+ delete next.pending;
303
+ return next;
304
+ }
305
+ function readDecision(output) {
306
+ if (output === null || typeof output !== "object" || Array.isArray(output)) throw new WorkflowFailure("agent: decide returned no decision object", output);
307
+ const tool = output["tool"];
308
+ if (typeof tool !== "string") throw new WorkflowFailure("agent: decide returned no `tool`", output);
309
+ return {
310
+ tool,
311
+ args: output["args"],
312
+ rationale: typeof output["rationale"] === "string" ? output["rationale"] : void 0,
313
+ final: output["final"]
314
+ };
315
+ }
316
+ function readScore(output) {
317
+ if (typeof output === "number") return output;
318
+ if (output !== null && typeof output === "object" && !Array.isArray(output)) {
319
+ const score = output["score"];
320
+ if (typeof score === "number") return score;
321
+ }
322
+ throw new WorkflowFailure("agent: evaluate returned no numeric `score`", output);
323
+ }
324
+ export { DEFAULT_MAX_ITERATIONS, DEFAULT_MEMORY_LIMIT, DEFAULT_TOPUP_SIGNAL, DONE, agentLoop, budgetTopup, decision };
package/dist/cli.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The `vlo` command: one verb per file under `cli/`.
3
+ *
4
+ * `dev` and `serve` are the same host with different defaults — that is deliberate, and it
5
+ * is why `doctor` and `deploy` read the same profile: whatever runs on a laptop, what a
6
+ * check reports and what a template deploys all come from one place in `vlo.config.ts`.
7
+ */
8
+ export declare function main(argv?: readonly string[], cwd?: string): Promise<void>;