Date a role by posted_at, not just first_seen

recency and the newest feed both counted from first_seen, when the app
itself first stored a role, so a newly added board's whole backlog scored
as freshly posted for two weeks: 10,223 of 15,415 board roles were posted
more than 30 days before they were first seen (average gap in days: Ashby
78, Greenhouse 143, Lever 226, Personio 333).

Add roles.posted_at (migration 9): the earlier of first_seen and a trusted
host's published_at, where TRUSTED_POSTED_HOSTS (app/models.py) records
which of the ats and search hosts report a creation date rather than one
that moves on an edit, checked against live boards and the live database.
A future or pre-2015 date is not trusted either. upsert_role sets it on
insert and keeps the earliest on update, so an edit on the board can only
move it earlier, never later. recency, the feed's newest order, the "new
since" days filter and _top_scope's tie-break now read posted_at;
first_seen is unchanged and still what the UI shows as when the app found
a role. The row's age in roles.js now shows the posted date instead.

A partial index (posted_at DESC, uid) WHERE closed_at IS NULL serves the
newest feed; sqlite3's bundled build has no STAT4, so its planner
otherwise sorts every open role instead of using it, measured 30x slower
on the live database, so Store.roles() forces it for that query shape.

Co-Authored-By: Claude Sonnet <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Sonnet committed 2026-09-29 14:13:06 +02:00
1 parent 0558ef19ed
commit 75eca1c914
8 files changed
+335 -33

No files matched your search

+36 -3
View File
@@ -12,10 +12,13 @@ shipped.
from __future__ import annotations
import json
import logging
import sqlite3
from collections.abc import Callable
from app.models import posted_at, posted_host
log = logging.getLogger(__name__)
SCHEMA = """
@@ -56,9 +59,15 @@ CREATE TABLE IF NOT EXISTS roles (
feed_text TEXT NOT NULL DEFAULT '', -- the teaser a feed handed over
original_url TEXT NOT NULL DEFAULT '', -- the posting page
published_at TEXT NOT NULL, -- what the source claims, UTC
-- What storing postings buys. first_seen is the only honest posted date,
-- since Greenhouse resets updated_at on every edit. closed_at is set when a
-- board fetched in full no longer lists the role, and cleared if it returns.
-- What storing postings buys. first_seen is when the app itself first
-- stored the role; posted_at (app.models.posted_at) is the earlier of
-- first_seen and published_at, trusted only for a host known to report a
-- creation date rather than one that moves on an edit
-- (app.models.TRUSTED_POSTED_HOSTS) — recency and the feed's newest order
-- read posted_at, first_seen stays "when the app found it". closed_at is
-- set when a board fetched in full no longer lists the role, and cleared
-- if it returns.
posted_at TEXT NOT NULL DEFAULT '',
first_seen TEXT NOT NULL,
last_seen TEXT NOT NULL,
closed_at TEXT,
@@ -241,6 +250,11 @@ CREATE INDEX IF NOT EXISTS idx_roles_company ON roles(company_key);
CREATE INDEX IF NOT EXISTS idx_roles_place ON roles(place_key);
CREATE INDEX IF NOT EXISTS idx_roles_first_seen ON roles(first_seen DESC);
CREATE INDEX IF NOT EXISTS idx_roles_closed ON roles(closed_at);
-- Serves the newest feed (open_only, ordered by posted_at DESC), which is
-- most of what `Store.roles` runs: partial, since only an open role's
-- position in that order is ever asked for.
CREATE INDEX IF NOT EXISTS idx_roles_posted
ON roles(posted_at DESC, uid) WHERE closed_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_roles_cluster ON roles(company_key, title_key, place_key);
CREATE INDEX IF NOT EXISTS idx_roles_board ON roles(source, subscription_id);
CREATE INDEX IF NOT EXISTS idx_application_events
@@ -257,6 +271,23 @@ MARKER = "roles"
Step = str | Callable[[sqlite3.Connection], None]
def _migrate_posted_at(conn: sqlite3.Connection) -> None:
"""v9's step: add `posted_at`, then backfill it for every row already
stored, by the same rule `Store.upsert_role` applies to a new one (see
`app.models.posted_at`). A callable, not SQL, because the rule reads
`raw` for which host a role came from. `'' NOT NULL` is a placeholder
only until the UPDATE below runs, in the same transaction as the ALTER
(`_apply` wraps every migration in one); every row gets a real value."""
conn.execute("ALTER TABLE roles ADD COLUMN posted_at TEXT NOT NULL DEFAULT ''")
rows = conn.execute("SELECT uid, published_at, first_seen, raw FROM roles").fetchall()
conn.executemany(
"UPDATE roles SET posted_at = ? WHERE uid = ?",
[(posted_at(row["published_at"], row["first_seen"],
posted_host(json.loads(row["raw"] or "{}"))), row["uid"])
for row in rows])
MIGRATIONS: tuple[tuple[int, str, Step], ...] = (
# Never run as a migration: a file without the marker table is created
# from `SCHEMA` and stamped. It exists so the first real migration is 2.
@@ -338,6 +369,8 @@ ALTER TABLE companies ADD COLUMN city TEXT NOT NULL DEFAULT '';
(8, "agents.limits: how deep into its ranking the model stages read is each agent's own", """
ALTER TABLE agents ADD COLUMN limits TEXT NOT NULL DEFAULT '{}';
"""),
(9, "roles.posted_at: recency and the newest feed count from when a role "
"was posted, not just when the app found it", _migrate_posted_at),
)
#: What a file created from `SCHEMA` today is already at.