Adds the third AI detective in the Investigation Bureau runtime: C. Auguste
Dupin, who scans a corpus shortlist for pairs (or small groups) of chunks
that cannot both be true under any ordinary reading.
Runtime:
- prompts/dupin.md — discipline (no contradiction without ≥2 distinct
chunk_ids; reject same-vocabulary near-misses; FEW high-confidence
over MANY weak ones; emit `NO_CONTRADICTIONS` when corpus is silent)
- src/detectives/dupin.ts — hybridSearch with k=18 (more chunks than
Holmes because contradictions emerge from comparing dispersed
claims), strict JSON-array parsing, AT MOST 3 contradictions per call
- src/tools/write_contradiction.ts — validates topic + ≥2 positions
drawn from ≥2 distinct chunks, resolves chunk_pk via DB lookup
(rejects positions citing unknown chunks), INSERTs into
public.contradictions + writes case/contradictions/R-NNNN.md
- orchestrator: new `contradiction_scan` kind dispatching to runDupin;
payload { topic, doc_id?, lang?, context_chunks? }
Chat + UI:
- request_investigation gains kind=contradiction_scan + topic arg;
triggered detective auto-resolves to dupin
- chat-bubble inline card renders dupin in orange (#ff8a4d) to
distinguish from holmes (cyan) and locard (green)
- /jobs/[id] page swaps title + subtitle + tone per detective;
"Question" label becomes "Topic" for contradiction_scan
- /api/jobs/[id] hydrates public.contradictions when outputs[] surfaces
contradiction_ids
- job-status-poller renders ContradictionCard: topic + N positions
(verbatim statements quoted, stance label optional, link to source
chunk) + optional notes panel, with resolution_status badge
(open/resolved/irreconcilable)
R-NNNN shares the contradiction_id_seq slot with relation per
CLAUDE.md naming — same conceptual class (a connection between two
pieces of evidence in tension).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
161 lines
5.5 KiB
TypeScript
161 lines
5.5 KiB
TypeScript
/**
|
|
* GET /api/jobs/[id] — public read of an investigation_jobs row.
|
|
*
|
|
* Hydrates the outputs[] payload by joining to public.evidence / public.hypotheses
|
|
* so the /jobs/[id] page can render evidence cards + hypothesis cards without
|
|
* n+1 round-trips.
|
|
*
|
|
* No auth required (read-only): anyone with a job_id can see status. This
|
|
* matches the chat tool's UX: the chat reveals the URL, the user opens it,
|
|
* the page renders. No PII is exposed beyond what the user already typed
|
|
* (triggered_by carries their email — we strip it).
|
|
*/
|
|
import { NextResponse } from "next/server";
|
|
import { pgQuery } from "@/lib/retrieval/db";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
interface JobRow {
|
|
job_id: string;
|
|
kind: string;
|
|
payload: Record<string, unknown> | null;
|
|
status: string;
|
|
worker_id: string | null;
|
|
started_at: string | null;
|
|
finished_at: string | null;
|
|
outputs: unknown;
|
|
error: string | null;
|
|
created_at: string;
|
|
}
|
|
|
|
interface EvidenceRow {
|
|
evidence_id: string;
|
|
grade: string | null;
|
|
source_page_id: string;
|
|
doc_id: string | null;
|
|
page: number | null;
|
|
chunk_id: string | null;
|
|
verbatim_excerpt: string | null;
|
|
custody_steps: unknown;
|
|
bbox: unknown;
|
|
confidence_band: string | null;
|
|
related_hypotheses: unknown;
|
|
}
|
|
|
|
interface HypothesisRow {
|
|
hypothesis_id: string;
|
|
question: string | null;
|
|
position: string | null;
|
|
argument_for: string | null;
|
|
argument_against: string | null;
|
|
prior: number | null;
|
|
posterior: number | null;
|
|
confidence_band: string | null;
|
|
status: string | null;
|
|
evidence_refs: unknown;
|
|
}
|
|
|
|
interface ContradictionRow {
|
|
contradiction_id: string;
|
|
topic: string;
|
|
chunks: unknown;
|
|
resolution_status: string | null;
|
|
notes: string | null;
|
|
detected_by: string | null;
|
|
}
|
|
|
|
function durationMs(started: string | null, finished: string | null, created: string): number | null {
|
|
const a = started ? new Date(started).getTime() : null;
|
|
const b = finished ? new Date(finished).getTime() : null;
|
|
if (a !== null && b !== null) return b - a;
|
|
if (a !== null) return Date.now() - a;
|
|
return Date.now() - new Date(created).getTime();
|
|
}
|
|
|
|
export async function GET(
|
|
_request: Request,
|
|
ctx: { params: Promise<{ id: string }> },
|
|
) {
|
|
const { id } = await ctx.params;
|
|
if (!/^[0-9a-f-]{36}$/i.test(id)) {
|
|
return NextResponse.json({ error: "bad_job_id" }, { status: 400 });
|
|
}
|
|
try {
|
|
const rows = await pgQuery<JobRow>(
|
|
`SELECT job_id, kind, payload, status, worker_id, started_at, finished_at,
|
|
outputs, error, created_at
|
|
FROM public.investigation_jobs WHERE job_id = $1`,
|
|
[id],
|
|
);
|
|
const job = rows[0];
|
|
if (!job) return NextResponse.json({ error: "not_found" }, { status: 404 });
|
|
|
|
// Collect IDs surfaced in outputs[] for hydration.
|
|
const evidenceIds: string[] = [];
|
|
const hypothesisIds: string[] = [];
|
|
const contradictionIds: string[] = [];
|
|
if (Array.isArray(job.outputs)) {
|
|
for (const o of job.outputs as Array<Record<string, unknown>>) {
|
|
if (typeof o.evidence_id === "string") evidenceIds.push(o.evidence_id);
|
|
if (typeof o.hypothesis_id === "string") hypothesisIds.push(o.hypothesis_id);
|
|
if (typeof o.contradiction_id === "string") contradictionIds.push(o.contradiction_id);
|
|
}
|
|
}
|
|
|
|
const [evidence, hypotheses, contradictions] = await Promise.all([
|
|
evidenceIds.length > 0
|
|
? pgQuery<EvidenceRow>(
|
|
`SELECT e.evidence_id, e.grade, e.source_page_id,
|
|
split_part(e.source_page_id, '/p', 1) AS doc_id,
|
|
NULLIF(split_part(e.source_page_id, '/p', 2), '')::int AS page,
|
|
c.chunk_id, e.verbatim_excerpt, e.custody_steps, e.bbox,
|
|
e.confidence_band, e.related_hypotheses
|
|
FROM public.evidence e
|
|
LEFT JOIN public.chunks c ON c.chunk_pk = e.source_chunk_pk
|
|
WHERE e.evidence_id = ANY($1::text[])
|
|
ORDER BY e.evidence_id`,
|
|
[evidenceIds],
|
|
)
|
|
: Promise.resolve([] as EvidenceRow[]),
|
|
hypothesisIds.length > 0
|
|
? pgQuery<HypothesisRow>(
|
|
`SELECT hypothesis_id, question, position, argument_for, argument_against,
|
|
prior, posterior, confidence_band, status, evidence_refs
|
|
FROM public.hypotheses
|
|
WHERE hypothesis_id = ANY($1::text[])
|
|
ORDER BY hypothesis_id`,
|
|
[hypothesisIds],
|
|
)
|
|
: Promise.resolve([] as HypothesisRow[]),
|
|
contradictionIds.length > 0
|
|
? pgQuery<ContradictionRow>(
|
|
`SELECT contradiction_id, topic, chunks, resolution_status, notes, detected_by
|
|
FROM public.contradictions
|
|
WHERE contradiction_id = ANY($1::text[])
|
|
ORDER BY contradiction_id`,
|
|
[contradictionIds],
|
|
)
|
|
: Promise.resolve([] as ContradictionRow[]),
|
|
]);
|
|
|
|
return NextResponse.json({
|
|
job_id: job.job_id,
|
|
kind: job.kind,
|
|
payload: job.payload,
|
|
status: job.status,
|
|
worker_id: job.worker_id,
|
|
started_at: job.started_at,
|
|
finished_at: job.finished_at,
|
|
created_at: job.created_at,
|
|
duration_ms: durationMs(job.started_at, job.finished_at, job.created_at),
|
|
error: job.error,
|
|
outputs: Array.isArray(job.outputs) ? job.outputs : [],
|
|
evidence,
|
|
hypotheses,
|
|
contradictions,
|
|
});
|
|
} catch (e) {
|
|
return NextResponse.json({ error: "db_unavailable", message: (e as Error).message }, { status: 503 });
|
|
}
|
|
}
|