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

@@ -1,6 +1,6 @@
{
"name": "@shade/cli",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/cli.ts",
"bin": {

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/core",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/crypto-web",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/dashboard",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"scripts": {
"dev": "vite",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/files",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/inbox-server",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/inbox",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/key-transparency",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/keychain",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/observability",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/observer",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/proto",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/recovery",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/sdk",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/server",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/storage-encrypted",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/storage-indexeddb",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/storage-postgres",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/storage-sqlite",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/streams",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/transfer",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/transport-bridge",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/transport-webrtc",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/transport",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/vault",
"version": "4.14.0",
"version": "4.14.1",
"description": "Server-side encrypted file store for Shade — content-addressed objects with an append-only manifest log",
"type": "module",
"main": "src/index.ts",

View File

@@ -299,6 +299,84 @@ export class VaultClient {
};
}
/**
* Change a few files against the head that was actually read, and only it.
*
* `push` publishes a whole collection — right for a backup, where this
* client is the only writer and the files on disk are the truth. It is
* wrong for a vault two devices write to, such as a queue a phone appends
* to while a PC drains it: a push computed from a stale view would commit
* a collection without the other device's latest file, and it would be
* gone. `edit` instead reads the head, lets `change` decide from exactly
* that manifest, keeps every entry it did not touch as-is, and commits
* `head + 1`. If another device committed in between, the relay refuses
* with SEQ_CONFLICT and this starts over from the new head — `change` is
* called again, so it must be a pure decision about the manifest it gets.
*
* Returning `null` (or an edit that changes nothing) commits nothing.
*/
async edit(
change: (head: VaultManifest | null) => VaultEdit | null | Promise<VaultEdit | null>,
at: number,
opts: { maxAttempts?: number } = {},
): Promise<{ seq: number; committed: boolean }> {
const attempts = Math.max(1, opts.maxAttempts ?? 5);
for (let attempt = 1; ; attempt++) {
const { head, entries: tail } = await this.transport.log(this.keys.vaultId, 1);
const row = tail.find((e) => e.seq === head);
const manifest = row ? await this.openRow(row) : null;
const edit = await change(manifest);
const put = edit?.put ?? [];
const remove = new Set(edit?.remove ?? []);
const current = manifest?.entries ?? [];
const replaced = new Set(put.map((f) => f.path));
if (put.length === 0 && !current.some((e) => remove.has(e.path))) {
return { seq: head, committed: false };
}
const entries = current.filter((e) => !remove.has(e.path) && !replaced.has(e.path));
for (const file of put) {
const sealed = await sealEntry(
this.crypto,
this.keys.contentKey,
this.keys.vaultId,
file.bytes,
this.options.chunkSize === undefined ? {} : { chunkSize: this.options.chunkSize },
);
for (const obj of sealed.objects) {
await this.transport.put(this.keys.vaultId, obj.hash, await this.sign({ data: toBase64(obj.bytes) }));
}
const entry: VaultEntry = { path: file.path, ...sealed.locator, size: file.bytes.length };
if (file.mode !== undefined) entry.mode = file.mode;
entries.push(entry);
}
const next: VaultManifest = { version: 2, seq: head + 1, at, entries };
const sealedManifest = await sealManifest(this.crypto, this.keys.contentKey, this.keys.vaultId, next);
await this.transport.put(
this.keys.vaultId,
sealedManifest.hash,
await this.sign({ data: toBase64(sealedManifest.bytes) }),
);
try {
const { seq } = await this.transport.commit(
this.keys.vaultId,
await this.sign({
manifest: sealedManifest.hash,
seq: next.seq,
at,
hashes: [...new Set(entries.flatMap((e) => entryObjectHashes(e)))],
}),
);
return { seq, committed: true };
} catch (e) {
// The objects just uploaded are orphans now; the relay's prune grace
// and GC take care of them. Only a lost race is worth another go.
if (attempt >= attempts || !isSeqConflict(e)) throw e;
}
}
}
/**
* Fetch and open just a version's manifest. Defaults to the newest.
*
@@ -366,6 +444,19 @@ export class VaultClient {
}
}
/** A few files changed, for `VaultClient.edit`. */
export interface VaultEdit {
/** Files to add, or to replace at the same path. */
put?: VaultFile[];
/** Paths to drop. A path that is not there is ignored. */
remove?: string[];
}
/** The relay's "someone committed first" — the one failure worth retrying. */
function isSeqConflict(e: unknown): boolean {
return e instanceof Error && /SEQ_CONFLICT/.test(e.message);
}
/**
* Build the index for a version you already hold in plaintext — after a
* restore, say, so the first push from a fresh device needs no download.

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']);
});
});

View File

@@ -1,6 +1,6 @@
{
"name": "@shade/widgets",
"version": "4.14.0",
"version": "4.14.1",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",