-
Notifications
You must be signed in to change notification settings - Fork 106
Recover translation-bench checkpoints after interrupted writes #2769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dominic Nguyen (datduyng)
merged 12 commits into
main
from
domnguyen/list-determiner-listname
Aug 21, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
28e2c9a
Reject placeholder list names from grammar matches
datduyng 49a9c69
style: apply prettier formatting and policy fixes
typeagent-bot[bot] e6a8526
Harden listName normalize/reject for determiner captures
datduyng d2c9336
style: apply prettier formatting and policy fixes
typeagent-bot[bot] fb8cb39
Merge remote-tracking branch 'origin/main' into domnguyen/repurpose-s…
datduyng f108b60
Remove superseded list agent changes
datduyng cdc601c
Make translation bench checkpoints crash-safe
datduyng fddbcca
docs: regenerate README.AUTOGEN.md, command reference, and action bro…
typeagent-bot[bot] 025b7da
refactor: reuse checkpoint line parser
datduyng 1705030
Remove unused checkpoint merge helper
datduyng 2bc9e5e
Merge remote-tracking branch 'origin/main' into domnguyen/repurpose-s…
datduyng ebbc0c7
Merge branch 'main' into domnguyen/list-determiner-listname
datduyng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
ts/packages/benchmarks/src/translationBench/synthesizer/jsonlCheckpoint.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
|
|
||
| import { splitTranslationBenchCheckpointLines } from "../runner/scale.js"; | ||
|
|
||
| function fsyncDirectory(filePath: string): void { | ||
| if (process.platform === "win32") return; | ||
| const directory = fs.openSync(path.dirname(filePath), "r"); | ||
| try { | ||
| fs.fsyncSync(directory); | ||
| } finally { | ||
| fs.closeSync(directory); | ||
| } | ||
| } | ||
|
|
||
| function writeAll(handle: number, buffer: Buffer, position: number): void { | ||
| let offset = 0; | ||
| while (offset < buffer.length) { | ||
| const written = fs.writeSync( | ||
| handle, | ||
| buffer, | ||
| offset, | ||
| buffer.length - offset, | ||
| position + offset, | ||
| ); | ||
| if (written === 0) throw new Error("Unable to complete JSONL write"); | ||
| offset += written; | ||
| } | ||
| } | ||
|
|
||
| /** Initializes a JSONL file owned by a single writer. */ | ||
| export function initializeSyncedJsonlFile( | ||
| filePath: string, | ||
| firstRecord: string, | ||
| ): void { | ||
| const temporaryPath = `${filePath}.tmp`; | ||
| let handle: number | undefined; | ||
| try { | ||
| handle = fs.openSync(temporaryPath, "w"); | ||
| fs.writeFileSync(handle, `${firstRecord}\n`, "utf8"); | ||
| fs.fsyncSync(handle); | ||
| } finally { | ||
| if (handle !== undefined) fs.closeSync(handle); | ||
| } | ||
| fs.renameSync(temporaryPath, filePath); | ||
| fsyncDirectory(filePath); | ||
| } | ||
|
|
||
| export function readRecoverableJsonlLines(filePath: string): string[] { | ||
| return splitTranslationBenchCheckpointLines( | ||
| fs.readFileSync(filePath, "utf8"), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Repairs a torn final line and appends records for one owning writer. | ||
| * Concurrent calls for the same path are not supported. | ||
| */ | ||
| export function appendSyncedJsonlRecords( | ||
| filePath: string, | ||
| records: readonly string[], | ||
| ): void { | ||
| if (records.length === 0) return; | ||
| const handle = fs.openSync(filePath, "r+"); | ||
| try { | ||
| const content = fs.readFileSync(handle); | ||
| let appendOffset = content.length; | ||
| let separator = ""; | ||
| if (appendOffset > 0 && content.at(-1) !== 0x0a) { | ||
| const lastNewline = content.lastIndexOf(0x0a); | ||
| if (lastNewline < 0) { | ||
| throw new Error( | ||
| `JSONL file '${filePath}' has no complete line`, | ||
| ); | ||
| } | ||
| const tail = content.subarray(lastNewline + 1).toString("utf8"); | ||
| try { | ||
| JSON.parse(tail); | ||
| separator = "\n"; | ||
| } catch { | ||
| appendOffset = lastNewline + 1; | ||
| fs.ftruncateSync(handle, appendOffset); | ||
| } | ||
| } | ||
| const payload = Buffer.from( | ||
| separator + records.map((record) => `${record}\n`).join(""), | ||
| ); | ||
| writeAll(handle, payload, appendOffset); | ||
| fs.fsyncSync(handle); | ||
| } finally { | ||
| fs.closeSync(handle); | ||
| } | ||
| } | ||
58 changes: 40 additions & 18 deletions
58
ts/packages/benchmarks/test/translationBench.checkpointPrimitives.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,49 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { describe, expect, it } from "@jest/globals"; | ||
|
|
||
| import { afterAll, describe, expect, it } from "@jest/globals"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { | ||
| appendTranslationBenchCheckpointRows, | ||
| createTranslationBenchRunFingerprint, | ||
| getTranslationBenchShardIndex, | ||
| splitTranslationBenchCheckpointLines, | ||
| } from "../src/translationBench/runner/scale.js"; | ||
| readTranslationBenchCheckpoint, | ||
| type TranslationBenchCheckpointHeader, | ||
| type TranslationBenchCheckpointRow, | ||
| } from "../src/translationBench/synthesizer/generationSupport.js"; | ||
|
|
||
| describe("translation bench checkpoint primitives", () => { | ||
| it("uses canonical fingerprints and stable shards", () => { | ||
| expect(createTranslationBenchRunFingerprint({ b: 2, a: 1 })).toBe( | ||
| createTranslationBenchRunFingerprint({ a: 1, b: 2 }), | ||
| ); | ||
| expect(getTranslationBenchShardIndex("case-1", 8)).toBe( | ||
| getTranslationBenchShardIndex("case-1", 8), | ||
| ); | ||
| }); | ||
| const directory = fs.mkdtempSync(path.join(os.tmpdir(), "translation-bench-")); | ||
| afterAll(() => fs.rmSync(directory, { recursive: true, force: true })); | ||
| const header: TranslationBenchCheckpointHeader = { | ||
| kind: "translation-bench-checkpoint", | ||
| version: 1, | ||
| runFingerprint: createTranslationBenchRunFingerprint({ run: 1 }), | ||
| settings: { model: "test" }, | ||
| shardIndex: 0, | ||
| shardCount: 1, | ||
| }; | ||
| const row = (caseId: string): TranslationBenchCheckpointRow<string> => ({ | ||
| kind: "translation-bench-row", | ||
| version: 1, | ||
| phase: "generate", | ||
| model: "test", | ||
| scenario: "default", | ||
| caseId, | ||
| value: caseId, | ||
| }); | ||
|
|
||
| it("drops only an incomplete trailing JSONL row", () => { | ||
| describe("translation bench checkpoints", () => { | ||
| it("recovers a torn final row before appending", () => { | ||
| const checkpointPath = path.join(directory, "checkpoint.jsonl"); | ||
| appendTranslationBenchCheckpointRows(checkpointPath, header, [ | ||
| row("1"), | ||
| ]); | ||
| fs.appendFileSync(checkpointPath, '{"kind":"translation-bench-row"'); | ||
| appendTranslationBenchCheckpointRows(checkpointPath, header, [ | ||
| row("2"), | ||
| ]); | ||
| expect( | ||
| splitTranslationBenchCheckpointLines('{"header":1}\n{"row":'), | ||
| ).toEqual(['{"header":1}']); | ||
| readTranslationBenchCheckpoint<string>(checkpointPath).rows, | ||
| ).toEqual([row("1"), row("2")]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.