Skip to content

Commit 54efbd1

Browse files
anandgupta42claude
andcommitted
fix(answer-commit): infer column scale and accept finite decimals
- Infer the maximum decimal scale per numeric column (by position) when `rows.precision` is omitted, so `5` and `4.18` render as `5.00` and `4.18` in markdown, csv, tsv and lines output. - Explicit `rows.precision` still overrides inference. - Drop the 15-significant-digit limit on numeric values; unsafe integers are still rejected and NaN/Infinity are still rejected by Zod. - Update the tool and schema descriptions and add regression tests. Part of #1481 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d8cf208 commit 54efbd1

2 files changed

Lines changed: 110 additions & 23 deletions

File tree

‎packages/opencode/src/altimate/tools/answer-commit.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import z from "zod"
2+
import { Decimal } from "decimal.js"
23
import { Effect, Option, Schema, SchemaIssue } from "effect"
34
import type { JSONSchema7 } from "@ai-sdk/provider"
45
import { Tool } from "../../tool/tool"
@@ -23,15 +24,10 @@ function optional<T extends z.ZodType>(schema: T) {
2324
})
2425
}
2526

26-
const number = z
27-
.number()
28-
.refine(
29-
(value) => {
30-
const digits = value.toExponential().split("e")[0]!.replace(/[-.]/g, "").length
31-
return digits <= 15 && (!Number.isInteger(value) || Number.isSafeInteger(value))
32-
},
33-
"Numeric values must have at most 15 significant digits and integers must be safe; pass the exact value as a string",
34-
)
27+
const number = z.number().refine(
28+
(value) => !Number.isInteger(value) || Number.isSafeInteger(value),
29+
"Numeric integers must be safe; pass the exact value as a string",
30+
)
3531

