Files
Jyotisha/frontend/scripts/research/consult-same-source-check.mts
T
ClaudeandClaude Opus 5.5 8b5c0fcc2f test(consult): deterministic same-source acceptance check (BUG-1774)
scripts/research/consult-same-source-check.mts flags an answer that claims
independent / multi-layer agreement while two items it lists in that claim are
one chart fact per the card's independence index (recomputed for cards
written before BUG-1773). Research acceptance check, not a runtime gate:
partial recall by design (explicit claim wording only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-08 19:32:57 +08:00

223 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* BUG-1773 acceptance check (astrologer acceptance note 2026-11-08: same-source rules are
* never independent votes). Deterministic, no model call.
*
* For one answer and the evidence card it was given, flag every place where the answer
* claims independent or multi-layer agreement (「三条互相独立的线索」「多层一致支持」
* 「相互印证」…) while the layers it names in that place rest on fewer distinct chart
* facts than layers, per the card's `independence` index (fact_id → layers reusing it).
* A card without the index (answers written before BUG-1773) gets it recomputed from its
* own base and domain section with the same builder, so old and new answers are scored
* the same way.
*
* What it can and cannot see:
* - it reads planets and layer cues (宫主, A2 / AL / UL, AmK / AK / DK, Shadbala, 大运,
* Narayana, yoga names, 受冲) inside the claim's sentence, or the list that follows a
* claim ending in 「:」;
* - it flags only when some fact is named through two or more of its layers in that
* window (a reuse the claim then counts as agreement), and the window does not itself
* say the layers share one fact (同一事实 / 同源 / 同一颗星 …);
* - it does not judge claims built from different facts, and it misses reuse spread over
* several paragraphs. Recall is therefore partial; see RESULTS-r7.md for its measured
* precision against the hand checks.
*
* Usage:
* tsx scripts/research/consult-same-source-check.mts <answer.md> [...] (payload beside each answer)
* Prints one JSON line per answer: { answer, domain, claims, flags: [{ quote, fact, layers }] }.
*/
import { readFileSync } from "node:fs";
import { evidenceCardIndependence } from "../../src/lib/consultation-evidence-card.ts";
type Json = Record<string, unknown>;
export type Independence = { facts: Record<string, string[]> };
export type SameSourceFlag = { quote: string; fact: string; layers: string[] };
const PLANET_ZH: Readonly<Record<string, string>> = {
Sun: "太阳", Moon: "月亮", Mars: "火星", Mercury: "水星", Jupiter: "木星", Venus: "金星", Saturn: "土星", Rahu: "罗睺", Ketu: "计都",
};
const SIGN_ZH: Readonly<Record<string, string>> = {
Aries: "白羊", Taurus: "金牛", Gemini: "双子", Cancer: "巨蟹", Leo: "狮子", Virgo: "处女",
Libra: "天秤", Scorpio: "天蝎", Sagittarius: "射手", Capricorn: "摩羯", Aquarius: "水瓶", Pisces: "双鱼",
};
const CN_NUM: Readonly<Record<string, string>> = { 一: "1", 二: "2", 三: "3", 四: "4", 五: "5", 六: "6", 七: "7", 八: "8", 九: "9", 十: "10", 十一: "11", 十二: "12" };
/** Places that claim agreement or independence between several pieces of evidence. */
export const INDEPENDENCE_CLAIM = new RegExp([
"(多层|多体系|多个体系|多套|几层|几套|多条|多重|多方|多个层面|多个技法|多种技法|[两三四五六2-6]\\s*(条|层|个|项|套|重|处|方面))[^。!?\\n]{0,14}(一致|共同|同向|都指向|指向同|印证|互证|叠加|独立)",
"(互相|彼此|各自|相互)独立",
"独立的?(证据|线索|来源|支持|信号|依据)",
"(交叉|相互|互相)(印证|佐证|支持)",
"(共同|同时|都)指向",
].join("|"));
const SAYS_SAME_SOURCE = /同一(个)?(事实|来源|颗星|落点|配置)|同源|不是(两|几|多)(票|条|个来源)|不算(独立|两)|算一个来源|只算一个/;
/**
* A layer's cue inside one item. `identifies` = the cue alone names the fact (a house lord,
* an Arudha pada's lord, a karaka); otherwise the item must also name the planet.
*/
function cueFor(layer: string): { pattern: RegExp; identifies: boolean } | null {
const house = /^D1 (\d+)宫(主|内)$/.exec(layer);
if (house) {
if (house[2] !== "主") return null;
const n = house[1]!;
const zh = Object.entries(CN_NUM).find(([, value]) => value === n)?.[0] ?? n;
return { pattern: new RegExp(`(?<![0-9、,,])(第\\s*)?(${n}|${zh})\\s*宫(的)?(宫主|主星|主)`), identifies: true };
}
const arudha = /^(A\d+|AL|UL)主星$/.exec(layer);
if (arudha) return { pattern: new RegExp(`(?<![A-Za-z0-9])${arudha[1]}(?![0-9])[^,。;、]{0,6}(主星|的主|之主)`), identifies: true };
if (/^(AK|AmK|DK|MK|PiK|PK|BK|GK)$/.test(layer)) return { pattern: new RegExp(`(?<![A-Za-z])${layer}(?![a-z])`), identifies: true };
const named = (pattern: RegExp) => ({ pattern, identifies: false });
if (layer === "Shadbala") return named(/Shadbala|六力/);
if (layer.startsWith("Vimshottari")) return named(/Vimshottari|大运|小运/);
if (layer.startsWith("Narayana")) return named(/Narayana/);
if (layer === "Karakamsha 主星") return named(/Karakamsha/);
if (layer === "事业分析") return named(/事业分析|career_analysis/);
if (layer.startsWith("受冲")) return named(/受冲|受压/);
if (layer === "主事星" || layer.endsWith("表征星")) return named(/主事星|表征星|代表星/);
const yoga = /^([A-Z][A-Za-z-]+)/.exec(layer);
if (yoga) return named(new RegExp(yoga[1]!));
return null;
}
function factPlanets(fact: string): string[] {
const placement = /^D1:([A-Z][a-z]+)@/.exec(fact);
if (placement) return [placement[1]!];
const pair = /^D1:([A-Z][a-z]+)[+>]([A-Z][a-z]+)$/.exec(fact);
if (pair) return [pair[1]!, pair[2]!];
const single = /^(?:D1|FBM|varga):([A-Z][a-z]+)/.exec(fact);
return single ? [single[1]!] : [];
}
const mentions = (text: string, planet: string) => text.includes(PLANET_ZH[planet] ?? planet) || new RegExp(`\\b${planet}\\b`).test(text);
const NEGATED = /(不|非|没有|未|并非|不能|不得|不算|不构成|不写成?|不是)[^,。;]{0,8}$/;
/** The claim's window: its sentence, plus the list right after a sentence ending in 「:」. */
function windows(answer: string): Array<{ quote: string; text: string }> {
const lines = answer.split("\n");
const out: Array<{ quote: string; text: string }> = [];
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i]!;
const sentences = line.split(/(?<=[。!?;])/);
for (const sentence of sentences) {
const hit = INDEPENDENCE_CLAIM.exec(sentence);
if (!hit) continue;
// 「不同向」「不构成两套都指向」「不完全一致」: a denial, not a claim.
if (NEGATED.test(sentence.slice(0, hit.index + hit[0].length - 2)) && /不|非|没有|未/.test(sentence.slice(Math.max(0, hit.index - 8), hit.index + hit[0].length))) continue;
let text = sentence;
if (/[::]\s*$/.test(line.trim()) && sentence === sentences.at(-1)) {
for (let j = i + 1; j < lines.length && j <= i + 8; j += 1) {
const next = lines[j]!.trim();
if (!next) { if (j > i + 1) break; continue; }
if (!/^([-*•]|\d+[.、)]|[((]?[一二三四五六七八九][))、])/.test(next)) break;
text += `\n${next}`;
}
}
out.push({ quote: hit[0], text });
}
}
return out;
}
/**
* Items of a claim window: split on 、,;, list lines. `described` = the item sits inside
* parentheses, where a placement usually describes the item before it (「AmK(月亮落第 5 宫)」).
*/
function splitItems(text: string): Array<{ text: string; described: boolean }> {
const items: Array<{ text: string; described: boolean }> = [];
let depth = 0;
let current = "";
let described = false;
const flush = () => {
if (current.trim()) items.push({ text: current.trim(), described });
current = "";
described = depth > 0;
};
for (const ch of text) {
if (ch === "(" || ch === "(") { depth += 1; flush(); continue; }
if ((ch === ")" || ch === ")") && depth > 0) { depth -= 1; flush(); continue; }
if (/[、,,;;\n]/.test(ch)) { flush(); continue; }
current += ch;
}
flush();
return items;
}
/**
* Within one claim window, split into items (、,;, list lines). A fact named in two or more
* different items — each time through one of its layers — is one fact counted as several
* agreeing pieces of evidence. Several layers inside one item (「A10 与 AL 的主星同为木星」)
* are one item and are not flagged.
*/
export function sameSourceFlags(answer: string, independence: Independence | undefined): { claims: number; flags: SameSourceFlag[] } {
const found = windows(answer);
const flags: SameSourceFlag[] = [];
if (!independence) return { claims: found.length, flags };
for (const { text } of found) {
if (SAYS_SAME_SOURCE.test(text)) continue;
const items = splitItems(text);
for (const [fact, layers] of Object.entries(independence.facts)) {
const planets = factPlanets(fact);
if (!planets.length) continue;
const hits: Array<{ item: number; layer: string }> = [];
items.forEach(({ text: item, described }, index) => {
const named = planets.every((planet) => mentions(item, planet));
for (const layer of layers) {
const cue = cueFor(layer);
if (cue && cue.pattern.test(item) && (cue.identifies || named)) hits.push({ item: index, layer });
}
// The D1 placement itself, written out (「金星同在 2 宫」「土星落天秤」).
const placement = /^D1:([A-Z][a-z]+)@([A-Z][a-z]+)\/(\d+)$/.exec(fact);
if (placement && named && !described && hits.every((hit) => hit.item !== index)) {
const [, planet, sign, house] = placement;
const zh = PLANET_ZH[planet!] ?? planet!;
const at = new RegExp(`${zh}[^,。;、((]{0,6}(落|在|位于|坐|入)[^,。;、((]{0,6}((第\\s*)?${house}\\s*宫(?![主头])|${SIGN_ZH[sign!] ?? sign!})`);
if (at.test(item)) hits.push({ item: index, layer: "D1 落点" });
}
});
const itemsHit = new Set(hits.map((hit) => hit.item));
const layersHit = [...new Set(hits.map((hit) => hit.layer))];
if (itemsHit.size >= 2 && layersHit.length >= 2) flags.push({ quote: text.slice(0, 160), fact, layers: layersHit });
}
}
return { claims: found.length, flags };
}
/** The domain's index from a captured model payload: the card's own, or recomputed from its base + section. */
export function independenceFromPayload(payload: unknown, domain: string): Independence | undefined {
const requests = Array.isArray(payload) ? payload as Json[] : [];
for (const request of requests.toReversed()) {
const messages = ((request.body as Json | undefined)?.messages ?? []) as Json[];
for (const message of messages) {
if (message.role !== "tool" || typeof message.content !== "string") continue;
let view: Json;
try { view = JSON.parse(message.content) as Json; } catch { continue; }
const cards = Array.isArray(view.claim_cards) ? view.claim_cards as Json[] : [];
const natal = cards.find((card) => card.category === "natal_foundation")?.evidence as Json | undefined;
const timing = cards.find((card) => card.category === "timing")?.evidence as Json | undefined;
const sections = cards.find((card) => card.category === "domain")?.evidence as Record<string, Json> | undefined;
const section = sections?.[domain];
if (!natal || !timing || !section) continue;
if (section.independence) return section.independence as Independence;
const base = (section.base as Json | undefined) ?? { natal, timing };
return evidenceCardIndependence(section as never, base as never) as Independence | undefined;
}
}
return undefined;
}
function answerBody(path: string): string {
const text = readFileSync(path, "utf8");
const at = text.indexOf("\n---\n\n");
return at >= 0 ? text.slice(at + 6).trim() : text;
}
if (import.meta.url === `file://${process.argv[1]}`) {
for (const path of process.argv.slice(2)) {
const domain = /([a-z]+)-run\d+\.md$/.exec(path)?.[1] ?? "";
const payload = JSON.parse(readFileSync(path.replace(/\.md$/, ".payload.json"), "utf8"));
const result = sameSourceFlags(answerBody(path), independenceFromPayload(payload, domain));
console.log(JSON.stringify({ answer: path, domain, ...result }));
}
}