export const meta = { name: 'sr-screener-ta', description: 'sr-screener: blinded dual title/abstract screening (Reviewer A + B), third-reviewer adjudication of advance-vs-exclude conflicts, optional QC recheck; missing decisions stay pending', phases: [ { title: 'Screening', detail: 'Reviewer A and Reviewer B screen each batch independently' }, { title: 'Adjudication', detail: 'A third reviewer re-reads only the disputed records' }, { title: 'Recheck', detail: 'A senior QC reviewer takes a second look at selected exclusions' }, ], } // Generated by sr-screener scripts/build_workflow.py - edit templates/prompts.md or the config, not this block. const CONFIG = __SR_CONFIG__ // Job scope was checked and any override recorded by the builder. Regenerate to change jobs. if (args && ['screen', 'adj', 'recheck'].some((key) => key in args)) { throw new Error('Runtime job overrides are disabled; regenerate with build_workflow.py so the pilot gate is checked') } const JOBS = CONFIG.jobs const P = CONFIG.prompts const fill = (tpl, vars) => tpl.replace(/\{\{([A-Z_]+)\}\}/g, (m, k) => (k in vars ? String(vars[k]) : m)) const CODES = ['INC', 'UNC'].concat(CONFIG.codes) const DEC = { type: 'object', properties: { id: { type: 'string', pattern: '^R\\d{' + CONFIG.idWidth + '}$' }, d: { type: 'string', enum: ['include', 'unclear', 'exclude'] }, code: { type: 'string', enum: CODES }, why: { type: 'string', description: 'at most 15 words, English' }, }, required: ['id', 'd', 'code', 'why'], } const SCHEMA = { type: 'object', properties: { decisions: { type: 'array', items: DEC } }, required: ['decisions'] } // Static prefix (identical for every batch of a role) first, batch-specific task last: cache friendly. const STATIC = {} function staticPart(role) { if (STATIC[role]) return STATIC[role] const vars = { STAGE_NAME: 'title/abstract', CORE: CONFIG.core, CODES: CONFIG.codesText, PROTOCOL: CONFIG.protocol } let intro if (role === 'A' || role === 'B') intro = fill(P.reviewer_intro, Object.assign({ PERSONA: CONFIG.personas[role] }, vars)) else if (role === 'ADJ') intro = fill(P.adjudicator_intro, vars) else intro = fill(P.qc_intro, vars) STATIC[role] = [intro, fill(P.rules_ta, vars), fill(P.protocol_wrapper, vars)].join('\n\n') return STATIC[role] } const pad = (n) => 'R' + String(n).padStart(CONFIG.idWidth, '0') const idsFor = (x) => { if (Array.isArray(x.ids)) return x.ids const out = [] for (let i = x.from; i <= x.to; i++) out.push(pad(i)) return out } const pathFor = (b) => CONFIG.dir + '/' + b + '.txt' const advances = (d) => d === 'include' || d === 'unclear' function taskFull(b, ids) { const half = Math.ceil(CONFIG.readLimit / 2) return fill(P.task_batch_read, { FILE: pathFor(b), READ_LIMIT: CONFIG.readLimit, READ_HALF: half, READ_NEXT: half + 1, N: ids.length, FIRST: ids[0], LAST: ids[ids.length - 1], }) } function taskSubset(b, ids) { return fill(P.task_grep_subset, { FILE: pathFor(b), ID_ALTERNATION: ids.join('|'), AFTER: CONFIG.grepAfter, N: ids.length, ID_LIST: ids.join(', '), }) } function opts(role, label, phase) { const o = { label: label + '@' + CONFIG.context.context_id, phase, schema: SCHEMA } if (CONFIG.models[role]) o.model = CONFIG.models[role] if (CONFIG.agentType) o.agentType = CONFIG.agentType return o } // Keeps only well-formed decisions for expected, not-yet-decided IDs. Label and code must agree; // anything else is dropped and the record is retried or left pending - never filled in by default. function taker(got, allowed) { const ok = new Set(allowed) return (res) => { const list = res && Array.isArray(res.decisions) ? res.decisions : [] for (const r of list) { if (!r || !ok.has(r.id) || got[r.id]) continue const consistent = (r.d === 'include' && r.code === 'INC') || (r.d === 'unclear' && r.code === 'UNC') || (r.d === 'exclude' && CONFIG.codes.includes(r.code)) if (consistent) got[r.id] = r } } } // mode: 'all' -> read the whole batch; array of IDs -> Grep only those records async function screenRole(role, b, allIds, mode) { const todo = mode === 'all' ? allIds : mode const got = {} const take = taker(got, todo) const sp = staticPart(role) if (mode === 'all') take(await agent(sp + '\n\n' + taskFull(b, allIds), opts(role, role + ':' + b, 'Screening'))) else take(await agent(sp + '\n\n' + taskSubset(b, todo), opts(role, role + ':' + b + ':part', 'Screening'))) let missing = todo.filter((id) => !got[id]) if (missing.length) { take(await agent(sp + '\n\n' + taskSubset(b, missing), opts(role, role + ':' + b + ':retry', 'Screening'))) missing = todo.filter((id) => !got[id]) } return { got, missing } } function disputeTask(b, items) { const lines = items.map((x) => '- ' + x.id + ': Reviewer A = ' + (x.A || 'no decision') + '; Reviewer B = ' + (x.B || 'no decision')).join('\n') return 'Records in dispute (' + items.length + '):\n' + lines + '\n\n' + fill(P.adjudicator_tiebreak, { CORE: CONFIG.core }) + '\n\n' + taskSubset(b, items.map((x) => x.id)) } async function adjudicate(b, items) { const got = {} const take = taker(got, items.map((x) => x.id)) const sp = staticPart('ADJ') take(await agent(sp + '\n\n' + disputeTask(b, items), opts('ADJ', 'ADJ:' + b, 'Adjudication'))) let left = items.filter((x) => !got[x.id]) if (left.length) { take(await agent(sp + '\n\n' + disputeTask(b, left), opts('ADJ', 'ADJ:' + b + ':retry', 'Adjudication'))) left = items.filter((x) => !got[x.id]) } return { got, left: left.length } } // full: the batch file holds exactly these records (QC batches, audits), so one Read is enough async function recheck(b, ids, full) { const got = {} const take = taker(got, ids) const sp = staticPart('QC') take(await agent(sp + '\n\n' + (full ? taskFull(b, ids) : taskSubset(b, ids)), opts('QC', 'QC:' + b, 'Recheck'))) let left = ids.filter((id) => !got[id]) if (left.length) { take(await agent(sp + '\n\n' + taskSubset(b, left), opts('QC', 'QC:' + b + ':retry', 'Recheck'))) left = ids.filter((id) => !got[id]) } return { got, left: left.length } } const collected = CONFIG.returnDecisions ? [] : null const keep = (label, got) => { if (collected && got) collected.push({ label: label + '@' + CONFIG.context.context_id, decisions: Object.values(got) }) } const screenJobs = JOBS.screen || [] const adjJobs = JOBS.adj || [] const recheckJobs = JOBS.recheck || [] // Check the jobs actually dispatched, including subset/range IDs and adjudication/QC jobs. const authorized = new Set(CONFIG.authorizedIds) function checkScope(job, ids) { const batch = new Set(CONFIG.batchIds[job.b] || []) if (!ids.length || ids.some((id) => !batch.has(id) || !authorized.has(id))) { throw new Error('Job outside the recorded pilot/authorized scope: ' + job.b) } } for (const job of screenJobs) { const ids = idsFor(job) checkScope(job, ids) for (const role of ['A', 'B']) { if (Array.isArray(job[role])) checkScope(job, job[role].length ? job[role] : ids) } } for (const job of adjJobs) checkScope(job, job.items.map((x) => x.id)) for (const job of recheckJobs) checkScope(job, job.ids) log('sr-screener TA run: ' + screenJobs.length + ' batch jobs, ' + adjJobs.length + ' adjudication jobs, ' + recheckJobs.length + ' recheck jobs') const [screenRes, adjRes, qcRes] = await parallel([ () => pipeline( screenJobs, async (x) => { const ids = idsFor(x) const mA = x.A === undefined ? 'all' : x.A const mB = x.B === undefined ? 'all' : x.B const run = (role, m) => (m === 'all' || (Array.isArray(m) && m.length) ? screenRole(role, x.b, ids, m) : Promise.resolve({ got: {}, missing: [], skipped: true })) const [A, B] = await parallel([() => run('A', mA), () => run('B', mB)]) return { ids, A, B, full: mA === 'all' && mB === 'all' } }, async (s, x) => { if (!s || !s.A || !s.B) { log(x.b + ': screening agent failed - records stay pending') return { b: x.b, status: 'screen-failed' } } keep('A:' + x.b, s.A.got) keep('B:' + x.b, s.B.got) if (s.A.missing.length || s.B.missing.length) { log(x.b + ': incomplete (A missing ' + s.A.missing.length + ', B missing ' + s.B.missing.length + ') - pending') return { b: x.b, status: 'screen-incomplete' } } if (!s.full) { log(x.b + ': partial re-screen done; conflicts are adjudicated in the next run') return { b: x.b, status: 'partial-done' } } const items = [] for (const id of s.ids) { const a = s.A.got[id] const bb = s.B.got[id] if (advances(a.d) !== advances(bb.d)) items.push({ id, A: a.d + '/' + a.code, B: bb.d + '/' + bb.code }) } let left = 0 if (items.length && CONFIG.conflictPolicy === 'adjudicate') { const r = await adjudicate(x.b, items) keep('ADJ:' + x.b, r.got) left = r.left } const both = s.ids.filter((id) => advances(s.A.got[id].d) && advances(s.B.got[id].d)).length log(x.b + ': ' + s.ids.length + ' screened, ' + both + ' advanced by both, ' + items.length + ' conflicts' + (left ? ', ' + left + ' NOT adjudicated (pending)' : '')) return { b: x.b, status: left ? 'adj-incomplete' : 'done', conflicts: items.length } }, ), () => pipeline(adjJobs, async (x) => { const r = await adjudicate(x.b, x.items) keep('ADJ:' + x.b, r.got) log(x.b + ': adjudicated ' + (x.items.length - r.left) + '/' + x.items.length) return { b: x.b, status: r.left ? 'adj-incomplete' : 'done' } }), () => pipeline(recheckJobs, async (x) => { const r = await recheck(x.b, x.ids, x.full) keep('QC:' + x.b, r.got) log(x.b + ': QC rechecked ' + (x.ids.length - r.left) + '/' + x.ids.length) return { b: x.b, status: r.left ? 'qc-incomplete' : 'done' } }), ]) const norm = (res, jobs) => (res || []).map((r, i) => r || { b: jobs[i].b, status: 'failed' }) const S = norm(screenRes, screenJobs) const A = norm(adjRes, adjJobs) const Q = norm(qcRes, recheckJobs) const count = (rs) => rs.reduce((m, r) => ((m[r.status] = (m[r.status] || 0) + 1), m), {}) const out = { context: CONFIG.context, screen: count(S), adjudication: count(A), recheck: count(Q), needsAttention: S.concat(A, Q).filter((r) => r.status !== 'done' && r.status !== 'partial-done').map((r) => r.b + ':' + r.status).slice(0, 300), next: 'Run scripts/merge_decisions.py on this run journal; it lists anything still pending.', } if (collected) out.decisions = collected return out