All files / src/services profileFingerprint.ts

100% Statements 36/36
100% Branches 9/9
100% Functions 3/3
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 207x 207x 207x 207x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 429x 429x 429x 1x 1x 107x 107x 107x  
import { createHash } from "node:crypto";
import type { ScoringProfile } from "../../../shared/types/profile";
import { profileToPromptText } from "../../../shared/types/profile";
 
/**
 * Profile fingerprints (research R5): lowercase hex SHA-256 of the
 * whitespace-normalized serialized profile. Computed on every read and on
 * every analysis, never stored as the current value — only previous values
 * land in `Profiles.fingerprintHistory` for rescore eligibility (R6).
 */
 
export function profileFingerprint(serialized: string): string {
  return createHash("sha256")
    .update(serialized.replace(/\s+/g, " ").trim(), "utf8")
    .digest("hex");
}
 
interface ProfileCarrier {
  profile?: string;
  profileData?: ScoringProfile;
}
 
/**
 * The profile text the model actually sees: `profileData` serialized with the
 * shared profileToPromptText when present (new clients), otherwise the legacy
 * `profile` string as sent (older extensions). Empty means no profile.
 */
export function resolveScoringProfile(body: ProfileCarrier): string | undefined {
  const serialized = body.profileData ? profileToPromptText(body.profileData) : body.profile;
  return serialized ? serialized : undefined;
}
 
export function fingerprintForRequest(body: ProfileCarrier): string | undefined {
  const serialized = resolveScoringProfile(body);
  return serialized === undefined ? undefined : profileFingerprint(serialized);
}