Repository navigation
Add CLI access to published benchmark reports #3
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,154 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { resolveManagementScope, type ManagementScopeOptions } from "../context/service.js"; | ||
| import { CliError } from "../errors.js"; | ||
| import { requestManagementJson } from "../management/client.js"; | ||
| import { resolveManagementSession } from "../management/session.js"; | ||
|
|
||
| const publicationSchema = z.object({ | ||
| id: z.string().min(1), | ||
| org_id: z.string().min(1), | ||
| project_id: z.string().min(1), | ||
| workload_id: z.string().min(1), | ||
| name: z.string().min(1), | ||
| artifact_sha256: z.string().regex(/^[a-f0-9]{64}$/), | ||
| model_count: z.number().int().positive(), | ||
| task_count: z.number().int().positive(), | ||
| best_score: z.number().min(0).max(1), | ||
| best_pass_rate: z.number().min(0).max(1), | ||
| published_at: z.string().min(1), | ||
| }); | ||
|
|
||
| const indexSchema = z.object({ | ||
| workloads: z.array(z.object({ | ||
| project_id: z.string().min(1), | ||
| project_name: z.string().min(1), | ||
| project_slug: z.string().min(1), | ||
| workload_id: z.string().min(1), | ||
| workload_name: z.string().min(1), | ||
| benchmark: publicationSchema.nullable(), | ||
| })), | ||
| }); | ||
|
|
||
| // Validate the identity and the fields used in the summary, while preserving | ||
| // every server-owned report field for the explicit full-report output. | ||
| const manifestSchema = z.object({ | ||
| id: z.string().min(1), | ||
| name: z.string().min(1), | ||
| project_id: z.string().min(1), | ||
| workload_id: z.string().min(1), | ||
| created_at: z.string().min(1), | ||
| models: z.array(z.object({ model: z.string().min(1) }).passthrough()).min(1), | ||
| report: z.object({ | ||
| recommendation: z.object({ | ||
| headline: z.string().min(1), | ||
| summary: z.string().min(1), | ||
| }).passthrough(), | ||
| arms: z.array(z.object({ | ||
| arm_id: z.string().min(1), | ||
| model: z.string().min(1), | ||
| display_name: z.string().min(1), | ||
| prompt: z.string().min(1), | ||
| role: z.enum(["current", "recommended", "compared"]), | ||
| score: z.number().min(0).max(1), | ||
| }).passthrough()).min(2), | ||
| prompt_change: z.object({ | ||
| diff: z.array(z.object({ | ||
| op: z.enum(["equal", "insert", "delete", "replace"]), | ||
| before: z.array(z.string()), | ||
| after: z.array(z.string()), | ||
| }).passthrough()).min(1), | ||
| }).passthrough().optional(), | ||
| }).passthrough().optional(), | ||
| }).passthrough(); | ||
|
|
||
| const detailSchema = z.object({ | ||
| benchmark: manifestSchema.nullable(), | ||
| publication: publicationSchema.nullable(), | ||
| }); | ||
|
|
||
| type Publication = z.infer<typeof publicationSchema>; | ||
| type IndexRow = z.infer<typeof indexSchema>["workloads"][number]; | ||
| type Manifest = z.infer<typeof manifestSchema>; | ||
|
|
||
| export interface BenchmarkListResult { | ||
| workloads: IndexRow[]; | ||
| } | ||
|
|
||
| export interface BenchmarkShowInput { | ||
| project?: string; | ||
| workload?: string; | ||
| includeReport?: boolean; | ||
| } | ||
|
|
||
| export interface BenchmarkShowResult { | ||
| project: { id: string; slug: string; name: string }; | ||
| workload: { id: string; name: string }; | ||
| publication: Publication | null; | ||
| recommendation: NonNullable<Manifest["report"]>["recommendation"] | null; | ||
| arms: Array<Pick<NonNullable<Manifest["report"]>["arms"][number], "arm_id" | "model" | "display_name" | "role" | "score">>; | ||
| hasPromptDiff: boolean; | ||
| reportAvailable: boolean; | ||
| manifestBytes: number | null; | ||
| benchmark?: Manifest | null; | ||
| } | ||
|
|
||
| export interface BenchmarksService { | ||
| list(): Promise<BenchmarkListResult>; | ||
| show(input: BenchmarkShowInput): Promise<BenchmarkShowResult>; | ||
| } | ||
|
|
||
| export function createBenchmarksService(options: ManagementScopeOptions): BenchmarksService { | ||
| return { | ||
| async list() { | ||
| const session = await resolveManagementSession(options); | ||
| const result = await requestManagementJson(session, { | ||
| path: `/admin/v1/orgs/${encodeURIComponent(session.organizationId)}/benchmarks`, | ||
| action: "Benchmark listing", | ||
| }, indexSchema); | ||
| const seen = new Set<string>(); | ||
| for (const row of result.workloads) { | ||
| const key = `${row.project_id}\0${row.workload_id}`; | ||
| if (seen.has(key) || (row.benchmark && ( | ||
| row.benchmark.org_id !== session.organizationId || | ||
| row.benchmark.project_id !== row.project_id || | ||
| row.benchmark.workload_id !== row.workload_id | ||
| ))) throw new CliError("Understudy returned an invalid benchmark listing."); | ||
| seen.add(key); | ||
| } | ||
| return result; | ||
| }, | ||
|
|
||
| async show(input) { | ||
| const scope = await resolveManagementScope(options, { | ||
| project: input.project, | ||
| workload: input.workload, | ||
| requireWorkload: true, | ||
| ignoreDefaults: input.project !== undefined, | ||
| }); | ||
| const { session, project, workload } = scope; | ||
| const result = await requestManagementJson(session, { | ||
| path: `/admin/v1/orgs/${encodeURIComponent(session.organizationId)}/projects/${encodeURIComponent(project!.id)}/workloads/${encodeURIComponent(workload!.id)}/benchmark`, | ||
| action: "Benchmark read", | ||
| }, detailSchema); | ||
| if ((result.benchmark === null) !== (result.publication === null) || | ||
| (result.benchmark && (result.benchmark.project_id !== project!.id || result.benchmark.workload_id !== workload!.id)) || | ||
| (result.publication && (result.publication.org_id !== session.organizationId || result.publication.project_id !== project!.id || result.publication.workload_id !== workload!.id))) { | ||
| throw new CliError("Understudy returned a benchmark outside the selected workload."); | ||
| } | ||
| const report = result.benchmark?.report; | ||
| return { | ||
| project: { id: project!.id, slug: project!.slug, name: project!.name }, | ||
| workload: { id: workload!.id, name: workload!.name }, | ||
| publication: result.publication, | ||
| recommendation: report?.recommendation ?? null, | ||
| arms: report?.arms.map(({ arm_id, model, display_name, role, score }) => | ||
| ({ arm_id, model, display_name, role, score })) ?? [], | ||
| hasPromptDiff: report?.prompt_change !== undefined, | ||
| reportAvailable: report !== undefined, | ||
| manifestBytes: result.benchmark ? Buffer.byteLength(JSON.stringify(result.benchmark), "utf8") : null, | ||
| ...(input.includeReport ? { benchmark: result.benchmark } : {}), | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { Command } from "commander"; | ||
|
|
||
| import { createCredentialStore } from "../auth/credentials.js"; | ||
| import { createBenchmarksService, type BenchmarksService, type BenchmarkShowResult } from "../benchmarks/service.js"; | ||
| import { formatOutput } from "../output.js"; | ||
|
|
||
| export function addBenchmarksCommands( | ||
| program: Command, | ||
| service: BenchmarksService = createBenchmarksService({ store: createCredentialStore() }), | ||
| output = (message: string) => process.stdout.write(`${message}\n`), | ||
| ): void { | ||
| const benchmarks = program.command("benchmarks") | ||
| .description("Read published workload benchmarks and model recommendations."); | ||
|
|
||
| benchmarks.command("list") | ||
| .description("List workloads and their current benchmark publication, if any.") | ||
| .action(async function (this: Command) { | ||
| const result = await service.list(); | ||
| const published = result.workloads.filter((row) => row.benchmark); | ||
| output(formatOutput(this, result, published.length === 0 | ||
| ? "No published benchmarks in the authenticated organization." | ||
| : [ | ||
| ...published.map((row) => `${row.project_slug}/${row.workload_name} ${row.benchmark!.name} published ${row.benchmark!.published_at}`), | ||
| `${published.length} published benchmark(s) across ${result.workloads.length} workload(s).`, | ||
| ])); | ||
| }); | ||
|
|
||
| benchmarks.command("show") | ||
| .description("Show the current benchmark for a workload. Use --include-report for the full manifest and prompt diff.") | ||
| .option("--project <selector>", "Exact project id, slug, or name; defaults to saved context.") | ||
| .option("--workload <selector>", "Exact workload id or name; defaults to saved context.") | ||
| .option("--include-report", "Include the full benchmark manifest, which may contain customer prompt and eval content.") | ||
| .action(async function (this: Command, options: { project?: string; workload?: string; includeReport?: boolean }) { | ||
| const result = await service.show(options); | ||
| output(formatBenchmark(this, result, options.includeReport === true)); | ||
| }); | ||
| } | ||
|
|
||
| function formatBenchmark(command: Command, result: BenchmarkShowResult, full: boolean): string { | ||
| if (full) { | ||
| return formatOutput(command, result, JSON.stringify(result, null, 2).split("\n")); | ||
| } | ||
| if (!result.publication) { | ||
| return formatOutput(command, result, `No published benchmark for ${result.project.slug}/${result.workload.name}.`); | ||
| } | ||
| const lines = [ | ||
| `Benchmark ${result.publication.name}`, | ||
| `Workload ${result.project.slug}/${result.workload.name}`, | ||
| `Published ${result.publication.published_at}`, | ||
| `Models ${result.publication.model_count}`, | ||
| `Tasks ${result.publication.task_count}`, | ||
| `Size ${result.manifestBytes} bytes (serialized manifest)`, | ||
| ...(result.recommendation ? [ | ||
| `Recommend ${result.recommendation.headline}`, | ||
| `Summary ${result.recommendation.summary}`, | ||
| ...result.arms.map((arm) => `${arm.role.padEnd(11)}${arm.model} (score ${arm.score})`), | ||
| `Prompt diff ${result.hasPromptDiff ? "available in full report" : "not published"}`, | ||
| ] : ["Recommendation report not published for this benchmark."]), | ||
| "Use --include-report --json to give a coding agent the full report and benchmark evidence.", | ||
| ]; | ||
| return formatOutput(command, result, lines); | ||
| } |
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,57 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { Command } from "commander"; | ||
|
|
||
| import { addBenchmarksCommands } from "../dist/commands/benchmarks.js"; | ||
|
|
||
| const report = { | ||
| project: { id: "synthetic-project", slug: "fictional", name: "Fictional" }, | ||
| workload: { id: "synthetic-workload", name: "writer" }, | ||
| publication: { | ||
| name: "Fictional comparison", published_at: "2030-01-01T00:00:00Z", | ||
| model_count: 2, task_count: 3, | ||
| }, | ||
| recommendation: { headline: "Try model A", summary: "Fictional evidence." }, | ||
| arms: [{ arm_id: "a", model: "example/model-a", display_name: "A", role: "recommended", score: 0.8 }], | ||
| hasPromptDiff: true, | ||
| reportAvailable: true, | ||
| manifestBytes: 2048, | ||
| }; | ||
|
|
||
| function setup() { | ||
| const calls = []; | ||
| const output = []; | ||
| const program = new Command().option("--json").exitOverride(); | ||
| program.configureOutput({ writeErr: () => undefined }); | ||
| addBenchmarksCommands(program, { | ||
| list: async () => ({ workloads: [{ project_slug: "fictional", workload_name: "writer", benchmark: report.publication }] }), | ||
| show: async (input) => { | ||
| calls.push(input); | ||
| return input.includeReport ? { ...report, benchmark: { report: { prompt_change: { diff: ["Invented prompt line"] } } } } : report; | ||
| }, | ||
| }, (message) => output.push(message)); | ||
| return { program, calls, output }; | ||
| } | ||
|
|
||
| test("benchmarks show prints a summary and forwards saved-context selection", async () => { | ||
| const { program, calls, output } = setup(); | ||
| await program.parseAsync(["node", "understudy", "benchmarks", "show"]); | ||
| assert.deepEqual(calls, [{}]); | ||
| assert.match(output[0], /Recommend Try model A/); | ||
| assert.match(output[0], /--include-report --json/); | ||
| assert.match(output[0], /2048 bytes/); | ||
| assert.doesNotMatch(output[0], /Invented prompt line/); | ||
| }); | ||
|
|
||
| test("benchmarks show gives an agent the full JSON report only when requested", async () => { | ||
| const { program, calls, output } = setup(); | ||
| await program.parseAsync(["node", "understudy", "--json", "benchmarks", "show", "--project", "fictional", "--workload", "writer", "--include-report"]); | ||
| assert.deepEqual(calls, [{ includeReport: true, project: "fictional", workload: "writer" }]); | ||
| assert.equal(JSON.parse(output[0]).benchmark.report.prompt_change.diff[0], "Invented prompt line"); | ||
| }); | ||
|
|
||
| test("benchmarks list prints available publications", async () => { | ||
| const { program, output } = setup(); | ||
| await program.parseAsync(["node", "understudy", "benchmarks", "list"]); | ||
| assert.match(output[0], /fictional\/writer Fictional comparison/); | ||
| }); |
Oops, something went wrong.
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.