feat(vault): v2 — compressed entries, chunked objects, index pushes, lazy reads, prune (4.14.0)
Some checks failed
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled

Entries are deflate-raw compressed when it pays, split into 4 MiB sealed
parts, and manifests are compressed (SVM2). A push with a local index
reuses unchanged files without downloading anything, and a push with no
changes commits nothing. Readers fetch one manifest and only the entries
they need. The relay records what each version references and can prune
old versions signed by the owner. Cross-language vectors in
test-vectors/vault-v2.json.

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

View File

@@ -6,6 +6,8 @@
* HEAD /v1/vault/:vaultId/object/:hash → 200 | 404 (have-check)
* PUT /v1/vault/:vaultId/object/:hash → { stored } (signed)
* POST /v1/vault/:vaultId/commit → { seq } (signed)
* POST /v1/vault/:vaultId/prune → { removedVersions, removedObjects,
* freedBytes, head } (signed)
*
* Auth is the same TOFU-Ed25519 scheme the blob primitive uses: the first
* signed write pins a pubkey for the vault, and every later write must be
@@ -30,10 +32,19 @@ export interface VaultRoutesOptions {
maxObjectBytes?: number;
/** Whole-vault ceiling. Defaults to 512 MiB. */
maxVaultBytes?: number;
/**
* How long an uploaded-but-never-committed object survives a prune.
* Defaults to one hour — long enough for a slow phone to finish a large
* push between its first upload and its commit.
*/
pruneGraceMs?: number;
}
const DEFAULT_MAX_OBJECT = 8 * 1024 * 1024;
const DEFAULT_MAX_VAULT = 512 * 1024 * 1024;
const DEFAULT_PRUNE_GRACE = 60 * 60 * 1000;
/** A keep-list longer than this is not a retention policy, it is a mistake. */
const MAX_KEEP = 100_000;
function fail(code: string, message: string, status: ContentfulStatusCode) {
return { body: { error: { code, message } }, status };
@@ -47,6 +58,7 @@ export function createVaultRoutes(
const app = new Hono();
const maxObject = options.maxObjectBytes ?? DEFAULT_MAX_OBJECT;
const maxVault = options.maxVaultBytes ?? DEFAULT_MAX_VAULT;
const pruneGrace = options.pruneGraceMs ?? DEFAULT_PRUNE_GRACE;
/**
* Check a signed request against the vault's pinned key, pinning it on the
@@ -208,19 +220,89 @@ export function createVaultRoutes(
return c.json({ ...f.body, missing: missing.slice(0, 20) }, f.status);
}
await store.appendLog(vaultId, {
seq: body.seq,
manifest: body.manifest,
at: typeof body.at === 'number' ? body.at : Date.now(),
bytes: await store.usage(vaultId),
});
// The signed `hashes` plus the manifest are everything this version
// references. Recording them is what lets prune collect garbage later
// without ever reading a manifest — so a client that leaves a part out of
// `hashes` is telling the relay that part may be deleted.
const refs = [...new Set([body.manifest, ...hashes])];
await store.appendLog(
vaultId,
{
seq: body.seq,
manifest: body.manifest,
at: typeof body.at === 'number' ? body.at : Date.now(),
bytes: await store.usage(vaultId),
},
refs,
);
return c.json({ seq: body.seq, head: body.seq });
});
/**
* Retention: drop versions and the objects only they used.
*
* Without this the log is append-only in the literal sense, and a vault
* pushed on every edit grows until it hits the quota and stops accepting
* backups — the failure arriving exactly when the user is relying on it.
* The client decides the policy (keep the last N, one per day, …) because
* only it can read the manifests; the relay just executes a keep-list.
*/
app.post('/v1/vault/:vaultId/prune', async (c) => {
const vaultId = c.req.param('vaultId');
if (!ID_REGEX.test(vaultId)) {
const f = fail('BAD_REQUEST', 'vaultId must be 64 lowercase hex chars', 400);
return c.json(f.body, f.status);
}
const body = (await c.req.json().catch(() => null)) as Record<string, unknown> | null;
const keep = body?.keep;
if (
!body ||
!Array.isArray(keep) ||
keep.length > MAX_KEEP ||
!keep.every((s) => Number.isSafeInteger(s) && (s as number) >= 0)
) {
const f = fail('BAD_REQUEST', 'expected { keep: number[], signedAt, signature }', 400);
return c.json(f.body, f.status);
}
// Deliberately not `authorize`'s TOFU path: a prune must never be the
// write that pins an owner. On a vault nobody has written to there is
// nothing to prune, and accepting it would hand the vault to whoever
// asked first.
if (!(await store.getOwner(vaultId))) {
const f = fail('NOT_FOUND', 'no such vault', 404);
return c.json(f.body, f.status);
}
const auth = await authorize(vaultId, body);
if (!auth.ok) {
const f = fail(auth.code, auth.message, auth.status);
return c.json(f.body, f.status);
}
const head = await store.head(vaultId);
if (!(keep as number[]).includes(head)) {
// Pruning the head would leave the vault restoring to something other
// than what was last backed up. It is also what a keep-list computed
// before another device's commit looks like — so this is a conflict the
// client resolves by re-reading, not a malformed request. A replayed
// prune hits the same wall as soon as anything has been committed.
const f = fail('SEQ_CONFLICT', `keep must include the current head ${head}`, 409);
return c.json({ ...f.body, head }, f.status);
}
const result = await store.prune(vaultId, {
keep: keep as number[],
upTo: head,
protectSince: Date.now() - pruneGrace,
});
return c.json({ ...result, head });
});
return app;
}
export type { VaultStore } from './store.js';
export type { VaultStore, VaultPrunePlan } from './store.js';
export { MemoryVaultStore } from './store.js';
export type { VaultManifest };