Skip to content
Merged
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
5 changes: 3 additions & 2 deletions packages/diffci-cli/src/client/command-batches.ts
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 13 additions & 3 deletions packages/diffci-cli/src/client/verify-savings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions packages/diffci-cli/tests/client/command-tail.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions release-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading