mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-27 22:15:33 +00:00
* rfc: temporal axis for contradiction probe Field report on residual HIGH findings from gbrain eval suspected-contradictions and proposal for a 4-phase fix (Phase 1 = judge prompt + verdict enum is the recommended starting point). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(eval): pass effective_date to judge prompt; bump PROMPT_VERSION Lane A1 of the temporal-contradiction-probe wave. Threads page-level effective_date through the search projection into the contradiction judge so the LLM can reason about supersession instead of treating every dated pair as a contradiction. Changes: - SearchResult interface adds optional effective_date + effective_date_source fields; rowToSearchResult populates them from the row data with date-only YYYY-MM-DD normalization (handles both postgres.js Date and PGLite string). - 8 SELECT projection sites (3 in postgres-engine, 5 in pglite-engine) now carry p.effective_date + p.effective_date_source through their inner CTEs and outer SELECTs so search results expose the field on both engines. - PairMember (eval-contradictions/types.ts) gets the two fields as required (string | null) so the type forces every constructor to think about temporal anchoring. Runner's searchResultToMember + takeToMember handle the normalization; takes inherit the chunk's page-level date. - buildJudgePrompt emits `Statement A (from: YYYY-MM-DD)` when effective_date is non-null, else `(date unknown)`. Prompt instructions explain the tag so the model knows what to do with it. - PROMPT_VERSION bumps '1' → '2'. Cache-key tuple shape unchanged; old rows miss naturally on first run against the new prompt. Test fixtures in 5 files updated to include the new required fields. All 205 eval-contradictions unit tests + 101 search-related tests pass. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(eval): replace contradicts:boolean with verdict:enum (6 members) Lane A2 of the temporal-contradiction-probe wave. Expands the judge's classification vocabulary from a binary contradicts:bool to a six-member verdict enum so the probe can distinguish "this changed" from "this is wrong". Verdict taxonomy: no_contradiction — drop from findings contradiction — genuine conflict at same point in time temporal_supersession — newer claim updates/replaces older; not an error temporal_regression — metric/status went backwards over time (signal) temporal_evolution — legitimate change, neither supersession nor regression negation_artifact — judge misread an explicit negation Changes: - types.ts: Verdict union (6 members); Severity gains 'info'; ResolutionKind extended with temporal_supersede, flag_for_review, log_timeline_change; JudgeVerdict.contradicts → verdict; ContradictionFinding now carries verdict; ProbeReport adds queries_with_any_finding + verdict_breakdown (additive). - judge.ts: parseResolutionKind + parseVerdict guards; normalizeVerdict reads the new field and applies the C1 confidence floor only to verdict='contradiction' (the new verdicts are informational classifications, no floor). Prompt rubric rewritten to ask for verdict + extended severity scale. - severity-classify.ts: 'info' joins the rank with value 0; defaultSeverityForVerdict maps each verdict to its baseline severity (D7 — supersession=info, regression=high, etc.). parseSeverity gains a fallback param so consumers can override 'low' default. - auto-supersession.ts: classifyResolution + renderResolutionCommand handle the three new resolution kinds. Probe still NEVER auto-mutates — the new kinds render paste-ready commands or informational lines. - cache.ts: isJudgeVerdict shape check matches the new verdict field; old v1 rows fail the guard and treat as misses. - runner.ts: emit predicate at cache-hit and judge-success branches changes from `verdict.contradicts` to `verdict.verdict !== 'no_contradiction'`. Without this, the new verdicts vanish from the report. Added per-verdict tally + queriesWithAnyFinding alongside the strict queriesWithContradiction. - trends.ts: latest run verdict breakdown surfaces in the trend chart. Test fixtures updated across 8 test files. All 210 eval-contradictions unit tests pass. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(eval): relax date-filter rule 3 when both sides dated Lane B of the temporal-contradiction-probe wave. The v1 date pre-filter skipped pairs whose chunk-text-extracted dates differed by >30 days as a cost-saving heuristic. That heuristic silently killed exactly the cases the new verdict taxonomy exists to surface — role transitions across years (e.g. a 2017 historical record vs. a 2025 current state), MRR claims years apart, status changes recorded over time. Lane A1+A2 made temporal supersession explicit and cheap to classify. The filter no longer needs to skip these pairs; the judge can label them. Changes: - date-filter.ts: shouldSkipForDateMismatch accepts optional effectiveDateA and effectiveDateB. When BOTH are non-null, returns skip=false with the new 'both_have_effective_date' reason — the judge will see the dates via the (from: YYYY-MM-DD) prompt tag from Lane A1. Other rules (same-paragraph dual-date override, missing-date fallback) preserved verbatim and still run first. - runner.ts: threads pair.{a,b}.effective_date into the date-filter call. Pairs that previously vanished into the skip bucket now reach the judge. Tests (R1 IRON RULE regression suite, 6 new cases): - both sides effective_date → not skipped - both sides effective_date overrides >30d chunk-text rule - rule 1 (same-paragraph dual-date) still wins over effective_date relaxation - rule 2 (missing chunk dates) still applies when effective_date partially present - undefined effective_dates fall through to v1 behavior (back-compat) - empty-string effective_date treated as missing (only real dates enable the relaxation) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(cli): cost-estimate prompt + --budget-usd + Haiku routing Lane C of the temporal-contradiction-probe wave. Three layers of cost guardrail, all stacked: (a) cost-estimate prompt at probe-run-time. Before the runner spends any tokens after a PROMPT_VERSION change, eval-suspected-contradictions reads the most recent persisted prompt_version from eval_contradictions_runs and compares. When they differ: - TTY: prints an upper-bound estimate + Ctrl-C window (default 10s, override via GBRAIN_PROBE_PROMPT_GRACE_SECONDS). - non-TTY: prints the estimate + auto-proceeds (autopilot path). - --yes override or GBRAIN_NO_PROBE_PROMPT=1: skip entirely. Mirrors the v0.32.7 runPostUpgradeReembedPrompt pattern. (b) --budget-usd N hard cap (pre-existing; PreFlightBudgetError surfaces when the estimate alone exceeds the cap, and CostTracker halts the run mid-flight when cumulative cost exceeds it). Documented in the help text alongside (a). (c) Judge model now routes through resolveModel() with configKey 'models.eval.contradictions_judge', tier 'utility' (Haiku-class default), and env var GBRAIN_CONTRADICTIONS_JUDGE_MODEL. The legacy --judge CLI flag still wins as the highest-precedence override. Doctor's model touchpoint registry (src/commands/models.ts:50) carries the new key so `gbrain models` and `gbrain models doctor` surface it. Also in this lane: - CLI: --severity accepts 'info' (the new Severity member from Lane A2). - CLI: --severity output shows [verdict] tag alongside slug pairs so operators distinguish genuine contradictions from temporal classifications. - Human summary: prints the new queries_with_any_finding metric and the per-verdict breakdown table. - Help text: explains the cost-prompt + budget-cap + model-routing interactions in one paragraph. New tests (9 cases on the cost-prompt helper): - --yes override skips - GBRAIN_NO_PROBE_PROMPT=1 skips - prompt_version unchanged → skips - non-TTY auto-proceeds with stderr note - TTY proceeds after grace - TTY aborts on Ctrl-C - fresh brain (no prior runs) fires the prompt - GBRAIN_PROBE_PROMPT_GRACE_SECONDS override honored - estimate banner contains query count + judge model + dollar amount All 225 eval-contradictions tests + 25 model-config tests pass. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(eval): R4/R5/R6 IRON-RULE regressions for the verdict-enum wave Lane D of the temporal-contradiction-probe wave. The Lanes A1/A2/B/C lanes landed the behavior; this lane pins the regressions that protect the wave against future drift. R4 (runner emit predicate): five new tests, one per non-no_contradiction verdict, prove the runner.ts emit rule surfaces each one as a finding with the correct verdict tag, and that: - queries_with_contradiction (Wilson-CI denominator) ONLY counts verdict ='contradiction' — the strict metric is preserved - queries_with_any_finding counts every non-no_contradiction verdict - verdict_breakdown tallies correctly Plus one negative case: verdict='no_contradiction' produces zero findings. Without R4, a future runner refactor could collapse the new verdicts back to /dev/null and the report would silently shrink. R5 (cache key shape): direct shape assertion on buildCacheKey output. The key tuple is exactly 5 fields (chunk_a_hash, chunk_b_hash, model_id, prompt_version, truncation_policy). Adding a 6th field would silently break every operator's brain (no migration path). R6 (contradiction severity unchanged): four tests on normalizeVerdict pin the legacy semantics — judge-supplied severity wins (whether 'high' or 'low'), and on garbage severity input the fallback is 'medium' (per defaultSeverityForVerdict('contradiction')) NOT 'low'. The contradiction verdict's severity must never default to 'low', which would silently mask genuine conflicts as cosmetic naming issues. The temporal_regression case is included for parity (garbage → 'high' since regressions are real investor red flags). 236 eval-contradictions tests pass (211 + 6 R4 + 1 R5 + 4 R6 + 9 cost-prompt from Lane C). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(ci): privacy lint for docs/proposals/*.md Captures the residual TODO from the temporal-contradiction-probe wave's plan: prevent the bug class where an RFC lands in docs/proposals/ with PII that should never appear in a public technical artifact. The original RFC had to be scrubbed at force-push time (Step 0); this lint catches the same patterns at CI time so the next one can't slip through. Sibling to scripts/check-privacy.sh: - check-privacy.sh: bans the literal "Wintermute" repo-wide. - check-proposal-pii.sh: focuses on docs/proposals/*.md and the OTHER PII classes — personal-relationship vocabulary, private repo refs. Design contract: the denylist names PATTERNS, not real people. Naming specific real names (deceased relatives, therapist first names, dealflow contacts) inside this script would leak PII into the repo just by appearing here. The structural patterns below catch the SURROUNDING vocabulary that always accompanies such content in personal RFC prose. Trade-off: a future RFC that names a real person without any contextual markers won't be caught — accepted as residual risk handled by human review. Patterns flagged in docs/proposals/*.md: - garrytan/brain (private repo reference) - trial separation, permanent separation - couples session, couples therapist - divorce attorney(s) - grandmother's funeral, aunt's funeral - wintermute (also caught by check-privacy.sh; listed here for proposal-scoped clarity) Bare common words (separation, funeral) are NOT banned — only the combined personal-context phrases. "Separation of concerns" and other software vocabulary survives. Wired into: - `bun run verify` (gates every push) - `bun run check:all` - `bun run check:proposal-pii` (standalone) Tests: 15 cases in test/scripts/check-proposal-pii.test.ts. - Each pattern flagged when present, plus exit-code + stderr signal. - Two negative cases (separation-of-concerns, funeral metaphor) prove the lint doesn't false-positive on legitimate software prose. - No-proposals-dir → exit 0 (not a failure). - Multi-hit case proves all patterns surface together with a summary count. - The two test fixtures that name "Wintermute" / "WINTERMUTE" as sentinel literals are allowlisted in check-test-real-names.sh per the same meta-rule-enforcement exception as check-privacy.sh itself. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(privacy): allowlist new privacy-guard files in check-privacy.sh check-privacy.sh bans the literal Wintermute repo-wide. The two new files from the v0.34 privacy lint (scripts/check-proposal-pii.sh and its test) necessarily name the token to do their job. Same meta-rule-enforcement exception as scripts/check-privacy.sh itself, scripts/check-test-real-names.sh, test/recency-decay.test.ts, and the existing entries — describing what the rule forbids requires naming it. Without this allowlist, `bun run verify` fails on check:privacy. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: bump version and changelog (v0.35.1.0) Temporal-contradiction-probe wave — Phase 1 of the RFC at docs/proposals/temporal-contradiction-probe.md. Headline: the contradiction probe now classifies pairs into a 6-member verdict enum (no_contradiction, contradiction, temporal_supersession, temporal_regression, temporal_evolution, negation_artifact) and sees the page-level effective_date for each chunk via a (from: YYYY-MM-DD) tag in the prompt. The pre-judge date filter no longer skips dated wide-gap pairs, so the role-transition class (e.g. a 2017 historical record vs. a 2025 current state) reaches the judge and gets classified as temporal_supersession instead of vanishing into the skip bucket. PROMPT_VERSION bumped 1 → 2 (cache fully invalidated). Three-layer cost guardrail: TTY-only cost-estimate prompt with Ctrl-C window, --budget-usd hard cap, Haiku-tier routing via new models.eval.contradictions_judge config key. Also adds a CI privacy lint (scripts/check-proposal-pii.sh) wired into bun run verify that catches PII patterns in docs/proposals/*.md so future RFCs can't ship with personal-context vocabulary the way this wave's source RFC did at draft time. Phases 2-4 deferred to follow-up RFCs per the plan. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: garrytan-agents <garrytan-agents@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
367 lines
12 KiB
TypeScript
367 lines
12 KiB
TypeScript
/**
|
|
* Judge wrapper tests — hermetic via direct `chatFn` stub.
|
|
*
|
|
* Covers:
|
|
* - prompt building (query-conditioned, holder included, truncation)
|
|
* - parseModelJSON 4-strategy fence-stripping
|
|
* - shape validation (missing/invalid fields throw)
|
|
* - C1 confidence-floor double-enforcement
|
|
* - severity classification
|
|
* - refusal detection
|
|
* - UTF-8-safe truncation
|
|
*/
|
|
|
|
import { describe, test, expect } from 'bun:test';
|
|
import {
|
|
buildJudgePrompt,
|
|
judgeContradiction,
|
|
normalizeVerdict,
|
|
truncateUtf8,
|
|
DEFAULT_MAX_PAIR_CHARS,
|
|
} from '../src/core/eval-contradictions/judge.ts';
|
|
import type { ChatOpts, ChatResult } from '../src/core/ai/gateway.ts';
|
|
|
|
function mkResult(text: string, overrides: Partial<ChatResult> = {}): ChatResult {
|
|
return {
|
|
text,
|
|
blocks: [],
|
|
stopReason: 'end',
|
|
usage: { input_tokens: 100, output_tokens: 50, cache_read_tokens: 0, cache_creation_tokens: 0 },
|
|
model: 'anthropic:claude-haiku-4-5',
|
|
providerId: 'anthropic',
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function stubChat(response: ChatResult | ((opts: ChatOpts) => ChatResult | Promise<ChatResult>)) {
|
|
return async (opts: ChatOpts): Promise<ChatResult> => {
|
|
if (typeof response === 'function') return await response(opts);
|
|
return response;
|
|
};
|
|
}
|
|
|
|
describe('truncateUtf8', () => {
|
|
test('returns unchanged when under limit', () => {
|
|
expect(truncateUtf8('short', 100)).toBe('short');
|
|
});
|
|
|
|
test('truncates at code-point boundary', () => {
|
|
const out = truncateUtf8('hello world', 5);
|
|
expect(out).toBe('hello');
|
|
});
|
|
|
|
test('handles empty string', () => {
|
|
expect(truncateUtf8('', 100)).toBe('');
|
|
});
|
|
|
|
test('does not split surrogate pairs (4-byte emoji)', () => {
|
|
// 🚀 = U+1F680 (surrogate pair in UTF-16, length 2 in JS string).
|
|
const text = 'a🚀b'; // length 4 in JS
|
|
const out = truncateUtf8(text, 3); // would split the emoji
|
|
// Should drop the high surrogate, leaving just 'a'.
|
|
expect(out).toBe('a');
|
|
});
|
|
});
|
|
|
|
describe('buildJudgePrompt', () => {
|
|
test('includes user query verbatim (query-conditioned, Codex fix)', () => {
|
|
const p = buildJudgePrompt({
|
|
query: 'what is acme MRR',
|
|
a: { slug: 'companies/acme', text: 'A text' },
|
|
b: { slug: 'openclaw/chat/1', text: 'B text' },
|
|
maxPairChars: 1500,
|
|
});
|
|
expect(p).toContain("User's query: what is acme MRR");
|
|
});
|
|
|
|
test('truncates per maxPairChars', () => {
|
|
const longText = 'x'.repeat(5000);
|
|
const p = buildJudgePrompt({
|
|
query: 'q',
|
|
a: { slug: 'a', text: longText },
|
|
b: { slug: 'b', text: longText },
|
|
maxPairChars: 500,
|
|
});
|
|
// longText was 5000; both sides should appear truncated.
|
|
expect(p.split('x'.repeat(501)).length).toBe(1); // no 501-x run survives
|
|
});
|
|
|
|
test('includes source-tier label when present', () => {
|
|
const p = buildJudgePrompt({
|
|
query: 'q',
|
|
a: { slug: 'a', text: 'A', source_tier: 'curated' },
|
|
b: { slug: 'b', text: 'B', source_tier: 'bulk' },
|
|
maxPairChars: 1500,
|
|
});
|
|
expect(p).toContain('source-tier curated');
|
|
expect(p).toContain('source-tier bulk');
|
|
});
|
|
|
|
test('includes holder for take pairs', () => {
|
|
const p = buildJudgePrompt({
|
|
query: 'q',
|
|
a: { slug: 'a', text: 'A' },
|
|
b: { slug: 'b', text: 'B', holder: 'garry' },
|
|
maxPairChars: 1500,
|
|
});
|
|
expect(p).toContain('holder garry');
|
|
});
|
|
});
|
|
|
|
describe('normalizeVerdict', () => {
|
|
test('valid contradiction verdict passes through', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'contradiction',
|
|
severity: 'medium',
|
|
axis: 'MRR figure',
|
|
confidence: 0.85,
|
|
resolution_kind: 'dream_synthesize',
|
|
});
|
|
expect(v.verdict).toBe('contradiction');
|
|
expect(v.severity).toBe('medium');
|
|
expect(v.confidence).toBe(0.85);
|
|
expect(v.resolution_kind).toBe('dream_synthesize');
|
|
});
|
|
|
|
test('valid temporal_supersession verdict passes through (Lane A2)', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'temporal_supersession',
|
|
severity: 'info',
|
|
axis: 'role over time',
|
|
confidence: 0.9,
|
|
resolution_kind: 'temporal_supersede',
|
|
});
|
|
expect(v.verdict).toBe('temporal_supersession');
|
|
expect(v.severity).toBe('info');
|
|
expect(v.resolution_kind).toBe('temporal_supersede');
|
|
});
|
|
|
|
test('all 6 verdict members accepted by parser', () => {
|
|
for (const verdict of [
|
|
'no_contradiction',
|
|
'contradiction',
|
|
'temporal_supersession',
|
|
'temporal_regression',
|
|
'temporal_evolution',
|
|
'negation_artifact',
|
|
] as const) {
|
|
const v = normalizeVerdict({ verdict, severity: 'medium', confidence: 0.8 });
|
|
expect(v.verdict).toBe(verdict);
|
|
}
|
|
});
|
|
|
|
test('throws on missing verdict field', () => {
|
|
expect(() => normalizeVerdict({ confidence: 0.9 })).toThrow();
|
|
});
|
|
|
|
test('throws on invalid verdict string', () => {
|
|
expect(() => normalizeVerdict({ verdict: 'maybe', confidence: 0.9 })).toThrow();
|
|
});
|
|
|
|
test('throws on invalid confidence', () => {
|
|
expect(() => normalizeVerdict({ verdict: 'contradiction', confidence: 'high' })).toThrow();
|
|
expect(() => normalizeVerdict({ verdict: 'contradiction', confidence: NaN })).toThrow();
|
|
});
|
|
|
|
test('throws on missing or non-object input', () => {
|
|
expect(() => normalizeVerdict(null)).toThrow();
|
|
expect(() => normalizeVerdict(undefined)).toThrow();
|
|
expect(() => normalizeVerdict('json string')).toThrow();
|
|
});
|
|
|
|
test('C1 double-enforce: verdict:contradiction + confidence<0.7 downgrades to no_contradiction', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'contradiction',
|
|
severity: 'high',
|
|
axis: 'something',
|
|
confidence: 0.6,
|
|
resolution_kind: 'takes_supersede',
|
|
});
|
|
expect(v.verdict).toBe('no_contradiction');
|
|
expect(v.axis).toBe('');
|
|
expect(v.resolution_kind).toBeNull();
|
|
});
|
|
|
|
test('C1 boundary: confidence exactly 0.7 stays as verdict:contradiction', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'contradiction',
|
|
severity: 'medium',
|
|
axis: 'something',
|
|
confidence: 0.7,
|
|
});
|
|
expect(v.verdict).toBe('contradiction');
|
|
expect(v.confidence).toBe(0.7);
|
|
});
|
|
|
|
test('C1 floor does NOT downgrade other verdicts on low confidence (Lane A2)', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'temporal_supersession',
|
|
severity: 'info',
|
|
confidence: 0.4,
|
|
});
|
|
// Non-contradiction verdicts have no confidence floor; they survive at 0.4.
|
|
expect(v.verdict).toBe('temporal_supersession');
|
|
});
|
|
|
|
test('clamps confidence into [0, 1]', () => {
|
|
const v1 = normalizeVerdict({ verdict: 'no_contradiction', severity: 'low', confidence: -0.5 });
|
|
expect(v1.confidence).toBe(0);
|
|
const v2 = normalizeVerdict({ verdict: 'no_contradiction', severity: 'low', confidence: 1.5 });
|
|
expect(v2.confidence).toBe(1);
|
|
});
|
|
|
|
test('garbage severity falls back to defaultSeverityForVerdict (no_contradiction → info)', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'no_contradiction',
|
|
severity: 'critical',
|
|
confidence: 0.5,
|
|
});
|
|
expect(v.severity).toBe('info');
|
|
});
|
|
|
|
test('garbage severity falls back to defaultSeverityForVerdict (contradiction → medium)', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'contradiction',
|
|
severity: 'critical',
|
|
confidence: 0.85,
|
|
});
|
|
expect(v.severity).toBe('medium');
|
|
});
|
|
|
|
test('unknown resolution_kind on contradiction falls back to manual_review (legacy)', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'contradiction',
|
|
severity: 'medium',
|
|
axis: 'X',
|
|
confidence: 0.85,
|
|
resolution_kind: 'invalid_kind',
|
|
});
|
|
expect(v.resolution_kind).toBe('manual_review');
|
|
});
|
|
|
|
test('axis cleared when verdict:no_contradiction', () => {
|
|
const v = normalizeVerdict({
|
|
verdict: 'no_contradiction',
|
|
severity: 'low',
|
|
axis: 'some axis',
|
|
confidence: 0.4,
|
|
});
|
|
expect(v.axis).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('judgeContradiction', () => {
|
|
const baseInput = {
|
|
query: 'what is acme MRR',
|
|
a: { slug: 'companies/acme', text: 'Acme MRR is $2M (compiled).' },
|
|
b: { slug: 'openclaw/chat/1', text: 'Acme MRR was $50K back in 2024.' },
|
|
model: 'anthropic:claude-haiku-4-5',
|
|
};
|
|
|
|
test('happy path: direct-parse JSON response', async () => {
|
|
const out = await judgeContradiction({
|
|
...baseInput,
|
|
chatFn: stubChat(mkResult(JSON.stringify({
|
|
verdict: 'contradiction',
|
|
severity: 'medium',
|
|
axis: 'MRR figure',
|
|
confidence: 0.85,
|
|
resolution_kind: 'dream_synthesize',
|
|
}))),
|
|
});
|
|
expect(out.verdict.verdict).toBe('contradiction');
|
|
expect(out.verdict.severity).toBe('medium');
|
|
expect(out.usage.inputTokens).toBe(100);
|
|
expect(out.usage.outputTokens).toBe(50);
|
|
});
|
|
|
|
test('fence-wrapped JSON: parseModelJSON 4-strategy fallback', async () => {
|
|
const fenced = '```json\n' + JSON.stringify({
|
|
verdict: 'no_contradiction',
|
|
severity: 'info',
|
|
confidence: 0.3,
|
|
}) + '\n```';
|
|
const out = await judgeContradiction({
|
|
...baseInput,
|
|
chatFn: stubChat(mkResult(fenced)),
|
|
});
|
|
expect(out.verdict.verdict).toBe('no_contradiction');
|
|
});
|
|
|
|
test('throws on parse failure (counted in judge_errors)', async () => {
|
|
await expect(
|
|
judgeContradiction({
|
|
...baseInput,
|
|
chatFn: stubChat(mkResult('not valid json at all')),
|
|
})
|
|
).rejects.toThrow();
|
|
});
|
|
|
|
test('detects refusal via stopReason', async () => {
|
|
await expect(
|
|
judgeContradiction({
|
|
...baseInput,
|
|
chatFn: stubChat(mkResult('Anything', { stopReason: 'refusal' })),
|
|
})
|
|
).rejects.toThrow(/refused/i);
|
|
});
|
|
|
|
test('detects refusal via response text', async () => {
|
|
await expect(
|
|
judgeContradiction({
|
|
...baseInput,
|
|
chatFn: stubChat(mkResult("I can't help with that")),
|
|
})
|
|
).rejects.toThrow(/refused/i);
|
|
});
|
|
|
|
test('passes maxPairChars through to truncation', async () => {
|
|
let capturedPrompt = '';
|
|
await judgeContradiction({
|
|
...baseInput,
|
|
a: { slug: 'a', text: 'x'.repeat(5000) },
|
|
b: { slug: 'b', text: 'y'.repeat(5000) },
|
|
maxPairChars: 100,
|
|
chatFn: stubChat(async (opts) => {
|
|
const userMsg = opts.messages.find((m) => m.role === 'user');
|
|
capturedPrompt = typeof userMsg?.content === 'string' ? userMsg.content : '';
|
|
return mkResult(JSON.stringify({
|
|
verdict: 'no_contradiction', severity: 'info', confidence: 0.5,
|
|
}));
|
|
}),
|
|
});
|
|
expect(capturedPrompt.split('x'.repeat(101)).length).toBe(1);
|
|
});
|
|
|
|
test('default maxPairChars constant is 1500', () => {
|
|
expect(DEFAULT_MAX_PAIR_CHARS).toBe(1500);
|
|
});
|
|
|
|
test('C1 enforcement reaches the verdict (low-confidence contradiction → no_contradiction)', async () => {
|
|
const out = await judgeContradiction({
|
|
...baseInput,
|
|
chatFn: stubChat(mkResult(JSON.stringify({
|
|
verdict: 'contradiction',
|
|
severity: 'high',
|
|
axis: 'something',
|
|
confidence: 0.5,
|
|
}))),
|
|
});
|
|
expect(out.verdict.verdict).toBe('no_contradiction');
|
|
});
|
|
|
|
test('query appears in the rendered prompt (Codex fix)', async () => {
|
|
let capturedQuery = '';
|
|
await judgeContradiction({
|
|
...baseInput,
|
|
query: 'distinctive-query-marker-12345',
|
|
chatFn: stubChat(async (opts) => {
|
|
const m = opts.messages[0]?.content;
|
|
capturedQuery = typeof m === 'string' ? m : '';
|
|
return mkResult(JSON.stringify({ verdict: 'no_contradiction', severity: 'info', confidence: 0.4 }));
|
|
}),
|
|
});
|
|
expect(capturedQuery).toContain('distinctive-query-marker-12345');
|
|
});
|
|
});
|