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

@@ -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.