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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 6x 6x 6x 6x 1x 1x 1x 5x 20x 6x 6x 5x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 72x 2x 2x 70x | /**
* Quality score for marketplace ranking.
*
* Locked decisions from /plan-ceo-review + /plan-eng-review:
* - 6A: rationale-backed weights, env-tunable, sum to 100
* - 2A2: permissive failure mode — clamp invalid inputs to 0, log, continue
* - E1A: NOT called from incrementViewCount path
* - 1B: invoked from the DAL save funnel and from the recompute cron only
*
* Score range: 0-100. Higher = better surface placement on marketplace listings.
*
* RATIONALE for the default weights:
* - W_PHOTO=30: a project with 1 photo looks empty in the homepage hero strip.
* Photos are the primary signal of "did the pro actually do this work".
* - W_COVER=15: a marked cover image signals the pro cared enough to curate.
* Cheap to game (just toggle isCover on any photo) so weighted lower.
* - W_COMPLETE=30: description + room metadata + materials all present means
* the pro filled in what visitors actually read. High-quality content
* correlates with high-trust pros.
* - W_RECENCY=25: keeps the marketplace feeling alive. Decays linearly over
* 6 months — projects past that point sink unless they have other strong
* signals.
*
* Tune at runtime via env: SCORE_WEIGHT_PHOTO, SCORE_WEIGHT_COVER,
* SCORE_WEIGHT_COMPLETE, SCORE_WEIGHT_RECENCY. Set via `wrangler secret put`
* to avoid a redeploy when iterating.
*/
const DEFAULT_WEIGHTS = {
W_PHOTO: 30,
W_COVER: 15,
W_COMPLETE: 30,
W_RECENCY: 25,
} as const;
const PHOTO_SATURATION = 10; // After 10 photos, more doesn't increase the score.
const RECENCY_DECAY_DAYS = 180; // Linear decay to 0 over 180 days.
const DESC_MIN_LENGTH = 50; // Below this, descriptionScore contribution is 0.
export interface QualityScoreInputs {
projectId: string;
photoCount: number;
hasCover: boolean;
descriptionLength: number;
roomCount: number;
hasMaterials: boolean;
daysSinceUpdate: number;
}
export interface QualityScoreWeights {
W_PHOTO: number;
W_COVER: number;
W_COMPLETE: number;
W_RECENCY: number;
}
export interface QualityScoreResult {
score: number; // 0-100, integer
components: {
photo: number;
cover: number;
complete: number;
recency: number;
};
}
/** Named error for unexpected internal failures. NOT thrown for clamped inputs. */
export class QualityScoreComputeError extends Error {
constructor(message: string, public readonly cause?: unknown) {
super(message);
this.name = "QualityScoreComputeError";
}
}
/**
* Read weights from env, falling back to defaults. Each weight is parsed as
* a non-negative integer; invalid values fall back to the default.
*/
export function weightsFromEnv(env: Partial<{
SCORE_WEIGHT_PHOTO: string;
SCORE_WEIGHT_COVER: string;
SCORE_WEIGHT_COMPLETE: string;
SCORE_WEIGHT_RECENCY: string;
}> = {}): QualityScoreWeights {
const parse = (raw: string | undefined, fallback: number) => {
if (!raw) return fallback;
const n = Number.parseInt(raw, 10);
return Number.isFinite(n) && n >= 0 ? n : fallback;
};
return {
W_PHOTO: parse(env.SCORE_WEIGHT_PHOTO, DEFAULT_WEIGHTS.W_PHOTO),
W_COVER: parse(env.SCORE_WEIGHT_COVER, DEFAULT_WEIGHTS.W_COVER),
W_COMPLETE: parse(env.SCORE_WEIGHT_COMPLETE, DEFAULT_WEIGHTS.W_COMPLETE),
W_RECENCY: parse(env.SCORE_WEIGHT_RECENCY, DEFAULT_WEIGHTS.W_RECENCY),
};
}
/**
* Compute the quality score for a project.
*
* Permissive (2A2): out-of-range, NaN, or negative inputs are clamped silently
* and logged via the callback. Returns 0 when inputs are unusable. Throws
* QualityScoreComputeError only for unexpected internal failures (the math
* itself; not input validation).
*/
export function computeQualityScore(
inputs: QualityScoreInputs,
weights: QualityScoreWeights = DEFAULT_WEIGHTS,
onClamp?: (event: ClampEvent) => void,
): QualityScoreResult {
try {
// Clamp every input. Out-of-range values become safe defaults and
// emit a structured log so we can spot bad data without breaking saves.
const photoCount = clampNonNegInt(
inputs.photoCount,
0,
"photoCount",
inputs.projectId,
onClamp,
);
const descriptionLength = clampNonNegInt(
inputs.descriptionLength,
0,
"descriptionLength",
inputs.projectId,
onClamp,
);
const roomCount = clampNonNegInt(
inputs.roomCount,
0,
"roomCount",
inputs.projectId,
onClamp,
);
const daysSinceUpdate = clampNonNegInt(
inputs.daysSinceUpdate,
0,
"daysSinceUpdate",
inputs.projectId,
onClamp,
);
const hasCover = Boolean(inputs.hasCover);
const hasMaterials = Boolean(inputs.hasMaterials);
// Each sub-score is in [0, 1] so each component contributes 0 to its
// weight value. The total is therefore in [0, sum_of_weights].
const photoSub = Math.min(photoCount, PHOTO_SATURATION) / PHOTO_SATURATION;
const coverSub = hasCover ? 1 : 0;
const completeSub =
(descriptionLength > DESC_MIN_LENGTH ? 0.5 : 0) +
(roomCount > 0 ? 0.3 : 0) +
(hasMaterials ? 0.2 : 0);
const recencySub = Math.max(
0,
1 - daysSinceUpdate / RECENCY_DECAY_DAYS,
);
const photo = photoSub * weights.W_PHOTO;
const cover = coverSub * weights.W_COVER;
const complete = completeSub * weights.W_COMPLETE;
const recency = recencySub * weights.W_RECENCY;
const total = photo + cover + complete + recency;
// Round to integer; clamp to [0, 100] in case weights sum > 100.
const score = Math.max(0, Math.min(100, Math.round(total)));
return {
score,
components: {
photo: Math.round(photo),
cover: Math.round(cover),
complete: Math.round(complete),
recency: Math.round(recency),
},
};
} catch (err) {
throw new QualityScoreComputeError(
`Failed to compute quality score for project ${inputs.projectId}`,
err,
);
}
}
export interface ClampEvent {
event: "quality_score.input.clamped";
projectId: string;
fieldName: string;
originalValue: unknown;
clampedValue: number;
}
function clampNonNegInt(
value: number | undefined | null,
fallback: number,
fieldName: string,
projectId: string,
onClamp: ((event: ClampEvent) => void) | undefined,
): number {
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
onClamp?.({
event: "quality_score.input.clamped",
projectId,
fieldName,
originalValue: value,
clampedValue: fallback,
});
return fallback;
}
return Math.floor(value);
}
|