From 3ab8035d0dd9d9ee1b557de2129b6a7fbed0afda Mon Sep 17 00:00:00 2001 From: Wolf Date: Tue, 29 Sep 2026 21:52:26 +0200 Subject: [PATCH] Gate a favourite by the require gates and drop its 0.9 floor A favourite company skips the any group only; a require gate it fails gates it with the usual reason. Its score is the weighted sum with the affinity term at 1, and weights that sum to 0 give "no weights" as for every other company. Co-Authored-By: Claude --- app/jobs/score.py | 22 ++++------ tests/test_score.py | 97 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 18 deletions(-) diff --git a/app/jobs/score.py b/app/jobs/score.py index cc0171c..8526829 100644 --- a/app/jobs/score.py +++ b/app/jobs/score.py @@ -29,8 +29,9 @@ roles out of extract's reach for good. Every term is 0..1. The score is the weighted sum over the sum of the weights (the agent's four and its prefer gates'), so it stays 0..1. A favourite -company (`affinity = 2`) skips the gates and is floored at 0.9; a hidden one -(`-1`) is not scored at all. A role with no vector is scored without `fit` +company (`affinity = 2`) skips the `any` group but not the `require` gates, and +is scored like any other company with its affinity term at 1: nothing puts +its roles above a better fit. A hidden one (`-1`) is not scored at all. A role with no vector is scored without `fit` and `taste`, unless `fit` is all there is, when it is gated as "no vector". An agent with no votes scores every role without `taste`; the direction is newsdesk's `centroid(liked) - centroid(disliked)`. An agent with no prompt @@ -63,9 +64,6 @@ log = logging.getLogger(__name__) HALF_LIFE_DAYS = 14.0 -#: Where a favourite company's roles start, whatever else they score. -FAVOURITE_FLOOR = 0.9 - #: The `state` key that records the starter agents were imported. SEEDED = "agents:seeded" @@ -160,13 +158,13 @@ def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]: """Why this role is gated, as reasons: every require gate it fails, and the `any` group when it matches none of them. A gate with an empty column (state -1) neither passes nor fails, so the group fails only when some - member had evidence. None for a favourite, which skips the gates.""" - if candidate.affinity == 2: - return [] + member had evidence. A favourite company's roles skip the `any` group but + not the require gates, so a star widens a search to a company's roles + within e.g. Europe, not to the whole world.""" states = list(zip(gates, candidate.gates, strict=True)) reasons = [_reason(g) for g, state in states if g.mode == "require" and state == 0] group = [(g, state) for g, state in states if g.mode == "any"] - if group and not any(state == 1 for _, state in group) \ + if candidate.affinity != 2 and group and not any(state == 1 for _, state in group) \ and any(state == 0 for _, state in group): reasons.append("none of " + ", ".join(_wanted(g) for g, _ in group)) return reasons @@ -179,7 +177,6 @@ def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights, already scaled to 0..1 (see `scale`), or None when there is nothing to measure: no role vector, no prompt vector, no votes. A None term leaves the denominator; `missing` says why fit is out.""" - favourite = candidate.affinity == 2 if failed := failed_gates(candidate, gates): return 0.0, ", ".join(failed) @@ -193,12 +190,9 @@ def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights, terms.append((weights.taste, taste)) total = sum(w for w, _ in terms) if not total: - if favourite: - return FAVOURITE_FLOOR, "" # Fit was the only term with any weight, and there is no fit. return 0.0, missing if fit is None and weights.fit else "no weights" - score = sum(w * t for w, t in terms) / total - return (max(score, FAVOURITE_FLOOR) if favourite else score), "" + return sum(w * t for w, t in terms) / total, "" # ------------------------------------------------------------------ vectors diff --git a/tests/test_score.py b/tests/test_score.py index e673276..baed3ad 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -379,17 +379,106 @@ def test_a_prefer_gate_adds_its_weight_to_the_score( assert scores[office][1] == "" -def test_a_favourite_skips_the_gates_and_is_floored( +def test_a_favourite_is_gated_by_a_require_gate_it_fails( store: Store, config: Config, fake: FakeEmbedder) -> None: + """A star widens the search to the company, not past a require gate: the + Munich role of a starred company is gated like anyone's.""" store.add_company("Fave") - fave = role(store, "1", company="Fave", place="Munich") + munich = role(store, "1", company="Fave", place="Munich") + berlin = role(store, "2", company="Fave", place="Berlin") agent = store.save_agent("Berlin", "", [Gate(field="place", values=["Berlin"])]) store.set_affinity(agent.id, "fave", 2) run(config, store, score.run_score) - value, reason = store.scores(agent.id)[fave] - assert reason == "" and value >= 0.9 + scores = store.scores(agent.id) + assert scores[munich] == (0.0, "place not Berlin") + assert scores[berlin][1] == "" and scores[berlin][0] > 0 + + +def test_a_favourite_passes_the_any_group_others_fail( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """"Denmark or remote" gates the onsite German role of a neutral company + and lets the same role of a starred one through.""" + store.add_company("Fave") + fave = role(store, "1", company="Fave", place="Berlin, Germany", remote="onsite") + other = role(store, "2", company="Other", place="Berlin, Germany", remote="onsite") + agent = store.save_agent("Copenhagen or remote", "", [ + Gate(field="country", values=["DK"], mode="any"), + Gate(field="remote", values=["remote"], mode="any")]) + store.set_affinity(agent.id, "fave", 2) + + run(config, store, score.run_score) + + scores = store.scores(agent.id) + assert scores[fave][1] == "" and scores[fave][0] > 0 + assert scores[other] == (0.0, "none of country DK, remote remote") + + +def test_a_favourite_takes_both_halves_of_the_rule_in_one_agent() -> None: + gates = [Gate(field="country", values=["DK"]), + Gate(field="remote", values=["remote"], mode="any"), + Gate(field="seniority", values=["senior"], mode="any")] + def cand(affinity: int, *states: int) -> Candidate: + return Candidate(uid="u", posted_at=NOW.isoformat(), affinity=affinity, + gates=list(states)) + # The any group fails for both; only the neutral company is gated by it. + assert score.failed_gates(cand(2, 1, 0, 0), gates) == [] + assert score.failed_gates(cand(0, 1, 0, 0), gates) == [ + "none of remote remote, seniority senior"] + # The require gate fails for both. + assert score.failed_gates(cand(2, 0, 0, 0), gates) == ["country not DK"] + assert score.failed_gates(cand(0, 0, 0, 0), gates) == [ + "country not DK", "none of remote remote, seniority senior"] + + +def test_a_favourite_with_an_empty_column_is_not_gated() -> None: + """A require gate rejects on evidence only, for a favourite as for anyone.""" + gates = [Gate(field="country", values=["DK"])] + candidate = Candidate(uid="u", posted_at=NOW.isoformat(), affinity=2, gates=[-1]) + assert score.failed_gates(candidate, gates) == [] + value, reason = score.score_one(candidate, gates, Weights(), 0.5, NOW) + assert reason == "" and value > 0 + + +def test_a_favourite_ranks_above_the_same_role_of_a_neutral_company( + store: Store, config: Config, fake: FakeEmbedder) -> None: + store.add_company("Fave") + fave = role(store, "1", company="Fave", body="python backend") + other = role(store, "2", company="Other", body="python backend") + agent = store.save_agent("Backend", "python backend") + store.set_affinity(agent.id, "fave", 2) + + run(config, store, score.run_score) + + scores = store.scores(agent.id) + assert scores[fave][1] == "" == scores[other][1] + # Same age, and no vectors here, so fit is out and recency and affinity + # share the weight: the affinity term alone (1 against 1/3) is the gap. + assert scores[fave][0] - scores[other][0] == pytest.approx(0.5 * (1 - 1 / 3), abs=1e-5) + + +def test_a_favourite_is_the_weighted_sum_with_its_affinity_at_one() -> None: + """No floor: a stale, poorly fitting role of a favourite scores what its + terms say, well under the old 0.9.""" + stale = (NOW - timedelta(days=28)).isoformat() + candidate = Candidate(uid="u", posted_at=stale, affinity=2) + value, reason = score.score_one(candidate, [], Weights(), 0.0, NOW) + assert reason == "" + assert value == pytest.approx((0.2 * 0.25 + 0.2 * 1.0) / 1.0) + assert value < 0.9 + # A perfect role is not lifted to anything but 1. + fresh = Candidate(uid="u", posted_at=NOW.isoformat(), affinity=2) + assert score.score_one(fresh, [], Weights(), 1.0, NOW)[0] == pytest.approx(1.0) + + +def test_a_favourite_with_no_weights_is_scored_like_any_other_company() -> None: + """Weights that sum to 0 leave nothing to average: 0 with a reason, not a + number a favourite alone would get.""" + none = Weights(fit=0, recency=0, affinity=0, taste=0) + for affinity in (0, 2): + candidate = Candidate(uid="u", posted_at=NOW.isoformat(), affinity=affinity) + assert score.score_one(candidate, [], none, None, NOW) == (0.0, "no weights") def test_a_hidden_company_is_never_scored_for_the_agent_that_hid_it(