- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/autosettle.test.ts?format=json
162 linesautosettle 100%Last change autosettle in version 211, 3 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { type Ai, adviseHeld, autoSettledOf, autoTrailer, refused, ruled, withAdvice } from "../autosettle.ts";
4import { outlastsSync, settledItem } from "../news.ts";
6/**
7 * A stand-in for Workers AI that always leans to `pick`: a System One answer for Clef (`top` on `pick`, the rest
8 * spread evenly) or a JSON-mode answer for a text model. It counts its calls.
9 */
10function fakeAi(pick: string, top = 0.85): Ai & { calls: number } {
11 const ai = {
12 calls: 0,
13 async run(_model: string, input: unknown) {
14 ai.calls++;
15 if ((input as { questions?: unknown }).questions) {
16 const probabilities = Object.fromEntries(
17 ["yours", "theirs", "both", "agent"].map((k) => [k, k === pick ? top : (1 - top) / 3]),
18 );
19 return {
20 answers: {
21 settle: { type: "choice", probabilities },
22 settle_reversed: { type: "choice", probabilities },
23 },
24 };
25 }
26 return { response: { pick, confidence: 0.9, reason: "a fixed answer" }, usage: { prompt_tokens: 10 } };
27 },
28 };
29 return ai;
30}
32const held = (yours: string, theirs: string, kind = "lines") => ({
33 kind,
34 file: "src/a.ts",
35 yours: { lines: [3, 3] as [number, number], text: yours },
36 theirs: { agent: "other", message: "their reason", text: theirs },
37});
38const routes = held('\t"/blame": blame,\n', '\t"/search": search,\n');
39const me = { agent: "me", message: "my reason" };
41test("code settles when one side's lines are all in the other's, without asking the model", async () => {
42 const ai = fakeAi("both");
43 const [a] = await adviseHeld(ai, [held("\ttimeoutMs: 30000,\n", "\ttimeoutMs: 30000,\n\tretries: 2,\n")], me);
44 assert.equal(a?.pick, "theirs");
45 assert.equal(a?.by, "rule");
46 assert.equal(ai.calls, 0);
47});
49test("a keep-both that sets one key twice, or holds one import in two versions, goes to an agent", () => {
50 const hold = (yours: string[], theirs: string[]) => ({ file: "a", base: [], yours, theirs, before: [], after: [] });
51 assert.match(refused(hold(["\tmaxNesting: 64,"], ["\tmaxNesting: 1000,"]), "both") ?? "", /maxNesting twice/);
52 assert.match(
53 refused(hold(['import { a, b, c } from "./x.ts";'], ['import { a, b, d } from "./x.ts";']), "both") ?? "",
54 /one statement in two versions/,
55 );
56 assert.equal(refused(hold(['\t"/blame": blame,'], ['\t"/search": search,']), "both"), undefined);
57 assert.equal(ruled(hold(['\t"/blame": blame,'], ['\t"/search": search,'])), undefined);
58});
60test("Clef advises by default: one call, its confidence from the probabilities, and other kinds get no advice", async () => {
61 const ai = fakeAi("both");
62 const advice = await adviseHeld(ai, [routes, held("x", "y", "definition")], me);
63 assert.equal(advice[0]?.pick, "both");
64 assert.equal(advice[0]?.by, "model");
65 assert.equal(advice[0]?.model, "@cf/cloudflare/clef");
66 assert.equal(advice[0]?.confidence.toFixed(2), "0.80");
67 assert.equal(advice[1], null);
68 assert.equal(ai.calls, 1);
69});
71test("a lean to one side under the threshold goes to an agent, saying which side it leaned to", async () => {
72 const [a] = await adviseHeld(fakeAi("both", 0.55), [routes], me);
73 assert.equal(a?.pick, "agent");
74 assert.equal(a?.by, "unsure");
75 assert.equal(a?.modelPick, "both");
76 const [b] = await adviseHeld(fakeAi("both", 0.55), [routes], me, { at: 0.3 });
77 assert.equal(b?.pick, "both");
78});
80test("a text model named instead takes 5 samples, its confidence the share that agree", async () => {
81 const ai = fakeAi("both");
82 const [a] = await adviseHeld(ai, [routes], me, { model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast" });
83 assert.equal(a?.pick, "both");
84 assert.equal(a?.confidence, 1);
85 assert.equal(ai.calls, 5);
86});
88test("a model that fails leaves no advice instead of failing the push's answer", async () => {
89 const ai: Ai = {
90 async run() {
91 throw new Error("Workers AI is down");
92 },
93 };
94 const [a] = await adviseHeld(ai, [held("a = 1", "b = 2")], me);
95 assert.equal(a, null);
96});
98test("the switch: off leaves the answer alone, on adds advice, act also tells the client to settle", async () => {
99 const answer = { ok: false, collisions: [routes] };
100 const request = { message: "my reason" };
101 assert.equal(await withAdvice({ AI: fakeAi("both") }, "publish", "me", request, answer), answer);
102 assert.equal(await withAdvice({ AUTOSETTLE: "on", AI: fakeAi("both") }, "draft", "me", request, answer), answer);
103 const on = (await withAdvice({ AUTOSETTLE: "on", AI: fakeAi("both") }, "publish", "me", request, answer)) as {
104 advice?: unknown[];
105 settle?: boolean;
106 };
107 assert.equal(on.advice?.length, 1);
108 assert.equal(on.settle, undefined);
109 const act = (await withAdvice({ AUTOSETTLE: "act", AI: fakeAi("both") }, "publish", "me", request, answer)) as {
110 settle?: boolean;
111 };
112 assert.equal(act.settle, true);
113 const ok = { ok: true, version: 4 };
114 assert.equal(await withAdvice({ AUTOSETTLE: "act", AI: fakeAi("both") }, "publish", "me", request, ok), ok);
115});
117test("the message trailer: the client writes what it settled by itself, and the server reads it back by file", () => {
118 const trailer = autoTrailer([
119 [
120 "docs/start.md",
121 { pick: "yours", by: "model", confidence: 0.8125, reason: "Both fixed the typo;\nyours adds a line." },
122 ],
123 [
124 "src/network.ts",
125 { pick: "theirs", by: "rule", confidence: 1, reason: "every line of yours is already in theirs" },
126 ],
127 ]);
128 assert.equal(
129 trailer,
130 [
131 "Settled automatically (abso 19):",
132 "- docs/start.md: kept yours, confidence 0.81. Both fixed the typo; yours adds a line.",
133 "- src/network.ts: kept theirs, by a check in code. every line of yours is already in theirs",
134 ].join("\n"),
135 );
136 const read = autoSettledOf(`Fix the jion typo\n\n${trailer}`);
137 assert.deepEqual(read.get("docs/start.md"), {
138 pick: "yours",
139 by: "model",
140 confidence: 0.81,
141 reason: "Both fixed the typo; yours adds a line.",
142 });
143 assert.equal(read.get("src/network.ts")?.by, "rule");
144 assert.equal(autoSettledOf("Settled the typo by hand").size, 0);
145});
147test("the other agent's news says the settle was automatic, and still outlasts a sync", () => {
148 const s = {
149 file: "docs/start.md",
150 kept: "yours" as const,
151 theirs: { agent: "client", version: 2 },
152 auto: { pick: "yours" as const, by: "model" as const, confidence: 0.81, reason: "r" },
153 };
154 const item = settledItem("docs", 3, s);
155 assert.equal(outlastsSync(item.text), true);
156 assert.match(
157 item.text,
158 /^docs settled its collision with your version 2 in docs\/start\.md automatically, as abso's model advised \(confidence 0\.81\), pushing version 3:/,
159 );
160 const byHand = settledItem("docs", 3, { ...s, auto: undefined });
161 assert.match(byHand.text, /in docs\/start\.md, pushing version 3:/);
162});