Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/scoring/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ export async function getOrCreateScoringModelSnapshot(env: Env): Promise<Scoring
export function parsePythonNumberConstants(source: string, options: { knownOnly?: boolean } = { knownOnly: true }): Record<string, number> {
const constants: Record<string, number> = {};
for (const line of source.split("\n")) {
// Match Python numeric literals including underscore separators (1_000_000), floats, and exponents
// (1e-9, 5.8e1). The previous /[-+]?\d+(?:\.\d+)?/ stopped at `_`/`e`, truncating 1_000_000 -> 1 and
// 1e-9 -> 1, which silently misparsed any such upstream constant and polluted the unmodeled list (#810).
const match = line.match(/^([A-Z][A-Z0-9_]+)\s*=\s*([-+]?(?:\d[\d_]*\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)/);
// Match Python numeric literals including underscore separators (1_000_000, 0.000_001), floats, and
// exponents (1e-9, 5.8e1). PEP 515 allows underscores between digits in ANY part of the literal, so the
// fractional digits accept `_` too — the previous frac classes (`\d*`/`\.\d+`) stopped at the first
// fractional underscore, truncating 0.000_001 -> 0. The earlier /[-+]?\d+(?:\.\d+)?/ also stopped at
// `_`/`e` (1_000_000 -> 1, 1e-9 -> 1). Both silently misparsed upstream constants (#810).
const match = line.match(/^([A-Z][A-Z0-9_]+)\s*=\s*([-+]?(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][-+]?\d+)?)/);
if (!match) continue;
const name = match[1]!;
const raw = match[2]!;
Expand Down
15 changes: 15 additions & 0 deletions test/unit/scoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ OSS_EMISSION_SHARE = 0.90
expect(parsed.OSS_EMISSION_SHARE).toBe(0.9);
});

it("parses underscores in the FRACTIONAL part (PEP 515) without truncating to zero", () => {
const parsed = parsePythonNumberConstants(
`
SMALL_RATE = 0.000_001
PRECISE_SCALE = 3.14_15
MIXED_GROUPED = 1_000.000_5
`,
{ knownOnly: false },
);
// Frac classes were \\d*/\\.\\d+ (no `_`), so 0.000_001 stopped at "0.000" -> 0 (a divide/multiply hazard).
expect(parsed.SMALL_RATE).toBe(0.000001);
expect(parsed.PRECISE_SCALE).toBe(3.1415);
expect(parsed.MIXED_GROUPED).toBe(1000.0005);
});

it("flags only scoring snapshots older than the freshness window as stale (#810)", () => {
const now = Date.parse("2026-06-21T12:00:00.000Z");
const justFresh = new Date(now - SCORING_SNAPSHOT_STALE_MS + 60_000).toISOString();
Expand Down
Loading