fix(workbench): accept a JSON-number decimal and deliver the source's exact string
CI (SIAX Cloud) / sonar (pull_request) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
CI (SIAX Cloud) / integration (push) Skipped
masterplan-lock / check (push) Successful in 4s
masterplan-lock / check (pull_request) Successful in 8s
CI (SIAX Cloud) / security (push) Successful in 17s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 24s
CI (SIAX Cloud) / security (pull_request) Successful in 25s
CI (SIAX Cloud) / integration (pull_request) Successful in 33s
Security Scan (SIAX Cloud) / security (push) Successful in 33s
CI (SIAX Cloud) / quality (push) Successful in 54s
CI (SIAX Cloud) / quality (pull_request) Successful in 58s
CI (SIAX Cloud) / sonar (pull_request) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
CI (SIAX Cloud) / integration (push) Skipped
masterplan-lock / check (push) Successful in 4s
masterplan-lock / check (pull_request) Successful in 8s
CI (SIAX Cloud) / security (push) Successful in 17s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 24s
CI (SIAX Cloud) / security (pull_request) Successful in 25s
CI (SIAX Cloud) / integration (pull_request) Successful in 33s
Security Scan (SIAX Cloud) / security (push) Successful in 33s
CI (SIAX Cloud) / quality (push) Successful in 54s
CI (SIAX Cloud) / quality (pull_request) Successful in 58s
The local model returns 12500.00 as a JSON number, which the contract rejects as a non-string decimal. A numeric value now matches a source span numerically and the delivered value is rewritten to that span's decimal string (12500.00), so precision is preserved instead of rounded. Exact string equality is still required for string decimals, and ambiguous formats still go to review.
This commit is contained in:
@@ -15,9 +15,10 @@ function collectMatches(
|
||||
text: string,
|
||||
field: OutputFieldContract,
|
||||
raw: unknown,
|
||||
): Array<{ start: number; end: number }> {
|
||||
const matches: Array<{ start: number; end: number }> = [];
|
||||
const push = (start: number, end: number) => matches.push({ start, end });
|
||||
): Array<{ start: number; end: number; normalized?: string }> {
|
||||
const matches: Array<{ start: number; end: number; normalized?: string }> = [];
|
||||
const push = (start: number, end: number, normalized?: string) =>
|
||||
matches.push(normalized === undefined ? { start, end } : { start, end, normalized });
|
||||
if (field.type === 'string') {
|
||||
if (typeof raw !== 'string' || raw.length === 0) return [];
|
||||
let index = text.indexOf(raw);
|
||||
@@ -35,11 +36,17 @@ function collectMatches(
|
||||
return matches;
|
||||
}
|
||||
if (field.type === 'decimal') {
|
||||
if (typeof raw !== 'string') return [];
|
||||
if (typeof raw !== 'string' && !(typeof raw === 'number' && Number.isFinite(raw))) return [];
|
||||
for (const match of text.matchAll(/[+-]?\d+(?:[\s\u00a0\u202f.,]\d+)*/g)) {
|
||||
if (decimalCandidates(match[0]).includes(raw)) {
|
||||
push(match.index!, match.index! + match[0].length);
|
||||
const candidates = decimalCandidates(match[0]);
|
||||
if (typeof raw === 'string') {
|
||||
if (candidates.includes(raw)) push(match.index!, match.index! + match[0].length);
|
||||
continue;
|
||||
}
|
||||
// A JSON number lost the source formatting. Accept a numeric match and
|
||||
// deliver the SOURCE's decimal string, so precision is never rounded.
|
||||
const matched = candidates.find((candidate) => Number(candidate) === raw);
|
||||
if (matched !== undefined) push(match.index!, match.index! + match[0].length, matched);
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
@@ -63,6 +70,30 @@ function collectMatches(
|
||||
return matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic type coercion for values a local model typed differently.
|
||||
* The contract is about the VALUE, not the JSON type: a decimal delivered as
|
||||
* a JSON number and an integer delivered as a digit string are the same value.
|
||||
* Anything that does not coerce cleanly is left untouched and fails validation.
|
||||
*/
|
||||
function coerceFieldValues(
|
||||
value: Record<string, unknown>,
|
||||
fields: readonly OutputFieldContract[],
|
||||
): Record<string, unknown> {
|
||||
const out: Record<string, unknown> = { ...value };
|
||||
for (const field of fields) {
|
||||
if (!Object.prototype.hasOwnProperty.call(out, field.name)) continue;
|
||||
const raw = out[field.name];
|
||||
if (field.type === 'integer' && typeof raw === 'string' && /^-?(0|[1-9]\d*)$/.test(raw.trim())) {
|
||||
const parsed = Number(raw.trim());
|
||||
if (Number.isSafeInteger(parsed)) out[field.name] = parsed;
|
||||
} else if (field.type === 'boolean' && (raw === 'true' || raw === 'false')) {
|
||||
out[field.name] = raw === 'true';
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic evidence for values the model returned.
|
||||
*
|
||||
@@ -85,8 +116,10 @@ function evidenceFromValues(
|
||||
}
|
||||
const raw = value[field.name];
|
||||
const matches: FieldEvidence[] = [];
|
||||
let normalized = raw;
|
||||
for (const source of sources) {
|
||||
for (const span of collectMatches(source.text, field, raw)) {
|
||||
if (span.normalized !== undefined) normalized = span.normalized;
|
||||
matches.push({
|
||||
field: field.name,
|
||||
sourceId: source.id,
|
||||
@@ -98,6 +131,8 @@ function evidenceFromValues(
|
||||
}
|
||||
if (matches.length !== 1) return undefined;
|
||||
evidence.push(matches[0]!);
|
||||
const out = value;
|
||||
out[field.name] = normalized;
|
||||
}
|
||||
return evidence;
|
||||
}
|
||||
@@ -218,6 +253,7 @@ export async function structureDocument(input: {
|
||||
asObject = candidate;
|
||||
}
|
||||
if (!asObject) return { status: 'needs_review', issues: [{ field: '', code: 'invalid_output' }], model };
|
||||
asObject = coerceFieldValues(asObject, snapshot.fields);
|
||||
|
||||
// 1. Trust model-supplied evidence only if it validates as-is.
|
||||
if (Array.isArray((candidate as { evidence?: unknown }).evidence)) {
|
||||
|
||||
Reference in New Issue
Block a user