-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbrain-bundle.test.js
More file actions
254 lines (227 loc) · 9.21 KB
/
Copy pathbrain-bundle.test.js
File metadata and controls
254 lines (227 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { describe, it, expect } from 'vitest';
import { Wallet } from 'ethers';
import {
canonicalizeMemory,
memoryDigest,
signDigest,
verifyMemorySignature,
signMessageBody,
MEMORY_SIG_VERSION,
} from '../api/_lib/brain-sign.js';
import {
computeBrainHash,
buildMemoryEntry,
buildBundle,
verifyBundle,
verifyBrainHashSignature,
BRAIN_BUNDLE_VERSION,
} from '../api/_lib/brain-bundle.js';
// A deterministic throwaway key — the test's "agent wallet". Never a real mint.
const TEST_PK = '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d';
const wallet = new Wallet(TEST_PK);
function fixtureMemory(overrides = {}) {
return {
id: '11111111-1111-1111-1111-111111111111',
agent_id: '22222222-2222-2222-2222-222222222222',
type: 'user',
content: 'The user prefers terse, senior-engineer feedback. No filler.',
tags: ['tone', 'feedback'],
salience: 0.8,
tier: 'working',
is_public: true,
created_at: '2026-06-23T12:00:00.000Z',
...overrides,
};
}
describe('brain-sign · canonicalization', () => {
it('is stable regardless of tag order and timestamp representation', () => {
const a = canonicalizeMemory(fixtureMemory({ tags: ['feedback', 'tone'] }));
const b = canonicalizeMemory(fixtureMemory({ tags: ['tone', 'feedback'] }));
expect(a).toBe(b);
// numeric ms vs ISO for the same instant
const ms = canonicalizeMemory(fixtureMemory({ created_at: 1781524800000 }));
const iso = canonicalizeMemory(fixtureMemory({ created_at: '2026-06-15T12:00:00.000Z' }));
expect(ms).toBe(iso);
});
it('excludes mutable fields so re-tiering does not break authorship', () => {
const a = memoryDigest(fixtureMemory({ tier: 'working', salience: 0.9, pinned: true }));
const b = memoryDigest(fixtureMemory({ tier: 'archival', salience: 0.1, pinned: false }));
expect(a).toBe(b);
});
it('changes the digest when content changes', () => {
const a = memoryDigest(fixtureMemory());
const b = memoryDigest(fixtureMemory({ content: 'tampered' }));
expect(a).not.toBe(b);
});
});
describe('brain-sign · sign + verify', () => {
it('produces a signature that recovers to the signer and verifies', async () => {
const mem = fixtureMemory();
const digest = memoryDigest(mem);
const { signature, signer_address } = await signDigest(TEST_PK, digest);
expect(signer_address.toLowerCase()).toBe(wallet.address.toLowerCase());
const v = verifyMemorySignature(mem, { signature, signer_address, content_hash: digest });
expect(v.valid).toBe(true);
expect(v.reason).toBe('ok');
expect(v.recovered.toLowerCase()).toBe(wallet.address.toLowerCase());
});
it('rejects tampered content', async () => {
const mem = fixtureMemory();
const { signature, signer_address } = await signDigest(TEST_PK, memoryDigest(mem));
const tampered = { ...mem, content: 'I have been edited' };
const v = verifyMemorySignature(tampered, { signature, signer_address });
expect(v.valid).toBe(false);
expect(v.reason).toBe('signer_mismatch');
});
it('detects a content_hash that does not match the memory', async () => {
const mem = fixtureMemory();
const v = verifyMemorySignature(mem, {
signature: '0xdead',
signer_address: wallet.address,
content_hash: 'f'.repeat(64),
});
expect(v.valid).toBe(false);
expect(v.reason).toBe('content_hash_mismatch');
});
it('reports unsigned and malformed honestly', async () => {
const mem = fixtureMemory();
expect(verifyMemorySignature(mem, {}).reason).toBe('unsigned');
expect(verifyMemorySignature(mem, { signature: 'not-a-sig', signer_address: wallet.address }).reason).toBe(
'malformed_signature',
);
});
it('rejects a signature over a different message framing', async () => {
// A raw signature over the digest WITHOUT the domain prefix must not verify.
const mem = fixtureMemory();
const digest = memoryDigest(mem);
const rogue = await wallet.signMessage(digest); // missing MEMORY_SIG_VERSION prefix
expect(MEMORY_SIG_VERSION).toBe('threews:brain:memory:v1');
const v = verifyMemorySignature(mem, { signature: rogue, signer_address: wallet.address });
expect(v.valid).toBe(false);
});
});
describe('brain-bundle · build + verify round trip', () => {
const agent = {
id: '22222222-2222-2222-2222-222222222222',
name: 'Atlas',
description: 'A research agent',
avatar_id: 'av_1',
chain_id: 8453,
erc8004_agent_id: 42,
wallet_address: wallet.address,
};
const persona = {
prompt: 'You are Atlas, a meticulous research agent.',
prompt_hash: 'a'.repeat(64),
prompt_sig: 'sig',
tone_tags: ['precise', 'calm'],
extracted_at: '2026-06-01T00:00:00.000Z',
};
async function signedRow(over = {}) {
const row = fixtureMemory(over);
const digest = memoryDigest(row);
const { signature, signer_address } = await signDigest(TEST_PK, digest);
return { ...row, content_hash: digest, signature, signer_address, signed_at: '2026-06-23T12:00:01.000Z' };
}
it('builds a schema-valid, signed bundle that verifies end to end', async () => {
const rows = [
await signedRow(),
await signedRow({ id: '33333333-3333-3333-3333-333333333333', content: 'Second memory', is_public: true }),
];
const entries = rows.map((r) => buildMemoryEntry(r, { includePrivatePlaintext: true }));
const bundle = await buildBundle({
agent,
persona,
memoryEntries: entries,
exportedAt: '2026-06-23T12:30:00.000Z',
signerPrivKey: TEST_PK,
});
expect(bundle.version).toBe(BRAIN_BUNDLE_VERSION);
expect(bundle.manifest.memory_count).toBe(2);
expect(bundle.signature.signer_address.toLowerCase()).toBe(wallet.address.toLowerCase());
const result = verifyBundle(bundle);
expect(result.valid).toBe(true);
expect(result.schemaValid).toBe(true);
expect(result.brainHashValid).toBe(true);
expect(result.bundleSignatureValid).toBe(true);
expect(result.verifiedCount).toBe(2);
expect(result.signedCount).toBe(2);
});
it('detects a tampered memory inside an otherwise valid bundle', async () => {
const rows = [await signedRow()];
const entries = rows.map((r) => buildMemoryEntry(r, { includePrivatePlaintext: true }));
const bundle = await buildBundle({
agent,
persona,
memoryEntries: entries,
exportedAt: '2026-06-23T12:30:00.000Z',
signerPrivKey: TEST_PK,
});
// Tamper the content but keep the original signature + content_hash.
bundle.memories[0].content = 'malicious rewrite';
const result = verifyBundle(bundle);
expect(result.valid).toBe(false);
// content_hash no longer matches the mutated content.
expect(result.memories[0].valid).toBe(false);
});
it('detects a brain_hash that does not cover the memory set', async () => {
const entries = [buildMemoryEntry(await signedRow(), { includePrivatePlaintext: true })];
const bundle = await buildBundle({
agent,
persona,
memoryEntries: entries,
exportedAt: '2026-06-23T12:30:00.000Z',
signerPrivKey: TEST_PK,
});
bundle.manifest.brain_hash = 'b'.repeat(64);
const result = verifyBundle(bundle);
expect(result.brainHashValid).toBe(false);
expect(result.valid).toBe(false);
});
it('keeps private memories as encrypted references, not plaintext', async () => {
const privateRow = await signedRow({
id: '44444444-4444-4444-4444-444444444444',
content: 'secret diary entry',
is_public: false,
});
const cipherRefs = new Map([[privateRow.id, { cid: 'bafyTestCid', filename: 'mem-44.enc' }]]);
const entry = buildMemoryEntry(privateRow, { includePrivatePlaintext: false, cipherRefs });
expect(entry.content).toBeUndefined();
expect(entry.cipher.cid).toBe('bafyTestCid');
const bundle = await buildBundle({
agent,
persona: null,
memoryEntries: [entry],
exportedAt: '2026-06-23T12:30:00.000Z',
signerPrivKey: TEST_PK,
});
expect(bundle.manifest.encrypted_count).toBe(1);
const result = verifyBundle(bundle);
// Encrypted entries are reported, not penalized; the bundle stays valid.
expect(result.memories[0].reason).toBe('encrypted');
expect(result.valid).toBe(true);
});
it('excludes a private memory with no cipher reference', async () => {
const privateRow = await signedRow({ is_public: false });
const entry = buildMemoryEntry(privateRow, { includePrivatePlaintext: false, cipherRefs: new Map() });
expect(entry).toBeNull();
});
});
describe('brain-bundle · brain hash + bundle signature', () => {
it('is deterministic over the memory set and persona', () => {
const h1 = computeBrainHash({ personaPromptHash: 'a'.repeat(64), memoryHashes: ['1', '2'], agentId: 'x' });
const h2 = computeBrainHash({ personaPromptHash: 'a'.repeat(64), memoryHashes: ['2', '1'], agentId: 'x' });
expect(h1).toBe(h2);
const h3 = computeBrainHash({ personaPromptHash: 'a'.repeat(64), memoryHashes: ['2', '1', '3'], agentId: 'x' });
expect(h3).not.toBe(h1);
});
it('round-trips a standalone brain_hash signature', async () => {
const brainHash = computeBrainHash({ personaPromptHash: null, memoryHashes: ['abc'], agentId: 'a' });
const { signature, signer_address } = await signDigest(TEST_PK, brainHash);
expect(verifyBrainHashSignature(brainHash, { value: signature, signer_address })).toBe(true);
expect(verifyBrainHashSignature('c'.repeat(64), { value: signature, signer_address })).toBe(false);
});
it('uses the documented message framing', () => {
expect(signMessageBody('deadbeef')).toBe('threews:brain:memory:v1:deadbeef');
});
});