Skip to content

Commit 477f834

Browse files
anandgupta42claude
andcommitted
fix(answer-commit): ignore float-noise cells when inferring column scale
- Cells with more than 6 decimals that do not survive 12-significant-digit rounding no longer raise the inferred column scale. - Noise cells render at the inferred scale, or in their own short form when no other cell sets a scale. - Add regression tests for a noise cell among clean cells, a noise-only column, and genuine high-scale values. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 54efbd1 commit 477f834

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,22 @@ export function renderAnswer(payload: unknown): string {
212212
if (args.rows) {
213213
// altimate_change start - render the requested format before applying the shared size cap
214214
const { columns, values, precision, format = "markdown" } = args.rows
215+
// Cells with float noise (more than 6 decimals that do not survive 12-significant-digit rounding) must not widen the column scale.
216+
const isNoise = (value: number) => {
217+
const decimal = new Decimal(value)
218+
return decimal.decimalPlaces() > 6 && !decimal.equals(decimal.toSignificantDigits(12))
219+
}
215220
const scales = columns.map((_, index) =>
216221
values.reduce((scale, cells) => {
217222
const value = cells[index]
218-
return typeof value === "number" ? Math.max(scale, new Decimal(value).decimalPlaces()) : scale
223+
return typeof value === "number" && !isNoise(value) ? Math.max(scale, new Decimal(value).decimalPlaces()) : scale
219224
}, 0),
220225
)
226+
const inferred = (value: number, index: number) => {
227+
const decimal = new Decimal(value)
228+
if (!isNoise(value)) return decimal.toFixed(scales[index]!)
229+
return scales[index]! > 0 ? decimal.toFixed(scales[index]!) : decimal.toFixed()
230+
}
221231
const row = (cells: (string | number | null)[]) => {
222232
if (format === "markdown") return `| ${cells.map(tableCell).join(" | ")} |`
223233
if (format === "lines") return cells.map(String).join(" ")
@@ -238,7 +248,7 @@ export function renderAnswer(payload: unknown): string {
238248
const places =
239249
precision && Object.hasOwn(precision, columns[index]!) ? precision[columns[index]!] : undefined
240250
if (typeof value !== "number") return value
241-
return places !== undefined ? value.toFixed(places) : new Decimal(value).toFixed(scales[index]!)
251+
return places !== undefined ? value.toFixed(places) : inferred(value, index)
242252
}),
243253
),
244254
),

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,36 @@ describe("answer_commit display precision", () => {
418418
expect((await tool.execute(payload, context)).output).toBe(expected)
419419
})
420420

421+
test("float-noise cells do not widen the inferred column scale", async () => {
422+
const payload = {
423+
basis: recordBasis,
424+
answer_only: true,
425+
rows: { columns: ["Value"], values: [[0.30000000000000004], [5], [4.18], [18.440000534057617]] },
426+
}
427+
const expected = "| Value |\n|---|\n| 0.30 |\n| 5.00 |\n| 4.18 |\n| 18.44 |"
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("a column of only float-noise cells renders each in its own form", () => {
434+
const payload = {
435+
basis: recordBasis,
436+
answer_only: true,
437+
rows: { columns: ["Value"], values: [[0.30000000000000004], [7]], format: "lines" as const },
438+
}
439+
expect(AnswerCommit.renderAnswer(payload)).toBe("0.30000000000000004\n7")
440+
})
441+
442+
test("genuine short high-scale values still set the column scale", () => {
443+
const payload = {
444+
basis: recordBasis,
445+
answer_only: true,
446+
rows: { columns: ["Value"], values: [[1.5], [0.1234567]], format: "lines" as const },
447+
}
448+
expect(AnswerCommit.renderAnswer(payload)).toBe("1.5000000\n0.1234567")
449+
})
450+
421451
test("inferred decimal scale supports small scientific values", async () => {
422452
const payload = {
423453
basis: recordBasis,

0 commit comments

Comments
 (0)