mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-15 22:08:03 +00:00
The on-device index was fetched once and never revalidated: the only freshness check was two existsSync calls. A move added after that fetch was invisible to meaning search permanently, not down-ranked but absent from the candidate set. The registry manifest on the same command carries a 24h TTL, so the two halves of one feature disagreed about staleness. The dropped count reported over-coverage only, names the index has that the registry lacks. Under-coverage was never computed, so the harmless direction was instrumented and the costly one was silent. Reproduced with an index truncated to 120 of 168 moves: dropped read 0, perfect health, while 48 moves were unreachable. Counts under-coverage from the name list the artifact already carries, so no extra request. Warns only when non-zero, and names the remedy. The remedy had to be made true: --on-device could not refresh a stale index because hasLocalVectors short-circuited the fetch. That flag now refetches when the index is absent or no longer covering. Two defects the reproduction surfaced. A failed refresh reported the tier unavailable while the old vectors were still on disk and still ranking. And the fetch wrote its two files one at a time, so failing between them paired a new name list with an old matrix, a hard load error rather than stale data. It now writes both or neither, which matters more once refresh runs on staleness. top_score returns, scoped to the on-device tier and set to the score of the best result actually shown rather than the ranking head, which can describe a row the caller never received. Also: scripts/ is now typechecked. It never was, which is how a build script that crashes after the paid embedding call, and two scripts whose imports do not resolve at all, went unnoticed. 43 errors fixed, no suppressions. And the docs stop describing a --smart hosted tier that was deleted, an item that does not exist, and a registry refresh that cannot fix a stale vector index.
175 lines
4.3 KiB
TypeScript
175 lines
4.3 KiB
TypeScript
export type InlineValueOption<Key extends string> = {
|
|
prefix: string;
|
|
key: Key;
|
|
};
|
|
|
|
export const CLI_SEMVER_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
|
|
type ParserConfig<
|
|
Parsed extends object,
|
|
ValueKey extends keyof Parsed & string,
|
|
BooleanKey extends keyof Parsed & string,
|
|
> = {
|
|
inlineValueOptions: Array<InlineValueOption<ValueKey>>;
|
|
valueOptions: Map<string, ValueKey>;
|
|
booleanOptions: Map<string, BooleanKey>;
|
|
parsePositional: (arg: string, index: number) => number;
|
|
fail: (message: string) => never;
|
|
};
|
|
|
|
export function parseMappedArgument<
|
|
Parsed extends object,
|
|
ValueKey extends keyof Parsed & string,
|
|
BooleanKey extends keyof Parsed & string,
|
|
>(
|
|
args: string[],
|
|
index: number,
|
|
parsed: Parsed,
|
|
config: ParserConfig<Parsed, ValueKey, BooleanKey>,
|
|
) {
|
|
const arg = args[index] ?? config.fail(`No argument at index ${index}`);
|
|
|
|
if (applyInlineValueOption(arg, parsed, config.inlineValueOptions)) {
|
|
return index;
|
|
}
|
|
if (applyBooleanOption(arg, parsed, config.booleanOptions)) {
|
|
return index;
|
|
}
|
|
|
|
return applyValueOrPositionalOption(args, index, parsed, config, arg);
|
|
}
|
|
|
|
function applyInlineValueOption<Parsed extends object, ValueKey extends keyof Parsed & string>(
|
|
arg: string,
|
|
parsed: Parsed,
|
|
inlineOptions: Array<InlineValueOption<ValueKey>>,
|
|
) {
|
|
const option = inlineOptions.find((candidate) => arg.startsWith(candidate.prefix));
|
|
if (!option) {
|
|
return false;
|
|
}
|
|
|
|
parsed[option.key] = arg.slice(option.prefix.length) as Parsed[ValueKey];
|
|
return true;
|
|
}
|
|
|
|
function applyBooleanOption<Parsed extends object, BooleanKey extends keyof Parsed & string>(
|
|
arg: string,
|
|
parsed: Parsed,
|
|
booleanOptions: Map<string, BooleanKey>,
|
|
) {
|
|
const option = booleanOptions.get(arg);
|
|
if (!option) {
|
|
return false;
|
|
}
|
|
|
|
parsed[option] = true as Parsed[BooleanKey];
|
|
return true;
|
|
}
|
|
|
|
function applyValueOrPositionalOption<
|
|
Parsed extends object,
|
|
ValueKey extends keyof Parsed & string,
|
|
BooleanKey extends keyof Parsed & string,
|
|
>(
|
|
args: string[],
|
|
index: number,
|
|
parsed: Parsed,
|
|
config: ParserConfig<Parsed, ValueKey, BooleanKey>,
|
|
arg: string,
|
|
) {
|
|
const option = config.valueOptions.get(arg);
|
|
if (!option) {
|
|
return config.parsePositional(arg, index);
|
|
}
|
|
|
|
parsed[option] = readNextArg(args, index, arg, config.fail) as Parsed[ValueKey];
|
|
return index + 1;
|
|
}
|
|
|
|
export function parseVersionOptionArgument<
|
|
Parsed extends { version?: string },
|
|
ValueKey extends keyof Parsed & string,
|
|
BooleanKey extends keyof Parsed & string,
|
|
>(
|
|
args: string[],
|
|
index: number,
|
|
parsed: Parsed,
|
|
config: Omit<ParserConfig<Parsed, ValueKey, BooleanKey>, "parsePositional"> & {
|
|
printUsage: () => void;
|
|
},
|
|
) {
|
|
return parseMappedArgument(args, index, parsed, {
|
|
...config,
|
|
parsePositional: (arg, positionalIndex) =>
|
|
parseVersionOrHelp(arg, positionalIndex, parsed, config),
|
|
});
|
|
}
|
|
|
|
function parseVersionOrHelp<Parsed extends { version?: string }>(
|
|
arg: string,
|
|
index: number,
|
|
parsed: Parsed,
|
|
config: { printUsage: () => void; fail: (message: string) => never },
|
|
) {
|
|
if (arg === "--help" || arg === "-h") {
|
|
config.printUsage();
|
|
process.exit(0);
|
|
}
|
|
|
|
parsed.version = parseVersionPositionalArg(arg, parsed.version, config.fail);
|
|
return index;
|
|
}
|
|
|
|
export function parseVersionPositionalArg(
|
|
arg: string,
|
|
currentVersion: string | undefined,
|
|
fail: (message: string) => never,
|
|
) {
|
|
if (arg.startsWith("--")) {
|
|
fail(`Unknown option: ${arg}`);
|
|
}
|
|
if (currentVersion) {
|
|
fail(`Unexpected positional argument: ${arg}`);
|
|
}
|
|
|
|
return arg.replace(/^v/, "");
|
|
}
|
|
|
|
export function readNextArg(
|
|
args: string[],
|
|
index: number,
|
|
flag: string,
|
|
fail: (message: string) => never,
|
|
) {
|
|
const value = args[index + 1];
|
|
if (!value || value.startsWith("--")) {
|
|
fail(`Missing value for ${flag}`);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export function validateCliVersion(
|
|
version: string,
|
|
pattern: RegExp,
|
|
fail: (message: string) => never,
|
|
) {
|
|
if (!pattern.test(version)) {
|
|
fail(`Invalid semver: ${version}`);
|
|
}
|
|
}
|
|
|
|
export function validateCliDate(date: string, fail: (message: string) => never) {
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
|
fail(`Invalid date: ${date}. Expected YYYY-MM-DD.`);
|
|
}
|
|
}
|
|
|
|
export function optionalFlagArg(flag: string, enabled: boolean) {
|
|
return enabled ? [flag] : [];
|
|
}
|
|
|
|
export function optionalValueArg(flag: string, value: string | undefined) {
|
|
return value ? [flag, value] : [];
|
|
}
|