Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/server/test/request-identity-digest.test.ts?format=json
148 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 184, 1 version changed it
1import assert from "node:assert/strict";
2import { createHash } from "node:crypto";
3import { test } from "node:test";
4import { answer } from "../../worker/project/http.ts";
5import { canonicalJson } from "../../worker/project/operation-protocol.ts";
6import { DiskFirstRuntime } from "../src/disk-first-runtime.ts";
7import { SqliteLog } from "../src/engine-project.ts";
8import { requestIdentityDigest } from "../src/request-identity-digest.ts";
9import { SqliteWriterState } from "../src/writer-host.ts";
10import { memoryStorage } from "./helpers.ts";
12test("request identity hashes preserve canonical bytes and legacy omission semantics", () => {
13 const shared = { z: 1, a: "same" };
14 for (const value of [
15 null,
16 true,
17 false,
18 0,
19 -0,
20 1e30,
21 NaN,
22 Infinity,
23 { z: [shared, shared], a: { "10": 10, "2": 2, b: undefined, c: Symbol("x"), d: () => 1 } },
24 Object.assign(new Array(5), { 0: undefined, 2: null, 3: Symbol("x"), 4: () => 1 }),
25 { toJSON: () => "ignored by canonicalJson", value: "original" },
26 { body: { files: { "large.txt": `${"x".repeat(16383)}🚀${'\u0000\n"\\é中\ud800'.repeat(100000)}` } } },
27 ]) {
28 assert.equal(requestIdentityDigest(value), createHash("sha256").update(canonicalJson(value)).digest("hex"));
29 }
30});
32test("large request strings never reach a whole-string JSON encoder", (t) => {
33 const value = { body: { files: { "a.txt": "🚀abc\n".repeat(500000) } } };
34 const expected = createHash("sha256").update(canonicalJson(value)).digest("hex");
35 const stringify = JSON.stringify;
36 t.mock.method(JSON, "stringify", (input: unknown) => {
37 if (typeof input === "string") assert.ok(input.length <= 16384);
38 else assert.ok(input === null || typeof input !== "object", "whole request serialization forbidden");
39 return stringify(input);
40 });
41 assert.equal(requestIdentityDigest(value), expected);
42});
44test("invalid identities fail instead of hashing a different value", () => {
45 for (const value of [undefined, Symbol("x"), () => 1, 1n]) assert.throws(() => requestIdentityDigest(value));
46 const cyclic: unknown[] = [];
47 cyclic.push(cyclic);
48 assert.throws(() => requestIdentityDigest(cyclic), /cyclic/);
49});
51test("a reopened runtime replays legacy receipts without executing the operation", (t) => {
52 const storage = memoryStorage();
53 t.after(() => storage.db.close());
54 const runtime = () => new DiskFirstRuntime(storage, "legacy-identity", "legacy-identity");
55 runtime().initialize(
56 (calls) => answer(calls, "init", undefined, { files: { "a.txt": "one\n" } }),
57 () => {},
58 );
59 while (!runtime().preparePage(() => {}).ready) {}
60 const state = new SqliteWriterState(storage, "legacy-identity");
61 const identity = { z: "last", body: { files: { "a.txt": "new\n" } }, a: "first" };
62 const digest = createHash("sha256").update(canonicalJson(identity)).digest("hex");
63 const result = { status: 200, body: "🚀\n".repeat(40000) };
64 const json = JSON.stringify({ result });
65 const parts = Math.ceil(json.length / 65536);
66 for (let part = 0; part < parts; part++)
67 state.write("runtime:receipt:legacy-3", String(part), json.slice(part * 65536, (part + 1) * 65536));
68 state.write("runtime:receipts", "legacy-3", {
69 format: 3,
70 identity: digest,
71 parts,
72 characters: json.length,
73 digest: createHash("sha256").update(json).digest("hex"),
74 });
75 state.write("runtime:receipts", "legacy-2", { format: 2, identity: digest, result });
76 const before = new SqliteLog(storage).seq();
77 for (const id of ["legacy-2", "legacy-3"]) {
78 assert.deepEqual(
79 runtime().write(
80 () => {
81 throw Error("legacy operation executed twice");
82 },
83 () => {},
84 { id, identity },
85 ),
86 result,
87 );
88 assert.throws(
89 () =>
90 runtime().write(
91 () => result,
92 () => {},
93 { id, identity: { changed: true } },
94 ),
95 /reused/,
96 );
97 assert.throws(
98 () =>
99 runtime().write(
100 () => result,
101 () => {
102 throw Error("revoked");
103 },
104 { id, identity },
105 ),
106 /revoked/,
107 );
108 }
109 assert.equal(new SqliteLog(storage).seq(), before);
110});
112test("receipt streaming preserves deep results and boxed primitive JSON behavior", (t) => {
113 const storage = memoryStorage();
114 t.after(() => storage.db.close());
115 const runtime = () => new DiskFirstRuntime(storage, "receipt-compat", "receipt-compat");
116 runtime().initialize(
117 (calls) => answer(calls, "init", undefined, { files: { "a.txt": "one\n" } }),
118 () => {},
119 );
120 while (!runtime().preparePage(() => {}).ready) {}
121 let deep: unknown = "leaf";
122 for (let i = 0; i < 200; i++) deep = { child: deep };
123 const values = [deep, new String("boxed"), new Number(42), new Boolean(false)];
124 for (let i = 0; i < values.length; i++) {
125 const request = { id: "compat-" + i, identity: { i } };
126 const before = new SqliteLog(storage).seq();
127 runtime().write(
128 (calls) => {
129 const result = answer(calls, "record", "fixture", { files: { "a.txt": "value-" + i } });
130 assert.equal(result.status, 200);
131 return values[i];
132 },
133 () => {},
134 request,
135 );
136 assert.ok(new SqliteLog(storage).seq() > before);
137 assert.deepEqual(
138 runtime().write(
139 () => {
140 throw Error("retry executed");
141 },
142 () => {},
143 request,
144 ),
145 JSON.parse(JSON.stringify(values[i])),
146 );
147 }
148});