@useavalon/caelence-agent 0.2.5-canary.3 → 0.3.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 +52 -0
- package/package.json +3 -3
- package/src/browser/browser-use.test.ts +131 -0
- package/src/browser/browser-use.ts +145 -0
- package/src/config.ts +16 -0
- package/src/core/approval.ts +18 -2
- package/src/core/run-turn.ts +77 -0
- package/src/core/stdio-mcp.test.ts +120 -0
- package/src/core/stdio-mcp.ts +64 -4
- package/src/decisions/approval.ts +47 -0
- package/src/decisions/client.ts +134 -0
- package/src/decisions/config.ts +83 -0
- package/src/decisions/decisions.test.ts +365 -0
- package/src/decisions/integration.test.ts +473 -0
- package/src/decisions/memory.ts +37 -0
- package/src/decisions/shortcuts.ts +28 -0
- package/src/index.ts +22 -0
- package/src/runtime.test.ts +167 -0
- package/src/runtime.ts +80 -5
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { createExecApprovalGate } from "../core/approval.ts";
|
|
3
|
+
import { classifyToolApproval } from "./approval.ts";
|
|
4
|
+
import {
|
|
5
|
+
clipState,
|
|
6
|
+
DEFAULT_JEV_MODEL,
|
|
7
|
+
decisionsUrl,
|
|
8
|
+
type FetchImpl,
|
|
9
|
+
readChoiceAnswer,
|
|
10
|
+
readNoulAnswer,
|
|
11
|
+
submitDecisions,
|
|
12
|
+
} from "./client.ts";
|
|
13
|
+
import { type ResolvedDecisions, resolveDecisions } from "./config.ts";
|
|
14
|
+
import { turnHasDurableFact } from "./memory.ts";
|
|
15
|
+
|
|
16
|
+
/** Shape copied from the OpenRouter Decisions API reference. */
|
|
17
|
+
const DOC_RESPONSE = {
|
|
18
|
+
answers: {
|
|
19
|
+
is_bug: { noul: 0.96, type: "noul" },
|
|
20
|
+
team: {
|
|
21
|
+
choice: "payments",
|
|
22
|
+
confidence: 0.75,
|
|
23
|
+
probabilities: { account: 0, frontend: 0.16, payments: 0.84 },
|
|
24
|
+
type: "choice",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
model: "typesafe/jev-1.14-20260917",
|
|
28
|
+
usage: { cost: 0.00002, input_tokens: 476, output_tokens: 70 },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function fakeFetch(body: unknown, init: { ok?: boolean } = {}) {
|
|
32
|
+
const calls: Array<{ url: string; body: Record<string, unknown>; headers: HeadersInit }> = [];
|
|
33
|
+
const impl: FetchImpl = async (url, req) => {
|
|
34
|
+
calls.push({
|
|
35
|
+
url: String(url),
|
|
36
|
+
body: JSON.parse(String(req?.body)),
|
|
37
|
+
headers: req?.headers ?? {},
|
|
38
|
+
});
|
|
39
|
+
return new Response(JSON.stringify(body), { status: init.ok === false ? 500 : 200 });
|
|
40
|
+
};
|
|
41
|
+
return { impl, calls };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function decisions(
|
|
45
|
+
fetchImpl: FetchImpl,
|
|
46
|
+
patch: Partial<ResolvedDecisions> = {},
|
|
47
|
+
): ResolvedDecisions {
|
|
48
|
+
return {
|
|
49
|
+
model: DEFAULT_JEV_MODEL,
|
|
50
|
+
minConfidence: 0.6,
|
|
51
|
+
approvalThreshold: 0.9,
|
|
52
|
+
shortcuts: true,
|
|
53
|
+
approvals: true,
|
|
54
|
+
memory: true,
|
|
55
|
+
apiKey: "sk-test",
|
|
56
|
+
baseUrl: "https://openrouter.ai/api/v1",
|
|
57
|
+
fetchImpl,
|
|
58
|
+
...patch,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe("decisions client", () => {
|
|
63
|
+
test("reads the documented answer shapes", () => {
|
|
64
|
+
expect(readNoulAnswer(DOC_RESPONSE, "is_bug")).toBe(0.96);
|
|
65
|
+
expect(readChoiceAnswer(DOC_RESPONSE, "team")).toEqual({
|
|
66
|
+
choice: "payments",
|
|
67
|
+
confidence: 0.75,
|
|
68
|
+
});
|
|
69
|
+
expect(readNoulAnswer(DOC_RESPONSE, "team")).toBeNull();
|
|
70
|
+
expect(readChoiceAnswer(DOC_RESPONSE, "missing")).toBeNull();
|
|
71
|
+
expect(readNoulAnswer(null, "is_bug")).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("falls back to the top probability when confidence is absent", () => {
|
|
75
|
+
const json = { answers: { x: { choice: "a", probabilities: { a: 0.7, b: 0.3 } } } };
|
|
76
|
+
expect(readChoiceAnswer(json, "x")?.confidence).toBe(0.7);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("posts model, state, and questions with a bearer key", async () => {
|
|
80
|
+
const { impl, calls } = fakeFetch(DOC_RESPONSE);
|
|
81
|
+
const out = await submitDecisions({
|
|
82
|
+
apiKey: "sk-test",
|
|
83
|
+
state: { a: 1 },
|
|
84
|
+
questions: { q: { type: "noul" } },
|
|
85
|
+
fetchImpl: impl,
|
|
86
|
+
});
|
|
87
|
+
expect(out).not.toBeNull();
|
|
88
|
+
expect(calls[0]?.url).toBe("https://openrouter.ai/api/alpha/decisions");
|
|
89
|
+
expect(calls[0]?.body.model).toBe(DEFAULT_JEV_MODEL);
|
|
90
|
+
expect(calls[0]?.body.state).toEqual({ a: 1 });
|
|
91
|
+
const headers = (calls[0]?.headers ?? {}) as Record<string, string>;
|
|
92
|
+
expect(headers.Authorization).toBe("Bearer sk-test");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("returns null instead of throwing", async () => {
|
|
96
|
+
const input = { apiKey: "sk-test", state: {}, questions: {} };
|
|
97
|
+
expect(await submitDecisions({ ...input, apiKey: " " })).toBeNull();
|
|
98
|
+
expect(
|
|
99
|
+
await submitDecisions({ ...input, fetchImpl: fakeFetch({}, { ok: false }).impl }),
|
|
100
|
+
).toBeNull();
|
|
101
|
+
expect(await submitDecisions({ ...input, fetchImpl: fakeFetch({ nope: 1 }).impl })).toBeNull();
|
|
102
|
+
const boom: FetchImpl = async () => {
|
|
103
|
+
throw new Error("network down");
|
|
104
|
+
};
|
|
105
|
+
expect(await submitDecisions({ ...input, fetchImpl: boom })).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("gives up after the timeout", async () => {
|
|
109
|
+
const never: FetchImpl = (_url, init) =>
|
|
110
|
+
new Promise((_resolve, reject) => {
|
|
111
|
+
init?.signal?.addEventListener("abort", () => reject(new Error("aborted")));
|
|
112
|
+
});
|
|
113
|
+
const started = Date.now();
|
|
114
|
+
const out = await submitDecisions({
|
|
115
|
+
apiKey: "sk-test",
|
|
116
|
+
state: {},
|
|
117
|
+
questions: {},
|
|
118
|
+
timeoutMs: 50,
|
|
119
|
+
fetchImpl: never,
|
|
120
|
+
});
|
|
121
|
+
expect(out).toBeNull();
|
|
122
|
+
expect(Date.now() - started).toBeLessThan(2000);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("builds the alpha URL beside /v1", () => {
|
|
126
|
+
expect(decisionsUrl()).toBe("https://openrouter.ai/api/alpha/decisions");
|
|
127
|
+
expect(decisionsUrl("https://proxy.example.com/api/v1/")).toBe(
|
|
128
|
+
"https://proxy.example.com/api/alpha/decisions",
|
|
129
|
+
);
|
|
130
|
+
expect(decisionsUrl("https://proxy.example.com/v1")).toBe(
|
|
131
|
+
"https://proxy.example.com/api/alpha/decisions",
|
|
132
|
+
);
|
|
133
|
+
expect(decisionsUrl("http://localhost:8787")).toBe("http://localhost:8787/api/alpha/decisions");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("matches OpenRouter by hostname, never by substring", () => {
|
|
137
|
+
// A proxy whose path mentions openrouter.ai must keep its own host.
|
|
138
|
+
expect(decisionsUrl("https://proxy.example.com/openrouter.ai/v1")).toBe(
|
|
139
|
+
"https://proxy.example.com/openrouter.ai/api/alpha/decisions",
|
|
140
|
+
);
|
|
141
|
+
expect(decisionsUrl("https://openrouter.ai.evil.example/v1")).toBe(
|
|
142
|
+
"https://openrouter.ai.evil.example/api/alpha/decisions",
|
|
143
|
+
);
|
|
144
|
+
expect(decisionsUrl("https://notopenrouter.ai/v1")).toBe(
|
|
145
|
+
"https://notopenrouter.ai/api/alpha/decisions",
|
|
146
|
+
);
|
|
147
|
+
expect(decisionsUrl("https://OpenRouter.AI/api/v1")).toBe(
|
|
148
|
+
"https://openrouter.ai/api/alpha/decisions",
|
|
149
|
+
);
|
|
150
|
+
expect(decisionsUrl("https://eu.openrouter.ai/api/v1")).toBe(
|
|
151
|
+
"https://openrouter.ai/api/alpha/decisions",
|
|
152
|
+
);
|
|
153
|
+
expect(decisionsUrl("not a url")).toBe("not a url/api/alpha/decisions");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("clips long strings and deep or long structures", () => {
|
|
157
|
+
const clipped = clipState({
|
|
158
|
+
body: "x".repeat(5000),
|
|
159
|
+
list: Array.from({ length: 50 }, (_, i) => i),
|
|
160
|
+
});
|
|
161
|
+
const row = clipped as { body: string; list: number[] };
|
|
162
|
+
expect(row.body.length).toBeLessThan(700);
|
|
163
|
+
expect(row.body).toContain("5000 chars");
|
|
164
|
+
expect(row.list).toHaveLength(20);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("resolveDecisions", () => {
|
|
169
|
+
const env = {};
|
|
170
|
+
test("is on by default when a key is set", () => {
|
|
171
|
+
const out = resolveDecisions({}, "sk-test", "https://openrouter.ai/api/v1", env);
|
|
172
|
+
expect(out?.model).toBe(DEFAULT_JEV_MODEL);
|
|
173
|
+
expect(out?.shortcuts && out.approvals && out.memory).toBe(true);
|
|
174
|
+
expect(out?.approvalThreshold).toBe(0.9);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("is off without a key", () => {
|
|
178
|
+
expect(resolveDecisions({}, " ", "https://openrouter.ai/api/v1", env)).toBeUndefined();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("is off by config or HARNESS_JEV", () => {
|
|
182
|
+
const url = "https://openrouter.ai/api/v1";
|
|
183
|
+
expect(resolveDecisions({ decisions: { enabled: false } }, "k", url, env)).toBeUndefined();
|
|
184
|
+
for (const value of ["0", "false", "off"]) {
|
|
185
|
+
expect(resolveDecisions({}, "k", url, { HARNESS_JEV: value })).toBeUndefined();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("falls back to the pinned model when model is not a usable string", () => {
|
|
190
|
+
const url = "https://openrouter.ai/api/v1";
|
|
191
|
+
for (const model of [42, null, {}, ["x"], " ", ""]) {
|
|
192
|
+
const config = { decisions: { model } } as unknown as Parameters<typeof resolveDecisions>[0];
|
|
193
|
+
expect(resolveDecisions(config, "k", url, env)?.model).toBe(DEFAULT_JEV_MODEL);
|
|
194
|
+
}
|
|
195
|
+
const custom = { decisions: { model: " typesafe/jev-1.13 " } };
|
|
196
|
+
expect(resolveDecisions(custom, "k", url, env)?.model).toBe("typesafe/jev-1.13");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("switches features off individually and ignores nonsense numbers", () => {
|
|
200
|
+
const out = resolveDecisions(
|
|
201
|
+
{ decisions: { approvals: false, memory: false, approvalThreshold: 7, minConfidence: -1 } },
|
|
202
|
+
"k",
|
|
203
|
+
"https://openrouter.ai/api/v1",
|
|
204
|
+
env,
|
|
205
|
+
);
|
|
206
|
+
expect(out?.approvals).toBe(false);
|
|
207
|
+
expect(out?.memory).toBe(false);
|
|
208
|
+
expect(out?.shortcuts).toBe(true);
|
|
209
|
+
expect(out?.approvalThreshold).toBe(0.9);
|
|
210
|
+
expect(out?.minConfidence).toBe(0.6);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("approval assist", () => {
|
|
215
|
+
const exec = { callId: "1", toolName: "exec", input: { command: "bun test" } };
|
|
216
|
+
const noul = (value: number) => fakeFetch({ answers: { safe_to_run: { noul: value } } });
|
|
217
|
+
|
|
218
|
+
test("allows only at or above the threshold", async () => {
|
|
219
|
+
expect(await classifyToolApproval(decisions(noul(0.95).impl), exec, "run the tests")).toBe(
|
|
220
|
+
"allow",
|
|
221
|
+
);
|
|
222
|
+
expect(await classifyToolApproval(decisions(noul(0.9).impl), exec, "run the tests")).toBe(
|
|
223
|
+
"allow",
|
|
224
|
+
);
|
|
225
|
+
expect(await classifyToolApproval(decisions(noul(0.89).impl), exec, "run the tests")).toBe(
|
|
226
|
+
"ask",
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("a low score asks and never denies", async () => {
|
|
231
|
+
const verdict = await classifyToolApproval(decisions(noul(0.01).impl), exec, "run the tests");
|
|
232
|
+
expect(verdict).toBe("ask");
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("destructive commands never reach the model", async () => {
|
|
236
|
+
const { impl, calls } = noul(0.99);
|
|
237
|
+
const verdict = await classifyToolApproval(
|
|
238
|
+
decisions(impl),
|
|
239
|
+
{ callId: "1", toolName: "exec", input: { command: "git push --force origin main" } },
|
|
240
|
+
"push my work",
|
|
241
|
+
);
|
|
242
|
+
expect(verdict).toBe("ask");
|
|
243
|
+
expect(calls).toHaveLength(0);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test("asks when there is no user message, the feature is off, or the call fails", async () => {
|
|
247
|
+
const { impl, calls } = noul(0.99);
|
|
248
|
+
expect(await classifyToolApproval(decisions(impl), exec, " ")).toBe("ask");
|
|
249
|
+
expect(await classifyToolApproval(decisions(impl, { approvals: false }), exec, "go")).toBe(
|
|
250
|
+
"ask",
|
|
251
|
+
);
|
|
252
|
+
expect(calls).toHaveLength(0);
|
|
253
|
+
expect(
|
|
254
|
+
await classifyToolApproval(decisions(fakeFetch({}, { ok: false }).impl), exec, "go"),
|
|
255
|
+
).toBe("ask");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("sends the clipped tool input, not a whole file", async () => {
|
|
259
|
+
const { impl, calls } = noul(0.99);
|
|
260
|
+
await classifyToolApproval(
|
|
261
|
+
decisions(impl),
|
|
262
|
+
{ callId: "1", toolName: "write_file", input: { path: "a.ts", content: "x".repeat(50_000) } },
|
|
263
|
+
"write a.ts",
|
|
264
|
+
);
|
|
265
|
+
expect(JSON.stringify(calls[0]?.body).length).toBeLessThan(3000);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe("approval gate with assist", () => {
|
|
270
|
+
const exec = { callId: "1", toolName: "exec", input: { command: "bun test" } };
|
|
271
|
+
|
|
272
|
+
test("allow skips the prompt", async () => {
|
|
273
|
+
let asked = 0;
|
|
274
|
+
const gate = createExecApprovalGate({
|
|
275
|
+
policy: "prompt",
|
|
276
|
+
ask: async () => {
|
|
277
|
+
asked += 1;
|
|
278
|
+
return false;
|
|
279
|
+
},
|
|
280
|
+
assist: async () => "allow",
|
|
281
|
+
});
|
|
282
|
+
expect(await gate.request(exec)).toBe(true);
|
|
283
|
+
expect(asked).toBe(0);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test("ask and a throwing assist both fall through to the prompt", async () => {
|
|
287
|
+
for (const assist of [async () => "ask" as const, async () => Promise.reject(new Error("x"))]) {
|
|
288
|
+
let asked = 0;
|
|
289
|
+
const gate = createExecApprovalGate({
|
|
290
|
+
policy: "prompt",
|
|
291
|
+
ask: async () => {
|
|
292
|
+
asked += 1;
|
|
293
|
+
return true;
|
|
294
|
+
},
|
|
295
|
+
assist,
|
|
296
|
+
});
|
|
297
|
+
expect(await gate.request(exec)).toBe(true);
|
|
298
|
+
expect(asked).toBe(1);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("assist never overrides the deny policy or the auto policy's denylist", async () => {
|
|
303
|
+
const allowAll = async () => "allow" as const;
|
|
304
|
+
const deny = createExecApprovalGate({ policy: "deny", assist: allowAll });
|
|
305
|
+
expect(await deny.request(exec)).toBe(false);
|
|
306
|
+
const auto = createExecApprovalGate({ policy: "auto", assist: allowAll });
|
|
307
|
+
expect(
|
|
308
|
+
await auto.request({ callId: "2", toolName: "exec", input: { command: "sudo reboot" } }),
|
|
309
|
+
).toBe(false);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("upload edits always show the user what they replace", async () => {
|
|
313
|
+
let assisted = 0;
|
|
314
|
+
let asked = 0;
|
|
315
|
+
const gate = createExecApprovalGate({
|
|
316
|
+
policy: "prompt",
|
|
317
|
+
ask: async () => {
|
|
318
|
+
asked += 1;
|
|
319
|
+
return true;
|
|
320
|
+
},
|
|
321
|
+
resolveUploadEdit: () => ({ rel: "copy.txt", sourcePath: "/orig.txt", name: "orig.txt" }),
|
|
322
|
+
assist: async () => {
|
|
323
|
+
assisted += 1;
|
|
324
|
+
return "allow";
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
await gate.request({ callId: "3", toolName: "write_file", input: { path: "copy.txt" } });
|
|
328
|
+
expect(assisted).toBe(0);
|
|
329
|
+
expect(asked).toBe(1);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test("a user who disabled prompts is not asked at all", async () => {
|
|
333
|
+
let assisted = 0;
|
|
334
|
+
const gate = createExecApprovalGate({
|
|
335
|
+
policy: "prompt",
|
|
336
|
+
assist: async () => {
|
|
337
|
+
assisted += 1;
|
|
338
|
+
return "allow";
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
expect(await gate.request({ callId: "4", toolName: "write_file", input: { path: "a" } })).toBe(
|
|
342
|
+
true,
|
|
343
|
+
);
|
|
344
|
+
expect(assisted).toBe(0);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
describe("memory gate", () => {
|
|
349
|
+
const durable = (value: number) => fakeFetch({ answers: { durable: { noul: value } } });
|
|
350
|
+
|
|
351
|
+
test("skips extraction only when Jev is confident there is nothing to keep", async () => {
|
|
352
|
+
expect(await turnHasDurableFact(decisions(durable(0.02).impl), "User: fix the bug")).toBe(
|
|
353
|
+
false,
|
|
354
|
+
);
|
|
355
|
+
expect(await turnHasDurableFact(decisions(durable(0.1).impl), "User: fix the bug")).toBe(true);
|
|
356
|
+
expect(await turnHasDurableFact(decisions(durable(0.9).impl), "User: I prefer bun")).toBe(true);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test("extracts when the call fails or the gate is off", async () => {
|
|
360
|
+
expect(await turnHasDurableFact(decisions(fakeFetch({}, { ok: false }).impl), "x")).toBe(true);
|
|
361
|
+
const { impl, calls } = durable(0);
|
|
362
|
+
expect(await turnHasDurableFact(decisions(impl, { memory: false }), "x")).toBe(true);
|
|
363
|
+
expect(calls).toHaveLength(0);
|
|
364
|
+
});
|
|
365
|
+
});
|