mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2026-10-27 07:52:03 +00:00
Type two functions to what they read, removing nine assertions
Nine of the gate's findings were not assertions at all in spirit — they were two function signatures narrower than their callers, paid for at every call site. `recomputeIndex0Stats` took a bare `Dict` while all five of its callers hold a real `Index0`, so each one laundered it through `as unknown as Dict`. It reads exactly three fields, so it now says so: a Pick over events, checkpoints and heads. Any Index0 satisfies it, a partial one still does, and the five casts are gone. `exportBackup` took `Record<string, unknown>` and is genuinely called two ways: with a live VaultIndex from three places in the CLI and API, and with a round-tripped backup payload from the tests. It treats either as an opaque JSON document to canonicalize and encrypt, so the signature now names both shapes instead of making the majority caller pretend to be the minority one. No type surgery and no behaviour change: the same values reach the same code, they just no longer have to lie about what they are on the way in. 44 -> 35. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Vtx94nXDmNgXEeThUZP4QN
This commit is contained in:
1 parent
51855ddea6
commit
de43ace259
5 files changed
+21
-10
No files matched your search
@@ -745,7 +745,7 @@ export function registerRoutes(server: ApiServer, ctx: ApiContext): void {
|
||||
const vault = await readVault(ctx);
|
||||
const body = bodyObject(req);
|
||||
const encrypt = body["plaintext"] !== true;
|
||||
const wrapper = await exportBackup(vault.index as unknown as Record<string, unknown>, {
|
||||
const wrapper = await exportBackup(vault.index, {
|
||||
mnemonic,
|
||||
fingerprint: generateFingerprint(mnemonic),
|
||||
encrypt,
|
||||
|
||||
@@ -2425,7 +2425,7 @@ export function buildProgram(io: ProgramIo = defaultIo): Command {
|
||||
existsSync(resolved) && statSync(resolved).isDirectory()
|
||||
? join(resolved, defaultBackupFilename(fingerprint))
|
||||
: resolved;
|
||||
const wrapper = await exportBackup(vault.index as Record<string, unknown>, {
|
||||
const wrapper = await exportBackup(vault.index, {
|
||||
mnemonic: vault.mnemonic,
|
||||
fingerprint,
|
||||
encrypt: !o.plaintext,
|
||||
|
||||
@@ -1505,7 +1505,7 @@ async function exportDatabase(s: Session): Promise<void> {
|
||||
} else if (!basename(dest).includes(".")) {
|
||||
dest = `${dest}${BACKUP_EXTENSION}`;
|
||||
}
|
||||
const payload = await exportBackup(s.vault.index as unknown as Record<string, unknown>, {
|
||||
const payload = await exportBackup(s.vault.index, {
|
||||
mnemonic: s.vault.mnemonic,
|
||||
fingerprint: s.fingerprint,
|
||||
});
|
||||
|
||||
@@ -343,7 +343,13 @@ export function normalizeViewManifest(raw: unknown): Dict {
|
||||
};
|
||||
}
|
||||
|
||||
export function recomputeIndex0Stats(index0: Dict): Dict {
|
||||
// Typed to exactly what it reads. It used to take a bare Dict, so every one
|
||||
// of its five callers — all holding a real Index0 — had to launder it through
|
||||
// `as unknown as Dict` to get in. A Pick says what the function needs, admits
|
||||
// any Index0 without ceremony, and still accepts a partial one.
|
||||
export function recomputeIndex0Stats(
|
||||
index0: Pick<Index0, "events" | "checkpoints" | "heads">,
|
||||
): Dict {
|
||||
const events = normalizeMapping(index0["events"]);
|
||||
const checkpoints = normalizeMapping(index0["checkpoints"]);
|
||||
const heads = normalizeMapping(index0["heads"]);
|
||||
@@ -394,7 +400,7 @@ export function normalizeIndex0(raw: unknown): Index0 {
|
||||
const normalizedHead = normalizeHead(head);
|
||||
if (normalizedHead) normalized.heads[String(writerId)] = normalizedHead;
|
||||
}
|
||||
normalized.stats = recomputeIndex0Stats(normalized as unknown as Dict);
|
||||
normalized.stats = recomputeIndex0Stats(normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
@@ -544,7 +550,7 @@ export function appendIndex0Event(payload: unknown, options: AppendEventOptions)
|
||||
head_hash: computeHeadHash(event),
|
||||
modified_ts: event["modified_ts"] as number,
|
||||
};
|
||||
system.stats = recomputeIndex0Stats(system as unknown as Dict);
|
||||
system.stats = recomputeIndex0Stats(system);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -658,7 +664,7 @@ export function compactIndex0(
|
||||
normalized as unknown as Dict,
|
||||
options,
|
||||
);
|
||||
normalized.stats = recomputeIndex0Stats(normalized as unknown as Dict);
|
||||
normalized.stats = recomputeIndex0Stats(normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
@@ -850,7 +856,7 @@ export function rebuildCanonicalViewsPayload(
|
||||
const sortedViews: Record<string, Dict> = {};
|
||||
for (const key of Object.keys(views).sort(compareCodePoints)) sortedViews[key] = views[key]!;
|
||||
index0.canonical_views = sortedViews;
|
||||
index0.stats = recomputeIndex0Stats(index0 as unknown as Dict);
|
||||
index0.stats = recomputeIndex0Stats(index0);
|
||||
(out["_system"] as Dict)["index0"] = index0;
|
||||
return out;
|
||||
}
|
||||
@@ -1038,6 +1044,6 @@ export function mergeSystemIndex0(current: unknown, incoming: unknown): Index0 {
|
||||
heads: sortRecords(heads),
|
||||
stats: {},
|
||||
};
|
||||
merged.stats = recomputeIndex0Stats(merged as unknown as Dict);
|
||||
merged.stats = recomputeIndex0Stats(merged);
|
||||
return merged;
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import { base64 } from "@scure/base";
|
||||
import { z } from "zod";
|
||||
import { canonicalJson, canonicalHash } from "../sync/canonical.js";
|
||||
import { deriveIndexKeyBytes } from "./indexKey.js";
|
||||
import type { VaultIndex } from "../schema/entries.js";
|
||||
import { decryptPayload } from "./payload.js";
|
||||
import { encryptV3 } from "./aead.js";
|
||||
import { utf8 } from "../util/bytes.js";
|
||||
@@ -102,7 +103,11 @@ export async function importBackup(
|
||||
|
||||
/** Export an index as a portable backup wrapper (format_version 1). */
|
||||
export async function exportBackup(
|
||||
index: Record<string, unknown>,
|
||||
// A live VaultIndex or a round-tripped backup payload: both are passed, and
|
||||
// the function treats either as an opaque JSON document to canonicalize and
|
||||
// encrypt. Naming both is what lets a caller holding a real index hand it
|
||||
// over without laundering it through `as unknown as Record<string, unknown>`.
|
||||
index: VaultIndex | Record<string, unknown>,
|
||||
options: {
|
||||
mnemonic: string;
|
||||
fingerprint: string;
|
||||
|
||||
Reference in new issue
Block a user