diff --git a/app/jobs/score.py b/app/jobs/score.py
index bdbf781..c6e3c09 100644
--- a/app/jobs/score.py
+++ b/app/jobs/score.py
@@ -6,7 +6,9 @@ good for a given search. For each agent:
require gates a role that fails one keeps its row, with score 0 and the
reason ("place unknown", "remote not remote")
prefer gates `weight` when the role matches, 0 when it does not
- fit cos(role vector, intent vector), mapped to 0..1
+ fit cos(role vector, intent vector), min-max scaled across
+ the roles this agent scores in this run: the closest is 1,
+ the farthest 0, a lone role 1
recency 0.5 ** (days since first_seen / 14)
affinity (companies.affinity + 1) / 3
@@ -65,13 +67,30 @@ def affinity_term(affinity: int) -> float:
return (affinity + 1) / 3
-def fit_term(role: np.ndarray, intent: np.ndarray) -> float:
- """Cosine to 0..1. Rescaled rather than clipped, as newsdesk does, so a
- role pointing away from the intent sits below an unrelated one."""
+def cosine(role: np.ndarray, intent: np.ndarray) -> float:
+ """Raw cosine, -1..1; 0 when either vector is zero."""
norms = float(np.linalg.norm(role) * np.linalg.norm(intent))
if norms == 0:
- return 0.5
- return (float(np.dot(role, intent)) / norms + 1.0) / 2.0
+ return 0.0
+ return float(np.dot(role, intent)) / norms
+
+
+def scale(cosines: dict[str, float]) -> dict[str, float]:
+ """Min-max to 0..1 across one agent's run, so `fit` ranks roles rather
+ than stating a level.
+
+ `(cos + 1) / 2` was the level, and on MiniLM it spans about 0.55 to 0.79
+ for real postings against a real intent, so a prefer gate weighted 0.2
+ moved a score more than the whole spread of fit did. Scaled, the closest
+ role is 1 and the farthest 0. A single role, or roles that are all equally
+ close, get 1: there is nothing to tell them apart by.
+ """
+ if not cosines:
+ return {}
+ low, high = min(cosines.values()), max(cosines.values())
+ if high - low < 1e-9:
+ return dict.fromkeys(cosines, 1.0)
+ return {uid: (value - low) / (high - low) for uid, value in cosines.items()}
def _reason(gate: Gate, state: int) -> str:
@@ -82,17 +101,24 @@ def _reason(gate: Gate, state: int) -> str:
return f"{gate.field} not {' / '.join(str(v) for v in gate.values)}"
+def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]:
+ """Every require gate this role fails, as reasons. None for a favourite,
+ which skips the gates."""
+ if candidate.affinity == 2:
+ return []
+ return [_reason(g, state) for g, state in zip(gates, candidate.gates, strict=True)
+ if g.mode == "require" and state != 1]
+
+
def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights,
fit: float | None, now: datetime, *,
missing: str = "no vector") -> tuple[float, str]:
- """One role for one agent: `(score, gated_reason)`. `fit` is None when
- the role or the intent has no vector; `missing` says which."""
+ """One role for one agent: `(score, gated_reason)`. `fit` is already
+ scaled to 0..1 (see `scale`), or None when the role or the intent has no
+ vector; `missing` says which."""
favourite = candidate.affinity == 2
- if not favourite:
- failed = [_reason(g, state) for g, state in zip(gates, candidate.gates, strict=True)
- if g.mode == "require" and state != 1]
- if failed:
- return 0.0, ", ".join(failed)
+ if failed := failed_gates(candidate, gates):
+ return 0.0, ", ".join(failed)
terms = [(weights.recency, recency(candidate.first_seen, now)),
(weights.affinity, affinity_term(candidate.affinity))]
@@ -157,14 +183,23 @@ def _intent(store: Store, agent: Agent, model: str,
def score_agent(store: Store, agent: Agent, vectors: dict[str, np.ndarray],
intent: np.ndarray | None, now: datetime) -> dict[str, int]:
+ """Two passes: the cosines of the roles this agent scores, then every
+ score with fit scaled across them. A gated role is left out of the
+ scaling, so a role that fails a require gate cannot stretch the range the
+ ranked roles share."""
+ candidates = store.score_candidates(agent.gates)
+ cosines: dict[str, float] = {}
+ if intent is not None:
+ for candidate in candidates:
+ role_vec = vectors.get(candidate.uid)
+ if role_vec is not None and not failed_gates(candidate, agent.gates):
+ cosines[candidate.uid] = cosine(role_vec, intent)
+ fits = scale(cosines)
rows: list[tuple[str, float, str]] = []
gated = 0
- for candidate in store.score_candidates(agent.gates):
- role_vec = vectors.get(candidate.uid)
- fit = fit_term(role_vec, intent) if role_vec is not None and intent is not None \
- else None
+ for candidate in candidates:
value, reason = score_one(
- candidate, agent.gates, agent.weights, fit, now,
+ candidate, agent.gates, agent.weights, fits.get(candidate.uid), now,
missing="no vector" if intent is not None else "no intent vector")
gated += bool(reason)
rows.append((candidate.uid, round(value, 6), reason))
diff --git a/app/templates/agents.html b/app/templates/agents.html
index 9780dd2..ea7b701 100644
--- a/app/templates/agents.html
+++ b/app/templates/agents.html
@@ -14,7 +14,7 @@
-