Files
substrata/scripts/db/003-sweep.sql
T
CatoandClaude Opus 5 c54611814a feat(sweep): run the research sweep on a timer, into a queue rather than the corpus (#68)
The site's promise is that it tells you what changed, and it has been reading
"Updated 2026-09-14" for four days because the sweep only ran when a person ran
it. 92% of company profiles carry no dated event at all. That is the deepest
problem on the site: a research service that is visibly stale has stopped being
the thing it claims to be.

The sweep now runs unattended, and deliberately cannot publish.

Where it writes is the whole design. The corpus stays files in git, accepted by
a person in a commit, because "a human read this and accepted it" is the claim
every row on the site makes. A timer writing into it would publish rows nobody
read, and a release directory is replaced by the next deploy anyway. So findings
land in `research_sweep_candidates`: a queue of leads, none of which reaches a
page until somebody promotes it. A test greps the scheduled path for `writeFile`
and the corpus paths to keep that true.

It is a route rather than a script because the box runs releases, not checkouts,
and the fleet's cron convention calls an authenticated route. A missing
CRON_SECRET refuses with 503 rather than running: an open sweep endpoint spends
web and model budget for whoever finds it.

Runs are bounded to four nodes and rotate by least-recently-swept, so a run fits
the wrapper's 300s and coverage still moves across all 33 bottlenecks. Each run
records what it did, so "how fresh is this" is answerable from evidence instead
of inferred from the newest event — and a node the search backend could not
reach is recorded as blindness, not as a quiet week.

What decides whether something IS an event moved to `lib/sweep.ts`, shared by
the script and the timer. Two copies of that judgement would have drifted, and
the hand-run and scheduled sweeps would have quietly stopped agreeing.


Claude-Session: https://claude.ai/code/session_01Y6ecRd91ZsPPcjuNkKTFV2

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 07:37:59 -07:00

50 lines
1.9 KiB
PL/PgSQL

BEGIN;
-- Candidates the scheduled sweep finds. NOT the corpus.
--
-- The corpus stays file-SSOT in git, accepted by a person in a commit, because
-- that is the whole claim of the site. A timer writing straight into it would
-- publish unread rows, and a release directory is replaced on every deploy
-- anyway. So the sweep lands here: a queue of leads a reviewer can promote.
CREATE TABLE IF NOT EXISTS research_sweep_candidates (
id text PRIMARY KEY,
bottleneck text NOT NULL,
term text NOT NULL,
url text NOT NULL,
title text NOT NULL,
published text,
excerpt text NOT NULL,
effect_guess text NOT NULL,
found_at timestamptz NOT NULL DEFAULT now(),
-- Set when a person has decided. Nothing here reaches a page until then.
reviewed_at timestamptz,
reviewed_by text,
verdict text CHECK (verdict IN ('accepted', 'rejected'))
);
CREATE INDEX IF NOT EXISTS research_sweep_candidates_open
ON research_sweep_candidates (found_at DESC) WHERE reviewed_at IS NULL;
-- When each node was last looked at, so a bounded run can rotate rather than
-- sweeping the same few nodes forever.
CREATE TABLE IF NOT EXISTS research_sweep_state (
node text PRIMARY KEY,
last_swept timestamptz NOT NULL DEFAULT now(),
last_status text NOT NULL
);
-- What the timer actually did, so "how fresh is this" has an answer that is not
-- a guess. A sweep that silently stops looks exactly like a quiet week.
CREATE TABLE IF NOT EXISTS research_sweep_runs (
id bigserial PRIMARY KEY,
started_at timestamptz NOT NULL DEFAULT now(),
finished_at timestamptz,
nodes_swept integer NOT NULL DEFAULT 0,
candidates_found integer NOT NULL DEFAULT 0,
could_not_look integer NOT NULL DEFAULT 0
);
COMMENT ON TABLE research_sweep_candidates IS 'Leads from the scheduled sweep. The corpus is git; nothing here is published until a person accepts it.';
COMMENT ON TABLE research_sweep_runs IS 'Evidence that the sweep ran, so staleness is measurable rather than inferred.';
COMMIT;