diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts
index f48bd94..a02428a 100644
--- a/server/src/db/schema.ts
+++ b/server/src/db/schema.ts
@@ -203,6 +203,14 @@ const MIGRATIONS: string[] = [
PRIMARY KEY (day, key)
);
`,
+ /* 2 — long-lived ledger of every article URL ever seen, so an item that is still in a feed
+ after its article was pruned (e.g. rejected ones after 14 days) is never processed again */ `
+ CREATE TABLE seen (
+ url_hash TEXT PRIMARY KEY,
+ first_seen TEXT NOT NULL
+ );
+ INSERT OR IGNORE INTO seen (url_hash, first_seen) SELECT url_hash, fetched_at FROM article;
+ `,
];
export function migrate(db: Database): number {
diff --git a/server/src/http/activity.ts b/server/src/http/activity.ts
index f8681ba..9013573 100644
--- a/server/src/http/activity.ts
+++ b/server/src/http/activity.ts
@@ -19,6 +19,16 @@ export function buildActivity(ctx: Ctx): ActivityDTO {
const calls = db.prepare(`SELECT ok, ms, tokens_in, tokens_out FROM llm_call WHERE at >= ?`).all<{ ok: number; ms: number; tokens_in: number | null; tokens_out: number | null }>(since24h);
const okMs = calls.filter((c) => c.ok).map((c) => c.ms).sort((a, b) => a - b);
const breaker = ctx.llm.breakerInfo();
+ const byPurpose = db
+ .prepare(
+ `SELECT purpose, COUNT(*) AS calls, SUM(CASE WHEN ok = 0 THEN 1 ELSE 0 END) AS errors,
+ COALESCE(SUM(tokens_in), 0) AS tokens_in, COALESCE(SUM(tokens_out), 0) AS tokens_out
+ FROM llm_call WHERE at >= ? GROUP BY purpose ORDER BY calls DESC`,
+ )
+ .all<{ purpose: string; calls: number; errors: number; tokens_in: number; tokens_out: number }>(since24h)
+ .map((r) => ({ purpose: r.purpose, calls: r.calls, errors: r.errors, tokensIn: r.tokens_in, tokensOut: r.tokens_out }));
+ const pipelineCalls = byPurpose.filter((p) => !['test', 'playground', 'dry-run'].includes(p.purpose)).reduce((s, p) => s + p.calls, 0);
+ const published24h = count(`SELECT COUNT(*) AS n FROM story WHERE first_published_at >= ?`, since24h);
const failures: ActivityFailure[] = [
...db
@@ -87,6 +97,15 @@ export function buildActivity(ctx: Ctx): ActivityDTO {
p95LatencyMs: okMs.length ? okMs[Math.min(okMs.length - 1, Math.floor(okMs.length * 0.95))]! : null,
tokensIn24h: calls.reduce((s, c) => s + (c.tokens_in ?? 0), 0),
tokensOut24h: calls.reduce((s, c) => s + (c.tokens_out ?? 0), 0),
+ error: ctx.llm.errorMessage(),
+ byPurpose,
+ callsPerStory: published24h ? Math.round((pipelineCalls / published24h) * 10) / 10 : null,
+ },
+ savings7d: {
+ duplicatesSkipped: statSum('duplicates_skipped'),
+ groupedByHeadline: statSum('grouped_by_headline'),
+ matchCallsSkipped: statSum('match_calls_skipped'),
+ triageBatched: statSum('triage_batched'),
},
failures: failures.slice(0, 60),
importance7d,
diff --git a/server/src/http/api.ts b/server/src/http/api.ts
index 249802e..2d8635b 100644
--- a/server/src/http/api.ts
+++ b/server/src/http/api.ts
@@ -319,6 +319,8 @@ async function playgroundRun(ctx: Ctx, url: string, models: string[]): Promise
) => {
res.json({
llm: ctx.llm.state(),
+ llmError: ctx.llm.errorMessage(),
waiting: waitingCount(ctx),
version: ctx.config.version,
latestDigestId: latestPublishedDigest(ctx)?.id ?? null,
diff --git a/server/src/llm/client.ts b/server/src/llm/client.ts
index ad27ac8..80f9e79 100644
--- a/server/src/llm/client.ts
+++ b/server/src/llm/client.ts
@@ -113,6 +113,8 @@ export class LlmClient {
private failures = 0;
private nextProbe = 0;
private probing = false;
+ /** Set when the server rejects requests as misconfigured (401/403/404); pauses LLM work. */
+ private configError: { message: string; until: number } | null = null;
private timer: NodeJS.Timeout | null = null;
private readonly listeners: (() => void)[] = [];
@@ -140,12 +142,28 @@ export class LlmClient {
this.sem.limit = this.getSettings().concurrency;
this.modeCache.clear();
// New connection settings deserve a fresh chance.
+ const hadConfigError = this.configError !== null;
+ this.configError = null;
if (this.failures > 0) this.reset();
+ else if (hadConfigError) for (const l of this.listeners) l();
+ }
+
+ /** Why LLM work is paused because the server rejected requests, if it is. */
+ errorMessage(): string | null {
+ if (!this.configError) return null;
+ if (Date.now() >= this.configError.until) {
+ // Try again after a while (the server may have been fixed without a settings change).
+ this.configError = null;
+ for (const l of this.listeners) l();
+ return null;
+ }
+ return this.configError.message;
}
state(): LlmState {
const s = this.getSettings();
if (!s.baseUrl || !s.summaryModel) return 'unconfigured';
+ if (this.errorMessage()) return 'offline';
return this.failures >= BREAKER_THRESHOLD ? 'offline' : 'ok';
}
@@ -252,6 +270,7 @@ export class LlmClient {
const s = this.conn(opts.override);
const affectBreaker = !opts.override;
if (affectBreaker && this.failures >= BREAKER_THRESHOLD) throw new LlmUnavailableError('LLM is offline');
+ if (affectBreaker && this.errorMessage()) throw new LlmUnavailableError(this.errorMessage()!);
if (!opts.model) throw new LlmUnavailableError('No model configured');
const release = await this.sem.acquire();
const started = Date.now();
@@ -273,6 +292,7 @@ export class LlmClient {
max_tokens: opts.maxTokens ?? 2048,
stream: false,
};
+ if (s.reasoningEffort) body.reasoning_effort = s.reasoningEffort;
if (mode === 'json_schema') {
body.response_format = { type: 'json_schema', json_schema: { name: opts.schemaName, strict: true, schema: opts.schema } };
} else if (mode === 'json_object') {
@@ -282,9 +302,11 @@ export class LlmClient {
try {
data = await this.request('/chat/completions', body, s, s.timeoutSec * 1000);
} catch (err) {
- if (err instanceof LlmRequestError && mode !== 'prompt') {
+ // Only a rejected request *shape* (400/422) is worth retrying in a simpler output mode;
+ // a bad key, unknown model or wrong URL would just fail three times.
+ if (err instanceof LlmRequestError && (err.status === 400 || err.status === 422) && mode !== 'prompt') {
firstError ??= err;
- continue; // try a simpler structured-output mode
+ continue;
}
throw err;
}
@@ -298,6 +320,14 @@ export class LlmClient {
const e = err as Error;
this.record(opts.purpose, opts.model, false, Date.now() - started, null, null, e.message);
if (affectBreaker && err instanceof LlmUnavailableError) this.recordFailure(e);
+ if (affectBreaker && err instanceof LlmRequestError && [401, 403, 404].includes(err.status)) {
+ // Misconfiguration (bad key, unknown model, wrong URL): every request would fail the same
+ // way, so pause all LLM work instead of burning attempts on each article.
+ const message = `The LLM server rejected requests for "${opts.model}" (HTTP ${err.status}). Check Settings → LLM connection. ${err.message.slice(0, 200)}`;
+ if (!this.configError) log.warn('pausing LLM work: the server rejected the request', { model: opts.model, status: err.status });
+ this.configError = { message, until: Date.now() + 10 * 60_000 };
+ throw new LlmUnavailableError(message);
+ }
throw err;
} finally {
release();
@@ -341,6 +371,7 @@ export class LlmClient {
// One repair attempt with the model's own answer in context.
const repairBody: Record = {
+ ...(s.reasoningEffort ? { reasoning_effort: s.reasoningEffort } : {}),
model: opts.model,
messages: [
...messages,
diff --git a/server/src/llm/prompts.ts b/server/src/llm/prompts.ts
index fc5a903..673f8e4 100644
--- a/server/src/llm/prompts.ts
+++ b/server/src/llm/prompts.ts
@@ -203,6 +203,66 @@ export function validateTriage(v: unknown): TriageOutput | string {
};
}
+// ───────────────────────────── triage, batched ─────────────────────────────
+
+export const TRIAGE_BATCH_SCHEMA: JsonSchema = {
+ type: 'object',
+ properties: {
+ results: {
+ type: 'array',
+ items: {
+ type: 'object',
+ properties: {
+ n: { type: 'integer' },
+ decision: { type: 'string', enum: ['keep', 'reject'] },
+ rule_id: { type: ['integer', 'null'] },
+ reason: { type: 'string' },
+ confidence: { type: 'number' },
+ },
+ required: ['n', 'decision', 'rule_id', 'reason', 'confidence'],
+ additionalProperties: false,
+ },
+ },
+ },
+ required: ['results'],
+ additionalProperties: false,
+};
+
+/** Several articles from one source in one call: the rules and instructions are sent once. */
+export function buildTriageBatchPrompt(
+ instructions: string,
+ negatives: TriageRuleRef[],
+ exceptions: TriageRuleRef[],
+ examples: TriageExample[],
+ articles: (ArticleForPrompt & { n: number })[],
+): { system: string; user: string } {
+ const single = buildTriagePrompt(instructions, negatives, exceptions, examples, articles[0]!);
+ const system = single.system.replace(
+ /\n\nOutput format:[\s\S]*$/,
+ `\n\nYou get several numbered articles. Judge each one on its own.
+
+Output format: reply with ONLY a JSON object:
+{"results": [{"n": , "decision": "keep" or "reject", "rule_id": , "reason": "", "confidence": }]}
+Include every article number exactly once.`,
+ );
+ const user = articles.map((a) => articleBlock(a, `article_${a.n}`)).join('\n\n');
+ return { system, user };
+}
+
+export function validateTriageBatch(v: unknown): (TriageOutput & { n: number })[] | string {
+ if (!v || typeof v !== 'object') return 'expected a JSON object';
+ const results = (v as Record).results;
+ if (!Array.isArray(results)) return 'results must be an array';
+ const out: (TriageOutput & { n: number })[] = [];
+ for (const r of results) {
+ if (!r || typeof r !== 'object') continue;
+ const n = Number((r as Record).n);
+ const t = validateTriage(r);
+ if (Number.isInteger(n) && typeof t !== 'string') out.push({ ...t, n });
+ }
+ return out;
+}
+
// ───────────────────────────── dry run (batched) ─────────────────────────────
export const DRY_RUN_SCHEMA: JsonSchema = {
diff --git a/server/src/pipeline/cluster.ts b/server/src/pipeline/cluster.ts
index 7b27348..2d46513 100644
--- a/server/src/pipeline/cluster.ts
+++ b/server/src/pipeline/cluster.ts
@@ -1,4 +1,5 @@
import type { Ctx } from '../context.ts';
+import { bumpStat } from '../context.ts';
import { HOUR, isoMinus } from '../lib/time.ts';
import { truncate } from '../lib/text.ts';
import { log } from '../lib/log.ts';
@@ -157,7 +158,47 @@ function sourceName(ctx: Ctx, id: number): string {
return ctx.db.prepare(`SELECT name FROM source WHERE id = ?`).get<{ name: string }>(id)?.name ?? 'Unknown';
}
-const MAX_LLM_CANDIDATES = 60;
+/** Recent stories scanned for a possible match, and the most sent to the model. */
+const SCAN_STORIES = 200;
+const MAX_LLM_CANDIDATES = 12;
+
+/** Capitalised words that start sentences or headlines in Croatian and English and say nothing about the event. */
+const COMMON = new Set(
+ (
+ 'the a an this that these those there here in on at for from with after before when how why what who new his her its their our your ' +
+ 'we you they it he she said says says breaking live update watch video photo photos exclusive report reports ' +
+ 'u na za iz od do po kako sto zasto gdje tko koji koja koje novi nova novo ovo ova ovaj taj ta to nakon prema ' +
+ 'danas jucer sutra foto video uzivo pogledajte evo ovo ovako sve svi vijesti hrvatska hrvatski hrvatske ' +
+ // Dates are shared by unrelated stories of the same day.
+ 'monday tuesday wednesday thursday friday saturday sunday january february march april may june july august ' +
+ 'september october november december ponedjeljak utorak srijeda srijedu cetvrtak petak subota subotu nedjelja nedjelju ' +
+ 'mr mrs ms dr'
+ ).split(/\s+/),
+);
+
+function fold(s: string): string {
+ return s.toLowerCase().normalize('NFKD').replace(/\p{M}+/gu, '').replace(/đ/g, 'd');
+}
+
+/**
+ * Cheap "could these be the same event?" signal that works across Croatian and English: names
+ * (capitalised words, cut to 4 letters so "Trump"/"Trumpov" and "Bahrain"/"Bahrein" agree) and
+ * numbers. A story that shares none of them with the new article is not worth asking the model about.
+ */
+export function eventStems(text: string): Set {
+ const out = new Set();
+ for (const m of text.matchAll(/(? {
@@ -170,13 +211,38 @@ async function clusterByLlm(ctx: Ctx, a: ArticleRow): Promise {
WHERE s.first_reported_at >= ? AND s.hidden = 0 AND s.state != 'failed'
ORDER BY s.last_reported_at DESC LIMIT ?`,
)
- .all<{ id: number; title: string | null; lead_title: string | null }>(since, MAX_LLM_CANDIDATES);
- if (!stories.length) {
+ .all<{ id: number; title: string | null; lead_title: string | null }>(since, SCAN_STORIES);
+
+ // Free pre-filter: only stories that share a name or number with the new article are candidates.
+ const mine = eventStems(`${a.title}\n${articleText(a).slice(0, 600)}`);
+ const memberTitles = new Map();
+ if (stories.length) {
+ const ids = stories.map((x) => x.id);
+ for (const r of ctx.db
+ .prepare(`SELECT story_id, title FROM article WHERE story_id IN (${ids.map(() => '?').join(',')})`)
+ .all<{ story_id: number; title: string }>(...ids)) {
+ const list = memberTitles.get(r.story_id) ?? [];
+ list.push(r.title);
+ memberTitles.set(r.story_id, list);
+ }
+ }
+ const scored = stories
+ .map((x) => {
+ const theirs = eventStems([x.title ?? '', ...(memberTitles.get(x.id) ?? [])].join('\n'));
+ let shared = 0;
+ for (const st of mine) if (theirs.has(st)) shared++;
+ return { story: x, shared };
+ })
+ .filter((x) => x.shared > 0)
+ .sort((p, q) => q.shared - p.shared)
+ .slice(0, MAX_LLM_CANDIDATES);
+ if (!scored.length) {
+ if (stories.length) bumpStat(ctx.db, 'match_calls_skipped');
createStoryForArticle(ctx, a);
ctx.wake();
return;
}
- const candidates = stories.map((x) => ({ id: x.id, title: x.title ?? x.lead_title ?? '', originalTitle: x.lead_title }));
+ const candidates = scored.map(({ story: x }) => ({ id: x.id, title: x.title ?? x.lead_title ?? '', originalTitle: x.lead_title }));
const prompt = buildStoryMatchPrompt(
promptText(s, 'sameEvent'),
{ sourceName: sourceName(ctx, a.source_id), title: a.title, publishedAt: a.published_at, categories: parseJsonArray(a.categories), text: truncate(articleText(a), 800) },
diff --git a/server/src/pipeline/fetcher.ts b/server/src/pipeline/fetcher.ts
index 7473e90..68a62b1 100644
--- a/server/src/pipeline/fetcher.ts
+++ b/server/src/pipeline/fetcher.ts
@@ -7,6 +7,7 @@ import { normalizeLang, normalizeTitle, sha1 } from '../lib/text.ts';
import { HOUR, MINUTE, isoMinus, isoPlus, nowIso } from '../lib/time.ts';
import { log, errorMessage } from '../lib/log.ts';
import { setMeta } from '../db/schema.ts';
+import { attachArticle } from './stories.ts';
export interface SourceRow {
id: number;
@@ -80,6 +81,19 @@ export async function fetchSource(ctx: Ctx, sourceId: number): Promise<{ newItem
}
}
+interface Twin {
+ id: number;
+ source_id: number;
+ state: string;
+ story_id: number | null;
+}
+
+/**
+ * Stores new feed items. Everything here is free (no AI): an item is dropped when its URL was
+ * ever seen before, when its source already delivered the same GUID or headline, and an
+ * identical headline from another outlet (wire copy) joins the existing story, or is filtered
+ * the same way, without triage, grouping or a summary.
+ */
function storeItems(ctx: Ctx, src: SourceRow, items: ReturnType['items'], feedLang: string | null): number {
const s = ctx.settings.get().pipeline;
const now = new Date();
@@ -87,31 +101,60 @@ function storeItems(ctx: Ctx, src: SourceRow, items: ReturnType= ?`);
+ const byTitle = ctx.db.prepare(
+ `SELECT id, source_id, state, story_id FROM article WHERE title_hash = ? AND fetched_at >= ? ORDER BY id LIMIT 1`,
+ );
const insert = ctx.db.prepare(
`INSERT INTO article (source_id, guid, url, url_hash, title_hash, title, excerpt, categories, text, partial, author,
image_url, language, published_at, fetched_at, state, state_changed_at)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
);
+ const copyVerdict = ctx.db.prepare(
+ `INSERT OR REPLACE INTO verdict (article_id, decision, decided_by, rule_id, rule_text, reason, confidence, model, prompt_version, overridden, decided_at)
+ SELECT ?, decision, decided_by, rule_id, rule_text, 'Same headline as an article already filtered. ' || reason, confidence, model, prompt_version, 0, ?
+ FROM verdict WHERE article_id = ?`,
+ );
let fresh = 0;
+ let duplicates = 0;
+ const attachLater: { storyId: number; article: { id: number; published_at: string | null; fetched_at: string } }[] = [];
+
ctx.db.transaction(() => {
for (const item of items) {
const url = item.link ? canonicalizeUrl(item.link) : null;
if (!url) continue;
const urlHash = sha1(url);
- if (byUrl.get(urlHash)) continue;
- if (item.guid && byGuid.get(src.id, item.guid)) continue;
+ if (seen.get(urlHash) || byUrl.get(urlHash)) continue;
+ markSeen.run(urlHash, fetchedAt);
+ if (item.guid && byGuid.get(src.id, item.guid)) {
+ duplicates++;
+ continue;
+ }
const normTitle = normalizeTitle(item.title);
const titleHash = sha1(normTitle);
- if (normTitle.length > 12 && byTitle.get(titleHash, titleWindow)) continue;
// Feeds sometimes publish future dates; never trust a date later than now.
const published = item.publishedAt && item.publishedAt.getTime() <= now.getTime() ? item.publishedAt.toISOString() : fetchedAt;
const tooOld = published < maxAgeCutoff || (!src.initial_import_done && published < firstAddCutoff);
const fullText = src.full_text === 1;
- const state = tooOld ? 'skipped' : fullText ? 'new' : 'extracted';
- insert.run(
+
+ let state = tooOld ? 'skipped' : fullText ? 'new' : 'extracted';
+ let twin: Twin | undefined;
+ if (!tooOld && normTitle.length > 12) {
+ twin = byTitle.get(titleHash, titleWindow);
+ if (twin) {
+ if (twin.source_id === src.id || (twin.state !== 'grouped' && twin.state !== 'rejected') || (twin.state === 'grouped' && !twin.story_id)) {
+ // Same outlet under a new URL, or the twin is still being processed: nothing to add.
+ duplicates++;
+ continue;
+ }
+ // Identical headline from another outlet: reuse the twin's outcome instead of the AI.
+ state = twin.state === 'rejected' ? 'rejected' : 'kept';
+ }
+ }
+ const id = insert.run(
src.id,
item.guid,
url,
@@ -120,8 +163,8 @@ function storeItems(ctx: Ctx, src: SourceRow, items: ReturnType): void {
+/**
+ * Adds an article to an existing story and triggers regeneration / feed re-evaluation as needed.
+ * `rewrite: false` for wire copy: the same text adds a source but nothing worth a new summary.
+ */
+export function attachArticle(
+ ctx: Ctx,
+ storyId: number,
+ a: Pick,
+ opts: { rewrite?: boolean } = {},
+): void {
const now = nowIso();
ctx.db.transaction(() => {
ctx.db.prepare(`UPDATE article SET story_id = ?, state = 'grouped', state_changed_at = ?, attempts = 0, next_attempt_at = NULL, last_error = NULL WHERE id = ?`).run(storyId, now, a.id);
@@ -268,7 +276,7 @@ export function attachArticle(ctx: Ctx, storyId: number, a: Pick= c.regenAtSources;
const written = s.version_written_at ? Date.parse(s.version_written_at) : Date.now();
const gap = c.regenGapHours > 0 && Date.parse(reportedAt(a)) - written > c.regenGapHours * HOUR;
diff --git a/server/src/pipeline/summarize.ts b/server/src/pipeline/summarize.ts
index 3282fb2..53d6055 100644
--- a/server/src/pipeline/summarize.ts
+++ b/server/src/pipeline/summarize.ts
@@ -124,7 +124,8 @@ export async function summarizeStory(ctx: Ctx, storyId: number): Promise {
const lead = all.find((a) => a.id === story.lead_article_id) ?? all[0]!;
const others = all.filter((a) => a.id !== lead.id).sort((x, y) => x.partial - y.partial);
const chosen = [lead, ...others].slice(0, 4);
- const perArticle = chosen.length > 1 ? Math.max(2000, Math.floor(settings.llm.maxArticleChars / chosen.length)) : settings.llm.maxArticleChars;
+ // A rewrite from several sources shares one article-sized budget instead of multiplying it.
+ const perArticle = chosen.length > 1 ? Math.max(1500, Math.floor(settings.llm.maxArticleChars / chosen.length)) : settings.llm.maxArticleChars;
const written = await writeTitleAndDescription(ctx.llm, settings, chosen.map((a) => articleForPrompt(ctx, a, perArticle)));
if (written.formatRetries) bumpStat(ctx.db, 'format_retries', written.formatRetries);
if (!written.languageOk) {
diff --git a/server/src/pipeline/triage.ts b/server/src/pipeline/triage.ts
index ff759b7..ef81e55 100644
--- a/server/src/pipeline/triage.ts
+++ b/server/src/pipeline/triage.ts
@@ -3,14 +3,19 @@ import type { DryRunHit, DryRunStatus, RuleDTO, RuleInput } from '../../../share
import type { Ctx } from '../context.ts';
import {
buildDryRunPrompt,
+ buildTriageBatchPrompt,
buildTriagePrompt,
DRY_RUN_SCHEMA,
promptText,
promptVersion,
+ TRIAGE_BATCH_SCHEMA,
TRIAGE_SCHEMA,
validateDryRun,
validateTriage,
+ validateTriageBatch,
} from '../llm/prompts.ts';
+import type { TriageOutput, TriageRuleRef } from '../llm/prompts.ts';
+import { bumpStat } from '../context.ts';
import { nowIso } from '../lib/time.ts';
import { truncate } from '../lib/text.ts';
import { errorMessage } from '../lib/log.ts';
@@ -120,13 +125,9 @@ function writeVerdict(ctx: Ctx, articleId: number, v: VerdictInput): void {
.run(articleId, v.decision, v.decidedBy, v.ruleId, v.ruleText, v.reason, v.confidence, v.model, v.promptVersion, nowIso());
}
-/** Whether triage for this article needs the LLM (any enabled AI negative rule applies). */
+/** Whether triage for this article needs the LLM (no keyword rule decides it and an AI negative applies). */
export function triageNeedsLlm(ctx: Ctx, a: Pick): boolean {
- const rules = activeRules(ctx, a.source_id);
- const hay = keywordHaystack(a);
- if (rules.some((r) => r.kind === 'keyword' && r.polarity === 'exception' && keywordMatcher(r.text)(hay))) return false;
- if (rules.some((r) => r.kind === 'keyword' && r.polarity === 'negative' && keywordMatcher(r.text)(hay))) return false;
- return rules.some((r) => r.kind === 'ai' && r.polarity === 'negative');
+ return freeVerdict(activeRules(ctx, a.source_id), a) === null;
}
function sourceName(ctx: Ctx, sourceId: number): string {
@@ -148,44 +149,85 @@ export interface TriageVerdict extends VerdictInput {
ms: number | null;
}
-/** Decides keep/reject without touching the database (used by triage and the playground). */
-export async function evaluateTriage(ctx: Ctx, input: TriageInput): Promise {
- const rules =
- input.sourceId === null
- ? listRules(ctx).filter((r) => r.enabled)
- : activeRules(ctx, input.sourceId);
- const hay = keywordHaystack(input);
- const base = { model: null, promptVersion: null, confidence: null, ms: null };
+/** Characters of article text the triage model sees: headline + lede is enough to judge the subject. */
+const TRIAGE_TEXT_CHARS = 800;
+/** Articles from one source judged in one call. */
+export const TRIAGE_BATCH_SIZE = 8;
+const NO_LLM = { model: null, promptVersion: null, confidence: null, ms: null };
+
+/** Keyword exceptions, keyword negatives, and "no AI rule applies" — all free. Null = needs the LLM. */
+function freeVerdict(rules: RuleDTO[], input: Pick): TriageVerdict | null {
+ const hay = keywordHaystack(input);
for (const r of rules.filter((x) => x.kind === 'keyword' && x.polarity === 'exception')) {
const hit = keywordMatcher(r.text)(hay);
- if (hit) return { ...base, decision: 'keep', decidedBy: 'keyword', ruleId: r.id, ruleText: r.text, reason: `Kept by exception: matched "${hit}"`, hitRuleId: r.id };
+ if (hit) return { ...NO_LLM, decision: 'keep', decidedBy: 'keyword', ruleId: r.id, ruleText: r.text, reason: `Kept by exception: matched "${hit}"`, hitRuleId: r.id };
}
for (const r of rules.filter((x) => x.kind === 'keyword' && x.polarity === 'negative')) {
const hit = keywordMatcher(r.text)(hay);
- if (hit) return { ...base, decision: 'reject', decidedBy: 'keyword', ruleId: r.id, ruleText: r.text, reason: `Matched keyword "${hit}"`, hitRuleId: r.id };
+ if (hit) return { ...NO_LLM, decision: 'reject', decidedBy: 'keyword', ruleId: r.id, ruleText: r.text, reason: `Matched keyword "${hit}"`, hitRuleId: r.id };
}
+ if (!rules.some((r) => r.kind === 'ai' && r.polarity === 'negative')) {
+ return { ...NO_LLM, decision: 'keep', decidedBy: 'none', ruleId: null, ruleText: null, reason: 'No rule applies', hitRuleId: null };
+ }
+ return null;
+}
+/** Turns the model's answer into a verdict: reject only with a valid rule at or above the threshold. */
+function interpretTriage(
+ out: TriageOutput,
+ rules: RuleDTO[],
+ threshold: number,
+ common: { model: string; promptVersion: string; ms: number | null },
+): TriageVerdict {
const negatives = rules.filter((r) => r.kind === 'ai' && r.polarity === 'negative');
const exceptions = rules.filter((r) => r.kind === 'ai' && r.polarity === 'exception');
- if (negatives.length === 0) {
- return { ...base, decision: 'keep', decidedBy: 'none', ruleId: null, ruleText: null, reason: 'No rule applies', hitRuleId: null };
+ const negRule = negatives.find((r) => r.id === out.ruleId);
+ const excRule = exceptions.find((r) => r.id === out.ruleId);
+ const base = { ...common, decidedBy: 'ai' as const, confidence: out.confidence };
+ if (out.decision === 'reject' && negRule && (out.confidence ?? 0) >= threshold) {
+ return { ...base, decision: 'reject', ruleId: negRule.id, ruleText: negRule.text, reason: out.reason, hitRuleId: negRule.id };
}
+ let reason = out.reason;
+ if (out.decision === 'reject') {
+ reason = negRule
+ ? `Kept: confidence ${Math.round((out.confidence ?? 0) * 100)}% is below the ${Math.round(threshold * 100)}% threshold. ${out.reason}`
+ : `Kept: the model did not name a valid rule. ${out.reason}`;
+ }
+ return {
+ ...base,
+ decision: 'keep',
+ ruleId: excRule?.id ?? negRule?.id ?? null,
+ ruleText: excRule?.text ?? negRule?.text ?? null,
+ reason,
+ hitRuleId: excRule?.id ?? null,
+ };
+}
+
+function ruleRefs(rules: RuleDTO[], polarity: 'negative' | 'exception'): TriageRuleRef[] {
+ return rules.filter((r) => r.kind === 'ai' && r.polarity === polarity).map((r) => ({ id: r.id, text: r.text }));
+}
+
+/** Decides keep/reject without touching the database (used by triage and the playground). */
+export async function evaluateTriage(ctx: Ctx, input: TriageInput): Promise {
+ const rules = input.sourceId === null ? listRules(ctx).filter((r) => r.enabled) : activeRules(ctx, input.sourceId);
+ const free = freeVerdict(rules, input);
+ if (free) return free;
const settings = ctx.settings.get();
const model = settings.llm.triageModel || settings.llm.summaryModel;
const instructions = promptText(settings, 'triage');
const prompt = buildTriagePrompt(
instructions,
- negatives.map((r) => ({ id: r.id, text: r.text })),
- exceptions.map((r) => ({ id: r.id, text: r.text })),
+ ruleRefs(rules, 'negative'),
+ ruleRefs(rules, 'exception'),
filterExamples(ctx.db, settings.pipeline.examplesInPrompt),
{
sourceName: input.sourceName,
title: input.title,
publishedAt: input.publishedAt,
categories: parseJsonArray(input.categories),
- text: truncate(input.text, 1500),
+ text: truncate(input.text, TRIAGE_TEXT_CHARS),
},
);
const res = await ctx.llm.chatJson({
@@ -198,46 +240,96 @@ export async function evaluateTriage(ctx: Ctx, input: TriageInput): Promise r.id === out.ruleId);
- const excRule = exceptions.find((r) => r.id === out.ruleId);
- const threshold = settings.pipeline.rejectThreshold;
- const common = { decidedBy: 'ai' as const, confidence: out.confidence, model, promptVersion: pv, ms: res.ms };
- if (out.decision === 'reject' && negRule && (out.confidence ?? 0) >= threshold) {
- return { ...common, decision: 'reject', ruleId: negRule.id, ruleText: negRule.text, reason: out.reason, hitRuleId: negRule.id };
- }
- let reason = out.reason;
- if (out.decision === 'reject') {
- reason = negRule
- ? `Kept: confidence ${Math.round((out.confidence ?? 0) * 100)}% is below the ${Math.round(threshold * 100)}% threshold. ${out.reason}`
- : `Kept: the model did not name a valid rule. ${out.reason}`;
- }
- return {
- ...common,
- decision: 'keep',
- ruleId: excRule?.id ?? negRule?.id ?? null,
- ruleText: excRule?.text ?? negRule?.text ?? null,
- reason,
- hitRuleId: excRule?.id ?? null,
- };
+ return interpretTriage(res.value, rules, settings.pipeline.rejectThreshold, { model, promptVersion: promptVersion(instructions), ms: res.ms });
+}
+
+function inputOf(ctx: Ctx, a: ArticleRow): TriageInput {
+ return { sourceId: a.source_id, sourceName: sourceName(ctx, a.source_id), title: a.title, categories: a.categories, text: articleText(a), publishedAt: a.published_at };
+}
+
+function record(ctx: Ctx, articleId: number, v: TriageVerdict): void {
+ if (v.hitRuleId !== null) bumpRule(ctx, v.hitRuleId);
+ writeVerdict(ctx, articleId, v);
}
/** Decides keep/reject for one stored article and writes the verdict. */
export async function triageArticle(ctx: Ctx, a: ArticleRow): Promise<'keep' | 'reject'> {
- const v = await evaluateTriage(ctx, {
- sourceId: a.source_id,
- sourceName: sourceName(ctx, a.source_id),
- title: a.title,
- categories: a.categories,
- text: articleText(a),
- publishedAt: a.published_at,
- });
- if (v.hitRuleId !== null) bumpRule(ctx, v.hitRuleId);
- writeVerdict(ctx, a.id, v);
+ const v = await evaluateTriage(ctx, inputOf(ctx, a));
+ record(ctx, a.id, v);
return v.decision;
}
+/**
+ * Triage for several articles from one source. Free decisions (keyword rules, no AI rule) are
+ * made first; the rest share one LLM call, so the rules and instructions are sent once.
+ */
+export async function triageBatch(ctx: Ctx, articles: ArticleRow[]): Promise