From bac9de21cdb5f888712eb07f0a298f59089e3fae Mon Sep 17 00:00:00 2001 From: jamesconwaydev <148910373+jamesconwaydev@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:12:44 -0700 Subject: [PATCH 1/3] fix(likes): start Trending on day one and stop publishing detection windows delta_7d now falls back to the oldest snapshot at least a day old when there is none from 7 days ago, and entries missing from the baseline count from 0 Likes. Burst and drip issue text no longer states the windows, counts or account-age cutoff. --- scripts/devstream/stats.py | 5 ++++ scripts/sync_likes.py | 50 ++++++++++++++++++------------------ scripts/tests/test_likes.py | 51 ++++++++++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 26 deletions(-) diff --git a/scripts/devstream/stats.py b/scripts/devstream/stats.py index c6273db..9cd7056 100644 --- a/scripts/devstream/stats.py +++ b/scripts/devstream/stats.py @@ -24,6 +24,11 @@ def read_json(gh, path): return json.loads(base64.b64decode(cur["content"])), cur["sha"] +def list_dir(gh, path) -> list[str]: + """File names in a stats-branch directory; a missing directory is empty.""" + return [f["name"] for f in gh.get(f"{API}/contents/{path}", {"ref": "stats"}) or []] + + def write_json(gh, path, data, sha, message) -> None: body = { "message": message, diff --git a/scripts/sync_likes.py b/scripts/sync_likes.py index 3c2c236..f8d3cd6 100644 --- a/scripts/sync_likes.py +++ b/scripts/sync_likes.py @@ -7,6 +7,7 @@ import json import logging import os +import re import tempfile from datetime import datetime, timedelta, timezone @@ -14,16 +15,14 @@ from scripts.devstream.entries import normalize_repo from scripts.devstream.gh import GitHub, GitHubError from scripts.devstream.likes import ( - YOUNG_DAYS, count_likes, detect_burst, detect_drip, fetch_meta, fetch_reactors, rank_entries, - young_count, ) -from scripts.devstream.stats import API, ensure_branch, read_json, write_json +from scripts.devstream.stats import API, ensure_branch, list_dir, read_json, write_json from scripts.devstream.trust import TrustCache, ensure_block_access # Flagged entries stay listed and ranked; only Trending hides them. @@ -91,37 +90,37 @@ def _record(kind: str, issue: int, now: datetime, aggregate: bool = False) -> di return rec -def _body(kind: str, repo: str, total: int, young: int) -> str: - # Fixed text and counts only: naming reactors would ping them. +def _body(kind: str, repo: str) -> str: + # Fixed text only: naming reactors would ping them, and numbers or windows would show how to tune around the checks. if kind == "burst": return ( - f"{repo} picked up {total} Likes in the last 24 hours, and {young} of them came from " - f"accounts under {YOUNG_DAYS} days old. That's what sock-puppet voting usually looks like, " - "so we've flagged the entry and opened this for a maintainer to check.\n\n" - "The flag stays on while this issue is open, so close it once you've checked. " - "The entry stays listed either way." + "Likes on this entry jumped quickly, and an unusual share came from newer accounts. " + "It's held out of Trending until a maintainer closes this issue." ) return ( - f"{repo} picked up {total} Likes over the last 7 days, and {young} of them came from " - f"accounts under {YOUNG_DAYS} days old. That's slower than a burst, but it's the same " - "pattern spread thin, so it's worth a maintainer's look.\n\n" - "Nothing is flagged for this one. Close the issue once it's been checked." + "A steady stream of Likes on this entry came mostly from newer accounts. " + "Nothing is hidden; this is for a maintainer to look at." ) def _aggregate(listed: list[tuple[str, str]]) -> dict: n = len(listed) - lines = "\n".join(f"- {key} ({kind})" for key, kind in sorted(listed)) + lines = "\n".join(f"- {key}" for key, _ in sorted(listed)) return { "title": f"Like burst: {n} {'entry' if n == 1 else 'entries'}", - "body": ( - "More entries showed a suspicious Like pattern than we open separate issues for, " - f"so they share this one:\n\n{lines}\n\n" - "Bursts stay flagged while this is open. Close it once they've all been checked." - ), + "body": f"Too many entries tripped the Like checks for one issue each, so they share this one:\n\n{lines}", } +def _baseline(gh, now: datetime) -> dict | None: + """The snapshot from 7 days ago, else the oldest one at least a day old, else None.""" + dates = sorted(m.group(1) for n in list_dir(gh, "snapshots") if (m := re.fullmatch(r"(\d{4}-\d\d-\d\d)\.json", n))) + week = f"{now - WEEK:%Y-%m-%d}" + old = [d for d in dates if d <= f"{now - DAY:%Y-%m-%d}"] + pick = week if week in dates else (old[0] if old else None) + return (read_json(gh, f"snapshots/{pick}.json")[0] or {}) if pick else None + + def _report(gh, todo: list[tuple[str, str, str, str]], flags: dict, now: datetime) -> None: """todo is (kind, key, repo, body). Records are set right after each post, so a failure keeps them.""" for kind, key, repo, body in todo[:ISSUE_CAP]: @@ -200,19 +199,20 @@ def trusted(login): if detect_burst(today, ages): bursts.add(k) if _due(flags, k, "burst", now): - candidates.append(("burst", k, repo, _body("burst", repo, len(today), young_count(today, ages)))) + candidates.append(("burst", k, repo, _body("burst", repo))) elif detect_drip(week, ages) and _due(flags, k, "drip", now): - candidates.append(("drip", k, repo, _body("drip", repo, len(week), young_count(week, ages)))) + candidates.append(("drip", k, repo, _body("drip", repo))) candidates.sort(key=lambda c: c[0] != "burst") ranks = rank_entries(likes, {k: m["stars"] for k, m in meta.items()}) - old = read_json(gh, f"snapshots/{now - WEEK:%Y-%m-%d}.json")[0] or {} + old = _baseline(gh, now) out = {} for k in keys: - o = old.get(k) + o = (old or {}).get(k) + base = (o["likes"] if o else 0) if old is not None else likes[k] out[k] = { "likes": likes[k], - "delta_7d": likes[k] - o["likes"] if o else 0, + "delta_7d": likes[k] - base, "rank": ranks[k], "rank_delta_7d": o["rank"] - ranks[k] if o else 0, "flagged": ranked[k]["status"] == "flagged" or k in bursts or _under_review(flags, k), diff --git a/scripts/tests/test_likes.py b/scripts/tests/test_likes.py index b74d86e..589c151 100644 --- a/scripts/tests/test_likes.py +++ b/scripts/tests/test_likes.py @@ -63,6 +63,9 @@ def __init__(self, entries, discussions, users=None, stats=None, stars=None, iss for path, data in self.stats.items(): routes[f"GET {API}/contents/{path}"] = { "content": base64.b64encode(json.dumps(data).encode()).decode(), "sha": f"sha-{path}"} + snaps = sorted(p for p in self.stats if p.startswith("snapshots/")) + if snaps: + routes[f"GET {API}/contents/snapshots"] = [{"name": p.split("/")[1], "type": "file"} for p in snaps] days = [f"snapshots/{NOW + timedelta(days=d):%Y-%m-%d}.json" for d in range(12)] for path in ("meta.json", "likes.json", "flags.json", "trust-cache.json", *days): routes[f"PUT {API}/contents/{path}"] = {} @@ -513,7 +516,20 @@ def test_burst_report_has_no_logins(self): self.assertEqual(sorted(issue["labels"]), ["needs-review", "report"]) self.assertNotIn("zqx", issue["body"]) self.assertNotIn("@", issue["body"]) - self.assertIn("12", issue["body"]) + for word in ("24", "hour", "7 day", "90", "days old"): + self.assertNotIn(word, issue["body"]) + + def test_issue_bodies_have_no_windows(self): + w = World([entry("o/a")], {"o/a": burst_nodes(10, 10)}) + w.run() + [burst] = w.issues_opened() + self.assertIn("jumped quickly", burst["body"]) + repos = [f"o/r{i}" for i in range(7)] + w2 = World([entry(r) for r in repos], {r: burst_nodes(10, 10, prefix=f"p{i}") for i, r in enumerate(repos)}) + w2.run() + for issue in w2.issues_opened(): + for word in ("24", "hour", "7 day", "90", "days old"): + self.assertNotIn(word, issue["body"]) def test_burst_report_deduped(self): w = World([entry("o/a")], {"o/a": burst_nodes(10, 10)}, stats=self.flags(), issues=[{"number": 7}]) @@ -560,6 +576,39 @@ def test_delta_from_snapshot_7_days_back(self): self.assertEqual((likes["o/b"]["delta_7d"], likes["o/b"]["rank_delta_7d"]), (-3, -1)) self.assertEqual((likes["o/c"]["delta_7d"], likes["o/c"]["rank_delta_7d"]), (0, 0)) + def snap(self, days_ago, data): + return {f"snapshots/{NOW - timedelta(days=days_ago):%Y-%m-%d}.json": data} + + def likes_for(self, stats): + w = World([entry("o/a"), entry("o/new")], + {"o/a": [node("x"), node("y"), node("z")], "o/new": [node("x"), node("y")]}, stats=stats) + w.run() + return w.written("likes.json") + + def test_oldest_snapshot_used_when_no_7_day(self): + stats = {**self.snap(3, {"o/a": {"likes": 2, "rank": 2}}), **self.snap(1, {"o/a": {"likes": 3, "rank": 1}})} + a = self.likes_for(stats)["o/a"] + self.assertEqual((a["delta_7d"], a["rank_delta_7d"]), (1, 1)) + + def test_only_today_snapshot_gives_zero(self): + got = self.likes_for(self.snap(0, {"o/a": {"likes": 0, "rank": 1}})) + self.assertEqual((got["o/a"]["delta_7d"], got["o/a"]["rank_delta_7d"]), (0, 0)) + + def test_new_entry_delta_is_its_likes(self): + got = self.likes_for(self.snap(3, {"o/a": {"likes": 1, "rank": 1}})) + self.assertEqual((got["o/new"]["delta_7d"], got["o/new"]["rank_delta_7d"]), (2, 0)) + + def test_exact_7_day_snapshot_preferred(self): + stats = {**self.snap(7, {"o/a": {"likes": 2, "rank": 1}}), **self.snap(3, {"o/a": {"likes": 3, "rank": 1}})} + self.assertEqual(self.likes_for(stats)["o/a"]["delta_7d"], 1) + + def test_listing_error_raises_without_writes(self): + w = World([entry("o/a")], {"o/a": [node("x")]}) + w.routes[f"GET {API}/contents/snapshots"] = Status(500) + with self.assertRaises(GitHubError): + w.run() + self.assertEqual(w.puts(), []) + def test_missing_snapshot_delta_zero(self): w = World([entry("o/a")], {"o/a": [node("x"), node("y")]}) w.run() From 5b92f4295feaecab960f9b99e5ebfe851be1b75b Mon Sep 17 00:00:00 2001 From: jamesconwaydev <148910373+jamesconwaydev@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:13:10 -0700 Subject: [PATCH 2/3] fix(likes): never baseline Trending on a snapshot over 7 days old --- scripts/sync_likes.py | 4 ++-- scripts/tests/test_likes.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/sync_likes.py b/scripts/sync_likes.py index f8d3cd6..04187b9 100644 --- a/scripts/sync_likes.py +++ b/scripts/sync_likes.py @@ -113,10 +113,10 @@ def _aggregate(listed: list[tuple[str, str]]) -> dict: def _baseline(gh, now: datetime) -> dict | None: - """The snapshot from 7 days ago, else the oldest one at least a day old, else None.""" + """The snapshot from 7 days ago, else the oldest one between 1 and 7 days old, else None.""" dates = sorted(m.group(1) for n in list_dir(gh, "snapshots") if (m := re.fullmatch(r"(\d{4}-\d\d-\d\d)\.json", n))) week = f"{now - WEEK:%Y-%m-%d}" - old = [d for d in dates if d <= f"{now - DAY:%Y-%m-%d}"] + old = [d for d in dates if f"{now - WEEK:%Y-%m-%d}" < d <= f"{now - DAY:%Y-%m-%d}"] pick = week if week in dates else (old[0] if old else None) return (read_json(gh, f"snapshots/{pick}.json")[0] or {}) if pick else None diff --git a/scripts/tests/test_likes.py b/scripts/tests/test_likes.py index 589c151..05c1858 100644 --- a/scripts/tests/test_likes.py +++ b/scripts/tests/test_likes.py @@ -590,6 +590,11 @@ def test_oldest_snapshot_used_when_no_7_day(self): a = self.likes_for(stats)["o/a"] self.assertEqual((a["delta_7d"], a["rank_delta_7d"]), (1, 1)) + def test_baseline_never_older_than_7_days(self): + stats = {**self.snap(10, {"o/a": {"likes": 0, "rank": 1}}), **self.snap(4, {"o/a": {"likes": 2, "rank": 3}})} + a = self.likes_for(stats)["o/a"] + self.assertEqual((a["delta_7d"], a["rank_delta_7d"]), (1, 2)) + def test_only_today_snapshot_gives_zero(self): got = self.likes_for(self.snap(0, {"o/a": {"likes": 0, "rank": 1}})) self.assertEqual((got["o/a"]["delta_7d"], got["o/a"]["rank_delta_7d"]), (0, 0)) From a927236b1e52a8c1339ccc6c0724f6ca9555ad15 Mon Sep 17 00:00:00 2001 From: jamesconwaydev <148910373+jamesconwaydev@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:15:45 -0700 Subject: [PATCH 3/3] fix(likes): harden snapshot baseline and gate Trending on age or seed Skip snapshot files with impossible or non-ASCII dates, fail closed when the chosen snapshot cannot be read, and make list_dir keep files only. Trending now needs delta_7d >= 5 and an entry listed for 7 days or added by seed. --- scripts/devstream/stats.py | 7 ++++++- scripts/sync_likes.py | 22 ++++++++++++++++++---- scripts/tests/test_likes.py | 18 ++++++++++++++++++ scripts/tests/test_stats.py | 21 +++++++++++++++++++++ site/src/lib/data.ts | 14 ++++++++++++-- site/tests/data.test.mjs | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 scripts/tests/test_stats.py diff --git a/scripts/devstream/stats.py b/scripts/devstream/stats.py index 9cd7056..6a6b053 100644 --- a/scripts/devstream/stats.py +++ b/scripts/devstream/stats.py @@ -26,7 +26,12 @@ def read_json(gh, path): def list_dir(gh, path) -> list[str]: """File names in a stats-branch directory; a missing directory is empty.""" - return [f["name"] for f in gh.get(f"{API}/contents/{path}", {"ref": "stats"}) or []] + got = gh.get(f"{API}/contents/{path}", {"ref": "stats"}) + if got is None: + return [] + if not isinstance(got, list): + raise GitHubError(200, f"{path} is not a directory listing") + return [f["name"] for f in got if f.get("type") == "file"] def write_json(gh, path, data, sha, message) -> None: diff --git a/scripts/sync_likes.py b/scripts/sync_likes.py index 04187b9..a8d110e 100644 --- a/scripts/sync_likes.py +++ b/scripts/sync_likes.py @@ -112,13 +112,27 @@ def _aggregate(listed: list[tuple[str, str]]) -> dict: } +def _snapshot_date(name: str): + m = re.fullmatch(r"([0-9]{4}-[0-9]{2}-[0-9]{2})\.json", name) + try: + return datetime.strptime(m.group(1), "%Y-%m-%d").date() if m else None + except ValueError: + return None + + def _baseline(gh, now: datetime) -> dict | None: """The snapshot from 7 days ago, else the oldest one between 1 and 7 days old, else None.""" - dates = sorted(m.group(1) for n in list_dir(gh, "snapshots") if (m := re.fullmatch(r"(\d{4}-\d\d-\d\d)\.json", n))) - week = f"{now - WEEK:%Y-%m-%d}" - old = [d for d in dates if f"{now - WEEK:%Y-%m-%d}" < d <= f"{now - DAY:%Y-%m-%d}"] + week, day = (now - WEEK).date(), (now - DAY).date() + dates = sorted(d for n in list_dir(gh, "snapshots") if (d := _snapshot_date(n))) + old = [d for d in dates if week < d <= day] pick = week if week in dates else (old[0] if old else None) - return (read_json(gh, f"snapshots/{pick}.json")[0] or {}) if pick else None + if pick is None: + return None + data = read_json(gh, f"snapshots/{pick:%Y-%m-%d}.json")[0] + if data is None: + # Listed but unreadable: fail closed rather than read it as an empty baseline. + raise GitHubError(404, f"snapshot {pick} is listed but could not be read") + return data def _report(gh, todo: list[tuple[str, str, str, str]], flags: dict, now: datetime) -> None: diff --git a/scripts/tests/test_likes.py b/scripts/tests/test_likes.py index 05c1858..a7769b7 100644 --- a/scripts/tests/test_likes.py +++ b/scripts/tests/test_likes.py @@ -595,6 +595,24 @@ def test_baseline_never_older_than_7_days(self): a = self.likes_for(stats)["o/a"] self.assertEqual((a["delta_7d"], a["rank_delta_7d"]), (1, 2)) + def test_impossible_and_non_ascii_dates_skipped(self): + stats = {**self.snap(3, {"o/a": {"likes": 2, "rank": 1}}), + "snapshots/2026-10-35.json": {"o/a": {"likes": 0, "rank": 1}}, + "snapshots/2026-10-٣٠.json": {"o/a": {"likes": 0, "rank": 1}}, + "snapshots/2026-10-31.json.bak": {"o/a": {"likes": 0, "rank": 1}}} + self.assertEqual(self.likes_for(stats)["o/a"]["delta_7d"], 1) + + def test_unreadable_baseline_raises_without_writes(self): + w = World([entry("o/a")], {"o/a": [node("x")]}, stats=self.snap(3, {})) + del w.routes[f"GET {API}/contents/snapshots/{NOW - timedelta(days=3):%Y-%m-%d}.json"] + with self.assertRaises(GitHubError): + w.run() + self.assertEqual(w.puts(), []) + + def test_empty_baseline_file_counts_from_zero(self): + got = self.likes_for(self.snap(3, {})) + self.assertEqual(got["o/a"]["delta_7d"], 3) + def test_only_today_snapshot_gives_zero(self): got = self.likes_for(self.snap(0, {"o/a": {"likes": 0, "rank": 1}})) self.assertEqual((got["o/a"]["delta_7d"], got["o/a"]["rank_delta_7d"]), (0, 0)) diff --git a/scripts/tests/test_stats.py b/scripts/tests/test_stats.py new file mode 100644 index 0000000..838eaa4 --- /dev/null +++ b/scripts/tests/test_stats.py @@ -0,0 +1,21 @@ +import unittest + +from scripts.devstream.gh import GitHubError +from scripts.devstream.stats import API, list_dir +from scripts.tests.fake_gh import FakeGitHub + + +class ListDir(unittest.TestCase): + def test_keeps_files_only(self): + gh = FakeGitHub({f"GET {API}/contents/d": [ + {"name": "a.json", "type": "file"}, {"name": "sub", "type": "dir"}, {"name": "l", "type": "symlink"}]}) + self.assertEqual(list_dir(gh, "d"), ["a.json"]) + + def test_missing_directory_is_empty(self): + self.assertEqual(list_dir(FakeGitHub({}), "d"), []) + + def test_non_list_raises(self): + # A path that is a file comes back as an object, not a listing. + gh = FakeGitHub({f"GET {API}/contents/d": {"name": "d", "type": "file", "content": ""}}) + with self.assertRaises(GitHubError): + list_dir(gh, "d") diff --git a/site/src/lib/data.ts b/site/src/lib/data.ts index ee74373..2b9ad18 100644 --- a/site/src/lib/data.ts +++ b/site/src/lib/data.ts @@ -141,9 +141,19 @@ export const liveList = (all: SiteEntry[]) => .sort(byTop) .map((e, i) => ({ ...e, rank: i + 1 })); -export const trending = (all: SiteEntry[]) => +// Trending needs a real climb, and a listing old enough (or curated) that a fresh account can't launch itself onto it. +export const TRENDING_MIN_DELTA = 5; +const TRENDING_MIN_AGE_MS = 7 * 24 * 60 * 60 * 1000; + +export const trending = (all: SiteEntry[], now = Date.now()) => liveList(all) - .filter((e) => !isFlagged(e) && e.delta_7d > 0) + .filter( + (e) => + !isFlagged(e) && + e.delta_7d >= TRENDING_MIN_DELTA && + (e.added_by_seed || + now - Date.parse(e.submitted_at) >= TRENDING_MIN_AGE_MS), + ) .sort((a, b) => b.delta_7d - a.delta_7d); export const newest = (all: SiteEntry[]) => diff --git a/site/tests/data.test.mjs b/site/tests/data.test.mjs index 0c31a61..e75306b 100644 --- a/site/tests/data.test.mjs +++ b/site/tests/data.test.mjs @@ -163,3 +163,40 @@ test("a Discussion link needs a positive integer", () => { null, ); }); + +const mk = (repo, o) => ({ + repo, + status: "live", + flagged: false, + added_by_seed: false, + submitted_at: "2026-09-01T00:00:00Z", + likes: 1, + stars: 0, + delta_7d: 0, + ...o, +}); +const NOW = Date.parse("2026-11-05T12:00:00Z"); + +test("trending needs delta >= 5 and age or seed, and no flag", () => { + const list = [ + mk("new/community", { delta_7d: 20, submitted_at: "2026-11-03T00:00:00Z" }), + mk("new/seed", { + delta_7d: 6, + submitted_at: "2026-11-03T00:00:00Z", + added_by_seed: true, + }), + mk("old/four", { delta_7d: 4 }), + mk("old/five", { delta_7d: 5 }), + mk("old/flagged", { delta_7d: 50, flagged: true }), + ]; + assert.deepEqual(names(trending(list, NOW)), ["new/seed", "old/five"]); + assert.equal(liveList(list).length, 5, "still listed and ranked"); +}); + +test("trending age: exactly 7 days old qualifies", () => { + const e = mk("edge/seven", { + delta_7d: 9, + submitted_at: "2026-10-29T12:00:00Z", + }); + assert.deepEqual(names(trending([e], NOW)), ["edge/seven"]); +});