diff --git a/packages/diffci-cli/src/client/command-batches.ts b/packages/diffci-cli/src/client/command-batches.ts index 9770127..f0c04a4 100644 --- a/packages/diffci-cli/src/client/command-batches.ts +++ b/packages/diffci-cli/src/client/command-batches.ts @@ -1,6 +1,6 @@ import { commandArguments } from "./selected-coverage.js"; import { mergeTestFileResults } from "./test-results.js"; -import { measureCommand, type CommandMeasurement } from "./verify-savings.js"; +import { measureCommand, tail as tailBytes, type CommandMeasurement } from "./verify-savings.js"; /** * Selected-test commands too long for the shell (2026-10-21). @@ -139,7 +139,8 @@ export function measureCommands(commands: readonly string[], options: { cwd: str const failingTests = [...new Set(parts.flatMap((part) => part.failingTests ?? []))].sort(); const counted = ranAll && parts.every((part) => part.runnerTestCount !== undefined); const testResults = mergeTestFileResults(...parts.map((part) => part.testResults)); - const tail = (key: "stdoutTail" | "stderrTail") => parts.map((part) => part[key]).join("").slice(-Math.max(options.tailBytes, 0) || undefined); + // The same byte-bounded cut as a single command's tail: by UTF-8 bytes, at a character boundary. + const tail = (key: "stdoutTail" | "stderrTail") => tailBytes(parts.map((part) => part[key]).join(""), options.tailBytes); return { command: commands.join(" && "), exitCode: failed ? failed.exitCode : ranAll ? 0 : null, diff --git a/packages/diffci-cli/src/client/verify-savings.ts b/packages/diffci-cli/src/client/verify-savings.ts index 110bd7f..e638c55 100644 --- a/packages/diffci-cli/src/client/verify-savings.ts +++ b/packages/diffci-cli/src/client/verify-savings.ts @@ -177,9 +177,19 @@ interface ResolvedSelection { observerVersion?: string; } -function tail(value: string, bytes: number): string { - if (Buffer.byteLength(value, "utf8") <= bytes) return value; - return value.slice(-bytes); +/** + * At most the last `bytes` bytes of `value`, cut at a character boundary. `bytes` 0 keeps nothing: + * `value.slice(-0)` is the whole string, so `--tail-bytes 0` used to keep the entire output, and a + * multi-byte tail was cut by characters, not bytes. + */ +export function tail(value: string, bytes: number): string { + if (!(bytes > 0)) return ""; + const buffer = Buffer.from(value, "utf8"); + if (buffer.length <= bytes) return value; + let start = buffer.length - Math.floor(bytes); + // Skip UTF-8 continuation bytes so the tail never starts inside a character. + while (start < buffer.length && (buffer[start]! & 0xc0) === 0x80) start++; + return buffer.subarray(start).toString("utf8"); } function readSelectionFromObservation(path: string, override?: string): ResolvedSelection { diff --git a/packages/diffci-cli/tests/client/command-tail.test.ts b/packages/diffci-cli/tests/client/command-tail.test.ts new file mode 100644 index 0000000..fd5ce0a --- /dev/null +++ b/packages/diffci-cli/tests/client/command-tail.test.ts @@ -0,0 +1,32 @@ +import { strict as assert } from "node:assert"; +import { tmpdir } from "node:os"; +import { it } from "node:test"; + +import { measureCommands } from "../../src/client/command-batches.js"; +import { measureCommand } from "../../src/client/verify-savings.js"; + +const print = (text: string) => `node -e "process.stdout.write(${JSON.stringify(text).replace(/"/g, "'")}); process.stderr.write('err')"`; + +it("--tail-bytes 0 keeps no output, whether a selection runs as one command or several", () => { + const options = { cwd: tmpdir(), timeoutMs: 60_000, tailBytes: 0 }; + for (const measurement of [measureCommand(print("hello"), options), measureCommands([print("hello")], options), measureCommands([print("a"), print("b")], options)]) { + assert.equal(measurement.exitCode, 0, measurement.stderrTail); + assert.equal(measurement.stdoutTail, ""); + assert.equal(measurement.stderrTail, ""); + } +}); + +it("a tail is at most tailBytes bytes and never starts inside a character", () => { + const measurement = measureCommand(print("abc\u00e9\u00e9\u00e9"), { cwd: tmpdir(), timeoutMs: 60_000, tailBytes: 3 }); + assert.equal(measurement.stdoutTail, "\u00e9"); + assert.ok(Buffer.byteLength(measurement.stdoutTail, "utf8") <= 3); + assert.equal(measureCommand(print("abcdef"), { cwd: tmpdir(), timeoutMs: 60_000, tailBytes: 4 }).stdoutTail, "cdef"); +}); + +it("a selection split into several commands keeps a tail of at most tailBytes bytes too", () => { + const measurement = measureCommands([print("abc"), print("\u00e9\u00e9\u00e9")], { cwd: tmpdir(), timeoutMs: 60_000, tailBytes: 3 }); + assert.equal(measurement.exitCode, 0, measurement.stderrTail); + // "abc" + "\u00e9" is 5 bytes and its last 3 are "c\u00e9". Before, the joined parts were cut by characters. + assert.equal(measurement.stdoutTail, "c\u00e9"); + assert.ok(Buffer.byteLength(measurement.stdoutTail, "utf8") <= 3); +}); diff --git a/release-files.json b/release-files.json index 685f2f1..3554cd9 100644 --- a/release-files.json +++ b/release-files.json @@ -401,6 +401,7 @@ "packages/diffci-cli/tests/client/check-plan-cli.test.ts", "packages/diffci-cli/tests/client/plan-cli.test.ts", "packages/diffci-cli/tests/client/cli-help.test.ts", + "packages/diffci-cli/tests/client/command-tail.test.ts", "packages/diffci-cli/tests/client/check-plan-review.test.ts", "packages/diffci-cli/tests/client/check-plan.test.ts", "packages/diffci-cli/tests/client/red-builds.test.ts",