Fix dark evidence resolution and improve trace readability

This commit is contained in:
dirtydishes 2026-04-27 15:52:44 -04:00
parent a45d5c85f6
commit 2229c8c09c
2 changed files with 103 additions and 9 deletions

View file

@ -473,7 +473,61 @@ const extractUnderlying = (contractId: string): string => {
const extractEquityTraceFromJoin = (joinId: string): string | null => { const extractEquityTraceFromJoin = (joinId: string): string | null => {
const match = joinId.match(/^equityjoin:(.+)$/); const match = joinId.match(/^equityjoin:(.+)$/);
return match?.[1] ?? null; if (match?.[1]) {
return match[1];
}
return joinId.trim().length > 0 ? joinId.trim() : null;
};
const normalizeJoinRefCandidates = (value: string): string[] => {
const ref = value.trim();
if (!ref) {
return [];
}
if (ref.startsWith("equityjoin:")) {
const rawTrace = ref.slice("equityjoin:".length);
return rawTrace ? [ref, rawTrace] : [ref];
}
return [ref, `equityjoin:${ref}`];
};
const resolveJoinFromRef = (
ref: string,
joins: Map<string, EquityPrintJoin>
): EquityPrintJoin | null => {
const candidates = normalizeJoinRefCandidates(ref);
for (const key of candidates) {
const match = joins.get(key);
if (match) {
return match;
}
}
return null;
};
const formatDarkTrace = (traceId: string): string => {
const normalized = traceId.trim();
if (!normalized) {
return "unknown";
}
if (normalized.startsWith("equityjoin:")) {
return normalized.slice("equityjoin:".length);
}
const parts = normalized.split(":").filter(Boolean);
if (parts.length < 2) {
return normalized;
}
const kind = parts[1]?.replace(/_/g, " ") ?? "event";
const remainder = parts.slice(2).join(" -> ");
if (!remainder) {
return kind;
}
return `${kind}: ${remainder}`;
}; };
const inferDarkUnderlying = ( const inferDarkUnderlying = (
@ -482,7 +536,7 @@ const inferDarkUnderlying = (
equityJoins: Map<string, EquityPrintJoin> equityJoins: Map<string, EquityPrintJoin>
): string | null => { ): string | null => {
for (const ref of event.evidence_refs) { for (const ref of event.evidence_refs) {
const join = equityJoins.get(ref); const join = resolveJoinFromRef(ref, equityJoins);
if (!join) { if (!join) {
continue; continue;
} }
@ -2935,7 +2989,7 @@ const DarkDrawer = ({ event, evidence, underlying, onClose }: DarkDrawerProps) =
<h4>Trace path</h4> <h4>Trace path</h4>
<div className="drawer-row"> <div className="drawer-row">
<div className="drawer-row-title">Event trace</div> <div className="drawer-row-title">Event trace</div>
<p className="drawer-note">{event.trace_id}</p> <p className="drawer-note">{formatDarkTrace(event.trace_id)}</p>
</div> </div>
{traceRefs.length === 0 ? ( {traceRefs.length === 0 ? (
<p className="drawer-empty">No evidence references attached.</p> <p className="drawer-empty">No evidence references attached.</p>
@ -2944,7 +2998,7 @@ const DarkDrawer = ({ event, evidence, underlying, onClose }: DarkDrawerProps) =
{traceRefs.map((ref) => ( {traceRefs.map((ref) => (
<div className="drawer-row" key={ref}> <div className="drawer-row" key={ref}>
<div className="drawer-row-title">Evidence ref</div> <div className="drawer-row-title">Evidence ref</div>
<p className="drawer-note">{ref}</p> <p className="drawer-note">{formatDarkTrace(ref)}</p>
</div> </div>
))} ))}
</div> </div>
@ -3386,15 +3440,23 @@ const useTerminalState = () => {
return; return;
} }
const missingIds = selectedDarkEvent.evidence_refs.filter((id) => !resolvedEquityJoinMap.has(id)); const missingIds = selectedDarkEvent.evidence_refs.filter(
(id) => resolveJoinFromRef(id, resolvedEquityJoinMap) === null
);
if (missingIds.length === 0) { if (missingIds.length === 0) {
return; return;
} }
incrementRetentionMetric("pinnedFetchMisses", missingIds.length); incrementRetentionMetric("pinnedFetchMisses", missingIds.length);
const url = new URL(buildApiUrl("/equity-joins/by-id")); const url = new URL(buildApiUrl("/equity-joins/by-id"));
const requested = new Set<string>();
for (const id of missingIds) { for (const id of missingIds) {
url.searchParams.append("id", id); for (const candidate of normalizeJoinRefCandidates(id)) {
if (!requested.has(candidate)) {
requested.add(candidate);
url.searchParams.append("id", candidate);
}
}
} }
void fetch(url.toString()) void fetch(url.toString())
.then(async (response) => { .then(async (response) => {
@ -3407,6 +3469,10 @@ const useTerminalState = () => {
const next = new Map<string, EquityPrintJoin>(); const next = new Map<string, EquityPrintJoin>();
for (const item of payload.data ?? []) { for (const item of payload.data ?? []) {
next.set(item.id, item); next.set(item.id, item);
next.set(item.trace_id, item);
if (item.print_trace_id) {
next.set(item.print_trace_id, item);
}
} }
if (next.size > 0) { if (next.size > 0) {
const now = Date.now(); const now = Date.now();
@ -3451,7 +3517,7 @@ const useTerminalState = () => {
} }
return selectedDarkEvent.evidence_refs.map((id) => { return selectedDarkEvent.evidence_refs.map((id) => {
const join = resolvedEquityJoinMap.get(id); const join = resolveJoinFromRef(id, resolvedEquityJoinMap);
if (join) { if (join) {
return { kind: "join", id, join }; return { kind: "join", id, join };
} }
@ -3782,7 +3848,9 @@ const useTerminalState = () => {
const keys = new Set<string>(); const keys = new Set<string>();
if (selectedDarkEvent) { if (selectedDarkEvent) {
for (const id of selectedDarkEvent.evidence_refs) { for (const id of selectedDarkEvent.evidence_refs) {
keys.add(id); for (const candidate of normalizeJoinRefCandidates(id)) {
keys.add(candidate);
}
} }
} }
return keys; return keys;

View file

@ -1317,8 +1317,34 @@ export const fetchEquityPrintJoinsByIds = async (
return []; return [];
} }
const joinIds = new Set<string>();
const printTraceIds = new Set<string>();
for (const id of uniqueIds) {
joinIds.add(id);
if (id.startsWith("equityjoin:")) {
const trace = id.slice("equityjoin:".length);
if (trace) {
printTraceIds.add(trace);
}
} else {
joinIds.add(`equityjoin:${id}`);
printTraceIds.add(id);
}
}
const joinIdList = Array.from(joinIds);
const printTraceList = Array.from(printTraceIds);
const whereParts = [
`id IN (${buildStringList(joinIdList)})`,
`trace_id IN (${buildStringList(joinIdList)})`
];
if (printTraceList.length > 0) {
whereParts.push(`print_trace_id IN (${buildStringList(printTraceList)})`);
}
const lookupLimit = clampLookupLimit(joinIdList.length + printTraceList.length);
const result = await client.query({ const result = await client.query({
query: `SELECT * FROM ${EQUITY_PRINT_JOINS_TABLE} WHERE id IN (${buildStringList(uniqueIds)}) ORDER BY source_ts DESC, seq DESC LIMIT ${clampLookupLimit(uniqueIds.length)}`, query: `SELECT * FROM ${EQUITY_PRINT_JOINS_TABLE} WHERE ${whereParts.join(" OR ")} ORDER BY source_ts DESC, seq DESC LIMIT ${lookupLimit}`,
format: "JSONEachRow" format: "JSONEachRow"
}); });