返回 last30days-skill
discovery-topic-queue-design-conventions.md
根目录 / docs / solutions / architecture-patterns / discovery-topic-queue-design-conventions.md
1 ---
2 title: "Persistent discovery topic queue: five interlocking design conventions"
3 date: 2026-07-20
4 category: architecture-patterns
5 module: discovery-topic-queue
6 problem_type: architecture_pattern
7 component: database
8 severity: high
9 applies_when:
10 - "Adding default-on local persistence (SQLite, JSON state) hooked onto the end of an expensive pipeline run"
11 - "Building a fuzzy identity layer over LLM-named entities whose names drift across runs"
12 - "Reading a feature toggle in an engine where .env-file values only reach code through env.get_config's keys allowlist"
13 - "Recording per-item state in a loop where a later item could fuzzy-match a row written earlier in the same run"
14 - "Persisting user-set status (covered, dismissed, read) that must survive entity renames"
15 tags:
16 - "discovery-topic-queue"
17 - "fuzzy-matching"
18 - "sqlite-persistence"
19 - "env-allowlist-opt-out"
20 - "two-phase-write"
21 - "covered-status-inheritance"
22 - "guarded-write-hook"
23 - "scoped-db"
24 - "llm-naming-drift"
25 related_components:
26 - "skills/last30days/scripts/store.py"
27 - "skills/last30days/scripts/last30days.py"
28 - "skills/last30days/scripts/lib/env.py"
29 - "tests/test_store.py"
30 - "tests/test_discover_mode.py"
31 ---
32
33
34 # Persistent discovery topic queue: five interlocking design conventions
35
36 ## Context
37
38 PR #852 shipped a persistent topic queue for `/last30days discover`: every real
39 discovery run records which topics it surfaced into a `discovery_topics` table in
40 research.db, so the podcast/X-article pipeline remembers what it has already seen
41 ("surfaced 3rd time") and what the user already produced content for ("marked
42 covered"). This is the design record for that queue - five conventions that were
43 each load-bearing in review, two of them caught as real bugs (one P0). The
44 seed-source corroboration change that landed in the same PR is documented
45 separately in
46 `docs/solutions/design-patterns/ranked-output-confidence-floor-honest-empty-state.md`
47 (section 2b); this doc does not cover it.
48
49 ## Guidance
50
51 ### 1. Default-on, disabled only via the config allowlist - never bare os.environ
52
53 The queue records every real (non-mock) run by default; the literal value `off`
54 disables it. The knob is registered in `env.get_config`'s keys allowlist
55 (`skills/last30days/scripts/lib/env.py:482`):
56
57 ```python
58 # Discovery topic queue (podcast/X-article pipeline memory). Default
59 # ON; the literal value "off" disables queue writes and annotations.
60 ('LAST30DAYS_DISCOVERY_QUEUE', None),
61 ```
62
63 and read from the resolved config dict, never `os.environ`
64 (`_annotate_and_record_discovery_queue` in `skills/last30days/scripts/last30days.py`):
65
66 ```python
67 queue_setting = str(config.get("LAST30DAYS_DISCOVERY_QUEUE") or "").strip().lower()
68 if queue_setting == "off" or not report.topics:
69 return report
70 ```
71
72 WHY: `.env`-file users' values only reach the engine through the `get_config`
73 allowlist merge - a bare `os.environ` read silently ignores them, a documented
74 invisible-failure class in this repo. Scoped runs (`--save-dir`) write the scoped
75 research.db via `store.scoped_db(_scoped_store_db(args))`
76 (`_scoped_store_db` in `last30days.py`, `store.scoped_db`), never the global one; `--mock` runs
77 stay 100% side-effect-free (both discovery call sites guard the queue hook with `if not args.mock`).
78
79 ### 2. Annotate-only fuzzy matching - a match stamps context, it never merges rows
80
81 `store.match_discovery_topic` tries exact normalized-name match first, then the
82 best entity-overlap candidate - the better of full `entity_key` token overlap and
83 anchor-token overlap - at a conservative floor
84 (`DISCOVERY_QUEUE_OVERLAP_THRESHOLD` and `match_discovery_topic` in `skills/last30days/scripts/store.py`):
85
86 ```python
87 DISCOVERY_QUEUE_OVERLAP_THRESHOLD = 0.6
88 ...
89 if best is not None and best_overlap >= DISCOVERY_QUEUE_OVERLAP_THRESHOLD:
90 return dict(best)
91 ```
92
93 A fuzzy match only annotates the rendered card - the `Pipeline: surfaced Nth
94 time, marked covered` line (`render_discovery` in `skills/last30days/scripts/lib/render.py`) -
95 and never merges or rewrites queue rows (see the `record_discovery_surfacing` and `match_discovery_topic` docstrings in `store.py`).
96
97 WHY: with annotate-only semantics a false-positive match costs one noisy line on
98 one card; a false merge would silently collapse two distinct stories into one
99 row and hide one of them forever. The threshold is tunable precisely because
100 mislabeling is recoverable and data loss is not.
101
102 ### 3. Two-phase hook: match ALL topics before recording ANY
103
104 `_annotate_and_record_discovery_queue` computes priors for every topic first,
105 then records surfacings, inside one `store.scoped_db` block
106 (`_annotate_and_record_discovery_queue` in `skills/last30days/scripts/last30days.py`):
107
108 ```python
109 with store.scoped_db(_scoped_store_db(args)):
110 store.init_db()
111 # Phase 1: match EVERY topic before recording ANY. Interleaving
112 # match+record in one loop lets topic N fuzzy-match a same-anchor
113 # sibling row this very run recorded seconds earlier, falsely
114 # annotating a first-ever topic as "surfaced 2nd time".
115 priors = [
116 _pre_run_prior_state(prior, run_ref)
117 for prior in (
118 store.match_discovery_topic(topic.name) for topic in report.topics
119 )
120 ]
121 # Phase 2: record this run's surfacings. ...
122 for topic, prior in zip(report.topics, priors):
123 ```
124
125 WHY: one report often contains same-anchor siblings ("Gemma 4 chat templates" /
126 "Gemma 4 tool calling fixes"). Interleaved match+record lets topic N fuzzy-match
127 the row topic N-1 wrote seconds earlier, falsely annotating a first-ever topic
128 as a repeat. Caught in review; regression-tested.
129
130 `_pre_run_prior_state` wraps each match so a finalize retry sees the state from
131 BEFORE its own earlier attempt: a row stamped with this run's `run_ref` has its
132 own surfacing subtracted (covered state kept), so the retry renders exactly like
133 the first attempt instead of annotating "surfaced 2nd time".
134
135 ### 4. Covered inheritance: fresh rows born covered, existing rows never mutated
136
137 `record_discovery_surfacing(inherit_covered_at=...)` makes a fresh row start in
138 `covered` status when its fuzzy-matched prior is covered; the `ON CONFLICT`
139 update path deliberately never touches `status`/`covered_at`
140 (`record_discovery_surfacing` in `skills/last30days/scripts/store.py`):
141
142 ```python
143 status = "covered" if inherit_covered_at else "surfaced"
144 ...
145 ON CONFLICT(normalized_name) DO UPDATE SET
146 surface_count = surface_count + 1,
147 last_surfaced = excluded.last_surfaced,
148 last_run_ref = excluded.last_run_ref,
149 domain = CASE WHEN excluded.domain <> '' THEN excluded.domain ELSE domain END
150 ```
151
152 The caller passes it when a topic's prior is covered
153 (`_annotate_and_record_discovery_queue`). Locked by the flip-flop regression test
154 `test_covered_status_survives_judge_rename_across_runs` and by
155 `test_record_discovery_surfacing_inherit_never_alters_existing_row_status`
156 (both in `tests/test_store.py`; the second pins that ON CONFLICT ignores
157 `inherit_covered_at`).
158
159 WHY: the LLM judge renames the same story across runs; without inheritance a
160 rename forks a fresh uncovered row and the user's covered mark silently
161 evaporates. Without the never-mutate rule, a stale inherit could flip a row the
162 user just changed.
163
164 ### 5. Guarded, synchronous end-of-run write - never crash a finished pipeline
165
166 Both discovery paths (one-shot `_run_discover` and the protocol's finalize leg)
167 call the hook through `_record_discovery_queue_safely`, so a broken queue db
168 degrades to a stderr warning and an unannotated report
169 (`skills/last30days/scripts/last30days.py`):
170
171 ```python
172 # call sites: `if not args.mock: report = _record_discovery_queue_safely(...)`
173 def _record_discovery_queue_safely(report, args, config, run_ref=None):
174 try:
175 return _annotate_and_record_discovery_queue(
176 report, args, config, run_ref=run_ref,
177 )
178 except (sqlite3.Error, OSError) as exc:
179 # A broken queue db (locked, read-only dir, corrupt) must never
180 # destroy a finished multi-minute pipeline run: warn and render
181 # the report without queue annotations (fields keep defaults).
182 sys.stderr.write(
183 f"[last30days] Warning: discovery queue unavailable ({exc}); "
184 "continuing without queue annotations.\n"
185 )
186 return report
187 ```
188
189 WHY: unguarded, a locked/read-only/corrupt research.db raises AFTER the
190 multi-minute research pipeline finished and discards all of its output - the PR
191 #852 code review's P0, empirically reproduced. The write also runs synchronously
192 after the pipeline returns (see the `_annotate_and_record_discovery_queue` docstring): it touches
193 disk, so the abandon-on-timeout daemon-thread pattern is forbidden here (see
194 `docs/solutions/logic-errors/non-daemon-executor-threads-defeat-wall-clock-budget.md`).
195
196 ## Why This Matters
197
198 Ranked by blast radius when a convention is violated:
199
200 - Unguarded end-of-run write (5): the whole run's output is destroyed by a
201 bookkeeping failure, and only in degraded environments (locked db, read-only
202 dir), so it ships green and detonates on exactly the machines you cannot see.
203 This was the review's P0.
204 - Interleaved match+record (3): the queue's core promise ("first time you've
205 seen this") is wrong on day one - a first-ever topic gets annotated "surfaced
206 2nd time" by its same-run sibling, and no cross-run test catches it because
207 the corruption happens inside a single run.
208 - Bare os.environ read (1): `.env`-file users cannot turn the queue off; the
209 toggle works in the maintainer's shell and fails invisibly for everyone
210 configuring via file.
211 - Merging on fuzzy match (2): a 0.6-overlap false positive stops being one
212 noisy line and becomes a hidden story - unrecoverable data loss from a
213 heuristic.
214 - Mutating rows or skipping inheritance (4): user covered marks flip-flop with
215 judge naming drift, so the queue re-pitches stories the user already produced,
216 which is the exact failure the queue exists to prevent.
217
218 ## When to Apply
219
220 - Any default-on local persistence bolted onto the end of an expensive pipeline:
221 the write must be guarded (degrade to a warning) and synchronous if it touches
222 disk.
223 - Any fuzzy identity layer over LLM-named entities: keep matching annotate-only,
224 batch all matches before any writes in a run, and inherit user-set status onto
225 fresh rows instead of mutating existing ones.
226 - Any new engine toggle in this repo: register it in `env.get_config`'s keys
227 allowlist and read it from the config dict, never bare `os.environ`.
228
229 ## Examples
230
231 Covered flip-flop, the archetype 3-run scenario (mirrors
232 `test_covered_status_survives_judge_rename_across_runs` in `tests/test_store.py`):
233
234 1. Run 1 surfaces "Gemma 4 chat templates"; the user records an episode and
235 runs `queue cover "Gemma 4 chat templates"` (row status: covered).
236 2. Run 2's judge names the same story "Gemma 4 template fixes". Exact match
237 misses; fuzzy match (anchor overlap `gemma`/`4` at >= 0.6) finds the covered
238 prior, so the new row is recorded born covered and the card renders
239 `Pipeline: surfaced 2nd time, marked covered` instead of pitching it fresh.
240 3. Run 3 resurfaces "Gemma 4 template fixes"; it exact-matches its own covered
241 row (`covered_at` still the run-1 date). Without convention 4, run 2 would
242 have forked an uncovered row and run 3 would re-pitch a story the user
243 already covered.
244
245 Queue failure behavior: with research.db locked by another process, a discovery
246 run still prints the full rendered report; stderr shows
247 `[last30days] Warning: discovery queue unavailable (database is locked);
248 continuing without queue annotations.` and the cards simply lack Pipeline lines.
249
250 ## Related
251
252 - PR #852 - judged topic names, junk gate, angles, topic queue (this design).
253 - `docs/solutions/design-patterns/ranked-output-confidence-floor-honest-empty-state.md`
254 section 2b - the seed-source corroboration rule from the same PR (not covered
255 here).
256 - `docs/solutions/logic-errors/non-daemon-executor-threads-defeat-wall-clock-budget.md`
257 - why abandon-on-timeout daemon threads are forbidden for disk writers.
258
258 lines MARKDOWN