User flagged that the bureau was emitting English-only output, violating
the project's bilingual rule. Every narrative field now ships in both
languages: stored in sibling DB columns + rendered as adjacent markdown
sections per CLAUDE.md §3.
Migration 0007 (apply as supabase_admin):
- public.hypotheses +question_pt_br, +position_pt_br,
+argument_for_pt_br, +argument_against_pt_br
- public.contradictions +topic_pt_br, +notes_pt_br
- public.witnesses +access_to_event_pt_br, +bias_notes_pt_br,
+verdict_pt_br
- public.gaps +description_pt_br, +suggested_next_move_pt_br
- public.evidence: unchanged (verbatim_excerpt stays source-language)
- JSONB siblings inside contradictions.chunks + gaps.scope handled at
runtime (statement_pt_br, title_pt_br, dominant_model_pt_br,
why_surprising_pt_br, what_it_implies_pt_br).
Detective prompts (all 7) rewritten with explicit bilingual JSON contract:
- Output protocol section names every EN field + its _pt_br sibling
- "Bilingual is mandatory" warning in the task instruction
- Sentinel skip-states unchanged (NO_HYPOTHESES, NO_CONTRADICTIONS,
INSUFFICIENT_TESTIMONY, INSUFFICIENT_HYPOTHESIS, NO_OUTLIERS,
NO_NEW_EVIDENCE, INSUFFICIENT_ARTEFACTS)
- Schneier: parallel arrays — hidden_assumptions[i] matches
hidden_assumptions_pt_br[i], lengths must match
- Case-Writer: interleaved §1 (EN) / §1 (PT-BR) per act in the body
Writer-side validation (all 7 tools):
- Reject INSERT if PT-BR sibling missing when EN field is set
- Persist both languages atomically in one INSERT (no half-updates)
- Markdown renderers write adjacent EN+PT-BR sections in case files
(## Argument for (EN) followed by ## Argumento a favor (PT-BR), etc.)
Detective parse layer (all 7 detectives):
- Coerce both keys from JSON output
- "incomplete_bilingual_*" skip reason when either side missing
- Defensive: PT-BR fields trimmed + length-capped same as EN
Orchestrator propagates question_pt_br + topic_pt_br through job payload
to runHolmes / runCaseWriter, mirroring the chat-tool entry point.
Web (UI):
- /api/jobs/[id] hydrates _pt_br siblings from pg
- job-status-poller HypothesisCard: PT-BR primary, EN in <details>
fallback when both exist
- ContradictionCard: PT-BR statement primary + secondary EN quote
- WitnessCard: PT-BR verdict primary + secondary EN quote, panels in PT
- GapCard: PT-BR title/why/implies primary
- /bureau hub: SELECTs both columns, renders PT-BR primary
- /h/[id]: ArgumentPanel renders PT-BR primary with collapsible EN
fallback when both exist
- BureauSnapshot homepage: position_pt_br / topic_pt_br / verdict_pt_br
primary
- DocBureauPanel /d/[doc]: same primary-PT-BR pattern
- New web/lib/i18n/pick.ts helper (unused yet by chat/agents — kept
for future locale-driven switching when both languages are equally
full; current rule is PT-BR-first since the user is brasileiro)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
219 lines
6.5 KiB
TypeScript
219 lines
6.5 KiB
TypeScript
/**
|
|
* 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;
|
|
statement_pt_br?: string;
|
|
/** Optional weight or stance label (e.g. "asserts", "denies"). */
|
|
stance?: string;
|
|
}
|
|
|
|
export interface WriteContradictionArgs {
|
|
topic: string;
|
|
topic_pt_br?: string;
|
|
positions: ContradictionPosition[];
|
|
notes?: string;
|
|
notes_pt_br?: string;
|
|
resolution_status?: "open" | "resolved" | "irreconcilable";
|
|
}
|
|
|
|
export interface WriteContradictionContext {
|
|
job_id: string;
|
|
detective: string;
|
|
}
|
|
|
|
interface ResolvedPosition extends ContradictionPosition {
|
|
chunk_pk: number;
|
|
page: number;
|
|
statement_pt_br: string;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
async function resolveChunk(doc_id: string, chunk_id: string): Promise<{ chunk_pk: number; page: number } | null> {
|
|
const cid = normalizeChunkId(chunk_id);
|
|
const row = await queryOne<{ chunk_pk: number; page: number }>(
|
|
`SELECT chunk_pk, page FROM public.chunks WHERE doc_id = $1 AND chunk_id = $2`,
|
|
[doc_id, cid],
|
|
);
|
|
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}` : ""}`,
|
|
"",
|
|
`**(EN)** > ${p.statement}`,
|
|
"",
|
|
`**(PT-BR)** > ${p.statement_pt_br}`,
|
|
"",
|
|
`Source: [[${p.doc_id}/p${pageStr}#${p.chunk_id}]]`,
|
|
].join("\n");
|
|
});
|
|
|
|
return [
|
|
fm,
|
|
"",
|
|
`# Contradiction ${id}`,
|
|
"",
|
|
`**Topic (EN).** ${body.topic}`,
|
|
`**Tópico (PT-BR).** ${body.topic_pt_br ?? body.topic}`,
|
|
"",
|
|
"## Positions in tension",
|
|
"",
|
|
positionBlocks.join("\n\n"),
|
|
"",
|
|
"## Notes (EN)",
|
|
"",
|
|
body.notes || "_(no commentary recorded)_",
|
|
"",
|
|
"## Notas (PT-BR)",
|
|
"",
|
|
body.notes_pt_br || "_(sem comentário registrado)_",
|
|
"",
|
|
].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 (!body.topic_pt_br?.trim()) throw new Error("topic_pt_br required (bilingual contract)");
|
|
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)`);
|
|
}
|
|
if (body.notes && !body.notes_pt_br?.trim()) {
|
|
throw new Error("notes_pt_br required when notes set (bilingual contract)");
|
|
}
|
|
|
|
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`);
|
|
}
|
|
if (!p?.statement_pt_br?.trim()) {
|
|
throw new Error(`position ${p.doc_id}/${p.chunk_id} missing statement_pt_br (bilingual contract)`);
|
|
}
|
|
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(),
|
|
statement_pt_br: p.statement_pt_br.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,
|
|
statement_pt_br: p.statement_pt_br,
|
|
stance: p.stance ?? null,
|
|
}));
|
|
|
|
await query(
|
|
`INSERT INTO public.contradictions
|
|
(contradiction_id, topic, topic_pt_br, chunks, detected_by,
|
|
resolution_status, notes, notes_pt_br)
|
|
VALUES ($1, $2, $3, $4::jsonb, $5, $6, $7, $8)`,
|
|
[
|
|
contradiction_id,
|
|
body.topic.trim(),
|
|
body.topic_pt_br!.trim(),
|
|
JSON.stringify(chunkPayload),
|
|
ctx.detective,
|
|
body.resolution_status ?? "open",
|
|
body.notes ?? null,
|
|
body.notes_pt_br ?? 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 };
|
|
}
|