W3.7: Dupin contradiction-scan detective + UI integration
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>
2026-05-24 00:34:04 +00:00
|
|
|
/**
|
|
|
|
|
* write_contradiction.ts — Dupin's primary writer.
|
|
|
|
|
*
|
|
|
|
|
* Inserts a row into public.contradictions and renders
|
|
|
|
|
* case/contradictions/R-NNNN.md.
|
|
|
|
|
*
|
|
|
|
|
* Validates:
|
|
|
|
|
* - topic + at least 2 positions
|
|
|
|
|
* - each position has a chunk_pk (resolved from doc_id + chunk_id via the DB)
|
|
|
|
|
* - position.statement is non-empty
|
|
|
|
|
* - notes ≤ 4000 chars
|
|
|
|
|
*
|
|
|
|
|
* Naming uses the R-NNNN slot from contradiction_id_seq. Note: this CLAUDE.md
|
|
|
|
|
* historically reserved R-NNNN for `relation` artefacts (chief-detective);
|
|
|
|
|
* contradictions are the same conceptual class (a connection between two
|
|
|
|
|
* pieces of evidence in tension), so they share the slot.
|
|
|
|
|
*/
|
|
|
|
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import { audit } from "../lib/audit";
|
|
|
|
|
import { env } from "../lib/env";
|
|
|
|
|
import { allocate } from "../lib/ids";
|
|
|
|
|
import { query, queryOne } from "../lib/pg";
|
|
|
|
|
|
|
|
|
|
export interface ContradictionPosition {
|
|
|
|
|
doc_id: string;
|
|
|
|
|
chunk_id: string;
|
|
|
|
|
/** The verbatim or paraphrased claim that puts this chunk on this side. */
|
|
|
|
|
statement: string;
|
|
|
|
|
/** Optional weight or stance label (e.g. "asserts", "denies"). */
|
|
|
|
|
stance?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface WriteContradictionArgs {
|
|
|
|
|
topic: string;
|
|
|
|
|
positions: ContradictionPosition[];
|
|
|
|
|
notes?: string;
|
|
|
|
|
resolution_status?: "open" | "resolved" | "irreconcilable";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface WriteContradictionContext {
|
|
|
|
|
job_id: string;
|
|
|
|
|
detective: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ResolvedPosition extends ContradictionPosition {
|
|
|
|
|
chunk_pk: number;
|
|
|
|
|
page: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 00:42:01 +00:00
|
|
|
/**
|
|
|
|
|
* Strip page-prefix idioms detectives sometimes emit. Canonical chunk_id is
|
|
|
|
|
* just `c0042`. Forms accepted: "c0042", "p007#c0042", "p007/c0042".
|
|
|
|
|
*/
|
|
|
|
|
function normalizeChunkId(raw: string): string {
|
|
|
|
|
const m = raw.match(/c\d{4,}$/);
|
|
|
|
|
return m ? m[0] : raw;
|
|
|
|
|
}
|
|
|
|
|
|
W3.7: Dupin contradiction-scan detective + UI integration
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>
2026-05-24 00:34:04 +00:00
|
|
|
async function resolveChunk(doc_id: string, chunk_id: string): Promise<{ chunk_pk: number; page: number } | null> {
|
2026-05-24 00:42:01 +00:00
|
|
|
const cid = normalizeChunkId(chunk_id);
|
W3.7: Dupin contradiction-scan detective + UI integration
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>
2026-05-24 00:34:04 +00:00
|
|
|
const row = await queryOne<{ chunk_pk: number; page: number }>(
|
|
|
|
|
`SELECT chunk_pk, page FROM public.chunks WHERE doc_id = $1 AND chunk_id = $2`,
|
2026-05-24 00:42:01 +00:00
|
|
|
[doc_id, cid],
|
W3.7: Dupin contradiction-scan detective + UI integration
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>
2026-05-24 00:34:04 +00:00
|
|
|
);
|
|
|
|
|
return row ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderMd(
|
|
|
|
|
id: string,
|
|
|
|
|
body: WriteContradictionArgs,
|
|
|
|
|
positions: ResolvedPosition[],
|
|
|
|
|
ctx: WriteContradictionContext,
|
|
|
|
|
): string {
|
|
|
|
|
const fm = [
|
|
|
|
|
"---",
|
|
|
|
|
`schema_version: "0.1.0"`,
|
|
|
|
|
`type: contradiction`,
|
|
|
|
|
`contradiction_id: ${id}`,
|
|
|
|
|
`topic: ${JSON.stringify(body.topic)}`,
|
|
|
|
|
`resolution_status: ${body.resolution_status ?? "open"}`,
|
|
|
|
|
`detected_by: ${ctx.detective}`,
|
|
|
|
|
`job_id: ${ctx.job_id}`,
|
|
|
|
|
`created_at: ${new Date().toISOString()}`,
|
|
|
|
|
"---",
|
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
|
|
const positionBlocks = positions.map((p, i) => {
|
|
|
|
|
const pageStr = String(p.page).padStart(3, "0");
|
|
|
|
|
return [
|
|
|
|
|
`### Position ${i + 1}${p.stance ? ` — ${p.stance}` : ""}`,
|
|
|
|
|
"",
|
|
|
|
|
`> ${p.statement}`,
|
|
|
|
|
"",
|
|
|
|
|
`Source: [[${p.doc_id}/p${pageStr}#${p.chunk_id}]]`,
|
|
|
|
|
].join("\n");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
fm,
|
|
|
|
|
"",
|
|
|
|
|
`# Contradiction ${id}`,
|
|
|
|
|
"",
|
|
|
|
|
`**Topic.** ${body.topic}`,
|
|
|
|
|
"",
|
|
|
|
|
"## Positions in tension",
|
|
|
|
|
"",
|
|
|
|
|
positionBlocks.join("\n\n"),
|
|
|
|
|
"",
|
|
|
|
|
"## Notes",
|
|
|
|
|
"",
|
|
|
|
|
body.notes || "_(no commentary recorded)_",
|
|
|
|
|
"",
|
|
|
|
|
].join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function writeContradiction(
|
|
|
|
|
body: WriteContradictionArgs,
|
|
|
|
|
ctx: WriteContradictionContext,
|
|
|
|
|
): Promise<{ contradiction_id: string; case_file: string }> {
|
|
|
|
|
if (!body.topic?.trim()) throw new Error("topic required");
|
|
|
|
|
if (!Array.isArray(body.positions) || body.positions.length < 2) {
|
|
|
|
|
throw new Error("at least 2 positions required");
|
|
|
|
|
}
|
|
|
|
|
if (body.notes && body.notes.length > 4000) {
|
|
|
|
|
throw new Error(`notes too long (${body.notes.length} > 4000)`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resolved: ResolvedPosition[] = [];
|
|
|
|
|
for (const p of body.positions) {
|
|
|
|
|
if (!p?.doc_id?.trim() || !p?.chunk_id?.trim()) {
|
|
|
|
|
throw new Error("position requires doc_id + chunk_id");
|
|
|
|
|
}
|
|
|
|
|
if (!p?.statement?.trim()) {
|
|
|
|
|
throw new Error(`position ${p.doc_id}/${p.chunk_id} missing statement`);
|
|
|
|
|
}
|
|
|
|
|
const chunk = await resolveChunk(p.doc_id, p.chunk_id);
|
|
|
|
|
if (!chunk) {
|
|
|
|
|
throw new Error(`chunk ${p.doc_id}/${p.chunk_id} not found`);
|
|
|
|
|
}
|
|
|
|
|
resolved.push({
|
|
|
|
|
...p,
|
|
|
|
|
statement: p.statement.trim(),
|
|
|
|
|
chunk_pk: chunk.chunk_pk,
|
|
|
|
|
page: chunk.page,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reject pairs that point at the same chunk on every side — that's not a
|
|
|
|
|
// contradiction, that's a single statement.
|
|
|
|
|
const uniqueChunks = new Set(resolved.map((p) => p.chunk_pk));
|
|
|
|
|
if (uniqueChunks.size < 2) {
|
|
|
|
|
throw new Error("contradiction requires positions from at least 2 distinct chunks");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const contradiction_id = await allocate.contradictionId();
|
|
|
|
|
const chunkPayload = resolved.map((p) => ({
|
|
|
|
|
chunk_pk: p.chunk_pk,
|
|
|
|
|
doc_id: p.doc_id,
|
|
|
|
|
chunk_id: p.chunk_id,
|
|
|
|
|
page: p.page,
|
|
|
|
|
statement: p.statement,
|
|
|
|
|
stance: p.stance ?? null,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await query(
|
|
|
|
|
`INSERT INTO public.contradictions
|
|
|
|
|
(contradiction_id, topic, chunks, detected_by, resolution_status, notes)
|
|
|
|
|
VALUES ($1, $2, $3::jsonb, $4, $5, $6)`,
|
|
|
|
|
[
|
|
|
|
|
contradiction_id,
|
|
|
|
|
body.topic.trim(),
|
|
|
|
|
JSON.stringify(chunkPayload),
|
|
|
|
|
ctx.detective,
|
|
|
|
|
body.resolution_status ?? "open",
|
|
|
|
|
body.notes ?? null,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const dir = path.join(env.CASE_ROOT, "contradictions");
|
|
|
|
|
await mkdir(dir, { recursive: true });
|
|
|
|
|
const file = path.join(dir, `${contradiction_id}.md`);
|
|
|
|
|
await writeFile(file, renderMd(contradiction_id, body, resolved, ctx), "utf-8");
|
|
|
|
|
|
|
|
|
|
await audit({
|
|
|
|
|
event: "write_contradiction",
|
|
|
|
|
job_id: ctx.job_id,
|
|
|
|
|
detective: ctx.detective,
|
|
|
|
|
contradiction_id,
|
|
|
|
|
n_positions: resolved.length,
|
|
|
|
|
distinct_chunks: uniqueChunks.size,
|
|
|
|
|
file,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { contradiction_id, case_file: file };
|
|
|
|
|
}
|