3632
const precisionRange = "precision must be an integer from 0 to 10 (inclusive)"
3733
const precision = z.number({ error: precisionRange }).int(precisionRange).min(0, precisionRange).max(10, precisionRange)
@@ -64,7 +60,7 @@ const rows = z
6460
"Concrete result rows; when the request names N, the row count must equal N, even with a tie at the top-N boundary",
6561
),
6662
precision: optional(z.record(z.string(), precision)).describe(
67-
"Display decimal places by column name: integer from 0 to 10 (inclusive). Numeric cells use fixed decimals, including integers; strings and nulls are unchanged. Omitted columns keep their default formatting.",
63+
"Display decimal places by column name: integer from 0 to 10 (inclusive). Numeric cells use fixed decimals, including integers; strings and nulls are unchanged. Omitted columns use the maximum decimal scale of their numeric cells.",
6864
),
6965
note: optional(z.string()).describe(
7066
"Brief result note, including any tie at the top-N boundary; omitted when answer_only is true",
@@ -216,6 +212,12 @@ export function renderAnswer(payload: unknown): string {
216212
if (args.rows) {
217213
// altimate_change start - render the requested format before applying the shared size cap
218214
const { columns, values, precision, format = "markdown" } = args.rows
215+
const scales = columns.map((_, index) =>
216+
values.reduce((scale, cells) => {
217+
const value = cells[index]
218+
return typeof value === "number" ? Math.max(scale, new Decimal(value).decimalPlaces()) : scale
219+
}, 0),
220+
)
219221
const row = (cells: (string | number | null)[]) => {
220222
if (format === "markdown") return `| ${cells.map(tableCell).join(" | ")} |`
221223
if (format === "lines") return cells.map(String).join(" ")
@@ -235,7 +237,8 @@ export function renderAnswer(payload: unknown): string {
235237
cells.map((value, index) => {
236238
const places =
237239
precision && Object.hasOwn(precision, columns[index]!) ? precision[columns[index]!] : undefined
238-
return typeof value === "number" && places !== undefined ? value.toFixed(places) : value
240+
if (typeof value !== "number") return value
241+
return places !== undefined ? value.toFixed(places) : new Decimal(value).toFixed(scales[index]!)
239242
}),
240243
),
241244
),
@@ -261,7 +264,7 @@ export const AnswerCommitTool = Tool.define(
261264
"answer_commit",
262265
Effect.succeed({
263266
// altimate_change start - expose format selection to the model before the terminal tool call
264-
description: `Deterministically finalize a data-question answer. Call this as the LAST tool call, once the answer is decided and all queries are finished. After it returns, reply with exactly the returned text and nothing else. Supply exactly one of fields, rows, or unresolved. Supply one field per item the request asks for, in the order asked: a request for 'the repository and the most copied file' needs two fields (one named for the repository, one for the file); never collapse a compound request into one field, and never add fields the request did not ask for. Numbers are accepted up to 15 significant digits; any decimal that must be reproduced exactly (more digits, trailing zeros, or currency strings) must be passed as a string. For fields.precision and each rows.precision value, use an integer from 0 to 10 (inclusive). Set rows.precision by column name to display numeric cells with fixed decimal places, including integers; strings are never changed. When the request names a tabular format, set rows.format accordingly: markdown (default), csv, tsv, or lines (one row per line with cells separated by a single space, without headers, for single-column or two-column plain lists). CSV/TSV include headers and quote cells containing the delimiter, quotes, or newlines, doubling embedded quotes; null cells are empty. Always supply \`basis\`; it is never shown when the request asks for only the answer. Supply one or two sentences, at most ${MAX_BASIS_CHARACTERS} characters, naming population, grain, the source object/column that supplies the requested entity or value, and caveats. Resolve policy eligibility separately from metric and entity attribution: a rule about a related object does not change which object supplies the requested entity. Say in \`basis\` which source object and column supply it. When the request names N, rows.values must contain exactly N concrete rows, even with a tie at the top-N boundary; note the tie briefly in rows.note. Use answer_only when the request asks for only the answer or a specific format: the reply is exactly the value(s) or table, with no basis, field names, note, or answer label. An unresolved answer retains its unable-to-determine status. Omit unused optional properties. ${renderedLengthGuidance} This tool is not available for JSON-schema requests; use StructuredOutput instead.`,
267+
description: `Deterministically finalize a data-question answer. Call this as the LAST tool call, once the answer is decided and all queries are finished. After it returns, reply with exactly the returned text and nothing else. Supply exactly one of fields, rows, or unresolved. Supply one field per item the request asks for, in the order asked: a request for 'the repository and the most copied file' needs two fields (one named for the repository, one for the file); never collapse a compound request into one field, and never add fields the request did not ask for. Numbers use JavaScript precision; integers must be safe. Pass exact decimal values, trailing zeros, or currency strings as strings. For fields.precision and each rows.precision value, use an integer from 0 to 10 (inclusive). Set rows.precision by column name to display numeric cells with fixed decimal places, including integers; strings are never changed. Without explicit precision, each column uses the maximum decimal scale of its numeric cells. When the request names a tabular format, set rows.format accordingly: markdown (default), csv, tsv, or lines (one row per line with cells separated by a single space, without headers, for single-column or two-column plain lists). CSV/TSV include headers and quote cells containing the delimiter, quotes, or newlines, doubling embedded quotes; null cells are empty. Always supply \`basis\`; it is never shown when the request asks for only the answer. Supply one or two sentences, at most ${MAX_BASIS_CHARACTERS} characters, naming population, grain, the source object/column that supplies the requested entity or value, and caveats. Resolve policy eligibility separately from metric and entity attribution: a rule about a related object does not change which object supplies the requested entity. Say in \`basis\` which source object and column supply it. When the request names N, rows.values must contain exactly N concrete rows, even with a tie at the top-N boundary; note the tie briefly in rows.note. Use answer_only when the request asks for only the answer or a specific format: the reply is exactly the value(s) or table, with no basis, field names, note, or answer label. An unresolved answer retains its unable-to-determine status. Omit unused optional properties. ${renderedLengthGuidance} This tool is not available for JSON-schema requests; use StructuredOutput instead.`,
265268
// altimate_change end
266269
parameters: Parameters,
267270
formatValidationError: (error: unknown) => (Schema.isSchemaError(error) ? error.message : String(error)),

‎packages/opencode/test/altimate/tools/answer-commit.test.ts‎

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ describe("answer_commit numeric safety", () => {
297297
test("description explains numeric precision and exact decimal strings", async () => {
298298
const tool = await initTool(AnswerCommit.AnswerCommitTool)
299299
expect(tool.description).toContain(
300-
"Numbers are accepted up to 15 significant digits; any decimal that must be reproduced exactly (more digits, trailing zeros, or currency strings) must be passed as a string.",
300+
"Numbers use JavaScript precision; integers must be safe. Pass exact decimal values, trailing zeros, or currency strings as strings.",
301301
)
302302
})
303303

@@ -315,17 +315,16 @@ describe("answer_commit numeric safety", () => {
315315
await rejects(tool.execute(payload, context), /rows.*values.*safe.*string/i)
316316
})
317317

318-
test.each(["1234567890123456.7", "9007199254740991.1", "-1234567890123456.7", "0.1234567890123456"])(
319-
"rejects precision-losing parsed number %s in fields and rows",
320-
async (literal) => {
321-
const value = JSON.parse(literal)
318+
test.each([NaN, Infinity, -Infinity])(
319+
"rejects nonfinite number %s in fields and rows",
320+
async (value) => {
322321
const tool = await initTool(AnswerCommit.AnswerCommitTool)
323322
for (const shape of [
324323
{ fields: [{ name: "", value }] },
325324
{ rows: { columns: ["Value"], values: [[value]] } },
326325
]) {
327326
const payload = { basis: recordBasis, ...shape }
328-
const error = /15 significant digits.*exact value as a string/i
327+
const error = /fields.*value|rows.*values/i
329328
expect(() => AnswerCommit.renderAnswer(payload)).toThrow(error)
330329
await rejects(tool.execute(payload, context), error)
331330
}
@@ -335,6 +334,12 @@ describe("answer_commit numeric safety", () => {
335334
test.each([
336335
999999999999999,
337336
-999999999999999,
337+
Number.MAX_SAFE_INTEGER,
338+
Number.MIN_SAFE_INTEGER,
339+
1234567890123456.7,
340+
-1234567890123456.7,
341+
0.1234567890123456,
342+
18.440000534057617,
338343
46919.15,
339344
12,
340345
0.1,
@@ -350,17 +355,96 @@ describe("answer_commit numeric safety", () => {
350355
const tool = await initTool(AnswerCommit.AnswerCommitTool)
351356
const fields = { basis: recordBasis, fields: [{ name: "", value }] }
352357
const rows = { basis: recordBasis, rows: { columns: ["Count"], values: [[value]] } }
358+
const renderedRow = value === -3.5e-7 ? "-0.00000035" : String(value)
353359
expect(AnswerCommit.renderAnswer(fields)).toBe(`${recordBasis}\n\n**Answer:** ${value}`)
354360
expect((await tool.execute(fields, context)).output).toBe(`${recordBasis}\n\n**Answer:** ${value}`)
355-
expect(AnswerCommit.renderAnswer(rows)).toBe(`${recordBasis}\n\n**Answer:**\n| Count |\n|---|\n| ${value} |`)
361+
expect(AnswerCommit.renderAnswer(rows)).toBe(`${recordBasis}\n\n**Answer:**\n| Count |\n|---|\n| ${renderedRow} |`)
356362
expect((await tool.execute(rows, context)).output).toBe(
357-
`${recordBasis}\n\n**Answer:**\n| Count |\n|---|\n| ${value} |`,
363+
`${recordBasis}\n\n**Answer:**\n| Count |\n|---|\n| ${renderedRow} |`,
358364
)
359365
},
360366
)
361367
})
362368

363369
describe("answer_commit display precision", () => {
370+
test.each([
371+
["markdown", "| Average |\n|---|\n| 5.00 |\n| 4.18 |"],
372+
["csv", "Average\n5.00\n4.18"],
373+
["tsv", "Average\n5.00\n4.18"],
374+
["lines", "5.00\n4.18"],
375+
])("%s infers a shared numeric column scale", async (format, expected) => {
376+
const payload = {
377+
basis: recordBasis,
378+
answer_only: true,
379+
rows: { format, columns: ["Average"], values: [[5], [4.18]] },
380+
}
381+
expect(AnswerCommit.renderAnswer(payload)).toBe(expected)
382+
const tool = await initTool(AnswerCommit.AnswerCommitTool)
383+
expect((await tool.execute(payload, context)).output).toBe(expected)
384+
})
385+
386+
test("infers column scales independently while preserving strings and nulls", async () => {
387+
const payload = {
388+
basis: recordBasis,
389+
answer_only: true,
390+
rows: {
391+
columns: ["Average", "Rate", "Count"],
392+
values: [
393+
[5, 2, 3],
394+
[4.18, 1.234, 4],
395+
["001.2000", null, "5.00"],
396+
],
397+
},
398+
}
399+
const expected = "| Average | Rate | Count |\n|---|---|---|\n| 5.00 | 2.000 | 3 |\n| 4.18 | 1.234 | 4 |\n| 001.2000 | null | 5.00 |"
400+
expect(AnswerCommit.renderAnswer(payload)).toBe(expected)
401+
const tool = await initTool(AnswerCommit.AnswerCommitTool)
402+
expect((await tool.execute(payload, context)).output).toBe(expected)
403+
})
404+
405+
test("explicit column precision overrides inference without changing other columns", async () => {
406+
const payload = {
407+
basis: recordBasis,
408+
answer_only: true,
409+
rows: {
410+
columns: ["Average", "Rate"],
411+
values: [[5, 2], [4.18, 1.234]],
412+
precision: { Average: 1 },
413+
},
414+
}
415+
const expected = "| Average | Rate |\n|---|---|\n| 5.0 | 2.000 |\n| 4.2 | 1.234 |"
416+
expect(AnswerCommit.renderAnswer(payload)).toBe(expected)
417+
const tool = await initTool(AnswerCommit.AnswerCommitTool)
418+
expect((await tool.execute(payload, context)).output).toBe(expected)
419+
})
420+
421+
test("inferred decimal scale supports small scientific values", async () => {
422+
const payload = {
423+
basis: recordBasis,
424+
answer_only: true,
425+
rows: { columns: ["Value"], values: [[1e-101], [0]] },
426+
}
427+
const expected = `| Value |\n|---|\n| 0.${"0".repeat(100)}1 |\n| 0.${"0".repeat(101)} |`
428+
expect(AnswerCommit.renderAnswer(payload)).toBe(expected)
429+
const tool = await initTool(AnswerCommit.AnswerCommitTool)
430+
expect((await tool.execute(payload, context)).output).toBe(expected)
431+
})
432+
433+
test.each([
434+
[18.440000534057617, "18.44"],
435+
[1.005, "1.00"],
436+
])("explicit precision rounds %s consistently in fields and rows", async (value, expected) => {
437+
const tool = await initTool(AnswerCommit.AnswerCommitTool)
438+
for (const shape of [
439+
{ fields: [{ name: "", value, precision: 2 }] },
440+
{ rows: { columns: ["Value"], values: [[value]], precision: { Value: 2 }, format: "lines" } },
441+
]) {
442+
const payload = { basis: recordBasis, answer_only: true, ...shape }
443+
expect(AnswerCommit.renderAnswer(payload)).toBe(expected)
444+
expect((await tool.execute(payload, context)).output).toBe(expected)
445+
}
446+
})
447+
364448
test("description and field and row schemas state the allowed precision range", async () => {
365449
const tool = await initTool(AnswerCommit.AnswerCommitTool)
366450
const range = /integer from 0 to 10 \(inclusive\)/i
@@ -471,13 +555,13 @@ describe("answer_commit display precision", () => {
471555
await rejects(tool.execute(payload, context), error)
472556
})
473557

474-
test("duplicate row column labels render unchanged without precision", async () => {
558+
test("duplicate row column labels infer scale independently by position", async () => {
475559
const payload = {
476560
basis: recordBasis,
477561
answer_only: true,
478-
rows: { columns: ["Amount", "Amount"], values: [[5, 1.234]] },
562+
rows: { columns: ["Amount", "Amount"], values: [[5, 1.234], [4.18, 2]] },
479563
}
480-
const expected = "| Amount | Amount |\n|---|---|\n| 5 | 1.234 |"
564+
const expected = "| Amount | Amount |\n|---|---|\n| 5.00 | 1.234 |\n| 4.18 | 2.000 |"
481565
expect(AnswerCommit.renderAnswer(payload)).toBe(expected)
482566
const tool = await initTool(AnswerCommit.AnswerCommitTool)
483567
expect((await tool.execute(payload, context)).output).toBe(expected)

0 commit comments

Comments
 (0)