Docs/Skills and code/src/

src/stages/29-discovery-v2-score.js

Code30 lines3.6 KBsrc/

import { filterQualifiedRows } from "../analysis/qualification-scope.js";
import { getDb } from '../db.js';
import { ensureDiscoveryV2Schema } from '../discovery/v2-schema.js';
import { scoreDiscoveryEvidence, parseArgs } from '../discovery/v2-utils.js';

const args=parseArgs(process.argv.slice(2)); const db=ensureDiscoveryV2Schema();
const phases = db.prepare('SELECT phase_id FROM discovery_v2_phases ORDER BY started_at DESC').all();
let phaseId = args.phase ? String(args.phase) : String(phases[0]?.phase_id || '');
if (args.phase && !phases.some(p => p.phase_id === phaseId) && phases.length === 1) {
  console.warn(`[discovery-v2-score] requested phase "${phaseId}" does not exist; this database has one phase, so using "${phases[0].phase_id}".`);
  phaseId = phases[0].phase_id;
}
if (!phaseId) throw new Error('No discovery phase found. Run discovery:v2 first.');
if (!phases.some(p => p.phase_id === phaseId)) {
  throw new Error(`Discovery phase "${phaseId}" not found. Available phase IDs: ${phases.slice(0, 10).map(p => p.phase_id).join(', ') || '(none)'}. Omit --phase to use the latest phase.`);
}
const rows=db.prepare(`SELECT c.channel_url,c.name,c.subscriber_count,c.video_count,COUNT(DISTINCT v.video_id) found_videos,MAX(COALESCE(v.view_count,0)) max_video_views,COALESCE((SELECT qlr.classification FROM channel_qualification_llm_results qlr WHERE qlr.channel_url=c.channel_url AND qlr.processing_status='succeeded' ORDER BY qlr.created_at DESC LIMIT 1),MAX(cq.classification),'unknown') classification FROM discovery_v2_channel_events e JOIN channels c ON c.channel_url=e.channel_url LEFT JOIN videos v ON v.channel_url=c.channel_url LEFT JOIN channel_qualifications cq ON cq.channel_url=c.channel_url WHERE e.phase_id=? AND e.was_new_channel=1 GROUP BY c.channel_url ORDER BY c.name`).all(phaseId);
rows.splice(0, rows.length, ...filterQualifiedRows(db, rows, "channel", { label: "29-discovery-v2-score.js" }));
const save=db.prepare(`INSERT INTO discovery_v2_scores(phase_id,channel_url,channel_name,discovery_score,evidence_json,scored_at) VALUES(?,?,?,?,?,?) ON CONFLICT(phase_id,channel_url) DO UPDATE SET channel_name=excluded.channel_name,discovery_score=excluded.discovery_score,evidence_json=excluded.evidence_json,scored_at=excluded.scored_at`);
const ranked=rows.map(row=>({...row,...scoreDiscoveryEvidence(row)})).sort((a,b)=>b.score-a.score);
const tx=db.transaction(()=>{for(const row of ranked)save.run(phaseId,row.channel_url,row.name,row.score,JSON.stringify({found_videos:row.found_videos,max_video_views:row.max_video_views,subscriber_count:row.subscriber_count,video_count:row.video_count,classification:row.classification,components:row.components,viewsPerSubscriber:row.viewsPerSubscriber,subscribersPerCatalogueVideo:row.subscribersPerCatalogueVideo,confidence:row.confidence}),new Date().toISOString())});tx();
if (ranked.length === 0) {
  console.error(`[discovery-v2-score] ERROR: phase=${phaseId} matched zero newly-added channels. Check discovery_v2_channel_events.was_new_channel and the active database path.`);
  process.exitCode = 2;
}
console.log(`[discovery-v2-score] phase=${phaseId} newly-added=${ranked.length} persisted=${ranked.length}`);
console.log('This is a discovery triage score, not one of the five queue scores. Missing data remains visible and confidence is lowered.');
for(const r of ranked.slice(0,50))console.log(`${r.score.toFixed(2).padStart(6)} | ${r.confidence.padEnd(9)} | ${String(r.classification||'unknown').padEnd(9)} | ${String(r.name||'').slice(0,42).padEnd(42)} | subs=${r.subscriber_count??'?'} videos=${r.video_count??'?'} observed=${r.found_videos} maxViews=${r.max_video_views??0} | ${r.channel_url}`);

Shown as it is in the repository at build time (2026-11-03); the home directory appears as <home>. If this page and the file differ, the file is right. Back to the catalogue.