disclosure-bureau/web/app/api/jobs/[id]/route.ts
Luiz Gustavo dd75a67964
Some checks failed
CI / Web — typecheck + lint + build (push) Failing after 45s
CI / Scripts — Python smoke (push) Failing after 5s
CI / Web — npm audit (push) Failing after 40s
CI / Retrieval — golden set (Recall@5 + MRR) (push) Failing after 3s
W3.8: Investigation Bureau complete — Poirot, Taleb, Tetlock, Case-Writer
Brings the bureau from 4 → 8 detectives. All eight run as Bun + claude-CLI
subprocesses against the same Supabase + investigation_jobs LISTEN/NOTIFY
queue, sharing search.ts hybridSearch and writer-side validators that
gate writes against schema + FK.

New detectives:

  Poirot (witness_analysis)
    - prompts/poirot.md — credibility / access / bias / corroboration /
      verdict; uses entity_mentions JOIN chunks to pull 12 chunks per
      person; resolves corroboration_refs chunk_ids defensively (accepts
      bare cNNNN even when the model emits pNNN/cNNNN).
    - INSERT into public.witnesses with W-NNNN naming.
    - Tone: purple (#9b5de5).

  Taleb (outlier_scan)
    - prompts/taleb.md — "surprise is relative to a model"; at most 3
      outliers; each requires explicit dominant_model + why_surprising +
      what_it_implies; fan-out into public.gaps with scope.kind="outlier".
    - Same unscoped-fallback as Dupin (Pass 1 with doc_id, Pass 2 widens
      to corpus if hits < 3).
    - Tone: yellow (#ffd23f).

  Tetlock (calibrate_hypothesis)
    - prompts/tetlock.md — honest Bayesian update; emits new_posterior +
      Δ + recommended_action ∈ {keep, downgrade, upgrade, supersede}.
    - write_calibration UPDATEs public.hypotheses + APPENDS a
      "## Calibration history" section to the H-NNNN.md case file
      (calibration is append-only — each datapoint matters). Posterior
      band auto-corrected to match Tetlock thresholds.
    - NO_NEW_EVIDENCE sentinel handled; pure 'keep' with |Δ|<0.005 only
      touches updated_at + reviewed_by.
    - Tone: teal (#26d4cc).

  Case-Writer (case_report)
    - prompts/case-writer.md — Dr. Watson assembles all artefacts
      (E-NNNN, H-NNNN, R-NNNN, W-NNNN, G-NNNN) into a five-act narrative.
      ILIKE filter on topic; doc_id optional scope.
    - Larger budget cap (≥ $0.50) + longer timeout for prose generation.
    - Writes case/reports/<slug>.md with frontmatter (topic + counts);
      no DB table for v0.
    - New page /c/[slug] renders the report via MarkdownBody + stat chips.
    - Tone: gold (#e0c080).

Hardening across the bureau:
  - Sentinel parsing now accepts backticked AND prose-trailing forms
    (Holmes NO_HYPOTHESES, Dupin NO_CONTRADICTIONS, Schneier
    INSUFFICIENT_HYPOTHESIS, Poirot INSUFFICIENT_TESTIMONY, Taleb
    NO_OUTLIERS, Tetlock NO_NEW_EVIDENCE, Case-Writer
    INSUFFICIENT_ARTEFACTS). Avoids the failure mode where the model
    refuses honestly but the runtime treated it as a parse error
    (observed live with Poirot+Hoover identifying the DIRECTOR
    false-positive disambiguation issue in entity_mentions).

Chat tool extensions (web/lib/chat/tools.ts):
  - request_investigation now accepts 7 kinds. Each routes to its
    detective with appropriate validation (hypothesis_id regex,
    person_id kebab-case, topic non-empty, doc_id for evidence_chain).
  - ETA per kind: Holmes/Dupin 60s, Poirot 45s, Schneier/Tetlock 30s,
    Taleb 50s, Case-Writer 180s (longer prose), Locard 30×n_chunks.

UI integration:
  - chat-bubble inline card paints each detective in its tone color.
  - /jobs/[id] page header swaps name/subtitle/tone per detective;
    question label adapts ("Topic" / "Hypothesis under attack" /
    "Witness under analysis" / "Topic to outlier-scan" / "Hypothesis
    under recalibration" / "Case to assemble").
  - job-status-poller renders: case-report link card (gold), outlier
    cards (yellow), witness cards (purple) — alongside existing
    hypothesis, evidence, contradiction cards.
  - /api/jobs/[id] hydrates witnesses (JOIN entities for canonical_name)
    + gaps (with scope JSONB).
  - /c/[slug] page reads /data/ufo/case/reports/<slug>.md and renders
    with MarkdownBody, frontmatter parsed for stat chips.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 22:11:39 -03:00

206 lines
6.9 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;
}
interface WitnessRow {
witness_id: string;
canonical_name: string | null;
entity_id: string | null;
credibility: string | null;
access_to_event: string | null;
bias_notes: string | null;
corroboration_refs: unknown;
verdict: string | null;
}
interface GapRow {
gap_id: string;
description: string;
scope: unknown;
suggested_next_move: string | null;
status: string;
created_by: string;
}
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[] = [];
const witnessIds: string[] = [];
const gapIds: 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);
if (typeof o.witness_id === "string") witnessIds.push(o.witness_id);
if (typeof o.gap_id === "string") gapIds.push(o.gap_id);
}
}
const [evidence, hypotheses, contradictions, witnesses, gaps] = 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[]),
witnessIds.length > 0
? pgQuery<WitnessRow>(
`SELECT w.witness_id, e.canonical_name, e.entity_id,
w.credibility, w.access_to_event, w.bias_notes,
w.corroboration_refs, w.verdict
FROM public.witnesses w
LEFT JOIN public.entities e ON e.entity_pk = w.person_entity_pk
WHERE w.witness_id = ANY($1::text[])
ORDER BY w.witness_id`,
[witnessIds],
)
: Promise.resolve([] as WitnessRow[]),
gapIds.length > 0
? pgQuery<GapRow>(
`SELECT gap_id, description, scope, suggested_next_move, status, created_by
FROM public.gaps WHERE gap_id = ANY($1::text[]) ORDER BY gap_id`,
[gapIds],
)
: Promise.resolve([] as GapRow[]),
]);
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,
witnesses,
gaps,
});
} catch (e) {
return NextResponse.json({ error: "db_unavailable", message: (e as Error).message }, { status: 503 });
}
}