feat(vault): edit — change a few files against the head that was read (4.14.1)
Some checks failed
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-16 01:42:18 +02:00
parent b82a803e0c
commit a1c370359e
29 changed files with 177 additions and 26 deletions

View File

@@ -660,3 +660,55 @@ describe('prune', () => {
expect((await store.log(keys.vaultId)).map((e) => e.seq)).toEqual([2, 3]);
});
});
describe('edit against the head that was read', () => {
const text = (s: string) => new TextEncoder().encode(s);
const decode = (b: Uint8Array) => new TextDecoder().decode(b);
test('a drain that raced an append keeps the appended file', async () => {
// The queue case: a phone appends while the PC removes what it applied.
// A whole-collection push from the PC's stale view would drop the
// phone's new file; edit retries on the new head instead.
const { client, device } = await harness();
const phone = device();
await phone.edit(() => ({ put: [{ path: 'intents/a.json', bytes: text('a') }] }), 1);
let calls = 0;
const res = await client.edit(async (head) => {
calls++;
if (calls === 1) {
// Between this read and the commit, the phone appends.
await phone.edit(() => ({ put: [{ path: 'intents/b.json', bytes: text('b') }] }), 2);
}
expect(head).not.toBeNull();
return { remove: ['intents/a.json'] };
}, 3);
expect(calls).toBe(2);
expect(res.committed).toBe(true);
const after = await client.manifest();
expect(after.entries.map((e) => e.path)).toEqual(['intents/b.json']);
expect(decode(await client.readEntry(after.entries[0]!))).toBe('b');
});
test('an edit that changes nothing commits nothing', async () => {
const { client, transport } = await harness();
await client.edit(() => ({ put: [{ path: 'x', bytes: text('x') }] }), 1);
const before = transport.counts.commit;
const res = await client.edit(() => ({ remove: ['not-there'] }), 2);
expect(res).toEqual({ seq: 1, committed: false });
expect(await client.edit(() => null, 3)).toEqual({ seq: 1, committed: false });
expect(transport.counts.commit).toBe(before);
});
test('untouched entries keep their objects', async () => {
const { client } = await harness();
await client.edit(() => ({ put: [{ path: 'keep', bytes: text('k') }, { path: 'drop', bytes: text('d') }] }), 1);
const first = await client.manifest();
await client.edit(() => ({ remove: ['drop'], put: [{ path: 'new', bytes: text('n') }] }), 2);
const second = await client.manifest();
const keep = (m: typeof first) => m.entries.find((e) => e.path === 'keep')!;
expect(keep(second).hash).toBe(keep(first).hash);
expect(second.entries.map((e) => e.path).sort()).toEqual(['keep', 'new']);
});
});